diagnostics.go

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