Files
OmniRoute/open-sse/services/usage/xai.ts
MumuTW f0b08f95c3 chore(usage): decompose services/usage.ts into per-provider usage/* leaves (999 → 253) (#8545)
* chore(usage): extract crof, nanogpt, qoder, opencode, deepseek, bailian, vertex, xiaomi-mimo, xai, github usage fetchers into usage/* leaves

Decompose services/usage.ts (god-file phase 1): move the remaining per-provider
usage fetcher/parser logic into co-located leaves under open-sse/services/usage/
so usage.ts becomes a thin dispatcher (imports + USAGE_FETCHER_PROVIDERS +
getUsageForProvider switch + __testing re-exports).

New leaves (each a pure data transform or independent fetcher, no orchestration):
  - usage/github.ts    getGitHubUsage, formatGitHubQuotaSnapshot, inferGitHubPlanName, shouldDisplayGitHubQuota
  - usage/crof.ts      getCrofUsage
  - usage/nanogpt.ts   getNanoGptUsage
  - usage/qoder.ts     getQoderUsage, parseQoderUserStatusUsage
  - usage/opencode.ts  getOpencodeUsage
  - usage/deepseek.ts  getDeepseekUsage
  - usage/bailian.ts   getBailianCodingPlanUsage
  - usage/vertex.ts    getVertexUsage
  - usage/xiaomi-mimo.ts getXiaomiMimoUsage
  - usage/xai.ts       getXaiUsage

usage.ts re-exports parseQoderUserStatusUsage (named) and threads every helper
the existing __testing contract exposes (usage-utils / usage-service-hardening /
qoder-usage-quota / xiaomi-mimo-selftrack / xai-usage / vertex-spend suites read
them from services/usage). External importers unchanged.

open-sse/services/usage.ts: 1065 -> 256 lines (cap 800).
npm run check:file-size: OK. typecheck:core: OK. eslint: clean. check:cycles: OK.

Added characterization tests (tests/unit/usage-<provider>-split.test.ts) that
import each leaf directly and pin its export surface + key edges, mirroring the
existing usage-quota-core-split / usage-scalars-split pattern.

* docs(changelog): add fragment for this PR
2026-07-26 03:52:19 -03:00

54 lines
2.2 KiB
TypeScript

/**
* usage/xai.ts — xAI (Grok) self-tracked cumulative usage fetcher.
*
* Extracted from services/usage.ts (god-file decomposition): the xAI family —
* xAI has no public per-account quota API (the billing console at console.x.ai
* requires a session cookie, not an API key), so — exactly like the Xiaomi MiMo
* self-track pattern — OmniRoute sums the tokens it itself routed to this
* connection (from `usage_history`) instead of calling an upstream endpoint.
* Unlike Xiaomi MiMo, xAI has no fixed monthly cap, so the aggregate is reported
* as `unlimited: true` with `remaining: 100`. Depends only on the sibling
* scalar/quota leaves + the usageStats dynamic import — no host coupling — so it
* lives as a co-located provider leaf. usage.ts imports getXaiUsage (dispatcher
* + __testing). Behavior-preserving move.
*/
import { type UsageQuota } from "./quota.ts";
/**
* xAI (Grok) — SELF-TRACKED cumulative usage.
*
* xAI has no public per-account quota API (the billing console at console.x.ai
* requires a session cookie, not an API key), so — exactly like the Xiaomi
* MiMo self-track pattern above — OmniRoute sums the tokens it itself routed
* to this connection (from `usage_history`) instead of calling an upstream
* endpoint. Unlike Xiaomi MiMo, xAI has no fixed monthly cap, so the
* aggregate is reported as `unlimited: true` with `remaining: 100` — this
* renders the dashboard's green "100%" badge instead of a meaningless
* progress bar against a `total: 0`.
*/
export async function getXaiUsage(connectionId: string) {
if (!connectionId) {
return { message: "xAI: connection id unavailable for self-tracked usage." };
}
try {
const { getMonthlyProviderTokensForConnection } = await import("@/lib/usage/usageStats");
const used = getMonthlyProviderTokensForConnection("xai", connectionId);
return {
plan: "xAI / Grok (OmniRoute-tracked)",
quotas: {
monthly: {
used,
total: 0,
remaining: 100,
remainingPercentage: 100,
resetAt: null,
unlimited: true,
} as UsageQuota,
},
};
} catch (error) {
return { message: `xAI self-tracked usage error: ${(error as Error).message}` };
}
}