@sema-agent/core
Version:
Stateless, task-oriented AI agent core
88 lines (87 loc) • 5.56 kB
TypeScript
import type { ClassicEventBase } from "../hooks.js";
import { type EngineNotice } from "../engine-notice.js";
import type { QueueSlot } from "../push-queue.js";
import type { RunEnd } from "../runner/end-of-run.js";
import type { TaskEvent, TaskEventIdentity } from "../task-event.js";
import { type LegEvents, type LegModBus } from "./stations.js";
/** The station's pace (CC 2.1.316 `VTs`, cli316.js:288153: a raise every 100 ms, three in flight, a window's ten seconds). */
export declare const MESSAGE_DISPLAY_PACE: Readonly<{
intervalMs: 100;
inFlight: 3;
windowMs: 10000;
}>;
/**
* Where the settled text of `text` ends, reading from `from`: after its last line end, or at the start of the last sentence the
* runtime reads in what follows `from` (every sentence before it is complete), whichever is later; `from` when nothing settled.
*/
export declare function settledEnd(text: string, from: number): number;
/** Why a `classic.MessageDisplay` answer cannot be taken, or undefined. */
export declare function displayAnswerViolation(answer: unknown): string | undefined;
/**
* A top-level leg whose bus hangs a `classic.MessageDisplay` handler, in a runtime that has no `Intl.Segmenter` (a node built
* without ICU): the station's window edge is a sentence end, so it cannot run there — the leg is refused, `config.mods_invalid`,
* rather than run with another edge than the one its handlers were written for.
*/
export declare function refuseDisplayWithoutSentences(bus: Pick<LegModBus, "hasHandlers">, legEvents: LegEvents, sentencesReadable?: boolean): void;
/** The station's clock: wall time and one-shot timers (a pin drives a virtual one). */
export interface DisplayClock {
now(): number;
/** Run `fn` after `ms`; the return cancels it. */
after(ms: number, fn: () => void): () => void;
}
/** What the leg hands the station. */
export interface MessageDisplayInput {
readonly bus: LegModBus;
/** The leg's two ends and its display — the station runs on a top-level leg only. */
readonly legEvents: LegEvents;
/** The members every classic `e` of the leg carries, read when a window is raised. */
readonly base: () => ClassicEventBase;
/** The model turn a message that starts now belongs to. */
readonly turn: () => number;
/** The leg's content push: a message no handler hears goes straight through it. */
readonly push: (e: TaskEvent) => void;
/** A place in the leg's stream at the present point (`undefined` once the stream is closed). */
readonly reserve: () => QueueSlot<TaskEvent> | undefined;
readonly ident: () => TaskEventIdentity;
/** The caller's stop: once it fires, nothing more of any message is shown (and nothing is said of what is not). */
readonly signal: AbortSignal;
/** The run's end (runner/end-of-run.ts): the station hears `signal` through it, until then — the caller's stop outlives the run. */
readonly runEnd: Pick<RunEnd, "listen">;
/** The leg's notice sink (`RunnerDeps.onNotice`): an answer that did not stand is said through the engine's one notice throat. */
readonly onNotice: ((notice: EngineNotice) => void) | undefined;
readonly sessionId: string;
/** The id each `e` names its message by (absent: a random UUID per message). */
readonly mintId?: () => string;
readonly clock?: DisplayClock;
}
/** What the station showed of a message's text, as the result reads it. */
export interface DisplayedText {
readonly text: string;
/** The extensions whose answers made the shown text differ from the streamed text, in the order they first did (or none). */
readonly by: readonly string[];
/** `true` when a message was left short: the display ended (the caller's stop, its own fault) with non-blank model text never shown
* — past the last shown window, arrived by then or after (a delta, a close's surplus). The text is then the part shown by then. */
readonly inPart?: true;
}
/** The leg's display: the stream writes into it, the turn boundary and the settle wait on it, the result reads it. */
export interface MessageDisplay {
/** An assistant message started streaming. `whole`: its text arrives whole (an answer given before the model was asked, not
* streamed) — it is raised once, at its end, as its one `final` window. */
begin(options?: {
readonly whole?: boolean;
}): void;
/** A (non-empty) text delta of the streaming message. */
text(delta: string): void;
/** The brain closed a text block whose full text is `content` (non-empty). */
blockEnd(content: string): void;
/** The streaming message ended (`entryId`: its committed entry, when it was committed). */
end(entryId: string | undefined): void;
/** Resolves once every window raised so far is shown (a message still streaming is ended first); `undefined` when
* no message is waited on, so a caller with nothing to wait for takes no await. */
settled(): Promise<void> | undefined;
/** What was shown of the committed messages `entryIds` names, read after {@link settled}, when a handler changed it or the
* caller's stop ended one of them short (the rest is never shown); else undefined (the result is the stored text). */
displayedOf(entryIds: readonly string[] | undefined): DisplayedText | undefined;
}
/** Open the leg's display, or `undefined` on a delegated leg (it raises no `classic.MessageDisplay`). */
export declare function openMessageDisplay(input: MessageDisplayInput): MessageDisplay | undefined;