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