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.
107 lines
4.0 KiB
Go
107 lines
4.0 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 (
|
|
"fmt"
|
|
"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))
|
|
}
|
|
|
|
// CodexCreditsLast says whether the Codex account user, which spends its
|
|
// credits, spends them only after everything else: once a usage window is
|
|
// used up, routing tries it after every account, key and fallback not on
|
|
// its credits (gateway's creditsLast) rather than in its place. Off unless
|
|
// the user set it, so a setting saved before it keeps its order.
|
|
func CodexCreditsLast(user string) bool {
|
|
user = accountKey(user)
|
|
s := settings.Load()
|
|
return user != "" && slices.Contains(s.CodexCreditsLast, user) && !slices.Contains(s.CodexNoCredits, user)
|
|
}
|
|
|
|
// Ways a Codex account spends its credits, for SetCodexCreditsMode.
|
|
const (
|
|
CreditsOn = "on" // in its place, the default
|
|
CreditsLast = "last" // after everything else
|
|
CreditsOff = "off" // never: held when a window is used up
|
|
)
|
|
|
|
// SetCodexCredits turns that on or off for user. On is in its place:
|
|
// it clears "last" too.
|
|
func SetCodexCredits(user string, on bool) error {
|
|
if on {
|
|
return SetCodexCreditsMode(user, CreditsOn)
|
|
}
|
|
return SetCodexCreditsMode(user, CreditsOff)
|
|
}
|
|
|
|
// CodexCreditsMode is how user spends its credits: CreditsOn,
|
|
// CreditsLast or CreditsOff.
|
|
func CodexCreditsMode(user string) string {
|
|
switch {
|
|
case !CodexCredits(user):
|
|
return CreditsOff
|
|
case CodexCreditsLast(user):
|
|
return CreditsLast
|
|
}
|
|
return CreditsOn
|
|
}
|
|
|
|
// SetCodexCreditsMode sets how user spends its credits: CreditsOn,
|
|
// CreditsLast or CreditsOff.
|
|
func SetCodexCreditsMode(user, mode string) error {
|
|
if mode != CreditsOn && mode != CreditsLast && mode != CreditsOff {
|
|
return fmt.Errorf("credits: %q is not on, last or off", mode)
|
|
}
|
|
user = accountKey(user)
|
|
s := settings.Load()
|
|
not := func(u string) bool { return u == user }
|
|
s.CodexNoCredits = slices.DeleteFunc(s.CodexNoCredits, not)
|
|
s.CodexCreditsLast = slices.DeleteFunc(s.CodexCreditsLast, not)
|
|
if user != "" {
|
|
switch mode {
|
|
case CreditsOff:
|
|
s.CodexNoCredits = append(s.CodexNoCredits, user)
|
|
case CreditsLast:
|
|
s.CodexCreditsLast = append(s.CodexCreditsLast, user)
|
|
}
|
|
}
|
|
return settings.Save(s)
|
|
}
|
|
|
|
// HoldCaps is the shares of its windows at which routing holds the
|
|
// account user of p, of agent, as used up: its usage cap and its windows'
|
|
// own (account_caps.go), and for a Codex account set not to spend its
|
|
// credits 100% of any window with no cap. Holds() is false when nothing
|
|
// holds it before the vendor does.
|
|
func HoldCaps(p Provider, agent, user string) WindowCaps {
|
|
c := p.CapsOf(user)
|
|
c.Credits = agent == "codex" && user != "" && !CodexCredits(user)
|
|
return c
|
|
}
|