/** * Framework-free core of the `useAgentRun` React hook. * * The React hook (`useAgentRun.ts`) is a thin wrapper around the pieces here: * keeping the state machine and the stream-driving loop out of React lets us * exhaustively unit-test the client-tool round-trip and the approval resume * without a React testing harness (the framework intentionally ships none). * * Builds on the named-event agent-SSE protocol in `../agent-sse.ts` * (`readAgentStream` + the `AgentStreamTurn` it accumulates). No `react` and no * `node:` imports — safe in any `fetch`-capable runtime. */ import type { AgentStreamCallbacks, AgentStreamTurn } from '../agent-sse.js'; import type { ToolCall } from '../types.js'; /** One renderable entry in a run's transcript, in arrival order. */ export type AgentRunOutput = { type: 'text'; text: string; } | { type: 'tool_call'; id?: string; tool: string; input?: Record; } | { type: 'tool_update'; id?: string; tool?: string; update: unknown; } | { type: 'tool_result'; id?: string; tool?: string; content: string; } | { type: 'approval_request'; toolCall: ToolCall; isClientTool: boolean; } | { type: 'handoff'; from: string; to: string; message?: string; } | { type: 'error'; message: string; }; /** * Append one decoded SSE event to the output transcript, returning a NEW array * (so a React consumer can use it as immutable state). Consecutive `text` * deltas coalesce into the trailing text entry so streamed text renders as one * growing block. `complete` and `pending_client_tools` produce no transcript * entry — they drive run status / pending-action state instead. */ export declare function appendAgentOutput(outputs: AgentRunOutput[], event: string, data: unknown): AgentRunOutput[]; /** A client-executed tool result, keyed to its originating tool call. */ export interface AgentToolResult { toolCallId: string; result: unknown; } /** * Run each pending client-tool call through the resolver, in order, collecting * results keyed by `toolCallId`. A resolver that throws yields an * `{ error }` result rather than aborting the batch — the model sees the * failure as that tool's result and can recover, matching the server-side * tool-error posture. */ export declare function executeClientTools(calls: ToolCall[], resolver: (call: ToolCall) => unknown | Promise): Promise; /** * The intent the consumer's `request` function turns into an SSE `Response`. * The hook owns the run/resume state machine; the app owns the endpoint and * the request body shape (only the app's route knows how to reconstruct the * server-side message history), so the driver hands it a typed intent and * expects a streaming `Response` back. */ export type AgentRunRequest = { type: 'run'; input: TInput; } | { type: 'resume'; /** The accumulated turn the resume continues from. */ turn: AgentStreamTurn; /** Results from client-tool calls executed in the browser. */ clientToolResults: AgentToolResult[]; /** Tool-call ids the user approved. */ approved: string[]; /** Tool-call ids the user rejected. */ rejected: string[]; }; export interface AgentRunDriverOptions { /** Turn a run/resume intent into the streaming SSE `Response`. */ request: (req: AgentRunRequest, signal: AbortSignal) => Promise; /** * Optional client-tool resolver. When provided, a run that pauses awaiting * client tools auto-executes them and resumes — no manual `respond`. Omit it * to surface the pending calls and resume by hand. */ clientTools?: (call: ToolCall) => unknown | Promise; /** Forwarded to `readAgentStream` so the consumer sees events as they arrive. */ callbacks?: AgentStreamCallbacks; signal: AbortSignal; } /** * Drive one logical agent run to a settling point: stream the response, * accumulate the turn, and — when a client-tool resolver is configured — * auto-resume across client-tool pauses until the run completes or parks on an * approval gate / manual client-tool round-trip. Returns the final * {@link AgentStreamTurn}. Framework-free; the hook wires `callbacks` to state. */ export declare function driveAgentRun(initial: AgentRunRequest, opts: AgentRunDriverOptions): Promise; //# sourceMappingURL=agent-run.d.ts.map