| 33 | replaceAll: z.boolean().optional().describe("Replace all occurrences of oldString (default false)"), |
| 34 | }), |
| 35 | async execute(params, ctx) { |
| 36 | if (!params.filePath) { |
| 37 | throw new Error("filePath is required") |
| 38 | } |
| 39 | |
| 40 | if (params.oldString === params.newString) { |
| 41 | throw new Error("oldString and newString must be different") |
| 42 | } |
| 43 | |
| 44 | const agent = await Agent.get(ctx.agent) |
| 45 | |
| 46 | const filePath = path.isAbsolute(params.filePath) ? params.filePath : path.join(Instance.directory, params.filePath) |
| 47 | if (!Filesystem.contains(Instance.directory, filePath)) { |
| 48 | const parentDir = path.dirname(filePath) |
| 49 | if (!(await Permission.isBypassEnabled())) { |
| 50 | if (agent.permission.external_directory === "ask") { |
| 51 | await Permission.ask({ |
| 52 | type: "external_directory", |
| 53 | pattern: [parentDir, path.join(parentDir, "*")], |
| 54 | sessionID: ctx.sessionID, |
| 55 | messageID: ctx.messageID, |
| 56 | callID: ctx.callID, |
| 57 | title: `Edit file outside working directory: ${filePath}`, |
| 58 | metadata: { |
| 59 | filepath: filePath, |
| 60 | parentDir, |
| 61 | }, |
| 62 | }) |
| 63 | } else if (agent.permission.external_directory === "deny") { |
| 64 | throw new Permission.RejectedError( |
| 65 | ctx.sessionID, |
| 66 | "external_directory", |
| 67 | ctx.callID, |
| 68 | { |
| 69 | filepath: filePath, |
| 70 | parentDir, |
| 71 | }, |
| 72 | `File ${filePath} is not in the current working directory`, |
| 73 | ) |
| 74 | } |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | let diff = "" |
| 79 | let contentOld = "" |
| 80 | let contentNew = "" |
| 81 | await (async () => { |
| 82 | if (params.oldString === "") { |
| 83 | contentNew = params.newString |
| 84 | diff = trimDiff(createTwoFilesPatch(filePath, filePath, contentOld, contentNew)) |
| 85 | if (agent.permission.edit === "ask") { |
| 86 | await Permission.ask({ |
| 87 | type: "edit", |
| 88 | sessionID: ctx.sessionID, |
| 89 | messageID: ctx.messageID, |
| 90 | callID: ctx.callID, |
| 91 | title: "Edit this file: " + filePath, |
| 92 | metadata: { |