From 0b3e5f5bd42a02c2a15b394b3768e517dc43f39c Mon Sep 17 00:00:00 2001 From: Kujtim Hoxha Date: Mon, 14 Apr 2025 11:24:36 +0200 Subject: handle errors correctly in the other tools --- internal/llm/tools/write.go | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) (limited to 'internal/llm/tools/write.go') diff --git a/internal/llm/tools/write.go b/internal/llm/tools/write.go index 9797239d9..8318f2851 100644 --- a/internal/llm/tools/write.go +++ b/internal/llm/tools/write.go @@ -30,8 +30,9 @@ type writeTool struct { } type WriteResponseMetadata struct { - Additions int `json:"additions"` - Removals int `json:"removals"` + Diff string `json:"diff"` + Additions int `json:"additions"` + Removals int `json:"removals"` } const ( @@ -128,12 +129,12 @@ func (w *writeTool) Run(ctx context.Context, call ToolCall) (ToolResponse, error return NewTextErrorResponse(fmt.Sprintf("File %s already contains the exact content. No changes made.", filePath)), nil } } else if !os.IsNotExist(err) { - return NewTextErrorResponse(fmt.Sprintf("Failed to access file: %s", err)), nil + return ToolResponse{}, fmt.Errorf("error checking file: %w", err) } dir := filepath.Dir(filePath) if err = os.MkdirAll(dir, 0o755); err != nil { - return NewTextErrorResponse(fmt.Sprintf("Failed to create parent directories: %s", err)), nil + return ToolResponse{}, fmt.Errorf("error creating directory: %w", err) } oldContent := "" @@ -146,7 +147,7 @@ func (w *writeTool) Run(ctx context.Context, call ToolCall) (ToolResponse, error sessionID, messageID := GetContextValues(ctx) if sessionID == "" || messageID == "" { - return NewTextErrorResponse("session ID or message ID is missing"), nil + return ToolResponse{}, fmt.Errorf("session_id and message_id are required") } diff, stats, err := git.GenerateGitDiffWithStats( removeWorkingDirectoryPrefix(filePath), @@ -154,7 +155,7 @@ func (w *writeTool) Run(ctx context.Context, call ToolCall) (ToolResponse, error params.Content, ) if err != nil { - return NewTextErrorResponse(fmt.Sprintf("Failed to get file diff: %s", err)), nil + return ToolResponse{}, fmt.Errorf("error generating diff: %w", err) } p := w.permissions.Request( permission.CreatePermissionRequest{ @@ -169,12 +170,12 @@ func (w *writeTool) Run(ctx context.Context, call ToolCall) (ToolResponse, error }, ) if !p { - return NewTextErrorResponse(fmt.Sprintf("Permission denied to create file: %s", filePath)), nil + return ToolResponse{}, permission.ErrorPermissionDenied } err = os.WriteFile(filePath, []byte(params.Content), 0o644) if err != nil { - return NewTextErrorResponse(fmt.Sprintf("Failed to write file: %s", err)), nil + return ToolResponse{}, fmt.Errorf("error writing file: %w", err) } recordFileWrite(filePath) @@ -186,6 +187,7 @@ func (w *writeTool) Run(ctx context.Context, call ToolCall) (ToolResponse, error result += getDiagnostics(filePath, w.lspClients) return WithResponseMetadata(NewTextResponse(result), WriteResponseMetadata{ + Diff: diff, Additions: stats.Additions, Removals: stats.Removals, }, -- cgit v1.2.3