The same GLM Coding Plan team seat was added twice: as a key provider the
user had named, and as a ZCode plugin account. The Usage page kept only the
account's card, which read "31****36@qq.com" or just "ZCode", and whenever
either read failed ("BigModel API keys: not JSON") or a window had no reset
yet, the key's card came back beside it.
Seat (the flicker): the old check returned "different" on any error. It
now returns same, different or unknown (seatMatch). A match is remembered in
seats.json beside quotas.json; while the remembered account is signed in an
unknown check keeps the key card hidden, windows that disagree forget it,
and a signed-out account brings the key card back (seatCards, used by
quotas, Allotted and CachedCards). A failed read is "unknown", never "not
the same".
Names: providers.json gains accountNames (by the account in lower case,
like accountCaps), set by provider.SetAccountName. They are magpie's own
and never written into the vendor's or a plugin's sign-in. Built-in and
plugin accounts both take one; key providers are refused. Each card carries
Alias (the user's name, else the seat) and Seat (the key provider's name,
and the key's when it has several).
- GUI: the editor's account row shows the name with the address beside
it, or the address with "≡ <key plan>"; a click or Enter renames it in
place (posted once to provider/accountname; Escape leaves it, empty
clears it). The name stays selectable text: dragging across it renames
nothing.
The Usage page's cards and the tray panel name the account the same way.
Hide accounts hides the name too. Strings in zh, zh-TW, ja, de.
- The tray tooltip says "ZCode · <name>".
- CLI: `magpie accounts alias <agent> <email> [<name>|--clear]`.
`magpie accounts` and `magpie quota` show the name; quota also matches
on it.
- /v1/magpie/quotas carries alias and seat.
Run:
- account_seats_test TestSeatKeptThroughAFailedRead fails on the old code
("a failed read brought the key's card back") and passes now.
TestAccountNameOfAPluginAccount uses the fake plugin and checks that
plugin-auth.json and logins.json are byte-for-byte unchanged.
TestAccountsAlias checks that Codex's auth.json is unchanged.
TestTrayNamesTheAccountByItsAlias.
- account-alias.test.cjs: Chromium and WebKit; en, zh, zh-TW, ja, de;
900 and 360px; 20/20 pass. It covers no scroll on a click, keyboard,
selectable text, no select, no stripe, no sideways scroll and masking.
With the post-once guard removed it goes red. account-arrange (text
selection) passes in both engines.
- Sandbox HOME, built binary, vendors behind a dead proxy:
- `magpie accounts alias codex Me@Example.com 某某·GLM高级版`, then
`magpie accounts` and `magpie quota` showed the name.
- An unknown account was refused.
- `magpie web` on spare ports: POST /api/provider/accountname saved
"Team seat" (whitespace folded), and an unknown account got an error.
- Gateway /v1/magpie/quotas and /api/usage/quotas carried the alias.
- auth.json's sha was unchanged throughout.
- go test -tags nogui ./... (temp HOME, load ~200): all ok except
TestSetAnswersWhileDevinAndProvidersHang (15.5s under load), which
passes alone 3/3 at ~250ms.
- gofmt -l; GOOS=linux and GOOS=windows go vet on internal/provider,
internal/gui and the root package.
- The whole GUI suite in Chromium and WebKit, each failed file rerun
alone. These are red on the parent commit 472b36cec as well:
mobile-web (9), usage-heatmap (11), old-webkit (Safari 15 :has and
:focus-visible rules), usage-alert on WebKit (2), and routing-proxy,
which flakes 2 of 3 runs on the base. Every other file passes alone.
Not run: a real GLM key plus a real ZCode account. The seat match against
real BigModel/ZCode answers is covered only by the fake-upstream tests.
169 lines
6.6 KiB
Go
169 lines
6.6 KiB
Go
package provider
|
|
|
|
import (
|
|
"context"
|
|
"math"
|
|
"time"
|
|
)
|
|
|
|
// Quotas is what is left everywhere magpie can ask: the signed-in
|
|
// subscriptions' windows, the plans bought with a key, and the keys'
|
|
// balances, as the Usage page shows them. The vendors are asked at once;
|
|
// what doesn't answer before ctx ends comes back with an error.
|
|
func Quotas(ctx context.Context) []SubscriptionQuota {
|
|
subs, plans, balances := quotas(ctx)
|
|
return cardsServed(subs, plans, balances, LastServed())
|
|
}
|
|
|
|
// Allotted is Quotas without the keys' balances: the accounts and plans
|
|
// whose allowance runs out and starts again, which is what magpie quota
|
|
// wait waits on; no key's balance is asked for.
|
|
func Allotted(ctx context.Context) []SubscriptionQuota {
|
|
plans, subs := seatCards(PlanQuotas(ctx), SubscriptionUsage(ctx))
|
|
return append(withAccountNames(subs), plans...)
|
|
}
|
|
|
|
func quotas(ctx context.Context) (subs, plans, balances []SubscriptionQuota) {
|
|
b := make(chan []SubscriptionQuota, 1)
|
|
p := make(chan []SubscriptionQuota, 1)
|
|
r := make(chan []SubscriptionQuota, 1)
|
|
go func() { b <- KeyBalances(ctx) }()
|
|
go func() { p <- PlanQuotas(ctx) }()
|
|
go func() { r <- RemoteCards(ctx) }()
|
|
plans, subs = seatCards(<-p, SubscriptionUsage(ctx))
|
|
subs, balances = withAccountNames(subs), <-b
|
|
// a remote magpie's cards, each after this computer's of its kind
|
|
for _, q := range <-r {
|
|
switch q.Kind {
|
|
case "plan":
|
|
plans = append(plans, q)
|
|
case "balance":
|
|
balances = append(balances, q)
|
|
default:
|
|
subs = append(subs, q)
|
|
}
|
|
}
|
|
return subs, plans, balances
|
|
}
|
|
|
|
// Quota is one account's or key's allowance as magpie quota --json and
|
|
// the gateway's GET /v1/magpie/quotas tell it, for an agent choosing
|
|
// where to send its work: Provider is the first part of the models'
|
|
// ids (provider/model) for a key's plan or balance, the agent for a
|
|
// subscription.
|
|
type Quota struct {
|
|
Provider string `json:"provider"`
|
|
Name string `json:"name"`
|
|
Kind string `json:"kind"` // subscription, plan (bought with a key) or balance (a key's money)
|
|
Plan string `json:"plan,omitempty"`
|
|
User string `json:"user,omitempty"`
|
|
Windows []QuotaSpan `json:"windows"`
|
|
Balance string `json:"balance,omitempty"`
|
|
Error string `json:"error,omitempty"`
|
|
AsOf *time.Time `json:"asOf,omitempty"` // the cached reading's time, nil for a new one
|
|
// ReadAt is when the windows or balance shown were read, nil when not
|
|
// known; a Claude account's can be well before now (claudeReadAt).
|
|
ReadAt *time.Time `json:"readAt,omitempty"`
|
|
// Until is when the plan's paid time ends, renewed then when Renew is
|
|
// "auto", over when "off", either when "".
|
|
Until *time.Time `json:"until,omitempty"`
|
|
Renew string `json:"renew,omitempty"`
|
|
// Resets are a Codex account's rate-limit resets, when it holds any.
|
|
Resets *ResetCredits `json:"resets,omitempty"`
|
|
// LastServedAt is when the account, plan or key last answered a
|
|
// request through the gateway, nil when it hasn't in the last 30 days
|
|
// (served.go); Last is on the latest of them (#570).
|
|
LastServedAt *time.Time `json:"lastServedAt,omitempty"`
|
|
Last bool `json:"last,omitempty"`
|
|
// From is the remote magpie whose account this is, by its name here
|
|
// (remote_quotas.go); "" for this computer's own.
|
|
From string `json:"from,omitempty"`
|
|
// Alias is the account's name of the user's own, else Seat: the name
|
|
// of the key plan whose card this account's stands in for (#1515).
|
|
Alias string `json:"alias,omitempty"`
|
|
Seat string `json:"seat,omitempty"`
|
|
}
|
|
|
|
// QuotaSpan is one window of an allowance: how much of it is used and
|
|
// left, in percent, and when it starts again.
|
|
type QuotaSpan struct {
|
|
Unlimited bool `json:"unlimited,omitempty"`
|
|
Name string `json:"name"`
|
|
Used float64 `json:"used"`
|
|
Remaining float64 `json:"remaining"`
|
|
ResetsAt *time.Time `json:"resetsAt,omitempty"`
|
|
Display string `json:"display,omitempty"` // the vendor's own count, "1.2k / 3k"
|
|
// Amount of Limit in Unit: the window's count, used, when the vendor
|
|
// counts it so ("credits")
|
|
Amount float64 `json:"amount,omitempty"`
|
|
Limit float64 `json:"limit,omitempty"`
|
|
Unit string `json:"unit,omitempty"`
|
|
Pool string `json:"pool,omitempty"` // the shared pool the window draws on ("Gemini")
|
|
}
|
|
|
|
// QuotaReport is Quotas as Quota, the reset times made absolute from now.
|
|
func QuotaReport(ctx context.Context, now time.Time) []Quota {
|
|
subs, plans, balances := quotas(ctx)
|
|
return withServed(quotaReport(subs, plans, balances, now), LastServed())
|
|
}
|
|
|
|
// quotaReport assembles the cards from readings the tests feed directly.
|
|
func quotaReport(subs, plans, balances []SubscriptionQuota, now time.Time) []Quota {
|
|
out := []Quota{}
|
|
for _, g := range []struct {
|
|
kind string
|
|
qs []SubscriptionQuota
|
|
}{{"subscription", subs}, {"plan", plans}, {"balance", balances}} {
|
|
for _, q := range g.qs {
|
|
r := Quota{Provider: q.Provider, Name: q.Name, Kind: g.kind, Plan: q.Plan, User: q.User, AsOf: q.AsOf, ReadAt: q.ReadAt,
|
|
Windows: []QuotaSpan{}, Balance: q.Balance, Error: q.Error, Until: q.Until, Renew: q.Renew, Resets: q.Resets, From: q.From, Alias: q.Alias, Seat: q.Seat}
|
|
// a pool's own windows stand in for the models' drawing on it,
|
|
// as the usage page shows them
|
|
for _, w := range PooledWindows(q.Windows) {
|
|
s := QuotaSpan{Unlimited: w.Unlimited, Name: w.Name, Used: w.Used, Remaining: max(0, 100-w.Used), ResetsAt: w.ResetsAt, Display: w.Display,
|
|
Amount: w.Amount, Limit: w.Limit, Unit: w.Unit, Pool: w.Pool}
|
|
if s.ResetsAt == nil && w.ResetSecs > 0 {
|
|
t := now.Add(time.Duration(w.ResetSecs) * time.Second)
|
|
s.ResetsAt = &t
|
|
}
|
|
r.Windows = append(r.Windows, s)
|
|
}
|
|
out = append(out, r)
|
|
}
|
|
}
|
|
return out
|
|
}
|
|
|
|
// ResetClock is when a window starts again, on the clock: "14:30" today,
|
|
// "tomorrow 09:00", "Wed 14:30" within the week, else "Oct 3 14:30".
|
|
func ResetClock(at, now time.Time) string {
|
|
at, now = at.Local(), now.Local()
|
|
day := func(t time.Time) time.Time { y, m, d := t.Date(); return time.Date(y, m, d, 0, 0, 0, 0, time.Local) }
|
|
switch days := int(math.Round(day(at).Sub(day(now)).Hours() / 24)); {
|
|
case days <= 0:
|
|
return at.Format("15:04")
|
|
case days == 1:
|
|
return "tomorrow " + at.Format("15:04")
|
|
case days < 7:
|
|
return at.Format("Mon 15:04")
|
|
}
|
|
return at.Format("Jan 2 15:04")
|
|
}
|
|
|
|
// PlanTerm says when a plan's paid time ends, "renews Oct 18", "expires
|
|
// Oct 18" or "until Oct 18" when the vendor doesn't say which; "" when
|
|
// it isn't known.
|
|
func PlanTerm(until *time.Time, renew string) string {
|
|
if until == nil {
|
|
return ""
|
|
}
|
|
d := until.Local().Format("Jan 2")
|
|
switch renew {
|
|
case "auto":
|
|
return "renews " + d
|
|
case "off":
|
|
return "expires " + d
|
|
}
|
|
return "until " + d
|
|
}
|