596
});
597
598
describe('sibling local tool routing', () => {
599
- it('should route to local tool when tryLocalExecute matches', async () => {
600
- const tryLocalExecute = vi.fn().mockReturnValue(
601
- Promise.resolve({ data: { local: true }, error: null, successful: true })
602
- );
603
-
604
- const ctx = new SessionContextImpl(mockClient as any, 'user_1', 'sess_1', tryLocalExecute);
599
+ it('should route to local tool when customToolsMap has a match', async () => {
600
+ const siblingExecute = vi.fn().mockResolvedValue({ local: true });
601
+ const customToolsMap = buildCustomToolsMap([
602
+ createCustomTool('SIBLING_TOOL', {
603
+ name: 'Sibling',
604
+ description: 'A sibling tool',
605
+ inputParams: z.object({ key: z.string() }),
606
+ execute: siblingExecute,
607
+ }),
608
+ ]);
609
+
610
+ const ctx = new SessionContextImpl(mockClient as any, 'user_1', 'sess_1', customToolsMap);
611
const result = await ctx.execute('SIBLING_TOOL', { key: 'val' });
612
607
- expect(tryLocalExecute).toHaveBeenCalledWith('SIBLING_TOOL', { key: 'val' });
613
+ expect(siblingExecute).toHaveBeenCalledWith({ key: 'val' }, ctx);
614
expect(result).toEqual({ data: { local: true }, error: null, successful: true });
615
+ // Should NOT call remote
616
expect(mockClient.toolRouter.session.execute).not.toHaveBeenCalled();
617
});
618
612
- it('should fall back to remote when tryLocalExecute returns undefined', async () => {
613
- const tryLocalExecute = vi.fn().mockReturnValue(undefined);
619
+ it('should fall back to remote when slug not in customToolsMap', async () => {
620
+ const customToolsMap = buildCustomToolsMap([
621
+ createCustomTool('OTHER_TOOL', {
622
+ name: 'Other',
623
+ description: 'Not the one we call',
624
+ inputParams: z.object({}),
625
+ execute: vi.fn().mockResolvedValue({}),
626
+ }),
627
+ ]);
628
mockClient.toolRouter.session.execute.mockResolvedValue({
629
data: { remote: true },
630
error: null,
631
log_id: 'log_3',
632
});
633
620
- const ctx = new SessionContextImpl(mockClient as any, 'user_1', 'sess_1', tryLocalExecute);
634
+ const ctx = new SessionContextImpl(mockClient as any, 'user_1', 'sess_1', customToolsMap);
635
const result = await ctx.execute('REMOTE_TOOL', { key: 'val' });
636
623
- expect(tryLocalExecute).toHaveBeenCalledWith('REMOTE_TOOL', { key: 'val' });
637
expect(mockClient.toolRouter.session.execute).toHaveBeenCalledWith('sess_1', {
638
tool_slug: 'REMOTE_TOOL',
639
arguments: { key: 'val' },
641
expect(result).toEqual({ data: { remote: true }, error: null, successful: true });
642
});
643
631
- it('should delegate to remote when no tryLocalExecute is provided', async () => {
644
+ it('should delegate to remote when no customToolsMap is provided', async () => {
645
mockClient.toolRouter.session.execute.mockResolvedValue({
646
data: { remote: true },
647
error: null,