275
);
276
});
277
278
- it('should provide a working execute() on SessionContext', async () => {
279
- // Tool that calls another tool via session.execute()
278
+ it('should provide a working execute() on SessionContext for remote tools', async () => {
279
+ // Tool that calls a remote tool via session.execute()
280
const chainedExecute = vi.fn().mockImplementation(async (input: any, session: any) => {
281
const inner = await session.execute('GMAIL_SEND_EMAIL', { to: input.to });
282
return { inner_result: inner.data };
299
arguments: { to: 'test@test.com' },
300
});
301
});
302
+
303
+ it('should route sibling local tools in-process without hitting the API', async () => {
304
+ // Tool B — a sibling local tool
305
+ const siblingExecute = vi.fn().mockResolvedValue({ siblingData: 'from-B' });
306
+ const toolB = createCustomTool({
307
+ slug: 'TOOL_B',
308
+ name: 'Tool B',
309
+ description: 'Sibling tool',
310
+ inputParams: z.object({ key: z.string() }),
311
+ execute: siblingExecute,
312
+ });
313
+
314
+ // Tool A — calls Tool B via session.execute()
315
+ const toolAExecute = vi.fn().mockImplementation(async (input: any, session: any) => {
316
+ const inner = await session.execute('TOOL_B', { key: input.value });
317
+ return { fromA: true, fromB: inner.data };
318
+ });
319
+ const toolA = createCustomTool({
320
+ slug: 'TOOL_A',
321
+ name: 'Tool A',
322
+ description: 'Calls sibling tool B',
323
+ inputParams: z.object({ value: z.string() }),
324
+ execute: toolAExecute,
325
+ });
326
+
327
+ const session = createSession(mockClient, [toolA, toolB]);
328
+ const result = await session.execute('TOOL_A', { value: 'hello' });
329
+
330
+ // Tool B should have been called in-process
331
+ expect(siblingExecute).toHaveBeenCalledWith(
332
+ { key: 'hello' },
333
+ expect.objectContaining({ userId: 'user_1' })
334
+ );
335
+ // Result should contain data from both tools
336
+ expect(result.data).toEqual({
337
+ fromA: true,
338
+ fromB: { siblingData: 'from-B' },
339
+ });
340
+ // Should NOT have called the remote API for Tool B
341
+ expect(mockClient.toolRouter.session.execute).not.toHaveBeenCalled();
342
+ });
343
});
344
345
describe('session.execute() — error handling', () => {
533
return { executeFn, toolsInstance, provider, session };
534
};
535
495
- it('should merge results with remote keys first, local keys last', async () => {
536
+ /** Build a backend-shaped results[] response */
537
+ const backendResponse = (
538
+ results: Array<{ tool_slug: string; data: any; error?: string }>,
539
+ ) => {
540
+ const items = results.map((r, i) => ({
541
+ response: {
542
+ successful: !r.error,
543
+ data: r.data,
544
+ ...(r.error ? { error: r.error } : {}),
545
+ },
546
+ tool_slug: r.tool_slug,
547
+ index: i,
548
+ ...(r.error ? { error: r.error } : {}),
549
+ }));
550
+ const errorCount = results.filter(r => r.error).length;
551
+ return {
552
+ data: {
553
+ results: items,
554
+ total_count: results.length,
555
+ success_count: results.length - errorCount,
556
+ error_count: errorCount,
557
+ },
558
+ error: errorCount > 0 ? `${errorCount} out of ${results.length} tools failed` : null,
559
+ successful: errorCount === 0,
560
+ };
561
+ };
562
+
563
+ /** Find a local tool result in the results[] array by slug */
564
+ const findResult = (results: any[], slug: string) =>
565
+ results.find((r: any) => r.tool_slug === slug);
566
+
567
+ it('should append local results to remote results array', async () => {
568
const { executeFn, toolsInstance } = await setupMultiExecute(mockClient, [customToolHandle]);
569
498
- toolsInstance.executeMetaTool.mockResolvedValueOnce({
499
- data: { GMAIL_SEND_EMAIL: { sent: true } },
500
- error: null,
501
- successful: true,
502
- });
570
+ toolsInstance.executeMetaTool.mockResolvedValueOnce(
571
+ backendResponse([{ tool_slug: 'GMAIL_SEND_EMAIL', data: { sent: true } }])
572
+ );
573
574
const result = await executeFn('COMPOSIO_MULTI_EXECUTE_TOOL', {
575
tools: [
579
sync_response_to_workbench: false,
580
});
581
512
- // Verify ordering: remote keys come before local keys
513
- const keys = Object.keys(result.data);
514
- const remoteIdx = keys.indexOf('GMAIL_SEND_EMAIL');
515
- const localIdx = keys.indexOf('LOCAL_GET_USER_CONTEXT');
516
- expect(remoteIdx).toBeLessThan(localIdx);
582
+ // Results is an array with both remote and local entries
583
+ const { results } = result.data;
584
+ expect(results).toHaveLength(2);
585
+
586
+ // Remote result comes first (from backend), local appended after
587
+ const remote = findResult(results, 'GMAIL_SEND_EMAIL');
588
+ const local = findResult(results, 'LOCAL_GET_USER_CONTEXT');
589
+ expect(remote.response.data).toEqual({ sent: true });
590
+ expect(local.response.data).toEqual({ local_result: true });
591
+ expect(local.response.successful).toBe(true);
592
});
593
594
it('should handle multiple local tools in same batch', async () => {
610
{ fields: 'id,name' },
611
expect.objectContaining({ userId: 'user_1' })
612
);
538
- expect(result.data).toHaveProperty('LOCAL_GET_USER_CONTEXT');
539
- expect(result.data).toHaveProperty('LOCAL_GET_AD_ACCOUNTS');
613
+
614
+ const { results } = result.data;
615
+ expect(results).toHaveLength(2);
616
+ expect(findResult(results, 'LOCAL_GET_USER_CONTEXT')).toBeDefined();
617
+ expect(findResult(results, 'LOCAL_GET_AD_ACCOUNTS')).toBeDefined();
618
});
619
620
it('should handle mixed batch with multiple locals + multiple remotes', async () => {
623
[customToolHandle, sessionToolHandle]
624
);
625
548
- toolsInstance.executeMetaTool.mockResolvedValueOnce({
549
- data: {
550
- GMAIL_SEND_EMAIL: { sent: true },
551
- SLACK_POST_MESSAGE: { ts: '999' },
552
- },
553
- error: null,
554
- successful: true,
555
- });
626
+ toolsInstance.executeMetaTool.mockResolvedValueOnce(
627
+ backendResponse([
628
+ { tool_slug: 'GMAIL_SEND_EMAIL', data: { sent: true } },
629
+ { tool_slug: 'SLACK_POST_MESSAGE', data: { ts: '999' } },
630
+ ])
631
+ );
632
633
const result = await executeFn('COMPOSIO_MULTI_EXECUTE_TOOL', {
634
tools: [
640
sync_response_to_workbench: false,
641
});
642
567
- // All 4 tools should appear in result
568
- expect(Object.keys(result.data)).toHaveLength(4);
569
- expect(result.data).toHaveProperty('LOCAL_GET_USER_CONTEXT');
570
- expect(result.data).toHaveProperty('LOCAL_GET_AD_ACCOUNTS');
571
- expect(result.data).toHaveProperty('GMAIL_SEND_EMAIL');
572
- expect(result.data).toHaveProperty('SLACK_POST_MESSAGE');
643
+ // All 4 tools should appear in results array
644
+ const { results } = result.data;
645
+ expect(results).toHaveLength(4);
646
+ expect(findResult(results, 'LOCAL_GET_USER_CONTEXT')).toBeDefined();
647
+ expect(findResult(results, 'LOCAL_GET_AD_ACCOUNTS')).toBeDefined();
648
+ expect(findResult(results, 'GMAIL_SEND_EMAIL')).toBeDefined();
649
+ expect(findResult(results, 'SLACK_POST_MESSAGE')).toBeDefined();
650
651
// Backend only got the 2 remote tools
652
const backendTools = toolsInstance.executeMetaTool.mock.calls[0][1].arguments.tools;
659
// Both local execute fns were called
660
expect(localExecute).toHaveBeenCalled();
661
expect(sessionExecute).toHaveBeenCalled();
662
+ expect(result.successful).toBe(true);
663
});
664
587
- it('should still succeed when one local tool errors in a mixed batch', async () => {
665
+ it('should surface errors from both local and remote tools', async () => {
666
const throwingHandle = createCustomTool({
667
slug: 'MIXED_THROWER',
668
name: 'Throws in batch',
676
[customToolHandle, throwingHandle]
677
);
678
601
- toolsInstance.executeMetaTool.mockResolvedValueOnce({
602
- data: { GMAIL_SEND_EMAIL: { sent: true } },
603
- error: null,
604
- successful: true,
605
- });
679
+ // Remote also returns an error for one tool
680
+ toolsInstance.executeMetaTool.mockResolvedValueOnce(
681
+ backendResponse([
682
+ { tool_slug: 'GMAIL_SEND_EMAIL', data: { sent: true } },
683
+ { tool_slug: 'SLACK_POST_MESSAGE', data: { message: 'auth failed' }, error: 'auth failed' },
684
+ ])
685
+ );
686
687
const result = await executeFn('COMPOSIO_MULTI_EXECUTE_TOOL', {
688
tools: [
689
{ tool_slug: 'LOCAL_GET_USER_CONTEXT', arguments: { category: 'ok' } },
690
{ tool_slug: 'LOCAL_MIXED_THROWER', arguments: {} },
691
{ tool_slug: 'GMAIL_SEND_EMAIL', arguments: { to: 'a@b.com' } },
692
+ { tool_slug: 'SLACK_POST_MESSAGE', arguments: { channel: '#dev' } },
693
],
694
sync_response_to_workbench: false,
695
});
696
616
- // Successful local tool still has its data
617
- expect(result.data).toHaveProperty('LOCAL_GET_USER_CONTEXT');
618
- // Throwing tool has empty data
619
- expect(result.data.LOCAL_MIXED_THROWER).toEqual({});
620
- // Remote tool still ran
621
- expect(result.data).toHaveProperty('GMAIL_SEND_EMAIL');
622
- // Error is surfaced
623
- expect(result.error).toBe('batch-boom');
697
+ const { results } = result.data;
698
+ expect(results).toHaveLength(4);
699
+
700
+ // Local success
701
+ const localOk = findResult(results, 'LOCAL_GET_USER_CONTEXT');
702
+ expect(localOk.response.successful).toBe(true);
703
+
704
+ // Local error — data is empty, error is surfaced
705
+ const localErr = findResult(results, 'LOCAL_MIXED_THROWER');
706
+ expect(localErr.response.successful).toBe(false);
707
+ expect(localErr.response.error).toBe('batch-boom');
708
+ expect(localErr.error).toBe('batch-boom');
709
+
710
+ // Remote error is also preserved (not dropped!)
711
+ const remoteErr = findResult(results, 'SLACK_POST_MESSAGE');
712
+ expect(remoteErr.response.error).toBe('auth failed');
713
+
714
+ // Top-level error reflects total failure count
715
expect(result.successful).toBe(false);
716
+ expect(result.error).toContain('2 out of 4');
717
});
718
719
it('should forward to backend when tools array is empty', async () => {
738
it('should handle non-object items in tools array gracefully', async () => {
739
const { executeFn, toolsInstance } = await setupMultiExecute(mockClient, [customToolHandle]);
740
649
- toolsInstance.executeMetaTool.mockResolvedValueOnce({
650
- data: {},
651
- error: null,
652
- successful: true,
653
- });
741
+ toolsInstance.executeMetaTool.mockResolvedValueOnce(
742
+ backendResponse([])
743
+ );
744
745
// Malformed: string instead of object — should not crash
746
const result = await executeFn('COMPOSIO_MULTI_EXECUTE_TOOL', {
753
{ category: 'ok' },
754
expect.objectContaining({ userId: 'user_1' })
755
);
666
- expect(result.data).toHaveProperty('LOCAL_GET_USER_CONTEXT');
756
+ const { results } = result.data;
757
+ expect(findResult(results, 'LOCAL_GET_USER_CONTEXT')).toBeDefined();
758
});
759
760
it('should execute local and remote in parallel (not sequentially)', async () => {
787
callOrder.push('remote-start');
788
await new Promise(r => setTimeout(r, 50));
789
callOrder.push('remote-end');
699
- return { data: { GMAIL_SEND_EMAIL: { sent: true } }, error: null, successful: true };
790
+ return backendResponse([{ tool_slug: 'GMAIL_SEND_EMAIL', data: { sent: true } }]);
791
});
792
793
await executeFn('COMPOSIO_MULTI_EXECUTE_TOOL', {
832
});
833
});
834
744
- describe('remote batch duplication fix — each remote tool gets its own result', () => {
745
- it('should return per-tool keyed results when multiple remote tools are in a mixed batch', async () => {
835
+ describe('per-tool results — each tool gets its own entry in results[]', () => {
836
+ it('should return per-tool results for mixed local+remote batch', async () => {
837
const provider = new MockProvider();
838
captureExecuteFn(provider);
839
const session = createSessionWithProvider(mockClient, provider, [customToolHandle]);
840
750
- // Mock executeMetaTool to return data keyed by slug (like real backend)
751
- const toolsInstance = (Tools as any).mock.results[
752
- (Tools as any).mock.results.length - 1
753
- ]?.value;
754
-
841
await session.tools();
842
757
- // Re-mock executeMetaTool after session.tools() created the Tools instance
843
const latestToolsInstance = (Tools as any).mock.results[
844
(Tools as any).mock.results.length - 1
845
].value;
846
latestToolsInstance.executeMetaTool.mockResolvedValueOnce({
847
data: {
763
- GMAIL_SEND_EMAIL: { message_id: 'msg_1' },
764
- SLACK_POST_MESSAGE: { ts: '123456' },
848
+ results: [
849
+ { response: { successful: true, data: { message_id: 'msg_1' } }, tool_slug: 'GMAIL_SEND_EMAIL', index: 0 },
850
+ { response: { successful: true, data: { ts: '123456' } }, tool_slug: 'SLACK_POST_MESSAGE', index: 1 },
851
+ ],
852
+ total_count: 2,
853
+ success_count: 2,
854
+ error_count: 0,
855
},
856
error: null,
857
successful: true,
868
sync_response_to_workbench: false,
869
});
870
781
- // Local tool result keyed by its slug
782
- expect(result.data).toHaveProperty('LOCAL_GET_USER_CONTEXT');
783
- expect(result.data.LOCAL_GET_USER_CONTEXT).toEqual({ local_result: true });
871
+ const { results } = result.data;
872
+ expect(results).toHaveLength(3);
873
785
- // Each remote tool gets its own keyed result, not the entire batch blob
786
- expect(result.data).toHaveProperty('GMAIL_SEND_EMAIL');
787
- expect(result.data.GMAIL_SEND_EMAIL).toEqual({ message_id: 'msg_1' });
874
+ // Each tool has its own entry
875
+ const local = results.find((r: any) => r.tool_slug === 'LOCAL_GET_USER_CONTEXT');
876
+ const gmail = results.find((r: any) => r.tool_slug === 'GMAIL_SEND_EMAIL');
877
+ const slack = results.find((r: any) => r.tool_slug === 'SLACK_POST_MESSAGE');
878
789
- expect(result.data).toHaveProperty('SLACK_POST_MESSAGE');
790
- expect(result.data.SLACK_POST_MESSAGE).toEqual({ ts: '123456' });
879
+ expect(local.response.data).toEqual({ local_result: true });
880
+ expect(gmail.response.data).toEqual({ message_id: 'msg_1' });
881
+ expect(slack.response.data).toEqual({ ts: '123456' });
882
});
883
});
884