import { Tool, ModelMessage, Experimental_ToolCallerTool } from 'ai'; export { setMaxWorkers as experimental_setMaxWorkers } from 'run'; /** * Allows the model to call a tool directly. * * Use a string instead of a Symbol to allow for serializability. */ declare const DIRECT_TOOL_CALL = "AI_SDK_DIRECT_TOOL_CALL"; /** * Host tools made available to sandboxed code through the global `tools` object. */ type CodeModeToolSet = Record>; /** * AI SDK execution metadata forwarded to nested host tool calls. */ interface CodeModeToolExecutionOptions { toolCallId: string; messages: ModelMessage[]; abortSignal?: AbortSignal; experimental_context?: unknown; context?: unknown; codeModeInterrupt?: CodeModeInterruptExecutionContext; } /** * AI SDK tool returned by `createCodeModeTool`. */ type CodeModeTool = Tool & { description: string; }; /** * Input accepted by the generated code-mode tool. */ interface CodeModeToolInput { /** * JavaScript or type-stripped TypeScript source to execute. * * The source is wrapped in an async function, so top-level `await` and * `return` are supported. */ js: string; } type ApprovalDecision = 'approved' | 'denied' | { approved: boolean; reason?: string; }; interface CodeModeApprovalRequest { toolName: string; input: unknown; toolCallId: string; } interface CodeModeApprovalResponse { approvalId: string; approved: boolean; reason?: string; } interface CodeModeInterruptPayload { kind: string; [key: string]: unknown; } interface CodeModeInterruptResolution { interruptId: string; resolution: TResolution; } interface CodeModeInterruptExecutionContext { interruptId: string; payload: TPayload; resolution: TResolution; } interface CodeModeContinuationAuth { alg: 'HMAC-SHA256'; nonce: string; issuedAtMs: number; expiresAtMs: number; signature: string; } /** * Opaque continuation state for an interrupted code-mode invocation. * * @remarks * The token and compatibility metadata are authenticated. Applications should * persist this value without reading or modifying its fields. */ interface CodeModeContinuation { version: 2; js: string; outerToolCallId: string; toolNames: string[]; token: string; pendingInterruptions: CodeModePendingInterruption[]; resolutions: CodeModePendingResolution[]; auth: CodeModeContinuationAuth; } interface CodeModeInterrupt { type: 'code-mode-interrupt'; interruptId: string; toolName: string; toolCallId: string; outerToolCallId: string; input: unknown; payload: TPayload; continuation: CodeModeContinuation; } type CodeModeUnwrappedResult = { status: 'completed'; output: unknown; } | { status: 'interrupted'; interrupt: CodeModeInterrupt; }; interface CodeModeApprovalInterruptPayload extends CodeModeInterruptPayload { kind: 'ai-sdk-code-mode/tool-approval'; } type CodeModeApprovalInterrupt = CodeModeInterrupt; /** * Execution limits applied to each sandbox invocation. */ interface CodeModeExecutionPolicy { /** @defaultValue `30_000` */ timeoutMs?: number; /** @defaultValue `64 * 1024 * 1024` */ memoryLimitBytes?: number; /** @defaultValue `2 * 1024 * 1024` */ maxStackSizeBytes?: number; /** @defaultValue `1024 * 1024` */ maxResultBytes?: number; /** @defaultValue `64 * 1024` */ maxConsoleOutputBytes?: number; /** @defaultValue `256 * 1024` */ maxSourceBytes?: number; /** @defaultValue `1024 * 1024` */ maxToolInputBytes?: number; /** @defaultValue `4 * 1024 * 1024` */ maxToolOutputBytes?: number; /** @defaultValue `256` */ maxBridgeRequests?: number; /** @defaultValue `32` */ maxInFlightBridgeRequests?: number; } /** * Options used by `createCodeModeTool` and `runCodeMode`. */ interface CodeModeOptions { executionPolicy?: CodeModeExecutionPolicy; continuationSecurity?: CodeModeContinuationSecurityOptions; approval?: { /** * @defaultValue `'callback'` */ mode?: 'callback' | 'interrupt'; onApprovalRequired?: (request: CodeModeApprovalRequest) => Promise | ApprovalDecision; }; } /** * Options for the code-mode tool caller used with `experimental_toolCallers`. */ interface CodeModeToolOptions extends CodeModeOptions { /** * Controls how host tools are presented to the model. * * - `'description'`: include host-tool signatures in the provider-visible * code-mode tool description. * - `'conversation'`: keep the code-mode tool definition stable and announce * the current host-tool catalog in a user message. * * @defaultValue `'description'` */ toolDiscovery?: 'description' | 'conversation'; } interface CodeModeContinuationSecurityOptions { signingKey?: string | Uint8Array; /** * @defaultValue `60 * 60 * 1000` */ maxAgeMs?: number; } /** * Input for `runCodeMode`. */ interface RunCodeModeInput { js: string; tools: CodeModeToolSet; toolExecutionOptions?: Partial; options?: CodeModeOptions; continuation?: CodeModeContinuation; interruptResolution?: CodeModeInterruptResolution; } /** * Creates an AI SDK tool that executes code-mode TypeScript in an isolated * sandbox. */ declare function createCodeModeTool(tools: CodeModeToolSet, options?: CodeModeOptions): CodeModeTool; /** * Creates a code-mode caller whose host tools are bound by the surrounding * AI SDK generation call. */ declare function codeModeTool(options?: CodeModeToolOptions): Experimental_ToolCallerTool; declare function isCodeModeApprovalInterrupt(value: unknown, continuationSecurity?: CodeModeContinuationSecurityOptions): value is CodeModeApprovalInterrupt; declare function continueCodeModeApproval({ interrupt, approvalResponse, tools, options, toolExecutionOptions, }: { interrupt: CodeModeApprovalInterrupt; approvalResponse: CodeModeApprovalResponse; tools: CodeModeToolSet; options?: CodeModeOptions; toolExecutionOptions?: Partial; }): Promise; declare function toCodeModeApprovalMessages(interrupt: CodeModeApprovalInterrupt): ModelMessage[]; declare function getCodeModeApprovalResponse(messages: ModelMessage[], interrupt: CodeModeApprovalInterrupt): CodeModeApprovalResponse | undefined; /** * Base class for errors raised by code mode. * * All package-specific errors include a stable `code` string and may include * structured `details` for diagnostics. */ declare class CodeModeError extends Error { /** * Stable machine-readable error code. */ code: string; /** * Optional structured diagnostic details. */ readonly details?: unknown; constructor(message: string, code?: string, details?: unknown); } /** * Raised when a sandbox invocation exceeds its timeout. */ declare class CodeModeTimeoutError extends CodeModeError { constructor(timeoutMs: number); } /** * Raised when the outer AI SDK abort signal aborts a code-mode invocation. */ declare class CodeModeAbortedError extends CodeModeError { constructor(); } /** * Raised when the process-global worker cap has been reached. * * Configure the cap with `setMaxWorkers`. */ declare class CodeModeConcurrencyError extends CodeModeError { constructor(maxWorkers: number); } /** * Raised when the provided source exceeds `executionPolicy.maxSourceBytes`. */ declare class CodeModeSourceTooLargeError extends CodeModeError { constructor(bytes: number, maxBytes: number); } /** * Raised when sandboxed code exceeds bridge request limits. */ declare class CodeModeBridgeLimitError extends CodeModeError { constructor(message: string, details?: unknown); } /** * Raised when sandboxed code starts host bridge work and returns without * awaiting or otherwise observing it. */ declare class CodeModeDetachedBridgeRequestError extends CodeModeError { constructor(message: string, details?: unknown); } /** * Raised when the main thread and worker protocol observes an invalid or * mismatched message. */ declare class CodeModeProtocolError extends CodeModeError { constructor(message: string, details?: unknown); } /** * Base class for failures caused by nested host tool execution. */ declare class CodeModeToolError extends CodeModeError { constructor(message: string, details?: unknown); } declare class CodeModeToolApprovalRequiredError extends CodeModeToolError { constructor(toolName: string, input: unknown, toolCallId: string); } declare class CodeModeToolApprovalDeniedError extends CodeModeToolError { constructor(toolName: string, input: unknown, toolCallId: string, reason?: string); } declare function requestCodeModeInterrupt(payload: TPayload): never; declare function isCodeModeInterrupt(value: unknown, continuationSecurity?: CodeModeContinuationSecurityOptions): value is CodeModeInterrupt; declare function continueCodeModeInterrupt({ interrupt, resolution, tools, options, toolExecutionOptions, }: { interrupt: CodeModeInterrupt; resolution: TResolution; tools: CodeModeToolSet; options?: CodeModeOptions; toolExecutionOptions?: Partial; }): Promise; declare function getCodeModeInterrupt(result: unknown, continuationSecurity?: CodeModeContinuationSecurityOptions): CodeModeInterrupt | undefined; declare function unwrapCodeModeResult(result: unknown, continuationSecurity?: CodeModeContinuationSecurityOptions): CodeModeUnwrappedResult; /** * Runs one code-mode invocation directly without wrapping it as an AI SDK tool. * * The source is executed in a fresh QuickJS context with the provided host tools * and execution limits. * * @param input - Source code, host tools, forwarded tool execution options, and code-mode options. * @returns The JSON-serializable value returned by the sandboxed program. */ declare function runCodeMode(input: RunCodeModeInput): Promise; declare function setCodeModeContinuationSigningKey(key?: string | Uint8Array, options?: { maxAgeMs?: number; }): void; export { type ApprovalDecision, CodeModeAbortedError, type CodeModeApprovalInterrupt, type CodeModeApprovalRequest, type CodeModeApprovalResponse, CodeModeBridgeLimitError, CodeModeConcurrencyError, type CodeModeContinuation, type CodeModeContinuationSecurityOptions, CodeModeDetachedBridgeRequestError, CodeModeError, type CodeModeExecutionPolicy, type CodeModeInterrupt, type CodeModeInterruptExecutionContext, type CodeModeInterruptPayload, type CodeModeInterruptResolution, type CodeModeOptions, CodeModeProtocolError, CodeModeSourceTooLargeError, CodeModeTimeoutError, type CodeModeTool, CodeModeToolApprovalDeniedError, CodeModeToolApprovalRequiredError, CodeModeToolError, type CodeModeToolExecutionOptions, type CodeModeToolInput, type CodeModeToolOptions, type CodeModeToolSet, type CodeModeUnwrappedResult, DIRECT_TOOL_CALL, type RunCodeModeInput, codeModeTool as experimental_codeModeTool, continueCodeModeApproval as experimental_continueCodeModeApproval, continueCodeModeInterrupt as experimental_continueCodeModeInterrupt, createCodeModeTool as experimental_createCodeModeTool, getCodeModeApprovalResponse as experimental_getCodeModeApprovalResponse, getCodeModeInterrupt as experimental_getCodeModeInterrupt, isCodeModeApprovalInterrupt as experimental_isCodeModeApprovalInterrupt, isCodeModeInterrupt as experimental_isCodeModeInterrupt, requestCodeModeInterrupt as experimental_requestCodeModeInterrupt, runCodeMode as experimental_runCodeMode, setCodeModeContinuationSigningKey as experimental_setCodeModeContinuationSigningKey, toCodeModeApprovalMessages as experimental_toCodeModeApprovalMessages, unwrapCodeModeResult as experimental_unwrapCodeModeResult };