UNPKG

@sema-agent/core

Version:

Stateless, task-oriented AI agent core

205 lines (204 loc) • 12.2 kB
/** * The registration form, the two seats, and the screen every registration passes before admission: * shape, unique name, the seat × tier table, the `uses` declaration, and the `budgetMs` fallback. */ import { type ModBusSide, type ModEventName, type ModRegistrationTier, type ModSeat, type ModTier } from "./events.js"; import { type ModBudgetReading } from "./budget.js"; import type { ModParticipant } from "./dispatch.js"; import type { ModEventPayload, ModFailureCause, ModMatcher, ModSkip } from "./site-rules.js"; /** The configuration refusal: a registration the kernel cannot take refuses the whole prepare. */ export declare const MODS_INVALID_CODE = "config.mods_invalid"; /** The notice an admission refusal of a task-seat registration becomes (the run goes on without the refused registration). */ export declare const MODS_REGISTRATION_REFUSED_CODE = "mods.registration_refused"; /** The notice an admission refusal of a deployment-seat registration becomes: the same fact, the operator's. */ export declare const MODS_REGISTRATION_REFUSED_DEPLOYMENT_CODE = "mods.registration_refused_deployment"; /** What a registration declares it will touch — the upper bound the kernel holds it to. */ export interface ModUses { /** Events it hangs handlers on (names or `<prefix>.*`, of its bus's side). */ readonly events: readonly string[]; /** The `$` members it calls — methods and callable events (names or `<prefix>.*`, of its bus's side). */ readonly calls: readonly string[]; } /** Who raised a dispatch: a registration (through its `$`), or the engine (`{plugin: "engine", tier: "core"}`). */ export interface ModOrigin { readonly plugin: string; readonly tier: ModTier; } /** The engine's own origin; its `plugin` word is reserved (no registration may take it). */ export declare const ENGINE_ORIGIN: ModOrigin; /** The form of a registration name: 1–64 of letters, digits, `.`, `_`, `:` and `-` (`:` is the separator of the `skill:<name>` spelling a skill's registrations use). The one rule — a name reaches sentences a person or * a model reads (`extension "<name>"`), so a name that could quote, bracket or fold a sentence is refused where it enters. */ export declare const MOD_REGISTRATION_NAME: RegExp; /** The extension a refusal names: the registration's own name — never the engine's origin word (a raise the engine could not run * names no extension), never an absent one. The one reading of "who refused" for every station that refuses a call at the hook * layer (`GateDisposition.extension`). Module-level: not part of the kernel's entry. */ export declare function namedRegistration(registration: string | undefined): string | undefined; /** The facts `$.plugin` carries. */ export interface ModPluginFacts { readonly name: string; } /** The part of `$` every bus gives; each side adds its methods (`$.<noun>.<method>`). */ export interface ModDollarBase { readonly plugin: ModPluginFacts; } /** One entry of `next.trace`: a handler (or the core) that ran beneath, and how it ended. */ export interface ModTraceEntry { readonly index: number; readonly plugin: string; readonly tier: ModTier; readonly outcome: ModTraceOutcome; readonly ms: number; } /** How a chain member ended (CC words). */ export declare const MOD_TRACE_OUTCOMES: readonly ["returned", "passed", "rejected", "caught", "expired", "kept", "skipped", "bypassed"]; export type ModTraceOutcome = (typeof MOD_TRACE_OUTCOMES)[number]; /** What a `.catch` reads off `next.error`. */ export interface ModCaughtError { readonly kind: ModFailureCause; readonly message?: string; readonly budget: number; } /** The third argument of a handler: continue the chain, or jump (prepend / append only). */ export interface ModNext { (e: ModEventPayload): Promise<unknown>; to(e: ModEventPayload, tier: ModTier): Promise<unknown>; readonly origin: ModOrigin; readonly signal: AbortSignal; /** What ran beneath this handler's last `next`. */ readonly trace: readonly ModTraceEntry[]; /** * Who stands in this dispatch's chain beneath this handler, in chain order — the chain as the dispatch assembled it, before * anything ran (`trace` records those the run reached). A handler a matcher leaves out of the event is not a member; one the * dispatch re-entered (asked through its `.catch`) is. An observer is outside the chain — it ran before it, detached — so it * is beneath no one, and an observer's own `below` is empty. Always present and frozen; empty when nothing stands beneath. */ readonly below: readonly ModParticipant[]; readonly budget: ModBudgetReading; /** Present on a `.catch`'s `next` only. */ readonly error?: ModCaughtError; /** Present on a `.catch`'s `next` only: whether the failed handler had called `next`. */ readonly called?: boolean; } export type ModHandler<D extends ModDollarBase = ModDollarBase> = ($: D, e: ModEventPayload, next: ModNext) => unknown; export interface ModHandlerOptions<D extends ModDollarBase = ModDollarBase> { /** Answers for the handler when it throws, runs out of budget or breaks the site's contract. */ readonly catch?: ModHandler<D>; /** Watch only: a detached `e`, outside the chain, its answer refused and disclosed. */ readonly observe?: boolean; } /** One event's `e` and answer form, as an event map pairs them. */ export interface ModEventPair { readonly e: object; readonly answer: unknown; } /** A side's typing of its events: each event name with its `e` and its answer form (the engine's is `ClassicEventMap`). */ export type ModEventMap = { readonly [name: string]: ModEventPair; }; /** `next` as a handler of a mapped event sees it: it takes that event's `e` and resolves with its answer form. */ export interface ModTypedNext<P extends ModEventPair> { (e: P["e"]): Promise<P["answer"]>; to(e: P["e"], tier: ModTier): Promise<P["answer"]>; readonly origin: ModOrigin; readonly signal: AbortSignal; readonly trace: readonly ModTraceEntry[]; readonly below: readonly ModParticipant[]; readonly budget: ModBudgetReading; readonly error?: ModCaughtError; readonly called?: boolean; } /** A handler of a mapped event: its `e` and its answer typed by the map. */ export type ModTypedHandler<D extends ModDollarBase, P extends ModEventPair> = ($: D, e: P["e"], next: ModTypedNext<P>) => P["answer"] | Promise<P["answer"]>; /** `on(pattern, handler, options?)` or `on(pattern, matcher, handler, options?)`. */ export interface On<D extends ModDollarBase = ModDollarBase> { (pattern: string, handler: ModHandler<D>, options?: ModHandlerOptions<D>): void; (pattern: string, matcher: ModMatcher, handler: ModHandler<D>, options?: ModHandlerOptions<D>): void; } /** {@link On} given its side's event map `M`: a pattern that is one of the map's names types its handler by that event; * any other pattern (a `<prefix>.*`, a name the map does not pair) takes the untyped handler. A side types its own * registrations by declaring `register(on: MappedOn<D, M>, options)` — they stay registrations of the kernel's form. */ export interface MappedOn<D extends ModDollarBase, M extends ModEventMap> { <K extends keyof M & string>(pattern: K, handler: ModTypedHandler<D, M[K]>, options?: ModHandlerOptions<D>): void; <K extends keyof M & string>(pattern: K, matcher: ModMatcher, handler: ModTypedHandler<D, M[K]>, options?: ModHandlerOptions<D>): void; (pattern: string, handler: ModHandler<D>, options?: ModHandlerOptions<D>): void; (pattern: string, matcher: ModMatcher, handler: ModHandler<D>, options?: ModHandlerOptions<D>): void; } export interface ModRegistration<D extends ModDollarBase = ModDollarBase> { /** Unique in the run; names the registration everywhere (origin, trace, disclosures, admission). */ readonly name: string; /** Deployment seat: required. Task seat: absent or `user`. */ readonly tier?: ModRegistrationTier; readonly uses: ModUses; /** The host's provenance label, handed to admission as given: one line of 1 to 256 characters, with no control character or * line break — any other value refuses the prepare (`config.mods_invalid`), never cut or cleaned. */ readonly provenance?: string; /** Each handler's own time; default 10 000 ms. */ readonly budgetMs?: number; /** The host's word that a failure of this registration's handlers is skipped on every station — a station whose failure * column refuses reads it as `skip` for these handlers alone (its `.catch` first, then what stands below). Absent: each * station's own column. Any other value refuses the prepare (`config.mods_invalid`). */ readonly failure?: "skip"; readonly options?: unknown; register(on: On<D>, options: unknown): void; } /** One admission result: mounted (with the patterns it actually hung, and among them the ones on which every handler it * hung watches) or refused (by whom, why). `provenance` is the registration's own label, carried as the host gave it on * either arm — absent when the host gave none. */ export type ModAdmissionRecord = { readonly name: string; readonly seat: ModSeat; readonly tier: ModTier; readonly provenance?: string; readonly events: readonly string[]; readonly observe?: readonly string[]; /** Judges skipped while this registration was judged (their failure or site violation), as the dispatch listed them. */ readonly skipped?: readonly ModSkip[]; } | { readonly name: string; readonly seat: ModSeat; readonly tier: ModTier; readonly provenance?: string; readonly refusedBy: string; readonly reason: string; }; /** The two seats as a bus receives them; a seat is a list of registrations or absent. Screened as untrusted input. */ export interface ModSeats<D extends ModDollarBase = ModDollarBase> { readonly deployment?: readonly ModRegistration<D>[]; readonly task?: readonly ModRegistration<D>[]; } /** A registration that passed the screen, in run order. */ export interface ScreenedRegistration<D extends ModDollarBase = ModDollarBase> { readonly registration: ModRegistration<D>; readonly name: string; readonly seat: ModSeat; readonly tier: ModRegistrationTier; readonly budgetMs: number; /** `failure: "skip"` read once at the screen: this registration's handler failures skip on every station. */ readonly failureSkips: boolean; readonly uses: ModUses; readonly provenance?: string; readonly eventsBound: ReadonlySet<ModEventName>; readonly callsBound: ReadonlySet<ModEventName>; } export declare function modsInvalid(message: string): Error & { readonly code: string; }; /** * Screen both seats and put the registrations in run order: by tier, then deployment before task, then * each seat's own order. Throws `config.mods_invalid` for anything the kernel cannot take; a bad * `budgetMs` is reported and replaced by the default instead. */ export declare function screenModSeats<D extends ModDollarBase>(seats: ModSeats<D>, side: ModBusSide, report: (err: Error) => void): ScreenedRegistration<D>[]; /** Whether a leg's registrations reach its execution environment on any leg (`always`) and on a leg that can call a tool * (`onCall`); `undefined` = open (a declaration that cannot be read), unless one already answered yes. */ export interface ModEnvReach { readonly always: boolean | undefined; readonly onCall: boolean | undefined; } /** * Do the seats' registrations reach the execution environment ({@link envReach}): a method in `uses.calls` whose bottom runs * on it, an event in `uses.events` whose payload states what it holds (on any leg), or a tool-call event whose payload hands * its handlers its face (on a leg that can call a tool)? Read with the screen's own readers, before the seats are admitted * (the kernel refuses a call or a subscription a registration did not declare, so the declarations bound what it can reach). */ export declare function seatsReachEnv(seats: ModSeats, side: ModBusSide): ModEnvReach;