| 1 | import type * as JsonSchema from "../../JsonSchema.ts" |
| 2 | import * as Schema from "../../Schema.ts" |
| 3 | import type { CodecTransformer } from "../LanguageModel.ts" |
| 4 | import * as InternalStructuredOutput from "./structured-output.ts" |
| 5 | |
| 6 | const makeDefaultCodecTransformer = ( |
| 7 | toJsonSchemaDocument: (schema: Schema.Constraint) => JsonSchema.Document<"draft-2020-12"> |
| 8 | ): CodecTransformer => { |
| 9 | return (codec) => { |
| 10 | const document = InternalStructuredOutput.resolveTopLevelReference(toJsonSchemaDocument(codec)) |
| 11 | const jsonSchema = { ...document.schema } |
| 12 | if (Object.keys(document.definitions).length > 0) { |
| 13 | jsonSchema.$defs = document.definitions |
| 14 | } |
| 15 | return { codec, jsonSchema } |
| 16 | } |
| 17 | } |
| 18 | |
| 19 | /** |
| 20 | * @internal |
| 21 | */ |
| 22 | export const defaultCodecTransformer = makeDefaultCodecTransformer(Schema.toJsonSchemaDocument) |