@devries-cameron20 / composio Mirror · read-only
Fork 0

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

next
ts 492 lines 18.2 KB
Raw
1 import type { ExecuteToolFn, Tool, ToolExecuteResponse } from '@composio/core';
2 import type { ApprovalContext } from 'eve/tools/approval';
3 import { describe, expect, it, vi } from 'vitest';
4 import {
5 EveProvider,
6 type EveTool,
7 defineComposioTools,
8 denyEveToolCall,
9 requireApprovalForTools,
10 } from '../src/eve';
11
12 // eve reads the durable descriptors its build transform stamps on authored
13 // `defineTool` calls. The transform never runs on this package, so the provider
14 // stamps its own; `defineTool` collects them onto the definition under this
15 // symbol, which is what eve validates and replays from.
16 const DURABLE_TOOL_CALLBACKS = Symbol.for('eve:durable-dynamic-tool-callbacks');
17
18 type DurableDescriptor = {
19 callback: (closure: Record<string, unknown>, ...args: never[]) => unknown;
20 closure: Record<string, unknown>;
21 };
22
23 const durableCallbacks = (eveTool: EveTool): Partial<Record<string, DurableDescriptor>> =>
24 (eveTool as unknown as Record<symbol, Partial<Record<string, DurableDescriptor>>>)[
25 DURABLE_TOOL_CALLBACKS
26 ] ?? {};
27
28 const requireDurableCallback = (eveTool: EveTool, phase: string): DurableDescriptor => {
29 const descriptor = durableCallbacks(eveTool)[phase];
30 if (!descriptor) {
31 throw new TypeError(`Expected a durable "${phase}" descriptor on the wrapped tool`);
32 }
33 return descriptor;
34 };
35
36 const tool = (slug: string): Tool =>
37 ({
38 slug,
39 name: slug,
40 description: `desc ${slug}`,
41 inputParameters: { type: 'object', properties: { q: { type: 'string' } } },
42 }) as unknown as Tool;
43
44 const ok = (data: Record<string, unknown> = {}): ToolExecuteResponse => ({
45 data,
46 error: null,
47 successful: true,
48 });
49
50 const approvalContext = (
51 toolName: string,
52 toolInput: Record<string, unknown>
53 ): ApprovalContext<Record<string, unknown>> => ({
54 abortSignal: new AbortController().signal,
55 approvedTools: new Set<string>(),
56 callId: 'call-1',
57 getSandbox: async () => {
58 throw new Error('Sandbox access is not expected in approval policy tests');
59 },
60 session: {
61 id: 'eve-session',
62 auth: { current: null, initiator: null },
63 turn: { id: 'turn-1', sequence: 0 },
64 },
65 toolInput,
66 toolName,
67 });
68
69 describe('EveProvider', () => {
70 it('identifies as the eve provider', () => {
71 expect(new EveProvider().name).toBe('eve');
72 });
73
74 it('wraps tools by slug with description and schema', () => {
75 const execute: ExecuteToolFn = vi.fn(async () => ok());
76 const wrapped = new EveProvider().wrapTools([tool('GITHUB_CREATE_ISSUE')], execute);
77 const eveTool = wrapped.GITHUB_CREATE_ISSUE;
78 expect(eveTool).toBeDefined();
79 expect(eveTool.description).toBe('desc GITHUB_CREATE_ISSUE');
80 expect(eveTool.inputSchema).toEqual({ type: 'object', properties: { q: { type: 'string' } } });
81 });
82
83 it("executes through Composio's executeTool", async () => {
84 const execute: ExecuteToolFn = vi.fn(async () => ok({ url: 'x' }));
85 const wrapped = new EveProvider().wrapTools([tool('GMAIL_SEND_EMAIL')], execute);
86 const result = await wrapped.GMAIL_SEND_EMAIL.execute({ q: 'hi' }, {} as never);
87 expect(execute).toHaveBeenCalledWith('GMAIL_SEND_EMAIL', { q: 'hi' });
88 expect(result).toEqual(ok({ url: 'x' }));
89 });
90
91 it('does not mutate the source schema in strict mode', () => {
92 const source = {
93 type: 'object',
94 properties: {
95 required: { type: 'string' },
96 optional: { type: 'string' },
97 },
98 required: ['required'],
99 } as const;
100 const composioTool = {
101 ...tool('GITHUB_CREATE_ISSUE'),
102 inputParameters: source,
103 } as unknown as Tool;
104
105 const wrapped = new EveProvider({ strict: true }).wrapTools(
106 [composioTool],
107 vi.fn(async () => ok())
108 );
109
110 expect(source.properties).toHaveProperty('optional');
111 expect(source).not.toHaveProperty('additionalProperties');
112 expect(wrapped.GITHUB_CREATE_ISSUE.inputSchema).toEqual({
113 type: 'object',
114 properties: { required: { type: 'string' } },
115 required: ['required'],
116 additionalProperties: false,
117 });
118 });
119
120 it('lets an execute hook deny a call', async () => {
121 const execute: ExecuteToolFn = vi.fn(async () => ok());
122 const provider = new EveProvider({
123 hooks: { execute: ctx => ctx.deny('blocked') },
124 });
125 const wrapped = provider.wrapTools([tool('COMPOSIO_MULTI_EXECUTE_TOOL')], execute);
126 const result = await wrapped.COMPOSIO_MULTI_EXECUTE_TOOL.execute({ q: 'x' }, {} as never);
127 expect(execute).not.toHaveBeenCalled();
128 expect(result).toEqual(denyEveToolCall('blocked'));
129 });
130
131 it('routes remote bash calls to the remoteBash hook', async () => {
132 const execute: ExecuteToolFn = vi.fn(async () => ok());
133 const provider = new EveProvider({
134 hooks: { remoteBash: ctx => ctx.deny('shell blocked') },
135 });
136 const wrapped = provider.wrapTools([tool('COMPOSIO_REMOTE_BASH_TOOL')], execute);
137
138 const result = await wrapped.COMPOSIO_REMOTE_BASH_TOOL.execute(
139 { command: 'rm -rf /tmp/example' },
140 {} as never
141 );
142
143 expect(execute).not.toHaveBeenCalled();
144 expect(result).toEqual(denyEveToolCall('shell blocked'));
145 });
146
147 it("passes eve's tool context to hooks", async () => {
148 const eveContext = { session: { id: 'eve-session' } } as never;
149 const search = vi.fn((_ctx, next) => next());
150 const provider = new EveProvider({ hooks: { search } });
151 const wrapped = provider.wrapTools(
152 [tool('COMPOSIO_SEARCH_TOOLS')],
153 vi.fn(async () => ok())
154 );
155
156 await wrapped.COMPOSIO_SEARCH_TOOLS.execute({ q: 'calendar' }, eveContext);
157
158 expect(search.mock.calls[0]?.[0].context.eve).toBe(eveContext);
159 });
160
161 it('maps approval policy onto wrapped tools', () => {
162 const needsApproval = vi.fn(
163 (composioTool: Tool) => composioTool.slug === 'LOCAL_IMESSAGE_SEND'
164 );
165 const provider = new EveProvider({ needsApproval });
166 const wrapped = provider.wrapTools(
167 [tool('LOCAL_IMESSAGE_SEND'), tool('GITHUB_GET_REPOSITORY')],
168 vi.fn(async () => ok())
169 );
170 const context = approvalContext('LOCAL_IMESSAGE_SEND', {
171 to: '+15551234567',
172 text: 'test',
173 });
174
175 const sendApproval = wrapped.LOCAL_IMESSAGE_SEND.approval;
176 const readApproval = wrapped.GITHUB_GET_REPOSITORY.approval;
177 expect(sendApproval).toBeTypeOf('function');
178 expect(readApproval).toBeTypeOf('function');
179 if (typeof sendApproval !== 'function' || typeof readApproval !== 'function') {
180 throw new TypeError('Expected Composio approval policies to be functions');
181 }
182 expect(sendApproval(context)).toBe(true);
183 expect(readApproval(context)).toBe(false);
184 expect(needsApproval).toHaveBeenCalledWith(
185 expect.objectContaining({ slug: 'LOCAL_IMESSAGE_SEND' }),
186 context
187 );
188 });
189
190 it('requires approval for protected tools nested in multi-execute', () => {
191 const provider = new EveProvider({
192 needsApproval: requireApprovalForTools('LOCAL_IMESSAGE_SEND'),
193 });
194 const wrapped = provider.wrapTools(
195 [tool('COMPOSIO_MULTI_EXECUTE_TOOL')],
196 vi.fn(async () => ok())
197 );
198
199 const approval = wrapped.COMPOSIO_MULTI_EXECUTE_TOOL.approval;
200 expect(approval).toBeTypeOf('function');
201 if (typeof approval !== 'function') {
202 throw new TypeError('Expected the multi-execute approval policy to be a function');
203 }
204 expect(
205 approval(
206 approvalContext('COMPOSIO_MULTI_EXECUTE_TOOL', {
207 tools: [
208 { tool_slug: 'GMAIL_FETCH_EMAILS', arguments: {} },
209 { tool_slug: 'LOCAL_IMESSAGE_SEND', arguments: { text: 'test' } },
210 ],
211 })
212 )
213 ).toBe(true);
214 });
215
216 it('finds auth links in circular data containing bigint values', async () => {
217 const circular: Record<string, unknown> = {
218 connectionUrl: 'https://connect.composio.dev/link_123',
219 sequence: 1n,
220 };
221 circular.self = circular;
222
223 const onAuthLink = vi.fn((_ctx, next) => next());
224 const provider = new EveProvider({ hooks: { onAuthLink } });
225 const wrapped = provider.wrapTools(
226 [tool('COMPOSIO_MANAGE_CONNECTIONS')],
227 vi.fn(async () => ok(circular))
228 );
229
230 await wrapped.COMPOSIO_MANAGE_CONNECTIONS.execute({}, {} as never);
231
232 expect(onAuthLink.mock.calls[0]?.[0].url).toBe('https://connect.composio.dev/link_123');
233 });
234
235 it('finds auth links in errors even when data cannot be serialized', async () => {
236 const circular: Record<string, unknown> = {};
237 circular.self = circular;
238 Object.defineProperty(circular, 'broken', {
239 enumerable: true,
240 get: () => {
241 throw new Error('unserializable');
242 },
243 });
244 const onAuthLink = vi.fn((_ctx, next) => next());
245 const provider = new EveProvider({ hooks: { onAuthLink } });
246 const wrapped = provider.wrapTools(
247 [tool('COMPOSIO_MANAGE_CONNECTIONS')],
248 vi.fn(async () => ({
249 data: circular,
250 error: 'Connect at https://connect.composio.dev/link_123',
251 successful: false,
252 }))
253 );
254
255 await expect(
256 wrapped.COMPOSIO_MANAGE_CONNECTIONS.execute({}, {} as never)
257 ).resolves.toMatchObject({ successful: false });
258 expect(onAuthLink.mock.calls[0]?.[0].url).toBe('https://connect.composio.dev/link_123');
259 });
260 });
261
262 describe('durable callbacks', () => {
263 type ExecuteCallback = (
264 closure: Record<string, unknown>,
265 input: Record<string, unknown>,
266 context: unknown
267 ) => Promise<ToolExecuteResponse>;
268 type ApproveCallback = (
269 closure: Record<string, unknown>,
270 context: ApprovalContext<Record<string, unknown>>
271 ) => boolean;
272 const persisted = (closure: Record<string, unknown>): Record<string, unknown> =>
273 JSON.parse(JSON.stringify(closure));
274
275 it('stamps a JSON-serializable execute descriptor on every wrapped tool', () => {
276 const wrapped = new EveProvider().wrapTools(
277 [tool('GITHUB_CREATE_ISSUE')],
278 vi.fn(async () => ok())
279 );
280
281 const { callback, closure } = requireDurableCallback(wrapped.GITHUB_CREATE_ISSUE, 'execute');
282
283 expect(callback).toBeTypeOf('function');
284 expect(closure).toEqual({ slug: 'GITHUB_CREATE_ISSUE', binding: expect.any(String) });
285 expect(persisted(closure)).toEqual(closure);
286 });
287
288 it('stamps an approval descriptor only when a policy is configured', () => {
289 const execute: ExecuteToolFn = vi.fn(async () => ok());
290 const unguarded = new EveProvider().wrapTools([tool('LOCAL_IMESSAGE_SEND')], execute);
291 const guarded = new EveProvider({
292 needsApproval: requireApprovalForTools('LOCAL_IMESSAGE_SEND'),
293 }).wrapTools([tool('LOCAL_IMESSAGE_SEND')], execute);
294
295 expect(durableCallbacks(unguarded.LOCAL_IMESSAGE_SEND).approvalRequest).toBeUndefined();
296 expect(requireDurableCallback(guarded.LOCAL_IMESSAGE_SEND, 'approvalRequest').closure).toEqual(
297 requireDurableCallback(guarded.LOCAL_IMESSAGE_SEND, 'execute').closure
298 );
299 });
300
301 it('replays an execute call from its closure alone', async () => {
302 const execute: ExecuteToolFn = vi.fn(async () => ok({ url: 'x' }));
303 const wrapped = new EveProvider({
304 hooks: { execute: (ctx, next) => next() },
305 }).wrapTools([tool('COMPOSIO_EXECUTE_TOOL')], execute);
306 const { callback, closure } = requireDurableCallback(wrapped.COMPOSIO_EXECUTE_TOOL, 'execute');
307
308 const replayed = await (callback as ExecuteCallback)(persisted(closure), { q: 'hi' }, {});
309
310 expect(execute).toHaveBeenCalledWith('COMPOSIO_EXECUTE_TOOL', { q: 'hi' });
311 expect(replayed).toEqual(ok({ url: 'x' }));
312 });
313
314 it('replays an approval decision from its closure alone', () => {
315 const wrapped = new EveProvider({
316 needsApproval: requireApprovalForTools('LOCAL_IMESSAGE_SEND'),
317 }).wrapTools(
318 [tool('LOCAL_IMESSAGE_SEND')],
319 vi.fn(async () => ok())
320 );
321 const { callback, closure } = requireDurableCallback(
322 wrapped.LOCAL_IMESSAGE_SEND,
323 'approvalRequest'
324 );
325
326 const decision = (callback as ApproveCallback)(
327 persisted(closure),
328 approvalContext('LOCAL_IMESSAGE_SEND', { text: 'test' })
329 );
330
331 expect(decision).toBe(true);
332 });
333
334 it('keeps each resolve bound to its own executor when sessions share a provider', async () => {
335 const provider = new EveProvider();
336 const userA: ExecuteToolFn = vi.fn(async () => ok({ from: 'a' }));
337 const userB: ExecuteToolFn = vi.fn(async () => ok({ from: 'b' }));
338 const { callback, closure } = requireDurableCallback(
339 provider.wrapTools([tool('GMAIL_SEND_EMAIL')], userA).GMAIL_SEND_EMAIL,
340 'execute'
341 );
342
343 provider.wrapTools([tool('GMAIL_SEND_EMAIL')], userB);
344 const replayed = await (callback as ExecuteCallback)(persisted(closure), {}, {});
345
346 expect(userB).not.toHaveBeenCalled();
347 expect(replayed).toEqual(ok({ from: 'a' }));
348 });
349
350 it('applies the hooks of the provider that produced the closure', async () => {
351 const execute: ExecuteToolFn = vi.fn(async () => ok());
352 const denying = new EveProvider({
353 hooks: { execute: ctx => ctx.deny('blocked by provider A') },
354 }).wrapTools([tool('COMPOSIO_EXECUTE_TOOL')], execute);
355 const permissive = new EveProvider().wrapTools([tool('COMPOSIO_EXECUTE_TOOL')], execute);
356
357 // eve keeps one callback per tool name; whichever provider registered last replays both.
358 const { callback } = requireDurableCallback(permissive.COMPOSIO_EXECUTE_TOOL, 'execute');
359 const { closure } = requireDurableCallback(denying.COMPOSIO_EXECUTE_TOOL, 'execute');
360 const replayed = await (callback as ExecuteCallback)(persisted(closure), {}, {});
361
362 expect(execute).not.toHaveBeenCalled();
363 expect(replayed).toMatchObject({ successful: false, error: 'blocked by provider A' });
364 });
365
366 it('reports a closure this process can no longer execute', async () => {
367 const { callback, closure } = requireDurableCallback(
368 new EveProvider().wrapTools(
369 [tool('GITHUB_CREATE_ISSUE')],
370 vi.fn(async () => ok())
371 ).GITHUB_CREATE_ISSUE,
372 'execute'
373 );
374
375 await expect(
376 (callback as ExecuteCallback)({ ...closure, slug: 'GITHUB_REMOVED_TOOL' }, {}, {})
377 ).rejects.toThrow('GITHUB_REMOVED_TOOL');
378 await expect(
379 (callback as ExecuteCallback)({ ...closure, binding: 'from-another-process' }, {}, {})
380 ).rejects.toThrow('GITHUB_CREATE_ISSUE');
381 });
382 });
383
384 describe('eve replay', () => {
385 // eve's durable-callback validation and replay are not on a public export, so
386 // load them from the installed package by path. If eve moves them, this suite
387 // fails loudly instead of the provider silently drifting from the contract.
388 const eveInternal = (file: string) =>
389 import(new URL(`../node_modules/eve/dist/src/${file}`, import.meta.url).href);
390
391 it('validates and replays a wrapped tool through eve itself', async () => {
392 const [
393 { validateDurableDynamicToolCallbacks },
394 { replayDynamicTools },
395 { callDurableDynamicCallback, lookupDurableDynamicCallback },
396 ] = await Promise.all([
397 eveInternal('context/dynamic-tool-lifecycle.js'),
398 eveInternal('context/build-dynamic-tools.js'),
399 eveInternal('tools/durable-callbacks.js'),
400 ]);
401 const execute: ExecuteToolFn = vi.fn(async () => ok({ replayed: true }));
402 const name = 'COMPOSIO_SEARCH_TOOLS';
403 const wrapped = new EveProvider({
404 needsApproval: requireApprovalForTools(name),
405 }).wrapTools([tool(name)], execute)[name];
406 const owner = {
407 sessionId: 'eve-session',
408 scope: 'session' as const,
409 resolverSlug: 'composio',
410 entryKey: name,
411 name,
412 };
413
414 const callbacks = validateDurableDynamicToolCallbacks(name, wrapped, owner);
415 expect(callbacks).toEqual({
416 execute: { closure: { slug: name, binding: expect.any(String) } },
417 approvalRequest: { closure: { slug: name, binding: expect.any(String) } },
418 });
419
420 const [replayed] = replayDynamicTools(
421 [
422 {
423 callbacks: JSON.parse(JSON.stringify(callbacks)),
424 description: wrapped.description,
425 entryKey: name,
426 inputSchema: wrapped.inputSchema,
427 name,
428 resolverSlug: 'composio',
429 },
430 ],
431 { sessionId: owner.sessionId, scope: owner.scope }
432 );
433 await expect(replayed.approval(approvalContext(name, { q: 'hi' }))).resolves.toBe(true);
434
435 // The harness execute wrapper needs a live eve context, so call the registry
436 // the way replayDynamicTools does: the registered callback plus the JSON closure.
437 const registered = lookupDurableDynamicCallback(owner, 'execute');
438 const persistedClosure = JSON.parse(JSON.stringify(callbacks.execute.closure));
439 await expect(
440 callDurableDynamicCallback(registered, persistedClosure, { q: 'hi' }, {})
441 ).resolves.toEqual(ok({ replayed: true }));
442 expect(execute).toHaveBeenCalledWith(name, { q: 'hi' });
443 });
444 });
445
446 describe('defineComposioTools', () => {
447 const resolve = (dynamic: ReturnType<typeof defineComposioTools>) =>
448 dynamic.events['step.started'] as NonNullable<(typeof dynamic.events)['step.started']>;
449
450 it('deduplicates concurrent discovery calls', async () => {
451 let release: ((tools: Record<string, never>) => void) | undefined;
452 const pending = new Promise<Record<string, never>>(done => {
453 release = done;
454 });
455 const tools = vi.fn(() => pending);
456 const handler = resolve(defineComposioTools({ tools }));
457
458 const first = handler({}, {} as never);
459 const second = handler({}, {} as never);
460 release?.({});
461
462 await expect(Promise.all([first, second])).resolves.toEqual([{}, {}]);
463 expect(tools).toHaveBeenCalledTimes(1);
464 });
465
466 it('retries discovery after a transient failure', async () => {
467 const tools = vi
468 .fn<() => Promise<Record<string, never>>>()
469 .mockRejectedValueOnce(new Error('temporary failure'))
470 .mockResolvedValueOnce({});
471 const handler = resolve(defineComposioTools({ tools }));
472
473 await expect(handler({}, {} as never)).rejects.toThrow('temporary failure');
474 await expect(handler({}, {} as never)).resolves.toEqual({});
475 expect(tools).toHaveBeenCalledTimes(2);
476 });
477
478 it('caches independently for sessions returned by a resolver', async () => {
479 const first = { tools: vi.fn(async () => ({})) };
480 const second = { tools: vi.fn(async () => ({})) };
481 const handler = resolve(
482 defineComposioTools(context => (context.session.id === 'first' ? first : second))
483 );
484
485 await handler({}, { session: { id: 'first' } } as never);
486 await handler({}, { session: { id: 'second' } } as never);
487 await handler({}, { session: { id: 'first' } } as never);
488
489 expect(first.tools).toHaveBeenCalledTimes(1);
490 expect(second.tools).toHaveBeenCalledTimes(1);
491 });
492 });