(schema: JsonSchema.JsonSchema)
| 78 | } |
| 79 | |
| 80 | function rewriteOpenAI(schema: JsonSchema.JsonSchema): JsonSchema.JsonSchema { |
| 81 | return InternalStructuredOutput.walkJsonSchema(schema, (schema) => { |
| 82 | const normalized = normalizeAllOf(schema) |
| 83 | const out: JsonSchema.JsonSchema = {} |
| 84 | let unsupportedFormat: string | undefined |
| 85 | for (const [key, value] of Object.entries(normalized)) { |
| 86 | if (key === "format") { |
| 87 | if (typeof value === "string" && formats.has(value) && supportsType(key, normalized.type)) out.format = value |
| 88 | else if (typeof value === "string") unsupportedFormat = value |
| 89 | } else if (supportedKeywords.has(key) && supportsType(key, normalized.type)) { |
| 90 | if (key !== "additionalProperties" || value === false) out[key] = value |
| 91 | } |
| 92 | } |
| 93 | if (unsupportedFormat !== undefined) { |
| 94 | InternalStructuredOutput.appendDescription(out, `a value with a format of ${unsupportedFormat}`) |
| 95 | } |
| 96 | if (out.type === "object" && out.properties === undefined && out.additionalProperties === false) { |
| 97 | out.properties = {} |
| 98 | } |
| 99 | return out |
| 100 | }) |
| 101 | } |
| 102 | |
| 103 | function supportsType(key: string, type: unknown): boolean { |
| 104 | if (typeof type !== "string") return true |
no test coverage detected