feat(providers): add weekly quota tracking for grok-web (#8127)

* feat(providers): add weekly quota fetcher for grok-web (grok.com SSO)

Implements a bespoke QuotaFetcher for the grok-web provider that:
- Reads OIDC tokens from ~/.grok/auth.json (local Grok CLI login)
- Refreshes tokens via auth.x.ai OIDC if expired
- Calls https://cli-chat-proxy.grok.com/v1/billing?format=credits
- Returns a single 'weekly' window with creditUsagePercent and resetAt
- Caches results with 60s TTL (matching codexQuotaFetcher pattern)
- Supports GROK_AUTH_PATH env var override for testing
- Registers in chat.ts before registerGenericQuotaFetchers

Tests cover: missing auth, successful fetch with header verification,
401-triggered token refresh with retry, 60s cache TTL, and preflight
integration.

Closes #6444

* chore(quality): rebaseline chat.ts for #8127 own-growth

---------

Co-authored-by: diegosouzapw <8016841+diegosouzapw@users.noreply.github.com>
This commit is contained in:
Apostol Apostolov
2026-07-23 14:47:49 +03:00
committed by GitHub
parent 2a865aaaa7
commit f3ed4a49d4
4 changed files with 769 additions and 1 deletions

View File

@@ -133,6 +133,7 @@ import { registerCrofUsageFetcher } from "@omniroute/open-sse/services/crofUsage
import { registerDeepseekQuotaFetcher } from "@omniroute/open-sse/services/deepseekQuotaFetcher.ts";
import { registerOpenrouterQuotaFetcher } from "@omniroute/open-sse/services/openrouterQuotaFetcher.ts";
import { registerOpencodeQuotaFetcher } from "@omniroute/open-sse/services/opencodeQuotaFetcher.ts";
import { registerGrokWebQuotaFetcher } from "@omniroute/open-sse/services/grokQuotaFetcher.ts";
import { registerGenericQuotaFetchers } from "@omniroute/open-sse/services/genericQuotaFetcher.ts";
import "@omniroute/open-sse/services/quotaTrackersBatch.ts";
import {
@@ -164,6 +165,14 @@ registerOpenrouterQuotaFetcher();
// quota-aware preflight switching between connections. (#2852)
registerOpencodeQuotaFetcher();
// Register Grok Web quota fetcher.
// Reads account-level OIDC tokens from ~/.grok/auth.json (the local Grok CLI
// login) to surface the weekly credit-usage percentage in the dashboard.
// This runs before registerGenericQuotaFetchers so the bespoke fetcher takes
// precedence over the generic path (which can't resolve grok OIDC auth from
// cookie-based connections).
registerGrokWebQuotaFetcher();
// Register the generic quota fetcher for every other provider that has a
// usage implementation in usage.ts but no bespoke preflight fetcher. This is
// what lets the per-window cutoff modal in Dashboard Limits actually