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 | /** |
| 2 | * The builders the shared suites run against. Each wraps one converter entry |
| 3 | * behind the same small runtime surface, so a suite states JSON Schema |
| 4 | * expectations once and every builder is held to them. |
| 5 | */ |
| 6 | import type { ZodTypeAny } from 'zod/v3'; |
| 7 | import { version } from 'zod/v4/core'; |
| 8 | import { zodToJsonSchema } from 'zod-to-json-schema'; |
| 9 | |
| 10 | import { meetsZodV4Floor } from '../../src/backend/zod-v4-floor'; |
| 11 | |
| 12 | import { |
| 13 | jsonSchemaToZod as v3Convert, |
| 14 | jsonSchemaToZodShape as v3ConvertShape, |
| 15 | } from '../../src/index'; |
| 16 | import type { JsonSchema } from '../../src/types'; |
| 17 | import * as z3 from 'zod/v3'; |
| 18 | |
| 19 | export type Issue = { |
| 20 | readonly code: string; |
| 21 | readonly path: ReadonlyArray<string | number>; |
| 22 | readonly message: string; |
| 23 | }; |
| 24 | |
| 25 | export type SafeParseResult = |
| 26 | | { readonly success: true; readonly data: unknown } |
| 27 | | { readonly success: false; readonly error: { readonly issues: ReadonlyArray<Issue> } }; |
| 28 | |
| 29 | export interface RuntimeSchema { |
| 30 | parse(value: unknown): unknown; |
| 31 | safeParse(value: unknown): SafeParseResult; |
| 32 | describe(description: string): RuntimeSchema; |
| 33 | } |
| 34 | |
| 35 | export type ConvertOptions = { |
| 36 | readonly withoutDefaults?: boolean; |
| 37 | readonly withoutDescribes?: boolean; |
| 38 | readonly depth?: number; |
| 39 | }; |
| 40 | |
| 41 | export type SchemaKind = 'string' | 'number' | 'boolean' | 'null' | 'object' | 'array' | 'other'; |
| 42 | |
| 43 | export interface Builder { |
| 44 | readonly name: 'zod-v3' | 'zod-v4'; |
| 45 | convert(schema: JsonSchema, options?: ConvertOptions): RuntimeSchema; |
| 46 | convertShape(schema: JsonSchema, options?: ConvertOptions): Record<string, RuntimeSchema>; |
| 47 | objectFromShape(shape: Record<string, RuntimeSchema>): RuntimeSchema; |
| 48 | kind(schema: RuntimeSchema): SchemaKind; |
| 49 | shape(schema: RuntimeSchema): Record<string, RuntimeSchema>; |
| 50 | description(schema: RuntimeSchema): string | undefined; |
| 51 | toJsonSchema(schema: RuntimeSchema): Record<string, unknown>; |
| 52 | } |
| 53 | |
| 54 | const V3_KINDS: Record<string, SchemaKind> = { |
| 55 | ZodString: 'string', |
| 56 | ZodNumber: 'number', |
| 57 | ZodBoolean: 'boolean', |
| 58 | ZodNull: 'null', |
| 59 | ZodObject: 'object', |
| 60 | ZodArray: 'array', |
| 61 | }; |
| 62 | |
| 63 | const asV3 = (schema: RuntimeSchema): ZodTypeAny => schema as unknown as ZodTypeAny; |
| 64 | |
| 65 | export const v3: Builder = { |
| 66 | name: 'zod-v3', |
| 67 | convert: (schema, options) => v3Convert(schema, options) as RuntimeSchema, |
| 68 | convertShape: (schema, options) => |
| 69 | v3ConvertShape(schema, options) as Record<string, RuntimeSchema>, |
| 70 | objectFromShape: shape => |
| 71 | z3.object(shape as unknown as Record<string, ZodTypeAny>) as unknown as RuntimeSchema, |
| 72 | kind: schema => V3_KINDS[asV3(schema)._def.typeName] ?? 'other', |
| 73 | shape: schema => (asV3(schema) as z3.AnyZodObject).shape as Record<string, RuntimeSchema>, |
| 74 | description: schema => asV3(schema).description, |
| 75 | toJsonSchema: schema => |
| 76 | zodToJsonSchema(asV3(schema), { target: 'jsonSchema7' }) as Record<string, unknown>, |
| 77 | }; |
| 78 | |
| 79 | /** The v4 builder joins only where the installed zod copy meets the `/v4` floor. */ |
| 80 | export const builders: ReadonlyArray<Builder> = meetsZodV4Floor(version) |
| 81 | ? [v3, (await import('./builder-v4')).v4] |
| 82 | : [v3]; |