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

87 lines
2.8 KiB
TypeScript

/**
* usage/opencode.ts — OpenCode / OpenCode Zen usage fetcher.
*
* Extracted from services/usage.ts (god-file decomposition): the OpenCode family —
* delegates to the dedicated opencodeQuotaFetcher and shapes the triple-window
* ($12/5h, $30/wk, $60/mo) result into the standard `{ plan, quotas }` usage
* response expected by the limits page. Depends only on the sibling scalar/quota
* leaves + fetchOpencodeQuota + sanitizeErrorMessage — no host coupling — so it
* lives as a co-located provider leaf. usage.ts imports getOpencodeUsage
* (dispatcher + __testing). Behavior-preserving move.
*/
import { fetchOpencodeQuota, type OpencodeTripleWindowQuota } from "../opencodeQuotaFetcher.ts";
import { sanitizeErrorMessage } from "../../utils/error.ts";
import { type UsageQuota } from "./quota.ts";
/**
* OpenCode Go / OpenCode / OpenCode Zen Usage
* Delegates to the dedicated opencodeQuotaFetcher and shapes the result into
* the standard `{ plan, quotas }` usage response expected by the limits page.
*
* Three rolling windows are surfaced: $12/5h, $30/wk, $60/mo.
*/
export async function getOpencodeUsage(connectionId: string, apiKey: string) {
if (!apiKey) {
return { message: "OpenCode API key not available. Add a key to view usage." };
}
try {
const quota = (await fetchOpencodeQuota(connectionId, {
apiKey,
})) as OpencodeTripleWindowQuota | null;
if (!quota) {
return { message: "OpenCode connected. Unable to fetch quota data." };
}
const { window5h, windowWeekly, windowMonthly, limitReached } = quota;
const quotas: Record<string, UsageQuota> = {};
// $12 / 5-hour rolling window
quotas["window_5h"] = {
used: window5h.percentUsed * 12,
total: 12,
remaining: (1 - window5h.percentUsed) * 12,
remainingPercentage: (1 - window5h.percentUsed) * 100,
resetAt: window5h.resetAt,
unlimited: false,
displayName: "$12 / 5-hour",
currency: "USD",
};
// $30 / weekly window
quotas["window_weekly"] = {
used: windowWeekly.percentUsed * 30,
total: 30,
remaining: (1 - windowWeekly.percentUsed) * 30,
remainingPercentage: (1 - windowWeekly.percentUsed) * 100,
resetAt: windowWeekly.resetAt,
unlimited: false,
displayName: "$30 / week",
currency: "USD",
};
// $60 / monthly window
quotas["window_monthly"] = {
used: windowMonthly.percentUsed * 60,
total: 60,
remaining: (1 - windowMonthly.percentUsed) * 60,
remainingPercentage: (1 - windowMonthly.percentUsed) * 100,
resetAt: windowMonthly.resetAt,
unlimited: false,
displayName: "$60 / month",
currency: "USD",
};
return {
plan: "OpenCode Go",
quotas,
limitReached,
};
} catch (error) {
return { message: `OpenCode error: ${sanitizeErrorMessage(error)}` };
}
}