diff options
| author | Wanlin Du <[email protected]> | 2025-08-11 16:12:41 -0700 |
|---|---|---|
| committer | GitHub <[email protected]> | 2025-08-11 23:12:41 +0000 |
| commit | d9fb08c9da3d2e8c501ec9badb2e2bd79eb15b93 (patch) | |
| tree | 7d07e1586b736c1b6c44e28828aa29205af335ac /packages/core/src/tools/tool-registry.ts | |
| parent | f52d073dfbfa4d5091a74bf33ac1c66e51265247 (diff) | |
feat: migrate tools to use parametersJsonSchema. (#5330)
Diffstat (limited to 'packages/core/src/tools/tool-registry.ts')
| -rw-r--r-- | packages/core/src/tools/tool-registry.ts | 84 |
1 files changed, 5 insertions, 79 deletions
diff --git a/packages/core/src/tools/tool-registry.ts b/packages/core/src/tools/tool-registry.ts index 70226052..17d324b3 100644 --- a/packages/core/src/tools/tool-registry.ts +++ b/packages/core/src/tools/tool-registry.ts @@ -4,7 +4,7 @@ * SPDX-License-Identifier: Apache-2.0 */ -import { FunctionDeclaration, Schema, Type } from '@google/genai'; +import { FunctionDeclaration } from '@google/genai'; import { AnyDeclarativeTool, Icon, ToolResult, BaseTool } from './tools.js'; import { Config } from '../config/config.js'; import { spawn } from 'node:child_process'; @@ -331,14 +331,12 @@ export class ToolRegistry { console.warn('Discovered a tool with no name. Skipping.'); continue; } - // Sanitize the parameters before registering the tool. const parameters = - func.parameters && - typeof func.parameters === 'object' && - !Array.isArray(func.parameters) - ? (func.parameters as Schema) + func.parametersJsonSchema && + typeof func.parametersJsonSchema === 'object' && + !Array.isArray(func.parametersJsonSchema) + ? func.parametersJsonSchema : {}; - sanitizeParameters(parameters); this.registerTool( new DiscoveredTool( this.config, @@ -413,75 +411,3 @@ export class ToolRegistry { return this.tools.get(name); } } - -/** - * Sanitizes a schema object in-place to ensure compatibility with the Gemini API. - * - * NOTE: This function mutates the passed schema object. - * - * It performs the following actions: - * - Removes the `default` property when `anyOf` is present. - * - Removes unsupported `format` values from string properties, keeping only 'enum' and 'date-time'. - * - Recursively sanitizes nested schemas within `anyOf`, `items`, and `properties`. - * - Handles circular references within the schema to prevent infinite loops. - * - * @param schema The schema object to sanitize. It will be modified directly. - */ -export function sanitizeParameters(schema?: Schema) { - _sanitizeParameters(schema, new Set<Schema>()); -} - -/** - * Internal recursive implementation for sanitizeParameters. - * @param schema The schema object to sanitize. - * @param visited A set used to track visited schema objects during recursion. - */ -function _sanitizeParameters(schema: Schema | undefined, visited: Set<Schema>) { - if (!schema || visited.has(schema)) { - return; - } - visited.add(schema); - - if (schema.anyOf) { - // Vertex AI gets confused if both anyOf and default are set. - schema.default = undefined; - for (const item of schema.anyOf) { - if (typeof item !== 'boolean') { - _sanitizeParameters(item, visited); - } - } - } - if (schema.items && typeof schema.items !== 'boolean') { - _sanitizeParameters(schema.items, visited); - } - if (schema.properties) { - for (const item of Object.values(schema.properties)) { - if (typeof item !== 'boolean') { - _sanitizeParameters(item, visited); - } - } - } - - // Handle enum values - Gemini API only allows enum for STRING type - if (schema.enum && Array.isArray(schema.enum)) { - if (schema.type !== Type.STRING) { - // If enum is present but type is not STRING, convert type to STRING - schema.type = Type.STRING; - } - // Filter out null and undefined values, then convert remaining values to strings for Gemini API compatibility - schema.enum = schema.enum - .filter((value: unknown) => value !== null && value !== undefined) - .map((value: unknown) => String(value)); - } - - // Vertex AI only supports 'enum' and 'date-time' for STRING format. - if (schema.type === Type.STRING) { - if ( - schema.format && - schema.format !== 'enum' && - schema.format !== 'date-time' - ) { - schema.format = undefined; - } - } -} |
