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

146 lines
5.1 KiB
TypeScript

/**
* usage/qoder.ts — Qoder usage fetcher + status parser.
*
* Extracted from services/usage.ts (god-file decomposition): the Qoder family —
* the /api/v3/user/status endpoint config, the plan-label prettifier, the pure
* status→quotas mapper (parseQoderUserStatusUsage), and the getQoderUsage
* fetcher that exchanges the PAT for a short-lived job token then reads the
* status endpoint. Depends only on the sibling scalar/quota leaves +
* resolveQoderJobToken + sanitizeErrorMessage — no host coupling — so it lives
* as a co-located provider leaf. usage.ts imports getQoderUsage (dispatcher) +
* re-exports parseQoderUserStatusUsage (named export + __testing, used by the
* qoder-usage-quota suite). Behavior-preserving move.
*/
import { sanitizeErrorMessage } from "../../utils/error.ts";
import { resolveQoderJobToken } from "../qoderCli.ts";
import { toRecord, toNumber, toTitleCase } from "./scalars.ts";
import { type UsageQuota, parseResetTime } from "./quota.ts";
type JsonRecord = Record<string, unknown>;
const QODER_USER_STATUS_URL = "https://openapi.qoder.sh/api/v3/user/status";
/** Human-readable plan label from Qoder's `PLAN_TIER_*` enum / `userTag`. */
function prettifyQoderPlan(planRaw: string, userTag: string): string {
const tag = String(userTag || "").trim();
if (tag) return tag;
const stripped = String(planRaw || "")
.trim()
.replace(/^PLAN_TIER_/i, "");
return stripped ? toTitleCase(stripped) : "Qoder";
}
/**
* Map a Qoder `/user/status` payload into the shared `{ plan, quotas }` shape.
* Pure (no I/O) so it can be unit-tested against captured payloads.
*/
export function parseQoderUserStatusUsage(status: JsonRecord): {
plan: string;
quotas: Record<string, UsageQuota>;
} {
const userType = String(status.userType || "")
.trim()
.toLowerCase();
const planLabel = prettifyQoderPlan(String(status.plan || ""), String(status.userTag || ""));
const isExceeded = status.isQuotaExceeded === true;
const quotaNum = toNumber(status.quota, 0);
const resetAt = parseResetTime(status.nextResetAt);
// Team/enterprise seats draw from a pooled org quota rather than a per-user
// counter, so `quota: 0` there means "pooled", not "exhausted".
const isPooled = userType === "teams" || userType === "enterprise";
const quotas: Record<string, UsageQuota> = {};
if (isExceeded) {
// Genuinely out of quota — remainingPercentage 0 lets routing skip it until reset.
quotas["Quota"] = {
used: quotaNum,
total: quotaNum,
remaining: 0,
remainingPercentage: 0,
resetAt,
unlimited: false,
displayName: "Quota exceeded",
};
} else if (isPooled || quotaNum <= 0) {
// Pooled/unlimited seat — MUST report 100% remaining. The quota→routing
// conversion (src/domain/quotaCache.ts) ignores `unlimited` and would treat a
// `total: 0` window as 0% (i.e. exhausted), wrongly 429-ing every request.
quotas["Plan"] = {
used: 0,
total: 0,
remaining: 0,
remainingPercentage: 100,
resetAt,
unlimited: true,
displayName: `${planLabel} plan · pooled quota`,
};
} else {
quotas["Requests"] = {
used: 0,
total: quotaNum,
remaining: quotaNum,
remainingPercentage: 100,
resetAt,
unlimited: false,
displayName: `${quotaNum} requests left`,
};
}
return { plan: planLabel, quotas };
}
/**
* Qoder Usage
*
* Qoder exposes account plan + quota at `openapi.qoder.sh/api/v3/user/status`,
* the same endpoint the official qodercli reads for its usage badge. The status
* call needs a short-lived `jt-*` job token, so we exchange the PAT the same way
* the chat/validation paths do (see qoderCli.ts::resolveQoderJobToken).
*/
export async function getQoderUsage(apiKey?: string, providerSpecificData?: JsonRecord) {
const token = (apiKey || "").trim() || String(providerSpecificData?.qoderPat || "").trim();
if (!token) {
return { message: "Qoder connected. Add a Personal Access Token to view quota." };
}
let jobToken: string;
try {
jobToken = await resolveQoderJobToken(token);
} catch {
return { message: "Qoder connected. Unable to resolve a usage token." };
}
let response: Response;
try {
response = await fetch(QODER_USER_STATUS_URL, {
method: "GET",
headers: { Authorization: `Bearer ${jobToken}`, Accept: "application/json" },
// @ts-ignore — AbortSignal.timeout is available on the Node runtime
signal: AbortSignal.timeout(15000),
});
} catch (error) {
return {
message: `Qoder connected. Unable to fetch usage: ${sanitizeErrorMessage((error as Error).message)}`,
};
}
if (response.status === 401 || response.status === 403) {
return {
message: "Qoder connected. The token was rejected by the usage API — re-test the connection.",
};
}
if (!response.ok) {
return { message: `Qoder connected. Usage API returned HTTP ${response.status}.` };
}
let status: JsonRecord;
try {
status = toRecord(await response.json());
} catch {
return { message: "Qoder connected. Unable to parse the usage response." };
}
return parseQoderUserStatusUsage(status);
}