{"version":3,"file":"json_schema.cjs","names":["isStandardJsonSchema","isZodSchemaV4","interopZodTransformInputSchema","isZodObjectV4","interopZodObjectStrict","isZodSchemaV3","zodToJsonSchema"],"sources":["../../src/utils/json_schema.ts"],"sourcesContent":["import { toJSONSchema, type $ZodType } from \"zod/v4/core\";\nimport { dereference, type Schema } from \"@cfworker/json-schema\";\nimport {\n  isZodSchemaV3,\n  isZodSchemaV4,\n  InteropZodType,\n  interopZodObjectStrict,\n  isZodObjectV4,\n  interopZodTransformInputSchema,\n} from \"./types/zod.js\";\nimport {\n  type JsonSchema7Type as JSONSchema,\n  zodToJsonSchema,\n} from \"./zod-to-json-schema/index.js\";\nimport { StandardJSONSchemaV1 } from \"@standard-schema/spec\";\nimport { isStandardJsonSchema } from \"./standard_schema.js\";\n\nexport { deepCompareStrict, Validator } from \"@cfworker/json-schema\";\n\nexport type ToJSONSchemaParams = NonNullable<\n  Parameters<typeof toJSONSchema>[1]\n>;\n\n/**\n * WeakMap cache for Zod/Standard-Schema → JSON Schema conversions.\n *\n * Keyed on the schema object reference. Since Zod schemas are immutable and\n * the same `tool.schema` reference is passed on every LLM call, this\n * eliminates redundant serializations. For example, an agent with 6 tools\n * doing 15 steps across 3 parallel subagents would otherwise run 270\n * identical conversions per invocation.\n *\n * Only used when no custom `params` are passed (the common case for tool\n * binding). WeakMap ensures cached entries are GC'd when the schema goes\n * out of scope.\n *\n * @internal\n */\nconst _jsonSchemaCache = new WeakMap<object, JSONSchema>();\n\n/**\n * Converts a Standard JSON schema, Zod schema or JSON schema to a JSON schema.\n * Results are cached by schema reference when no custom params are passed.\n * @param schema - The schema to convert.\n * @param params - The parameters to pass to the toJSONSchema function.\n * @returns The converted schema.\n */\nexport function toJsonSchema(\n  schema: StandardJSONSchemaV1 | InteropZodType | JSONSchema,\n  params?: ToJSONSchemaParams\n): JSONSchema {\n  // Cache when no custom params are passed (the default for tool binding).\n  // Params can change the output (e.g., different target draft), so we\n  // bypass the cache when they're provided.\n  const canCache = !params && schema != null && typeof schema === \"object\";\n\n  if (canCache) {\n    const cached = _jsonSchemaCache.get(schema);\n    if (cached) return cached;\n  }\n\n  let result: JSONSchema;\n\n  if (isStandardJsonSchema(schema) && !isZodSchemaV4(schema)) {\n    result = schema[\"~standard\"].jsonSchema.input({\n      target: \"draft-07\",\n    }) as JSONSchema;\n  } else if (isZodSchemaV4(schema)) {\n    const inputSchema = interopZodTransformInputSchema(schema, true);\n    if (isZodObjectV4(inputSchema)) {\n      const strictSchema = interopZodObjectStrict(inputSchema, true);\n      result = toJSONSchema(strictSchema as unknown as $ZodType, params);\n    } else {\n      result = toJSONSchema(schema as unknown as $ZodType, params);\n    }\n  } else if (isZodSchemaV3(schema)) {\n    result = zodToJsonSchema(schema as never);\n  } else {\n    result = schema as JSONSchema;\n  }\n\n  if (canCache && result != null && typeof result === \"object\") {\n    _jsonSchemaCache.set(schema, result);\n  }\n\n  return result;\n}\n\n/**\n * Validates if a JSON schema validates only strings. May return false negatives in some edge cases\n * (like recursive or unresolvable refs).\n *\n * @param schema - The schema to validate.\n * @returns `true` if the schema validates only strings, `false` otherwise.\n */\nexport function validatesOnlyStrings(schema: unknown): boolean {\n  // Null, undefined, or empty schema\n  if (\n    !schema ||\n    typeof schema !== \"object\" ||\n    Object.keys(schema).length === 0 ||\n    Array.isArray(schema)\n  ) {\n    return false; // Validates anything, not just strings\n  }\n\n  // Explicit type constraint\n  if (\"type\" in schema) {\n    if (typeof schema.type === \"string\") {\n      return schema.type === \"string\";\n    }\n\n    if (Array.isArray(schema.type)) {\n      // not sure why someone would do `\"type\": [\"string\"]` or especially `\"type\": [\"string\",\n      // \"string\", \"string\", ...]` but we're not here to judge\n      return schema.type.every((t) => t === \"string\");\n    }\n    return false; // Invalid or non-string type\n  }\n\n  // Enum with only string values\n  if (\"enum\" in schema) {\n    return (\n      Array.isArray(schema.enum) &&\n      schema.enum.length > 0 &&\n      schema.enum.every((val) => typeof val === \"string\")\n    );\n  }\n\n  // String constant\n  if (\"const\" in schema) {\n    return typeof schema.const === \"string\";\n  }\n\n  // Schema combinations\n  if (\"allOf\" in schema && Array.isArray(schema.allOf)) {\n    // If any subschema validates only strings, then the overall schema validates only strings\n    return schema.allOf.some((subschema) => validatesOnlyStrings(subschema));\n  }\n\n  if (\n    (\"anyOf\" in schema && Array.isArray(schema.anyOf)) ||\n    (\"oneOf\" in schema && Array.isArray(schema.oneOf))\n  ) {\n    const subschemas = (\n      \"anyOf\" in schema ? schema.anyOf : schema.oneOf\n    ) as unknown[];\n\n    // All subschemas must validate only strings\n    return (\n      subschemas.length > 0 &&\n      subschemas.every((subschema) => validatesOnlyStrings(subschema))\n    );\n  }\n\n  // We're not going to try on this one, it's too complex - we just assume if it has a \"not\" key and hasn't matched one of the above checks, it's not a string schema.\n  if (\"not\" in schema) {\n    return false; // The not case can validate non-strings\n  }\n\n  if (\"$ref\" in schema && typeof schema.$ref === \"string\") {\n    const ref = schema.$ref as string;\n    const resolved = dereference(schema as Schema);\n    if (resolved[ref]) {\n      return validatesOnlyStrings(resolved[ref]);\n    }\n    return false;\n  }\n\n  // ignore recursive refs and other cases where type is omitted for now\n  // ignore other cases for now where type is omitted\n\n  return false;\n}\n\n// Re-export of the types used throughout langchain for json schema serialization.\n// The plan is to eventually nix zod-to-json-schema altogether in place for\n// zod v4 / a more standardized way of serializing validated inputs, so its re-exported\n// here to remove the dependency on zod-to-json-schema in downstream packages until\n// a determination is made.\n\nexport {\n  type JsonSchema7Type,\n  type JsonSchema7Type as JSONSchema,\n  type JsonSchema7ArrayType,\n  type JsonSchema7ObjectType,\n  type JsonSchema7StringType,\n  type JsonSchema7NumberType,\n  type JsonSchema7NullableType,\n} from \"./zod-to-json-schema/index.js\";\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAsCA,MAAM,mCAAmB,IAAI,SAA6B;;;;;;;;AAS1D,SAAgB,aACd,QACA,QACY;CAIZ,MAAM,WAAW,CAAC,UAAU,UAAU,QAAQ,OAAO,WAAW;AAE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