fix(openai): 非 OAuth 账号 Chat Completions 也提取 system 消息并注入 instructions

GPT-5/Codex 等模型要求请求中必须包含 instructions 字段。
之前 extractSystemMessagesFromInput + applyInstructions 仅在
OAuth 路径执行,API Key 账号走 Chat Completions 时 system 消息
保留在 input 中但缺少 instructions,导致上游返回 400。

同时在 ChatCompletionsRequest 和 ResponsesRequest 中增加
Instructions 字段,支持客户端直接传入 instructions 透传到上游。
This commit is contained in:
erio committed 2026-03-31 20:11:39 +08:00
1 parent 788288c86b
commit adcf6328b4
3 files changed
+29 -11

No files matched your search

@@ -82,20 +82,35 @@ func (s *OpenAIGatewayService) ForwardAsChatCompletions(
return nil, fmt.Errorf("marshal responses request: %w", err)
}
if account.Type == AccountTypeOAuth {
{
var reqBody map[string]any
if err := json.Unmarshal(responsesBody, &reqBody); err != nil {
return nil, fmt.Errorf("unmarshal for codex transform: %w", err)
}
codexResult := applyCodexOAuthTransform(reqBody, false, false)
if codexResult.PromptCacheKey != "" {
promptCacheKey = codexResult.PromptCacheKey
} else if promptCacheKey != "" {
reqBody["prompt_cache_key"] = promptCacheKey
modified := false
if account.Type == AccountTypeOAuth {
codexResult := applyCodexOAuthTransform(reqBody, false, false)
modified = codexResult.Modified
if codexResult.PromptCacheKey != "" {
promptCacheKey = codexResult.PromptCacheKey
} else if promptCacheKey != "" {
reqBody["prompt_cache_key"] = promptCacheKey
}
} else {
// 非 OAuth 账号也需要提取 system 消息并注入 instructions,
// 否则上游 GPT-5/Codex 等模型会报 "Instructions are required"。
if extractSystemMessagesFromInput(reqBody) {
modified = true
}
if applyInstructions(reqBody, false) {
modified = true
}
}
responsesBody, err = json.Marshal(reqBody)
if err != nil {
return nil, fmt.Errorf("remarshal after codex transform: %w", err)
if modified {
responsesBody, err = json.Marshal(reqBody)
if err != nil {
return nil, fmt.Errorf("remarshal after codex transform: %w", err)
}
}
}