Skip to content
Source
Sign inSign up
TanStack/schemas
Open History to see the last commit

native-reasoning.test.ts

363 lines12.1 KB

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 }
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 the GPT-6.2 "rejects none" sentence as mandatory effort with no list', () => {
74 for (const id of ['gpt-6.2-astra', 'gpt-6.2-sol']) {
75 expect(
76 parseReasoningEffort(
77 docs[`https://developers.openai.com/api/docs/models/${id}.md`] ?? '',
78 ),
79 ).toEqual({ mode: 'effort', mandatory: true })
80 }
81})
82it('reads Mistral normative scoped control values and the model-specific override', () => {
83 const facts = parseMistralReasoning(
84 docs['https://docs.mistral.ai/studio/conversations/reasoning.md'] ?? '',
85 )
86 expect(facts.get('mistral-large-4-0')).toEqual({
87 mode: 'effort',
88 mandatory: false,
89 efforts: ['high', 'none'],
90 })
91 expect(facts.get('zai-glm-5-3')?.efforts).toEqual(['low', 'high', 'max'])
92 expect(facts.get('zai-glm-5-3')?.mandatory).toBe(true)
93 expect(facts.has('magistral-medium-latest')).toBe(false)
94 const withExample = (
95 docs['https://docs.mistral.ai/studio/conversations/reasoning.md'] ?? ''
96 ).replace(
97 'The `reasoning_effort` parameter controls',
98 '```text\n- `reasoning_effort = "invented"`: only an example\n```\nThe `reasoning_effort` parameter controls',
99 )
100 expect(
101 parseMistralReasoning(withExample).get('mistral-large-4-0')?.efforts,
102 ).toEqual(['high', 'none'])
103 const changed = (
104 docs['https://docs.mistral.ai/studio/conversations/reasoning.md'] ?? ''
105 )
106 .replace(
107 /the model thinks minimally and the thinking chunk is omitted/gi,
108 'the response format varies',
109 )
110 .replace(/`zai-glm-5-3` supports[^\n]+/g, '')
111 const silentMandatory = parseMistralReasoning(changed)
112 expect(silentMandatory.get('mistral-large-4-0')?.mandatory).toBeNull()
113 expect(silentMandatory.get('zai-glm-5-3')?.mandatory).toBeNull()
114})
115it('binds own OpenRouter gateway toggle when native effort metadata is absent', async () => {
116 serve()
117 const { models } = await openrouterProvider.listModels({})
118 expect(models).toHaveLength(3)
119 for (const model of models) expect(model.capabilities).toContain('reasoning')
120 expect(models.find((m) => m.rawId === 'qwen/qwen3-max')?.reasoning).toEqual({
121 mode: 'toggle',
122 mandatory: false,
123 })
124 expect(openRouterReasoning({ reasoning: { mandatory: false } })).toBeNull()
125 expect(() => openrouterCapabilities({ reasoning: [] })).toThrow(
126 'unreadable native reasoning',
127 )
128 expect(() =>
129 openrouterCapabilities({ supported_parameters: 'reasoning' }),
130 ).toThrow('unreadable supported_parameters')
131 globalThis.fetch = async () => new Response(JSON.stringify({ data: [] }))
132 await expect(openrouterProvider.listModels({})).rejects.toThrow(
133 'empty native model catalog',
134 )
135})
136it('uses native Gemini model thinking evidence without a listing flag and binds only sourced aliases', async () => {
137 serve()
138 const { features } = await geminiModelFeatures([
139 'gemini-3.8-flash',
140 'gemini-flash-latest',
141 'gemma-4-31b-it',
142 ])
143 expect(features('gemini-3.8-flash', undefined)).toMatchObject({
144 capabilities: ['reasoning'],
145 reasoning: { mode: 'effort', efforts: ['low', 'medium', 'high'] },
146 })
147 expect(
148 parsePageThinking(
149 docs['https://aistudio.google.com/docs/models/gemini-3.8-flash.md'] ?? '',
150 )?.efforts,
151 ).toEqual(['low', 'medium', 'high'])
152 expect(features('gemini-3.8-flash', false)).toMatchObject({
153 reasoning: null,
154 budget: null,
155 })
156 expect(features('gemini-3.8-flash', false).capabilities).toBeUndefined()
157 expect(features('gemini-flash-latest', false).reasoning).toBeNull()
158 expect(features('gemma-4-31b-it', false).reasoning).toBeNull()
159 for (const id of ['gemini-flash-latest', 'gemma-4-31b-it']) {
160 const record = JSON.parse(
161 docs['https://generativelanguage.googleapis.com/v1beta/models/' + id] ??
162 '{}',
163 ) as { thinking?: boolean }
164 expect(geminiCapabilities(record)).toContain('reasoning')
165 }
166})
167it('scopes Dashscope hybrid capability and replay to published IDs, excluding samples and sibling guesses', () => {
168 const parsed = parseDashscopeThinking(docs[DASHSCOPE_THINKING_URL] ?? '')
169 const evidence = { ...parsed, hash: 'native-fixture' }
170 expect(parsed.models['qwen3.7-max']?.hybrid).toBe(true)
171 expect(parsed.models['qwq-plus']?.hybrid).toBe(false)
172 expect(parsed.models['qwen3.5-flash']?.hybrid).toBe(true)
173 expect(parsed.models['model-studio']).toBeUndefined()
174 expect(parsed.models['qwen-omni']).toBeUndefined()
175 const known = applyDashscopeThinking(
176 {
177 rawId: 'qwen3.7-max',
178 activity: 'chat',
179 requestMap: {
180 thinking: null,
181 maxTokensField: null,
182 developerRole: null,
183 replayReasoningContent: null,
184 store: null,
185 strictTools: null,
186 sessionAffinity: null,
187 cacheControl: null,
188 toolStream: null,
189 reasoningEffort: null,
190 },
191 },
192 evidence,
193 )
194 expect(known.reasoning).toEqual({ mode: 'toggle', mandatory: false })
195 expect(known.requestMap?.replayReasoningContent).toBe(true)
196 expect(known.requestMap?.thinking).toBeNull()
197 expect(
198 applyDashscopeThinking({ rawId: 'qwq-plus', activity: 'chat' }, evidence),
199 ).toMatchObject({ capabilities: ['reasoning'] })
200 expect(
201 applyDashscopeThinking(
202 { rawId: 'unpublished-model', activity: 'chat' },
203 evidence,
204 ).capabilities,
205 ).toBeUndefined()
206 expect(() => parseDashscopeThinking('malformed source')).toThrow(
207 'missing native modes',
208 )
209})
210it('uses Together own preserved-thinking wire and keeps undocumented sibling controls unknown', async () => {
211 serve()
212 const evidence = await togetherNativeThinking()
213 const glm = parseTogetherNativeThinking(nativeDocs[TOGETHER_GLM_URL])
214 expect(glm.ids).toEqual(['zai-org/GLM-5.3', 'zai-org/GLM-5.3-Flash'])
215 expect(glm.replay).toEqual(['zai-org/GLM-5.3'])
216 const deep = parseTogetherNativeThinking(nativeDocs[TOGETHER_DEEPSEEK_URL])
217 expect(deep.replay).toEqual(['deepseek-ai/DeepSeek-V4-Pro-0813'])
218 const model = applyTogetherNativeThinking(
219 { rawId: 'deepseek-ai/DeepSeek-V4-Pro-0813', activity: 'chat' },
220 evidence,
221 )
222 expect(model.requestMap?.replayReasoningContent).toBe(true)
223 expect(model.requestMap?.thinking).toBeNull()
224 expect(model.requestMap?.reasoningEffort).toBeNull()
225 expect(
226 applyTogetherNativeThinking(
227 { rawId: 'deepseek-ai/DeepSeek-V4.1-Flash', activity: 'chat' },
228 evidence,
229 ).requestMap,
230 ).toBeUndefined()
231 const flash = applyTogetherNativeThinking(
232 { rawId: 'zai-org/GLM-5.3-Flash', activity: 'chat' },
233 evidence,
234 )
235 expect(flash.capabilities).toContain('reasoning')
236 expect(flash.requestMap).toBeNull()
237})
238​
239it('preserves native map provenance, capability negatives and explicit replay rejections', async () => {
240 serve()
241 const evidence = await togetherNativeThinking()
242 const oldSource = {
243 derivation: 'docs-derived' as const,
244 sourceUrl: 'https://native.test/request',
245 sourceHash: 'old',
246 path: 'request',
247 }
248 const oldMap = {
249 thinking: null,
250 maxTokensField: 'max_tokens' as const,
251 developerRole: false,
252 replayReasoningContent: null,
253 store: null,
254 strictTools: null,
255 sessionAffinity: null,
256 cacheControl: null,
257 toolStream: null,
258 reasoningEffort: null,
259 }
260 const together = applyTogetherNativeThinking(
261 {
262 rawId: 'zai-org/GLM-5.3',
263 activity: 'chat',
264 capabilities: { tools: true, vision: false },
265 requestMap: oldMap,
266 factSources: { requestMap: oldSource },
267 },
268 evidence,
269 )
270 expect(together.capabilities).toEqual({
271 tools: true,
272 vision: false,
273 reasoning: true,
274 })
275 expect(together.requestMap?.maxTokensField).toBe('max_tokens')
276 expect(together.requestMap?.developerRole).toBe(false)
277 expect(together.factSources?.requestMap).toEqual(oldSource)
278 expect(
279 together.factSources?.requestMapFields?.replayReasoningContent?.sourceUrl,
280 ).toBe(TOGETHER_GLM_URL)
281 const qwenEvidence = {
282 ...parseDashscopeThinking(docs[DASHSCOPE_THINKING_URL] ?? ''),
283 hash: 'fixture',
284 }
285 const qwen = applyDashscopeThinking(
286 {
287 rawId: 'qwen3.7-max',
288 activity: 'chat',
289 capabilities: { tools: true, vision: false },
290 requestMap: oldMap,
291 factSources: { requestMap: oldSource },
292 },
293 qwenEvidence,
294 )
295 expect(qwen.capabilities).toEqual({
296 tools: true,
297 vision: false,
298 reasoning: true,
299 })
300 expect(qwen.factSources?.requestMap).toEqual(oldSource)
301 expect(
302 qwen.factSources?.requestMapFields?.replayReasoningContent?.sourceUrl,
303 ).toBe(DASHSCOPE_THINKING_URL)
304 expect(() =>
305 applyTogetherNativeThinking(
306 {
307 rawId: 'zai-org/GLM-5.3',
308 activity: 'chat',
309 capabilities: { reasoning: false },
310 },
311 evidence,
312 ),
313 ).toThrow('explicit rejection')
314 expect(() =>
315 applyDashscopeThinking(
316 {
317 rawId: 'qwen3.7-max',
318 activity: 'chat',
319 capabilities: { reasoning: false },
320 },
321 qwenEvidence,
322 ),
323 ).toThrow('explicit rejection')
324 expect(() =>
325 applyTogetherNativeThinking(
326 {
327 rawId: 'zai-org/GLM-5.3',
328 activity: 'chat',
329 requestMap: { ...oldMap, replayReasoningContent: false },
330 },
331 evidence,
332 ),
333 ).toThrow('explicit rejection')
334 expect(() =>
335 applyDashscopeThinking(
336 {
337 rawId: 'qwen3.7-max',
338 activity: 'chat',
339 requestMap: { ...oldMap, replayReasoningContent: false },
340 },
341 qwenEvidence,
342 ),
343 ).toThrow('explicit rejection')
344})
345​
346it('does not read explicitly negated always-on prose as mandatory reasoning', () => {
347 expect(
348 parseReasoningEffort(
349 'Reasoning.effort supports: low, high. Thinking is not always on.',
350 )?.mandatory,
351 ).toBeNull()
352 expect(
353 parseReasoningEffort(
354 'Reasoning.effort supports: low, high. Thinking is always on.',
355 )?.mandatory,
356 ).toBe(true)
357 const mistral = (
358 docs['https://docs.mistral.ai/studio/conversations/reasoning.md'] ?? ''
359 ).replace('is always returned', 'is not always returned')
360 expect(
361 parseMistralReasoning(mistral).get('zai-glm-5-3')?.mandatory,
362 ).toBeNull()
363})