mirror of
https://github.com/QwenLM/qwen-code.git
synced 2025-12-21 17:27:54 +00:00
feat: migrate tools to use parametersJsonSchema. (#5330)
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user