Files
magpie/internal/provider/codex_credits.go
T
forever 8754221203 gateway, usage: when the credits switch holds every account, the record and 429 say so accurately; the switch's text says it follows the latest reading (#999)
* gateway: a request refused as every account is held records why, caps or credits

When every account was held, the request's record always read "every
account at its usage cap", though the client's 429 (cappedError) already
told a usage cap, credits set not to be spent, and the two together
apart. The record now comes from the same classification (heldBy): the
cap-only wording is kept, credits-only reads "every account held: set not
to spend its credits", and a mix names both.

TestCodexNoCreditsEveryAccount checks the record for both requests; with
the old fixed string it fails with: recorded 429 "every account at its
usage cap" (twice).

Follow-up to #974.

* gateway: the 429 for accounts held off their credits says to spend them only if one holds some

When every account was held as set not to spend its credits, the 429
told the user to let an account spend them to go on now. An account that
holds no credits is refused by the vendor at 100% all the same, so
following that got one more 429. The hint now reads "if an account holds
credits, let it spend them", in the credits-only and the mixed message.
The gateway's allowance readings carry only the windows, and a Codex
reading's balance is "" for none, unlimited and unreadable alike, so a
known-empty balance isn't available to drop the hint by.

TestCodexNoCreditsEveryAccount checks the new wording in both messages;
with the old one it fails with: no "if an account holds credits, let it
spend them" in the body (both requests).

Follow-up to #974.

* usage, docs: a Codex account set not to spend its credits is held by magpie's latest reading

The Use credits hint, magpie quota credits' help and the docs said that,
turned off, the account's credits aren't spent. The hold goes by magpie's
latest reading of the account's windows, which is read again about once a
minute, so for a minute or two after a window fills, or while nothing is
read yet, a request can still reach the account and spend some credits.
Each now says the hold follows that reading, refreshed about every minute
(Usage card in en/zh/ja/de, quota help, reference.md, gateway-routing.md).

codex-credits (Chromium and WebKit, en/zh/ja/de: 8/8), i18n-keys (1/1),
gui-ja (3/3) and gui-de (3/3) pass; codex-credits checks the reworded
string has its zh, ja and de.

Follow-up to #974.
2026-10-06 16:09:00 +08:00

60 lines
2.4 KiB
Go

package provider
// Whether a Codex account spends its credits. A ChatGPT account that holds
// credits (#571) keeps answering once a usage window is used up: the vendor
// spends the credits instead, and a request routed to it goes through. That
// is the default, and it carries a task on when the allowance runs out.
// Turned off for an account, routing holds it as used up at 100% of a
// window, as a usage cap holds it at its share (account_caps.go): never
// tried till the window renews, so the user's other accounts, groups and
// fallbacks take the request rather than it spending the credits. The hold
// goes by magpie's latest reading (Allowances, read again about every
// minute), so a request in the minute after a window fills may still reach
// the account. An account with
// no credits is refused by the vendor at 100% either way; the switch only
// spares it the request.
//
// It doesn't change which account Codex is signed in to: NextLogin moves
// off an account at its routing's spent share (100% at most) and onto none
// past it, credits or not. Unlike the cap, it holds at the vendor's own
// 100%, so an account that spends its resets by itself spends one there
// (the gateway's autoReset) where a cap never does.
import (
"slices"
"github.com/yetone/magpie/internal/settings"
)
// CodexCredits says whether the Codex account user spends its credits
// once a usage window is used up: true unless the user turned it off.
func CodexCredits(user string) bool {
return !slices.Contains(settings.Load().CodexNoCredits, accountKey(user))
}
// SetCodexCredits turns that on or off for user.
func SetCodexCredits(user string, on bool) error {
user = accountKey(user)
s := settings.Load()
s.CodexNoCredits = slices.DeleteFunc(s.CodexNoCredits, func(u string) bool { return u == user })
if !on && user != "" {
s.CodexNoCredits = append(s.CodexNoCredits, user)
}
return settings.Save(s)
}
// HoldShare is the share of its windows (in percent) at which routing
// holds the account user of p, of agent, as used up: its usage cap when it
// has one, 100 for a Codex account set not to spend its credits, 0 when
// nothing holds it before the vendor does. noCredits says it is the credits
// that hold it.
func HoldShare(p Provider, agent, user string) (share int, noCredits bool) {
if c := p.AccountCap(user); c > 0 {
return c, false
}
if agent == "codex" && user != "" && !CodexCredits(user) {
return 100, true
}
return 0, false
}