Files
CLIProxyAPI/internal/runtime/executor/codex_executor_compact_test.go
2026-10-12 11:45:25 +08:00

155 lines
5.6 KiB
Go

package executor
import (
"context"
"io"
"net/http"
"net/http/httptest"
"testing"
"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"
sdktranslator "github.com/router-for-me/CLIProxyAPI/v8/sdk/translator"
"github.com/tidwall/gjson"
)
func TestCodexExecutorCompactAddsDefaultInstructionsWithoutInjectingImageTool(t *testing.T) {
cases := []struct {
name string
payload string
}{
{
name: "missing instructions",
payload: `{"model":"gpt-5.4","input":[{"type":"message","role":"user","content":"history"},{"type":"compaction_trigger"}]}`,
},
{
name: "null instructions",
payload: `{"model":"gpt-5.4","instructions":null,"input":[{"type":"message","role":"user","content":"history"},{"type":"compaction_trigger"}]}`,
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
var gotPath string
var gotBody []byte
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
gotPath = r.URL.Path
body, _ := io.ReadAll(r.Body)
gotBody = body
w.Header().Set("Content-Type", "application/json")
_, _ = w.Write([]byte(`{"id":"resp_1","object":"response.compaction","usage":{"input_tokens":1,"output_tokens":2,"total_tokens":3}}`))
}))
defer server.Close()
executor := NewCodexExecutor(&config.Config{})
auth := &cliproxyauth.Auth{Attributes: map[string]string{
"base_url": server.URL,
"api_key": "test",
}}
resp, err := executor.Execute(context.Background(), auth, cliproxyexecutor.Request{
Model: "gpt-5.4",
Payload: []byte(tc.payload),
}, cliproxyexecutor.Options{
SourceFormat: sdktranslator.FromString("openai-response"),
Alt: "responses/compact",
Stream: false,
})
if err != nil {
t.Fatalf("Execute error: %v", err)
}
if gotPath != "/responses/compact" {
t.Fatalf("path = %q, want %q", gotPath, "/responses/compact")
}
if instructions := gjson.GetBytes(gotBody, "instructions"); instructions.Type != gjson.String || instructions.String() != "" {
t.Fatalf("instructions = %s, want empty string; body=%s", instructions.Raw, gotBody)
}
if gjson.GetBytes(gotBody, "tools").Exists() {
t.Fatalf("compact request injected image_generation tool: %s", gotBody)
}
input := gjson.GetBytes(gotBody, "input").Array()
if len(input) != 2 || input[1].Get("type").String() != "compaction_trigger" {
t.Fatalf("compact input order changed: %s", gotBody)
}
if string(resp.Payload) != `{"id":"resp_1","object":"response.compaction","usage":{"input_tokens":1,"output_tokens":2,"total_tokens":3}}` {
t.Fatalf("payload = %s", string(resp.Payload))
}
})
}
}
func TestCodexExecutorCompactUseSIWCAppliesSIWCRulesBeforePayloadFinalizer(t *testing.T) {
var gotPath string
var gotHeaders http.Header
var gotBody []byte
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
gotPath = r.URL.Path
gotHeaders = r.Header.Clone()
body, _ := io.ReadAll(r.Body)
gotBody = body
w.Header().Set("Content-Type", "application/json")
_, _ = w.Write([]byte(`{"id":"resp_siwc_compact","object":"response.compaction","usage":{"input_tokens":1,"output_tokens":2,"total_tokens":3}}`))
}))
defer server.Close()
cfg := &config.Config{
Payload: config.PayloadConfig{
Override: []config.PayloadRule{
{
Models: []config.PayloadModelRule{{Name: "gpt-5.4", Protocol: "openai-response"}},
Params: map[string]any{"temperature": 0.25},
},
},
},
}
executor := NewCodexExecutor(cfg)
auth := &cliproxyauth.Auth{
ID: "codex-siwc-compact",
Provider: "codex",
Attributes: map[string]string{
"base_url": server.URL + "/v1",
},
Metadata: map[string]any{
"type": "codex",
"access_token": "outer-tok",
"account_id": "acct-1",
"use_siwc": true,
"siwc": map[string]any{
"access_token": "siwc-compact-tok",
"client_id": "oaiapp_compact",
},
},
}
payload := `{"model":"gpt-5.4","instructions":null,"temperature":0.9,"max_output_tokens":512,"prompt_cache_retention":"in_memory","prompt_cache_options":{"ttl":60},"input":[{"type":"message","role":"user","content":"history"},{"type":"compaction_trigger"}]}`
_, err := executor.Execute(context.Background(), auth, cliproxyexecutor.Request{
Model: "gpt-5.4",
Payload: []byte(payload),
}, cliproxyexecutor.Options{
SourceFormat: sdktranslator.FromString("openai-response"),
Alt: "responses/compact",
})
if err != nil {
t.Fatalf("Execute compact error: %v", err)
}
if gotPath != "/v1/responses/compact" {
t.Fatalf("path = %q, want /v1/responses/compact", gotPath)
}
if gotHeaders.Get("Authorization") != "Bearer siwc-compact-tok" {
t.Fatalf("Authorization = %q, want Bearer siwc-compact-tok", gotHeaders.Get("Authorization"))
}
if gotHeaders.Get("Chatgpt-Account-Id") != "" || gotHeaders.Get("Originator") != "" {
t.Fatalf("unexpected Codex headers on SIWC compact request: %v", gotHeaders)
}
for _, omitted := range []string{"instructions", "max_output_tokens", "prompt_cache_retention", "prompt_cache_options", "stream", "store"} {
if gjson.GetBytes(gotBody, omitted).Exists() {
t.Fatalf("field %q should be omitted in SIWC compact body: %s", omitted, gotBody)
}
}
// PayloadFinalizer runs last and can override temperature after built-in SIWC stripping.
if gotTemp := gjson.GetBytes(gotBody, "temperature").Float(); gotTemp != 0.25 {
t.Fatalf("PayloadFinalizer temperature override = %v, want 0.25; body=%s", gotTemp, gotBody)
}
}