Open History to see the last commit
1import { describe, expect, it } from 'vitest'
2import {
3 codeExecutionTool,
4 computerUseTool,
5 fileSearchTool,
6 googleMapsTool,
7 googleSearchRetrievalTool,
8 googleSearchTool,
9 urlContextTool,
10} from '../src/tools'
11import { convertToolsToProviderFormat } from '../src/tools/tool-converter'
12import { DuplicateToolNameError } from '@tanstack/ai'
13import type { Tool } from '@tanstack/ai'
14
15function isRoundTrippedTool(value: unknown): value is Tool {
16 return (
17 typeof value === 'object' &&
18 value !== null &&
19 'name' in value &&
20 typeof value.name === 'string' &&
21 'description' in value &&
22 typeof value.description === 'string'
23 )
24}
25
26const PROVIDER_TOOL_NAMES = [
27 'code_execution',
28 'computer_use',
29 'file_search',
30 'google_maps',
31 'google_search',
32 'google_search_retrieval',
33 'url_context',
34] as const
35
36const FACTORY_CASES = [
37 {
38 name: 'code_execution',
39 kind: 'gemini.code_execution',
40 tool: codeExecutionTool(),
41 wire: { codeExecution: {} },
42 },
43 {
44 name: 'computer_use',
45 kind: 'gemini.computer_use',
46 tool: computerUseTool({}),
47 wire: { computerUse: {} },
48 },
49 {
50 name: 'file_search',
51 kind: 'gemini.file_search',
52 tool: fileSearchTool({ fileSearchStoreNames: [] }),
53 wire: { fileSearch: { fileSearchStoreNames: [] } },
54 },
55 {
56 name: 'google_maps',
57 kind: 'gemini.google_maps',
58 tool: googleMapsTool(),
59 wire: { googleMaps: {} },
60 },
61 {
62 name: 'google_search',
63 kind: 'gemini.google_search',
64 tool: googleSearchTool(),
65 wire: { googleSearch: {} },
66 },
67 {
68 name: 'google_search_retrieval',
69 kind: 'gemini.google_search_retrieval',
70 tool: googleSearchRetrievalTool(),
71 wire: { googleSearchRetrieval: {} },
72 },
73 {
74 name: 'url_context',
75 kind: 'gemini.url_context',
76 tool: urlContextTool(),
77 wire: { urlContext: {} },
78 },
79] as const
80
81describe('Gemini provider tool dispatch', () => {
82 it.each(PROVIDER_TOOL_NAMES)(
83 'keeps an ordinary function named %s as a function declaration',
84 (name) => {
85 const inputSchema = {
86 type: 'object',
87 properties: {
88 query: { type: 'string' },
89 unit: { const: 'celsius' },
90 },
91 required: ['query', 'unit'],
92 }
93 const [converted] = convertToolsToProviderFormat([
94 {
95 name,
96 description: 'Run an application function',
97 inputSchema,
98 } satisfies Tool,
99 ])
100
101 expect(converted).toEqual({
102 functionDeclarations: [
103 {
104 name,
105 description: 'Run an application function',
106 parametersJsonSchema: inputSchema,
107 },
108 ],
109 })
110 },
111 )
112
113 it.each(FACTORY_CASES)(
114 'converts $name to the native wire shape',
115 ({ kind, tool, wire }) => {
116 expect(tool.metadata).toMatchObject({ __kind: kind })
117 const [converted] = convertToolsToProviderFormat([tool])
118 expect(converted).toEqual(wire)
119 expect(JSON.stringify(converted)).not.toContain('__kind')
120 },
121 )
122
123 it('throws when a factory tool and a custom tool share a name', () => {
124 expect(() =>
125 convertToolsToProviderFormat([
126 googleSearchTool(),
127 {
128 name: 'google_search',
129 description: 'Search application data',
130 inputSchema: {
131 type: 'object',
132 properties: { query: { type: 'string' } },
133 required: ['query'],
134 },
135 } satisfies Tool,
136 ]),
137 ).toThrow(DuplicateToolNameError)
138 })
139
140 it('keeps provider identity through a plain-data round trip', () => {
141 const parsed: unknown = JSON.parse(JSON.stringify(googleSearchTool()))
142 if (!isRoundTrippedTool(parsed)) {
143 throw new Error('round-tripped tool is not a Tool-shaped object')
144 }
145
146 expect(convertToolsToProviderFormat([parsed])).toEqual([
147 { googleSearch: {} },
148 ])
149 })
150})