gokoshiki wants a Codex subscription first, then an OpenAI API key, then the subscription's credits. Until now an account at 100% that holds credits either kept its place and spent them there (on) or was held (off). This adds a third way per account, "last": - settings: CodexCreditsLast []string, empty by default, so a setting saved before it keeps its order. provider.CodexCreditsLast, CodexCreditsMode and SetCodexCreditsMode(user, on|last|off); SetCodexCredits(on) still means "in its place" and clears last. - gateway: candidate.creditsLast is true for such an account once a window for the model is used up and it is going on its credits (a call ChatGPT counts in no window stays in its place). restLast orders ready, then resting, then those, and marks Weighed.CreditsLast. restLast runs over the asides too, so such an account goes behind a provider's reserves (Accounts at once,2c6b7c063) as well. lastBehind keeps In turn's next turn (after), a new conversation's spread (leastHeld) and a subagent's lead (readyForNew) from bringing it forward, and a kept conversation moves off it to one not on its credits (Affinity.Why "credits"). - CLI: magpie quota credits <account> [on|last|off]; alone it lists accounts set off and last. - API: POST /api/settings/codex-credits takes {user, on, last?}. - GUI: under a Codex account's Use credits, while on, a second switch "Only after every other account, key and fallback", in the Usage page's card and the tray panel's (both wrap it to its own line at 360 to 900px), and the panel's folded brief says "On, after everything else". The Routing page's trace says why such an account is behind (aside, the kept conversation's why, the row tag). zh, zh-TW, ja, de. creditsButton and the new switch share creditsSwitch, and the Codex options' comments in app.js were cut, so the core budget holds with no raise (1,532,664 of 1,533,105). - docs: reference.md (quota credits) and gateway-routing.md. Tests: - gateway: TestCodexCreditsLastAfterTheOthers (two accounts, In order: the one at 100% on its credits goes behind, the trace says so, and back in place when turned on), TestCodexCreditsLastAfterTheFallback (both accounts at 100% and set last: the API key the provider falls back to answers before either one's credits; with the credits in their place an account answers on them, as before), TestCodexCreditsLastNotKept (a kept conversation moves off it). All three red with creditsLast and lastBehind forced false. - provider: TestCodexCreditsLastMode. - gui: settings_keep_test carries CodexCreditsLast; tests/codex-credits-last.test.cjs (Chromium and WebKit, en zh zh-TW ja de; Usage at 900 and 440, the panel at 440 and 360, the folded brief, the Routing page's aside and why, every key in I18N). Red on the old assets (no .ar-credits-last). Run at this change on origin/mainc76bcaea7, on mote (pushed onfd70def82, whose one commit since,fd70def82, changes only the Providers poll's Go files in internal/gui): gofmt clean; go vet of gateway, provider, gui, cli and settings ok; their tests ok but internal/gui's #4514 and #3949, both on main. The 8 commits betweend6aa9cdf6andc76bcaea7touch none of this change's files. Run at this change on origin/maind6aa9cdf6, Go 1.26.8: - mote: gofmt -l clean; go vet ./..., and GOOS=linux and GOOS=windows go vet -tags nogui ./...: ok. go test -count=1 -timeout 45m ./...: 85 packages ok; internal/gui red only on TestSettingsSaveKeepsWhatIt DoesNotSend's SearchAll (#4514), TestMainWindowCorners (#3949) and TestColdTurnsTaken (#4510), all three on main. TestCoreScriptBudget passes. - WSL magpie-desktop, Playwright 1.62.0, both engines: codex-credits*, i18n-keys, gui-ja, gui-zh-tw, panel-options-fold, provider-at-once and every routing-* file: 2127 tests, 2064 pass. Every codex-credits and codex-credits-last test passed (26). The red files (gui-ja, 19 routing files) run again at this tree and atd6aa9cdf6, alike: 200 tests, 61 and 57 red. gui-ja is #4580; the routing reds are timeouts and waits, on both. The one file red at this tree only, routing-request-model (WebKit 420px "the model is cut"), is 5 of 5 green alone at both, and with six copies at once red at both (29 and 27 of 30 here, 28 and 27 of 30 atd6aa9cdf6): a fixed 150ms wait after a resize, filed as #4650. Nothing this change adds to Routing draws unless an account is set last (fixtures don't). - Earlier, at 3564eb04b (this change on7428f3229), WSL: the whole GUI suite beside two other sessions' suites, 16944 tests, 16496 pass; its 102 red files again at that head and at7428f3229, 444 and 425 red. The files red at the head only, run alone: view-open-wheel, usage-accounts, add-button, usage-ledger-open-settled 0 red; view-wheel-after-click, view-wheel-unheard and context-map (#3488) red alone at7428f3229too. mote, the credits and i18n files: 172 of 174, the two #4580 and a click timeout in quota-curve at load 148 (alone 30 of 30). Not tried: a real ChatGPT account at 100% with credits beside a real OpenAI key; the order is shown with the gateway's fake Codex backend.
181 lines
7.0 KiB
Go
181 lines
7.0 KiB
Go
package provider
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"slices"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/yetone/magpie/internal/settings"
|
|
)
|
|
|
|
// The credits a ChatGPT account holds besides its windows are its balance
|
|
// (#571), shown when it has some to spend and they are not unlimited.
|
|
// Routing is told the account goes on past its windows on them (onCredits:
|
|
// every window's Credits, and the allowance's OnCredits) when it holds
|
|
// some or they are unlimited, as the Codex app reads has_credits; a
|
|
// balance said and spent is none.
|
|
func TestCodexCreditsBalance(t *testing.T) {
|
|
for _, c := range []struct {
|
|
name string
|
|
credits any
|
|
want string
|
|
onCredits bool
|
|
}{
|
|
{"none said", nil, "", false},
|
|
{"held", map[string]any{"has_credits": true, "unlimited": false, "balance": "1234.5"}, "1234.5 credits", true},
|
|
{"a few", map[string]any{"has_credits": true, "unlimited": false, "balance": "42"}, "42 credits", true},
|
|
{"none held", map[string]any{"has_credits": false, "unlimited": false, "balance": "0"}, "", false},
|
|
{"spent", map[string]any{"has_credits": true, "unlimited": false, "balance": "0"}, "", false},
|
|
{"unlimited", map[string]any{"has_credits": true, "unlimited": true, "balance": ""}, "", true},
|
|
{"unreadable", map[string]any{"has_credits": true, "balance": "lots"}, "", true},
|
|
} {
|
|
t.Run(c.name, func(t *testing.T) {
|
|
fake := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
body := map[string]any{"plan_type": "plus", "rate_limit": map[string]any{
|
|
"primary_window": map[string]any{"used_percent": 100, "limit_window_seconds": 18000}}}
|
|
if c.credits != nil {
|
|
body["credits"] = c.credits
|
|
}
|
|
json.NewEncoder(w).Encode(body)
|
|
}))
|
|
defer fake.Close()
|
|
old := CodexBase
|
|
CodexBase = fake.URL + "/backend-api/codex"
|
|
defer func() { CodexBase = old }()
|
|
plan, windows, _, credits, _, err := codexWindows(context.Background(), "tok", "acct-1")
|
|
if err != nil || plan != "plus" || len(windows) != 1 {
|
|
t.Fatalf("plan %q, windows %v, err %v", plan, windows, err)
|
|
}
|
|
if credits != c.want {
|
|
t.Errorf("credits %q, want %q", credits, c.want)
|
|
}
|
|
if windows[0].Credits != c.onCredits || allowanceOf(windows, time.Now()).OnCredits() != c.onCredits {
|
|
t.Errorf("on credits %v / %v, want %v", windows[0].Credits, allowanceOf(windows, time.Now()).OnCredits(), c.onCredits)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// Whether an account spends its credits is kept by account, whatever its
|
|
// case: on unless turned off. Turned off, a Codex account is held at 100%
|
|
// of its windows; a cap holds it sooner, and nothing holds another
|
|
// vendor's account for its credits.
|
|
func TestCodexCreditsSwitch(t *testing.T) {
|
|
signIn(t)
|
|
if !CodexCredits("me@example.com") {
|
|
t.Fatal("off before it was turned off")
|
|
}
|
|
if err := SetCodexCredits(" Me@Example.com ", false); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if got := settings.Load().CodexNoCredits; !slices.Equal(got, []string{"me@example.com"}) {
|
|
t.Fatalf("kept %v", got)
|
|
}
|
|
if CodexCredits("ME@example.com") || !CodexCredits("other@example.com") {
|
|
t.Fatal("read back wrong")
|
|
}
|
|
p := Provider{ID: "codex", AccountCaps: map[string]int{"capped@example.com": 70}}
|
|
for _, c := range []struct {
|
|
agent, user string
|
|
share int
|
|
noCredits bool
|
|
}{
|
|
{"codex", "me@example.com", 100, true},
|
|
{"codex", "other@example.com", 0, false},
|
|
{"claude", "me@example.com", 0, false},
|
|
{"codex", "capped@example.com", 70, false},
|
|
} {
|
|
if share, no := HoldCaps(p, c.agent, c.user).Of("5 hours"); share != c.share || no != c.noCredits {
|
|
t.Errorf("%s %s: held at %d (credits %v)", c.agent, c.user, share, no)
|
|
}
|
|
}
|
|
if err := SetCodexCredits("me@example.com", true); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !CodexCredits("me@example.com") || len(settings.Load().CodexNoCredits) != 0 {
|
|
t.Fatal("still off")
|
|
}
|
|
}
|
|
|
|
// Spending credits last is a third way, kept by account whatever its
|
|
// case: set, the account still spends them (nothing holds it), and off
|
|
// or on clears it; a mode not known is refused, nothing written.
|
|
func TestCodexCreditsLastMode(t *testing.T) {
|
|
signIn(t)
|
|
if CodexCreditsMode("me@example.com") != CreditsOn || CodexCreditsLast("me@example.com") {
|
|
t.Fatal("last before it was set")
|
|
}
|
|
if err := SetCodexCreditsMode(" Me@Example.com ", CreditsLast); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
s := settings.Load()
|
|
if !slices.Equal(s.CodexCreditsLast, []string{"me@example.com"}) || len(s.CodexNoCredits) != 0 {
|
|
t.Fatalf("kept last %v, off %v", s.CodexCreditsLast, s.CodexNoCredits)
|
|
}
|
|
if !CodexCreditsLast("ME@example.com") || !CodexCredits("me@example.com") || CodexCreditsMode("me@example.com") != CreditsLast || CodexCreditsLast("other@example.com") {
|
|
t.Fatal("read back wrong")
|
|
}
|
|
if share, _ := HoldCaps(Provider{ID: "codex"}, "codex", "me@example.com").Of("5 hours"); share != 0 {
|
|
t.Fatalf("held at %d: last still spends them", share)
|
|
}
|
|
if err := SetCodexCreditsMode("me@example.com", "sometimes"); err == nil || !CodexCreditsLast("me@example.com") {
|
|
t.Fatalf("a mode not known: %v", err)
|
|
}
|
|
if err := SetCodexCredits("me@example.com", false); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if s := settings.Load(); len(s.CodexCreditsLast) != 0 || CodexCreditsMode("me@example.com") != CreditsOff {
|
|
t.Fatalf("off kept last: %v", s.CodexCreditsLast)
|
|
}
|
|
if err := SetCodexCreditsMode("me@example.com", CreditsLast); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := SetCodexCredits("me@example.com", true); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if s := settings.Load(); len(s.CodexCreditsLast)+len(s.CodexNoCredits) != 0 || CodexCreditsMode("me@example.com") != CreditsOn {
|
|
t.Fatalf("on kept last: %v", s.CodexCreditsLast)
|
|
}
|
|
}
|
|
|
|
// A reset spent starts an account's windows again: what was read of them
|
|
// before is forgotten, so what holds the account at a share of them (its
|
|
// cap, its credits not spent) holds it no more, and they are read again
|
|
// at once; the other accounts' readings stay.
|
|
func TestRenewedAccountForgetsItsAllowance(t *testing.T) {
|
|
signIn(t)
|
|
usedCache.Lock()
|
|
usedCache.m, usedCache.at, usedCache.loading = map[string]map[string]Allowance{}, map[string]time.Time{}, map[string]chan struct{}{}
|
|
usedCache.stale = nil
|
|
usedCache.m["codex"] = map[string]Allowance{
|
|
"me@example.com": {{Used: 100, Span: 5 * time.Hour}},
|
|
"spare@example.com": {{Used: 10, Span: 5 * time.Hour}},
|
|
}
|
|
usedCache.at["codex"] = time.Now()
|
|
handed := usedCache.m["codex"]
|
|
usedCache.Unlock()
|
|
t.Cleanup(func() {
|
|
usedCache.Lock()
|
|
usedCache.m, usedCache.at, usedCache.loading = map[string]map[string]Allowance{}, map[string]time.Time{}, map[string]chan struct{}{}
|
|
usedCache.stale = nil
|
|
usedCache.Unlock()
|
|
})
|
|
renewedNow("codex", "Me@Example.com")
|
|
usedCache.Lock()
|
|
m, at := usedCache.m["codex"], usedCache.at["codex"]
|
|
usedCache.Unlock()
|
|
if _, ok := m["me@example.com"]; ok {
|
|
t.Fatalf("still read as before: %v", m["me@example.com"])
|
|
}
|
|
if _, ok := m["spare@example.com"]; !ok || !at.IsZero() {
|
|
t.Fatalf("spare kept %v, read again at once %v", ok, at.IsZero())
|
|
}
|
|
if _, ok := handed["me@example.com"]; !ok {
|
|
t.Fatal("the map handed out before was changed")
|
|
}
|
|
}
|