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 480 lines 17 KB
Raw
1 import { spawnSync } from 'child_process';
2 import { describe, expect, it } from 'vitest';
3 import { generatePythonToolkitSources } from 'src/generation/python/generate-toolkit-sources';
4 import { createToolkitIndex } from 'src/generation/create-toolkit-index';
5 import { BANNER } from 'src/generation/constants';
6 import { makeTestToolkits } from 'test/__utils__/models/toolkits';
7 import { assertPythonIsValid } from 'test/__utils__/python-compiler';
8 import { TRIGGER_TYPES_GMAIL } from 'test/__mocks__/trigger-types-gmail';
9 import { TOOLS_GMAIL } from 'test/__mocks__/tools-gmail';
10 import { TOOLS_TYPES_GMAIL } from 'test/__mocks__/tools-types-gmail';
11
12 describe('generatePythonToolkitSources', () => {
13 it('[Given] empty toolkits, tools, triggerTypes [Then] it returns an empty array', () => {
14 const index = createToolkitIndex({
15 toolkits: [],
16 typeableTools: { withTypes: false, tools: [] },
17 triggerTypes: [],
18 });
19
20 const sources = generatePythonToolkitSources(BANNER)(index);
21
22 expect(sources).toEqual([]);
23 });
24
25 it('[Given] a single toolkit with no tools or triggerTypes [Then] it returns a single Python source file', () => {
26 const toolkits = makeTestToolkits([
27 {
28 name: 'Slack Helper',
29 slug: 'slack',
30 },
31 ]);
32
33 const index = createToolkitIndex({
34 toolkits,
35 typeableTools: { withTypes: false, tools: [] },
36 triggerTypes: [],
37 });
38
39 const sources = generatePythonToolkitSources(BANNER)(index);
40 expect(sources).toHaveLength(1);
41 expect(sources[0]).toHaveLength(2);
42 expect(sources[0][0]).toBe('slack.py');
43 expect(sources[0][1]).toMatchInlineSnapshot(`
44 "# Auto-generated by Composio CLI. Do not modify manually..
45
46 class SLACK:
47 """Map of Composio's SLACK toolkit."""
48
49 slug: str = "slack"
50
51 class tools:
52 pass
53
54 class triggers:
55 pass
56 "
57 `);
58
59 assertPythonIsValid({ files: Object.fromEntries(sources) });
60 });
61
62 it('[Given] toolkits with tools and triggerTypes [Then] it returns a Python source file for each toolkit', () => {
63 const toolkits = makeTestToolkits([
64 {
65 name: 'Gmail',
66 slug: 'gmail',
67 },
68 {
69 name: 'Slack Helper',
70 slug: 'slack',
71 },
72 ]);
73
74 const index = createToolkitIndex({
75 toolkits,
76 typeableTools: { withTypes: false, tools: [...TOOLS_GMAIL.slice(0, 3)] },
77 triggerTypes: [...TRIGGER_TYPES_GMAIL],
78 });
79
80 const sources = generatePythonToolkitSources(BANNER)(index);
81 expect(sources).toHaveLength(2);
82
83 expect(sources[0]).toHaveLength(2);
84 expect(sources[0][0]).toBe('gmail.py');
85 expect(sources[0][1]).toMatchInlineSnapshot(`
86 "# Auto-generated by Composio CLI. Do not modify manually..
87
88 class GMAIL:
89 """Map of Composio's GMAIL toolkit."""
90
91 slug: str = "gmail"
92
93 class tools:
94 ADD_LABEL_TO_EMAIL = "GMAIL_ADD_LABEL_TO_EMAIL"
95 CREATE_EMAIL_DRAFT = "GMAIL_CREATE_EMAIL_DRAFT"
96 CREATE_LABEL = "GMAIL_CREATE_LABEL"
97
98 class triggers:
99 NEW_GMAIL_MESSAGE = {
100 "config": {
101 "properties": {
102 "interval": {
103 "default": 1,
104 "description": "Periodic Interval to Check for Updates & Send a Trigger in Minutes",
105 "title": "Interval",
106 "type": "number"
107 },
108 "labelIds": {
109 "default": "INBOX",
110 "description": "Filter messages by a single label ID. Labels identify the status or category of messages. Supported labels include 'INBOX', 'SPAM', 'TRASH', 'UNREAD', 'STARRED', 'IMPORTANT', 'CATEGORY_PERSONAL', 'CATEGORY_SOCIAL', 'CATEGORY_PROMOTIONS', 'CATEGORY_UPDATES', and 'CATEGORY_FORUMS'. For complex label filtering, use the 'query' parameter instead.",
111 "examples": ["INBOX", "UNREAD", "STARRED"],
112 "title": "Labelids",
113 "type": "string"
114 },
115 "query": {
116 "default": "",
117 "description": "Advanced Gmail search using the same syntax as Gmail's search box. Use 'AND' for messages that match all conditions, 'OR' for any condition. Search by sender (from:email@domain.com), labels (label:inbox), status (is:unread), attachments (has:attachment), dates (after:2023/1/1), and more. If specified, this takes precedence over labelIds.",
118 "examples": ["label:inbox OR label:sent", "from:example@gmail.com is:unread", "has:attachment after:2023/1/1", "is:important is:unread"],
119 "title": "Query",
120 "type": "string"
121 },
122 "userId": {
123 "default": "me",
124 "description": "The user's email address or 'me' for the authenticated user.",
125 "examples": ["me"],
126 "title": "Userid",
127 "type": "string"
128 }
129 },
130 "title": "NewMessageConfig",
131 "type": "object"
132 },
133 "description": "Triggers when a new message is received in Gmail.",
134 "instructions": "\\n **Instructions for Setting Up the Trigger:**\\n\\n - Ensure that the Gmail API is enabled for your Google account.\\n - Provide the user ID (usually 'me' for the authenticated user).\\n - Optionally, provide label IDs to filter messages.\\n ",
135 "name": "New Gmail Message Received Trigger",
136 "payload": {
137 "properties": {
138 "attachment_list": {
139 "anyOf": [{
140 "items": {},
141 "type": "array"
142 }, {
143 "type": "null"
144 }],
145 "default": None,
146 "description": "The list of attachments in the message",
147 "title": "Attachment List"
148 },
149 "message_id": {
150 "anyOf": [{
151 "type": "string"
152 }, {
153 "type": "null"
154 }],
155 "default": None,
156 "description": "The message ID of the message",
157 "title": "Message ID"
158 },
159 "message_text": {
160 "anyOf": [{
161 "type": "string"
162 }, {
163 "type": "null"
164 }],
165 "default": None,
166 "description": "The text of the message",
167 "title": "Message Text"
168 },
169 "message_timestamp": {
170 "anyOf": [{
171 "type": "string"
172 }, {
173 "type": "null"
174 }],
175 "default": None,
176 "description": "The timestamp of the message",
177 "title": "Message Timestamp"
178 },
179 "payload": {
180 "anyOf": [{
181 "type": "object"
182 }, {
183 "type": "null"
184 }],
185 "default": None,
186 "description": "The payload of the message",
187 "title": "Payload"
188 },
189 "sender": {
190 "anyOf": [{
191 "type": "string"
192 }, {
193 "type": "null"
194 }],
195 "default": None,
196 "description": "The sender of the message",
197 "title": "Sender"
198 },
199 "subject": {
200 "anyOf": [{
201 "type": "string"
202 }, {
203 "type": "null"
204 }],
205 "default": None,
206 "description": "The subject of the message",
207 "title": "Subject"
208 },
209 "thread_id": {
210 "anyOf": [{
211 "type": "string"
212 }, {
213 "type": "null"
214 }],
215 "default": None,
216 "description": "The thread ID of the message",
217 "title": "Thread ID"
218 },
219 "to": {
220 "anyOf": [{
221 "type": "string"
222 }, {
223 "type": "null"
224 }],
225 "default": None,
226 "description": "The recipient of the message",
227 "title": "To"
228 }
229 },
230 "title": "NewMessagePayload",
231 "type": "object"
232 },
233 "slug": "GMAIL_NEW_GMAIL_MESSAGE",
234 "type": "poll"
235 }
236 "
237 `);
238
239 expect(sources[1]).toHaveLength(2);
240 expect(sources[1][0]).toBe('slack.py');
241 expect(sources[1][1]).toMatchInlineSnapshot(`
242 "# Auto-generated by Composio CLI. Do not modify manually..
243
244 class SLACK:
245 """Map of Composio's SLACK toolkit."""
246
247 slug: str = "slack"
248
249 class tools:
250 pass
251
252 class triggers:
253 pass
254 "
255 `);
256
257 assertPythonIsValid({ files: Object.fromEntries(sources) });
258 });
259
260 it('[Given] tool and trigger slugs containing Python source [Then] importing the module is safe', () => {
261 const toolkits = makeTestToolkits([
262 {
263 name: 'Gmail',
264 slug: 'gmail',
265 },
266 ]);
267 const maliciousToolSlug = 'GMAIL_x = "\'; __import__("os").system("echo compromised"); #';
268 const maliciousTriggerSlug = 'GMAIL_y = "\'; __import__("os").system("echo compromised"); #';
269
270 const index = createToolkitIndex({
271 toolkits,
272 typeableTools: { withTypes: false, tools: [maliciousToolSlug] },
273 triggerTypes: [{ ...TRIGGER_TYPES_GMAIL[0], slug: maliciousTriggerSlug }],
274 });
275
276 const sources = generatePythonToolkitSources(BANNER)(index);
277 const source = sources[0][1];
278
279 expect(source).toContain(
280 `locals()[${JSON.stringify(maliciousToolSlug.slice('GMAIL_'.length))}] = ${JSON.stringify(maliciousToolSlug)}`
281 );
282 expect(source).toContain(
283 `locals()[${JSON.stringify(maliciousTriggerSlug.slice('GMAIL_'.length))}] = `
284 );
285 assertPythonIsValid({ files: Object.fromEntries(sources) });
286
287 const result = spawnSync('python3', ['-c', source], { encoding: 'utf8' });
288 expect(result.stderr).toBe('');
289 expect(result.stdout).toBe('');
290 expect(result.status).toBe(0);
291 });
292
293 it('[Given] a locals slug before a fallback assignment [Then] importing the module succeeds', () => {
294 const toolkits = makeTestToolkits([
295 {
296 name: 'Gmail',
297 slug: 'gmail',
298 },
299 ]);
300 const localsSlug = 'GMAIL_locals';
301 const fallbackSlug = 'GMAIL_bad-name';
302
303 const index = createToolkitIndex({
304 toolkits,
305 typeableTools: { withTypes: false, tools: [localsSlug, fallbackSlug] },
306 triggerTypes: [
307 { ...TRIGGER_TYPES_GMAIL[0], slug: localsSlug },
308 { ...TRIGGER_TYPES_GMAIL[0], slug: fallbackSlug },
309 ],
310 });
311
312 const sources = generatePythonToolkitSources(BANNER)(index);
313 const source = sources[0][1];
314
315 expect(source.indexOf('locals()["bad-name"]')).toBeLessThan(source.indexOf('locals ='));
316 assertPythonIsValid({ files: Object.fromEntries(sources) });
317
318 const result = spawnSync('python3', ['-c', source], { encoding: 'utf8' });
319 expect(result.stderr).toBe('');
320 expect(result.status).toBe(0);
321 });
322
323 describe('[Given] versionMap with toolkit version overrides', () => {
324 describe('without type tools', () => {
325 it('[Given] a toolkit with version override [Then] it includes version comment in generated file', () => {
326 const toolkits = makeTestToolkits([
327 {
328 name: 'Gmail',
329 slug: 'gmail',
330 },
331 ]);
332
333 const versionMap = new Map([['gmail', '20250901_00']]) as Map<Lowercase<string>, string>;
334
335 const index = createToolkitIndex({
336 toolkits,
337 typeableTools: { withTypes: false, tools: [] },
338 triggerTypes: [],
339 versionMap,
340 });
341
342 const sources = generatePythonToolkitSources(BANNER)(index);
343 expect(sources).toHaveLength(1);
344 expect(sources[0][0]).toBe('gmail.py');
345 expect(sources[0][1]).toContain('# @toolkit-version: 20250901_00');
346
347 assertPythonIsValid({ files: Object.fromEntries(sources) });
348 });
349
350 it('[Given] multiple toolkits with different version overrides [Then] each file includes its version comment', () => {
351 const toolkits = makeTestToolkits([
352 {
353 name: 'Gmail',
354 slug: 'gmail',
355 },
356 {
357 name: 'Slack Helper',
358 slug: 'slack',
359 },
360 ]);
361
362 const versionMap = new Map([
363 ['gmail', '20250901_00'],
364 ['slack', '20250815_00'],
365 ]) as Map<Lowercase<string>, string>;
366
367 const index = createToolkitIndex({
368 toolkits,
369 typeableTools: { withTypes: false, tools: [] },
370 triggerTypes: [],
371 versionMap,
372 });
373
374 const sources = generatePythonToolkitSources(BANNER)(index);
375 expect(sources).toHaveLength(2);
376
377 // Gmail should have its version comment
378 expect(sources[0][0]).toBe('gmail.py');
379 expect(sources[0][1]).toContain('# @toolkit-version: 20250901_00');
380
381 // Slack should have its version comment
382 expect(sources[1][0]).toBe('slack.py');
383 expect(sources[1][1]).toContain('# @toolkit-version: 20250815_00');
384
385 assertPythonIsValid({ files: Object.fromEntries(sources) });
386 });
387
388 it('[Given] only some toolkits with version override [Then] only those files include version comment', () => {
389 const toolkits = makeTestToolkits([
390 {
391 name: 'Gmail',
392 slug: 'gmail',
393 },
394 {
395 name: 'Slack Helper',
396 slug: 'slack',
397 },
398 ]);
399
400 // Only gmail has a version override
401 const versionMap = new Map([['gmail', '20250901_00']]) as Map<Lowercase<string>, string>;
402
403 const index = createToolkitIndex({
404 toolkits,
405 typeableTools: { withTypes: false, tools: [] },
406 triggerTypes: [],
407 versionMap,
408 });
409
410 const sources = generatePythonToolkitSources(BANNER)(index);
411 expect(sources).toHaveLength(2);
412
413 // Gmail should have version comment
414 expect(sources[0][0]).toBe('gmail.py');
415 expect(sources[0][1]).toContain('# @toolkit-version: 20250901_00');
416
417 // Slack should NOT have version comment
418 expect(sources[1][0]).toBe('slack.py');
419 expect(sources[1][1]).not.toContain('@toolkit-version');
420
421 assertPythonIsValid({ files: Object.fromEntries(sources) });
422 });
423
424 it('[Given] empty versionMap [Then] no files include version comment', () => {
425 const toolkits = makeTestToolkits([
426 {
427 name: 'Gmail',
428 slug: 'gmail',
429 },
430 ]);
431
432 const versionMap = new Map() as Map<Lowercase<string>, string>;
433
434 const index = createToolkitIndex({
435 toolkits,
436 typeableTools: { withTypes: false, tools: [] },
437 triggerTypes: [],
438 versionMap,
439 });
440
441 const sources = generatePythonToolkitSources(BANNER)(index);
442 expect(sources).toHaveLength(1);
443 expect(sources[0][0]).toBe('gmail.py');
444 expect(sources[0][1]).not.toContain('@toolkit-version');
445
446 assertPythonIsValid({ files: Object.fromEntries(sources) });
447 });
448 });
449
450 describe('with type tools', () => {
451 it('[Given] a toolkit with version override [Then] it includes version comment in generated file', () => {
452 const toolkits = makeTestToolkits([
453 {
454 name: 'Gmail',
455 slug: 'gmail',
456 },
457 ]);
458
459 const versionMap = new Map([['gmail', '20250901_00']]) as Map<Lowercase<string>, string>;
460
461 const index = createToolkitIndex({
462 toolkits,
463 typeableTools: {
464 withTypes: true,
465 tools: [...TOOLS_TYPES_GMAIL.slice(0, 1)],
466 },
467 triggerTypes: [],
468 versionMap,
469 });
470
471 const sources = generatePythonToolkitSources(BANNER)(index);
472 expect(sources).toHaveLength(1);
473 expect(sources[0][0]).toBe('gmail.py');
474 expect(sources[0][1]).toContain('# @toolkit-version: 20250901_00');
475
476 assertPythonIsValid({ files: Object.fromEntries(sources) });
477 });
478 });
479 });
480 });