From 714f052618c8a8c017efe81a97aa51311d96ddcd Mon Sep 17 00:00:00 2001 From: Luis Pater Date: Tue, 10 Nov 2026 04:00:07 +0800 Subject: [PATCH] fix(codex): allow hosted image_generation injection on standard Responses without reasoning - gate automatic image_generation tool injection for use_responses_lite catalog models on actual Responses-Lite routing (including credential and model header overrides) or presence of reasoning effort - retract untouched auto-injected image_generation tools during single-pass payload finalization when user payload rules configure reasoning effort or switch to an incompatible route - update Codex executor and payload barrier regression tests across HTTP and WebSocket streaming and non-streaming paths Closes: #7384 Co-authored-by: Dylan <1990016@gmail.com> --- .../executor/codex_executor_execute.go | 8 +- .../executor/codex_executor_imagegen_test.go | 526 +++++++++++++++++- .../executor/codex_executor_request.go | 62 ++- .../runtime/executor/codex_executor_stream.go | 8 +- .../executor/codex_websockets_execute.go | 8 +- .../executor/codex_websockets_stream.go | 8 +- .../executor/helps/codex_hosted_images.go | 77 ++- .../executor/helps/payload_finalizer.go | 16 +- .../runtime/executor/helps/payload_helpers.go | 186 ++++++- .../runtime/executor/payload_barrier_test.go | 9 +- 10 files changed, 863 insertions(+), 45 deletions(-) diff --git a/internal/runtime/executor/codex_executor_execute.go b/internal/runtime/executor/codex_executor_execute.go index 2750ed4b3e..dc3f3c5cbb 100644 --- a/internal/runtime/executor/codex_executor_execute.go +++ b/internal/runtime/executor/codex_executor_execute.go @@ -65,7 +65,7 @@ func (e *CodexExecutor) Execute(ctx context.Context, auth *cliproxyauth.Auth, re return resp, err } - ctx = helps.WithPayloadFinalizer(ctx, helps.NewPayloadFinalizer(e.cfg, e.Identifier(), baseModel, to.String(), "", originalTranslated, req, opts)) + ctx = helps.WithPayloadFinalizer(ctx, helps.NewPayloadFinalizer(e.cfg, e.Identifier(), baseModel, to.String(), "", originalTranslated, req, opts, auth)) helps.SetStringIfDifferentDoc(doc, "model", baseModel) helps.SetBoolIfDifferentDoc(doc, "stream", true) _ = doc.Delete("previous_response_id") @@ -80,7 +80,11 @@ func (e *CodexExecutor) Execute(ctx context.Context, auth *cliproxyauth.Auth, re if !useSIWC { normalizeCodexInstructionsDoc(doc, helps.IsNativeCodexRequest(req.Payload, opts)) if e.cfg == nil || e.cfg.DisableImageGeneration == config.DisableImageGenerationOff { - ensureImageGenerationToolDoc(doc, baseModel, auth, opts.Headers) + checkModel := req.Model + if strings.TrimSpace(checkModel) == "" { + checkModel = baseModel + } + ensureImageGenerationToolDoc(doc, checkModel, auth, opts.Headers) if !codexAuthUsesAPIKey(auth) { normalizeCodexImageGenerationToolsDoc(doc) } diff --git a/internal/runtime/executor/codex_executor_imagegen_test.go b/internal/runtime/executor/codex_executor_imagegen_test.go index 405a2d158d..a43243ee9b 100644 --- a/internal/runtime/executor/codex_executor_imagegen_test.go +++ b/internal/runtime/executor/codex_executor_imagegen_test.go @@ -11,6 +11,8 @@ import ( "github.com/MoeForge/moego/ojson" "github.com/gorilla/websocket" "github.com/router-for-me/CLIProxyAPI/v8/internal/config" + "github.com/router-for-me/CLIProxyAPI/v8/internal/registry" + "github.com/router-for-me/CLIProxyAPI/v8/internal/runtime/executor/helps" cliproxyauth "github.com/router-for-me/CLIProxyAPI/v8/sdk/cliproxy/auth" cliproxyexecutor "github.com/router-for-me/CLIProxyAPI/v8/sdk/cliproxy/executor" sdktranslator "github.com/router-for-me/CLIProxyAPI/v8/sdk/translator" @@ -152,8 +154,14 @@ func TestEnsureImageGenerationTool_ResponsesLiteFalseMetadataStillInjectsTool(t liteHeaders := make(http.Header) liteHeaders.Set(codexResponsesLiteHeader, "false") liteResult := ensureImageGenerationTool(liteBody, liteModel, nil, liteHeaders) - if ojson.GetBytes(liteResult, "tools").Exists() { - t.Fatalf("for %s: expected no auto-injected tools even when responses-lite is explicitly false, got %s", liteModel, liteResult) + if got := ojson.GetBytes(liteResult, "tools.0.type").String(); got != "image_generation" { + t.Fatalf("for %s: tools.0.type = %q, want image_generation when responses-lite is false and reasoning is absent; body=%s", liteModel, got, liteResult) + } + + reasoningBody := []byte(`{"model":"` + liteModel + `","reasoning":{"effort":"medium"},"client_metadata":{"ws_request_header_x_openai_internal_codex_responses_lite":"false"},"input":"hello"}`) + reasoningResult := ensureImageGenerationTool(reasoningBody, liteModel, nil, liteHeaders) + if ojson.GetBytes(reasoningResult, "tools").Exists() { + t.Fatalf("for %s: expected no auto-injected tools when reasoning.effort is present, got %s", liteModel, reasoningResult) } } } @@ -300,7 +308,7 @@ func TestEnsureImageGenerationTool_FreeCodexAuthDoesNotInjectTool(t *testing.T) } func TestEnsureImageGenerationTool_ResponsesLiteModelsDoNotInjectToolByDefault(t *testing.T) { - for _, model := range []string{ + liteModels := []string{ "gpt-6-astra", "gpt-6-sol", "gpt-6.1-sol", @@ -312,21 +320,56 @@ func TestEnsureImageGenerationTool_ResponsesLiteModelsDoNotInjectToolByDefault(t "gpt-5.6-luna", "gpt-reserve", "codex-auto-review", + } + + for _, model := range liteModels { + t.Run("standard_without_reasoning/"+model, func(t *testing.T) { + body := []byte(`{"model":"` + model + `","input":"hello"}`) + result := ensureImageGenerationTool(body, model, nil, nil) + if got := ojson.GetBytes(result, "tools.0.type").String(); got != "image_generation" { + t.Fatalf("for %s without reasoning: tools.0.type = %q, want image_generation; body=%s", model, got, result) + } + + withFunctionTool := []byte(`{"model":"` + model + `","input":"Generate a photorealistic image of a mountain lake at sunset.","tools":[{"type":"function","name":"get_weather","description":"Get current weather","parameters":{"type":"object","properties":{"city":{"type":"string"}}}}]}`) + withFunctionResult := ensureImageGenerationTool(withFunctionTool, model, nil, nil) + if got := ojson.GetBytes(withFunctionResult, "tools.1.type").String(); got != "image_generation" { + t.Fatalf("for %s with function tool and no reasoning: tools.1.type = %q, want image_generation; body=%s", model, got, withFunctionResult) + } + }) + + t.Run("with_reasoning_effort/"+model, func(t *testing.T) { + body := []byte(`{"model":"` + model + `","reasoning":{"effort":"medium"},"input":"hello"}`) + result := ensureImageGenerationTool(body, model, nil, nil) + if ojson.GetBytes(result, "tools").Exists() { + t.Fatalf("expected no auto-injected tools for %s with reasoning.effort, got %s", model, result) + } + }) + + t.Run("with_configuration_update_reasoning/"+model, func(t *testing.T) { + body := []byte(`{"model":"` + model + `","input":[{"type":"configuration_update","reasoning":{"effort":"high"}},{"type":"message","role":"user","content":[{"type":"input_text","text":"hello"}]}]}`) + result := ensureImageGenerationTool(body, model, nil, nil) + if ojson.GetBytes(result, "tools").Exists() { + t.Fatalf("expected no auto-injected tools for %s with configuration_update reasoning, got %s", model, result) + } + }) + } + + for _, suffixedModel := range []string{ "codex/gpt-6-astra(medium)", "codex/gpt-5.6-sol(high)", } { - t.Run(model, func(t *testing.T) { - body := []byte(`{"model":"` + model + `","input":"hello"}`) - result := ensureImageGenerationTool(body, model, nil, nil) + t.Run("with_thinking_suffix/"+suffixedModel, func(t *testing.T) { + body := []byte(`{"model":"` + suffixedModel + `","input":"hello"}`) + result := ensureImageGenerationTool(body, suffixedModel, nil, nil) if ojson.GetBytes(result, "tools").Exists() { - t.Fatalf("expected no auto-injected tools for %s, got %s", model, result) + t.Fatalf("expected no auto-injected tools for %s, got %s", suffixedModel, result) } }) } for _, nonLiteModel := range []string{"gpt-5.5", "gpt-5.4"} { t.Run("non-lite/"+nonLiteModel, func(t *testing.T) { - body := []byte(`{"model":"` + nonLiteModel + `","input":"hello"}`) + body := []byte(`{"model":"` + nonLiteModel + `","reasoning":{"effort":"medium"},"input":"hello"}`) result := ensureImageGenerationTool(body, nonLiteModel, nil, nil) if got := ojson.GetBytes(result, "tools.0.type").String(); got != "image_generation" { t.Fatalf("for %s: tools.0.type = %q, want image_generation; body=%s", nonLiteModel, got, result) @@ -335,6 +378,473 @@ func TestEnsureImageGenerationTool_ResponsesLiteModelsDoNotInjectToolByDefault(t } } +func TestCodexExecutorStandardResponsesInjectsImageGenerationWithoutReasoningAndSkipsWithReasoning(t *testing.T) { + for _, transport := range []string{"http", "websocket"} { + for _, stream := range []bool{false, true} { + streamLabel := "false" + if stream { + streamLabel = "true" + } + for _, tc := range []struct { + name string + model string + sourceFmt sdktranslator.Format + payload string + reqMetadata map[string]any + reqHeaders http.Header + authAttrs map[string]string + cfg config.Config + overrideHeaders map[string]string + wantTools int + wantImgIdx string + wantToolType string + wantEffort string + }{ + { + name: "standard_responses_with_function_tool_no_reasoning", + model: "gpt-6-astra", + sourceFmt: sdktranslator.FormatOpenAIResponse, + payload: `{"model":"gpt-6-astra","input":"Generate a photorealistic image of a mountain lake at sunset.","tools":[{"type":"function","name":"get_weather","description":"Get current weather","parameters":{"type":"object","properties":{"city":{"type":"string"}}}}]}`, + wantTools: 2, + wantImgIdx: "tools.1.type", + }, + { + name: "standard_responses_bare_no_reasoning", + model: "gpt-6-astra", + sourceFmt: sdktranslator.FormatOpenAIResponse, + payload: `{"model":"gpt-6-astra","input":"Generate a photorealistic image of a mountain lake at sunset."}`, + wantTools: 1, + wantImgIdx: "tools.0.type", + }, + { + name: "standard_responses_with_reasoning_effort", + model: "gpt-6-astra", + sourceFmt: sdktranslator.FormatOpenAIResponse, + payload: `{"model":"gpt-6-astra","reasoning":{"effort":"medium"},"input":"Generate a photorealistic image of a mountain lake at sunset."}`, + wantTools: 0, + }, + { + name: "standard_responses_with_model_thinking_suffix", + model: "gpt-6-astra(medium)", + sourceFmt: sdktranslator.FormatOpenAIResponse, + payload: `{"model":"gpt-6-astra(medium)","input":"Generate a photorealistic image of a mountain lake at sunset."}`, + wantTools: 0, + }, + { + name: "openai_chat_completions_defaults_reasoning_effort", + model: "gpt-6-astra", + sourceFmt: sdktranslator.FormatOpenAI, + payload: `{"model":"gpt-6-astra","messages":[{"role":"user","content":"Generate a photorealistic image of a mountain lake at sunset."}]}`, + wantTools: 0, + }, + { + name: "payload_default_sets_reasoning_effort_skips_auto_injection", + model: "gpt-6-astra", + sourceFmt: sdktranslator.FormatOpenAIResponse, + payload: `{"model":"gpt-6-astra","input":"hello"}`, + cfg: config.Config{ + Payload: config.PayloadConfig{ + Default: []config.PayloadRule{ + { + Models: []config.PayloadModelRule{{Name: "gpt-6-astra"}}, + Params: map[string]any{"reasoning.effort": "medium"}, + }, + }, + }, + }, + wantTools: 0, + wantEffort: "medium", + }, + { + name: "payload_override_sets_reasoning_effort_with_function_tool_skips_auto_injection", + model: "gpt-6-astra", + sourceFmt: sdktranslator.FormatOpenAIResponse, + payload: `{"model":"gpt-6-astra","input":"hello","tools":[{"type":"function","name":"get_weather","description":"Get current weather","parameters":{"type":"object","properties":{"city":{"type":"string"}}}}]}`, + cfg: config.Config{ + Payload: config.PayloadConfig{ + Override: []config.PayloadRule{ + { + Models: []config.PayloadModelRule{{Name: "gpt-6-astra"}}, + Params: map[string]any{"reasoning.effort": "high"}, + }, + }, + }, + }, + wantTools: 1, + wantImgIdx: "tools.0.type", + wantToolType: "function", + wantEffort: "high", + }, + { + name: "payload_conditional_exist_tools_sets_reasoning_effort_skips_auto_injection", + model: "gpt-6-astra", + sourceFmt: sdktranslator.FormatOpenAIResponse, + payload: `{"model":"gpt-6-astra","input":"hello"}`, + cfg: config.Config{ + Payload: config.PayloadConfig{ + Override: []config.PayloadRule{ + { + Models: []config.PayloadModelRule{{Name: "gpt-6-astra", Exist: []string{"tools"}}}, + Params: map[string]any{"reasoning.effort": "medium"}, + }, + }, + }, + }, + wantTools: 0, + wantEffort: "medium", + }, + { + name: "payload_conditional_exist_prompt_cache_key_sets_reasoning_effort_skips_auto_injection", + model: "gpt-6-astra", + sourceFmt: sdktranslator.FormatOpenAIResponse, + payload: `{"model":"gpt-6-astra","input":"hello"}`, + reqMetadata: map[string]any{cliproxyexecutor.ExecutionSessionMetadataKey: "sess-prompt-cache-7384"}, + cfg: config.Config{ + Payload: config.PayloadConfig{ + Override: []config.PayloadRule{ + { + Models: []config.PayloadModelRule{{Name: "gpt-6-astra", Exist: []string{"prompt_cache_key"}}}, + Params: map[string]any{"reasoning.effort": "medium"}, + }, + }, + }, + }, + wantTools: 0, + wantEffort: "medium", + }, + { + name: "payload_override_switches_to_lite_model_with_reasoning_skips_auto_injection", + model: "gpt-5.4", + sourceFmt: sdktranslator.FormatOpenAIResponse, + payload: `{"model":"gpt-5.4","reasoning":{"effort":"medium"},"input":"hello"}`, + cfg: config.Config{ + Payload: config.PayloadConfig{ + Override: []config.PayloadRule{ + { + Models: []config.PayloadModelRule{{Name: "gpt-5.4"}}, + Params: map[string]any{"model": "gpt-6-astra"}, + }, + }, + }, + }, + wantTools: 0, + wantEffort: "medium", + }, + { + name: "model_override_header_enables_responses_lite_skips_auto_injection", + model: "gpt-6-astra", + sourceFmt: sdktranslator.FormatOpenAIResponse, + payload: `{"model":"gpt-6-astra","input":"hello"}`, + overrideHeaders: map[string]string{codexResponsesLiteHeader: "true"}, + wantTools: 0, + }, + { + name: "payload_override_sets_reasoning_effort_and_tool_choice_auto_skips_auto_injection", + model: "gpt-6-astra", + sourceFmt: sdktranslator.FormatOpenAIResponse, + payload: `{"model":"gpt-6-astra","input":"hello"}`, + cfg: config.Config{ + Payload: config.PayloadConfig{ + Override: []config.PayloadRule{ + { + Models: []config.PayloadModelRule{{Name: "gpt-6-astra"}}, + Params: map[string]any{ + "reasoning.effort": "medium", + "tool_choice": "auto", + }, + }, + }, + }, + }, + wantTools: 0, + wantEffort: "medium", + }, + { + name: "client_lite_header_overridden_false_by_auth_with_payload_rule_keeps_auto_injection", + model: "gpt-6-astra", + sourceFmt: sdktranslator.FormatOpenAIResponse, + payload: `{"model":"gpt-6-astra","input":"hello"}`, + reqHeaders: http.Header{codexResponsesLiteHeader: []string{"true"}}, + authAttrs: map[string]string{"header:" + codexResponsesLiteHeader: "false"}, + cfg: config.Config{ + Payload: config.PayloadConfig{ + Override: []config.PayloadRule{ + { + Models: []config.PayloadModelRule{{Name: "gpt-6-astra"}}, + Params: map[string]any{"service_tier": "default"}, + }, + }, + }, + }, + wantTools: 1, + wantImgIdx: "tools.0.type", + }, + { + name: "payload_default_image_tool_choice_overridden_to_auto_with_reasoning_skips_auto_injection", + model: "gpt-6-astra", + sourceFmt: sdktranslator.FormatOpenAIResponse, + payload: `{"model":"gpt-6-astra","input":"hello"}`, + cfg: config.Config{ + Payload: config.PayloadConfig{ + Default: []config.PayloadRule{ + { + Models: []config.PayloadModelRule{{Name: "gpt-6-astra"}}, + Params: map[string]any{"tool_choice": map[string]any{"type": "image_generation"}}, + }, + }, + Override: []config.PayloadRule{ + { + Models: []config.PayloadModelRule{{Name: "gpt-6-astra"}}, + Params: map[string]any{ + "tool_choice": "auto", + "reasoning.effort": "medium", + }, + }, + }, + }, + }, + wantTools: 0, + wantEffort: "medium", + }, + { + name: "auth_lite_header_true_overridden_false_by_initial_model_then_switched_to_model_without_override_skips_auto_injection", + model: "gpt-6-astra", + sourceFmt: sdktranslator.FormatOpenAIResponse, + payload: `{"model":"gpt-6-astra","input":"hello"}`, + authAttrs: map[string]string{"header:" + codexResponsesLiteHeader: "true"}, + overrideHeaders: map[string]string{codexResponsesLiteHeader: "false"}, + cfg: config.Config{ + Payload: config.PayloadConfig{ + Override: []config.PayloadRule{ + { + Models: []config.PayloadModelRule{{Name: "gpt-6-astra"}}, + Params: map[string]any{"model": "gpt-5.5"}, + }, + }, + }, + }, + wantTools: 0, + }, + { + name: "payload_override_function_tool_then_filter_deletes_it_with_reasoning_skips_auto_injection", + model: "gpt-6-astra", + sourceFmt: sdktranslator.FormatOpenAIResponse, + payload: `{"model":"gpt-6-astra","input":"hello","tools":[{"type":"function","name":"get_weather","description":"Get current weather","parameters":{"type":"object"}}]}`, + cfg: config.Config{ + Payload: config.PayloadConfig{ + Override: []config.PayloadRule{ + { + Models: []config.PayloadModelRule{{Name: "gpt-6-astra"}}, + Params: map[string]any{ + "tools.0.description": "Updated weather", + "reasoning.effort": "medium", + }, + }, + }, + Filter: []config.PayloadFilterRule{ + { + Models: []config.PayloadModelRule{{Name: "gpt-6-astra"}}, + Params: []string{"tools.0"}, + }, + }, + }, + }, + wantTools: 0, + wantEffort: "medium", + }, + { + name: "payload_override_image_tool_then_filter_deletes_earlier_tool_with_reasoning_keeps_configured_image_tool", + model: "gpt-6-astra", + sourceFmt: sdktranslator.FormatOpenAIResponse, + payload: `{"model":"gpt-6-astra","input":"hello","tools":[{"type":"function","name":"get_weather","description":"Get current weather","parameters":{"type":"object"}}]}`, + cfg: config.Config{ + Payload: config.PayloadConfig{ + Override: []config.PayloadRule{ + { + Models: []config.PayloadModelRule{{Name: "gpt-6-astra"}}, + Params: map[string]any{ + "tools.1.output_format": "webp", + "reasoning.effort": "medium", + }, + }, + }, + Filter: []config.PayloadFilterRule{ + { + Models: []config.PayloadModelRule{{Name: "gpt-6-astra"}}, + Params: []string{"tools.0"}, + }, + }, + }, + }, + wantTools: 1, + wantImgIdx: "tools.0.type", + wantEffort: "medium", + }, + { + name: "payload_override_raw_escaped_configuration_update_reasoning_skips_auto_injection", + model: "gpt-6-astra", + sourceFmt: sdktranslator.FormatOpenAIResponse, + payload: `{"model":"gpt-6-astra","input":"hello"}`, + cfg: config.Config{ + Payload: config.PayloadConfig{ + OverrideRaw: []config.PayloadRule{ + { + Models: []config.PayloadModelRule{{Name: "gpt-6-astra"}}, + Params: map[string]any{ + "input.-1": `{"type":"\u0063onfiguration_update","reasoning":{"effort":"medium"}}`, + }, + }, + }, + }, + }, + wantTools: 0, + }, + } { + t.Run(tc.name+"/"+transport+"/stream="+streamLabel, func(t *testing.T) { + capturedBody := make(chan []byte, 1) + completedEvent := []byte(`{"type":"response.completed","response":{"id":"resp_7384","object":"response","status":"completed","model":"gpt-6-astra","output":[],"usage":{"input_tokens":10,"output_tokens":5,"total_tokens":15}}}`) + + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if transport == "websocket" { + upgrader := websocket.Upgrader{} + conn, errUpgrade := upgrader.Upgrade(w, r, nil) + if errUpgrade != nil { + t.Errorf("upgrade websocket: %v", errUpgrade) + return + } + defer func() { + _ = conn.Close() + }() + _, msg, errRead := conn.ReadMessage() + if errRead != nil { + t.Errorf("read websocket message: %v", errRead) + return + } + capturedBody <- msg + if errWrite := conn.WriteMessage(websocket.TextMessage, completedEvent); errWrite != nil { + t.Errorf("write websocket message: %v", errWrite) + } + return + } + body, errRead := io.ReadAll(r.Body) + if errRead != nil { + t.Errorf("read http body: %v", errRead) + return + } + capturedBody <- body + w.Header().Set("Content-Type", "text/event-stream") + _, _ = w.Write(append(append([]byte("data: "), completedEvent...), '\n', '\n')) + })) + defer server.Close() + + cfg := tc.cfg + var exec cliproxyauth.ProviderExecutor = NewCodexExecutor(&cfg) + if transport == "websocket" { + exec = NewCodexWebsocketsExecutor(&cfg) + } + if len(tc.overrideHeaders) > 0 { + reg := registry.GetGlobalRegistry() + clientID := t.Name() + "/override-header" + reg.RegisterClient(clientID, "codex", []*registry.ModelInfo{{ + ID: tc.model, + Type: "codex", + Config: ®istry.ModelConfig{OverrideHeader: tc.overrideHeaders}, + }}) + t.Cleanup(func() { reg.UnregisterClient(clientID) }) + } + attrs := map[string]string{ + "base_url": server.URL, + "plan_type": "pro", + } + for k, v := range tc.authAttrs { + attrs[k] = v + } + auth := &cliproxyauth.Auth{ + ID: t.Name(), + Provider: "codex", + Attributes: attrs, + Metadata: map[string]any{ + "access_token": "test-token", + "account_id": "acct-1", + }, + } + req := cliproxyexecutor.Request{ + Model: tc.model, + Payload: []byte(tc.payload), + Metadata: tc.reqMetadata, + } + finalized := 0 + var headers http.Header + if tc.reqHeaders != nil { + headers = make(http.Header, len(tc.reqHeaders)) + for k, vals := range tc.reqHeaders { + for _, v := range vals { + headers.Add(k, v) + } + } + } + opts := cliproxyexecutor.Options{ + SourceFormat: tc.sourceFmt, + Stream: stream, + Headers: headers, + Metadata: map[string]any{ + cliproxyexecutor.FinalizedResponseFormatCallbackMetadataKey: func(string) { finalized++ }, + }, + } + evalsBefore := helps.PayloadConfigEvaluationCount() + var errExec error + if stream { + var res *cliproxyexecutor.StreamResult + res, errExec = exec.ExecuteStream(context.Background(), auth, req, opts) + if res != nil { + for chunk := range res.Chunks { + if chunk.Err != nil { + errExec = chunk.Err + } + } + } + } else { + _, errExec = exec.Execute(context.Background(), auth, req, opts) + } + + if gotEvals := helps.PayloadConfigEvaluationCount() - evalsBefore; gotEvals != 1 { + t.Fatalf("payload rule engine evaluated %d times, want 1", gotEvals) + } + if finalized != 1 { + t.Fatalf("payload finalizer evaluated %d times, want 1", finalized) + } + if errExec != nil { + t.Fatalf("execute error = %v", errExec) + } + gotBody := <-capturedBody + if tc.wantEffort != "" { + if gotEffort := ojson.GetBytes(gotBody, "reasoning.effort").String(); gotEffort != tc.wantEffort { + t.Fatalf("reasoning.effort = %q, want %q; body=%s", gotEffort, tc.wantEffort, gotBody) + } + } + tools := ojson.GetBytes(gotBody, "tools") + if tc.wantTools == 0 { + if tools.Exists() { + t.Fatalf("expected no tools, got %s (body=%s)", tools.Raw, gotBody) + } + return + } + if !tools.IsArray() || len(tools.Array()) != tc.wantTools { + t.Fatalf("tools count = %d, want %d; body=%s", len(tools.Array()), tc.wantTools, gotBody) + } + wantType := tc.wantToolType + if wantType == "" { + wantType = "image_generation" + } + if gotType := ojson.GetBytes(gotBody, tc.wantImgIdx).String(); gotType != wantType { + t.Fatalf("%s = %q, want %s; body=%s", tc.wantImgIdx, gotType, wantType, gotBody) + } + }) + } + } + } +} + func TestCodexExecutorHostedImageGenerationSIWCAndAPIKeyPreserveUpstreamModelAndParams(t *testing.T) { for _, credKind := range []string{"api_key", "siwc"} { for _, transport := range []string{"http", "websocket"} { diff --git a/internal/runtime/executor/codex_executor_request.go b/internal/runtime/executor/codex_executor_request.go index 17b140cdba..1acd7d7949 100644 --- a/internal/runtime/executor/codex_executor_request.go +++ b/internal/runtime/executor/codex_executor_request.go @@ -808,29 +808,61 @@ func ensureImageGenerationTool(body []byte, baseModel string, auth *cliproxyauth return doc.Bytes() } -func ensureImageGenerationToolDoc(doc *ojson.Doc, baseModel string, auth *cliproxyauth.Auth, headers http.Header) bool { - if doc == nil || isCodexResponsesLiteRequestDoc(doc, headers) || isCodexResponsesLiteModel(baseModel) { - return false - } - if strings.HasSuffix(baseModel, "spark") { - return false - } - if isCodexFreePlanAuth(auth) { - return false - } +func codexRequestHasReasoningForHostedImageDoc(doc *ojson.Doc, model string) bool { + return helps.CodexRequestHasReasoningForHostedImageDoc(doc, model) +} +func isCodexResponsesLiteRequestForAuthDoc(doc *ojson.Doc, modelName string, auth *cliproxyauth.Auth, headers http.Header) bool { + return helps.CodexResponsesLiteRequestForAuthDoc(doc, modelName, auth, headers) +} + +func codexToolsAlreadyContainImageGenDoc(doc *ojson.Doc) bool { + if doc == nil { + return false + } + tools := doc.Get("tools") + if !tools.Exists() || !tools.IsArray() { + return false + } + for _, t := range tools.Array() { + if t.Get("type").String() == "image_generation" || isImageGenerationFunctionTool(t) { + return true + } + } + return false +} + +func appendImageGenerationToolDoc(doc *ojson.Doc) bool { + if doc == nil { + return false + } tools := doc.Get("tools") if !tools.Exists() || !tools.IsArray() { return doc.SetRawBytes("tools", imageGenToolArrayJSON) == nil } - for _, t := range tools.Array() { - if t.Get("type").String() == "image_generation" || isImageGenerationFunctionTool(t) { - return false - } - } return doc.SetRawBytes("tools.-1", imageGenToolJSON) == nil } +func ensureImageGenerationToolDoc(doc *ojson.Doc, baseModel string, auth *cliproxyauth.Auth, headers http.Header) bool { + if doc == nil || isCodexFreePlanAuth(auth) || codexToolsAlreadyContainImageGenDoc(doc) { + return false + } + modelName := strings.TrimSpace(thinking.ParseSuffix(baseModel).ModelName) + if modelName == "" { + modelName = strings.TrimSpace(thinking.ParseSuffix(doc.Get("model").String()).ModelName) + } + if isCodexResponsesLiteRequestForAuthDoc(doc, modelName, auth, headers) { + return false + } + if isCodexResponsesLiteModel(modelName) && codexRequestHasReasoningForHostedImageDoc(doc, baseModel) { + return false + } + if strings.HasSuffix(strings.ToLower(modelName), "spark") { + return false + } + return appendImageGenerationToolDoc(doc) +} + func resolveCodexGPTImage2BaseModel(cfg *config.Config) string { if cfg == nil { return codexOpenAIImagesMainModel diff --git a/internal/runtime/executor/codex_executor_stream.go b/internal/runtime/executor/codex_executor_stream.go index 32244fd7cf..6074124a53 100644 --- a/internal/runtime/executor/codex_executor_stream.go +++ b/internal/runtime/executor/codex_executor_stream.go @@ -69,7 +69,7 @@ func (e *CodexExecutor) ExecuteStream(ctx context.Context, auth *cliproxyauth.Au return nil, err } - ctx = helps.WithPayloadFinalizer(ctx, helps.NewPayloadFinalizer(e.cfg, e.Identifier(), baseModel, to.String(), "", originalTranslated, req, opts)) + ctx = helps.WithPayloadFinalizer(ctx, helps.NewPayloadFinalizer(e.cfg, e.Identifier(), baseModel, to.String(), "", originalTranslated, req, opts, auth)) _ = doc.Delete("previous_response_id") _ = doc.Delete("generate") _ = doc.Delete("prompt_cache_retention") @@ -92,7 +92,11 @@ func (e *CodexExecutor) ExecuteStream(ctx context.Context, auth *cliproxyauth.Au if !useSIWC { normalizeCodexInstructionsDoc(doc, preserveNativeOutput) if e.cfg == nil || e.cfg.DisableImageGeneration == config.DisableImageGenerationOff { - ensureImageGenerationToolDoc(doc, baseModel, auth, opts.Headers) + checkModel := req.Model + if strings.TrimSpace(checkModel) == "" { + checkModel = baseModel + } + ensureImageGenerationToolDoc(doc, checkModel, auth, opts.Headers) if !codexAuthUsesAPIKey(auth) { normalizeCodexImageGenerationToolsDoc(doc) } diff --git a/internal/runtime/executor/codex_websockets_execute.go b/internal/runtime/executor/codex_websockets_execute.go index f56a5dd4d0..bafe8f305f 100644 --- a/internal/runtime/executor/codex_websockets_execute.go +++ b/internal/runtime/executor/codex_websockets_execute.go @@ -72,7 +72,7 @@ func (e *CodexWebsocketsExecutor) Execute(ctx context.Context, auth *cliproxyaut instructionsResult := ojson.GetBytesNoCopy(req.Payload, "instructions") hasExplicitInstructions := instructionsResult.Exists() && strings.TrimSpace(instructionsResult.String()) != "" - ctx = helps.WithPayloadFinalizer(ctx, helps.NewPayloadFinalizer(e.cfg, "codex-websockets", baseModel, to.String(), "", originalTranslated, req, opts)) + ctx = helps.WithPayloadFinalizer(ctx, helps.NewPayloadFinalizer(e.cfg, "codex-websockets", baseModel, to.String(), "", originalTranslated, req, opts, auth)) helps.SetStringIfDifferentDoc(doc, "model", baseModel) helps.SetBoolIfDifferentDoc(doc, "stream", true) _ = doc.Delete("prompt_cache_retention") @@ -81,7 +81,11 @@ func (e *CodexWebsocketsExecutor) Execute(ctx context.Context, auth *cliproxyaut if !useSIWC { normalizeCodexInstructionsDoc(doc, nativeRequest) if e.cfg == nil || e.cfg.DisableImageGeneration == config.DisableImageGenerationOff { - ensureImageGenerationToolDoc(doc, baseModel, auth, opts.Headers) + checkModel := req.Model + if strings.TrimSpace(checkModel) == "" { + checkModel = baseModel + } + ensureImageGenerationToolDoc(doc, checkModel, auth, opts.Headers) if !codexAuthUsesAPIKey(auth) { normalizeCodexImageGenerationToolsDoc(doc) } diff --git a/internal/runtime/executor/codex_websockets_stream.go b/internal/runtime/executor/codex_websockets_stream.go index 540fb46836..d533e83e9c 100644 --- a/internal/runtime/executor/codex_websockets_stream.go +++ b/internal/runtime/executor/codex_websockets_stream.go @@ -900,13 +900,17 @@ func (e *CodexWebsocketsExecutor) prepareCodexWebsocketStream(ctx context.Contex instructionsResult := ojson.GetBytesNoCopy(req.Payload, "instructions") hasExplicitInstructions := instructionsResult.Exists() && strings.TrimSpace(instructionsResult.String()) != "" - ctx = helps.WithPayloadFinalizer(ctx, helps.NewPayloadFinalizer(e.cfg, "codex-websockets", baseModel, to.String(), "", originalTranslated, req, opts)) + ctx = helps.WithPayloadFinalizer(ctx, helps.NewPayloadFinalizer(e.cfg, "codex-websockets", baseModel, to.String(), "", originalTranslated, req, opts, auth)) helps.SetStringIfDifferentDoc(doc, "model", baseModel) ensureWebSearchToolForHistoryDoc(doc, opts.Headers) if !useSIWC { normalizeCodexInstructionsDoc(doc, preserveNativeOutput) if e.cfg == nil || e.cfg.DisableImageGeneration == config.DisableImageGenerationOff { - ensureImageGenerationToolDoc(doc, baseModel, auth, opts.Headers) + checkModel := req.Model + if strings.TrimSpace(checkModel) == "" { + checkModel = baseModel + } + ensureImageGenerationToolDoc(doc, checkModel, auth, opts.Headers) if !codexAuthUsesAPIKey(auth) { normalizeCodexImageGenerationToolsDoc(doc) } diff --git a/internal/runtime/executor/helps/codex_hosted_images.go b/internal/runtime/executor/helps/codex_hosted_images.go index 48318e7d2d..1e9f924276 100644 --- a/internal/runtime/executor/helps/codex_hosted_images.go +++ b/internal/runtime/executor/helps/codex_hosted_images.go @@ -1,6 +1,7 @@ package helps import ( + "context" "net/http" "strings" @@ -8,9 +9,12 @@ import ( "github.com/router-for-me/CLIProxyAPI/v8/internal/config" "github.com/router-for-me/CLIProxyAPI/v8/internal/registry" "github.com/router-for-me/CLIProxyAPI/v8/internal/thinking" + "github.com/router-for-me/CLIProxyAPI/v8/internal/util" cliproxyauth "github.com/router-for-me/CLIProxyAPI/v8/sdk/cliproxy/auth" ) +const codexResponsesLiteHeaderName = "X-OpenAI-Internal-Codex-Responses-Lite" + // CodexModelUsesResponsesLite identifies models whose native Codex route uses Responses Lite. func CodexModelUsesResponsesLite(model string) bool { slug := strings.ToLower(strings.TrimSpace(thinking.ParseSuffix(model).ModelName)) @@ -22,7 +26,7 @@ func CodexModelUsesResponsesLite(model string) bool { // CodexResponsesLiteRequestDoc reads the route hints without changing the payload. func CodexResponsesLiteRequestDoc(doc *ojson.Doc, headers http.Header) bool { - if strings.EqualFold(strings.TrimSpace(headers.Get("X-OpenAI-Internal-Codex-Responses-Lite")), "true") { + if strings.EqualFold(strings.TrimSpace(headers.Get(codexResponsesLiteHeaderName)), "true") { return true } if doc == nil { @@ -32,15 +36,82 @@ func CodexResponsesLiteRequestDoc(doc *ojson.Doc, headers http.Header) bool { return value.Type == ojson.True || (value.Type == ojson.String && strings.EqualFold(strings.TrimSpace(value.String()), "true")) } +// CodexResponsesLiteRequestForAuthDoc reports whether the request uses the +// Codex Responses-Lite protocol after resolving client_metadata, client +// headers, credential header: attributes, and models.json override_header. +func CodexResponsesLiteRequestForAuthDoc(doc *ojson.Doc, modelName string, auth *cliproxyauth.Auth, headers http.Header) bool { + if CodexResponsesLiteRequestDoc(doc, nil) { + return true + } + var effective string + if headers != nil { + effective = strings.TrimSpace(headers.Get(codexResponsesLiteHeaderName)) + } + if auth != nil && len(auth.Attributes) > 0 { + resolved := (&http.Request{Header: http.Header{}}).WithContext(context.Background()) + util.ApplyCustomHeadersFromAttrs(resolved, auth.Attributes, headers) + if opLite := strings.TrimSpace(resolved.Header.Get(codexResponsesLiteHeaderName)); opLite != "" { + effective = opLite + } + } + if overrides := registry.ModelOverrideHeaders(modelName); len(overrides) > 0 { + for key, value := range overrides { + if strings.EqualFold(strings.TrimSpace(key), codexResponsesLiteHeaderName) { + effective = strings.TrimSpace(value) + } + } + } + return strings.EqualFold(effective, "true") +} + +// CodexRequestHasReasoningForHostedImageDoc reports whether the request carries +// a reasoning configuration (top-level reasoning.effort, configuration_update +// input item, or model thinking suffix). Upstream Codex /responses binds hosted +// image_generation to gpt-image-2-codex, which rejects complexity whenever +// reasoning effort is present on Responses-Lite models (#7270), whereas +// standard Responses requests without reasoning effort succeed (#7384). +func CodexRequestHasReasoningForHostedImageDoc(doc *ojson.Doc, model string) bool { + if thinking.HasThinkingSuffix(model) { + return true + } + if doc == nil { + return false + } + if thinking.HasThinkingSuffix(doc.Get("model").String()) { + return true + } + if reasoning := doc.Get("reasoning"); reasoning.Exists() && reasoning.Type != ojson.Null { + if reasoning.IsObject() { + if effort := reasoning.Get("effort"); effort.Exists() && effort.Type != ojson.Null && strings.TrimSpace(effort.String()) != "" { + return true + } + } else if strings.TrimSpace(reasoning.String()) != "" { + return true + } + } + if input := doc.Get("input"); input.IsArray() { + for _, item := range input.Array() { + if strings.TrimSpace(item.Get("type").String()) != "configuration_update" { + continue + } + if effort := item.Get("reasoning.effort"); effort.Exists() && effort.Type != ojson.Null && strings.TrimSpace(effort.String()) != "" { + return true + } + } + } + return false +} + // ValidateCodexHostedImageGenerationDoc checks the final, configured Responses payload // after all outbound headers, including credential and model overrides, are resolved. -// Native Codex Responses Lite cannot serve forced hosted images. API-key, SIWC, +// Native Codex Responses Lite cannot serve forced hosted images, while API-key, SIWC, // explicit passthrough, and standalone Images API requests retain their own contracts. func ValidateCodexHostedImageGenerationDoc(doc *ojson.Doc, upstreamHeaders http.Header, cfg *config.Config, nativeCodex bool) error { if !nativeCodex || doc == nil || (cfg != nil && cfg.DisableImageGeneration == config.DisableImageGenerationPassthrough) { return nil } - if !CodexModelUsesResponsesLite(doc.Get("model").String()) && !CodexResponsesLiteRequestDoc(doc, upstreamHeaders) { + model := doc.Get("model").String() + if !CodexModelUsesResponsesLite(model) && !CodexResponsesLiteRequestDoc(doc, upstreamHeaders) { return nil } if !codexRequestForcesImageGenerationDoc(doc) { diff --git a/internal/runtime/executor/helps/payload_finalizer.go b/internal/runtime/executor/helps/payload_finalizer.go index a18cd6fdce..b1d45f2086 100644 --- a/internal/runtime/executor/helps/payload_finalizer.go +++ b/internal/runtime/executor/helps/payload_finalizer.go @@ -9,6 +9,7 @@ import ( "github.com/MoeForge/moego/ojson" "github.com/router-for-me/CLIProxyAPI/v8/internal/config" + cliproxyauth "github.com/router-for-me/CLIProxyAPI/v8/sdk/cliproxy/auth" cliproxyexecutor "github.com/router-for-me/CLIProxyAPI/v8/sdk/cliproxy/executor" ) @@ -47,11 +48,15 @@ func (f PayloadFinalizer) FinalizeDoc(doc *ojson.Doc) { } // NewPayloadFinalizer snapshots matching context before built-in request mutations. -func NewPayloadFinalizer(cfg *config.Config, executor, model, protocol, root string, original []byte, req cliproxyexecutor.Request, opts cliproxyexecutor.Options) PayloadFinalizer { +func NewPayloadFinalizer(cfg *config.Config, executor, model, protocol, root string, original []byte, req cliproxyexecutor.Request, opts cliproxyexecutor.Options, auths ...*cliproxyauth.Auth) PayloadFinalizer { requestedModel := PayloadRequestedModel(opts, req.Model) requestPath := PayloadRequestPath(opts) headers := opts.Headers.Clone() source := append([]byte(nil), original...) + var auth *cliproxyauth.Auth + if len(auths) > 0 { + auth = auths[0] + } var formatCallback func(string) if opts.Metadata != nil { formatCallback, _ = opts.Metadata[cliproxyexecutor.FinalizedResponseFormatCallbackMetadataKey].(func(string)) @@ -61,7 +66,7 @@ func NewPayloadFinalizer(cfg *config.Config, executor, model, protocol, root str key := &body[:1][0] if v, ok := payloadDocProbes.LoadAndDelete(key); ok { if doc, okDoc := v.(*ojson.Doc); okDoc && doc != nil { - ApplyPayloadConfigWithRequestForExecutorDoc(cfg, executor, model, protocol, opts.SourceFormat.String(), root, doc, source, requestedModel, requestPath, headers) + applyPayloadConfigWithTrackedPathsForExecutorAndAuthDoc(cfg, executor, model, protocol, opts.SourceFormat.String(), root, doc, source, requestedModel, requestPath, headers, auth) if formatCallback != nil { formatCallback(strings.Clone(strings.TrimSpace(doc.Get(buildPayloadPath(root, "response_format")).String()))) } @@ -69,7 +74,12 @@ func NewPayloadFinalizer(cfg *config.Config, executor, model, protocol, root str return nil } } - out := ApplyPayloadConfigWithRequestForExecutor(cfg, executor, model, protocol, opts.SourceFormat.String(), root, body, source, requestedModel, requestPath, headers) + doc := ojson.NewDoc(body) + _, mutated := applyPayloadConfigWithTrackedPathsForExecutorAndAuthDoc(cfg, executor, model, protocol, opts.SourceFormat.String(), root, doc, source, requestedModel, requestPath, headers, auth) + out := body + if mutated { + out = doc.Bytes() + } if formatCallback != nil { formatCallback(strings.Clone(strings.TrimSpace(ojson.GetBytesNoCopy(out, buildPayloadPath(root, "response_format")).String()))) } diff --git a/internal/runtime/executor/helps/payload_helpers.go b/internal/runtime/executor/helps/payload_helpers.go index b53bafba4e..f6e4150a3c 100644 --- a/internal/runtime/executor/helps/payload_helpers.go +++ b/internal/runtime/executor/helps/payload_helpers.go @@ -6,14 +6,24 @@ import ( "reflect" "strconv" "strings" + "sync/atomic" "github.com/MoeForge/moego/ojson" "github.com/router-for-me/CLIProxyAPI/v8/internal/config" "github.com/router-for-me/CLIProxyAPI/v8/internal/thinking" "github.com/router-for-me/CLIProxyAPI/v8/internal/util" + cliproxyauth "github.com/router-for-me/CLIProxyAPI/v8/sdk/cliproxy/auth" cliproxyexecutor "github.com/router-for-me/CLIProxyAPI/v8/sdk/cliproxy/executor" ) +var payloadConfigEvaluations atomic.Int64 + +// PayloadConfigEvaluationCount returns the cumulative number of payload rule +// engine evaluations across the process. +func PayloadConfigEvaluationCount() int64 { + return payloadConfigEvaluations.Load() +} + // ApplyPayloadConfigWithRoot behaves like applyPayloadConfig but treats all parameter // paths as relative to the provided root path and restricts matches to the given // protocol when supplied. Defaults are checked @@ -91,6 +101,11 @@ func ApplyPayloadConfigWithTrackedPathsForExecutorDoc(cfg *config.Config, target } func applyPayloadConfigWithTrackedPathsForExecutorDoc(cfg *config.Config, targetExecutor, model, protocol, fromProtocol, root string, doc *ojson.Doc, original []byte, requestedModel, requestPath string, headers http.Header, trackedPaths ...string) (map[string]bool, bool) { + return applyPayloadConfigWithTrackedPathsForExecutorAndAuthDoc(cfg, targetExecutor, model, protocol, fromProtocol, root, doc, original, requestedModel, requestPath, headers, nil, trackedPaths...) +} + +func applyPayloadConfigWithTrackedPathsForExecutorAndAuthDoc(cfg *config.Config, targetExecutor, model, protocol, fromProtocol, root string, doc *ojson.Doc, original []byte, requestedModel, requestPath string, headers http.Header, auth *cliproxyauth.Auth, trackedPaths ...string) (map[string]bool, bool) { + payloadConfigEvaluations.Add(1) touched := make(map[string]bool) if doc == nil || doc.Len() == 0 { return touched, false @@ -133,6 +148,59 @@ func applyPayloadConfigWithTrackedPathsForExecutorDoc(cfg *config.Config, target model = strings.TrimSpace(model) requestedModel = strings.TrimSpace(requestedModel) if model != "" || requestedModel != "" { + toolsPath := buildPayloadPath(root, "tools") + toolChoicePath := buildPayloadPath(root, "tool_choice") + autoInjectedToolIdx := -1 + if isCodexTargetExecutor(targetExecutor) && + cfg.DisableImageGeneration == config.DisableImageGenerationOff && + len(original) > 0 && + !rawToolsContainImageGeneration(original, toolsPath) { + autoInjectedToolIdx = findLastImageGenerationToolIndexDoc(doc, toolsPath) + } + origTools := ojson.GetBytesNoCopy(original, toolsPath) + originalHadEmptyTools := len(original) > 0 && origTools.Exists() && origTools.IsArray() && len(origTools.Array()) == 0 + userWroteToolChoice := false + noteRuleWrite := func(resolvedPath string) { + markTouched(resolvedPath) + if toolChoicePath != "" && payloadRuleTargetsPath(resolvedPath, toolChoicePath) { + userWroteToolChoice = true + return + } + if autoInjectedToolIdx < 0 || toolsPath == "" || !payloadRuleTargetsPath(resolvedPath, toolsPath) { + return + } + if resolvedPath == toolsPath { + autoInjectedToolIdx = -1 + return + } + rest := strings.TrimPrefix(resolvedPath, toolsPath+".") + if idx := strings.IndexByte(rest, '.'); idx >= 0 { + rest = rest[:idx] + } + if elemIdx, errIdx := strconv.Atoi(rest); errIdx == nil && elemIdx == autoInjectedToolIdx { + autoInjectedToolIdx = -1 + } + } + noteRuleDelete := func(resolvedPath string) { + if autoInjectedToolIdx < 0 || toolsPath == "" || !payloadRuleTargetsPath(resolvedPath, toolsPath) { + return + } + if resolvedPath == toolsPath { + autoInjectedToolIdx = -1 + return + } + rest := strings.TrimPrefix(resolvedPath, toolsPath+".") + if strings.Contains(rest, ".") { + return + } + if delIdx, errIdx := strconv.Atoi(rest); errIdx == nil { + if delIdx == autoInjectedToolIdx { + autoInjectedToolIdx = -1 + } else if delIdx >= 0 && delIdx < autoInjectedToolIdx { + autoInjectedToolIdx-- + } + } + } candidates := payloadModelCandidates(model, requestedModel) appliedDefaults := make(map[string]struct{}) var createdByDefault map[string]struct{} @@ -223,7 +291,7 @@ func applyPayloadConfigWithTrackedPathsForExecutorDoc(cfg *config.Config, target } mutated = true appliedDefaults[resolvedPath] = struct{}{} - markTouched(resolvedPath) + noteRuleWrite(resolvedPath) } } } @@ -255,7 +323,7 @@ func applyPayloadConfigWithTrackedPathsForExecutorDoc(cfg *config.Config, target } mutated = true appliedDefaults[resolvedPath] = struct{}{} - markTouched(resolvedPath) + noteRuleWrite(resolvedPath) } } } @@ -276,7 +344,7 @@ func applyPayloadConfigWithTrackedPathsForExecutorDoc(cfg *config.Config, target mutated = true } if applied { - markTouched(resolvedPath) + noteRuleWrite(resolvedPath) } } } @@ -302,7 +370,7 @@ func applyPayloadConfigWithTrackedPathsForExecutorDoc(cfg *config.Config, target mutated = true } if applied { - markTouched(resolvedPath) + noteRuleWrite(resolvedPath) } } } @@ -327,16 +395,126 @@ func applyPayloadConfigWithTrackedPathsForExecutorDoc(cfg *config.Config, target } if existed { mutated = true + noteRuleDelete(resolvedPath) } markTouched(resolvedPath) } } } + if autoInjectedToolIdx >= 0 && !(userWroteToolChoice && docToolChoiceSelectsImageGeneration(doc, toolChoicePath)) { + if codexConfiguredPayloadIncompatibleWithAutoImageGenDoc(doc, root, model, auth, headers) { + if retractAutoInjectedImageGenerationToolAtIndexDoc(doc, toolsPath, autoInjectedToolIdx, originalHadEmptyTools) { + mutated = true + } + } + } } } return touched, mutated } +func findLastImageGenerationToolIndexDoc(doc *ojson.Doc, toolsPath string) int { + if doc == nil { + return -1 + } + tools := doc.Get(toolsPath) + if !tools.Exists() || !tools.IsArray() { + return -1 + } + items := tools.Array() + for idx := len(items) - 1; idx >= 0; idx-- { + if strings.TrimSpace(items[idx].Get("type").String()) == "image_generation" { + return idx + } + } + return -1 +} + +func rawToolsContainImageGeneration(raw []byte, toolsPath string) bool { + if len(raw) == 0 { + return false + } + tools := ojson.GetBytesNoCopy(raw, toolsPath) + if !tools.Exists() || !tools.IsArray() { + return false + } + for _, tool := range tools.Array() { + if strings.TrimSpace(tool.Get("type").String()) == "image_generation" { + return true + } + } + return false +} + +func docToolChoiceSelectsImageGeneration(doc *ojson.Doc, toolChoicePath string) bool { + if doc == nil || toolChoicePath == "" { + return false + } + choice := doc.Get(toolChoicePath) + if !choice.Exists() { + return false + } + if choice.Type == ojson.String { + return strings.EqualFold(strings.TrimSpace(choice.String()), "image_generation") + } + if !choice.IsObject() { + return false + } + switch strings.ToLower(strings.TrimSpace(choice.Get("type").String())) { + case "image_generation": + return true + case "tool": + return strings.EqualFold(strings.TrimSpace(choice.Get("name").String()), "image_generation") + case "allowed_tools": + allowed := choice.Get("tools") + if !allowed.IsArray() { + return false + } + for _, tool := range allowed.Array() { + if strings.EqualFold(strings.TrimSpace(tool.Get("type").String()), "image_generation") { + return true + } + } + } + return false +} + +func codexConfiguredPayloadIncompatibleWithAutoImageGenDoc(doc *ojson.Doc, root, fallbackModel string, auth *cliproxyauth.Auth, headers http.Header) bool { + if doc == nil { + return false + } + configuredModel := strings.TrimSpace(doc.Get(buildPayloadPath(root, "model")).String()) + if configuredModel == "" { + configuredModel = strings.TrimSpace(fallbackModel) + } + modelName := strings.TrimSpace(thinking.ParseSuffix(configuredModel).ModelName) + if CodexResponsesLiteRequestForAuthDoc(doc, modelName, auth, headers) { + return true + } + if strings.HasSuffix(strings.ToLower(modelName), "spark") { + return true + } + return CodexModelUsesResponsesLite(modelName) && CodexRequestHasReasoningForHostedImageDoc(doc, configuredModel) +} + +func retractAutoInjectedImageGenerationToolAtIndexDoc(doc *ojson.Doc, toolsPath string, targetIdx int, originalHadEmptyTools bool) bool { + tools := doc.Get(toolsPath) + if !tools.Exists() || !tools.IsArray() { + return false + } + toolItems := tools.Array() + if targetIdx < 0 || targetIdx >= len(toolItems) { + return false + } + if strings.TrimSpace(toolItems[targetIdx].Get("type").String()) != "image_generation" { + return false + } + if len(toolItems) == 1 && !originalHadEmptyTools { + return doc.Delete(toolsPath) == nil + } + return doc.Delete(toolsPath+"."+strconv.Itoa(targetIdx)) == nil +} + // ApplyPayloadConfigWithRequestTracked applies payload config and reports whether // an applied rule targeted trackedPath or one of its descendants. func ApplyPayloadConfigWithRequestTracked(cfg *config.Config, model, protocol, fromProtocol, root string, payload, original []byte, requestedModel string, requestPath string, headers http.Header, trackedPath string) ([]byte, bool) { diff --git a/internal/runtime/executor/payload_barrier_test.go b/internal/runtime/executor/payload_barrier_test.go index ac3b9ad95a..c47d7af4cd 100644 --- a/internal/runtime/executor/payload_barrier_test.go +++ b/internal/runtime/executor/payload_barrier_test.go @@ -21,7 +21,7 @@ import ( func TestPayloadBarrierCodexImageFilter(t *testing.T) { for _, transport := range []string{"http", "websocket"} { for _, stream := range []bool{false, true} { - for _, model := range []string{"gpt-5.4", "gpt-5.5"} { + for _, model := range []string{"gpt-5.4", "gpt-5.5", "gpt-6-astra", "gpt-6-sol"} { t.Run(transport+"/"+model+map[bool]string{false: "/execute", true: "/stream"}[stream], func(t *testing.T) { captured := make(chan []byte, 1) completed := []byte(`{"type":"response.completed","response":{"id":"resp_barrier","status":"completed","output":[],"usage":{"input_tokens":0,"output_tokens":0,"total_tokens":0}}}`) @@ -59,7 +59,7 @@ func TestPayloadBarrierCodexImageFilter(t *testing.T) { _, _ = w.Write(append(append([]byte("data: "), completed...), '\n', '\n')) })) defer server.Close() - cfg := &config.Config{Payload: config.PayloadConfig{Filter: []config.PayloadFilterRule{{Models: []config.PayloadModelRule{{Name: "gpt-5.4"}}, Params: []string{`tools.#(type=="image_generation")#`, "instructions", "prompt_cache_key"}}}}} + cfg := &config.Config{Payload: config.PayloadConfig{Filter: []config.PayloadFilterRule{{Models: []config.PayloadModelRule{{Name: "gpt-5.4"}, {Name: "gpt-6-astra"}}, Params: []string{`tools.#(type=="image_generation")#`, "instructions", "prompt_cache_key"}}}}} var executor cliproxyauth.ProviderExecutor = NewCodexExecutor(cfg) if transport == "websocket" { executor = NewCodexWebsocketsExecutor(cfg) @@ -81,11 +81,12 @@ func TestPayloadBarrierCodexImageFilter(t *testing.T) { t.Fatal(errExecute) } body := <-captured + filteredModel := model == "gpt-5.4" || model == "gpt-6-astra" image := ojson.GetBytes(body, `tools.#(type=="image_generation")`).Exists() - if image != (model == "gpt-5.5") { + if image != !filteredModel { t.Fatalf("image tool presence = %v: %s", image, body) } - if model == "gpt-5.4" && (ojson.GetBytes(body, "instructions").Exists() || ojson.GetBytes(body, "prompt_cache_key").Exists()) { + if filteredModel && (ojson.GetBytes(body, "instructions").Exists() || ojson.GetBytes(body, "prompt_cache_key").Exists()) { t.Fatalf("late injection undid filter: %s", body) } })