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 | #!/usr/bin/env bun |
| 2 | |
| 3 | import { createServer, type IncomingMessage, type ServerResponse } from 'node:http'; |
| 4 | |
| 5 | const GMAIL_TOOLKIT = { |
| 6 | name: 'Gmail', |
| 7 | slug: 'gmail', |
| 8 | auth_schemes: ['OAUTH2'], |
| 9 | composio_managed_auth_schemes: ['OAUTH2'], |
| 10 | is_local_toolkit: false, |
| 11 | meta: { |
| 12 | description: 'Gmail toolkit for deterministic CLI e2e tests.', |
| 13 | categories: [], |
| 14 | created_at: '2024-01-01T00:00:00.000Z', |
| 15 | updated_at: '2024-01-01T00:00:00.000Z', |
| 16 | available_versions: ['2024.01.01'], |
| 17 | tools_count: 40, |
| 18 | triggers_count: 2, |
| 19 | }, |
| 20 | no_auth: false, |
| 21 | } as const; |
| 22 | |
| 23 | const GMAIL_TOOLKIT_DETAILED = { |
| 24 | name: 'Gmail', |
| 25 | slug: 'gmail', |
| 26 | is_local_toolkit: false, |
| 27 | composio_managed_auth_schemes: ['OAUTH2'], |
| 28 | no_auth: false, |
| 29 | meta: { |
| 30 | description: 'Gmail toolkit for deterministic CLI e2e tests.', |
| 31 | categories: [], |
| 32 | created_at: '2024-01-01T00:00:00.000Z', |
| 33 | updated_at: '2024-01-01T00:00:00.000Z', |
| 34 | available_versions: ['2024.01.01'], |
| 35 | tools_count: 40, |
| 36 | triggers_count: 2, |
| 37 | }, |
| 38 | auth_config_details: [ |
| 39 | { |
| 40 | mode: 'OAUTH2', |
| 41 | name: 'OAuth 2.0', |
| 42 | fields: { |
| 43 | auth_config_creation: { |
| 44 | required: [], |
| 45 | optional: [], |
| 46 | }, |
| 47 | connected_account_initiation: { |
| 48 | required: [], |
| 49 | optional: [], |
| 50 | }, |
| 51 | }, |
| 52 | }, |
| 53 | ], |
| 54 | } as const; |
| 55 | |
| 56 | export interface MockToolkitsServer { |
| 57 | readonly hostBaseUrl: string; |
| 58 | readonly dockerBaseUrl: string; |
| 59 | readonly requests: string[]; |
| 60 | readonly close: () => Promise<void>; |
| 61 | } |
| 62 | |
| 63 | const sendJson = (res: ServerResponse, statusCode: number, body: unknown): void => { |
| 64 | res.writeHead(statusCode, { 'Content-Type': 'application/json' }); |
| 65 | res.end(JSON.stringify(body)); |
| 66 | }; |
| 67 | |
| 68 | const matchingToolkits = (search: string | null): ReadonlyArray<typeof GMAIL_TOOLKIT> => { |
| 69 | if (search === null || search.length === 0) { |
| 70 | return [GMAIL_TOOLKIT]; |
| 71 | } |
| 72 | |
| 73 | const normalized = search.toLowerCase(); |
| 74 | if (normalized === 'gmail' || normalized === 'gmai') { |
| 75 | return [GMAIL_TOOLKIT]; |
| 76 | } |
| 77 | |
| 78 | return []; |
| 79 | }; |
| 80 | |
| 81 | const parseLimit = (req: IncomingMessage): number => { |
| 82 | const url = new URL(req.url ?? '/', `http://${req.headers.host ?? '127.0.0.1'}`); |
| 83 | const parsed = Number.parseInt(url.searchParams.get('limit') ?? '', 10); |
| 84 | return Number.isFinite(parsed) && parsed > 0 ? parsed : 30; |
| 85 | }; |
| 86 | |
| 87 | export async function startMockToolkitsListServer(options?: { |
| 88 | host?: string; |
| 89 | port?: number; |
| 90 | }): Promise<MockToolkitsServer> { |
| 91 | const host = options?.host ?? '0.0.0.0'; |
| 92 | const port = options?.port ?? 0; |
| 93 | const requests: string[] = []; |
| 94 | |
| 95 | const server = createServer((req, res) => { |
| 96 | const url = new URL(req.url ?? '/', `http://${req.headers.host ?? '127.0.0.1'}`); |
| 97 | const method = req.method ?? 'GET'; |
| 98 | requests.push(`${method} ${url.pathname}${url.search}`); |
| 99 | |
| 100 | if ( |
| 101 | method === 'GET' && |
| 102 | (url.pathname === '/api/v3/toolkits' || url.pathname === '/api/v3.1/toolkits') |
| 103 | ) { |
| 104 | const items = matchingToolkits(url.searchParams.get('search')).slice(0, parseLimit(req)); |
| 105 | sendJson(res, 200, { |
| 106 | items, |
| 107 | total_items: items.length, |
| 108 | total_pages: 1, |
| 109 | current_page: 1, |
| 110 | next_cursor: null, |
| 111 | }); |
| 112 | return; |
| 113 | } |
| 114 | |
| 115 | if ( |
| 116 | method === 'GET' && |
| 117 | (url.pathname === '/api/v3/toolkits/gmail' || url.pathname === '/api/v3.1/toolkits/gmail') |
| 118 | ) { |
| 119 | sendJson(res, 200, GMAIL_TOOLKIT_DETAILED); |
| 120 | return; |
| 121 | } |
| 122 | |
| 123 | if ( |
| 124 | method === 'GET' && |
| 125 | (url.pathname.startsWith('/api/v3/toolkits/') || |
| 126 | url.pathname.startsWith('/api/v3.1/toolkits/')) |
| 127 | ) { |
| 128 | sendJson(res, 404, { |
| 129 | error: `Toolkit not found: ${url.pathname.split('/').at(-1) ?? 'unknown'}`, |
| 130 | }); |
| 131 | return; |
| 132 | } |
| 133 | |
| 134 | if (method === 'POST' && url.pathname === '/api/v3/cli/analytics') { |
| 135 | sendJson(res, 204, {}); |
| 136 | return; |
| 137 | } |
| 138 | |
| 139 | sendJson(res, 404, { |
| 140 | error: `Unhandled mock route: ${method} ${url.pathname}`, |
| 141 | }); |
| 142 | }); |
| 143 | |
| 144 | await new Promise<void>((resolve, reject) => { |
| 145 | server.once('error', reject); |
| 146 | server.listen({ host, port }, () => { |
| 147 | server.off('error', reject); |
| 148 | resolve(); |
| 149 | }); |
| 150 | }); |
| 151 | |
| 152 | const address = server.address(); |
| 153 | if (address === null || typeof address === 'string') { |
| 154 | throw new Error('Failed to bind mock toolkits list server to a TCP port'); |
| 155 | } |
| 156 | |
| 157 | const close = () => |
| 158 | new Promise<void>((resolve, reject) => { |
| 159 | server.close(error => { |
| 160 | if (error) { |
| 161 | reject(error); |
| 162 | return; |
| 163 | } |
| 164 | resolve(); |
| 165 | }); |
| 166 | }); |
| 167 | |
| 168 | return { |
| 169 | hostBaseUrl: `http://127.0.0.1:${address.port}`, |
| 170 | dockerBaseUrl: `http://host.docker.internal:${address.port}`, |
| 171 | requests, |
| 172 | close, |
| 173 | }; |
| 174 | } |
| 175 | |
| 176 | const parsePortArg = (): number | undefined => { |
| 177 | const index = process.argv.indexOf('--port'); |
| 178 | if (index === -1) return undefined; |
| 179 | |
| 180 | const value = Number.parseInt(process.argv[index + 1] ?? '', 10); |
| 181 | return Number.isFinite(value) ? value : undefined; |
| 182 | }; |
| 183 | |
| 184 | if (import.meta.main) { |
| 185 | const server = await startMockToolkitsListServer({ port: parsePortArg() }); |
| 186 | |
| 187 | const shutdown = async () => { |
| 188 | await server.close(); |
| 189 | process.exit(0); |
| 190 | }; |
| 191 | |
| 192 | process.on('SIGINT', () => { |
| 193 | void shutdown(); |
| 194 | }); |
| 195 | process.on('SIGTERM', () => { |
| 196 | void shutdown(); |
| 197 | }); |
| 198 | |
| 199 | await new Promise(() => {}); |
| 200 | } |