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

request-map.ts

358 lines10.6 KB

1import { effortLevelMapSchema } from '#/server/api-schemas.ts'
2import type {
3 ChatRequestMap,
4 EffortLevelMap,
5 ThinkingRequest,
6} from '#/server/api-schemas.ts'
7​
8export type { ChatRequestMap, EffortLevelMap, ThinkingRequest }
9​
10export const SHARED_EFFORT_LEVELS = effortLevelMapSchema.keyof().options
11​
12export type SharedEffortLevel = (typeof SHARED_EFFORT_LEVELS)[number]
13​
14const OPENAI_ON = { reasoning_effort: 'high' }
15const DEEPSEEK_ON = {
16 thinking: { type: 'enabled' },
17 reasoning_effort: 'high',
19const DEEPSEEK_OFF = { thinking: { type: 'disabled' } }
20const GLM_ON = { thinking: { type: 'enabled', clear_thinking: false } }
21const GLM_OFF = { thinking: { type: 'disabled' } }
22const QWEN_ON = { enable_thinking: true }
23const VLLM_QWEN_ON = {
24 chat_template_kwargs: { enable_thinking: true, preserve_thinking: true },
26const KIMI_ON = { thinking: { type: 'enabled' } }
27const KIMI_OFF = { thinking: { type: 'disabled' } }
28const MINIMAX_ON = { thinking: { type: 'adaptive' } }
29const MINIMAX_OFF = { thinking: { type: 'disabled' } }
30​
31/** `openai/gpt-5.1`: off is sent as "none"; minimal, xhigh, and max are omitted. */
32const GPT_51_LEVELS: EffortLevelMap = {
33 off: 'none',
34 minimal: null,
35 low: 'low',
36 medium: 'medium',
37 high: 'high',
38 xhigh: null,
39 max: null,
41​
42/**
43 * SambaNova `ChatCompletionRequest.reasoning_effort`: low, medium, high.
44 * The enum has no off.
45 * https://raw.githubusercontent.com/sambanova/sambanova-inference-api-spec/refs/heads/main/openapi.documented.json
46 */
47const SAMBANOVA_LEVELS: EffortLevelMap = {
48 off: null,
49 minimal: null,
50 low: 'low',
51 medium: 'medium',
52 high: 'high',
53 xhigh: null,
54 max: null,
56​
57/** `minimax/MiniMax-M3.1-Flash-Preview`: the spec's `reasoning_effort` enum. It cannot stop thinking. */
58const MINIMAX_M31_FLASH_LEVELS: EffortLevelMap = {
59 off: null,
60 minimal: null,
61 low: 'low',
62 medium: 'medium',
63 high: 'high',
64 xhigh: 'xhigh',
65 max: 'max',
67​
68/** `deepseek/deepseek-flash`: only low, high, and max. medium is omitted. */
69const DEEPSEEK_FLASH_LEVELS: EffortLevelMap = {
70 off: null,
71 minimal: null,
72 low: 'low',
73 medium: null,
74 high: 'high',
75 xhigh: null,
76 max: 'max',
78​
79/** `kimi-k3`: the spec's `reasoning_effort` enum. It cannot stop thinking. */
80const KIMI_K3_LEVELS: EffortLevelMap = {
81 off: null,
82 minimal: null,
83 low: 'low',
84 medium: null,
85 high: 'high',
86 xhigh: null,
87 max: 'max',
89​
90/**
91 * Moonshot's per-model chat schemas, by exact id: `kimi-k3` takes
92 * `reasoning_effort` and always thinks, `kimi-k2.7-code` takes only
93 * `thinking.type: enabled`, `kimi-k2.6` also takes `disabled`. An id the
94 * spec does not map stays null.
95 */
96function kimiThinking(rawId: string): ThinkingRequest | null {
97 switch (rawId) {
98 case 'kimi-k3':
99 return { on: OPENAI_ON, off: null, levels: KIMI_K3_LEVELS }
100 case 'kimi-k2.7-code':
101 case 'kimi-k2.7-code-highspeed':
102 return { on: KIMI_ON, off: null, levels: null }
103 case 'kimi-k2.6':
104 return { on: KIMI_ON, off: KIMI_OFF, levels: null }
105 default:
106 return null
107 }
109​
110/** `zai/glm-5.2`: only high and max. */
111const GLM_52_LEVELS: EffortLevelMap = {
112 off: null,
113 minimal: null,
114 low: null,
115 medium: null,
116 high: 'high',
117 xhigh: null,
118 max: 'max',
120​
121/**
122 * `zai/glm-5.3` and `glm-5.3-flash`: only low, high, and max; any other
123 * value is an error (docs.z.ai/guides/capabilities/thinking).
124 */
125const GLM_53_LEVELS: EffortLevelMap = {
126 off: null,
127 minimal: null,
128 low: 'low',
129 medium: null,
130 high: 'high',
131 xhigh: null,
132 max: 'max',
134​
135/**
136 * Series the Z.AI spec names under `tool_stream`
137 * (docs.z.ai/openapi.json, ChatCompletionTextRequest). The vision request
138 * has no such field; for `glm-5.3-flash` and `glm-5.3-flashx` the source is
139 * docs.z.ai/guides/vlm/glm-5.3-flash ("enabling both stream: true and
140 * `tool_stream: true`").
141 */
142const GLM_TOOL_STREAM = [
143 'glm-5.3',
144 'glm-5.2',
145 'glm-5.1',
146 'glm-5',
147 'glm-4.7',
148 'glm-4.6',
150​
151function blank(partial: Partial<ChatRequestMap>): ChatRequestMap {
152 return {
153 thinking: null,
154 maxTokensField: null,
155 developerRole: null,
156 replayReasoningContent: null,
157 store: null,
158 strictTools: null,
159 sessionAffinity: null,
160 cacheControl: null,
161 toolStream: null,
162 reasoningEffort: null,
163 ...partial,
164 }
166​
167function bareId(rawId: string): string {
168 const slash = rawId.lastIndexOf('/')
169 return slash === -1 ? rawId : rawId.slice(slash + 1)
171​
172function matches(rawId: string, name: string): boolean {
173 const bare = bareId(rawId)
174 return bare === name || bare.startsWith(`${name}-`)
176​
177function levelsFor(providerId: string, rawId: string): EffortLevelMap | null {
178 if (providerId === 'openai' && matches(rawId, 'gpt-5.1')) return GPT_51_LEVELS
179 if (
180 (providerId === 'minimax' || providerId === 'minimax-cn') &&
181 matches(rawId, 'MiniMax-M3.1-Flash-Preview')
182 ) {
183 return MINIMAX_M31_FLASH_LEVELS
184 }
185 if (providerId === 'deepseek' && matches(rawId, 'deepseek-flash')) {
186 return DEEPSEEK_FLASH_LEVELS
187 }
188 if (providerId === 'zai' || providerId === 'glm') {
189 if (matches(rawId, 'glm-5.2')) return GLM_52_LEVELS
190 if (matches(rawId, 'glm-5.3')) return GLM_53_LEVELS
191 }
192 return null
194​
195function withLevels(
196 thinking: ThinkingRequest,
197 providerId: string,
198 rawId: string,
199): ThinkingRequest {
200 const levels = levelsFor(providerId, rawId)
201 return levels === null ? thinking : { ...thinking, levels }
203​
204/**
205 * Provider-wide request facts for one chat model. Model-specific level maps
206 * override the provider's `thinking.levels`. Anything not verified stays null.
207 */
208export function chatRequestMap(
209 providerId: string,
210 rawId: string,
211 activity: string | null | undefined,
212): ChatRequestMap | null {
213 if (activity !== 'chat') return null
214 switch (providerId) {
215 case 'openai':
216 return blank({
217 thinking: withLevels(
218 { on: OPENAI_ON, off: null, levels: null },
219 providerId,
220 rawId,
221 ),
222 maxTokensField: 'max_completion_tokens',
223 developerRole: true,
224 reasoningEffort: true,
225 })
226 case 'deepseek':
227 return blank({
228 thinking: withLevels(
229 { on: DEEPSEEK_ON, off: DEEPSEEK_OFF, levels: null },
230 providerId,
231 rawId,
232 ),
233 maxTokensField: 'max_tokens',
234 developerRole: false,
235 replayReasoningContent: true,
236 reasoningEffort: true,
237 })
238 case 'zai':
239 case 'glm':
240 return blank({
241 // The spec's `thinking` is "GLM-4.5 series and higher" only, and
242 // `thinking.type` is enabled | disabled, except that the GLM-5.3
243 // and GLM-5.3-FLASH series "can only be enabled".
244 thinking: matches(rawId, 'glm-4-32b')
245 ? null
246 : withLevels(
247 {
248 on: GLM_ON,
249 off: matches(rawId, 'glm-5.3') ? null : GLM_OFF,
250 levels: null,
251 },
252 providerId,
253 rawId,
254 ),
255 maxTokensField: 'max_tokens',
256 developerRole: false,
257 toolStream: GLM_TOOL_STREAM.some((name) => matches(rawId, name))
258 ? true
259 : null,
260 // `reasoning_effort` is "supported by GLM-5.2 and above"; the spec
261 // does not say what older models do with it.
262 reasoningEffort: levelsFor(providerId, rawId) === null ? null : true,
263 })
264 case 'qwen':
265 return blank({
266 thinking: withLevels(
267 { on: QWEN_ON, off: null, levels: null },
268 providerId,
269 rawId,
270 ),
271 developerRole: false,
272 })
273 // From MiniMax's chat spec, not probed: `max_tokens` is deprecated, the
274 // role enum has no `developer`, and every model takes adaptive thinking.
275 // `disabled` skips thinking on MiniMax-M3 only: M3.1-Flash-Preview
276 // answers 400 and the M2 models ignore it. The China platform's chat
277 // spec states the same of the same ids.
278 case 'minimax':
279 case 'minimax-cn':
280 return blank({
281 thinking: withLevels(
282 {
283 on: MINIMAX_ON,
284 off: matches(rawId, 'MiniMax-M3') ? MINIMAX_OFF : null,
285 levels: null,
286 },
287 providerId,
288 rawId,
289 ),
290 maxTokensField: 'max_completion_tokens',
291 developerRole: false,
292 reasoningEffort: true,
293 })
294 case 'vllm':
295 return blank({
296 thinking: withLevels(
297 { on: VLLM_QWEN_ON, off: null, levels: null },
298 providerId,
299 rawId,
300 ),
301 developerRole: false,
302 })
303 // Hugging Face's chat-completion schema names `max_tokens` only. The
304 // rest depends on the routed provider, so it stays null.
305 case 'huggingface':
306 return blank({ maxTokensField: 'max_tokens' })
307 // From Moonshot's chat spec, not probed. The international and China
308 // documents agree: `max_tokens` is deprecated for
309 // `max_completion_tokens`, the role enum has no `developer`, and only
310 // `kimi-k3` declares `reasoning_effort`. The spec does not say the other
311 // models reject it, so that stays null. For `kimi-k3` Moonshot's OpenClaw
312 // guide sets `maxTokensField: "max_tokens"` against the spec, so its
313 // field stays null until probed.
314 case 'moonshot':
315 case 'moonshotai-cn':
316 return blank({
317 thinking: kimiThinking(rawId),
318 maxTokensField: rawId === 'kimi-k3' ? null : 'max_completion_tokens',
319 developerRole: false,
320 reasoningEffort: rawId === 'kimi-k3' ? true : null,
321 })
322 case 'grok':
323 return blank({
324 developerRole: false,
325 reasoningEffort: false,
326 })
327 // Cohere's `/v2/chat` spec: the output cap is `max_tokens`, the message
328 // roles are user, assistant, system and tool, and the body has no
329 // `reasoning_effort` (the API answers 422 to it and to `developer`).
330 // `thinking` is null: only reasoning models take it, and this map is
331 // not per capability.
332 case 'cohere':
333 return blank({
334 maxTokensField: 'max_tokens',
335 developerRole: false,
336 reasoningEffort: false,
337 })
338 // SambaNova's own chat schema, not the OpenAI document schemas are
339 // generated from. `max_tokens` is what the guides send;
340 // `max_completion_tokens` describes the same cap. Roles are system,
341 // user, assistant, and tool. `reasoning_effort` is on the shared body
342 // (low | medium | high) with no off value.
343 // https://raw.githubusercontent.com/sambanova/sambanova-inference-api-spec/refs/heads/main/openapi.documented.json
344 case 'sambanova':
345 return blank({
346 thinking: {
347 on: { reasoning_effort: 'high' },
348 off: null,
349 levels: SAMBANOVA_LEVELS,
350 },
351 maxTokensField: 'max_tokens',
352 developerRole: false,
353 reasoningEffort: true,
354 })
355 default:
356 return null
357 }