Files
magpie/internal/provider/grok_usage.go
T

85 lines
2.6 KiB
Go

package provider
// 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
}
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}