UNPKG

@code-yeongyu/senpi

Version:

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

111 lines • 3.66 kB
import { randomBytes } from "node:crypto"; import { createWriteStream } from "node:fs"; import { open, rm } from "node:fs/promises"; import { tmpdir } from "node:os"; import { join } from "node:path"; import { OutputAccumulator as CoreOutputAccumulator, joinHeadAndTail, } from "./output-accumulator-core.js"; function defaultTempFilePath(prefix) { const id = randomBytes(8).toString("hex"); return join(tmpdir(), `${prefix}-${id}.log`); } function destroyStream(stream) { return new Promise((resolve) => { if (stream.closed) { resolve(); return; } stream.once("close", resolve); stream.destroy(); }); } function endStream(stream) { return new Promise((resolve, reject) => { let finished = false; let closed = false; let streamError; const settle = () => { if (!closed) { return; } stream.off("finish", onFinish); stream.off("error", onError); stream.off("close", onClose); if (streamError) { reject(streamError); } else if (finished) { resolve(); } else { reject(new Error("Output spill stream closed before finish")); } }; const onError = (error) => { streamError ??= error; }; const onFinish = () => { finished = true; }; const onClose = () => { closed = true; if (!finished && !stream.destroyed) { stream.destroy(); } settle(); }; stream.once("error", onError); stream.once("finish", onFinish); stream.once("close", onClose); stream.end(); }); } async function readTempFile(path, maxBytes) { const file = await open(path, "r"); try { const size = (await file.stat()).size; if (size <= maxBytes) { return { content: new TextDecoder().decode(await file.readFile()), truncated: false }; } const headBytes = Math.floor(maxBytes / 2); const tailBytes = maxBytes - headBytes; const head = Buffer.alloc(headBytes); const tail = Buffer.alloc(tailBytes); await file.read(head, 0, headBytes, 0); await file.read(tail, 0, tailBytes, size - tailBytes); return { content: joinHeadAndTail(head, tail, size - headBytes - tailBytes), truncated: true }; } finally { await file.close(); } } /** The local spill: a temp file under the OS temp directory, streamed as output arrives. */ export const createTempFileSpill = (prefix) => { const path = defaultTempFilePath(prefix); const stream = createWriteStream(path); let writeError; stream.on("error", (error) => { writeError ??= error; }); return { path, write: (chunk) => { stream.write(chunk); }, close: async () => { if (writeError) { await destroyStream(stream); throw writeError; } await endStream(stream); }, remove: () => rm(path, { force: true }), readFull: (maxBytes) => readTempFile(path, maxBytes), }; }; /** The output accumulator of the local tools: it spills to a temp file unless the caller supplies a spill. */ export class OutputAccumulator extends CoreOutputAccumulator { constructor(options = {}) { super({ ...options, spill: options.spill ?? createTempFileSpill }); } } //# sourceMappingURL=output-accumulator.js.map