UNPKG

@code-yeongyu/senpi

Version:

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

152 lines • 5.92 kB
import { modelOnlyText } from "./model-only-text.js"; import { SecretOutputRedactor } from "./secret-output-redactor.js"; import { DEFAULT_MAX_BYTES, formatSize } from "./truncate.js"; const BASH_UPDATE_THROTTLE_MS = 100; /** * One shell run's output stream: optional secret redaction per stream, throttled partial updates, and the terminal * flush that settles the spill before the result is built. */ export function createShellOutput(output, onUpdate, redactSecretsInOutput) { const streams = redactSecretsInOutput ? new Map() : undefined; let acceptingOutput = true; let updateTimer; let updateDirty = false; let lastUpdateAt = 0; let outputUpdateError; let hasOutputUpdateError = false; const emitOutputUpdate = () => { if (!onUpdate || !updateDirty || hasOutputUpdateError) return; updateDirty = false; lastUpdateAt = Date.now(); const snapshot = output.snapshot({ persistIfTruncated: true }); try { onUpdate({ content: [{ type: "text", text: snapshot.content || "" }], details: { truncation: snapshot.truncation.truncated ? snapshot.truncation : undefined, fullOutputPath: snapshot.fullOutputPath, }, }); } catch (error) { outputUpdateError = error; hasOutputUpdateError = true; } }; const clearUpdateTimer = () => { if (updateTimer) { clearTimeout(updateTimer); updateTimer = undefined; } }; const scheduleOutputUpdate = () => { if (!onUpdate) return; updateDirty = true; const delay = BASH_UPDATE_THROTTLE_MS - (Date.now() - lastUpdateAt); if (delay <= 0) { clearUpdateTimer(); emitOutputUpdate(); return; } updateTimer ??= setTimeout(() => { updateTimer = undefined; emitOutputUpdate(); }, delay); }; const handleData = (data, stream = "stdout") => { if (!acceptingOutput) return; if (streams) { let state = streams.get(stream); if (!state) { state = { redactor: new SecretOutputRedactor(), decoder: new TextDecoder() }; streams.set(stream, state); } output.appendText(state.redactor.push(state.decoder.decode(data, { stream: true }))); } else output.append(data); scheduleOutputUpdate(); }; const finishOutput = async () => { acceptingOutput = false; clearUpdateTimer(); let primaryError; let hasPrimaryError = false; let snapshot; try { if (streams) { for (const { redactor, decoder } of streams.values()) { output.appendText(redactor.push(decoder.decode()) + redactor.finish()); } updateDirty = true; } output.finish(); emitOutputUpdate(); snapshot = output.snapshot({ persistIfTruncated: true }); if (hasOutputUpdateError) { primaryError = outputUpdateError; hasPrimaryError = true; } } catch (error) { primaryError = error; hasPrimaryError = true; } try { await output.closeTempFile(); } catch (closeError) { if (hasPrimaryError) { throw new AggregateError([primaryError, closeError], "Bash output finalization and spill cleanup failed"); } throw closeError; } if (hasPrimaryError) { try { await output.removeTempFile(); } catch (unlinkError) { if (!(unlinkError instanceof Error)) throw unlinkError; throw new AggregateError([primaryError, unlinkError], "Bash output finalization and spill cleanup failed"); } throw primaryError; } if (snapshot === undefined) { throw new Error("Bash output finalization produced no snapshot"); } return snapshot; }; const formatOutput = (snapshot, emptyText = "(no output)") => { const truncation = snapshot.truncation; const text = snapshot.content || emptyText; let notice; let details; if (truncation.truncated) { details = { truncation, fullOutputPath: snapshot.fullOutputPath }; const startLine = truncation.totalLines - truncation.outputLines + 1; const endLine = truncation.totalLines; if (truncation.lastLinePartial) { const lastLineSize = formatSize(output.getLastLineBytes()); notice = `[Showing last ${formatSize(truncation.outputBytes)} of line ${endLine} (line is ${lastLineSize}). Full output: ${snapshot.fullOutputPath}]`; } else if (truncation.truncatedBy === "lines") { notice = `[Showing lines ${startLine}-${endLine} of ${truncation.totalLines}. Full output: ${snapshot.fullOutputPath}]`; } else { notice = `[Showing lines ${startLine}-${endLine} of ${truncation.totalLines} (${formatSize(DEFAULT_MAX_BYTES)} limit). Full output: ${snapshot.fullOutputPath}]`; } } const content = [{ type: "text", text: notice === undefined ? text : `${text}\n` }]; if (notice !== undefined) content.push(modelOnlyText(notice)); return { text: notice === undefined ? text : `${text}\n\n${notice}`, content, details }; }; return { handleData, finishOutput, formatOutput, clearUpdateTimer }; } //# sourceMappingURL=shell-output.js.map