@sema-agent/core
Version:
Stateless, task-oriented AI agent core
89 lines (88 loc) • 5.56 kB
TypeScript
/**
* A leg's wait for its MCP attempts, and the servers it did not wait for. The wait itself is the materialization's (mcp.ts, THE WAIT): each
* attempt until it settles — or, under the host's budget (`RunnerDeps.mcpFirstRequestWaitMs`, read by `mcpFirstRequestWaitMs`), at most that
* or its startup bound — and the person hears whom it waits for once it has lasted {@link MCP_CONNECTING_NOTICE_AFTER_MS}. An attempt still
* in the air when the wait ends reads `pending` and LANDS later through the leg's {@link McpPendingFace}: the run takes each landing and puts
* it on the live roster through the lane a re-dial's result takes (runner/mcp-redial.ts `takeLateMcpLandings`).
*/
import type { AgentTool } from "../internal/harness.js";
import type { McpDroppedTool, McpServerStatus, McpToolAxis } from "./mcp.js";
/** How long a leg's wait for its MCP attempts must last before it tells the person whom it waits for (`mcp.server_connecting`). */
export declare const MCP_CONNECTING_NOTICE_AFTER_MS = 2000;
/** A leg's wait for its MCP attempts (the runner's; the library entrance and the probe wait for every attempt and tell nothing). `budgetMs`:
* the host's `mcpFirstRequestWaitMs` as its reader answered it — each attempt is waited for at most this or its startup bound, whichever
* is less; absent ⇒ until it settles. `connecting`: told of each server whose attempt is still in the air once the wait has lasted
* {@link MCP_CONNECTING_NOTICE_AFTER_MS} (once: the wait is the leg's, and so is the fact). */
export interface McpLegWait {
budgetMs?: number;
connecting?: {
tell(server: string, waitedMs: number, boundMs: number): void;
};
/** Told of a failure a late attempt met once no run takes its landing (the leg ended, or was disposed): the operator and the person hear
* it then, as prepare tells a failure met within the wait — a failure the pool keeps is answered from memory, silently, next message. */
unheard?(warning: Error, status: McpServerStatus): void;
}
/** Wait for `p` at most `ms`: resolves when it settles (either way) or when the time is up, whichever is first. */
export declare function settledWithin(p: Promise<unknown>, ms: number): Promise<void>;
/** One server's attempt, landed: what the live roster lane applies (connected: its tools, as a re-dial's are) or tells (failed). */
export interface McpLanding {
server: string;
prefix: string;
outcome: "connected" | "failed";
status: McpServerStatus;
tools: AgentTool[];
axes: McpToolAxis[];
dropped: McpDroppedTool[];
/** The operator lines its landing raised (an unavailable server's, an incomplete listing's, a schema advisory's). */
warnings: Error[];
/** On the first landing of a server whose resources the leg may list or read: the three global resource tools, mounted after every server's domain. */
resources?: {
tools: AgentTool[];
axes: McpToolAxis[];
};
}
/**
* The leg's MCP attempts its wait budget did not wait for. A landing is held until the run opens the face (once the leg's roster is
* published); from then on each is handed over as its attempt settles, in settle order, one at a time. One subscriber.
*/
export interface McpPendingFace {
/** The servers whose attempt has not landed yet, in declaration order. */
names(): string[];
/** Who takes the landings. `land` may be async; a landing counts as landed once it resolved. Nothing is handed over before {@link open}. */
subscribe(land: (landing: McpLanding) => void | Promise<void>): void;
/** Start handing landings over: the held ones now, each later one as it settles (the run calls it once the leg's roster is published). */
open(): void;
/** Resolves once each of `servers` (every pending server when absent) has landed, or after `ms` (absent: the longest startup bound
* among them — an attempt the dial does not end within its bound is waited for no longer), or on `signal` — whichever comes first. */
settled(opts?: {
servers?: readonly string[];
ms?: number;
signal?: AbortSignal;
}): Promise<void>;
}
/** One attempt the wait did not wait for, as the materialization hands it over. */
export interface LateAttempt<S> {
name: string;
/** Its startup bound. */
boundMs: number;
/** Its place in the leg's declarations. */
declaredAt: number;
attempt: Promise<S | null>;
}
/** What the materialization does with a late attempt. `taken` — may the leg still take its landing (false once the leg ended or its
* materialization is disposed). `connected` makes
* the server live and answers its landing; `failed` answers a failure's (undefined: the run's own stop, which is no server's answer);
* both are called when the landing is handed over, so the record reads `pending` until then. `close` lets a connection nobody will take
* go; `unheard` tells a failure that ended no run's wait. */
export interface LateHooks<S> {
taken(late: LateAttempt<S>): boolean;
connected(late: LateAttempt<S>, settled: S): McpLanding;
failed(late: LateAttempt<S>, err: unknown): McpLanding | undefined;
close(settled: S): void;
unheard(landing: McpLanding): void;
}
/** The pending face over `late` (in declaration order), and the drop the materialization's dispose calls (every landing still held is let go). */
export declare function pendingFaceOf<S>(late: ReadonlyArray<LateAttempt<S>>, hooks: LateHooks<S>): {
face: McpPendingFace;
dropHeld: () => void;
};