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 478 lines 15.2 KB
Raw
1 import { describe, expect, it, vi } from 'vitest';
2 import type { ExecuteToolFn, Tool } from '@composio/core';
3 import type { ToolDefinition } from '@earendil-works/pi-coding-agent';
4 import {
5 PiProvider,
6 PI_COMPOSIO_SESSION_TOOL_NAMES,
7 extractComposioConnectLinks,
8 } from '../src/index';
9
10 const composioTool = {
11 slug: 'GITHUB_CREATE_ISSUE',
12 name: 'Create Issue',
13 description: 'Create a GitHub issue',
14 inputParameters: {
15 type: 'object',
16 properties: {
17 owner: { type: 'string' },
18 repo: { type: 'string' },
19 title: { type: 'string' },
20 },
21 required: ['owner', 'repo', 'title'],
22 },
23 } as Tool;
24
25 describe('PiProvider', () => {
26 it('wraps a Composio tool as a Pi custom tool', async () => {
27 const executeTool = vi.fn(async () => ({
28 successful: true,
29 data: { issueNumber: 123 },
30 error: null,
31 logId: 'log_123',
32 })) as unknown as ExecuteToolFn;
33 const provider = new PiProvider();
34
35 const tool = provider.wrapTool(composioTool, executeTool);
36
37 expect(tool.name).toBe('GITHUB_CREATE_ISSUE');
38 expect(tool.label).toContain('Create Issue');
39 expect(tool.parameters).toMatchObject({ type: 'object' });
40
41 const result = await tool.execute(
42 'call_1',
43 { owner: 'ComposioHQ', repo: 'composio', title: 'Test' } as never,
44 undefined,
45 undefined,
46 undefined as never
47 );
48
49 expect(executeTool).toHaveBeenCalledWith('GITHUB_CREATE_ISSUE', {
50 owner: 'ComposioHQ',
51 repo: 'composio',
52 title: 'Test',
53 });
54 expect((result.content[0] as { text: string } | undefined)?.text).toContain('issueNumber');
55 expect((result.details as { slug?: string }).slug).toBe('GITHUB_CREATE_ISSUE');
56 });
57
58 it('creates dynamic session tools for search, manage connections, and execute', async () => {
59 const session = {
60 sessionId: 'trs_123',
61 search: vi.fn(async () => ({ results: [{ tool: 'GITHUB_CREATE_ISSUE' }] })),
62 execute: vi.fn(async (toolSlug: string) => ({
63 successful: true,
64 data: { toolSlug },
65 error: null,
66 })),
67 authorize: vi.fn(async (toolkit: string) => ({
68 redirectUrl: `https://connect.composio.dev/${toolkit}`,
69 })),
70 };
71 const provider = new PiProvider();
72 const tools = provider.createSessionTools(session);
73 const piCompatibleTools: ToolDefinition[] = tools;
74
75 expect(piCompatibleTools.map(tool => tool.name)).toEqual([
76 PI_COMPOSIO_SESSION_TOOL_NAMES.search,
77 PI_COMPOSIO_SESSION_TOOL_NAMES.manageConnections,
78 PI_COMPOSIO_SESSION_TOOL_NAMES.execute,
79 ]);
80
81 const search = tools[0]!;
82 const searchResult = await search.execute(
83 'call_search',
84 { query: 'create github issue', toolkits: ['github'] } as never,
85 undefined,
86 undefined,
87 undefined as never
88 );
89 expect(session.search).toHaveBeenCalledWith({
90 query: 'create github issue',
91 toolkits: ['github'],
92 });
93 expect((searchResult.content[0] as { text: string } | undefined)?.text).toContain(
94 'GITHUB_CREATE_ISSUE'
95 );
96
97 const execute = tools[2]!;
98 await execute.execute(
99 'call_execute',
100 { toolSlug: 'GITHUB_CREATE_ISSUE', arguments: { title: 'Hello' }, account: 'acct' } as never,
101 undefined,
102 undefined,
103 undefined as never
104 );
105 expect(session.execute).toHaveBeenCalledWith(
106 'GITHUB_CREATE_ISSUE',
107 { title: 'Hello' },
108 { account: 'acct' }
109 );
110 });
111
112 it('can include first-class remote workbench helpers', async () => {
113 const session = {
114 sessionId: 'trs_123',
115 search: vi.fn(),
116 execute: vi.fn(async (toolSlug: string, args: Record<string, unknown>) => ({
117 successful: true,
118 data: { toolSlug, args },
119 error: null,
120 })),
121 authorize: vi.fn(),
122 };
123 const provider = new PiProvider();
124 const tools = provider.createSessionTools(session, { includeWorkbenchTools: true });
125
126 expect(tools.map(tool => tool.name)).toEqual([
127 PI_COMPOSIO_SESSION_TOOL_NAMES.search,
128 PI_COMPOSIO_SESSION_TOOL_NAMES.manageConnections,
129 PI_COMPOSIO_SESSION_TOOL_NAMES.execute,
130 PI_COMPOSIO_SESSION_TOOL_NAMES.remoteWorkbench,
131 PI_COMPOSIO_SESSION_TOOL_NAMES.remoteBash,
132 ]);
133
134 const remoteWorkbench = tools[3]!;
135 await remoteWorkbench.execute(
136 'call_workbench',
137 { code_to_execute: 'print("hello")' } as never,
138 undefined,
139 undefined,
140 undefined as never
141 );
142
143 expect(session.execute).toHaveBeenCalledWith('COMPOSIO_REMOTE_WORKBENCH', {
144 code_to_execute: 'print("hello")',
145 session_id: 'trs_123',
146 });
147
148 const remoteBash = tools[4]!;
149 await remoteBash.execute(
150 'call_bash',
151 { command: 'ls -la', session_id: 'workflow-1' } as never,
152 undefined,
153 undefined,
154 undefined as never
155 );
156
157 expect(session.execute).toHaveBeenCalledWith('COMPOSIO_REMOTE_BASH_TOOL', {
158 command: 'ls -la',
159 session_id: 'workflow-1',
160 });
161 });
162
163 it('applies remote workbench and bash hooks', async () => {
164 const execute = vi.fn(async (toolSlug: string, args: Record<string, unknown>) => ({
165 successful: true,
166 data: { toolSlug, args },
167 error: null,
168 }));
169 const provider = new PiProvider();
170 const tools = provider.createSessionTools({
171 sessionId: 'trs_123',
172 search: vi.fn(async () => ({})),
173 execute,
174 includeWorkbenchTools: true,
175 hooks: {
176 remoteWorkbench: async (ctx, next) => {
177 ctx.request.code_to_execute = 'print("patched")';
178 const result = await next();
179 return { successful: true, data: { wrapped: result }, error: null };
180 },
181 remoteBash: ctx => ({
182 successful: true,
183 data: { skippedCommand: ctx.request.command },
184 error: null,
185 }),
186 },
187 });
188
189 const remoteWorkbench = tools[3]!;
190 const workbenchResult = await remoteWorkbench.execute(
191 'call_workbench',
192 { code_to_execute: 'print("hello")' } as never,
193 undefined,
194 undefined,
195 undefined as never
196 );
197
198 expect(execute).toHaveBeenCalledWith(
199 'COMPOSIO_REMOTE_WORKBENCH',
200 { code_to_execute: 'print("patched")', session_id: 'trs_123' },
201 undefined,
202 expect.objectContaining({ sourceTool: PI_COMPOSIO_SESSION_TOOL_NAMES.remoteWorkbench })
203 );
204 expect((workbenchResult.content[0] as { text: string } | undefined)?.text).toContain('wrapped');
205
206 const remoteBash = tools[4]!;
207 const bashResult = await remoteBash.execute(
208 'call_bash',
209 { command: 'cat secret.txt' } as never,
210 undefined,
211 undefined,
212 undefined as never
213 );
214
215 expect(execute).not.toHaveBeenCalledWith(
216 'COMPOSIO_REMOTE_BASH_TOOL',
217 expect.anything(),
218 expect.anything(),
219 expect.anything()
220 );
221 expect((bashResult.content[0] as { text: string } | undefined)?.text).toContain(
222 'skippedCommand'
223 );
224 });
225
226 it('manages connections through native toolkit-state and authorize handlers without executing a meta tool', async () => {
227 const session = {
228 sessionId: 'trs_123',
229 search: vi.fn(async (_params: { query: string; toolkits?: string[] }) => ({})),
230 execute: vi.fn(
231 async (
232 _toolSlug: string,
233 _args?: Record<string, unknown>,
234 _options?: { account?: string }
235 ) => ({})
236 ),
237 toolkits: vi.fn(async (_options?: { toolkits?: string[] }) => ({
238 items: [
239 { slug: 'github', connection: { isActive: true } },
240 { slug: 'gmail', connection: undefined },
241 ],
242 })),
243 authorize: vi.fn(async (toolkit: string, _options?: unknown) => ({
244 redirectUrl: `https://connect.composio.dev/${toolkit}`,
245 })),
246 };
247 const handleAuthLink = vi.fn();
248 const provider = new PiProvider();
249 const [_, manageConnections] = provider.createSessionTools({
250 sessionId: session.sessionId,
251 search: session.search,
252 execute: session.execute,
253 connections: {
254 getToolkitStates: (toolkits: string[]) => session.toolkits({ toolkits }),
255 authorizeToolkit: (toolkit: string, options: unknown) =>
256 session.authorize(toolkit, options),
257 },
258 hooks: { onAuthLink: handleAuthLink },
259 callbackUrl: 'https://example.com/callback',
260 });
261
262 const result = await manageConnections!.execute(
263 'call_manage',
264 { toolkits: ['github', 'gmail'] } as never,
265 undefined,
266 undefined,
267 undefined as never
268 );
269
270 expect(session.toolkits).toHaveBeenCalledWith({ toolkits: ['github', 'gmail'] });
271 expect(session.authorize).toHaveBeenCalledWith('gmail', {
272 callbackUrl: 'https://example.com/callback',
273 reinitiate: false,
274 });
275 expect(session.execute).not.toHaveBeenCalledWith(
276 'COMPOSIO_MANAGE_CONNECTIONS',
277 expect.anything(),
278 expect.anything()
279 );
280 expect(handleAuthLink).toHaveBeenCalledWith(
281 expect.objectContaining({
282 url: 'https://connect.composio.dev/gmail',
283 toolkit: 'gmail',
284 sourceTool: PI_COMPOSIO_SESSION_TOOL_NAMES.manageConnections,
285 }),
286 expect.any(Function)
287 );
288 expect((result.content[0] as { text: string } | undefined)?.text).toContain('auth_initiated');
289 });
290
291 it('applies search and execute hooks', async () => {
292 const search = vi.fn(async () => ({ results: [{ tool: 'SLACKBOT_SEND_MESSAGE' }] }));
293 const execute = vi.fn(async (toolSlug: string, args: Record<string, unknown>) => ({
294 successful: true,
295 data: { toolSlug, args },
296 error: null,
297 }));
298 const provider = new PiProvider();
299 const tools = provider.createSessionTools({
300 search,
301 execute,
302 hooks: {
303 search: (ctx, next) => {
304 ctx.request.toolkits = ctx.request.toolkits?.map(toolkit =>
305 toolkit === 'slack' ? 'slackbot' : toolkit
306 );
307 return next();
308 },
309 execute: (ctx, next) => {
310 if (ctx.request.toolSlug.startsWith('COMPOSIO_')) {
311 return ctx.deny('meta tools blocked');
312 }
313 ctx.request.toolSlug = ctx.request.toolSlug.replace(/^SLACK_/, 'SLACKBOT_');
314 return next();
315 },
316 },
317 });
318
319 const searchTool = tools[0]!;
320 await searchTool.execute(
321 'call_search',
322 { query: 'message channel', toolkits: ['slack'] } as never,
323 undefined,
324 undefined,
325 undefined as never
326 );
327 expect(search).toHaveBeenCalledWith(
328 { query: 'message channel', toolkits: ['slackbot'] },
329 expect.objectContaining({
330 requestedToolkits: ['slackbot'],
331 originalRequest: expect.objectContaining({ toolkits: ['slack'] }),
332 })
333 );
334
335 const executeTool = tools[2]!;
336 await executeTool.execute(
337 'call_execute',
338 { toolSlug: 'SLACK_SEND_MESSAGE', arguments: { text: 'hi' } } as never,
339 undefined,
340 undefined,
341 undefined as never
342 );
343 expect(execute).toHaveBeenCalledWith(
344 'SLACKBOT_SEND_MESSAGE',
345 { text: 'hi' },
346 undefined,
347 expect.objectContaining({ toolSlug: 'SLACKBOT_SEND_MESSAGE' })
348 );
349
350 const denied = await executeTool.execute(
351 'call_execute',
352 { toolSlug: 'COMPOSIO_MANAGE_CONNECTIONS', arguments: {} } as never,
353 undefined,
354 undefined,
355 undefined as never
356 );
357 expect((denied.content[0] as { text: string } | undefined)?.text).toContain(
358 'meta tools blocked'
359 );
360 expect((denied.details as { denied?: boolean }).denied).toBe(true);
361 });
362
363 it('lets hooks inspect default results and replace what the model sees', async () => {
364 const loggedResults: unknown[] = [];
365 const execute = vi.fn(async (toolSlug: string) => ({
366 successful: true,
367 data: { toolSlug, largePayload: 'secret-inline-data' },
368 error: null,
369 }));
370 const provider = new PiProvider();
371 const tools = provider.createSessionTools({
372 search: vi.fn(async () => ({})),
373 execute,
374 hooks: {
375 execute: async (_ctx, next) => {
376 const response = await next();
377 loggedResults.push(response);
378 return {
379 successful: true,
380 data: { file: '/mnt/composio/tool-output.json' },
381 error: null,
382 };
383 },
384 },
385 });
386
387 const result = await tools[2]!.execute(
388 'call_execute',
389 { toolSlug: 'GITHUB_LIST_ISSUES', arguments: {} } as never,
390 undefined,
391 undefined,
392 undefined as never
393 );
394
395 expect(loggedResults).toEqual([
396 {
397 successful: true,
398 data: { toolSlug: 'GITHUB_LIST_ISSUES', largePayload: 'secret-inline-data' },
399 error: null,
400 },
401 ]);
402 const text = (result.content[0] as { text: string } | undefined)?.text;
403 expect(text).toContain('/mnt/composio/tool-output.json');
404 expect(text).not.toContain('secret-inline-data');
405 });
406
407 it('lets auth-link hooks choose whether the link goes to the model', async () => {
408 const provider = new PiProvider();
409 const tools = provider.createSessionTools({
410 search: vi.fn(async () => ({})),
411 execute: vi.fn(async () => ({})),
412 connections: {
413 getToolkitStates: () => ({ items: [{ slug: 'gmail', connection: undefined }] }),
414 authorizeToolkit: toolkit => ({ redirectUrl: `https://connect.composio.dev/${toolkit}` }),
415 },
416 hooks: {
417 onAuthLink: async ctx => {
418 expect(ctx.url).toBe('https://connect.composio.dev/gmail');
419 return { message: 'Connection link sent out-of-band.' };
420 },
421 },
422 });
423
424 const result = await tools[1]!.execute(
425 'call_manage',
426 { toolkits: ['gmail'] } as never,
427 undefined,
428 undefined,
429 undefined as never
430 );
431
432 const text = (result.content[0] as { text: string } | undefined)?.text;
433 expect(text).toContain('Connection link sent out-of-band.');
434 expect(text).not.toContain('https://connect.composio.dev/gmail');
435 expect((result.details as { authLinks?: string[] }).authLinks).toEqual([
436 'https://connect.composio.dev/gmail',
437 ]);
438 });
439
440 it('extracts Composio connect links from nested results', () => {
441 expect(
442 extractComposioConnectLinks({
443 data: { redirectUrl: 'https://connect.composio.dev/abc123.' },
444 })
445 ).toEqual(['https://connect.composio.dev/abc123']);
446 });
447
448 it('extracts generic Composio auth links case-insensitively', () => {
449 expect(extractComposioConnectLinks('Open HTTPS://auth.Composio.dev/LINK/abc123')).toEqual([
450 'HTTPS://auth.Composio.dev/LINK/abc123',
451 ]);
452 });
453
454 it('returns connect links first and deduplicates links across classifiers', () => {
455 const genericLink = 'https://auth.composio.dev/link/generic';
456 const connectLink = 'https://connect.composio.dev/link/connect';
457
458 expect(
459 extractComposioConnectLinks(`${genericLink} ${connectLink} ${genericLink} ${connectLink}`)
460 ).toEqual([connectLink, genericLink]);
461 });
462
463 it('trims repeated trailing punctuation from auth links', () => {
464 const repeatedPunctuation = '!?.,;:'.repeat(5_000);
465
466 expect(
467 extractComposioConnectLinks(
468 `Connect at https://connect.composio.dev/abc123${repeatedPunctuation} or https://auth.composio.dev/link/xyz!?.,`
469 )
470 ).toEqual(['https://connect.composio.dev/abc123', 'https://auth.composio.dev/link/xyz']);
471 });
472
473 it('handles long adversarial nonmatches without backtracking', () => {
474 const nonMatch = `https://${'composio'.repeat(20_000)}/not-a-link`;
475
476 expect(extractComposioConnectLinks(nonMatch)).toEqual([]);
477 });
478 });