UNPKG

histoire

Version:

A fast story book powered by Vite

72 lines (71 loc) • 2.96 kB
import { launchAgentProcess } from './agent-process.js'; import { connectAgentClient } from './client.js'; /** Bounded subprocess + SDK lifetime, available before initialize resolves. */ export function createAgentRuntime(preset, options) { const process = launchAgentProcess(preset, options.root, options.environment); let stopped = false; let error; const lines = []; let stderr = ''; /** Stores only bounded redacted lines; no raw transport frame is logged. */ function flushStderr(final = false) { const split = stderr.split(/\r?\n/); stderr = final ? '' : split.pop(); for (const line of split) { if (line) lines.push(options.sanitize(line)); } lines.splice(0, Math.max(0, lines.length - 30)); options.log([...lines]); } process.child.stderr.on('data', (chunk) => { stderr = `${stderr}${chunk.toString()}`.slice(-32_000); flushStderr(); }); process.child.once('error', (cause) => { error = cause.code === 'ENOENT' ? `Agent command "${preset.command}" is not installed` : `Agent process failed (${cause.code ?? 'unknown'})`; if (!stopped) options.failed(options.sanitize(error), cause.code === 'ENOENT'); }); process.child.once('close', (code, signal) => { flushStderr(true); if (!stopped && !error) { error = options.sanitize(`Agent exited (${signal ?? code ?? 'unknown'})${lines.length ? `: ${lines.slice(-3).join('\n')}` : ''}`); options.failed(error, false); } }); const sdk = connectAgentClient(process, options); const ready = withAgentTimeout(sdk.initialize(), options.timeout, 'ACP initialize handshake timed out'); void ready.catch(() => { }); return { ...sdk, ready, /** Last controlled/redacted process failure, replacing generic stream errors. */ failure: () => error, /** Waits briefly for stderr/exit before projecting a generic transport close. */ async failureMessage() { if (sdk.connection.signal.aborted && !stopped) await withAgentTimeout(process.exited, 250, 'Agent exit pending').catch(() => { }); return error; }, /** Stops SDK transport before killing the owned agent process. */ async stop() { stopped = true; sdk.connection.close(new Error('Agent runtime closed')); await process.stop(); }, }; } /** Bounded handshake operations cannot keep a retired dev server alive forever. */ export async function withAgentTimeout(operation, timeout, message) { let timer; try { return await Promise.race([operation, new Promise((_, reject) => { timer = setTimeout(() => reject(new Error(message)), timeout); timer.unref(); })]); } finally { clearTimeout(timer); } }