fix(网关): OAuth 请求强制 store=false

避免上游 Store 必须为 false 的错误

仅在缺失或 true 时写回 store

测试: go test ./internal/service -run TestApplyCodexOAuthTransform

测试: make test-backend(golangci-lint 已单独执行)
This commit is contained in:
yangjianbo
2026-01-14 09:17:58 +08:00
parent 7bbf49fd65
commit 3663951d11
2 changed files with 35 additions and 18 deletions

View File

@@ -90,17 +90,14 @@ func applyCodexOAuthTransform(reqBody map[string]any) codexTransformResult {
result.NormalizedModel = normalizedModel result.NormalizedModel = normalizedModel
} }
// 续链场景强制启用 store非续链仍按原策略强制关闭存储 // OAuth 走 ChatGPT internal API 时store 必须为 false显式 true 也会强制覆盖
if needsToolContinuation { // 避免上游返回 "Store must be set to false"。
if v, ok := reqBody["store"].(bool); !ok || !v { if v, ok := reqBody["store"].(bool); !ok {
reqBody["store"] = true reqBody["store"] = false
result.Modified = true result.Modified = true
} } else if v {
} else { reqBody["store"] = false
if v, ok := reqBody["store"].(bool); !ok || v { result.Modified = true
reqBody["store"] = false
result.Modified = true
}
} }
if v, ok := reqBody["stream"].(bool); !ok || !v { if v, ok := reqBody["stream"].(bool); !ok || !v {
reqBody["stream"] = true reqBody["stream"] = true

View File

@@ -11,7 +11,7 @@ import (
) )
func TestApplyCodexOAuthTransform_ToolContinuationPreservesInput(t *testing.T) { func TestApplyCodexOAuthTransform_ToolContinuationPreservesInput(t *testing.T) {
// 续链场景:保留 item_reference 与 id并启用 store。 // 续链场景:保留 item_reference 与 id但不再强制 store=true。
setupCodexCache(t) setupCodexCache(t)
reqBody := map[string]any{ reqBody := map[string]any{
@@ -25,9 +25,10 @@ func TestApplyCodexOAuthTransform_ToolContinuationPreservesInput(t *testing.T) {
applyCodexOAuthTransform(reqBody) applyCodexOAuthTransform(reqBody)
// 未显式设置 store=true默认为 false。
store, ok := reqBody["store"].(bool) store, ok := reqBody["store"].(bool)
require.True(t, ok) require.True(t, ok)
require.True(t, store) require.False(t, store)
input, ok := reqBody["input"].([]any) input, ok := reqBody["input"].([]any)
require.True(t, ok) require.True(t, ok)
@@ -45,8 +46,8 @@ func TestApplyCodexOAuthTransform_ToolContinuationPreservesInput(t *testing.T) {
require.Equal(t, "o1", second["id"]) require.Equal(t, "o1", second["id"])
} }
func TestApplyCodexOAuthTransform_ToolContinuationForcesStoreTrue(t *testing.T) { func TestApplyCodexOAuthTransform_ExplicitStoreFalsePreserved(t *testing.T) {
// 续链场景:显式 store=false 也会被强制为 true。 // 续链场景:显式 store=false 不再强制为 true,保持 false
setupCodexCache(t) setupCodexCache(t)
reqBody := map[string]any{ reqBody := map[string]any{
@@ -62,16 +63,35 @@ func TestApplyCodexOAuthTransform_ToolContinuationForcesStoreTrue(t *testing.T)
store, ok := reqBody["store"].(bool) store, ok := reqBody["store"].(bool)
require.True(t, ok) require.True(t, ok)
require.True(t, store) require.False(t, store)
} }
func TestApplyCodexOAuthTransform_NonContinuationForcesStoreFalseAndStripsIDs(t *testing.T) { func TestApplyCodexOAuthTransform_ExplicitStoreTrueForcedFalse(t *testing.T) {
// 非续链场景:强制 store=false并移除 input 中的 id // 显式 store=true 也会强制为 false
setupCodexCache(t) setupCodexCache(t)
reqBody := map[string]any{ reqBody := map[string]any{
"model": "gpt-5.1", "model": "gpt-5.1",
"store": true, "store": true,
"input": []any{
map[string]any{"type": "function_call_output", "call_id": "call_1"},
},
"tool_choice": "auto",
}
applyCodexOAuthTransform(reqBody)
store, ok := reqBody["store"].(bool)
require.True(t, ok)
require.False(t, store)
}
func TestApplyCodexOAuthTransform_NonContinuationDefaultsStoreFalseAndStripsIDs(t *testing.T) {
// 非续链场景:未设置 store 时默认 false并移除 input 中的 id。
setupCodexCache(t)
reqBody := map[string]any{
"model": "gpt-5.1",
"input": []any{ "input": []any{
map[string]any{"type": "text", "id": "t1", "text": "hi"}, map[string]any{"type": "text", "id": "t1", "text": "hi"},
}, },