diagnostics.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. package tools
  2. import (
  3. "context"
  4. _ "embed"
  5. "fmt"
  6. "log/slog"
  7. "sort"
  8. "strings"
  9. "sync"
  10. "time"
  11. "charm.land/fantasy"
  12. "github.com/charmbracelet/crush/internal/lsp"
  13. "github.com/charmbracelet/x/powernap/pkg/lsp/protocol"
  14. )
  15. type DiagnosticsParams struct {
  16. FilePath string `json:"file_path,omitempty" description:"The path to the file to get diagnostics for (leave empty for project diagnostics)"`
  17. }
  18. const DiagnosticsToolName = "lsp_diagnostics"
  19. //go:embed diagnostics.md
  20. var diagnosticsDescription []byte
  21. func NewDiagnosticsTool(lspManager *lsp.Manager) fantasy.AgentTool {
  22. return fantasy.NewAgentTool(
  23. DiagnosticsToolName,
  24. string(diagnosticsDescription),
  25. func(ctx context.Context, params DiagnosticsParams, call fantasy.ToolCall) (fantasy.ToolResponse, error) {
  26. if lspManager.Clients().Len() == 0 {
  27. return fantasy.NewTextErrorResponse("no LSP clients available"), nil
  28. }
  29. notifyLSPs(ctx, lspManager, params.FilePath)
  30. output := getDiagnostics(params.FilePath, lspManager)
  31. return fantasy.NewTextResponse(output), nil
  32. })
  33. }
  34. // openInLSPs ensures LSP servers are running and aware of the file, but does
  35. // not notify changes or wait for fresh diagnostics. Use this for read-only
  36. // operations like view where the file content hasn't changed.
  37. func openInLSPs(
  38. ctx context.Context,
  39. manager *lsp.Manager,
  40. filepath string,
  41. ) {
  42. if filepath == "" || manager == nil {
  43. return
  44. }
  45. manager.Start(ctx, filepath)
  46. for client := range manager.Clients().Seq() {
  47. if !client.HandlesFile(filepath) {
  48. continue
  49. }
  50. _ = client.OpenFileOnDemand(ctx, filepath)
  51. }
  52. }
  53. // waitForLSPDiagnostics waits briefly for diagnostics publication after a file
  54. // has been opened. Intended for read-only situations where viewing up-to-date
  55. // files matters but latency should remain low (i.e. when using the view tool).
  56. func waitForLSPDiagnostics(
  57. ctx context.Context,
  58. manager *lsp.Manager,
  59. filepath string,
  60. timeout time.Duration,
  61. ) {
  62. if filepath == "" || manager == nil || timeout <= 0 {
  63. return
  64. }
  65. var wg sync.WaitGroup
  66. for client := range manager.Clients().Seq() {
  67. if !client.HandlesFile(filepath) {
  68. continue
  69. }
  70. wg.Go(func() {
  71. client.WaitForDiagnostics(ctx, timeout)
  72. })
  73. }
  74. wg.Wait()
  75. }
  76. // notifyLSPs notifies LSP servers that a file has changed and waits for
  77. // updated diagnostics. Use this after edit/multiedit operations.
  78. func notifyLSPs(
  79. ctx context.Context,
  80. manager *lsp.Manager,
  81. filepath string,
  82. ) {
  83. if filepath == "" || manager == nil {
  84. return
  85. }
  86. manager.Start(ctx, filepath)
  87. for client := range manager.Clients().Seq() {
  88. if !client.HandlesFile(filepath) {
  89. continue
  90. }
  91. _ = client.OpenFileOnDemand(ctx, filepath)
  92. _ = client.NotifyChange(ctx, filepath)
  93. client.WaitForDiagnostics(ctx, 5*time.Second)
  94. }
  95. }
  96. func getDiagnostics(filePath string, manager *lsp.Manager) string {
  97. if manager == nil {
  98. return ""
  99. }
  100. var fileDiagnostics []string
  101. var projectDiagnostics []string
  102. for lspName, client := range manager.Clients().Seq2() {
  103. for location, diags := range client.GetDiagnostics() {
  104. path, err := location.Path()
  105. if err != nil {
  106. slog.Error("Failed to convert diagnostic location URI to path", "uri", location, "error", err)
  107. continue
  108. }
  109. isCurrentFile := path == filePath
  110. for _, diag := range diags {
  111. formattedDiag := formatDiagnostic(path, diag, lspName)
  112. if isCurrentFile {
  113. fileDiagnostics = append(fileDiagnostics, formattedDiag)
  114. } else {
  115. projectDiagnostics = append(projectDiagnostics, formattedDiag)
  116. }
  117. }
  118. }
  119. }
  120. sortDiagnostics(fileDiagnostics)
  121. sortDiagnostics(projectDiagnostics)
  122. var output strings.Builder
  123. writeDiagnostics(&output, "file_diagnostics", fileDiagnostics)
  124. writeDiagnostics(&output, "project_diagnostics", projectDiagnostics)
  125. if len(fileDiagnostics) > 0 || len(projectDiagnostics) > 0 {
  126. fileErrors := countSeverity(fileDiagnostics, "Error")
  127. fileWarnings := countSeverity(fileDiagnostics, "Warn")
  128. projectErrors := countSeverity(projectDiagnostics, "Error")
  129. projectWarnings := countSeverity(projectDiagnostics, "Warn")
  130. output.WriteString("\n<diagnostic_summary>\n")
  131. fmt.Fprintf(&output, "Current file: %d errors, %d warnings\n", fileErrors, fileWarnings)
  132. fmt.Fprintf(&output, "Project: %d errors, %d warnings\n", projectErrors, projectWarnings)
  133. output.WriteString("</diagnostic_summary>\n")
  134. }
  135. out := output.String()
  136. slog.Debug("Diagnostics", "output", out)
  137. return out
  138. }
  139. func writeDiagnostics(output *strings.Builder, tag string, in []string) {
  140. if len(in) == 0 {
  141. return
  142. }
  143. output.WriteString("\n<" + tag + ">\n")
  144. if len(in) > 10 {
  145. output.WriteString(strings.Join(in[:10], "\n"))
  146. fmt.Fprintf(output, "\n... and %d more diagnostics", len(in)-10)
  147. } else {
  148. output.WriteString(strings.Join(in, "\n"))
  149. }
  150. output.WriteString("\n</" + tag + ">\n")
  151. }
  152. func sortDiagnostics(in []string) []string {
  153. sort.Slice(in, func(i, j int) bool {
  154. iIsError := strings.HasPrefix(in[i], "Error")
  155. jIsError := strings.HasPrefix(in[j], "Error")
  156. if iIsError != jIsError {
  157. return iIsError // Errors come first
  158. }
  159. return in[i] < in[j] // Then alphabetically
  160. })
  161. return in
  162. }
  163. func formatDiagnostic(pth string, diagnostic protocol.Diagnostic, source string) string {
  164. severity := "Info"
  165. switch diagnostic.Severity {
  166. case protocol.SeverityError:
  167. severity = "Error"
  168. case protocol.SeverityWarning:
  169. severity = "Warn"
  170. case protocol.SeverityHint:
  171. severity = "Hint"
  172. }
  173. location := fmt.Sprintf("%s:%d:%d", pth, diagnostic.Range.Start.Line+1, diagnostic.Range.Start.Character+1)
  174. sourceInfo := source
  175. if diagnostic.Source != "" {
  176. sourceInfo += " " + diagnostic.Source
  177. }
  178. codeInfo := ""
  179. if diagnostic.Code != nil {
  180. codeInfo = fmt.Sprintf("[%v]", diagnostic.Code)
  181. }
  182. tagsInfo := ""
  183. if len(diagnostic.Tags) > 0 {
  184. var tags []string
  185. for _, tag := range diagnostic.Tags {
  186. switch tag {
  187. case protocol.Unnecessary:
  188. tags = append(tags, "unnecessary")
  189. case protocol.Deprecated:
  190. tags = append(tags, "deprecated")
  191. }
  192. }
  193. if len(tags) > 0 {
  194. tagsInfo = fmt.Sprintf(" (%s)", strings.Join(tags, ", "))
  195. }
  196. }
  197. return fmt.Sprintf("%s: %s [%s]%s%s %s",
  198. severity,
  199. location,
  200. sourceInfo,
  201. codeInfo,
  202. tagsInfo,
  203. diagnostic.Message)
  204. }
  205. func countSeverity(diagnostics []string, severity string) int {
  206. count := 0
  207. for _, diag := range diagnostics {
  208. if strings.HasPrefix(diag, severity) {
  209. count++
  210. }
  211. }
  212. return count
  213. }