@sema-agent/core
Version:
Stateless, task-oriented AI agent core
119 lines (118 loc) • 7.92 kB
TypeScript
/**
* The delegation's two stations over the delegating leg's bus. `agent.offer`: the agent types the model is offered — ONE set
* ({@link offeredAgents}) read by the listing and by the dispatch; a handler answers `next(e)` or `{ isOffered: false }`.
* `agent.spawn`: raised before a delegated subagent is let start, every member pinned; a handler answers `next(e)` or
* `{ deny: <reason> }` in place of `next`; any other answer, a changed member, a throw or a timeout refuses while the
* subagent has not started, after which the spawn stands. A revival and an observer leg raise no spawn.
*
* `$.agent.spawn({ prompt, ... })` is `agent.spawn` called (the table's `call` column): the host entry
* ({@link createAgentSpawnEntry}) runs the delegation tool's own per-call pipeline, as `$.tool.call` runs any other tool's — the
* whole gate, `tool.call`, the delegation (whose `agent.spawn` station is raised once, at its own place), the post-execution
* stations — as the caller's call, with the subagent in the background. The delegation reads ONE fact of the call
* ({@link AgentStations.extensionSpawn}, the extension call's own record): an extension's spawn is no offer, and it never holds the
* foreground; where the delegation lets the subagent run it writes `{ agentId, model }` on the record, and the entry answers off
* that record alone (never off the call's result, which a post-execution station may withhold after the subagent started).
*/
import { type ModCallEntry } from "./kernel/index.js";
import type { HookInvocationIdentity } from "../hooks.js";
import type { PermissionMode } from "../ask-origin.js";
import { type LegModBus } from "./stations.js";
import type { ExtensionRequester } from "../task-event.js";
import { type ToolCallEntryInput } from "./tool-call.js";
/** One agent type as the model is offered it. */
export interface OfferedAgent {
readonly name: string;
readonly description: string;
}
/**
* The offered set — the ONE reading of "which agent types is the model offered": each type the chain does not answer
* `{ isOffered: false }`, in the order given. A seat nothing hears ({@link seatHears}), no dispatch: the types as given.
* A dispatch the engine itself could not raise offers the type (an observation of the offer is never what hides it).
*/
export declare function offeredAgents<A extends OfferedAgent>(seat: {
readonly bus: LegModBus;
} | undefined, agents: readonly A[], signal: AbortSignal | undefined): Promise<readonly A[]>;
/** A rendered agent listing read through the offered set: the same listing when every type is offered, else a copy with the
* types the chain hid left out — what a listing frame renders, commits and mirrors. */
export declare function offeredAgentListing<L extends {
readonly entries: readonly OfferedAgent[];
}>(seat: {
readonly bus: LegModBus;
} | undefined, listing: L | undefined, signal: AbortSignal | undefined): Promise<L | undefined>;
/** What the delegation decided about the subagent it is about to start, as `agent.spawn` names it. */
export interface AgentSpawnFacts {
/** The delegation call this spawn belongs to. */
readonly toolCallId: string;
readonly prompt: string;
readonly description: string;
/** The resolved type (a definition's name, `general-purpose`, `fork`). */
readonly subagentType: string;
/** The model the subagent starts on, as the delegation settled it; absent when its own prepare decides (a stated tier). */
readonly model?: string;
/** The parent's own model. */
readonly parentModel?: string;
readonly background: boolean;
readonly fork: boolean;
/** The name it is addressed by (a teammate), when the call gave one. */
readonly name?: string;
readonly cwd?: string;
/** A workflow script's agent rather than a model's delegation. */
readonly workflow?: true;
}
/** What `agent.spawn` came to: the subagent may start, or the chain refused it (`by` = the registration). */
export type AgentSpawnVerdict = {
readonly go: true;
} | {
readonly go: false;
readonly by: string;
readonly reason: string;
};
/** The model a started subagent is named by: the one the delegation settled, `inherit` when the child's own prepare decides —
* what `agent.spawn`'s bottom comes up as and what `$.agent.spawn` answers. */
export declare function spawnedModel(model: string | undefined): string;
/** The extension's own spawn a delegation call is ({@link AgentStations.extensionSpawn}): who asked, and the one write of "started". */
export interface ExtensionSpawn {
/** The registration that called `$.agent.spawn`. */
readonly by: string;
/** Who asked, as the registry row and the durable record carry it ({@link ExtensionRequester}): this extension, by its registration name. */
readonly requestedBy: ExtensionRequester;
/** The subagent was let run under `agentId` — its registry task id, which is its leg's task id and so its events' `agentId`;
* `model` is the one the delegation settled (`undefined` ⇒ the child's prepare decides). Written once. */
launched(agentId: string, model: string | undefined): void;
}
/** The delegating leg's two agent stations, as its delegation tools read them (a Runner-filled seat, never a model argument). */
export interface AgentStations {
/** Whether a handler hangs on the event — a caller with nothing to raise reads none (no await on its path). */
listens(event: "agent.offer" | "agent.spawn"): boolean;
/** {@link offeredAgents} over the leg's bus. */
offered<A extends OfferedAgent>(agents: readonly A[], signal: AbortSignal | undefined): Promise<readonly A[]>;
/** Raise `agent.spawn` (no handler ⇒ nothing raised: the subagent may start). */
spawn(facts: AgentSpawnFacts, signal: AbortSignal | undefined): Promise<AgentSpawnVerdict>;
/** The extension's own call (`$.agent.spawn`) the delegation call `toolCallId` is, on this leg — undefined for any other call
* (the model's, or a tool's an extension called with `$.tool.call` that runs the delegation in its body). The one reading of
* "an extension asked for this spawn": read once per call. */
extensionSpawn(toolCallId: string | undefined): ExtensionSpawn | undefined;
}
export interface AgentStationsInput {
readonly bus: LegModBus;
/** The delegating leg's identity (its `agentId` is the spawn's `parentAgentId`). */
readonly identity: HookInvocationIdentity | undefined;
/** The delegating leg's mode as observed at the spawn. */
readonly permissionMode: () => PermissionMode | undefined;
}
/** The leg's agent stations over its bus. */
export declare function createAgentStations(input: AgentStationsInput): AgentStations;
/** What `$.agent.spawn`'s host entry runs a spawn on: the leg's harness (the loop's pipeline under the running prompt), the
* names its live roster mounts, and the tools as the caller handed them (a delegation tool is known by its `execute`). */
export interface AgentSpawnEntryInput extends Pick<ToolCallEntryInput, "bus" | "harness" | "specOf"> {
/** The names the leg's live roster mounts. */
readonly mounted: () => readonly string[];
}
/**
* `$.agent.spawn`'s host entry (see the module header): the leg's one delegation tool, run through the loop's per-call pipeline as
* the caller's call. It answers off the call's record: started ⇒ `{ model, agentId }` (whatever the post-execution stations did to
* the result); not started and refused or failed ⇒ `{ deny: <the result's text> }`; not started with no error (an extension answered
* the call in place of its body) ⇒ `{ model }`, no `agentId`. It throws on an empty prompt, and when the leg mounts no delegation
* tool or more than one (it names them; it never picks).
*/
export declare function createAgentSpawnEntry(input: AgentSpawnEntryInput): ModCallEntry;