From 666aecfc7cc10b70608796852392f40e9b9ed0df Mon Sep 17 00:00:00 2001 From: kwanLeeFrmVi Date: Sat, 4 Apr 2026 23:47:39 +0700 Subject: [PATCH] feat(translator): lossless passthrough via CLI tool + provider pairing MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Add clientDetector utility to identify CLI tools (Claude Code, Gemini CLI, Antigravity, Codex) from request headers. When the CLI tool and provider are a native pair, skip all translation — only swap model and Bearer token. Made-with: Cursor --- open-sse/handlers/chatCore.js | 27 +++++++++++----- open-sse/utils/clientDetector.js | 53 ++++++++++++++++++++++++++++++++ 2 files changed, 73 insertions(+), 7 deletions(-) create mode 100644 open-sse/utils/clientDetector.js diff --git a/open-sse/handlers/chatCore.js b/open-sse/handlers/chatCore.js index 5a0193a5..203db79c 100644 --- a/open-sse/handlers/chatCore.js +++ b/open-sse/handlers/chatCore.js @@ -15,6 +15,7 @@ import { buildRequestDetail, extractRequestConfig } from "./chatCore/requestDeta import { handleForcedSSEToJson } from "./chatCore/sseToJsonHandler.js"; import { handleNonStreamingResponse } from "./chatCore/nonStreamingHandler.js"; import { handleStreamingResponse, buildOnStreamComplete } from "./chatCore/streamingHandler.js"; +import { detectClientTool, isNativePassthrough } from "../utils/clientDetector.js"; /** * Core chat handler - shared between SSE and Worker @@ -56,14 +57,26 @@ export async function handleChatCore({ body, modelInfo, credentials, log, onCred reqLogger.logRawRequest(body); log?.debug?.("FORMAT", `${sourceFormat} → ${targetFormat} | stream=${stream}`); - let translatedBody = translateRequest(sourceFormat, targetFormat, model, body, stream, credentials, provider, reqLogger, modelCaps); - if (!translatedBody) { - trackPendingRequest(model, provider, connectionId, false, true); - return createErrorResult(HTTP_STATUS.BAD_REQUEST, `Failed to translate request for ${sourceFormat} → ${targetFormat}`); + // Native passthrough: CLI tool and provider are the same ecosystem + // Skip all translation/normalization — only model and Bearer are swapped + const clientTool = detectClientTool(clientRawRequest?.headers || {}, body); + const passthrough = isNativePassthrough(clientTool, provider); + + let translatedBody; + let toolNameMap; + if (passthrough) { + log?.debug?.("PASSTHROUGH", `${clientTool} → ${provider} | native lossless`); + translatedBody = { ...body, model }; + } else { + translatedBody = translateRequest(sourceFormat, targetFormat, model, body, stream, credentials, provider, reqLogger, modelCaps); + if (!translatedBody) { + trackPendingRequest(model, provider, connectionId, false, true); + return createErrorResult(HTTP_STATUS.BAD_REQUEST, `Failed to translate request for ${sourceFormat} → ${targetFormat}`); + } + toolNameMap = translatedBody._toolNameMap; + delete translatedBody._toolNameMap; + translatedBody.model = model; } - const toolNameMap = translatedBody._toolNameMap; - delete translatedBody._toolNameMap; - translatedBody.model = model; const executor = getExecutor(provider); trackPendingRequest(model, provider, connectionId, true); diff --git a/open-sse/utils/clientDetector.js b/open-sse/utils/clientDetector.js new file mode 100644 index 00000000..99f31970 --- /dev/null +++ b/open-sse/utils/clientDetector.js @@ -0,0 +1,53 @@ +/** + * Detect CLI tool identity from request headers/body. + * Used to determine if a request can be passed through losslessly. + */ + +// Map of CLI tool identifiers to provider IDs they are "native" to +const NATIVE_PAIRS = { + "claude": ["claude", "anthropic"], + "gemini-cli": ["gemini-cli"], + "antigravity": ["antigravity"], + "codex": ["codex"], +}; + +/** + * Detect which CLI tool is making the request. + * Returns one of: "claude" | "gemini-cli" | "antigravity" | "codex" | null + * @param {object} headers - Lowercase header key/value object + * @param {object} body - Parsed request body + */ +export function detectClientTool(headers = {}, body = {}) { + const ua = (headers["user-agent"] || "").toLowerCase(); + const xApp = (headers["x-app"] || "").toLowerCase(); + + // Antigravity: detected via body field (not header) + if (body.userAgent === "antigravity") return "antigravity"; + + // Claude Code / Claude CLI + if (ua.includes("claude-cli") || ua.includes("claude-code") || xApp === "cli") return "claude"; + + // Gemini CLI + if (ua.includes("gemini-cli")) return "gemini-cli"; + + // Codex CLI + if (ua.includes("codex-cli")) return "codex"; + + return null; +} + +/** + * Check if this CLI tool + provider pair should be passed through losslessly. + * @param {string|null} clientTool - Result of detectClientTool() + * @param {string} provider - Provider ID (e.g. "claude", "gemini-cli") + */ +export function isNativePassthrough(clientTool, provider) { + if (!clientTool) return false; + const nativeProviders = NATIVE_PAIRS[clientTool]; + if (!nativeProviders) return false; + // Support anthropic-compatible-* variants + const normalizedProvider = provider.startsWith("anthropic-compatible") + ? "anthropic" + : provider; + return nativeProviders.includes(normalizedProvider); +}