Open History to see the last commit
1import { afterEach, describe, expect, it, vi } from 'vitest'
2
3import { requestUrl } from '#/lib/text.ts'
4
5import { GROK_FILES_DOC_URL, grokProvider, parseGrokFileTool } from './grok.ts'
6import {
7 OPENAI_MODELS_INDEX_URL,
8 OPENAI_MODEL_PAGE,
9 openaiModelFacts,
10} from './openai-model-docs.ts'
11import { openrouterServerTools } from './openrouter.ts'
12import {
13 GROQ_BUILTIN_TOOLS_URL,
14 groqModelServerTools,
15 parseByteplusServerTools,
16 parseGroqBuiltinTools,
17 parseMistralPageTools,
18} from './server-tools.ts'
19
20const GROQ_TOOLS = `# Groq Built-In Tools
21
22### GPT-OSS Models
23
24**Models:**
25
26* \`openai/gpt-oss-120b\`
27* \`openai/gpt-oss-20b\`
28
29**Available Tools:**
30
31| Tool | Identifier |
32| --- | --- |
33| Browser Search | browser\\_search |
34| Code Execution | code\\_interpreter |
35
36### Something else
37
38Llama stays on the models page and is not listed here.
39`
40
41const MISTRAL_TOOLS = `<html><body>
42<h2>Supported tools</h2>
43<p>This model accepts <code>web_search</code> and <code>code_interpreter</code>.</p>
44<p>Client <code>function</code> tools are not hosted.</p>
45</body></html>`
46
47const BYTEPLUS_TOOLS = `# Image generation API
48
49## Seedream 4.5
50
51Model ID: \`seedream-4-5-251128\`
52
53tools: [{"type": "web_search"}]
54
55## Seedream 5.0 pro
56
57Model ID: \`seedream-5-0-pro-260115\`
58
59Sequential image generation, web search, and streaming output are not currently supported.
60`
61
62describe('server tools from docs (issue #123)', () => {
63 it('reads Groq tool type ids only for models the built-in tools page names', () => {
64 const tools = parseGroqBuiltinTools(GROQ_TOOLS)
65 expect(tools.get('openai/gpt-oss-120b')).toEqual([
66 'browser_search',
67 'code_interpreter',
68 ])
69 expect(tools.get('openai/gpt-oss-20b')).toEqual([
70 'browser_search',
71 'code_interpreter',
72 ])
73 expect(tools.has('llama-3.3-70b-versatile')).toBe(false)
74 })
75
76 it('reads Mistral tool type ids from a model page and ignores a page that names none', () => {
77 expect(parseMistralPageTools(MISTRAL_TOOLS)).toEqual([
78 'web_search',
79 'code_interpreter',
80 ])
81 expect(
82 parseMistralPageTools(
83 '<html><p>No tool section.</p><script>"web_search"</script></html>',
84 ),
85 ).toEqual([])
86 })
87
88 it('reads a BytePlus tool type only next to the model the section names', () => {
89 const tools = parseByteplusServerTools(BYTEPLUS_TOOLS)
90 expect(tools.get('seedream-4-5-251128')).toEqual(['web_search'])
91 expect(tools.has('seedream-5-0-pro-260115')).toBe(false)
92 })
93
94 it('reads the files page tool id and its agentic models, or throws', () => {
95 // docs.x.ai/developers/files.md as served on 2026-10-22, cut down.
96 const files = [
97 'Grok can search through documents you attach; the system automatically activates the `attachment_search` tool and transforms your request.',
98 'When you attach files, the xAI API automatically activates the `attachment_search` [server-side tool](/developers/tools/overview).',
99 '',
100 '## Limitations',
101 '',
102 '* **Agentic models only**: Requires models that support agentic tool calling (e.g., `grok-4.20`, `grok-4.5`, `grok-4.6`, `grok-4.7`)',
103 ].join('\n')
104 expect(parseGrokFileTool(files)).toEqual({
105 tool: 'attachment_search',
106 models: ['grok-4.20', 'grok-4.5', 'grok-4.6', 'grok-4.7'],
107 })
108 expect(() =>
109 parseGrokFileTool(files.replace(/^\* \*\*Agentic.*$/m, '')),
110 ).toThrow('no Agentic models only list')
111 expect(() =>
112 parseGrokFileTool(
113 files.replace('`attachment_search` [server', '`doc_search` [server'),
114 ),
115 ).toThrow('no single attachment tool id')
116 })
117
118 it('leaves OpenRouter null', () => {
119 expect(
120 openrouterServerTools({
121 supported_parameters: ['tools', 'tool_choice', 'web_search'],
122 }),
123 ).toBeNull()
124 })
125})
126
127/** Serve each URL's body; anything else is a 404. */
128function serve(pages: Record<string, string>) {
129 vi.stubGlobal(
130 'fetch',
131 vi.fn((input: string | URL | Request) => {
132 const url = requestUrl(input)
133 const body = pages[url]
134 return Promise.resolve(
135 body === undefined
136 ? new Response('not found', { status: 404 })
137 : new Response(body, { status: 200 }),
138 )
139 }),
140 )
141}
142
143const endpoints = (live: string, responses: string, realtime: string) => [
144 '## Endpoints',
145 '',
146 '| Endpoint | Route | Support |',
147 '| --- | --- | --- |',
148 `| Live | \`v1/live/sessions\` | ${live} |`,
149 '| Chat Completions | `v1/chat/completions` | Supported |',
150 `| Responses | \`v1/responses\` | ${responses} |`,
151 `| Realtime | \`v1/realtime\` | ${realtime} |`,
152 '| Realtime translation | `v1/realtime/translations` | Not supported |',
153 '| Realtime transcription | `v1/realtime/transcription_sessions` | Not supported |',
154 '| Assistants | `v1/assistants` | Not supported |',
155 '',
156]
157
158describe('a read page that names no tool is source-silent (#707)', () => {
159 afterEach(() => {
160 vi.unstubAllGlobals()
161 })
162
163 it('Groq: a model the built-in tools page does not name states nothing', async () => {
164 serve({ [GROQ_BUILTIN_TOOLS_URL]: GROQ_TOOLS })
165 const tools = await groqModelServerTools()
166 expect(tools('openai/gpt-oss-20b').serverTools).toEqual([
167 'browser_search',
168 'code_interpreter',
169 ])
170 // The page is not exhaustive (browser search names more models), so a
171 // model it does not name gets no entry.
172 expect(tools('allam-2-7b')).toEqual({})
173 })
174
175 it('OpenAI: silent only when no Responses, Realtime or Live route is supported', async () => {
176 // developers.openai.com/api/docs/models/gpt-audio.md, 2026-11-21, cut down.
177 const audio = [
178 'Model ID: `gpt-audio`',
179 '',
180 '## Model details',
181 '',
182 '- Default snapshot: `gpt-audio-2025-08-28`',
183 '- 128,000 context window',
184 '',
185 ...endpoints('Not supported', 'Not supported', 'Not supported'),
186 '## Supported features',
187 '',
188 '- streaming',
189 '- function_calling',
190 '',
191 '## Snapshots',
192 '',
193 '- `gpt-audio-2025-08-28` — Deprecated',
194 ].join('\n')
195 // developers.openai.com/api/docs/models/gpt-4o.md, 2026-11-21, cut down.
196 const gpt4o = [
197 'Model ID: `gpt-4o`',
198 '',
199 ...endpoints('Not supported', 'Supported', 'Not supported'),
200 '## Supported tools',
201 '',
202 'Tools supported by this model when using the Responses API.',
203 '',
204 '- function_calling',
205 '- web_search',
206 ].join('\n')
207 // developers.openai.com/api/docs/models/gpt-realtime-2.md and
208 // gpt-live-1.md, 2026-11-21, cut down: no Supported tools section.
209 const realtime = [
210 'Model ID: `gpt-realtime-2`',
211 '',
212 ...endpoints('Not supported', 'Not supported', 'Supported'),
213 ].join('\n')
214 const live = [
215 'Model ID: `gpt-live-1`',
216 '',
217 ...endpoints('Supported', 'Not supported', 'Not supported'),
218 ].join('\n')
219 // developers.openai.com/api/docs/models/gpt-4.md, 2026-11-21, cut down:
220 // Responses supported, no Supported tools section.
221 const gpt4 = [
222 'Model ID: `gpt-4`',
223 '',
224 ...endpoints('Not supported', 'Supported', 'Not supported'),
225 ].join('\n')
226 serve({
227 [OPENAI_MODELS_INDEX_URL]: [
228 'gpt-audio',
229 'gpt-4o',
230 'gpt-realtime-2',
231 'gpt-live-1',
232 'gpt-4',
233 ]
234 .map((slug) => `[${slug}](/api/docs/models/${slug}.md)`)
235 .join('\n'),
236 [OPENAI_MODEL_PAGE('gpt-audio')]: audio,
237 [OPENAI_MODEL_PAGE('gpt-4o')]: gpt4o,
238 [OPENAI_MODEL_PAGE('gpt-realtime-2')]: realtime,
239 [OPENAI_MODEL_PAGE('gpt-live-1')]: live,
240 [OPENAI_MODEL_PAGE('gpt-4')]: gpt4,
241 })
242 const facts = await openaiModelFacts([
243 'gpt-audio',
244 'gpt-audio-2025-08-28',
245 'gpt-4o',
246 'gpt-5-search-api',
247 'gpt-realtime-2',
248 'gpt-live-1',
249 'gpt-4',
250 ])
251 for (const id of ['gpt-realtime-2', 'gpt-live-1', 'gpt-4']) {
252 expect(facts(id).serverTools).toBeNull()
253 expect(facts(id).factSources?.serverTools).toBeUndefined()
254 }
255 for (const id of ['gpt-audio', 'gpt-audio-2025-08-28']) {
256 expect(facts(id).serverTools).toBeNull()
257 expect(facts(id).factSources?.serverTools).toEqual({
258 derivation: 'source-silent',
259 sourceUrl: OPENAI_MODEL_PAGE('gpt-audio'),
260 })
261 }
262 expect(facts('gpt-4o').serverTools).toEqual(['web_search'])
263 // No model page read: nothing is stated (the ledger covers it).
264 expect(facts('gpt-5-search-api').factSources?.serverTools).toBeUndefined()
265 })
266
267 it('Grok: a model the "e.g." files list does not name stays unstated', async () => {
268 // docs.x.ai/developers/files.md as served on 2026-11-21, cut down.
269 const files = [
270 'When you attach files to a chat message, the xAI API automatically activates the `attachment_search` [server-side tool](/developers/tools/overview).',
271 '* **Agentic models only**: Requires models that support agentic tool calling (e.g., `grok-4.20`, `grok-4.5`, `grok-4.6`, `grok-4.7`)',
272 ].join('\n')
273 const api = {
274 'https://api.x.ai/v1/models': JSON.stringify({
275 data: [{ id: 'grok-4.7' }, { id: 'grok-4.3' }],
276 }),
277 'https://api.x.ai/v1/language-models': JSON.stringify({
278 models: [{ id: 'grok-4.7' }, { id: 'grok-4.3' }],
279 }),
280 'https://api.x.ai/v1/image-generation-models': '{"models":[]}',
281 'https://api.x.ai/v1/video-generation-models': '{"models":[]}',
282 'https://docs.x.ai/docs/models.md':
283 '| Model | Context |\n| --- | --- |\n| grok-4.7 | 500k |\n| grok-4.3 | 1M |',
284 }
285 const env = { XAI_API_KEY: 'test' }
286 serve({ ...api, [GROK_FILES_DOC_URL]: files })
287 const read = await grokProvider.listModels(env)
288 const byId = new Map(read.models.map((m) => [m.rawId, m]))
289 expect(byId.get('grok-4.7')?.serverTools).toEqual(['attachment_search'])
290 expect(byId.get('grok-4.3')?.serverTools).toBeUndefined()
291 // The bullet is "e.g.": not being named states nothing.
292 expect(byId.get('grok-4.3')?.factSources?.serverTools).toBeUndefined()
293
294 serve(api)
295 const failed = await grokProvider.listModels(env)
296 const grok43 = failed.models.find((m) => m.rawId === 'grok-4.3')
297 expect(grok43?.absent?.serverTools).toBe('unavailable')
298 expect(grok43?.factSources?.serverTools).toBeUndefined()
299 })
300})