fix: 恢复 isOpenAIThinkingEnabled 和 buildOpenAIRequestBody 导出

thinking.test.ts 引用了两个已被删除的函数,导致模块加载失败
(1 fail + 1 error)。恢复了完整的 OpenAI thinking mode 支持:

- isOpenAIThinkingEnabled: env var OPENAI_ENABLE_THINKING 优先级最高,
  其次自动检测 deepseek 系列模型名
- buildOpenAIRequestBody: 构建请求体,同时注入三种 thinking 格式
  (OpenAI official / vLLM / chat_template),thinking 开启时排除 temperature

测试:3591 pass, 4 skip, 0 fail, 0 error(首次全绿)
This commit is contained in:
James Feng 2026-06-04 19:32:39 +08:00
parent f6488bce8f
commit 05cb6b0a7a

View File

@ -271,3 +271,92 @@ export async function* queryModelOpenAI(
})
}
}
/**
* Checks whether OpenAI thinking/reasoning mode is enabled for a given model.
*
* Priority:
* 1. OPENAI_ENABLE_THINKING env var if set to a truthy value (1/true/yes/on,
* case-insensitive), thinking is forced ON for all models. If set to a falsy
* value (0/false/empty), thinking is forced OFF for all models.
* 2. Model name auto-detect if the env var is unset, any model whose name
* contains "deepseek" (case-insensitive) gets thinking enabled.
* 3. Default: false.
*/
export function isOpenAIThinkingEnabled(model: string): boolean {
const env = process.env.OPENAI_ENABLE_THINKING
if (env !== undefined) {
const trimmed = env.trim().toLowerCase()
if (
trimmed === '1' ||
trimmed === 'true' ||
trimmed === 'yes' ||
trimmed === 'on'
) {
return true
}
return false
}
return model.toLowerCase().includes('deepseek')
}
/**
* Builds an OpenAI-compatible chat completions request body.
*
* Injects thinking params for all three known formats simultaneously when
* enableThinking is true:
* - thinking: { type: 'enabled' } official OpenAI/DeepSeek API
* - enable_thinking: true vLLM / self-hosted
* - chat_template_kwargs: { thinking: true } vLLM chat template
*
* Temperature is excluded when thinking is on (thinking models reject it).
*/
export function buildOpenAIRequestBody(params: {
model: string
messages: unknown[]
tools: unknown[]
toolChoice: unknown
enableThinking?: boolean
temperatureOverride?: number
maxTokens?: number
systemPrompt?: unknown
}): Record<string, unknown> {
const {
model,
messages,
tools,
toolChoice,
enableThinking = false,
temperatureOverride,
maxTokens,
systemPrompt,
} = params
const body: Record<string, unknown> = {
model,
messages,
stream: true,
stream_options: { include_usage: true },
}
if (tools.length > 0) {
body.tools = tools
if (toolChoice !== undefined) {
body.tool_choice = toolChoice
}
}
if (enableThinking) {
body.thinking = { type: 'enabled' }
body.enable_thinking = true
body.chat_template_kwargs = { thinking: true }
} else if (temperatureOverride !== undefined) {
body.temperature = temperatureOverride
}
if (maxTokens !== undefined) {
body.max_completion_tokens = maxTokens
}
return body
}