panel: nothing is shaved at an edge (竟然有裁边 / 上面这个border也被裁掉了) — the model's logo on a row's hover comes in as its column opens from nothing to its own width, so a stack of three is as whole as one, with its clip padded past the discs' rings; and the tab's view, which scrolls and so clips at its own top, has the 2px the card's ring outside its box needs, taken from the gap under the tabs. usage: an account's last allowance read is kept (quotas.json beside magpie's settings) and shown in place of one that can't be read for a reason that says nothing about the account — Anthropic rate limiting its usage endpoint, a 5xx, a timeout — across restarts too, with its windows that have reset since starting again from nothing and a tooltip saying when it was read; a reading that comes back is used at once, and a sign-in gone bad is still said (因为rate limit获取不到用量的情况下,能否先暂时用上一次获取到的用量顶替着)

This commit is contained in:
yetone
2026-09-28 17:34:08 +08:00
parent f8be98bc8a
commit e6e09aa348
8 changed files with 193 additions and 7 deletions
+12 -3
View File
@@ -528,7 +528,10 @@ body.window .field.solo { grid-column: auto; }
/* ---- the panel's tabs: agents, usage, profiles ---------------------------- */
/* outside the scrolling page, so they stay put under the header */
.ptabs { position: relative; display: flex; flex: none; gap: 2px; margin: 0 12px 10px; }
.ptabs { position: relative; display: flex; flex: none; gap: 2px; margin: 0 12px 8px; }
/* the view scrolls, so it clips at its own top: the card's edge is a ring
outside its box and needs the 2px the tabs give up to be drawn whole */
body.panel .view { padding-top: 2px; }
.ptabs > .thumb { top: 0; bottom: 0; border-radius: 7px; box-shadow: 0 1px 2px rgba(0, 0, 0, .08), var(--card-edge); }
.ptabs[hidden] { display: none; }
.ptabs button { position: relative; z-index: 1; flex: 1; display: flex; align-items: center; justify-content: center; gap: 3px; height: 26px; border-radius: 7px; font-size: 12px; color: var(--muted); transition: color .15s, background-color .15s; }
@@ -552,9 +555,15 @@ body.panel .row.agent.open { background: var(--card-2); }
.ag-sum { display: grid; grid-template-columns: auto 12px 10px; align-items: center; gap: 10px; min-width: 0; max-width: 100%; font-size: 12px; color: var(--muted); }
.ag-sum .v { display: flex; align-items: center; justify-content: flex-end; min-width: 0; }
.ag-sum .vt { min-width: 0; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; text-align: right; }
.ag-sum .mi { display: inline-flex; flex: none; max-width: 0; overflow: hidden; margin-right: 0; opacity: 0; transform: scale(.7); transition: max-width .22s var(--ease), margin-right .22s var(--ease), opacity .18s var(--ease), transform .22s var(--ease); }
/* the logo comes in as its column opens from nothing to its own width, so
a stack of three is as whole as one; the clip is padded out past the
discs' rings, which it would otherwise shave */
.ag-sum .mi { display: inline-grid; grid-template-columns: 0fr; flex: none; margin-right: 0; opacity: 0; transform: scale(.7); transition: grid-template-columns .22s var(--ease), margin-right .22s var(--ease), opacity .18s var(--ease), transform .22s var(--ease); }
.ag-sum .mi-in { display: inline-flex; align-items: center; min-width: 0; overflow: hidden; margin: -2px; padding: 2px; }
.row.agent .ag-sum .mi .ic { width: 14px; height: 14px; }
.row.agent:hover .ag-sum .mi { max-width: 48px; margin-right: 6px; opacity: 1; transform: none; }
.row.agent .ag-sum .mi .ic-stack .disc { width: 16px; height: 16px; }
.row.agent .ag-sum .mi .ic-stack .disc > .ic { width: 10px !important; height: 10px !important; }
.row.agent:hover .ag-sum .mi { grid-template-columns: 1fr; margin-right: 6px; opacity: 1; transform: none; }
#addSheet { scroll-margin-top: 12px; }
.ag-sum .v.empty { color: var(--muted); }
.ag-sum .chev { display: inline-flex; width: 10px; color: var(--faint); transform: rotate(-90deg); transition: transform .4s var(--row-spring); }
+10 -1
View File
@@ -242,7 +242,8 @@ function renderAgents() {
if (main?.value && (opt?.icon || opt?.icons?.length)) {
const mi = el("span", "mi");
mi.setAttribute("aria-hidden", "true");
mi.append(optionIcon(opt));
mi.append(el("span", "mi-in"));
mi.firstChild.append(optionIcon(opt));
v.append(mi);
}
v.append(el("span", "vt", main ? (opt?.label || main.value || t("default")) : ""));
@@ -3694,6 +3695,7 @@ function accountQuota(data, user) {
}
// the two rolling windows fit a line; the per-model ones go in its tooltip
line.title = q.windows.slice(2).map((w) => t(w.name) + " " + quotaText(w)).join(" · ");
if (q.asOf) line.title = [line.title, asOfText(q)].filter(Boolean).join("\n");
for (const w of q.windows.slice(0, 2)) {
const used = Math.max(0, Math.min(100, w.used));
const m = el("span", "aq-w" + (used >= 90 ? " full" : ""));
@@ -4279,6 +4281,12 @@ function renderPanelQuota() {
fit();
}
// asOfText: an allowance standing in for one that couldn't be read just
// now (a vendor rate limiting its usage endpoint) says when it was read.
function asOfText(q) {
return t("As of {when} — couldn't be read just now", { when: new Date(q.asOf).toLocaleString() });
}
// shortWindow: "5 hours" as 5h, "7 days" as 7d; any other name as it is.
function shortWindow(name) {
const m = /^(\d+)\s*(minute|hour|day|week|month)s?$/i.exec(name || "");
@@ -4295,6 +4303,7 @@ function panelQuotaCard(q) {
card.title += "\n" + q.error;
return card;
}
if (q.asOf) card.title += "\n" + asOfText(q);
const ws = q.windows.slice(0, 3);
// when the windows begun start again: the first bare, the others by name
const begun = ws.filter((w) => w.resetsAt && w.used > 0);
+1
View File
@@ -614,6 +614,7 @@ const I18N = {
"Other Models": "其他模型",
"Total": "合计",
"Usage unavailable": "暂时无法获取用量",
"As of {when} — couldn't be read just now": "截至 {when},暂时无法获取最新用量",
"Hide usage": "收起用量",
"Show usage": "展开用量",
"Signed out — add this account again to use it": "登录已失效,请重新添加此账号",
+1 -1
View File
@@ -140,7 +140,7 @@ func grokLoginUsage(ctx context.Context) map[string]SubscriptionQuota {
wg.Add(1)
go func(g grokLogin) {
defer wg.Done()
q := grokUsageAt(ctx, g.Home)
q := keepLast(grokUsageAt(ctx, g.Home), g.User)
if q.Error != "" && ok {
q = e.q // a hiccup keeps what was known
}
+1 -1
View File
@@ -66,7 +66,7 @@ func LoginUsage(ctx context.Context, agent string) map[string]SubscriptionQuota
wg.Add(1)
go func(l Login) {
defer wg.Done()
q := loginQuota(ctx, l)
q := keepLast(loginQuota(ctx, l), l.User)
if q.Error != "" && ok {
q = e.q // a hiccup keeps what was known
}
+105
View File
@@ -0,0 +1,105 @@
package provider
// The last allowance each account reported, kept on disk, so a vendor
// turning the usage endpoint away for a while (Anthropic's 429s) or not
// answering shows what was last read, not an error — across restarts too.
// A reading that comes back replaces it at once.
import (
"encoding/json"
"os"
"path/filepath"
"regexp"
"strings"
"sync"
"time"
)
var lastQuotas struct {
sync.Mutex
m map[string]lastQuota // provider/user
loaded bool
}
type lastQuota struct {
At time.Time `json:"at"`
Q SubscriptionQuota `json:"quota"`
// what the windows' json leaves out, for routing
Spans []lastWindow `json:"spans,omitempty"`
}
type lastWindow struct {
Span time.Duration `json:"span,omitempty"`
Model string `json:"model,omitempty"`
Aside bool `json:"aside,omitempty"`
}
func lastQuotasPath() string { return filepath.Join(filepath.Dir(Path()), "quotas.json") }
// passing is an error that says nothing about the account: the vendor
// rate limiting, failing or out of reach. A sign-in gone bad is not one.
var passing = regexp.MustCompile(`(?i)rate.?limit|too many requests|429|internal server error|bad gateway|service unavailable|gateway timeout|deadline exceeded|timeout|timed out|connection (refused|reset)|no such host|network is unreachable|EOF|asks again`)
// keepLast is q, or the last reading of its account when q failed in
// passing; a good reading is kept for next time.
func keepLast(q SubscriptionQuota, user string) SubscriptionQuota {
if user == "" {
user = q.User
}
if user == "" || q.Provider == "" {
return q
}
key := q.Provider + "/" + strings.ToLower(user)
c := &lastQuotas
c.Lock()
defer c.Unlock()
if !c.loaded {
c.loaded = true
if b, err := os.ReadFile(lastQuotasPath()); err == nil {
json.Unmarshal(b, &c.m)
}
}
if q.Error == "" {
if len(q.Windows) == 0 && q.Balance == "" {
return q
}
e := lastQuota{At: time.Now(), Q: q}
for _, w := range q.Windows {
if w.matches != nil {
return q // a pool that can't be written down isn't kept
}
e.Spans = append(e.Spans, lastWindow{w.Span, w.Model, w.Aside})
}
e.Q.Error = ""
e.Q.AsOf = nil
if c.m == nil {
c.m = map[string]lastQuota{}
}
c.m[key] = e
if b, err := json.Marshal(c.m); err == nil {
os.MkdirAll(filepath.Dir(lastQuotasPath()), 0o700)
writeFileAtomic(lastQuotasPath(), b)
}
return q
}
e, ok := c.m[key]
if !ok || !passing.MatchString(q.Error) {
return q
}
out := e.Q
out.Windows = make([]QuotaWindow, len(e.Q.Windows))
copy(out.Windows, e.Q.Windows)
for i := range out.Windows {
if i < len(e.Spans) {
out.Windows[i].Span, out.Windows[i].Model, out.Windows[i].Aside = e.Spans[i].Span, e.Spans[i].Model, e.Spans[i].Aside
}
}
out.Windows = elapsed(out.Windows, time.Now())
out.Name, out.Icon, out.User = q.Name, q.Icon, q.User
if out.Name == "" {
out.Name, out.Icon = e.Q.Name, e.Q.Icon
}
at := e.At
out.AsOf = &at
return out
}
+59
View File
@@ -0,0 +1,59 @@
package provider
import (
"testing"
"time"
)
func TestKeepLast(t *testing.T) {
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
restart := func() {
lastQuotas.Lock()
lastQuotas.m, lastQuotas.loaded = nil, false
lastQuotas.Unlock()
}
restart()
defer restart()
later := time.Now().Add(time.Hour)
good := SubscriptionQuota{Provider: "claude", Name: "Claude Code", User: "A@x.com", Windows: []QuotaWindow{
{Name: "5 hours", Used: 40, ResetsAt: &later, Span: 5 * time.Hour},
{Name: "7 days · Opus", Used: 10, Model: "opus", Span: 7 * 24 * time.Hour},
}}
if q := keepLast(good, ""); q.AsOf != nil || q.Windows[0].Used != 40 {
t.Fatalf("%+v", q)
}
restart() // what was read is on disk
limited := SubscriptionQuota{Provider: "claude", Name: "Claude Code", User: "a@x.com", Windows: []QuotaWindow{},
Error: claudeLimited(5 * time.Minute).Error()}
q := keepLast(limited, "")
if q.Error != "" || q.AsOf == nil || len(q.Windows) != 2 || q.Windows[0].Used != 40 || q.Windows[1].Model != "opus" || q.Windows[0].Span != 5*time.Hour {
t.Fatalf("rate limited: %+v", q)
}
// the same through LoginUsage, which knows the user but not the name
q = keepLast(SubscriptionQuota{Provider: "claude", Windows: []QuotaWindow{}, Error: "Too Many Requests"}, "a@x.com")
if q.Error != "" || q.Name != "Claude Code" || q.Windows[0].Used != 40 {
t.Fatalf("login: %+v", q)
}
// a sign-in gone bad is said, not covered up
if q := keepLast(SubscriptionQuota{Provider: "claude", User: "a@x.com", Error: "Claude Code's sign-in has expired"}, ""); q.Error == "" {
t.Fatalf("expired: %+v", q)
}
// a new reading is used at once
good.Windows[0].Used = 70
keepLast(good, "")
if q := keepLast(limited, ""); q.Windows[0].Used != 70 {
t.Fatalf("newer: %+v", q)
}
// a window that has reset since starts again from nothing
past := time.Now().Add(-time.Minute)
good.Windows[0].ResetsAt = &past
keepLast(good, "")
if q := keepLast(limited, ""); q.Windows[0].Used != 0 {
t.Fatalf("reset: %+v", q)
}
}
+4 -1
View File
@@ -53,6 +53,9 @@ type SubscriptionQuota struct {
Until *time.Time `json:"until,omitempty"`
Renew string `json:"renew,omitempty"`
Error string `json:"error,omitempty"`
// AsOf is when an allowance shown in place of one that couldn't be
// read was read (see keepLast); nil for a reading just made.
AsOf *time.Time `json:"asOf,omitempty"`
}
var subscriptionUsageCache struct {
@@ -224,7 +227,7 @@ func fetchSubscriptionUsage() []SubscriptionQuota {
var wg sync.WaitGroup
for i, f := range fetches {
wg.Add(1)
go func() { defer wg.Done(); out[i] = f() }()
go func() { defer wg.Done(); out[i] = keepLast(f(), "") }()
}
wg.Wait()
return out