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