|
| 1 | +import { readFile, writeFile } from "fs/promises"; |
| 2 | +import { resolve } from "path"; |
| 3 | +import { homedir } from "os"; |
| 4 | +import type { AgentTool } from "@mariozechner/pi-agent-core"; |
| 5 | +import { editSchema } from "./shared.js"; |
| 6 | + |
| 7 | +function resolvePath(path: string, cwd: string): string { |
| 8 | + if (path.startsWith("~/") || path === "~") { |
| 9 | + path = homedir() + path.slice(1); |
| 10 | + } |
| 11 | + return path.startsWith("/") ? path : resolve(cwd, path); |
| 12 | +} |
| 13 | + |
| 14 | +function countOccurrences(haystack: string, needle: string): number { |
| 15 | + if (!needle) return 0; |
| 16 | + let count = 0; |
| 17 | + let idx = 0; |
| 18 | + while ((idx = haystack.indexOf(needle, idx)) !== -1) { |
| 19 | + count++; |
| 20 | + idx += needle.length; |
| 21 | + } |
| 22 | + return count; |
| 23 | +} |
| 24 | + |
| 25 | +export function createEditTool(cwd: string): AgentTool<typeof editSchema> { |
| 26 | + return { |
| 27 | + name: "edit", |
| 28 | + label: "edit", |
| 29 | + description: |
| 30 | + "Edit a file by replacing text. By default, performs an exact string replacement that must match uniquely. Set replace_all=true to replace every occurrence. Set regex=true to treat old_string as a JS regular expression (new_string may use $1-style backreferences).", |
| 31 | + parameters: editSchema, |
| 32 | + execute: async ( |
| 33 | + _toolCallId: string, |
| 34 | + { |
| 35 | + path, |
| 36 | + old_string, |
| 37 | + new_string, |
| 38 | + replace_all, |
| 39 | + regex, |
| 40 | + flags, |
| 41 | + }: { path: string; old_string: string; new_string: string; replace_all?: boolean; regex?: boolean; flags?: string }, |
| 42 | + signal?: AbortSignal, |
| 43 | + ) => { |
| 44 | + if (signal?.aborted) throw new Error("Operation aborted"); |
| 45 | + |
| 46 | + const absolutePath = resolvePath(path, cwd); |
| 47 | + const original = await readFile(absolutePath, "utf-8"); |
| 48 | + |
| 49 | + if (old_string === new_string) { |
| 50 | + throw new Error("old_string and new_string are identical — nothing to change"); |
| 51 | + } |
| 52 | + |
| 53 | + let updated: string; |
| 54 | + let replacements = 0; |
| 55 | + |
| 56 | + if (regex) { |
| 57 | + let rxFlags = flags || ""; |
| 58 | + if (replace_all && !rxFlags.includes("g")) rxFlags += "g"; |
| 59 | + let rx: RegExp; |
| 60 | + try { |
| 61 | + rx = new RegExp(old_string, rxFlags); |
| 62 | + } catch (err: any) { |
| 63 | + throw new Error(`Invalid regex: ${err.message}`); |
| 64 | + } |
| 65 | + const matches = original.match(new RegExp(old_string, rxFlags.includes("g") ? rxFlags : rxFlags + "g")); |
| 66 | + replacements = matches ? matches.length : 0; |
| 67 | + if (replacements === 0) { |
| 68 | + throw new Error(`Regex pattern not found in ${path}`); |
| 69 | + } |
| 70 | + if (!replace_all && replacements > 1) { |
| 71 | + throw new Error( |
| 72 | + `Regex matched ${replacements} times in ${path}. Make the pattern more specific or set replace_all=true.`, |
| 73 | + ); |
| 74 | + } |
| 75 | + updated = original.replace(rx, new_string); |
| 76 | + } else { |
| 77 | + if (!old_string) { |
| 78 | + throw new Error("old_string cannot be empty"); |
| 79 | + } |
| 80 | + replacements = countOccurrences(original, old_string); |
| 81 | + if (replacements === 0) { |
| 82 | + throw new Error(`old_string not found in ${path}`); |
| 83 | + } |
| 84 | + if (!replace_all && replacements > 1) { |
| 85 | + throw new Error( |
| 86 | + `old_string matches ${replacements} times in ${path}. Provide more surrounding context to make it unique, or set replace_all=true.`, |
| 87 | + ); |
| 88 | + } |
| 89 | + if (replace_all) { |
| 90 | + updated = original.split(old_string).join(new_string); |
| 91 | + } else { |
| 92 | + updated = original.replace(old_string, new_string); |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + if (updated === original) { |
| 97 | + throw new Error("No changes applied — replacement produced identical content"); |
| 98 | + } |
| 99 | + |
| 100 | + await writeFile(absolutePath, updated, "utf-8"); |
| 101 | + |
| 102 | + const applied = replace_all ? replacements : 1; |
| 103 | + return { |
| 104 | + content: [{ type: "text", text: `Edited ${path} — ${applied} replacement${applied === 1 ? "" : "s"} applied` }], |
| 105 | + details: undefined, |
| 106 | + }; |
| 107 | + }, |
| 108 | + }; |
| 109 | +} |
0 commit comments