UNPKG

@sema-agent/core

Version:

Stateless, task-oriented AI agent core

171 lines (170 loc) • 12.4 kB
import type { AgentParkGate } from "./agent-state.js"; import type { AssertAllKeysHandled } from "./ask-origin.js"; import type { BackgroundAgentRecord } from "./background-agent-store.js"; /** * The protocol's task states, as ONE list — the type is derived from it, so a value and its type can * never drift apart, and a consumer validating wire input iterates the same source the table does. */ export declare const A2A_TASK_STATES: readonly ["submitted", "working", "input-required", "completed", "canceled", "failed", "rejected", "auth-required", "unknown"]; /** One A2A task state (see {@link A2A_TASK_STATES}). */ export type A2ATaskState = (typeof A2A_TASK_STATES)[number]; /** * The extensions a sema deployment's agent card declares (`capabilities.extensions[].uri`), one fact each, so a deployment can declare * one without the other: * - `skillId` — the deployment routes a `message/send` by the skill id the caller carries in the message's metadata under * {@link A2A_SEMA_SKILL_ID_KEY}; a caller carries it only to a peer that declares this; * - `taskStates` — the deployment sends its task states by {@link A2A_STATE_TABLE}: `input-required` only when its agent asks the caller * something, and a task waiting on its own side's approval as `working` with {@link A2A_SEMA_WAITING_ON_KEY} in the task's metadata; * - `refusals` — the deployment answers a `message/send` it does not take with the JSON-RPC code of what the caller can change about it, * {@link A2A_SEMA_REFUSAL_CODES}; a caller reads those codes that way only from a peer that declares this (outside the protocol's own * codes, a JSON-RPC server error code means what each server says it means). */ export declare const A2A_SEMA_EXTENSIONS: Readonly<{ readonly skillId: "urn:sema-agent:a2a:skill-id:v1"; readonly taskStates: "urn:sema-agent:a2a:task-states:v1"; readonly refusals: "urn:sema-agent:a2a:refusals:v1"; }>; /** * What a caller can change to change a refusal of a `message/send`, one word each: nothing it can send (`not_retryable` — the same * request, as it is or reworded, is refused again), the request (`changeable` — the refusal's own words say what to change), or only * when it sends (`retry_later` — the answer asks for the request later). What the peer did with the refused request is not the class's * to say: it is whether the request reached the peer (an HTTP status: not known) and what the answer's definition says (this table's * `-32603`: nothing recorded, nothing started). */ export type A2ARefusalClass = keyof typeof A2A_SEMA_REFUSAL_CODES; /** * The refusal table of a deployment that declares {@link A2A_SEMA_EXTENSIONS}`.refusals`: the JSON-RPC error code it answers each * {@link A2ARefusalClass} with — `-32050` (in the protocol's server-error range: what it means is this table's), `-32602` (the protocol's * invalid-params, whose message names what to change), `-32603` (the protocol's internal error, said of a request it recorded nothing of * and started nothing for — every `-32603` it answers, its framework's own included). Written once here: the serving side answers with it * and the calling side reads it. */ export declare const A2A_SEMA_REFUSAL_CODES: Readonly<{ readonly not_retryable: -32050; readonly changeable: -32602; readonly retry_later: -32603; }>; /** The message-metadata key a caller names the skill under (to a peer declaring {@link A2A_SEMA_EXTENSIONS}`.skillId`). */ export declare const A2A_SEMA_SKILL_ID_KEY = "sema.skillId"; /** The task-metadata key a serving deployment says what its task waits on under (one declaring {@link A2A_SEMA_EXTENSIONS}`.taskStates`). */ export declare const A2A_SEMA_WAITING_ON_KEY = "sema.waitingOn"; /** What `sema.waitingOn.kind` names. One word today: the serving deployment's own approval. */ export declare const A2A_WAITING_ON_KINDS: readonly ["callee_approval"]; /** One {@link A2A_WAITING_ON_KINDS} word. */ export type A2AWaitingOnKind = (typeof A2A_WAITING_ON_KINDS)[number]; /** The `sema.waitingOn` member: what the task waits on, since when (display only), and the serving side's opaque reference to the * pending item. It names nothing to a caller and there is no way to hand it back: this engine's calling side shows it (when it is a * non-empty string) as one neutralized, length-bounded member of the tool result's `details` and sends it nowhere. */ export interface A2AWaitingOn { kind: A2AWaitingOnKind; since?: string; approvalRef?: string; } /** * What a task is doing, one word per meaning — the rows of {@link A2A_STATE_TABLE}. `awaiting_own_approval` is the serving side's own * person deciding (nothing the caller can supply moves it); `asking_caller` is the serving side's agent asking the caller something; * `needs_caller_auth` is the serving side asking the caller's person to authorize there; `unreadable` is a task whose outcome the * answering side cannot see. */ export declare const A2A_STATE_CLASSES: readonly ["working", "awaiting_own_approval", "asking_caller", "needs_caller_auth", "completed", "failed", "cancelled", "unreadable"]; /** One {@link A2A_STATE_CLASSES} word. */ export type A2AStateClass = (typeof A2A_STATE_CLASSES)[number]; /** Whom a task that is not over waits on: its own agent (`peer`), its own side's person (`peer_approver`), the caller (`caller`), or a * reading that cannot tell (`unknown`). A terminal task waits on nobody and has no word. */ export declare const A2A_WAITING_FOR: readonly ["peer", "peer_approver", "caller", "unknown"]; /** One {@link A2A_WAITING_FOR} word. */ export type A2AWaitingFor = (typeof A2A_WAITING_FOR)[number]; /** One row of the table. */ export interface A2AStateClassRow { /** The protocol state the serving side sends for this class. */ readonly state: A2ATaskState; /** The `sema.waitingOn` kind that rides with it — the one member that tells two classes sharing a state apart. */ readonly waitingOn?: A2AWaitingOnKind; /** Whom the task waits on; absent = the class is terminal. */ readonly waitingFor?: A2AWaitingFor; /** Protocol states no sema deployment sends that a peer may, read as this class (they mean the same thing to the caller). */ readonly alsoReads?: readonly A2ATaskState[]; /** The class a peer that does NOT declare the table may mean by this class's state: the meaning the table moved off that state. Its * reading then waits on `unknown`. */ readonly undeclaredMayBe?: A2AStateClass; } /** * THE table: class → what the serving side sends, and what the calling side reads. Every function below is a view of it. * * `awaiting_own_approval` and `working` share `working` and are told apart by `sema.waitingOn` (a person deciding on the serving side is * not something the caller can answer, and the protocol has no state for it — `input-required` is the caller's turn). Before this table * a sema deployment sent its own approval wait as `input-required`, so that state from a peer that does not declare the table is read * as either reading (`undeclaredMayBe`). `submitted` and `rejected` are never sent by a sema deployment (a task exists once it runs; * a refusal is an admission-time answer) and read as `working` and `failed`. */ export declare const A2A_STATE_TABLE: Readonly<Record<A2AStateClass, A2AStateClassRow>>; /** Compile-time fence: `never` while every class has a row. */ export type A2AStateTableCoversEveryClass = AssertAllKeysHandled<Exclude<A2AStateClass, keyof typeof A2A_STATE_TABLE>>; /** What the serving side sends for a task: the state, and the task metadata that rides with it. */ export interface A2AWireStatus { state: A2ATaskState; /** Present exactly when the class has a `waitingOn` kind: the task-level metadata member {@link A2A_SEMA_WAITING_ON_KEY}. */ metadata?: { readonly [A2A_SEMA_WAITING_ON_KEY]: A2AWaitingOn; }; } /** * Class → what to send. `facts` fills the `sema.waitingOn` member of a class that waits on its own side (the time the wait began, the * pending item's reference) — the serving layer holds them; the table holds only the kind. */ export declare function a2aWireStatusOf(stateClass: A2AStateClass, facts?: { readonly since?: string; readonly approvalRef?: string; }): A2AWireStatus; /** * This engine's durable background-agent row → class: the engine's own half of "what is this task doing" (a serving layer over its own * records keeps the same half for them; the word is the table's). TOTAL and pure: every (status, stoppedBy, gate) has an answer, and the * answer depends on nothing else. * * - `running` → `working`; `completed` → `completed`; `failed` → `failed`; * - `parked` → by what it waits on, `gate` ({@link PARKED_CLASS_BY_GATE}): a person on its own side ⇒ `awaiting_own_approval`, its own * machinery ⇒ `working`. The row does not carry its checkpoint's gate (the park face reads it off the checkpoint, `AgentPark.gate`); * a parked row given none — the card not read, or carrying no gate this engine knows (a kind that is not one of * {@link PARKED_CLASS_BY_GATE}'s own string keys: a name that is only on the object's prototype, such as `constructor`, and a value * that is not a string at all are not kinds) — is read as a person on its own side deciding: either way nothing the caller sends moves * it, and that reading is the one that does not send the caller polling a person; * - `killed` splits on the attribution: a stop by `user`, `parent`, or `manager` is a deliberate cancellation (`cancelled`); a `system` stop, an * unrecognized source or no attribution is a termination nobody asked for, and `failed` is the honest terminal for it. * * Neither `asking_caller` nor `needs_caller_auth` nor `unreadable` comes out of a row: this engine's agent asks its caller nothing * across the protocol, an authorization demand is an admission-time answer, and `unreadable` belongs to the read face one level up (a * `running` row written by another instance, whose outcome this one cannot see). The switch is exhaustive over `status` with no default * arm, so tsc fails when a sixth durable status is added. */ export declare function a2aStateClassOfRecord(record: Pick<BackgroundAgentRecord, "status" | "stoppedBy">, gate?: AgentParkGate): A2AStateClass; /** What a caller reads off one task a peer reported. */ export interface A2APeerStateReading { /** The class the state reads as. */ stateClass: A2AStateClass; /** Whom the task waits on; absent = terminal. `unknown` where the class's own answer is not one this peer is bound to. */ waitingFor?: A2AWaitingFor; /** Read `unknown` because the peer does not declare the table and the state may mean this class instead (the table's * `undeclaredMayBe`). */ mayBe?: A2AStateClass; /** The peer's `sema.waitingOn`, as it sent it (strings as given — a display reads them through its own neutralizer). */ waitingOn?: { readonly kind: string; readonly since?: string; readonly approvalRef?: string; }; } /** * A peer's task → its reading, by the table and nothing else: the classes whose `state` (or `alsoReads`) is the reported state; between * the two that share `working`, the one whose `waitingOn` kind the task's `sema.waitingOn` names. `declaresTable` is whether the peer's * card declares {@link A2A_SEMA_EXTENSIONS}`.taskStates`: * - a peer that declares it is read with its `sema.waitingOn`, and only on a state some class gives a `waitingOn` kind to (today `working` * alone): there, a string `kind` the table does not have reads as `working` waiting on `unknown` (it waits on something; what, this * side cannot say), while a member whose `kind` is not a string is not read at all (the plain `working`). On any other state the member * is not part of the reading (`submitted`, `input-required`, `completed` … read as their plain class and its answer, the member dropped); * - a peer that does not is read without it (the member is not its vocabulary), and a state whose row has `undeclaredMayBe` reads as the * row's class waiting on `unknown`, with that other class named. */ export declare function readA2APeerState(task: { readonly state: A2ATaskState; readonly metadata?: unknown; }, declaresTable: boolean): A2APeerStateReading;