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

native-reasoning.test.ts

355 lines11.8 KB

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