diff --git a/internal/gateway/codex_accounts_test.go b/internal/gateway/codex_accounts_test.go index ce347dbfc..9fe317f74 100644 --- a/internal/gateway/codex_accounts_test.go +++ b/internal/gateway/codex_accounts_test.go @@ -688,3 +688,48 @@ func older(a, b string) bool { } return false } + +// Codex's Ultrafast (service_tier "ultrafast", offered on a Pro plan's +// models) reaches the ChatGPT backend when the pool of Codex's accounts +// serves the turn, as it does on Codex's own sign-in, and Fast's +// "priority" still does (yxinyu715 on X: two Pro accounts, Ultrafast +// does nothing through magpie and works without it). +func TestCodexPoolKeepsUltrafast(t *testing.T) { + codexSignedIn(t, "spare@example.com") + var tiers []any + up := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + b, _ := io.ReadAll(r.Body) + var m map[string]any + json.Unmarshal(b, &m) + tiers = append(tiers, m["service_tier"]) + io.WriteString(w, sse( + `data: {"type":"response.created","response":{"id":"r1","model":"gpt-5.5"}}`, + `data: {"type":"response.output_text.delta","delta":"pong"}`, + `data: {"type":"response.completed","response":{"id":"r1","usage":{"input_tokens":7,"output_tokens":1}}}`)) + })) + t.Cleanup(up.Close) + was := provider.CodexBase + provider.CodexBase = up.URL + "/backend-api/codex" + t.Cleanup(func() { provider.CodexBase = was }) + + s := New() + for _, tier := range []string{"ultrafast", "priority", "flex"} { + tiers = nil + rec := httptest.NewRecorder() + req := httptest.NewRequest("POST", CodexPath+"/responses", strings.NewReader(`{"model":"gpt-5.5","stream":true,"input":"ping","service_tier":"`+tier+`"}`)) + req.Header.Set("Authorization", "Bearer chatgpt-token") + req.Header.Set("chatgpt-account-id", "acct-1") + req.Header.Set("User-Agent", "codex_cli_rs/0.160.1") + s.Handler().ServeHTTP(rec, req) + if rec.Code != 200 || len(tiers) != 1 { + t.Fatalf("%s: status %d, %d requests: %s", tier, rec.Code, len(tiers), rec.Body.String()) + } + want := any(tier) + if tier == "flex" { + want = nil + } + if tiers[0] != want { + t.Errorf("%s: backend asked for %v", tier, tiers[0]) + } + } +} diff --git a/internal/gateway/fast_test.go b/internal/gateway/fast_test.go index 072a6dcb0..dd0b166be 100644 --- a/internal/gateway/fast_test.go +++ b/internal/gateway/fast_test.go @@ -18,6 +18,18 @@ func TestResponsesFastTier(t *testing.T) { t.Errorf("%q: tier sent %v, want %v: %s", host, got, want, b) } } + // Codex's Ultrafast goes to the ChatGPT backend, the one that offers + // it; OpenAI's API gets it as Fast + r, _ = parseResponses([]byte(`{"model":"gpt-5.5","input":"hi","service_tier":"ultrafast"}`)) + if !r.Fast || !r.Ultrafast { + t.Fatalf("ultrafast not read: %+v", r) + } + for host, want := range map[string]string{"chatgpt.com": `"service_tier":"ultrafast"`, "api.openai.com": `"service_tier":"priority"`, "openrouter.ai": ""} { + b := string(buildResponses(r, "gpt-5.5", host, false)) + if want == "" && strings.Contains(b, "service_tier") || want != "" && !strings.Contains(b, want) { + t.Errorf("ultrafast to %q: %s", host, b) + } + } r, _ = parseResponses([]byte(`{"model":"m","input":"hi","service_tier":"flex"}`)) if r.Fast { t.Error("flex read as fast") diff --git a/internal/gateway/ir.go b/internal/gateway/ir.go index 12cc8c9f6..1256ebdde 100644 --- a/internal/gateway/ir.go +++ b/internal/gateway/ir.go @@ -136,6 +136,10 @@ type Request struct { Parallel *bool // parallel tool calls allowed WebSearch bool // the client offered its provider's own web search Fast bool // the client asked for priority processing: service_tier priority (Codex's Fast mode) + // Ultrafast is Codex's Ultrafast (service_tier "ultrafast"), which a + // ChatGPT account on a plan with it offers on its models; Fast is set + // with it, so where there is no Ultrafast the request goes fast + Ultrafast bool // CacheKey is the client's prompt_cache_key (Codex sends its thread's // id), which OpenAI, and relays in front of it, route a conversation by // to where its prompt is cached. diff --git a/internal/gateway/responses.go b/internal/gateway/responses.go index f88d53afb..976f38a1b 100644 --- a/internal/gateway/responses.go +++ b/internal/gateway/responses.go @@ -255,7 +255,7 @@ func parseResponses(body []byte) (*Request, error) { return nil, fmt.Errorf("invalid request: %v", err) } r := &Request{Model: q.Model, System: q.Instructions, MaxTokens: q.MaxOutputTokens, Temp: q.Temperature, - TopP: q.TopP, Stream: q.Stream, Parallel: q.ParallelToolCalls, Fast: q.ServiceTier == "priority", CacheKey: q.PromptCacheKey, Include: q.Include, + TopP: q.TopP, Stream: q.Stream, Parallel: q.ParallelToolCalls, Fast: q.ServiceTier == "priority" || q.ServiceTier == "ultrafast", Ultrafast: q.ServiceTier == "ultrafast", CacheKey: q.PromptCacheKey, Include: q.Include, ClientMetadata: sentRaw(q.ClientMetadata)} r.Text, r.Format = textFormat(sentRaw(q.Text)) if q.Reasoning != nil { @@ -771,6 +771,10 @@ func buildResponses(r *Request, model, host string, rejectTemp bool) []byte { if r.Fast && (host == "chatgpt.com" || host == "api.openai.com") { out["service_tier"] = "priority" } + // Ultrafast only the ChatGPT backend offers + if r.Ultrafast && host == "chatgpt.com" { + out["service_tier"] = "ultrafast" + } if r.Effort != "" { out["reasoning"] = map[string]any{"effort": r.Effort, "summary": "auto"} } else if r.Thinking { diff --git a/internal/provider/codex_request.go b/internal/provider/codex_request.go index 48871f24b..b6d479814 100644 --- a/internal/provider/codex_request.go +++ b/internal/provider/codex_request.go @@ -236,7 +236,7 @@ func codexInstructions(model string) string { // codexBody makes a Responses request the one Codex CLI would send: it // streams only, keeps nothing (so its items carry no ids), carries Codex's // instructions — the client's own riding first in the input — and rejects -// the sampling knobs and any service tier but Fast's. +// the sampling knobs and any service tier but Fast's and Ultrafast's. func codexBody(body []byte) []byte { dec := json.NewDecoder(bytes.NewReader(body)) dec.UseNumber() @@ -247,8 +247,9 @@ func codexBody(body []byte) []byte { for _, k := range []string{"max_output_tokens", "max_completion_tokens", "temperature", "top_p", "previous_response_id", "user", "safety_identifier"} { delete(m, k) } - // Codex's Fast mode sends "priority"; any other tier stays out - if m["service_tier"] != "priority" { + // Codex's Fast mode sends "priority", its Ultrafast "ultrafast"; any + // other tier stays out + if t := m["service_tier"]; t != "priority" && t != "ultrafast" { delete(m, "service_tier") } model, _ := m["model"].(string) diff --git a/internal/provider/codex_request_test.go b/internal/provider/codex_request_test.go index 61104c9af..3076c0166 100644 --- a/internal/provider/codex_request_test.go +++ b/internal/provider/codex_request_test.go @@ -181,8 +181,8 @@ func TestCodexPromptsSameMtime(t *testing.T) { } } -// Codex's Fast mode reaches the ChatGPT backend as service_tier "priority"; -// any other tier is dropped as before. +// Codex's Fast mode reaches the ChatGPT backend as service_tier "priority", +// its Ultrafast as "ultrafast"; any other tier is dropped as before. func TestCodexBodyServiceTier(t *testing.T) { t.Setenv("CODEX_HOME", t.TempDir()) t.Setenv("XDG_CACHE_HOME", t.TempDir()) @@ -195,6 +195,9 @@ func TestCodexBodyServiceTier(t *testing.T) { if v, _ := tier(`{"model":"gpt-6-sol","service_tier":"priority","input":"hi"}`); v != "priority" { t.Errorf("priority: %v", v) } + if v, _ := tier(`{"model":"gpt-6-sol","service_tier":"ultrafast","input":"hi"}`); v != "ultrafast" { + t.Errorf("ultrafast: %v", v) + } for _, s := range []string{`"flex"`, `"auto"`, `"default"`, `"fast"`, `null`} { if v, ok := tier(`{"model":"gpt-6-sol","service_tier":` + s + `,"input":"hi"}`); ok { t.Errorf("%s kept as %v", s, v)