Open History to see the last commit
1import { describe, expect, it } from 'vitest'
2import {
3 applyPatchTool,
4 codeInterpreterTool,
5 computerUseTool,
6 customTool,
7 fileSearchTool,
8 imageGenerationTool,
9 localShellTool,
10 mcpTool,
11 shellTool,
12 webSearchPreviewTool,
13 webSearchTool,
14} from '../src/tools'
15import { convertToolsToProviderFormat } from '../src/tools/tool-converter'
16import { DuplicateToolNameError } from '@tanstack/ai'
17import type { Tool } from '@tanstack/ai'
18
19function isRoundTrippedTool(value: unknown): value is Tool {
20 return (
21 typeof value === 'object' &&
22 value !== null &&
23 'name' in value &&
24 typeof value.name === 'string' &&
25 'description' in value &&
26 typeof value.description === 'string'
27 )
28}
29
30const PROVIDER_TOOL_NAMES = [
31 'apply_patch',
32 'code_interpreter',
33 'computer',
34 'computer_use_preview',
35 'custom',
36 'file_search',
37 'image_generation',
38 'local_shell',
39 'mcp',
40 'shell',
41 'web_search_preview',
42 'web_search',
43] as const
44
45const FACTORY_CASES = [
46 {
47 name: 'apply_patch',
48 kind: 'openai.apply_patch',
49 tool: applyPatchTool(),
50 wire: { type: 'apply_patch' },
51 },
52 {
53 name: 'code_interpreter',
54 kind: 'openai.code_interpreter',
55 tool: codeInterpreterTool({
56 type: 'code_interpreter',
57 container: { type: 'auto' },
58 }),
59 wire: { type: 'code_interpreter' },
60 },
61 {
62 name: 'computer',
63 kind: 'openai.computer_use',
64 tool: computerUseTool({ type: 'computer' }),
65 wire: { type: 'computer' },
66 },
67 {
68 name: 'computer_use_preview',
69 kind: 'openai.computer_use',
70 tool: computerUseTool({
71 type: 'computer_use_preview',
72 display_height: 768,
73 display_width: 1024,
74 environment: 'linux',
75 }),
76 wire: {
77 type: 'computer_use_preview',
78 display_height: 768,
79 display_width: 1024,
80 environment: 'linux',
81 },
82 },
83 {
84 name: 'custom',
85 kind: 'openai.custom',
86 tool: customTool({
87 type: 'custom',
88 name: 'lookup_order',
89 description: 'Look up an order',
90 }),
91 wire: {
92 type: 'custom',
93 name: 'lookup_order',
94 description: 'Look up an order',
95 },
96 },
97 {
98 name: 'file_search',
99 kind: 'openai.file_search',
100 tool: fileSearchTool({
101 type: 'file_search',
102 vector_store_ids: ['vs_123'],
103 }),
104 wire: { type: 'file_search', vector_store_ids: ['vs_123'] },
105 },
106 {
107 name: 'image_generation',
108 kind: 'openai.image_generation',
109 tool: imageGenerationTool({}),
110 wire: { type: 'image_generation' },
111 },
112 {
113 name: 'local_shell',
114 kind: 'openai.local_shell',
115 tool: localShellTool(),
116 wire: { type: 'local_shell' },
117 },
118 {
119 name: 'mcp',
120 kind: 'openai.mcp',
121 tool: mcpTool({
122 server_label: 'my-server',
123 server_url: 'https://example.com/mcp',
124 }),
125 wire: {
126 type: 'mcp',
127 server_label: 'my-server',
128 server_url: 'https://example.com/mcp',
129 },
130 },
131 {
132 name: 'shell',
133 kind: 'openai.shell',
134 tool: shellTool(),
135 wire: { type: 'shell' },
136 },
137 {
138 name: 'web_search_preview',
139 kind: 'openai.web_search_preview',
140 tool: webSearchPreviewTool({ type: 'web_search_preview' }),
141 wire: { type: 'web_search_preview' },
142 },
143 {
144 name: 'web_search',
145 kind: 'openai.web_search',
146 tool: webSearchTool({ type: 'web_search' }),
147 wire: { type: 'web_search' },
148 },
149] as const
150
151describe('OpenAI provider tool dispatch', () => {
152 it.each(PROVIDER_TOOL_NAMES)(
153 'keeps an ordinary function named %s as a function tool',
154 (name) => {
155 const [converted] = convertToolsToProviderFormat([
156 {
157 name,
158 description: 'Run an application function',
159 inputSchema: {
160 type: 'object',
161 properties: { query: { type: 'string' } },
162 required: ['query'],
163 },
164 } satisfies Tool,
165 ])
166
167 expect(converted).toMatchObject({ type: 'function', name })
168 },
169 )
170
171 it.each(FACTORY_CASES)(
172 'converts $name to the native wire shape',
173 ({ kind, tool, wire }) => {
174 expect(tool.metadata).toMatchObject({ __kind: kind })
175 const [converted] = convertToolsToProviderFormat([tool])
176 expect(converted).toMatchObject(wire)
177 expect(JSON.stringify(converted)).not.toContain('__kind')
178 },
179 )
180
181 it('throws when a factory tool and a custom tool share a name', () => {
182 expect(() =>
183 convertToolsToProviderFormat([
184 webSearchTool({ type: 'web_search' }),
185 {
186 name: 'web_search',
187 description: 'Search application data',
188 inputSchema: {
189 type: 'object',
190 properties: { query: { type: 'string' } },
191 required: ['query'],
192 },
193 } satisfies Tool,
194 ]),
195 ).toThrow(DuplicateToolNameError)
196 })
197
198 it('keeps provider identity through a plain-data round trip', () => {
199 const parsed: unknown = JSON.parse(
200 JSON.stringify(webSearchTool({ type: 'web_search' })),
201 )
202 if (!isRoundTrippedTool(parsed)) {
203 throw new Error('round-tripped tool is not a Tool-shaped object')
204 }
205
206 expect(convertToolsToProviderFormat([parsed])).toEqual([
207 { type: 'web_search' },
208 ])
209 })
210})