@sema-agent/core
Version:
Stateless, task-oriented AI agent core
273 lines (272 loc) • 13.1 kB
TypeScript
/**
* The engine's `$`: the seat view a runner hands the mods bus, and the chain bottom of each wave-one method
* event over it. Every method's bottom is a seat the engine already has; each method's limits are part of
* its contract and are checked here (a refused input is the caller's throw, "(host check)").
*/
import type { AgentMessage, ExecutionEnv } from "../../internal/harness-types.js";
import type { Context, Message, Usage } from "../../internal/llm.js";
import type { CompactOutcome } from "../task-event.js";
import { type ModDollarBase, type ModMethodBottoms, type ModTimer } from "./kernel/index.js";
import type { ContextOccupancy } from "./lifecycle-stations.js";
/** `$.process.run`'s default command timeout (CC). */
export declare const PROCESS_RUN_DEFAULT_TIMEOUT_MS = 30000;
/** `$.process.run`'s largest command timeout (CC). */
export declare const PROCESS_RUN_MAX_TIMEOUT_MS = 600000;
/** `$.model.complete`'s output cap when the call names none. */
export declare const MODEL_COMPLETE_DEFAULT_MAX_TOKENS = 1024;
/** How many of the session's latest messages `$.session.messages` returns (CC). */
export declare const SESSION_MESSAGES_MAX = 4096;
/** One block of a `$.model.complete` prompt or system text (CC form): its text, and at most `cache: true`. */
export interface ModelCompleteBlock {
readonly text: string;
readonly cache?: boolean;
}
/** What the leg's model seat is asked: the model, the cap, and the request already in the brain's own form. */
export interface ModelCompleteRequest {
/** A model reference the leg's catalog resolves; absent ⇒ the leg's own model. */
readonly model?: string;
readonly maxTokens: number;
/** The system text (with its cache-span blocks on the brain's `systemBlocks` seat) and the one user message. */
readonly context: Pick<Context, "systemPrompt" | "systemBlocks" | "messages">;
}
export interface ModelCompleteResult {
readonly text: string;
readonly model: string;
/** The provider's usage, returned to the caller; the same reply is booked once on the run's account ({@link ModSeatView.book}). */
readonly usage?: Usage;
}
/**
* What a runner hands the bus: the seats behind the wave-one methods. `env` is the leg's execution
* environment itself — a `$.process.run` command is the extension's, not a model tool call, so it does
* not pass the approval gate; the deployment's environment is its boundary.
*/
export interface ModSeatView {
readonly env: Pick<ExecutionEnv, "exec">;
/** The leg's model seat (brain / role resolution). */
complete(request: ModelCompleteRequest, signal: AbortSignal): Promise<ModelCompleteResult>;
/** The run's one account of side model calls: every `$.model.complete` reply is booked here once, its usage as the
* provider reported it (absent ⇒ the reply reported none). */
book(reply: {
readonly model: string;
readonly usage?: Usage;
}): void;
readonly session: {
id(): string;
/** The task's tracked working directory (not the environment's own cwd). */
cwd(): string;
root(): string;
model(): string;
/** Shallow record of the latest dispatched main-loop message list in a run (shared message objects, so post-dispatch in-place mutations show through; session projection on standalone compaction). */
messages(): readonly Message[];
/** A subagent's stored transcript rows, read through the leg's own task list row for it; rejects with the reason when the
* leg reaches no such row, the row has no session, or the session cannot be read. */
agentMessages(agentId: string): Promise<readonly AgentMessage[]>;
/** How many prompts the session's transcript holds from its people (`promptCountOf`, `session-append.ts`). */
turns(): Promise<number>;
usage(): SessionUsage;
version(): SessionVersion;
};
readonly turn: {
/** End the leg's running model turn `turn` for registration `by`, judged and cut in one synchronous step
* ({@link TurnAbortVerdict}). */
abort(turn: number, by: string): TurnAbortVerdict;
};
/** The deployment clock (`RunnerDeps.now`). */
now(): number;
tools(): readonly string[];
agents(): readonly string[];
}
/** `$.session.usage()`: the leg's latest turn-boundary measurement — the context occupancy the boundary's `context_usage`
* frame carries, absent before the leg's first boundary (a session-level pass measures none). */
export interface SessionUsage {
readonly context?: ContextOccupancy;
}
/** `$.session.version()`: the engine's package version. */
export interface SessionVersion {
readonly version: string;
}
/** What the leg found for a `$.turn.abort`: it cut the running turn; no model request is running; the request running
* is another turn's (`running`, its number); or that turn is already ending. */
export type TurnAbortVerdict = {
readonly kind: "cut";
} | {
readonly kind: "none";
} | {
readonly kind: "not_running";
readonly running: number;
} | {
readonly kind: "ending";
};
/** The leg's `$.turn.abort` seat, minted with the leg's facade and filled by the run once its turn count exists (unfilled
* ⇒ no model request is running). */
export interface TurnAbortRef {
seat?: (turn: number, by: string) => TurnAbortVerdict;
}
export interface ProcessRunResult {
readonly stdout: string;
readonly stderr: string;
readonly exitCode: number;
}
/** The engine `$` a handler receives on the engine bus. */
export interface EngineDollar extends ModDollarBase {
readonly process: {
run(args: {
argv: readonly string[];
init?: {
cwd?: string;
timeoutMs?: number;
};
}): Promise<ProcessRunResult>;
};
readonly model: {
complete(args: {
prompt: string | readonly ModelCompleteBlock[];
system?: string | readonly ModelCompleteBlock[];
model?: string;
maxTokens?: number;
}): Promise<ModelCompleteResult>;
};
readonly session: {
id(): Promise<string>;
cwd(): Promise<string>;
root(): Promise<string>;
model(): Promise<string>;
/** Latest dispatched main-loop message list in a run — newest {@link SESSION_MESSAGES_MAX}, cloned on read from the shallow record (post-dispatch in-place mutations show through; session projection on standalone compaction). */
messages(): Promise<readonly Message[]>;
/** `$.session.messages({ agentId })`: a subagent's stored transcript rows (the newest {@link SESSION_MESSAGES_MAX}, copies),
* read through this leg's own task list row for that agent; rejects with the reason when this leg reaches no such row, the
* row has no session, or the session cannot be read. */
messages(args: {
readonly agentId: string;
}): Promise<readonly AgentMessage[]>;
/** `$.session.turns()`: how many prompts the session's transcript holds from its people (a steer is not one). */
turns(): Promise<number>;
/** `$.session.usage()`: the leg's latest turn-boundary measurement. */
usage(): Promise<SessionUsage>;
/** `$.session.version()`: the engine's package version. */
version(): Promise<SessionVersion>;
/** `$.session.append({ message: { role: "user", content }, agentId? })`: an extension's own note, admitted at the
* `session.append` station (`session-append.ts`). */
append(args: {
readonly message: {
readonly role: "user";
readonly content: string | readonly {
readonly type: "text";
readonly text: string;
}[];
};
readonly agentId?: string;
}): Promise<SessionAppendAnswer>;
/** `$.session.send({ to, text })`: the leg's SendMessage, run as the caller's own call and raised once at the
* `session.send` station (`session-send.ts`). */
send(args: {
readonly to: string;
readonly text: string;
}): Promise<SessionSendAnswer>;
/** `$.session.compact({ instructions? })`: a manual compaction pass at the turn boundary, in place — from a handler of
* `turn.complete` only (`session-compact.ts`). */
compact(args?: {
readonly instructions?: string;
}): Promise<SessionCompactAnswer>;
};
readonly prompt: {
/** `$.prompt.submit({ text })`: an extension's prompt entering this leg in the person's place (`prompt-submit.ts`). */
submit(args: {
readonly text: string;
}): Promise<PromptSubmitAnswer>;
};
readonly turn: {
/** `$.turn.abort({ turn })`: end this leg's running model turn — `turn` is the number `turn.start` carried. The turn
* settles and the run goes on at its ordinary boundary (queued input, else the stop gate); a call that finds no
* request running, another turn running, or the turn already ending rejects, saying which. */
abort(args: {
readonly turn: number;
}): Promise<void>;
};
readonly clock: {
now(): Promise<number>;
sleep(args: {
ms: number;
}): Promise<void>;
/** `$.clock.after({ ms }, fn)`: run `fn` once, `ms` from now — the handle at once, the wait one dispatch of `clock.after`.
* It lives within the leg: cancelled when the leg stops, or by `cancel()`. */
after(args: {
ms: number;
}, fn: () => unknown): ModTimer;
/** `$.clock.every({ ms }, fn)`: run `fn` every `ms` (at least 1) — each wait one dispatch of `clock.every`, a refused one
* stopping it; a throw of `fn` is reported and the timer runs on. It lives within the leg: cancelled when the leg stops,
* or by `cancel()`. */
every(args: {
ms: number;
}, fn: () => unknown): ModTimer;
};
readonly tool: {
list(): Promise<readonly string[]>;
/** `$.tool.call({ tool, ...input })`: the run's own pipeline for one call of a mounted tool (`tool-call.ts`). */
call(args: {
readonly tool: string;
} & Readonly<Record<string, unknown>>): Promise<ToolCallAnswer>;
};
readonly agent: {
list(): Promise<readonly string[]>;
/** `$.agent.spawn({ prompt, ... })`: the delegation tool's own pipeline as the caller's call, the subagent in the background
* (`createAgentSpawnEntry`, `agent-stations.ts`). */
spawn(args: {
readonly prompt: string;
readonly description?: string;
readonly subagentType?: string;
readonly model?: string;
readonly name?: string;
readonly cwd?: string;
}): Promise<AgentSpawnAnswer>;
};
}
/** What `$.agent.spawn` answers: the subagent started (`agentId`, the id its events carry; `model`, as `agent.spawn`'s bottom
* names it), none started because an extension answered the call in place of its body (`model` alone), or the refusal. */
export type AgentSpawnAnswer = {
readonly model: string;
readonly agentId?: string;
} | {
readonly deny: string;
};
/** What `$.tool.call` answers — `tool.call`'s own forms: the frozen view of the call's result, or the refusal (returned, not thrown). */
export type ToolCallAnswer = {
readonly result: {
readonly content: readonly object[];
readonly isError: boolean;
};
} | {
readonly deny: string;
};
/** What `$.session.append` answers — `session.append`'s own forms: the note as kept (a frozen view), or the refusal (returned, not thrown). */
export type SessionAppendAnswer = {
readonly message: {
readonly role: "user";
readonly content: string | readonly object[];
};
} | {
readonly deny: string;
};
/** What `$.session.send` answers — `session.send`'s own forms: delivered, or not and why (returned, not thrown). */
export type SessionSendAnswer = {
readonly isDelivered: true;
} | {
readonly isDelivered: false;
readonly reason: string;
};
/** What `$.prompt.submit` answers when the input is taken: the text that entered, the answer another registration's prompt
* screen gave it (`reply`: it entered, and the turn it opens is that answer — no model is asked), or why it did not
* (returned, not thrown). */
export type PromptSubmitAnswer = {
readonly text: string;
} | {
readonly reply: string;
} | {
readonly drop: string;
};
/** What `$.session.compact` answers: the in-place pass's outcome (a call it refuses throws, its reason on the report sink too). */
export interface SessionCompactAnswer {
readonly outcome: CompactOutcome;
}
/** The chain bottoms of the wave-one method events over a runner's seat view. */
export declare function engineMethodBottoms(view: ModSeatView): ModMethodBottoms;