Files
magpie/internal/provider/window_holds.go
T
yetone de3b1c646a usage: a subscription card's window says what the whole window holds, "Whole ≈ N tokens · ≈ $X", reckoned from what magpie routed through the account since the window began over the share used, at API list prices (Chiao on Discord)
usage.WithWindowHolds fills QuotaWindow.Holds on GET /api/usage/quotas and its refresh, from usage.jsonl: the card's provider (by its id now, so a moved plugin's card and a renamed provider count), its account (ProviderAccount, or an older record's Host), calls to the models the window counts (Claude's Opus-only or Fable week), not turned away, from the window's start up to when the share was read. Priced by catalog.Price.CostSplit at the maker's list price, else the provider's own list, never the user's own subscription price: Anthropic's 5-minute and 1-hour cache writes at 1.25x and 2x input, OpenAI's cached input and no cache write price. A call with no API price leaves the window tokens-only. The caller's cards are not written to.

Not told where it wouldn't be honest: under 5% used, nothing routed in the window, magpie's log starting after the window did, no reset or no span (credits with no reset, Copilot's premium requests), Antigravity's per-model family and pool windows, Aside windows, a card with an error, a balance, or one read from another computer. The tooltip says how it was reckoned and that use of the account outside magpie makes it read low (the request said 偏高; such use raises the share without adding to what magpie saw, so the estimate is lower than the truth). en, zh, zh-TW, ja, de.

Run: go test -tags nogui ./... in Docker (golang:1.26.8): all pass but internal/agent's Aside* chmod tests, which fail only as root and pass with --user 1000. The new tests fail with the log-start check removed, the model scope removed, the caller's windows not cloned, and 1-hour cache writes at the 5-minute price. Against a read-only copy of the owner's usage.jsonl, quotas.json and models.json: Claude's week (71%) ≈ 46K tokens / $14.16 from 165 calls, its Fable week and 5 hours (12%) nothing, every other card nothing (under 5%, or no reset). New quota-holds.test.cjs passes in Chromium and WebKit at 360, 440 and 1000px in en, zh (万), zh-TW (萬), ja, de, and fails with the new CSS removed; click-scroll, gui-ja, gui-de, i18n-keys, quota-* and usage-* pass (304/304). go vet -tags nogui (darwin, GOOS=linux, GOOS=windows) and gofmt -l on the touched packages pass.
2026-10-08 20:53:03 +08:00

68 lines
2.4 KiB
Go

package provider
// What a whole subscription window holds, as the Usage page's account
// cards say beside its share used: what magpie routed through the account
// since the window began, over the share the vendor says is used. Package
// usage reckons it (WithWindowHolds), from its log; here are the figures and
// the window's bounds.
import (
"strings"
"time"
)
// WindowHolds is a window's whole allowance reckoned from magpie's own
// calls: Tokens of input and output (cache traffic aside, as the Usage
// page counts tokens) and, when every call's model has an API list price,
// Cost at those prices (cache reads and writes at the vendor's own rates).
// Routed is what magpie routed through the account in the window so far,
// up to when the share was read, and Used that share. A call made with the
// account outside magpie is in Used but not in Routed, so the estimate is
// then lower than the window really holds.
type WindowHolds struct {
Tokens int64 `json:"tokens"`
Cost float64 `json:"cost,omitempty"`
Priced bool `json:"priced"`
Used float64 `json:"used"`
Routed struct {
Calls int `json:"calls"`
Tokens int64 `json:"tokens"`
CacheRead int64 `json:"cacheRead,omitempty"`
CacheWrite int64 `json:"cacheWrite,omitempty"`
Cost float64 `json:"cost,omitempty"`
} `json:"routed"`
Since time.Time `json:"since"`
}
// HoldsFloor is the least share used a window's whole is reckoned from:
// below it a percent the vendor rounds is too coarse to scale up.
const HoldsFloor = 5.0
// Bounds is when w, read at readAt, began and ends: its reset, and that
// less how long it runs; a month's window began on the same day a month
// before. ok is false when either isn't known.
func (w QuotaWindow) Bounds(readAt time.Time) (start, end time.Time, ok bool) {
switch {
case w.ResetsAt != nil:
end = *w.ResetsAt
case w.ResetSecs > 0:
end = readAt.Add(time.Duration(w.ResetSecs) * time.Second)
default:
return time.Time{}, time.Time{}, false
}
span := windowSpan(w)
if span <= 0 {
return time.Time{}, time.Time{}, false
}
if span == 30*24*time.Hour {
return end.AddDate(0, -1, 0), end, true
}
return end.Add(-span), end, true
}
// Counts is whether a call to model counts against w: every model, or
// only the ones its scope names ("opus", a plugin's list).
func (w QuotaWindow) Counts(model string) bool {
return Limit{Model: w.Model, matches: w.matches}.applies(strings.ToLower(model))
}