UNPKG

@code-yeongyu/senpi

Version:

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

83 lines • 3.56 kB
import { carryAssistantCapture } from "@earendil-works/pi-ai"; export class StructuredLaneUnavailableError extends Error { constructor() { super("Structured lane unavailable: Agent was constructed outside the session layer; construct the session via the " + "session layer, or turn the engine-socket compact-updates flag off"); this.name = "StructuredLaneUnavailableError"; } } /** * The AgentSession end of the internal-only agent channel (senpi#4272, R3.C6.2). While a structured listener is * attached, it holds the channel's capture subscription; block controls enter the session's own event queue, so they * keep their position among the ordinary events. Extension listeners, `session.subscribe` and every legacy * destination never see a control. * * The capture subscription is module-wide (`subscribeAssistantCapture`): while it is held, every capture-enabled * provider stream in the process that first pushes afterwards builds capture records, including streams of sessions * with no structured listener, which drop them at their public iterator. */ export class StructuredSessionLane { #channel; #enqueue; #own; /** `own` binds a block control to the message open when it leaves the session queue (its emission point). */ constructor(channel, enqueue, own = () => { }) { this.listeners = new Map(); this.#channel = channel; this.#enqueue = enqueue; this.#own = own; } get active() { return this.listeners.size > 0; } /** Whether this session's Agent was built with the internal channel, so `subscribe` can succeed. */ get available() { return this.#channel !== undefined; } /** `failed` hears a record this lane could not build; that record is skipped, never the event's legacy delivery. */ subscribe(listener, failed) { if (!this.#channel) throw new StructuredLaneUnavailableError(); this.listeners.set(listener, failed); this.releaseChannel ??= this.#channel.subscribe((record) => { if ("progress" in record) this.#enqueue(() => { this.#own(record.progress); this.emit(record.progress); }); }); return () => { this.listeners.delete(listener); if (this.listeners.size === 0) this.dispose(); }; } /** Publishes a session event with the provider capture of its `source` agent event. */ emitEvent(event, source) { const message = (event.type === "message_start" || event.type === "message_end") && event.message.role === "assistant" ? event.message : undefined; // A user-facing description replaced the engine message, so the capture's frozen message must follow it. const described = message && "message" in source && source.message !== message ? message : undefined; let record; try { record = carryAssistantCapture(source, event, described); } catch (cause) { for (const failed of [...this.listeners.values()]) failed?.(cause); return; } this.emit(record); } dispose() { this.listeners.clear(); this.releaseChannel?.(); this.releaseChannel = undefined; } emit(record) { for (const listener of [...this.listeners.keys()]) listener(record); } } //# sourceMappingURL=structured-session-lane.js.map