willz asked for a cap per window: a friend's account should stop at 50%
of its five hours, while its week has its own 40%. Until now an account
had one usage cap, the same share of every window.
What the user sees: the account's pill still sets the cap for all its
windows and stays the default. Each window's meter on the account's row
is now a button: a click opens the app's own menu headed "Cap for
<window>" with the account's cap, No cap on this window, 50–90%, and
Other… typed in place. A window with its own cap says it beside its
figure ("Cap 50%", or "No cap"), its mark sits there, and the account's
pill says "Caps by window" when only windows have caps. The account is
held while any window is past its own cap, and the row's note, the
gateway's 429 and the Routing trace name that window's cap. CLI:
magpie provider account-cap <id> <account> --window "5 hours" 50
(none: no cap on that window; default: back to the account's), and
`account-cap <id>` lists the overrides.
How: Provider.AccountWindowCaps (providers.json, by account and the
window's capId, its name in lower case; 1–99, or 100 for none) beside
AccountCaps. provider.WindowCaps gives a window its share (its own, else
the account's); Allowance.CapHeld and capReached judge each window by
its own share and report the fullest holding window's cap.
provider.HoldShare becomes HoldCaps, used by gateway capHeld (plan and
group filtering, cappedError, trace) and codex_backend's route-or-relay
choice; codex_switch's NextLogin moves the agent off at per-window caps.
QuotaWindow carries capId; Limit.name is exported as Limit.Name. A
config with no window caps reads and routes exactly as before
(TestSetWindowCap reads an old one). The meter is a span with
role=button, Enter/Space: a <button> as a flex item of the wrapping
meter line kept WebKit's row at its one-line height until the next
layout, so the row grew under the pointer at the first click.
Verified:
- go test -tags nogui ./... in a sandbox HOME: all ok. New
TestWindowCapHeld and TestSetWindowCap (provider), TestWindowCapsHoldTheAccount
through the real gateway (5h 60%/cap 50 goes to the spare; 20/30 under
50/40 is used; 20/45 with the spare past its own cap is a 429 naming
"past its 40% cap"), TestAccountCapCmd --window cases.
- Break check: with WindowCaps.Of ignoring the window's own share,
TestWindowCapHeld fails ("5 hours at 60% of 50%: <nil>") and
TestWindowCapsHoldTheAccount fails (the request is answered 200 by the
capped account).
- internal/gui/tests/window-cap.test.cjs: Chromium and WebKit, 420 and
1100 wide, en, zh, zh-TW, ja, de (60 tests): held note per window,
marks, menu from the meter with no native select, keyboard, posts of
50, typed 45, 100 and 0, no page scroll on a click, no sideways scroll,
no left border stripes, every new string translated. With
windowOwnCapOf returning 0 it fails.
- account-cap, gui-zh-tw, gui-ja, gui-de, i18n-keys pass; full GUI suite
in both engines (483 files, concurrency 6): 4315 of 4323
pass; the 8 that failed (library-servers-bulk, purpose-filter,
routing-proxy, routing-zcode-blocked) touch no account row and all 32
of their tests pass when run alone
- gofmt -l clean; go vet -tags nogui ./... on darwin, linux and windows,
and windows without the tag for ., internal/gui, internal/provider,
internal/gateway; go build ./...
- Not tried in the real window with a live gateway or real accounts.
Docs: gateway-routing.md (HoldCaps, window caps, tests),
providers-accounts.md (AccountWindowCaps, tests), reference.md.
56 lines
2.4 KiB
Go
56 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)
|
|
}
|
|
|
|
// 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
|
|
}
|