import type { Agent } from './agent'; import type { Computer } from './computer'; import type { Shell, ShellAction } from './shell'; import type { Editor, ApplyPatchOperation } from './editor'; import { JsonObjectSchema, JsonObjectSchemaNonStrict, JsonObjectSchemaStrict, UnknownContext } from './types'; import { RunContext } from './runContext'; import type { RunConfig } from './run'; import type { RunResult } from './result'; import { ToolTimeoutError } from './errors'; import { RunToolApprovalItem, RunToolCallOutputItem } from './items'; import * as ProviderData from './types/providerData'; import * as protocol from './types/protocol'; import type { ZodInfer, ZodObjectLike } from './utils/zodCompat'; import { ToolInputGuardrailDefinition, ToolOutputGuardrailDefinition, ToolInputGuardrailFunction, ToolOutputGuardrailFunction } from './toolGuardrail'; export type { ToolOutputText, ToolOutputImage, ToolOutputFileContent, ToolCallStructuredOutput, ToolCallOutputContent, } from './types/protocol'; /** * A function that determines if a tool call should be approved. * * @param runContext The current run context * @param input The input to the tool * @param callId The ID of the tool call * @returns True if the tool call should be approved, false otherwise */ export type ToolApprovalFunction = (runContext: RunContext, input: ToolExecuteArgument, callId?: string) => Promise; export type ToolCallDetails = { toolCall?: protocol.FunctionCallItem; resumeState?: string; signal?: AbortSignal; /** * Internal: parent runner config for nested agent-tool runs (Agent.asTool). */ parentRunConfig?: Partial; }; export type FunctionToolTimeoutBehavior = 'error_as_result' | 'raise_exception'; export type ToolTimeoutErrorFunction = (context: RunContext, error: ToolTimeoutError) => Promise | string; export type ShellApprovalFunction = (runContext: RunContext, action: ShellAction, callId?: string) => Promise; export type ShellOnApprovalFunction = (runContext: RunContext, approvalItem: RunToolApprovalItem) => Promise<{ approve: boolean; reason?: string; }>; export type ShellToolLocalSkill = { description: string; name: string; path: string; }; export type ShellToolSkillReference = { type: 'skill_reference'; skillId: string; version?: string; }; export type ShellToolInlineSkillSource = { data: string; mediaType: 'application/zip'; type: 'base64'; }; export type ShellToolInlineSkill = { description: string; name: string; source: ShellToolInlineSkillSource; type: 'inline'; }; export type ShellToolContainerSkill = ShellToolSkillReference | ShellToolInlineSkill; export type ShellToolContainerNetworkPolicyDomainSecret = { domain: string; name: string; value: string; }; export type ShellToolContainerNetworkPolicyAllowlist = { allowedDomains: string[]; domainSecrets?: ShellToolContainerNetworkPolicyDomainSecret[]; type: 'allowlist'; }; export type ShellToolContainerNetworkPolicyDisabled = { type: 'disabled'; }; export type ShellToolContainerNetworkPolicy = ShellToolContainerNetworkPolicyAllowlist | ShellToolContainerNetworkPolicyDisabled; export type ShellToolLocalEnvironment = { type: 'local'; skills?: ShellToolLocalSkill[]; }; export type ShellToolContainerAutoEnvironment = { type: 'container_auto'; fileIds?: string[]; memoryLimit?: '1g' | '4g' | '16g' | '64g' | null; networkPolicy?: ShellToolContainerNetworkPolicy; skills?: ShellToolContainerSkill[]; }; export type ShellToolContainerReferenceEnvironment = { type: 'container_reference'; containerId: string; }; export type ShellToolHostedEnvironment = ShellToolContainerAutoEnvironment | ShellToolContainerReferenceEnvironment; export type ShellToolEnvironment = ShellToolLocalEnvironment | ShellToolHostedEnvironment; export type ApplyPatchApprovalFunction = (runContext: RunContext, operation: ApplyPatchOperation, callId?: string) => Promise; export type ApplyPatchOnApprovalFunction = (runContext: RunContext, approvalItem: RunToolApprovalItem) => Promise<{ approve: boolean; reason?: string; }>; export type ComputerApprovalFunction = (runContext: RunContext, action: protocol.ComputerAction, callId?: string) => Promise; export type ComputerSafetyCheck = { id: string; code: string; message?: string; [key: string]: unknown; }; export type ComputerSafetyCheckResult = void | boolean | { acknowledgedSafetyChecks: ComputerSafetyCheck[]; } | { acknowledged_safety_checks: ComputerSafetyCheck[]; }; export type ComputerOnSafetyCheckFunction = (args: { runContext: RunContext; pendingSafetyChecks: ComputerSafetyCheck[]; toolCall: protocol.ComputerUseCallItem; }) => Promise; export type ToolEnabledFunction = (runContext: RunContext, agent: Agent) => Promise; type ToolEnabledPredicate = (args: { runContext: RunContext; agent: Agent; }) => boolean | Promise; type ToolEnabledOption = boolean | ToolEnabledPredicate; /** * Exposes a function to the agent as a tool to be called * * @param Context The context of the tool * @param Result The result of the tool */ export type FunctionTool = { type: 'function'; /** * The name of the tool. */ name: string; /** * The description of the tool that helps the model to understand when to use the tool */ description: string; /** * A JSON schema describing the parameters of the tool. */ parameters: JsonObjectSchema; /** * Whether the tool is strict. If true, the model must try to strictly follow the schema (might result in slower response times). */ strict: boolean; /** * Responses API only. Hides a top-level function tool definition until tool search loads it. */ deferLoading?: boolean; /** * The function to invoke when the tool is called. */ invoke: (runContext: RunContext, input: string, details?: ToolCallDetails) => Promise; /** * Whether the tool needs human approval before it can be called. If this is true, the run will result in an `interruption` that the * program has to resolve by approving or rejecting the tool call. */ needsApproval: ToolApprovalFunction; /** * Optional timeout in milliseconds for each tool invocation. */ timeoutMs?: number; /** * Defines how timeout errors are handled. * * - `error_as_result`: return a model-visible timeout message. * - `raise_exception`: raise `ToolTimeoutError` and fail the run. */ timeoutBehavior?: FunctionToolTimeoutBehavior; /** * Optional formatter for timeout errors when timeoutBehavior is `error_as_result`. */ timeoutErrorFunction?: ToolTimeoutErrorFunction; /** * Determines whether the tool should be made available to the model for the current run. */ isEnabled: ToolEnabledFunction; /** * Guardrails that run before the tool executes. */ inputGuardrails?: ToolInputGuardrailDefinition[]; /** * Guardrails that run after the tool executes. */ outputGuardrails?: ToolOutputGuardrailDefinition[]; }; /** * Arguments provided to computer initializers. */ export type ComputerInitializerArgs = { runContext: RunContext; }; /** * A function that initializes a computer for the current run. */ type BivariantComputerCreate = { bivarianceHack: (args: ComputerInitializerArgs) => TComputer | Promise; }['bivarianceHack']; export type ComputerCreate = BivariantComputerCreate; /** * Optional cleanup invoked after a run finishes when the computer was created via an initializer. */ type BivariantComputerDispose = { bivarianceHack: (args: ComputerInitializerArgs & { computer: TComputer; }) => void | Promise; }['bivarianceHack']; export type ComputerDispose = BivariantComputerDispose; /** * Initializes a computer for the current run and optionally tears it down after the run. */ export type ComputerProvider = { create: ComputerCreate; dispose?: ComputerDispose; }; type ComputerInitializer = ComputerCreate | ComputerProvider; export type ComputerConfig = Computer | ComputerInitializer; /** * Exposes a computer to the model as a tool to be called * * @param Context The context of the tool * @param Result The result of the tool */ export type ComputerTool = { type: 'computer'; /** * The name of the tool. */ name: 'computer_use_preview' | (string & {}); /** * The computer to use. */ computer: ComputerConfig; /** * Predicate determining whether this computer action requires approval. */ needsApproval: ComputerApprovalFunction; /** * Optional handler to acknowledge pending safety checks. */ onSafetyCheck?: ComputerOnSafetyCheckFunction; }; /** * Exposes a computer to the agent as a tool to be called. * * @param options Additional configuration for the computer tool like specifying the location of your agent * @returns a computer tool definition */ export declare function computerTool(options: { name?: string; computer: ComputerConfig; needsApproval?: boolean | ComputerApprovalFunction; onSafetyCheck?: ComputerOnSafetyCheckFunction; }): ComputerTool; type ShellToolBase = { type: 'shell'; /** * Public name exposed to the model. Defaults to `shell`. */ name: string; /** * Predicate determining whether this shell action requires approval. */ needsApproval: ShellApprovalFunction; /** * Optional handler to auto-approve or reject when approval is required. * If provided, it will be invoked immediately when an approval is needed. */ onApproval?: ShellOnApprovalFunction; }; type LocalShellTool = ShellToolBase & { /** * Optional for backward compatibility with direct `ShellTool` literals. * When omitted, local mode is assumed. */ environment?: ShellToolLocalEnvironment; /** * The shell implementation to execute commands in local mode. */ shell: Shell; }; type NormalizedLocalShellTool = Omit & { environment: ShellToolLocalEnvironment; }; type HostedShellTool = ShellToolBase & { environment: ShellToolHostedEnvironment; /** * Hosted environments do not accept local shell implementations. */ shell?: never; }; export type ShellTool = LocalShellTool | HostedShellTool; type LocalShellToolOptions = { name?: string; environment?: ShellToolLocalEnvironment; shell: Shell; needsApproval?: boolean | ShellApprovalFunction; onApproval?: ShellOnApprovalFunction; }; type HostedShellToolOptions = { name?: string; environment: ShellToolHostedEnvironment; shell?: never; needsApproval?: never; onApproval?: never; }; export declare function shellTool(options: LocalShellToolOptions): NormalizedLocalShellTool; export declare function shellTool(options: HostedShellToolOptions): HostedShellTool; export type ApplyPatchTool = { type: 'apply_patch'; /** * Public name exposed to the model. Defaults to `apply_patch`. */ name: string; /** * Diff applier invoked when the tool is called. */ editor: Editor; /** * Predicate determining whether this apply_patch operation requires approval. */ needsApproval: ApplyPatchApprovalFunction; /** * Optional handler to auto-approve or reject when approval is required. */ onApproval?: ApplyPatchOnApprovalFunction; }; export declare function applyPatchTool(options: Partial> & { editor: Editor; needsApproval?: boolean | ApplyPatchApprovalFunction; onApproval?: ApplyPatchOnApprovalFunction; }): ApplyPatchTool; export type HostedMCPApprovalFunction = (context: RunContext, data: RunToolApprovalItem) => Promise<{ approve: boolean; reason?: string; }>; /** * A hosted MCP tool that lets the model call a remote MCP server directly * without a round trip back to your code. */ export type HostedMCPTool = HostedTool & { name: 'hosted_mcp'; providerData: ProviderData.HostedMCPTool; }; /** * Creates a hosted MCP tool definition. * * @param options - Configuration for the hosted MCP tool, including server connection details * and approval requirements. */ export declare function hostedMcpTool(options: { allowedTools?: string[] | { toolNames?: string[]; }; deferLoading?: boolean; serverDescription?: string; } & ({ serverLabel: string; serverUrl?: string; authorization?: string; headers?: Record; } | { serverLabel: string; connectorId: string; authorization?: string; headers?: Record; }) & ({ requireApproval?: never; } | { requireApproval: 'never'; } | { requireApproval: 'always' | { never?: { toolNames: string[]; }; always?: { toolNames: string[]; }; }; onApproval?: HostedMCPApprovalFunction; })): HostedMCPTool; /** * A built-in hosted tool that will be executed directly by the model during the request and won't result in local code executions. * Examples of these are `web_search_call` or `file_search_call`. * * @param Context The context of the tool * @param Result The result of the tool */ export type HostedTool = { type: 'hosted_tool'; /** * A unique name for the tool. */ name: string; /** * Additional configuration data that gets passed to the tool */ providerData?: Record; }; export type ClientToolSearchLoadDefaultFunction = (paths: string[]) => Tool[]; export type ClientToolSearchExecutorArgs = { agent: Agent; availableTools: Tool[]; loadDefault: ClientToolSearchLoadDefaultFunction; runContext: RunContext; toolCall: protocol.ToolSearchCallItem; }; export type ClientToolSearchExecutorResult = Tool | Tool[] | null | undefined; export type ClientToolSearchExecutor = (args: ClientToolSearchExecutorArgs) => ClientToolSearchExecutorResult | Promise>; export declare const CLIENT_TOOL_SEARCH_EXECUTOR: unique symbol; export declare function attachClientToolSearchExecutor(tool: HostedTool, executor: ClientToolSearchExecutor): HostedTool; export declare function getClientToolSearchExecutor(tool: Tool): ClientToolSearchExecutor | undefined; export declare function getToolSearchRuntimeToolKey(tool: Tool): string | undefined; /** * A tool that can be called by the model. * @template Context The context passed to the tool */ export type Tool = FunctionTool | ComputerTool | ShellTool | ApplyPatchTool | HostedTool; /** * The result of invoking a function tool. Either the actual output of the execution or a tool * approval request. * * These get passed for example to the `toolUseBehavior` option of the `Agent` constructor. */ export type FunctionToolResult = { type: 'function_output'; /** * The tool that was called. */ tool: FunctionTool; /** * The output of the tool call. This can be a string or a stringifable item. */ output: string | unknown; /** * The run item representing the tool call output. */ runItem: RunToolCallOutputItem; /** * The result returned when the tool execution runs another agent. Populated when the * invocation originated from {@link Agent.asTool} and the nested agent completed a run. */ agentRunResult?: RunResult>; /** * Any interruptions collected while the nested agent executed. These are surfaced to allow * callers to pause and resume workflows that require approvals. */ interruptions?: RunToolApprovalItem[]; } | { /** * Indicates that the tool requires approval before it can be called. */ type: 'function_approval'; /** * The tool that is requiring to be approved. */ tool: FunctionTool; /** * The item representing the tool call that is requiring approval. */ runItem: RunToolApprovalItem; } | { /** * Indicates that the tool requires approval before it can be called. */ type: 'hosted_mcp_tool_approval'; /** * The tool that is requiring to be approved. */ tool: HostedMCPTool; /** * The item representing the tool call that is requiring approval. */ runItem: RunToolApprovalItem; }; /** * The parameters of a tool. * * This can be a Zod schema, a JSON schema or undefined. * * If a Zod schema is provided, the arguments to the tool will automatically be parsed and validated * against the schema. * * If a JSON schema is provided, the arguments to the tool will be passed as is. * * If undefined is provided, the arguments to the tool will be passed as a string. */ export type ToolInputParameters = undefined | ZodObjectLike | JsonObjectSchema; /** * The parameters of a tool that has strict mode enabled. * * This can be a Zod schema, a JSON schema or undefined. * * If a Zod schema is provided, the arguments to the tool will automatically be parsed and validated * against the schema. * * If a JSON schema is provided, the arguments to the tool will be parsed as JSON but not validated. * * If undefined is provided, the arguments to the tool will be passed as a string. */ export type ToolInputParametersStrict = undefined | ZodObjectLike | JsonObjectSchemaStrict; /** * The parameters of a tool that has strict mode disabled. * * If a JSON schema is provided, the arguments to the tool will be parsed as JSON but not validated. * * Zod schemas are not supported without strict: true. */ export type ToolInputParametersNonStrict = undefined | JsonObjectSchemaNonStrict; /** * The arguments to a tool. * * The type of the arguments are derived from the parameters passed to the tool definition. * * If the parameters are passed as a JSON schema the type is `unknown`. For Zod schemas it will * match the inferred Zod type. Otherwise the type is `string` */ export type ToolExecuteArgument = TParameters extends ZodObjectLike ? ZodInfer : TParameters extends JsonObjectSchema ? unknown : string; /** * The function to invoke when the tool is called. * * @param input The arguments to the tool (see ToolExecuteArgument) * @param context An instance of the current RunContext */ type ToolExecuteFunction = (input: ToolExecuteArgument, context?: RunContext, details?: ToolCallDetails) => Promise | unknown; /** * The function to invoke when an error occurs while running the tool. This can be used to define * what the model should receive as tool output in case of an error. It can be used to provide * for example additional context or a fallback value. * * @param context An instance of the current RunContext * @param error The error that occurred */ type ToolErrorFunction = (context: RunContext, error: Error | unknown) => Promise | string; type ToolGuardrailOptions = { /** * Guardrails that validate or block tool invocation before it runs. */ inputGuardrails?: ToolInputGuardrailDefinition[] | { name: string; run: ToolInputGuardrailFunction; }[]; /** * Guardrails that validate or alter tool output after it runs. */ outputGuardrails?: ToolOutputGuardrailDefinition[] | { name: string; run: ToolOutputGuardrailFunction; }[]; }; /** * The options for a tool that has strict mode enabled. * * @param TParameters The parameters of the tool * @param Context The context of the tool */ type StrictToolOptions = ToolGuardrailOptions & { /** * The name of the tool. Must be unique within the agent. */ name?: string; /** * The description of the tool. This is used to help the model understand when to use the tool. */ description: string; /** * A Zod schema or JSON schema describing the parameters of the tool. * If a Zod schema is provided, the arguments to the tool will automatically be parsed and validated * against the schema. */ parameters: TParameters; /** * Whether the tool is strict. If true, the model must try to strictly follow the schema (might result in slower response times). */ strict?: true; /** * Responses API only. Hides a top-level function tool definition until tool search loads it. */ deferLoading?: boolean; /** * The function to invoke when the tool is called. */ execute: ToolExecuteFunction; /** * The function to invoke when an error occurs while running the tool. */ errorFunction?: ToolErrorFunction | null; /** * Whether the tool needs human approval before it can be called. If this is true, the run will result in an `interruption` that the * program has to resolve by approving or rejecting the tool call. */ needsApproval?: boolean | ToolApprovalFunction; /** * Determines whether the tool should be exposed to the model for the current run. */ isEnabled?: ToolEnabledOption; /** * Optional timeout in milliseconds for each tool call. */ timeoutMs?: number; /** * Timeout handling mode. `error_as_result` returns a model-visible message and * `raise_exception` throws `ToolTimeoutError`. */ timeoutBehavior?: FunctionToolTimeoutBehavior; /** * Optional formatter used for timeout messages when timeoutBehavior is `error_as_result`. */ timeoutErrorFunction?: ToolTimeoutErrorFunction; }; /** * The options for a tool that has strict mode disabled. * * @param TParameters The parameters of the tool * @param Context The context of the tool */ type NonStrictToolOptions = ToolGuardrailOptions & { /** * The name of the tool. Must be unique within the agent. */ name?: string; /** * The description of the tool. This is used to help the model understand when to use the tool. */ description: string; /** * A JSON schema of the tool. To use a Zod schema, you need to use a `strict` schema. */ parameters: TParameters; /** * Whether the tool is strict If true, the model must try to strictly follow the schema (might result in slower response times). */ strict: false; /** * Responses API only. Hides a top-level function tool definition until tool search loads it. */ deferLoading?: boolean; /** * The function to invoke when the tool is called. */ execute: ToolExecuteFunction; /** * The function to invoke when an error occurs while running the tool. */ errorFunction?: ToolErrorFunction | null; /** * Whether the tool needs human approval before it can be called. If this is true, the run will result in an `interruption` that the * program has to resolve by approving or rejecting the tool call. */ needsApproval?: boolean | ToolApprovalFunction; /** * Determines whether the tool should be exposed to the model for the current run. */ isEnabled?: ToolEnabledOption; /** * Optional timeout in milliseconds for each tool call. */ timeoutMs?: number; /** * Timeout handling mode. `error_as_result` returns a model-visible message and * `raise_exception` throws `ToolTimeoutError`. */ timeoutBehavior?: FunctionToolTimeoutBehavior; /** * Optional formatter used for timeout messages when timeoutBehavior is `error_as_result`. */ timeoutErrorFunction?: ToolTimeoutErrorFunction; }; /** * The options for a tool. * * @param TParameters The parameters of the tool * @param Context The context of the tool */ export type ToolOptions = StrictToolOptions, Context> | NonStrictToolOptions, Context>; export type ToolOptionsWithGuardrails = ToolOptions; type AnyFunctionTool = FunctionTool; type FunctionToolInvocationArgs = { tool: FunctionTool; runContext: RunContext; input: string; details?: ToolCallDetails; }; /** * Invoke a function tool while enforcing optional per-tool timeout settings. */ export declare function invokeFunctionTool(args: FunctionToolInvocationArgs): Promise; /** * Exposes a function to the agent as a tool to be called * * @param options The options for the tool * @returns A new tool */ export declare function tool(options: ToolOptions): FunctionTool; export type ToolNamespaceOptions = { /** * The namespace name to expose to the model. */ name: string; /** * Description shared by all tools in the namespace. */ description: string; /** * Function tools to attach to the namespace. */ tools: TTools; }; /** * Responses API only. Group function tools under a shared namespace. * * @param options - Namespace metadata and function tools to attach. * @returns shallow-cloned function tools carrying namespace metadata. */ export declare function toolNamespace(options: ToolNamespaceOptions): TTools;