@sema-agent/core
Version:
Stateless, task-oriented AI agent core
112 lines (111 loc) • 8.6 kB
TypeScript
/** The `_meta` key carrying the executing call's id. */
export declare const MCP_TOOL_USE_ID_META_KEY = "sema/toolUseId";
/** The `_meta` key carrying the request meta the call's `tool.call` chain handed on. */
export declare const MCP_REQUEST_META_KEY = "sema/requestMeta";
/** The most UTF-8 bytes a request meta may hold, its keys and values together (the reference's bound on its values: 262144). */
export declare const MCP_REQUEST_META_MAX_BYTES = 262144;
/** A request meta as it rides: string members only. */
export type McpRequestMeta = Readonly<Record<string, string>>;
/** A judged request meta: the copy that rides (undefined when there is none, or it has no member), or why it cannot ride. */
export type RequestMetaReading = {
readonly meta: McpRequestMeta | undefined;
} | {
readonly problem: string;
};
/**
* The request meta `value` stands for. It can ride when it is a plain record (the ordinary prototype, or none) whose own members
* are all enumerable string-keyed data properties holding strings, at most {@link MCP_REQUEST_META_MAX_BYTES} UTF-8 bytes of keys
* and values together; what rides is a frozen copy of those members with no prototype (an own `__proto__` key stays a key).
*/
export declare function judgeRequestMeta(value: unknown): RequestMetaReading;
/** The request meta a handler handed on in `payload`: its OWN `requestMeta` data member (one inherited from a prototype is not
* handed on, and an accessor is never run), judged by {@link judgeRequestMeta}. */
export declare function readHandedRequestMeta(payload: object): RequestMetaReading;
/** The most characters a declared session key may have. */
export declare const MCP_SESSION_META_KEY_MAX_CHARS = 128;
/** A judged session key: the key that rides (undefined when none is declared), or why it cannot. */
export type SessionMetaKeyReading = {
readonly key: string | undefined;
} | {
readonly problem: string;
};
/**
* The one judge of a declared `sessionMetaKey` ({@link import("./mcp-server-spec.js").McpServerSpec.sessionMetaKey}). It can ride when it is
* a non-empty string of at most {@link MCP_SESSION_META_KEY_MAX_CHARS} characters in the MCP `_meta` key-name format (an optional
* prefix of dot-separated labels and a slash, then a name — the format is what a conforming server accepts, and it keeps out names such
* as `__proto__` that the SDK's own request parse drops) that names no `_meta` member of anyone else's: not the SDK's `progressToken`,
* not a key under the engine's `sema/` namespace, not under a prefix MCP reserves (the prefix is the text before the first `/`; its
* labels are DNS labels, so the second one is compared with case folded). A refusal says which rule the key breaks and never the key: the
* key is the declaration's own text, shown in the one caller's-text shape (`callerText`: the redaction mark and its length).
*/
export declare function judgeSessionMetaKey(value: unknown): SessionMetaKeyReading;
/** The `_meta` key carrying the calling leg's identity ({@link McpCaller}) to a server that declares `callerMeta: true`. */
export declare const MCP_CALLER_META_KEY = "sema/caller";
/**
* Who is calling: the identity facts of the leg a request is sent for, every one a value the engine already mints for the leg's own
* frames (none is minted for this record). Frozen, no prototype.
* - `taskId` — the leg's task id, minted once at prepare (`legTaskIdOf`): the value `TaskResult.taskId` and `task_progress.taskId` carry,
* whatever that reading answers on this run (the leg's registry row id; on a ledger run, the host's task id if it gave one other than the session id, the id the parked row recorded on a resume, otherwise the minted `rl-…` id; else the host's task id or the leg's session id).
* - `runId` — this invocation of the engine (`Prepared.runId`): new on every run, a resume included.
* - `rootSessionId` — the delegation tree's root session (`rootAnchorOf`): the leg's own session on a top-level leg.
* - `parentTaskId` — the spawning leg's task id (`RunInternals.parentTaskId`, the value `task_progress.parentTaskId` carries); absent on a
* leg nobody spawned under a known task.
* Built once per leg and run, at prepare: every call and fetch of that run carries the same record. No principal, path, model or display
* name rides it. Session ids do: `taskId` is the leg's own session id wherever that is its task id (a foreground child, a workflow member
* or a non-ledger top-level leg handed no task id; on a ledger run the task id is the host's if it gave one other than the session id, the id the parked row recorded on a resume, otherwise the minted `rl-…` one), and `rootSessionId` is the tree root's session — independent of `sessionMetaKey`.
*/
export type McpCaller = Readonly<{
taskId: string;
runId: string;
rootSessionId: string;
parentTaskId?: string;
}>;
/** The one construction of a leg's {@link McpCaller}: the frozen record, `parentTaskId` only when it is a non-empty string. */
export declare function mcpCaller(facts: {
readonly taskId: string;
readonly runId: string;
readonly rootSessionId: string;
readonly parentTaskId?: string | undefined;
}): McpCaller;
/** A judged `callerMeta` declaration: whether the caller rides, or why the value cannot be read. */
export type CallerMetaReading = {
readonly send: boolean;
} | {
readonly problem: string;
};
/**
* The one judge of a declared `callerMeta` ({@link import("./mcp-server-spec.js").McpServerSpec.callerMeta}): `true` sends the caller,
* `false` and absent send nothing; any other value (a string such as `"true"`, a number such as `1`, `null`, an object) is not read as
* either — it is refused, so a deployment never believes a server is told who calls while it is not, or the reverse.
*/
export declare function judgeCallerMeta(value: unknown): CallerMetaReading;
/**
* The `_meta` of one request the engine sends a server — a `tools/call` (the executing call's id, when it has one, and the request meta,
* when there is one: the judge answers none for a record with no member, so whatever it hands here rides) or a fetch (`resources/read`,
* `prompts/get`: neither) —
* and, when the server declared a session key, the leg's own session id under it; when the server declared `callerMeta: true`, the leg's
* caller ({@link McpCaller}) under {@link MCP_CALLER_META_KEY} (the caller is passed here only then). THE one mint: the key was judged where
* the declaration was ({@link judgeSessionMetaKey}), so it never lands on another member — nor on the caller's, which is under `sema/`; no
* session id or caller (a dial with no leg) rides nothing.
*/
export declare function mcpCallMeta(toolUseId: string | undefined, requestMeta: McpRequestMeta | undefined, sessionMetaKey?: string, sessionId?: string, caller?: McpCaller): Record<string, unknown> | undefined;
/**
* The carrier from a call's `tool.call` chain bottom to the MCP tool's `execute`, one per leg. A tool's `execute` takes the call id
* and no per-call context, so the request meta rides the body's own async chain: {@link McpRequestMetaSeat.of} answers it only to
* an `execute` that runs inside that body, for the same call id, on the same leg's seat, while the body has not settled. Two calls
* in flight (even under one id), a child leg's calls (its own seat), a direct `execute` outside any body and a timer the body left
* behind never read each other's.
*/
export interface McpRequestMetaSeat {
/** Run `body` with `meta` standing for the call `toolCallId` names, on this body's async chain; it stops standing when it settles. */
hold<T>(toolCallId: string, meta: McpRequestMeta | undefined, body: () => Promise<T>): Promise<T>;
/** What stands for the call `toolCallId` names, read from inside its body. */
of(toolCallId: string): McpRequestMeta | undefined;
/** The leg's own session id: what a server that declared a session key is sent ({@link mcpCallMeta}). Absent for a seat made without one. */
readonly sessionId?: string;
/** The leg's caller: what a server that declared `callerMeta: true` is sent ({@link mcpCallMeta}). Absent for a seat made without one. */
readonly caller?: McpCaller;
}
/** What the MCP materialization reads of its leg's seat: the request meta standing for a call, the leg's session id and its caller. */
export type McpLegMetaReader = Pick<McpRequestMetaSeat, "of" | "sessionId" | "caller">;
export declare function createMcpRequestMetaSeat(sessionId?: string, caller?: McpCaller): McpRequestMetaSeat;