562
const STRICT_STRIP_KEYWORDS = new Set(['examples', 'default']);
563
564
/** Keywords OpenAI structured outputs reject and that have no lossless rewrite. */
565
-const STRICT_UNSUPPORTED_KEYWORDS = ['allOf', 'prefixItems'];
565
+const STRICT_UNSUPPORTED_KEYWORDS = [
566
+ 'allOf',
567
+ 'prefixItems',
568
+ 'additionalItems',
569
+ 'contains',
570
+ 'dependencies',
571
+ 'dependentSchemas',
572
+ 'else',
573
+ 'if',
574
+ 'not',
575
+ 'propertyNames',
576
+ 'then',
577
+ 'unevaluatedItems',
578
+ 'unevaluatedProperties',
579
+];
580
+
581
+/** Whether a `type` value declares exactly the object type. */
582
+const isObjectType = (type: unknown): boolean =>
583
+ type === 'object' || (Array.isArray(type) && type.length === 1 && type[0] === 'object');
584
585
type StrictWalkMode = 'schema' | 'schema-array' | 'schema-map' | 'value';
586
616
if (Array.isArray(node.type)) {
617
return node.type.includes('null') ? node : { ...node, type: [...node.type, 'null'] };
618
}
619
+ if ((Array.isArray(node.enum) && node.enum.includes(null)) || node.const === null) return node;
620
// No `type` at all: an empty schema already accepts null; anything else
621
// (enum-only, const, composition) is wrapped so the annotation stays outside.
622
const { description, title, ...rest } = node;
678
* null branch;
679
* - constructs strict mode cannot express — objects that accept arbitrary
680
* keys (schema-valued or `true` `additionalProperties`, `patternProperties`,
662
- * property-less free-form objects), `allOf`, `prefixItems`, external or
663
- * dangling `$ref`s, and a non-object root — are reported in `unsupported`
664
- * rather than rewritten into something narrower. Callers send such a tool
665
- * without strict mode.
681
+ * property-less free-form objects), boolean subschemas, `allOf`,
682
+ * `prefixItems`, tuple-form `items`, conditional and dependency keywords,
683
+ * external or dangling `$ref`s, and a non-object root — are reported in
684
+ * `unsupported` rather than rewritten into something narrower. Callers send
685
+ * such a tool without strict mode.
686
*
687
* The input is never mutated. Every rewrite is recorded in `changes` (capped
688
* at 50 entries; `totalChanges` carries the real count), and `required`
773
});
774
}
775
}
776
+ if (node.oneOf !== undefined) {
777
+ unsupported.push({ path, keyword: 'oneOf', detail: 'oneOf beside anyOf cannot be merged' });
778
+ }
779
+ if (Array.isArray(node.items)) {
780
+ unsupported.push({
781
+ path,
782
+ keyword: 'items',
783
+ detail: 'tuple-form items has no strict-mode equivalent',
784
+ });
785
+ } else if (typeof node.items === 'boolean') {
786
+ unsupported.push({ path, keyword: 'items', detail: 'boolean subschema' });
787
+ }
788
+ if (node.properties !== undefined && !isPlainObject(node.properties)) {
789
+ unsupported.push({ path, keyword: 'properties', detail: 'properties is not an object' });
790
+ }
791
792
const out = walkChildren(node, mode, depth, path);
793
809
const declaresObjectType =
810
out.type === 'object' || (Array.isArray(out.type) && out.type.includes('object'));
811
if (!declaresObjectType && !isPlainObject(out.properties)) return out;
812
+ if (out.properties !== undefined && !isPlainObject(out.properties)) return out;
813
814
const properties: Record<string, unknown> = {};
815
if (isPlainObject(out.properties)) {
821
Array.isArray(out.required) ? out.required.filter(entry => typeof entry === 'string') : []
822
);
823
for (const [name, propertySchema] of Object.entries(properties)) {
824
+ if (typeof propertySchema === 'boolean') {
825
+ unsupported.push({
826
+ path: joinPath(path, `properties.${name}`),
827
+ keyword: 'properties',
828
+ detail: 'boolean subschema',
829
+ });
830
+ continue;
831
+ }
832
if (declaredRequired.has(name) || !isPlainObject(propertySchema)) continue;
833
setOwn(properties, name, widenToNullable(propertySchema));
834
recordChange({
870
}
871
872
const normalized = walk(root, 'schema', 0, '');
829
- if (!isPlainObject(normalized) || normalized.type !== 'object') {
873
+ if (!isPlainObject(normalized) || !isObjectType(normalized.type)) {
874
unsupported.push({
875
path: '',
876
keyword: 'type',
911
return omitNulls(args, root, root, 0) as Record<string, unknown>;
912
}
913
914
+/**
915
+ * Picks the composition branch that describes a value's shape: the first
916
+ * branch with `properties` for an object, the first with `items` for an
917
+ * array. Nodes without a composition are returned as they are.
918
+ */
919
+function selectBranchFor(
920
+ schema: unknown,
921
+ value: unknown,
922
+ root: Record<string, unknown>
923
+): Record<string, unknown> | undefined {
924
+ const resolved = resolveLocalRefs(schema, root);
925
+ if (!isPlainObject(resolved)) return undefined;
926
+ const wanted = Array.isArray(value) ? 'items' : 'properties';
927
+ const find = (node: unknown, depth: number): Record<string, unknown> | undefined => {
928
+ const candidate = resolveLocalRefs(node, root);
929
+ if (!isPlainObject(candidate) || depth > MAX_NODE_DEPTH) return undefined;
930
+ if (candidate[wanted] !== undefined) return candidate;
931
+ for (const keyword of ['anyOf', 'oneOf']) {
932
+ const branches = candidate[keyword];
933
+ if (!Array.isArray(branches)) continue;
934
+ for (const branch of branches) {
935
+ const found = find(branch, depth + 1);
936
+ if (found) return found;
937
+ }
938
+ }
939
+ return undefined;
940
+ };
941
+ return find(resolved, 0) ?? resolved;
942
+}
943
+
944
function omitNulls(
945
value: unknown,
946
schema: unknown,
950
if (depth > MAX_NODE_DEPTH) {
951
throw new RangeError(`Tool arguments exceed maximum nesting depth of ${MAX_NODE_DEPTH}`);
952
}
879
- const resolved = resolveLocalRefs(schema, root);
880
- const node = isPlainObject(resolved) ? resolved : undefined;
953
+ const node = selectBranchFor(schema, value, root);
954
if (Array.isArray(value)) {
955
const items = node && isPlainObject(node.items) ? node.items : undefined;
956
return value.map(item => omitNulls(item, items, root, depth + 1));