import { type FunctionDeclarationSchema as GenerativeAIFunctionDeclarationSchema, type SchemaType as FunctionDeclarationSchemaType, } from '@google/generative-ai'; import { InteropZodType, isInteropZodSchema, } from '@langchain/core/utils/types'; import { type JsonSchema7Type, toJsonSchema, } from '@langchain/core/utils/json_schema'; export interface GenerativeAIJsonSchema extends Record { properties?: Record; type: FunctionDeclarationSchemaType; } export interface GenerativeAIJsonSchemaDirty extends GenerativeAIJsonSchema { properties?: Record; additionalProperties?: boolean; } export function removeAdditionalProperties( // eslint-disable-next-line @typescript-eslint/no-explicit-any obj: Record ): GenerativeAIJsonSchema { if (typeof obj === 'object' && obj !== null) { const newObj = { ...obj }; if ('additionalProperties' in newObj) { delete newObj.additionalProperties; } if ('$schema' in newObj) { delete newObj.$schema; } if ('strict' in newObj) { delete newObj.strict; } for (const key in newObj) { if (key in newObj) { if (Array.isArray(newObj[key])) { newObj[key] = newObj[key].map(removeAdditionalProperties); } else if (typeof newObj[key] === 'object' && newObj[key] !== null) { newObj[key] = removeAdditionalProperties(newObj[key]); } } } return newObj as GenerativeAIJsonSchema; } return obj as GenerativeAIJsonSchema; } export function schemaToGenerativeAIParameters< // eslint-disable-next-line @typescript-eslint/no-explicit-any RunOutput extends Record = Record, >( schema: InteropZodType | JsonSchema7Type ): GenerativeAIFunctionDeclarationSchema { // GenerativeAI doesn't accept either the $schema or additionalProperties // attributes, so we need to explicitly remove them. const jsonSchema = removeAdditionalProperties( isInteropZodSchema(schema) ? toJsonSchema(schema) : schema ); const { $schema: _s, ...rest } = jsonSchema; return rest as GenerativeAIFunctionDeclarationSchema; } export function jsonSchemaToGeminiParameters( // eslint-disable-next-line @typescript-eslint/no-explicit-any schema: Record ): GenerativeAIFunctionDeclarationSchema { // Gemini doesn't accept either the $schema or additionalProperties // attributes, so we need to explicitly remove them. const jsonSchema = removeAdditionalProperties( schema as GenerativeAIJsonSchemaDirty ); const { $schema: _s, ...rest } = jsonSchema; return rest as GenerativeAIFunctionDeclarationSchema; }