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 { readFile } from 'fs/promises'; |
| 2 | import { readFileSync } from 'fs'; |
| 3 | import { join } from 'path'; |
| 4 | import type { Toolkit } from '@/types/toolkit'; |
| 5 | |
| 6 | const TOOLKITS_PATH = join(process.cwd(), 'public/data/toolkits.json'); |
| 7 | |
| 8 | let cached: { list: Toolkit[]; bySlug: Map<string, Toolkit> } | null = null; |
| 9 | |
| 10 | function buildCache(toolkits: Toolkit[]) { |
| 11 | cached = { |
| 12 | list: toolkits, |
| 13 | bySlug: new Map(toolkits.map(t => [t.slug, t])), |
| 14 | }; |
| 15 | return cached; |
| 16 | } |
| 17 | |
| 18 | function parse(data: string): Toolkit[] { |
| 19 | const toolkits = JSON.parse(data) as Toolkit[]; |
| 20 | |
| 21 | if (!Array.isArray(toolkits)) { |
| 22 | throw new Error('toolkits.json must contain an array'); |
| 23 | } |
| 24 | |
| 25 | if (toolkits.length === 0) { |
| 26 | console.warn('[Toolkits] Warning: toolkits.json is empty'); |
| 27 | } |
| 28 | |
| 29 | return toolkits; |
| 30 | } |
| 31 | |
| 32 | export async function getAllToolkits(): Promise<Toolkit[]> { |
| 33 | if (cached) return cached.list; |
| 34 | const data = await readFile(TOOLKITS_PATH, 'utf-8'); |
| 35 | return buildCache(parse(data)).list; |
| 36 | } |
| 37 | |
| 38 | export async function getToolkitBySlug(slug: string): Promise<Toolkit | null> { |
| 39 | if (cached) return cached.bySlug.get(slug) ?? null; |
| 40 | const data = await readFile(TOOLKITS_PATH, 'utf-8'); |
| 41 | return buildCache(parse(data)).bySlug.get(slug) ?? null; |
| 42 | } |
| 43 | |
| 44 | export function getAllToolkitsSync(): Toolkit[] { |
| 45 | if (cached) return cached.list; |
| 46 | const data = readFileSync(TOOLKITS_PATH, 'utf-8'); |
| 47 | return buildCache(parse(data)).list; |
| 48 | } |