alanwalk asked for "自动使用重置卡" to be split: they want a reset that is
about to expire used rather than lost, but a used-up week waited out rather
than spending a reset on it (and the other way round for others).
What changed:
- settings: codexAutoResetOnly narrows an account in codexAutoReset to
"week" or "expiring". An account with no entry keeps both, so every
existing config means what it meant: nothing to migrate, and no user
value is rewritten. Load drops entries for accounts not in the list or
with an unknown value.
- provider: CodexAutoResetWhen(user) reads both times. AutoUseCodexReset
(the week used up, called from the gateway's autoReset and
autoResetSignedIn through AutoResetsWeek) only acts when the week is on;
SpendExpiringCodexResets and routing's resetRunsOut only count on the
expiring reset when that is on. SetCodexAutoResetCase(user, when, on)
sets one time and keeps the other; SetCodexAutoReset still sets both.
Both off removes the account.
- gateway: POST /api/settings/codex-auto-reset takes an optional "when";
POST /api/settings keeps codexAutoResetOnly, which the page doesn't send.
- GUI: the account's card (Usage page and the menu bar panel) says
"Auto-use resets:" with each time on its own line and its own switch;
the brief account's one switch still sets both, its title naming each
time; Settings › Usage gives each account an Off/On per time. The status
line says which times are on now. "· auto-used before it expires" on
the resets held follows the expiring switch alone. Strings in zh, zh-TW,
ja and de.
- CLI/TUI: `magpie quota auto-reset <account> week|expiring on|off`; the
quota and accounts tables and the TUI say "auto", "auto when the week
runs out" or "auto before one expires".
What was run (worktree on origin/main 2fbbd1694):
- gofmt -l on the changed Go files: clean. go vet ./... (darwin),
GOOS=linux go vet -tags nogui ./..., GOOS=windows go vet ./...: clean.
- go test -tags nogui ./internal/provider ./internal/gateway ./internal/cli
./internal/tui ./internal/gui ./internal/settings: ok. The gateway
package hit go test's 10m default once while ~15 sibling sessions were
building on this machine (no test failed; TestRemoteMagpieModels was
merely running); with -timeout 30m it passed in 967s.
- New Go tests: TestCodexAutoResetCases (provider), a week-only step in
TestSpendExpiringCodexResets, an "only before one expires" case in
TestCodexAutoResetNot (gateway), CodexAutoResetOnly in
TestSettingsSaveKeepsWhatItDoesNotSend. Broken by hand: making
CodexAutoResetWhen ignore the narrowing turned all three reset tests
red (the gateway case answered 200, a week-only account read 2 and
spent 1); dropping the keep line in api.go turned the settings test red.
- GUI (Playwright, chromium and webkit): codex-auto-reset.test.cjs, now in
en, zh, zh-TW, ja and de at 1100 and 420 wide, flips each time's switch
on the card and the panel and checks posts carry "when", the other time
is kept, the status, the resets-held line, the brief switch, fit at 420,
no left border, no select; codex-auto-reset-settings.test.cjs does the
same for Settings › Usage. Both, with codex-resets-used, routing-auto-reset,
quota-curve (its class list now holds ar-split) and i18n-keys: 56 + 18
pass. Broken by hand: posting without "when" turned 20 of 21 red.
quota-curve failed 2 of 18 once while a go test build ran beside it; it
passed 15/15 serially and 18/18 again at concurrency 4 just after.
Not tried: a real ChatGPT account using or keeping a reset; the times are
driven with the faked usage reads the existing tests use.
99 lines
3.4 KiB
Go
99 lines
3.4 KiB
Go
package gateway
|
|
|
|
// A Codex rate-limit reset spent by itself: when everyone a request could
|
|
// go to is out of their allowance, a ChatGPT account the user lets spend
|
|
// its resets, its weekly window used up, spends one and is asked again
|
|
// (provider.AutoUseCodexReset has the rules).
|
|
|
|
import (
|
|
"context"
|
|
"log"
|
|
"slices"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/yetone/magpie/internal/provider"
|
|
)
|
|
|
|
// autoResetTimeout bounds the look at an account's week and the reset.
|
|
var autoResetTimeout = 20 * time.Second
|
|
|
|
// autoReset spends a reset of one of cands' Codex accounts, c — the last
|
|
// one, just out of its allowance — first: only when every other one of
|
|
// them sits out too. held are accounts left out as they won't spend their
|
|
// credits (planned.held): they sit out, and are out of their allowance as
|
|
// c is, so one of them may spend one after cands'. It says which account it
|
|
// was and what spending did.
|
|
func (s *Server) autoReset(ctx context.Context, cands []candidate, c candidate, held []candidate) (candidate, provider.ResetOutcome, bool) {
|
|
for _, x := range cands {
|
|
if x.restKey() == c.restKey() {
|
|
continue
|
|
}
|
|
_, resting := restOf(x.restKey())
|
|
if !resting && x.restID() != x.restKey() {
|
|
_, resting = restOf(x.restID())
|
|
}
|
|
if !resting {
|
|
return candidate{}, provider.ResetOutcome{}, false
|
|
}
|
|
}
|
|
seen := map[string]bool{}
|
|
for _, x := range append(append([]candidate{c}, cands...), held...) {
|
|
a := x.p.Account
|
|
if a == nil || a.User == "" || seen[x.restKey()] || !provider.AutoResetsWeek(a.Agent, a.User) {
|
|
continue
|
|
}
|
|
seen[x.restKey()] = true
|
|
if out, ok := autoResetOf(ctx, a.Agent, a.User); ok {
|
|
s.Unrest(x.restKey())
|
|
x.capped = nil // its windows started again: held no more
|
|
return x, out, true
|
|
}
|
|
}
|
|
return candidate{}, provider.ResetOutcome{}, false
|
|
}
|
|
|
|
// autoResetSignedIn is autoReset for a request relayed with Codex's own
|
|
// sign-in, the only account there is.
|
|
func (s *Server) autoResetSignedIn(ctx context.Context) (string, provider.ResetOutcome, bool) {
|
|
who, ok := provider.CodexSignedIn()
|
|
if !ok || !provider.AutoResetsWeek("codex", who) {
|
|
return "", provider.ResetOutcome{}, false
|
|
}
|
|
out, ok := autoResetOf(ctx, "codex", who)
|
|
if ok {
|
|
s.Unrest("codex@" + strings.ToLower(who))
|
|
}
|
|
return who, out, ok
|
|
}
|
|
|
|
// autoResetOf spends one of agent's account user's resets if its rules
|
|
// let it, and says whether the windows started again.
|
|
func autoResetOf(ctx context.Context, agent, user string) (provider.ResetOutcome, bool) {
|
|
ctx, cancel := context.WithTimeout(ctx, autoResetTimeout)
|
|
defer cancel()
|
|
out, err := provider.AutoUseCodexReset(ctx, user)
|
|
switch {
|
|
case err != nil:
|
|
log.Printf("%s reset for %s not used: %v", agent, user, err)
|
|
case out.Code == "reset":
|
|
log.Printf("%s reset used for %s, its week used up: %s", agent, user, out.Text())
|
|
case out.Code != "":
|
|
log.Printf("%s reset for %s not used: %s", agent, user, out.Text())
|
|
}
|
|
return out, err == nil && out.Code == "reset"
|
|
}
|
|
|
|
// unhold moves the account c of a plan's left, held as it won't spend its
|
|
// credits, to its order: a reset started its windows again, and it is
|
|
// tried.
|
|
func unhold(order, left []Weighed, c candidate) ([]Weighed, []Weighed) {
|
|
i := slices.IndexFunc(left, func(w Weighed) bool { return w.ID == c.rest && w.NoCredits })
|
|
if i < 0 {
|
|
return order, left
|
|
}
|
|
w := left[i]
|
|
w.Unlisted, w.Capped, w.CapBack, w.NoCredits = false, 0, nil, false
|
|
return append(slices.Clip(order), w), slices.Delete(slices.Clone(left), i, i+1)
|
|
}
|