@sema-agent/core
Version:
Stateless, task-oriented AI agent core
76 lines (75 loc) • 4.53 kB
TypeScript
import type { PeerSendVerdict } from "../agents/cross-session-judge.js";
import type { PermissionModeClass } from "../agents/cross-session-envelope.js";
/** The listening session: whose box an inbound frame lands in, and the host's hooks. */
export interface PeerChannelSelf {
/** This session's id — its box is `session.<sessionId>`; a frame addressed to another id is not appended. */
sessionId: string;
/** The mailbox scope this session's box lives in. */
scope: string;
/** The occupant of this session's row — its `SessionMetadata.incarnation` — or `null` when the row carries no token. REQUIRED,
* `null` included: every frame the listener appends is stamped for it (`MailboxPeerMeta.occupant`), the session's drain hands a
* record only to the occupant it names, and the published registration row carries it so a peer of this engine addresses this
* session's box the same way. An addressing fact, never a credential. */
occupant: string | null;
/** The doorbell rang (a peer appended to this session's box). The HOST decides what to do with it —
* start a turn now, or let the next boundary drain; the engine never starts a turn on a frame. */
onNudge?: () => void;
/** One event per refused connection, dropped frame or appended frame (the channel's account — a host
* logs or counts it). Never thrown into the listener. */
onEvent?: (event: PeerChannelEvent) => void;
}
/**
* Why the listener dropped a connection or a frame (closed set):
* - `first_line_deadline` — no complete line within the first-line deadline (the connection closes);
* - `line_too_long` — a line grew past the line wall before its newline (the connection closes);
* - `unauthenticated` — the first line was not an auth frame, or was blank / not JSON (the connection closes);
* - `bad_token` — the auth frame's token is not this session's (the connection closes);
* - `malformed_line` — an authenticated line that is not JSON, not an object, or a message without text;
* - `session_mismatch` — a message addressed to another session id;
* - `box_full` — this session's box is at its queued-message bound, so the message was not appended;
* - `unhandled_frame` — a frame type, control action or attachment this engine does not take over the socket;
* - `append_failed` — the box refused the append (the store's own error).
*/
export declare const PEER_CHANNEL_DROP_REASONS: readonly ["first_line_deadline", "line_too_long", "unauthenticated", "bad_token", "malformed_line", "session_mismatch", "box_full", "unhandled_frame", "append_failed"];
export type PeerChannelDropReason = (typeof PEER_CHANNEL_DROP_REASONS)[number];
export type PeerChannelEvent = {
kind: "dropped";
reason: PeerChannelDropReason;
detail?: string;
} | {
kind: "appended";
seq: number;
} | {
kind: "nudged";
};
/** A bound listener. */
export interface PeerChannelListener {
/** The address peers reach this session at (the socket path). */
readonly address: string;
/** Stop listening and withdraw everything `listen` published. */
close(): Promise<void>;
}
/** One outbound message (the reference agent's dialect is the channel's business). */
export interface PeerChannelMessage {
/** The message text. */
body: string;
/** The sender's session id (self-claimed on the wire). */
fromSession?: string;
/** The sender's display name. */
fromName?: string;
/** The sender's self-attested permission-mode class. */
fromMode?: PermissionModeClass;
/** The prospective forwarding chain (this sender's own token already appended). */
hopChain: readonly string[];
}
/** The transport contract. A host wires one implementation; the engine dials and rings through it. */
export interface PeerChannel {
/** Bind this session's inbound address and publish whatever peers need to reach it. */
listen(self: PeerChannelSelf): Promise<PeerChannelListener>;
/** Carry one message to a reference-agent session at `address`. Never throws: every outcome is a verdict
* (`via: "uds"` on success; the `peer_send.*` transport codes otherwise). */
dial(address: string, message: PeerChannelMessage): Promise<PeerSendVerdict>;
/** Ring the doorbell of a session of this engine at `address`. Best-effort: `false` when the frame did not
* go out; never throws — the message is already in the recipient's box either way. */
nudge(address: string): Promise<boolean>;
}