| 1 | package builtin |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "encoding/json" |
| 6 | "fmt" |
| 7 | |
| 8 | "reasonix/internal/tool" |
| 9 | ) |
| 10 | |
| 11 | func init() { tool.RegisterBuiltin(editFile{}) } |
| 12 | |
| 13 | // editFile replaces an exact string in a file. roots confines the target to the |
| 14 | // workspace when non-empty (see writeFile); guard rejects Reasonix session-data |
| 15 | // targets (see SessionDataGuard); workDir, when non-empty, is the directory a |
| 16 | // relative path resolves against (see resolveIn). |
| 17 | type editFile struct { |
| 18 | roots []string |
| 19 | guard SessionDataGuard |
| 20 | managed ManagedConfigPaths |
| 21 | workDir string |
| 22 | } |
| 23 | |
| 24 | func (editFile) Name() string { return "edit_file" } |
| 25 | |
| 26 | func (editFile) Description() string { |
| 27 | return "Replace an exact string in a file with another. old_string must occur exactly once; add surrounding context to disambiguate. Use for targeted edits instead of rewriting the whole file." |
| 28 | } |
| 29 | |
| 30 | func (editFile) Schema() json.RawMessage { |
| 31 | return json.RawMessage(`{"type":"object","properties":{"path":{"type":"string","description":"File path"},"old_string":{"type":"string","description":"Exact text to replace (must be unique in the file)"},"new_string":{"type":"string","description":"Replacement text (may be empty to delete)"}},"required":["path","old_string","new_string"]}`) |
| 32 | } |
| 33 | |
| 34 | func (editFile) ReadOnly() bool { return false } |
| 35 | |
| 36 | func (e editFile) Execute(ctx context.Context, args json.RawMessage) (string, error) { |
| 37 | var p struct { |
| 38 | Path string `json:"path"` |
| 39 | OldString string `json:"old_string"` |
| 40 | NewString string `json:"new_string"` |
| 41 | } |
| 42 | if err := json.Unmarshal(args, &p); err != nil { |
| 43 | return "", fmt.Errorf("invalid args: %w", err) |
| 44 | } |
| 45 | if p.Path == "" { |
| 46 | return "", fmt.Errorf("path is required") |
| 47 | } |
| 48 | if p.OldString == "" { |
| 49 | return "", fmt.Errorf("old_string is required") |
| 50 | } |
| 51 | p.Path = resolveIn(e.workDir, p.Path) |
| 52 | if err := confineWrite(ctx, e.roots, e.guard, e.managed, p.Path); err != nil { |
| 53 | return "", err |
| 54 | } |
| 55 | |
| 56 | content, enc, err := readFileEncoded(p.Path) |
| 57 | if err != nil { |
| 58 | return "", fmt.Errorf("read %s: %w", p.Path, err) |
| 59 | } |
| 60 | |
| 61 | applied := applyOldStringEdit(content, p.OldString, p.NewString, false) |
| 62 | switch { |
| 63 | case applied.applied == 1: |
| 64 | // ok |
| 65 | case applied.matches == 0: |
| 66 | return "", oldStringNotFoundError(p.Path, p.OldString, content) |
| 67 | default: |
| 68 | return "", oldStringNotUniqueError(p.Path, p.OldString, content, applied.matches, false) |
| 69 | } |
| 70 | |
| 71 | if err := writeFileEncoded(p.Path, applied.updated, enc); err != nil { |
| 72 | return "", fmt.Errorf("write %s: %w", p.Path, err) |
| 73 | } |
| 74 | summary := fmt.Sprintf("edited %s", p.Path) |
| 75 | if applied.fuzzy { |
| 76 | summary += " (fuzzy match)" |
| 77 | } |
| 78 | return withActualPostWriteReceipts(summary, []editReplacementReceipt{applied.receipt}), nil |
| 79 | } |
| 80 |