Open History to see the last commit
1import { requestUrl } from '#/lib/text.ts'
2import { afterEach, expect, it } from 'vitest'
3import nativeDocs from './fixtures/native-reasoning-docs.json'
4import { parseReasoningEffort } from './openai-model-docs.ts'
5import {
6 parseMistralReasoning,
7 openRouterReasoning,
8} from './reasoning-config.ts'
9import { geminiModelFeatures, parsePageThinking } from './gemini-features.ts'
10import { openrouterProvider, openrouterCapabilities } from './openrouter.ts'
11import { geminiCapabilities } from './gemini.ts'
12import {
13 applyDashscopeThinking,
14 parseDashscopeThinking,
15 DASHSCOPE_THINKING_URL,
16} from './dashscope-thinking.ts'
17import {
18 applyTogetherNativeThinking,
19 parseTogetherNativeThinking,
20 togetherNativeThinking,
21 TOGETHER_GLM_URL,
22 TOGETHER_DEEPSEEK_URL,
23} from './together-native-thinking.ts'
24
25import openRouterGuide from './fixtures/openrouter-native-toggle.json'
26
27const docs = nativeDocs as Record<string, string>
28const originalFetch = globalThis.fetch
29function serve() {
30 globalThis.fetch = async (input) => {
31 const url = requestUrl(input)
32 const body =
33 url === openRouterGuide.sourceUrl ? openRouterGuide.markdown : docs[url]
34 if (body === undefined) throw new Error(`unexpected native source ${url}`)
35 return new Response(body)
36 }
37}
38afterEach(() => {
39 globalThis.fetch = originalFetch
40})
41it('reads actual OpenAI multiline, Codex and GPT-OSS effort lists without negative prose or sample leakage', () => {
42 const parse = (id: string) =>
43 parseReasoningEffort(
44 docs[`https://developers.openai.com/api/docs/models/${id}.md`] ?? '',
45 )
46 expect(parse('gpt-6.1-sol')?.efforts).toEqual([
47 'low',
48 'medium',
49 'high',
50 'xhigh',
51 'max',
52 ])
53 expect(parse('gpt-5.2-codex')?.efforts).toEqual([
54 'low',
55 'medium',
56 'high',
57 'xhigh',
58 ])
59 expect(parse('gpt-oss-120b')?.efforts).toEqual(['low', 'medium', 'high'])
60 expect(parse('gpt-6-luna')?.mandatory).toBe(false)
61 expect(parse('gpt-5.2-codex')?.mandatory).toBe(true)
62 expect(parse('gpt-oss-120b')?.mandatory).toBe(true)
63 expect(
64 parseReasoningEffort(
65 'Reasoning.effort supports: low, high. Thinking cannot be disabled.',
66 )?.mandatory,
67 ).toBe(true)
68 expect(parseReasoningEffort('Reasoning tokens are supported.')).toBeNull()
69 expect(
70 parseReasoningEffort('```\nReasoning.effort supports: high.\n```'),
71 ).toBeNull()
72})
73it('reads Mistral normative scoped control values and the model-specific override', () => {
74 const facts = parseMistralReasoning(
75 docs['https://docs.mistral.ai/studio/conversations/reasoning.md'] ?? '',
76 )
77 expect(facts.get('mistral-large-4-0')).toEqual({
78 mode: 'effort',
79 mandatory: false,
80 efforts: ['high', 'none'],
81 })
82 expect(facts.get('zai-glm-5-3')?.efforts).toEqual(['low', 'high', 'max'])
83 expect(facts.get('zai-glm-5-3')?.mandatory).toBe(true)
84 expect(facts.has('magistral-medium-latest')).toBe(false)
85 const withExample = (
86 docs['https://docs.mistral.ai/studio/conversations/reasoning.md'] ?? ''
87 ).replace(
88 'The `reasoning_effort` parameter controls',
89 '```text\n- `reasoning_effort = "invented"`: only an example\n```\nThe `reasoning_effort` parameter controls',
90 )
91 expect(
92 parseMistralReasoning(withExample).get('mistral-large-4-0')?.efforts,
93 ).toEqual(['high', 'none'])
94 const changed = (
95 docs['https://docs.mistral.ai/studio/conversations/reasoning.md'] ?? ''
96 )
97 .replace(
98 /the model thinks minimally and the thinking chunk is omitted/gi,
99 'the response format varies',
100 )
101 .replace(/`zai-glm-5-3` supports[^\n]+/g, '')
102 const silentMandatory = parseMistralReasoning(changed)
103 expect(silentMandatory.get('mistral-large-4-0')?.mandatory).toBeNull()
104 expect(silentMandatory.get('zai-glm-5-3')?.mandatory).toBeNull()
105})
106it('binds own OpenRouter gateway toggle when native effort metadata is absent', async () => {
107 serve()
108 const { models } = await openrouterProvider.listModels({})
109 expect(models).toHaveLength(3)
110 for (const model of models) expect(model.capabilities).toContain('reasoning')
111 expect(models.find((m) => m.rawId === 'qwen/qwen3-max')?.reasoning).toEqual({
112 mode: 'toggle',
113 mandatory: false,
114 })
115 expect(openRouterReasoning({ reasoning: { mandatory: false } })).toBeNull()
116 expect(() => openrouterCapabilities({ reasoning: [] })).toThrow(
117 'unreadable native reasoning',
118 )
119 expect(() =>
120 openrouterCapabilities({ supported_parameters: 'reasoning' }),
121 ).toThrow('unreadable supported_parameters')
122 globalThis.fetch = async () => new Response(JSON.stringify({ data: [] }))
123 await expect(openrouterProvider.listModels({})).rejects.toThrow(
124 'empty native model catalog',
125 )
126})
127it('uses native Gemini model thinking evidence without a listing flag and binds only sourced aliases', async () => {
128 serve()
129 const { features } = await geminiModelFeatures([
130 'gemini-3.8-flash',
131 'gemini-flash-latest',
132 'gemma-4-31b-it',
133 ])
134 expect(features('gemini-3.8-flash', undefined)).toMatchObject({
135 capabilities: ['reasoning'],
136 reasoning: { mode: 'effort', efforts: ['low', 'medium', 'high'] },
137 })
138 expect(
139 parsePageThinking(
140 docs['https://aistudio.google.com/docs/models/gemini-3.8-flash.md'] ?? '',
141 )?.efforts,
142 ).toEqual(['low', 'medium', 'high'])
143 expect(features('gemini-3.8-flash', false)).toMatchObject({
144 reasoning: null,
145 budget: null,
146 })
147 expect(features('gemini-3.8-flash', false).capabilities).toBeUndefined()
148 expect(features('gemini-flash-latest', false).reasoning).toBeNull()
149 expect(features('gemma-4-31b-it', false).reasoning).toBeNull()
150 for (const id of ['gemini-flash-latest', 'gemma-4-31b-it']) {
151 const record = JSON.parse(
152 docs['https://generativelanguage.googleapis.com/v1beta/models/' + id] ??
153 '{}',
154 ) as { thinking?: boolean }
155 expect(geminiCapabilities(record)).toContain('reasoning')
156 }
157})
158it('scopes Dashscope hybrid capability and replay to published IDs, excluding samples and sibling guesses', () => {
159 const parsed = parseDashscopeThinking(docs[DASHSCOPE_THINKING_URL] ?? '')
160 const evidence = { ...parsed, hash: 'native-fixture' }
161 expect(parsed.models['qwen3.7-max']?.hybrid).toBe(true)
162 expect(parsed.models['qwq-plus']?.hybrid).toBe(false)
163 expect(parsed.models['qwen3.5-flash']?.hybrid).toBe(true)
164 expect(parsed.models['model-studio']).toBeUndefined()
165 expect(parsed.models['qwen-omni']).toBeUndefined()
166 const known = applyDashscopeThinking(
167 {
168 rawId: 'qwen3.7-max',
169 activity: 'chat',
170 requestMap: {
171 thinking: null,
172 maxTokensField: null,
173 developerRole: null,
174 replayReasoningContent: null,
175 store: null,
176 strictTools: null,
177 sessionAffinity: null,
178 cacheControl: null,
179 toolStream: null,
180 reasoningEffort: null,
181 },
182 },
183 evidence,
184 )
185 expect(known.reasoning).toEqual({ mode: 'toggle', mandatory: false })
186 expect(known.requestMap?.replayReasoningContent).toBe(true)
187 expect(known.requestMap?.thinking).toBeNull()
188 expect(
189 applyDashscopeThinking({ rawId: 'qwq-plus', activity: 'chat' }, evidence),
190 ).toMatchObject({ capabilities: ['reasoning'] })
191 expect(
192 applyDashscopeThinking(
193 { rawId: 'unpublished-model', activity: 'chat' },
194 evidence,
195 ).capabilities,
196 ).toBeUndefined()
197 expect(() => parseDashscopeThinking('malformed source')).toThrow(
198 'missing native modes',
199 )
200})
201it('uses Together own preserved-thinking wire and keeps undocumented sibling controls unknown', async () => {
202 serve()
203 const evidence = await togetherNativeThinking()
204 const glm = parseTogetherNativeThinking(nativeDocs[TOGETHER_GLM_URL])
205 expect(glm.ids).toEqual(['zai-org/GLM-5.3', 'zai-org/GLM-5.3-Flash'])
206 expect(glm.replay).toEqual(['zai-org/GLM-5.3'])
207 const deep = parseTogetherNativeThinking(nativeDocs[TOGETHER_DEEPSEEK_URL])
208 expect(deep.replay).toEqual(['deepseek-ai/DeepSeek-V4-Pro-0813'])
209 const model = applyTogetherNativeThinking(
210 { rawId: 'deepseek-ai/DeepSeek-V4-Pro-0813', activity: 'chat' },
211 evidence,
212 )
213 expect(model.requestMap?.replayReasoningContent).toBe(true)
214 expect(model.requestMap?.thinking).toBeNull()
215 expect(model.requestMap?.reasoningEffort).toBeNull()
216 expect(
217 applyTogetherNativeThinking(
218 { rawId: 'deepseek-ai/DeepSeek-V4.1-Flash', activity: 'chat' },
219 evidence,
220 ).requestMap,
221 ).toBeUndefined()
222 const flash = applyTogetherNativeThinking(
223 { rawId: 'zai-org/GLM-5.3-Flash', activity: 'chat' },
224 evidence,
225 )
226 expect(flash.capabilities).toContain('reasoning')
227 expect(flash.requestMap).toBeNull()
228})
229
230it('preserves native map provenance, capability negatives and explicit replay rejections', async () => {
231 serve()
232 const evidence = await togetherNativeThinking()
233 const oldSource = {
234 derivation: 'docs-derived' as const,
235 sourceUrl: 'https://native.test/request',
236 sourceHash: 'old',
237 path: 'request',
238 }
239 const oldMap = {
240 thinking: null,
241 maxTokensField: 'max_tokens' as const,
242 developerRole: false,
243 replayReasoningContent: null,
244 store: null,
245 strictTools: null,
246 sessionAffinity: null,
247 cacheControl: null,
248 toolStream: null,
249 reasoningEffort: null,
250 }
251 const together = applyTogetherNativeThinking(
252 {
253 rawId: 'zai-org/GLM-5.3',
254 activity: 'chat',
255 capabilities: { tools: true, vision: false },
256 requestMap: oldMap,
257 factSources: { requestMap: oldSource },
258 },
259 evidence,
260 )
261 expect(together.capabilities).toEqual({
262 tools: true,
263 vision: false,
264 reasoning: true,
265 })
266 expect(together.requestMap?.maxTokensField).toBe('max_tokens')
267 expect(together.requestMap?.developerRole).toBe(false)
268 expect(together.factSources?.requestMap).toEqual(oldSource)
269 expect(
270 together.factSources?.requestMapFields?.replayReasoningContent?.sourceUrl,
271 ).toBe(TOGETHER_GLM_URL)
272 const qwenEvidence = {
273 ...parseDashscopeThinking(docs[DASHSCOPE_THINKING_URL] ?? ''),
274 hash: 'fixture',
275 }
276 const qwen = applyDashscopeThinking(
277 {
278 rawId: 'qwen3.7-max',
279 activity: 'chat',
280 capabilities: { tools: true, vision: false },
281 requestMap: oldMap,
282 factSources: { requestMap: oldSource },
283 },
284 qwenEvidence,
285 )
286 expect(qwen.capabilities).toEqual({
287 tools: true,
288 vision: false,
289 reasoning: true,
290 })
291 expect(qwen.factSources?.requestMap).toEqual(oldSource)
292 expect(
293 qwen.factSources?.requestMapFields?.replayReasoningContent?.sourceUrl,
294 ).toBe(DASHSCOPE_THINKING_URL)
295 expect(() =>
296 applyTogetherNativeThinking(
297 {
298 rawId: 'zai-org/GLM-5.3',
299 activity: 'chat',
300 capabilities: { reasoning: false },
301 },
302 evidence,
303 ),
304 ).toThrow('explicit rejection')
305 expect(() =>
306 applyDashscopeThinking(
307 {
308 rawId: 'qwen3.7-max',
309 activity: 'chat',
310 capabilities: { reasoning: false },
311 },
312 qwenEvidence,
313 ),
314 ).toThrow('explicit rejection')
315 expect(() =>
316 applyTogetherNativeThinking(
317 {
318 rawId: 'zai-org/GLM-5.3',
319 activity: 'chat',
320 requestMap: { ...oldMap, replayReasoningContent: false },
321 },
322 evidence,
323 ),
324 ).toThrow('explicit rejection')
325 expect(() =>
326 applyDashscopeThinking(
327 {
328 rawId: 'qwen3.7-max',
329 activity: 'chat',
330 requestMap: { ...oldMap, replayReasoningContent: false },
331 },
332 qwenEvidence,
333 ),
334 ).toThrow('explicit rejection')
335})
336
337it('does not read explicitly negated always-on prose as mandatory reasoning', () => {
338 expect(
339 parseReasoningEffort(
340 'Reasoning.effort supports: low, high. Thinking is not always on.',
341 )?.mandatory,
342 ).toBeNull()
343 expect(
344 parseReasoningEffort(
345 'Reasoning.effort supports: low, high. Thinking is always on.',
346 )?.mandatory,
347 ).toBe(true)
348 const mistral = (
349 docs['https://docs.mistral.ai/studio/conversations/reasoning.md'] ?? ''
350 ).replace('is always returned', 'is not always returned')
351 expect(
352 parseMistralReasoning(mistral).get('zai-glm-5-3')?.mandatory,
353 ).toBeNull()
354})