diff --git a/src/providers/grok.ts b/src/providers/grok.ts index 098eb3b..d710f9d 100644 --- a/src/providers/grok.ts +++ b/src/providers/grok.ts @@ -352,12 +352,15 @@ export async function fetchGrokUsage( const payload = await response.json() as { config?: GrokBillingConfig | null; subscriptionTier?: string } const config = typeof payload.config === 'object' && payload.config !== null ? payload.config : {} const windows: UsageWindow[] = [] - if (typeof config.creditUsagePercent === 'number' && Number.isFinite(config.creditUsagePercent)) { + if (config.currentPeriod || (typeof config.creditUsagePercent === 'number' && Number.isFinite(config.creditUsagePercent))) { const kind: UsageWindow['kind'] = config.currentPeriod?.type === 'USAGE_PERIOD_TYPE_WEEKLY' ? 'weekly' : 'other' const resetsAt = grokResetsAt(config.currentPeriod?.end) - windows.push({ kind, usedPercent: config.creditUsagePercent, ...resetsAt === undefined ? {} : { resetsAt } }) + const usedPercent = typeof config.creditUsagePercent === 'number' && Number.isFinite(config.creditUsagePercent) + ? config.creditUsagePercent + : 0 + windows.push({ kind, usedPercent, ...resetsAt === undefined ? {} : { resetsAt } }) } else if (typeof config.monthlyLimit?.val === 'number' && config.monthlyLimit.val > 0) { const used = typeof config.used?.val === 'number' ? config.used.val : 0 const resetsAt = grokResetsAt(config.billingPeriodEnd) diff --git a/test/usage.spec.ts b/test/usage.spec.ts index 674d64e..0fef6c0 100644 --- a/test/usage.spec.ts +++ b/test/usage.spec.ts @@ -276,6 +276,20 @@ test('fetchGrokUsage tolerates a null config and non-2xx responses', async () => await assert.rejects(fetchGrokUsage(grokSession, failing), /grok billing/) }) +test('fetchGrokUsage extracts reset window when creditUsagePercent is absent but currentPeriod is present', async () => { + const { fetchFn } = fakeFetch({ + config: { + currentPeriod: { type: 'USAGE_PERIOD_TYPE_WEEKLY', end: '2026-09-10T12:00:00Z' }, + }, + }) + const usage = await fetchGrokUsage(grokSession, fetchFn) + assert.ok(usage.windows) + assert.equal(usage.windows.length, 1) + assert.equal(usage.windows[0]?.kind, 'weekly') + assert.equal(usage.windows[0]?.usedPercent, 0) + assert.equal(usage.windows[0]?.resetsAt, Date.parse('2026-09-10T12:00:00Z')) +}) + /** Mount the plugin with fake llm/connection; return the RPC handler. */ async function mount(): Promise { let handler: ConnectionRpcHandler | undefined