lsp_tool.go 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. package tool
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. "github.com/opencode-ai/opencode/internal/config"
  7. "github.com/opencode-ai/opencode/internal/logging"
  8. "github.com/opencode-ai/opencode/internal/lsp/discovery"
  9. )
  10. // ConfigureLspServerRequest represents the request for the configureLspServer tool
  11. type ConfigureLspServerRequest struct {
  12. // Language identifier (e.g., "go", "typescript", "python")
  13. Language string `json:"language"`
  14. }
  15. // ConfigureLspServerResponse represents the response from the configureLspServer tool
  16. type ConfigureLspServerResponse struct {
  17. // Whether the server was found
  18. Found bool `json:"found"`
  19. // Path to the server executable
  20. Path string `json:"path,omitempty"`
  21. // Command to run the server
  22. Command string `json:"command,omitempty"`
  23. // Arguments to pass to the command
  24. Args []string `json:"args,omitempty"`
  25. // Installation instructions if the server was not found
  26. InstallInstructions string `json:"installInstructions,omitempty"`
  27. // Whether the server was added to the configuration
  28. Added bool `json:"added,omitempty"`
  29. }
  30. // ConfigureLspServer searches for an LSP server for the given language
  31. func ConfigureLspServer(ctx context.Context, rawArgs json.RawMessage) (any, error) {
  32. var args ConfigureLspServerRequest
  33. if err := json.Unmarshal(rawArgs, &args); err != nil {
  34. return nil, fmt.Errorf("failed to parse arguments: %w", err)
  35. }
  36. if args.Language == "" {
  37. return nil, fmt.Errorf("language parameter is required")
  38. }
  39. // Find the LSP server for the language
  40. serverInfo, err := discovery.FindLSPServer(args.Language)
  41. if err != nil {
  42. // Server not found, return instructions
  43. return ConfigureLspServerResponse{
  44. Found: false,
  45. Command: serverInfo.Command,
  46. Args: serverInfo.Args,
  47. InstallInstructions: serverInfo.InstallCmd,
  48. Added: false,
  49. }, nil
  50. }
  51. // Server found, update the configuration if available
  52. added := false
  53. if serverInfo.Available {
  54. // Get the current configuration
  55. cfg := config.Get()
  56. if cfg != nil {
  57. // Add the server to the configuration
  58. cfg.LSP[args.Language] = config.LSPConfig{
  59. Disabled: false,
  60. Command: serverInfo.Path,
  61. Args: serverInfo.Args,
  62. }
  63. added = true
  64. logging.Info("Added LSP server to configuration",
  65. "language", args.Language,
  66. "command", serverInfo.Command,
  67. "path", serverInfo.Path)
  68. }
  69. }
  70. // Return the server information
  71. return ConfigureLspServerResponse{
  72. Found: true,
  73. Path: serverInfo.Path,
  74. Command: serverInfo.Command,
  75. Args: serverInfo.Args,
  76. Added: added,
  77. }, nil
  78. }