picoclaw/pkg/tools/edit.go

168 lines
4.1 KiB
Go
Raw Normal View History

2026-02-04 11:06:13 +00:00
package tools
import (
"context"
"fmt"
"os"
"strings"
)
// EditFileTool edits a file by replacing old_text with new_text.
// The old_text must exist exactly in the file.
type EditFileTool struct {
allowedDir string
restrict bool
}
2026-02-04 11:06:13 +00:00
// NewEditFileTool creates a new EditFileTool with optional directory restriction.
func NewEditFileTool(allowedDir string, restrict bool) *EditFileTool {
return &EditFileTool{
allowedDir: allowedDir,
restrict: restrict,
}
2026-02-04 11:06:13 +00:00
}
func (t *EditFileTool) Name() string {
return "edit_file"
}
func (t *EditFileTool) Description() string {
return "Edit a file by replacing old_text with new_text. The old_text must exist exactly in the file."
}
2026-02-18 19:48:23 +00:00
func (t *EditFileTool) Parameters() map[string]any {
return map[string]any{
2026-02-04 11:06:13 +00:00
"type": "object",
2026-02-18 19:48:23 +00:00
"properties": map[string]any{
"path": map[string]any{
2026-02-04 11:06:13 +00:00
"type": "string",
"description": "The file path to edit",
},
2026-02-18 19:48:23 +00:00
"old_text": map[string]any{
2026-02-04 11:06:13 +00:00
"type": "string",
"description": "The exact text to find and replace",
},
2026-02-18 19:48:23 +00:00
"new_text": map[string]any{
2026-02-04 11:06:13 +00:00
"type": "string",
"description": "The text to replace with",
},
},
"required": []string{"path", "old_text", "new_text"},
}
}
2026-02-18 19:48:23 +00:00
func (t *EditFileTool) Execute(ctx context.Context, args map[string]any) *ToolResult {
2026-02-04 11:06:13 +00:00
path, ok := args["path"].(string)
if !ok {
return ErrorResult("path is required")
2026-02-04 11:06:13 +00:00
}
oldText, ok := args["old_text"].(string)
if !ok {
return ErrorResult("old_text is required")
2026-02-04 11:06:13 +00:00
}
newText, ok := args["new_text"].(string)
if !ok {
return ErrorResult("new_text is required")
2026-02-04 11:06:13 +00:00
}
resolvedPath, err := validatePath(path, t.allowedDir, t.restrict)
if err != nil {
return ErrorResult(err.Error())
}
2026-02-04 11:06:13 +00:00
2026-02-20 20:35:16 +00:00
if _, err = os.Stat(resolvedPath); os.IsNotExist(err) {
return ErrorResult(fmt.Sprintf("file not found: %s", path))
2026-02-04 11:06:13 +00:00
}
content, err := os.ReadFile(resolvedPath)
2026-02-04 11:06:13 +00:00
if err != nil {
return ErrorResult(fmt.Sprintf("failed to read file: %v", err))
2026-02-04 11:06:13 +00:00
}
contentStr := string(content)
if !strings.Contains(contentStr, oldText) {
return ErrorResult("old_text not found in file. Make sure it matches exactly")
2026-02-04 11:06:13 +00:00
}
count := strings.Count(contentStr, oldText)
if count > 1 {
2026-02-18 19:48:23 +00:00
return ErrorResult(
fmt.Sprintf("old_text appears %d times. Please provide more context to make it unique", count),
)
2026-02-04 11:06:13 +00:00
}
newContent := strings.Replace(contentStr, oldText, newText, 1)
2026-02-18 19:48:23 +00:00
if err := os.WriteFile(resolvedPath, []byte(newContent), 0o644); err != nil {
return ErrorResult(fmt.Sprintf("failed to write file: %v", err))
2026-02-04 11:06:13 +00:00
}
return SilentResult(fmt.Sprintf("File edited: %s", path))
2026-02-04 11:06:13 +00:00
}
type AppendFileTool struct {
workspace string
restrict bool
}
2026-02-04 11:06:13 +00:00
func NewAppendFileTool(workspace string, restrict bool) *AppendFileTool {
return &AppendFileTool{workspace: workspace, restrict: restrict}
2026-02-04 11:06:13 +00:00
}
func (t *AppendFileTool) Name() string {
return "append_file"
}
func (t *AppendFileTool) Description() string {
return "Append content to the end of a file"
}
2026-02-18 19:48:23 +00:00
func (t *AppendFileTool) Parameters() map[string]any {
return map[string]any{
2026-02-04 11:06:13 +00:00
"type": "object",
2026-02-18 19:48:23 +00:00
"properties": map[string]any{
"path": map[string]any{
2026-02-04 11:06:13 +00:00
"type": "string",
"description": "The file path to append to",
},
2026-02-18 19:48:23 +00:00
"content": map[string]any{
2026-02-04 11:06:13 +00:00
"type": "string",
"description": "The content to append",
},
},
"required": []string{"path", "content"},
}
}
2026-02-18 19:48:23 +00:00
func (t *AppendFileTool) Execute(ctx context.Context, args map[string]any) *ToolResult {
2026-02-04 11:06:13 +00:00
path, ok := args["path"].(string)
if !ok {
return ErrorResult("path is required")
2026-02-04 11:06:13 +00:00
}
content, ok := args["content"].(string)
if !ok {
return ErrorResult("content is required")
2026-02-04 11:06:13 +00:00
}
resolvedPath, err := validatePath(path, t.workspace, t.restrict)
if err != nil {
return ErrorResult(err.Error())
}
2026-02-04 11:06:13 +00:00
2026-02-18 19:48:23 +00:00
f, err := os.OpenFile(resolvedPath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0o644)
2026-02-04 11:06:13 +00:00
if err != nil {
return ErrorResult(fmt.Sprintf("failed to open file: %v", err))
2026-02-04 11:06:13 +00:00
}
defer f.Close()
if _, err := f.WriteString(content); err != nil {
return ErrorResult(fmt.Sprintf("failed to append to file: %v", err))
2026-02-04 11:06:13 +00:00
}
return SilentResult(fmt.Sprintf("Appended to %s", path))
2026-02-04 11:06:13 +00:00
}