@ai-sdk/google
Advanced tools
+1
-1
| { | ||
| "name": "@ai-sdk/google", | ||
| "version": "3.0.110", | ||
| "version": "3.0.111", | ||
| "license": "Apache-2.0", | ||
@@ -5,0 +5,0 @@ "sideEffects": false, |
@@ -1,3 +0,17 @@ | ||
| import type { JSONSchema7Definition } from '@ai-sdk/provider'; | ||
| import { | ||
| UnsupportedFunctionalityError, | ||
| type JSONSchema7, | ||
| type JSONSchema7Definition, | ||
| } from '@ai-sdk/provider'; | ||
| type JSONSchema7WithDefinitions = JSONSchema7 & { | ||
| $defs?: Record<string, JSONSchema7Definition>; | ||
| }; | ||
| type ReferenceContext = { | ||
| definitions: Record<string, JSONSchema7Definition> | undefined; | ||
| dollarDefinitions: Record<string, JSONSchema7Definition> | undefined; | ||
| resolvingReferences: ReadonlySet<string>; | ||
| }; | ||
| /** | ||
@@ -10,3 +24,19 @@ * Converts JSON Schema 7 to OpenAPI Schema 3.0 | ||
| ): unknown { | ||
| // Handle empty object schemas: undefined at root, preserved when nested | ||
| const rootSchema = | ||
| typeof jsonSchema === 'object' | ||
| ? (jsonSchema as JSONSchema7WithDefinitions) | ||
| : undefined; | ||
| return convertJSONSchemaDefinition(jsonSchema, isRoot, { | ||
| definitions: rootSchema?.definitions, | ||
| dollarDefinitions: rootSchema?.$defs, | ||
| resolvingReferences: new Set(), | ||
| }); | ||
| } | ||
| function convertJSONSchemaDefinition( | ||
| jsonSchema: JSONSchema7Definition | undefined, | ||
| isRoot: boolean, | ||
| referenceContext: ReferenceContext, | ||
| ): unknown { | ||
| if (jsonSchema == null) { | ||
@@ -16,2 +46,16 @@ return undefined; | ||
| if (typeof jsonSchema === 'boolean') { | ||
| return { type: 'boolean', properties: {} }; | ||
| } | ||
| if (jsonSchema.$ref != null) { | ||
| return convertJSONSchemaReference({ | ||
| jsonSchema, | ||
| reference: jsonSchema.$ref, | ||
| isRoot, | ||
| referenceContext, | ||
| }); | ||
| } | ||
| // Handle empty object schemas: undefined at root, preserved when nested | ||
| if (isEmptyObjectSchema(jsonSchema)) { | ||
@@ -22,3 +66,3 @@ if (isRoot) { | ||
| if (typeof jsonSchema === 'object' && jsonSchema.description) { | ||
| if (jsonSchema.description) { | ||
| return { type: 'object', description: jsonSchema.description }; | ||
@@ -29,6 +73,2 @@ } | ||
| if (typeof jsonSchema === 'boolean') { | ||
| return { type: 'boolean', properties: {} }; | ||
| } | ||
| const { | ||
@@ -88,3 +128,3 @@ type, | ||
| (acc, [key, value]) => { | ||
| acc[key] = convertJSONSchemaToOpenAPISchema(value, false); | ||
| acc[key] = convertJSONSchemaDefinition(value, false, referenceContext); | ||
| return acc; | ||
@@ -98,4 +138,6 @@ }, | ||
| result.items = Array.isArray(items) | ||
| ? items.map(item => convertJSONSchemaToOpenAPISchema(item, false)) | ||
| : convertJSONSchemaToOpenAPISchema(items, false); | ||
| ? items.map(item => | ||
| convertJSONSchemaDefinition(item, false, referenceContext), | ||
| ) | ||
| : convertJSONSchemaDefinition(items, false, referenceContext); | ||
| } | ||
@@ -105,3 +147,3 @@ | ||
| result.allOf = allOf.map(item => | ||
| convertJSONSchemaToOpenAPISchema(item, false), | ||
| convertJSONSchemaDefinition(item, false, referenceContext), | ||
| ); | ||
@@ -122,5 +164,6 @@ } | ||
| // If there's only one non-null schema, convert it and make it nullable | ||
| const converted = convertJSONSchemaToOpenAPISchema( | ||
| const converted = convertJSONSchemaDefinition( | ||
| nonNullSchemas[0], | ||
| false, | ||
| referenceContext, | ||
| ); | ||
@@ -134,3 +177,3 @@ if (typeof converted === 'object') { | ||
| result.anyOf = nonNullSchemas.map(item => | ||
| convertJSONSchemaToOpenAPISchema(item, false), | ||
| convertJSONSchemaDefinition(item, false, referenceContext), | ||
| ); | ||
@@ -141,3 +184,3 @@ result.nullable = true; | ||
| result.anyOf = anyOf.map(item => | ||
| convertJSONSchemaToOpenAPISchema(item, false), | ||
| convertJSONSchemaDefinition(item, false, referenceContext), | ||
| ); | ||
@@ -148,3 +191,3 @@ } | ||
| result.oneOf = oneOf.map(item => | ||
| convertJSONSchemaToOpenAPISchema(item, false), | ||
| convertJSONSchemaDefinition(item, false, referenceContext), | ||
| ); | ||
@@ -160,2 +203,119 @@ } | ||
| function convertJSONSchemaReference({ | ||
| jsonSchema, | ||
| reference, | ||
| isRoot, | ||
| referenceContext, | ||
| }: { | ||
| jsonSchema: JSONSchema7; | ||
| reference: string; | ||
| isRoot: boolean; | ||
| referenceContext: ReferenceContext; | ||
| }): unknown { | ||
| const { definition, referenceKey } = getReferencedDefinition( | ||
| reference, | ||
| referenceContext, | ||
| ); | ||
| if (referenceContext.resolvingReferences.has(referenceKey)) { | ||
| throw new UnsupportedFunctionalityError({ | ||
| functionality: `recursive JSON Schema reference: ${reference}`, | ||
| message: | ||
| 'Google schema conversion does not support recursive JSON Schema references.', | ||
| }); | ||
| } | ||
| const resolvingReferences = new Set(referenceContext.resolvingReferences); | ||
| resolvingReferences.add(referenceKey); | ||
| // Inline references instead of emitting Google's `ref` / `defs` fields. | ||
| // Those fields are supported by Vertex AI's Schema representation but are | ||
| // rejected by the Gemini Developer API representation used by this shared | ||
| // converter. | ||
| const { $ref: _reference, ...siblingSchema } = jsonSchema; | ||
| const resolvedSchema = | ||
| typeof definition === 'boolean' | ||
| ? definition | ||
| ? siblingSchema | ||
| : false | ||
| : { ...definition, ...siblingSchema }; | ||
| return convertJSONSchemaDefinition(resolvedSchema, isRoot, { | ||
| ...referenceContext, | ||
| resolvingReferences, | ||
| }); | ||
| } | ||
| function getReferencedDefinition( | ||
| reference: string, | ||
| referenceContext: ReferenceContext, | ||
| ): { | ||
| definition: JSONSchema7Definition; | ||
| referenceKey: string; | ||
| } { | ||
| const definitionSources = [ | ||
| { | ||
| prefix: '#/$defs/', | ||
| definitions: referenceContext.dollarDefinitions, | ||
| }, | ||
| { | ||
| prefix: '#/definitions/', | ||
| definitions: referenceContext.definitions, | ||
| }, | ||
| ]; | ||
| const source = definitionSources.find(({ prefix }) => | ||
| reference.startsWith(prefix), | ||
| ); | ||
| const encodedDefinitionName = source | ||
| ? reference.slice(source.prefix.length) | ||
| : undefined; | ||
| if ( | ||
| source == null || | ||
| encodedDefinitionName == null || | ||
| encodedDefinitionName.length === 0 || | ||
| encodedDefinitionName.includes('/') | ||
| ) { | ||
| throwUnsupportedReference(reference); | ||
| } | ||
| let decodedDefinitionName: string; | ||
| try { | ||
| decodedDefinitionName = decodeURIComponent(encodedDefinitionName); | ||
| } catch { | ||
| throwUnsupportedReference(reference); | ||
| } | ||
| if ( | ||
| decodedDefinitionName.includes('/') || | ||
| /~(?![01])/u.test(decodedDefinitionName) || | ||
| source.definitions == null | ||
| ) { | ||
| throwUnsupportedReference(reference); | ||
| } | ||
| const definitionName = decodedDefinitionName.replace(/~[01]/g, match => | ||
| match === '~1' ? '/' : '~', | ||
| ); | ||
| if ( | ||
| !Object.prototype.hasOwnProperty.call(source.definitions, definitionName) | ||
| ) { | ||
| throwUnsupportedReference(reference); | ||
| } | ||
| return { | ||
| definition: source.definitions[definitionName], | ||
| referenceKey: `${source.prefix}${definitionName}`, | ||
| }; | ||
| } | ||
| function throwUnsupportedReference(reference: string): never { | ||
| throw new UnsupportedFunctionalityError({ | ||
| functionality: `JSON Schema reference: ${reference}`, | ||
| message: | ||
| 'Google schema conversion only supports references to direct children of root-level $defs or definitions.', | ||
| }); | ||
| } | ||
| function isEmptyObjectSchema(jsonSchema: JSONSchema7Definition): boolean { | ||
@@ -162,0 +322,0 @@ return ( |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
URL strings
Supply chain riskPackage contains fragments of external URLs or IP addresses, which the package may be accessing at runtime.
URL strings
Supply chain riskPackage contains fragments of external URLs or IP addresses, which the package may be accessing at runtime.
3184657
1.37%34798
1.48%