Files
magpie/internal/gateway/codex_tier.go
T
yetone 967fefcec6 gateway: a key can be set to a service tier (JackyFill on X)
A gateway key now carries ServiceTier: follow the request (unset),
standard, Fast (priority) or Ultrafast. keyTiered sets the Responses and
Chat request's service_tier to it, over the client's ask and Codex's own
setting; standard takes the tier off and Ultrafast on a bare model no
account lists it on goes as priority. What is sent upstream stays the
providers' own rule (fits, takesTier, fallback), so a vendor with no
service tier is sent none; Anthropic-protocol requests are untouched.
Set with the tier-key action, `magpie gateway-key tier <id> ...`, or the
key row's badge (zh, zh-TW, ja, de strings).

Run (wsl-run, Linux): TestGatewayKeyServiceTier, TestKeyTieredChat,
TestKeyServiceTier, TestGatewayKeyTierCLI green; internal/gui -run
Key|Tier|Caller|Access ok; gateway-key-tier and gateway-key-concurrency
GUI files 40 of 40 in chromium and webkit (serial); go vet nogui for
linux, windows and darwin; gofmt clean. Broken by hand: with the
keyTiered call removed TestGatewayKeyServiceTier fails on 4 rows.
A parallel run of all gateway-key and i18n GUI files had 8 unrelated
timeouts that pass serially (load). The whole internal/cli package hit
the default 10m timeout on this box; the key tests in it pass.
Not tried: a real ChatGPT account, the Windows box, the full suite.
2026-11-11 17:59:36 +08:00

84 lines
2.8 KiB
Go

package gateway
import (
"bytes"
"net/http"
"strings"
"github.com/yetone/magpie/internal/access"
"github.com/yetone/magpie/internal/provider"
"github.com/yetone/magpie/internal/settings"
)
// codexTiered is the body of a request of Codex's with the service tier
// the user set for Codex (provider.CodexServiceTier, JackyFill on X) in
// place of the one Codex asked: none for the standard tier, "priority"
// (Codex's Fast) or "ultrafast" (its Ultra Fast). Unset, or another
// agent's request, goes as it came. The tier is then served as Codex's own
// ask would be: an account whose plan has no Ultrafast is put after one
// that has, or asked for priority (serve), a provider added by its address
// that turns a tier away is not sent it again (fits). One of Codex's own
// models no account in magpie lists Ultrafast on is asked for priority
// instead, as it may go on to Codex's own sign-in, whose plan magpie
// doesn't know.
func codexTiered(r *http.Request, body []byte, model string) []byte {
set := provider.CodexServiceTier()
if set == "" || !fromCodex(r.Header) && agentOf(r) != "codex" {
return body
}
if set == "ultrafast" && !strings.Contains(model, "/") && !provider.UltraModels()[model] {
set = "priority"
}
if set == settings.CodexTierStandard {
set = ""
}
return withServiceTier(body, set)
}
// keyTiered is body with the service tier the gateway key behind r is set
// to (access.Key.ServiceTier, JackyFill on X) in place of the one the client
// asked, for any OpenAI-API request. "" follows the request. A bare model
// that no account lists Ultrafast on is asked for priority instead, as
// codexTiered does. Whether the tier is then sent is the providers' own
// say (serve, fits), so a vendor without service tiers never sees it.
func keyTiered(r *http.Request, body []byte, model string) []byte {
set := access.Caller(r.Context()).ServiceTier
if set == "" {
return body
}
if set == "ultrafast" && !strings.Contains(model, "/") && !provider.UltraModels()[model] {
set = "priority"
}
if set == settings.CodexTierStandard {
set = ""
}
return withServiceTier(body, set)
}
// withServiceTier is body with its top-level service_tier set to tier, or
// without one for "", every other byte as it came. A body that isn't a
// JSON object goes as it came.
func withServiceTier(body []byte, tier string) []byte {
cur, found, ok := topMember(body, tierField)
if !ok || !found && tier == "" {
return body
}
want := []byte(`"` + tier + `"`)
if found && bytes.Equal(bytes.TrimSpace(cur), want) {
return body
}
b := withoutTopMember(body, tierField)
if tier == "" {
return b
}
i := bytes.IndexByte(b, '{')
if i < 0 {
return body
}
sep := ","
if j := skipJSONSpace(b, i+1); j < len(b) && b[j] == '}' {
sep = ""
}
return insertAt(b, i+1, `"`+tierField+`":`+string(want)+sep)
}