2
2
3
3
import type { z } from "zod" ;
4
4
import { zodToJsonSchema } from "zod-to-json-schema" ;
5
- import { GeminiFunctionSchema } from "../types.js" ;
5
+ import {
6
+ GeminiFunctionSchema ,
7
+ GeminiJsonSchema ,
8
+ GeminiJsonSchemaDirty ,
9
+ } from "../types.js" ;
10
+
11
+ function removeAdditionalProperties (
12
+ schema : GeminiJsonSchemaDirty
13
+ ) : GeminiJsonSchema {
14
+ const updatedSchema : GeminiJsonSchemaDirty = { ...schema } ;
15
+ if ( Object . hasOwn ( updatedSchema , "additionalProperties" ) ) {
16
+ delete updatedSchema . additionalProperties ;
17
+ }
18
+ if ( updatedSchema . properties ) {
19
+ const keys = Object . keys ( updatedSchema . properties ) ;
20
+ removeProperties ( updatedSchema . properties , keys , 0 ) ;
21
+ }
22
+
23
+ return updatedSchema ;
24
+ }
25
+
26
+ function removeProperties (
27
+ properties : Record < string , GeminiJsonSchemaDirty > ,
28
+ keys : string [ ] ,
29
+ index : number
30
+ ) : void {
31
+ if ( index >= keys . length ) {
32
+ return ;
33
+ }
34
+
35
+ const key = keys [ index ] ;
36
+ // eslint-disable-next-line no-param-reassign
37
+ properties [ key ] = removeAdditionalProperties ( properties [ key ] ) ;
38
+ removeProperties ( properties , keys , index + 1 ) ;
39
+ }
6
40
7
41
export function zodToGeminiParameters (
8
42
// eslint-disable-next-line @typescript-eslint/no-explicit-any
@@ -11,8 +45,11 @@ export function zodToGeminiParameters(
11
45
// Gemini doesn't accept either the $schema or additionalProperties
12
46
// attributes, so we need to explicitly remove them.
13
47
// eslint-disable-next-line @typescript-eslint/no-explicit-any
14
- const jsonSchema = zodToJsonSchema ( zodObj ) as any ;
15
- const { $schema, additionalProperties, ...rest } = jsonSchema ;
48
+ // const jsonSchema = zodToJsonSchema(zodObj) as any;
49
+ const jsonSchema = removeAdditionalProperties (
50
+ zodToJsonSchema ( zodObj ) as GeminiJsonSchemaDirty
51
+ ) ;
52
+ const { $schema, ...rest } = jsonSchema ;
16
53
17
54
return rest ;
18
55
}
0 commit comments