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 { isPublicToolkitSlug, normalizeToolkitSlug } from '@/lib/public-toolkit-policy'; |
| 4 | import { createComposioClient } from './composio-api'; |
| 5 | import type { AuthConfigDetail, AuthConfigField, Toolkit } from '@/types/toolkit'; |
| 6 | |
| 7 | const NEGATIVE_CACHE_TTL_MS = 60_000; |
| 8 | const NEGATIVE_CACHE_MAX_ENTRIES = 1_024; |
| 9 | const IN_FLIGHT_LOOKUP_MAX_ENTRIES = 1_024; |
| 10 | const PRODUCTION_REQUEST_TIMEOUT_MS = 15_000; |
| 11 | // Production includes public toolkit slugs such as `_1password`. |
| 12 | const TOOLKIT_SLUG_PATTERN = /^[a-z0-9_][a-z0-9_-]{0,63}$/; |
| 13 | |
| 14 | const negativeCache = new Map<string, number>(); |
| 15 | const inFlightLookups = new Map<string, Promise<Toolkit | null>>(); |
| 16 | |
| 17 | function filteredArray<T>(schema: z.ZodType<T>) { |
| 18 | return z |
| 19 | .array(z.unknown()) |
| 20 | .catch([]) |
| 21 | .transform(items => |
| 22 | items.flatMap(item => { |
| 23 | const parsed = schema.safeParse(item); |
| 24 | return parsed.success ? [parsed.data] : []; |
| 25 | }) |
| 26 | ); |
| 27 | } |
| 28 | |
| 29 | const optionalString = z.string().optional().catch(undefined); |
| 30 | const stringArray = filteredArray(z.string()); |
| 31 | |
| 32 | const rawAuthConfigFieldSchema = z.object({ |
| 33 | name: z.string().catch(''), |
| 34 | displayName: optionalString, |
| 35 | type: z.string().catch('string'), |
| 36 | description: z.string().catch(''), |
| 37 | required: z.boolean().optional().catch(undefined), |
| 38 | default: z |
| 39 | .union([z.string(), z.number(), z.boolean()]) |
| 40 | .transform(value => String(value)) |
| 41 | .nullable() |
| 42 | .optional() |
| 43 | .catch(undefined), |
| 44 | }); |
| 45 | |
| 46 | const requirementListsSchema = z |
| 47 | .object({ |
| 48 | required: z.array(z.unknown()).catch([]), |
| 49 | optional: z.array(z.unknown()).catch([]), |
| 50 | }) |
| 51 | .catch({ required: [], optional: [] }); |
| 52 | |
| 53 | const authConfigFieldsSchema = z |
| 54 | .object({ |
| 55 | auth_config_creation: requirementListsSchema, |
| 56 | connected_account_initiation: requirementListsSchema, |
| 57 | }) |
| 58 | .catch({ |
| 59 | auth_config_creation: { required: [], optional: [] }, |
| 60 | connected_account_initiation: { required: [], optional: [] }, |
| 61 | }); |
| 62 | |
| 63 | const rawAuthConfigDetailSchema = z.object({ |
| 64 | mode: z.string().min(1), |
| 65 | name: optionalString, |
| 66 | fields: authConfigFieldsSchema, |
| 67 | }); |
| 68 | |
| 69 | const categorySchema = z.union([ |
| 70 | z.string(), |
| 71 | z.object({ name: z.string() }).transform(category => category.name), |
| 72 | ]); |
| 73 | |
| 74 | const rawToolkitSchema = z |
| 75 | .object({ |
| 76 | instant: instantSchema, |
| 77 | slug: z.string().catch(''), |
| 78 | name: optionalString, |
| 79 | type: z.enum(['native', 'custom']).optional().catch(undefined), |
| 80 | enabled: z.boolean().optional().catch(undefined), |
| 81 | composio_managed_auth_schemes: stringArray, |
| 82 | auth_config_details: filteredArray(rawAuthConfigDetailSchema), |
| 83 | meta: z |
| 84 | .object({ |
| 85 | description: z.string().catch(''), |
| 86 | logo: optionalString, |
| 87 | categories: z.array(z.unknown()).catch([]), |
| 88 | tools_count: z.number().catch(0), |
| 89 | triggers_count: z.number().catch(0), |
| 90 | version: z.string().nullable().optional().catch(undefined), |
| 91 | }) |
| 92 | .catch({ |
| 93 | description: '', |
| 94 | logo: undefined, |
| 95 | categories: [], |
| 96 | tools_count: 0, |
| 97 | triggers_count: 0, |
| 98 | version: undefined, |
| 99 | }), |
| 100 | }) |
| 101 | .catch({ |
| 102 | slug: '', |
| 103 | name: undefined, |
| 104 | type: undefined, |
| 105 | enabled: undefined, |
| 106 | composio_managed_auth_schemes: [], |
| 107 | auth_config_details: [], |
| 108 | meta: { |
| 109 | description: '', |
| 110 | logo: undefined, |
| 111 | categories: [], |
| 112 | tools_count: 0, |
| 113 | triggers_count: 0, |
| 114 | version: undefined, |
| 115 | }, |
| 116 | }); |
| 117 | |
| 118 | type RawAuthConfigField = z.infer<typeof rawAuthConfigFieldSchema>; |
| 119 | type RawAuthConfigDetail = z.infer<typeof rawAuthConfigDetailSchema>; |
| 120 | type RawToolkit = z.infer<typeof rawToolkitSchema>; |
| 121 | |
| 122 | function normalizeAuthMode(mode: string): string { |
| 123 | return mode.toUpperCase(); |
| 124 | } |
| 125 | |
| 126 | function uniqueAuthModes(modes: string[]): string[] { |
| 127 | const seen = new Set<string>(); |
| 128 | return modes.flatMap(mode => { |
| 129 | const normalized = normalizeAuthMode(mode); |
| 130 | const key = normalized.toLowerCase(); |
| 131 | if (!normalized || seen.has(key)) return []; |
| 132 | seen.add(key); |
| 133 | return [normalized]; |
| 134 | }); |
| 135 | } |
| 136 | |
| 137 | function transformAuthConfigField(field: RawAuthConfigField, required: boolean): AuthConfigField { |
| 138 | return { |
| 139 | name: field.name, |
| 140 | displayName: field.displayName || field.name, |
| 141 | type: field.type, |
| 142 | description: field.description, |
| 143 | required: field.required ?? required, |
| 144 | default: field.default ?? null, |
| 145 | }; |
| 146 | } |
| 147 | |
| 148 | function authConfigFieldFromUnknown(value: unknown, required: boolean): AuthConfigField { |
| 149 | const parsed = rawAuthConfigFieldSchema.safeParse(value); |
| 150 | const field = parsed.success ? parsed.data : rawAuthConfigFieldSchema.parse({}); |
| 151 | return transformAuthConfigField(field, required); |
| 152 | } |
| 153 | |
| 154 | function transformAuthConfigDetail(detail: RawAuthConfigDetail): AuthConfigDetail { |
| 155 | const fields = detail.fields; |
| 156 | return { |
| 157 | mode: normalizeAuthMode(detail.mode), |
| 158 | name: detail.name || detail.mode, |
| 159 | fields: { |
| 160 | auth_config_creation: { |
| 161 | required: fields.auth_config_creation.required.map(field => |
| 162 | authConfigFieldFromUnknown(field, true) |
| 163 | ), |
| 164 | optional: fields.auth_config_creation.optional.map(field => |
| 165 | authConfigFieldFromUnknown(field, false) |
| 166 | ), |
| 167 | }, |
| 168 | connected_account_initiation: { |
| 169 | required: fields.connected_account_initiation.required.map(field => |
| 170 | authConfigFieldFromUnknown(field, true) |
| 171 | ), |
| 172 | optional: fields.connected_account_initiation.optional.map(field => |
| 173 | authConfigFieldFromUnknown(field, false) |
| 174 | ), |
| 175 | }, |
| 176 | }, |
| 177 | }; |
| 178 | } |
| 179 | |
| 180 | function toolkitFromRaw(raw: RawToolkit, slug: string): Toolkit { |
| 181 | const category = categorySchema.safeParse(raw.meta.categories[0]); |
| 182 | const authConfigDetails = raw.auth_config_details.map(transformAuthConfigDetail); |
| 183 | const composioManagedAuthSchemes = uniqueAuthModes(raw.composio_managed_auth_schemes); |
| 184 | const authSchemes = uniqueAuthModes([ |
| 185 | ...authConfigDetails.map(detail => detail.mode), |
| 186 | ...composioManagedAuthSchemes, |
| 187 | ]); |
| 188 | |
| 189 | return { |
| 190 | slug, |
| 191 | name: raw.name || raw.slug || slug, |
| 192 | ...(raw.instant ? { instant: raw.instant } : {}), |
| 193 | logo: raw.meta.logo || null, |
| 194 | description: raw.meta.description, |
| 195 | category: category.success && category.data ? category.data : null, |
| 196 | authSchemes, |
| 197 | ...(composioManagedAuthSchemes.length > 0 ? { composioManagedAuthSchemes } : {}), |
| 198 | toolCount: raw.meta.tools_count, |
| 199 | triggerCount: raw.meta.triggers_count, |
| 200 | version: raw.meta.version ?? null, |
| 201 | tools: [], |
| 202 | triggers: [], |
| 203 | ...(authConfigDetails.length > 0 ? { authConfigDetails } : {}), |
| 204 | }; |
| 205 | } |
| 206 | |
| 207 | function recordNegative(slug: string) { |
| 208 | const now = Date.now(); |
| 209 | for (const [cachedSlug, expiresAt] of negativeCache) { |
| 210 | if (expiresAt > now) break; |
| 211 | negativeCache.delete(cachedSlug); |
| 212 | } |
| 213 | |
| 214 | negativeCache.delete(slug); |
| 215 | if (negativeCache.size >= NEGATIVE_CACHE_MAX_ENTRIES) { |
| 216 | const oldestSlug = negativeCache.keys().next().value; |
| 217 | if (oldestSlug !== undefined) negativeCache.delete(oldestSlug); |
| 218 | } |
| 219 | negativeCache.set(slug, now + NEGATIVE_CACHE_TTL_MS); |
| 220 | } |
| 221 | |
| 222 | /** Fetch one toolkit's metadata from production after a checked-in snapshot miss. */ |
| 223 | export async function fetchToolkitFromProduction(slug: string): Promise<Toolkit | null> { |
| 224 | if (process.env.COMPOSIO_TOOLKIT_LIVE_FALLBACK === '0') return null; |
| 225 | |
| 226 | const apiKey = process.env.COMPOSIO_API_KEY; |
| 227 | if (!apiKey) { |
| 228 | console.warn(`[Toolkits] Production toolkit lookup skipped for "${slug}": missing API key`); |
| 229 | return null; |
| 230 | } |
| 231 | |
| 232 | const normalizedSlug = normalizeToolkitSlug(slug); |
| 233 | if (!TOOLKIT_SLUG_PATTERN.test(normalizedSlug)) return null; |
| 234 | if (!isPublicToolkitSlug(normalizedSlug)) return null; |
| 235 | |
| 236 | const negativeUntil = negativeCache.get(normalizedSlug); |
| 237 | if (negativeUntil !== undefined) { |
| 238 | if (negativeUntil > Date.now()) return null; |
| 239 | negativeCache.delete(normalizedSlug); |
| 240 | } |
| 241 | |
| 242 | const inFlightLookup = inFlightLookups.get(normalizedSlug); |
| 243 | if (inFlightLookup) return inFlightLookup; |
| 244 | |
| 245 | const request = fetchAndMapToolkit(normalizedSlug, apiKey); |
| 246 | let trackedRequest: Promise<Toolkit | null>; |
| 247 | trackedRequest = request.finally(() => { |
| 248 | if (inFlightLookups.get(normalizedSlug) === trackedRequest) { |
| 249 | inFlightLookups.delete(normalizedSlug); |
| 250 | } |
| 251 | }); |
| 252 | |
| 253 | if (inFlightLookups.size >= IN_FLIGHT_LOOKUP_MAX_ENTRIES) { |
| 254 | const oldestSlug = inFlightLookups.keys().next().value; |
| 255 | if (oldestSlug !== undefined) inFlightLookups.delete(oldestSlug); |
| 256 | } |
| 257 | inFlightLookups.set(normalizedSlug, trackedRequest); |
| 258 | |
| 259 | return trackedRequest; |
| 260 | } |
| 261 | |
| 262 | async function fetchAndMapToolkit(normalizedSlug: string, apiKey: string): Promise<Toolkit | null> { |
| 263 | try { |
| 264 | const client = createComposioClient(apiKey, { revalidate: 3600, maxRetries: 0 }); |
| 265 | const response = await client.toolkits.retrieve(normalizedSlug, null, { |
| 266 | signal: AbortSignal.timeout(PRODUCTION_REQUEST_TIMEOUT_MS), |
| 267 | }); |
| 268 | const payload = rawToolkitSchema.parse(response); |
| 269 | if (payload.type !== 'native' || payload.enabled !== true) { |
| 270 | recordNegative(normalizedSlug); |
| 271 | console.warn( |
| 272 | `[Toolkits] Production toolkit lookup rejected non-public toolkit "${normalizedSlug}"` |
| 273 | ); |
| 274 | return null; |
| 275 | } |
| 276 | return toolkitFromRaw(payload, normalizedSlug); |
| 277 | } catch (error) { |
| 278 | recordNegative(normalizedSlug); |
| 279 | const message = error instanceof Error ? error.message : String(error); |
| 280 | console.warn(`[Toolkits] Production toolkit lookup failed for "${normalizedSlug}": ${message}`); |
| 281 | return null; |
| 282 | } |
| 283 | } |