67 lines
1.6 KiB
JavaScript
67 lines
1.6 KiB
JavaScript
let input = '';
|
|
let activeTimer = null;
|
|
let activeMarker = null;
|
|
|
|
function write(record) {
|
|
process.stdout.write(`${JSON.stringify(record)}\n`);
|
|
}
|
|
|
|
function respond(command, data = {}) {
|
|
write({
|
|
type: 'response',
|
|
id: command.id,
|
|
command: command.type,
|
|
success: true,
|
|
data,
|
|
});
|
|
}
|
|
|
|
function settle(reason) {
|
|
if (!activeMarker) return;
|
|
write({ type: 'agent_end', marker: activeMarker, reason });
|
|
write({ type: 'agent_settled', marker: activeMarker, reason });
|
|
activeMarker = null;
|
|
activeTimer = null;
|
|
}
|
|
|
|
function handle(command) {
|
|
if (command.type === 'prompt') {
|
|
activeMarker = command.message;
|
|
respond(command, { accepted: true });
|
|
write({ type: 'agent_start', marker: activeMarker });
|
|
activeTimer = setTimeout(() => settle('completed'), Number(command.delayMs ?? 100));
|
|
return;
|
|
}
|
|
if (command.type === 'abort') {
|
|
if (activeTimer) clearTimeout(activeTimer);
|
|
respond(command, { aborted: Boolean(activeMarker) });
|
|
settle('aborted');
|
|
return;
|
|
}
|
|
if (command.type === 'get_state') {
|
|
respond(command, { sessionId: `session-${process.pid}` });
|
|
return;
|
|
}
|
|
if (command.type === 'get_entries') {
|
|
respond(command, { entries: [] });
|
|
return;
|
|
}
|
|
respond(command);
|
|
}
|
|
|
|
process.stdin.setEncoding('utf8');
|
|
process.stdin.on('data', (chunk) => {
|
|
input += chunk;
|
|
while (true) {
|
|
const newline = input.indexOf('\n');
|
|
if (newline === -1) break;
|
|
const line = input.slice(0, newline).replace(/\r$/, '');
|
|
input = input.slice(newline + 1);
|
|
if (line) handle(JSON.parse(line));
|
|
}
|
|
});
|
|
process.stdin.on('end', () => {
|
|
if (activeTimer) clearTimeout(activeTimer);
|
|
process.exit(0);
|
|
});
|