@sema-agent/core
Version:
Stateless, task-oriented AI agent core
94 lines (93 loc) • 7.36 kB
TypeScript
/**
* The `tool.call` station: raised on the leg's bus between the gate letting a call go and the tool's body, for every
* execution of a call (the live loop's lanes through the loop's execution seat, the resumed leg's approved call through
* the same function). The chain's bottom is the tool's body; the event's failure column is `refuse_until_run`: a handler's
* failure refuses the call while the body has not started; once its own `next(e)` ran it, the handler is skipped and the
* result stands (said on the bus's failure sink). Only a `{ deny }` withholds a result that ran.
*
* `e = { tool, tool_use_id, agentId?, input }` (all four pinned — what the gate judged is what runs; a rewrite belongs to
* `classic.PreToolUse`) plus the identity envelope; a handler may hand on `requestMeta` too (a record of strings an MCP call carries
* as `_meta["sema/requestMeta"]`, judged where it is handed on — `mcp-call-meta.ts`). A handler answers:
* - `next(e)` — the body runs; what came up is `{ result: { content, isError } }`, a frozen view of the real result;
* - `{ deny: <reason> }` — in place of `next` (before the body): refused, not executed, the record names the extension; over
* what its `next` answered once the body ran: WITHHELD — the call ran (no `notExecuted`), the model reads who withheld it
* and why. Over a `next` still running, or over a body that threw (its throw is the outcome): a violation;
* - `{ result }` — an extension's own result in place of the body (a string, or a list of text blocks), taken only when
* both facts hold: the gate let this call go without anyone being asked about it (its latest gate record is an allow
* with no settlement, release or clearance — a person's or a rule's yes is a yes to running the tool), and the body
* has not started (the kernel's own record). Either missing, the answer is a violation: before the body the call is
* refused, after it the handler is skipped and the result stands. A taken result is the call's result; the record says
* the body never ran and names the extension (`tool_end.notExecuted`, `tool_end.answeredBy`).
* Context for the model is added at `classic.PostToolUse`, not here. Whatever an extension answered in place of the body,
* the call's post-execution stations stay silent, as for a call the gate blocked.
*
* `$.tool.call({ tool, ...input })` is the event called (the table's `call` column): the host entry
* ({@link createToolCallEntry}) runs the loop's own per-call pipeline for the named tool — the whole gate, this station,
* the body, the post-execution stations — under the run's principal, with the caller's frames and origin on every event
* its pipeline raises (`extension-call.ts`), and no model message carrying it. It answers this event's own forms:
* `{ result: { content, isError } }` (the frozen view a handler's `next` gets), or `{ deny: <reason> }` when the call was
* refused (returned, not thrown).
*/
import type { AgentToolResult } from "../../internal/harness-types.js";
import type { HookInvocationIdentity } from "../hooks.js";
import type { ToolOccurrenceTable } from "../runner/tool-occurrence.js";
import { type McpRequestMetaSeat } from "../mcp-call-meta.js";
import { type LegModBus } from "./stations.js";
import { type FrameSettlement } from "../frame-settlement.js";
import type { ModBus, ModCallEntry } from "./kernel/index.js";
/** One execution as the station sees it: the call, the tool that runs (its mounted name), the arguments the gate let go. */
export interface ToolCallExecution {
readonly toolCallId: string;
readonly toolName: string;
readonly args: unknown;
}
/** The station over one execution: `run` runs the tool's body (at most once); the result is the call's result. */
export type ToolCallStation = (call: ToolCallExecution, run: () => Promise<AgentToolResult<unknown>>, signal: AbortSignal | undefined) => Promise<AgentToolResult<unknown>>;
export interface ToolCallStationInput {
readonly bus: LegModBus;
/** The leg's occurrence table: the gate record a substitute result is read against, and the answered-in-place record. */
readonly occurrences: Pick<ToolOccurrenceTable, "get" | "answeredInPlace" | "refuse">;
/** The calls whose post-execution stations stay silent (the gate's blocked set). */
readonly blockedToolCalls: Set<string>;
readonly identity?: HookInvocationIdentity;
readonly reminderMark?: string;
/** The leg's request-meta seat: what the chain handed on as `requestMeta` stands under the call's id while its body runs. */
readonly requestMeta: Pick<McpRequestMetaSeat, "hold">;
/** The leg's settlement seam: a result this station withholds settles the frames it carried as withheld by the host side
* (`settleWithheldResult`), so none is owed again through another lane. Absent ⇒ no frame rides a call of this station. */
readonly frameSettlement?: FrameSettlement;
/** The leg's report sink: a deny that gave no readable reason is said there. */
readonly report: (err: Error) => void;
}
/** The station over the leg's bus, or undefined when no handler hangs on `tool.call` (nothing is raised; the body runs). */
export declare function createToolCallStation(input: ToolCallStationInput): ToolCallStation | undefined;
/** What the host entry runs a call on: the leg's harness (the loop's pipeline under the running prompt) and the tool identities. */
export interface ToolCallEntryInput {
/** The leg bus every station raises on ({@link import("./extension-call.js").legBusOf}). */
readonly bus: ModBus;
readonly harness: Pick<import("../../internal/harness.js").AgentHarness, "callTool">;
/** The live roster's face of a mounted tool, by the name it is mounted under (`toolCallFaceOf`): its catalog contract. */
readonly faceOf: (toolName: string) => {
readonly contractId: string;
} | undefined;
/** The tool the leg mounted under this name, as the caller handed it (the one an `execute` binding is read off). */
readonly specOf: (toolName: string) => unknown;
}
/** `$.tool.call`'s host entry: the loop's per-call pipeline for the named tool, run as the caller's call (see the module header). */
export declare function createToolCallEntry(input: ToolCallEntryInput): ModCallEntry;
/**
* The gate's last word on an extension's own call, at its allow exit, on the arguments it lets go: the loop gives a call that
* is not batch-safe (a `cd`) the run to itself, and an extension's call is in no batch of the loop's — so one that is not
* batch-safe is let go only when no other call of the run is in flight, and a batch-safe one only while no call that is not
* batch-safe has been let go. `batchSafe` is the loop's own reading (`isBatchSafeToolCall`, through the harness). Undefined:
* let it go; a sentence: refused before it runs.
*/
export declare function extensionCallOverlap(input: {
readonly occurrences: Pick<ToolOccurrenceTable, "live">;
readonly toolCallId: string;
readonly toolName: string;
readonly args: unknown;
readonly batchSafe: (toolName: string, args: unknown) => boolean;
}): string | undefined;
/** A call result's text, as a host entry answers a refusal with it (`{ deny: <the text> }`). */
export declare const resultText: (content: unknown) => string;