UNPKG

@code-yeongyu/senpi

Version:

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

61 lines • 3.36 kB
import { clientFacingIdentity } from "./client-admission-record.js"; import { ClientAdmissions } from "./client-admissions.js"; import { ClientMessageEvents } from "./client-message-events.js"; import { observeLifecycle } from "./lifecycle-delivery.js"; /** Subscribes `output` to the session's records (from `source`, by default `session.subscribe`); returns the unsubscribe. */ export function subscribeSessionRecords(session, output, onAbortSource = () => { }, source = (listener) => session.subscribe(listener)) { const correlatedEvents = new ClientMessageEvents(output, () => ClientAdmissions.forSession(session).hasIdentities); return source((event) => { if (event.type === "session_lifecycle") { // The connection adds the routing handle; classic stdio has no envelope. const { sessionId: _sessionId, ...record } = event; output({ ...record, lifecycle: observeLifecycle(event.lifecycle) }); return; } if (event.type === "skill_invocation") { correlatedEvents.accept(event); return; } if (event.type === "command_invocation") { correlatedEvents.accept(event); return; } if (event.type === "queue_update") { // Same route as every other event; only the minted ids are taken out of the projection. correlatedEvents.accept({ ...event, ordered: event.ordered.map(clientFacingIdentity) }); return; } if (event.type === "thinking_level_changed" || event.type === "model_changed") { // Core emits the effective level only; the selection provenance lives beside it // on the session and is what distinguishes an explicit choice from a default. const thinkingSelection = session.thinkingSelection; output(thinkingSelection === undefined ? event : { ...event, thinkingSelection }); return; } if (event.type === "agent_end") { // The subscribe-path `agent_end` carries only { type, messages, willRetry }; // the abort provenance the extension hook receives is stripped from it. Read // it from the session, which still reports the owner at this point, and retain // it for `get_state` because the getter clears once the turn settles. const abortSource = session.currentAbortSource; if (abortSource !== undefined) onAbortSource(abortSource); const deadline = session.currentDeadline; const abortedBy = abortSource === undefined ? undefined : ClientAdmissions.forSession(session).abortedBy; correlatedEvents.accept({ ...event, ...(abortSource === undefined ? {} : { aborted: true, abortSource }), ...(abortedBy === undefined ? {} : { abortedBy }), ...(deadline === undefined ? {} : { deadline }), }); return; } if (event.type === "session_abort") { const abortedBy = ClientAdmissions.forSession(session).abortedBy; correlatedEvents.accept(abortedBy === undefined ? event : { ...event, abortedBy }); return; } correlatedEvents.accept(event); }); } //# sourceMappingURL=session-record-stream.js.map