@fudrouter/fsrouter 0.6.64 → 0.6.65
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/open-sse/AGENTS.md +39 -0
- package/open-sse/config/anthropicHeaders.js +87 -0
- package/open-sse/config/appConstants.js +238 -0
- package/open-sse/config/audioRegistry.js +506 -0
- package/open-sse/config/cliFingerprints.js +304 -0
- package/open-sse/config/codexInstructions.js +119 -0
- package/open-sse/config/codexQuotaScopes.js +42 -0
- package/open-sse/config/constants.js +4 -0
- package/open-sse/config/contextEditing.js +68 -0
- package/open-sse/config/credentialLoader.js +83 -0
- package/open-sse/config/defaultThinkingSignature.js +12 -0
- package/open-sse/config/embeddingRegistry.js +264 -0
- package/open-sse/config/errorConfig.js +96 -0
- package/open-sse/config/freeModelCatalog.data.js +121 -0
- package/open-sse/config/freeModelCatalog.js +90 -0
- package/open-sse/config/freeTierCatalog.js +81 -0
- package/open-sse/config/googleTtsLanguages.js +62 -0
- package/open-sse/config/imageParamDefs.js +360 -0
- package/open-sse/config/imageRegistry.js +493 -0
- package/open-sse/config/kiroConstants.js +268 -0
- package/open-sse/config/mediaConfig.js +27 -0
- package/open-sse/config/mediaServiceKinds.js +71 -0
- package/open-sse/config/models.js +13 -0
- package/open-sse/config/moderationRegistry.js +71 -0
- package/open-sse/config/musicRegistry.js +99 -0
- package/open-sse/config/ocrRegistry.js +61 -0
- package/open-sse/config/ollamaModels.js +19 -0
- package/open-sse/config/providerErrorRules.js +114 -0
- package/open-sse/config/providerFieldStrips.js +35 -0
- package/open-sse/config/providerHeaderProfiles.js +191 -0
- package/open-sse/config/providerModels.js +106 -0
- package/open-sse/config/providerRegistry.js +16 -0
- package/open-sse/config/providers.js +19 -0
- package/open-sse/config/registryUtils.js +106 -0
- package/open-sse/config/rerankRegistry.js +177 -0
- package/open-sse/config/runtimeConfig.js +96 -0
- package/open-sse/config/runway.js +39 -0
- package/open-sse/config/searchRegistry.js +153 -0
- package/open-sse/config/toolCloaking.js +199 -0
- package/open-sse/config/ttsModels.js +129 -0
- package/open-sse/config/videoRegistry.js +207 -0
- package/open-sse/executors/adapta-web.js +435 -0
- package/open-sse/executors/antigravity.js +534 -0
- package/open-sse/executors/antigravityUpstreamError.js +13 -0
- package/open-sse/executors/auggie.js +437 -0
- package/open-sse/executors/azure-openai.js +32 -0
- package/open-sse/executors/azure.js +57 -0
- package/open-sse/executors/base.js +181 -0
- package/open-sse/executors/bedrock.js +633 -0
- package/open-sse/executors/blackbox-web.js +578 -0
- package/open-sse/executors/chatgpt-web.js +2042 -0
- package/open-sse/executors/chipotle.js +357 -0
- package/open-sse/executors/claude-web-with-auto-refresh.js +73 -0
- package/open-sse/executors/claude-web.js +586 -0
- package/open-sse/executors/cliproxyapi.js +296 -0
- package/open-sse/executors/cloudflare-ai.js +46 -0
- package/open-sse/executors/codebuddy-cn.js +40 -0
- package/open-sse/executors/codex.js +1324 -0
- package/open-sse/executors/commandCode.js +558 -0
- package/open-sse/executors/commandcode.js +88 -0
- package/open-sse/executors/copilot-m365-connection.js +176 -0
- package/open-sse/executors/copilot-m365-frames.js +192 -0
- package/open-sse/executors/copilot-m365-web.js +299 -0
- package/open-sse/executors/copilot-web.js +580 -0
- package/open-sse/executors/cursor.js +795 -0
- package/open-sse/executors/deepseek-web-with-auto-refresh.js +150 -0
- package/open-sse/executors/deepseek-web.js +875 -0
- package/open-sse/executors/default.js +430 -0
- package/open-sse/executors/devin-cli.js +351 -0
- package/open-sse/executors/doubao-web.js +550 -0
- package/open-sse/executors/duckduckgo-web.js +629 -0
- package/open-sse/executors/forceResponsesUpstream.js +33 -0
- package/open-sse/executors/gemini-business.js +320 -0
- package/open-sse/executors/gemini-cli.js +89 -0
- package/open-sse/executors/gemini-web.js +297 -0
- package/open-sse/executors/github.js +379 -0
- package/open-sse/executors/gitlab.js +569 -0
- package/open-sse/executors/glm.js +361 -0
- package/open-sse/executors/grok-cli.js +119 -0
- package/open-sse/executors/grok-web.js +345 -0
- package/open-sse/executors/huggingchat.js +467 -0
- package/open-sse/executors/iflow.js +108 -0
- package/open-sse/executors/index.js +78 -0
- package/open-sse/executors/inner-ai.js +527 -0
- package/open-sse/executors/kimchi.js +123 -0
- package/open-sse/executors/kimi-web.js +325 -0
- package/open-sse/executors/kimi.js +97 -0
- package/open-sse/executors/kiro.js +503 -0
- package/open-sse/executors/kiroThinking.js +44 -0
- package/open-sse/executors/lmarena.js +395 -0
- package/open-sse/executors/mimo-free.js +167 -0
- package/open-sse/executors/mimocode.js +392 -0
- package/open-sse/executors/ninerouter.js +155 -0
- package/open-sse/executors/nlpcloud.js +438 -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 +581 -0
- package/open-sse/executors/poe-web.js +133 -0
- package/open-sse/executors/pollinations.js +95 -0
- package/open-sse/executors/puter.js +31 -0
- package/open-sse/executors/qoder.js +448 -0
- package/open-sse/executors/qwen-web.js +372 -0
- package/open-sse/executors/qwen.js +129 -0
- package/open-sse/executors/t3-chat-web.js +417 -0
- package/open-sse/executors/theoldllm.js +290 -0
- package/open-sse/executors/trae.js +418 -0
- package/open-sse/executors/v0-vercel-web.js +141 -0
- package/open-sse/executors/venice-web.js +142 -0
- package/open-sse/executors/vertex.js +131 -0
- package/open-sse/executors/vertexMedia.js +236 -0
- package/open-sse/executors/windsurf.js +560 -0
- package/open-sse/executors/xai.js +46 -0
- package/open-sse/executors/xiaomi-tokenplan.js +19 -0
- package/open-sse/executors/yuanbao-web.js +385 -0
- package/open-sse/executors/zed-hosted.js +281 -0
- package/open-sse/executors/zenmux-free.js +247 -0
- package/open-sse/handlers/audioSpeech.js +759 -0
- package/open-sse/handlers/audioTranscription.js +395 -0
- package/open-sse/handlers/audioTranslation.js +181 -0
- package/open-sse/handlers/chatCore/nonStreamingHandler.js +300 -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 +95 -0
- package/open-sse/handlers/chatCore.js +404 -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 +230 -0
- package/open-sse/handlers/imageProviders/_base.js +31 -0
- package/open-sse/handlers/imageProviders/antigravity.js +73 -0
- package/open-sse/handlers/imageProviders/blackForestLabs.js +63 -0
- package/open-sse/handlers/imageProviders/cloudflareAi.js +207 -0
- package/open-sse/handlers/imageProviders/codex.js +217 -0
- package/open-sse/handlers/imageProviders/comfyui.js +25 -0
- package/open-sse/handlers/imageProviders/falAi.js +62 -0
- package/open-sse/handlers/imageProviders/gemini.js +40 -0
- package/open-sse/handlers/imageProviders/huggingface.js +38 -0
- package/open-sse/handlers/imageProviders/index.js +44 -0
- package/open-sse/handlers/imageProviders/leonardo.js +435 -0
- package/open-sse/handlers/imageProviders/nanobanana.js +79 -0
- package/open-sse/handlers/imageProviders/openai.js +61 -0
- package/open-sse/handlers/imageProviders/runwayml.js +72 -0
- package/open-sse/handlers/imageProviders/sdwebui.js +36 -0
- package/open-sse/handlers/imageProviders/stabilityAi.js +59 -0
- package/open-sse/handlers/imageProviders/weavy.js +222 -0
- package/open-sse/handlers/moderations.js +92 -0
- package/open-sse/handlers/ocr.js +89 -0
- package/open-sse/handlers/rerank.js +223 -0
- package/open-sse/handlers/responseSanitizer/reasoning.js +144 -0
- package/open-sse/handlers/responsesHandler.js +99 -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/search.js +1056 -0
- package/open-sse/handlers/sseParser.js +469 -0
- package/open-sse/handlers/sttCore.js +193 -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/handlers/usageExtractor.js +93 -0
- package/open-sse/handlers/videoGenerationCore.js +119 -0
- package/open-sse/handlers/videoProviders/index.js +22 -0
- package/open-sse/handlers/videoProviders/leonardo.js +511 -0
- package/open-sse/handlers/videoProviders/weavy.js +198 -0
- package/open-sse/handlers/webFetch.js +200 -0
- package/open-sse/index.js +79 -0
- package/open-sse/mcp-server/audit.js +283 -0
- package/open-sse/mcp-server/catalog.js +184 -0
- package/open-sse/mcp-server/descriptionCompressor.js +175 -0
- package/open-sse/mcp-server/httpAuthContext.js +32 -0
- package/open-sse/mcp-server/httpTransport.js +229 -0
- package/open-sse/mcp-server/index.js +33 -0
- package/open-sse/mcp-server/mcpCallerIdentity.js +21 -0
- package/open-sse/mcp-server/runtimeHeartbeat.js +110 -0
- package/open-sse/mcp-server/schemas/a2a.js +153 -0
- package/open-sse/mcp-server/schemas/audit.js +17 -0
- package/open-sse/mcp-server/schemas/index.js +166 -0
- package/open-sse/mcp-server/schemas/pickFastestModel.js +90 -0
- package/open-sse/mcp-server/schemas/toolDefinition.js +0 -0
- package/open-sse/mcp-server/schemas/toolSearch.js +32 -0
- package/open-sse/mcp-server/schemas/tools.js +1219 -0
- package/open-sse/mcp-server/scopeEnforcement.js +80 -0
- package/open-sse/mcp-server/server.js +1168 -0
- package/open-sse/mcp-server/toolCardinality.js +78 -0
- package/open-sse/mcp-server/toolSearch/catalog.js +66 -0
- package/open-sse/mcp-server/toolSearch/handler.js +20 -0
- package/open-sse/mcp-server/toolSearch/index.js +10 -0
- package/open-sse/mcp-server/toolSearch/register.js +21 -0
- package/open-sse/mcp-server/toolSearch/search.js +55 -0
- package/open-sse/mcp-server/toolSearch/signature.js +67 -0
- package/open-sse/mcp-server/tools/advancedTools.js +893 -0
- package/open-sse/mcp-server/tools/agentSkillTools.js +69 -0
- package/open-sse/mcp-server/tools/compressionTools.js +309 -0
- package/open-sse/mcp-server/tools/gamificationTools.js +134 -0
- package/open-sse/mcp-server/tools/githubSkillTools.js +102 -0
- package/open-sse/mcp-server/tools/memoryTools.js +118 -0
- package/open-sse/mcp-server/tools/notionTools.js +101 -0
- package/open-sse/mcp-server/tools/obsidianTools.js +307 -0
- package/open-sse/mcp-server/tools/pickFastestModel.js +223 -0
- package/open-sse/mcp-server/tools/pluginTools.js +181 -0
- package/open-sse/mcp-server/tools/poolTools.js +139 -0
- package/open-sse/mcp-server/tools/skillTools.js +113 -0
- package/open-sse/package.json +10 -0
- package/open-sse/providers/REGISTRY_TEMPLATE.js +98 -0
- package/open-sse/providers/capabilities.js +293 -0
- package/open-sse/providers/index.js +51 -0
- package/open-sse/providers/models/helpers.js +20 -0
- package/open-sse/providers/models/namePatterns.js +33 -0
- package/open-sse/providers/models/schema.js +40 -0
- package/open-sse/providers/pricing.js +308 -0
- package/open-sse/providers/registry/alicode-intl.js +30 -0
- package/open-sse/providers/registry/alicode.js +31 -0
- package/open-sse/providers/registry/anthropic.js +37 -0
- package/open-sse/providers/registry/antigravity.js +81 -0
- package/open-sse/providers/registry/assemblyai.js +38 -0
- package/open-sse/providers/registry/aws-polly.js +45 -0
- package/open-sse/providers/registry/azure.js +21 -0
- package/open-sse/providers/registry/black-forest-labs.js +32 -0
- package/open-sse/providers/registry/blackbox.js +41 -0
- package/open-sse/providers/registry/brave-search.js +35 -0
- package/open-sse/providers/registry/byteplus.js +35 -0
- package/open-sse/providers/registry/cartesia.js +36 -0
- package/open-sse/providers/registry/cerebras.js +31 -0
- package/open-sse/providers/registry/chutes.js +24 -0
- package/open-sse/providers/registry/claude.js +87 -0
- package/open-sse/providers/registry/cline.js +51 -0
- package/open-sse/providers/registry/clinepass.js +57 -0
- package/open-sse/providers/registry/cloudflare-ai.js +55 -0
- package/open-sse/providers/registry/codebuddy-cn.js +77 -0
- package/open-sse/providers/registry/codex.js +100 -0
- package/open-sse/providers/registry/cohere.js +27 -0
- package/open-sse/providers/registry/comfyui.js +20 -0
- package/open-sse/providers/registry/commandcode.js +43 -0
- package/open-sse/providers/registry/coqui.js +30 -0
- package/open-sse/providers/registry/cursor.js +58 -0
- package/open-sse/providers/registry/deepgram.js +33 -0
- package/open-sse/providers/registry/deepseek.js +51 -0
- package/open-sse/providers/registry/digitalocean.js +92 -0
- package/open-sse/providers/registry/edge-tts.js +24 -0
- package/open-sse/providers/registry/elevenlabs.js +35 -0
- package/open-sse/providers/registry/exa.js +50 -0
- package/open-sse/providers/registry/fal-ai.js +34 -0
- package/open-sse/providers/registry/featherless.js +34 -0
- package/open-sse/providers/registry/firecrawl.js +34 -0
- package/open-sse/providers/registry/fireworks.js +29 -0
- package/open-sse/providers/registry/gemini-cli.js +58 -0
- package/open-sse/providers/registry/gemini.js +83 -0
- package/open-sse/providers/registry/github.js +88 -0
- package/open-sse/providers/registry/gitlab.js +32 -0
- package/open-sse/providers/registry/glm-cn.js +35 -0
- package/open-sse/providers/registry/glm.js +58 -0
- package/open-sse/providers/registry/google-pse.js +35 -0
- package/open-sse/providers/registry/google-tts.js +24 -0
- package/open-sse/providers/registry/grok-cli.js +86 -0
- package/open-sse/providers/registry/grok-web.js +39 -0
- package/open-sse/providers/registry/groq.js +37 -0
- package/open-sse/providers/registry/huggingface.js +34 -0
- package/open-sse/providers/registry/hyperbolic.js +35 -0
- package/open-sse/providers/registry/ibm-bob.js +27 -0
- package/open-sse/providers/registry/iflow.js +52 -0
- package/open-sse/providers/registry/index.js +210 -0
- package/open-sse/providers/registry/inworld.js +36 -0
- package/open-sse/providers/registry/jina-ai.js +42 -0
- package/open-sse/providers/registry/jina-reader.js +34 -0
- package/open-sse/providers/registry/kilocode.js +51 -0
- package/open-sse/providers/registry/kimchi.js +49 -0
- package/open-sse/providers/registry/kimi-coding.js +65 -0
- package/open-sse/providers/registry/kimi.js +56 -0
- package/open-sse/providers/registry/kiro.js +115 -0
- package/open-sse/providers/registry/linkup.js +34 -0
- package/open-sse/providers/registry/local-device.js +24 -0
- package/open-sse/providers/registry/mimo-free.js +24 -0
- package/open-sse/providers/registry/minimax-cn.js +76 -0
- package/open-sse/providers/registry/minimax.js +83 -0
- package/open-sse/providers/registry/mistral.js +34 -0
- package/open-sse/providers/registry/mmf.js +19 -0
- package/open-sse/providers/registry/nanobanana.js +35 -0
- package/open-sse/providers/registry/nebius.js +27 -0
- package/open-sse/providers/registry/nvidia.js +43 -0
- package/open-sse/providers/registry/ollama-local.js +19 -0
- package/open-sse/providers/registry/ollama.js +36 -0
- package/open-sse/providers/registry/openai.js +84 -0
- package/open-sse/providers/registry/opencode-go.js +41 -0
- package/open-sse/providers/registry/opencode.js +25 -0
- package/open-sse/providers/registry/openrouter.js +60 -0
- package/open-sse/providers/registry/perplexity-agent.js +49 -0
- package/open-sse/providers/registry/perplexity-web.js +33 -0
- package/open-sse/providers/registry/perplexity.js +37 -0
- package/open-sse/providers/registry/playht.js +36 -0
- package/open-sse/providers/registry/qoder.js +54 -0
- package/open-sse/providers/registry/qwen.js +33 -0
- package/open-sse/providers/registry/recraft.js +24 -0
- package/open-sse/providers/registry/runwayml.js +30 -0
- package/open-sse/providers/registry/sdwebui.js +20 -0
- package/open-sse/providers/registry/searchapi.js +35 -0
- package/open-sse/providers/registry/searxng.js +35 -0
- package/open-sse/providers/registry/serper.js +35 -0
- package/open-sse/providers/registry/siliconflow.js +39 -0
- package/open-sse/providers/registry/stability-ai.js +31 -0
- package/open-sse/providers/registry/tavily.js +50 -0
- package/open-sse/providers/registry/together.js +31 -0
- package/open-sse/providers/registry/topaz.js +19 -0
- package/open-sse/providers/registry/tortoise.js +30 -0
- package/open-sse/providers/registry/venice.js +56 -0
- package/open-sse/providers/registry/vercel-ai-gateway.js +41 -0
- package/open-sse/providers/registry/vertex-partner.js +29 -0
- package/open-sse/providers/registry/vertex.js +32 -0
- package/open-sse/providers/registry/volcengine-ark.js +35 -0
- package/open-sse/providers/registry/voyage-ai.js +30 -0
- package/open-sse/providers/registry/xai.js +45 -0
- package/open-sse/providers/registry/xiaomi-mimo.js +46 -0
- package/open-sse/providers/registry/xiaomi-tokenplan.js +63 -0
- package/open-sse/providers/registry/youcom.js +35 -0
- package/open-sse/providers/schema.js +76 -0
- package/open-sse/providers/shared.js +74 -0
- package/open-sse/providers/thinkingLevels.js +48 -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 +50 -0
- package/open-sse/rtk/filters/gitDiff.js +92 -0
- package/open-sse/rtk/filters/gitLog.js +99 -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/headroom.js +215 -0
- package/open-sse/rtk/index.js +155 -0
- package/open-sse/rtk/paleo.js +47 -0
- package/open-sse/rtk/ponytail.js +9 -0
- package/open-sse/rtk/ponytailPrompt.js +52 -0
- package/open-sse/rtk/ponytailPrompts.js +76 -0
- package/open-sse/rtk/pxpipe.js +104 -0
- package/open-sse/rtk/registry.js +38 -0
- package/open-sse/rtk/systemInject.js +98 -0
- package/open-sse/services/accountFallback.js +323 -0
- package/open-sse/services/antigravityHeaders.js +91 -0
- package/open-sse/services/antigravityVersion.js +154 -0
- package/open-sse/services/clinepassModels.js +63 -0
- package/open-sse/services/codexQuotaFetcher.js +312 -0
- package/open-sse/services/codexVerbosity.js +30 -0
- package/open-sse/services/combo/context.js +12 -0
- package/open-sse/services/combo/fingerprintExpansion.js +51 -0
- package/open-sse/services/combo/headroomRanking.js +28 -0
- package/open-sse/services/combo/quotaExhaustionCutoff.js +87 -0
- package/open-sse/services/combo/quotaScoring.js +226 -0
- package/open-sse/services/combo/quotaShareConcurrency.js +25 -0
- package/open-sse/services/combo/quotaShareInflight.js +50 -0
- package/open-sse/services/combo/quotaShareStrategy.js +130 -0
- package/open-sse/services/combo/resolveAutoStrategy.js +231 -0
- package/open-sse/services/combo/responseValidation.js +140 -0
- package/open-sse/services/combo/runtimeUnits.js +195 -0
- package/open-sse/services/combo/sessionStickiness.js +128 -0
- package/open-sse/services/combo/shadowRouting.js +131 -0
- package/open-sse/services/combo/targetExhaustion.js +75 -0
- package/open-sse/services/combo/targetSorters.js +126 -0
- package/open-sse/services/combo/targetTimeoutRunner.js +65 -0
- package/open-sse/services/combo/validateQuality.js +271 -0
- package/open-sse/services/combo.js +571 -0
- package/open-sse/services/comboAgentMiddleware.js +91 -0
- package/open-sse/services/comboConfig.js +153 -0
- package/open-sse/services/comboManifestMetrics.js +10 -0
- package/open-sse/services/comboMetrics.js +278 -0
- package/open-sse/services/compact.js +71 -0
- package/open-sse/services/copilotModels.js +155 -0
- package/open-sse/services/emergencyFallback.js +109 -0
- package/open-sse/services/errorClassifier.js +190 -0
- package/open-sse/services/evalRouting.js +201 -0
- package/open-sse/services/freeWebSearch.js +4 -0
- package/open-sse/services/fusion.js +223 -0
- package/open-sse/services/intentClassifier.js +631 -0
- package/open-sse/services/kimchiModels.js +176 -0
- package/open-sse/services/kiroModels.js +332 -0
- package/open-sse/services/model.js +136 -0
- package/open-sse/services/modelCapabilities.js +5 -0
- package/open-sse/services/modelDeprecation.js +101 -0
- package/open-sse/services/modelFamilyFallback.js +166 -0
- package/open-sse/services/oauthCredentialManager.js +156 -0
- package/open-sse/services/pipeline.js +107 -0
- package/open-sse/services/projectId.js +306 -0
- package/open-sse/services/provider.js +169 -0
- package/open-sse/services/providerCooldownTracker.js +94 -0
- package/open-sse/services/providerDefaultRateLimit.js +11 -0
- package/open-sse/services/proxyAutoSelector.js +24 -0
- package/open-sse/services/qoderModels.js +214 -0
- package/open-sse/services/quotaFetchThrottle.js +103 -0
- package/open-sse/services/quotaMonitor.js +310 -0
- package/open-sse/services/quotaPreflight.js +266 -0
- package/open-sse/services/rateLimitManager/headers.js +56 -0
- package/open-sse/services/rateLimitManager.js +616 -0
- package/open-sse/services/rateLimitSemaphore.js +112 -0
- package/open-sse/services/responsesInputSanitizer.js +99 -0
- package/open-sse/services/slidingWindowLimiter.js +44 -0
- package/open-sse/services/streamRecovery.js +355 -0
- package/open-sse/services/taskAwareRouter.js +227 -0
- package/open-sse/services/taskAwareRouting.js +332 -0
- package/open-sse/services/thinkingBudget.js +247 -0
- package/open-sse/services/tokenRefresh/dedup.js +31 -0
- package/open-sse/services/tokenRefresh/providers.js +624 -0
- package/open-sse/services/tokenRefresh.js +254 -0
- package/open-sse/services/usage/claude.js +147 -0
- package/open-sse/services/usage/codebuddy-cn.js +138 -0
- package/open-sse/services/usage/codex.js +267 -0
- package/open-sse/services/usage/github.js +100 -0
- package/open-sse/services/usage/google.js +241 -0
- package/open-sse/services/usage/grok-cli.js +274 -0
- package/open-sse/services/usage/kiro.js +188 -0
- package/open-sse/services/usage/minimax.js +234 -0
- package/open-sse/services/usage/misc.js +288 -0
- package/open-sse/services/usage/shared.js +70 -0
- package/open-sse/services/usage.js +62 -0
- package/open-sse/services/usageDb.js +2 -0
- package/open-sse/services/webSearchRouting.js +25 -0
- package/open-sse/services/wildcardRouter.js +59 -0
- package/open-sse/shared/clineAuth.js +37 -0
- package/open-sse/shared/machineId.js +19 -0
- package/open-sse/shared/qoder/constants.js +64 -0
- package/open-sse/shared/qoder/cosy.js +175 -0
- package/open-sse/shared/qoder/encoding.js +55 -0
- package/open-sse/transformer/responsesTransformer.js +439 -0
- package/open-sse/transformer/streamToJsonConverter.js +103 -0
- package/open-sse/translator/concerns/chunk.js +11 -0
- package/open-sse/translator/concerns/finishReason.js +63 -0
- package/open-sse/translator/concerns/image.js +124 -0
- package/open-sse/translator/concerns/json.js +5 -0
- package/open-sse/translator/concerns/message.js +7 -0
- package/open-sse/translator/concerns/modality.js +155 -0
- package/open-sse/translator/concerns/paramSupport.js +75 -0
- package/open-sse/translator/concerns/prefetch.js +96 -0
- package/open-sse/translator/concerns/reasoning.js +24 -0
- package/open-sse/translator/concerns/thinking.js +53 -0
- package/open-sse/translator/concerns/thinkingUnified.js +329 -0
- package/open-sse/translator/concerns/toolCall.js +153 -0
- package/open-sse/translator/concerns/usage.js +69 -0
- package/open-sse/translator/formats/claude.js +368 -0
- package/open-sse/translator/formats/gemini.js +380 -0
- package/open-sse/translator/formats/maxTokens.js +34 -0
- package/open-sse/translator/formats/openai.js +134 -0
- package/open-sse/translator/formats/responsesApi.js +141 -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 +252 -0
- package/open-sse/translator/request/antigravity-to-openai.js +242 -0
- package/open-sse/translator/request/claude-to-kiro.js +478 -0
- package/open-sse/translator/request/claude-to-openai.js +263 -0
- package/open-sse/translator/request/gemini-to-openai.js +152 -0
- package/open-sse/translator/request/openai-responses.js +385 -0
- package/open-sse/translator/request/openai-to-claude.js +383 -0
- package/open-sse/translator/request/openai-to-commandcode.js +176 -0
- package/open-sse/translator/request/openai-to-cursor.js +185 -0
- package/open-sse/translator/request/openai-to-gemini.js +442 -0
- package/open-sse/translator/request/openai-to-kiro.js +607 -0
- package/open-sse/translator/request/openai-to-kiro.old.js +278 -0
- package/open-sse/translator/request/openai-to-ollama.js +195 -0
- package/open-sse/translator/request/openai-to-vertex.js +42 -0
- package/open-sse/translator/response/claude-to-openai.js +195 -0
- package/open-sse/translator/response/commandcode-to-openai.js +188 -0
- package/open-sse/translator/response/cursor-to-openai.js +30 -0
- package/open-sse/translator/response/gemini-to-openai.js +147 -0
- package/open-sse/translator/response/kiro-to-claude.js +261 -0
- package/open-sse/translator/response/kiro-to-openai.js +160 -0
- package/open-sse/translator/response/ollama-to-openai.js +134 -0
- package/open-sse/translator/response/openai-responses.js +535 -0
- package/open-sse/translator/response/openai-to-antigravity.js +123 -0
- package/open-sse/translator/response/openai-to-claude.js +265 -0
- package/open-sse/translator/schema/blocks.js +43 -0
- package/open-sse/translator/schema/defaults.js +7 -0
- package/open-sse/translator/schema/finishReasons.js +27 -0
- package/open-sse/translator/schema/index.js +8 -0
- package/open-sse/translator/schema/roles.js +16 -0
- package/open-sse/utils/agentGoalPolicy.js +85 -0
- package/open-sse/utils/aiSdkCompat.js +75 -0
- package/open-sse/utils/bypassHandler.js +298 -0
- package/open-sse/utils/cacheControlPolicy.js +189 -0
- package/open-sse/utils/claudeCloaking.js +155 -0
- package/open-sse/utils/claudeHeaderCache.js +70 -0
- package/open-sse/utils/claudeSignature.js +41 -0
- package/open-sse/utils/clientDetector.js +63 -0
- package/open-sse/utils/codebuddySanitizer.js +96 -0
- package/open-sse/utils/composerToolCalls.js +158 -0
- package/open-sse/utils/cors.js +13 -0
- package/open-sse/utils/cursorChecksum.js +149 -0
- package/open-sse/utils/cursorImages.js +224 -0
- package/open-sse/utils/cursorProtobuf.js +904 -0
- package/open-sse/utils/cursorVersionDetector.js +55 -0
- package/open-sse/utils/debugLog.js +14 -0
- package/open-sse/utils/diagnostics.js +143 -0
- package/open-sse/utils/error.js +164 -0
- package/open-sse/utils/estimateSize.js +33 -0
- package/open-sse/utils/finishReason.js +32 -0
- package/open-sse/utils/flattenToolHistory.js +68 -0
- package/open-sse/utils/headers.js +36 -0
- package/open-sse/utils/heapPressure.js +35 -0
- package/open-sse/utils/jsonToSse.js +125 -0
- package/open-sse/utils/keepaliveThreshold.js +40 -0
- package/open-sse/utils/logger.js +104 -0
- package/open-sse/utils/noThinkingAlias.js +74 -0
- package/open-sse/utils/number.js +7 -0
- package/open-sse/utils/ollamaTransform.js +85 -0
- package/open-sse/utils/opencodeHeaders.js +47 -0
- package/open-sse/utils/progressTracker.js +81 -0
- package/open-sse/utils/providerRequestLogging.js +219 -0
- package/open-sse/utils/proxyFetch.js +369 -0
- package/open-sse/utils/reasoningContentInjector.js +79 -0
- package/open-sse/utils/reasoningFields.js +70 -0
- package/open-sse/utils/requestLogger.js +260 -0
- package/open-sse/utils/responsesInputNormalization.js +92 -0
- package/open-sse/utils/responsesStatePolicy.js +52 -0
- package/open-sse/utils/responsesStreamHelpers.js +49 -0
- package/open-sse/utils/sessionManager.js +240 -0
- package/open-sse/utils/sleep.js +6 -0
- package/open-sse/utils/sse.js +14 -0
- package/open-sse/utils/sseConstants.js +23 -0
- package/open-sse/utils/sseHeartbeat.js +104 -0
- package/open-sse/utils/stream.js +517 -0
- package/open-sse/utils/streamFailureFinalization.js +134 -0
- package/open-sse/utils/streamHandler.js +250 -0
- package/open-sse/utils/streamHelpers.js +137 -0
- package/open-sse/utils/textualToolCall.js +104 -0
- package/open-sse/utils/thinkCloseMarker.js +72 -0
- package/open-sse/utils/thinkTagParser.js +146 -0
- package/open-sse/utils/toolCallArguments.js +12 -0
- package/open-sse/utils/toolDeduper.js +49 -0
- package/open-sse/utils/toolSources.js +31 -0
- package/open-sse/utils/upstreamResponseHeaders.js +25 -0
- package/open-sse/utils/urlSanitize.js +15 -0
- package/open-sse/utils/usageTracking.js +347 -0
- package/package.json +1 -1
|
@@ -0,0 +1,904 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Cursor Protobuf Encoder/Decoder
|
|
3
|
+
* Implements ConnectRPC protobuf wire format for Cursor API
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { v4 as uuidv4 } from "uuid";
|
|
7
|
+
import zlib from "zlib";
|
|
8
|
+
|
|
9
|
+
const DEBUG = process.env.CURSOR_PROTOBUF_DEBUG === "1";
|
|
10
|
+
const log = (tag, ...args) => DEBUG && console.log(`[PROTOBUF:${tag}]`, ...args);
|
|
11
|
+
const textDecoder = new TextDecoder();
|
|
12
|
+
|
|
13
|
+
const PROTOBUF_SCHEMA_VERSION = "1.1.3";
|
|
14
|
+
|
|
15
|
+
// ==================== SCHEMAS ====================
|
|
16
|
+
|
|
17
|
+
const WIRE_TYPE = { VARINT: 0, FIXED64: 1, LEN: 2, FIXED32: 5 };
|
|
18
|
+
|
|
19
|
+
const ROLE = { USER: 1, ASSISTANT: 2 };
|
|
20
|
+
|
|
21
|
+
const UNIFIED_MODE = { CHAT: 1, AGENT: 2 };
|
|
22
|
+
|
|
23
|
+
const THINKING_LEVEL = { UNSPECIFIED: 0, MEDIUM: 1, HIGH: 2 };
|
|
24
|
+
const CLIENT_SIDE_TOOL_V2 = { MCP: 19 };
|
|
25
|
+
const CLIENT_SIDE_TOOL_V2_MCP = 19;
|
|
26
|
+
|
|
27
|
+
const FIELD = {
|
|
28
|
+
// StreamUnifiedChatRequestWithTools (top level)
|
|
29
|
+
REQUEST: 1,
|
|
30
|
+
|
|
31
|
+
// StreamUnifiedChatRequest
|
|
32
|
+
MESSAGES: 1,
|
|
33
|
+
UNKNOWN_2: 2,
|
|
34
|
+
INSTRUCTION: 3,
|
|
35
|
+
UNKNOWN_4: 4,
|
|
36
|
+
MODEL: 5,
|
|
37
|
+
WEB_TOOL: 8,
|
|
38
|
+
UNKNOWN_13: 13,
|
|
39
|
+
CURSOR_SETTING: 15,
|
|
40
|
+
UNKNOWN_19: 19,
|
|
41
|
+
CONVERSATION_ID: 23,
|
|
42
|
+
METADATA: 26,
|
|
43
|
+
IS_AGENTIC: 27,
|
|
44
|
+
SUPPORTED_TOOLS: 29,
|
|
45
|
+
MESSAGE_IDS: 30,
|
|
46
|
+
MCP_TOOLS: 34,
|
|
47
|
+
LARGE_CONTEXT: 35,
|
|
48
|
+
UNKNOWN_38: 38,
|
|
49
|
+
UNIFIED_MODE: 46,
|
|
50
|
+
UNKNOWN_47: 47,
|
|
51
|
+
SHOULD_DISABLE_TOOLS: 48,
|
|
52
|
+
THINKING_LEVEL: 49,
|
|
53
|
+
UNKNOWN_51: 51,
|
|
54
|
+
UNKNOWN_53: 53,
|
|
55
|
+
UNIFIED_MODE_NAME: 54,
|
|
56
|
+
|
|
57
|
+
// ConversationMessage
|
|
58
|
+
MSG_CONTENT: 1,
|
|
59
|
+
MSG_ROLE: 2,
|
|
60
|
+
MSG_ID: 13,
|
|
61
|
+
MSG_TOOL_RESULTS: 18,
|
|
62
|
+
MSG_IS_AGENTIC: 29,
|
|
63
|
+
MSG_SERVER_BUBBLE_ID: 32,
|
|
64
|
+
MSG_UNIFIED_MODE: 47,
|
|
65
|
+
MSG_SUPPORTED_TOOLS: 51,
|
|
66
|
+
|
|
67
|
+
// ConversationMessage.ToolResult
|
|
68
|
+
TOOL_RESULT_CALL_ID: 1,
|
|
69
|
+
TOOL_RESULT_NAME: 2,
|
|
70
|
+
TOOL_RESULT_INDEX: 3,
|
|
71
|
+
TOOL_RESULT_RAW_ARGS: 5,
|
|
72
|
+
TOOL_RESULT_RESULT: 8,
|
|
73
|
+
TOOL_RESULT_TOOL_CALL: 11,
|
|
74
|
+
TOOL_RESULT_MODEL_CALL_ID: 12,
|
|
75
|
+
|
|
76
|
+
// ClientSideToolV2Result (nested inside ToolResult.result)
|
|
77
|
+
CLIENT_RESULT_TOOL: 1,
|
|
78
|
+
CLIENT_RESULT_MCP_RESULT: 28,
|
|
79
|
+
CLIENT_RESULT_TOOL_CALL_ID: 35,
|
|
80
|
+
CLIENT_RESULT_MODEL_CALL_ID: 48,
|
|
81
|
+
CLIENT_RESULT_TOOL_INDEX: 49,
|
|
82
|
+
// Aliases used by encodeClientSideToolV2Result
|
|
83
|
+
CV2R_TOOL: 1,
|
|
84
|
+
CV2R_MCP_RESULT: 28,
|
|
85
|
+
CV2R_CALL_ID: 35,
|
|
86
|
+
CV2R_MODEL_CALL_ID: 48,
|
|
87
|
+
CV2R_TOOL_INDEX: 49,
|
|
88
|
+
|
|
89
|
+
// MCPResult (nested inside ClientSideToolV2Result.mcp_result)
|
|
90
|
+
MCP_RESULT_SELECTED_TOOL: 1,
|
|
91
|
+
MCP_RESULT_RESULT: 2,
|
|
92
|
+
// Aliases used by encodeMcpResult
|
|
93
|
+
MCPR_SELECTED_TOOL: 1,
|
|
94
|
+
MCPR_RESULT: 2,
|
|
95
|
+
|
|
96
|
+
// ClientSideToolV2Call (nested inside ToolResult.tool_call)
|
|
97
|
+
CLIENT_CALL_TOOL: 1,
|
|
98
|
+
CLIENT_CALL_MCP_PARAMS: 27,
|
|
99
|
+
CLIENT_CALL_TOOL_CALL_ID: 3,
|
|
100
|
+
CLIENT_CALL_NAME: 9,
|
|
101
|
+
CLIENT_CALL_RAW_ARGS: 10,
|
|
102
|
+
CLIENT_CALL_TOOL_INDEX: 48,
|
|
103
|
+
CLIENT_CALL_MODEL_CALL_ID: 49,
|
|
104
|
+
// Aliases used by encodeClientSideToolV2Call
|
|
105
|
+
CV2C_TOOL: 1,
|
|
106
|
+
CV2C_MCP_PARAMS: 27,
|
|
107
|
+
CV2C_CALL_ID: 3,
|
|
108
|
+
CV2C_NAME: 9,
|
|
109
|
+
CV2C_RAW_ARGS: 10,
|
|
110
|
+
CV2C_TOOL_INDEX: 48,
|
|
111
|
+
CV2C_MODEL_CALL_ID: 49,
|
|
112
|
+
|
|
113
|
+
// Model
|
|
114
|
+
MODEL_NAME: 1,
|
|
115
|
+
MODEL_EMPTY: 4,
|
|
116
|
+
|
|
117
|
+
// Instruction
|
|
118
|
+
INSTRUCTION_TEXT: 1,
|
|
119
|
+
|
|
120
|
+
// CursorSetting
|
|
121
|
+
SETTING_PATH: 1,
|
|
122
|
+
SETTING_UNKNOWN_3: 3,
|
|
123
|
+
SETTING_UNKNOWN_6: 6,
|
|
124
|
+
SETTING_UNKNOWN_8: 8,
|
|
125
|
+
SETTING_UNKNOWN_9: 9,
|
|
126
|
+
|
|
127
|
+
// CursorSetting.Unknown6
|
|
128
|
+
SETTING6_FIELD_1: 1,
|
|
129
|
+
SETTING6_FIELD_2: 2,
|
|
130
|
+
|
|
131
|
+
// Metadata
|
|
132
|
+
META_PLATFORM: 1,
|
|
133
|
+
META_ARCH: 2,
|
|
134
|
+
META_VERSION: 3,
|
|
135
|
+
META_CWD: 4,
|
|
136
|
+
META_TIMESTAMP: 5,
|
|
137
|
+
|
|
138
|
+
// MessageId
|
|
139
|
+
MSGID_ID: 1,
|
|
140
|
+
MSGID_SUMMARY: 2,
|
|
141
|
+
MSGID_ROLE: 3,
|
|
142
|
+
|
|
143
|
+
// MCPTool
|
|
144
|
+
MCP_TOOL_NAME: 1,
|
|
145
|
+
MCP_TOOL_DESC: 2,
|
|
146
|
+
MCP_TOOL_PARAMS: 3,
|
|
147
|
+
MCP_TOOL_SERVER: 4,
|
|
148
|
+
|
|
149
|
+
// StreamUnifiedChatResponseWithTools (response)
|
|
150
|
+
TOOL_CALL: 1,
|
|
151
|
+
RESPONSE: 2,
|
|
152
|
+
|
|
153
|
+
// ClientSideToolV2Call
|
|
154
|
+
TOOL_ID: 3,
|
|
155
|
+
TOOL_NAME: 9,
|
|
156
|
+
TOOL_RAW_ARGS: 10,
|
|
157
|
+
TOOL_IS_LAST: 11,
|
|
158
|
+
TOOL_IS_LAST_ALT: 15,
|
|
159
|
+
TOOL_MCP_PARAMS: 27,
|
|
160
|
+
|
|
161
|
+
// MCPParams
|
|
162
|
+
MCP_TOOLS_LIST: 1,
|
|
163
|
+
|
|
164
|
+
// MCPParams.Tool (nested)
|
|
165
|
+
MCP_NESTED_NAME: 1,
|
|
166
|
+
MCP_NESTED_PARAMS: 3,
|
|
167
|
+
|
|
168
|
+
// StreamUnifiedChatResponse
|
|
169
|
+
RESPONSE_TEXT: 1,
|
|
170
|
+
THINKING: 25,
|
|
171
|
+
|
|
172
|
+
// Thinking
|
|
173
|
+
THINKING_TEXT: 1
|
|
174
|
+
};
|
|
175
|
+
|
|
176
|
+
// Known response field numbers — used to detect unknown fields from protocol updates
|
|
177
|
+
const KNOWN_RESPONSE_FIELDS = new Set([
|
|
178
|
+
FIELD.TOOL_CALL,
|
|
179
|
+
FIELD.RESPONSE,
|
|
180
|
+
FIELD.TOOL_ID,
|
|
181
|
+
FIELD.TOOL_NAME,
|
|
182
|
+
FIELD.TOOL_RAW_ARGS,
|
|
183
|
+
FIELD.TOOL_IS_LAST,
|
|
184
|
+
FIELD.TOOL_MCP_PARAMS,
|
|
185
|
+
FIELD.RESPONSE_TEXT,
|
|
186
|
+
FIELD.THINKING
|
|
187
|
+
]);
|
|
188
|
+
|
|
189
|
+
// ==================== PRIMITIVE ENCODING ====================
|
|
190
|
+
|
|
191
|
+
export function encodeVarint(value) {
|
|
192
|
+
const bytes = [];
|
|
193
|
+
while (value >= 0x80) {
|
|
194
|
+
bytes.push((value & 0x7F) | 0x80);
|
|
195
|
+
value >>>= 7;
|
|
196
|
+
}
|
|
197
|
+
bytes.push(value & 0x7F);
|
|
198
|
+
return new Uint8Array(bytes);
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
export function encodeField(fieldNum, wireType, value) {
|
|
202
|
+
const tag = (fieldNum << 3) | wireType;
|
|
203
|
+
const tagBytes = encodeVarint(tag);
|
|
204
|
+
|
|
205
|
+
if (wireType === WIRE_TYPE.VARINT) {
|
|
206
|
+
const valueBytes = encodeVarint(value);
|
|
207
|
+
return concatArrays(tagBytes, valueBytes);
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
if (wireType === WIRE_TYPE.LEN) {
|
|
211
|
+
const dataBytes = typeof value === "string"
|
|
212
|
+
? new TextEncoder().encode(value)
|
|
213
|
+
: value instanceof Uint8Array ? value
|
|
214
|
+
: Buffer.isBuffer(value) ? new Uint8Array(value)
|
|
215
|
+
: new Uint8Array(0);
|
|
216
|
+
|
|
217
|
+
const lengthBytes = encodeVarint(dataBytes.length);
|
|
218
|
+
return concatArrays(tagBytes, lengthBytes, dataBytes);
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
return new Uint8Array(0);
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
function concatArrays(...arrays) {
|
|
225
|
+
const totalLength = arrays.reduce((sum, arr) => sum + arr.length, 0);
|
|
226
|
+
const result = new Uint8Array(totalLength);
|
|
227
|
+
let offset = 0;
|
|
228
|
+
for (const arr of arrays) {
|
|
229
|
+
result.set(arr, offset);
|
|
230
|
+
offset += arr.length;
|
|
231
|
+
}
|
|
232
|
+
return result;
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
// ==================== MESSAGE ENCODING ====================
|
|
236
|
+
|
|
237
|
+
/**
|
|
238
|
+
* Format tool name: "toolName" → "mcp_custom_toolName"
|
|
239
|
+
* Also handles: "mcp__server__tool" → "mcp_server_tool"
|
|
240
|
+
*/
|
|
241
|
+
function formatToolName(name) {
|
|
242
|
+
const base = typeof name === "string" && name.length > 0 ? name : "tool";
|
|
243
|
+
|
|
244
|
+
if (base.startsWith("mcp__")) {
|
|
245
|
+
const rest = base.slice("mcp__".length);
|
|
246
|
+
const splitIdx = rest.indexOf("__");
|
|
247
|
+
if (splitIdx >= 0) {
|
|
248
|
+
const server = rest.slice(0, splitIdx) || "custom";
|
|
249
|
+
const toolName = rest.slice(splitIdx + 2) || "tool";
|
|
250
|
+
return `mcp_${server}_${toolName}`;
|
|
251
|
+
}
|
|
252
|
+
return `mcp_custom_${rest || "tool"}`;
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
if (base.startsWith("mcp_")) return base;
|
|
256
|
+
return `mcp_custom_${base}`;
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
/**
|
|
260
|
+
* Parse formatted tool name: "mcp_server_tool" → { serverName, selectedTool }
|
|
261
|
+
*/
|
|
262
|
+
function parseToolName(formattedName) {
|
|
263
|
+
if (typeof formattedName !== "string" || !formattedName.startsWith("mcp_")) {
|
|
264
|
+
return { serverName: "custom", selectedTool: formattedName || "tool" };
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
const tail = formattedName.slice("mcp_".length);
|
|
268
|
+
const splitIdx = tail.indexOf("_");
|
|
269
|
+
if (splitIdx < 0) {
|
|
270
|
+
return { serverName: "custom", selectedTool: tail || "tool" };
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
return {
|
|
274
|
+
serverName: tail.slice(0, splitIdx) || "custom",
|
|
275
|
+
selectedTool: tail.slice(splitIdx + 1) || "tool"
|
|
276
|
+
};
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
/**
|
|
280
|
+
* Parse tool_call_id into { toolCallId, modelCallId }
|
|
281
|
+
* Cursor uses "\nmc_" delimiter for model_call_id
|
|
282
|
+
*/
|
|
283
|
+
function parseToolId(id) {
|
|
284
|
+
const delimiter = "\nmc_";
|
|
285
|
+
const idx = id.indexOf(delimiter);
|
|
286
|
+
if (idx >= 0) {
|
|
287
|
+
return { toolCallId: id.slice(0, idx), modelCallId: id.slice(idx + delimiter.length) };
|
|
288
|
+
}
|
|
289
|
+
return { toolCallId: id, modelCallId: null };
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
/**
|
|
293
|
+
* Encode MCPResult proto: { selected_tool, result }
|
|
294
|
+
*/
|
|
295
|
+
function encodeMcpResult(selectedTool, resultContent) {
|
|
296
|
+
return concatArrays(
|
|
297
|
+
encodeField(FIELD.MCPR_SELECTED_TOOL, WIRE_TYPE.LEN, selectedTool),
|
|
298
|
+
encodeField(FIELD.MCPR_RESULT, WIRE_TYPE.LEN, resultContent)
|
|
299
|
+
);
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
/**
|
|
303
|
+
* Encode ClientSideToolV2Result proto: { tool, mcp_result, call_id, model_call_id, tool_index }
|
|
304
|
+
* Represents the result of executing a tool
|
|
305
|
+
*/
|
|
306
|
+
function encodeClientSideToolV2Result(toolCallId, modelCallId, selectedTool, resultContent, toolIndex = 1) {
|
|
307
|
+
return concatArrays(
|
|
308
|
+
encodeField(FIELD.CV2R_TOOL, WIRE_TYPE.VARINT, CLIENT_SIDE_TOOL_V2_MCP),
|
|
309
|
+
encodeField(FIELD.CV2R_MCP_RESULT, WIRE_TYPE.LEN, encodeMcpResult(selectedTool, resultContent)),
|
|
310
|
+
encodeField(FIELD.CV2R_CALL_ID, WIRE_TYPE.LEN, toolCallId),
|
|
311
|
+
...(modelCallId ? [encodeField(FIELD.CV2R_MODEL_CALL_ID, WIRE_TYPE.LEN, modelCallId)] : []),
|
|
312
|
+
encodeField(FIELD.CV2R_TOOL_INDEX, WIRE_TYPE.VARINT, toolIndex > 0 ? toolIndex : 1)
|
|
313
|
+
);
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
/**
|
|
317
|
+
* Encode MCPParams.Tool nested inside ClientSideToolV2Call
|
|
318
|
+
*/
|
|
319
|
+
function encodeMcpParamsForCall(toolName, rawArgs, serverName) {
|
|
320
|
+
const tool = concatArrays(
|
|
321
|
+
encodeField(FIELD.MCP_TOOL_NAME, WIRE_TYPE.LEN, toolName),
|
|
322
|
+
encodeField(FIELD.MCP_TOOL_PARAMS, WIRE_TYPE.LEN, rawArgs),
|
|
323
|
+
encodeField(FIELD.MCP_TOOL_SERVER, WIRE_TYPE.LEN, serverName)
|
|
324
|
+
);
|
|
325
|
+
return encodeField(FIELD.MCP_TOOLS_LIST, WIRE_TYPE.LEN, tool);
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
/**
|
|
329
|
+
* Encode ClientSideToolV2Call proto: { tool, mcp_params, call_id, name, raw_args, tool_index, model_call_id }
|
|
330
|
+
* Represents a tool call definition
|
|
331
|
+
*/
|
|
332
|
+
function encodeClientSideToolV2Call(toolCallId, toolName, selectedTool, serverName, rawArgs, modelCallId, toolIndex = 1) {
|
|
333
|
+
return concatArrays(
|
|
334
|
+
encodeField(FIELD.CV2C_TOOL, WIRE_TYPE.VARINT, CLIENT_SIDE_TOOL_V2_MCP),
|
|
335
|
+
encodeField(FIELD.CV2C_MCP_PARAMS, WIRE_TYPE.LEN, encodeMcpParamsForCall(selectedTool, rawArgs, serverName)),
|
|
336
|
+
encodeField(FIELD.CV2C_CALL_ID, WIRE_TYPE.LEN, toolCallId),
|
|
337
|
+
encodeField(FIELD.CV2C_NAME, WIRE_TYPE.LEN, toolName),
|
|
338
|
+
encodeField(FIELD.CV2C_RAW_ARGS, WIRE_TYPE.LEN, rawArgs),
|
|
339
|
+
encodeField(FIELD.CV2C_TOOL_INDEX, WIRE_TYPE.VARINT, toolIndex > 0 ? toolIndex : 1),
|
|
340
|
+
...(modelCallId ? [encodeField(FIELD.CV2C_MODEL_CALL_ID, WIRE_TYPE.LEN, modelCallId)] : [])
|
|
341
|
+
);
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
/**
|
|
345
|
+
* Encode ConversationMessage.ToolResult with full structure
|
|
346
|
+
* Matches Cursor proto: tool_call_id, tool_name, tool_index, raw_args, result, tool_call
|
|
347
|
+
*/
|
|
348
|
+
export function encodeToolResult(toolResult) {
|
|
349
|
+
const originalName = toolResult.tool_name || toolResult.name || "";
|
|
350
|
+
const toolName = formatToolName(originalName);
|
|
351
|
+
const rawArgs = toolResult.raw_args || "{}";
|
|
352
|
+
const resultContent = toolResult.result_content || toolResult.result || "";
|
|
353
|
+
const { toolCallId, modelCallId } = parseToolId(toolResult.tool_call_id || "");
|
|
354
|
+
const toolIndex = toolResult.tool_index || toolResult.index || 1;
|
|
355
|
+
|
|
356
|
+
// Parse tool name to extract server and selected tool
|
|
357
|
+
const { serverName, selectedTool } = parseToolName(toolName);
|
|
358
|
+
|
|
359
|
+
return concatArrays(
|
|
360
|
+
encodeField(FIELD.TOOL_RESULT_CALL_ID, WIRE_TYPE.LEN, toolCallId),
|
|
361
|
+
encodeField(FIELD.TOOL_RESULT_NAME, WIRE_TYPE.LEN, toolName),
|
|
362
|
+
encodeField(FIELD.TOOL_RESULT_INDEX, WIRE_TYPE.VARINT, toolIndex > 0 ? toolIndex : 1),
|
|
363
|
+
...(modelCallId ? [encodeField(FIELD.TOOL_RESULT_MODEL_CALL_ID, WIRE_TYPE.LEN, modelCallId)] : []),
|
|
364
|
+
encodeField(FIELD.TOOL_RESULT_RAW_ARGS, WIRE_TYPE.LEN, rawArgs),
|
|
365
|
+
encodeField(FIELD.TOOL_RESULT_RESULT, WIRE_TYPE.LEN,
|
|
366
|
+
encodeClientSideToolV2Result(toolCallId, modelCallId, selectedTool, resultContent, toolIndex)
|
|
367
|
+
),
|
|
368
|
+
encodeField(FIELD.TOOL_RESULT_TOOL_CALL, WIRE_TYPE.LEN,
|
|
369
|
+
encodeClientSideToolV2Call(toolCallId, toolName, selectedTool, serverName, rawArgs, modelCallId, toolIndex)
|
|
370
|
+
)
|
|
371
|
+
);
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
export function encodeMessage(content, role, messageId, chatModeEnum = null, isLast = false, hasTools = false, toolResults = [], serverBubbleId = null) {
|
|
375
|
+
const hasToolResults = toolResults.length > 0;
|
|
376
|
+
return concatArrays(
|
|
377
|
+
encodeField(FIELD.MSG_CONTENT, WIRE_TYPE.LEN, content),
|
|
378
|
+
encodeField(FIELD.MSG_ROLE, WIRE_TYPE.VARINT, role),
|
|
379
|
+
encodeField(FIELD.MSG_ID, WIRE_TYPE.LEN, messageId),
|
|
380
|
+
// Only include server_bubble_id if explicitly provided (last assistant message only)
|
|
381
|
+
...(serverBubbleId ? [encodeField(FIELD.MSG_SERVER_BUBBLE_ID, WIRE_TYPE.LEN, serverBubbleId)] : []),
|
|
382
|
+
...(hasToolResults ? toolResults.map(tr =>
|
|
383
|
+
encodeField(FIELD.MSG_TOOL_RESULTS, WIRE_TYPE.LEN, encodeToolResult(tr))
|
|
384
|
+
) : []),
|
|
385
|
+
encodeField(FIELD.MSG_IS_AGENTIC, WIRE_TYPE.VARINT, hasTools ? 1 : 0),
|
|
386
|
+
encodeField(FIELD.MSG_UNIFIED_MODE, WIRE_TYPE.VARINT, hasTools ? UNIFIED_MODE.AGENT : UNIFIED_MODE.CHAT),
|
|
387
|
+
...(isLast && hasTools ? [encodeField(FIELD.MSG_SUPPORTED_TOOLS, WIRE_TYPE.LEN, encodeVarint(1))] : [])
|
|
388
|
+
);
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
export function encodeInstruction(text) {
|
|
392
|
+
return text ? encodeField(FIELD.INSTRUCTION_TEXT, WIRE_TYPE.LEN, text) : new Uint8Array(0);
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
export function encodeModel(modelName) {
|
|
396
|
+
return concatArrays(
|
|
397
|
+
encodeField(FIELD.MODEL_NAME, WIRE_TYPE.LEN, modelName),
|
|
398
|
+
encodeField(FIELD.MODEL_EMPTY, WIRE_TYPE.LEN, new Uint8Array(0))
|
|
399
|
+
);
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
export function encodeCursorSetting() {
|
|
403
|
+
const unknown6 = concatArrays(
|
|
404
|
+
encodeField(FIELD.SETTING6_FIELD_1, WIRE_TYPE.LEN, new Uint8Array(0)),
|
|
405
|
+
encodeField(FIELD.SETTING6_FIELD_2, WIRE_TYPE.LEN, new Uint8Array(0))
|
|
406
|
+
);
|
|
407
|
+
|
|
408
|
+
return concatArrays(
|
|
409
|
+
encodeField(FIELD.SETTING_PATH, WIRE_TYPE.LEN, "cursor\\aisettings"),
|
|
410
|
+
encodeField(FIELD.SETTING_UNKNOWN_3, WIRE_TYPE.LEN, new Uint8Array(0)),
|
|
411
|
+
encodeField(FIELD.SETTING_UNKNOWN_6, WIRE_TYPE.LEN, unknown6),
|
|
412
|
+
encodeField(FIELD.SETTING_UNKNOWN_8, WIRE_TYPE.VARINT, 1),
|
|
413
|
+
encodeField(FIELD.SETTING_UNKNOWN_9, WIRE_TYPE.VARINT, 1)
|
|
414
|
+
);
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
export function encodeMetadata() {
|
|
418
|
+
return concatArrays(
|
|
419
|
+
encodeField(FIELD.META_PLATFORM, WIRE_TYPE.LEN, process.platform || "linux"),
|
|
420
|
+
encodeField(FIELD.META_ARCH, WIRE_TYPE.LEN, process.arch || "x64"),
|
|
421
|
+
encodeField(FIELD.META_VERSION, WIRE_TYPE.LEN, process.version || "v20.0.0"),
|
|
422
|
+
encodeField(FIELD.META_CWD, WIRE_TYPE.LEN, process.cwd?.() || "/"),
|
|
423
|
+
encodeField(FIELD.META_TIMESTAMP, WIRE_TYPE.LEN, new Date().toISOString())
|
|
424
|
+
);
|
|
425
|
+
}
|
|
426
|
+
|
|
427
|
+
export function encodeMessageId(messageId, role, summaryId = null) {
|
|
428
|
+
return concatArrays(
|
|
429
|
+
encodeField(FIELD.MSGID_ID, WIRE_TYPE.LEN, messageId),
|
|
430
|
+
...(summaryId ? [encodeField(FIELD.MSGID_SUMMARY, WIRE_TYPE.LEN, summaryId)] : []),
|
|
431
|
+
encodeField(FIELD.MSGID_ROLE, WIRE_TYPE.VARINT, role)
|
|
432
|
+
);
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
export function encodeMcpTool(tool) {
|
|
436
|
+
const toolName = tool.function?.name || tool.name || "";
|
|
437
|
+
const toolDesc = tool.function?.description || tool.description || "";
|
|
438
|
+
const inputSchema = tool.function?.parameters || tool.input_schema || {};
|
|
439
|
+
|
|
440
|
+
return concatArrays(
|
|
441
|
+
...(toolName ? [encodeField(FIELD.MCP_TOOL_NAME, WIRE_TYPE.LEN, toolName)] : []),
|
|
442
|
+
...(toolDesc ? [encodeField(FIELD.MCP_TOOL_DESC, WIRE_TYPE.LEN, toolDesc)] : []),
|
|
443
|
+
...(Object.keys(inputSchema).length > 0 ? [encodeField(FIELD.MCP_TOOL_PARAMS, WIRE_TYPE.LEN, JSON.stringify(inputSchema))] : []),
|
|
444
|
+
encodeField(FIELD.MCP_TOOL_SERVER, WIRE_TYPE.LEN, "custom")
|
|
445
|
+
);
|
|
446
|
+
}
|
|
447
|
+
|
|
448
|
+
// ==================== REQUEST BUILDING ====================
|
|
449
|
+
|
|
450
|
+
export function encodeRequest(messages, modelName, tools = [], reasoningEffort = null, forceAgentMode = false) {
|
|
451
|
+
const hasTools = tools?.length > 0;
|
|
452
|
+
const isAgentic = hasTools || forceAgentMode;
|
|
453
|
+
const formattedMessages = [];
|
|
454
|
+
const messageIds = [];
|
|
455
|
+
const normalizedMessages = [];
|
|
456
|
+
|
|
457
|
+
// Guardrail: split mixed assistant payload into separate assistant messages
|
|
458
|
+
// This prevents protobuf encoding errors when tool calls and results are in same message
|
|
459
|
+
for (let i = 0; i < messages.length; i++) {
|
|
460
|
+
const msg = messages[i];
|
|
461
|
+
const hasToolCalls = Array.isArray(msg?.tool_calls) && msg.tool_calls.length > 0;
|
|
462
|
+
const hasToolResults = Array.isArray(msg?.tool_results) && msg.tool_results.length > 0;
|
|
463
|
+
|
|
464
|
+
if (msg?.role === "assistant" && hasToolCalls && hasToolResults) {
|
|
465
|
+
log(
|
|
466
|
+
"ENCODE",
|
|
467
|
+
`normalizing mixed assistant tool payload at msg[${i}] (calls=${msg.tool_calls.length}, results=${msg.tool_results.length})`
|
|
468
|
+
);
|
|
469
|
+
|
|
470
|
+
// Keep assistant tool call message without embedded results
|
|
471
|
+
normalizedMessages.push({
|
|
472
|
+
...msg,
|
|
473
|
+
tool_results: []
|
|
474
|
+
});
|
|
475
|
+
|
|
476
|
+
// Avoid inserting duplicate assistant tool-result message if next one already matches
|
|
477
|
+
const nextMsg = messages[i + 1];
|
|
478
|
+
const nextHasToolResults =
|
|
479
|
+
nextMsg?.role === "assistant" &&
|
|
480
|
+
Array.isArray(nextMsg?.tool_results) &&
|
|
481
|
+
nextMsg.tool_results.length > 0;
|
|
482
|
+
const currentIds = new Set(
|
|
483
|
+
msg.tool_results.map(tr => tr?.tool_call_id).filter(id => typeof id === "string")
|
|
484
|
+
);
|
|
485
|
+
const nextIds = new Set(
|
|
486
|
+
(nextMsg?.tool_results || [])
|
|
487
|
+
.map(tr => tr?.tool_call_id)
|
|
488
|
+
.filter(id => typeof id === "string")
|
|
489
|
+
);
|
|
490
|
+
let sameIds = currentIds.size > 0 && currentIds.size === nextIds.size;
|
|
491
|
+
if (sameIds) {
|
|
492
|
+
for (const id of currentIds) {
|
|
493
|
+
if (!nextIds.has(id)) {
|
|
494
|
+
sameIds = false;
|
|
495
|
+
break;
|
|
496
|
+
}
|
|
497
|
+
}
|
|
498
|
+
}
|
|
499
|
+
|
|
500
|
+
if (!(nextHasToolResults && sameIds)) {
|
|
501
|
+
normalizedMessages.push({
|
|
502
|
+
role: "assistant",
|
|
503
|
+
content: "",
|
|
504
|
+
tool_results: msg.tool_results
|
|
505
|
+
});
|
|
506
|
+
}
|
|
507
|
+
|
|
508
|
+
continue;
|
|
509
|
+
}
|
|
510
|
+
|
|
511
|
+
normalizedMessages.push(msg);
|
|
512
|
+
}
|
|
513
|
+
|
|
514
|
+
// Prepare messages
|
|
515
|
+
for (let i = 0; i < normalizedMessages.length; i++) {
|
|
516
|
+
const msg = normalizedMessages[i];
|
|
517
|
+
const role = msg.role === "user" ? ROLE.USER : ROLE.ASSISTANT;
|
|
518
|
+
const msgId = uuidv4();
|
|
519
|
+
const isLast = i === normalizedMessages.length - 1;
|
|
520
|
+
|
|
521
|
+
formattedMessages.push({
|
|
522
|
+
content: msg.content,
|
|
523
|
+
role,
|
|
524
|
+
messageId: msgId,
|
|
525
|
+
isLast,
|
|
526
|
+
hasTools,
|
|
527
|
+
toolResults: msg.tool_results || []
|
|
528
|
+
});
|
|
529
|
+
|
|
530
|
+
messageIds.push({ messageId: msgId, role });
|
|
531
|
+
}
|
|
532
|
+
|
|
533
|
+
// Map reasoning effort to thinking level
|
|
534
|
+
let thinkingLevel = THINKING_LEVEL.UNSPECIFIED;
|
|
535
|
+
if (reasoningEffort === "medium") thinkingLevel = THINKING_LEVEL.MEDIUM;
|
|
536
|
+
else if (reasoningEffort === "high") thinkingLevel = THINKING_LEVEL.HIGH;
|
|
537
|
+
|
|
538
|
+
// Build request
|
|
539
|
+
return concatArrays(
|
|
540
|
+
// Messages
|
|
541
|
+
...formattedMessages.map(fm =>
|
|
542
|
+
encodeField(FIELD.MESSAGES, WIRE_TYPE.LEN,
|
|
543
|
+
encodeMessage(fm.content, fm.role, fm.messageId, null, fm.isLast, fm.hasTools, fm.toolResults)
|
|
544
|
+
)
|
|
545
|
+
),
|
|
546
|
+
|
|
547
|
+
// Static fields
|
|
548
|
+
encodeField(FIELD.UNKNOWN_2, WIRE_TYPE.VARINT, 1),
|
|
549
|
+
encodeField(FIELD.INSTRUCTION, WIRE_TYPE.LEN, encodeInstruction("")),
|
|
550
|
+
encodeField(FIELD.UNKNOWN_4, WIRE_TYPE.VARINT, 1),
|
|
551
|
+
encodeField(FIELD.MODEL, WIRE_TYPE.LEN, encodeModel(modelName)),
|
|
552
|
+
encodeField(FIELD.WEB_TOOL, WIRE_TYPE.LEN, ""),
|
|
553
|
+
encodeField(FIELD.UNKNOWN_13, WIRE_TYPE.VARINT, 1),
|
|
554
|
+
encodeField(FIELD.CURSOR_SETTING, WIRE_TYPE.LEN, encodeCursorSetting()),
|
|
555
|
+
encodeField(FIELD.UNKNOWN_19, WIRE_TYPE.VARINT, 1),
|
|
556
|
+
encodeField(FIELD.CONVERSATION_ID, WIRE_TYPE.LEN, uuidv4()),
|
|
557
|
+
encodeField(FIELD.METADATA, WIRE_TYPE.LEN, encodeMetadata()),
|
|
558
|
+
|
|
559
|
+
// Tool-related fields
|
|
560
|
+
encodeField(FIELD.IS_AGENTIC, WIRE_TYPE.VARINT, isAgentic ? 1 : 0),
|
|
561
|
+
...(isAgentic ? [encodeField(FIELD.SUPPORTED_TOOLS, WIRE_TYPE.LEN, encodeVarint(1))] : []),
|
|
562
|
+
|
|
563
|
+
// Message IDs
|
|
564
|
+
...messageIds.map(mid =>
|
|
565
|
+
encodeField(FIELD.MESSAGE_IDS, WIRE_TYPE.LEN, encodeMessageId(mid.messageId, mid.role))
|
|
566
|
+
),
|
|
567
|
+
|
|
568
|
+
// MCP Tools
|
|
569
|
+
...(tools?.length > 0 ? tools.map(tool =>
|
|
570
|
+
encodeField(FIELD.MCP_TOOLS, WIRE_TYPE.LEN, encodeMcpTool(tool))
|
|
571
|
+
) : []),
|
|
572
|
+
|
|
573
|
+
// Mode fields
|
|
574
|
+
encodeField(FIELD.LARGE_CONTEXT, WIRE_TYPE.VARINT, 0),
|
|
575
|
+
encodeField(FIELD.UNKNOWN_38, WIRE_TYPE.VARINT, 0),
|
|
576
|
+
encodeField(FIELD.UNIFIED_MODE, WIRE_TYPE.VARINT, isAgentic ? UNIFIED_MODE.AGENT : UNIFIED_MODE.CHAT),
|
|
577
|
+
encodeField(FIELD.UNKNOWN_47, WIRE_TYPE.LEN, ""),
|
|
578
|
+
encodeField(FIELD.SHOULD_DISABLE_TOOLS, WIRE_TYPE.VARINT, isAgentic ? 0 : 1),
|
|
579
|
+
encodeField(FIELD.THINKING_LEVEL, WIRE_TYPE.VARINT, thinkingLevel),
|
|
580
|
+
encodeField(FIELD.UNKNOWN_51, WIRE_TYPE.VARINT, 0),
|
|
581
|
+
encodeField(FIELD.UNKNOWN_53, WIRE_TYPE.VARINT, 1),
|
|
582
|
+
encodeField(FIELD.UNIFIED_MODE_NAME, WIRE_TYPE.LEN, isAgentic ? "Agent" : "Ask")
|
|
583
|
+
);
|
|
584
|
+
}
|
|
585
|
+
|
|
586
|
+
export function buildChatRequest(messages, modelName, tools = [], reasoningEffort = null, forceAgentMode = false) {
|
|
587
|
+
return encodeField(FIELD.REQUEST, WIRE_TYPE.LEN, encodeRequest(messages, modelName, tools, reasoningEffort, forceAgentMode));
|
|
588
|
+
}
|
|
589
|
+
|
|
590
|
+
/**
|
|
591
|
+
* Encode a tool result as ClientSideToolV2Result (field 2 of StreamUnifiedChatRequestWithTools)
|
|
592
|
+
* This is sent as a SEPARATE request frame, not inside conversation messages.
|
|
593
|
+
* Proto: StreamUnifiedChatRequestWithTools.client_side_tool_v2_result = 2
|
|
594
|
+
*/
|
|
595
|
+
export function buildToolResultRequest(toolResult) {
|
|
596
|
+
const { toolCallId, modelCallId } = parseToolId(toolResult.tool_call_id || "");
|
|
597
|
+
const rawName = toolResult.tool_name || "";
|
|
598
|
+
const resultContent = toolResult.result_content || "";
|
|
599
|
+
|
|
600
|
+
// selected_tool = raw tool name (e.g. "Write", "Read") per cursor-api Rust source:
|
|
601
|
+
// McpResult { selected_tool: tool_name, result } where tool_name is the mcpParams.tools[0].name
|
|
602
|
+
// which is the name AFTER server prefix stripping (e.g. "custom_Write" -> name = "Write")
|
|
603
|
+
// Actually cursor-api uses: name = tool_name.slice_unchecked(d+1..) → raw name without "custom_"
|
|
604
|
+
// So selected_tool = raw tool name without any prefix
|
|
605
|
+
const selectedTool = rawName.startsWith("mcp_custom_")
|
|
606
|
+
? rawName.slice("mcp_custom_".length)
|
|
607
|
+
: rawName.startsWith("mcp_")
|
|
608
|
+
? rawName.slice(4)
|
|
609
|
+
: rawName;
|
|
610
|
+
|
|
611
|
+
// ClientSideToolV2Result per proto:
|
|
612
|
+
// field 1 (tool): varint = 19 (MCP)
|
|
613
|
+
// field 28 (mcp_result): LEN { field 1: selected_tool, field 2: result }
|
|
614
|
+
// field 35 (tool_call_id): string
|
|
615
|
+
// field 48 (model_call_id): string (optional)
|
|
616
|
+
// NO tool_index (None in Rust source: encode_tool_result sets tool_index: None)
|
|
617
|
+
const cv2Result = concatArrays(
|
|
618
|
+
encodeField(FIELD.CV2R_TOOL, WIRE_TYPE.VARINT, CLIENT_SIDE_TOOL_V2_MCP),
|
|
619
|
+
encodeField(FIELD.CV2R_MCP_RESULT, WIRE_TYPE.LEN, encodeMcpResult(selectedTool, resultContent)),
|
|
620
|
+
encodeField(FIELD.CV2R_CALL_ID, WIRE_TYPE.LEN, toolCallId),
|
|
621
|
+
...(modelCallId ? [encodeField(FIELD.CV2R_MODEL_CALL_ID, WIRE_TYPE.LEN, modelCallId)] : [])
|
|
622
|
+
// tool_index intentionally omitted (None per Rust source)
|
|
623
|
+
);
|
|
624
|
+
|
|
625
|
+
// StreamUnifiedChatRequestWithTools: field 2 = client_side_tool_v2_result
|
|
626
|
+
return encodeField(2, WIRE_TYPE.LEN, cv2Result);
|
|
627
|
+
}
|
|
628
|
+
|
|
629
|
+
export function wrapConnectRPCFrame(payload, compress = false) {
|
|
630
|
+
let finalPayload = payload;
|
|
631
|
+
let flags = 0x00;
|
|
632
|
+
|
|
633
|
+
if (compress) {
|
|
634
|
+
finalPayload = new Uint8Array(zlib.gzipSync(Buffer.from(payload)));
|
|
635
|
+
flags = 0x01;
|
|
636
|
+
}
|
|
637
|
+
|
|
638
|
+
const frame = new Uint8Array(5 + finalPayload.length);
|
|
639
|
+
frame[0] = flags;
|
|
640
|
+
frame[1] = (finalPayload.length >> 24) & 0xFF;
|
|
641
|
+
frame[2] = (finalPayload.length >> 16) & 0xFF;
|
|
642
|
+
frame[3] = (finalPayload.length >> 8) & 0xFF;
|
|
643
|
+
frame[4] = finalPayload.length & 0xFF;
|
|
644
|
+
frame.set(finalPayload, 5);
|
|
645
|
+
|
|
646
|
+
return frame;
|
|
647
|
+
}
|
|
648
|
+
|
|
649
|
+
export function generateCursorBody(messages, modelName, tools = [], reasoningEffort = null, forceAgentMode = false) {
|
|
650
|
+
log("BODY", `Generating: ${messages.length} msgs, model=${modelName}, tools=${tools.length}, reasoning=${reasoningEffort || "none"}, forceAgentMode=${forceAgentMode}`);
|
|
651
|
+
|
|
652
|
+
const protobuf = buildChatRequest(messages, modelName, tools, reasoningEffort, forceAgentMode);
|
|
653
|
+
const framed = wrapConnectRPCFrame(protobuf, false); // Cursor doesn't support compressed requests
|
|
654
|
+
|
|
655
|
+
log("BODY", `Protobuf=${protobuf.length}B, Framed=${framed.length}B`);
|
|
656
|
+
return framed;
|
|
657
|
+
}
|
|
658
|
+
|
|
659
|
+
/**
|
|
660
|
+
* Generate a framed tool result body to send as a separate request frame.
|
|
661
|
+
* Uses field 2 (client_side_tool_v2_result) of StreamUnifiedChatRequestWithTools.
|
|
662
|
+
*/
|
|
663
|
+
export function generateToolResultBody(toolResult) {
|
|
664
|
+
const protobuf = buildToolResultRequest(toolResult);
|
|
665
|
+
return wrapConnectRPCFrame(protobuf, false);
|
|
666
|
+
}
|
|
667
|
+
|
|
668
|
+
// ==================== PRIMITIVE DECODING ====================
|
|
669
|
+
|
|
670
|
+
export function decodeVarint(buffer, offset) {
|
|
671
|
+
let result = 0;
|
|
672
|
+
let shift = 0;
|
|
673
|
+
let pos = offset;
|
|
674
|
+
|
|
675
|
+
while (pos < buffer.length) {
|
|
676
|
+
const b = buffer[pos];
|
|
677
|
+
result |= (b & 0x7F) << shift;
|
|
678
|
+
pos++;
|
|
679
|
+
if (!(b & 0x80)) break;
|
|
680
|
+
shift += 7;
|
|
681
|
+
}
|
|
682
|
+
|
|
683
|
+
return [result, pos];
|
|
684
|
+
}
|
|
685
|
+
|
|
686
|
+
export function decodeField(buffer, offset) {
|
|
687
|
+
if (offset >= buffer.length) return [null, null, null, offset];
|
|
688
|
+
|
|
689
|
+
const [tag, pos1] = decodeVarint(buffer, offset);
|
|
690
|
+
const fieldNum = tag >> 3;
|
|
691
|
+
const wireType = tag & 0x07;
|
|
692
|
+
|
|
693
|
+
let value;
|
|
694
|
+
let pos = pos1;
|
|
695
|
+
|
|
696
|
+
if (wireType === WIRE_TYPE.VARINT) {
|
|
697
|
+
[value, pos] = decodeVarint(buffer, pos);
|
|
698
|
+
} else if (wireType === WIRE_TYPE.LEN) {
|
|
699
|
+
const [length, pos2] = decodeVarint(buffer, pos);
|
|
700
|
+
value = buffer.slice(pos2, pos2 + length);
|
|
701
|
+
pos = pos2 + length;
|
|
702
|
+
} else if (wireType === WIRE_TYPE.FIXED64) {
|
|
703
|
+
value = buffer.slice(pos, pos + 8);
|
|
704
|
+
pos += 8;
|
|
705
|
+
} else if (wireType === WIRE_TYPE.FIXED32) {
|
|
706
|
+
value = buffer.slice(pos, pos + 4);
|
|
707
|
+
pos += 4;
|
|
708
|
+
} else {
|
|
709
|
+
value = null;
|
|
710
|
+
}
|
|
711
|
+
|
|
712
|
+
return [fieldNum, wireType, value, pos];
|
|
713
|
+
}
|
|
714
|
+
|
|
715
|
+
export function decodeMessage(data) {
|
|
716
|
+
const fields = new Map();
|
|
717
|
+
let pos = 0;
|
|
718
|
+
|
|
719
|
+
while (pos < data.length) {
|
|
720
|
+
const [fieldNum, wireType, value, newPos] = decodeField(data, pos);
|
|
721
|
+
if (fieldNum === null) break;
|
|
722
|
+
|
|
723
|
+
if (!fields.has(fieldNum)) fields.set(fieldNum, []);
|
|
724
|
+
fields.get(fieldNum).push({ wireType, value });
|
|
725
|
+
pos = newPos;
|
|
726
|
+
}
|
|
727
|
+
|
|
728
|
+
return fields;
|
|
729
|
+
}
|
|
730
|
+
|
|
731
|
+
// ==================== RESPONSE PARSING ====================
|
|
732
|
+
|
|
733
|
+
export function parseConnectRPCFrame(buffer) {
|
|
734
|
+
if (buffer.length < 5) return null;
|
|
735
|
+
|
|
736
|
+
const flags = buffer[0];
|
|
737
|
+
const length = (buffer[1] << 24) | (buffer[2] << 16) | (buffer[3] << 8) | buffer[4];
|
|
738
|
+
|
|
739
|
+
if (buffer.length < 5 + length) return null;
|
|
740
|
+
|
|
741
|
+
let payload = buffer.slice(5, 5 + length);
|
|
742
|
+
|
|
743
|
+
// Decompress if gzip
|
|
744
|
+
if (flags === 0x01) {
|
|
745
|
+
try {
|
|
746
|
+
payload = new Uint8Array(zlib.gunzipSync(Buffer.from(payload)));
|
|
747
|
+
} catch (err) {
|
|
748
|
+
log("PARSE", `Decompression failed: ${err.message}`);
|
|
749
|
+
}
|
|
750
|
+
}
|
|
751
|
+
|
|
752
|
+
return { flags, length, payload, consumed: 5 + length };
|
|
753
|
+
}
|
|
754
|
+
|
|
755
|
+
function extractToolCall(toolCallData) {
|
|
756
|
+
const toolCall = decodeMessage(toolCallData);
|
|
757
|
+
let toolCallId = "";
|
|
758
|
+
let toolName = "";
|
|
759
|
+
let rawArgs = "";
|
|
760
|
+
let isLast = false;
|
|
761
|
+
|
|
762
|
+
// Extract tool call ID
|
|
763
|
+
if (toolCall.has(FIELD.TOOL_ID)) {
|
|
764
|
+
const fullId = new TextDecoder().decode(toolCall.get(FIELD.TOOL_ID)[0].value);
|
|
765
|
+
toolCallId = fullId.split("\n")[0]; // Cursor returns multi-line ID, take first line
|
|
766
|
+
}
|
|
767
|
+
|
|
768
|
+
// Extract tool name
|
|
769
|
+
if (toolCall.has(FIELD.TOOL_NAME)) {
|
|
770
|
+
toolName = new TextDecoder().decode(toolCall.get(FIELD.TOOL_NAME)[0].value);
|
|
771
|
+
}
|
|
772
|
+
|
|
773
|
+
// Extract is_last flag
|
|
774
|
+
if (toolCall.has(FIELD.TOOL_IS_LAST)) {
|
|
775
|
+
isLast = toolCall.get(FIELD.TOOL_IS_LAST)[0].value !== 0;
|
|
776
|
+
}
|
|
777
|
+
|
|
778
|
+
// Extract MCP params - nested real tool info
|
|
779
|
+
if (toolCall.has(FIELD.TOOL_MCP_PARAMS)) {
|
|
780
|
+
try {
|
|
781
|
+
const mcpParams = decodeMessage(toolCall.get(FIELD.TOOL_MCP_PARAMS)[0].value);
|
|
782
|
+
|
|
783
|
+
if (mcpParams.has(FIELD.MCP_TOOLS_LIST)) {
|
|
784
|
+
const tool = decodeMessage(mcpParams.get(FIELD.MCP_TOOLS_LIST)[0].value);
|
|
785
|
+
|
|
786
|
+
if (tool.has(FIELD.MCP_NESTED_NAME)) {
|
|
787
|
+
toolName = new TextDecoder().decode(tool.get(FIELD.MCP_NESTED_NAME)[0].value);
|
|
788
|
+
}
|
|
789
|
+
|
|
790
|
+
if (tool.has(FIELD.MCP_NESTED_PARAMS)) {
|
|
791
|
+
rawArgs = new TextDecoder().decode(tool.get(FIELD.MCP_NESTED_PARAMS)[0].value);
|
|
792
|
+
}
|
|
793
|
+
}
|
|
794
|
+
} catch (err) {
|
|
795
|
+
log("EXTRACT", `MCP parse error: ${err.message}`);
|
|
796
|
+
}
|
|
797
|
+
}
|
|
798
|
+
|
|
799
|
+
// Fallback to raw_args
|
|
800
|
+
if (!rawArgs && toolCall.has(FIELD.TOOL_RAW_ARGS)) {
|
|
801
|
+
rawArgs = new TextDecoder().decode(toolCall.get(FIELD.TOOL_RAW_ARGS)[0].value);
|
|
802
|
+
}
|
|
803
|
+
|
|
804
|
+
if (toolCallId && toolName) {
|
|
805
|
+
return {
|
|
806
|
+
id: toolCallId,
|
|
807
|
+
type: "function",
|
|
808
|
+
function: {
|
|
809
|
+
name: toolName,
|
|
810
|
+
arguments: rawArgs || "{}"
|
|
811
|
+
},
|
|
812
|
+
isLast
|
|
813
|
+
};
|
|
814
|
+
}
|
|
815
|
+
|
|
816
|
+
return null;
|
|
817
|
+
}
|
|
818
|
+
|
|
819
|
+
function extractTextAndThinking(responseData) {
|
|
820
|
+
const nested = decodeMessage(responseData);
|
|
821
|
+
let text = null;
|
|
822
|
+
let thinking = null;
|
|
823
|
+
|
|
824
|
+
// Extract text
|
|
825
|
+
if (nested.has(FIELD.RESPONSE_TEXT)) {
|
|
826
|
+
text = new TextDecoder().decode(nested.get(FIELD.RESPONSE_TEXT)[0].value);
|
|
827
|
+
}
|
|
828
|
+
|
|
829
|
+
// Extract thinking
|
|
830
|
+
if (nested.has(FIELD.THINKING)) {
|
|
831
|
+
try {
|
|
832
|
+
const thinkingMsg = decodeMessage(nested.get(FIELD.THINKING)[0].value);
|
|
833
|
+
if (thinkingMsg.has(FIELD.THINKING_TEXT)) {
|
|
834
|
+
thinking = new TextDecoder().decode(thinkingMsg.get(FIELD.THINKING_TEXT)[0].value);
|
|
835
|
+
}
|
|
836
|
+
} catch (err) {
|
|
837
|
+
log("EXTRACT", `Thinking parse error: ${err.message}`);
|
|
838
|
+
}
|
|
839
|
+
}
|
|
840
|
+
|
|
841
|
+
return { text, thinking };
|
|
842
|
+
}
|
|
843
|
+
|
|
844
|
+
export function extractTextFromResponse(payload) {
|
|
845
|
+
try {
|
|
846
|
+
const fields = decodeMessage(payload);
|
|
847
|
+
|
|
848
|
+
// Warn about unknown field numbers — may indicate a Cursor protocol update
|
|
849
|
+
for (const fieldNum of fields.keys()) {
|
|
850
|
+
if (!KNOWN_RESPONSE_FIELDS.has(fieldNum)) {
|
|
851
|
+
log(
|
|
852
|
+
"SCHEMA",
|
|
853
|
+
`Unknown response field #${fieldNum} detected. Schema v${PROTOBUF_SCHEMA_VERSION} may be outdated.`
|
|
854
|
+
);
|
|
855
|
+
}
|
|
856
|
+
}
|
|
857
|
+
|
|
858
|
+
// Field 1: ClientSideToolV2Call
|
|
859
|
+
if (fields.has(FIELD.TOOL_CALL)) {
|
|
860
|
+
const toolCall = extractToolCall(fields.get(FIELD.TOOL_CALL)[0].value);
|
|
861
|
+
if (toolCall) {
|
|
862
|
+
log("EXTRACT", `Tool call: ${toolCall.function.name}`);
|
|
863
|
+
return { text: null, error: null, toolCall, thinking: null };
|
|
864
|
+
}
|
|
865
|
+
}
|
|
866
|
+
|
|
867
|
+
// Field 2: StreamUnifiedChatResponse
|
|
868
|
+
if (fields.has(FIELD.RESPONSE)) {
|
|
869
|
+
const { text, thinking } = extractTextAndThinking(fields.get(FIELD.RESPONSE)[0].value);
|
|
870
|
+
|
|
871
|
+
if (text || thinking) {
|
|
872
|
+
return { text, error: null, toolCall: null, thinking };
|
|
873
|
+
}
|
|
874
|
+
}
|
|
875
|
+
|
|
876
|
+
return { text: null, error: null, toolCall: null, thinking: null };
|
|
877
|
+
} catch (err) {
|
|
878
|
+
log("EXTRACT", `Decode failed (schema v${PROTOBUF_SCHEMA_VERSION}): ${err.message}`);
|
|
879
|
+
return {
|
|
880
|
+
text: null,
|
|
881
|
+
error: null,
|
|
882
|
+
toolCall: null,
|
|
883
|
+
thinking: null,
|
|
884
|
+
raw: Buffer.from(payload).toString("base64"),
|
|
885
|
+
decodeError: err.message
|
|
886
|
+
};
|
|
887
|
+
}
|
|
888
|
+
}
|
|
889
|
+
|
|
890
|
+
// ==================== EXPORTS ====================
|
|
891
|
+
|
|
892
|
+
export default {
|
|
893
|
+
encodeVarint,
|
|
894
|
+
encodeField,
|
|
895
|
+
encodeMessage,
|
|
896
|
+
buildChatRequest,
|
|
897
|
+
wrapConnectRPCFrame,
|
|
898
|
+
generateCursorBody,
|
|
899
|
+
decodeVarint,
|
|
900
|
+
decodeField,
|
|
901
|
+
decodeMessage,
|
|
902
|
+
parseConnectRPCFrame,
|
|
903
|
+
extractTextFromResponse
|
|
904
|
+
};
|