UNPKG

@sema-agent/core

Version:

Stateless, task-oriented AI agent core

157 lines (156 loc) • 9.63 kB
import { type ModBusSide, type ModEventName, type ModSeat } from "./events.js"; import { type ModAdmissionRecord, type ModDollarBase, type ModOrigin, type ModSeats } from "./registration.js"; import { type ModAnswerCheck, type ModAnswerer, type ModAnswerRanks, type ModEventPayload, type ModFailureCause, type ModMatcher, type ModSkip, type ProxyProbe } from "./site-rules.js"; /** A frame on the dispatch stack: a registration's own `$` call, or one of its handlers running — on `event` (a handler may hang * on several events; the frame says which one it is running on, every frame from its mint, {@link handlerFrame}). */ export type ModFrame = { readonly kind: "dollar"; readonly plugin: string; } | { readonly kind: "handler"; readonly plugin: string; readonly handler: number; readonly event: ModEventName; }; /** What a chain bottom (the core of an event) receives besides `e`. */ export interface ModBottomContext { readonly frames: readonly ModFrame[]; readonly signal: AbortSignal; readonly origin: ModOrigin; } /** A `$` method's chain bottom: the host's seat behind it. Its throw is the caller's throw. */ export type ModMethodBottom = (args: ModEventPayload, ctx: ModBottomContext) => unknown; /** * A `$` method on a bus: `run` is its chain bottom; `enter`, when the method has one, shapes the caller's (received) * arguments into the event payload the chain sees — the one place a method's argument form is normalised before any * handler reads it. Either one's throw is the caller's throw. */ export interface ModMethodSeat { readonly run: ModMethodBottom; readonly enter?: (args: ModEventPayload) => ModEventPayload; } export type ModMethodBottoms = Partial<Record<ModEventName, ModMethodSeat>>; /** * A callable event's host entry: what a `$` call of the event reaches. It runs whatever stands before the event and * raises the event once at its own station, under `ctx.frames` and `ctx.origin` (so the caller's own handlers stay out * of that raise); its answer is the call's answer (a `{ deny }` is returned, not thrown) and its throw the caller's. */ export type ModCallEntry = (args: ModEventPayload, ctx: ModBottomContext) => unknown; export type ModCallEntries = Partial<Record<ModEventName, ModCallEntry>>; export type ModDispatchOutcome = { readonly kind: "answered"; readonly answer: unknown; readonly answeredBy?: string; readonly skipped: readonly ModSkip[]; } /** `bottomStarted`: the chain's bottom had started when the handler failed — on a `refuse` event, what it did happened * (a `refuse_until_run` event never fails once the failing handler's own `next` started it: that handler is skipped). */ | { readonly kind: "failed"; readonly registration: string; readonly cause: ModFailureCause; readonly reason: string; readonly skipped: readonly ModSkip[]; readonly bottomStarted?: true; } /** A cancelled dispatch, with what it had reached: `skipped` — the handlers skipped before the cancel (the same record an * answered or a failed dispatch carries); `lastAnswer` — the answer the highest handler whose `next` had answered was handed * (absent when no handler's `next` had answered), with the extension it came from, as `answeredBy` names it on an answered * dispatch. A station that shows what stands of a cancelled chain reads the cancel as a skip of every handler that had not * answered — a handler skipped after its `next` answered keeps that answer — and a station that takes nothing from a cancel * reads its kind alone. `bottomStarted`: the chain's bottom had started when the dispatch was cancelled — on an effect event, * what it did is not known from here. */ | { readonly kind: "aborted"; readonly skipped: readonly ModSkip[]; readonly lastAnswer?: { readonly answer: unknown; readonly answeredBy?: string; }; readonly bottomStarted?: true; }; /** What a participant filter sees of one mounted handler: its registration, that registration's seat, and whether it only observes. */ export interface ModParticipant { readonly plugin: string; readonly seat: ModSeat; readonly observe: boolean; } export interface ModDispatchOptions { /** The core of the event: what the engine (or shell) does when the chain reaches the bottom. */ readonly bottom: (e: ModEventPayload, ctx: ModBottomContext) => unknown; readonly signal?: AbortSignal; /** The frames the event is raised under (a chain bottom hands its own `ctx.frames` on). */ readonly frames?: readonly ModFrame[]; readonly origin?: ModOrigin; /** The raiser's reading of an answer; an answer it refuses is a violation of the site. */ readonly checkAnswer?: ModAnswerCheck; /** The raiser's reading of what a handler hands on (a payload other than the dispatch's own `e`, received, its pinned keys * judged), with the handler that hands it on; a payload it refuses is a violation of the site — the other direction of * {@link checkAnswer}. */ readonly checkHandOn?: (payload: ModEventPayload, by: ModAnswerer) => string | undefined; /** The station's ranking of its answers — named by every dispatch of an event whose failure column refuses (one that * names none is not run), and read by the seat-trust law. */ readonly ranks?: ModAnswerRanks; /** Which mounted handlers take part (absent ⇒ every handler on the event). A handler it leaves out neither runs nor observes. */ readonly participant?: (p: ModParticipant) => boolean; /** The owning plugin of an event whose row is `ownerOnly` (required there; delivery is that plugin's handlers only); not read * on any other event. */ readonly owner?: string; } /** One handler as admission hung it — what a registration's `register` mounted, never its code. */ export interface ModMountedHandler { readonly plugin: string; readonly seat: ModSeat; readonly pattern: string; readonly matcher?: ModMatcher; readonly observe: boolean; /** Whether it was hung with a `.catch`. */ readonly caught: boolean; } export interface ModBus { readonly records: readonly ModAdmissionRecord[]; /** Every mounted handler, in mount order (registrations in run order, each in the order its `register` hung them). */ mounted(): readonly ModMountedHandler[]; /** Whether any mounted handler hangs on `event` (among those `participant` admits) — no handler, no dispatch (the station skips the call). */ hasHandlers(event: ModEventName, participant?: (p: ModParticipant) => boolean): boolean; /** Raise `event` with the raiser's `e` (an object the bus receives whole, an event map's `e` as it is). */ dispatch(event: ModEventName, e: object, options: ModDispatchOptions): Promise<ModDispatchOutcome>; } export interface ModBusInput<D extends ModDollarBase = ModDollarBase> { readonly side: ModBusSide; readonly seats: ModSeats<D>; readonly bottoms: ModMethodBottoms; /** The host's entries of the callable events its bus raises (absent ⇒ none: a `$` call of such an event throws). */ readonly entries?: ModCallEntries; /** Where handler failures go (the host's error channel). Never throws back into the bus. */ readonly report: (err: Error, classification?: "observational-hook-verdict-ignored") => void; /** The host's one rule for turning a THROWN value into text (a handler, a `.catch` or a `register` may throw * anything). The bus has no rendering of its own: every failure reason it writes names the value this way. */ readonly describeThrown: (err: unknown) => string; /** The host's Proxy probe: a Proxy a handler answers or hands on is refused before any of its traps runs (a violation * of the site). Absent ⇒ a Proxy is read like any object, its traps run by the copy. */ readonly isProxy?: ProxyProbe; /** Cancels the admission (`plugin.register`'s dispatches) — nothing after it. */ readonly signal?: AbortSignal; /** * The bus's lifetime (absent ⇒ it lives as long as its host keeps it). What a registration starts that outlives its * handler's own run lives only within it: a timer (`$.clock.after`, `$.clock.every`) is cancelled when it aborts, and a * `$` call made after the handler settled is made under it — and refused once it has aborted. */ readonly lifetime?: AbortSignal; /** The host's reading of a handler's matcher, taken once when the handler is hung (absent ⇒ as written): a host whose events name * one thing by more than one spelling hands back the spelling its events carry. The engine reads a retired tool name in a `tool` * field as the tool its leg reads it as (#1781). The matcher matches the event's fields exactly, as ever. */ readonly readMatcher?: (matcher: ModMatcher) => ModMatcher; } /** What `$.clock.after` and `$.clock.every` hand back: `cancel()` stops the timer (no tick or callback after it; a second call does nothing). */ export interface ModTimer { cancel(): void; } /** * Screen both seats, then admit registrations one by one in run order: each is put to the * `plugin.register` handlers of the registrations already admitted; a refused one is never registered * (its register is not run on this bus; a host may have run code of it before — a reference-form package's register ran once, * at its load). Throws `config.mods_invalid` for a registration the kernel cannot take. */ export declare function admitMods<D extends ModDollarBase = ModDollarBase>(input: ModBusInput<D>): Promise<ModBus>;