summaryrefslogtreecommitdiffhomepage
path: root/internal/llm/tools
diff options
context:
space:
mode:
Diffstat (limited to 'internal/llm/tools')
-rw-r--r--internal/llm/tools/diagnostics.go5
-rw-r--r--internal/llm/tools/edit.go33
-rw-r--r--internal/llm/tools/write.go11
3 files changed, 41 insertions, 8 deletions
diff --git a/internal/llm/tools/diagnostics.go b/internal/llm/tools/diagnostics.go
index 97ac149b6..3eb72eafc 100644
--- a/internal/llm/tools/diagnostics.go
+++ b/internal/llm/tools/diagnostics.go
@@ -72,6 +72,7 @@ func notifyLspOpenFile(ctx context.Context, filePath string, lsps map[string]*ls
// Create a notification handler that will signal when diagnostics are received
handler := func(params json.RawMessage) {
+ lsp.HandleDiagnostics(client, params)
var diagParams protocol.PublishDiagnosticsParams
if err := json.Unmarshal(params, &diagParams); err != nil {
return
@@ -103,8 +104,8 @@ func notifyLspOpenFile(ctx context.Context, filePath string, lsps map[string]*ls
select {
case <-diagChan:
// Diagnostics received
- case <-time.After(5 * time.Second):
- // Timeout after 2 seconds - this is a fallback in case no diagnostics are published
+ case <-time.After(10 * time.Second):
+ // Timeout after 5 seconds - this is a fallback in case no diagnostics are published
case <-ctx.Done():
// Context cancelled
}
diff --git a/internal/llm/tools/edit.go b/internal/llm/tools/edit.go
index f12a1eb21..394ce13b9 100644
--- a/internal/llm/tools/edit.go
+++ b/internal/llm/tools/edit.go
@@ -303,23 +303,46 @@ func GenerateDiff(oldContent, newContent string) string {
diffs = dmp.DiffCharsToLines(diffs, dmpStrings)
diffs = dmp.DiffCleanupSemantic(diffs)
buff := strings.Builder{}
+
+ // Add a header to make the diff more readable
+ buff.WriteString("Changes:\n")
+
for _, diff := range diffs {
text := diff.Text
switch diff.Type {
case diffmatchpatch.DiffInsert:
- for line := range strings.SplitSeq(text, "\n") {
+ for _, line := range strings.Split(text, "\n") {
+ if line == "" {
+ continue
+ }
_, _ = buff.WriteString("+ " + line + "\n")
}
case diffmatchpatch.DiffDelete:
- for line := range strings.SplitSeq(text, "\n") {
+ for _, line := range strings.Split(text, "\n") {
+ if line == "" {
+ continue
+ }
_, _ = buff.WriteString("- " + line + "\n")
}
case diffmatchpatch.DiffEqual:
- if len(text) > 40 {
- _, _ = buff.WriteString(" " + text[:20] + "..." + text[len(text)-20:] + "\n")
+ // Only show a small context for unchanged text
+ lines := strings.Split(text, "\n")
+ if len(lines) > 3 {
+ // Show only first and last line of context with a separator
+ if lines[0] != "" {
+ _, _ = buff.WriteString(" " + lines[0] + "\n")
+ }
+ _, _ = buff.WriteString(" ...\n")
+ if lines[len(lines)-1] != "" {
+ _, _ = buff.WriteString(" " + lines[len(lines)-1] + "\n")
+ }
} else {
- for line := range strings.SplitSeq(text, "\n") {
+ // Show all lines for small contexts
+ for _, line := range lines {
+ if line == "" {
+ continue
+ }
_, _ = buff.WriteString(" " + line + "\n")
}
}
diff --git a/internal/llm/tools/write.go b/internal/llm/tools/write.go
index 3d66d64e2..1c32410f7 100644
--- a/internal/llm/tools/write.go
+++ b/internal/llm/tools/write.go
@@ -101,6 +101,15 @@ func (w *writeTool) Run(ctx context.Context, call ToolCall) (ToolResponse, error
}
notifyLspOpenFile(ctx, filePath, w.lspClients)
+ // Get old content for diff if file exists
+ oldContent := ""
+ if fileInfo != nil && !fileInfo.IsDir() {
+ oldBytes, readErr := os.ReadFile(filePath)
+ if readErr == nil {
+ oldContent = string(oldBytes)
+ }
+ }
+
p := permission.Default.Request(
permission.CreatePermissionRequest{
Path: filePath,
@@ -109,7 +118,7 @@ func (w *writeTool) Run(ctx context.Context, call ToolCall) (ToolResponse, error
Description: fmt.Sprintf("Create file %s", filePath),
Params: WritePermissionsParams{
FilePath: filePath,
- Content: GenerateDiff("", params.Content),
+ Content: GenerateDiff(oldContent, params.Content),
},
},
)