{"version":3,"file":"zod_to_genai_parameters.cjs","names":[],"sources":["../../src/utils/zod_to_genai_parameters.ts"],"sourcesContent":["import {\n  type FunctionDeclarationSchema as GenerativeAIFunctionDeclarationSchema,\n  type SchemaType as FunctionDeclarationSchemaType,\n} from \"@google/generative-ai\";\nimport {\n  InteropZodType,\n  isInteropZodSchema,\n} from \"@langchain/core/utils/types\";\nimport {\n  type JsonSchema7Type,\n  toJsonSchema,\n} from \"@langchain/core/utils/json_schema\";\nimport {\n  isSerializableSchema,\n  SerializableSchema,\n} from \"@langchain/core/utils/standard_schema\";\n\nexport interface GenerativeAIJsonSchema extends Record<string, unknown> {\n  properties?: Record<string, GenerativeAIJsonSchema>;\n  type: FunctionDeclarationSchemaType;\n}\n\nexport interface GenerativeAIJsonSchemaDirty extends GenerativeAIJsonSchema {\n  properties?: Record<string, GenerativeAIJsonSchemaDirty>;\n  additionalProperties?: boolean;\n}\n\nexport function removeAdditionalProperties(\n  // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n  obj: Record<string, any>\n): GenerativeAIJsonSchema {\n  if (typeof obj === \"object\" && obj !== null) {\n    const newObj = { ...obj };\n\n    if (\"additionalProperties\" in newObj) {\n      delete newObj.additionalProperties;\n    }\n    if (\"$schema\" in newObj) {\n      delete newObj.$schema;\n    }\n    if (\"strict\" in newObj) {\n      delete newObj.strict;\n    }\n\n    for (const key in newObj) {\n      if (key in newObj) {\n        if (Array.isArray(newObj[key])) {\n          newObj[key] = newObj[key].map(removeAdditionalProperties);\n        } else if (typeof newObj[key] === \"object\" && newObj[key] !== null) {\n          newObj[key] = removeAdditionalProperties(newObj[key]);\n        }\n      }\n    }\n\n    return newObj as GenerativeAIJsonSchema;\n  }\n\n  return obj as GenerativeAIJsonSchema;\n}\n\nexport function schemaToGenerativeAIParameters<\n  // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n  RunOutput extends Record<string, any> = Record<string, any>,\n>(\n  schema:\n    | SerializableSchema<RunOutput>\n    | InteropZodType<RunOutput>\n    | JsonSchema7Type\n): GenerativeAIFunctionDeclarationSchema {\n  // GenerativeAI doesn't accept either the $schema or additionalProperties\n  // attributes, so we need to explicitly remove them.\n  const jsonSchema = removeAdditionalProperties(\n    isInteropZodSchema(schema) || isSerializableSchema(schema)\n      ? toJsonSchema(schema)\n      : schema\n  );\n  const { $schema, ...rest } = jsonSchema;\n  return rest as GenerativeAIFunctionDeclarationSchema;\n}\n\nexport function jsonSchemaToGeminiParameters(\n  // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n  schema: Record<string, any>\n): GenerativeAIFunctionDeclarationSchema {\n  // Gemini doesn't accept either the $schema or additionalProperties\n  // attributes, so we need to explicitly remove them.\n  const jsonSchema = removeAdditionalProperties(\n    schema as GenerativeAIJsonSchemaDirty\n  );\n  const { $schema, ...rest } = jsonSchema;\n\n  return rest as GenerativeAIFunctionDeclarationSchema;\n}\n"],"mappings":";;;;AA2BA,SAAgB,2BAEd,KACwB;AACxB,KAAI,OAAO,QAAQ,YAAY,QAAQ,MAAM;EAC3C,MAAM,SAAS,EAAE,GAAG,KAAK;AAEzB,MAAI,0BAA0B,OAC5B,QAAO,OAAO;AAEhB,MAAI,aAAa,OACf,QAAO,OAAO;AAEhB,MAAI,YAAY,OACd,QAAO,OAAO;AAGhB,OAAK,MAAM,OAAO,OAChB,KAAI,OAAO;OACL,MAAM,QAAQ,OAAO,KAAK,CAC5B,QAAO,OAAO,OAAO,KAAK,IAAI,2BAA2B;YAChD,OAAO,OAAO,SAAS,YAAY,OAAO,SAAS,KAC5D,QAAO,OAAO,2BAA2B,OAAO,KAAK;;AAK3D,SAAO;;AAGT,QAAO;;AAGT,SAAgB,+BAId,QAIuC;CAQvC,MAAM,EAAE,SAAS,GAAG,SALD,4BAAA,GAAA,4BAAA,oBACE,OAAO,KAAA,GAAA,sCAAA,sBAAyB,OAAO,IAAA,GAAA,kCAAA,cACzC,OAAO,GACpB,OACL;AAED,QAAO;;AAGT,SAAgB,6BAEd,QACuC;CAMvC,MAAM,EAAE,SAAS,GAAG,SAHD,2BACjB,OACD;AAGD,QAAO"}