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,450 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* QoderExecutor — sends OpenAI-format chat requests to Qoder's COSY-signed
|
|
3
|
+
* inference endpoint at api3.qoder.sh, then unwraps Qoder's `{statusCodeValue,
|
|
4
|
+
* body}` SSE envelope back into plain OpenAI SSE for the rest of the pipeline.
|
|
5
|
+
*
|
|
6
|
+
* Differences vs the previous placeholder:
|
|
7
|
+
* - URL is api3.qoder.sh/algo/api/v2/service/pro/sse/agent_chat_generation
|
|
8
|
+
* with `&Encode=1` so we can ship the body through the WAF-bypass
|
|
9
|
+
* encoder.
|
|
10
|
+
* - Authentication is COSY (RSA + AES + MD5 + ~17 Cosy-* headers), not
|
|
11
|
+
* a static HMAC.
|
|
12
|
+
* - The request shape Qoder expects is non-trivial (chat_context with
|
|
13
|
+
* mirrored modelConfig, business block with stable IDs, system text
|
|
14
|
+
* hoisted out of the messages array). All ported from the reference.
|
|
15
|
+
* - Model identifier is one of the canonical Qoder keys (auto / ultimate /
|
|
16
|
+
* performance / efficient / lite + frontier "*model" ids); the
|
|
17
|
+
* translator layer feeds us "qoder/<key>" so we strip the prefix.
|
|
18
|
+
* - Per-model `model_config` is fetched live from /algo/api/v2/model/list
|
|
19
|
+
* and cached. Sending the wrong block silently downgrades to a
|
|
20
|
+
* different model upstream, so a missing entry is a hard error.
|
|
21
|
+
*/
|
|
22
|
+
|
|
23
|
+
import { qoderEncodeBody } from "@/lib/qoder/encoding.js";
|
|
24
|
+
import { buildCosyHeaders } from "@/lib/qoder/cosy.js";
|
|
25
|
+
import { v4 as uuidv4 } from "uuid";
|
|
26
|
+
import { createHash } from "crypto";
|
|
27
|
+
|
|
28
|
+
import { BaseExecutor } from "./base.js";
|
|
29
|
+
import { PROVIDERS } from "../config/providers.js";
|
|
30
|
+
import { proxyAwareFetch } from "../utils/proxyFetch.js";
|
|
31
|
+
import {
|
|
32
|
+
QODER_CHAT_URL_ENCODED,
|
|
33
|
+
QODER_MODEL_MAP,
|
|
34
|
+
} from "@/lib/qoder/constants.js";
|
|
35
|
+
import { getQoderModelConfig, resolveQoderModels } from "../services/qoderModels.js";
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Hoist role:"system" messages out of the messages array (Qoder rejects
|
|
39
|
+
* system in messages) and flatten any multipart content arrays.
|
|
40
|
+
*/
|
|
41
|
+
function normalizeMessages(messages) {
|
|
42
|
+
if (!Array.isArray(messages) || messages.length === 0) {
|
|
43
|
+
return { messages: [], systemText: "" };
|
|
44
|
+
}
|
|
45
|
+
const systemParts = [];
|
|
46
|
+
const out = [];
|
|
47
|
+
for (const msg of messages) {
|
|
48
|
+
if (!msg || typeof msg !== "object") continue;
|
|
49
|
+
const text = extractText(msg.content);
|
|
50
|
+
if (msg.role === "system") {
|
|
51
|
+
if (text) systemParts.push(text);
|
|
52
|
+
continue;
|
|
53
|
+
}
|
|
54
|
+
const cloned = { ...msg };
|
|
55
|
+
cloned.content = text;
|
|
56
|
+
out.push(cloned);
|
|
57
|
+
}
|
|
58
|
+
return { messages: out, systemText: systemParts.join("\n\n") };
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
function extractText(content) {
|
|
62
|
+
if (typeof content === "string") return content;
|
|
63
|
+
if (content == null) return "";
|
|
64
|
+
if (Array.isArray(content)) {
|
|
65
|
+
const parts = [];
|
|
66
|
+
for (const item of content) {
|
|
67
|
+
if (item && typeof item === "object") {
|
|
68
|
+
if (item.type === "text" && typeof item.text === "string") {
|
|
69
|
+
parts.push(item.text);
|
|
70
|
+
} else if (typeof item.text === "string") {
|
|
71
|
+
parts.push(item.text);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
return parts.join("\n");
|
|
76
|
+
}
|
|
77
|
+
return String(content);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
function lastUserText(messages) {
|
|
81
|
+
for (let i = messages.length - 1; i >= 0; i--) {
|
|
82
|
+
const m = messages[i];
|
|
83
|
+
if (m?.role === "user" && typeof m.content === "string") {
|
|
84
|
+
return m.content;
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
return "";
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
function stableHash(prefix, ...parts) {
|
|
91
|
+
const h = createHash("sha256");
|
|
92
|
+
h.update(prefix);
|
|
93
|
+
for (const p of parts) {
|
|
94
|
+
h.update("\0");
|
|
95
|
+
h.update(String(p ?? ""));
|
|
96
|
+
}
|
|
97
|
+
return h.digest("hex").slice(0, 16);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
function stableChatRecordId(model, messages, tools, maxTokens) {
|
|
101
|
+
const h = createHash("sha256");
|
|
102
|
+
h.update("qoder-record\0");
|
|
103
|
+
h.update(String(model));
|
|
104
|
+
for (const m of messages) {
|
|
105
|
+
if (!m || typeof m !== "object") continue;
|
|
106
|
+
if (m.role) { h.update("\0"); h.update(m.role); }
|
|
107
|
+
if (typeof m.content === "string" && m.content) {
|
|
108
|
+
h.update("\0"); h.update(m.content);
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
if (tools) {
|
|
112
|
+
h.update("\0");
|
|
113
|
+
try { h.update(JSON.stringify(tools)); } catch {}
|
|
114
|
+
}
|
|
115
|
+
h.update(`\0mt=${maxTokens}`);
|
|
116
|
+
return h.digest("hex").slice(0, 16);
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
function truncate(s, n) {
|
|
120
|
+
return s && s.length > n ? `${s.slice(0, n)}...` : s || "";
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* Map the OpenAI-style request body into the exact shape Qoder expects.
|
|
125
|
+
*/
|
|
126
|
+
async function buildQoderRequestBody({ model, body, credentials, log, proxyOptions, signal }) {
|
|
127
|
+
const qoderKey = String(model || "").replace(/^qoder\//, "");
|
|
128
|
+
|
|
129
|
+
// Fetch model config from dynamic API instead of relying on static QODER_MODEL_MAP.
|
|
130
|
+
// This allows support for new Qoder models (e.g., qmodel_latest) without code changes.
|
|
131
|
+
let modelConfig = await getQoderModelConfig(credentials, qoderKey, { log, proxyOptions, signal });
|
|
132
|
+
if (!modelConfig) {
|
|
133
|
+
// Try a forced refresh once before giving up — the cache may simply
|
|
134
|
+
// not be populated yet on first ever call for this credential.
|
|
135
|
+
const refreshed = await resolveQoderModels(credentials, { forceRefresh: true, log, proxyOptions, signal });
|
|
136
|
+
const retried = refreshed?.rawConfigs.get(qoderKey);
|
|
137
|
+
if (!retried) {
|
|
138
|
+
throw new Error(
|
|
139
|
+
`qoder: model_config for "${qoderKey}" not yet known (run a model list fetch or check upstream connectivity)`,
|
|
140
|
+
);
|
|
141
|
+
}
|
|
142
|
+
modelConfig = { ...retried, key: qoderKey };
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
const { messages, systemText } = normalizeMessages(body.messages || []);
|
|
146
|
+
const tools = body.tools;
|
|
147
|
+
const isReasoning = !!modelConfig.is_reasoning;
|
|
148
|
+
const maxOutputTokens = Number(modelConfig.max_output_tokens) || 0;
|
|
149
|
+
|
|
150
|
+
let maxTokens = 32_768;
|
|
151
|
+
if (maxOutputTokens > 0) maxTokens = maxOutputTokens;
|
|
152
|
+
if (typeof body.max_tokens === "number" && body.max_tokens > 0 && body.max_tokens < maxTokens) {
|
|
153
|
+
maxTokens = body.max_tokens;
|
|
154
|
+
}
|
|
155
|
+
if (typeof body.max_completion_tokens === "number" && body.max_completion_tokens > 0 && body.max_completion_tokens < maxTokens) {
|
|
156
|
+
maxTokens = body.max_completion_tokens;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
const lastUser = lastUserText(messages);
|
|
160
|
+
const psd = credentials.providerSpecificData || {};
|
|
161
|
+
const sessionId = stableHash("qoder-session", psd.userId, qoderKey);
|
|
162
|
+
const recordId = stableChatRecordId(qoderKey, messages, tools, maxTokens);
|
|
163
|
+
|
|
164
|
+
return {
|
|
165
|
+
qoderKey,
|
|
166
|
+
payload: {
|
|
167
|
+
request_id: uuidv4(),
|
|
168
|
+
request_set_id: recordId,
|
|
169
|
+
chat_record_id: recordId,
|
|
170
|
+
session_id: sessionId,
|
|
171
|
+
stream: true,
|
|
172
|
+
chat_task: "FREE_INPUT",
|
|
173
|
+
is_reply: true,
|
|
174
|
+
is_retry: false,
|
|
175
|
+
source: 1,
|
|
176
|
+
version: "3",
|
|
177
|
+
session_type: "qodercli",
|
|
178
|
+
agent_id: "agent_common",
|
|
179
|
+
task_id: "common",
|
|
180
|
+
code_language: "",
|
|
181
|
+
chat_prompt: "",
|
|
182
|
+
image_urls: null,
|
|
183
|
+
aliyun_user_type: "",
|
|
184
|
+
system: systemText,
|
|
185
|
+
messages,
|
|
186
|
+
tools: Array.isArray(tools) ? tools : [],
|
|
187
|
+
parameters: { max_tokens: maxTokens },
|
|
188
|
+
chat_context: {
|
|
189
|
+
chatPrompt: "",
|
|
190
|
+
imageUrls: null,
|
|
191
|
+
extra: {
|
|
192
|
+
context: [],
|
|
193
|
+
modelConfig: { key: qoderKey, is_reasoning: isReasoning },
|
|
194
|
+
originalContent: lastUser,
|
|
195
|
+
},
|
|
196
|
+
features: [],
|
|
197
|
+
text: lastUser,
|
|
198
|
+
},
|
|
199
|
+
model_config: modelConfig,
|
|
200
|
+
business: {
|
|
201
|
+
product: "cli",
|
|
202
|
+
version: "1.0.0",
|
|
203
|
+
type: "agent",
|
|
204
|
+
stage: "start",
|
|
205
|
+
id: uuidv4(),
|
|
206
|
+
name: truncate(lastUser, 30),
|
|
207
|
+
begin_at: Date.now(),
|
|
208
|
+
},
|
|
209
|
+
},
|
|
210
|
+
modelConfig,
|
|
211
|
+
};
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
/**
|
|
215
|
+
* Wrap the upstream's `{statusCodeValue, body}` SSE envelope into plain
|
|
216
|
+
* OpenAI SSE chunks the rest of the chatCore pipeline understands.
|
|
217
|
+
*
|
|
218
|
+
* Each upstream line looks like:
|
|
219
|
+
* data: {"statusCodeValue":200,"body":"{\"choices\":[{\"delta\":{...}}]}"}
|
|
220
|
+
* The inner body is an OpenAI streaming chunk (or "[DONE]"). We unwrap it
|
|
221
|
+
* and re-emit as `data: <inner>\n\n`. Errors become `data: [DONE]\n\n` plus
|
|
222
|
+
* a synthetic OpenAI error chunk.
|
|
223
|
+
*/
|
|
224
|
+
function wrapQoderSSE(response, model) {
|
|
225
|
+
if (!response.ok || !response.body) return response;
|
|
226
|
+
|
|
227
|
+
const decoder = new TextDecoder();
|
|
228
|
+
const encoder = new TextEncoder();
|
|
229
|
+
let buffer = "";
|
|
230
|
+
let doneEmitted = false;
|
|
231
|
+
|
|
232
|
+
// Process one already-extracted SSE line (no trailing newline). Returns
|
|
233
|
+
// false when the line indicated end-of-stream so the caller can stop
|
|
234
|
+
// forwarding any remaining chunks after [DONE].
|
|
235
|
+
const processLine = (line, controller) => {
|
|
236
|
+
const trimmed = line.replace(/\r$/, "").trim();
|
|
237
|
+
if (!trimmed) return;
|
|
238
|
+
if (!trimmed.startsWith("data:")) return;
|
|
239
|
+
if (doneEmitted) return; // never forward chunks past stream end
|
|
240
|
+
|
|
241
|
+
const data = trimmed.slice(5).trimStart();
|
|
242
|
+
if (data === "[DONE]") {
|
|
243
|
+
controller.enqueue(encoder.encode("data: [DONE]\n\n"));
|
|
244
|
+
doneEmitted = true;
|
|
245
|
+
return;
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
let envelope;
|
|
249
|
+
try { envelope = JSON.parse(data); } catch { return; }
|
|
250
|
+
const statusVal = typeof envelope.statusCodeValue === "number" ? envelope.statusCodeValue : 200;
|
|
251
|
+
const inner = typeof envelope.body === "string" ? envelope.body : "";
|
|
252
|
+
if (statusVal !== 200) {
|
|
253
|
+
const msg = inner || `upstream status ${statusVal}`;
|
|
254
|
+
const errChunk = JSON.stringify({
|
|
255
|
+
id: `qoder-error-${Date.now()}`,
|
|
256
|
+
object: "chat.completion.chunk",
|
|
257
|
+
created: Math.floor(Date.now() / 1000),
|
|
258
|
+
model,
|
|
259
|
+
choices: [{ index: 0, delta: { content: `\n[qoder error ${statusVal}: ${truncate(msg, 200)}]` }, finish_reason: "stop" }],
|
|
260
|
+
});
|
|
261
|
+
controller.enqueue(encoder.encode(`data: ${errChunk}\n\n`));
|
|
262
|
+
controller.enqueue(encoder.encode("data: [DONE]\n\n"));
|
|
263
|
+
doneEmitted = true;
|
|
264
|
+
return;
|
|
265
|
+
}
|
|
266
|
+
if (!inner) return;
|
|
267
|
+
if (inner === "[DONE]") {
|
|
268
|
+
controller.enqueue(encoder.encode("data: [DONE]\n\n"));
|
|
269
|
+
doneEmitted = true;
|
|
270
|
+
return;
|
|
271
|
+
}
|
|
272
|
+
// Inner is an OpenAI-shaped chunk. Strip any embedded newlines so the
|
|
273
|
+
// SSE frame stays a single event (a literal "\n" inside `inner` would
|
|
274
|
+
// otherwise split the frame across multiple data: lines and downstream
|
|
275
|
+
// parsers would reassemble them as separate events).
|
|
276
|
+
const sanitized = inner.replace(/\r?\n/g, "");
|
|
277
|
+
controller.enqueue(encoder.encode(`data: ${sanitized}\n\n`));
|
|
278
|
+
};
|
|
279
|
+
|
|
280
|
+
const transform = new TransformStream({
|
|
281
|
+
transform(chunk, controller) {
|
|
282
|
+
buffer += decoder.decode(chunk, { stream: true });
|
|
283
|
+
let nl;
|
|
284
|
+
while ((nl = buffer.indexOf("\n")) !== -1) {
|
|
285
|
+
const line = buffer.slice(0, nl);
|
|
286
|
+
buffer = buffer.slice(nl + 1);
|
|
287
|
+
processLine(line, controller);
|
|
288
|
+
}
|
|
289
|
+
},
|
|
290
|
+
flush(controller) {
|
|
291
|
+
// Finalize the decoder so any pending multi-byte sequence is
|
|
292
|
+
// released into `buffer` instead of being silently dropped.
|
|
293
|
+
buffer += decoder.decode();
|
|
294
|
+
// Drain any trailing line that arrived without a terminating newline
|
|
295
|
+
// (e.g. upstream closed the socket immediately after the last write,
|
|
296
|
+
// or a CDN stripped the final CRLF). Without this, the chunk that
|
|
297
|
+
// carries finish_reason is silently lost.
|
|
298
|
+
if (buffer.length > 0) {
|
|
299
|
+
processLine(buffer, controller);
|
|
300
|
+
buffer = "";
|
|
301
|
+
}
|
|
302
|
+
if (!doneEmitted) {
|
|
303
|
+
controller.enqueue(encoder.encode("data: [DONE]\n\n"));
|
|
304
|
+
doneEmitted = true;
|
|
305
|
+
}
|
|
306
|
+
},
|
|
307
|
+
});
|
|
308
|
+
|
|
309
|
+
const transformed = response.body.pipeThrough(transform);
|
|
310
|
+
// Build a Response with passable headers; the streaming handler reads
|
|
311
|
+
// `.body` as a ReadableStream regardless of Content-Type.
|
|
312
|
+
return new Response(transformed, {
|
|
313
|
+
status: response.status,
|
|
314
|
+
statusText: response.statusText,
|
|
315
|
+
headers: {
|
|
316
|
+
"Content-Type": "text/event-stream",
|
|
317
|
+
"Cache-Control": "no-cache",
|
|
318
|
+
},
|
|
319
|
+
});
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
export class QoderExecutor extends BaseExecutor {
|
|
323
|
+
constructor() {
|
|
324
|
+
super("qoder", PROVIDERS.qoder);
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
buildUrl() {
|
|
328
|
+
return QODER_CHAT_URL_ENCODED;
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
// Override execute entirely — Qoder needs:
|
|
332
|
+
// - body built from translated chat completion payload
|
|
333
|
+
// - body encoded with QoderEncodeBody before signing
|
|
334
|
+
// - COSY headers built from the *encoded* body bytes
|
|
335
|
+
// - response stream re-wrapped from {statusCodeValue, body} to OpenAI SSE
|
|
336
|
+
async execute({ model, body, stream, credentials, signal, log, proxyOptions = null }) {
|
|
337
|
+
const url = this.buildUrl();
|
|
338
|
+
|
|
339
|
+
const psd = credentials?.providerSpecificData || {};
|
|
340
|
+
if (!psd.userId) {
|
|
341
|
+
// No user id → no way to sign. Surface a 401 so the dashboard nudges
|
|
342
|
+
// the user back to OAuth.
|
|
343
|
+
const fakeResp = new Response(
|
|
344
|
+
JSON.stringify({ error: { message: "qoder credential is missing userId; reconnect the account" } }),
|
|
345
|
+
{ status: 401, headers: { "Content-Type": "application/json" } },
|
|
346
|
+
);
|
|
347
|
+
return { response: fakeResp, url, headers: {}, transformedBody: body };
|
|
348
|
+
}
|
|
349
|
+
if (!credentials?.accessToken) {
|
|
350
|
+
// Same shape as the userId guard — clean 401 so chatCore reports
|
|
351
|
+
// "reconnect" rather than bubbling cosy.js's synchronous throw as 500.
|
|
352
|
+
const fakeResp = new Response(
|
|
353
|
+
JSON.stringify({ error: { message: "qoder credential is missing accessToken; reconnect the account" } }),
|
|
354
|
+
{ status: 401, headers: { "Content-Type": "application/json" } },
|
|
355
|
+
);
|
|
356
|
+
return { response: fakeResp, url, headers: {}, transformedBody: body };
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
let qoderKey;
|
|
360
|
+
let payload;
|
|
361
|
+
try {
|
|
362
|
+
({ qoderKey, payload } = await buildQoderRequestBody({ model, body, credentials, log, proxyOptions, signal }));
|
|
363
|
+
} catch (err) {
|
|
364
|
+
const fakeResp = new Response(
|
|
365
|
+
JSON.stringify({ error: { message: err.message } }),
|
|
366
|
+
{ status: 400, headers: { "Content-Type": "application/json" } },
|
|
367
|
+
);
|
|
368
|
+
return { response: fakeResp, url, headers: {}, transformedBody: body };
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
const plainBody = Buffer.from(JSON.stringify(payload), "utf8");
|
|
372
|
+
const encodedBodyStr = qoderEncodeBody(plainBody);
|
|
373
|
+
const encodedBodyBuf = Buffer.from(encodedBodyStr, "latin1");
|
|
374
|
+
|
|
375
|
+
let cosyHeaders;
|
|
376
|
+
try {
|
|
377
|
+
cosyHeaders = buildCosyHeaders(
|
|
378
|
+
encodedBodyBuf,
|
|
379
|
+
url,
|
|
380
|
+
{
|
|
381
|
+
userId: psd.userId,
|
|
382
|
+
authToken: credentials.accessToken,
|
|
383
|
+
name: credentials.displayName || "",
|
|
384
|
+
email: credentials.email || "",
|
|
385
|
+
machineId: psd.machineId || "",
|
|
386
|
+
},
|
|
387
|
+
);
|
|
388
|
+
} catch (err) {
|
|
389
|
+
// cosy.js throws synchronously on missing userId/authToken — surface
|
|
390
|
+
// as 401 so chatCore prompts re-auth instead of returning a 500.
|
|
391
|
+
const fakeResp = new Response(
|
|
392
|
+
JSON.stringify({ error: { message: `qoder cosy signing failed: ${err.message}` } }),
|
|
393
|
+
{ status: 401, headers: { "Content-Type": "application/json" } },
|
|
394
|
+
);
|
|
395
|
+
return { response: fakeResp, url, headers: {}, transformedBody: body };
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
const modelSource = (payload.model_config && payload.model_config.source) || "system";
|
|
399
|
+
const headers = {
|
|
400
|
+
"Content-Type": "application/json",
|
|
401
|
+
Accept: "text/event-stream",
|
|
402
|
+
"Cache-Control": "no-cache",
|
|
403
|
+
"X-Model-Key": qoderKey,
|
|
404
|
+
"X-Model-Source": modelSource,
|
|
405
|
+
// gzip triggers signature validation on Qoder's CDN; force identity.
|
|
406
|
+
"Accept-Encoding": "identity",
|
|
407
|
+
...cosyHeaders,
|
|
408
|
+
};
|
|
409
|
+
|
|
410
|
+
let response;
|
|
411
|
+
try {
|
|
412
|
+
response = await proxyAwareFetch(
|
|
413
|
+
url,
|
|
414
|
+
{ method: "POST", headers, body: encodedBodyBuf, signal },
|
|
415
|
+
proxyOptions,
|
|
416
|
+
);
|
|
417
|
+
} catch (err) {
|
|
418
|
+
throw err;
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
if (!response.ok) {
|
|
422
|
+
// Pass error response through unchanged so chatCore can capture it.
|
|
423
|
+
return { response, url, headers, transformedBody: payload };
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
const wrapped = wrapQoderSSE(response, `qoder/${qoderKey}`);
|
|
427
|
+
return { response: wrapped, url, headers, transformedBody: payload };
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
// Qoder device tokens don't refresh through OAuth — the upstream returns
|
|
431
|
+
// 403 for our flow. Surfacing failure via 401-on-chat is enough; the
|
|
432
|
+
// dashboard tells users to re-login when their token expires (~30 days).
|
|
433
|
+
async refreshCredentials() {
|
|
434
|
+
return null;
|
|
435
|
+
}
|
|
436
|
+
|
|
437
|
+
needsRefresh() {
|
|
438
|
+
return false;
|
|
439
|
+
}
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
export default QoderExecutor;
|
|
443
|
+
|
|
444
|
+
// Internals exposed for unit tests. Not part of the public API — callers
|
|
445
|
+
// should import QoderExecutor and use its public methods.
|
|
446
|
+
export const __test__ = {
|
|
447
|
+
normalizeMessages,
|
|
448
|
+
wrapQoderSSE,
|
|
449
|
+
buildQoderRequestBody,
|
|
450
|
+
};
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
import { DefaultExecutor } from "./default.js";
|
|
2
|
+
import { PROVIDERS } from "../config/providers.js";
|
|
3
|
+
import { OAUTH_ENDPOINTS } from "../config/appConstants.js";
|
|
4
|
+
|
|
5
|
+
/** portal.qwen.ai — static fingerprint matching stable Qwen Code release */
|
|
6
|
+
const QWEN_USER_AGENT = "QwenCode/0.12.3 (linux; x64)";
|
|
7
|
+
const QWEN_STAINLESS = {
|
|
8
|
+
os: "Linux",
|
|
9
|
+
arch: "x64",
|
|
10
|
+
lang: "js",
|
|
11
|
+
runtime: "node",
|
|
12
|
+
runtimeVersion: "v18.19.1",
|
|
13
|
+
packageVersion: "5.11.0",
|
|
14
|
+
retryCount: "1"
|
|
15
|
+
};
|
|
16
|
+
const QWEN_DEFAULT_SYSTEM_MESSAGE = {
|
|
17
|
+
role: "system",
|
|
18
|
+
content: [{ type: "text", text: "", cache_control: { type: "ephemeral" } }]
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
function ensureQwenSystemMessage(body) {
|
|
22
|
+
if (!body || typeof body !== "object") return body;
|
|
23
|
+
const next = { ...body };
|
|
24
|
+
if (Array.isArray(next.messages)) {
|
|
25
|
+
next.messages = [QWEN_DEFAULT_SYSTEM_MESSAGE, ...next.messages];
|
|
26
|
+
} else {
|
|
27
|
+
next.messages = [QWEN_DEFAULT_SYSTEM_MESSAGE];
|
|
28
|
+
}
|
|
29
|
+
return next;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function isQwenThinkingActive(body) {
|
|
33
|
+
const thinking = body?.thinking;
|
|
34
|
+
if (thinking === true || body?.enable_thinking === true) return true;
|
|
35
|
+
return typeof thinking === "object" && thinking !== null && !Array.isArray(thinking) && thinking.type === "enabled";
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
// Qwen rejects tool_choice="required" or object forms when thinking is active; neutralize to "auto".
|
|
39
|
+
function sanitizeQwenThinkingToolChoice(body) {
|
|
40
|
+
if (!isQwenThinkingActive(body)) return body;
|
|
41
|
+
const tc = body.tool_choice;
|
|
42
|
+
const incompatible = tc === "required" || (typeof tc === "object" && tc !== null);
|
|
43
|
+
if (!incompatible) return body;
|
|
44
|
+
return { ...body, tool_choice: "auto" };
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function buildQwenUpstreamHeaders(credentials, stream = true) {
|
|
48
|
+
const token = credentials?.apiKey || credentials?.accessToken || "";
|
|
49
|
+
const headers = {
|
|
50
|
+
"Content-Type": "application/json",
|
|
51
|
+
Authorization: `Bearer ${token}`,
|
|
52
|
+
"User-Agent": QWEN_USER_AGENT,
|
|
53
|
+
"X-DashScope-AuthType": "qwen-oauth",
|
|
54
|
+
"X-DashScope-CacheControl": "enable",
|
|
55
|
+
"X-DashScope-UserAgent": QWEN_USER_AGENT,
|
|
56
|
+
"X-Stainless-Arch": QWEN_STAINLESS.arch,
|
|
57
|
+
"X-Stainless-Lang": QWEN_STAINLESS.lang,
|
|
58
|
+
"X-Stainless-Os": QWEN_STAINLESS.os,
|
|
59
|
+
"X-Stainless-Package-Version": QWEN_STAINLESS.packageVersion,
|
|
60
|
+
"X-Stainless-Retry-Count": QWEN_STAINLESS.retryCount,
|
|
61
|
+
"X-Stainless-Runtime": QWEN_STAINLESS.runtime,
|
|
62
|
+
"X-Stainless-Runtime-Version": QWEN_STAINLESS.runtimeVersion,
|
|
63
|
+
Connection: "keep-alive",
|
|
64
|
+
"Accept-Language": "*",
|
|
65
|
+
"Sec-Fetch-Mode": "cors"
|
|
66
|
+
};
|
|
67
|
+
headers.Accept = stream ? "text/event-stream" : "application/json";
|
|
68
|
+
return headers;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
export class QwenExecutor extends DefaultExecutor {
|
|
72
|
+
constructor() {
|
|
73
|
+
super("qwen");
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
// Qwen tokens are bound to a resource_url returned at OAuth time.
|
|
77
|
+
// Using portal.qwen.ai when the token is issued for another shard returns 401/403.
|
|
78
|
+
buildUrl(model, stream, urlIndex = 0, credentials = null) {
|
|
79
|
+
const resourceUrl = credentials?.providerSpecificData?.resourceUrl;
|
|
80
|
+
const host = resourceUrl ? resourceUrl.replace(/^https?:\/\//, "").replace(/\/$/, "") : "portal.qwen.ai";
|
|
81
|
+
return `https://${host}/v1/chat/completions`;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
buildHeaders(credentials, stream = true) {
|
|
85
|
+
return buildQwenUpstreamHeaders(credentials, stream);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
transformRequest(model, body, stream, credentials) {
|
|
89
|
+
let next = body && typeof body === "object" ? { ...body } : body;
|
|
90
|
+
if (stream && next?.messages && !next.stream_options && !next.thinking && !next.enable_thinking && next.stream !== false) {
|
|
91
|
+
next.stream_options = { include_usage: true };
|
|
92
|
+
}
|
|
93
|
+
next = sanitizeQwenThinkingToolChoice(next);
|
|
94
|
+
return ensureQwenSystemMessage(next);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
// Override to capture resource_url from refresh response (required for buildUrl).
|
|
98
|
+
async refreshCredentials(credentials, log) {
|
|
99
|
+
if (!credentials?.refreshToken) return null;
|
|
100
|
+
try {
|
|
101
|
+
const response = await fetch(OAUTH_ENDPOINTS.qwen.token, {
|
|
102
|
+
method: "POST",
|
|
103
|
+
headers: { "Content-Type": "application/x-www-form-urlencoded", Accept: "application/json" },
|
|
104
|
+
body: new URLSearchParams({
|
|
105
|
+
grant_type: "refresh_token",
|
|
106
|
+
refresh_token: credentials.refreshToken,
|
|
107
|
+
client_id: PROVIDERS.qwen.clientId
|
|
108
|
+
})
|
|
109
|
+
});
|
|
110
|
+
if (!response.ok) return null;
|
|
111
|
+
const tokens = await response.json();
|
|
112
|
+
log?.info?.("TOKEN", "qwen refreshed");
|
|
113
|
+
return {
|
|
114
|
+
accessToken: tokens.access_token,
|
|
115
|
+
refreshToken: tokens.refresh_token || credentials.refreshToken,
|
|
116
|
+
expiresIn: tokens.expires_in,
|
|
117
|
+
providerSpecificData: {
|
|
118
|
+
...(credentials.providerSpecificData || {}),
|
|
119
|
+
...(tokens.resource_url ? { resourceUrl: tokens.resource_url } : {})
|
|
120
|
+
}
|
|
121
|
+
};
|
|
122
|
+
} catch (error) {
|
|
123
|
+
log?.error?.("TOKEN", `qwen refresh error: ${error.message}`);
|
|
124
|
+
return null;
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
export default QwenExecutor;
|