{"version":3,"file":"json-schema.mjs","names":[],"sources":["../../../../../../../ai/src/utils/json-schema.ts"],"sourcesContent":["import type { StandardSchemaV1 } from \"@standard-schema/spec\";\n\n/**\n * Supported JSON Schema output targets per the Standard JSON Schema V1\n * spec. `openai-strict` is the richest for OpenAI structured outputs —\n * every property listed in `required`, optionals expressed as\n * `[\"T\", \"null\"]`, `additionalProperties: false` everywhere. Other\n * targets produce standards-compliant but looser output.\n */\nexport type JsonSchemaTarget = \"draft-2020-12\" | \"draft-07\" | \"openapi-3.0\" | \"openai-strict\";\n\n/**\n * Options for `extractJsonSchema`. `target` is forwarded to libraries that\n * implement the Standard JSON Schema V1 spec (Seal, and any future lib\n * that follows the spec). Libraries using their own top-level `.jsonSchema`\n * / `._jsonSchema` property ignore the target.\n */\nexport type ExtractJsonSchemaOptions = {\n  target?: JsonSchemaTarget | (string & {});\n};\n\n/**\n * Best-effort JSON Schema extraction from a Standard Schema instance.\n *\n * Different libraries expose their JSON representation through different\n * paths:\n * - **Seal / Standard JSON Schema V1**: `[\"~standard\"].jsonSchema.input({ target })`\n *   — nested under the spec object, takes a target switch. Default target\n *   is `\"openai-strict\"` since the primary consumer is OpenAI's native\n *   structured-output mechanism; pass `options.target` to override.\n * - **Zod / similar**: top-level `.jsonSchema` property. Zod v4 actually\n *   ships `toJSONSchema` as a module function, not a method, so it does\n *   NOT hit this probe — Zod users pass their converted schema via the\n *   `AgentExecuteOptions.responseSchema` escape hatch.\n *\n * Deliberately does NOT probe `toJSON` — that's a generic JavaScript\n * serialization hook (Seal's schemas have one that dumps internal rule\n * state) and matching it would return garbage disguised as a JSON Schema.\n *\n * Returns `undefined` when no path matches. The caller then either skips\n * native structured-output wiring or falls back to a schema-less\n * instruction.\n *\n * Shared across every SDK adapter package (OpenAI, Anthropic, Bedrock…)\n * so each provider converts schemas identically.\n *\n * @example\n * const schema = extractJsonSchema(mySealSchema);\n * // { type: \"object\", properties: { ... }, required: [ ... ], additionalProperties: false }\n *\n * @example\n * // Ask for a different target explicitly\n * const draft = extractJsonSchema(mySealSchema, { target: \"draft-2020-12\" });\n */\nexport function extractJsonSchema(\n  schema: StandardSchemaV1<unknown> | undefined,\n  options: ExtractJsonSchemaOptions = {},\n): Record<string, unknown> | undefined {\n  if (!schema) return undefined;\n\n  const target = options.target ?? \"openai-strict\";\n\n  // 1. Seal / Standard JSON Schema V1 pattern: [\"~standard\"].jsonSchema.input({ target })\n  const sealJsonSchema = extractFromSealPath(schema, target);\n\n  if (sealJsonSchema) {\n    return sealJsonSchema;\n  }\n\n  // 2. Top-level jsonSchema / _jsonSchema (Zod-like, property or method form)\n  const topLevel = extractFromCandidateKeys(schema as unknown as Record<string, unknown>);\n\n  if (topLevel) {\n    return topLevel;\n  }\n\n  return undefined;\n}\n\n/**\n * Probe the Standard JSON Schema V1 extension path on `schema[\"~standard\"]`.\n * The spec defines `jsonSchema.input({ target, libraryOptions? })` as a\n * function returning a JSON Schema tailored to the requested target. We\n * pass the caller's target (default `\"openai-strict\"`) so the library\n * produces output ready for OpenAI's native structured-output mode\n * without additional post-processing.\n *\n * Calling `.input()` without a target would throw (or return garbage) per\n * the spec — a failure here returns `undefined` so the fallback probe\n * runs.\n */\nfunction extractFromSealPath(\n  schema: StandardSchemaV1<unknown>,\n  target: string,\n): Record<string, unknown> | undefined {\n  const standardSlot = (schema as unknown as Record<string, unknown>)[\"~standard\"];\n\n  if (!standardSlot || typeof standardSlot !== \"object\") {\n    return undefined;\n  }\n\n  const jsonSchemaSlot = (standardSlot as Record<string, unknown>)[\"jsonSchema\"];\n\n  if (!jsonSchemaSlot || typeof jsonSchemaSlot !== \"object\") {\n    return undefined;\n  }\n\n  const inputFn = (jsonSchemaSlot as Record<string, unknown>)[\"input\"];\n\n  if (typeof inputFn !== \"function\") {\n    return undefined;\n  }\n\n  try {\n    const result = (inputFn as (options: { target: string }) => unknown).call(jsonSchemaSlot, {\n      target,\n    });\n\n    if (result && typeof result === \"object\") {\n      return result as Record<string, unknown>;\n    }\n  } catch {\n    // fall through — library didn't support the target or threw otherwise\n  }\n\n  return undefined;\n}\n\n/**\n * Probe well-known top-level keys libraries use to expose their JSON\n * Schema. Supports both method form (rare) and property form (common).\n * Deliberately narrow — `toJSON` is NOT probed here because it's a\n * generic serialization hook that returns library-internal state for\n * many validators (Seal included), not a JSON Schema.\n */\nfunction extractFromCandidateKeys(\n  schemaRecord: Record<string, unknown>,\n): Record<string, unknown> | undefined {\n  const candidateKeys = [\"jsonSchema\", \"_jsonSchema\"] as const;\n\n  for (const key of candidateKeys) {\n    const value = schemaRecord[key];\n\n    if (typeof value === \"function\") {\n      try {\n        const result = (value as () => unknown).call(schemaRecord);\n\n        if (result && typeof result === \"object\") {\n          return result as Record<string, unknown>;\n        }\n      } catch {\n        // try next candidate\n      }\n\n      continue;\n    }\n\n    if (value && typeof value === \"object\") {\n      return value as Record<string, unknown>;\n    }\n  }\n\n  return undefined;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAsDA,SAAgB,kBACd,QACA,UAAoC,CAAC,GACA;CACrC,IAAI,CAAC,QAAQ,OAAO;CAKpB,MAAM,iBAAiB,oBAAoB,QAH5B,QAAQ,UAAU,eAGwB;CAEzD,IAAI,gBACF,OAAO;CAIT,MAAM,WAAW,yBAAyB,MAA4C;CAEtF,IAAI,UACF,OAAO;AAIX;;;;;;;;;;;;;AAcA,SAAS,oBACP,QACA,QACqC;CACrC,MAAM,eAAgB,OAA8C;CAEpE,IAAI,CAAC,gBAAgB,OAAO,iBAAiB,UAC3C;CAGF,MAAM,iBAAkB,aAAyC;CAEjE,IAAI,CAAC,kBAAkB,OAAO,mBAAmB,UAC/C;CAGF,MAAM,UAAW,eAA2C;CAE5D,IAAI,OAAO,YAAY,YACrB;CAGF,IAAI;EACF,MAAM,SAAU,QAAqD,KAAK,gBAAgB,EACxF,OACF,CAAC;EAED,IAAI,UAAU,OAAO,WAAW,UAC9B,OAAO;CAEX,QAAQ,CAER;AAGF;;;;;;;;AASA,SAAS,yBACP,cACqC;CAGrC,KAAK,MAAM,OAAO,CAFK,cAAc,aAEP,GAAG;EAC/B,MAAM,QAAQ,aAAa;EAE3B,IAAI,OAAO,UAAU,YAAY;GAC/B,IAAI;IACF,MAAM,SAAU,MAAwB,KAAK,YAAY;IAEzD,IAAI,UAAU,OAAO,WAAW,UAC9B,OAAO;GAEX,QAAQ,CAER;GAEA;EACF;EAEA,IAAI,SAAS,OAAO,UAAU,UAC5B,OAAO;CAEX;AAGF"}