mirror of
https://github.com/router-for-me/CLIProxyAPI.git
synced 2026-10-28 02:21:59 +08:00
fix(codex): reconcile replayed web_search_call history with tool declarations
- Inject a web_search tool declaration for standard Codex requests when input replays web_search_call items without a web_search tool so upstream response protection remains available during compaction. - Strip replayed web_search_call items from Responses Lite requests where top-level hosted tool declarations are omitted. Closes: #6770
This commit is contained in:
@@ -152,3 +152,284 @@ func TestCodexExecutorCompactUseSIWCAppliesSIWCRulesBeforePayloadFinalizer(t *te
|
||||
t.Fatalf("PayloadFinalizer temperature override = %v, want 0.25; body=%s", gotTemp, gotBody)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCodexExecutorReplayedWebSearchCallHistoryEnsuresWebSearchTool_Issue6770(t *testing.T) {
|
||||
hasWebSearchCallInInput := func(body []byte) bool {
|
||||
for _, item := range gjson.GetBytes(body, "input").Array() {
|
||||
if item.Get("type").String() == "web_search_call" {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
countWebSearchTools := func(body []byte) int {
|
||||
count := 0
|
||||
for _, tool := range gjson.GetBytes(body, "tools").Array() {
|
||||
if tool.Get("type").String() == "web_search" {
|
||||
count++
|
||||
}
|
||||
}
|
||||
return count
|
||||
}
|
||||
|
||||
compactionPayloadWithEmptyTools := `{"model":"gpt-6.1-sol","input":[{"type":"message","role":"user","content":[{"type":"input_text","text":"search docs"}]},{"id":"ws_1","type":"web_search_call","status":"completed","action":{"type":"search","query":"openai responses web search"}},{"id":"ws_2","type":"web_search_call","status":"completed","action":{"type":"open_page","url":"https://platform.openai.com/docs"}},{"type":"message","role":"assistant","content":[{"type":"output_text","text":"Found docs."}]},{"type":"message","role":"user","content":[{"type":"input_text","text":"Summarize conversation"}]}],"tools":[],"tool_choice":"auto"}`
|
||||
|
||||
t.Run("ExecuteStream with empty tools injects web_search and avoids 502 response protection error", func(t *testing.T) {
|
||||
var gotBody []byte
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
body, _ := io.ReadAll(r.Body)
|
||||
gotBody = body
|
||||
w.Header().Set("Content-Type", "text/event-stream")
|
||||
if hasWebSearchCallInInput(body) && countWebSearchTools(body) == 0 {
|
||||
_, _ = w.Write([]byte("data: {\"type\":\"error\",\"error\":{\"message\":\"response protection is unavailable\",\"type\":\"internal_error\",\"param\":null,\"code\":null}}\n\n"))
|
||||
return
|
||||
}
|
||||
_, _ = w.Write([]byte("data: {\"type\":\"response.completed\",\"response\":{\"id\":\"resp_1\",\"object\":\"response\",\"status\":\"completed\",\"output\":[{\"type\":\"message\",\"role\":\"assistant\",\"content\":[{\"type\":\"output_text\",\"text\":\"summary\"}]}],\"usage\":{\"input_tokens\":10,\"output_tokens\":5,\"total_tokens\":15}}}\n\n"))
|
||||
}))
|
||||
defer server.Close()
|
||||
|
||||
executor := NewCodexExecutor(&config.Config{Codex: config.CodexConfig{StreamBootstrapBuffering: true}})
|
||||
auth := &cliproxyauth.Auth{
|
||||
Provider: "codex",
|
||||
Attributes: map[string]string{
|
||||
"base_url": server.URL,
|
||||
"api_key": "test",
|
||||
"plan_type": "plus",
|
||||
},
|
||||
}
|
||||
|
||||
streamResult, err := executor.ExecuteStream(context.Background(), auth, cliproxyexecutor.Request{
|
||||
Model: "gpt-6.1-sol",
|
||||
Payload: []byte(compactionPayloadWithEmptyTools),
|
||||
}, cliproxyexecutor.Options{
|
||||
SourceFormat: sdktranslator.FromString("openai-response"),
|
||||
Stream: true,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("ExecuteStream() error = %v", err)
|
||||
}
|
||||
for chunk := range streamResult.Chunks {
|
||||
if chunk.Err != nil {
|
||||
t.Fatalf("stream chunk error = %v", chunk.Err)
|
||||
}
|
||||
}
|
||||
if got := countWebSearchTools(gotBody); got != 1 {
|
||||
t.Fatalf("web_search tool count = %d, want 1; body=%s", got, gotBody)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("Execute non-stream with image generation disabled still injects web_search", func(t *testing.T) {
|
||||
var gotBody []byte
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
body, _ := io.ReadAll(r.Body)
|
||||
gotBody = body
|
||||
w.Header().Set("Content-Type", "text/event-stream")
|
||||
if hasWebSearchCallInInput(body) && countWebSearchTools(body) == 0 {
|
||||
_, _ = w.Write([]byte("data: {\"type\":\"error\",\"error\":{\"message\":\"response protection is unavailable\",\"type\":\"internal_error\",\"param\":null,\"code\":null}}\n\n"))
|
||||
return
|
||||
}
|
||||
_, _ = w.Write([]byte("data: {\"type\":\"response.completed\",\"response\":{\"id\":\"resp_1\",\"object\":\"response\",\"status\":\"completed\",\"output\":[],\"usage\":{\"input_tokens\":10,\"output_tokens\":5,\"total_tokens\":15}}}\n\n"))
|
||||
}))
|
||||
defer server.Close()
|
||||
|
||||
executor := NewCodexExecutor(&config.Config{SDKConfig: config.SDKConfig{DisableImageGeneration: config.DisableImageGenerationAll}})
|
||||
auth := &cliproxyauth.Auth{
|
||||
Provider: "codex",
|
||||
Attributes: map[string]string{
|
||||
"base_url": server.URL,
|
||||
"api_key": "test",
|
||||
},
|
||||
}
|
||||
|
||||
_, err := executor.Execute(context.Background(), auth, cliproxyexecutor.Request{
|
||||
Model: "gpt-6.1-sol",
|
||||
Payload: []byte(compactionPayloadWithEmptyTools),
|
||||
}, cliproxyexecutor.Options{
|
||||
SourceFormat: sdktranslator.FromString("openai-response"),
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("Execute() error = %v", err)
|
||||
}
|
||||
if got := countWebSearchTools(gotBody); got != 1 {
|
||||
t.Fatalf("web_search tool count = %d, want 1; body=%s", got, gotBody)
|
||||
}
|
||||
if got := len(gjson.GetBytes(gotBody, "tools").Array()); got != 1 {
|
||||
t.Fatalf("tools len = %d, want 1; body=%s", got, gotBody)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("Execute compact endpoint injects web_search when input replays web_search_call", func(t *testing.T) {
|
||||
var gotBody []byte
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
body, _ := io.ReadAll(r.Body)
|
||||
gotBody = body
|
||||
if hasWebSearchCallInInput(body) && countWebSearchTools(body) == 0 {
|
||||
w.WriteHeader(http.StatusBadGateway)
|
||||
_, _ = w.Write([]byte(`{"error":{"message":"response protection is unavailable","type":"internal_error","param":null,"code":null}}`))
|
||||
return
|
||||
}
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
_, _ = w.Write([]byte(`{"id":"resp_compact_1","object":"response.compaction","usage":{"input_tokens":10,"output_tokens":2,"total_tokens":12}}`))
|
||||
}))
|
||||
defer server.Close()
|
||||
|
||||
executor := NewCodexExecutor(&config.Config{})
|
||||
auth := &cliproxyauth.Auth{
|
||||
Provider: "codex",
|
||||
Attributes: map[string]string{
|
||||
"base_url": server.URL,
|
||||
"api_key": "test",
|
||||
},
|
||||
}
|
||||
|
||||
compactPayload := `{"model":"gpt-6.1-sol","input":[{"type":"message","role":"user","content":"history"},{"id":"ws_1","type":"web_search_call","status":"completed","action":{"type":"search","query":"q"}},{"type":"compaction_trigger"}]}`
|
||||
_, err := executor.Execute(context.Background(), auth, cliproxyexecutor.Request{
|
||||
Model: "gpt-6.1-sol",
|
||||
Payload: []byte(compactPayload),
|
||||
}, cliproxyexecutor.Options{
|
||||
SourceFormat: sdktranslator.FromString("openai-response"),
|
||||
Alt: "responses/compact",
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("Execute compact error = %v", err)
|
||||
}
|
||||
if got := countWebSearchTools(gotBody); got != 1 {
|
||||
t.Fatalf("compact web_search tool count = %d, want 1; body=%s", got, gotBody)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("prepareCodexWebsocketStream injects web_search when input replays web_search_call", func(t *testing.T) {
|
||||
wsExecutor := NewCodexWebsocketsExecutor(&config.Config{})
|
||||
auth := &cliproxyauth.Auth{
|
||||
Provider: "codex",
|
||||
Attributes: map[string]string{
|
||||
"base_url": "http://127.0.0.1:1",
|
||||
"api_key": "test",
|
||||
"plan_type": "plus",
|
||||
},
|
||||
}
|
||||
prepared, err := wsExecutor.prepareCodexWebsocketStream(context.Background(), auth, cliproxyexecutor.Request{
|
||||
Model: "gpt-6.1-sol",
|
||||
Payload: []byte(compactionPayloadWithEmptyTools),
|
||||
}, cliproxyexecutor.Options{
|
||||
SourceFormat: sdktranslator.FromString("openai-response"),
|
||||
Stream: true,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("prepareCodexWebsocketStream() error = %v", err)
|
||||
}
|
||||
if got := countWebSearchTools(prepared.clientBody); got != 1 {
|
||||
t.Fatalf("websocket clientBody web_search tool count = %d, want 1; body=%s", got, prepared.clientBody)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("existing web_search tool declaration is preserved without duplication", func(t *testing.T) {
|
||||
payloadWithWebSearch := `{"model":"gpt-6.1-sol","input":[{"id":"ws_1","type":"web_search_call","status":"completed","action":{"type":"search","query":"q"}}],"tools":[{"type":"web_search","external_web_access":true}]}`
|
||||
wsExecutor := NewCodexWebsocketsExecutor(&config.Config{SDKConfig: config.SDKConfig{DisableImageGeneration: config.DisableImageGenerationAll}})
|
||||
auth := &cliproxyauth.Auth{
|
||||
Provider: "codex",
|
||||
Attributes: map[string]string{
|
||||
"base_url": "http://127.0.0.1:1",
|
||||
"api_key": "test",
|
||||
},
|
||||
}
|
||||
prepared, err := wsExecutor.prepareCodexWebsocketStream(context.Background(), auth, cliproxyexecutor.Request{
|
||||
Model: "gpt-6.1-sol",
|
||||
Payload: []byte(payloadWithWebSearch),
|
||||
}, cliproxyexecutor.Options{
|
||||
SourceFormat: sdktranslator.FromString("openai-response"),
|
||||
Stream: true,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("prepareCodexWebsocketStream() error = %v", err)
|
||||
}
|
||||
if got := countWebSearchTools(prepared.clientBody); got != 1 {
|
||||
t.Fatalf("web_search tool count = %d, want 1; body=%s", got, prepared.clientBody)
|
||||
}
|
||||
if !gjson.GetBytes(prepared.clientBody, "tools.0.external_web_access").Bool() {
|
||||
t.Fatalf("existing web_search options not preserved: %s", prepared.clientBody)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("Responses Lite request strips replayed web_search_call items without injecting top-level tools", func(t *testing.T) {
|
||||
litePayload := `{"model":"gpt-6.1-sol","input":[{"type":"additional_tools","role":"developer","tools":[{"type":"namespace","name":"functions","description":"","tools":[{"type":"custom","name":"exec"}]}]},{"type":"message","role":"user","content":[{"type":"input_text","text":"search"}]},{"id":"ws_1","type":"web_search_call","status":"completed","action":{"type":"search","query":"q"}},{"id":"rs_1","type":"reasoning","summary":[]},{"type":"message","role":"assistant","content":[{"type":"output_text","text":"answer"}]},{"type":"compaction_trigger"}],"client_metadata":{"ws_request_header_x_openai_internal_codex_responses_lite":"true"}}`
|
||||
wsExecutor := NewCodexWebsocketsExecutor(&config.Config{})
|
||||
auth := &cliproxyauth.Auth{
|
||||
Provider: "codex",
|
||||
Attributes: map[string]string{
|
||||
"base_url": "http://127.0.0.1:1",
|
||||
"api_key": "test",
|
||||
},
|
||||
}
|
||||
headers := make(http.Header)
|
||||
headers.Set(codexResponsesLiteHeader, "true")
|
||||
|
||||
prepared, err := wsExecutor.prepareCodexWebsocketStream(context.Background(), auth, cliproxyexecutor.Request{
|
||||
Model: "gpt-6.1-sol",
|
||||
Payload: []byte(litePayload),
|
||||
}, cliproxyexecutor.Options{
|
||||
SourceFormat: sdktranslator.FromString("openai-response"),
|
||||
Headers: headers,
|
||||
Stream: true,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("prepareCodexWebsocketStream() error = %v", err)
|
||||
}
|
||||
if gjson.GetBytes(prepared.clientBody, "tools").Exists() {
|
||||
t.Fatalf("Responses Lite request must not inject top-level tools: %s", prepared.clientBody)
|
||||
}
|
||||
if hasWebSearchCallInInput(prepared.clientBody) {
|
||||
t.Fatalf("Responses Lite request must strip replayed web_search_call items: %s", prepared.clientBody)
|
||||
}
|
||||
inputItems := gjson.GetBytes(prepared.clientBody, "input").Array()
|
||||
if len(inputItems) != 5 {
|
||||
t.Fatalf("input len = %d, want 5; body=%s", len(inputItems), prepared.clientBody)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("PayloadFinalizer runs after ensureWebSearchToolForHistory", func(t *testing.T) {
|
||||
var gotBody []byte
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
body, _ := io.ReadAll(r.Body)
|
||||
gotBody = body
|
||||
w.Header().Set("Content-Type", "text/event-stream")
|
||||
_, _ = w.Write([]byte("data: {\"type\":\"response.completed\",\"response\":{\"id\":\"resp_1\",\"object\":\"response\",\"status\":\"completed\",\"output\":[],\"usage\":{\"input_tokens\":10,\"output_tokens\":5,\"total_tokens\":15}}}\n\n"))
|
||||
}))
|
||||
defer server.Close()
|
||||
|
||||
cfg := &config.Config{
|
||||
SDKConfig: config.SDKConfig{
|
||||
DisableImageGeneration: config.DisableImageGenerationAll,
|
||||
},
|
||||
Payload: config.PayloadConfig{
|
||||
Filter: []config.PayloadFilterRule{
|
||||
{
|
||||
Models: []config.PayloadModelRule{{Name: "gpt-6.1-sol", Protocol: "codex"}},
|
||||
Params: []string{"tools"},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
executor := NewCodexExecutor(cfg)
|
||||
auth := &cliproxyauth.Auth{
|
||||
Provider: "codex",
|
||||
Attributes: map[string]string{
|
||||
"base_url": server.URL,
|
||||
"api_key": "test",
|
||||
},
|
||||
}
|
||||
|
||||
_, err := executor.Execute(context.Background(), auth, cliproxyexecutor.Request{
|
||||
Model: "gpt-6.1-sol",
|
||||
Payload: []byte(compactionPayloadWithEmptyTools),
|
||||
}, cliproxyexecutor.Options{
|
||||
SourceFormat: sdktranslator.FromString("openai-response"),
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("Execute() error = %v", err)
|
||||
}
|
||||
if gjson.GetBytes(gotBody, "tools").Exists() {
|
||||
t.Fatalf("expected user payload filter rule to remove tools after built-in injection, got body=%s", gotBody)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user