@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 39 lines 1.13 KB
Raw
1 import { parseSchema } from './parse-schema';
2 import type { RunFailure } from '../backend/types';
3 import type { ParseContext } from '../context';
4 import type { JsonSchemaObject, JsonSchema } from '../types';
5
6 export const parseOneOf = <S>(
7 jsonSchema: JsonSchemaObject & { oneOf: JsonSchema[] },
8 ctx: ParseContext<S>
9 ): S => {
10 const { backend } = ctx;
11 if (!jsonSchema.oneOf.length) {
12 return backend.any();
13 }
14
15 if (jsonSchema.oneOf.length === 1) {
16 return parseSchema(jsonSchema.oneOf[0], {
17 ...ctx,
18 path: [...ctx.path, 'oneOf', 0],
19 });
20 }
21
22 return backend.superRefine<unknown>(backend.any(), (x, issues) => {
23 const schemas = jsonSchema.oneOf.map((schema, i) =>
24 parseSchema(schema, {
25 ...ctx,
26 path: [...ctx.path, 'oneOf', i],
27 })
28 );
29
30 const unionErrors = schemas.reduce<RunFailure[]>((errors, schema) => {
31 const result = backend.run(schema, x);
32 return result.success ? errors : [...errors, result.failure];
33 }, []);
34
35 if (schemas.length - unionErrors.length !== 1) {
36 issues.invalidUnion('Invalid input: Should pass single schema', unionErrors);
37 }
38 });
39 };