refactor(schema): share compact input fit checks (#17440)

This commit is contained in:
Vincent Koc
2026-12-06 15:29:30 +08:00
committed by GitHub
parent 235371230c
commit 76e0f8ede8
+12 -16
View File
@@ -119,6 +119,16 @@ const canonicalDefaultGuidance = (
resolveRootProperty(property, references),
);
const fitsInline = (
schema: Record<string, unknown>,
budgetBytes: number,
): boolean =>
serializedBytes(schema) <= budgetBytes &&
schemaDepth(schema) <= COMPACT_SCHEMA_MAX_DEPTH &&
!hasAdvertisedInputDialectViolation(schema, {
skipRootBounds: budgetBytes <= ADVERTISED_INPUT_SCHEMA_MAX_BYTES,
});
/**
* Render an advertised input schema within a per-schema byte budget for
* providers that validate the literal serialized schema size. The complete
@@ -141,14 +151,7 @@ export const compactAdvertisedInputSchema = (
retainRootPropertyGuidance(schema, expanded),
);
const defaults = canonicalDefaultGuidance(schema, references);
if (
inline !== undefined &&
serializedBytes(inline.schema) <= budgetBytes &&
schemaDepth(inline.schema) <= COMPACT_SCHEMA_MAX_DEPTH &&
!hasAdvertisedInputDialectViolation(inline.schema, {
skipRootBounds: budgetBytes <= ADVERTISED_INPUT_SCHEMA_MAX_BYTES,
})
)
if (inline !== undefined && fitsInline(inline.schema, budgetBytes))
return {
...inline,
guidance: [...inline.guidance, ...defaults],
@@ -158,14 +161,7 @@ export const compactAdvertisedInputSchema = (
// Only the branches past the depth limit are elided; the rest of the
// validation structure stays advertised instead of a whole reduction.
const pruned = pruneSchemaBeyondDepth(inline.schema);
if (
pruned !== undefined &&
serializedBytes(pruned) <= budgetBytes &&
schemaDepth(pruned) <= COMPACT_SCHEMA_MAX_DEPTH &&
!hasAdvertisedInputDialectViolation(pruned, {
skipRootBounds: budgetBytes <= ADVERTISED_INPUT_SCHEMA_MAX_BYTES,
})
)
if (pruned !== undefined && fitsInline(pruned, budgetBytes))
return {
schema: pruned,
guidance: [