Files
sub2api/backend/internal/service/openai_ws_string_input.go
HuntercodeT 0e35d49c99 fix(openai): convert string input to a message list on Codex WS upstream
The ChatGPT codex backend rejects a string `input` with
"Input must be a list". The HTTP path already converts it in
applyCodexOAuthTransform (#919), but the Responses WebSocket ingress
forwarded the client frame unchanged, so the same request succeeded over
HTTP and failed over WS. Clients that treat a failed WS turn as "WS
unsupported" then fall back to HTTP for the whole provider.

Convert only the copy written to the native upstream WS, using the same
rule as the HTTP path. The session's stored client payloads and the
http_bridge path are unchanged.
2026-12-01 09:40:42 +08:00

38 lines
1.3 KiB
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package service
import (
"strings"
"github.com/tidwall/gjson"
"github.com/tidwall/sjson"
)
// normalizeOpenAIWSCodexStringInput 把 WS response.create 里字符串形式的 input
// 转成 ChatGPT codex 后端要求的 message 列表。
//
// 标准 Responses API 允许 input 为字符串,但 chatgpt.com/backend-api/codex 只接受
// 列表,字符串会被上游以 "Input must be a list" 拒绝。HTTP 路径已在
// applyCodexOAuthTransform 中做同样的转换;WS 入口此前缺这一步,导致同一请求走 HTTP
// 成功、走 WS 失败(客户端随之把整个 provider 降级回 HTTP)。转换规则与 HTTP 路径
// 保持逐字一致:非空字符串 -> 单条 user message(content 为原字符串),空白 -> []。
func normalizeOpenAIWSCodexStringInput(payload []byte) ([]byte, bool, error) {
input := gjson.GetBytes(payload, "input")
if !input.Exists() || input.Type != gjson.String {
return payload, false, nil
}
text := input.String()
var value any = []any{}
if strings.TrimSpace(text) != "" {
value = []any{map[string]any{
"type": "message",
"role": "user",
"content": text,
}}
}
next, err := sjson.SetBytes(payload, "input", value)
if err != nil {
return payload, false, err
}
return next, true, nil
}