Archivemylife on X sent a Responses request for gpt-6.1-sol on a Codex account with "service_tier": "fast" and got "default" back. magpie dropped the key: codexBody kept only "priority" and "ultrafast", and the IR's Fast (Chat and Responses parse) is set by "priority" alone, so "fast" never reached the ChatGPT backend on either API. "fast" is Codex's own name for its Fast mode. Codex never sends it: ServiceTier::from_request_value reads "fast" and "priority" alike as Fast and request_value sends "priority" (openai/codex codex-rs/protocol/src/config_types.rs; step_settings.rs normalizes the legacy "fast" the same way; read at 7fb4014d5). - handle() turns a top-level string service_tier "fast" into "priority" on Chat and Responses (fastTier), before the Codex and key tier settings, routing and tier_asked. Those settings still win: they are the user's own value. Other tiers, "Fast", nested or non-string values go as they came. - codexBody sends "fast" as "priority" too, for any path that reaches it without the handler. - Anthropic Messages: Claude's equivalent is speed "fast", already read as Fast and sent to the ChatGPT backend as priority (covered by the new test). Anthropic's own service_tier (auto/standard_only) is about capacity, not speed, and is not mapped. - Not changed: "flex" (Codex's Flex) is still dropped for ChatGPT accounts, as before; responses.go untouched. The backend's reply still says "auto" then "default" with priority honoured (servedTierAt already knows this), so the client's response service_tier is not proof either way; speed is. Run: - Red on the old code (production files from HEAD, tests new): TestFastTierWordReachesCodex red on Responses fast, whole fast, Chat fast, whole fast; TestCodexBodyServiceTier red on "fast". Green with the change. - Real Pro ChatGPT account in a sandbox HOME (codex logins only, refresh tokens blanked, no agent config touched), binary built at this change, gpt-6.1-sol: Responses "fast" (whole and streamed), Chat "fast" and Messages speed "fast" each went out with tier priority (trace tries); "default" went with none. Streamed Responses reply said service_tier auto/default. Two pairs, 600-word essay, effort low: priority 28.3 and 30.1 tok/s, none 22.3 and 21.2 tok/s. - mica: go test -count=1 -timeout 45m ./... at this tree: 103 ok, no FAIL. go vet ./..., GOOS=linux go vet -tags nogui ./..., GOOS=windows go vet ./..., go build: ok. gofmt -l: empty. That tree was on be6f8b730; after rebasing onto3f4372cc9(a gateway change), internal/gateway (962s) and internal/provider ran ok again at this tree on mica. Rebased again ontoc5f2a67d0(routing.go, agy quota): go vet ./internal/gateway ./internal/provider and go test -run "Tier|Fast|Codex|Agy" on both packages ok at the pushed tree. Not tried: a Plus account; Codex's own sign-in route (codexBackend), where Codex itself never sends "fast".
102 lines
3.6 KiB
Go
102 lines
3.6 KiB
Go
package gateway
|
|
|
|
import (
|
|
"bytes"
|
|
"net/http"
|
|
"strings"
|
|
|
|
"github.com/tidwall/gjson"
|
|
|
|
"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)
|
|
}
|
|
|
|
// fastTier is body with a service_tier of "fast" as "priority", every
|
|
// other byte as it came. "fast" is Codex's own name for its Fast mode,
|
|
// which Codex sends as "priority" and nothing else
|
|
// (ServiceTier::from_request_value in openai/codex's config_types.rs,
|
|
// which reads "fast" and "priority" alike as Fast): a client that writes
|
|
// Codex's word (Archivemylife on X, gpt-6.1-sol on a Codex account) had
|
|
// it dropped by the ChatGPT backend's filter (codexBody) and by every
|
|
// check of the IR's Fast, which go by "priority". Any other tier, and a
|
|
// body with none, goes as it came.
|
|
func fastTier(body []byte) []byte {
|
|
if t := topValue(body, tierField); t.Type != gjson.String || t.Str != "fast" {
|
|
return body
|
|
}
|
|
return withServiceTier(body, "priority")
|
|
}
|
|
|
|
// 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)
|
|
}
|