histoire
Version:
A fast story book powered by Vite
43 lines (42 loc) • 1.98 kB
JavaScript
import { projectAgentPlan } from './plan.js';
import { projectAgentCommands } from './projection.js';
import { emitReplyChunks } from './reply-chunks.js';
/**
* Routes one ACP session notification: commands and config updates apply to the record,
* reply chunks and activity only to the exact active prompt/session.
* @param agentId Agent preset id.
* @param record Captured agent record.
* @param notification Incoming notification.
* @param routing Manager callbacks.
*/
export function routeAgentNotification(agentId, record, notification, routing) {
const update = notification.update;
if (update.sessionUpdate === 'available_commands_update') {
routing.remember(agentId, projectAgentCommands(update.availableCommands, routing.sanitize));
return;
}
if (update.sessionUpdate === 'config_option_update') {
const session = [...record.sessions.values()].find(session => session.sessionId === notification.sessionId);
if (session)
session.configOptions = update.configOptions;
return;
}
const active = record.active;
if (!active || notification.sessionId !== active.sessionId)
return;
if (update.sessionUpdate === 'plan') {
active.plan = projectAgentPlan(update.entries ?? [], routing.sanitize);
routing.emit({ type: 'plan', value: { agentId, threadId: active.request.threadId, entries: active.plan } });
return;
}
active.changes.update(notification);
for (const step of active.activity.update(notification))
routing.emit({ type: 'activity', value: { agentId, threadId: active.request.threadId, step } });
if (update.sessionUpdate === 'agent_message_chunk' && update.content.type === 'text') {
const text = active.output.push(update.content.text);
if (!text)
return;
active.text = `${active.text}${text}`.slice(-256_000);
emitReplyChunks(agentId, active.request, text, routing.emit);
}
}