Skip to content
Open
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
7 changes: 5 additions & 2 deletions src/providers/grok.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
14 changes: 14 additions & 0 deletions test/usage.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<ConnectionRpcHandler> {
let handler: ConnectionRpcHandler | undefined
Expand Down