import type { StandardJSONSchemaV1, StandardSchemaV1 } from "#compiled/@standard-schema/spec/index.js"; import type { Approval } from "#approval/definition.js"; import type { MessageResult, SendTurnOptions } from "#client/types.js"; import type { JsonObject } from "#shared/json.js"; import { type PublicToolDefinition, type ToolContext, type ToolInputRequest, type ToolInputRequestOptions, type ToolInputResponse } from "#tools/definition.js"; import type { ToolModelOutput } from "#tools/model-output.js"; import { type WorkflowToolEntryPoint } from "#tools/workflow-entry-point.js"; export type { WorkflowToolEntryPoint }; export interface WorkflowAgentMetadata { readonly description: string; } /** Context capabilities available inside an authored `"use step"` helper. */ export type WorkflowStepToolContext = Pick; /** Options for one message to a `ctx.agent` session. */ export interface AgentSendOptions { /** Structured output for the turn, as on the client's `send`. */ readonly outputSchema?: SendTurnOptions["outputSchema"]; /** Aborting cancels the turn this message started or joined; the turn still reports. */ readonly signal?: AbortSignal; } /** * How one agent turn ended, as the client's `MessageResult` reports it: * `"waiting"` when the session waits for its next message, including after a * cancelled turn, which carries neither `data` nor `message`; `"completed"` * when the session ended with the turn; and `"failed"` when the turn failed, * with the reason in `error`. */ export type AgentMessageResult = Pick, "data" | "message" | "status"> & { /** Why the turn failed, present only when `status` is `"failed"`. */ readonly error?: { readonly message: string; }; }; /** The response to one message sent to a `ctx.agent` session. */ export interface AgentResponse { /** Resolves when the turn the message started or joined ends. */ result(): Promise>; } /** * A session the workflow run owns, opened by its first `send` and ended when * the run finishes. Only the run can address it. */ export interface AgentSession { /** * Delivers a message. It joins the session's running turn, whose result the * response resolves, or starts the next turn when the session is idle or the * running turn ends before the agent reads it. */ send(message: string, options?: AgentSendOptions): Promise>; } /** Members the context of every workflow tool entry point shares. */ export type WorkflowSharedContext = Pick & { /** * Returns a new session with the agent of this invocation name. Nothing * starts until the first `send`. */ agent(name: string): AgentSession; /** Metadata for agents callable by this workflow, including hidden agents. */ agents: Readonly>; /** * Ask the human on the session's channel; awaiting the answer suspends the * run. The request is withdrawn, resolving as `cancelled`, when * `options.signal` or the call's `abortSignal` aborts. */ ask(request: ToolInputRequest, options?: ToolInputRequestOptions): PromiseLike; }; /** * Context of an `execute(input, ctx)` or `task(input, ctx)` call. When passed * directly to a step, eve replaces it with {@link WorkflowStepToolContext}. */ export type WorkflowToolContext = WorkflowSharedContext & Pick & { /** * Aborts when the call's work should stop: the call is cancelled, by * `eve__task_cancel`, `session.cancel()`, a failed turn, or the end of the * session, or, for an `execute` call, a steering message arrives while the * turn waits on it. Stop and return what you have. After a steering * message the call settles with what the body returns, or with * `{ interrupted: true }` if it rejects; a cancelled call's result is * discarded. A task never sees a steering message. */ readonly abortSignal: AbortSignal; }; /** * One call a `serve` body receives: the call that started the task, or a * later call the model made with the task's `taskId`. */ export interface WorkflowServeCall { readonly callId: string; /** The call's input, validated by the tool's `inputSchema`. */ readonly input: TInput; /** * Aborts when the task's current stretch of work is cancelled or the * session ends. A stretch starts when a call reaches an idle task and ends * when a reply or a cancel settles its calls; calls that arrive during it * share its signal, and the next stretch gets a new one. */ readonly abortSignal: AbortSignal; } /** * Resolves with the task's next call. The first `receive()` resolves at once * with the call that started the task; each later one with the next call made * with the task's `taskId`, in arrival order. A pending `receive()` is shared: * calling it again returns the same promise. A call counts as received once its * promise resolves, even if that promise lost a `Promise.race`, and the next * `ctx.reply()` settles it, so keep a raced promise until you read its call. * It rejects when the session ends. */ export type WorkflowServeReceive = () => Promise>; /** * Context of a `serve(receive, ctx)` task. Each call brings its own `callId` * and `abortSignal` from `receive()`, and steering never interrupts a task, so * the context has neither. `session` (its turn and auth) and `agents` * describe the latest call received, the call the body serves now. A session * opened with `agent()` is the child of the call served when it opens, and * each message sent to it carries the auth of the call served when it is sent. * `ctx.ask` throws while no call is * waiting for a result. When passed directly to a step, eve replaces it with * {@link WorkflowStepToolContext} for the latest call waiting for a result. */ export type WorkflowServeContext = WorkflowSharedContext & { /** * Settles every call received so far with `output`, and the task goes idle * until its next call. A reply with no call left to settle, including one * after a cancel, is dropped. */ reply(output: TOutput): void; }; declare const WORKFLOW_TOOL_BRAND: unique symbol; interface WorkflowToolDefinitionBase extends PublicToolDefinition { readonly [WORKFLOW_TOOL_BRAND]: true; approval?: Approval : TInput>; toModelOutput?: (output: TOutput) => ToolModelOutput | Promise; } /** A workflow tool whose calls are ordinary tool calls: the turn waits until each settles. */ export interface WorkflowExecuteToolDefinition extends WorkflowToolDefinitionBase { execute(input: TInput, ctx: WorkflowToolContext): Promise | AsyncIterable; task?: never; serve?: never; } /** * A workflow tool whose every call runs as a task: the model gets a receipt at * once and the conversation continues, and the result arrives later in a * `task.result` message. */ export interface WorkflowTaskToolDefinition extends WorkflowToolDefinitionBase { task(input: TInput, ctx: WorkflowToolContext): Promise | AsyncIterable; execute?: never; serve?: never; } /** * A workflow tool whose calls reach resumable tasks. A call without `taskId` * starts a task, whose body runs once and gets that call and every later call * made with its `taskId` from `receive()`; each `ctx.reply()` delivers a * result. eve adds the optional `taskId` to the tool's model input, so * `inputSchema` must not declare its own. Returning settles the calls still * waiting and ends the task; throwing fails them. */ export interface WorkflowServeToolDefinition extends WorkflowToolDefinitionBase { serve(receive: WorkflowServeReceive, ctx: WorkflowServeContext): Promise; execute?: never; task?: never; } /** * A static tool whose entry point runs as a durable workflow and must start * with "use workflow". It defines exactly one entry point. */ export type WorkflowToolDefinition = WorkflowExecuteToolDefinition | WorkflowTaskToolDefinition | WorkflowServeToolDefinition; type Unbranded = T extends unknown ? Omit : never; type WorkflowReturn = T extends AsyncIterable ? Output : Awaited; type Schema = StandardSchemaV1 | StandardJSONSchemaV1; type EntryPointReturn = Promise | AsyncIterable; type InferOutput> = StandardJSONSchemaV1.InferOutput; type InferInput = StandardSchemaV1.InferOutput; type DefinitionFields = Omit, typeof WORKFLOW_TOOL_BRAND>; type ExecuteDefinition = DefinitionFields> & { execute(input: TInput, ctx: WorkflowToolContext): TReturn; task?: never; serve?: never; }; type TaskDefinition = DefinitionFields> & { task(input: TInput, ctx: WorkflowToolContext): TReturn; execute?: never; serve?: never; }; type ServeDefinition = DefinitionFields & { serve(receive: WorkflowServeReceive, ctx: WorkflowServeContext): Promise; execute?: never; task?: never; }; type WithSchemas = Omit & { inputSchema: TInputSchema; outputSchema: TOutputSchema; }; type WithInputSchema = Omit & { inputSchema: TSchema; }; export declare function defineWorkflowTool, TReturn extends EntryPointReturn>>(definition: WithSchemas, TReturn>, TInputSchema, TOutputSchema>): WorkflowExecuteToolDefinition, InferOutput>; export declare function defineWorkflowTool, TReturn extends EntryPointReturn>>(definition: WithSchemas, TReturn>, TInputSchema, TOutputSchema>): WorkflowTaskToolDefinition, InferOutput>; export declare function defineWorkflowTool>(definition: WithSchemas, InferOutput>, TInputSchema, TOutputSchema>): WorkflowServeToolDefinition, InferOutput>; export declare function defineWorkflowTool>(definition: WithInputSchema, TReturn>, TSchema>): WorkflowExecuteToolDefinition, WorkflowReturn>; export declare function defineWorkflowTool>(definition: WithInputSchema, TReturn>, TSchema>): WorkflowTaskToolDefinition, WorkflowReturn>; export declare function defineWorkflowTool(definition: WithInputSchema, unknown>, TSchema>): WorkflowServeToolDefinition>; export declare function defineWorkflowTool>(definition: ExecuteDefinition, TReturn> & { inputSchema: JsonObject; }): WorkflowExecuteToolDefinition, WorkflowReturn>; export declare function defineWorkflowTool>(definition: TaskDefinition, TReturn> & { inputSchema: JsonObject; }): WorkflowTaskToolDefinition, WorkflowReturn>; export declare function defineWorkflowTool(definition: ServeDefinition, unknown> & { inputSchema: JsonObject; }): WorkflowServeToolDefinition>; export declare function defineWorkflowTool(definition: Unbranded>): WorkflowExecuteToolDefinition; export declare function defineWorkflowTool(definition: Unbranded>): WorkflowTaskToolDefinition; export declare function defineWorkflowTool(definition: Unbranded>): WorkflowServeToolDefinition; export declare function isWorkflowToolDefinition(value: unknown): boolean; /** The entry points a definition defines; a workflow tool defines exactly one. */ export declare function readWorkflowToolEntryPoints(definition: object): WorkflowToolEntryPoint[];