Files
OmniRoute/open-sse/services/usage/xiaomi-mimo.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

52 lines
2.3 KiB
TypeScript

/**
* usage/xiaomi-mimo.ts — Xiaomi MiMo self-tracked monthly quota fetcher.
*
* Extracted from services/usage.ts (god-file decomposition): the Xiaomi MiMo family —
* Xiaomi exposes plan usage only behind the console session cookie (the API key
* cannot reach the `tokenPlan/usage` endpoint), so OmniRoute self-tracks the
* tokens it routed to the connection in the current UTC month (from usage_history)
* and compares them to the known Token Plan monthly limit. 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 getXiaomiMimoUsage
* (dispatcher + __testing). Behavior-preserving move.
*/
import { createQuotaFromUsage } from "./quota.ts";
// Xiaomi MiMo Token Plan monthly limit (tokens). Keep in sync with the
// "xiaomi-mimo" preset in src/lib/quota/planRegistry.ts.
const XIAOMI_MIMO_MONTHLY_TOKEN_LIMIT = 4_100_000_000;
/**
* Xiaomi MiMo — SELF-TRACKED monthly quota.
*
* Xiaomi exposes plan usage only behind the console session cookie (the API key
* cannot reach the `tokenPlan/usage` endpoint), so there is no upstream usage
* API to call. Instead we count the tokens OmniRoute itself routed to this
* connection in the current UTC month (from `usage_history`) and compare them
* to the known Token Plan monthly limit. This reflects only traffic that went
* through OmniRoute, not the provider's own dashboard figure.
*/
export async function getXiaomiMimoUsage(connectionId: string) {
if (!connectionId) {
return { message: "Xiaomi MiMo: connection id unavailable for self-tracked quota." };
}
try {
const { getMonthlyProviderTokensForConnection } = await import("@/lib/usage/usageStats");
const used = getMonthlyProviderTokensForConnection("xiaomi-mimo", connectionId);
const total = XIAOMI_MIMO_MONTHLY_TOKEN_LIMIT;
const now = new Date();
const resetAt = new Date(
Date.UTC(now.getUTCFullYear(), now.getUTCMonth() + 1, 1)
).toISOString();
return {
plan: "Xiaomi MiMo Token Plan (OmniRoute-tracked)",
quotas: {
monthly: createQuotaFromUsage(used, total, resetAt),
},
};
} catch (error) {
return { message: `Xiaomi MiMo self-tracked usage error: ${(error as Error).message}` };
}
}