UNPKG

@code-yeongyu/senpi

Version:

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

108 lines • 5.51 kB
import { Type } from "typebox"; import { decodeUtf8, encodeUtf8 } from "./encoding.js"; import { fsErrorCode } from "./fs-error-code.js"; import { inProcessFileMutationQueue, lexicalCanonicalPath, posixDirname, resolvePosixToolPath, } from "./file-tool-host.js"; import { wrapToolDefinition } from "./tool-definition-wrapper.js"; import { createWriteDetails } from "./write-result.js"; export const writeSchema = Type.Object({ path: Type.String({ description: "Path to the file to write (relative or absolute)" }), content: Type.String({ description: "Content to write to the file" }), }); export const writeToolSystemPromptContribution = { snippet: "Create or overwrite files", guidelines: ["write only for new files or full rewrites."], fullGuidelines: ["Use write only for new files or complete rewrites."], }; /** * The default write operations over the shared operation layer. `files.write` creates the parent directory in the * same call (`createParentDirs`), so `mkdir` has nothing left to do; the tool already holds the file's mutation slot. */ function machineWriteOperations(machine, signal) { const ctx = machine.context(signal, "caller"); return { writeFile: async (path, content) => { await machine.ops.files.write({ path, bytes: encodeUtf8(content), createParentDirs: true }, ctx); }, mkdir: async () => { }, }; } /** The file's content before the write, read on the machine; it feeds the patch in the result details. */ function machineWriteBaseline(machine) { return async (absolutePath) => { try { const { bytes } = await machine.ops.files.read({ path: absolutePath }, machine.context(undefined, "caller")); return { kind: "present", content: decodeUtf8(bytes) }; } catch (error) { if (error instanceof Error && fsErrorCode(error) === "ENOENT") return { kind: "missing" }; if (error instanceof Error) return { kind: "unavailable" }; throw error; } }; } export function createCoreWriteToolDefinition(cwd, options) { const machine = options.machine; const readBaseline = options.operations ? undefined : (options.readBaseline ?? machineWriteBaseline(machine)); const filesystemPolicy = options.filesystemPolicy; const resolvePath = options.resolvePath ?? resolvePosixToolPath; const dirname = options.dirname ?? posixDirname; const withMutationQueue = options.withMutationQueue ?? inProcessFileMutationQueue; const canonicalizePath = options.canonicalizePath ?? lexicalCanonicalPath; return { name: "write", label: "write", description: "Write content to a file. Creates the file if it doesn't exist, overwrites if it does. Automatically creates parent directories.", promptSnippet: writeToolSystemPromptContribution.snippet, promptGuidelines: [...writeToolSystemPromptContribution.guidelines], fullPromptGuidelines: [...writeToolSystemPromptContribution.fullGuidelines], parameters: writeSchema, constrainedSampling: { type: "json_schema", strict: "prefer" }, async execute(_toolCallId, { path, content }, signal, _onUpdate, ctx) { const admitted = machine.begin("files.write", { path, content }, signal); if (admitted !== undefined) await admitted; const ops = options.operations ?? machineWriteOperations(machine, signal); const absolutePath = resolvePath(path, ctx?.cwd || cwd); const dir = dirname(absolutePath); 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: "write", }); if (!decision.allow) throw new Error(decision.reason); throwIfAborted(); } const baseline = readBaseline ? await readBaseline(absolutePath) : { kind: "unavailable" }; throwIfAborted(); // Create parent directories if needed. await ops.mkdir(dir); throwIfAborted(); // Write the file contents. await ops.writeFile(absolutePath, content); throwIfAborted(); return { content: [{ type: "text", text: `Successfully wrote to ${path}` }], details: createWriteDetails(path, content, baseline), }; }); }, }; } export function createCoreWriteTool(cwd, options) { return wrapToolDefinition(createCoreWriteToolDefinition(cwd, options)); } //# sourceMappingURL=write-core.js.map