add initial git support

This commit is contained in:
Kujtim Hoxha
2025-04-12 18:45:36 +02:00
parent 0697dcc1d9
commit bd2ec29b65
19 changed files with 791 additions and 176 deletions

View File

@@ -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
}