Open History to see the last commit
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',
18}
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 },
25}
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,
40}
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,
55}
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',
66}
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',
77}
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',
88}
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 }
108}
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',
119}
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',
133}
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',
149]
150
151export function blankRequestMap(
152 partial: Partial<ChatRequestMap>,
153): ChatRequestMap {
154 return {
155 thinking: null,
156 maxTokensField: null,
157 developerRole: null,
158 replayReasoningContent: null,
159 store: null,
160 strictTools: null,
161 sessionAffinity: null,
162 cacheControl: null,
163 toolStream: null,
164 reasoningEffort: null,
165 ...partial,
166 }
167}
168
169function bareId(rawId: string): string {
170 const slash = rawId.lastIndexOf('/')
171 return slash === -1 ? rawId : rawId.slice(slash + 1)
172}
173
174function matches(rawId: string, name: string): boolean {
175 const bare = bareId(rawId)
176 return bare === name || bare.startsWith(`${name}-`)
177}
178
179function levelsFor(providerId: string, rawId: string): EffortLevelMap | null {
180 if (providerId === 'openai' && matches(rawId, 'gpt-5.1')) return GPT_51_LEVELS
181 if (
182 (providerId === 'minimax' || providerId === 'minimax-cn') &&
183 matches(rawId, 'MiniMax-M3.1-Flash-Preview')
184 ) {
185 return MINIMAX_M31_FLASH_LEVELS
186 }
187 if (providerId === 'deepseek' && matches(rawId, 'deepseek-flash')) {
188 return DEEPSEEK_FLASH_LEVELS
189 }
190 if (providerId === 'zai' || providerId === 'glm') {
191 if (matches(rawId, 'glm-5.2')) return GLM_52_LEVELS
192 if (matches(rawId, 'glm-5.3')) return GLM_53_LEVELS
193 }
194 return null
195}
196
197function withLevels(
198 thinking: ThinkingRequest,
199 providerId: string,
200 rawId: string,
201): ThinkingRequest {
202 const levels = levelsFor(providerId, rawId)
203 return levels === null ? thinking : { ...thinking, levels }
204}
205
206/**
207 * Provider-wide request facts for one chat model. Model-specific level maps
208 * override the provider's `thinking.levels`. Anything not verified stays null.
209 */
210export function chatRequestMap(
211 providerId: string,
212 rawId: string,
213 activity: string | null | undefined,
214): ChatRequestMap | null {
215 if (activity !== 'chat') return null
216 switch (providerId) {
217 case 'openai':
218 return blankRequestMap({
219 thinking: withLevels(
220 { on: OPENAI_ON, off: null, levels: null },
221 providerId,
222 rawId,
223 ),
224 maxTokensField: 'max_completion_tokens',
225 developerRole: true,
226 reasoningEffort: true,
227 })
228 case 'deepseek':
229 return blankRequestMap({
230 thinking: withLevels(
231 { on: DEEPSEEK_ON, off: DEEPSEEK_OFF, levels: null },
232 providerId,
233 rawId,
234 ),
235 maxTokensField: 'max_tokens',
236 developerRole: false,
237 replayReasoningContent: true,
238 reasoningEffort: true,
239 })
240 case 'zai':
241 case 'glm':
242 return blankRequestMap({
243 // The spec's `thinking` is "GLM-4.5 series and higher" only, and
244 // `thinking.type` is enabled | disabled, except that the GLM-5.3
245 // and GLM-5.3-FLASH series "can only be enabled".
246 thinking: matches(rawId, 'glm-4-32b')
247 ? null
248 : withLevels(
249 {
250 on: GLM_ON,
251 off: matches(rawId, 'glm-5.3') ? null : GLM_OFF,
252 levels: null,
253 },
254 providerId,
255 rawId,
256 ),
257 maxTokensField: 'max_tokens',
258 developerRole: false,
259 toolStream: GLM_TOOL_STREAM.some((name) => matches(rawId, name))
260 ? true
261 : null,
262 // `reasoning_effort` is "supported by GLM-5.2 and above"; the spec
263 // does not say what older models do with it.
264 reasoningEffort: levelsFor(providerId, rawId) === null ? null : true,
265 })
266 case 'qwen':
267 return blankRequestMap({
268 thinking: withLevels(
269 { on: QWEN_ON, off: null, levels: null },
270 providerId,
271 rawId,
272 ),
273 developerRole: false,
274 })
275 // From MiniMax's chat spec, not probed: `max_tokens` is deprecated, the
276 // role enum has no `developer`, and every model takes adaptive thinking.
277 // `disabled` skips thinking on MiniMax-M3 only: M3.1-Flash-Preview
278 // answers 400 and the M2 models ignore it. The China platform's chat
279 // spec states the same of the same ids.
280 case 'minimax':
281 case 'minimax-cn':
282 return blankRequestMap({
283 thinking: withLevels(
284 {
285 on: MINIMAX_ON,
286 off: matches(rawId, 'MiniMax-M3') ? MINIMAX_OFF : null,
287 levels: null,
288 },
289 providerId,
290 rawId,
291 ),
292 maxTokensField: 'max_completion_tokens',
293 developerRole: false,
294 reasoningEffort: true,
295 })
296 case 'vllm':
297 return blankRequestMap({
298 thinking: withLevels(
299 { on: VLLM_QWEN_ON, off: null, levels: null },
300 providerId,
301 rawId,
302 ),
303 developerRole: false,
304 })
305 // Hugging Face's chat-completion schema names `max_tokens` only. The
306 // rest depends on the routed provider, so it stays null.
307 case 'huggingface':
308 return blankRequestMap({ maxTokensField: 'max_tokens' })
309 // From Moonshot's chat spec, not probed. The international and China
310 // documents agree: `max_tokens` is deprecated for
311 // `max_completion_tokens`, the role enum has no `developer`, and only
312 // `kimi-k3` declares `reasoning_effort`. The spec does not say the other
313 // models reject it, so that stays null. For `kimi-k3` Moonshot's OpenClaw
314 // guide sets `maxTokensField: "max_tokens"` against the spec, so its
315 // field stays null until probed.
316 case 'moonshot':
317 case 'moonshotai-cn':
318 return blankRequestMap({
319 thinking: kimiThinking(rawId),
320 maxTokensField: rawId === 'kimi-k3' ? null : 'max_completion_tokens',
321 developerRole: false,
322 reasoningEffort: rawId === 'kimi-k3' ? true : null,
323 })
324 case 'grok':
325 return blankRequestMap({
326 developerRole: false,
327 reasoningEffort: false,
328 })
329 // Cohere's `/v2/chat` spec: the output cap is `max_tokens`, the message
330 // roles are user, assistant, system and tool, and the body has no
331 // `reasoning_effort` (the API answers 422 to it and to `developer`).
332 // `thinking` is null: only reasoning models take it, and this map is
333 // not per capability.
334 case 'cohere':
335 return blankRequestMap({
336 maxTokensField: 'max_tokens',
337 developerRole: false,
338 reasoningEffort: false,
339 })
340 // SambaNova's own chat schema, not the OpenAI document schemas are
341 // generated from. `max_tokens` is what the guides send;
342 // `max_completion_tokens` describes the same cap. Roles are system,
343 // user, assistant, and tool. `reasoning_effort` is on the shared body
344 // (low | medium | high) with no off value.
345 // https://raw.githubusercontent.com/sambanova/sambanova-inference-api-spec/refs/heads/main/openapi.documented.json
346 case 'sambanova':
347 return blankRequestMap({
348 thinking: {
349 on: { reasoning_effort: 'high' },
350 off: null,
351 levels: SAMBANOVA_LEVELS,
352 },
353 maxTokensField: 'max_tokens',
354 developerRole: false,
355 reasoningEffort: true,
356 })
357 default:
358 return null
359 }
360}