The reporter starts magpie at login with the proxy on "follow system
proxy". magpie comes up before the proxy app. With no system proxy set
yet, netproxy.For gives none, so magpie's own allowance reads went out
direct, from the user's real IP. That reading was then kept for up to
15s. A system proxy that is set but not listening fails the request
("proxyconnect ... connection refused"); it never falls back to direct.
Proxy freshness:
- netproxy.System re-reads a "none" reading after 2s (noneKeep) instead
of 15s (systemKeep, unchanged for a set proxy).
- The plugin host looks again every 2s while it has no proxy
(proxyLookNone), so a host started before the proxy app takes it soon.
- Catalog sync, the update check, plugin updates and token refresh all go
through netproxy.Func / Dispatch, so they use the current proxy too.
Settings -> Allowances -> "Allowance reads": Automatic (default, as
before) or When I ask (settings.QuotaReads = "asked",
POST /api/settings/quota-reads):
- These count as asking (provider.Asked): the Usage page opened or
refreshed (?asked=1), a card's refresh (RefreshUsage), magpie quota,
magpie accounts, magpie quota wait and the TUI's quotas.
- Every other read (quotaReading -> holdUnasked) carries netproxy.Hold.
netproxy.Func, Dispatch's choice proxies and the plugin host's calls
refuse a held request before it leaves.
- The readers return the last reading, dated (AsOf), or "not read yet".
That covers routing's Allowances / AwaitAllowance, the used-up switch,
the menu bar, alerts, Quotas/KeyBalances/PlanQuotas, Codex/Claude/
Cursor/Grok/Kiro usage and KeyAllowance.
- An account refused for its quota (StaleAllowance) or a key refused
(StaleKeyAllowance) is still read, once.
- Warm-up and check-in are unchanged.
- Strings are added in zh, zh-TW, ja and de. The docs are updated:
providers-accounts (Allowance row), provider-plugins (host proxy).
Run:
- go test -tags nogui ./... (sandbox HOME), gofmt -l, and go vet -tags
nogui on GOOS=linux/windows/darwin for ., netproxy, plugin, settings,
provider, gui and tui.
- New tests, each seen red by breaking the change by hand:
- TestSystemProxySetAfterStartIsTakenSoon and TestHeldRequestNeverLeaves
(netproxy).
- TestHostWithNoProxyLooksAgainSoon (plugin; red with 15s).
- TestAllowancesReadOnlyWhenAsked (red without the hold and without
markStaleRead), TestAllowancesReadByThemselvesByDefault and
TestKeyReadsOnlyWhenAsked (red without holdUnasked and without
unheld for a refused key).
- GUI quota-reads.test.cjs in Chromium and WebKit, at 900 and 360px, in
en/zh/zh-TW/ja/de.
- i18n-keys, gui-ja, gui-de, settings-sections, settings-groups,
currency, number-units and tray-bird.
98 lines
3.2 KiB
Go
98 lines
3.2 KiB
Go
package provider
|
|
|
|
// PLUGIN-SERVED (see AGENTS.md): Grok ("grok") is a deprecated built-in
|
|
// subscription served by its plugin, @magpie-community/opencode-grok-auth,
|
|
// once moved onto it (provider.Moved; the default for a new sign-in). A
|
|
// moved one's sign-ins, models, requests and usage are all the plugin's,
|
|
// never this code's (only the move, in migrate*.go, still reads its
|
|
// accounts). A fix here alone doesn't reach those users; fix the plugin
|
|
// (github.com/magpie-community/plugins, packages/grok) and raise the mover's
|
|
// min in internal/provider/migrate_side.go.
|
|
|
|
// How much of a Grok subscription's allowance is gone, as the CLI's own
|
|
// /usage reads it: the credits of the current period, weekly for SuperGrok,
|
|
// and what on-demand spending has used of its cap.
|
|
|
|
import (
|
|
"context"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
// GrokBase is the CLI's backend; a var so tests can point it elsewhere.
|
|
var GrokBase = "https://cli-chat-proxy.grok.com/v1"
|
|
|
|
func grokSubscriptionUsage(ctx context.Context) SubscriptionQuota {
|
|
q := SubscriptionQuota{Provider: "grok", Name: "Grok (SuperGrok)", Icon: "xai", Windows: []QuotaWindow{}}
|
|
if c, ok := readGrokCredential(GrokHome()); ok {
|
|
q.User = c.Email
|
|
}
|
|
if holding(ctx) {
|
|
q.Error = errNotAsked.Error()
|
|
return q
|
|
}
|
|
c, err := grokAccessToken(GrokHome(), GrokExecutable(), false)
|
|
if err == nil {
|
|
q.Windows, err = grokWindows(ctx, c.Key)
|
|
}
|
|
if err != nil {
|
|
q.Error = err.Error()
|
|
}
|
|
return q
|
|
}
|
|
|
|
type grokAmount struct {
|
|
Val float64 `json:"val"`
|
|
}
|
|
|
|
// grokWindows: the credits used this period, and the on-demand spending
|
|
// when the account allows any.
|
|
func grokWindows(ctx context.Context, token string) ([]QuotaWindow, error) {
|
|
var data struct {
|
|
Config struct {
|
|
CreditUsagePercent float64 `json:"creditUsagePercent"`
|
|
CurrentPeriod *struct {
|
|
Type string `json:"type"`
|
|
End string `json:"end"`
|
|
} `json:"currentPeriod"`
|
|
BillingPeriodEnd string `json:"billingPeriodEnd"`
|
|
OnDemandCap grokAmount `json:"onDemandCap"`
|
|
OnDemandUsed grokAmount `json:"onDemandUsed"`
|
|
} `json:"config"`
|
|
}
|
|
if err := accountJSON(ctx, GrokBase+"/billing?format=credits", token, nil, &data); err != nil {
|
|
return []QuotaWindow{}, err
|
|
}
|
|
cfg := data.Config
|
|
name, end := "Allowance", cfg.BillingPeriodEnd
|
|
if p := cfg.CurrentPeriod; p != nil {
|
|
name, end = grokPeriodName(p.Type), p.End
|
|
}
|
|
w := QuotaWindow{Name: name, Used: cfg.CreditUsagePercent, Span: grokPeriodSpan[name]}
|
|
if t, err := time.Parse(time.RFC3339Nano, end); err == nil {
|
|
w.ResetsAt = &t
|
|
}
|
|
out := []QuotaWindow{w}
|
|
if cfg.OnDemandCap.Val > 0 {
|
|
out = append(out, QuotaWindow{Name: "On-demand", Used: 100 * cfg.OnDemandUsed.Val / cfg.OnDemandCap.Val, ResetsAt: w.ResetsAt, Aside: true})
|
|
}
|
|
return out, nil
|
|
}
|
|
|
|
// grokPeriodName names a USAGE_PERIOD_TYPE_* the way the other allowances
|
|
// are named.
|
|
func grokPeriodName(t string) string {
|
|
switch strings.TrimPrefix(t, "USAGE_PERIOD_TYPE_") {
|
|
case "DAILY":
|
|
return "1 day"
|
|
case "WEEKLY":
|
|
return "7 days"
|
|
case "MONTHLY":
|
|
return "Month"
|
|
}
|
|
return "Allowance"
|
|
}
|
|
|
|
// grokPeriodSpan is how long each of grokPeriodName's periods runs.
|
|
var grokPeriodSpan = map[string]time.Duration{"1 day": 24 * time.Hour, "7 days": 7 * 24 * time.Hour, "Month": 30 * 24 * time.Hour}
|