1import type * as JsonSchema from "../../JsonSchema.ts"
2import * as Schema from "../../Schema.ts"
3import type { CodecTransformer } from "../LanguageModel.ts"
4import * as InternalStructuredOutput from "./structured-output.ts"
5
6const 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 */
22export const defaultCodecTransformer = makeDefaultCodecTransformer(Schema.toJsonSchemaDocument)