feat(codex): preserve native fidelity for responses-lite requests

- Detect native responses-lite requests via headers and client metadata.
- Skip instructions normalization and synthetic session cloaking for native requests.
- Preserve upstream completion output during websocket response forwarding.

Closes: #5780
This commit is contained in:
Luis Pater
2026-09-13 15:05:03 +08:00
parent e696ea47c5
commit f702bc1ac2
15 changed files with 403 additions and 59 deletions

View File

@@ -27,7 +27,6 @@ const (
codexOriginator = "codex-tui"
codexDefaultImageToolModel = "gpt-image-2"
codexResponsesLiteHeader = "X-OpenAI-Internal-Codex-Responses-Lite"
codexResponsesLiteMetadata = "client_metadata.ws_request_header_x_openai_internal_codex_responses_lite"
)
var dataTag = []byte("data:")
@@ -378,7 +377,10 @@ func applyCodexCloakingHeaders(headers http.Header, cfg *config.Config) {
headers.Set("Originator", codexOriginator)
}
func normalizeCodexInstructions(body []byte) []byte {
func normalizeCodexInstructions(body []byte, nativeRequest ...bool) []byte {
if len(nativeRequest) > 0 && nativeRequest[0] {
return body
}
instructions := gjson.GetBytes(body, "instructions")
if !instructions.Exists() || instructions.Type == gjson.Null {
body, _ = sjson.SetBytes(body, "instructions", "")
@@ -420,20 +422,8 @@ func isImageGenerationFunctionTool(tool gjson.Result) bool {
return false
}
func isCodexResponsesLiteRequest(body []byte, headers http.Header) bool {
if strings.EqualFold(strings.TrimSpace(headers.Get(codexResponsesLiteHeader)), "true") {
return true
}
// Codex Desktop mirrors websocket-only request headers into client_metadata.
value := gjson.GetBytes(body, codexResponsesLiteMetadata)
if !value.Exists() {
return false
}
return value.Type == gjson.True || value.Type == gjson.String && strings.EqualFold(strings.TrimSpace(value.String()), "true")
}
func ensureImageGenerationTool(body []byte, baseModel string, auth *cliproxyauth.Auth, headers http.Header) []byte {
if isCodexResponsesLiteRequest(body, headers) {
if util.IsCodexResponsesLiteRequest(body, headers) {
return body
}
if strings.HasSuffix(baseModel, "spark") {
@@ -458,7 +448,7 @@ func ensureImageGenerationTool(body []byte, baseModel string, auth *cliproxyauth
}
func normalizeCodexParallelToolCalls(body []byte, headers http.Header) []byte {
if isCodexResponsesLiteRequest(body, headers) {
if util.IsCodexResponsesLiteRequest(body, headers) {
body = helps.SetBoolIfDifferent(body, "parallel_tool_calls", false)
return body
}