Open History to see the last commit
1import { describe, expect, it } from 'vitest'
2import {
3 executeHostTool,
4 remoteToolStubs,
5 toolDescriptors,
6} from '../src/remote-tools'
7import type { RemoteToolExecutor } from '../src/remote-tools'
8import type { AnyTool } from '@tanstack/ai'
9
10const getTime: AnyTool = {
11 name: 'getTime',
12 description: 'returns the time',
13 inputSchema: { type: 'object', properties: { tz: { type: 'string' } } },
14 execute: (args) => Promise.resolve(`now in ${(args as { tz: string }).tz}`),
15}
16
17describe('remote-tools (co-located host-tool delegation)', () => {
18 it('toolDescriptors serializes name/description/json-schema', () => {
19 const [d] = toolDescriptors([getTime])
20 expect(d).toEqual({
21 name: 'getTime',
22 description: 'returns the time',
23 inputSchema: { type: 'object', properties: { tz: { type: 'string' } } },
24 })
25 })
26
27 it('falls back to an empty object schema for a non-JSON-schema input', () => {
28 const zodish: AnyTool = {
29 name: 'x',
30 description: 'd',
31 // a Standard Schema (object) rather than a JSON-schema object
32 inputSchema: { '~standard': { version: 1 } },
33 execute: () => Promise.resolve(1),
34 }
35 expect(toolDescriptors([zodish])[0]!.inputSchema).toEqual({
36 type: 'object',
37 properties: {},
38 })
39 })
40
41 it('remoteToolStubs builds tools whose execute delegates to the executor', async () => {
42 const calls: Array<{ name: string; args: unknown }> = []
43 const executor: RemoteToolExecutor = {
44 execute: (name, args) => {
45 calls.push({ name, args })
46 return Promise.resolve(`remote:${name}`)
47 },
48 }
49 const [stub] = remoteToolStubs(toolDescriptors([getTime]), executor)
50 expect(stub!.name).toBe('getTime')
51 expect(stub!.inputSchema).toEqual(getTime.inputSchema)
52 const result = await stub!.execute!({ tz: 'utc' }, {})
53 expect(result).toBe('remote:getTime')
54 expect(calls).toEqual([{ name: 'getTime', args: { tz: 'utc' } }])
55 })
56
57 it('round-trips: a stub whose executor runs executeHostTool calls the REAL tool', async () => {
58 // Simulates the container↔orchestrator loop without HTTP: the executor
59 // (would be httpRemoteToolExecutor over the wire) runs the host tool.
60 const executor: RemoteToolExecutor = {
61 execute: (name, args) =>
62 executeHostTool([getTime], name, args, { threadId: 'thread-1' }),
63 }
64 const [stub] = remoteToolStubs(toolDescriptors([getTime]), executor)
65 expect(await stub!.execute!({ tz: 'mars' }, {})).toBe('now in mars')
66 })
67
68 it('executeHostTool throws for an unknown tool', async () => {
69 await expect(
70 executeHostTool([getTime], 'nope', {}, { threadId: 'thread-1' }),
71 ).rejects.toThrow(/Unknown tool/)
72 })
73
74 it('executeHostTool refuses a tool with needsApproval', async () => {
75 let ran = false
76 const gated: AnyTool = {
77 ...getTime,
78 needsApproval: true,
79 execute: () => {
80 ran = true
81 return Promise.resolve('ran')
82 },
83 }
84 await expect(
85 executeHostTool([gated], 'getTime', {}, { threadId: 'thread-1' }),
86 ).rejects.toThrow(/needsApproval/)
87 expect(ran).toBe(false)
88 })
89
90 it('executeHostTool forwards the thread id and context to the real tool', async () => {
91 let seenCtx: unknown
92 const tool: AnyTool = {
93 name: 't',
94 description: 'd',
95 execute: (_args, ctx) => {
96 seenCtx = ctx
97 return Promise.resolve('ok')
98 },
99 }
100 await executeHostTool(
101 [tool],
102 't',
103 {},
104 { threadId: 'thread-1', context: { userId: 'u1' } },
105 )
106 expect(seenCtx).toMatchObject({
107 threadId: 'thread-1',
108 context: { userId: 'u1' },
109 })
110 })
111})