62
},
63
});
64
65
-const localExecute = vi.fn().mockResolvedValue({
66
- data: { local_result: true },
67
- error: null,
68
- successful: true,
69
-});
65
+// Execute returns data directly (simplified API)
66
+const localExecute = vi.fn().mockResolvedValue({ local_result: true });
67
68
const customToolHandle = CustomTool({
69
slug: 'GET_USER_CONTEXT',
74
});
75
76
const sessionExecute = vi.fn().mockImplementation(async (input: any, session: any) => ({
80
- data: { userId: session.userId },
81
- error: null,
82
- successful: true,
77
+ userId: session.userId,
78
}));
79
80
const sessionToolHandle = CustomTool({
81
slug: 'GET_AD_ACCOUNTS',
82
name: 'Get ad accounts',
83
description: 'Get ad account IDs',
89
- toolkit: 'meta_ads',
84
+ connectedToolkit: 'meta_ads',
85
inputParams: z.object({ fields: z.string() }),
86
execute: sessionExecute,
87
});
120
]);
121
});
122
128
- it('should include toolkit in local_tools when present', async () => {
123
+ it('should include toolkit in local_tools when connectedToolkit is present', async () => {
124
await router.create('user_1', {
125
customTools: [sessionToolHandle],
126
});
189
190
const result = await session.execute('GET_USER_CONTEXT', { category: 'prefs' });
191
192
+ // User returns data directly, SDK wraps it
193
expect(result.data).toEqual({ local_result: true });
194
expect(result.logId).toBe('local');
195
expect(localExecute).toHaveBeenCalledWith(
259
// Tool that calls another tool via session.execute()
260
const chainedExecute = vi.fn().mockImplementation(async (input: any, session: any) => {
261
const inner = await session.execute('GMAIL_SEND_EMAIL', { to: input.to });
266
- return { data: { inner_result: inner.data }, error: null, successful: true };
262
+ return { inner_result: inner.data };
263
});
264
265
const chainedTool = CustomTool({
302
303
it('should apply Zod defaults when input is missing optional fields', async () => {
304
const defaultExecute = vi.fn().mockImplementation(async (input: any) => ({
309
- data: { category: input.category },
310
- error: null,
311
- successful: true,
305
+ category: input.category,
306
}));
307
308
const toolWithDefaults = CustomTool({
358
});
359
});
360
367
- describe('session.tools() — COMPOSIO_MULTI_EXECUTE_TOOL routing', () => {
368
- it('should route local tools via COMPOSIO_MULTI_EXECUTE_TOOL to in-process execution', async () => {
369
- const provider = new MockProvider();
370
- // Mock wrapTools to capture the execute function and call it
371
- provider.wrapTools.mockImplementation((tools: any, executeFn: any) => {
372
- // Store executeFn so we can call it
373
- (provider as any)._capturedExecuteFn = executeFn;
374
- return 'wrapped-tools-with-routing';
375
- });
376
-
377
- const session = new ToolRouterSession(
378
- mockClient as unknown as ComposioClient,
361
+ describe('session.tools() — COMPOSIO_MULTI_EXECUTE_TOOL routing with tools[] array', () => {
362
+ const createSessionWithProvider = (
363
+ client: ReturnType<typeof createMockClient>,
364
+ provider: MockProvider,
365
+ customTools: CustomToolHandle[]
366
+ ) => {
367
+ return new ToolRouterSession(
368
+ client as unknown as ComposioClient,
369
{ apiKey: 'key', provider },
370
'sess_123',
371
{ type: 'http' as const, url: 'https://mcp.example.com/sess_123' },
372
undefined,
383
- buildLocalToolsMap([customToolHandle]),
373
+ buildLocalToolsMap(customTools),
374
'user_1'
375
);
376
+ };
377
387
- await session.tools();
378
+ const captureExecuteFn = (provider: MockProvider) => {
379
+ provider.wrapTools.mockImplementation((tools: any, executeFn: any) => {
380
+ (provider as any)._capturedExecuteFn = executeFn;
381
+ return 'wrapped-tools-with-routing';
382
+ });
383
+ };
384
389
- // Now simulate the LLM calling COMPOSIO_MULTI_EXECUTE_TOOL with a local tool
385
+ it('should route all-local tools[] to in-process execution', async () => {
386
+ const provider = new MockProvider();
387
+ captureExecuteFn(provider);
388
+ const session = createSessionWithProvider(mockClient, provider, [customToolHandle]);
389
+
390
+ await session.tools();
391
const executeFn = (provider as any)._capturedExecuteFn;
391
- expect(executeFn).toBeDefined();
392
393
const result = await executeFn('COMPOSIO_MULTI_EXECUTE_TOOL', {
394
- tool_slug: 'LOCAL_GET_USER_CONTEXT',
395
- arguments: { category: 'test' },
394
+ tools: [
395
+ { tool_slug: 'LOCAL_GET_USER_CONTEXT', arguments: { category: 'test' } },
396
+ ],
397
+ sync_response_to_workbench: false,
398
});
399
400
expect(result.data).toEqual({ local_result: true });
404
);
405
});
406
405
- it('should route remote tools via COMPOSIO_MULTI_EXECUTE_TOOL to backend', async () => {
407
+ it('should route all-remote tools[] to backend', async () => {
408
const provider = new MockProvider();
407
- provider.wrapTools.mockImplementation((tools: any, executeFn: any) => {
408
- (provider as any)._capturedExecuteFn = executeFn;
409
- return 'wrapped';
410
- });
411
-
412
- const session = new ToolRouterSession(
413
- mockClient as unknown as ComposioClient,
414
- { apiKey: 'key', provider },
415
- 'sess_123',
416
- { type: 'http' as const, url: 'https://mcp.example.com/sess_123' },
417
- undefined,
418
- buildLocalToolsMap([customToolHandle]),
419
- 'user_1'
420
- );
409
+ captureExecuteFn(provider);
410
+ const session = createSessionWithProvider(mockClient, provider, [customToolHandle]);
411
412
await session.tools();
423
-
413
const executeFn = (provider as any)._capturedExecuteFn;
414
+
415
const result = await executeFn('COMPOSIO_MULTI_EXECUTE_TOOL', {
426
- tool_slug: 'GMAIL_SEND_EMAIL',
427
- arguments: { to: 'test@test.com' },
416
+ tools: [
417
+ { tool_slug: 'GMAIL_SEND_EMAIL', arguments: { to: 'test@test.com' } },
418
+ ],
419
+ sync_response_to_workbench: false,
420
});
421
430
- // Should have gone to remote via executeMetaTool
422
+ // All remote — goes to executeMetaTool
423
expect(result.data).toEqual({ remote: true });
424
expect(localExecute).not.toHaveBeenCalled();
425
});
426
435
- it('should route non-MULTI_EXECUTE meta tools to backend', async () => {
427
+ it('should split mixed local+remote tools and merge results', async () => {
428
const provider = new MockProvider();
437
- provider.wrapTools.mockImplementation((tools: any, executeFn: any) => {
438
- (provider as any)._capturedExecuteFn = executeFn;
439
- return 'wrapped';
429
+ captureExecuteFn(provider);
430
+ const session = createSessionWithProvider(mockClient, provider, [customToolHandle]);
431
+
432
+ await session.tools();
433
+ const executeFn = (provider as any)._capturedExecuteFn;
434
+
435
+ const result = await executeFn('COMPOSIO_MULTI_EXECUTE_TOOL', {
436
+ tools: [
437
+ { tool_slug: 'LOCAL_GET_USER_CONTEXT', arguments: { category: 'prefs' } },
438
+ { tool_slug: 'GMAIL_SEND_EMAIL', arguments: { to: 'test@test.com' } },
439
+ ],
440
+ sync_response_to_workbench: false,
441
});
442
442
- const session = new ToolRouterSession(
443
- mockClient as unknown as ComposioClient,
444
- { apiKey: 'key', provider },
445
- 'sess_123',
446
- { type: 'http' as const, url: 'https://mcp.example.com/sess_123' },
447
- undefined,
448
- buildLocalToolsMap([customToolHandle]),
449
- 'user_1'
450
- );
443
+ // Both should have been called
444
+ expect(localExecute).toHaveBeenCalled();
445
+ // Remote goes to backend — merged results keyed by slug
446
+ expect(result.data).toHaveProperty('LOCAL_GET_USER_CONTEXT');
447
+ expect(result.data).toHaveProperty('GMAIL_SEND_EMAIL');
448
+ });
449
452
- await session.tools();
450
+ it('should route non-MULTI_EXECUTE meta tools to backend', async () => {
451
+ const provider = new MockProvider();
452
+ captureExecuteFn(provider);
453
+ const session = createSessionWithProvider(mockClient, provider, [customToolHandle]);
454
455
+ await session.tools();
456
const executeFn = (provider as any)._capturedExecuteFn;
457
+
458
const result = await executeFn('COMPOSIO_SEARCH_TOOLS', {
459
queries: [{ use_case: 'send email' }],
460
});