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