@sema-agent/core
Version:
Stateless, task-oriented AI agent core
122 lines (121 loc) • 8.32 kB
TypeScript
import { type ModBudgetReading, type ModEventPayload, type ModMatcher, type ModOrigin, type ModPackageUnreadMember, type ModTier, type ModTraceEntry, type ModUses } from "./kernel/index.js";
/** The largest hooks module phase one imports (the reference's limit of one file). */
export declare const REFERENCE_MODULE_MAX_BYTES: number;
/** The default paths the reference loads a component from without the manifest naming it: folders, and files at the root. */
export declare const REFERENCE_COMPONENT_FOLDERS: readonly string[];
export declare const REFERENCE_COMPONENT_FILES: readonly string[];
/** A reference-form package's manifests, read. */
export interface ReferenceManifests {
/** `plugin.json`'s `name` (the kernel's screen judges its form at admission). */
readonly name: string;
/** The hooks module's path inside the package (`/`-separated). */
readonly modulePath: string;
/** The members of the two manifests phase one does not read (`plugin.json`'s first, each in its parsed object's key order). */
readonly unread: readonly ModPackageUnreadMember[];
}
/** Read a reference-form package's two manifests (`hooksJson` parsed already by the form's judgment): refused, or what it says. */
export declare function readReferenceManifests(plugin: Uint8Array, hooksJson: Readonly<Record<string, unknown>>): ReferenceManifests;
/** A default component folder holding anything: the refusal (row 16). */
export declare function componentFolderRefusal(folder: string): Error;
/** A root component file's bytes: refused unless they configure nothing — empty, blank, or `{}` (row 16). */
export declare function judgeComponentFile(file: string, bytes: Uint8Array): void;
/** The hooks module's bytes: refused above the size phase one imports (row 57). */
export declare function judgeReferenceModule(bytes: Uint8Array): void;
/** The import and the register together have this long (the reference's load limit), the clock started before the import. */
export declare const REFERENCE_LOAD_MS = 10000;
/** One `on` call the package made, as recorded: what replaying it onto a bus starts from. */
export interface ReferenceHook {
readonly pattern: string;
/** The matcher as it stood when `on` was called: an own copy, frozen. Present iff the package gave one. */
readonly matcher?: ModMatcher;
/** The package's own function, as given — never wrapped here. */
readonly hook: ReferenceHookFn;
/** The package's `.catch`, as given. Present iff it gave one. */
readonly catch?: ReferenceHookFn;
}
/**
* A reference-form package recorded at load: what its one run of `register(on, options)` hung, the frozen data it ran with, and
* the facts of its bytes. Frozen, its hooks and their matchers too (the package's functions are its own). Turning it into a
* registration a bus admits — the replay, the projection of `$` and `next`, the handler's wrapper — starts here.
*/
export interface ReferencePackageRecord {
readonly form: "reference";
/** `plugin.json`'s `name`. */
readonly name: string;
/** The package's directory, by its real path. */
readonly root: string;
/** The digest over `plugin.json`, `hooks.json` and the module, as the engine's own form takes it. */
readonly digest: string;
/** The host's options as the package's `register` received them: plain data, frozen all the way down. */
readonly options: unknown;
/** Each `on` call in the order it was made. */
readonly hooks: readonly ReferenceHook[];
/** The declaration read off the record: `events` the patterns hung (each once, in first-hung order), `calls` none. The kernel's
* rule that a declaration names one name of its side's own is not read here: the side is the host's, and a package that hangs
* nothing, or only judges, is a package all the same. */
readonly uses: ModUses;
/** The manifests' members phase one does not read. */
readonly unread: readonly ModPackageUnreadMember[];
}
/** What `recordReferencePackage` is handed: the package read, and the one import of its bytes (it maps its own failure). */
export interface ReferenceLoad {
readonly root: string;
readonly manifests: ReferenceManifests;
readonly digest: string;
/** `referenceOptions(host.options)`. */
readonly options: unknown;
readonly importModule: () => Promise<unknown>;
}
/**
* The host's options as a reference-form package receives them: copied into plain data — records and lists of strings, numbers,
* booleans, null, undefined and bigints — and frozen all the way down, so its `register` holds nothing of the host's to call or
* change. A function, a symbol, an object of another kind (a class instance, a Map, a Proxy), an accessor or a cycle is refused
* (row 56). None given: `{}`, as the reference hands a package without settings.
*/
export declare function referenceOptions(options: unknown): unknown;
/** A failed import of the module, as the sentence names it: a static import it cannot resolve from its own bytes (rows 4, 5), or
* the module's own failure. `said` is the runtime's sentence with the module's URL already replaced by the member's name. */
export declare function referenceImportRefusal(err: unknown, said: string): Error;
/**
* Import a reference-form package and run its `register(on, options)` once against a recorder: its record, or a refusal. The
* import and the register share one clock of {@link REFERENCE_LOAD_MS}, started before the import (a module whose top level never
* settles is refused as one whose register never does). The register is started only while the clock still runs, and its run
* is measured again when it returns (a synchronous register that spends the time holds the clock's callback back). When the
* register's own settling is read back (an `on` it queued as a microtask before returning is taken, as the reference takes it —
* it awaits the call, then marks the plugin registered) or the clock runs out, `on` and every `.catch` not yet used are sealed
* together; the first violation the package made — even one it caught — refuses it then.
*
* A refused package is not undone: its module's top level ran, and whatever its register did before the refusal (a timer it
* started, a value it stored) stays in the process — the engine has no unloading. A register not yet started when the package
* is refused is never started.
*/
export declare function recordReferencePackage(load: ReferenceLoad): Promise<ReferencePackageRecord>;
/** What a reference-form hook's `.catch` reads off its `next.error`: the reference's two kinds. */
export interface ReferenceCaughtError {
readonly kind: "throw" | "timeout";
readonly message?: string;
readonly budget: number;
}
/** The `next` a reference-form hook (or its `.catch`) is handed: the projection of the kernel's (`onceNext`). */
export interface ReferenceNext {
(e: ModEventPayload): Promise<unknown>;
to(e: ModEventPayload, tier: ModTier): Promise<unknown>;
readonly signal: AbortSignal;
readonly origin: ModOrigin;
readonly trace: readonly ModTraceEntry[];
readonly budget: ModBudgetReading;
/** On a `.catch`'s `next` only. */
readonly error?: ReferenceCaughtError;
/** On a `.catch`'s `next` only: whether the failed hook had called `next`. */
readonly called?: boolean;
}
/** A reference-form package's hook, or its `.catch`, as the package wrote it (its own function, typed by what it is handed). */
export type ReferenceHookFn = ($: unknown, e: ModEventPayload, next: ReferenceNext) => unknown;
/**
* The register of a reference-form package's registration: each admission replays what the package's one run of `register`
* recorded at load — never runs the package's register again — after checking it is handed the very options that run received
* (a registration copied with other options is refused). Each hook is hung on the kernel's `on` as the package hung it, through
* the projections of `$` and `next` ({@link referenceMounts}). That a failing hook is skipped on every station is not this
* register's doing: the registration carries `failure: "skip"`, which the kernel reads.
*/
export declare function referenceReplay(record: ReferencePackageRecord): (on: unknown, options: unknown) => void;