@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
65 lines • 2.95 kB
JavaScript
import { encodeUtf8 } from "./encoding.js";
import { joinHeadAndTail } from "./output-accumulator-core.js";
function randomHex(bytes) {
return Array.from(crypto.getRandomValues(new Uint8Array(bytes)), (byte) => byte.toString(16).padStart(2, "0")).join("");
}
/**
* Spills to a file on the tool machine, so the path a truncation notice names is one the model can read there.
* Chunks are written in order at their byte offsets as they arrive. The operation layer has no delete, so `remove`
* empties the file instead, and does nothing when no chunk ever reached the machine.
*/
export function createMachineOutputSpill(machine, options) {
const tempDir = options?.tempDir ?? "/tmp";
return (prefix) => {
const path = `${tempDir}/${prefix}-${randomHex(8)}.log`;
const ctx = machine.context(undefined);
let size = 0;
let written = false;
let writeFailed = false;
let writeError;
let pending = Promise.resolve();
const read = (offset, limitBytes) => machine.ops.files.read({ path, offset, limitBytes }, ctx).then((result) => result.bytes);
return {
path,
write: (chunk) => {
const bytes = typeof chunk === "string" ? encodeUtf8(chunk) : chunk;
const offset = size;
size += bytes.length;
pending = pending.then(async () => {
if (writeFailed)
return;
try {
await machine.ops.files.write({ path, bytes, offset, createParentDirs: true }, ctx);
written = true;
}
catch (error) {
writeFailed = true;
writeError = error;
}
});
},
close: async () => {
await pending;
if (writeFailed)
throw writeError;
},
remove: async () => {
await pending;
// Nothing reached the machine (the first write failed): there is no file to empty.
if (!written)
return;
await machine.ops.files.write({ path, bytes: new Uint8Array() }, ctx);
},
readFull: async (maxBytes) => {
if (size <= maxBytes) {
return { content: new TextDecoder().decode(await read(0, size)), truncated: false };
}
const headBytes = Math.floor(maxBytes / 2);
const tailBytes = maxBytes - headBytes;
const [head, tail] = await Promise.all([read(0, headBytes), read(size - tailBytes, tailBytes)]);
return { content: joinHeadAndTail(head, tail, size - headBytes - tailBytes), truncated: true };
},
};
};
}
//# sourceMappingURL=machine-output-spill.js.map