summaryrefslogtreecommitdiffhomepage
path: root/internal/llm/tools/write.go
diff options
context:
space:
mode:
authorKujtim Hoxha <[email protected]>2025-04-12 18:45:36 +0200
committerKujtim Hoxha <[email protected]>2025-04-21 13:38:42 +0200
commitbd2ec29b65e430f83f430db5fdc424c7d631989d (patch)
tree0d7ee1a29a7932d54ffa1f247303568d85a3cf11 /internal/llm/tools/write.go
parent0697dcc1d9c7330d8c9d8a2be0bb94b3d46c9345 (diff)
downloadopencode-bd2ec29b65e430f83f430db5fdc424c7d631989d.tar.gz
opencode-bd2ec29b65e430f83f430db5fdc424c7d631989d.zip
add initial git support
Diffstat (limited to 'internal/llm/tools/write.go')
-rw-r--r--internal/llm/tools/write.go29
1 files changed, 26 insertions, 3 deletions
diff --git a/internal/llm/tools/write.go b/internal/llm/tools/write.go
index 7b698d2d8..27c98bb9d 100644
--- a/internal/llm/tools/write.go
+++ b/internal/llm/tools/write.go
@@ -9,6 +9,7 @@ import (
"time"
"github.com/kujtimiihoxha/termai/internal/config"
+ "github.com/kujtimiihoxha/termai/internal/git"
"github.com/kujtimiihoxha/termai/internal/lsp"
"github.com/kujtimiihoxha/termai/internal/permission"
)
@@ -20,7 +21,7 @@ type WriteParams struct {
type WritePermissionsParams struct {
FilePath string `json:"file_path"`
- Content string `json:"content"`
+ Diff string `json:"diff"`
}
type writeTool struct {
@@ -28,6 +29,11 @@ type writeTool struct {
permissions permission.Service
}
+type WriteResponseMetadata struct {
+ Additions int `json:"additions"`
+ Removals int `json:"removals"`
+}
+
const (
WriteToolName = "write"
writeDescription = `File writing tool that creates or updates files in the filesystem, allowing you to save or modify text content.
@@ -138,6 +144,18 @@ 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
+ }
+ diff, stats, err := git.GenerateGitDiffWithStats(
+ removeWorkingDirectoryPrefix(filePath),
+ oldContent,
+ params.Content,
+ )
+ if err != nil {
+ return NewTextErrorResponse(fmt.Sprintf("Failed to get file diff: %s", err)), nil
+ }
p := w.permissions.Request(
permission.CreatePermissionRequest{
Path: filePath,
@@ -146,7 +164,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(oldContent, params.Content),
+ Diff: diff,
},
},
)
@@ -166,5 +184,10 @@ func (w *writeTool) Run(ctx context.Context, call ToolCall) (ToolResponse, error
result := fmt.Sprintf("File successfully written: %s", filePath)
result = fmt.Sprintf("<result>\n%s\n</result>", result)
result += appendDiagnostics(filePath, w.lspClients)
- return NewTextResponse(result), nil
+ return WithResponseMetadata(NewTextResponse(result),
+ WriteResponseMetadata{
+ Additions: stats.Additions,
+ Removals: stats.Removals,
+ },
+ ), nil
}