@composio/core 0.16.0 → 0.18.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/{CustomTool-Bk38kFp0.d.mts → CustomTool-BKhHv2hJ.d.mts} +1 -1
- package/dist/{CustomTool-l-j5HtEf.mjs → CustomTool-C4tv-GL1.mjs} +1 -1
- package/dist/FileToolModifier.utils.neutral-D_YkHwZP.mjs +188 -0
- package/dist/{Telemetry-DuGJSwUt.mjs → Telemetry-CBtSnWrm.mjs} +27 -11
- package/dist/cancellation-jUVItfHw.mjs +704 -0
- package/dist/{composio-DSs-1AhC.d.mts → composio-CldtZUyC.d.mts} +254 -247
- package/dist/{BaseProvider-D7p2OPEx.d.mts → customTool.types-DC5fPOOi.d.mts} +2954 -82
- package/dist/docs/reference/sdk-reference/typescript/session-files.mdx +1 -1
- package/dist/{errors-BcEUHZbq.mjs → errors-DyZt79fi.mjs} +3 -0
- package/dist/experimental/index.d.mts +2 -2
- package/dist/experimental/index.mjs +1 -1
- package/dist/{fileUtils.node-BQ0fWge3.mjs → fileUtils.node-BwpUOF0s.mjs} +4 -4
- package/dist/index.d.mts +105 -7
- package/dist/index.mjs +94 -116
- package/dist/models/Files.node.mjs +2 -2
- package/dist/{sensitiveFileUploadPaths-BVEt0peg.mjs → sensitiveFileUploadPaths-B0w8dqpv.mjs} +23 -6
- package/dist/ssrfGuard.node-DlxO8uzr.mjs +307 -0
- package/dist/{types-B4k-qoOf.d.mts → types-BgsRW-XH.d.mts} +2 -2
- package/dist/utils/config-defaults/ConfigDefaults.node.d.mts +1 -1
- package/dist/utils/config-defaults/ConfigDefaults.workerd.d.mts +1 -1
- package/dist/utils/modifiers/FileToolModifier.node.d.mts +1 -1
- package/dist/utils/modifiers/FileToolModifier.node.mjs +57 -101
- package/dist/utils/modifiers/FileToolModifier.workerd.d.mts +1 -1
- package/dist/utils/modifiers/FileToolModifier.workerd.mjs +1 -1
- package/dist/utils/ssrfGuard.node.d.mts +25 -6
- package/dist/utils/ssrfGuard.node.mjs +2 -152
- package/dist/utils/ssrfGuard.workerd.d.mts +13 -1
- package/dist/utils/ssrfGuard.workerd.mjs +13 -1
- package/package.json +8 -4
- package/src/errors/ToolErrors.ts +1 -0
- package/src/errors/ValidationErrors.ts +2 -0
- package/src/index.ts +6 -0
- package/src/models/RemoteFile.ts +7 -2
- package/src/models/ToolRouterSession.ts +36 -8
- package/src/models/ToolRouterSessionFileMount.ts +14 -8
- package/src/models/Tools.ts +102 -39
- package/src/provider/BaseProvider.ts +53 -1
- package/src/provider/OpenAIProvider.ts +43 -15
- package/src/services/telemetry/TelemetryService.ts +27 -12
- package/src/types/provider.types.ts +15 -0
- package/src/utils/fileUtils.node.ts +8 -2
- package/src/utils/jsonSchema.ts +505 -27
- package/src/utils/modifiers/FileToolModifier.node.ts +85 -181
- package/src/utils/modifiers/FileToolModifier.utils.neutral.ts +199 -0
- package/src/utils/pinnedDispatcher.node.ts +118 -0
- package/src/utils/sensitiveFileUploadPaths.ts +36 -9
- package/src/utils/ssrfGuard.node.ts +100 -11
- package/src/utils/ssrfGuard.workerd.ts +16 -0
- package/dist/FileToolModifier.utils.neutral-B5rT711M.mjs +0 -72
- package/dist/cancellation-NVtGf3St.mjs +0 -364
- package/dist/customTool.types-cGHcIBX-.d.mts +0 -2859
package/src/utils/jsonSchema.ts
CHANGED
|
@@ -8,6 +8,33 @@ const MAX_REF_CHAIN_DEPTH = 100;
|
|
|
8
8
|
const MAX_NODE_DEPTH = 512;
|
|
9
9
|
const CYCLE_BREAK_SENTINEL = { type: 'object', additionalProperties: true } as const;
|
|
10
10
|
|
|
11
|
+
/** Keywords whose value is a single subschema. */
|
|
12
|
+
const SCHEMA_KEYWORDS = new Set([
|
|
13
|
+
'additionalItems',
|
|
14
|
+
'additionalProperties',
|
|
15
|
+
'contains',
|
|
16
|
+
'contentSchema',
|
|
17
|
+
'else',
|
|
18
|
+
'if',
|
|
19
|
+
'not',
|
|
20
|
+
'propertyNames',
|
|
21
|
+
'then',
|
|
22
|
+
'unevaluatedItems',
|
|
23
|
+
'unevaluatedProperties',
|
|
24
|
+
]);
|
|
25
|
+
/** Keywords whose value is an array of subschemas. */
|
|
26
|
+
const SCHEMA_ARRAY_KEYWORDS = new Set(['allOf', 'anyOf', 'oneOf', 'prefixItems']);
|
|
27
|
+
/** Keywords whose value is a map from name to subschema. */
|
|
28
|
+
const SCHEMA_MAP_KEYWORDS = new Set([
|
|
29
|
+
'$defs',
|
|
30
|
+
'definitions',
|
|
31
|
+
'dependentSchemas',
|
|
32
|
+
'patternProperties',
|
|
33
|
+
'properties',
|
|
34
|
+
]);
|
|
35
|
+
/** Keywords whose values are instance data, never subschemas. */
|
|
36
|
+
const INSTANCE_VALUE_KEYWORDS = new Set(['const', 'default', 'enum', 'examples']);
|
|
37
|
+
|
|
11
38
|
/**
|
|
12
39
|
* In-band hint attached to the cycle-break sentinel when lenient mode
|
|
13
40
|
* substitutes it for a dangling `$ref`. Makes the degradation visible to
|
|
@@ -272,28 +299,6 @@ export function dereferenceJsonSchema<T = unknown>(
|
|
|
272
299
|
export function ensureObjectTypeOnProperties<T = unknown>(schema: T): T {
|
|
273
300
|
type WalkMode = 'schema' | 'schema-array' | 'schema-map' | 'dependencies-map' | 'value';
|
|
274
301
|
|
|
275
|
-
const schemaKeywords = new Set([
|
|
276
|
-
'additionalItems',
|
|
277
|
-
'additionalProperties',
|
|
278
|
-
'contains',
|
|
279
|
-
'contentSchema',
|
|
280
|
-
'else',
|
|
281
|
-
'if',
|
|
282
|
-
'not',
|
|
283
|
-
'propertyNames',
|
|
284
|
-
'then',
|
|
285
|
-
'unevaluatedItems',
|
|
286
|
-
'unevaluatedProperties',
|
|
287
|
-
]);
|
|
288
|
-
const schemaArrayKeywords = new Set(['allOf', 'anyOf', 'oneOf', 'prefixItems']);
|
|
289
|
-
const schemaMapKeywords = new Set([
|
|
290
|
-
'$defs',
|
|
291
|
-
'definitions',
|
|
292
|
-
'dependentSchemas',
|
|
293
|
-
'patternProperties',
|
|
294
|
-
'properties',
|
|
295
|
-
]);
|
|
296
|
-
|
|
297
302
|
function walk(value: unknown, mode: WalkMode, depth = 0): unknown {
|
|
298
303
|
if (depth > MAX_NODE_DEPTH) {
|
|
299
304
|
throw new RangeError(`JSON Schema exceeds maximum nesting depth of ${MAX_NODE_DEPTH}`);
|
|
@@ -314,11 +319,11 @@ export function ensureObjectTypeOnProperties<T = unknown>(schema: T): T {
|
|
|
314
319
|
} else if (mode === 'dependencies-map') {
|
|
315
320
|
childMode = Array.isArray(child) ? 'value' : 'schema';
|
|
316
321
|
} else if (mode === 'schema') {
|
|
317
|
-
if (
|
|
322
|
+
if (SCHEMA_MAP_KEYWORDS.has(key)) {
|
|
318
323
|
childMode = 'schema-map';
|
|
319
|
-
} else if (
|
|
324
|
+
} else if (SCHEMA_ARRAY_KEYWORDS.has(key) || (key === 'items' && Array.isArray(child))) {
|
|
320
325
|
childMode = 'schema-array';
|
|
321
|
-
} else if (
|
|
326
|
+
} else if (SCHEMA_KEYWORDS.has(key) || key === 'items') {
|
|
322
327
|
childMode = 'schema';
|
|
323
328
|
} else if (key === 'dependencies') {
|
|
324
329
|
childMode = 'dependencies-map';
|
|
@@ -356,7 +361,6 @@ export function ensureObjectTypeOnProperties<T = unknown>(schema: T): T {
|
|
|
356
361
|
export function deduplicateJsonSchemaRequiredArrays<T = unknown>(schema: T): T {
|
|
357
362
|
const seenSchemaValues = new WeakMap<object, unknown>();
|
|
358
363
|
const seenInstanceValues = new WeakMap<object, unknown>();
|
|
359
|
-
const instanceValueKeywords = new Set(['const', 'default', 'enum', 'examples']);
|
|
360
364
|
|
|
361
365
|
function walk(value: unknown, isSchema: boolean, depth = 0): unknown {
|
|
362
366
|
if (depth > MAX_NODE_DEPTH) {
|
|
@@ -391,7 +395,7 @@ export function deduplicateJsonSchemaRequiredArrays<T = unknown>(schema: T): T {
|
|
|
391
395
|
value:
|
|
392
396
|
isSchema && key === 'required' && Array.isArray(child)
|
|
393
397
|
? [...new Set(walk(child, false, depth + 1) as unknown[])]
|
|
394
|
-
: walk(child, isSchema && !
|
|
398
|
+
: walk(child, isSchema && !INSTANCE_VALUE_KEYWORDS.has(key), depth + 1),
|
|
395
399
|
writable: true,
|
|
396
400
|
});
|
|
397
401
|
}
|
|
@@ -496,3 +500,477 @@ export function jsonSchemaToZodSchema<T extends z.ZodTypeAny>(
|
|
|
496
500
|
});
|
|
497
501
|
}
|
|
498
502
|
}
|
|
503
|
+
|
|
504
|
+
/**
|
|
505
|
+
* Reason recorded when strict normalization rewrites a schema node. Every
|
|
506
|
+
* rewrite is lossless: the model can still express the same values.
|
|
507
|
+
*
|
|
508
|
+
* - `optional-property-nullable` — a property missing from `required` was
|
|
509
|
+
* added to it and widened to accept `null`, the emulation of optional
|
|
510
|
+
* fields that OpenAI structured outputs document.
|
|
511
|
+
* - `unsupported-keyword-stripped` — an annotation keyword the API rejects
|
|
512
|
+
* (`default`, `examples`) was removed.
|
|
513
|
+
* - `one-of-converted` — `oneOf` (unsupported) became `anyOf`.
|
|
514
|
+
*/
|
|
515
|
+
export type StrictSchemaChangeReason =
|
|
516
|
+
'optional-property-nullable' | 'unsupported-keyword-stripped' | 'one-of-converted';
|
|
517
|
+
|
|
518
|
+
/** A single lossless rewrite applied by {@link toStrictJsonSchema}. */
|
|
519
|
+
export interface StrictSchemaChange {
|
|
520
|
+
/** Path to the changed node relative to the root, e.g. `properties.cfg.items`. */
|
|
521
|
+
path: string;
|
|
522
|
+
reason: StrictSchemaChangeReason;
|
|
523
|
+
/** Human-readable detail about what specifically changed. */
|
|
524
|
+
detail?: string;
|
|
525
|
+
}
|
|
526
|
+
|
|
527
|
+
/**
|
|
528
|
+
* A schema construct strict structured outputs cannot express. Rewriting it
|
|
529
|
+
* would change what the tool accepts, so it is reported instead; providers
|
|
530
|
+
* send such a tool without strict mode.
|
|
531
|
+
*/
|
|
532
|
+
export interface StrictSchemaIncompatibility {
|
|
533
|
+
/** Path to the offending node relative to the root. */
|
|
534
|
+
path: string;
|
|
535
|
+
/** The keyword (or `$ref`) that has no strict-mode equivalent. */
|
|
536
|
+
keyword: string;
|
|
537
|
+
detail: string;
|
|
538
|
+
}
|
|
539
|
+
|
|
540
|
+
/** Result of {@link toStrictJsonSchema}. */
|
|
541
|
+
export interface StrictJsonSchemaResult {
|
|
542
|
+
/**
|
|
543
|
+
* The strict schema. Only usable when `unsupported` is empty; otherwise it
|
|
544
|
+
* is best-effort and the original schema should be sent without strict mode.
|
|
545
|
+
*/
|
|
546
|
+
schema: Record<string, unknown>;
|
|
547
|
+
/**
|
|
548
|
+
* The input schema the rewrite was computed from. Optionality is decided
|
|
549
|
+
* against this schema, so it is what `omitNullToolArguments` needs.
|
|
550
|
+
*/
|
|
551
|
+
source: Record<string, unknown>;
|
|
552
|
+
/** Lossless rewrites, capped at 50 entries; see `totalChanges`. */
|
|
553
|
+
changes: StrictSchemaChange[];
|
|
554
|
+
totalChanges: number;
|
|
555
|
+
/** Constructs strict mode cannot express. Non-empty means "do not use `schema`". */
|
|
556
|
+
unsupported: StrictSchemaIncompatibility[];
|
|
557
|
+
}
|
|
558
|
+
|
|
559
|
+
const MAX_STRICT_CHANGES = 50;
|
|
560
|
+
|
|
561
|
+
/** Annotation-only keywords OpenAI structured outputs rejects; safe to strip. */
|
|
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 = [
|
|
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
|
+
|
|
587
|
+
const joinPath = (parent: string, key: string): string => (parent ? `${parent}.${key}` : key);
|
|
588
|
+
|
|
589
|
+
/**
|
|
590
|
+
* Sets an own property regardless of its name: a plain assignment to a key
|
|
591
|
+
* such as `__proto__` would set the prototype instead of storing the value.
|
|
592
|
+
*/
|
|
593
|
+
function setOwn(target: Record<string, unknown>, key: string, value: unknown): void {
|
|
594
|
+
Object.defineProperty(target, key, {
|
|
595
|
+
configurable: true,
|
|
596
|
+
enumerable: true,
|
|
597
|
+
value,
|
|
598
|
+
writable: true,
|
|
599
|
+
});
|
|
600
|
+
}
|
|
601
|
+
|
|
602
|
+
/**
|
|
603
|
+
* Widens a property schema so that `null` is an accepted value, without
|
|
604
|
+
* placing `type` beside `anyOf` (the API rejects that combination).
|
|
605
|
+
*/
|
|
606
|
+
function widenToNullable(node: Record<string, unknown>): Record<string, unknown> {
|
|
607
|
+
if (Array.isArray(node.anyOf)) {
|
|
608
|
+
const alreadyNullable = node.anyOf.some(
|
|
609
|
+
branch => isPlainObject(branch) && branch.type === 'null'
|
|
610
|
+
);
|
|
611
|
+
return alreadyNullable ? node : { ...node, anyOf: [...node.anyOf, { type: 'null' }] };
|
|
612
|
+
}
|
|
613
|
+
if (typeof node.type === 'string') {
|
|
614
|
+
return node.type === 'null' ? node : { ...node, type: [node.type, 'null'] };
|
|
615
|
+
}
|
|
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;
|
|
623
|
+
if (Object.keys(rest).length === 0) return node;
|
|
624
|
+
return {
|
|
625
|
+
...(description !== undefined ? { description } : {}),
|
|
626
|
+
...(title !== undefined ? { title } : {}),
|
|
627
|
+
anyOf: [rest, { type: 'null' }],
|
|
628
|
+
};
|
|
629
|
+
}
|
|
630
|
+
|
|
631
|
+
/**
|
|
632
|
+
* Follows local `$ref` pointers from `node` until a concrete schema node is
|
|
633
|
+
* reached. Unresolvable or over-long chains return the last node seen.
|
|
634
|
+
*/
|
|
635
|
+
function resolveLocalRefs(node: unknown, root: Record<string, unknown>): unknown {
|
|
636
|
+
let current = node;
|
|
637
|
+
for (let hops = 0; hops < MAX_REF_CHAIN_DEPTH; hops++) {
|
|
638
|
+
if (!isPlainObject(current) || typeof current.$ref !== 'string') return current;
|
|
639
|
+
const resolution = tryResolvePointer(root, current.$ref);
|
|
640
|
+
if (resolution.kind === 'unresolved') return current;
|
|
641
|
+
current = resolution.value;
|
|
642
|
+
}
|
|
643
|
+
return current;
|
|
644
|
+
}
|
|
645
|
+
|
|
646
|
+
/** Whether a schema node accepts `null` as an instance. */
|
|
647
|
+
function schemaAcceptsNull(schema: unknown, root: Record<string, unknown>): boolean {
|
|
648
|
+
const node = resolveLocalRefs(schema, root);
|
|
649
|
+
if (!isPlainObject(node)) return true;
|
|
650
|
+
if (typeof node.type === 'string') return node.type === 'null';
|
|
651
|
+
if (Array.isArray(node.type)) return node.type.includes('null');
|
|
652
|
+
if (Array.isArray(node.enum)) return node.enum.includes(null);
|
|
653
|
+
if ('const' in node) return node.const === null;
|
|
654
|
+
for (const keyword of ['anyOf', 'oneOf']) {
|
|
655
|
+
const branches = node[keyword];
|
|
656
|
+
if (Array.isArray(branches)) return branches.some(branch => schemaAcceptsNull(branch, root));
|
|
657
|
+
}
|
|
658
|
+
return true;
|
|
659
|
+
}
|
|
660
|
+
|
|
661
|
+
/**
|
|
662
|
+
* Normalizes a tool parameter schema for OpenAI structured outputs
|
|
663
|
+
* (`strict: true`) at every depth, following the contract OpenAI documents:
|
|
664
|
+
*
|
|
665
|
+
* - every object lists all of its properties in `required` and sets
|
|
666
|
+
* `additionalProperties: false`;
|
|
667
|
+
* - properties that were optional stay available and are widened to accept
|
|
668
|
+
* `null` (`type: ['string', 'null']`, or an extra `anyOf` branch) — the
|
|
669
|
+
* documented way to emulate optional fields. Nothing is dropped, so the
|
|
670
|
+
* model keeps every parameter it could pass before. A `null` the model
|
|
671
|
+
* sends for such a property means "omitted"; the strict providers drop it
|
|
672
|
+
* before executing the tool (see {@link omitNullToolArguments});
|
|
673
|
+
* - `type` arrays are kept as-is (the API accepts them); `oneOf` becomes
|
|
674
|
+
* `anyOf`; `default` and `examples` are stripped;
|
|
675
|
+
* - local `$ref`s into `$defs`/`definitions` are kept (the API supports
|
|
676
|
+
* them, including recursion) and the definitions themselves are
|
|
677
|
+
* normalized; an optional `$ref` property is widened with an `anyOf`
|
|
678
|
+
* null branch;
|
|
679
|
+
* - constructs strict mode cannot express — objects that accept arbitrary
|
|
680
|
+
* keys (schema-valued or `true` `additionalProperties`, `patternProperties`,
|
|
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`
|
|
689
|
+
* arrays are de-duplicated at the end.
|
|
690
|
+
*/
|
|
691
|
+
export function toStrictJsonSchema(schema: unknown): StrictJsonSchemaResult {
|
|
692
|
+
const changes: StrictSchemaChange[] = [];
|
|
693
|
+
const unsupported: StrictSchemaIncompatibility[] = [];
|
|
694
|
+
let totalChanges = 0;
|
|
695
|
+
const recordChange = (change: StrictSchemaChange): void => {
|
|
696
|
+
totalChanges += 1;
|
|
697
|
+
if (changes.length < MAX_STRICT_CHANGES) changes.push(change);
|
|
698
|
+
};
|
|
699
|
+
|
|
700
|
+
const root: Record<string, unknown> = isPlainObject(schema) ? schema : {};
|
|
701
|
+
|
|
702
|
+
function walkChildren(
|
|
703
|
+
node: Record<string, unknown>,
|
|
704
|
+
mode: StrictWalkMode,
|
|
705
|
+
depth: number,
|
|
706
|
+
path: string
|
|
707
|
+
): Record<string, unknown> {
|
|
708
|
+
const clone: Record<string, unknown> = {};
|
|
709
|
+
for (const [key, child] of Object.entries(node)) {
|
|
710
|
+
let childMode: StrictWalkMode = 'value';
|
|
711
|
+
if (mode === 'schema-map') {
|
|
712
|
+
childMode = 'schema';
|
|
713
|
+
} else if (mode === 'schema' && !INSTANCE_VALUE_KEYWORDS.has(key)) {
|
|
714
|
+
if (SCHEMA_MAP_KEYWORDS.has(key)) {
|
|
715
|
+
childMode = 'schema-map';
|
|
716
|
+
} else if (SCHEMA_ARRAY_KEYWORDS.has(key) || (key === 'items' && Array.isArray(child))) {
|
|
717
|
+
childMode = 'schema-array';
|
|
718
|
+
} else if (SCHEMA_KEYWORDS.has(key) || key === 'items') {
|
|
719
|
+
childMode = 'schema';
|
|
720
|
+
}
|
|
721
|
+
}
|
|
722
|
+
|
|
723
|
+
if (childMode === 'schema-map' && isPlainObject(child)) {
|
|
724
|
+
const mapClone: Record<string, unknown> = {};
|
|
725
|
+
for (const [name, subSchema] of Object.entries(child)) {
|
|
726
|
+
setOwn(
|
|
727
|
+
mapClone,
|
|
728
|
+
name,
|
|
729
|
+
walk(subSchema, 'schema', depth + 1, joinPath(joinPath(path, key), name))
|
|
730
|
+
);
|
|
731
|
+
}
|
|
732
|
+
setOwn(clone, key, mapClone);
|
|
733
|
+
continue;
|
|
734
|
+
}
|
|
735
|
+
setOwn(clone, key, walk(child, childMode, depth + 1, joinPath(path, key)));
|
|
736
|
+
}
|
|
737
|
+
return clone;
|
|
738
|
+
}
|
|
739
|
+
|
|
740
|
+
function walk(value: unknown, mode: StrictWalkMode, depth: number, path: string): unknown {
|
|
741
|
+
if (depth > MAX_NODE_DEPTH) {
|
|
742
|
+
throw new RangeError(`JSON Schema exceeds maximum nesting depth of ${MAX_NODE_DEPTH}`);
|
|
743
|
+
}
|
|
744
|
+
if (Array.isArray(value)) {
|
|
745
|
+
const itemMode = mode === 'schema-array' ? 'schema' : 'value';
|
|
746
|
+
return value.map((item, index) => walk(item, itemMode, depth + 1, `${path}[${index}]`));
|
|
747
|
+
}
|
|
748
|
+
if (!isPlainObject(value) || mode === 'value') return value;
|
|
749
|
+
|
|
750
|
+
const node: Record<string, unknown> = {};
|
|
751
|
+
for (const [key, child] of Object.entries(value)) {
|
|
752
|
+
if (STRICT_STRIP_KEYWORDS.has(key)) {
|
|
753
|
+
recordChange({
|
|
754
|
+
path,
|
|
755
|
+
reason: 'unsupported-keyword-stripped',
|
|
756
|
+
detail: `keyword "${key}" removed`,
|
|
757
|
+
});
|
|
758
|
+
continue;
|
|
759
|
+
}
|
|
760
|
+
setOwn(node, key, child);
|
|
761
|
+
}
|
|
762
|
+
if (Array.isArray(node.oneOf) && node.anyOf === undefined) {
|
|
763
|
+
node.anyOf = node.oneOf;
|
|
764
|
+
delete node.oneOf;
|
|
765
|
+
recordChange({ path, reason: 'one-of-converted', detail: 'oneOf became anyOf' });
|
|
766
|
+
}
|
|
767
|
+
for (const keyword of STRICT_UNSUPPORTED_KEYWORDS) {
|
|
768
|
+
if (node[keyword] !== undefined) {
|
|
769
|
+
unsupported.push({
|
|
770
|
+
path,
|
|
771
|
+
keyword,
|
|
772
|
+
detail: `"${keyword}" has no strict-mode equivalent`,
|
|
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
|
+
|
|
794
|
+
if (typeof node.$ref === 'string') {
|
|
795
|
+
const resolution =
|
|
796
|
+
node.$ref === '#' || node.$ref.startsWith('#/')
|
|
797
|
+
? tryResolvePointer(root, node.$ref)
|
|
798
|
+
: ({ kind: 'unresolved', reason: 'malformed-pointer' } as const);
|
|
799
|
+
if (resolution.kind === 'unresolved') {
|
|
800
|
+
unsupported.push({
|
|
801
|
+
path,
|
|
802
|
+
keyword: '$ref',
|
|
803
|
+
detail: `unresolved $ref "${node.$ref}"`,
|
|
804
|
+
});
|
|
805
|
+
}
|
|
806
|
+
// The referenced definition is normalized where it is declared.
|
|
807
|
+
return out;
|
|
808
|
+
}
|
|
809
|
+
|
|
810
|
+
const declaresObjectType =
|
|
811
|
+
out.type === 'object' || (Array.isArray(out.type) && out.type.includes('object'));
|
|
812
|
+
if (!declaresObjectType && !isPlainObject(out.properties)) return out;
|
|
813
|
+
if (out.properties !== undefined && !isPlainObject(out.properties)) return out;
|
|
814
|
+
|
|
815
|
+
const properties: Record<string, unknown> = {};
|
|
816
|
+
if (isPlainObject(out.properties)) {
|
|
817
|
+
for (const [name, propertySchema] of Object.entries(out.properties)) {
|
|
818
|
+
setOwn(properties, name, propertySchema);
|
|
819
|
+
}
|
|
820
|
+
}
|
|
821
|
+
const declaredRequired = new Set(
|
|
822
|
+
Array.isArray(out.required) ? out.required.filter(entry => typeof entry === 'string') : []
|
|
823
|
+
);
|
|
824
|
+
for (const [name, propertySchema] of Object.entries(properties)) {
|
|
825
|
+
if (typeof propertySchema === 'boolean') {
|
|
826
|
+
unsupported.push({
|
|
827
|
+
path: joinPath(path, `properties.${name}`),
|
|
828
|
+
keyword: 'properties',
|
|
829
|
+
detail: 'boolean subschema',
|
|
830
|
+
});
|
|
831
|
+
continue;
|
|
832
|
+
}
|
|
833
|
+
if (declaredRequired.has(name) || !isPlainObject(propertySchema)) continue;
|
|
834
|
+
setOwn(properties, name, widenToNullable(propertySchema));
|
|
835
|
+
recordChange({
|
|
836
|
+
path: joinPath(path, `properties.${name}`),
|
|
837
|
+
reason: 'optional-property-nullable',
|
|
838
|
+
detail: `property "${name}" is now required and accepts null`,
|
|
839
|
+
});
|
|
840
|
+
}
|
|
841
|
+
|
|
842
|
+
const acceptsDynamicKeys =
|
|
843
|
+
out.additionalProperties === true || isPlainObject(out.additionalProperties);
|
|
844
|
+
if (acceptsDynamicKeys) {
|
|
845
|
+
unsupported.push({
|
|
846
|
+
path,
|
|
847
|
+
keyword: 'additionalProperties',
|
|
848
|
+
detail: 'object accepts arbitrary keys',
|
|
849
|
+
});
|
|
850
|
+
} else if (out.patternProperties !== undefined) {
|
|
851
|
+
unsupported.push({
|
|
852
|
+
path,
|
|
853
|
+
keyword: 'patternProperties',
|
|
854
|
+
detail: 'object accepts pattern-matched keys',
|
|
855
|
+
});
|
|
856
|
+
} else if (Object.keys(properties).length === 0 && out.additionalProperties === undefined) {
|
|
857
|
+
unsupported.push({
|
|
858
|
+
path,
|
|
859
|
+
keyword: 'properties',
|
|
860
|
+
detail: 'free-form object accepts arbitrary keys',
|
|
861
|
+
});
|
|
862
|
+
}
|
|
863
|
+
|
|
864
|
+
return {
|
|
865
|
+
...out,
|
|
866
|
+
...(out.type === undefined ? { type: 'object' } : {}),
|
|
867
|
+
properties,
|
|
868
|
+
required: Object.keys(properties),
|
|
869
|
+
...(acceptsDynamicKeys ? {} : { additionalProperties: false }),
|
|
870
|
+
};
|
|
871
|
+
}
|
|
872
|
+
|
|
873
|
+
const normalized = walk(root, 'schema', 0, '');
|
|
874
|
+
if (!isPlainObject(normalized) || !isObjectType(normalized.type)) {
|
|
875
|
+
unsupported.push({
|
|
876
|
+
path: '',
|
|
877
|
+
keyword: 'type',
|
|
878
|
+
detail: 'root must be a non-nullable object',
|
|
879
|
+
});
|
|
880
|
+
}
|
|
881
|
+
|
|
882
|
+
return {
|
|
883
|
+
schema: deduplicateJsonSchemaRequiredArrays(normalized) as Record<string, unknown>,
|
|
884
|
+
source: root,
|
|
885
|
+
changes,
|
|
886
|
+
totalChanges,
|
|
887
|
+
unsupported,
|
|
888
|
+
};
|
|
889
|
+
}
|
|
890
|
+
|
|
891
|
+
/**
|
|
892
|
+
* Drops `null`-valued arguments that the tool's own schema does not accept,
|
|
893
|
+
* recursively through nested objects and arrays.
|
|
894
|
+
*
|
|
895
|
+
* Strict structured outputs cannot express optional parameters, so
|
|
896
|
+
* {@link toStrictJsonSchema} makes every parameter required and nullable. The
|
|
897
|
+
* model then sends `null` for a parameter it would otherwise have omitted;
|
|
898
|
+
* forwarding that `null` to the tool would fail validation against the tool's
|
|
899
|
+
* real schema, so it is treated as "omitted". A `null` the original schema
|
|
900
|
+
* accepts (a nullable field, an explicit "clear this value") is kept.
|
|
901
|
+
*
|
|
902
|
+
* @param args - Normalized tool arguments.
|
|
903
|
+
* @param schema - The dereferenced tool parameter schema the arguments were
|
|
904
|
+
* produced for (`StrictJsonSchemaResult.source`).
|
|
905
|
+
* @returns A new object; the input is not mutated.
|
|
906
|
+
*/
|
|
907
|
+
export function omitNullToolArguments(
|
|
908
|
+
args: Record<string, unknown>,
|
|
909
|
+
schema: unknown
|
|
910
|
+
): Record<string, unknown> {
|
|
911
|
+
const root: Record<string, unknown> = isPlainObject(schema) ? schema : {};
|
|
912
|
+
return omitNulls(args, root, root, 0) as Record<string, unknown>;
|
|
913
|
+
}
|
|
914
|
+
|
|
915
|
+
/**
|
|
916
|
+
* Picks the composition branch that describes a value's shape: the first
|
|
917
|
+
* branch with `properties` for an object, the first with `items` for an
|
|
918
|
+
* array. Nodes without a composition are returned as they are.
|
|
919
|
+
*/
|
|
920
|
+
function selectBranchFor(
|
|
921
|
+
schema: unknown,
|
|
922
|
+
value: unknown,
|
|
923
|
+
root: Record<string, unknown>
|
|
924
|
+
): Record<string, unknown> | undefined {
|
|
925
|
+
const resolved = resolveLocalRefs(schema, root);
|
|
926
|
+
if (!isPlainObject(resolved)) return undefined;
|
|
927
|
+
const wanted = Array.isArray(value) ? 'items' : 'properties';
|
|
928
|
+
const find = (node: unknown, depth: number): Record<string, unknown> | undefined => {
|
|
929
|
+
const candidate = resolveLocalRefs(node, root);
|
|
930
|
+
if (!isPlainObject(candidate) || depth > MAX_NODE_DEPTH) return undefined;
|
|
931
|
+
if (candidate[wanted] !== undefined) return candidate;
|
|
932
|
+
for (const keyword of ['anyOf', 'oneOf']) {
|
|
933
|
+
const branches = candidate[keyword];
|
|
934
|
+
if (!Array.isArray(branches)) continue;
|
|
935
|
+
for (const branch of branches) {
|
|
936
|
+
const found = find(branch, depth + 1);
|
|
937
|
+
if (found) return found;
|
|
938
|
+
}
|
|
939
|
+
}
|
|
940
|
+
return undefined;
|
|
941
|
+
};
|
|
942
|
+
return find(resolved, 0) ?? resolved;
|
|
943
|
+
}
|
|
944
|
+
|
|
945
|
+
function omitNulls(
|
|
946
|
+
value: unknown,
|
|
947
|
+
schema: unknown,
|
|
948
|
+
root: Record<string, unknown>,
|
|
949
|
+
depth: number
|
|
950
|
+
): unknown {
|
|
951
|
+
if (depth > MAX_NODE_DEPTH) {
|
|
952
|
+
throw new RangeError(`Tool arguments exceed maximum nesting depth of ${MAX_NODE_DEPTH}`);
|
|
953
|
+
}
|
|
954
|
+
const node = selectBranchFor(schema, value, root);
|
|
955
|
+
if (Array.isArray(value)) {
|
|
956
|
+
const items = node && isPlainObject(node.items) ? node.items : undefined;
|
|
957
|
+
return value.map(item => omitNulls(item, items, root, depth + 1));
|
|
958
|
+
}
|
|
959
|
+
if (!isPlainObject(value)) return value;
|
|
960
|
+
|
|
961
|
+
const properties = node && isPlainObject(node.properties) ? node.properties : {};
|
|
962
|
+
const clone: Record<string, unknown> = {};
|
|
963
|
+
for (const [key, child] of Object.entries(value)) {
|
|
964
|
+
const propertySchema = Object.prototype.hasOwnProperty.call(properties, key)
|
|
965
|
+
? properties[key]
|
|
966
|
+
: undefined;
|
|
967
|
+
if (child === null) {
|
|
968
|
+
if (propertySchema === undefined || schemaAcceptsNull(propertySchema, root)) {
|
|
969
|
+
setOwn(clone, key, child);
|
|
970
|
+
}
|
|
971
|
+
continue;
|
|
972
|
+
}
|
|
973
|
+
setOwn(clone, key, omitNulls(child, propertySchema, root, depth + 1));
|
|
974
|
+
}
|
|
975
|
+
return clone;
|
|
976
|
+
}
|