/** * @license * Copyright 2025 Google LLC * SPDX-License-Identifier: Apache-2.0 */ import { FunctionDeclaration, Schema } from '@google/genai'; import { z as z3 } from 'zod/v3'; import { z as z4 } from 'zod/v4'; import { Context } from '../agents/context.js'; import { BaseTool, RunAsyncToolRequest } from './base_tool.js'; /** * Input parameters of the function tool. */ export type ToolInputParameters = z3.ZodObject | z4.ZodObject | Schema | undefined; /** * The arguments passed to the function tool's `execute` callback, inferred * from the `parameters` schema type. */ export type ToolExecuteArgument = TParameters extends z3.ZodObject ? z3.infer> : TParameters extends z4.ZodObject ? z4.infer> : TParameters extends Schema ? unknown : string; /** * The signature of the user-provided function executed by a {@link FunctionTool}. */ export type ToolExecuteFunction = (input: ToolExecuteArgument, toolContext?: Context) => Promise | unknown; /** * Whether a {@link FunctionTool} requires user confirmation before it runs: a * boolean, or a predicate over the (validated) call arguments and tool context. * See {@link ToolOptions.requireConfirmation}. */ export type RequireConfirmation = boolean | ((input: ToolExecuteArgument, toolContext?: Context) => boolean | Promise); /** * The configuration options for creating a function-based tool. * The `name`, `description` and `parameters` fields are used to generate the * tool definition that is passed to the LLM prompt. * * Note: Unlike Python's ADK, JSDoc on the `execute` function is ignored * for tool definition generation. */ export type ToolOptions = { name?: string; description: string; parameters?: TParameters; execute: ToolExecuteFunction; isLongRunning?: boolean; /** * Whether this tool requires user confirmation before it runs. A boolean, or * a predicate over the (validated) call arguments and tool context returning * a boolean. * * The HITL gate is enforced when the tool is invoked through an `LlmAgent` * turn: `agents/functions.ts` surfaces an `adk_request_confirmation` * interrupt from the tool's `requestedToolConfirmations`, and the tool only * executes once the user approves (via the * `RequestConfirmationLlmRequestProcessor`). * * NOTE: a workflow `ToolNode` does not yet route through that path, so a * `requireConfirmation` tool used directly as a node does not pause — it * returns the "requires confirmation" error as its node output. Approval for * workflow nodes is not wired up. Mirrors Python's * `FunctionTool(require_confirmation=...)`. */ requireConfirmation?: RequireConfirmation; }; /** * A unique symbol to identify ADK agent classes. * Defined once and shared by all BaseTool instances. */ declare const FUNCTION_TOOL_SIGNATURE_SYMBOL: unique symbol; /** * Type guard to check if an object is an instance of BaseTool. * @param obj The object to check. * @returns True if the object is an instance of BaseTool, false otherwise. */ export declare function isFunctionTool(obj: unknown): obj is FunctionTool; /** * A tool that wraps a user-defined function, making it callable by an LLM. * * The function's name, description, and parameter schema are exposed to the * model as a function declaration. When the model requests a call, the * framework validates the arguments and invokes the user-provided `execute` * callback. */ export declare class FunctionTool extends BaseTool { /** A unique symbol to identify ADK function tool class. */ readonly [FUNCTION_TOOL_SIGNATURE_SYMBOL] = true; private readonly execute; private readonly parameters?; private readonly requireConfirmation; /** * The constructor acts as the user-friendly factory. * @param options The configuration for the tool. */ constructor(options: ToolOptions); /** * Returns the function declaration derived from the tool's name, description, * and parameter schema. */ _getDeclaration(): FunctionDeclaration; /** * Validates the model-provided arguments against the parameter schema and * invokes the user-defined `execute` function. * * @param req The tool request containing arguments and tool context. * @returns A promise resolving to the function's return value. */ runAsync(req: RunAsyncToolRequest): Promise; /** * Whether this call is gated on human approval — the static flag, or the * predicate evaluated against the validated arguments. * * @param args The arguments the tool would run with. * @param toolContext The context of the call, when there is one. * @return Whether the call requires confirmation. */ checkRequireConfirmation(args: Record, toolContext?: Context): Promise; /** Parses `args` against the parameter schema, when one is declared. */ private validateArgs; /** Resolves `requireConfirmation`, which may be a flag or a predicate. */ private evaluateRequireConfirmation; /** * Evaluates the confirmation gate. Returns `undefined` if the tool may * proceed; otherwise returns the function response payload to surface instead * of running (a request-for-confirmation on the first pass, or a rejection * once the user declined). */ private checkConfirmation; } export {};