fix(grok-cli): display public subscription tier

Map the Grok OAuth access-token tier claim to the public plan label
and prefer it over internal /user entitlement names. Fails open to
existing plan detection for opaque or malformed tokens; upstream
remains authoritative for access and quota enforcement.
This commit is contained in:
ryanngit
2026-08-05 11:43:13 +07:00
committed by decolua
parent 948dd8f89b
commit d0751bcff7
2 changed files with 28 additions and 3 deletions

View File

@@ -91,6 +91,24 @@ function resolvePlan(user, config) {
return "Grok Build";
}
// Display only; upstream remains authoritative for access and quota enforcement.
function planFromAccessToken(accessToken) {
try {
const payload = JSON.parse(Buffer.from(accessToken.split(".")[1], "base64url"));
return {
0: "Free",
1: "SuperGrok",
2: "X Basic",
3: "X Premium",
4: "X Premium Plus",
5: "SuperGrok Heavy",
6: "SuperGrok Lite",
}[payload.tier] || "";
} catch {
return "";
}
}
function makeQuota({ used, total, resetAt, unlimited = false }) {
const safeTotal = Math.max(0, toFiniteNumber(total, 0));
const safeUsed = Math.max(0, toFiniteNumber(used, 0));
@@ -371,6 +389,7 @@ export async function getGrokCliUsage(accessToken, providerSpecificData = null,
}
const parsed = parseGrokCliBilling(billing, user);
parsed.plan = planFromAccessToken(accessToken) || parsed.plan;
if (!parsed.quotas || Object.keys(parsed.quotas).length === 0) {
// Paid SuperGrok often returns cap=0 over REST but exposes the shared