/** * @license * Copyright 2026 Google LLC * SPDX-License-Identifier: Apache-2.0 */ import { z } from 'zod'; import { zodToJsonSchema } from 'zod-to-json-schema'; import { BaseDeclarativeTool, BaseToolInvocation, type ToolResult, type ToolInvocation, Kind, type MessageBus, } from '@google/gemini-cli-core'; export { z }; export interface ToolDefinition { name: string; description: string; inputSchema: T; } export interface Tool extends ToolDefinition { action: (params: z.infer) => Promise; } class SdkToolInvocation extends BaseToolInvocation< z.infer, ToolResult > { constructor( params: z.infer, messageBus: MessageBus, private readonly action: (params: z.infer) => Promise, toolName: string, ) { super(params, messageBus, toolName); } getDescription(): string { return `Executing ${this._toolName}...`; } async execute( _signal: AbortSignal, _updateOutput?: (output: string) => void, ): Promise { try { const result = await this.action(this.params); const output = typeof result === 'string' ? result : JSON.stringify(result, null, 2); return { llmContent: output, returnDisplay: output, }; } catch (error) { const errorMessage = error instanceof Error ? error.message : String(error); return { llmContent: `Error: ${errorMessage}`, returnDisplay: `Error: ${errorMessage}`, error: { message: errorMessage, }, }; } } } export class SdkTool extends BaseDeclarativeTool< z.infer, ToolResult > { constructor( private readonly definition: Tool, messageBus: MessageBus, ) { super( definition.name, definition.name, definition.description, Kind.Other, zodToJsonSchema(definition.inputSchema), messageBus, ); } protected createInvocation( params: z.infer, messageBus: MessageBus, toolName?: string, ): ToolInvocation, ToolResult> { return new SdkToolInvocation( params, messageBus, this.definition.action, toolName || this.name, ); } } export function tool( definition: ToolDefinition, action: (params: z.infer) => Promise, ): Tool { return { ...definition, action, }; }