{"version":3,"file":"zod_to_gemini_parameters.cjs","names":[],"sources":["../../src/utils/zod_to_gemini_parameters.ts"],"sourcesContent":["import {\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  GeminiFunctionSchema,\n  GeminiJsonSchema,\n  GeminiJsonSchemaDirty,\n} from \"../types.js\";\nimport {\n  isSerializableSchema,\n  SerializableSchema,\n} from \"@langchain/core/utils/standard_schema\";\n\nexport function adjustObjectType(\n  // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n  obj: Record<string, any>\n  // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n): Record<string, any> {\n  if (!Array.isArray(obj.type)) {\n    return obj;\n  }\n\n  const len = obj.type.length;\n  const nullIndex = obj.type.indexOf(\"null\");\n  if (len === 2 && nullIndex >= 0) {\n    // There are only two values set for the type, and one of them is \"null\".\n    // Set the type to the other one and set nullable to true.\n    const typeIndex = nullIndex === 0 ? 1 : 0;\n    obj.type = obj.type[typeIndex];\n    obj.nullable = true;\n  } else if (len === 1 && nullIndex === 0) {\n    // This is nullable only without a type, which doesn't\n    // make sense for Gemini\n    throw new Error(\"zod_to_gemini_parameters: Gemini cannot handle null type\");\n  } else if (len === 1) {\n    // Although an array, it has only one value.\n    // So set it to the string to match what Gemini expects.\n    obj.type = obj?.type[0];\n  } else {\n    // Anything else could be a union type, so reject it.\n    throw new Error(\n      \"zod_to_gemini_parameters: Gemini cannot handle union types\"\n    );\n  }\n  return obj;\n}\n\nexport function removeAdditionalProperties(\n  // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n  obj: Record<string, any>\n): GeminiJsonSchema {\n  if (typeof obj === \"object\" && obj !== null) {\n    const newObj = { ...obj };\n\n    if (\"additionalProperties\" in newObj) {\n      delete newObj.additionalProperties;\n    }\n\n    // Check for union types (anyOf, oneOf) which Gemini doesn't support\n    if (\"anyOf\" in newObj || \"oneOf\" in newObj) {\n      const unionTypes = newObj.anyOf || newObj.oneOf;\n\n      // Check if this is a nullable union (e.g., T | null)\n      // This is a 2-element array where one element is {type: \"null\"}\n      if (Array.isArray(unionTypes) && unionTypes.length === 2) {\n        const nullIndex = unionTypes.findIndex((t) => t.type === \"null\");\n\n        if (nullIndex >= 0) {\n          // This is a nullable union - extract the non-null type\n          const nonNullType = unionTypes[nullIndex === 0 ? 1 : 0];\n          delete newObj.anyOf;\n          delete newObj.oneOf;\n          // Merge the non-null type properties and add nullable: true\n          for (const key in nonNullType) {\n            if (key in nonNullType) {\n              newObj[key] = nonNullType[key];\n            }\n          }\n          newObj.nullable = true;\n        } else {\n          // Not a simple nullable union - reject it\n          throw new Error(\n            \"zod_to_gemini_parameters: Gemini cannot handle union types (discriminatedUnion, anyOf, oneOf). \" +\n              \"Consider using a flat object structure with optional fields instead.\"\n          );\n        }\n      } else {\n        // Not a simple nullable union - reject it\n        throw new Error(\n          \"zod_to_gemini_parameters: Gemini cannot handle union types (discriminatedUnion, anyOf, oneOf). \" +\n            \"Consider using a flat object structure with optional fields instead.\"\n        );\n      }\n    }\n\n    // Convert exclusiveMinimum (from .positive()) to minimum\n    if (\"exclusiveMinimum\" in newObj && newObj.exclusiveMinimum === 0) {\n      // Convert .positive() to .min(0.01)\n      newObj.minimum = 0.01;\n      delete newObj.exclusiveMinimum;\n    } else if (\"exclusiveMinimum\" in newObj) {\n      // Convert other exclusiveMinimum to minimum with a small increment\n      newObj.minimum = newObj.exclusiveMinimum + 0.00001;\n      delete newObj.exclusiveMinimum;\n    }\n\n    adjustObjectType(newObj);\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 GeminiJsonSchema;\n  }\n\n  return obj as GeminiJsonSchema;\n}\n\nexport function schemaToGeminiParameters<\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): GeminiFunctionSchema {\n  // Gemini doesn't accept either the $schema or additionalProperties\n  // attributes, so we need to explicitly remove them.\n  // Zod sometimes also makes an array of type (because of .nullish()),\n  // which needs cleaning up.\n  const jsonSchema = removeAdditionalProperties(\n    isInteropZodSchema(schema) || isSerializableSchema(schema)\n      ? toJsonSchema(schema)\n      : schema\n  );\n  const { $schema, ...rest } = jsonSchema;\n  return rest;\n}\n\nexport function jsonSchemaToGeminiParameters(\n  // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n  schema: Record<string, any>\n): GeminiFunctionSchema {\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 GeminiJsonSchemaDirty\n  );\n  const { $schema, ...rest } = jsonSchema;\n\n  return rest;\n}\n"],"mappings":";;;;AAkBA,SAAgB,iBAEd,KAEqB;AACrB,KAAI,CAAC,MAAM,QAAQ,IAAI,KAAK,CAC1B,QAAO;CAGT,MAAM,MAAM,IAAI,KAAK;CACrB,MAAM,YAAY,IAAI,KAAK,QAAQ,OAAO;AAC1C,KAAI,QAAQ,KAAK,aAAa,GAAG;EAG/B,MAAM,YAAY,cAAc,IAAI,IAAI;AACxC,MAAI,OAAO,IAAI,KAAK;AACpB,MAAI,WAAW;YACN,QAAQ,KAAK,cAAc,EAGpC,OAAM,IAAI,MAAM,2DAA2D;UAClE,QAAQ,EAGjB,KAAI,OAAO,KAAK,KAAK;KAGrB,OAAM,IAAI,MACR,6DACD;AAEH,QAAO;;AAGT,SAAgB,2BAEd,KACkB;AAClB,KAAI,OAAO,QAAQ,YAAY,QAAQ,MAAM;EAC3C,MAAM,SAAS,EAAE,GAAG,KAAK;AAEzB,MAAI,0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