swayrouter 1.0.0
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/.env.example +43 -0
- package/CHANGELOG.md +132 -0
- package/LICENSE +30 -0
- package/README.id.md +633 -0
- package/README.md +628 -0
- package/cli/cli.js +388 -0
- package/cli/package.json +4 -0
- package/cli/src/cli/commands/xaiVideo.js +280 -0
- package/cli/src/cli/tray/SwayAvatar.svg +20 -0
- package/cli/src/cli/tray/autostart.js +351 -0
- package/cli/src/cli/tray/icon.ico +0 -0
- package/cli/src/cli/tray/icon.png +0 -0
- package/cli/src/cli/tray/tray.js +258 -0
- package/cli/src/cli/tray/tray.ps1 +129 -0
- package/cli/src/cli/tray/trayHost.js +157 -0
- package/cli/src/cli/tray/trayWin.js +83 -0
- package/cli/src/cli/utils/display.js +123 -0
- package/cli/src/cli/utils/input.js +138 -0
- package/cli/src/runtime/discover.js +153 -0
- package/cli/src/runtime/lifecycle.js +333 -0
- package/cli/src/runtime/runMenu.js +250 -0
- package/cli/src/runtime/uiAdapters.js +12 -0
- package/cli/src/runtime/update.js +130 -0
- package/dashboard/dist/assets/CliTools-Ch3gKuNw.js +24 -0
- package/dashboard/dist/assets/Combo-BBCldjWi.js +1 -0
- package/dashboard/dist/assets/ConsoleLog-wywAYcPT.js +6 -0
- package/dashboard/dist/assets/Dashboard-DxzyF41b.js +1 -0
- package/dashboard/dist/assets/Header-C_9Su9BF.js +1 -0
- package/dashboard/dist/assets/Keys-D0ufgcsc.js +1 -0
- package/dashboard/dist/assets/Login-BeKpipS_.js +1 -0
- package/dashboard/dist/assets/ManageApiKey-CVXsmW2B.js +1 -0
- package/dashboard/dist/assets/OAuthCallback-D_UZYcF6.js +1 -0
- package/dashboard/dist/assets/Provider-BU7JfTqm.js +4 -0
- package/dashboard/dist/assets/ProviderModelAccordion-BAifkfVR.js +1 -0
- package/dashboard/dist/assets/Proxy-CU15VJ--.js +1 -0
- package/dashboard/dist/assets/QuotaMonitor-HzGK7bcc.js +1 -0
- package/dashboard/dist/assets/Settings-Dt4y2dC0.js +1 -0
- package/dashboard/dist/assets/Setup-DSDUwU7N.js +1 -0
- package/dashboard/dist/assets/StatusBadge-1nAOyHU2.js +1 -0
- package/dashboard/dist/assets/SwayChat-Bup7up6D.js +10 -0
- package/dashboard/dist/assets/Usage-eWnrPQ_v.js +4 -0
- package/dashboard/dist/assets/accordion-DOOPDDgt.js +1 -0
- package/dashboard/dist/assets/admin-extras-api-CCdu5TFM.js +1 -0
- package/dashboard/dist/assets/api-keys-api-ifAC2Ok3.js +1 -0
- package/dashboard/dist/assets/card-CC0Omjcv.js +1 -0
- package/dashboard/dist/assets/chatStudio-C0zPz_Z_.js +2 -0
- package/dashboard/dist/assets/checkbox-7SZBrmbN.js +1 -0
- package/dashboard/dist/assets/connections-api-_N54UD3S.js +1 -0
- package/dashboard/dist/assets/copy-button-qBvM6H9z.js +1 -0
- package/dashboard/dist/assets/currency-api-SV9CBf5e.js +1 -0
- package/dashboard/dist/assets/flip-button-BGgkjtl0.js +1 -0
- package/dashboard/dist/assets/frame-cUiti6Lq.js +1 -0
- package/dashboard/dist/assets/icons-vendor-CUx7SB5V.js +541 -0
- package/dashboard/dist/assets/index-B6j-9zHy.js +10 -0
- package/dashboard/dist/assets/index-C-_l0yYz.css +1 -0
- package/dashboard/dist/assets/live-mode-CHShLY4E.js +1 -0
- package/dashboard/dist/assets/markdown-vendor-BY8e1gQ8.js +29 -0
- package/dashboard/dist/assets/motion-vendor-g5fpGzmJ.js +9 -0
- package/dashboard/dist/assets/oauth-callback-CqYQzyd7.js +1 -0
- package/dashboard/dist/assets/query-vendor-DVevaSVS.js +1 -0
- package/dashboard/dist/assets/react-vendor-xnYrwzsL.js +61 -0
- package/dashboard/dist/assets/ripple-button-l8aMiVVG.js +1 -0
- package/dashboard/dist/assets/segmented-CqmEnQjZ.js +1 -0
- package/dashboard/dist/assets/select-D8pUtMXp.js +1 -0
- package/dashboard/dist/assets/skeleton-7uJDiIXe.js +1 -0
- package/dashboard/dist/assets/table-D0M60Tu1.js +1 -0
- package/dashboard/dist/assets/tabs-C6YkuulG.js +1 -0
- package/dashboard/dist/assets/tunnel-api-DDPsBaDV.js +1 -0
- package/dashboard/dist/assets/ui-vendor-BuM9f2NY.js +17 -0
- package/dashboard/dist/image2/SwayAvatar.svg +20 -0
- package/dashboard/dist/index.html +32 -0
- package/dashboard/dist/logo/avatar.svg +1 -0
- package/dashboard/dist/logo/sway.svg +20 -0
- package/dashboard/dist/logo/swaychat.svg +20 -0
- package/dashboard/dist/providers/SwayCustom.svg +19 -0
- package/dashboard/dist/providers/alicode.svg +12 -0
- package/dashboard/dist/providers/anthropic.svg +11 -0
- package/dashboard/dist/providers/antigravity.svg +3 -0
- package/dashboard/dist/providers/azure.svg +5 -0
- package/dashboard/dist/providers/baidu.svg +3 -0
- package/dashboard/dist/providers/blackbox.svg +5 -0
- package/dashboard/dist/providers/byteplus.svg +3 -0
- package/dashboard/dist/providers/cerebras.svg +4 -0
- package/dashboard/dist/providers/chutes.svg +4 -0
- package/dashboard/dist/providers/claude.svg +3 -0
- package/dashboard/dist/providers/cline.svg +11 -0
- package/dashboard/dist/providers/cloudflare-ai.svg +4 -0
- package/dashboard/dist/providers/codebuddy.svg +11 -0
- package/dashboard/dist/providers/codex.svg +10 -0
- package/dashboard/dist/providers/commandcode.svg +3 -0
- package/dashboard/dist/providers/cursor.svg +3 -0
- package/dashboard/dist/providers/deepseek.svg +10 -0
- package/dashboard/dist/providers/featherless.svg +3 -0
- package/dashboard/dist/providers/fireworks.svg +3 -0
- package/dashboard/dist/providers/gemini.svg +3 -0
- package/dashboard/dist/providers/geminicli.svg +11 -0
- package/dashboard/dist/providers/github.svg +3 -0
- package/dashboard/dist/providers/grok.svg +3 -0
- package/dashboard/dist/providers/groq.svg +3 -0
- package/dashboard/dist/providers/hermesagent.svg +1 -0
- package/dashboard/dist/providers/kilocode.svg +15 -0
- package/dashboard/dist/providers/kimchi.svg +3 -0
- package/dashboard/dist/providers/kimi.svg +4 -0
- package/dashboard/dist/providers/kiro.svg +10 -0
- package/dashboard/dist/providers/meta.svg +3 -0
- package/dashboard/dist/providers/mimo.svg +10 -0
- package/dashboard/dist/providers/minimax.svg +3 -0
- package/dashboard/dist/providers/mistral.svg +3 -0
- package/dashboard/dist/providers/morph.svg +3 -0
- package/dashboard/dist/providers/nebius.svg +4 -0
- package/dashboard/dist/providers/nvidia.svg +3 -0
- package/dashboard/dist/providers/ollama.svg +3 -0
- package/dashboard/dist/providers/omp.svg +5 -0
- package/dashboard/dist/providers/openai.svg +3 -0
- package/dashboard/dist/providers/opencode.svg +3 -0
- package/dashboard/dist/providers/openrouter.svg +3 -0
- package/dashboard/dist/providers/perplexity.svg +3 -0
- package/dashboard/dist/providers/poolside.svg +36 -0
- package/dashboard/dist/providers/qoder.svg +11 -0
- package/dashboard/dist/providers/sumopod.svg +3 -0
- package/dashboard/dist/providers/tencent.svg +5 -0
- package/dashboard/dist/providers/together.svg +12 -0
- package/dashboard/dist/providers/trae.svg +3 -0
- package/dashboard/dist/providers/venice.svg +4 -0
- package/dashboard/dist/providers/vercel.svg +3 -0
- package/dashboard/dist/providers/vertex.svg +17 -0
- package/dashboard/dist/providers/windsurf.svg +10 -0
- package/dashboard/dist/providers/zai.svg +3 -0
- package/dashboard/tsconfig.json +13 -0
- package/package.json +87 -0
- package/src/config/appConstants.js +153 -0
- package/src/config/defaultThinkingSignature.js +7 -0
- package/src/config/errorConfig.js +117 -0
- package/src/config/grokCli.js +9 -0
- package/src/config/kiroConstants.js +235 -0
- package/src/config/mediaConfig.js +20 -0
- package/src/config/ollamaModels.js +18 -0
- package/src/config/providerModels.js +109 -0
- package/src/config/providers.js +17 -0
- package/src/config/runtimeConfig.js +84 -0
- package/src/dashboardGuard.js +254 -0
- package/src/executors/antigravity.js +389 -0
- package/src/executors/azure.js +49 -0
- package/src/executors/base.js +225 -0
- package/src/executors/codebuddy-cn.js +23 -0
- package/src/executors/codebuddy-intl.js +60 -0
- package/src/executors/codex.js +436 -0
- package/src/executors/commandcode.js +83 -0
- package/src/executors/cursor.js +1075 -0
- package/src/executors/default.js +294 -0
- package/src/executors/gemini-cli.js +88 -0
- package/src/executors/github.js +393 -0
- package/src/executors/grok-cli.js +510 -0
- package/src/executors/grok-web.js +342 -0
- package/src/executors/index.js +86 -0
- package/src/executors/kimchi.js +116 -0
- package/src/executors/kiro.js +1155 -0
- package/src/executors/ollama-local.js +14 -0
- package/src/executors/opencode-go.js +47 -0
- package/src/executors/opencode.js +31 -0
- package/src/executors/qoder.js +433 -0
- package/src/executors/trae.js +318 -0
- package/src/executors/vertex.js +151 -0
- package/src/executors/windsurf.js +555 -0
- package/src/executors/xiaomi-tokenplan.js +16 -0
- package/src/handlers/chatCore/nonStreamingHandler.js +370 -0
- package/src/handlers/chatCore/requestDetail.js +130 -0
- package/src/handlers/chatCore/sseToJsonHandler.js +318 -0
- package/src/handlers/chatCore/streamingHandler.js +250 -0
- package/src/handlers/chatCore.js +652 -0
- package/src/lib/auth/dashboardSession.js +83 -0
- package/src/lib/auth/loginLimiter.js +99 -0
- package/src/lib/cache/ttlCache.js +153 -0
- package/src/lib/consoleLogBuffer.js +127 -0
- package/src/lib/dataDir.js +45 -0
- package/src/lib/db/adapters/betterSqliteAdapter.js +36 -0
- package/src/lib/db/adapters/bunSqliteAdapter.js +47 -0
- package/src/lib/db/adapters/nodeSqliteAdapter.js +65 -0
- package/src/lib/db/adapters/runtimeHelpers.js +23 -0
- package/src/lib/db/adapters/sqljsAdapter.js +103 -0
- package/src/lib/db/backup.js +75 -0
- package/src/lib/db/driver.js +110 -0
- package/src/lib/db/helpers/jsonCol.js +18 -0
- package/src/lib/db/helpers/kvStore.js +48 -0
- package/src/lib/db/helpers/metaStore.js +32 -0
- package/src/lib/db/index.js +473 -0
- package/src/lib/db/migrate.js +267 -0
- package/src/lib/db/migrations/001-initial.js +12 -0
- package/src/lib/db/migrations/002-prune-headroom-pxpipe-settings.js +32 -0
- package/src/lib/db/migrations/004-request-details-trace-id.js +12 -0
- package/src/lib/db/migrations/005-request-details-payload-capture.js +11 -0
- package/src/lib/db/migrations/007-headroom-readd-settings.js +35 -0
- package/src/lib/db/migrations/008-account-model-locks.js +22 -0
- package/src/lib/db/migrations/009-warp-pool-lifecycle.js +5 -0
- package/src/lib/db/migrations/010-api-key-file-permissions.js +7 -0
- package/src/lib/db/migrations/011-remove-upstream-user-agent-profile.js +41 -0
- package/src/lib/db/migrations/012-remove-headroom-settings.js +33 -0
- package/src/lib/db/migrations/013-default-fill-first-routing.js +20 -0
- package/src/lib/db/migrations/014-normalize-fill-first-routing.js +37 -0
- package/src/lib/db/migrations/015-remove-ip-ban-tables.js +8 -0
- package/src/lib/db/migrations/016-prune-legacy-codebuddy-intl-models.js +25 -0
- package/src/lib/db/migrations/index.js +20 -0
- package/src/lib/db/nineRouterMigration.js +785 -0
- package/src/lib/db/paths.js +23 -0
- package/src/lib/db/repos/accountModelLocksRepo.js +74 -0
- package/src/lib/db/repos/aliasRepo.js +108 -0
- package/src/lib/db/repos/apiKeysRepo.js +204 -0
- package/src/lib/db/repos/combosRepo.js +85 -0
- package/src/lib/db/repos/connectionsRepo.js +522 -0
- package/src/lib/db/repos/disabledModelsRepo.js +63 -0
- package/src/lib/db/repos/nodesRepo.js +127 -0
- package/src/lib/db/repos/pricingRepo.js +108 -0
- package/src/lib/db/repos/proxyPoolsRepo.js +104 -0
- package/src/lib/db/repos/requestDetailsRepo.js +345 -0
- package/src/lib/db/repos/settingsRepo.js +135 -0
- package/src/lib/db/repos/usageRepo.js +1335 -0
- package/src/lib/db/schema.js +179 -0
- package/src/lib/db/version.js +32 -0
- package/src/lib/db/writeBehind.js +174 -0
- package/src/lib/disabledModelsDb.js +3 -0
- package/src/lib/env.ts +175 -0
- package/src/lib/errors/publicError.js +18 -0
- package/src/lib/grokBuildConfig.js +243 -0
- package/src/lib/guardRequest.js +51 -0
- package/src/lib/localDb.js +21 -0
- package/src/lib/mcp/stdioSseBridge.js +174 -0
- package/src/lib/network/connectionProxy.js +200 -0
- package/src/lib/network/proxyTest.js +158 -0
- package/src/lib/network/proxyUrl.js +76 -0
- package/src/lib/network/relay.js +71 -0
- package/src/lib/oauth/constants/oauth.js +152 -0
- package/src/lib/oauth/constants/xai.js +43 -0
- package/src/lib/oauth/kiroExternalIdp.js +167 -0
- package/src/lib/oauth/providerHelpers.js +90 -0
- package/src/lib/oauth/providers/_shared.js +12 -0
- package/src/lib/oauth/providers/antigravity.js +126 -0
- package/src/lib/oauth/providers/claude.js +60 -0
- package/src/lib/oauth/providers/cline.js +62 -0
- package/src/lib/oauth/providers/clinepass.js +62 -0
- package/src/lib/oauth/providers/codebuddy-cn.js +75 -0
- package/src/lib/oauth/providers/codebuddy-intl.js +73 -0
- package/src/lib/oauth/providers/codex.js +72 -0
- package/src/lib/oauth/providers/cursor.js +18 -0
- package/src/lib/oauth/providers/gemini-cli.js +84 -0
- package/src/lib/oauth/providers/github.js +96 -0
- package/src/lib/oauth/providers/grok-cli.js +123 -0
- package/src/lib/oauth/providers/index.js +233 -0
- package/src/lib/oauth/providers/kilocode.js +60 -0
- package/src/lib/oauth/providers/kimchi.js +79 -0
- package/src/lib/oauth/providers/kimi.js +79 -0
- package/src/lib/oauth/providers/kiro.js +147 -0
- package/src/lib/oauth/providers/qoder.js +90 -0
- package/src/lib/oauth/providers/trae.js +244 -0
- package/src/lib/oauth/providers/windsurf.js +123 -0
- package/src/lib/oauth/providers/xai.js +95 -0
- package/src/lib/oauth/providers.js +16 -0
- package/src/lib/oauth/services/cursor.js +132 -0
- package/src/lib/oauth/services/kiro.js +351 -0
- package/src/lib/oauth/services/oauth.js +135 -0
- package/src/lib/oauth/services/qoder.js +151 -0
- package/src/lib/oauth/services/xai.js +204 -0
- package/src/lib/oauth/utils/ideDetect.js +55 -0
- package/src/lib/oauth/utils/pkce.js +25 -0
- package/src/lib/oauth/utils/server.js +732 -0
- package/src/lib/oauth/utils/ui.js +43 -0
- package/src/lib/providerNormalization.js +40 -0
- package/src/lib/qoder/constants.js +1 -0
- package/src/lib/requestDetailsDb.js +3 -0
- package/src/lib/resourceProfile.ts +124 -0
- package/src/lib/runtimeSecrets.ts +130 -0
- package/src/lib/security/payloadCapture.js +87 -0
- package/src/lib/tunnel/cloudflare/cloudflared.js +436 -0
- package/src/lib/tunnel/cloudflare/config.js +8 -0
- package/src/lib/tunnel/cloudflare/healthCheck.js +37 -0
- package/src/lib/tunnel/cloudflare/manager.js +149 -0
- package/src/lib/tunnel/cloudflare/pid.js +31 -0
- package/src/lib/tunnel/cloudflare/publicUrl.js +41 -0
- package/src/lib/tunnel/cloudflare/requirements.js +12 -0
- package/src/lib/tunnel/index.js +26 -0
- package/src/lib/tunnel/shared/dnsResolver.js +16 -0
- package/src/lib/tunnel/shared/internetCheck.js +30 -0
- package/src/lib/tunnel/shared/state.js +45 -0
- package/src/lib/tunnel/shared/watchdogConfig.js +6 -0
- package/src/lib/update/installer.js +52 -0
- package/src/lib/update/version.js +116 -0
- package/src/lib/usageDb.js +6 -0
- package/src/lib/usageRefresh.js +88 -0
- package/src/lifecycle/appLifecycle.ts +99 -0
- package/src/models.js +36 -0
- package/src/next/headers.js +12 -0
- package/src/next/server.js +74 -0
- package/src/observability/metrics.js +439 -0
- package/src/observability/tracing.js +107 -0
- package/src/open-sse-index.js +70 -0
- package/src/providers/capabilities.js +299 -0
- package/src/providers/display.ts +142 -0
- package/src/providers/index.js +27 -0
- package/src/providers/models/helpers.js +19 -0
- package/src/providers/models/namePatterns.js +27 -0
- package/src/providers/models/schema.js +32 -0
- package/src/providers/pricing.js +248 -0
- package/src/providers/registry/agentrouter.js +52 -0
- package/src/providers/registry/alicode-intl.js +32 -0
- package/src/providers/registry/alicode.js +33 -0
- package/src/providers/registry/alims-intl.js +30 -0
- package/src/providers/registry/anthropic.js +29 -0
- package/src/providers/registry/antigravity.js +139 -0
- package/src/providers/registry/azure.js +23 -0
- package/src/providers/registry/baidu.js +35 -0
- package/src/providers/registry/blackbox.js +41 -0
- package/src/providers/registry/byteplus.js +36 -0
- package/src/providers/registry/cerebras.js +33 -0
- package/src/providers/registry/chutes.js +26 -0
- package/src/providers/registry/claude.js +80 -0
- package/src/providers/registry/cline.js +53 -0
- package/src/providers/registry/clinepass.js +62 -0
- package/src/providers/registry/cloudflare-ai.js +84 -0
- package/src/providers/registry/codebuddy-cn.js +95 -0
- package/src/providers/registry/codebuddy-intl.js +91 -0
- package/src/providers/registry/codex.js +145 -0
- package/src/providers/registry/commandcode.js +45 -0
- package/src/providers/registry/cursor.js +61 -0
- package/src/providers/registry/deepseek.js +55 -0
- package/src/providers/registry/featherless.js +36 -0
- package/src/providers/registry/fireworks.js +34 -0
- package/src/providers/registry/gemini-cli.js +59 -0
- package/src/providers/registry/gemini.js +76 -0
- package/src/providers/registry/github.js +131 -0
- package/src/providers/registry/glm-cn.js +35 -0
- package/src/providers/registry/glm.js +58 -0
- package/src/providers/registry/grok-cli.js +87 -0
- package/src/providers/registry/grok-web.js +41 -0
- package/src/providers/registry/groq.js +38 -0
- package/src/providers/registry/index.js +77 -0
- package/src/providers/registry/kilo-gateway.js +34 -0
- package/src/providers/registry/kilocode.js +48 -0
- package/src/providers/registry/kimchi.js +49 -0
- package/src/providers/registry/kimi.js +82 -0
- package/src/providers/registry/kiro.js +132 -0
- package/src/providers/registry/meta.js +34 -0
- package/src/providers/registry/minimax-cn.js +90 -0
- package/src/providers/registry/minimax.js +90 -0
- package/src/providers/registry/mistral.js +36 -0
- package/src/providers/registry/morph.js +31 -0
- package/src/providers/registry/nebius.js +26 -0
- package/src/providers/registry/nvidia.js +55 -0
- package/src/providers/registry/ollama-local.js +18 -0
- package/src/providers/registry/ollama.js +38 -0
- package/src/providers/registry/openai.js +114 -0
- package/src/providers/registry/opencode-go.js +41 -0
- package/src/providers/registry/opencode.js +26 -0
- package/src/providers/registry/openrouter.js +36 -0
- package/src/providers/registry/perplexity.js +56 -0
- package/src/providers/registry/poolside.js +30 -0
- package/src/providers/registry/qoder.js +58 -0
- package/src/providers/registry/sumopod.js +84 -0
- package/src/providers/registry/tencent.js +29 -0
- package/src/providers/registry/together.js +38 -0
- package/src/providers/registry/trae.js +71 -0
- package/src/providers/registry/venice.js +69 -0
- package/src/providers/registry/vercel-ai-gateway.js +37 -0
- package/src/providers/registry/vertex-partner.js +31 -0
- package/src/providers/registry/vertex.js +33 -0
- package/src/providers/registry/windsurf.js +129 -0
- package/src/providers/registry/xai.js +46 -0
- package/src/providers/registry/xiaomi-mimo.js +66 -0
- package/src/providers/registry/xiaomi-tokenplan.js +58 -0
- package/src/providers/registryEntry.js +3 -0
- package/src/providers/schema.js +33 -0
- package/src/providers/shared.js +114 -0
- package/src/providers/thinkingLevels.js +49 -0
- package/src/routes/api/auth/login/route.js +82 -0
- package/src/routes/api/auth/logout/route.js +9 -0
- package/src/routes/api/auth/reset-password/route.js +12 -0
- package/src/routes/api/auth/status/route.js +33 -0
- package/src/routes/api/auth/verify-password/route.js +39 -0
- package/src/routes/api/chat/tools/curl/route.js +100 -0
- package/src/routes/api/chat/tools/router/route.js +215 -0
- package/src/routes/api/chat/tools/search/route.js +362 -0
- package/src/routes/api/chat/tools/shell/route.js +228 -0
- package/src/routes/api/cli-tools/all-statuses/route.js +37 -0
- package/src/routes/api/cli-tools/claude-settings/route.js +261 -0
- package/src/routes/api/cli-tools/codex-settings/route.js +220 -0
- package/src/routes/api/cli-tools/cowork-mcp-registry/route.js +80 -0
- package/src/routes/api/cli-tools/cowork-mcp-tools/route.js +96 -0
- package/src/routes/api/cli-tools/cowork-settings/route.js +380 -0
- package/src/routes/api/cli-tools/grok-build-settings/route.js +152 -0
- package/src/routes/api/cli-tools/hermes-settings/route.js +169 -0
- package/src/routes/api/cli-tools/kilo-settings/route.js +131 -0
- package/src/routes/api/cli-tools/model-mappings/route.js +43 -0
- package/src/routes/api/cli-tools/omp-settings/route.js +266 -0
- package/src/routes/api/cli-tools/opencode-settings/route.js +243 -0
- package/src/routes/api/combos/[id]/route.js +73 -0
- package/src/routes/api/combos/route.js +43 -0
- package/src/routes/api/health/live/route.js +7 -0
- package/src/routes/api/health/ready/route.js +19 -0
- package/src/routes/api/health/route.js +17 -0
- package/src/routes/api/inflight/stream/route.js +24 -0
- package/src/routes/api/init/route.js +3 -0
- package/src/routes/api/keys/[id]/route.js +71 -0
- package/src/routes/api/keys/route.js +41 -0
- package/src/routes/api/logs/route.js +19 -0
- package/src/routes/api/mcp/[plugin]/message/route.js +23 -0
- package/src/routes/api/mcp/[plugin]/sse/route.js +35 -0
- package/src/routes/api/models/availability/route.js +104 -0
- package/src/routes/api/models/custom/route.js +58 -0
- package/src/routes/api/models/disabled/route.js +47 -0
- package/src/routes/api/models/route.js +64 -0
- package/src/routes/api/models/test/ping.js +236 -0
- package/src/routes/api/models/test/route.js +14 -0
- package/src/routes/api/oauth/[provider]/[action]/route.js +491 -0
- package/src/routes/api/oauth/codex/bulk-import/route.js +105 -0
- package/src/routes/api/oauth/codex/import-token/route.js +82 -0
- package/src/routes/api/oauth/cursor/auto-import/route.js +250 -0
- package/src/routes/api/oauth/cursor/import/route.js +86 -0
- package/src/routes/api/oauth/kiro/api-key/route.js +60 -0
- package/src/routes/api/oauth/kiro/auto-import/route.js +120 -0
- package/src/routes/api/oauth/kiro/import/route.js +72 -0
- package/src/routes/api/oauth/kiro/import-cli-proxy/route.js +42 -0
- package/src/routes/api/oauth/kiro/social-authorize/route.js +40 -0
- package/src/routes/api/oauth/kiro/social-exchange/route.js +62 -0
- package/src/routes/api/pricing/route.js +96 -0
- package/src/routes/api/provider-nodes/[id]/route.js +106 -0
- package/src/routes/api/provider-nodes/route.js +110 -0
- package/src/routes/api/provider-nodes/validate/route.js +197 -0
- package/src/routes/api/providers/[id]/models/route.js +716 -0
- package/src/routes/api/providers/[id]/route.js +195 -0
- package/src/routes/api/providers/[id]/test/route.js +27 -0
- package/src/routes/api/providers/[id]/test/testUtils.js +912 -0
- package/src/routes/api/providers/[id]/test-models/route.js +211 -0
- package/src/routes/api/providers/catalog/route.js +58 -0
- package/src/routes/api/providers/client/route.js +133 -0
- package/src/routes/api/providers/counts/route.js +43 -0
- package/src/routes/api/providers/kilo/free-models/route.js +58 -0
- package/src/routes/api/providers/route.js +361 -0
- package/src/routes/api/providers/suggested-models/filters.js +24 -0
- package/src/routes/api/providers/suggested-models/route.js +34 -0
- package/src/routes/api/providers/test-batch/route.js +145 -0
- package/src/routes/api/providers/validate/route.js +633 -0
- package/src/routes/api/proxy-pools/[id]/route.js +129 -0
- package/src/routes/api/proxy-pools/[id]/test/route.js +64 -0
- package/src/routes/api/proxy-pools/cloudflare-deploy/route.js +183 -0
- package/src/routes/api/proxy-pools/route.js +85 -0
- package/src/routes/api/proxy-pools/vercel-deploy/route.js +181 -0
- package/src/routes/api/settings/currency/route.js +29 -0
- package/src/routes/api/settings/database/path/route.js +8 -0
- package/src/routes/api/settings/database/route.js +89 -0
- package/src/routes/api/settings/migrate-9router/route.js +81 -0
- package/src/routes/api/settings/require-login/route.js +14 -0
- package/src/routes/api/settings/route.js +186 -0
- package/src/routes/api/shutdown/route.js +25 -0
- package/src/routes/api/tags/route.js +17 -0
- package/src/routes/api/translator/console-logs/route.js +28 -0
- package/src/routes/api/translator/console-logs/stream/route.js +95 -0
- package/src/routes/api/tunnel/disable/route.js +18 -0
- package/src/routes/api/tunnel/enable/route.js +32 -0
- package/src/routes/api/tunnel/status/route.js +23 -0
- package/src/routes/api/update/route.js +42 -0
- package/src/routes/api/usage/[connectionId]/codex-reset-credits/route.js +169 -0
- package/src/routes/api/usage/[connectionId]/route.js +222 -0
- package/src/routes/api/usage/chart/route.js +27 -0
- package/src/routes/api/usage/history/route.js +94 -0
- package/src/routes/api/usage/logs/route.js +14 -0
- package/src/routes/api/usage/providers/route.js +36 -0
- package/src/routes/api/usage/request-details/route.js +61 -0
- package/src/routes/api/usage/request-logs/route.js +15 -0
- package/src/routes/api/usage/stats/route.js +23 -0
- package/src/routes/api/usage/stream/route.js +78 -0
- package/src/routes/api/v1/api/chat/route.js +36 -0
- package/src/routes/api/v1/chat/completions/route.js +28 -0
- package/src/routes/api/v1/images/edits/route.js +70 -0
- package/src/routes/api/v1/images/generations/route.js +50 -0
- package/src/routes/api/v1/images/imageValidation.js +45 -0
- package/src/routes/api/v1/messages/count_tokens/route.js +87 -0
- package/src/routes/api/v1/messages/route.js +26 -0
- package/src/routes/api/v1/models/route.js +440 -0
- package/src/routes/api/v1/responses/compact/route.js +34 -0
- package/src/routes/api/v1/responses/route.js +26 -0
- package/src/routes/api/v1/route.js +1 -0
- package/src/routes/api/version/route.js +11 -0
- package/src/routes/table.js +1141 -0
- package/src/rtk/applyFilter.js +13 -0
- package/src/rtk/autodetect.js +104 -0
- package/src/rtk/caveman.js +6 -0
- package/src/rtk/cavemanPrompts.js +107 -0
- package/src/rtk/constants.js +51 -0
- package/src/rtk/filters/buildOutput.js +120 -0
- package/src/rtk/filters/dedupLog.js +43 -0
- package/src/rtk/filters/find.js +47 -0
- package/src/rtk/filters/gitDiff.js +90 -0
- package/src/rtk/filters/gitLog.js +88 -0
- package/src/rtk/filters/gitStatus.js +101 -0
- package/src/rtk/filters/grep.js +45 -0
- package/src/rtk/filters/ls.js +73 -0
- package/src/rtk/filters/readNumbered.js +22 -0
- package/src/rtk/filters/searchList.js +49 -0
- package/src/rtk/filters/smartTruncate.js +13 -0
- package/src/rtk/filters/tree.js +28 -0
- package/src/rtk/index.js +143 -0
- package/src/rtk/ponytail.js +6 -0
- package/src/rtk/ponytailPrompt.js +49 -0
- package/src/rtk/systemInject.js +89 -0
- package/src/server.ts +474 -0
- package/src/services/accountFallback.js +241 -0
- package/src/services/accountPool.js +389 -0
- package/src/services/capacityAdapter.js +140 -0
- package/src/services/cliModelMapping.js +84 -0
- package/src/services/clinepassModels.js +60 -0
- package/src/services/codebuddyModels.js +250 -0
- package/src/services/combo.js +482 -0
- package/src/services/copilotModels.js +126 -0
- package/src/services/currencyRate.js +56 -0
- package/src/services/cursorModels.js +197 -0
- package/src/services/grokCliModels.js +130 -0
- package/src/services/kimchiModels.js +175 -0
- package/src/services/kiroModels.js +246 -0
- package/src/services/model.js +83 -0
- package/src/services/oauthCredentialManager.js +153 -0
- package/src/services/projectId.js +240 -0
- package/src/services/provider.js +145 -0
- package/src/services/qoderModels.js +274 -0
- package/src/services/tokenRefresh/dedup.js +70 -0
- package/src/services/tokenRefresh/providers.js +626 -0
- package/src/services/tokenRefresh.js +274 -0
- package/src/services/usage/claude.js +134 -0
- package/src/services/usage/codebuddy-cn.js +212 -0
- package/src/services/usage/codex.js +193 -0
- package/src/services/usage/deepseek.js +103 -0
- package/src/services/usage/github.js +94 -0
- package/src/services/usage/google.js +218 -0
- package/src/services/usage/grok-cli.js +368 -0
- package/src/services/usage/grokCliQuotaFrame.js +170 -0
- package/src/services/usage/kimi.js +185 -0
- package/src/services/usage/kiro.js +172 -0
- package/src/services/usage/minimax.js +215 -0
- package/src/services/usage/misc.js +264 -0
- package/src/services/usage/shared.js +59 -0
- package/src/services/usage.js +56 -0
- package/src/shared/clineAuth.js +37 -0
- package/src/shared/constants/config.js +80 -0
- package/src/shared/constants/coworkPlugins.js +69 -0
- package/src/shared/constants/models.js +41 -0
- package/src/shared/constants/providers.js +128 -0
- package/src/shared/constants/providersDisplay.js +1 -0
- package/src/shared/qoder/constants.js +50 -0
- package/src/shared/qoder/cosy.js +134 -0
- package/src/shared/qoder/encoding.js +36 -0
- package/src/shared/services/initializeApp.js +226 -0
- package/src/shared/services/quotaAutoPing.js +310 -0
- package/src/shared/utils/apiKey.js +65 -0
- package/src/shared/utils/clineAuth.js +1 -0
- package/src/shared/utils/index.js +3 -0
- package/src/shared/utils/machineId.js +60 -0
- package/src/shared/utils/ssrfGuard.js +280 -0
- package/src/sse/handlers/chat.js +426 -0
- package/src/sse/oauthLease.js +32 -0
- package/src/sse/recovery.js +235 -0
- package/src/sse/services/auth.js +489 -0
- package/src/sse/services/backgroundTokenRefresh.js +173 -0
- package/src/sse/services/model.js +129 -0
- package/src/sse/services/requestCancellation.js +4 -0
- package/src/sse/services/tokenRefresh.js +264 -0
- package/src/sse/utils/logger.js +142 -0
- package/src/staticServe.ts +95 -0
- package/src/transformer/streamToJsonConverter.js +87 -0
- package/src/translator/canonical.js +81 -0
- package/src/translator/concerns/chunk.js +9 -0
- package/src/translator/concerns/compatFallback.js +147 -0
- package/src/translator/concerns/finishReason.js +59 -0
- package/src/translator/concerns/image.js +105 -0
- package/src/translator/concerns/json.js +4 -0
- package/src/translator/concerns/kiroConversation.js +428 -0
- package/src/translator/concerns/message.js +5 -0
- package/src/translator/concerns/modality.js +134 -0
- package/src/translator/concerns/paramSupport.js +97 -0
- package/src/translator/concerns/prefetch.js +86 -0
- package/src/translator/concerns/reasoning.js +18 -0
- package/src/translator/concerns/thinking.js +40 -0
- package/src/translator/concerns/thinkingUnified.js +319 -0
- package/src/translator/concerns/toolCall.js +134 -0
- package/src/translator/concerns/usage.js +61 -0
- package/src/translator/detection.js +42 -0
- package/src/translator/formats/claude.js +323 -0
- package/src/translator/formats/gemini.js +366 -0
- package/src/translator/formats/maxTokens.js +19 -0
- package/src/translator/formats/openai.js +116 -0
- package/src/translator/formats/responsesApi.js +117 -0
- package/src/translator/formats.js +28 -0
- package/src/translator/index.js +237 -0
- package/src/translator/request/antigravity-to-openai.js +217 -0
- package/src/translator/request/claude-to-kiro.js +288 -0
- package/src/translator/request/claude-to-openai.js +199 -0
- package/src/translator/request/gemini-to-openai.js +142 -0
- package/src/translator/request/openai-responses.js +373 -0
- package/src/translator/request/openai-to-claude.js +260 -0
- package/src/translator/request/openai-to-commandcode.js +161 -0
- package/src/translator/request/openai-to-cursor.js +175 -0
- package/src/translator/request/openai-to-gemini.js +422 -0
- package/src/translator/request/openai-to-kiro.js +360 -0
- package/src/translator/request/openai-to-ollama.js +152 -0
- package/src/translator/request/openai-to-vertex.js +36 -0
- package/src/translator/response/claude-to-openai.js +185 -0
- package/src/translator/response/commandcode-to-openai.js +164 -0
- package/src/translator/response/cursor-to-openai.js +18 -0
- package/src/translator/response/gemini-to-openai.js +127 -0
- package/src/translator/response/kiro-to-claude.js +230 -0
- package/src/translator/response/kiro-to-openai.js +136 -0
- package/src/translator/response/ollama-to-openai.js +106 -0
- package/src/translator/response/openai-responses.js +534 -0
- package/src/translator/response/openai-to-antigravity.js +107 -0
- package/src/translator/response/openai-to-claude.js +231 -0
- package/src/translator/schema/blocks.js +40 -0
- package/src/translator/schema/defaults.js +3 -0
- package/src/translator/schema/finishReasons.js +22 -0
- package/src/translator/schema/index.js +7 -0
- package/src/translator/schema/roles.js +12 -0
- package/src/transport/requestBoundary.ts +223 -0
- package/src/transport/routerAdmission.ts +380 -0
- package/src/types/provider.ts +59 -0
- package/src/utils/bypassHandler.js +272 -0
- package/src/utils/claudeCloaking.js +10 -0
- package/src/utils/claudeSignature.js +35 -0
- package/src/utils/clientDetector.js +183 -0
- package/src/utils/cursorChecksum.js +104 -0
- package/src/utils/cursorProtobuf.js +801 -0
- package/src/utils/debugLog.js +12 -0
- package/src/utils/error.js +120 -0
- package/src/utils/inflight.js +127 -0
- package/src/utils/kiroSessionReplay.js +139 -0
- package/src/utils/ollamaTransform.js +83 -0
- package/src/utils/proxyFetch.js +308 -0
- package/src/utils/reasoningContentInjector.js +71 -0
- package/src/utils/requestLogger.js +272 -0
- package/src/utils/responsesStreamHelpers.js +46 -0
- package/src/utils/sessionManager.js +205 -0
- package/src/utils/sse.js +13 -0
- package/src/utils/sseConstants.js +21 -0
- package/src/utils/stream.js +511 -0
- package/src/utils/streamHandler.js +256 -0
- package/src/utils/streamHelpers.js +113 -0
- package/src/utils/toolDeduper.js +44 -0
- package/src/utils/usageTracking.js +329 -0
- package/tsconfig.json +45 -0
|
@@ -0,0 +1,801 @@
|
|
|
1
|
+
import { v4 as uuidv4 } from "uuid";
|
|
2
|
+
import zlib from "zlib";
|
|
3
|
+
|
|
4
|
+
const DEBUG = process.env.CURSOR_PROTOBUF_DEBUG === "1";
|
|
5
|
+
const log = (tag, ...args) => DEBUG && console.log(`[PROTOBUF:${tag}]`, ...args);
|
|
6
|
+
const textDecoder = new TextDecoder();
|
|
7
|
+
|
|
8
|
+
const PROTOBUF_SCHEMA_VERSION = "1.1.3";
|
|
9
|
+
|
|
10
|
+
const WIRE_TYPE = { VARINT: 0, FIXED64: 1, LEN: 2, FIXED32: 5 };
|
|
11
|
+
|
|
12
|
+
const ROLE = { USER: 1, ASSISTANT: 2 };
|
|
13
|
+
|
|
14
|
+
const UNIFIED_MODE = { CHAT: 1, AGENT: 2 };
|
|
15
|
+
|
|
16
|
+
const THINKING_LEVEL = { UNSPECIFIED: 0, MEDIUM: 1, HIGH: 2 };
|
|
17
|
+
const CLIENT_SIDE_TOOL_V2 = { MCP: 19 };
|
|
18
|
+
const CLIENT_SIDE_TOOL_V2_MCP = 19;
|
|
19
|
+
|
|
20
|
+
const FIELD = {
|
|
21
|
+
|
|
22
|
+
REQUEST: 1,
|
|
23
|
+
|
|
24
|
+
MESSAGES: 1,
|
|
25
|
+
UNKNOWN_2: 2,
|
|
26
|
+
INSTRUCTION: 3,
|
|
27
|
+
UNKNOWN_4: 4,
|
|
28
|
+
MODEL: 5,
|
|
29
|
+
WEB_TOOL: 8,
|
|
30
|
+
UNKNOWN_13: 13,
|
|
31
|
+
CURSOR_SETTING: 15,
|
|
32
|
+
UNKNOWN_19: 19,
|
|
33
|
+
CONVERSATION_ID: 23,
|
|
34
|
+
METADATA: 26,
|
|
35
|
+
IS_AGENTIC: 27,
|
|
36
|
+
SUPPORTED_TOOLS: 29,
|
|
37
|
+
MESSAGE_IDS: 30,
|
|
38
|
+
MCP_TOOLS: 34,
|
|
39
|
+
LARGE_CONTEXT: 35,
|
|
40
|
+
UNKNOWN_38: 38,
|
|
41
|
+
UNIFIED_MODE: 46,
|
|
42
|
+
UNKNOWN_47: 47,
|
|
43
|
+
SHOULD_DISABLE_TOOLS: 48,
|
|
44
|
+
THINKING_LEVEL: 49,
|
|
45
|
+
UNKNOWN_51: 51,
|
|
46
|
+
UNKNOWN_53: 53,
|
|
47
|
+
UNIFIED_MODE_NAME: 54,
|
|
48
|
+
|
|
49
|
+
MSG_CONTENT: 1,
|
|
50
|
+
MSG_ROLE: 2,
|
|
51
|
+
MSG_ID: 13,
|
|
52
|
+
MSG_TOOL_RESULTS: 18,
|
|
53
|
+
MSG_IS_AGENTIC: 29,
|
|
54
|
+
MSG_SERVER_BUBBLE_ID: 32,
|
|
55
|
+
MSG_UNIFIED_MODE: 47,
|
|
56
|
+
MSG_SUPPORTED_TOOLS: 51,
|
|
57
|
+
|
|
58
|
+
TOOL_RESULT_CALL_ID: 1,
|
|
59
|
+
TOOL_RESULT_NAME: 2,
|
|
60
|
+
TOOL_RESULT_INDEX: 3,
|
|
61
|
+
TOOL_RESULT_RAW_ARGS: 5,
|
|
62
|
+
TOOL_RESULT_RESULT: 8,
|
|
63
|
+
TOOL_RESULT_TOOL_CALL: 11,
|
|
64
|
+
TOOL_RESULT_MODEL_CALL_ID: 12,
|
|
65
|
+
|
|
66
|
+
CLIENT_RESULT_TOOL: 1,
|
|
67
|
+
CLIENT_RESULT_MCP_RESULT: 28,
|
|
68
|
+
CLIENT_RESULT_TOOL_CALL_ID: 35,
|
|
69
|
+
CLIENT_RESULT_MODEL_CALL_ID: 48,
|
|
70
|
+
CLIENT_RESULT_TOOL_INDEX: 49,
|
|
71
|
+
|
|
72
|
+
CV2R_TOOL: 1,
|
|
73
|
+
CV2R_MCP_RESULT: 28,
|
|
74
|
+
CV2R_CALL_ID: 35,
|
|
75
|
+
CV2R_MODEL_CALL_ID: 48,
|
|
76
|
+
CV2R_TOOL_INDEX: 49,
|
|
77
|
+
|
|
78
|
+
MCP_RESULT_SELECTED_TOOL: 1,
|
|
79
|
+
MCP_RESULT_RESULT: 2,
|
|
80
|
+
|
|
81
|
+
MCPR_SELECTED_TOOL: 1,
|
|
82
|
+
MCPR_RESULT: 2,
|
|
83
|
+
|
|
84
|
+
CLIENT_CALL_TOOL: 1,
|
|
85
|
+
CLIENT_CALL_MCP_PARAMS: 27,
|
|
86
|
+
CLIENT_CALL_TOOL_CALL_ID: 3,
|
|
87
|
+
CLIENT_CALL_NAME: 9,
|
|
88
|
+
CLIENT_CALL_RAW_ARGS: 10,
|
|
89
|
+
CLIENT_CALL_TOOL_INDEX: 48,
|
|
90
|
+
CLIENT_CALL_MODEL_CALL_ID: 49,
|
|
91
|
+
|
|
92
|
+
CV2C_TOOL: 1,
|
|
93
|
+
CV2C_MCP_PARAMS: 27,
|
|
94
|
+
CV2C_CALL_ID: 3,
|
|
95
|
+
CV2C_NAME: 9,
|
|
96
|
+
CV2C_RAW_ARGS: 10,
|
|
97
|
+
CV2C_TOOL_INDEX: 48,
|
|
98
|
+
CV2C_MODEL_CALL_ID: 49,
|
|
99
|
+
|
|
100
|
+
MODEL_NAME: 1,
|
|
101
|
+
MODEL_EMPTY: 4,
|
|
102
|
+
|
|
103
|
+
INSTRUCTION_TEXT: 1,
|
|
104
|
+
|
|
105
|
+
SETTING_PATH: 1,
|
|
106
|
+
SETTING_UNKNOWN_3: 3,
|
|
107
|
+
SETTING_UNKNOWN_6: 6,
|
|
108
|
+
SETTING_UNKNOWN_8: 8,
|
|
109
|
+
SETTING_UNKNOWN_9: 9,
|
|
110
|
+
|
|
111
|
+
SETTING6_FIELD_1: 1,
|
|
112
|
+
SETTING6_FIELD_2: 2,
|
|
113
|
+
|
|
114
|
+
META_PLATFORM: 1,
|
|
115
|
+
META_ARCH: 2,
|
|
116
|
+
META_VERSION: 3,
|
|
117
|
+
META_CWD: 4,
|
|
118
|
+
META_TIMESTAMP: 5,
|
|
119
|
+
|
|
120
|
+
MSGID_ID: 1,
|
|
121
|
+
MSGID_SUMMARY: 2,
|
|
122
|
+
MSGID_ROLE: 3,
|
|
123
|
+
|
|
124
|
+
MCP_TOOL_NAME: 1,
|
|
125
|
+
MCP_TOOL_DESC: 2,
|
|
126
|
+
MCP_TOOL_PARAMS: 3,
|
|
127
|
+
MCP_TOOL_SERVER: 4,
|
|
128
|
+
|
|
129
|
+
TOOL_CALL: 1,
|
|
130
|
+
RESPONSE: 2,
|
|
131
|
+
|
|
132
|
+
TOOL_ID: 3,
|
|
133
|
+
TOOL_NAME: 9,
|
|
134
|
+
TOOL_RAW_ARGS: 10,
|
|
135
|
+
TOOL_IS_LAST: 11,
|
|
136
|
+
TOOL_IS_LAST_ALT: 15,
|
|
137
|
+
TOOL_MCP_PARAMS: 27,
|
|
138
|
+
|
|
139
|
+
MCP_TOOLS_LIST: 1,
|
|
140
|
+
|
|
141
|
+
MCP_NESTED_NAME: 1,
|
|
142
|
+
MCP_NESTED_PARAMS: 3,
|
|
143
|
+
|
|
144
|
+
RESPONSE_TEXT: 1,
|
|
145
|
+
THINKING: 25,
|
|
146
|
+
|
|
147
|
+
THINKING_TEXT: 1
|
|
148
|
+
};
|
|
149
|
+
|
|
150
|
+
const KNOWN_RESPONSE_FIELDS = new Set([
|
|
151
|
+
FIELD.TOOL_CALL,
|
|
152
|
+
FIELD.RESPONSE,
|
|
153
|
+
FIELD.TOOL_ID,
|
|
154
|
+
FIELD.TOOL_NAME,
|
|
155
|
+
FIELD.TOOL_RAW_ARGS,
|
|
156
|
+
FIELD.TOOL_IS_LAST,
|
|
157
|
+
FIELD.TOOL_MCP_PARAMS,
|
|
158
|
+
FIELD.RESPONSE_TEXT,
|
|
159
|
+
FIELD.THINKING
|
|
160
|
+
]);
|
|
161
|
+
|
|
162
|
+
export function encodeVarint(value) {
|
|
163
|
+
const bytes = [];
|
|
164
|
+
while (value >= 0x80) {
|
|
165
|
+
bytes.push((value & 0x7F) | 0x80);
|
|
166
|
+
value >>>= 7;
|
|
167
|
+
}
|
|
168
|
+
bytes.push(value & 0x7F);
|
|
169
|
+
return new Uint8Array(bytes);
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
export function encodeField(fieldNum, wireType, value) {
|
|
173
|
+
const tag = (fieldNum << 3) | wireType;
|
|
174
|
+
const tagBytes = encodeVarint(tag);
|
|
175
|
+
|
|
176
|
+
if (wireType === WIRE_TYPE.VARINT) {
|
|
177
|
+
const valueBytes = encodeVarint(value);
|
|
178
|
+
return concatArrays(tagBytes, valueBytes);
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
if (wireType === WIRE_TYPE.LEN) {
|
|
182
|
+
const dataBytes = typeof value === "string"
|
|
183
|
+
? new TextEncoder().encode(value)
|
|
184
|
+
: value instanceof Uint8Array ? value
|
|
185
|
+
: Buffer.isBuffer(value) ? new Uint8Array(value)
|
|
186
|
+
: new Uint8Array(0);
|
|
187
|
+
|
|
188
|
+
const lengthBytes = encodeVarint(dataBytes.length);
|
|
189
|
+
return concatArrays(tagBytes, lengthBytes, dataBytes);
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
return new Uint8Array(0);
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
function concatArrays(...arrays) {
|
|
196
|
+
const totalLength = arrays.reduce((sum, arr) => sum + arr.length, 0);
|
|
197
|
+
const result = new Uint8Array(totalLength);
|
|
198
|
+
let offset = 0;
|
|
199
|
+
for (const arr of arrays) {
|
|
200
|
+
result.set(arr, offset);
|
|
201
|
+
offset += arr.length;
|
|
202
|
+
}
|
|
203
|
+
return result;
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
function formatToolName(name) {
|
|
207
|
+
const base = typeof name === "string" && name.length > 0 ? name : "tool";
|
|
208
|
+
|
|
209
|
+
if (base.startsWith("mcp__")) {
|
|
210
|
+
const rest = base.slice("mcp__".length);
|
|
211
|
+
const splitIdx = rest.indexOf("__");
|
|
212
|
+
if (splitIdx >= 0) {
|
|
213
|
+
const server = rest.slice(0, splitIdx) || "custom";
|
|
214
|
+
const toolName = rest.slice(splitIdx + 2) || "tool";
|
|
215
|
+
return `mcp_${server}_${toolName}`;
|
|
216
|
+
}
|
|
217
|
+
return `mcp_custom_${rest || "tool"}`;
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
if (base.startsWith("mcp_")) return base;
|
|
221
|
+
return `mcp_custom_${base}`;
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
function parseToolName(formattedName) {
|
|
225
|
+
if (typeof formattedName !== "string" || !formattedName.startsWith("mcp_")) {
|
|
226
|
+
return { serverName: "custom", selectedTool: formattedName || "tool" };
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
const tail = formattedName.slice("mcp_".length);
|
|
230
|
+
const splitIdx = tail.indexOf("_");
|
|
231
|
+
if (splitIdx < 0) {
|
|
232
|
+
return { serverName: "custom", selectedTool: tail || "tool" };
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
return {
|
|
236
|
+
serverName: tail.slice(0, splitIdx) || "custom",
|
|
237
|
+
selectedTool: tail.slice(splitIdx + 1) || "tool"
|
|
238
|
+
};
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
function parseToolId(id) {
|
|
242
|
+
const delimiter = "\nmc_";
|
|
243
|
+
const idx = id.indexOf(delimiter);
|
|
244
|
+
if (idx >= 0) {
|
|
245
|
+
return { toolCallId: id.slice(0, idx), modelCallId: id.slice(idx + delimiter.length) };
|
|
246
|
+
}
|
|
247
|
+
return { toolCallId: id, modelCallId: null };
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
function encodeMcpResult(selectedTool, resultContent) {
|
|
251
|
+
return concatArrays(
|
|
252
|
+
encodeField(FIELD.MCPR_SELECTED_TOOL, WIRE_TYPE.LEN, selectedTool),
|
|
253
|
+
encodeField(FIELD.MCPR_RESULT, WIRE_TYPE.LEN, resultContent)
|
|
254
|
+
);
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
function encodeClientSideToolV2Result(toolCallId, modelCallId, selectedTool, resultContent, toolIndex = 1) {
|
|
258
|
+
return concatArrays(
|
|
259
|
+
encodeField(FIELD.CV2R_TOOL, WIRE_TYPE.VARINT, CLIENT_SIDE_TOOL_V2_MCP),
|
|
260
|
+
encodeField(FIELD.CV2R_MCP_RESULT, WIRE_TYPE.LEN, encodeMcpResult(selectedTool, resultContent)),
|
|
261
|
+
encodeField(FIELD.CV2R_CALL_ID, WIRE_TYPE.LEN, toolCallId),
|
|
262
|
+
...(modelCallId ? [encodeField(FIELD.CV2R_MODEL_CALL_ID, WIRE_TYPE.LEN, modelCallId)] : []),
|
|
263
|
+
encodeField(FIELD.CV2R_TOOL_INDEX, WIRE_TYPE.VARINT, toolIndex > 0 ? toolIndex : 1)
|
|
264
|
+
);
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
function encodeMcpParamsForCall(toolName, rawArgs, serverName) {
|
|
268
|
+
const tool = concatArrays(
|
|
269
|
+
encodeField(FIELD.MCP_TOOL_NAME, WIRE_TYPE.LEN, toolName),
|
|
270
|
+
encodeField(FIELD.MCP_TOOL_PARAMS, WIRE_TYPE.LEN, rawArgs),
|
|
271
|
+
encodeField(FIELD.MCP_TOOL_SERVER, WIRE_TYPE.LEN, serverName)
|
|
272
|
+
);
|
|
273
|
+
return encodeField(FIELD.MCP_TOOLS_LIST, WIRE_TYPE.LEN, tool);
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
function encodeClientSideToolV2Call(toolCallId, toolName, selectedTool, serverName, rawArgs, modelCallId, toolIndex = 1) {
|
|
277
|
+
return concatArrays(
|
|
278
|
+
encodeField(FIELD.CV2C_TOOL, WIRE_TYPE.VARINT, CLIENT_SIDE_TOOL_V2_MCP),
|
|
279
|
+
encodeField(FIELD.CV2C_MCP_PARAMS, WIRE_TYPE.LEN, encodeMcpParamsForCall(selectedTool, rawArgs, serverName)),
|
|
280
|
+
encodeField(FIELD.CV2C_CALL_ID, WIRE_TYPE.LEN, toolCallId),
|
|
281
|
+
encodeField(FIELD.CV2C_NAME, WIRE_TYPE.LEN, toolName),
|
|
282
|
+
encodeField(FIELD.CV2C_RAW_ARGS, WIRE_TYPE.LEN, rawArgs),
|
|
283
|
+
encodeField(FIELD.CV2C_TOOL_INDEX, WIRE_TYPE.VARINT, toolIndex > 0 ? toolIndex : 1),
|
|
284
|
+
...(modelCallId ? [encodeField(FIELD.CV2C_MODEL_CALL_ID, WIRE_TYPE.LEN, modelCallId)] : [])
|
|
285
|
+
);
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
export function encodeToolResult(toolResult) {
|
|
289
|
+
const originalName = toolResult.tool_name || toolResult.name || "";
|
|
290
|
+
const toolName = formatToolName(originalName);
|
|
291
|
+
const rawArgs = toolResult.raw_args || "{}";
|
|
292
|
+
const resultContent = toolResult.result_content || toolResult.result || "";
|
|
293
|
+
const { toolCallId, modelCallId } = parseToolId(toolResult.tool_call_id || "");
|
|
294
|
+
const toolIndex = toolResult.tool_index || toolResult.index || 1;
|
|
295
|
+
|
|
296
|
+
const { serverName, selectedTool } = parseToolName(toolName);
|
|
297
|
+
|
|
298
|
+
return concatArrays(
|
|
299
|
+
encodeField(FIELD.TOOL_RESULT_CALL_ID, WIRE_TYPE.LEN, toolCallId),
|
|
300
|
+
encodeField(FIELD.TOOL_RESULT_NAME, WIRE_TYPE.LEN, toolName),
|
|
301
|
+
encodeField(FIELD.TOOL_RESULT_INDEX, WIRE_TYPE.VARINT, toolIndex > 0 ? toolIndex : 1),
|
|
302
|
+
...(modelCallId ? [encodeField(FIELD.TOOL_RESULT_MODEL_CALL_ID, WIRE_TYPE.LEN, modelCallId)] : []),
|
|
303
|
+
encodeField(FIELD.TOOL_RESULT_RAW_ARGS, WIRE_TYPE.LEN, rawArgs),
|
|
304
|
+
encodeField(FIELD.TOOL_RESULT_RESULT, WIRE_TYPE.LEN,
|
|
305
|
+
encodeClientSideToolV2Result(toolCallId, modelCallId, selectedTool, resultContent, toolIndex)
|
|
306
|
+
),
|
|
307
|
+
encodeField(FIELD.TOOL_RESULT_TOOL_CALL, WIRE_TYPE.LEN,
|
|
308
|
+
encodeClientSideToolV2Call(toolCallId, toolName, selectedTool, serverName, rawArgs, modelCallId, toolIndex)
|
|
309
|
+
)
|
|
310
|
+
);
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
export function encodeMessage(content, role, messageId, chatModeEnum = null, isLast = false, hasTools = false, toolResults = [], serverBubbleId = null) {
|
|
314
|
+
const hasToolResults = toolResults.length > 0;
|
|
315
|
+
return concatArrays(
|
|
316
|
+
encodeField(FIELD.MSG_CONTENT, WIRE_TYPE.LEN, content),
|
|
317
|
+
encodeField(FIELD.MSG_ROLE, WIRE_TYPE.VARINT, role),
|
|
318
|
+
encodeField(FIELD.MSG_ID, WIRE_TYPE.LEN, messageId),
|
|
319
|
+
|
|
320
|
+
...(serverBubbleId ? [encodeField(FIELD.MSG_SERVER_BUBBLE_ID, WIRE_TYPE.LEN, serverBubbleId)] : []),
|
|
321
|
+
...(hasToolResults ? toolResults.map(tr =>
|
|
322
|
+
encodeField(FIELD.MSG_TOOL_RESULTS, WIRE_TYPE.LEN, encodeToolResult(tr))
|
|
323
|
+
) : []),
|
|
324
|
+
encodeField(FIELD.MSG_IS_AGENTIC, WIRE_TYPE.VARINT, hasTools ? 1 : 0),
|
|
325
|
+
encodeField(FIELD.MSG_UNIFIED_MODE, WIRE_TYPE.VARINT, hasTools ? UNIFIED_MODE.AGENT : UNIFIED_MODE.CHAT),
|
|
326
|
+
...(isLast && hasTools ? [encodeField(FIELD.MSG_SUPPORTED_TOOLS, WIRE_TYPE.LEN, encodeVarint(1))] : [])
|
|
327
|
+
);
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
export function encodeInstruction(text) {
|
|
331
|
+
return text ? encodeField(FIELD.INSTRUCTION_TEXT, WIRE_TYPE.LEN, text) : new Uint8Array(0);
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
export function encodeModel(modelName) {
|
|
335
|
+
return concatArrays(
|
|
336
|
+
encodeField(FIELD.MODEL_NAME, WIRE_TYPE.LEN, modelName),
|
|
337
|
+
encodeField(FIELD.MODEL_EMPTY, WIRE_TYPE.LEN, new Uint8Array(0))
|
|
338
|
+
);
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
export function encodeCursorSetting() {
|
|
342
|
+
const unknown6 = concatArrays(
|
|
343
|
+
encodeField(FIELD.SETTING6_FIELD_1, WIRE_TYPE.LEN, new Uint8Array(0)),
|
|
344
|
+
encodeField(FIELD.SETTING6_FIELD_2, WIRE_TYPE.LEN, new Uint8Array(0))
|
|
345
|
+
);
|
|
346
|
+
|
|
347
|
+
return concatArrays(
|
|
348
|
+
encodeField(FIELD.SETTING_PATH, WIRE_TYPE.LEN, "cursor\\aisettings"),
|
|
349
|
+
encodeField(FIELD.SETTING_UNKNOWN_3, WIRE_TYPE.LEN, new Uint8Array(0)),
|
|
350
|
+
encodeField(FIELD.SETTING_UNKNOWN_6, WIRE_TYPE.LEN, unknown6),
|
|
351
|
+
encodeField(FIELD.SETTING_UNKNOWN_8, WIRE_TYPE.VARINT, 1),
|
|
352
|
+
encodeField(FIELD.SETTING_UNKNOWN_9, WIRE_TYPE.VARINT, 1)
|
|
353
|
+
);
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
export function encodeMetadata() {
|
|
357
|
+
return concatArrays(
|
|
358
|
+
encodeField(FIELD.META_PLATFORM, WIRE_TYPE.LEN, process.platform || "linux"),
|
|
359
|
+
encodeField(FIELD.META_ARCH, WIRE_TYPE.LEN, process.arch || "x64"),
|
|
360
|
+
encodeField(FIELD.META_VERSION, WIRE_TYPE.LEN, process.version || "v20.0.0"),
|
|
361
|
+
encodeField(FIELD.META_CWD, WIRE_TYPE.LEN, process.cwd?.() || "/"),
|
|
362
|
+
encodeField(FIELD.META_TIMESTAMP, WIRE_TYPE.LEN, new Date().toISOString())
|
|
363
|
+
);
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
export function encodeMessageId(messageId, role, summaryId = null) {
|
|
367
|
+
return concatArrays(
|
|
368
|
+
encodeField(FIELD.MSGID_ID, WIRE_TYPE.LEN, messageId),
|
|
369
|
+
...(summaryId ? [encodeField(FIELD.MSGID_SUMMARY, WIRE_TYPE.LEN, summaryId)] : []),
|
|
370
|
+
encodeField(FIELD.MSGID_ROLE, WIRE_TYPE.VARINT, role)
|
|
371
|
+
);
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
export function encodeMcpTool(tool) {
|
|
375
|
+
const toolName = tool.function?.name || tool.name || "";
|
|
376
|
+
const toolDesc = tool.function?.description || tool.description || "";
|
|
377
|
+
const inputSchema = tool.function?.parameters || tool.input_schema || {};
|
|
378
|
+
|
|
379
|
+
return concatArrays(
|
|
380
|
+
...(toolName ? [encodeField(FIELD.MCP_TOOL_NAME, WIRE_TYPE.LEN, toolName)] : []),
|
|
381
|
+
...(toolDesc ? [encodeField(FIELD.MCP_TOOL_DESC, WIRE_TYPE.LEN, toolDesc)] : []),
|
|
382
|
+
...(Object.keys(inputSchema).length > 0 ? [encodeField(FIELD.MCP_TOOL_PARAMS, WIRE_TYPE.LEN, JSON.stringify(inputSchema))] : []),
|
|
383
|
+
encodeField(FIELD.MCP_TOOL_SERVER, WIRE_TYPE.LEN, "custom")
|
|
384
|
+
);
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
export function encodeRequest(messages, modelName, tools = [], reasoningEffort = null, forceAgentMode = false) {
|
|
388
|
+
const hasTools = tools?.length > 0;
|
|
389
|
+
const isAgentic = hasTools || forceAgentMode;
|
|
390
|
+
const formattedMessages = [];
|
|
391
|
+
const messageIds = [];
|
|
392
|
+
const normalizedMessages = [];
|
|
393
|
+
const system = messages
|
|
394
|
+
.filter((message) => message?.role === "system" || message?.role === "developer")
|
|
395
|
+
.map((message) => typeof message.content === "string"
|
|
396
|
+
? message.content
|
|
397
|
+
: Array.isArray(message.content)
|
|
398
|
+
? message.content.map((part) => part?.text || "").filter(Boolean).join("\n")
|
|
399
|
+
: "")
|
|
400
|
+
.filter(Boolean)
|
|
401
|
+
.join("\n\n");
|
|
402
|
+
|
|
403
|
+
for (let i = 0; i < messages.length; i++) {
|
|
404
|
+
const msg = messages[i];
|
|
405
|
+
if (msg?.role === "system" || msg?.role === "developer") continue;
|
|
406
|
+
const hasToolCalls = Array.isArray(msg?.tool_calls) && msg.tool_calls.length > 0;
|
|
407
|
+
const hasToolResults = Array.isArray(msg?.tool_results) && msg.tool_results.length > 0;
|
|
408
|
+
|
|
409
|
+
if (msg?.role === "assistant" && hasToolCalls && hasToolResults) {
|
|
410
|
+
log(
|
|
411
|
+
"ENCODE",
|
|
412
|
+
`normalizing mixed assistant tool payload at msg[${i}] (calls=${msg.tool_calls.length}, results=${msg.tool_results.length})`
|
|
413
|
+
);
|
|
414
|
+
|
|
415
|
+
normalizedMessages.push({
|
|
416
|
+
...msg,
|
|
417
|
+
tool_results: []
|
|
418
|
+
});
|
|
419
|
+
|
|
420
|
+
const nextMsg = messages[i + 1];
|
|
421
|
+
const nextHasToolResults =
|
|
422
|
+
nextMsg?.role === "assistant" &&
|
|
423
|
+
Array.isArray(nextMsg?.tool_results) &&
|
|
424
|
+
nextMsg.tool_results.length > 0;
|
|
425
|
+
const currentIds = new Set(
|
|
426
|
+
msg.tool_results.map(tr => tr?.tool_call_id).filter(id => typeof id === "string")
|
|
427
|
+
);
|
|
428
|
+
const nextIds = new Set(
|
|
429
|
+
(nextMsg?.tool_results || [])
|
|
430
|
+
.map(tr => tr?.tool_call_id)
|
|
431
|
+
.filter(id => typeof id === "string")
|
|
432
|
+
);
|
|
433
|
+
let sameIds = currentIds.size > 0 && currentIds.size === nextIds.size;
|
|
434
|
+
if (sameIds) {
|
|
435
|
+
for (const id of currentIds) {
|
|
436
|
+
if (!nextIds.has(id)) {
|
|
437
|
+
sameIds = false;
|
|
438
|
+
break;
|
|
439
|
+
}
|
|
440
|
+
}
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
if (!(nextHasToolResults && sameIds)) {
|
|
444
|
+
normalizedMessages.push({
|
|
445
|
+
role: "assistant",
|
|
446
|
+
content: "",
|
|
447
|
+
tool_results: msg.tool_results
|
|
448
|
+
});
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
continue;
|
|
452
|
+
}
|
|
453
|
+
|
|
454
|
+
normalizedMessages.push(msg);
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
for (let i = 0; i < normalizedMessages.length; i++) {
|
|
458
|
+
const msg = normalizedMessages[i];
|
|
459
|
+
const role = msg.role === "user" ? ROLE.USER : ROLE.ASSISTANT;
|
|
460
|
+
const msgId = uuidv4();
|
|
461
|
+
const isLast = i === normalizedMessages.length - 1;
|
|
462
|
+
|
|
463
|
+
formattedMessages.push({
|
|
464
|
+
content: msg.content,
|
|
465
|
+
role,
|
|
466
|
+
messageId: msgId,
|
|
467
|
+
isLast,
|
|
468
|
+
hasTools,
|
|
469
|
+
toolResults: msg.tool_results || []
|
|
470
|
+
});
|
|
471
|
+
|
|
472
|
+
messageIds.push({ messageId: msgId, role });
|
|
473
|
+
}
|
|
474
|
+
|
|
475
|
+
let thinkingLevel = THINKING_LEVEL.UNSPECIFIED;
|
|
476
|
+
if (reasoningEffort === "medium") thinkingLevel = THINKING_LEVEL.MEDIUM;
|
|
477
|
+
else if (reasoningEffort === "high") thinkingLevel = THINKING_LEVEL.HIGH;
|
|
478
|
+
|
|
479
|
+
return concatArrays(
|
|
480
|
+
|
|
481
|
+
...formattedMessages.map(fm =>
|
|
482
|
+
encodeField(FIELD.MESSAGES, WIRE_TYPE.LEN,
|
|
483
|
+
encodeMessage(fm.content, fm.role, fm.messageId, null, fm.isLast, fm.hasTools, fm.toolResults)
|
|
484
|
+
)
|
|
485
|
+
),
|
|
486
|
+
|
|
487
|
+
encodeField(FIELD.UNKNOWN_2, WIRE_TYPE.VARINT, 1),
|
|
488
|
+
encodeField(FIELD.INSTRUCTION, WIRE_TYPE.LEN, encodeInstruction(system)),
|
|
489
|
+
encodeField(FIELD.UNKNOWN_4, WIRE_TYPE.VARINT, 1),
|
|
490
|
+
encodeField(FIELD.MODEL, WIRE_TYPE.LEN, encodeModel(modelName)),
|
|
491
|
+
encodeField(FIELD.WEB_TOOL, WIRE_TYPE.LEN, ""),
|
|
492
|
+
encodeField(FIELD.UNKNOWN_13, WIRE_TYPE.VARINT, 1),
|
|
493
|
+
encodeField(FIELD.CURSOR_SETTING, WIRE_TYPE.LEN, encodeCursorSetting()),
|
|
494
|
+
encodeField(FIELD.UNKNOWN_19, WIRE_TYPE.VARINT, 1),
|
|
495
|
+
encodeField(FIELD.CONVERSATION_ID, WIRE_TYPE.LEN, uuidv4()),
|
|
496
|
+
encodeField(FIELD.METADATA, WIRE_TYPE.LEN, encodeMetadata()),
|
|
497
|
+
|
|
498
|
+
encodeField(FIELD.IS_AGENTIC, WIRE_TYPE.VARINT, isAgentic ? 1 : 0),
|
|
499
|
+
...(isAgentic ? [encodeField(FIELD.SUPPORTED_TOOLS, WIRE_TYPE.LEN, encodeVarint(1))] : []),
|
|
500
|
+
|
|
501
|
+
...messageIds.map(mid =>
|
|
502
|
+
encodeField(FIELD.MESSAGE_IDS, WIRE_TYPE.LEN, encodeMessageId(mid.messageId, mid.role))
|
|
503
|
+
),
|
|
504
|
+
|
|
505
|
+
...(tools?.length > 0 ? tools.map(tool =>
|
|
506
|
+
encodeField(FIELD.MCP_TOOLS, WIRE_TYPE.LEN, encodeMcpTool(tool))
|
|
507
|
+
) : []),
|
|
508
|
+
|
|
509
|
+
encodeField(FIELD.LARGE_CONTEXT, WIRE_TYPE.VARINT, 0),
|
|
510
|
+
encodeField(FIELD.UNKNOWN_38, WIRE_TYPE.VARINT, 0),
|
|
511
|
+
encodeField(FIELD.UNIFIED_MODE, WIRE_TYPE.VARINT, isAgentic ? UNIFIED_MODE.AGENT : UNIFIED_MODE.CHAT),
|
|
512
|
+
encodeField(FIELD.UNKNOWN_47, WIRE_TYPE.LEN, ""),
|
|
513
|
+
encodeField(FIELD.SHOULD_DISABLE_TOOLS, WIRE_TYPE.VARINT, isAgentic ? 0 : 1),
|
|
514
|
+
encodeField(FIELD.THINKING_LEVEL, WIRE_TYPE.VARINT, thinkingLevel),
|
|
515
|
+
encodeField(FIELD.UNKNOWN_51, WIRE_TYPE.VARINT, 0),
|
|
516
|
+
encodeField(FIELD.UNKNOWN_53, WIRE_TYPE.VARINT, 1),
|
|
517
|
+
encodeField(FIELD.UNIFIED_MODE_NAME, WIRE_TYPE.LEN, isAgentic ? "Agent" : "Ask")
|
|
518
|
+
);
|
|
519
|
+
}
|
|
520
|
+
|
|
521
|
+
export function buildChatRequest(messages, modelName, tools = [], reasoningEffort = null, forceAgentMode = false) {
|
|
522
|
+
return encodeField(FIELD.REQUEST, WIRE_TYPE.LEN, encodeRequest(messages, modelName, tools, reasoningEffort, forceAgentMode));
|
|
523
|
+
}
|
|
524
|
+
|
|
525
|
+
export function buildToolResultRequest(toolResult) {
|
|
526
|
+
const { toolCallId, modelCallId } = parseToolId(toolResult.tool_call_id || "");
|
|
527
|
+
const rawName = toolResult.tool_name || "";
|
|
528
|
+
const resultContent = toolResult.result_content || "";
|
|
529
|
+
|
|
530
|
+
const selectedTool = rawName.startsWith("mcp_custom_")
|
|
531
|
+
? rawName.slice("mcp_custom_".length)
|
|
532
|
+
: rawName.startsWith("mcp_")
|
|
533
|
+
? rawName.slice(4)
|
|
534
|
+
: rawName;
|
|
535
|
+
|
|
536
|
+
const cv2Result = concatArrays(
|
|
537
|
+
encodeField(FIELD.CV2R_TOOL, WIRE_TYPE.VARINT, CLIENT_SIDE_TOOL_V2_MCP),
|
|
538
|
+
encodeField(FIELD.CV2R_MCP_RESULT, WIRE_TYPE.LEN, encodeMcpResult(selectedTool, resultContent)),
|
|
539
|
+
encodeField(FIELD.CV2R_CALL_ID, WIRE_TYPE.LEN, toolCallId),
|
|
540
|
+
...(modelCallId ? [encodeField(FIELD.CV2R_MODEL_CALL_ID, WIRE_TYPE.LEN, modelCallId)] : [])
|
|
541
|
+
|
|
542
|
+
);
|
|
543
|
+
|
|
544
|
+
return encodeField(2, WIRE_TYPE.LEN, cv2Result);
|
|
545
|
+
}
|
|
546
|
+
|
|
547
|
+
export function wrapConnectRPCFrame(payload, compress = false) {
|
|
548
|
+
let finalPayload = payload;
|
|
549
|
+
let flags = 0x00;
|
|
550
|
+
|
|
551
|
+
if (compress) {
|
|
552
|
+
finalPayload = new Uint8Array(zlib.gzipSync(Buffer.from(payload)));
|
|
553
|
+
flags = 0x01;
|
|
554
|
+
}
|
|
555
|
+
|
|
556
|
+
const frame = new Uint8Array(5 + finalPayload.length);
|
|
557
|
+
frame[0] = flags;
|
|
558
|
+
frame[1] = (finalPayload.length >> 24) & 0xFF;
|
|
559
|
+
frame[2] = (finalPayload.length >> 16) & 0xFF;
|
|
560
|
+
frame[3] = (finalPayload.length >> 8) & 0xFF;
|
|
561
|
+
frame[4] = finalPayload.length & 0xFF;
|
|
562
|
+
frame.set(finalPayload, 5);
|
|
563
|
+
|
|
564
|
+
return frame;
|
|
565
|
+
}
|
|
566
|
+
|
|
567
|
+
export function generateCursorBody(messages, modelName, tools = [], reasoningEffort = null, forceAgentMode = false) {
|
|
568
|
+
log("BODY", `Generating: ${messages.length} msgs, model=${modelName}, tools=${tools.length}, reasoning=${reasoningEffort || "none"}, forceAgentMode=${forceAgentMode}`);
|
|
569
|
+
|
|
570
|
+
const protobuf = buildChatRequest(messages, modelName, tools, reasoningEffort, forceAgentMode);
|
|
571
|
+
const framed = wrapConnectRPCFrame(protobuf, false);
|
|
572
|
+
|
|
573
|
+
log("BODY", `Protobuf=${protobuf.length}B, Framed=${framed.length}B`);
|
|
574
|
+
return framed;
|
|
575
|
+
}
|
|
576
|
+
|
|
577
|
+
export function generateToolResultBody(toolResult) {
|
|
578
|
+
const protobuf = buildToolResultRequest(toolResult);
|
|
579
|
+
return wrapConnectRPCFrame(protobuf, false);
|
|
580
|
+
}
|
|
581
|
+
|
|
582
|
+
export function decodeVarint(buffer, offset) {
|
|
583
|
+
let result = 0;
|
|
584
|
+
let shift = 0;
|
|
585
|
+
let pos = offset;
|
|
586
|
+
|
|
587
|
+
while (pos < buffer.length) {
|
|
588
|
+
const b = buffer[pos];
|
|
589
|
+
result |= (b & 0x7F) << shift;
|
|
590
|
+
pos++;
|
|
591
|
+
if (!(b & 0x80)) break;
|
|
592
|
+
shift += 7;
|
|
593
|
+
}
|
|
594
|
+
|
|
595
|
+
return [result, pos];
|
|
596
|
+
}
|
|
597
|
+
|
|
598
|
+
export function decodeField(buffer, offset) {
|
|
599
|
+
if (offset >= buffer.length) return [null, null, null, offset];
|
|
600
|
+
|
|
601
|
+
const [tag, pos1] = decodeVarint(buffer, offset);
|
|
602
|
+
const fieldNum = tag >> 3;
|
|
603
|
+
const wireType = tag & 0x07;
|
|
604
|
+
|
|
605
|
+
let value;
|
|
606
|
+
let pos = pos1;
|
|
607
|
+
|
|
608
|
+
if (wireType === WIRE_TYPE.VARINT) {
|
|
609
|
+
[value, pos] = decodeVarint(buffer, pos);
|
|
610
|
+
} else if (wireType === WIRE_TYPE.LEN) {
|
|
611
|
+
const [length, pos2] = decodeVarint(buffer, pos);
|
|
612
|
+
value = buffer.slice(pos2, pos2 + length);
|
|
613
|
+
pos = pos2 + length;
|
|
614
|
+
} else if (wireType === WIRE_TYPE.FIXED64) {
|
|
615
|
+
value = buffer.slice(pos, pos + 8);
|
|
616
|
+
pos += 8;
|
|
617
|
+
} else if (wireType === WIRE_TYPE.FIXED32) {
|
|
618
|
+
value = buffer.slice(pos, pos + 4);
|
|
619
|
+
pos += 4;
|
|
620
|
+
} else {
|
|
621
|
+
value = null;
|
|
622
|
+
}
|
|
623
|
+
|
|
624
|
+
return [fieldNum, wireType, value, pos];
|
|
625
|
+
}
|
|
626
|
+
|
|
627
|
+
export function decodeMessage(data) {
|
|
628
|
+
const fields = new Map();
|
|
629
|
+
let pos = 0;
|
|
630
|
+
|
|
631
|
+
while (pos < data.length) {
|
|
632
|
+
const [fieldNum, wireType, value, newPos] = decodeField(data, pos);
|
|
633
|
+
if (fieldNum === null) break;
|
|
634
|
+
|
|
635
|
+
if (!fields.has(fieldNum)) fields.set(fieldNum, []);
|
|
636
|
+
fields.get(fieldNum).push({ wireType, value });
|
|
637
|
+
pos = newPos;
|
|
638
|
+
}
|
|
639
|
+
|
|
640
|
+
return fields;
|
|
641
|
+
}
|
|
642
|
+
|
|
643
|
+
export function parseConnectRPCFrame(buffer) {
|
|
644
|
+
if (buffer.length < 5) return null;
|
|
645
|
+
|
|
646
|
+
const flags = buffer[0];
|
|
647
|
+
const length = (buffer[1] << 24) | (buffer[2] << 16) | (buffer[3] << 8) | buffer[4];
|
|
648
|
+
|
|
649
|
+
if (buffer.length < 5 + length) return null;
|
|
650
|
+
|
|
651
|
+
let payload = buffer.slice(5, 5 + length);
|
|
652
|
+
|
|
653
|
+
if (flags === 0x01) {
|
|
654
|
+
try {
|
|
655
|
+
payload = new Uint8Array(zlib.gunzipSync(Buffer.from(payload)));
|
|
656
|
+
} catch (err) {
|
|
657
|
+
log("PARSE", `Decompression failed: ${err.message}`);
|
|
658
|
+
}
|
|
659
|
+
}
|
|
660
|
+
|
|
661
|
+
return { flags, length, payload, consumed: 5 + length };
|
|
662
|
+
}
|
|
663
|
+
|
|
664
|
+
function extractToolCall(toolCallData) {
|
|
665
|
+
const toolCall = decodeMessage(toolCallData);
|
|
666
|
+
let toolCallId = "";
|
|
667
|
+
let toolName = "";
|
|
668
|
+
let rawArgs = "";
|
|
669
|
+
let isLast = false;
|
|
670
|
+
|
|
671
|
+
if (toolCall.has(FIELD.TOOL_ID)) {
|
|
672
|
+
const fullId = new TextDecoder().decode(toolCall.get(FIELD.TOOL_ID)[0].value);
|
|
673
|
+
toolCallId = fullId.split("\n")[0];
|
|
674
|
+
}
|
|
675
|
+
|
|
676
|
+
if (toolCall.has(FIELD.TOOL_NAME)) {
|
|
677
|
+
toolName = new TextDecoder().decode(toolCall.get(FIELD.TOOL_NAME)[0].value);
|
|
678
|
+
}
|
|
679
|
+
|
|
680
|
+
if (toolCall.has(FIELD.TOOL_IS_LAST)) {
|
|
681
|
+
isLast = toolCall.get(FIELD.TOOL_IS_LAST)[0].value !== 0;
|
|
682
|
+
}
|
|
683
|
+
|
|
684
|
+
if (toolCall.has(FIELD.TOOL_MCP_PARAMS)) {
|
|
685
|
+
try {
|
|
686
|
+
const mcpParams = decodeMessage(toolCall.get(FIELD.TOOL_MCP_PARAMS)[0].value);
|
|
687
|
+
|
|
688
|
+
if (mcpParams.has(FIELD.MCP_TOOLS_LIST)) {
|
|
689
|
+
const tool = decodeMessage(mcpParams.get(FIELD.MCP_TOOLS_LIST)[0].value);
|
|
690
|
+
|
|
691
|
+
if (tool.has(FIELD.MCP_NESTED_NAME)) {
|
|
692
|
+
toolName = new TextDecoder().decode(tool.get(FIELD.MCP_NESTED_NAME)[0].value);
|
|
693
|
+
}
|
|
694
|
+
|
|
695
|
+
if (tool.has(FIELD.MCP_NESTED_PARAMS)) {
|
|
696
|
+
rawArgs = new TextDecoder().decode(tool.get(FIELD.MCP_NESTED_PARAMS)[0].value);
|
|
697
|
+
}
|
|
698
|
+
}
|
|
699
|
+
} catch (err) {
|
|
700
|
+
log("EXTRACT", `MCP parse error: ${err.message}`);
|
|
701
|
+
}
|
|
702
|
+
}
|
|
703
|
+
|
|
704
|
+
if (!rawArgs && toolCall.has(FIELD.TOOL_RAW_ARGS)) {
|
|
705
|
+
rawArgs = new TextDecoder().decode(toolCall.get(FIELD.TOOL_RAW_ARGS)[0].value);
|
|
706
|
+
}
|
|
707
|
+
|
|
708
|
+
if (toolCallId && toolName) {
|
|
709
|
+
return {
|
|
710
|
+
id: toolCallId,
|
|
711
|
+
type: "function",
|
|
712
|
+
function: {
|
|
713
|
+
name: toolName,
|
|
714
|
+
arguments: rawArgs || "{}"
|
|
715
|
+
},
|
|
716
|
+
isLast
|
|
717
|
+
};
|
|
718
|
+
}
|
|
719
|
+
|
|
720
|
+
return null;
|
|
721
|
+
}
|
|
722
|
+
|
|
723
|
+
function extractTextAndThinking(responseData) {
|
|
724
|
+
const nested = decodeMessage(responseData);
|
|
725
|
+
let text = null;
|
|
726
|
+
let thinking = null;
|
|
727
|
+
|
|
728
|
+
if (nested.has(FIELD.RESPONSE_TEXT)) {
|
|
729
|
+
text = new TextDecoder().decode(nested.get(FIELD.RESPONSE_TEXT)[0].value);
|
|
730
|
+
}
|
|
731
|
+
|
|
732
|
+
if (nested.has(FIELD.THINKING)) {
|
|
733
|
+
try {
|
|
734
|
+
const thinkingMsg = decodeMessage(nested.get(FIELD.THINKING)[0].value);
|
|
735
|
+
if (thinkingMsg.has(FIELD.THINKING_TEXT)) {
|
|
736
|
+
thinking = new TextDecoder().decode(thinkingMsg.get(FIELD.THINKING_TEXT)[0].value);
|
|
737
|
+
}
|
|
738
|
+
} catch (err) {
|
|
739
|
+
log("EXTRACT", `Thinking parse error: ${err.message}`);
|
|
740
|
+
}
|
|
741
|
+
}
|
|
742
|
+
|
|
743
|
+
return { text, thinking };
|
|
744
|
+
}
|
|
745
|
+
|
|
746
|
+
export function extractTextFromResponse(payload) {
|
|
747
|
+
try {
|
|
748
|
+
const fields = decodeMessage(payload);
|
|
749
|
+
|
|
750
|
+
for (const fieldNum of fields.keys()) {
|
|
751
|
+
if (!KNOWN_RESPONSE_FIELDS.has(fieldNum)) {
|
|
752
|
+
log(
|
|
753
|
+
"SCHEMA",
|
|
754
|
+
`Unknown response field #${fieldNum} detected. Schema v${PROTOBUF_SCHEMA_VERSION} may be outdated.`
|
|
755
|
+
);
|
|
756
|
+
}
|
|
757
|
+
}
|
|
758
|
+
|
|
759
|
+
if (fields.has(FIELD.TOOL_CALL)) {
|
|
760
|
+
const toolCall = extractToolCall(fields.get(FIELD.TOOL_CALL)[0].value);
|
|
761
|
+
if (toolCall) {
|
|
762
|
+
log("EXTRACT", `Tool call: ${toolCall.function.name}`);
|
|
763
|
+
return { text: null, error: null, toolCall, thinking: null };
|
|
764
|
+
}
|
|
765
|
+
}
|
|
766
|
+
|
|
767
|
+
if (fields.has(FIELD.RESPONSE)) {
|
|
768
|
+
const { text, thinking } = extractTextAndThinking(fields.get(FIELD.RESPONSE)[0].value);
|
|
769
|
+
|
|
770
|
+
if (text || thinking) {
|
|
771
|
+
return { text, error: null, toolCall: null, thinking };
|
|
772
|
+
}
|
|
773
|
+
}
|
|
774
|
+
|
|
775
|
+
return { text: null, error: null, toolCall: null, thinking: null };
|
|
776
|
+
} catch (err) {
|
|
777
|
+
log("EXTRACT", `Decode failed (schema v${PROTOBUF_SCHEMA_VERSION}): ${err.message}`);
|
|
778
|
+
return {
|
|
779
|
+
text: null,
|
|
780
|
+
error: null,
|
|
781
|
+
toolCall: null,
|
|
782
|
+
thinking: null,
|
|
783
|
+
raw: Buffer.from(payload).toString("base64"),
|
|
784
|
+
decodeError: err.message
|
|
785
|
+
};
|
|
786
|
+
}
|
|
787
|
+
}
|
|
788
|
+
|
|
789
|
+
export default {
|
|
790
|
+
encodeVarint,
|
|
791
|
+
encodeField,
|
|
792
|
+
encodeMessage,
|
|
793
|
+
buildChatRequest,
|
|
794
|
+
wrapConnectRPCFrame,
|
|
795
|
+
generateCursorBody,
|
|
796
|
+
decodeVarint,
|
|
797
|
+
decodeField,
|
|
798
|
+
decodeMessage,
|
|
799
|
+
parseConnectRPCFrame,
|
|
800
|
+
extractTextFromResponse
|
|
801
|
+
};
|