diff --git a/internal/provider/balance.go b/internal/provider/balance.go index 88b5abaa2..9ab9903d0 100644 --- a/internal/provider/balance.go +++ b/internal/provider/balance.go @@ -1788,9 +1788,9 @@ func KeyBalances(ctx context.Context) []SubscriptionQuota { wg.Wait() out = slices.DeleteFunc(out, func(q SubscriptionQuota) bool { return q.Provider == "" }) if ctx.Err() == nil { - // each balance kept, and drawn over time with its runs-out; not - // one estimated from a balance typed, which is no reading - noteBalanceHistory(slices.DeleteFunc(slices.Clone(out), func(q SubscriptionQuota) bool { return q.Estimate != nil }), time.Now()) + // each balance kept, and drawn over time with its runs-out, on + // the cards themselves (an estimate is neither: noteBalanceHistory) + noteBalanceHistory(out, time.Now()) // the history of the cards read now and of those kept, a partial // read (one card's refresh) keeping the others h := sharedBalanceHist(slices.Concat(kept, out)) diff --git a/internal/provider/balance_history.go b/internal/provider/balance_history.go index 67389efc5..409cb1c89 100644 --- a/internal/provider/balance_history.go +++ b/internal/provider/balance_history.go @@ -124,7 +124,8 @@ func addBalancePoint(pts []BalancePoint, n BalancePoint) []BalancePoint { // under, if it is one to keep: one under the card's key, or, told in // several currencies, one for each under the card's key and the currency. func balancePointsOf(q SubscriptionQuota, now time.Time) map[string]BalancePoint { - if q.Error != "" || q.AsOf != nil || q.Provider == "" || q.Balance == "" || len(q.BalanceParts) > 0 { + // an estimate counted down from a balance typed is no reading + if q.Error != "" || q.AsOf != nil || q.Estimate != nil || q.Provider == "" || q.Balance == "" || len(q.BalanceParts) > 0 { return nil } at := now @@ -187,7 +188,8 @@ func balanceCurrencies(s string) []currencyAmount { func currencyHistKey(key, sign string) string { return key + "|" + strings.TrimSpace(sign) } // noteBalanceHistory keeps the balance each card among qs tells, and gives -// every card with a balance its trend. +// every card with a balance its trend, on qs itself. A balance estimated +// from one typed (Estimate) is neither kept nor given one. func noteBalanceHistory(qs []SubscriptionQuota, now time.Time) { balanceHistMu.Lock() defer balanceHistMu.Unlock() @@ -236,7 +238,7 @@ func noteBalanceHistory(qs []SubscriptionQuota, now time.Time) { writeBalanceHist(h) } for i, q := range qs { - if q.Balance == "" || len(q.BalanceParts) > 0 { + if q.Balance == "" || len(q.BalanceParts) > 0 || q.Estimate != nil { continue } if h == nil { diff --git a/internal/provider/key_balance_trend_test.go b/internal/provider/key_balance_trend_test.go new file mode 100644 index 000000000..db95f75db --- /dev/null +++ b/internal/provider/key_balance_trend_test.go @@ -0,0 +1,49 @@ +package provider + +import ( + "context" + "net/http" + "net/http/httptest" + "path/filepath" + "testing" + "time" + + "github.com/yetone/magpie/internal/agentenv" +) + +// A key's balance card carries its trend: the Usage page's "Balance over +// time" is drawn from it. f8525323d noted the history on a copy of the +// cards, so every key's card went out with none (nf1qaz2w on Discord). +func TestKeyBalancesCarryTheirTrend(t *testing.T) { + isolate(t) + h := t.TempDir() + t.Setenv("HOME", h) + t.Setenv("USERPROFILE", h) + t.Setenv("XDG_CONFIG_HOME", filepath.Join(h, ".config")) + t.Setenv("XDG_CACHE_HOME", filepath.Join(h, ".cache")) + t.Setenv("PATH", h) + for _, v := range agentenv.Vars { + t.Setenv(v, "") + } + keyBalanceCache.data = nil + t.Cleanup(func() { keyBalanceCache.data = nil }) + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.Write([]byte(`{"data":{"total_available":1000000}}`)) + })) + defer srv.Close() + if err := Save(Provider{ID: "relay", Name: "Relay", Chat: srv.URL + "/v1", Key: "sk-one", KeyName: "main", + BalanceURL: srv.URL + "/api/usage/token", BalancePath: "$data.total_available / 500000"}); err != nil { + t.Fatal(err) + } + // read an hour ago at $3 + before := time.Now().Add(-time.Hour).UTC().Truncate(time.Second) + writeBalanceHist(balanceHist{quotaHistKey("relay", ""): {{At: before, Amount: 3}}}) + got := KeyBalances(context.Background()) + if len(got) != 1 || got[0].Balance != "$2.00" { + t.Fatalf("cards %+v", got) + } + tr := got[0].BalanceTrend + if tr == nil || len(tr.Points) != 2 || tr.Points[0].Amount != 3 || tr.Points[1].Amount != 2 { + t.Fatalf("the key's card has trend %+v, want $3 an hour ago then $2", tr) + } +}