Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.
| 1 | import { z } from 'zod'; |
| 2 | import { instantSchema } from './instant'; |
| 3 | import type { ParameterSchema, Tool, Trigger } from '@/types/toolkit'; |
| 4 | |
| 5 | // Zod schemas for the untyped tool/trigger payloads the Composio API returns. |
| 6 | // Each boundary parses once (leniently — malformed fields degrade to the same |
| 7 | // fallbacks the UI applied by hand before) and lets the inferred types flow. |
| 8 | |
| 9 | /** Keep only the array members that satisfy `schema`; junk entries are dropped. */ |
| 10 | function filteredArray<T>(schema: z.ZodType<T>) { |
| 11 | return z |
| 12 | .array(z.unknown()) |
| 13 | .catch([]) |
| 14 | .transform(items => |
| 15 | items.flatMap(item => { |
| 16 | const parsed = schema.safeParse(item); |
| 17 | return parsed.success ? [parsed.data] : []; |
| 18 | }) |
| 19 | ); |
| 20 | } |
| 21 | |
| 22 | const lenientString = z.string().catch(''); |
| 23 | const optionalString = z.string().optional().catch(undefined); |
| 24 | const stringArray = filteredArray(z.string()); |
| 25 | const scalarStringArray = filteredArray( |
| 26 | z.union([z.string(), z.number(), z.boolean()]).transform(value => String(value)) |
| 27 | ); |
| 28 | |
| 29 | // --- JSON-Schema parameter nodes ------------------------------------------- |
| 30 | |
| 31 | // Recursive JSON-Schema property node. Nested properties, items, and |
| 32 | // object-valued additionalProperties survive whole (the toolkit detail UI |
| 33 | // reads items.additionalProperties and additionalProperties.properties to |
| 34 | // synthesize the "[key: string]" row). Tuple-form `items` arrays are not |
| 35 | // object nodes and parse to undefined instead of spreading into numeric keys. |
| 36 | interface RawSchemaNode { |
| 37 | type: string; |
| 38 | description?: string; |
| 39 | default?: unknown; |
| 40 | example?: unknown; |
| 41 | enum: string[]; |
| 42 | properties?: Record<string, unknown>; |
| 43 | required?: string[]; |
| 44 | items?: RawSchemaNode; |
| 45 | additionalProperties?: boolean | RawSchemaNode; |
| 46 | } |
| 47 | |
| 48 | const rawSchemaNodeSchema: z.ZodType<RawSchemaNode> = z.object({ |
| 49 | type: lenientString, |
| 50 | description: optionalString, |
| 51 | default: z.unknown().optional(), |
| 52 | example: z.unknown().optional(), |
| 53 | enum: scalarStringArray, |
| 54 | properties: z.record(z.string(), z.unknown()).optional().catch(undefined), |
| 55 | required: filteredArray(z.string()).optional().catch(undefined), |
| 56 | get items(): z.ZodType<RawSchemaNode | undefined> { |
| 57 | return rawSchemaNodeSchema.optional().catch(undefined); |
| 58 | }, |
| 59 | get additionalProperties(): z.ZodType<boolean | RawSchemaNode | undefined> { |
| 60 | return z.union([z.boolean(), rawSchemaNodeSchema]).optional().catch(undefined); |
| 61 | }, |
| 62 | }); |
| 63 | |
| 64 | function toParameterSchema(node: RawSchemaNode): ParameterSchema { |
| 65 | const param: ParameterSchema = { |
| 66 | type: node.type, |
| 67 | description: node.description, |
| 68 | default: node.default, |
| 69 | example: node.example, |
| 70 | enum: node.enum.length > 0 ? node.enum : undefined, |
| 71 | }; |
| 72 | |
| 73 | if (node.properties) { |
| 74 | param.properties = processParams(node.properties, node.required ?? []); |
| 75 | } |
| 76 | if (node.required) { |
| 77 | param.requiredFields = node.required; |
| 78 | } |
| 79 | if (node.items) { |
| 80 | param.items = toParameterSchema(node.items); |
| 81 | } |
| 82 | if (typeof node.additionalProperties === 'boolean') { |
| 83 | param.additionalProperties = node.additionalProperties; |
| 84 | } else if (node.additionalProperties) { |
| 85 | param.additionalProperties = toParameterSchema(node.additionalProperties); |
| 86 | } |
| 87 | |
| 88 | return param; |
| 89 | } |
| 90 | |
| 91 | // Add required flag to each property based on the required array |
| 92 | // Preserves nested properties/items for object and array types |
| 93 | function processParams( |
| 94 | props: Record<string, unknown>, |
| 95 | requiredList: string[] |
| 96 | ): Record<string, ParameterSchema> | undefined { |
| 97 | const result: Record<string, ParameterSchema> = {}; |
| 98 | for (const [key, value] of Object.entries(props)) { |
| 99 | const parsed = rawSchemaNodeSchema.safeParse(value); |
| 100 | if (parsed.success) { |
| 101 | result[key] = { |
| 102 | ...toParameterSchema(parsed.data), |
| 103 | required: requiredList.includes(key), |
| 104 | }; |
| 105 | } |
| 106 | } |
| 107 | return Object.keys(result).length > 0 ? result : undefined; |
| 108 | } |
| 109 | |
| 110 | const rawSchemaRootSchema = z.object({ |
| 111 | properties: z.record(z.string(), z.unknown()).optional().catch(undefined), |
| 112 | required: stringArray, |
| 113 | }); |
| 114 | |
| 115 | // Process a raw JSON Schema (with top-level properties + required) into ParameterSchema records |
| 116 | export function processSchema(schema: unknown): Record<string, ParameterSchema> | undefined { |
| 117 | const record = z.record(z.string(), z.unknown()).safeParse(schema); |
| 118 | if (!record.success) return undefined; |
| 119 | const root = rawSchemaRootSchema.parse(record.data); |
| 120 | return processParams(root.properties ?? record.data, root.required); |
| 121 | } |
| 122 | |
| 123 | // --- Tools ------------------------------------------------------------------ |
| 124 | |
| 125 | const rawApiToolSchema = z.object({ |
| 126 | instant: instantSchema, |
| 127 | slug: lenientString, |
| 128 | name: optionalString, |
| 129 | display_name: optionalString, |
| 130 | description: lenientString, |
| 131 | input_parameters: z.unknown().optional(), |
| 132 | parameters: z.unknown().optional(), |
| 133 | output_parameters: z.unknown().optional(), |
| 134 | response: z.unknown().optional(), |
| 135 | scopes: stringArray, |
| 136 | tags: stringArray, |
| 137 | is_deprecated: z.boolean().catch(false), |
| 138 | }); |
| 139 | |
| 140 | type RawApiTool = z.infer<typeof rawApiToolSchema>; |
| 141 | |
| 142 | function toolFromRaw(raw: RawApiTool): Tool { |
| 143 | return { |
| 144 | slug: raw.slug, |
| 145 | name: raw.name || raw.display_name || raw.slug, |
| 146 | description: raw.description, |
| 147 | input_parameters: processSchema(raw.input_parameters || raw.parameters), |
| 148 | output_parameters: processSchema(raw.output_parameters || raw.response), |
| 149 | scopes: raw.scopes.length > 0 ? raw.scopes : undefined, |
| 150 | tags: raw.tags.length > 0 ? raw.tags : undefined, |
| 151 | is_deprecated: raw.is_deprecated, |
| 152 | ...(raw.instant ? { instant: raw.instant } : {}), |
| 153 | }; |
| 154 | } |
| 155 | |
| 156 | // Convert a raw API tool object into our Tool type |
| 157 | export function toolFromApi(tool: unknown): Tool { |
| 158 | const parsed = rawApiToolSchema.safeParse(tool); |
| 159 | return toolFromRaw(parsed.success ? parsed.data : rawApiToolSchema.parse({})); |
| 160 | } |
| 161 | |
| 162 | // --- Triggers --------------------------------------------------------------- |
| 163 | |
| 164 | const rawApiTriggerSchema = z.object({ |
| 165 | slug: lenientString, |
| 166 | name: optionalString, |
| 167 | display_name: optionalString, |
| 168 | description: lenientString, |
| 169 | type: z.enum(['webhook', 'poll']).optional().catch(undefined), |
| 170 | config: z.unknown().optional(), |
| 171 | payload: z.unknown().optional(), |
| 172 | instructions: optionalString, |
| 173 | }); |
| 174 | |
| 175 | type RawApiTrigger = z.infer<typeof rawApiTriggerSchema>; |
| 176 | |
| 177 | function triggerFromRaw(raw: RawApiTrigger): Trigger { |
| 178 | return { |
| 179 | slug: raw.slug, |
| 180 | name: raw.name || raw.display_name || raw.slug, |
| 181 | description: raw.description, |
| 182 | type: raw.type, |
| 183 | config: processSchema(raw.config), |
| 184 | payload: processSchema(raw.payload), |
| 185 | instructions: raw.instructions, |
| 186 | }; |
| 187 | } |
| 188 | |
| 189 | // Convert a raw API trigger object into our Trigger type |
| 190 | export function triggerFromApi(trigger: unknown): Trigger { |
| 191 | const parsed = rawApiTriggerSchema.safeParse(trigger); |
| 192 | return triggerFromRaw(parsed.success ? parsed.data : rawApiTriggerSchema.parse({})); |
| 193 | } |
| 194 | |
| 195 | // --- List responses --------------------------------------------------------- |
| 196 | |
| 197 | // Paged API responses arrive either as a bare array or as an { items } envelope. |
| 198 | const listEnvelopeSchema = z.union([ |
| 199 | z.object({ items: z.array(z.unknown()) }).transform(data => data.items), |
| 200 | z.array(z.unknown()), |
| 201 | ]); |
| 202 | |
| 203 | /** Parses a /tools list response into Tool records, dropping non-object entries. */ |
| 204 | export const apiToolListSchema = listEnvelopeSchema.transform(items => |
| 205 | items.flatMap(item => { |
| 206 | const parsed = rawApiToolSchema.safeParse(item); |
| 207 | return parsed.success ? [toolFromRaw(parsed.data)] : []; |
| 208 | }) |
| 209 | ); |
| 210 | |
| 211 | /** Parses a /triggers_types list response into Trigger records, dropping non-object entries. */ |
| 212 | export const apiTriggerListSchema = listEnvelopeSchema.transform(items => |
| 213 | items.flatMap(item => { |
| 214 | const parsed = rawApiTriggerSchema.safeParse(item); |
| 215 | return parsed.success ? [triggerFromRaw(parsed.data)] : []; |
| 216 | }) |
| 217 | ); |