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) => executeHostTool([getTime], name, args),
62 }
63 const [stub] = remoteToolStubs(toolDescriptors([getTime]), executor)
64 expect(await stub!.execute!({ tz: 'mars' }, {})).toBe('now in mars')
65 })
66
67 it('executeHostTool throws for an unknown tool', async () => {
68 await expect(executeHostTool([getTime], 'nope', {})).rejects.toThrow(
69 /Unknown tool/,
70 )
71 })
72
73 it('executeHostTool forwards context to the real tool', async () => {
74 let seenCtx: unknown
75 const tool: AnyTool = {
76 name: 't',
77 description: 'd',
78 execute: (_args, ctx) => {
79 seenCtx = (ctx as { context?: unknown }).context
80 return Promise.resolve('ok')
81 },
82 }
83 await executeHostTool([tool], 't', {}, { context: { userId: 'u1' } })
84 expect(seenCtx).toEqual({ userId: 'u1' })
85 })
86})