From bd2ec29b65e430f83f430db5fdc424c7d631989d Mon Sep 17 00:00:00 2001 From: Kujtim Hoxha Date: Sat, 12 Apr 2025 18:45:36 +0200 Subject: add initial git support --- internal/llm/tools/write.go | 29 ++++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) (limited to 'internal/llm/tools/write.go') 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("\n%s\n", result) result += appendDiagnostics(filePath, w.lspClients) - return NewTextResponse(result), nil + return WithResponseMetadata(NewTextResponse(result), + WriteResponseMetadata{ + Additions: stats.Additions, + Removals: stats.Removals, + }, + ), nil } -- cgit v1.2.3