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
| 1 | import { parseSchema } from './parse-schema'; |
| 2 | import type { ParseContext } from '../context'; |
| 3 | import type { JsonSchemaObject, JsonSchema } from '../types'; |
| 4 | import { extendSchemaWithMessage } from '../utils/extend-schema'; |
| 5 | import { its } from '../utils/its'; |
| 6 | import type { JSONSchema7TypeName } from 'json-schema'; |
| 7 | |
| 8 | type MetadataFields = |
| 9 | keyof Pick<JsonSchemaObject, 'default' | 'description' | 'title'> | 'examples'; |
| 10 | |
| 11 | export const parseArray = <S>( |
| 12 | jsonSchema: JsonSchemaObject & { type: 'array' }, |
| 13 | ctx: ParseContext<S> |
| 14 | ): S => { |
| 15 | const { backend } = ctx; |
| 16 | |
| 17 | // Handle anyOf pattern first |
| 18 | if (its.an.anyOf(jsonSchema)) { |
| 19 | const types = new Set<string>(); |
| 20 | const itemsSchemas: JsonSchema[] = []; |
| 21 | |
| 22 | // Collect all types and items from anyOf array |
| 23 | jsonSchema.anyOf.forEach(option => { |
| 24 | if (typeof option === 'object' && option.type) { |
| 25 | types.add(typeof option.type === 'string' ? option.type : option.type[0]); |
| 26 | } |
| 27 | if (typeof option === 'object' && option.items) { |
| 28 | const optionItems = option.items; |
| 29 | if (!Array.isArray(optionItems) && typeof optionItems === 'object') { |
| 30 | itemsSchemas.push(optionItems); |
| 31 | } |
| 32 | } |
| 33 | }); |
| 34 | |
| 35 | // If we have multiple item schemas, create a union |
| 36 | let finalItems: JsonSchema | undefined; |
| 37 | if (itemsSchemas.length === 1) { |
| 38 | finalItems = itemsSchemas[0]; |
| 39 | } else if (itemsSchemas.length > 1) { |
| 40 | finalItems = { anyOf: itemsSchemas }; |
| 41 | } |
| 42 | |
| 43 | // Create new schema with combined types |
| 44 | const newSchema: JsonSchemaObject = { |
| 45 | ...(types.size > 0 |
| 46 | ? { type: Array.from(types) as JSONSchema7TypeName[] } |
| 47 | : { type: 'array' }), |
| 48 | ...(finalItems && { items: finalItems }), |
| 49 | }; |
| 50 | |
| 51 | // Copy over metadata fields |
| 52 | const metadataFields: MetadataFields[] = ['default', 'description', 'examples', 'title']; |
| 53 | metadataFields.forEach(field => { |
| 54 | const value = jsonSchema[field as keyof typeof jsonSchema]; |
| 55 | if (value !== undefined) { |
| 56 | (newSchema as { [key in MetadataFields]: unknown })[field] = value; |
| 57 | } |
| 58 | }); |
| 59 | |
| 60 | return parseSchema(newSchema, ctx); |
| 61 | } |
| 62 | |
| 63 | // Handle regular array schema |
| 64 | if (Array.isArray(jsonSchema.items)) { |
| 65 | const parsedItems = jsonSchema.items.map((item, index) => |
| 66 | parseSchema(item, { ...ctx, path: [...ctx.path, 'items', index] }) |
| 67 | ); |
| 68 | const prefixSchemas = parsedItems.map((_, length) => |
| 69 | backend.tuple(parsedItems.slice(0, length), undefined) |
| 70 | ); |
| 71 | |
| 72 | let fullLengthSchema: S; |
| 73 | if (jsonSchema.additionalItems === false) { |
| 74 | fullLengthSchema = backend.tuple(parsedItems, undefined); |
| 75 | } else if (jsonSchema.additionalItems && jsonSchema.additionalItems !== true) { |
| 76 | fullLengthSchema = backend.tuple( |
| 77 | parsedItems, |
| 78 | parseSchema(jsonSchema.additionalItems, { |
| 79 | ...ctx, |
| 80 | path: [...ctx.path, 'additionalItems'], |
| 81 | }) |
| 82 | ); |
| 83 | } else { |
| 84 | fullLengthSchema = backend.tuple(parsedItems, backend.any()); |
| 85 | } |
| 86 | |
| 87 | const variants = [...prefixSchemas, fullLengthSchema]; |
| 88 | let tupleSchema: S = |
| 89 | variants.length === 1 ? variants[0] : backend.union(variants as unknown as [S, S, ...S[]]); |
| 90 | |
| 91 | if (typeof jsonSchema.minItems === 'number') { |
| 92 | tupleSchema = backend.refine<unknown[]>( |
| 93 | tupleSchema, |
| 94 | value => value.length >= jsonSchema.minItems!, |
| 95 | `Array must contain at least ${jsonSchema.minItems} element(s)` |
| 96 | ); |
| 97 | } |
| 98 | if (typeof jsonSchema.maxItems === 'number') { |
| 99 | tupleSchema = backend.refine<unknown[]>( |
| 100 | tupleSchema, |
| 101 | value => value.length <= jsonSchema.maxItems!, |
| 102 | `Array must contain at most ${jsonSchema.maxItems} element(s)` |
| 103 | ); |
| 104 | } |
| 105 | |
| 106 | return tupleSchema; |
| 107 | } |
| 108 | |
| 109 | let zodSchema = !jsonSchema.items |
| 110 | ? backend.array(backend.any()) |
| 111 | : backend.array(parseSchema(jsonSchema.items, { ...ctx, path: [...ctx.path, 'items'] })); |
| 112 | |
| 113 | zodSchema = extendSchemaWithMessage( |
| 114 | zodSchema, |
| 115 | jsonSchema, |
| 116 | 'minItems', |
| 117 | (zs, minItems, errorMessage) => backend.minItems(zs, minItems, errorMessage) |
| 118 | ); |
| 119 | zodSchema = extendSchemaWithMessage( |
| 120 | zodSchema, |
| 121 | jsonSchema, |
| 122 | 'maxItems', |
| 123 | (zs, maxItems, errorMessage) => backend.maxItems(zs, maxItems, errorMessage) |
| 124 | ); |
| 125 | |
| 126 | // Handle generic 'min' property as alias for 'minItems' |
| 127 | if (typeof jsonSchema.min === 'number' && typeof jsonSchema.minItems !== 'number') { |
| 128 | zodSchema = extendSchemaWithMessage( |
| 129 | zodSchema, |
| 130 | { ...jsonSchema, minItems: jsonSchema.min }, |
| 131 | 'minItems', |
| 132 | (zs, minItems, errorMessage) => backend.minItems(zs, minItems, errorMessage) |
| 133 | ); |
| 134 | } |
| 135 | |
| 136 | // Handle generic 'max' property as alias for 'maxItems' |
| 137 | if (typeof jsonSchema.max === 'number' && typeof jsonSchema.maxItems !== 'number') { |
| 138 | zodSchema = extendSchemaWithMessage( |
| 139 | zodSchema, |
| 140 | { ...jsonSchema, maxItems: jsonSchema.max }, |
| 141 | 'maxItems', |
| 142 | (zs, maxItems, errorMessage) => backend.maxItems(zs, maxItems, errorMessage) |
| 143 | ); |
| 144 | } |
| 145 | |
| 146 | return zodSchema; |
| 147 | }; |