Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.

next
ts 133 lines 3.8 KB
Raw
1 #!/usr/bin/env bun
2
3 import { createServer, type ServerResponse } from 'node:http';
4 import { SESSION_INFO } from './mock-whoami-server';
5
6 /**
7 * Mock agents.composio.dev + the minimal Apollo endpoints the login flows
8 * touch. Point the CLI at it via COMPOSIO_AGENTS_BASE_URL / COMPOSIO_BASE_URL.
9 */
10
11 const AGENT_IDENTITY = {
12 status: 'READY',
13 slug: 'e2e-agent',
14 email: 'e2e-agent@agent.composio.ai',
15 composio_agent_key: 'cak_e2e_agent',
16 composio: {
17 member_id: 'mem_e2e_agent',
18 org_id: 'org_e2e_agent',
19 project_id: 'proj_e2e_agent',
20 user_api_key: 'uak_e2e_agent',
21 },
22 } as const;
23
24 export interface MockAgentsServer {
25 readonly hostBaseUrl: string;
26 readonly dockerBaseUrl: string;
27 readonly requests: string[];
28 readonly agentIdentity: typeof AGENT_IDENTITY;
29 readonly close: () => Promise<void>;
30 }
31
32 const sendJson = (res: ServerResponse, statusCode: number, body: unknown): void => {
33 res.writeHead(statusCode, { 'Content-Type': 'application/json' });
34 res.end(JSON.stringify(body));
35 };
36
37 export async function startMockAgentsServer(options?: {
38 host?: string;
39 port?: number;
40 }): Promise<MockAgentsServer> {
41 const host = options?.host ?? '0.0.0.0';
42 const port = options?.port ?? 0;
43 const requests: string[] = [];
44
45 const server = createServer((req, res) => {
46 const url = new URL(req.url ?? '/', `http://${req.headers.host ?? '127.0.0.1'}`);
47 const method = req.method ?? 'GET';
48 requests.push(`${method} ${url.pathname}`);
49
50 // agents.composio.dev surface
51 if (method === 'POST' && url.pathname === '/api/signup') {
52 sendJson(res, 200, AGENT_IDENTITY);
53 return;
54 }
55
56 if (method === 'GET' && url.pathname === '/api/whoami') {
57 const authorized =
58 req.headers.authorization === `Bearer ${AGENT_IDENTITY.composio_agent_key}`;
59 sendJson(
60 res,
61 authorized ? 200 : 401,
62 authorized ? AGENT_IDENTITY : { message: 'unauthorized' }
63 );
64 return;
65 }
66
67 // Apollo surface used by the default login path
68 if (method === 'POST' && url.pathname === '/api/v3.1/cli/create-session') {
69 sendJson(res, 200, {
70 id: 'e2e00000-0000-4000-8000-000000000001',
71 code: 'E2E123',
72 expiresAt: new Date(Date.now() + 10 * 60 * 1000).toISOString(),
73 status: 'pending',
74 });
75 return;
76 }
77
78 // The session `composio whoami` reads, as the agent's account.
79 if (method === 'GET' && url.pathname === '/api/v3.1/auth/session/info') {
80 sendJson(res, 200, {
81 ...SESSION_INFO,
82 org_member: { ...SESSION_INFO.org_member, email: AGENT_IDENTITY.email },
83 });
84 return;
85 }
86
87 if (method === 'GET' && url.pathname === '/api/v3.1/org/consumer/config') {
88 sendJson(res, 200, { enhanced_controls: false });
89 return;
90 }
91
92 if (method === 'POST' && url.pathname === '/api/v3/cli/analytics') {
93 sendJson(res, 204, {});
94 return;
95 }
96
97 // Unmocked routes are non-fatal to the flows under test; 404 exercises
98 // their degrade paths.
99 sendJson(res, 404, { error: `Unhandled mock route: ${method} ${url.pathname}` });
100 });
101
102 await new Promise<void>((resolve, reject) => {
103 server.once('error', reject);
104 server.listen({ host, port }, () => {
105 server.off('error', reject);
106 resolve();
107 });
108 });
109
110 const address = server.address();
111 if (address === null || typeof address === 'string') {
112 throw new Error('Failed to bind mock agents server to a TCP port');
113 }
114
115 const close = () =>
116 new Promise<void>((resolve, reject) => {
117 server.close(error => {
118 if (error) {
119 reject(error);
120 return;
121 }
122 resolve();
123 });
124 });
125
126 return {
127 hostBaseUrl: `http://127.0.0.1:${address.port}`,
128 dockerBaseUrl: `http://host.docker.internal:${address.port}`,
129 requests,
130 agentIdentity: AGENT_IDENTITY,
131 close,
132 };
133 }