@sema-agent/core
Version:
Stateless, task-oriented AI agent core
73 lines (72 loc) • 4.3 kB
TypeScript
/**
* The `session.send` station, and `$.session.send`'s host entry.
*
* THE RULE. A message the leg sends goes out through SendMessage, and SendMessage raises `session.send` once per message,
* after its own pre-delivery refusals (an empty address or message, a broadcast, a qualified name, an observer on either
* end) and before any delivery arm resolves the address — the peer-session arm, the `main` uplink and the agent ladder each
* resolve it their own way, so the one point before all of them is the point every message passes. A pure idle subscription
* (no message) raises nothing. With no handler on the event nothing is raised and the send goes on.
*
* `e = { origin, to, text, agentId? }` plus the identity envelope; every member is pinned — the address and the text are
* what the gate judged, so they are what goes (the reference lets a handler rewrite both: `Bu` (cli309.js:255554)).
* `origin` is `{ kind: "model" }`, or `{ kind: "plugin", name }` for an extension's own `$.session.send`.
*
* A handler answers:
* - `next(e)` — the send goes on: what comes up is the go-ahead (`{ to, text }`), and the answer is that go-ahead;
* - `{ isDelivered: false, reason }` — only in place of `next`: the message is not sent, and the call says so naming the
* extension. `{ isDelivered: true }` — a handler claiming it delivered the message itself — is a violation: the engine is
* the one that delivers (the reference takes it: `qa` (cli309.js:288155)).
* A failure refuses while the go-ahead has not been given, and once it has the send goes on (the table's column): the
* station is a go-ahead station, as `agent.spawn` is.
*
* `$.session.send({ to, text })` is the event called ({@link createSessionSendEntry}): the leg's SendMessage pipeline — the
* same entry `$.tool.call` runs, its gate and its stations — run as the caller's call, which raises `session.send` here
* under the caller's frames. It answers `{ isDelivered: true }` when SendMessage reports the message sent, else
* `{ isDelivered: false, reason }` (returned, not thrown); a session with no SendMessage mounted answers the latter at once.
*/
import type { HookInvocationIdentity } from "../hooks.js";
import { type ModCallEntry } from "./kernel/index.js";
import { type LegModBus } from "./stations.js";
/** Who sends a message: the model (its SendMessage call), or an extension's own `$.session.send`. */
export declare const SESSION_SEND_ORIGIN_KINDS: readonly ["model", "plugin"];
export type SessionSendOrigin = {
readonly kind: "model";
} | {
readonly kind: "plugin";
readonly name: string;
};
/** What the station answers SendMessage: go on, or the refusal and who gave it. */
export type SendVerdict = {
readonly go: true;
} | {
readonly go: false;
readonly by: string;
readonly reason: string;
};
/** The leg's `session.send` station, as SendMessage reads it (a Runner-filled seat, never a model argument). */
export interface SendStation {
/** Whether a handler hangs on `session.send` (none ⇒ SendMessage raises nothing and awaits nothing). */
listens(): boolean;
/** Raise `session.send` for one message (no handler ⇒ nothing raised: the send goes on). */
send(facts: {
readonly to: string;
readonly text: string;
readonly toolCallId: string;
}, signal: AbortSignal | undefined): Promise<SendVerdict>;
}
export interface SendStationInput {
readonly bus: LegModBus;
/** The sending leg's identity (its `agentId`). */
readonly identity: HookInvocationIdentity | undefined;
}
/** The leg's `session.send` station over its bus. */
export declare function createSessionSendStation(input: SendStationInput): SendStation;
/** What the host entry reaches: the leg's `$.tool.call` entry (its one pipeline for a call of a mounted tool), the name the
* leg's SendMessage is mounted under, and the leg's mounted tools. */
export interface SessionSendEntryInput {
readonly toolCall: ModCallEntry;
readonly sendToolName: string;
readonly tools: () => readonly string[];
}
/** `$.session.send`'s host entry: the leg's SendMessage, run as the caller's call (the module header). */
export declare function createSessionSendEntry(input: SessionSendEntryInput): ModCallEntry;