diff options
| author | Kujtim Hoxha <[email protected]> | 2025-04-14 11:24:36 +0200 |
|---|---|---|
| committer | Kujtim Hoxha <[email protected]> | 2025-04-21 13:41:27 +0200 |
| commit | 0b3e5f5bd42a02c2a15b394b3768e517dc43f39c (patch) | |
| tree | 578f95913a12eebda908384f45e1d0e27977ba20 /internal/llm/tools/write.go | |
| parent | 921f5ee5bd74837ff4566fc2d1e45051c87d9c38 (diff) | |
| download | opencode-0b3e5f5bd42a02c2a15b394b3768e517dc43f39c.tar.gz opencode-0b3e5f5bd42a02c2a15b394b3768e517dc43f39c.zip | |
handle errors correctly in the other tools
Diffstat (limited to 'internal/llm/tools/write.go')
| -rw-r--r-- | internal/llm/tools/write.go | 18 |
1 files changed, 10 insertions, 8 deletions
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, }, |
