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, 04:45 UTC
| 1 | import type { ZodBackend } from './backend/types'; |
| 2 | import type { OptionsOf, ParseContext } from './context'; |
| 3 | import { parseObjectShape } from './parsers/parse-object-shape'; |
| 4 | import { parseSchema } from './parsers/parse-schema'; |
| 5 | import type { JsonSchema, JsonSchemaObject } from './types'; |
| 6 | import { createWholeSchemaGuard, requiresWholeSchemaValidation } from './whole-schema-validation'; |
| 7 | |
| 8 | type ConverterOptions<S> = OptionsOf<S> & { withoutAnnotations?: boolean }; |
| 9 | |
| 10 | /** The two conversion entry points, bound to one backend. */ |
| 11 | export const createConverter = <S>(backend: ZodBackend<S>) => { |
| 12 | const contextFor = (root: JsonSchema, options: ConverterOptions<S>): ParseContext<S> => ({ |
| 13 | path: [], |
| 14 | seen: new Map(), |
| 15 | root, |
| 16 | ...options, |
| 17 | backend, |
| 18 | }); |
| 19 | |
| 20 | const jsonSchemaToZod = (schema: JsonSchema, options: ConverterOptions<S> = {}): S => { |
| 21 | const parsedSchema = parseSchema(schema, contextFor(schema, options)); |
| 22 | |
| 23 | return requiresWholeSchemaValidation(schema) |
| 24 | ? backend.attachGuard(parsedSchema, createWholeSchemaGuard(schema)) |
| 25 | : parsedSchema; |
| 26 | }; |
| 27 | |
| 28 | const jsonSchemaToZodShape = ( |
| 29 | schema: JsonSchema, |
| 30 | options: ConverterOptions<S> = {} |
| 31 | ): Record<string, S> => { |
| 32 | if (typeof schema !== 'object' || schema === null) { |
| 33 | return {}; |
| 34 | } |
| 35 | |
| 36 | const objectSchema = schema as JsonSchemaObject; |
| 37 | |
| 38 | return parseObjectShape(objectSchema, contextFor(objectSchema, options)); |
| 39 | }; |
| 40 | |
| 41 | return { jsonSchemaToZod, jsonSchemaToZodShape }; |
| 42 | }; |