The CLI's side of harness spec 3.11.0, on serve --stdio and on the link. - acp/editor.ts: _lembas/complete (one HTTP request with the login the agent holds, the window trimmed, a newer request closes the older one's connection and that one is answered as cancelled), _lembas/edit (one model request with no tools; the stream cleaned of a fence around the whole answer and given the selection's ending; deltas; cancel), _lembas/models, _lembas/editor/settings, the editor block of a prompt, and ACP diff items. - acp/files.ts: _lembas/fs/list, stat, read, write, mkdir, move, copy, delete, search. A path is judged where it really is, symlinks followed first; outside remote.roots, the CLI's own directories and protected paths are refused, and so is a folder that holds them; read-only where the device allows plan mode only. A write is checked against the hash the client last saw, twice for an upload in parts, and put in place whole. A delete goes to the system's trash or is refused until the client says permanent. - tool/buffers.ts: read, write, edit, multiedit and apply_patch take a file's text from the client's editor and write through it where the client offers ACP's fs methods; any failure there is the disk. The call is judged before any of it, as before. An overwrite of text that changed in the editor since it was read is refused. - A tool's preview gives each file's text before and after beside the unified diff; a client that said editor: true gets them as diff items on the approval card and on every finished edit. The texts are kept only for such a client. - tool/show.ts: to a client that said media: true a file that is not text is handed over by a handle (_lembas/show/read), good while the request is open and for that file as it was; a table, an SVG and a notebook are named. On stdio the request also says where the file is. - The hardline, the modes and the rules are untouched; tool/replace.ts is untouched. Tests: tests/acp-editor.test.ts (19), tests/acp-files.test.ts (13), and the conformance areas editor, device_files and show. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
53 lines
2.5 KiB
TypeScript
53 lines
2.5 KiB
TypeScript
import { createTwoFilesPatch } from "diff"
|
||
import { z } from "zod"
|
||
import { replace } from "./replace-text.ts"
|
||
import { absPath, defineTool, showPath, ToolError, type ToolContext } from "./tool.ts"
|
||
import { diffStat, guardExisting, stampOf } from "./write.ts"
|
||
import { fileText, noteChanges, putText } from "./buffers.ts"
|
||
import { described } from "../harness.ts"
|
||
import { hunks, sha256 } from "../session/results.ts"
|
||
|
||
async function planEdit(a: { path: string; old: string; new: string; all?: boolean }, ctx: ToolContext) {
|
||
const abs = absPath(a.path, ctx)
|
||
const display = showPath(abs, ctx)
|
||
if (!(await guardExisting(abs, ctx))) throw new ToolError(`No such file or directory: ${abs}`)
|
||
const raw = await fileText(abs, ctx)
|
||
const crlf = raw.includes("\r\n")
|
||
const before = crlf ? raw.replaceAll("\r\n", "\n") : raw
|
||
try {
|
||
return { abs, display, before, crlf, after: replace(before, a.old.replaceAll("\r\n", "\n"), a.new.replaceAll("\r\n", "\n"), a.all ?? false) }
|
||
} catch (e) {
|
||
throw new ToolError(`${display}: ${(e as Error).message}`)
|
||
}
|
||
}
|
||
|
||
export const editTool = defineTool({
|
||
...described("edit"),
|
||
access: "edit",
|
||
schema: z.object({
|
||
path: z.string(),
|
||
old: z.string(),
|
||
new: z.string(),
|
||
all: z.boolean().optional(),
|
||
}),
|
||
permission: (a, ctx) => {
|
||
const abs = absPath(a.path, ctx)
|
||
return { permission: "edit", class: "write", patterns: [showPath(abs, ctx)], paths: [abs] }
|
||
},
|
||
async preview(a, ctx) {
|
||
const { abs, display, before, after, crlf } = await planEdit(a, ctx)
|
||
const nl = (t: string) => (crlf ? t.replaceAll("\n", "\r\n") : t)
|
||
return { diff: createTwoFilesPatch(display, display, before, after, "", "", { context: 3 }), files: [{ path: abs, oldText: nl(before), newText: nl(after) }] }
|
||
},
|
||
async run(a, ctx) {
|
||
const { abs, display, before, after, crlf } = await planEdit(a, ctx)
|
||
const written = crlf ? after.replaceAll("\n", "\r\n") : after
|
||
await putText(abs, written, ctx)
|
||
ctx.fileStamps.set(abs, await stampOf(abs))
|
||
const patch = createTwoFilesPatch(display, display, before, after, "", "", { context: 3 })
|
||
const { added, removed } = diffStat(patch)
|
||
noteChanges(ctx, [{ path: abs, oldText: crlf ? before.replaceAll("\n", "\r\n") : before, newText: written }])
|
||
return { output: `Edited ${display} (+${added} −${removed}).`, title: `${display} +${added} −${removed}`, meta: { diff: patch }, context: { kind: "edit", files: [{ path: a.path, sha: sha256(written), hunks: hunks(before, after) }] } }
|
||
},
|
||
})
|