MCPcopy Create free account
hub / github.com/Effect-TS/effect / makeNormalizer

Function makeNormalizer

packages/tools/ai-codegen/src/Generator.ts:405–511  ·  view source on GitHub ↗
(resolve: (key: string) => JsonSchema.JsonSchema | undefined)

Source from the content-addressed store, hash-verified

403 * and removes redundant object wrappers. Unsupported compositions are left for the importer.
404 */
405const makeNormalizer = (resolve: (key: string) => JsonSchema.JsonSchema | undefined) => {
406 const resolving = new Set<string>()
407
408 // Prove nullability through type constraints, references, allOf and anyOf.
409 // Other constraints (notably enum and oneOf) can exclude null despite a nullable type.
410 const acceptsNull = (js: JsonSchema.JsonSchema, seen = new Set<string>()): boolean => {
411 if (Predicate.isString(js.$ref)) {
412 if (!Object.keys(js).every((key) => key === "$ref" || isAnnotation(key))) return false
413 const key = definitionKey(js.$ref)
414 if (key === undefined || seen.has(key)) return false
415 const definition = resolve(key)
416 if (definition === undefined) return false
417 seen.add(key)
418 const out = acceptsNull(definition, seen)
419 seen.delete(key)
420 return out
421 }
422 if (
423 !Object.keys(js).every((key) => objectKeys.has(key) || key === "allOf" || key === "anyOf" || isAnnotation(key))
424 ) {
425 return false
426 }
427 return (js.type === undefined || js.type === "null" || isStringArray(js.type) && js.type.includes("null")) &&
428 (!Array.isArray(js.allOf) ||
429 js.allOf.every((member) => Predicate.isObject(member) && acceptsNull(member, seen))) &&
430 (!Array.isArray(js.anyOf) || js.anyOf.some((member) => Predicate.isObject(member) && acceptsNull(member, seen)))
431 }
432
433 const prepare = (js: JsonSchema.JsonSchema): JsonSchema.JsonSchema =>
434 rewriteNot(rewriteConditional(rewriteNullable(js, acceptsNull)))
435
436 const dereference = (member: unknown): JsonSchema.JsonSchema | undefined => {
437 if (!Predicate.isObject(member)) return undefined
438 if (!Predicate.isString(member.$ref)) return compose(prepare(member))
439 const { $ref, ...siblings } = member
440 const key = definitionKey($ref)
441 if (key === undefined || resolving.has(key) || !Object.keys(siblings).every(isAnnotation)) return undefined
442 const definition = resolve(key)
443 if (definition === undefined) return undefined
444 resolving.add(key)
445 try {
446 return { ...compose(prepare(definition)), ...siblings }
447 } finally {
448 resolving.delete(key)
449 }
450 }
451
452 const toPart = (member: unknown): Part | undefined => {
453 const schema = dereference(member)
454 if (schema === undefined) return undefined
455 if (isPlainObject(schema)) return { _tag: "Object", schema }
456 const mode = bareChoice(schema)
457 if (mode === undefined) return undefined
458 const branches: Array<JsonSchema.JsonSchema> = []
459 for (const branch of schema[mode] as ReadonlyArray<unknown>) {
460 const resolved = dereference(branch)
461 if (resolved === undefined || !(isPlainObject(resolved) || excludesObject(resolved))) return undefined
462 branches.push(resolved)

Callers 1

Generator.tsFile · 0.85

Calls 3

dropRedundantObjectTypeFunction · 0.85
composeFunction · 0.70
prepareFunction · 0.70

Tested by

no test coverage detected