UNPKG

@sema-agent/core

Version:

Stateless, task-oriented AI agent core

94 lines (93 loc) • 5.91 kB
/** * The `session.receive` station: the session's one receiving station ({@link FrameReceiver}). * * THE RULE. A frame reaches the model by one of three faces — PUSHED on the live lane (a producer's injection: a task's * notification, a teammate's message, a host's `notify`; a frame routed in from another run of the session; a peer letter * the box drain injects), REDELIVERED at a run's turn open (what was parked while the session sat idle), or PULLED by * `ReadInbox` — and every face hands it to this station before the hand-over, once, and hands over what it answers. With * a handler on `session.receive` the station raises the event; with none, or before the leg's bus is bound, the frame * passes as made and nothing is raised (each face keeps its own synchronous path). * * `e = { origin, event, text, agentId? }` plus the identity envelope; `origin`, `event` and `agentId` are pinned. `origin` * is read off the frame's own typed facts ({@link receiveOriginOf}); `event` names it (`taskId`, `taskType`, `status`, * `seq`); `text` is what the model would read — the frame as the engine renders it. * * A handler answers (the reference's forms: `Du` (cli309.js:255554)): * - `next(e)` / `next({ ...e, text })` — what comes up is `{ text }`, the text as handed on; a handler answers it, or * `{ text }` of its own — the frame is handed over with the text of the answer that stands; * - `{ consumed: <reason> }` — the frame is not handed over: the model never sees it, and it is not owed again (a peer * letter's sender is sent a `refused` receipt naming the extension). `next` has no effect here, so a consume after it * stands as well. * A text that is not a non-blank string is a violation. * * A failure refuses (the table's column): a handler that throws, runs past its budget or breaks the event's form leaves the * frame consumed — an input station that can keep a frame from the model fails closed. So does an engine fault of the * dispatch. A cancel (the leg is stopping) hands the frame over as made: the face's own lossless path takes it from there. * * What the station does not hold: the host's `task_notification` event for a pushed or redelivered frame is emitted when * the frame arrives, before the station answers — it records the frame as made, and a frame the station then consumed is * on the stream, not in the conversation. */ import type { HookInvocationIdentity } from "../hooks.js"; import { type FrameReceiver, type ReceivedFrame, type TaskNotificationPayload } from "../task-notification.js"; import { type LegModBus } from "./stations.js"; /** Who a frame comes from: read off the frame ({@link receiveOriginOf}). */ export declare const SESSION_RECEIVE_ORIGIN_KINDS: readonly ["task", "agent", "peer", "host", "engine"]; export type SessionReceiveOrigin = { readonly kind: "task"; readonly taskType: TaskNotificationPayload["task_type"]; } | { readonly kind: "agent"; readonly from: string; } | { readonly kind: "peer"; readonly from: string; } | { readonly kind: "host"; readonly from?: string; } | { readonly kind: "engine"; }; /** A frame's origin, from its typed facts: an agent's message (`agentMessage`), another session's letter * (`crossSessionMessage`), a line the engine composed about the lane itself (a delivery or idle notice, the inbox notice), * a host's own injection (the `external` lane, with its attribution label when it carries one), else a task's notification. */ export declare function receiveOriginOf(p: TaskNotificationPayload): SessionReceiveOrigin; /** What the leg binds the station to once its bus is admitted. */ export interface ReceiveSeatBinding { readonly bus: LegModBus; readonly identity: HookInvocationIdentity | undefined; /** The leg's report sink (handler failures and the station's own disclosures). */ readonly report: (err: Error) => void; /** The leg's abort: a dispatch is cancelled with the leg. */ readonly signal?: AbortSignal; } /** The leg's receiving station: a {@link FrameReceiver} bound to the leg's bus once it is admitted. */ export interface ReceiveSeat extends FrameReceiver { bind(binding: ReceiveSeatBinding): void; } /** The leg's receiving station (see the module header). */ export declare function createReceiveSeat(): ReceiveSeat; /** What a face does with one frame's answer: hand over what the station answered, settle a consumed frame, and answer a * link that threw (said, or the frame re-pended — the face's own lossless path). */ export interface ReceivingArms { readonly handOver: (shown: TaskNotificationPayload) => void; readonly consumed?: (verdict: Extract<ReceivedFrame, { kind: "consumed"; }>) => void; readonly fault: (err: unknown) => void; } /** A face that hands frames over one at a time as they come (the live lane, the turn-open redelivery). */ export interface ReceivingChain { /** Hand `payload` over through the station: at once, as made, with no handler listening; else once the station has * answered it and every frame passed before it. */ pass(payload: TaskNotificationPayload, arms: ReceivingArms): void; /** Settles once every frame passed so far has been answered; `undefined` while none has waited on the station. */ readonly settled: Promise<void> | undefined; } /** One chain per face: a frame never overtakes one that arrived before it, and a link that throws does not stop the next. * `seatOf` is read per frame (a face can be wired before the leg's station exists). */ export declare function createReceivingChain(seatOf: () => FrameReceiver): ReceivingChain; /** The sentence a consumed frame's report carries: which frame, by whom, why. */ export declare function consumedFrameReport(payload: TaskNotificationPayload, verdict: Extract<ReceivedFrame, { kind: "consumed"; }>): Error;