JackyFill asked for a choice in Codex's agent settings that rewrites
service_tier: follow the request, force Fast, force Ultra Fast. Codex's
settings on the Agents page now have "Codex service tier", picked from
the app menu: As Codex asks (the default, nothing changes), Standard,
Fast or Ultrafast.
The values are Codex's own request values (codex-rs protocol
config_types.rs ServiceTier, tui service_tier_resolution.rs): Fast is
"priority", Ultra Fast is "ultrafast", and Standard sends no
service_tier. Flex isn't offered: the ChatGPT backend's requests drop
it (codex_request.go).
settings.CodexServiceTier ("", "default", "priority", "ultrafast";
anything else refused) is set through POST
/api/settings/codex-service-tier. codexTiered rewrites the body where
Codex's requests come in, on both ways: codexBackend (Codex signed in,
/backend-api/codex) and the main handler's Codex-on-Responses block,
which a websocket turn goes through too. It applies only when the
request is Codex's (fromCodex or agentOf == codex). Everything after
treats it as Codex's own ask: an account whose list lacks Ultrafast is
put after a mate that has it or sent priority. A bare model (Codex's own
sign-in, whose plan magpie doesn't know) gets ultrafast only where one
of magpie's Codex accounts lists it on the model (UltraModels), else
priority. The body is edited with topMember/withoutTopMember, no full
decode. Fast picks still apply after, unchanged.
Run (mote):
- TestCodexServiceTierSet, TestWithServiceTier: ok. The test signs in a
Plus and a Pro ChatGPT account against a fake backend whose lists offer
gpt-6-luna's Ultrafast to Pro only, and checks the tier sent for each
setting x what Codex asked, through /v1/responses (account:tier tried)
and on /backend-api/codex for a model with Ultrafast and one without;
OpenCode's own priority is left alone; "flex" is refused.
- go test -count=1 -timeout 45m ./internal/settings ./internal/provider
./internal/gui ./internal/gateway: ok but internal/gui, which was red
twice: TestCoreScriptBudget (1,535,075 > 1,533,802) and
TestSettingsSaveKeepsWhatItDoesNotSend (a save of the Settings page
dropped the tier). Paid for by eight Codex-row comments cut in app.js
(now 1,533,416), no raise; settingsapi.Apply keeps CodexServiceTier
as it keeps the other Codex rows. At the pushed head: go test
./internal/settingsapi ./internal/settings ./internal/gui ok, the two
gateway tier tests ok.
- go vet darwin; GOOS=linux -tags nogui; GOOS=windows (gateway,
provider, settings, gui): clean. gofmt -l: clean.
- GUI, chromium + webkit: codex-service-tier (new, en/zh/zh-TW/ja/de at
900 and 400px: 20/20), agent-prefs (56/56, Codex's row list updated),
i18n-keys, i18n-en-no-cjk, settings-search, codex-titles,
codex-subagent-model, settings-general, codex-sheet-providers,
click-scroll: all pass; at the pushed head codex-service-tier,
agent-prefs, i18n-keys, settings-search, codex-titles,
codex-subagent-model, settings-general, click-scroll: 166/166.
Broken by hand:
- codexTiered returning the body as it came: TestCodexServiceTierSet red
(17 lines).
- the Ultrafast-to-Fast downgrade for a bare model removed: red on
gpt-6-sol (sent ultrafast, want priority).
- the GUI posting to the wrong path, and the row left out of Codex's
AGENT_PREFS: codex-service-tier red in chromium and webkit.
- settingsapi without the CodexServiceTier keep:
TestSettingsSaveKeepsWhatItDoesNotSend red.
Not tried: a real ChatGPT account's Ultrafast or Fast request with the
setting on (only the fake backend), a real Codex websocket session, and
Windows/Linux runs of the GUI.
63 lines
2.0 KiB
Go
63 lines
2.0 KiB
Go
package gateway
|
|
|
|
import (
|
|
"bytes"
|
|
"net/http"
|
|
"strings"
|
|
|
|
"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)
|
|
}
|
|
|
|
// 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)
|
|
}
|