2026-01-05 10:37:09 +07:00
|
|
|
/**
|
|
|
|
|
* Usage Fetcher - Get usage data from provider APIs
|
|
|
|
|
*/
|
|
|
|
|
|
2026-06-14 19:31:09 +07:00
|
|
|
import { getGitHubUsage } from "./usage/github.js";
|
|
|
|
|
import { getGeminiUsage, getAntigravityUsage } from "./usage/google.js";
|
|
|
|
|
import { getClaudeUsage } from "./usage/claude.js";
|
2026-07-03 15:02:28 +07:00
|
|
|
import { getCodexUsage, consumeCodexRateLimitResetCredit, getCodexRateLimitResetCredits } from "./usage/codex.js";
|
2026-06-15 18:18:04 +07:00
|
|
|
|
2026-07-03 15:02:28 +07:00
|
|
|
export { consumeCodexRateLimitResetCredit, getCodexRateLimitResetCredits };
|
2026-06-14 19:31:09 +07:00
|
|
|
import { getKiroUsage } from "./usage/kiro.js";
|
|
|
|
|
import { getMiniMaxUsage } from "./usage/minimax.js";
|
2026-08-05 10:51:29 +07:00
|
|
|
import { getCodeBuddyCnUsage, getCodeBuddyIntlUsage } from "./usage/codebuddy-cn.js";
|
2026-07-10 11:41:19 +07:00
|
|
|
import { getGrokCliUsage } from "./usage/grok-cli.js";
|
2026-07-29 18:09:35 +07:00
|
|
|
import { getKimiUsage } from "./usage/kimi.js";
|
|
|
|
|
import { getDeepseekUsage } from "./usage/deepseek.js";
|
2026-09-05 21:07:58 +07:00
|
|
|
import { getOpenCodeGoUsage } from "./usage/opencode-go.js";
|
2026-09-19 10:01:51 +07:00
|
|
|
import { getOpenCodeZenUsage } from "./usage/opencode-zen.js";
|
2026-09-02 20:03:44 +07:00
|
|
|
import { getGroqUsage } from "./usage/groq.js";
|
2026-08-28 15:52:51 +07:00
|
|
|
import { getZedUsage } from "./usage/zed.js";
|
2026-09-10 23:41:40 +07:00
|
|
|
import { getXiaomiMimoUsage } from "./usage/xiaomi-mimo.js";
|
2026-08-05 11:16:01 +07:00
|
|
|
import { resolveQoderCredentials } from "./qoderModels.js";
|
2026-08-28 15:34:37 +07:00
|
|
|
import { getGlmUsage } from "./usage/glm.js";
|
2026-09-16 20:16:32 +07:00
|
|
|
import { getCommandCodeUsage } from "./usage/commandcode.js";
|
2026-06-14 19:31:09 +07:00
|
|
|
import {
|
|
|
|
|
getIflowUsage,
|
|
|
|
|
getOllamaUsage,
|
|
|
|
|
getVercelAiGatewayUsage,
|
|
|
|
|
getQoderUsage,
|
|
|
|
|
} from "./usage/misc.js";
|
2026-01-05 10:37:09 +07:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get usage data for a provider connection
|
|
|
|
|
* @param {Object} connection - Provider connection with accessToken
|
|
|
|
|
* @returns {Object} Usage data with quotas
|
|
|
|
|
*/
|
2026-06-13 21:46:13 +07:00
|
|
|
// 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),
|
2026-08-13 11:27:57 +07:00
|
|
|
claude: (c) => getClaudeUsage(c.accessToken, c.proxyOptions, { force: c.force }),
|
2026-06-13 21:46:13 +07:00
|
|
|
codex: (c) => getCodexUsage(c.accessToken, c.proxyOptions),
|
|
|
|
|
kiro: (c) => getKiroUsage(c.accessToken, c.providerSpecificData, c.proxyOptions),
|
2026-08-05 11:16:01 +07:00
|
|
|
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);
|
|
|
|
|
},
|
2026-06-13 21:46:13 +07:00
|
|
|
iflow: (c) => getIflowUsage(c.accessToken),
|
2026-08-02 09:34:27 +07:00
|
|
|
ollama: (c) => getOllamaUsage(c.apiKey, c.providerSpecificData, c.proxyOptions),
|
2026-06-13 21:46:13 +07:00
|
|
|
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),
|
2026-06-21 16:11:20 +07:00
|
|
|
"codebuddy-cn": (c) => getCodeBuddyCnUsage(c.accessToken, c.apiKey, c.providerSpecificData, c.proxyOptions),
|
2026-08-05 10:51:29 +07:00
|
|
|
"codebuddy-intl": (c) => getCodeBuddyIntlUsage(c.accessToken, c.apiKey, c.providerSpecificData, c.proxyOptions),
|
2026-07-10 11:41:19 +07:00
|
|
|
"grok-cli": (c) => getGrokCliUsage(c.accessToken, c.providerSpecificData, c.proxyOptions),
|
2026-07-29 18:09:35 +07:00
|
|
|
kimi: (c) => getKimiUsage(c.accessToken, c.apiKey, c.proxyOptions, c.providerSpecificData),
|
2026-09-05 21:07:58 +07:00
|
|
|
"opencode-go": (c) => getOpenCodeGoUsage(c.apiKey, c.proxyOptions),
|
2026-09-19 10:01:51 +07:00
|
|
|
"opencode-zen": (c) => getOpenCodeZenUsage(c.apiKey, c.proxyOptions),
|
2026-07-29 18:09:35 +07:00
|
|
|
deepseek: (c) => getDeepseekUsage(c.apiKey, c.proxyOptions),
|
2026-09-02 20:03:44 +07:00
|
|
|
groq: (c) => getGroqUsage(c.apiKey, c.proxyOptions),
|
2026-08-28 15:52:51 +07:00
|
|
|
zed: (c) => getZedUsage(c.accessToken, c.providerSpecificData, c.proxyOptions),
|
2026-09-10 23:41:40 +07:00
|
|
|
"xiaomi-mimo": (c) => getXiaomiMimoUsage(c.accessToken, c.providerSpecificData, c.proxyOptions),
|
2026-09-16 20:16:32 +07:00
|
|
|
commandcode: (c) => getCommandCodeUsage(c.apiKey, c.proxyOptions),
|
2026-06-13 21:46:13 +07:00
|
|
|
};
|
|
|
|
|
|
2026-08-13 11:27:57 +07:00
|
|
|
export async function getUsageForProvider(connection, proxyOptions = null, options = {}) {
|
2026-05-26 06:23:47 +02:00
|
|
|
const { provider, accessToken, apiKey, providerSpecificData, projectId } = connection;
|
|
|
|
|
const providerDataWithProjectId = {
|
|
|
|
|
...(providerSpecificData || {}),
|
|
|
|
|
...(projectId ? { projectId } : {}),
|
|
|
|
|
};
|
2026-01-05 10:37:09 +07:00
|
|
|
|
2026-06-13 21:46:13 +07:00
|
|
|
const handler = USAGE_HANDLERS[provider];
|
|
|
|
|
if (!handler) return { message: `Usage API not implemented for ${provider}` };
|
2026-08-13 11:27:57 +07:00
|
|
|
return await handler({
|
|
|
|
|
provider,
|
|
|
|
|
accessToken,
|
|
|
|
|
apiKey,
|
|
|
|
|
providerSpecificData,
|
|
|
|
|
providerDataWithProjectId,
|
|
|
|
|
proxyOptions,
|
|
|
|
|
force: options.force === true,
|
|
|
|
|
});
|
2026-01-05 10:37:09 +07:00
|
|
|
}
|