|
4 | 4 | * SPDX-License-Identifier: Apache-2.0 |
5 | 5 | */ |
6 | 6 |
|
7 | | -import {FunctionDeclaration, Schema, Type} from '@google/genai'; |
8 | | -import { |
9 | | - type infer as zInfer, |
10 | | - ZodObject, |
11 | | - type ZodRawShape, |
12 | | -} from 'zod'; |
| 7 | +import { FunctionDeclaration, Schema, Type } from '@google/genai'; |
| 8 | +import { z as z3 } from 'zod/v3'; |
| 9 | +import { z as z4 } from 'zod/v4'; |
13 | 10 |
|
14 | | -import {isZodObject, zodObjectToSchema} from '../utils/simple_zod_to_json.js'; |
| 11 | +import { isZodObject, zodObjectToSchema } from '../utils/simple_zod_to_json.js'; |
15 | 12 |
|
16 | | -import {BaseTool, RunAsyncToolRequest} from './base_tool.js'; |
17 | | -import {ToolContext} from './tool_context.js'; |
| 13 | +import { BaseTool, RunAsyncToolRequest } from './base_tool.js'; |
| 14 | +import { ToolContext } from './tool_context.js'; |
18 | 15 |
|
19 | 16 | /** |
20 | 17 | * Input parameters of the function tool. |
21 | 18 | */ |
22 | | -export type ToolInputParameters = |
23 | | - | undefined |
24 | | - | ZodObject<ZodRawShape> |
25 | | - | Schema; |
| 19 | +export type ToolInputParameters = | undefined | z3.ZodObject<z3.ZodRawShape> | z4.ZodObject | Schema; |
| 20 | + |
| 21 | +type ZodObject<T extends Record<string, any>> = z3.ZodObject<z3.ZodRawShape> | z4.ZodObject<T>; |
26 | 22 |
|
27 | 23 | /* |
28 | 24 | * The arguments of the function tool. |
29 | 25 | */ |
30 | 26 | export type ToolExecuteArgument<TParameters extends ToolInputParameters> = |
31 | | - TParameters extends ZodObject<infer T, infer U, infer V> |
32 | | - ? zInfer<ZodObject<T, U, V>> |
33 | | - : TParameters extends Schema |
34 | | - ? unknown |
35 | | - : string; |
| 27 | + TParameters extends z3.ZodObject<infer T, infer U, infer V> |
| 28 | + ? z3.infer<z3.ZodObject<T, U, V>> |
| 29 | + : TParameters extends z4.ZodObject<infer T> |
| 30 | + ? z4.infer<z4.ZodObject<T>> |
| 31 | + : TParameters extends Schema |
| 32 | + ? unknown |
| 33 | + : string; |
36 | 34 |
|
37 | 35 | /* |
38 | 36 | * The function to execute by the tool. |
@@ -65,7 +63,7 @@ export type ToolOptions< |
65 | 63 | function toSchema<TParameters extends ToolInputParameters>( |
66 | 64 | parameters: TParameters): Schema { |
67 | 65 | if (parameters === undefined) { |
68 | | - return {type: Type.OBJECT, properties: {}}; |
| 66 | + return { type: Type.OBJECT, properties: {} }; |
69 | 67 | } |
70 | 68 |
|
71 | 69 | if (isZodObject(parameters)) { |
@@ -120,7 +118,7 @@ export class FunctionTool< |
120 | 118 | override async runAsync(req: RunAsyncToolRequest): Promise<unknown> { |
121 | 119 | try { |
122 | 120 | let validatedArgs: unknown = req.args; |
123 | | - if (this.parameters instanceof ZodObject) { |
| 121 | + if (isZodObject(this.parameters)) { |
124 | 122 | validatedArgs = this.parameters.parse(req.args); |
125 | 123 | } |
126 | 124 | return await this.execute( |
|
0 commit comments