The ChatGPT Codex backend says service_tier "auto" in response.created and "default" in response.completed whether priority ran or not. jjt's measurements on gpt-6.1-sol with a Plus account: Fast decodes at about 200 tok/s, without it about 110, and the reply says "default" both times (real-priority.sse, speed-plus.txt). magpie copied that into tier_served, so the usage log, its CSV and /v1/magpie/route said served = default for requests that ran Fast. servedTierAt (tier_record.go) leaves those two words empty when the reply came from CodexBase's host. It is applied once per routed try, in attempt (gateway.go, a defer, so every return path and the route's tries see it), and on the relay of Codex's own sign-in (codexUpstreamOn). OpenAI's API, where service_tier means what it says, keeps it as given: a provider at api.openai.com and Codex with an sk- key relayed to it. "priority" or "ultrafast" said by the ChatGPT backend is still kept. Display: tier_served is shown by the CSV (an empty cell), the JSON log and /v1/magpie/route (omitempty). No GUI page or TUI shows a tier (grep of internal/gui/assets and internal/tui for tier_served, tier_asked, tierServed: none), so no strings changed. docs/reference.md and docs/subsystems/gateway-routing.md say it. Tests: - TestChatGPTBackendTierUnknown (new): the backend says auto, then default (jjt's events, cut to the fields read); a routed try asked priority and one asked nothing, and Codex's own sign-in relayed, all record "" in usage and the route. - TestOpenAIAPITierKept (new): api.openai.com's "priority" and "default" kept, routed and through the sk- relay. - TestTierRecordedPerTry: its "none asked" case on the ChatGPT backend now expects "" where it expected "default". Run on mote (mote-run; the wider run at basecc81ad4ea, the tier run again after the rebase, at this commit onc37b8b659: ok, 22 pass): - go test ./internal/gateway -run 'Tier|ChatGPTBackend|OpenAIAPI' -count=1 -v: ok, 22 tests pass. - go test ./internal/usage: ok. go test ./internal/gateway -run 'Codex|Usage|Route|Tier': two reds, TestAntigravityTurnsCodexs- GPT5ThreadAwayOnce and TestCodexSpentGrokFailingIsTold, red the same on a fresh origin/mainc37b8b659and filed as #4362; the rest ok. - go vet ./internal/gateway, GOOS=linux and GOOS=windows: clean. - gofmt -l internal/gateway: clean. Broken by hand: servedTierAt returning t unchanged -> TestChatGPT- BackendTierUnknown and TestTierRecordedPerTry red ("served default"); the host check removed -> TestOpenAIAPITierKept red (served "", want "default"). Both green again with the fix. Not tried: a real ChatGPT account through the built gateway; the full Go suite (the coordinator asked for the targeted run under mote's load of ~200); Windows and Linux test runs (vet only). No GUI file reads the tier, so no GUI test was run.
122 lines
3.8 KiB
Go
122 lines
3.8 KiB
Go
package gateway
|
|
|
|
// The service tier a request asked for, what went out on each try and
|
|
// what the vendor said it ran at, for the usage log, its CSV and the
|
|
// route (#1784). Fast's and Ultrafast's priority processing bills at
|
|
// more than the default, and the tier a request went at could change on
|
|
// the way: a fast pick asks for priority, and an account whose plan
|
|
// doesn't offer Ultrafast is asked for priority in its place.
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"encoding/json"
|
|
"strings"
|
|
|
|
"github.com/tidwall/gjson"
|
|
|
|
"github.com/yetone/magpie/internal/provider"
|
|
)
|
|
|
|
// servedTierAt is the served tier a reply from host says, as the log keeps
|
|
// it. The ChatGPT backend's (CodexBase's host) "auto", in response.created,
|
|
// and "default", in response.completed, are what it says whether priority
|
|
// ran or not: on gpt-6.1-sol with a Plus account a Fast turn decoded at
|
|
// about 200 tok/s and one without at about 110, both "default" (jjt on
|
|
// Discord). There they tell nothing, and are kept as unknown (""), never
|
|
// as the default tier. OpenAI's API means them, and they are kept as said.
|
|
func servedTierAt(host, t string) string {
|
|
if (t == "auto" || t == "default") && host != "" && host == provider.HostOf(provider.CodexBase) {
|
|
return ""
|
|
}
|
|
return t
|
|
}
|
|
|
|
// bodyTier is the service tier a request body in proto asks for, in
|
|
// OpenAI's words (service_tier: "priority", "ultrafast", "flex" …);
|
|
// Claude's speed "fast" reads as "fast". "" when it asks for none.
|
|
func bodyTier(proto provider.Protocol, body []byte) string {
|
|
switch proto {
|
|
case provider.Anthropic:
|
|
if topValue(body, "speed").String() == "fast" {
|
|
return "fast"
|
|
}
|
|
fallthrough
|
|
case provider.Chat, provider.Responses:
|
|
if t := topValue(body, "service_tier"); t.Type == gjson.String {
|
|
return tierWord(t.String())
|
|
}
|
|
}
|
|
return ""
|
|
}
|
|
|
|
// topValue is gjson's value of body's top-level member key. A long body is
|
|
// asked through its split (topMember), made once a request: gjson walked
|
|
// the whole conversation for a member Codex sends after it (#3394).
|
|
func topValue(body []byte, key string) gjson.Result {
|
|
if len(body) >= validMemoMin {
|
|
if v, found, ok := topMember(body, key); ok {
|
|
if !found {
|
|
return gjson.Result{}
|
|
}
|
|
return gjson.ParseBytes(v)
|
|
}
|
|
}
|
|
return gjson.GetBytes(body, key)
|
|
}
|
|
|
|
// tierWord is a tier as the log keeps it: short, and nothing else.
|
|
func tierWord(t string) string {
|
|
t = strings.TrimSpace(t)
|
|
if len(t) > 32 {
|
|
return ""
|
|
}
|
|
return t
|
|
}
|
|
|
|
// noteWireTier leaves, where the request's context has a place for it
|
|
// (wireAt), the tier body goes out to the vendor at: the last request a
|
|
// try sent is the one that answered or failed.
|
|
func noteWireTier(ctx context.Context, to provider.Protocol, body []byte) {
|
|
if w, _ := ctx.Value(wireAtKey{}).(*wireAt); w != nil {
|
|
w.tier = bodyTier(to, body)
|
|
}
|
|
}
|
|
|
|
// upstreamTier is the service tier a reply (a JSON body, or one event of
|
|
// its stream) says it ran at: a top-level service_tier (a Chat body and
|
|
// its chunks, a Responses body), a Responses stream's response.service_tier,
|
|
// an Anthropic usage's service_tier. "" when it says none, which is never
|
|
// read as the tier that was asked.
|
|
func upstreamTier(b []byte) string {
|
|
if !bytes.Contains(b, []byte(`"service_tier"`)) {
|
|
return ""
|
|
}
|
|
var v struct {
|
|
Tier json.RawMessage `json:"service_tier"`
|
|
Response struct {
|
|
Tier json.RawMessage `json:"service_tier"`
|
|
} `json:"response"`
|
|
Usage struct {
|
|
Tier json.RawMessage `json:"service_tier"`
|
|
} `json:"usage"`
|
|
Message struct {
|
|
Usage struct {
|
|
Tier json.RawMessage `json:"service_tier"`
|
|
} `json:"usage"`
|
|
} `json:"message"`
|
|
}
|
|
if json.Unmarshal(b, &v) != nil {
|
|
return ""
|
|
}
|
|
for _, raw := range []json.RawMessage{v.Tier, v.Response.Tier, v.Usage.Tier, v.Message.Usage.Tier} {
|
|
var s string
|
|
if json.Unmarshal(raw, &s) == nil {
|
|
if s = tierWord(s); s != "" {
|
|
return s
|
|
}
|
|
}
|
|
}
|
|
return ""
|
|
}
|