Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 46 additions & 1 deletion internal/app/quota.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,13 +102,19 @@ func (q *QuotaSnapshot) Blocked() bool {
type planSpec struct {
Name string
MonthlyCredits float64
// FiveHourCredits and WeeklyCredits override the upstream rolling caps when
// non-zero. The upstream endpoint scales its windows from the base
// per-model allowance, so for models whose allowance exceeds that base it
// reports caps lower than the plan's published limits.
FiveHourCredits float64
WeeklyCredits float64
}

// knownPlans mirrors the community CLI's planId mapping. Unknown plans are
// reported by ID with no monthly cap, so the UI hides the derived monthly bar.
var knownPlans = map[string]planSpec{
"individual-go": {Name: "Go", MonthlyCredits: 10},
"individual-goat": {Name: "GOAT", MonthlyCredits: 70},
"individual-goat": {Name: "GOAT", MonthlyCredits: 100, FiveHourCredits: 20, WeeklyCredits: 50},
"individual-pro": {Name: "Pro", MonthlyCredits: 30},
"individual-pro-v1": {Name: "Pro", MonthlyCredits: 80},
"individual-provider": {Name: "Provider", MonthlyCredits: 15},
Expand Down Expand Up @@ -313,6 +319,43 @@ func buildQuotaPlan(planID string, data map[string]any) *QuotaPlan {
return plan
}

// applyPlanWindows replaces the upstream rolling caps with the plan's published
// limits. The upstream caps are derived from a base per-model allowance, so
// they under-report plans or models with a higher allowance.
func applyPlanWindows(snap *QuotaSnapshot) {
if snap == nil || snap.Plan == nil {
return
}
spec, ok := planInfo(snap.Plan.PlanID)
if !ok {
return
}
if spec.FiveHourCredits > 0 && snap.FiveHour != nil {
snap.FiveHour.Cap = spec.FiveHourCredits
}
if spec.WeeklyCredits > 0 && snap.Weekly != nil {
snap.Weekly.Cap = spec.WeeklyCredits
}
}

// applyPlanRemaining rewrites the upstream monthly balance for plans whose
// published allowance is higher than the upstream base allowance. The
// remaining figure becomes the plan cap minus the billing-period spend.
func applyPlanRemaining(snap *QuotaSnapshot) {
if snap == nil || snap.Plan == nil || snap.Plan.MonthlyCredits == nil || snap.Usage == nil {
return
}
spec, ok := planInfo(snap.Plan.PlanID)
if !ok || (spec.FiveHourCredits == 0 && spec.WeeklyCredits == 0) {
return
}
remaining := *snap.Plan.MonthlyCredits - snap.Usage.TotalCredits
if remaining < 0 {
remaining = 0
}
snap.MonthlyCredits = &remaining
}

func parseQuotaUsage(raw map[string]any) *QuotaUsage {
if raw == nil {
return nil
Expand Down Expand Up @@ -469,6 +512,7 @@ func fetchQuotaSnapshot(ctx context.Context, client *http.Client, baseURL, apiKe
snap.Plan = buildQuotaPlan(planID, data)
}
}
applyPlanWindows(snap)

// 4. usage/summary → billing-period aggregates.
if usageResp, err := quotaGet(ctx, client, base+"/alpha/usage/summary", apiKey); err != nil {
Expand All @@ -480,6 +524,7 @@ func fetchQuotaSnapshot(ctx context.Context, client *http.Client, baseURL, apiKe
}
snap.Usage = parseQuotaUsage(root)
}
applyPlanRemaining(snap)

if len(failures) > 0 {
snap.Failures = failures
Expand Down
36 changes: 35 additions & 1 deletion internal/app/quota_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func TestPlanInfoLongestPrefix(t *testing.T) {
{"individual-pro", "Pro", 30, true},
{"individual-pro-v1", "Pro", 80, true},
{"individual_pro", "Pro", 30, true},
{"individual-goat", "GOAT", 70, true},
{"individual-goat", "GOAT", 100, true},
{"teams-pro", "Teams Pro", 40, true},
{"individual-max", "Max", 150, true},
{"enterprise-unknown", "", 0, false},
Expand Down Expand Up @@ -188,6 +188,40 @@ func TestFetchQuotaSnapshotHappyPath(t *testing.T) {
}
}

func TestFetchQuotaSnapshotAppliesPlanWindowCaps(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
switch r.URL.Path {
case "/alpha/billing/credits":
fmt.Fprint(w, `{"credits":{"monthlyCredits":70,"planId":"individual-goat"},"windowLimits":{"limited":true,"fiveHour":{"used":5,"cap":14},"weekly":{"used":40,"cap":35,"exceeded":true}}}`)
case "/alpha/billing/subscriptions":
fmt.Fprint(w, `{"data":{"planId":"individual-goat","status":"active"}}`)
case "/alpha/usage/summary":
fmt.Fprint(w, `{"data":{"totalCost":10,"totalCredits":10}}`)
default:
http.NotFound(w, r)
}
}))
defer srv.Close()

snap, err := fetchQuotaSnapshot(context.Background(), srv.Client(), srv.URL, "cc-key")
if err != nil {
t.Fatalf("fetch failed: %v", err)
}
if snap.Plan == nil || snap.Plan.MonthlyCredits == nil || *snap.Plan.MonthlyCredits != 100 {
t.Fatalf("GOAT plan = %+v", snap.Plan)
}
if snap.FiveHour == nil || snap.FiveHour.Cap != 20 {
t.Fatalf("GOAT five hour window = %+v", snap.FiveHour)
}
if snap.Weekly == nil || snap.Weekly.Cap != 50 || !snap.Weekly.Exceeded {
t.Fatalf("GOAT weekly window = %+v", snap.Weekly)
}
if snap.MonthlyCredits == nil || *snap.MonthlyCredits != 90 {
t.Fatalf("GOAT remaining credits = %v, want 90", snap.MonthlyCredits)
}
}

func TestFetchQuotaSnapshotSnakeCaseAndNested(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
Expand Down
9 changes: 6 additions & 3 deletions internal/web/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -1186,13 +1186,16 @@ <h3 data-i18n="oauth.title">Add account via OAuth</h3>
}

// The API exposes no monthly window: cap comes from the plan mapping and
// used = cap − remaining monthly credits. Hidden for unknown plans.
// used is the billing-period spend, falling back to cap − remaining credits.
// Hidden for unknown plans.
function quotaMonthBar(q) {
const plan = q.plan || {};
const cap = plan.monthly_credits;
const remaining = q.monthly_credits;
if (cap == null || remaining == null || cap <= 0) return "";
const used = Math.max(0, cap - remaining);
const spent = q.usage ? Number(q.usage.total_credits) : NaN;
if (cap == null || cap <= 0) return "";
const used = isFinite(spent) ? spent : (remaining != null ? Math.max(0, cap - remaining) : null);
if (used == null) return "";
return quotaBar(t("quota.monthly"), { used, cap, exceeded: used >= cap, reset_at: plan.current_period_end }, true);
}

Expand Down
Loading