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, 03:37 UTC
| 1 | import { readFile } from 'fs/promises'; |
| 2 | import { join } from 'path'; |
| 3 | import { z } from 'zod'; |
| 4 | import { getMetaToolOverride } from './meta-tool-overrides'; |
| 5 | |
| 6 | const jsonTypeSchema = z.enum([ |
| 7 | 'string', |
| 8 | 'number', |
| 9 | 'integer', |
| 10 | 'boolean', |
| 11 | 'object', |
| 12 | 'array', |
| 13 | 'null', |
| 14 | ]); |
| 15 | |
| 16 | // Normalize the schema fields used by the reference renderers. The generated |
| 17 | // snapshot remains unchanged; `any` is a display label for an unrestricted type. |
| 18 | const parameterSchema = z.object({ |
| 19 | type: z |
| 20 | .union([jsonTypeSchema, z.array(jsonTypeSchema).min(1)]) |
| 21 | .optional() |
| 22 | .transform(type => { |
| 23 | if (type === undefined) return 'any'; |
| 24 | return Array.isArray(type) ? type.join(' | ') : type; |
| 25 | }), |
| 26 | description: z |
| 27 | .string() |
| 28 | .default('') |
| 29 | .transform(description => |
| 30 | description.replace(/\*\*/g, '').replace(/__/g, '').replace(/\n+/g, ' ').trim() |
| 31 | ), |
| 32 | required: z.array(z.string()).default([]), |
| 33 | default: z.unknown().optional(), |
| 34 | enum: z |
| 35 | .array(z.union([z.string(), z.number(), z.boolean(), z.null()])) |
| 36 | .default([]) |
| 37 | .transform(values => values.map(value => String(value))), |
| 38 | get properties(): z.ZodDefault<z.ZodRecord<z.ZodString, typeof parameterSchema>> { |
| 39 | return z.record(z.string(), parameterSchema).default({}); |
| 40 | }, |
| 41 | get items(): z.ZodOptional<typeof parameterSchema> { |
| 42 | return parameterSchema.optional(); |
| 43 | }, |
| 44 | }); |
| 45 | |
| 46 | type ParsedParameter = z.infer<typeof parameterSchema>; |
| 47 | |
| 48 | export type MetaToolParameter = Omit< |
| 49 | ParsedParameter, |
| 50 | 'type' | 'default' | 'properties' | 'items' |
| 51 | > & { |
| 52 | typeLabel: string; |
| 53 | defaultLabel: string | null; |
| 54 | properties: Record<string, MetaToolParameter>; |
| 55 | items?: MetaToolParameter; |
| 56 | }; |
| 57 | |
| 58 | /** Project schema nodes into one display model before they reach either renderer. */ |
| 59 | function toDisplayParameter({ |
| 60 | type, |
| 61 | default: defaultValue, |
| 62 | properties, |
| 63 | items, |
| 64 | ...parameter |
| 65 | }: ParsedParameter): MetaToolParameter { |
| 66 | const displayItems = items ? toDisplayParameter(items) : undefined; |
| 67 | const typeLabel = |
| 68 | type === 'array' ? `array<${displayItems ? displayItems.typeLabel : 'any'}>` : type; |
| 69 | const defaultLabel = |
| 70 | defaultValue === undefined || defaultValue === null || defaultValue === '' |
| 71 | ? null |
| 72 | : String(defaultValue); |
| 73 | return { |
| 74 | ...parameter, |
| 75 | typeLabel, |
| 76 | defaultLabel, |
| 77 | properties: Object.fromEntries( |
| 78 | Object.entries(properties).map(([name, value]) => [name, toDisplayParameter(value)]) |
| 79 | ), |
| 80 | items: displayItems, |
| 81 | }; |
| 82 | } |
| 83 | |
| 84 | export type MetaToolSchema = MetaToolParameter; |
| 85 | |
| 86 | export const metaToolSchema = z |
| 87 | .object({ |
| 88 | slug: z.string(), |
| 89 | name: z.string(), |
| 90 | // Generated pages put it on one frontmatter line. |
| 91 | displayName: z.string().refine(value => !/[\r\n]/.test(value), { |
| 92 | message: 'Meta-tool displayName must be a single line', |
| 93 | }), |
| 94 | description: z.string(), |
| 95 | tags: z.array(z.string()), |
| 96 | inputParameters: parameterSchema.transform(toDisplayParameter), |
| 97 | responseSchema: parameterSchema.transform(toDisplayParameter), |
| 98 | }) |
| 99 | .transform(tool => { |
| 100 | const override = getMetaToolOverride(tool.slug); |
| 101 | return { |
| 102 | ...tool, |
| 103 | summary: override ? override.summary : tool.description, |
| 104 | whenToUse: override ? override.whenToUse : '', |
| 105 | usageNote: override?.usageNote ?? '', |
| 106 | }; |
| 107 | }); |
| 108 | |
| 109 | export type MetaTool = z.infer<typeof metaToolSchema>; |
| 110 | |
| 111 | export const metaToolsSchema = z |
| 112 | .array(metaToolSchema) |
| 113 | .min(1) |
| 114 | .refine(tools => tools.every(tool => /^COMPOSIO_[A-Z0-9_]+$/.test(tool.slug)), { |
| 115 | message: 'Invalid meta-tool slug', |
| 116 | }) |
| 117 | .refine(tools => new Set(tools.map(tool => tool.slug)).size === tools.length, { |
| 118 | message: 'Meta-tool slugs must be unique', |
| 119 | }); |
| 120 | const META_TOOLS_PATH = join(process.cwd(), 'public/data/meta-tools.json'); |
| 121 | |
| 122 | let cached: { list: MetaTool[]; bySlug: Map<string, MetaTool> } | null = null; |
| 123 | |
| 124 | async function getCache() { |
| 125 | if (cached) return cached; |
| 126 | const data = await readFile(META_TOOLS_PATH, 'utf-8'); |
| 127 | const list = metaToolsSchema.parse(JSON.parse(data)); |
| 128 | cached = { list, bySlug: new Map(list.map(tool => [tool.slug, tool])) }; |
| 129 | return cached; |
| 130 | } |
| 131 | |
| 132 | export async function getAllMetaTools(): Promise<MetaTool[]> { |
| 133 | return (await getCache()).list; |
| 134 | } |
| 135 | |
| 136 | export async function getMetaToolBySlug(slug: string): Promise<MetaTool | null> { |
| 137 | return (await getCache()).bySlug.get(slug) ?? null; |
| 138 | } |