|
| 1 | +import { mkdir, readFile, rm, writeFile } from "node:fs/promises"; |
| 2 | +import { dirname, isAbsolute, relative, resolve } from "node:path"; |
| 3 | +import { stringTool } from "@intx/agent"; |
| 4 | +import type { AgentTool } from "@intx/agent"; |
| 5 | +import type { ToolDefinition } from "@intx/types/runtime"; |
| 6 | +import { type } from "arktype"; |
| 7 | + |
| 8 | +import { |
| 9 | + applyUpdateHunks, |
| 10 | + CodexApplyPatchError, |
| 11 | + parseCodexApplyPatch, |
| 12 | + type PatchOp, |
| 13 | +} from "./codex-apply-patch.js"; |
| 14 | + |
| 15 | +export const applyPatchDefinition: ToolDefinition = { |
| 16 | + name: "apply_patch", |
| 17 | + description: |
| 18 | + "Edit files with a patch envelope: *** Begin Patch, then *** Add File: / *** Update File: (optional *** Move to:) / *** Delete File: sections, then *** End Patch. Update hunks start with @@ and use ' ', '-', '+' line prefixes. Paths are workspace-relative.", |
| 19 | + inputSchema: { |
| 20 | + type: "object", |
| 21 | + properties: { |
| 22 | + input: { type: "string", description: "The full patch envelope" }, |
| 23 | + }, |
| 24 | + required: ["input"], |
| 25 | + }, |
| 26 | +}; |
| 27 | + |
| 28 | +const ApplyPatchArgs = type({ input: "string" }); |
| 29 | + |
| 30 | +type Planned = |
| 31 | + | { kind: "write"; path: string; content: string; label: string } |
| 32 | + | { kind: "remove"; path: string; label: string }; |
| 33 | + |
| 34 | +function contained(cwd: string, path: string): string { |
| 35 | + const abs = resolve(cwd, path); |
| 36 | + const rel = relative(cwd, abs); |
| 37 | + if (rel === "" || rel.startsWith("..") || isAbsolute(rel)) { |
| 38 | + throw new CodexApplyPatchError(`path escapes the workspace: ${path}`); |
| 39 | + } |
| 40 | + return abs; |
| 41 | +} |
| 42 | + |
| 43 | +async function plan(cwd: string, op: PatchOp): Promise<Planned[]> { |
| 44 | + if (op.type === "add") { |
| 45 | + const path = contained(cwd, op.path); |
| 46 | + return [ |
| 47 | + { kind: "write", path, content: op.content, label: `A ${op.path}` }, |
| 48 | + ]; |
| 49 | + } |
| 50 | + if (op.type === "delete") { |
| 51 | + return [ |
| 52 | + { kind: "remove", path: contained(cwd, op.path), label: `D ${op.path}` }, |
| 53 | + ]; |
| 54 | + } |
| 55 | + const source = contained(cwd, op.path); |
| 56 | + let original: string; |
| 57 | + try { |
| 58 | + original = await readFile(source, "utf8"); |
| 59 | + } catch { |
| 60 | + throw new CodexApplyPatchError(`cannot read ${op.path} to update it`); |
| 61 | + } |
| 62 | + const content = applyUpdateHunks(original, op.hunks); |
| 63 | + if (op.moveTo === undefined) { |
| 64 | + return [{ kind: "write", path: source, content, label: `M ${op.path}` }]; |
| 65 | + } |
| 66 | + return [ |
| 67 | + { |
| 68 | + kind: "write", |
| 69 | + path: contained(cwd, op.moveTo), |
| 70 | + content, |
| 71 | + label: `M ${op.moveTo}`, |
| 72 | + }, |
| 73 | + { kind: "remove", path: source, label: "" }, |
| 74 | + ]; |
| 75 | +} |
| 76 | + |
| 77 | +/** |
| 78 | + * Plans every op before touching disk so a bad hunk in the last file cannot |
| 79 | + * leave the earlier files half-patched. |
| 80 | + */ |
| 81 | +export function createApplyPatchTool(cwd: string): AgentTool { |
| 82 | + return stringTool({ |
| 83 | + definition: applyPatchDefinition, |
| 84 | + handler: async (rawArgs: Record<string, unknown>): Promise<string> => { |
| 85 | + const parsedArgs = ApplyPatchArgs(rawArgs); |
| 86 | + if (parsedArgs instanceof type.errors) { |
| 87 | + return "Error: apply_patch requires input (string)."; |
| 88 | + } |
| 89 | + try { |
| 90 | + const patch = parseCodexApplyPatch(parsedArgs.input); |
| 91 | + const steps: Planned[] = []; |
| 92 | + for (const op of patch.ops) steps.push(...(await plan(cwd, op))); |
| 93 | + for (const step of steps) { |
| 94 | + if (step.kind === "write") { |
| 95 | + await mkdir(dirname(step.path), { recursive: true }); |
| 96 | + await writeFile(step.path, step.content); |
| 97 | + } else { |
| 98 | + await rm(step.path, { force: true }); |
| 99 | + } |
| 100 | + } |
| 101 | + const labels = steps.map((s) => s.label).filter((l) => l !== ""); |
| 102 | + return `Success. Updated the following files:\n${labels.join("\n")}`; |
| 103 | + } catch (err) { |
| 104 | + if (err instanceof CodexApplyPatchError) return `Error: ${err.message}`; |
| 105 | + throw err; |
| 106 | + } |
| 107 | + }, |
| 108 | + }); |
| 109 | +} |
0 commit comments