wyxrouter 0.4.71
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +222 -0
- package/LICENSE +21 -0
- package/README.md +96 -0
- package/README.zh-CN.md +1311 -0
- package/assets/pixel-router-2d.png +0 -0
- package/cli/LICENSE +42 -0
- package/cli/README.md +125 -0
- package/cli/app/package.json +58 -0
- package/cli/cli.js +806 -0
- package/cli/package.json +48 -0
- package/i18n/README.ja-JP.md +1209 -0
- package/i18n/README.ru.md +1311 -0
- package/i18n/README.vi.md +1310 -0
- package/i18n/README.zh-CN.md +1306 -0
- package/images/9router.png +0 -0
- package/jsconfig.json +12 -0
- package/next.config.mjs +71 -0
- package/open-sse/config/appConstants.js +203 -0
- package/open-sse/config/codexInstructions.js +119 -0
- package/open-sse/config/constants.js +4 -0
- package/open-sse/config/defaultThinkingSignature.js +12 -0
- package/open-sse/config/errorConfig.js +85 -0
- package/open-sse/config/googleTtsLanguages.js +62 -0
- package/open-sse/config/kiroConstants.js +322 -0
- package/open-sse/config/models.js +13 -0
- package/open-sse/config/ollamaModels.js +19 -0
- package/open-sse/config/providerModels.js +944 -0
- package/open-sse/config/providers.js +458 -0
- package/open-sse/config/runtimeConfig.js +72 -0
- package/open-sse/config/ttsModels.js +129 -0
- package/open-sse/executors/antigravity.js +504 -0
- package/open-sse/executors/azure.js +57 -0
- package/open-sse/executors/base.js +185 -0
- package/open-sse/executors/codex.js +469 -0
- package/open-sse/executors/commandcode.js +88 -0
- package/open-sse/executors/cursor.js +795 -0
- package/open-sse/executors/default.js +497 -0
- package/open-sse/executors/gemini-cli.js +89 -0
- package/open-sse/executors/github.js +379 -0
- package/open-sse/executors/grok-web.js +345 -0
- package/open-sse/executors/iflow.js +108 -0
- package/open-sse/executors/index.js +75 -0
- package/open-sse/executors/kiro.js +508 -0
- package/open-sse/executors/ollama-local.js +14 -0
- package/open-sse/executors/opencode-go.js +41 -0
- package/open-sse/executors/opencode.js +32 -0
- package/open-sse/executors/perplexity-web.js +507 -0
- package/open-sse/executors/qoder.js +450 -0
- package/open-sse/executors/qwen.js +129 -0
- package/open-sse/executors/vertex.js +131 -0
- package/open-sse/executors/xiaomi-tokenplan.js +19 -0
- package/open-sse/handlers/chatCore/nonStreamingHandler.js +230 -0
- package/open-sse/handlers/chatCore/requestDetail.js +102 -0
- package/open-sse/handlers/chatCore/sseToJsonHandler.js +231 -0
- package/open-sse/handlers/chatCore/streamingHandler.js +103 -0
- package/open-sse/handlers/chatCore.js +287 -0
- package/open-sse/handlers/embeddingProviders/_base.js +4 -0
- package/open-sse/handlers/embeddingProviders/gemini.js +54 -0
- package/open-sse/handlers/embeddingProviders/index.js +23 -0
- package/open-sse/handlers/embeddingProviders/openai.js +39 -0
- package/open-sse/handlers/embeddingProviders/openaiCompatNode.js +13 -0
- package/open-sse/handlers/embeddingsCore.js +126 -0
- package/open-sse/handlers/fetch/index.js +237 -0
- package/open-sse/handlers/imageGenerationCore.js +189 -0
- package/open-sse/handlers/imageProviders/_base.js +31 -0
- package/open-sse/handlers/imageProviders/blackForestLabs.js +43 -0
- package/open-sse/handlers/imageProviders/cloudflareAi.js +178 -0
- package/open-sse/handlers/imageProviders/codex.js +198 -0
- package/open-sse/handlers/imageProviders/comfyui.js +8 -0
- package/open-sse/handlers/imageProviders/falAi.js +41 -0
- package/open-sse/handlers/imageProviders/gemini.js +25 -0
- package/open-sse/handlers/imageProviders/huggingface.js +22 -0
- package/open-sse/handlers/imageProviders/index.js +40 -0
- package/open-sse/handlers/imageProviders/nanobanana.js +58 -0
- package/open-sse/handlers/imageProviders/openai.js +40 -0
- package/open-sse/handlers/imageProviders/runwayml.js +47 -0
- package/open-sse/handlers/imageProviders/sdwebui.js +17 -0
- package/open-sse/handlers/imageProviders/stabilityAi.js +34 -0
- package/open-sse/handlers/responsesHandler.js +103 -0
- package/open-sse/handlers/search/callers.js +371 -0
- package/open-sse/handlers/search/chatSearch.js +409 -0
- package/open-sse/handlers/search/index.js +201 -0
- package/open-sse/handlers/search/normalizers.js +223 -0
- package/open-sse/handlers/sttCore.js +194 -0
- package/open-sse/handlers/ttsCore.js +74 -0
- package/open-sse/handlers/ttsProviders/_base.js +39 -0
- package/open-sse/handlers/ttsProviders/edgeTts.js +89 -0
- package/open-sse/handlers/ttsProviders/elevenlabs.js +48 -0
- package/open-sse/handlers/ttsProviders/gemini.js +117 -0
- package/open-sse/handlers/ttsProviders/genericFormats.js +169 -0
- package/open-sse/handlers/ttsProviders/googleTts.js +54 -0
- package/open-sse/handlers/ttsProviders/index.js +50 -0
- package/open-sse/handlers/ttsProviders/localDevice.js +87 -0
- package/open-sse/handlers/ttsProviders/minimax.js +59 -0
- package/open-sse/handlers/ttsProviders/openai.js +30 -0
- package/open-sse/handlers/ttsProviders/openrouter.js +70 -0
- package/open-sse/index.js +82 -0
- package/open-sse/rtk/applyFilter.js +15 -0
- package/open-sse/rtk/autodetect.js +111 -0
- package/open-sse/rtk/caveman.js +100 -0
- package/open-sse/rtk/cavemanPrompts.js +78 -0
- package/open-sse/rtk/constants.js +55 -0
- package/open-sse/rtk/filters/buildOutput.js +127 -0
- package/open-sse/rtk/filters/dedupLog.js +44 -0
- package/open-sse/rtk/filters/find.js +49 -0
- package/open-sse/rtk/filters/gitDiff.js +92 -0
- package/open-sse/rtk/filters/gitStatus.js +117 -0
- package/open-sse/rtk/filters/grep.js +48 -0
- package/open-sse/rtk/filters/ls.js +79 -0
- package/open-sse/rtk/filters/readNumbered.js +27 -0
- package/open-sse/rtk/filters/searchList.js +52 -0
- package/open-sse/rtk/filters/smartTruncate.js +15 -0
- package/open-sse/rtk/filters/tree.js +32 -0
- package/open-sse/rtk/index.js +155 -0
- package/open-sse/rtk/registry.js +38 -0
- package/open-sse/services/accountFallback.js +238 -0
- package/open-sse/services/combo.js +198 -0
- package/open-sse/services/compact.js +71 -0
- package/open-sse/services/kiroModels.js +332 -0
- package/open-sse/services/model.js +261 -0
- package/open-sse/services/oauthCredentialManager.js +151 -0
- package/open-sse/services/projectId.js +306 -0
- package/open-sse/services/provider.js +356 -0
- package/open-sse/services/qoderModels.js +214 -0
- package/open-sse/services/tokenRefresh.js +939 -0
- package/open-sse/services/usage.js +1496 -0
- package/open-sse/transformer/responsesTransformer.js +439 -0
- package/open-sse/transformer/streamToJsonConverter.js +103 -0
- package/open-sse/translator/formats.js +36 -0
- package/open-sse/translator/helpers/claudeHelper.js +216 -0
- package/open-sse/translator/helpers/geminiHelper.js +372 -0
- package/open-sse/translator/helpers/imageHelper.js +34 -0
- package/open-sse/translator/helpers/maxTokensHelper.js +27 -0
- package/open-sse/translator/helpers/openaiHelper.js +130 -0
- package/open-sse/translator/helpers/responsesApiHelper.js +139 -0
- package/open-sse/translator/helpers/toolCallHelper.js +148 -0
- package/open-sse/translator/index.js +251 -0
- package/open-sse/translator/request/antigravity-to-openai.js +229 -0
- package/open-sse/translator/request/claude-to-openai.js +232 -0
- package/open-sse/translator/request/gemini-to-openai.js +147 -0
- package/open-sse/translator/request/openai-responses.js +318 -0
- package/open-sse/translator/request/openai-to-claude.js +401 -0
- package/open-sse/translator/request/openai-to-commandcode.js +170 -0
- package/open-sse/translator/request/openai-to-cursor.js +183 -0
- package/open-sse/translator/request/openai-to-gemini.js +470 -0
- package/open-sse/translator/request/openai-to-kiro.js +629 -0
- package/open-sse/translator/request/openai-to-kiro.old.js +278 -0
- package/open-sse/translator/request/openai-to-ollama.js +192 -0
- package/open-sse/translator/request/openai-to-vertex.js +42 -0
- package/open-sse/translator/response/claude-to-openai.js +206 -0
- package/open-sse/translator/response/commandcode-to-openai.js +197 -0
- package/open-sse/translator/response/cursor-to-openai.js +30 -0
- package/open-sse/translator/response/gemini-to-openai.js +245 -0
- package/open-sse/translator/response/kiro-to-openai.js +195 -0
- package/open-sse/translator/response/ollama-to-openai.js +152 -0
- package/open-sse/translator/response/openai-responses.js +590 -0
- package/open-sse/translator/response/openai-to-antigravity.js +122 -0
- package/open-sse/translator/response/openai-to-claude.js +266 -0
- package/open-sse/utils/bypassHandler.js +298 -0
- package/open-sse/utils/claudeCloaking.js +155 -0
- package/open-sse/utils/claudeHeaderCache.js +70 -0
- package/open-sse/utils/clientDetector.js +63 -0
- package/open-sse/utils/cursorChecksum.js +149 -0
- package/open-sse/utils/cursorProtobuf.js +904 -0
- package/open-sse/utils/debugLog.js +14 -0
- package/open-sse/utils/error.js +147 -0
- package/open-sse/utils/ollamaTransform.js +85 -0
- package/open-sse/utils/proxyFetch.js +368 -0
- package/open-sse/utils/reasoningContentInjector.js +79 -0
- package/open-sse/utils/requestLogger.js +260 -0
- package/open-sse/utils/responsesStreamHelpers.js +49 -0
- package/open-sse/utils/sessionManager.js +82 -0
- package/open-sse/utils/stream.js +462 -0
- package/open-sse/utils/streamHandler.js +250 -0
- package/open-sse/utils/streamHelpers.js +122 -0
- package/open-sse/utils/toolDeduper.js +49 -0
- package/open-sse/utils/usageTracking.js +347 -0
- package/package.json +100 -0
- package/postcss.config.mjs +12 -0
- package/public/favicon.svg +11 -0
- package/public/file.svg +1 -0
- package/public/globe.svg +1 -0
- package/public/i18n/literals/ar.json +195 -0
- package/public/i18n/literals/bn.json +195 -0
- package/public/i18n/literals/cs.json +195 -0
- package/public/i18n/literals/da.json +195 -0
- package/public/i18n/literals/de.json +195 -0
- package/public/i18n/literals/el.json +195 -0
- package/public/i18n/literals/es.json +195 -0
- package/public/i18n/literals/fi.json +195 -0
- package/public/i18n/literals/fr.json +195 -0
- package/public/i18n/literals/he.json +195 -0
- package/public/i18n/literals/hi.json +195 -0
- package/public/i18n/literals/hu.json +195 -0
- package/public/i18n/literals/id.json +195 -0
- package/public/i18n/literals/it.json +195 -0
- package/public/i18n/literals/ja.json +195 -0
- package/public/i18n/literals/ko.json +195 -0
- package/public/i18n/literals/nl.json +195 -0
- package/public/i18n/literals/no.json +195 -0
- package/public/i18n/literals/pl.json +195 -0
- package/public/i18n/literals/pt-BR.json +195 -0
- package/public/i18n/literals/pt-PT.json +195 -0
- package/public/i18n/literals/ro.json +195 -0
- package/public/i18n/literals/ru.json +195 -0
- package/public/i18n/literals/sv.json +195 -0
- package/public/i18n/literals/th.json +195 -0
- package/public/i18n/literals/tl.json +195 -0
- package/public/i18n/literals/tr.json +195 -0
- package/public/i18n/literals/uk.json +195 -0
- package/public/i18n/literals/ur.json +195 -0
- package/public/i18n/literals/vi.json +195 -0
- package/public/i18n/literals/zh-CN.json +772 -0
- package/public/i18n/literals/zh-TW.json +195 -0
- package/public/icons/discord.svg +4 -0
- package/public/icons/icon-192.svg +4 -0
- package/public/icons/icon-512.svg +4 -0
- package/public/next.svg +1 -0
- package/public/providers/alicode-intl.png +0 -0
- package/public/providers/alicode.png +0 -0
- package/public/providers/amp.png +0 -0
- package/public/providers/anthropic-m.png +0 -0
- package/public/providers/anthropic.png +0 -0
- package/public/providers/antigravity.png +0 -0
- package/public/providers/assemblyai.png +0 -0
- package/public/providers/aws-polly.png +0 -0
- package/public/providers/azure.png +0 -0
- package/public/providers/black-forest-labs.png +0 -0
- package/public/providers/blackbox.png +0 -0
- package/public/providers/brave-search.png +0 -0
- package/public/providers/byteplus.png +0 -0
- package/public/providers/cartesia.png +0 -0
- package/public/providers/cerebras.png +0 -0
- package/public/providers/chutes.png +0 -0
- package/public/providers/claude.png +0 -0
- package/public/providers/cline.png +0 -0
- package/public/providers/cloudflare-ai.png +0 -0
- package/public/providers/codebuddy.svg +37 -0
- package/public/providers/codex.png +0 -0
- package/public/providers/cohere.png +0 -0
- package/public/providers/comfyui.png +0 -0
- package/public/providers/commandcode.png +0 -0
- package/public/providers/continue.png +0 -0
- package/public/providers/copilot.png +0 -0
- package/public/providers/coqui.png +0 -0
- package/public/providers/cursor.png +0 -0
- package/public/providers/deepgram.png +0 -0
- package/public/providers/deepseek-tui.png +0 -0
- package/public/providers/deepseek.png +0 -0
- package/public/providers/droid.png +0 -0
- package/public/providers/edge-tts.png +0 -0
- package/public/providers/elevenlabs.png +0 -0
- package/public/providers/exa.png +0 -0
- package/public/providers/fal-ai.png +0 -0
- package/public/providers/firecrawl.png +0 -0
- package/public/providers/fireworks.png +0 -0
- package/public/providers/gemini-cli.png +0 -0
- package/public/providers/gemini.png +0 -0
- package/public/providers/github.png +0 -0
- package/public/providers/glm-cn.png +0 -0
- package/public/providers/glm.png +0 -0
- package/public/providers/google-pse.png +0 -0
- package/public/providers/google-tts.png +0 -0
- package/public/providers/grok-web.png +0 -0
- package/public/providers/groq.png +0 -0
- package/public/providers/hermes.png +0 -0
- package/public/providers/huggingface.png +0 -0
- package/public/providers/hyperbolic.png +0 -0
- package/public/providers/iflow.png +0 -0
- package/public/providers/inworld.png +0 -0
- package/public/providers/jcode.png +0 -0
- package/public/providers/jina-ai.png +0 -0
- package/public/providers/jina-reader.png +0 -0
- package/public/providers/kilocode.png +0 -0
- package/public/providers/kimi-coding.png +0 -0
- package/public/providers/kimi.png +0 -0
- package/public/providers/kiro.png +0 -0
- package/public/providers/linkup.png +0 -0
- package/public/providers/local-device.png +0 -0
- package/public/providers/minimax-cn.png +0 -0
- package/public/providers/minimax.png +0 -0
- package/public/providers/mistral.png +0 -0
- package/public/providers/nanobanana.png +0 -0
- package/public/providers/nebius.png +0 -0
- package/public/providers/nvidia.png +0 -0
- package/public/providers/oai-cc.png +0 -0
- package/public/providers/oai-r.png +0 -0
- package/public/providers/ollama-local.png +0 -0
- package/public/providers/ollama.png +0 -0
- package/public/providers/openai.png +0 -0
- package/public/providers/openclaw.png +0 -0
- package/public/providers/opencode-go.png +0 -0
- package/public/providers/opencode.png +0 -0
- package/public/providers/openrouter.png +0 -0
- package/public/providers/perplexity-web.png +0 -0
- package/public/providers/perplexity.png +0 -0
- package/public/providers/playht.png +0 -0
- package/public/providers/qoder.png +0 -0
- package/public/providers/qwen.png +0 -0
- package/public/providers/recraft.png +0 -0
- package/public/providers/roo.png +0 -0
- package/public/providers/runwayml.png +0 -0
- package/public/providers/sdwebui.png +0 -0
- package/public/providers/searchapi.png +0 -0
- package/public/providers/searxng.png +0 -0
- package/public/providers/serper.png +0 -0
- package/public/providers/siliconflow.png +0 -0
- package/public/providers/stability-ai.png +0 -0
- package/public/providers/tavily.png +0 -0
- package/public/providers/together.png +0 -0
- package/public/providers/topaz.png +0 -0
- package/public/providers/tortoise.png +0 -0
- package/public/providers/vertex-partner.png +0 -0
- package/public/providers/vertex.png +0 -0
- package/public/providers/volcengine-ark.png +0 -0
- package/public/providers/voyage-ai.png +0 -0
- package/public/providers/xai.png +0 -0
- package/public/providers/xiaomi-mimo.png +0 -0
- package/public/providers/xiaomi-tokenplan.png +0 -0
- package/public/providers/youcom.png +0 -0
- package/public/sw.js +22 -0
- package/public/vercel.svg +1 -0
- package/public/window.svg +1 -0
- package/scripts/compact-request-details.mjs +71 -0
- package/scripts/import-codex-gptjson.mjs +342 -0
- package/scripts/start-standalone.mjs +25 -0
- package/scripts/translate-readme.js +201 -0
- package/src/app/(dashboard)/dashboard/automation/page.js +294 -0
- package/src/app/(dashboard)/dashboard/basic-chat/BasicChatPageClient.js +967 -0
- package/src/app/(dashboard)/dashboard/basic-chat/page.js +5 -0
- package/src/app/(dashboard)/dashboard/cli-tools/CLIToolsPageClient.js +66 -0
- package/src/app/(dashboard)/dashboard/cli-tools/[toolId]/ToolDetailClient.js +173 -0
- package/src/app/(dashboard)/dashboard/cli-tools/[toolId]/page.js +11 -0
- package/src/app/(dashboard)/dashboard/cli-tools/components/AntigravityToolCard.js +481 -0
- package/src/app/(dashboard)/dashboard/cli-tools/components/ApiKeySelect.js +66 -0
- package/src/app/(dashboard)/dashboard/cli-tools/components/BaseUrlSelect.js +174 -0
- package/src/app/(dashboard)/dashboard/cli-tools/components/ClaudeToolCard.js +390 -0
- package/src/app/(dashboard)/dashboard/cli-tools/components/ClineToolCard.js +301 -0
- package/src/app/(dashboard)/dashboard/cli-tools/components/CodexToolCard.js +458 -0
- package/src/app/(dashboard)/dashboard/cli-tools/components/CopilotToolCard.js +323 -0
- package/src/app/(dashboard)/dashboard/cli-tools/components/CoworkToolCard.js +640 -0
- package/src/app/(dashboard)/dashboard/cli-tools/components/DeepSeekTuiToolCard.js +338 -0
- package/src/app/(dashboard)/dashboard/cli-tools/components/DefaultToolCard.js +271 -0
- package/src/app/(dashboard)/dashboard/cli-tools/components/DroidToolCard.js +410 -0
- package/src/app/(dashboard)/dashboard/cli-tools/components/EndpointPresetControl.js +128 -0
- package/src/app/(dashboard)/dashboard/cli-tools/components/HermesToolCard.js +317 -0
- package/src/app/(dashboard)/dashboard/cli-tools/components/JcodeToolCard.js +380 -0
- package/src/app/(dashboard)/dashboard/cli-tools/components/KiloToolCard.js +275 -0
- package/src/app/(dashboard)/dashboard/cli-tools/components/MitmLinkCard.js +40 -0
- package/src/app/(dashboard)/dashboard/cli-tools/components/MitmServerCard.js +329 -0
- package/src/app/(dashboard)/dashboard/cli-tools/components/MitmToolCard.js +318 -0
- package/src/app/(dashboard)/dashboard/cli-tools/components/OpenClawToolCard.js +388 -0
- package/src/app/(dashboard)/dashboard/cli-tools/components/OpenCodeToolCard.js +500 -0
- package/src/app/(dashboard)/dashboard/cli-tools/components/ToolSummaryCard.js +39 -0
- package/src/app/(dashboard)/dashboard/cli-tools/components/cliEndpointMatch.js +13 -0
- package/src/app/(dashboard)/dashboard/cli-tools/components/index.js +19 -0
- package/src/app/(dashboard)/dashboard/cli-tools/page.js +7 -0
- package/src/app/(dashboard)/dashboard/combos/page.js +612 -0
- package/src/app/(dashboard)/dashboard/console-log/ConsoleLogClient.js +91 -0
- package/src/app/(dashboard)/dashboard/console-log/page.js +8 -0
- package/src/app/(dashboard)/dashboard/endpoint/EndpointPageClient.js +1555 -0
- package/src/app/(dashboard)/dashboard/endpoint/page.js +7 -0
- package/src/app/(dashboard)/dashboard/media-providers/[kind]/[id]/page.js +1903 -0
- package/src/app/(dashboard)/dashboard/media-providers/[kind]/page.js +289 -0
- package/src/app/(dashboard)/dashboard/media-providers/combo/[id]/page.js +410 -0
- package/src/app/(dashboard)/dashboard/media-providers/web/page.js +209 -0
- package/src/app/(dashboard)/dashboard/mitm/MitmPageClient.js +117 -0
- package/src/app/(dashboard)/dashboard/mitm/page.js +5 -0
- package/src/app/(dashboard)/dashboard/page.js +7 -0
- package/src/app/(dashboard)/dashboard/profile/page.js +1140 -0
- package/src/app/(dashboard)/dashboard/providers/[id]/AddApiKeyModal.js +389 -0
- package/src/app/(dashboard)/dashboard/providers/[id]/AddCustomModelModal.js +125 -0
- package/src/app/(dashboard)/dashboard/providers/[id]/CompatibleModelsSection.js +250 -0
- package/src/app/(dashboard)/dashboard/providers/[id]/ConnectionRow.js +299 -0
- package/src/app/(dashboard)/dashboard/providers/[id]/CooldownTimer.js +42 -0
- package/src/app/(dashboard)/dashboard/providers/[id]/EditCompatibleNodeModal.js +161 -0
- package/src/app/(dashboard)/dashboard/providers/[id]/ModelRow.js +95 -0
- package/src/app/(dashboard)/dashboard/providers/[id]/PassthroughModelsSection.js +183 -0
- package/src/app/(dashboard)/dashboard/providers/[id]/page.js +2053 -0
- package/src/app/(dashboard)/dashboard/providers/[id]/page.new.js +1724 -0
- package/src/app/(dashboard)/dashboard/providers/components/ConnectionsCard.js +497 -0
- package/src/app/(dashboard)/dashboard/providers/components/ModelAvailabilityBadge.js +185 -0
- package/src/app/(dashboard)/dashboard/providers/components/ModelsCard.js +294 -0
- package/src/app/(dashboard)/dashboard/providers/new/page.js +220 -0
- package/src/app/(dashboard)/dashboard/providers/page.js +1365 -0
- package/src/app/(dashboard)/dashboard/proxy-pools/page.js +1092 -0
- package/src/app/(dashboard)/dashboard/quota/page.js +11 -0
- package/src/app/(dashboard)/dashboard/skills/page.js +112 -0
- package/src/app/(dashboard)/dashboard/translator/page.js +303 -0
- package/src/app/(dashboard)/dashboard/usage/components/OverviewCards.js +35 -0
- package/src/app/(dashboard)/dashboard/usage/components/ProviderLimits/ProviderLimitCard.js +185 -0
- package/src/app/(dashboard)/dashboard/usage/components/ProviderLimits/QuotaProgressBar.js +127 -0
- package/src/app/(dashboard)/dashboard/usage/components/ProviderLimits/QuotaTable.js +259 -0
- package/src/app/(dashboard)/dashboard/usage/components/ProviderLimits/index.js +1394 -0
- package/src/app/(dashboard)/dashboard/usage/components/ProviderLimits/utils.js +244 -0
- package/src/app/(dashboard)/dashboard/usage/components/ProviderTopology.js +327 -0
- package/src/app/(dashboard)/dashboard/usage/components/RequestDetailsTab.js +433 -0
- package/src/app/(dashboard)/dashboard/usage/components/UsageChart.js +141 -0
- package/src/app/(dashboard)/dashboard/usage/components/UsageTable.js +247 -0
- package/src/app/(dashboard)/dashboard/usage/page.js +75 -0
- package/src/app/(dashboard)/layout.js +6 -0
- package/src/app/api/auth/login/route.js +76 -0
- package/src/app/api/auth/logout/route.js +12 -0
- package/src/app/api/auth/oidc/callback/route.js +87 -0
- package/src/app/api/auth/oidc/start/route.js +52 -0
- package/src/app/api/auth/oidc/test/route.js +84 -0
- package/src/app/api/auth/status/route.js +45 -0
- package/src/app/api/cli-tools/all-statuses/route.js +46 -0
- package/src/app/api/cli-tools/antigravity-mitm/alias/route.js +53 -0
- package/src/app/api/cli-tools/antigravity-mitm/route.js +202 -0
- package/src/app/api/cli-tools/claude-settings/route.js +203 -0
- package/src/app/api/cli-tools/cline-settings/route.js +133 -0
- package/src/app/api/cli-tools/codex-gateway/accounts/route.js +16 -0
- package/src/app/api/cli-tools/codex-settings/route.js +239 -0
- package/src/app/api/cli-tools/copilot-settings/route.js +148 -0
- package/src/app/api/cli-tools/cowork-mcp-registry/route.js +77 -0
- package/src/app/api/cli-tools/cowork-mcp-tools/route.js +95 -0
- package/src/app/api/cli-tools/cowork-settings/route.js +412 -0
- package/src/app/api/cli-tools/deepseek-tui-settings/route.js +164 -0
- package/src/app/api/cli-tools/droid-settings/route.js +213 -0
- package/src/app/api/cli-tools/hermes-settings/route.js +175 -0
- package/src/app/api/cli-tools/jcode-settings/route.js +216 -0
- package/src/app/api/cli-tools/kilo-settings/route.js +131 -0
- package/src/app/api/cli-tools/openclaw-settings/route.js +292 -0
- package/src/app/api/cli-tools/opencode-settings/route.js +259 -0
- package/src/app/api/combos/[id]/route.js +81 -0
- package/src/app/api/combos/route.js +48 -0
- package/src/app/api/health/route.js +15 -0
- package/src/app/api/init/route.js +4 -0
- package/src/app/api/keys/[id]/route.js +58 -0
- package/src/app/api/keys/route.js +42 -0
- package/src/app/api/locale/route.js +30 -0
- package/src/app/api/mcp/[plugin]/message/route.js +21 -0
- package/src/app/api/mcp/[plugin]/sse/route.js +37 -0
- package/src/app/api/media-providers/tts/deepgram/voices/route.js +65 -0
- package/src/app/api/media-providers/tts/elevenlabs/voices/route.js +71 -0
- package/src/app/api/media-providers/tts/inworld/voices/route.js +61 -0
- package/src/app/api/media-providers/tts/minimax/voices/route.js +113 -0
- package/src/app/api/media-providers/tts/voices/route.js +99 -0
- package/src/app/api/models/alias/route.js +53 -0
- package/src/app/api/models/availability/route.js +103 -0
- package/src/app/api/models/custom/route.js +48 -0
- package/src/app/api/models/disabled/route.js +50 -0
- package/src/app/api/models/route.js +64 -0
- package/src/app/api/models/test/ping.js +191 -0
- package/src/app/api/models/test/route.js +14 -0
- package/src/app/api/oauth/[provider]/[action]/route.js +343 -0
- package/src/app/api/oauth/codebuddy/bulk-import/[jobId]/cancel/route.js +19 -0
- package/src/app/api/oauth/codebuddy/bulk-import/[jobId]/manual/[workerId]/route.js +30 -0
- package/src/app/api/oauth/codebuddy/bulk-import/[jobId]/route.js +23 -0
- package/src/app/api/oauth/codebuddy/bulk-import/latest/route.js +25 -0
- package/src/app/api/oauth/codebuddy/bulk-import/route.js +49 -0
- package/src/app/api/oauth/codebuddy/quota-cookie/route.js +133 -0
- package/src/app/api/oauth/codex/import-token/route.js +96 -0
- package/src/app/api/oauth/cursor/auto-import/route.js +258 -0
- package/src/app/api/oauth/cursor/import/route.js +100 -0
- package/src/app/api/oauth/gitlab/pat/route.js +62 -0
- package/src/app/api/oauth/iflow/cookie/route.js +137 -0
- package/src/app/api/oauth/kiro/auto-import/route.js +85 -0
- package/src/app/api/oauth/kiro/bulk-import/[jobId]/cancel/route.js +18 -0
- package/src/app/api/oauth/kiro/bulk-import/[jobId]/manual/[workerId]/route.js +29 -0
- package/src/app/api/oauth/kiro/bulk-import/[jobId]/route.js +22 -0
- package/src/app/api/oauth/kiro/bulk-import/latest/route.js +25 -0
- package/src/app/api/oauth/kiro/bulk-import/route.js +49 -0
- package/src/app/api/oauth/kiro/import/route.js +110 -0
- package/src/app/api/oauth/kiro/social-authorize/route.js +27 -0
- package/src/app/api/oauth/kiro/social-exchange/route.js +41 -0
- package/src/app/api/pricing/route.js +134 -0
- package/src/app/api/provider-nodes/[id]/route.js +101 -0
- package/src/app/api/provider-nodes/route.js +104 -0
- package/src/app/api/provider-nodes/validate/route.js +201 -0
- package/src/app/api/providers/[id]/models/route.js +526 -0
- package/src/app/api/providers/[id]/route.js +189 -0
- package/src/app/api/providers/[id]/test/route.js +23 -0
- package/src/app/api/providers/[id]/test/testUtils.js +714 -0
- package/src/app/api/providers/[id]/test-models/route.js +66 -0
- package/src/app/api/providers/client/route.js +126 -0
- package/src/app/api/providers/kilo/free-models/route.js +55 -0
- package/src/app/api/providers/route.js +206 -0
- package/src/app/api/providers/suggested-models/filters.js +20 -0
- package/src/app/api/providers/suggested-models/route.js +32 -0
- package/src/app/api/providers/test-batch/route.js +131 -0
- package/src/app/api/providers/validate/route.js +637 -0
- package/src/app/api/proxy-pools/[id]/route.js +123 -0
- package/src/app/api/proxy-pools/[id]/test/route.js +70 -0
- package/src/app/api/proxy-pools/cloudflare-deploy/route.js +145 -0
- package/src/app/api/proxy-pools/deno-deploy/route.js +175 -0
- package/src/app/api/proxy-pools/route.js +93 -0
- package/src/app/api/proxy-pools/vercel-deploy/route.js +142 -0
- package/src/app/api/settings/database/route.js +36 -0
- package/src/app/api/settings/proxy-test/route.js +23 -0
- package/src/app/api/settings/require-login/route.js +15 -0
- package/src/app/api/settings/route.js +100 -0
- package/src/app/api/shutdown/route.js +24 -0
- package/src/app/api/tags/route.js +18 -0
- package/src/app/api/translator/console-logs/route.js +24 -0
- package/src/app/api/translator/console-logs/stream/route.js +79 -0
- package/src/app/api/translator/load/route.js +45 -0
- package/src/app/api/translator/save/route.js +44 -0
- package/src/app/api/translator/send/route.js +94 -0
- package/src/app/api/translator/translate/route.js +90 -0
- package/src/app/api/tunnel/disable/route.js +12 -0
- package/src/app/api/tunnel/enable/route.js +16 -0
- package/src/app/api/tunnel/status/route.js +13 -0
- package/src/app/api/tunnel/tailscale-check/route.js +50 -0
- package/src/app/api/tunnel/tailscale-disable/route.js +12 -0
- package/src/app/api/tunnel/tailscale-enable/route.js +12 -0
- package/src/app/api/tunnel/tailscale-install/route.js +72 -0
- package/src/app/api/usage/[connectionId]/route.js +188 -0
- package/src/app/api/usage/chart/route.js +21 -0
- package/src/app/api/usage/history/route.js +12 -0
- package/src/app/api/usage/logs/route.js +12 -0
- package/src/app/api/usage/providers/route.js +42 -0
- package/src/app/api/usage/request-details/route.js +57 -0
- package/src/app/api/usage/request-logs/route.js +13 -0
- package/src/app/api/usage/stats/route.js +23 -0
- package/src/app/api/usage/stream/route.js +79 -0
- package/src/app/api/v1/api/chat/route.js +37 -0
- package/src/app/api/v1/audio/speech/route.js +16 -0
- package/src/app/api/v1/audio/transcriptions/route.js +19 -0
- package/src/app/api/v1/audio/voices/route.js +68 -0
- package/src/app/api/v1/chat/completions/route.js +35 -0
- package/src/app/api/v1/embeddings/route.js +21 -0
- package/src/app/api/v1/images/generations/route.js +16 -0
- package/src/app/api/v1/messages/count_tokens/route.js +52 -0
- package/src/app/api/v1/messages/route.js +36 -0
- package/src/app/api/v1/models/[kind]/route.js +55 -0
- package/src/app/api/v1/models/info/route.js +110 -0
- package/src/app/api/v1/models/route.js +451 -0
- package/src/app/api/v1/responses/compact/route.js +37 -0
- package/src/app/api/v1/responses/route.js +30 -0
- package/src/app/api/v1/route.js +1 -0
- package/src/app/api/v1/search/route.js +21 -0
- package/src/app/api/v1/web/fetch/route.js +21 -0
- package/src/app/api/v1beta/models/[...path]/route.js +328 -0
- package/src/app/api/v1beta/models/route.js +44 -0
- package/src/app/api/version/route.js +45 -0
- package/src/app/api/version/shutdown/route.js +15 -0
- package/src/app/api/version/update/route.js +21 -0
- package/src/app/callback/page.js +148 -0
- package/src/app/dashboard/settings/pricing/page.js +173 -0
- package/src/app/favicon.ico +0 -0
- package/src/app/globals.css +496 -0
- package/src/app/landing/components/AnimatedBackground.js +57 -0
- package/src/app/landing/components/Features.js +133 -0
- package/src/app/landing/components/FlowAnimation.js +175 -0
- package/src/app/landing/components/Footer.js +61 -0
- package/src/app/landing/components/GetStarted.js +97 -0
- package/src/app/landing/components/HeroSection.js +47 -0
- package/src/app/landing/components/HowItWorks.js +66 -0
- package/src/app/landing/components/Navigation.js +72 -0
- package/src/app/landing/page.js +106 -0
- package/src/app/layout.js +49 -0
- package/src/app/login/page.js +197 -0
- package/src/app/manifest.js +30 -0
- package/src/app/page.js +5 -0
- package/src/dashboardGuard.js +242 -0
- package/src/i18n/RuntimeI18nProvider.js +27 -0
- package/src/i18n/config.js +146 -0
- package/src/i18n/runtime.js +162 -0
- package/src/lib/appUpdater.js +200 -0
- package/src/lib/auth/dashboardSession.js +68 -0
- package/src/lib/auth/loginLimiter.js +52 -0
- package/src/lib/auth/oidc.js +234 -0
- package/src/lib/consoleLogBuffer.js +79 -0
- package/src/lib/dataDir.js +29 -0
- package/src/lib/db/adapters/betterSqliteAdapter.js +55 -0
- package/src/lib/db/adapters/bunSqliteAdapter.js +63 -0
- package/src/lib/db/adapters/nodeSqliteAdapter.js +84 -0
- package/src/lib/db/adapters/sqljsAdapter.js +115 -0
- package/src/lib/db/backup.js +35 -0
- package/src/lib/db/driver.js +85 -0
- package/src/lib/db/helpers/jsonCol.js +9 -0
- package/src/lib/db/helpers/kvStore.js +39 -0
- package/src/lib/db/helpers/metaStore.js +22 -0
- package/src/lib/db/index.js +171 -0
- package/src/lib/db/migrate.js +286 -0
- package/src/lib/db/migrations/001-initial.js +14 -0
- package/src/lib/db/migrations/index.js +10 -0
- package/src/lib/db/paths.js +18 -0
- package/src/lib/db/repos/aliasRepo.js +62 -0
- package/src/lib/db/repos/apiKeysRepo.js +75 -0
- package/src/lib/db/repos/combosRepo.js +73 -0
- package/src/lib/db/repos/connectionsRepo.js +226 -0
- package/src/lib/db/repos/disabledModelsRepo.js +56 -0
- package/src/lib/db/repos/nodesRepo.js +95 -0
- package/src/lib/db/repos/pricingRepo.js +108 -0
- package/src/lib/db/repos/proxyPoolsRepo.js +103 -0
- package/src/lib/db/repos/requestDetailsRepo.js +259 -0
- package/src/lib/db/repos/settingsRepo.js +104 -0
- package/src/lib/db/repos/usageRepo.js +731 -0
- package/src/lib/db/schema.js +157 -0
- package/src/lib/db/version.js +21 -0
- package/src/lib/disabledModelsDb.js +4 -0
- package/src/lib/localDb.js +21 -0
- package/src/lib/mcp/stdioSseBridge.js +198 -0
- package/src/lib/mitmAliasCache.js +46 -0
- package/src/lib/network/connectionProxy.js +160 -0
- package/src/lib/network/initOutboundProxy.js +25 -0
- package/src/lib/network/outboundProxy.js +68 -0
- package/src/lib/network/proxyTest.js +91 -0
- package/src/lib/oauth/constants/oauth.js +284 -0
- package/src/lib/oauth/constants/xai.js +61 -0
- package/src/lib/oauth/providers.js +1506 -0
- package/src/lib/oauth/services/antigravity.js +321 -0
- package/src/lib/oauth/services/claude.js +136 -0
- package/src/lib/oauth/services/codebuddyBulkImportManager.js +454 -0
- package/src/lib/oauth/services/codex.js +144 -0
- package/src/lib/oauth/services/cursor.js +179 -0
- package/src/lib/oauth/services/gemini.js +240 -0
- package/src/lib/oauth/services/github.js +225 -0
- package/src/lib/oauth/services/iflow.js +202 -0
- package/src/lib/oauth/services/index.js +17 -0
- package/src/lib/oauth/services/kiro.js +334 -0
- package/src/lib/oauth/services/kiroBulkImportManager.js +778 -0
- package/src/lib/oauth/services/kiroConnections.js +93 -0
- package/src/lib/oauth/services/kiroGoogleAutomation.js +1136 -0
- package/src/lib/oauth/services/oauth.js +157 -0
- package/src/lib/oauth/services/openai.js +123 -0
- package/src/lib/oauth/services/qoder.js +216 -0
- package/src/lib/oauth/services/qwen.js +170 -0
- package/src/lib/oauth/services/xai.js +238 -0
- package/src/lib/oauth/utils/banner.js +63 -0
- package/src/lib/oauth/utils/pkce.js +39 -0
- package/src/lib/oauth/utils/server.js +415 -0
- package/src/lib/oauth/utils/ui.js +48 -0
- package/src/lib/providerNormalization.js +45 -0
- package/src/lib/qoder/constants.js +64 -0
- package/src/lib/qoder/cosy.js +175 -0
- package/src/lib/qoder/encoding.js +55 -0
- package/src/lib/requestDetailsDb.js +4 -0
- package/src/lib/tunnel/cloudflare/cloudflared.js +449 -0
- package/src/lib/tunnel/cloudflare/config.js +9 -0
- package/src/lib/tunnel/cloudflare/healthCheck.js +29 -0
- package/src/lib/tunnel/cloudflare/manager.js +151 -0
- package/src/lib/tunnel/cloudflare/pid.js +23 -0
- package/src/lib/tunnel/index.js +48 -0
- package/src/lib/tunnel/shared/dnsResolver.js +17 -0
- package/src/lib/tunnel/shared/internetCheck.js +26 -0
- package/src/lib/tunnel/shared/state.js +41 -0
- package/src/lib/tunnel/shared/watchdogConfig.js +8 -0
- package/src/lib/tunnel/tailscale/config.js +7 -0
- package/src/lib/tunnel/tailscale/healthCheck.js +29 -0
- package/src/lib/tunnel/tailscale/manager.js +129 -0
- package/src/lib/tunnel/tailscale/tailscale.js +790 -0
- package/src/lib/updater/updater.js +235 -0
- package/src/lib/usage/fetcher.js +208 -0
- package/src/lib/usageDb.js +7 -0
- package/src/mitm/antigravityIdeVersion.js +50 -0
- package/src/mitm/cert/generate.js +32 -0
- package/src/mitm/cert/install.js +269 -0
- package/src/mitm/cert/rootCA.js +173 -0
- package/src/mitm/config.js +87 -0
- package/src/mitm/dbReader.js +22 -0
- package/src/mitm/dns/dnsConfig.js +266 -0
- package/src/mitm/handlers/antigravity.js +33 -0
- package/src/mitm/handlers/base.js +226 -0
- package/src/mitm/handlers/copilot.js +35 -0
- package/src/mitm/handlers/cursor.js +15 -0
- package/src/mitm/handlers/kiro.js +526 -0
- package/src/mitm/logger.js +106 -0
- package/src/mitm/manager.js +851 -0
- package/src/mitm/paths.js +32 -0
- package/src/mitm/server.js +435 -0
- package/src/mitm/winElevated.js +81 -0
- package/src/models/index.js +38 -0
- package/src/proxy.js +5 -0
- package/src/shared/components/AddCustomEmbeddingModal.js +183 -0
- package/src/shared/components/Avatar.js +88 -0
- package/src/shared/components/Badge.js +54 -0
- package/src/shared/components/BulkAccountAutomationModal.js +508 -0
- package/src/shared/components/Button.js +56 -0
- package/src/shared/components/Card.js +116 -0
- package/src/shared/components/ChangelogModal.js +97 -0
- package/src/shared/components/CodeBuddyQuotaCookieModal.js +109 -0
- package/src/shared/components/ComboFormModal.js +176 -0
- package/src/shared/components/CursorAuthModal.js +212 -0
- package/src/shared/components/DonateModal.js +136 -0
- package/src/shared/components/Drawer.js +82 -0
- package/src/shared/components/EditConnectionModal.js +286 -0
- package/src/shared/components/Footer.js +132 -0
- package/src/shared/components/GitLabAuthModal.js +194 -0
- package/src/shared/components/Header.js +380 -0
- package/src/shared/components/HeaderLanguage.js +46 -0
- package/src/shared/components/HeaderMenu.js +126 -0
- package/src/shared/components/IFlowCookieModal.js +132 -0
- package/src/shared/components/Input.js +65 -0
- package/src/shared/components/KiroAuthModal.js +1171 -0
- package/src/shared/components/KiroOAuthWrapper.js +149 -0
- package/src/shared/components/KiroSocialOAuthModal.js +205 -0
- package/src/shared/components/LanguageSwitcher.js +190 -0
- package/src/shared/components/Loading.js +75 -0
- package/src/shared/components/ManualConfigModal.js +44 -0
- package/src/shared/components/McpMarketplaceModal.js +255 -0
- package/src/shared/components/Modal.js +146 -0
- package/src/shared/components/ModelSelectModal.js +537 -0
- package/src/shared/components/NineRemoteButton.js +23 -0
- package/src/shared/components/NineRemotePromoModal.js +99 -0
- package/src/shared/components/NoAuthProxyCard.js +86 -0
- package/src/shared/components/OAuthModal.js +682 -0
- package/src/shared/components/Pagination.js +150 -0
- package/src/shared/components/PricingModal.js +208 -0
- package/src/shared/components/ProviderIcon.js +63 -0
- package/src/shared/components/ProviderInfoCard.js +82 -0
- package/src/shared/components/RequestLogger.js +121 -0
- package/src/shared/components/SegmentedControl.js +48 -0
- package/src/shared/components/Select.js +67 -0
- package/src/shared/components/Sidebar.js +441 -0
- package/src/shared/components/ThemeProvider.js +15 -0
- package/src/shared/components/ThemeToggle.js +42 -0
- package/src/shared/components/Toggle.js +69 -0
- package/src/shared/components/Tooltip.js +25 -0
- package/src/shared/components/UsageStats.js +505 -0
- package/src/shared/components/index.js +46 -0
- package/src/shared/components/layouts/AuthLayout.js +29 -0
- package/src/shared/components/layouts/DashboardLayout.js +104 -0
- package/src/shared/components/layouts/index.js +4 -0
- package/src/shared/constants/cliTools.js +397 -0
- package/src/shared/constants/colors.js +77 -0
- package/src/shared/constants/config.js +99 -0
- package/src/shared/constants/coworkPlugins.js +75 -0
- package/src/shared/constants/index.js +4 -0
- package/src/shared/constants/locales.js +36 -0
- package/src/shared/constants/mitmToolHosts.js +12 -0
- package/src/shared/constants/models.js +38 -0
- package/src/shared/constants/pricing.js +303 -0
- package/src/shared/constants/providers.js +289 -0
- package/src/shared/constants/skills.js +78 -0
- package/src/shared/constants/ttsProviders.js +138 -0
- package/src/shared/hooks/index.js +2 -0
- package/src/shared/hooks/useCopyToClipboard.js +43 -0
- package/src/shared/hooks/useTheme.js +60 -0
- package/src/shared/services/bootstrap.js +12 -0
- package/src/shared/services/initializeApp.js +268 -0
- package/src/shared/utils/api.js +93 -0
- package/src/shared/utils/apiKey.js +98 -0
- package/src/shared/utils/clineAuth.js +37 -0
- package/src/shared/utils/cn.js +11 -0
- package/src/shared/utils/connectionStatus.js +162 -0
- package/src/shared/utils/index.js +40 -0
- package/src/shared/utils/machine.js +6 -0
- package/src/shared/utils/machineId.js +66 -0
- package/src/shared/utils/providerModelsFetcher.js +30 -0
- package/src/sse/handlers/chat.js +261 -0
- package/src/sse/handlers/embeddings.js +141 -0
- package/src/sse/handlers/fetch.js +213 -0
- package/src/sse/handlers/imageGeneration.js +142 -0
- package/src/sse/handlers/search.js +206 -0
- package/src/sse/handlers/stt.js +88 -0
- package/src/sse/handlers/tts.js +114 -0
- package/src/sse/services/auth.js +346 -0
- package/src/sse/services/codexGateway.js +215 -0
- package/src/sse/services/model.js +99 -0
- package/src/sse/services/tokenRefresh.js +319 -0
- package/src/sse/utils/logger.js +75 -0
- package/src/store/headerSearchStore.js +19 -0
- package/src/store/index.js +6 -0
- package/src/store/notificationStore.js +45 -0
- package/src/store/providerStore.js +55 -0
- package/src/store/settingsStore.js +51 -0
- package/src/store/themeStore.js +54 -0
- package/src/store/userStore.js +20 -0
- package/start.sh +4 -0
|
@@ -0,0 +1,1496 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Usage Fetcher - Get usage data from provider APIs
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import { CLIENT_METADATA, getPlatformUserAgent } from "../config/appConstants.js";
|
|
6
|
+
import { proxyAwareFetch } from "../utils/proxyFetch.js";
|
|
7
|
+
|
|
8
|
+
// GitHub API config
|
|
9
|
+
const GITHUB_CONFIG = {
|
|
10
|
+
apiVersion: "2022-11-28",
|
|
11
|
+
userAgent: "GitHubCopilotChat/0.26.7",
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
// GLM quota endpoints (region-aware)
|
|
15
|
+
const GLM_QUOTA_URLS = {
|
|
16
|
+
international: "https://api.z.ai/api/monitor/usage/quota/limit",
|
|
17
|
+
china: "https://open.bigmodel.cn/api/monitor/usage/quota/limit",
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
// MiniMax usage endpoints (try in order, fallback on transient errors)
|
|
21
|
+
const MINIMAX_USAGE_URLS = {
|
|
22
|
+
minimax: [
|
|
23
|
+
"https://www.minimax.io/v1/token_plan/remains",
|
|
24
|
+
"https://api.minimax.io/v1/api/openplatform/coding_plan/remains",
|
|
25
|
+
],
|
|
26
|
+
"minimax-cn": [
|
|
27
|
+
"https://www.minimaxi.com/v1/api/openplatform/coding_plan/remains",
|
|
28
|
+
"https://api.minimaxi.com/v1/api/openplatform/coding_plan/remains",
|
|
29
|
+
],
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
// Antigravity API config (from Quotio)
|
|
33
|
+
const ANTIGRAVITY_CONFIG = {
|
|
34
|
+
quotaApiUrl: "https://cloudcode-pa.googleapis.com/v1internal:fetchAvailableModels",
|
|
35
|
+
loadProjectApiUrl: "https://cloudcode-pa.googleapis.com/v1internal:loadCodeAssist",
|
|
36
|
+
tokenUrl: "https://oauth2.googleapis.com/token",
|
|
37
|
+
clientId: "1071006060591-tmhssin2h21lcre235vtolojh4g403ep.apps.googleusercontent.com",
|
|
38
|
+
clientSecret: "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",
|
|
39
|
+
userAgent: getPlatformUserAgent(),
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
// Codex (OpenAI) API config
|
|
43
|
+
const CODEX_CONFIG = {
|
|
44
|
+
usageUrl: "https://chatgpt.com/backend-api/wham/usage",
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
// Claude API config
|
|
48
|
+
const CLAUDE_CONFIG = {
|
|
49
|
+
oauthUsageUrl: "https://api.anthropic.com/api/oauth/usage",
|
|
50
|
+
usageUrl: "https://api.anthropic.com/v1/organizations/{org_id}/usage",
|
|
51
|
+
settingsUrl: "https://api.anthropic.com/v1/settings",
|
|
52
|
+
apiVersion: "2023-06-01",
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
const CODEBUDDY_CONFIG = {
|
|
56
|
+
usageUrl: "https://www.codebuddy.ai/billing/meter/get-user-resource",
|
|
57
|
+
productCode: "p_tcaca",
|
|
58
|
+
packageCodes: {
|
|
59
|
+
free: "TCACA_code_001_PqouKr6QWV",
|
|
60
|
+
proMon: "TCACA_code_002_AkiJS3ZHF5",
|
|
61
|
+
gift: "TCACA_code_006_DbXS0lrypC",
|
|
62
|
+
activity: "TCACA_code_007_nzdH5h4Nl0",
|
|
63
|
+
proYear: "TCACA_code_003_FAnt7lcmRT",
|
|
64
|
+
freeMon: "TCACA_code_008_cfWoLwvjU4",
|
|
65
|
+
extra: "TCACA_code_009_0XmEQc2xOf",
|
|
66
|
+
},
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Get usage data for a provider connection
|
|
71
|
+
* @param {Object} connection - Provider connection with accessToken
|
|
72
|
+
* @returns {Object} Usage data with quotas
|
|
73
|
+
*/
|
|
74
|
+
export async function getUsageForProvider(connection, proxyOptions = null) {
|
|
75
|
+
const { provider, accessToken, apiKey, providerSpecificData, projectId } = connection;
|
|
76
|
+
const providerDataWithProjectId = {
|
|
77
|
+
...(providerSpecificData || {}),
|
|
78
|
+
...(projectId ? { projectId } : {}),
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
switch (provider) {
|
|
82
|
+
case "github":
|
|
83
|
+
return await getGitHubUsage(accessToken, providerSpecificData, proxyOptions);
|
|
84
|
+
case "gemini-cli":
|
|
85
|
+
return await getGeminiUsage(accessToken, providerDataWithProjectId, proxyOptions);
|
|
86
|
+
case "antigravity":
|
|
87
|
+
return await getAntigravityUsage(accessToken, providerSpecificData, proxyOptions);
|
|
88
|
+
case "claude":
|
|
89
|
+
return await getClaudeUsage(accessToken, proxyOptions);
|
|
90
|
+
case "codex":
|
|
91
|
+
return await getCodexUsage(accessToken, proxyOptions);
|
|
92
|
+
case "kiro":
|
|
93
|
+
return await getKiroUsage(accessToken, providerSpecificData, proxyOptions);
|
|
94
|
+
case "codebuddy":
|
|
95
|
+
return await getCodeBuddyUsage(accessToken, providerSpecificData, proxyOptions);
|
|
96
|
+
case "qoder":
|
|
97
|
+
return await getQoderUsage(accessToken, proxyOptions);
|
|
98
|
+
case "qwen":
|
|
99
|
+
return await getQwenUsage(accessToken, providerSpecificData);
|
|
100
|
+
case "iflow":
|
|
101
|
+
return await getIflowUsage(accessToken);
|
|
102
|
+
case "ollama":
|
|
103
|
+
return await getOllamaUsage(accessToken);
|
|
104
|
+
case "glm":
|
|
105
|
+
case "glm-cn":
|
|
106
|
+
return await getGlmUsage(apiKey, provider, proxyOptions);
|
|
107
|
+
case "minimax":
|
|
108
|
+
case "minimax-cn":
|
|
109
|
+
return await getMiniMaxUsage(apiKey, provider, proxyOptions);
|
|
110
|
+
default:
|
|
111
|
+
return { message: `Usage API not implemented for ${provider}` };
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
async function getCodeBuddyUsage(accessToken, providerSpecificData = {}, proxyOptions = null) {
|
|
116
|
+
if (!accessToken) {
|
|
117
|
+
return { plan: "CodeBuddy", message: "CodeBuddy access token not available.", quotas: {} };
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
try {
|
|
121
|
+
const response = await proxyAwareFetch(CODEBUDDY_CONFIG.usageUrl, {
|
|
122
|
+
method: "POST",
|
|
123
|
+
headers: buildCodeBuddyUsageHeaders(accessToken, providerSpecificData),
|
|
124
|
+
body: JSON.stringify(buildCodeBuddyUsageBody()),
|
|
125
|
+
}, proxyOptions);
|
|
126
|
+
|
|
127
|
+
const rawText = await response.text();
|
|
128
|
+
let payload = null;
|
|
129
|
+
try {
|
|
130
|
+
payload = rawText ? JSON.parse(rawText) : null;
|
|
131
|
+
} catch {
|
|
132
|
+
payload = null;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
if (response.status === 401 || response.status === 403) {
|
|
136
|
+
return {
|
|
137
|
+
plan: "CodeBuddy",
|
|
138
|
+
message: "CodeBuddy quota endpoint requires a web console session; the current CLI/plugin OAuth token can fetch account data but not credit usage.",
|
|
139
|
+
quotas: {},
|
|
140
|
+
};
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
if (!response.ok) {
|
|
144
|
+
return {
|
|
145
|
+
plan: "CodeBuddy",
|
|
146
|
+
message: `CodeBuddy quota endpoint returned ${response.status}.`,
|
|
147
|
+
quotas: {},
|
|
148
|
+
};
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
return parseCodeBuddyUsage(payload);
|
|
152
|
+
} catch (error) {
|
|
153
|
+
return { plan: "CodeBuddy", message: `CodeBuddy connected. Unable to fetch quota: ${error.message}`, quotas: {} };
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/**
|
|
158
|
+
* Parse reset date/time to ISO string
|
|
159
|
+
* Handles multiple formats: Unix timestamp (ms), ISO date string, etc.
|
|
160
|
+
*/
|
|
161
|
+
function parseResetTime(resetValue) {
|
|
162
|
+
if (!resetValue) return null;
|
|
163
|
+
|
|
164
|
+
try {
|
|
165
|
+
// If it's already a Date object
|
|
166
|
+
if (resetValue instanceof Date) {
|
|
167
|
+
return resetValue.toISOString();
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
// Unix timestamps from provider APIs may be seconds or milliseconds.
|
|
171
|
+
if (typeof resetValue === 'number') {
|
|
172
|
+
return new Date(resetValue < 1e12 ? resetValue * 1000 : resetValue).toISOString();
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
// If it's a numeric string, treat it like a Unix timestamp too.
|
|
176
|
+
if (typeof resetValue === 'string') {
|
|
177
|
+
if (/^\d+$/.test(resetValue)) {
|
|
178
|
+
const timestamp = Number(resetValue);
|
|
179
|
+
return new Date(timestamp < 1e12 ? timestamp * 1000 : timestamp).toISOString();
|
|
180
|
+
}
|
|
181
|
+
return new Date(resetValue).toISOString();
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
return null;
|
|
185
|
+
} catch (error) {
|
|
186
|
+
console.warn(`Failed to parse reset time: ${resetValue}`, error);
|
|
187
|
+
return null;
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
function formatCodeBuddyDate(date) {
|
|
192
|
+
const pad = (value) => String(value).padStart(2, "0");
|
|
193
|
+
return `${date.getFullYear()}-${pad(date.getMonth() + 1)}-${pad(date.getDate())} ${pad(date.getHours())}:${pad(date.getMinutes())}:${pad(date.getSeconds())}`;
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
function buildCodeBuddyUsageBody() {
|
|
197
|
+
const now = new Date();
|
|
198
|
+
const rangeEnd = new Date(now);
|
|
199
|
+
rangeEnd.setFullYear(rangeEnd.getFullYear() + 101);
|
|
200
|
+
|
|
201
|
+
return {
|
|
202
|
+
PageNumber: 1,
|
|
203
|
+
PageSize: 200,
|
|
204
|
+
ProductCode: CODEBUDDY_CONFIG.productCode,
|
|
205
|
+
Status: [0, 3],
|
|
206
|
+
PackageCodes: Object.values(CODEBUDDY_CONFIG.packageCodes),
|
|
207
|
+
PackageEndTimeRangeBegin: formatCodeBuddyDate(now),
|
|
208
|
+
PackageEndTimeRangeEnd: formatCodeBuddyDate(rangeEnd),
|
|
209
|
+
};
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
function buildCodeBuddyUsageHeaders(accessToken, providerSpecificData = {}) {
|
|
213
|
+
const domain = providerSpecificData?.domain || providerSpecificData?.rawAuth?.domain || "www.codebuddy.ai";
|
|
214
|
+
const webCookie = providerSpecificData?.webCookie || providerSpecificData?.cookie || providerSpecificData?.codeBuddyCookie;
|
|
215
|
+
|
|
216
|
+
return {
|
|
217
|
+
...(webCookie ? { Cookie: webCookie } : { Authorization: `Bearer ${accessToken}` }),
|
|
218
|
+
Accept: "application/json, text/plain, */*",
|
|
219
|
+
"Content-Type": "application/json",
|
|
220
|
+
"User-Agent": "Mozilla/5.0 CodeBuddy quota probe",
|
|
221
|
+
"X-Requested-With": "XMLHttpRequest",
|
|
222
|
+
"X-Domain": domain,
|
|
223
|
+
Origin: `https://${domain}`,
|
|
224
|
+
Referer: `https://${domain}/profile/usage`,
|
|
225
|
+
};
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
function parseCodeBuddyUsage(payload) {
|
|
229
|
+
const data = payload?.Response?.Data || payload?.data || payload || {};
|
|
230
|
+
const accounts = Array.isArray(data?.Accounts)
|
|
231
|
+
? data.Accounts
|
|
232
|
+
: Array.isArray(data?.accounts)
|
|
233
|
+
? data.accounts
|
|
234
|
+
: [];
|
|
235
|
+
|
|
236
|
+
if (accounts.length === 0) {
|
|
237
|
+
return {
|
|
238
|
+
plan: "CodeBuddy",
|
|
239
|
+
message: "CodeBuddy connected. No quota records were returned.",
|
|
240
|
+
quotas: {},
|
|
241
|
+
};
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
const quotas = {};
|
|
245
|
+
let hasProPackage = false;
|
|
246
|
+
|
|
247
|
+
for (const account of accounts) {
|
|
248
|
+
if (!account || typeof account !== "object") continue;
|
|
249
|
+
const label = getCodeBuddyQuotaLabel(account.PackageCode);
|
|
250
|
+
if (!label) continue;
|
|
251
|
+
|
|
252
|
+
if (account.PackageCode === CODEBUDDY_CONFIG.packageCodes.proMon || account.PackageCode === CODEBUDDY_CONFIG.packageCodes.proYear) {
|
|
253
|
+
hasProPackage = true;
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
const quota = getCodeBuddyQuotaValues(account);
|
|
257
|
+
if (!quota) continue;
|
|
258
|
+
|
|
259
|
+
if (!quotas[label]) {
|
|
260
|
+
quotas[label] = {
|
|
261
|
+
used: 0,
|
|
262
|
+
total: 0,
|
|
263
|
+
remaining: 0,
|
|
264
|
+
resetAt: null,
|
|
265
|
+
unit: "credits",
|
|
266
|
+
unlimited: false,
|
|
267
|
+
};
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
quotas[label].used += quota.used;
|
|
271
|
+
quotas[label].total += quota.total;
|
|
272
|
+
quotas[label].remaining += quota.remaining;
|
|
273
|
+
quotas[label].resetAt = getEarlierReset(quotas[label].resetAt, quota.resetAt);
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
if (Object.keys(quotas).length === 0) {
|
|
277
|
+
return {
|
|
278
|
+
plan: hasProPackage ? "Pro" : "Free",
|
|
279
|
+
message: "CodeBuddy connected. Unable to extract quota values.",
|
|
280
|
+
quotas: {},
|
|
281
|
+
};
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
for (const quota of Object.values(quotas)) {
|
|
285
|
+
quota.remainingPercentage = quota.total > 0
|
|
286
|
+
? Math.max(0, Math.min(100, (quota.remaining / quota.total) * 100))
|
|
287
|
+
: 0;
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
return {
|
|
291
|
+
plan: hasProPackage ? "Pro" : "Free",
|
|
292
|
+
quotas,
|
|
293
|
+
};
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
function getCodeBuddyQuotaLabel(packageCode) {
|
|
297
|
+
const codes = CODEBUDDY_CONFIG.packageCodes;
|
|
298
|
+
switch (packageCode) {
|
|
299
|
+
case codes.free:
|
|
300
|
+
case codes.freeMon:
|
|
301
|
+
case codes.proMon:
|
|
302
|
+
case codes.proYear:
|
|
303
|
+
return "Monthly Credits";
|
|
304
|
+
case codes.gift:
|
|
305
|
+
return "Gift Credits";
|
|
306
|
+
case codes.extra:
|
|
307
|
+
return "Extra Credits";
|
|
308
|
+
case codes.activity:
|
|
309
|
+
return "Activity Credits";
|
|
310
|
+
default:
|
|
311
|
+
return packageCode ? "Other Credits" : null;
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
function getCodeBuddyQuotaValues(account) {
|
|
316
|
+
const total = firstFiniteNumber(
|
|
317
|
+
account.CycleCapacitySizePrecise,
|
|
318
|
+
account.CycleCapacitySize,
|
|
319
|
+
account.CapacitySizePrecise,
|
|
320
|
+
account.CapacitySize,
|
|
321
|
+
);
|
|
322
|
+
const remaining = firstFiniteNumber(
|
|
323
|
+
account.CycleCapacityRemainPrecise,
|
|
324
|
+
account.CapacityRemainPrecise,
|
|
325
|
+
account.CapacityRemain,
|
|
326
|
+
);
|
|
327
|
+
const used = firstFiniteNumber(
|
|
328
|
+
account.CapacityUsedPrecise,
|
|
329
|
+
account.CapacityUsed,
|
|
330
|
+
total !== null && remaining !== null ? Math.max(0, total - remaining) : null,
|
|
331
|
+
);
|
|
332
|
+
|
|
333
|
+
if (total === null && remaining === null && used === null) return null;
|
|
334
|
+
|
|
335
|
+
const safeTotal = Math.max(0, total ?? ((used ?? 0) + (remaining ?? 0)));
|
|
336
|
+
const safeRemaining = Math.max(0, remaining ?? Math.max(0, safeTotal - (used ?? 0)));
|
|
337
|
+
const safeUsed = Math.max(0, used ?? Math.max(0, safeTotal - safeRemaining));
|
|
338
|
+
|
|
339
|
+
return {
|
|
340
|
+
total: safeTotal,
|
|
341
|
+
remaining: safeRemaining,
|
|
342
|
+
used: safeUsed,
|
|
343
|
+
resetAt: parseResetTime(account.CycleEndTime || account.DeductionEndTime || account.ExpiredTime),
|
|
344
|
+
};
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
function firstFiniteNumber(...values) {
|
|
348
|
+
for (const value of values) {
|
|
349
|
+
if (value === null || value === undefined || value === "") continue;
|
|
350
|
+
const number = Number(value);
|
|
351
|
+
if (Number.isFinite(number)) return number;
|
|
352
|
+
}
|
|
353
|
+
return null;
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
function getEarlierReset(current, next) {
|
|
357
|
+
if (!current) return next || null;
|
|
358
|
+
if (!next) return current;
|
|
359
|
+
return new Date(next).getTime() < new Date(current).getTime() ? next : current;
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
/**
|
|
363
|
+
* GitHub Copilot Usage
|
|
364
|
+
* Uses GitHub accessToken (not copilotToken) to call copilot_internal/user API
|
|
365
|
+
*/
|
|
366
|
+
async function getGitHubUsage(accessToken, providerSpecificData, proxyOptions = null) {
|
|
367
|
+
try {
|
|
368
|
+
if (!accessToken) {
|
|
369
|
+
throw new Error("No GitHub access token available. Please re-authorize the connection.");
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
// copilot_internal/user API requires GitHub OAuth token, not copilotToken
|
|
373
|
+
const response = await proxyAwareFetch("https://api.github.com/copilot_internal/user", {
|
|
374
|
+
headers: {
|
|
375
|
+
"Authorization": `token ${accessToken}`,
|
|
376
|
+
"Accept": "application/json",
|
|
377
|
+
"X-GitHub-Api-Version": GITHUB_CONFIG.apiVersion,
|
|
378
|
+
"User-Agent": GITHUB_CONFIG.userAgent,
|
|
379
|
+
"Editor-Version": "vscode/1.100.0",
|
|
380
|
+
"Editor-Plugin-Version": "copilot-chat/0.26.7",
|
|
381
|
+
},
|
|
382
|
+
}, proxyOptions);
|
|
383
|
+
|
|
384
|
+
if (!response.ok) {
|
|
385
|
+
const error = await response.text();
|
|
386
|
+
throw new Error(`GitHub API error: ${error}`);
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
const data = await response.json();
|
|
390
|
+
|
|
391
|
+
// Handle different response formats (paid vs free)
|
|
392
|
+
if (data.quota_snapshots) {
|
|
393
|
+
// Paid plan format
|
|
394
|
+
const snapshots = data.quota_snapshots;
|
|
395
|
+
const resetAt = parseResetTime(data.quota_reset_date);
|
|
396
|
+
|
|
397
|
+
return {
|
|
398
|
+
plan: data.copilot_plan,
|
|
399
|
+
resetDate: data.quota_reset_date,
|
|
400
|
+
quotas: {
|
|
401
|
+
chat: { ...formatGitHubQuotaSnapshot(snapshots.chat), resetAt },
|
|
402
|
+
completions: { ...formatGitHubQuotaSnapshot(snapshots.completions), resetAt },
|
|
403
|
+
premium_interactions: { ...formatGitHubQuotaSnapshot(snapshots.premium_interactions), resetAt },
|
|
404
|
+
},
|
|
405
|
+
};
|
|
406
|
+
} else if (data.monthly_quotas || data.limited_user_quotas) {
|
|
407
|
+
// Free/limited plan format
|
|
408
|
+
const monthlyQuotas = data.monthly_quotas || {};
|
|
409
|
+
const usedQuotas = data.limited_user_quotas || {};
|
|
410
|
+
const resetAt = parseResetTime(data.limited_user_reset_date);
|
|
411
|
+
|
|
412
|
+
return {
|
|
413
|
+
plan: data.copilot_plan || data.access_type_sku,
|
|
414
|
+
resetDate: data.limited_user_reset_date,
|
|
415
|
+
quotas: {
|
|
416
|
+
chat: {
|
|
417
|
+
used: usedQuotas.chat || 0,
|
|
418
|
+
total: monthlyQuotas.chat || 0,
|
|
419
|
+
unlimited: false,
|
|
420
|
+
resetAt,
|
|
421
|
+
},
|
|
422
|
+
completions: {
|
|
423
|
+
used: usedQuotas.completions || 0,
|
|
424
|
+
total: monthlyQuotas.completions || 0,
|
|
425
|
+
unlimited: false,
|
|
426
|
+
resetAt,
|
|
427
|
+
},
|
|
428
|
+
},
|
|
429
|
+
};
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
return { message: "GitHub Copilot connected. Unable to parse quota data." };
|
|
433
|
+
} catch (error) {
|
|
434
|
+
throw new Error(`Failed to fetch GitHub usage: ${error.message}`);
|
|
435
|
+
}
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
function formatGitHubQuotaSnapshot(quota) {
|
|
439
|
+
if (!quota) return { used: 0, total: 0, unlimited: true };
|
|
440
|
+
|
|
441
|
+
return {
|
|
442
|
+
used: quota.entitlement - quota.remaining,
|
|
443
|
+
total: quota.entitlement,
|
|
444
|
+
remaining: quota.remaining,
|
|
445
|
+
unlimited: quota.unlimited || false,
|
|
446
|
+
};
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
/**
|
|
450
|
+
* Gemini CLI Usage — fetch per-model quota via Cloud Code Assist API.
|
|
451
|
+
* Uses retrieveUserQuota (same endpoint as `gemini /stats`) returning
|
|
452
|
+
* per-model buckets with remainingFraction + resetTime.
|
|
453
|
+
*/
|
|
454
|
+
async function getGeminiUsage(accessToken, providerSpecificData, proxyOptions = null) {
|
|
455
|
+
if (!accessToken) {
|
|
456
|
+
return { plan: "Free", message: "Gemini CLI access token not available." };
|
|
457
|
+
}
|
|
458
|
+
|
|
459
|
+
try {
|
|
460
|
+
// Resolve project id: prefer connection-stored id, else loadCodeAssist lookup.
|
|
461
|
+
// #1271: OAuth save stores projectId on the connection, not providerSpecificData.
|
|
462
|
+
let projectId = normalizeCloudCodeProjectId(providerSpecificData?.projectId);
|
|
463
|
+
let plan = "Free";
|
|
464
|
+
|
|
465
|
+
if (!projectId) {
|
|
466
|
+
const subInfo = await getGeminiSubscriptionInfo(accessToken, proxyOptions);
|
|
467
|
+
projectId = normalizeCloudCodeProjectId(subInfo?.cloudaicompanionProject);
|
|
468
|
+
plan = subInfo?.currentTier?.name || plan;
|
|
469
|
+
}
|
|
470
|
+
|
|
471
|
+
if (!projectId) {
|
|
472
|
+
return {
|
|
473
|
+
plan,
|
|
474
|
+
message: "Gemini CLI project ID not available. Reconnect Gemini CLI, or configure a Google Cloud project with Gemini Code Assist access before checking quota.",
|
|
475
|
+
};
|
|
476
|
+
}
|
|
477
|
+
|
|
478
|
+
const controller = new AbortController();
|
|
479
|
+
const timeoutId = setTimeout(() => controller.abort(), 10000);
|
|
480
|
+
let response;
|
|
481
|
+
try {
|
|
482
|
+
response = await proxyAwareFetch(
|
|
483
|
+
"https://cloudcode-pa.googleapis.com/v1internal:retrieveUserQuota",
|
|
484
|
+
{
|
|
485
|
+
method: "POST",
|
|
486
|
+
headers: {
|
|
487
|
+
Authorization: `Bearer ${accessToken}`,
|
|
488
|
+
"Content-Type": "application/json",
|
|
489
|
+
},
|
|
490
|
+
body: JSON.stringify({ project: projectId }),
|
|
491
|
+
signal: controller.signal,
|
|
492
|
+
},
|
|
493
|
+
proxyOptions
|
|
494
|
+
);
|
|
495
|
+
} finally {
|
|
496
|
+
clearTimeout(timeoutId);
|
|
497
|
+
}
|
|
498
|
+
|
|
499
|
+
if (!response.ok) {
|
|
500
|
+
return { plan, message: `Gemini CLI quota error (${response.status}).` };
|
|
501
|
+
}
|
|
502
|
+
|
|
503
|
+
const data = await response.json();
|
|
504
|
+
const quotas = {};
|
|
505
|
+
|
|
506
|
+
if (Array.isArray(data.buckets)) {
|
|
507
|
+
for (const bucket of data.buckets) {
|
|
508
|
+
if (!bucket.modelId || bucket.remainingFraction == null) continue;
|
|
509
|
+
|
|
510
|
+
const remainingFraction = Number(bucket.remainingFraction) || 0;
|
|
511
|
+
const total = 1000; // Normalized base, matches antigravity convention
|
|
512
|
+
const remaining = Math.round(total * remainingFraction);
|
|
513
|
+
const used = Math.max(0, total - remaining);
|
|
514
|
+
|
|
515
|
+
quotas[bucket.modelId] = {
|
|
516
|
+
used,
|
|
517
|
+
total,
|
|
518
|
+
resetAt: parseResetTime(bucket.resetTime),
|
|
519
|
+
remainingPercentage: remainingFraction * 100,
|
|
520
|
+
unlimited: false,
|
|
521
|
+
};
|
|
522
|
+
}
|
|
523
|
+
}
|
|
524
|
+
|
|
525
|
+
return { plan, quotas };
|
|
526
|
+
} catch (error) {
|
|
527
|
+
return { message: `Gemini CLI error: ${error.message}` };
|
|
528
|
+
}
|
|
529
|
+
}
|
|
530
|
+
|
|
531
|
+
function normalizeCloudCodeProjectId(project) {
|
|
532
|
+
if (typeof project === "string") return project.trim() || null;
|
|
533
|
+
if (project && typeof project === "object" && typeof project.id === "string") {
|
|
534
|
+
return project.id.trim() || null;
|
|
535
|
+
}
|
|
536
|
+
return null;
|
|
537
|
+
}
|
|
538
|
+
|
|
539
|
+
/**
|
|
540
|
+
* Get Gemini CLI subscription info via loadCodeAssist
|
|
541
|
+
*/
|
|
542
|
+
async function getGeminiSubscriptionInfo(accessToken, proxyOptions = null) {
|
|
543
|
+
const controller = new AbortController();
|
|
544
|
+
const timeoutId = setTimeout(() => controller.abort(), 10000);
|
|
545
|
+
try {
|
|
546
|
+
const response = await proxyAwareFetch(
|
|
547
|
+
"https://cloudcode-pa.googleapis.com/v1internal:loadCodeAssist",
|
|
548
|
+
{
|
|
549
|
+
method: "POST",
|
|
550
|
+
headers: {
|
|
551
|
+
Authorization: `Bearer ${accessToken}`,
|
|
552
|
+
"Content-Type": "application/json",
|
|
553
|
+
},
|
|
554
|
+
body: JSON.stringify({
|
|
555
|
+
metadata: CLIENT_METADATA,
|
|
556
|
+
}),
|
|
557
|
+
signal: controller.signal,
|
|
558
|
+
},
|
|
559
|
+
proxyOptions
|
|
560
|
+
);
|
|
561
|
+
if (!response.ok) return null;
|
|
562
|
+
return await response.json();
|
|
563
|
+
} catch {
|
|
564
|
+
return null;
|
|
565
|
+
} finally {
|
|
566
|
+
clearTimeout(timeoutId);
|
|
567
|
+
}
|
|
568
|
+
}
|
|
569
|
+
|
|
570
|
+
/**
|
|
571
|
+
* Antigravity Usage - Fetch quota from Google Cloud Code API
|
|
572
|
+
*/
|
|
573
|
+
async function getAntigravityUsage(accessToken, providerSpecificData, proxyOptions = null) {
|
|
574
|
+
try {
|
|
575
|
+
// Fetch subscription info once — reuse for both projectId and plan
|
|
576
|
+
const subscriptionInfo = await getAntigravitySubscriptionInfo(accessToken, proxyOptions);
|
|
577
|
+
const projectId = subscriptionInfo?.cloudaicompanionProject || null;
|
|
578
|
+
|
|
579
|
+
// Fetch quota data with timeout
|
|
580
|
+
const controller = new AbortController();
|
|
581
|
+
const timeoutId = setTimeout(() => controller.abort(), 10000); // 10s timeout
|
|
582
|
+
|
|
583
|
+
let response;
|
|
584
|
+
try {
|
|
585
|
+
response = await proxyAwareFetch(ANTIGRAVITY_CONFIG.quotaApiUrl, {
|
|
586
|
+
method: "POST",
|
|
587
|
+
headers: {
|
|
588
|
+
"Authorization": `Bearer ${accessToken}`,
|
|
589
|
+
"User-Agent": ANTIGRAVITY_CONFIG.userAgent,
|
|
590
|
+
"Content-Type": "application/json",
|
|
591
|
+
"X-Client-Name": "antigravity",
|
|
592
|
+
"X-Client-Version": "1.107.0",
|
|
593
|
+
"x-request-source": "local", // MITM bypass
|
|
594
|
+
},
|
|
595
|
+
body: JSON.stringify({
|
|
596
|
+
...(projectId ? { project: projectId } : {})
|
|
597
|
+
}),
|
|
598
|
+
signal: controller.signal,
|
|
599
|
+
}, proxyOptions);
|
|
600
|
+
} finally {
|
|
601
|
+
clearTimeout(timeoutId);
|
|
602
|
+
}
|
|
603
|
+
|
|
604
|
+
if (response.status === 403) {
|
|
605
|
+
return {
|
|
606
|
+
message: "Antigravity quota API access forbidden. Chat may still work.",
|
|
607
|
+
quotas: {}
|
|
608
|
+
};
|
|
609
|
+
}
|
|
610
|
+
|
|
611
|
+
if (response.status === 401) {
|
|
612
|
+
return {
|
|
613
|
+
message: "Antigravity quota API authentication expired. Chat may still work.",
|
|
614
|
+
quotas: {}
|
|
615
|
+
};
|
|
616
|
+
}
|
|
617
|
+
|
|
618
|
+
if (!response.ok) {
|
|
619
|
+
throw new Error(`Antigravity API error: ${response.status}`);
|
|
620
|
+
}
|
|
621
|
+
|
|
622
|
+
const data = await response.json();
|
|
623
|
+
const quotas = {};
|
|
624
|
+
|
|
625
|
+
// Parse model quotas (inspired by vscode-antigravity-cockpit)
|
|
626
|
+
if (data.models) {
|
|
627
|
+
// Filter only recommended/important models (must match PROVIDER_MODELS ag ids)
|
|
628
|
+
const importantModels = [
|
|
629
|
+
'gemini-3-flash-agent',
|
|
630
|
+
'gemini-3.5-flash-low',
|
|
631
|
+
'gemini-3.5-flash-extra-low',
|
|
632
|
+
'gemini-pro-agent',
|
|
633
|
+
'gemini-3.1-pro-low',
|
|
634
|
+
'claude-sonnet-4-6',
|
|
635
|
+
'claude-opus-4-6-thinking',
|
|
636
|
+
'gpt-oss-120b-medium',
|
|
637
|
+
'gemini-3-flash',
|
|
638
|
+
];
|
|
639
|
+
|
|
640
|
+
for (const [modelKey, info] of Object.entries(data.models)) {
|
|
641
|
+
// Skip models without quota info
|
|
642
|
+
if (!info.quotaInfo) {
|
|
643
|
+
continue;
|
|
644
|
+
}
|
|
645
|
+
|
|
646
|
+
// Skip internal models and non-important models
|
|
647
|
+
if (info.isInternal || !importantModels.includes(modelKey)) {
|
|
648
|
+
continue;
|
|
649
|
+
}
|
|
650
|
+
|
|
651
|
+
const remainingFraction = info.quotaInfo.remainingFraction || 0;
|
|
652
|
+
const remainingPercentage = remainingFraction * 100;
|
|
653
|
+
|
|
654
|
+
// Convert percentage to used/total for UI compatibility
|
|
655
|
+
const total = 1000; // Normalized base
|
|
656
|
+
const remaining = Math.round(total * remainingFraction);
|
|
657
|
+
const used = total - remaining;
|
|
658
|
+
|
|
659
|
+
// Use modelKey as key (matches PROVIDER_MODELS id)
|
|
660
|
+
quotas[modelKey] = {
|
|
661
|
+
used,
|
|
662
|
+
total,
|
|
663
|
+
resetAt: parseResetTime(info.quotaInfo.resetTime),
|
|
664
|
+
remainingPercentage,
|
|
665
|
+
unlimited: false,
|
|
666
|
+
displayName: info.displayName || modelKey,
|
|
667
|
+
};
|
|
668
|
+
}
|
|
669
|
+
}
|
|
670
|
+
|
|
671
|
+
return {
|
|
672
|
+
plan: subscriptionInfo?.currentTier?.name || "Unknown",
|
|
673
|
+
quotas,
|
|
674
|
+
subscriptionInfo,
|
|
675
|
+
};
|
|
676
|
+
} catch (error) {
|
|
677
|
+
console.error("[Antigravity Usage] Error:", error.message, error.cause);
|
|
678
|
+
return { message: `Antigravity error: ${error.message}` };
|
|
679
|
+
}
|
|
680
|
+
}
|
|
681
|
+
|
|
682
|
+
/**
|
|
683
|
+
* Get Antigravity project ID from subscription info
|
|
684
|
+
*/
|
|
685
|
+
async function getAntigravityProjectId(accessToken) {
|
|
686
|
+
try {
|
|
687
|
+
const info = await getAntigravitySubscriptionInfo(accessToken);
|
|
688
|
+
return info?.cloudaicompanionProject || null;
|
|
689
|
+
} catch {
|
|
690
|
+
return null;
|
|
691
|
+
}
|
|
692
|
+
}
|
|
693
|
+
|
|
694
|
+
/**
|
|
695
|
+
* Get Antigravity subscription info
|
|
696
|
+
*/
|
|
697
|
+
async function getAntigravitySubscriptionInfo(accessToken, proxyOptions = null) {
|
|
698
|
+
const controller = new AbortController();
|
|
699
|
+
const timeoutId = setTimeout(() => controller.abort(), 10000); // 10s timeout
|
|
700
|
+
try {
|
|
701
|
+
const response = await proxyAwareFetch(ANTIGRAVITY_CONFIG.loadProjectApiUrl, {
|
|
702
|
+
method: "POST",
|
|
703
|
+
headers: {
|
|
704
|
+
"Authorization": `Bearer ${accessToken}`,
|
|
705
|
+
"User-Agent": ANTIGRAVITY_CONFIG.userAgent,
|
|
706
|
+
"Content-Type": "application/json",
|
|
707
|
+
"x-request-source": "local", // MITM bypass
|
|
708
|
+
},
|
|
709
|
+
body: JSON.stringify({ metadata: CLIENT_METADATA, mode: 1 }),
|
|
710
|
+
signal: controller.signal,
|
|
711
|
+
}, proxyOptions);
|
|
712
|
+
|
|
713
|
+
if (!response.ok) return null;
|
|
714
|
+
return await response.json();
|
|
715
|
+
} catch (error) {
|
|
716
|
+
console.error("[Antigravity Subscription] Error:", error.message);
|
|
717
|
+
return null;
|
|
718
|
+
} finally {
|
|
719
|
+
clearTimeout(timeoutId);
|
|
720
|
+
}
|
|
721
|
+
}
|
|
722
|
+
|
|
723
|
+
/**
|
|
724
|
+
* Claude Usage - Primary: OAuth endpoint, Fallback: legacy settings/org endpoint
|
|
725
|
+
*/
|
|
726
|
+
async function getClaudeUsage(accessToken, proxyOptions = null) {
|
|
727
|
+
try {
|
|
728
|
+
// Primary: OAuth usage endpoint (Claude Code consumer OAuth tokens)
|
|
729
|
+
const oauthResponse = await proxyAwareFetch(CLAUDE_CONFIG.oauthUsageUrl, {
|
|
730
|
+
method: "GET",
|
|
731
|
+
headers: {
|
|
732
|
+
"Authorization": `Bearer ${accessToken}`,
|
|
733
|
+
"anthropic-beta": "oauth-2025-04-20",
|
|
734
|
+
"anthropic-version": CLAUDE_CONFIG.apiVersion,
|
|
735
|
+
},
|
|
736
|
+
}, proxyOptions);
|
|
737
|
+
|
|
738
|
+
if (oauthResponse.ok) {
|
|
739
|
+
const data = await oauthResponse.json();
|
|
740
|
+
const quotas = {};
|
|
741
|
+
|
|
742
|
+
// utilization = % USED (e.g. 87 means 87% used, 13% remaining)
|
|
743
|
+
const hasUtilization = (window) =>
|
|
744
|
+
window && typeof window === "object" && typeof window.utilization === "number";
|
|
745
|
+
|
|
746
|
+
const createQuotaObject = (window) => {
|
|
747
|
+
const used = window.utilization;
|
|
748
|
+
const remaining = Math.max(0, 100 - used);
|
|
749
|
+
return {
|
|
750
|
+
used,
|
|
751
|
+
total: 100,
|
|
752
|
+
remaining,
|
|
753
|
+
remainingPercentage: remaining,
|
|
754
|
+
resetAt: parseResetTime(window.resets_at),
|
|
755
|
+
unlimited: false,
|
|
756
|
+
};
|
|
757
|
+
};
|
|
758
|
+
|
|
759
|
+
if (hasUtilization(data.five_hour)) {
|
|
760
|
+
quotas["session (5h)"] = createQuotaObject(data.five_hour);
|
|
761
|
+
}
|
|
762
|
+
|
|
763
|
+
if (hasUtilization(data.seven_day)) {
|
|
764
|
+
quotas["weekly (7d)"] = createQuotaObject(data.seven_day);
|
|
765
|
+
}
|
|
766
|
+
|
|
767
|
+
// Parse model-specific weekly windows (e.g. seven_day_sonnet, seven_day_opus)
|
|
768
|
+
for (const [key, value] of Object.entries(data)) {
|
|
769
|
+
if (key.startsWith("seven_day_") && key !== "seven_day" && hasUtilization(value)) {
|
|
770
|
+
const modelName = key.replace("seven_day_", "");
|
|
771
|
+
quotas[`weekly ${modelName} (7d)`] = createQuotaObject(value);
|
|
772
|
+
}
|
|
773
|
+
}
|
|
774
|
+
|
|
775
|
+
return {
|
|
776
|
+
plan: "Claude Code",
|
|
777
|
+
extraUsage: data.extra_usage ?? null,
|
|
778
|
+
quotas,
|
|
779
|
+
};
|
|
780
|
+
}
|
|
781
|
+
|
|
782
|
+
// Fallback: legacy settings + org usage endpoint
|
|
783
|
+
console.warn(`[Claude Usage] OAuth endpoint returned ${oauthResponse.status}, falling back to legacy`);
|
|
784
|
+
return await getClaudeUsageLegacy(accessToken, proxyOptions);
|
|
785
|
+
} catch (error) {
|
|
786
|
+
return { message: `Claude connected. Unable to fetch usage: ${error.message}` };
|
|
787
|
+
}
|
|
788
|
+
}
|
|
789
|
+
|
|
790
|
+
/**
|
|
791
|
+
* Legacy Claude usage for API key / org admin users
|
|
792
|
+
*/
|
|
793
|
+
async function getClaudeUsageLegacy(accessToken, proxyOptions = null) {
|
|
794
|
+
try {
|
|
795
|
+
const settingsResponse = await proxyAwareFetch(CLAUDE_CONFIG.settingsUrl, {
|
|
796
|
+
method: "GET",
|
|
797
|
+
headers: {
|
|
798
|
+
"Authorization": `Bearer ${accessToken}`,
|
|
799
|
+
"anthropic-version": CLAUDE_CONFIG.apiVersion,
|
|
800
|
+
},
|
|
801
|
+
}, proxyOptions);
|
|
802
|
+
|
|
803
|
+
if (settingsResponse.ok) {
|
|
804
|
+
const settings = await settingsResponse.json();
|
|
805
|
+
|
|
806
|
+
if (settings.organization_id) {
|
|
807
|
+
const usageResponse = await proxyAwareFetch(
|
|
808
|
+
CLAUDE_CONFIG.usageUrl.replace("{org_id}", settings.organization_id),
|
|
809
|
+
{
|
|
810
|
+
method: "GET",
|
|
811
|
+
headers: {
|
|
812
|
+
"Authorization": `Bearer ${accessToken}`,
|
|
813
|
+
"anthropic-version": CLAUDE_CONFIG.apiVersion,
|
|
814
|
+
},
|
|
815
|
+
},
|
|
816
|
+
proxyOptions
|
|
817
|
+
);
|
|
818
|
+
|
|
819
|
+
if (usageResponse.ok) {
|
|
820
|
+
const usage = await usageResponse.json();
|
|
821
|
+
return {
|
|
822
|
+
plan: settings.plan || "Unknown",
|
|
823
|
+
organization: settings.organization_name,
|
|
824
|
+
quotas: usage,
|
|
825
|
+
};
|
|
826
|
+
}
|
|
827
|
+
}
|
|
828
|
+
|
|
829
|
+
return {
|
|
830
|
+
plan: settings.plan || "Unknown",
|
|
831
|
+
organization: settings.organization_name,
|
|
832
|
+
message: "Claude connected. Usage details require admin access.",
|
|
833
|
+
};
|
|
834
|
+
}
|
|
835
|
+
|
|
836
|
+
return { message: "Claude connected. Usage API requires admin permissions." };
|
|
837
|
+
} catch (error) {
|
|
838
|
+
return { message: `Claude connected. Unable to fetch usage: ${error.message}` };
|
|
839
|
+
}
|
|
840
|
+
}
|
|
841
|
+
|
|
842
|
+
/**
|
|
843
|
+
* Codex (OpenAI) Usage - Fetch from ChatGPT backend API
|
|
844
|
+
*/
|
|
845
|
+
function toFiniteNumber(value, fallback = 0) {
|
|
846
|
+
if (typeof value === "number" && Number.isFinite(value)) return value;
|
|
847
|
+
if (typeof value === "string" && value.trim()) {
|
|
848
|
+
const parsed = Number(value);
|
|
849
|
+
if (Number.isFinite(parsed)) return parsed;
|
|
850
|
+
}
|
|
851
|
+
return fallback;
|
|
852
|
+
}
|
|
853
|
+
|
|
854
|
+
function getCodexRateLimitBody(snapshot) {
|
|
855
|
+
if (!snapshot || typeof snapshot !== "object" || Array.isArray(snapshot)) return null;
|
|
856
|
+
return snapshot.rate_limit && typeof snapshot.rate_limit === "object"
|
|
857
|
+
? snapshot.rate_limit
|
|
858
|
+
: snapshot;
|
|
859
|
+
}
|
|
860
|
+
|
|
861
|
+
function formatCodexWindow(window) {
|
|
862
|
+
const used = Math.max(0, Math.min(100, toFiniteNumber(window?.used_percent ?? window?.percent_used, 0)));
|
|
863
|
+
return {
|
|
864
|
+
used,
|
|
865
|
+
total: 100,
|
|
866
|
+
remaining: Math.max(0, 100 - used),
|
|
867
|
+
resetAt: parseResetTime(window?.reset_at ?? window?.resets_at ?? window?.resetAt ?? null),
|
|
868
|
+
unlimited: false,
|
|
869
|
+
};
|
|
870
|
+
}
|
|
871
|
+
|
|
872
|
+
function appendCodexQuotaWindows(quotas, prefix, snapshot) {
|
|
873
|
+
const rateLimit = getCodexRateLimitBody(snapshot);
|
|
874
|
+
if (!rateLimit) return false;
|
|
875
|
+
|
|
876
|
+
const primary = rateLimit.primary_window || rateLimit.primary || snapshot.primary_window || snapshot.primary;
|
|
877
|
+
const secondary = rateLimit.secondary_window || rateLimit.secondary || snapshot.secondary_window || snapshot.secondary;
|
|
878
|
+
let added = false;
|
|
879
|
+
|
|
880
|
+
if (primary) {
|
|
881
|
+
quotas[prefix ? `${prefix}_session` : "session"] = formatCodexWindow(primary);
|
|
882
|
+
added = true;
|
|
883
|
+
}
|
|
884
|
+
if (secondary) {
|
|
885
|
+
quotas[prefix ? `${prefix}_weekly` : "weekly"] = formatCodexWindow(secondary);
|
|
886
|
+
added = true;
|
|
887
|
+
}
|
|
888
|
+
|
|
889
|
+
return added;
|
|
890
|
+
}
|
|
891
|
+
|
|
892
|
+
function getCodexReviewRateLimit(data) {
|
|
893
|
+
if (data.code_review_rate_limit || data.review_rate_limit) {
|
|
894
|
+
return data.code_review_rate_limit || data.review_rate_limit;
|
|
895
|
+
}
|
|
896
|
+
|
|
897
|
+
const byLimitId = data.rate_limits_by_limit_id;
|
|
898
|
+
if (byLimitId && typeof byLimitId === "object" && !Array.isArray(byLimitId)) {
|
|
899
|
+
return byLimitId.code_review || byLimitId.codex_review || byLimitId.review || null;
|
|
900
|
+
}
|
|
901
|
+
|
|
902
|
+
const additional = Array.isArray(data.additional_rate_limits) ? data.additional_rate_limits : [];
|
|
903
|
+
return additional.find((entry) => {
|
|
904
|
+
const id = String(entry?.limit_name || entry?.metered_feature || entry?.id || "").toLowerCase();
|
|
905
|
+
return id === "code_review" || id === "codex_review" || id === "review" || id.includes("review");
|
|
906
|
+
}) || null;
|
|
907
|
+
}
|
|
908
|
+
|
|
909
|
+
async function getCodexUsage(accessToken, proxyOptions = null) {
|
|
910
|
+
try {
|
|
911
|
+
const response = await proxyAwareFetch(CODEX_CONFIG.usageUrl, {
|
|
912
|
+
method: "GET",
|
|
913
|
+
headers: {
|
|
914
|
+
"Authorization": `Bearer ${accessToken}`,
|
|
915
|
+
"Accept": "application/json",
|
|
916
|
+
},
|
|
917
|
+
}, proxyOptions);
|
|
918
|
+
|
|
919
|
+
if (!response.ok) {
|
|
920
|
+
return { message: `Codex connected. Usage API temporarily unavailable (${response.status}).` };
|
|
921
|
+
}
|
|
922
|
+
|
|
923
|
+
const data = await response.json();
|
|
924
|
+
const normalRateLimit = data.rate_limit || data.rate_limits || data.rate_limits_by_limit_id?.codex || {};
|
|
925
|
+
const reviewRateLimit = getCodexReviewRateLimit(data);
|
|
926
|
+
const quotas = {};
|
|
927
|
+
|
|
928
|
+
appendCodexQuotaWindows(quotas, "", normalRateLimit);
|
|
929
|
+
appendCodexQuotaWindows(quotas, "review", reviewRateLimit);
|
|
930
|
+
|
|
931
|
+
return {
|
|
932
|
+
plan: data.plan_type || data.summary?.plan || "unknown",
|
|
933
|
+
limitReached: getCodexRateLimitBody(normalRateLimit)?.limit_reached || false,
|
|
934
|
+
reviewLimitReached: getCodexRateLimitBody(reviewRateLimit)?.limit_reached || false,
|
|
935
|
+
quotas,
|
|
936
|
+
};
|
|
937
|
+
} catch (error) {
|
|
938
|
+
throw new Error(`Failed to fetch Codex usage: ${error.message}`);
|
|
939
|
+
}
|
|
940
|
+
}
|
|
941
|
+
|
|
942
|
+
/**
|
|
943
|
+
* Kiro (AWS CodeWhisperer) Usage
|
|
944
|
+
*/
|
|
945
|
+
function parseKiroQuotaData(data) {
|
|
946
|
+
const usageList = data.usageBreakdownList || [];
|
|
947
|
+
const quotaInfo = {};
|
|
948
|
+
const resetAt = parseResetTime(data.nextDateReset || data.resetDate);
|
|
949
|
+
|
|
950
|
+
usageList.forEach((breakdown) => {
|
|
951
|
+
const resourceType = breakdown.resourceType?.toLowerCase() || "unknown";
|
|
952
|
+
const used = breakdown.currentUsageWithPrecision || 0;
|
|
953
|
+
const total = breakdown.usageLimitWithPrecision || 0;
|
|
954
|
+
|
|
955
|
+
quotaInfo[resourceType] = {
|
|
956
|
+
used,
|
|
957
|
+
total,
|
|
958
|
+
remaining: total - used,
|
|
959
|
+
resetAt,
|
|
960
|
+
unlimited: false,
|
|
961
|
+
};
|
|
962
|
+
|
|
963
|
+
// Add free trial if available
|
|
964
|
+
if (breakdown.freeTrialInfo) {
|
|
965
|
+
const freeUsed = breakdown.freeTrialInfo.currentUsageWithPrecision || 0;
|
|
966
|
+
const freeTotal = breakdown.freeTrialInfo.usageLimitWithPrecision || 0;
|
|
967
|
+
|
|
968
|
+
quotaInfo[`${resourceType}_freetrial`] = {
|
|
969
|
+
used: freeUsed,
|
|
970
|
+
total: freeTotal,
|
|
971
|
+
remaining: freeTotal - freeUsed,
|
|
972
|
+
resetAt: parseResetTime(breakdown.freeTrialInfo.freeTrialExpiry || resetAt),
|
|
973
|
+
unlimited: false,
|
|
974
|
+
};
|
|
975
|
+
}
|
|
976
|
+
});
|
|
977
|
+
|
|
978
|
+
return {
|
|
979
|
+
plan: data.subscriptionInfo?.subscriptionTitle || "Kiro",
|
|
980
|
+
quotas: quotaInfo,
|
|
981
|
+
};
|
|
982
|
+
}
|
|
983
|
+
|
|
984
|
+
async function getKiroUsage(accessToken, providerSpecificData, proxyOptions = null) {
|
|
985
|
+
// Default profileArn fallback
|
|
986
|
+
const DEFAULT_PROFILE_ARN = "arn:aws:codewhisperer:us-east-1:638616132270:profile/AAAACCCCXXXX";
|
|
987
|
+
const profileArn = providerSpecificData?.profileArn || DEFAULT_PROFILE_ARN;
|
|
988
|
+
const authMethod = providerSpecificData?.authMethod || "builder-id";
|
|
989
|
+
|
|
990
|
+
const getUsageParams = new URLSearchParams({
|
|
991
|
+
isEmailRequired: "true",
|
|
992
|
+
origin: "AI_EDITOR",
|
|
993
|
+
resourceType: "AGENTIC_REQUEST",
|
|
994
|
+
});
|
|
995
|
+
|
|
996
|
+
// For compatibility, try multiple known Kiro usage endpoints
|
|
997
|
+
const attempts = [
|
|
998
|
+
{
|
|
999
|
+
name: "codewhisperer-get",
|
|
1000
|
+
run: async () => proxyAwareFetch(
|
|
1001
|
+
`https://codewhisperer.us-east-1.amazonaws.com/getUsageLimits?${getUsageParams.toString()}`,
|
|
1002
|
+
{
|
|
1003
|
+
method: "GET",
|
|
1004
|
+
headers: {
|
|
1005
|
+
"Authorization": `Bearer ${accessToken}`,
|
|
1006
|
+
"Accept": "application/json",
|
|
1007
|
+
"x-amz-user-agent": "aws-sdk-js/1.0.0 KiroIDE",
|
|
1008
|
+
"user-agent": "aws-sdk-js/1.0.0 KiroIDE",
|
|
1009
|
+
},
|
|
1010
|
+
},
|
|
1011
|
+
proxyOptions
|
|
1012
|
+
),
|
|
1013
|
+
},
|
|
1014
|
+
{
|
|
1015
|
+
name: "codewhisperer-post",
|
|
1016
|
+
run: async () => proxyAwareFetch("https://codewhisperer.us-east-1.amazonaws.com", {
|
|
1017
|
+
method: "POST",
|
|
1018
|
+
headers: {
|
|
1019
|
+
"Authorization": `Bearer ${accessToken}`,
|
|
1020
|
+
"Content-Type": "application/x-amz-json-1.0",
|
|
1021
|
+
"x-amz-target": "AmazonCodeWhispererService.GetUsageLimits",
|
|
1022
|
+
"Accept": "application/json",
|
|
1023
|
+
},
|
|
1024
|
+
body: JSON.stringify({
|
|
1025
|
+
origin: "AI_EDITOR",
|
|
1026
|
+
profileArn,
|
|
1027
|
+
resourceType: "AGENTIC_REQUEST",
|
|
1028
|
+
}),
|
|
1029
|
+
}, proxyOptions),
|
|
1030
|
+
},
|
|
1031
|
+
{
|
|
1032
|
+
name: "q-get",
|
|
1033
|
+
run: async () => {
|
|
1034
|
+
const params = new URLSearchParams({
|
|
1035
|
+
origin: "AI_EDITOR",
|
|
1036
|
+
profileArn,
|
|
1037
|
+
resourceType: "AGENTIC_REQUEST",
|
|
1038
|
+
});
|
|
1039
|
+
return proxyAwareFetch(`https://q.us-east-1.amazonaws.com/getUsageLimits?${params}`, {
|
|
1040
|
+
method: "GET",
|
|
1041
|
+
headers: {
|
|
1042
|
+
"Authorization": `Bearer ${accessToken}`,
|
|
1043
|
+
"Accept": "application/json",
|
|
1044
|
+
},
|
|
1045
|
+
}, proxyOptions);
|
|
1046
|
+
},
|
|
1047
|
+
},
|
|
1048
|
+
];
|
|
1049
|
+
|
|
1050
|
+
let sawAuthError = false;
|
|
1051
|
+
const errors = [];
|
|
1052
|
+
|
|
1053
|
+
for (const attempt of attempts) {
|
|
1054
|
+
try {
|
|
1055
|
+
const response = await attempt.run();
|
|
1056
|
+
if (!response.ok) {
|
|
1057
|
+
const errorText = await response.text().catch(() => "");
|
|
1058
|
+
if (response.status === 401 || response.status === 403) {
|
|
1059
|
+
sawAuthError = true;
|
|
1060
|
+
}
|
|
1061
|
+
errors.push(`${attempt.name}:${response.status}${errorText ? `:${errorText}` : ""}`);
|
|
1062
|
+
continue;
|
|
1063
|
+
}
|
|
1064
|
+
|
|
1065
|
+
const data = await response.json();
|
|
1066
|
+
return parseKiroQuotaData(data);
|
|
1067
|
+
} catch (error) {
|
|
1068
|
+
errors.push(`${attempt.name}:${error.message}`);
|
|
1069
|
+
}
|
|
1070
|
+
}
|
|
1071
|
+
|
|
1072
|
+
if (sawAuthError && authMethod === "idc") {
|
|
1073
|
+
return {
|
|
1074
|
+
message: "Kiro quota API is unavailable for the current AWS IAM Identity Center session. Chat may still work. If this persists after renewing your session, reconnect Kiro.",
|
|
1075
|
+
quotas: {},
|
|
1076
|
+
};
|
|
1077
|
+
}
|
|
1078
|
+
|
|
1079
|
+
// Social auth (Google/GitHub) - these use a different token format that may not work with AWS CodeWhisperer quota APIs
|
|
1080
|
+
if (sawAuthError && (authMethod === "google" || authMethod === "github")) {
|
|
1081
|
+
return {
|
|
1082
|
+
message: "Kiro quota API authentication expired. Chat may still work.",
|
|
1083
|
+
quotas: {},
|
|
1084
|
+
};
|
|
1085
|
+
}
|
|
1086
|
+
|
|
1087
|
+
if (sawAuthError) {
|
|
1088
|
+
return {
|
|
1089
|
+
message: "Kiro quota API rejected the current token. Chat may still work.",
|
|
1090
|
+
quotas: {},
|
|
1091
|
+
};
|
|
1092
|
+
}
|
|
1093
|
+
|
|
1094
|
+
const fallbackMessage =
|
|
1095
|
+
errors.length > 0
|
|
1096
|
+
? `Unable to fetch Kiro usage right now. (${errors[errors.length - 1]})`
|
|
1097
|
+
: "Unable to fetch Kiro usage right now.";
|
|
1098
|
+
|
|
1099
|
+
return {
|
|
1100
|
+
message: fallbackMessage,
|
|
1101
|
+
quotas: {},
|
|
1102
|
+
};
|
|
1103
|
+
}
|
|
1104
|
+
|
|
1105
|
+
/**
|
|
1106
|
+
* Qwen Usage
|
|
1107
|
+
*/
|
|
1108
|
+
async function getQwenUsage(accessToken, providerSpecificData) {
|
|
1109
|
+
try {
|
|
1110
|
+
const resourceUrl = providerSpecificData?.resourceUrl;
|
|
1111
|
+
if (!resourceUrl) {
|
|
1112
|
+
return { message: "Qwen connected. No resource URL available." };
|
|
1113
|
+
}
|
|
1114
|
+
|
|
1115
|
+
// Qwen may have usage endpoint at resource URL
|
|
1116
|
+
return { message: "Qwen connected. Usage tracked per request." };
|
|
1117
|
+
} catch (error) {
|
|
1118
|
+
return { message: "Unable to fetch Qwen usage." };
|
|
1119
|
+
}
|
|
1120
|
+
}
|
|
1121
|
+
|
|
1122
|
+
/**
|
|
1123
|
+
* iFlow Usage
|
|
1124
|
+
*/
|
|
1125
|
+
async function getIflowUsage(accessToken) {
|
|
1126
|
+
try {
|
|
1127
|
+
// iFlow may have usage endpoint
|
|
1128
|
+
return { message: "iFlow connected. Usage tracked per request." };
|
|
1129
|
+
} catch (error) {
|
|
1130
|
+
return { message: "Unable to fetch iFlow usage." };
|
|
1131
|
+
}
|
|
1132
|
+
}
|
|
1133
|
+
|
|
1134
|
+
/**
|
|
1135
|
+
* Ollama Cloud Usage
|
|
1136
|
+
* Ollama Cloud uses an API key from ollama.com/settings/keys
|
|
1137
|
+
* and has no public usage API — free tier has light usage limits (resets every 5h & 7d).
|
|
1138
|
+
* This returns an informational message with the plan details.
|
|
1139
|
+
*/
|
|
1140
|
+
async function getOllamaUsage(accessToken, providerSpecificData) {
|
|
1141
|
+
try {
|
|
1142
|
+
// Ollama Cloud does not expose a public quota/usage API.
|
|
1143
|
+
// The provider is configured as noAuth with a notice explaining limits.
|
|
1144
|
+
// We return a graceful message so the UI shows a friendly state instead of an error.
|
|
1145
|
+
const plan = providerSpecificData?.plan || "Free";
|
|
1146
|
+
return {
|
|
1147
|
+
plan,
|
|
1148
|
+
message: "Ollama Cloud uses a free tier with light usage limits (resets every 5h & 7d). For detailed usage tracking, visit ollama.com/settings/keys.",
|
|
1149
|
+
quotas: [],
|
|
1150
|
+
};
|
|
1151
|
+
} catch (error) {
|
|
1152
|
+
return { message: "Unable to fetch Ollama Cloud usage." };
|
|
1153
|
+
}
|
|
1154
|
+
}
|
|
1155
|
+
|
|
1156
|
+
/**
|
|
1157
|
+
* GLM Coding Plan usage (international + China regions)
|
|
1158
|
+
*/
|
|
1159
|
+
async function getGlmUsage(apiKey, provider, proxyOptions = null) {
|
|
1160
|
+
if (!apiKey) {
|
|
1161
|
+
return { message: "GLM API key not available." };
|
|
1162
|
+
}
|
|
1163
|
+
|
|
1164
|
+
const region = provider === "glm-cn" ? "china" : "international";
|
|
1165
|
+
const quotaUrl = GLM_QUOTA_URLS[region];
|
|
1166
|
+
|
|
1167
|
+
try {
|
|
1168
|
+
const response = await proxyAwareFetch(quotaUrl, {
|
|
1169
|
+
headers: {
|
|
1170
|
+
Authorization: `Bearer ${apiKey}`,
|
|
1171
|
+
Accept: "application/json",
|
|
1172
|
+
},
|
|
1173
|
+
}, proxyOptions);
|
|
1174
|
+
|
|
1175
|
+
if (!response.ok) {
|
|
1176
|
+
if (response.status === 401) {
|
|
1177
|
+
return { message: "GLM API key invalid or expired." };
|
|
1178
|
+
}
|
|
1179
|
+
return { message: `GLM quota API error (${response.status}).` };
|
|
1180
|
+
}
|
|
1181
|
+
|
|
1182
|
+
const json = await response.json();
|
|
1183
|
+
const data = json?.data && typeof json.data === "object" ? json.data : {};
|
|
1184
|
+
const limits = Array.isArray(data.limits) ? data.limits : [];
|
|
1185
|
+
const quotas = {};
|
|
1186
|
+
|
|
1187
|
+
for (const limit of limits) {
|
|
1188
|
+
if (!limit || limit.type !== "TOKENS_LIMIT") continue;
|
|
1189
|
+
const usedPercent = Number(limit.percentage) || 0;
|
|
1190
|
+
const resetMs = Number(limit.nextResetTime) || 0;
|
|
1191
|
+
const remaining = Math.max(0, 100 - usedPercent);
|
|
1192
|
+
|
|
1193
|
+
quotas["session"] = {
|
|
1194
|
+
used: usedPercent,
|
|
1195
|
+
total: 100,
|
|
1196
|
+
remaining,
|
|
1197
|
+
remainingPercentage: remaining,
|
|
1198
|
+
resetAt: resetMs > 0 ? new Date(resetMs).toISOString() : null,
|
|
1199
|
+
unlimited: false,
|
|
1200
|
+
};
|
|
1201
|
+
}
|
|
1202
|
+
|
|
1203
|
+
const levelRaw = typeof data.level === "string" ? data.level : "";
|
|
1204
|
+
const plan = levelRaw
|
|
1205
|
+
? levelRaw.charAt(0).toUpperCase() + levelRaw.slice(1).toLowerCase()
|
|
1206
|
+
: "Unknown";
|
|
1207
|
+
|
|
1208
|
+
return { plan, quotas };
|
|
1209
|
+
} catch (error) {
|
|
1210
|
+
return { message: `GLM error: ${error.message}` };
|
|
1211
|
+
}
|
|
1212
|
+
}
|
|
1213
|
+
|
|
1214
|
+
// ── MiniMax helpers ──────────────────────────────────────────────────────
|
|
1215
|
+
function getMiniMaxField(model, snakeKey, camelKey) {
|
|
1216
|
+
if (!model || typeof model !== "object") return null;
|
|
1217
|
+
return model[snakeKey] ?? model[camelKey] ?? null;
|
|
1218
|
+
}
|
|
1219
|
+
|
|
1220
|
+
function getMiniMaxModelName(model) {
|
|
1221
|
+
return String(getMiniMaxField(model, "model_name", "modelName") || "").trim();
|
|
1222
|
+
}
|
|
1223
|
+
|
|
1224
|
+
function formatMiniMaxQuotaName(model) {
|
|
1225
|
+
const rawName = getMiniMaxModelName(model);
|
|
1226
|
+
if (!rawName) return "MiniMax";
|
|
1227
|
+
|
|
1228
|
+
// M3+ shared quota pool: MiniMax reports M-series as a single wildcard
|
|
1229
|
+
// bucket ("MiniMax-M*"). Newer responses rename it to plain "general".
|
|
1230
|
+
// Render both as a friendly series label rather than leaking the
|
|
1231
|
+
// asterisk or the vague "general" word to the UI.
|
|
1232
|
+
if (rawName === "MiniMax-M*" || rawName === "general") return "M-series";
|
|
1233
|
+
|
|
1234
|
+
return rawName
|
|
1235
|
+
.replace(/[_-]+/g, " ")
|
|
1236
|
+
.replace(/\s+/g, " ")
|
|
1237
|
+
.trim()
|
|
1238
|
+
.replace(/\b\w/g, (ch) => ch.toUpperCase())
|
|
1239
|
+
.replace(/\bTo\b/g, "to")
|
|
1240
|
+
.replace(/\bTts\b/g, "TTS")
|
|
1241
|
+
.replace(/\bHd\b/g, "HD");
|
|
1242
|
+
}
|
|
1243
|
+
|
|
1244
|
+
function getMiniMaxProvidedPercent(model, snakeKey, camelKey) {
|
|
1245
|
+
if (!model || typeof model !== "object") return null;
|
|
1246
|
+
const raw = model[snakeKey] ?? model[camelKey];
|
|
1247
|
+
if (raw === null || raw === undefined) return null;
|
|
1248
|
+
const num = Number(raw);
|
|
1249
|
+
if (!Number.isFinite(num)) return null;
|
|
1250
|
+
return Math.max(0, Math.min(100, num));
|
|
1251
|
+
}
|
|
1252
|
+
|
|
1253
|
+
function getMiniMaxSessionTotal(model) {
|
|
1254
|
+
return Math.max(0, Number(getMiniMaxField(model, "current_interval_total_count", "currentIntervalTotalCount")) || 0);
|
|
1255
|
+
}
|
|
1256
|
+
|
|
1257
|
+
function getMiniMaxWeeklyTotal(model) {
|
|
1258
|
+
return Math.max(0, Number(getMiniMaxField(model, "current_weekly_total_count", "currentWeeklyTotalCount")) || 0);
|
|
1259
|
+
}
|
|
1260
|
+
|
|
1261
|
+
function hasMiniMaxQuota(model) {
|
|
1262
|
+
// Old format has real count totals; M3-era M-series buckets ship percent-only
|
|
1263
|
+
// (count fields are 0) so accept those too.
|
|
1264
|
+
if (getMiniMaxSessionTotal(model) > 0 || getMiniMaxWeeklyTotal(model) > 0) return true;
|
|
1265
|
+
if (getMiniMaxProvidedPercent(model, "current_interval_remaining_percent", "currentIntervalRemainingPercent") !== null) return true;
|
|
1266
|
+
if (getMiniMaxProvidedPercent(model, "current_weekly_remaining_percent", "currentWeeklyRemainingPercent") !== null) return true;
|
|
1267
|
+
return false;
|
|
1268
|
+
}
|
|
1269
|
+
|
|
1270
|
+
function getMiniMaxResetAt(model, capturedAtMs, remainsSnake, remainsCamel, endSnake, endCamel) {
|
|
1271
|
+
const remainsMs = Number(getMiniMaxField(model, remainsSnake, remainsCamel)) || 0;
|
|
1272
|
+
if (remainsMs > 0) return new Date(capturedAtMs + remainsMs).toISOString();
|
|
1273
|
+
return parseResetTime(getMiniMaxField(model, endSnake, endCamel));
|
|
1274
|
+
}
|
|
1275
|
+
|
|
1276
|
+
function buildMiniMaxQuota(total, count, resetAt, countMeansRemaining, providedPercent = null) {
|
|
1277
|
+
const safeTotal = Math.max(0, total);
|
|
1278
|
+
const used = countMeansRemaining ? Math.max(safeTotal - count, 0) : Math.min(Math.max(0, count), safeTotal);
|
|
1279
|
+
const remaining = Math.max(safeTotal - used, 0);
|
|
1280
|
+
// M-series buckets ship percent-only (count = 0). Prefer the upstream value
|
|
1281
|
+
// when present, otherwise fall back to the computed percentage. When the
|
|
1282
|
+
// quota is unbounded (no count) and no upstream percent is available, surface
|
|
1283
|
+
// the percent anyway as long as it is defined.
|
|
1284
|
+
const remainingPercentage = providedPercentage(providedPercent, remaining, safeTotal);
|
|
1285
|
+
return {
|
|
1286
|
+
used,
|
|
1287
|
+
total: safeTotal,
|
|
1288
|
+
remaining,
|
|
1289
|
+
remainingPercentage,
|
|
1290
|
+
resetAt,
|
|
1291
|
+
unlimited: false,
|
|
1292
|
+
};
|
|
1293
|
+
}
|
|
1294
|
+
|
|
1295
|
+
function providedPercentage(provided, remaining, total) {
|
|
1296
|
+
if (provided !== null && provided !== undefined && Number.isFinite(provided)) {
|
|
1297
|
+
return Math.max(0, Math.min(100, provided));
|
|
1298
|
+
}
|
|
1299
|
+
return total > 0 ? Math.max(0, Math.min(100, (remaining / total) * 100)) : 0;
|
|
1300
|
+
}
|
|
1301
|
+
|
|
1302
|
+
function addMiniMaxQuota(quotas, key, model, getTotal, countSnake, countCamel, percentSnake, percentCamel, resetArgs, countMeansRemaining) {
|
|
1303
|
+
const total = getTotal(model);
|
|
1304
|
+
const providedPercent = getMiniMaxProvidedPercent(model, percentSnake, percentCamel);
|
|
1305
|
+
if (total <= 0 && providedPercent === null) return;
|
|
1306
|
+
|
|
1307
|
+
const count = Math.max(0, Number(getMiniMaxField(model, countSnake, countCamel)) || 0);
|
|
1308
|
+
let effectiveTotal = total;
|
|
1309
|
+
let effectiveCount = count;
|
|
1310
|
+
if (total <= 0) {
|
|
1311
|
+
// M-series bucket: API only ships *_remaining_percent (count = 0). Normalize
|
|
1312
|
+
// to total=100. The downstream buildMiniMaxQuota treats the count as
|
|
1313
|
+
// "used" or "remaining" depending on countMeansRemaining, so the synthetic
|
|
1314
|
+
// count has to match that semantic — otherwise the UI flips the percentage.
|
|
1315
|
+
effectiveTotal = 100;
|
|
1316
|
+
const pct = providedPercent;
|
|
1317
|
+
effectiveCount = countMeansRemaining
|
|
1318
|
+
? Math.round(effectiveTotal * (pct / 100))
|
|
1319
|
+
: Math.round(effectiveTotal * (1 - pct / 100));
|
|
1320
|
+
}
|
|
1321
|
+
quotas[key] = buildMiniMaxQuota(
|
|
1322
|
+
effectiveTotal,
|
|
1323
|
+
effectiveCount,
|
|
1324
|
+
getMiniMaxResetAt(model, ...resetArgs),
|
|
1325
|
+
countMeansRemaining,
|
|
1326
|
+
providedPercent
|
|
1327
|
+
);
|
|
1328
|
+
}
|
|
1329
|
+
|
|
1330
|
+
/**
|
|
1331
|
+
* MiniMax Token Plan / Coding Plan usage
|
|
1332
|
+
*/
|
|
1333
|
+
async function getMiniMaxUsage(apiKey, provider, proxyOptions = null) {
|
|
1334
|
+
if (!apiKey) {
|
|
1335
|
+
return { message: "MiniMax API key not available." };
|
|
1336
|
+
}
|
|
1337
|
+
|
|
1338
|
+
const usageUrls = MINIMAX_USAGE_URLS[provider] || [];
|
|
1339
|
+
let lastErrorMessage = "";
|
|
1340
|
+
|
|
1341
|
+
for (let index = 0; index < usageUrls.length; index += 1) {
|
|
1342
|
+
const usageUrl = usageUrls[index];
|
|
1343
|
+
const canFallback = index < usageUrls.length - 1;
|
|
1344
|
+
|
|
1345
|
+
try {
|
|
1346
|
+
const response = await proxyAwareFetch(usageUrl, {
|
|
1347
|
+
method: "GET",
|
|
1348
|
+
headers: {
|
|
1349
|
+
Authorization: `Bearer ${apiKey}`,
|
|
1350
|
+
Accept: "application/json",
|
|
1351
|
+
"Content-Type": "application/json",
|
|
1352
|
+
},
|
|
1353
|
+
}, proxyOptions);
|
|
1354
|
+
|
|
1355
|
+
const rawText = await response.text();
|
|
1356
|
+
let payload = {};
|
|
1357
|
+
if (rawText) {
|
|
1358
|
+
try { payload = JSON.parse(rawText); } catch { payload = {}; }
|
|
1359
|
+
}
|
|
1360
|
+
|
|
1361
|
+
const baseResp = (payload?.base_resp ?? payload?.baseResp) || {};
|
|
1362
|
+
const apiStatusCode = Number(baseResp.status_code ?? baseResp.statusCode) || 0;
|
|
1363
|
+
const apiStatusMessage = String(baseResp.status_msg ?? baseResp.statusMsg ?? "").trim();
|
|
1364
|
+
const combined = `${apiStatusMessage} ${rawText}`.trim();
|
|
1365
|
+
const authLike = /token plan|coding plan|invalid api key|invalid key|unauthorized|inactive/i;
|
|
1366
|
+
|
|
1367
|
+
if (response.status === 401 || response.status === 403 || apiStatusCode === 1004 || authLike.test(combined)) {
|
|
1368
|
+
return { message: "MiniMax API key invalid or inactive. Use an active Token/Coding Plan key." };
|
|
1369
|
+
}
|
|
1370
|
+
|
|
1371
|
+
if (!response.ok) {
|
|
1372
|
+
lastErrorMessage = `MiniMax usage endpoint error (${response.status})`;
|
|
1373
|
+
if ((response.status === 404 || response.status === 405 || response.status >= 500) && canFallback) continue;
|
|
1374
|
+
return { message: `MiniMax connected. ${lastErrorMessage}` };
|
|
1375
|
+
}
|
|
1376
|
+
|
|
1377
|
+
if (apiStatusCode !== 0) {
|
|
1378
|
+
return { message: `MiniMax connected. ${apiStatusMessage || "Upstream quota API error"}` };
|
|
1379
|
+
}
|
|
1380
|
+
|
|
1381
|
+
const modelRemains = payload?.model_remains ?? payload?.modelRemains;
|
|
1382
|
+
const allModels = Array.isArray(modelRemains) ? modelRemains : [];
|
|
1383
|
+
const quotaModels = allModels.filter(hasMiniMaxQuota);
|
|
1384
|
+
|
|
1385
|
+
if (quotaModels.length === 0) {
|
|
1386
|
+
return { message: "MiniMax connected. No quota data was returned." };
|
|
1387
|
+
}
|
|
1388
|
+
|
|
1389
|
+
const capturedAtMs = Date.now();
|
|
1390
|
+
const countMeansRemaining = usageUrl.includes("/coding_plan/remains");
|
|
1391
|
+
const quotas = {};
|
|
1392
|
+
|
|
1393
|
+
for (const model of quotaModels) {
|
|
1394
|
+
const displayName = formatMiniMaxQuotaName(model);
|
|
1395
|
+
addMiniMaxQuota(
|
|
1396
|
+
quotas,
|
|
1397
|
+
`${displayName} (5h)`,
|
|
1398
|
+
model,
|
|
1399
|
+
getMiniMaxSessionTotal,
|
|
1400
|
+
"current_interval_usage_count",
|
|
1401
|
+
"currentIntervalUsageCount",
|
|
1402
|
+
"current_interval_remaining_percent",
|
|
1403
|
+
"currentIntervalRemainingPercent",
|
|
1404
|
+
[capturedAtMs, "remains_time", "remainsTime", "end_time", "endTime"],
|
|
1405
|
+
countMeansRemaining
|
|
1406
|
+
);
|
|
1407
|
+
|
|
1408
|
+
addMiniMaxQuota(
|
|
1409
|
+
quotas,
|
|
1410
|
+
`${displayName} (7d)`,
|
|
1411
|
+
model,
|
|
1412
|
+
getMiniMaxWeeklyTotal,
|
|
1413
|
+
"current_weekly_usage_count",
|
|
1414
|
+
"currentWeeklyUsageCount",
|
|
1415
|
+
"current_weekly_remaining_percent",
|
|
1416
|
+
"currentWeeklyRemainingPercent",
|
|
1417
|
+
[capturedAtMs, "weekly_remains_time", "weeklyRemainsTime", "weekly_end_time", "weeklyEndTime"],
|
|
1418
|
+
countMeansRemaining
|
|
1419
|
+
);
|
|
1420
|
+
}
|
|
1421
|
+
|
|
1422
|
+
if (Object.keys(quotas).length === 0) {
|
|
1423
|
+
return { message: "MiniMax connected. Unable to extract quota usage." };
|
|
1424
|
+
}
|
|
1425
|
+
|
|
1426
|
+
return { quotas };
|
|
1427
|
+
} catch (error) {
|
|
1428
|
+
lastErrorMessage = error.message;
|
|
1429
|
+
if (!canFallback) break;
|
|
1430
|
+
}
|
|
1431
|
+
}
|
|
1432
|
+
|
|
1433
|
+
return { message: lastErrorMessage ? `MiniMax connected. Unable to fetch usage: ${lastErrorMessage}` : "MiniMax connected. Unable to fetch usage." };
|
|
1434
|
+
}
|
|
1435
|
+
|
|
1436
|
+
async function getQoderUsage(accessToken, proxyOptions = null) {
|
|
1437
|
+
if (!accessToken) {
|
|
1438
|
+
return { message: "Qoder usage unavailable: no access token" };
|
|
1439
|
+
}
|
|
1440
|
+
try {
|
|
1441
|
+
const response = await proxyAwareFetch(
|
|
1442
|
+
"https://openapi.qoder.sh/api/v2/quota/usage",
|
|
1443
|
+
{
|
|
1444
|
+
method: "GET",
|
|
1445
|
+
headers: {
|
|
1446
|
+
Authorization: `Bearer ${accessToken}`,
|
|
1447
|
+
Accept: "application/json",
|
|
1448
|
+
},
|
|
1449
|
+
},
|
|
1450
|
+
proxyOptions,
|
|
1451
|
+
);
|
|
1452
|
+
if (!response.ok) {
|
|
1453
|
+
return { message: `Qoder connected. Usage fetch returned ${response.status}.` };
|
|
1454
|
+
}
|
|
1455
|
+
const body = await response.json().catch(() => null);
|
|
1456
|
+
if (!body) {
|
|
1457
|
+
return { message: "Qoder connected. Usage response was not JSON." };
|
|
1458
|
+
}
|
|
1459
|
+
// Quota records live under `quotas`; scalar metadata
|
|
1460
|
+
// (totalUsagePercentage, isQuotaExceeded, expiresAt) are surfaced as
|
|
1461
|
+
// siblings so the dashboard parser doesn't try to render them as rows.
|
|
1462
|
+
const userQuota = body.userQuota || {};
|
|
1463
|
+
const orgQuota = body.orgResourcePackage || {};
|
|
1464
|
+
// Qoder publishes a single absolute reset timestamp (`expiresAt` in ms);
|
|
1465
|
+
// surface it on every quota record as ISO so the table can render
|
|
1466
|
+
// "resets at" alongside used/total.
|
|
1467
|
+
const expiresAtMs = Number.isFinite(Number(body.expiresAt)) && Number(body.expiresAt) > 0
|
|
1468
|
+
? Number(body.expiresAt)
|
|
1469
|
+
: null;
|
|
1470
|
+
const resetAt = expiresAtMs ? new Date(expiresAtMs).toISOString() : null;
|
|
1471
|
+
const quotas = {
|
|
1472
|
+
user: {
|
|
1473
|
+
total: Number(userQuota.total) || 0,
|
|
1474
|
+
used: Number(userQuota.used) || 0,
|
|
1475
|
+
remaining: Number(userQuota.remaining) || 0,
|
|
1476
|
+
unit: userQuota.unit || "credits",
|
|
1477
|
+
resetAt,
|
|
1478
|
+
},
|
|
1479
|
+
organization: {
|
|
1480
|
+
total: Number(orgQuota.total) || 0,
|
|
1481
|
+
used: Number(orgQuota.used) || 0,
|
|
1482
|
+
remaining: Number(orgQuota.remaining) || 0,
|
|
1483
|
+
unit: orgQuota.unit || "credits",
|
|
1484
|
+
resetAt,
|
|
1485
|
+
},
|
|
1486
|
+
};
|
|
1487
|
+
return {
|
|
1488
|
+
quotas,
|
|
1489
|
+
totalUsagePercentage: Number(body.totalUsagePercentage) || 0,
|
|
1490
|
+
isQuotaExceeded: !!body.isQuotaExceeded,
|
|
1491
|
+
expiresAt: expiresAtMs,
|
|
1492
|
+
};
|
|
1493
|
+
} catch (error) {
|
|
1494
|
+
return { message: `Qoder connected. Unable to fetch usage: ${error.message}` };
|
|
1495
|
+
}
|
|
1496
|
+
}
|