usage: the allowance cards come in the Providers page's order (Zhenbao TONG on Discord)
Reported: Zhenbao TONG moved a provider on the Providers page, and the
tray panel's Allowances tab kept the old order.
Changed: provider.InProviderOrder(qs) (internal/provider/quotas.go) sorts
a copy of the quota cards, stably, by the provider's place in All().
Providers it doesn't know go last. Used by /api/quotas's two routes
(internal/gui/usage.go) and the TUI (internal/tui/pages.go). The app.js
comment says the order now comes from the server.
Test: TestQuotaCardsInProviderOrder (quotas_order_test.go). Broken by
hand:
- InProviderOrder as the identity: "as added: [a/a1 remote:x/r c/c1
b/pro a/a2 c/$1]".
- sorting without the clone: "Quotas' list was sorted in place".
Not covered: the /api/quotas wiring has no test beyond the function's
unit test.
Run, go1.26.8 darwin/arm64, sandbox HOME, private GOCACHE:
At this stack on origin/main df6bd3efe (head 48522d851):
gofmt -l: nothing
go vet ./... (darwin), GOOS=linux go vet -tags nogui ./...,
GOOS=windows go vet ./...: ok
set -o pipefail; go test -timeout 45m ./...: ok, EXIT=0
(68 ok, 19 with no test files; agent 528s, gateway 628s,
library 482s)
GUI, playwright 1.62.0, chromium and webkit, concurrency 3: the
171 files near this change (routing*, panel-routing*,
window-mask, privacy*, usage*, quota*, providers*): 2217 tests,
2183 pass, 34 fail. Every red is also red alone on origin/main:
quota-count-wrap and quota-name-translated (#2228),
usage-agents-hide (#2276), usage-checkin (#3091), usage-fold
(#2163), usage-ledger (#3104, red since df6bd3efe, green at its
parent). window-mask: 20 of 20.
At this stack on origin/main c922225f1 (head 91baa7adb), before
df6bd3efe's routing trace change:
set -o pipefail; go test -timeout 45m ./...: ok, EXIT=0
(66 ok, 20 with no test files)
the whole GUI suite (823 files, both engines, concurrency 3):
11589 tests, 11554 pass, 35 fail. The 9 files red were run again
alone at --test-concurrency=1 on the head and on origin/main
a8ecf4020. The same 7 files were red on both: the five issues
above plus s3-sync (#2448) and library-row-remove (#3099).
nav-sub-unroll (#1950) and plugin-updates (#3103) were red only
under the full run. codex-predictions (#2624) was 20 of 20.
This commit is contained in:
@@ -15669,8 +15669,9 @@ function usageAcctFold(q, open) {
|
||||
|
||||
// The Usage page's cards in the order they were dragged to (settings
|
||||
// usageOrder, by provider id), as the Agents page's rows are; one it doesn't
|
||||
// name keeps magpie's own order after them. The tray panel's Usage tab
|
||||
// follows it too.
|
||||
// name keeps the order magpie sends them in after them, the Providers
|
||||
// page's (provider.InProviderOrder). The tray panel's Usage tab follows it
|
||||
// too.
|
||||
function byUsageOrder(list) {
|
||||
const order = state.settings?.usageOrder || [];
|
||||
const rank = (q) => { const i = order.indexOf(q.provider); return i < 0 ? order.length : i; };
|
||||
|
||||
@@ -187,7 +187,7 @@ func usageRoutes(mux *http.ServeMux, w Windows) {
|
||||
defer cancel()
|
||||
// a WorkBuddy (China) account's card says how its daily check-in
|
||||
// went (#694)
|
||||
qs := provider.WithCheckins(provider.Quotas(ctx))
|
||||
qs := provider.InProviderOrder(provider.WithCheckins(provider.Quotas(ctx)))
|
||||
// and what each window holds whole, by what magpie routed in it
|
||||
qs = usage.WithWindowHolds(qs, usage.Clock())
|
||||
// and when each began, for its Pace number (fflake33)
|
||||
@@ -211,7 +211,7 @@ func usageRoutes(mux *http.ServeMux, w Windows) {
|
||||
ctx, cancel := context.WithTimeout(r.Context(), 30*time.Second)
|
||||
defer cancel()
|
||||
provider.RefreshUsage(ctx, id, r.URL.Query().Get("user"))
|
||||
writeJSON(rw, provider.WithWindowStarts(usage.WithWindowHolds(provider.WithCheckins(provider.Quotas(ctx)), usage.Clock()), usage.Clock()))
|
||||
writeJSON(rw, provider.WithWindowStarts(usage.WithWindowHolds(provider.InProviderOrder(provider.WithCheckins(provider.Quotas(ctx))), usage.Clock()), usage.Clock()))
|
||||
})
|
||||
// WorkBuddy's daily check-in pressed now, from the Usage card, for
|
||||
// each account not in yet today, as `magpie accounts checkin` does; the
|
||||
|
||||
@@ -16,6 +16,28 @@ func Quotas(ctx context.Context) []SubscriptionQuota {
|
||||
return cardsServed(subs, plans, balances, LastServed())
|
||||
}
|
||||
|
||||
// InProviderOrder is qs in the order the user put the providers in on the
|
||||
// Providers page (SetOrder), each provider's cards as they came; a card of
|
||||
// no provider here (a remote magpie's) after them all. The window's Usage
|
||||
// page and the tray panel's Allowances tab draw them so, where the user
|
||||
// hasn't arranged the cards themselves (Zhenbao TONG on Discord: the
|
||||
// providers moved in the window, the tray kept its own order).
|
||||
func InProviderOrder(qs []SubscriptionQuota) []SubscriptionQuota {
|
||||
at := map[string]int{}
|
||||
for i, p := range All() {
|
||||
at[p.ID] = i
|
||||
}
|
||||
rank := func(q SubscriptionQuota) int {
|
||||
if i, ok := at[q.Provider]; ok {
|
||||
return i
|
||||
}
|
||||
return len(at)
|
||||
}
|
||||
out := slices.Clone(qs)
|
||||
slices.SortStableFunc(out, func(a, b SubscriptionQuota) int { return rank(a) - rank(b) })
|
||||
return out
|
||||
}
|
||||
|
||||
// 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.
|
||||
|
||||
@@ -0,0 +1,47 @@
|
||||
package provider
|
||||
|
||||
import (
|
||||
"slices"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// Zhenbao TONG on Discord: the providers moved on the window's Providers
|
||||
// page, and the tray panel's Allowances tab kept its own order. The cards
|
||||
// come in the providers' order now, each provider's as they came, and a
|
||||
// card of no provider here (a remote magpie's) after them.
|
||||
func TestQuotaCardsInProviderOrder(t *testing.T) {
|
||||
t.Setenv("HOME", t.TempDir())
|
||||
t.Setenv("USERPROFILE", t.TempDir())
|
||||
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
|
||||
t.Setenv("XDG_CACHE_HOME", t.TempDir())
|
||||
for _, id := range []string{"a", "b", "c"} {
|
||||
if err := Save(Provider{ID: id, Name: id, Key: "sk-" + id, Chat: "https://" + id + ".example.com/v1", Models: []string{"m"}}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
// as Quotas has them: subscriptions, then plans, then balances
|
||||
qs := []SubscriptionQuota{
|
||||
{Provider: "a", User: "a1"}, {Provider: "remote:x", User: "r"}, {Provider: "c", User: "c1"},
|
||||
{Provider: "b", Plan: "pro"}, {Provider: "a", User: "a2"}, {Provider: "c", Balance: "$1"},
|
||||
}
|
||||
cards := func(qs []SubscriptionQuota) []string {
|
||||
var out []string
|
||||
for _, q := range qs {
|
||||
out = append(out, q.Provider+"/"+q.User+q.Plan+q.Balance)
|
||||
}
|
||||
return out
|
||||
}
|
||||
if got, want := cards(InProviderOrder(qs)), []string{"a/a1", "a/a2", "b/pro", "c/c1", "c/$1", "remote:x/r"}; !slices.Equal(got, want) {
|
||||
t.Fatalf("as added: %v, want %v", got, want)
|
||||
}
|
||||
if err := SetOrder([]string{"c", "a", "b"}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got, want := cards(InProviderOrder(qs)), []string{"c/c1", "c/$1", "a/a1", "a/a2", "b/pro", "remote:x/r"}; !slices.Equal(got, want) {
|
||||
t.Fatalf("after SetOrder(c, a, b): %v, want %v", got, want)
|
||||
}
|
||||
// the cache's own array is left as it was
|
||||
if qs[0].User != "a1" || qs[1].User != "r" {
|
||||
t.Fatalf("Quotas' list was sorted in place: %v", cards(qs))
|
||||
}
|
||||
}
|
||||
@@ -834,7 +834,7 @@ func quotasCmd() tea.Msg {
|
||||
defer cancel()
|
||||
// a WorkBuddy (China) account's line says how its daily check-in went,
|
||||
// as its card in the app does
|
||||
return quotaMsg(provider.WithCheckins(provider.Quotas(ctx)))
|
||||
return quotaMsg(provider.InProviderOrder(provider.WithCheckins(provider.Quotas(ctx))))
|
||||
}
|
||||
|
||||
// checkinMsg is what came of c, WorkBuddy's daily check-in pressed now.
|
||||
|
||||
Reference in New Issue
Block a user