9router/open-sse/services/usage.js

78 lines
3.6 KiB
JavaScript
Raw Normal View History

/**
* Usage Fetcher - Get usage data from provider APIs
*/
import { getGitHubUsage } from "./usage/github.js";
import { getGeminiUsage, getAntigravityUsage } from "./usage/google.js";
import { getClaudeUsage } from "./usage/claude.js";
import { getCodexUsage, consumeCodexRateLimitResetCredit, getCodexRateLimitResetCredits } from "./usage/codex.js";
2026-06-15 07:18:04 -04:00
export { consumeCodexRateLimitResetCredit, getCodexRateLimitResetCredits };
import { getKiroUsage } from "./usage/kiro.js";
import { getMiniMaxUsage } from "./usage/minimax.js";
import { getCodeBuddyCnUsage, getCodeBuddyIntlUsage } from "./usage/codebuddy-cn.js";
import { getGrokCliUsage } from "./usage/grok-cli.js";
import { getKimiUsage } from "./usage/kimi.js";
import { getDeepseekUsage } from "./usage/deepseek.js";
import { resolveQoderCredentials } from "./qoderModels.js";
import {
getIflowUsage,
getOllamaUsage,
getGlmUsage,
getVercelAiGatewayUsage,
getQoderUsage,
} from "./usage/misc.js";
/**
* Get usage data for a provider connection
* @param {Object} connection - Provider connection with accessToken
* @returns {Object} Usage data with quotas
*/
// provider → usage handler (ctx carries every arg each handler needs)
const USAGE_HANDLERS = {
github: (c) => getGitHubUsage(c.accessToken, c.providerSpecificData, c.proxyOptions),
"gemini-cli": (c) => getGeminiUsage(c.accessToken, c.providerDataWithProjectId, c.proxyOptions),
antigravity: (c) => getAntigravityUsage(c.accessToken, c.providerSpecificData, c.proxyOptions),
claude: (c) => getClaudeUsage(c.accessToken, c.proxyOptions, { force: c.force }),
codex: (c) => getCodexUsage(c.accessToken, c.proxyOptions),
kiro: (c) => getKiroUsage(c.accessToken, c.providerSpecificData, c.proxyOptions),
qoder: async (c) => {
// PAT (pt-...) connections must be exchanged to a job token before the
// quota endpoint accepts them.
const resolved = await resolveQoderCredentials(c, c.proxyOptions).catch(() => null);
return getQoderUsage(resolved?.accessToken || c.accessToken, c.proxyOptions);
},
iflow: (c) => getIflowUsage(c.accessToken),
ollama: (c) => getOllamaUsage(c.apiKey, c.providerSpecificData, c.proxyOptions),
glm: (c) => getGlmUsage(c.apiKey, c.provider, c.proxyOptions),
"glm-cn": (c) => getGlmUsage(c.apiKey, c.provider, c.proxyOptions),
minimax: (c) => getMiniMaxUsage(c.apiKey, c.provider, c.proxyOptions),
"minimax-cn": (c) => getMiniMaxUsage(c.apiKey, c.provider, c.proxyOptions),
"vercel-ai-gateway": (c) => getVercelAiGatewayUsage(c.apiKey, c.proxyOptions),
feat(codebuddy-cn): add API key auth + credit quota tracker Two additions on top of the merged CodeBuddy CN OAuth provider: 1. API key auth — connect with a direct API key (sent as Authorization: Bearer), not only via OAuth device-code flow. - registry: add authModes ["oauth","apikey"] + hasOAuth; combined Bearer auth already forwards the key, token-refresh skips key connections. - providers POST: accept dual-auth providers (authModes includes "apikey") that live under category "oauth" — previously rejected as "Invalid provider". Also fixes the same latent gap for xai. 2. Quota tracker — surface CodeBuddy CN credit balance on the usage dashboard for both OAuth and API-key connections. - registry: add transport.usage.url (Tencent billing endpoint) + features.usage/usageApikey so the connection is quota-eligible. - new CN-scoped handler services/usage/codebuddy-cn.js: POST the billing meter endpoint, unwrap data.Response.Data.Accounts[]. The payload mixes two credit types that must not be merged: * refill/base ("基础体验包") — recurring allowance; cycle resets well before the resource expires (CycleEndTime << DeductionEndTime). Reads the *Cycle* balance, resetAt = next refresh. Cadence-labelled. * bonus ("活动赠送包") — one-shot credits that expire at CycleEndTime. Reads the plain Capacity balance. Labelled "Bonus Pack N". One quota row per package, soonest-expiring first. - register handler under "codebuddy-cn" in USAGE_HANDLERS. Frontend needs no change — USAGE_SUPPORTED_PROVIDERS/USAGE_APIKEY_PROVIDERS and the generic parseQuotaData branch already cover this shape. Co-authored-by: Cursor <cursoragent@cursor.com>
2026-06-21 05:11:20 -04:00
"codebuddy-cn": (c) => getCodeBuddyCnUsage(c.accessToken, c.apiKey, c.providerSpecificData, c.proxyOptions),
"codebuddy-intl": (c) => getCodeBuddyIntlUsage(c.accessToken, c.apiKey, c.providerSpecificData, c.proxyOptions),
"grok-cli": (c) => getGrokCliUsage(c.accessToken, c.providerSpecificData, c.proxyOptions),
kimi: (c) => getKimiUsage(c.accessToken, c.apiKey, c.proxyOptions, c.providerSpecificData),
deepseek: (c) => getDeepseekUsage(c.apiKey, c.proxyOptions),
};
export async function getUsageForProvider(connection, proxyOptions = null, options = {}) {
const { provider, accessToken, apiKey, providerSpecificData, projectId } = connection;
const providerDataWithProjectId = {
...(providerSpecificData || {}),
...(projectId ? { projectId } : {}),
};
const handler = USAGE_HANDLERS[provider];
if (!handler) return { message: `Usage API not implemented for ${provider}` };
return await handler({
provider,
accessToken,
apiKey,
providerSpecificData,
providerDataWithProjectId,
proxyOptions,
force: options.force === true,
});
}