UNPKG

@code-yeongyu/senpi

Version:

Coding agent CLI with read, bash, edit, write tools and session management

175 lines • 9.19 kB
import { Type } from "typebox"; import { splitBom } from "../../utils/text.js"; import { applyEditsToNormalizedContent, detectLineEnding, generateDiffString, generateUnifiedPatch, normalizeToLF, restoreLineEndings, } from "./edit-diff.js"; import { decodeUtf8, encodeUtf8 } from "./encoding.js"; import { fsErrorCode } from "./fs-error-code.js"; import { inProcessFileMutationQueue, lexicalCanonicalPath, resolvePosixToolPath, } from "./file-tool-host.js"; import { wrapToolDefinition } from "./tool-definition-wrapper.js"; const replaceEditSchema = Type.Object({ oldText: Type.String({ description: "Exact text for one targeted replacement. It must be unique in the original file and must not overlap with any other edits[].oldText in the same call.", }), newText: Type.String({ description: "Replacement text for this targeted edit." }), }, {}); const editSchema = Type.Object({ path: Type.String({ description: "Path to the file to edit (relative or absolute)" }), edits: Type.Array(replaceEditSchema, { description: "One or more targeted replacements. Each edit is matched against the original file, not incrementally. Do not include overlapping or nested edits. If two changes touch the same block or nearby lines, merge them into one edit instead.", }), }, {}); export const editToolSystemPromptContribution = { snippet: "Make precise file edits with exact text replacement, including multiple disjoint edits in one call", guidelines: ["All changes to one file -> one edit call."], fullGuidelines: [ "Use edit for precise changes (edits[].oldText must match exactly)", "When changing multiple separate locations in one file, use one edit call with multiple entries in edits[] instead of multiple edit calls", "Each edits[].oldText is matched against the original file, not after earlier edits are applied. Do not emit overlapping or nested edits. Merge nearby changes into one edit.", "Keep edits[].oldText as small as possible while still being unique in the file. Do not pad with large unchanged regions.", ], }; function isSingleEditInput(value) { if (!value || typeof value !== "object" || Array.isArray(value)) { return false; } const edit = value; return typeof edit.oldText === "string" && typeof edit.newText === "string"; } /** The default edit operations over the shared operation layer; the tool already holds the file's mutation slot. */ function machineEditOperations(machine, signal) { const ctx = machine.context(signal, "caller"); return { readFile: async (path) => (await machine.ops.files.read({ path }, ctx)).bytes, writeFile: async (path, content) => { await machine.ops.files.write({ path, bytes: encodeUtf8(content) }, ctx); }, access: async (path) => { await machine.ops.files.stat({ path, access: "readwrite" }, ctx); }, }; } function prepareEditArguments(input) { if (!input || typeof input !== "object") { return input; } const args = input; // Some models (Opus 4.6, GLM-5.1) send edits as a JSON string instead of an array. // Others send a single edit object instead of a one-element edits array. if (typeof args.edits === "string") { try { const parsed = JSON.parse(args.edits); if (Array.isArray(parsed)) { args.edits = parsed; } else if (isSingleEditInput(parsed)) { args.edits = [parsed]; } } catch { } } else if (isSingleEditInput(args.edits)) { args.edits = [args.edits]; } const legacy = args; if (typeof legacy.oldText !== "string" || typeof legacy.newText !== "string") { return args; } const edits = Array.isArray(legacy.edits) ? [...legacy.edits] : []; edits.push({ oldText: legacy.oldText, newText: legacy.newText }); const { oldText: _oldText, newText: _newText, ...rest } = legacy; return { ...rest, edits }; } function validateEditInput(input) { if (!Array.isArray(input.edits) || input.edits.length === 0) { throw new Error("Edit tool input is invalid. edits must contain at least one replacement."); } return { path: input.path, edits: input.edits }; } export function createCoreEditToolDefinition(cwd, options) { const machine = options.machine; const filesystemPolicy = options.filesystemPolicy; const resolvePath = options.resolvePath ?? resolvePosixToolPath; const withMutationQueue = options.withMutationQueue ?? inProcessFileMutationQueue; const canonicalizePath = options.canonicalizePath ?? lexicalCanonicalPath; return { name: "edit", label: "edit", description: "Edit a single file using exact text replacement. Every edits[].oldText must match a unique, non-overlapping region of the original file. If two changes affect the same block or nearby lines, merge them into one edit instead of emitting overlapping edits. Do not include large unchanged regions just to connect distant changes.", promptSnippet: editToolSystemPromptContribution.snippet, promptGuidelines: [...editToolSystemPromptContribution.guidelines], fullPromptGuidelines: [...editToolSystemPromptContribution.fullGuidelines], parameters: editSchema, constrainedSampling: { type: "json_schema", strict: "prefer" }, renderShell: "self", prepareArguments: prepareEditArguments, async execute(_toolCallId, input, signal, _onUpdate, ctx) { const { path, edits } = validateEditInput(input); const admitted = machine.begin("files.edit", { path, edits }, signal); if (admitted !== undefined) await admitted; const ops = options.operations ?? machineEditOperations(machine, signal); const absolutePath = resolvePath(path, ctx?.cwd || cwd); return withMutationQueue(absolutePath, async () => { // Do not reject from an abort event listener here: that would release the // mutation queue while an in-flight filesystem operation may still finish. // Checking signal.aborted after each await observes the same aborts while // keeping the queue locked until the current operation has settled. const throwIfAborted = () => { if (signal?.aborted) throw new Error("Operation aborted"); }; throwIfAborted(); if (filesystemPolicy) { const decision = await filesystemPolicy({ operation: "write", canonicalPath: await canonicalizePath(absolutePath), toolName: "edit", }); if (!decision.allow) throw new Error(decision.reason); throwIfAborted(); } // Check if file exists. try { await ops.access(absolutePath); } catch (error) { throwIfAborted(); const errorMessage = error instanceof Error && "code" in error ? `Error code: ${fsErrorCode(error) ?? error.code}` : String(error); throw new Error(`Could not edit file: ${path}. ${errorMessage}.`); } throwIfAborted(); // Read the file. const buffer = await ops.readFile(absolutePath); const rawContent = decodeUtf8(buffer); throwIfAborted(); // Strip BOM before matching. The model will not include an invisible BOM in oldText. const { bom, text: content } = splitBom(rawContent); const originalEnding = detectLineEnding(content); const normalizedContent = normalizeToLF(content); const { baseContent, newContent } = applyEditsToNormalizedContent(normalizedContent, edits, path); throwIfAborted(); const finalContent = bom + restoreLineEndings(newContent, originalEnding); await ops.writeFile(absolutePath, finalContent); throwIfAborted(); const diffResult = generateDiffString(baseContent, newContent); const patch = generateUnifiedPatch(path, baseContent, newContent); return { content: [ { type: "text", text: `Successfully replaced ${edits.length} block(s) in ${path}.`, }, ], details: { diff: diffResult.diff, patch, firstChangedLine: diffResult.firstChangedLine }, }; }); }, }; } export function createCoreEditTool(cwd, options) { return wrapToolDefinition(createCoreEditToolDefinition(cwd, options)); } export { editSchema }; //# sourceMappingURL=edit-core.js.map