@devries-cameron20 / composio Mirror · read-only
Fork 0

Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.

Mirrored from https://github.com/Camo67/composio.git

Synced Nov 21, 06:00 UTC

next
ts 89 lines 2.82 KB
Raw
1 import { parseSchema } from './parse-schema';
2 import type { ParseContext } from '../context';
3 import type { JsonSchemaObject, JsonSchema } from '../types';
4 import { half } from '../utils/half';
5 import { its } from '../utils/its';
6
7 const originalIndex = Symbol('Original index');
8
9 const ensureOriginalIndex = (arr: JsonSchema[]) => {
10 const newArr = [];
11
12 for (let i = 0; i < arr.length; i++) {
13 const item = arr[i];
14 if (typeof item === 'boolean') {
15 newArr.push(item ? { [originalIndex]: i } : { [originalIndex]: i, not: {} });
16 } else if (originalIndex in item) {
17 return arr;
18 } else {
19 newArr.push({ ...item, [originalIndex]: i });
20 }
21 }
22
23 return newArr;
24 };
25
26 const isClosedObjectBranch = (
27 branch: JsonSchema
28 ): branch is JsonSchemaObject & { properties: Record<string, JsonSchema> } =>
29 typeof branch === 'object' &&
30 its.an.object(branch) &&
31 branch.properties !== undefined &&
32 Object.keys(branch.properties).length > 0 &&
33 branch.additionalProperties === undefined &&
34 branch.patternProperties === undefined &&
35 !('$ref' in branch);
36
37 const intersect = <S>(branches: JsonSchema[], ctx: ParseContext<S>): S => {
38 if (branches.length === 1) {
39 const item = branches[0];
40
41 return parseSchema(item, {
42 ...ctx,
43 path: [...ctx.path, 'allOf', (item as never)[originalIndex]],
44 });
45 }
46
47 const [left, right] = half(branches);
48 return ctx.backend.intersection(intersect(left, ctx), intersect(right, ctx));
49 };
50
51 export function parseAllOf<S>(
52 jsonSchema: JsonSchemaObject & { allOf: JsonSchema[] },
53 ctx: ParseContext<S>
54 ): S {
55 if (jsonSchema.allOf.length === 0) {
56 return ctx.backend.never();
57 }
58
59 if (jsonSchema.allOf.length === 1) {
60 return intersect(ensureOriginalIndex(jsonSchema.allOf), ctx);
61 }
62
63 // Sibling object branches must not reject each other's keys: each branch is
64 // materialized open, and the strictness a lone named-property object would
65 // have had is re-applied once over the union of every branch's properties.
66 const opened = jsonSchema.allOf.map(branch =>
67 isClosedObjectBranch(branch) ? { ...branch, additionalProperties: true } : branch
68 );
69 const parsed = intersect(ensureOriginalIndex(opened), ctx);
70
71 if (!jsonSchema.allOf.every(isClosedObjectBranch)) {
72 return parsed;
73 }
74
75 const declaredKeys = new Set(
76 jsonSchema.allOf.flatMap(branch => Object.keys((branch as JsonSchemaObject).properties ?? {}))
77 );
78
79 return ctx.backend.superRefine<unknown>(parsed, (value, issues) => {
80 if (typeof value !== 'object' || value === null || Array.isArray(value)) return;
81 const unrecognizedKeys = Object.keys(value).filter(key => !declaredKeys.has(key));
82 if (unrecognizedKeys.length > 0) {
83 issues.unrecognizedKeys(
84 unrecognizedKeys,
85 `Unrecognized key(s) in object: ${unrecognizedKeys.map(key => `'${key}'`).join(', ')}`
86 );
87 }
88 });
89 }