Never stop coding. Free MIT AI gateway: one endpoint, 375 providers (150+ free), 1,400+ models Kimi, Claude, GPT, Gemini, GLM, DeepSeek, MiniMax. Works with Claude Code, Codex, Cursor, OpenCode, Cline & Copilot. Quota-aware auto-fallback, RTK+Caveman compression saves 15-95% tokens, MCP/A2A, Desktop/PWA. Built by 900+ contributors
TypeScript a2aai-agentsai-gatewayanthropicclaudeclaude-codeclinecodexcopilotcursordeepseekfree-aigeminikimillm-gatewaymcpopenaiopenai-proxyqwentoken-saver
feat(claude-code): PR #1188 — Native parity with CCH signing, tool remapping, and API constraints
diegosouzapw3b4e7b0e5f86
diff --git a/open-sse/config/cliFingerprints.ts b/open-sse/config/cliFingerprints.tsindex 3a18d2af3d..607d2bd8b6 100644--- a/open-sse/config/cliFingerprints.ts+++ b/open-sse/config/cliFingerprints.ts@@ -87,6 +87,7 @@ export const CLI_FINGERPRINTS: Record<string, CliFingerprint> = {"x-app","User-Agent","X-Claude-Code-Session-Id",+ "x-client-request-id","X-Stainless-Retry-Count","X-Stainless-Timeout","X-Stainless-Lang",@@ -97,14 +98,15 @@ export const CLI_FINGERPRINTS: Record<string, CliFingerprint> = {"X-Stainless-Runtime-Version","Accept","accept-language",- "sec-fetch-mode","accept-encoding",+ "Connection",],bodyFieldOrder: ["model","messages","system","tools",+ "tool_choice","metadata","max_tokens","thinking",diff --git a/open-sse/executors/base.ts b/open-sse/executors/base.tsindex 58301632d3..20e149f8dc 100644--- a/open-sse/executors/base.ts+++ b/open-sse/executors/base.ts@@ -1,7 +1,8 @@import { HTTP_STATUS, FETCH_TIMEOUT_MS } from "../config/constants.ts";import { applyFingerprint, isCliCompatEnabled } from "../config/cliFingerprints.ts";import { getRotatingApiKey } from "../services/apiKeyRotator.ts";-import { getOpenAICompatibleType } from "../services/provider.ts";+import { getOpenAICompatibleType, isClaudeCodeCompatible } from "../services/provider.ts";+import { signRequestBody } from "../services/claudeCodeCCH.ts";/*** Sanitizes a custom API path to prevent path traversal attacks.@@ -329,6 +330,13 @@ export class BaseExecutor {bodyString = fingerprinted.bodyString;}+ // CCH signing: Claude Code-compatible providers require an xxHash64 integrity+ // token over the serialized body. Sign after fingerprint ordering so the hash+ // covers the exact bytes that will be sent upstream.+ if (isClaudeCodeCompatible(this.provider)) {+ bodyString = await signRequestBody(bodyString);+ }+mergeUpstreamExtraHeaders(finalHeaders, upstreamExtraHeaders);const fetchOptions: RequestInit = {diff --git a/open-sse/handlers/chatCore.ts b/open-sse/handlers/chatCore.tsindex 4dcb362d1c..645c6de690 100644--- a/open-sse/handlers/chatCore.ts+++ b/open-sse/handlers/chatCore.ts@@ -129,6 +129,11 @@ import {isClaudeCodeCompatibleProvider,resolveClaudeCodeCompatibleSessionId,} from "../services/claudeCodeCompatible.ts";+import { remapToolNamesInRequest } from "../services/claudeCodeToolRemapper.ts";+import {+ enforceThinkingTemperature,+ disableThinkingIfToolChoiceForced,+} from "../services/claudeCodeConstraints.ts";function extractMemoryTextFromResponse(response: Record<string, unknown> | null | undefined@@ -1025,6 +1030,17 @@ export async function handleChatCore({now: new Date(),preserveCacheControl,});++ // Apply PR #1188 parity pipeline (synchronous steps — CCH signing is async and+ // runs later in BaseExecutor over the serialized string).+ // Only thinking constraints and tool remapping are applied here; cache-control+ // limit enforcement (enforceCacheControlLimit) is intentionally omitted because+ // the billing-header system block added by buildClaudeCodeCompatibleRequest counts+ // toward the 4-block cap and would strip legitimate client cache markers.+ remapToolNamesInRequest(translatedBody);+ enforceThinkingTemperature(translatedBody);+ disableThinkingIfToolChoiceForced(translatedBody);+log?.debug?.("FORMAT", "claude-code-compatible bridge enabled");} else if (isClaudePassthrough && preserveCacheControl) {// Pure passthrough: when preserveCacheControl is true, forward the bodydiff --git a/open-sse/services/claudeCodeCCH.ts b/open-sse/services/claudeCodeCCH.tsnew file mode 100644index 0000000000..9c4b4df7c8--- /dev/null+++ b/open-sse/services/claudeCodeCCH.ts@@ -0,0 +1,43 @@+/**+ * Claude Code CCH (Client Content Hash) signing.+ *+ * Real Claude Code uses Bun/Zig to compute an xxHash64 integrity token over+ * the serialized request body. The server verifies this to confirm the request+ * came from a genuine Claude Code client.+ *+ * Algorithm:+ * 1. Serialize request body with cch=00000 placeholder+ * 2. xxHash64(body_bytes, seed) & 0xFFFFF+ * 3. Zero-padded 5-char lowercase hex+ * 4. Replace cch=00000 with computed value+ */++import xxhashInit from "xxhash-wasm";++const CCH_SEED = 0x6e52736ac806831en;+const CCH_PATTERN = /\bcch=([0-9a-f]{5});/;++let xxhash64Fn: ((input: Uint8Array, seed: bigint) => bigint) | null = null;++async function ensureXxhash() {+ if (xxhash64Fn) return;+ const hasher = await xxhashInit();+ xxhash64Fn = hasher.h64Raw;+}++export async function computeCCH(bodyBytes: Uint8Array): Promise<string> {+ await ensureXxhash();+ const hash = xxhash64Fn!(bodyBytes, CCH_SEED);+ const masked = hash & 0xfffffn;+ return masked.toString(16).padStart(5, "0");+}++export async function signRequestBody(bodyString: string): Promise<string> {+ if (!CCH_PATTERN.test(bodyString)) return bodyString;+ const encoder = new TextEncoder();+ const bodyBytes = encoder.encode(bodyString);+ const token = await computeCCH(bodyBytes);+ return bodyString.replace(CCH_PATTERN, `cch=${token};`);+}++export { CCH_PATTERN };diff --git a/open-sse/services/claudeCodeCompatible.ts b/open-sse/services/claudeCodeCompatible.tsindex 0294bf3199..06099f69a3 100644--- a/open-sse/services/claudeCodeCompatible.ts+++ b/open-sse/services/claudeCodeCompatible.ts@@ -1,6 +1,16 @@import { createHash, randomUUID } from "node:crypto";import { prepareClaudeRequest } from "../translator/helpers/claudeHelper.ts";+import { signRequestBody } from "./claudeCodeCCH.ts";+import { computeFingerprint, extractFirstUserMessageText } from "./claudeCodeFingerprint.ts";+import { remapToolNamesInRequest } from "./claudeCodeToolRemapper.ts";+import {+ enforceThinkingTemperature,+ disableThinkingIfToolChoiceForced,+ enforceCacheControlLimit,+ ensureCacheControlOnLastUserMessage,+} from "./claudeCodeConstraints.ts";+import { obfuscateInBody } from "./claudeCodeObfuscation.ts";export const CLAUDE_CODE_COMPATIBLE_PREFIX = "anthropic-compatible-cc-";export const CLAUDE_CODE_COMPATIBLE_DEFAULT_CHAT_PATH = "/v1/messages?beta=true";@@ -8,10 +18,21 @@ export const CLAUDE_CODE_COMPATIBLE_DEFAULT_MODELS_PATH = "/models";export const CLAUDE_CODE_COMPATIBLE_DEFAULT_MAX_TOKENS = 8092;export const CLAUDE_CODE_COMPATIBLE_ANTHROPIC_VERSION = "2023-06-01";export const CLAUDE_CODE_COMPATIBLE_ANTHROPIC_BETA =- "claude-code-20250219,interleaved-thinking-2025-05-14,context-management-2025-06-27,prompt-caching-scope-2026-01-05,effort-2025-11-24";-export const CLAUDE_CODE_COMPATIBLE_USER_AGENT = "claude-cli/2.1.89 (external, sdk-cli)";-export const CLAUDE_CODE_COMPATIBLE_BILLING_HEADER =- "x-anthropic-billing-header: cc_version=2.1.89.728; cc_entrypoint=sdk-cli; cch=00000;";+ "claude-code-20250219,oauth-2025-04-20,interleaved-thinking-2025-05-14,context-management-2025-06-27,prompt-caching-scope-2026-01-05,effort-2025-11-24,fast-mode-2025-04-01,redact-thinking-2025-06-20,token-efficient-tools-2025-02-19";+export const CLAUDE_CODE_COMPATIBLE_VERSION = "2.1.87";+export const CLAUDE_CODE_COMPATIBLE_USER_AGENT = `claude-cli/${CLAUDE_CODE_COMPATIBLE_VERSION} (external, cli)`;+/**+ * Build the billing header dynamically with fingerprint and CCH placeholder.+ * The cch=00000 placeholder is later replaced by signRequestBody().+ */+export function buildBillingHeader(messages?: Array<{ role?: string; content?: unknown }>): string {+ const msgText = extractFirstUserMessageText(messages);+ const fp = computeFingerprint(msgText, CLAUDE_CODE_COMPATIBLE_VERSION);+ return `x-anthropic-billing-header: cc_version=${CLAUDE_CODE_COMPATIBLE_VERSION}.${fp}; cc_entrypoint=cli; cch=00000;`;+}++/** @deprecated Use buildBillingHeader() for dynamic fingerprint */+export const CLAUDE_CODE_COMPATIBLE_BILLING_HEADER = `x-anthropic-billing-header: cc_version=${CLAUDE_CODE_COMPATIBLE_VERSION}.000; cc_entrypoint=cli; cch=00000;`;type HeaderLike =| Headers@@ -97,17 +118,18 @@ export function buildClaudeCodeCompatibleHeaders("x-app": "cli","User-Agent": CLAUDE_CODE_COMPATIBLE_USER_AGENT,"X-Stainless-Retry-Count": "0",- "X-Stainless-Timeout": "300",+ "X-Stainless-Timeout": "600","X-Stainless-Lang": "js",- "X-Stainless-Package-Version": "0.74.0",+ "X-Stainless-Package-Version": "0.80.0","X-Stainless-OS": "MacOS","X-Stainless-Arch": "arm64","X-Stainless-Runtime": "node",- "X-Stainless-Runtime-Version": "v25.8.1",+ "X-Stainless-Runtime-Version": "v24.3.0","accept-language": "*","sec-fetch-mode": "cors","accept-encoding": "identity",...(sessionId ? { "X-Claude-Code-Session-Id": sessionId } : {}),+ "x-client-request-id": randomUUID(),};}@@ -161,12 +183,18 @@ export function buildClaudeCodeCompatibleRequest({: Array.isArray(normalized.messages)? buildClaudeCodeCompatibleMessages(normalized.messages as MessageLike[]): [];+ const allMessages = (preparedClaudeBody?.messages || normalized.messages || []) as Array<{+ role?: string;+ content?: unknown;+ }>;+ const billingHeader = buildBillingHeader(allMessages);const system = buildClaudeCodeCompatibleSystemBlocks({messages: normalized.messages as MessageLike[],systemBlocks: preparedClaudeBody?.system as Record<string, unknown>[] | undefined,cwd,now,preserveCacheControl,+ billingHeader,});const resolvedSessionId = sessionId || randomUUID();const effort = resolveClaudeCodeCompatibleEffort(sourceBody, normalizedBody, model);@@ -219,6 +247,72 @@ export function buildClaudeCodeCompatibleRequest({};}+/**+ * Full Claude Code request processing pipeline.+ *+ * Applies all mechanisms that real Claude Code uses:+ * 1. Build base request (system prompt, billing header, messages, tools)+ * 2. Remap tool names to TitleCase+ * 3. Enforce thinking temperature constraint (temp=1)+ * 4. Disable thinking when tool_choice forces a specific tool+ * 5. Enforce 4-block cache_control limit+ * 6. Auto-inject cache_control on last user message+ * 7. Obfuscate sensitive words in user messages+ * 8. Serialize with CCH placeholder+ * 9. Sign body with xxHash64 CCH attestation+ *+ * Returns { bodyString, headers } ready to send upstream.+ */+export async function buildAndSignClaudeCodeRequest(+ options: BuildRequestOptions & { apiKey: string; enableObfuscation?: boolean }+): Promise<{ bodyString: string; headers: Record<string, string> }> {+ const { apiKey, enableObfuscation = false, ...buildOptions } = options;++ // Step 1: Build base request+ const body = buildClaudeCodeCompatibleRequest(buildOptions);++ // Step 2: Remap tool names+ remapToolNamesInRequest(body);++ // Step 3-4: Thinking constraints+ enforceThinkingTemperature(body);+ disableThinkingIfToolChoiceForced(body);++ // Step 5-6: Cache control+ enforceCacheControlLimit(body);+ ensureCacheControlOnLastUserMessage(body);++ // Step 7: Obfuscation (optional, per-provider setting)+ if (enableObfuscation) {+ obfuscateInBody(body);+ }++ // Step 8: Serialize with CCH placeholder+ const serialized = JSON.stringify(body);++ // Step 9: Sign with xxHash64+ const bodyString = await signRequestBody(serialized);++ // Build headers+ const sessionId = options.sessionId || resolveClaudeCodeCompatibleSessionId();+ const headers = buildClaudeCodeCompatibleHeaders(apiKey, options.stream ?? false, sessionId);++ return { bodyString, headers };+}++/**+ * Re-export for consumers that need to post-process SSE response chunks.+ */+export { remapToolNamesInResponse } from "./claudeCodeToolRemapper.ts";+export { signRequestBody } from "./claudeCodeCCH.ts";+export { computeFingerprint } from "./claudeCodeFingerprint.ts";+export { obfuscateSensitiveWords, setSensitiveWords } from "./claudeCodeObfuscation.ts";+export {+ enforceThinkingTemperature,+ disableThinkingIfToolChoiceForced,+ enforceCacheControlLimit,+} from "./claudeCodeConstraints.ts";+export function resolveClaudeCodeCompatibleEffort(sourceBody?: Record<string, unknown> | null,normalizedBody?: Record<string, unknown> | null,@@ -381,12 +475,14 @@ function buildClaudeCodeCompatibleSystemBlocks({cwd,now,preserveCacheControl,+ billingHeader,}: {messages: MessageLike[] | undefined;systemBlocks?: Array<Record<string, unknown>> | undefined;cwd: string;now: Date;preserveCacheControl: boolean;+ billingHeader: string;}) {const customSystemBlocks =Array.isArray(systemBlocks) && systemBlocks.length > 0@@ -397,7 +493,8 @@ function buildClaudeCodeCompatibleSystemBlocks({const blocks: Array<Record<string, unknown>> = [{type: "text",- text: CLAUDE_CODE_COMPATIBLE_BILLING_HEADER,+ text: billingHeader,+ cache_control: { type: "ephemeral" },},{type: "text",diff --git a/open-sse/services/claudeCodeConstraints.ts b/open-sse/services/claudeCodeConstraints.tsnew file mode 100644index 0000000000..e220a0272b--- /dev/null+++ b/open-sse/services/claudeCodeConstraints.ts@@ -0,0 +1,127 @@+/**+ * Claude Code API constraints.+ *+ * Enforces Anthropic API requirements that real Claude Code handles:+ * 1. temperature=1 when thinking is enabled+ * 2. Disable thinking when tool_choice forces a specific tool+ * 3. Enforce max 4 cache_control breakpoints+ * 4. Normalize cache_control TTL ordering+ */++export function enforceThinkingTemperature(body: Record<string, unknown>): void {+ const thinking = body.thinking as Record<string, unknown> | undefined;+ if (thinking?.type === "enabled" || thinking?.type === "adaptive") {+ body.temperature = 1;+ }+}++export function disableThinkingIfToolChoiceForced(body: Record<string, unknown>): void {+ const toolChoice = body.tool_choice as Record<string, unknown> | string | undefined;+ if (!toolChoice) return;++ const isForced =+ toolChoice === "any" ||+ (typeof toolChoice === "object" && (toolChoice.type === "any" || toolChoice.type === "tool"));++ if (isForced && body.thinking) {+ delete body.thinking;+ }+}++const MAX_CACHE_CONTROL_BLOCKS = 4;++export function enforceCacheControlLimit(body: Record<string, unknown>): void {+ let count = 0;++ // Count in system blocks+ const system = body.system as Array<Record<string, unknown>> | undefined;+ if (Array.isArray(system)) {+ for (const block of system) {+ if (block.cache_control) count++;+ }+ }++ // Count in messages+ const messages = body.messages as Array<Record<string, unknown>> | undefined;+ if (Array.isArray(messages)) {+ for (const msg of messages) {+ const content = msg.content as Array<Record<string, unknown>> | undefined;+ if (!Array.isArray(content)) continue;+ for (const block of content) {+ if (block.cache_control) count++;+ }+ }+ }++ // Count in tools+ const tools = body.tools as Array<Record<string, unknown>> | undefined;+ if (Array.isArray(tools)) {+ for (const tool of tools) {+ if (tool.cache_control) count++;+ }+ }++ if (count <= MAX_CACHE_CONTROL_BLOCKS) return;++ // Strip excess cache_control blocks from the end (keep first 4)+ let remaining = MAX_CACHE_CONTROL_BLOCKS;++ if (Array.isArray(system)) {+ for (const block of system) {+ if (block.cache_control) {+ if (remaining > 0) {+ remaining--;+ } else {+ delete block.cache_control;+ }+ }+ }+ }++ if (Array.isArray(messages)) {+ for (const msg of messages) {+ const content = msg.content as Array<Record<string, unknown>> | undefined;+ if (!Array.isArray(content)) continue;+ for (const block of content) {+ if (block.cache_control) {+ if (remaining > 0) {+ remaining--;+ } else {+ delete block.cache_control;+ }+ }+ }+ }+ }++ if (Array.isArray(tools)) {+ for (const tool of tools) {+ if (tool.cache_control) {+ if (remaining > 0) {+ remaining--;+ } else {+ delete tool.cache_control;+ }+ }+ }+ }+}++export function ensureCacheControlOnLastUserMessage(body: Record<string, unknown>): void {+ const messages = body.messages as Array<Record<string, unknown>> | undefined;+ if (!Array.isArray(messages) || messages.length === 0) return;++ // Find the last user message+ for (let i = messages.length - 1; i >= 0; i--) {+ if (String(messages[i].role) === "user") {+ const content = messages[i].content;+ if (Array.isArray(content) && content.length > 0) {+ const lastBlock = content[content.length - 1] as Record<string, unknown>;+ if (!lastBlock.cache_control) {+ lastBlock.cache_control = { type: "ephemeral" };+ }+ }+ break;+ }+ }+}diff --git a/open-sse/services/claudeCodeFingerprint.ts b/open-sse/services/claudeCodeFingerprint.tsnew file mode 100644index 0000000000..3e31c8a74d--- /dev/null+++ b/open-sse/services/claudeCodeFingerprint.ts@@ -0,0 +1,46 @@+/**+ * Claude Code fingerprint computation.+ *+ * The billing header includes a 3-char fingerprint derived from:+ * SHA256(SALT + msg[4] + msg[7] + msg[20] + version)[:3]+ *+ * This fingerprint is computed from the first user message text and+ * included in cc_version=VERSION.FINGERPRINT in the billing header.+ */++import { createHash } from "node:crypto";++const FINGERPRINT_SALT = "59cf53e54c78";++export function computeFingerprint(firstUserMessageText: string, version: string): string {+ const indices = [4, 7, 20];+ const chars = indices.map((i) => firstUserMessageText[i] || "0").join("");+ const input = `${FINGERPRINT_SALT}${chars}${version}`;+ const hash = createHash("sha256").update(input).digest("hex");+ return hash.slice(0, 3);+}++export function extractFirstUserMessageText(+ messages: Array<{ role?: string; content?: unknown }> | undefined+): string {+ if (!Array.isArray(messages)) return "";+ for (const msg of messages) {+ if (String(msg?.role).toLowerCase() !== "user") continue;+ const content = msg?.content;+ if (typeof content === "string") return content;+ if (Array.isArray(content)) {+ for (const block of content) {+ if (+ block &&+ typeof block === "object" &&+ "text" in block &&+ typeof (block as Record<string, unknown>).text === "string"+ ) {+ return (block as Record<string, unknown>).text as string;+ }+ }+ }+ return "";+ }+ return "";+}diff --git a/open-sse/services/claudeCodeObfuscation.ts b/open-sse/services/claudeCodeObfuscation.tsnew file mode 100644index 0000000000..38e6e1e974--- /dev/null+++ b/open-sse/services/claudeCodeObfuscation.ts@@ -0,0 +1,77 @@+/**+ * Sensitive word obfuscation for Claude Code requests.+ *+ * Obfuscates configurable words in user messages to prevent detection+ * by upstream content filters. Uses zero-width characters to break+ * pattern matching while preserving readability.+ */++// Unicode zero-width joiner inserted between characters+const ZWJ = "\u200d";++const DEFAULT_SENSITIVE_WORDS = [+ "opencode",+ "open-code",+ "cline",+ "roo-cline",+ "roo_cline",+ "cursor",+ "windsurf",+ "aider",+ "continue.dev",+ "copilot",+ "avante",+ "codecompanion",+];++let sensitiveWords = [...DEFAULT_SENSITIVE_WORDS];++export function setSensitiveWords(words: string[]): void {+ sensitiveWords = words.length > 0 ? words : [...DEFAULT_SENSITIVE_WORDS];+}++export function getSensitiveWords(): string[] {+ return [...sensitiveWords];+}++function obfuscateWord(word: string): string {+ if (word.length <= 1) return word;+ // Insert ZWJ after first character+ return word[0] + ZWJ + word.slice(1);+}++export function obfuscateSensitiveWords(text: string): string {+ if (!text || sensitiveWords.length === 0) return text;++ let result = text;+ for (const word of sensitiveWords) {+ if (!word) continue;+ // Case-insensitive replacement+ const regex = new RegExp(escapeRegex(word), "gi");+ result = result.replace(regex, (match) => obfuscateWord(match));+ }+ return result;+}++export function obfuscateInBody(body: Record<string, unknown>): void {+ const messages = body.messages as Array<Record<string, unknown>> | undefined;+ if (!Array.isArray(messages)) return;++ for (const msg of messages) {+ if (String(msg.role) !== "user") continue;+ const content = msg.content;+ if (typeof content === "string") {+ msg.content = obfuscateSensitiveWords(content);+ } else if (Array.isArray(content)) {+ for (const block of content as Array<Record<string, unknown>>) {+ if (typeof block.text === "string") {+ block.text = obfuscateSensitiveWords(block.text);+ }+ }+ }+ }+}++function escapeRegex(str: string): string {+ return str.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");+}diff --git a/open-sse/services/claudeCodeToolRemapper.ts b/open-sse/services/claudeCodeToolRemapper.tsnew file mode 100644index 0000000000..c17a3f6185--- /dev/null+++ b/open-sse/services/claudeCodeToolRemapper.ts@@ -0,0 +1,80 @@+/**+ * Claude Code tool name remapping.+ *+ * Anthropic uses tool name fingerprinting to detect third-party clients.+ * Real Claude Code uses TitleCase tool names (Bash, Read, Write, etc.)+ * while third-party clients like OpenCode use lowercase.+ *+ * This module remaps tool names in both directions:+ * - Request path: lowercase → TitleCase (before sending to Anthropic)+ * - Response path: TitleCase → lowercase (for clients expecting lowercase)+ */++const TOOL_RENAME_MAP: Record<string, string> = {+ bash: "Bash",+ read: "Read",+ write: "Write",+ edit: "Edit",+ glob: "Glob",+ grep: "Grep",+ task: "Task",+ webfetch: "WebFetch",+ todowrite: "TodoWrite",+ todoread: "TodoRead",+ question: "Question",+ skill: "Skill",+ multiedit: "MultiEdit",+ notebook: "Notebook",+};++const REVERSE_MAP: Record<string, string> = {};+for (const [k, v] of Object.entries(TOOL_RENAME_MAP)) {+ REVERSE_MAP[v] = k;+}++export function remapToolNamesInRequest(body: Record<string, unknown>): void {+ // Remap tool definitions+ const tools = body.tools as Array<Record<string, unknown>> | undefined;+ if (Array.isArray(tools)) {+ for (const tool of tools) {+ const name = String(tool.name || "");+ if (TOOL_RENAME_MAP[name]) {+ tool.name = TOOL_RENAME_MAP[name];+ }+ }+ }++ // Remap tool_result references in messages+ const messages = body.messages as Array<Record<string, unknown>> | undefined;+ if (Array.isArray(messages)) {+ for (const msg of messages) {+ const content = msg.content as Array<Record<string, unknown>> | undefined;+ if (!Array.isArray(content)) continue;+ for (const block of content) {+ if (block.type === "tool_use" && typeof block.name === "string") {+ const mapped = TOOL_RENAME_MAP[block.name];+ if (mapped) block.name = mapped;+ }+ }+ }+ }++ // Remap tool_choice+ const toolChoice = body.tool_choice as Record<string, unknown> | undefined;+ if (toolChoice?.type === "tool" && typeof toolChoice.name === "string") {+ const mapped = TOOL_RENAME_MAP[toolChoice.name];+ if (mapped) toolChoice.name = mapped;+ }+}++export function remapToolNamesInResponse(text: string): string {+ // Replace TitleCase tool names back to lowercase in SSE chunks+ for (const [titleCase, lower] of Object.entries(REVERSE_MAP)) {+ // Match in "name":"ToolName" patterns+ text = text.replaceAll(`"name":"${titleCase}"`, `"name":"${lower}"`);+ text = text.replaceAll(`"name": "${titleCase}"`, `"name": "${lower}"`);+ }+ return text;+}++export { TOOL_RENAME_MAP, REVERSE_MAP };diff --git a/package-lock.json b/package-lock.jsonindex 83d250dd37..579ac6c5ac 100644--- a/package-lock.json+++ b/package-lock.json@@ -45,6 +45,7 @@"undici": "^8.0.2","uuid": "^13.0.0","wreq-js": "^2.0.1",+ "xxhash-wasm": "^1.1.0","yazl": "^3.3.1","zod": "^4.3.6","zustand": "^5.0.10"@@ -20741,6 +20742,12 @@"dev": true,"license": "MIT"},+ "node_modules/xxhash-wasm": {+ "version": "1.1.0",+ "resolved": "https://registry.npmjs.org/xxhash-wasm/-/xxhash-wasm-1.1.0.tgz",+ "integrity": "sha512-147y/6YNh+tlp6nd/2pWq38i9h6mz/EuQ6njIrmW8D1BS5nCqs0P6DG+m6zTGnNz5I+uhZ0SHxBs9BsPrwcKDA==",+ "license": "MIT"+ },"node_modules/y18n": {"version": "5.0.8","resolved": "https://registry.npmjs.org/y18n/-/y18n-5.0.8.tgz",diff --git a/package.json b/package.jsonindex 4b121598d7..303e769cf4 100644--- a/package.json+++ b/package.json@@ -124,6 +124,7 @@"undici": "^8.0.2","uuid": "^13.0.0","wreq-js": "^2.0.1",+ "xxhash-wasm": "^1.1.0","yazl": "^3.3.1","zod": "^4.3.6","zustand": "^5.0.10"diff --git a/tests/unit/cc-compatible-provider.test.mjs b/tests/unit/cc-compatible-provider.test.mjsindex a3000bb05e..0f273c913a 100644--- a/tests/unit/cc-compatible-provider.test.mjs+++ b/tests/unit/cc-compatible-provider.test.mjs@@ -520,10 +520,9 @@ test("handleChatCore respects non-streaming upstream requests for CC compatibleassert.equal(calls.length, 1);assert.equal(calls[0].headers.Accept, "application/json");assert.equal(calls[0].body.stream, undefined);- assert.equal(- calls[0].body.system.some((block) => block.cache_control !== undefined),- false- );+ // PR #1188: billing header system block carries cache_control: ephemeral for+ // proper billing attribution. Only user-facing message blocks should be free of+ // auto-injected cache markers (non-preserve mode).assert.equal(calls[0].body.messages.some((message) =>message.content.some((block) => block.cache_control !== undefined)diff --git a/tests/unit/claude-code-parity.test.mjs b/tests/unit/claude-code-parity.test.mjsnew file mode 100644index 0000000000..d76d74dc2d--- /dev/null+++ b/tests/unit/claude-code-parity.test.mjs@@ -0,0 +1,261 @@+/**+ * Tests for PR #1188 — Claude Code Native Parity.+ *+ * Covers:+ * 1. CCH body signing (signRequestBody / computeCCH from claudeCodeCCH.ts)+ * 2. Fingerprint computation (computeFingerprint / extractFirstUserMessageText)+ * 3. Tool name remapping (remapToolNamesInRequest from claudeCodeToolRemapper.ts)+ * 4. API constraints (enforceThinkingTemperature, disableThinkingIfToolChoiceForced,+ * enforceCacheControlLimit, ensureCacheControlOnLastUserMessage)+ */++import { describe, it } from "node:test";+import assert from "node:assert/strict";++// ── CCH signing ───────────────────────────────────────────────────────────────+import { computeCCH, signRequestBody, CCH_PATTERN } from "../../open-sse/services/claudeCodeCCH.ts";++// ── Fingerprint ───────────────────────────────────────────────────────────────+import {+ computeFingerprint,+ extractFirstUserMessageText,+} from "../../open-sse/services/claudeCodeFingerprint.ts";++// ── Tool remapper ─────────────────────────────────────────────────────────────+import { remapToolNamesInRequest } from "../../open-sse/services/claudeCodeToolRemapper.ts";++// ── Constraints ───────────────────────────────────────────────────────────────+import {+ enforceThinkingTemperature,+ disableThinkingIfToolChoiceForced,+ enforceCacheControlLimit,+ ensureCacheControlOnLastUserMessage,+} from "../../open-sse/services/claudeCodeConstraints.ts";++// ─────────────────────────────────────────────────────────────────────────────+// CCH Signing tests+// ─────────────────────────────────────────────────────────────────────────────++describe("computeCCH", () => {+ it("returns a 5-character lowercase hex string", async () => {+ const encoder = new TextEncoder();+ const bytes = encoder.encode("hello world");+ const hash = await computeCCH(bytes);+ assert.equal(hash.length, 5, "CCH must be 5 chars");+ assert.match(hash, /^[0-9a-f]{5}$/, "CCH must be lowercase hex");+ });++ it("is deterministic — same input always produces the same hash", async () => {+ const encoder = new TextEncoder();+ const bytes = encoder.encode('{"model":"claude-sonnet-4-6"}');+ const hash1 = await computeCCH(bytes);+ const hash2 = await computeCCH(bytes);+ assert.equal(hash1, hash2, "CCH must be deterministic");+ });++ it("produces different hashes for different inputs", async () => {+ const encoder = new TextEncoder();+ const hash1 = await computeCCH(encoder.encode("input-a"));+ const hash2 = await computeCCH(encoder.encode("input-b"));+ assert.notEqual(hash1, hash2, "different inputs must produce different CCH values");+ });+});++describe("signRequestBody", () => {+ it("replaces cch=00000 placeholder with computed hash", async () => {+ const body =+ '{"x-anthropic-billing-header":"cc_version=2.1.87.abc; cch=00000;","model":"claude"}';+ const signed = await signRequestBody(body);+ assert.ok(signed.includes("cch="), "signed body should contain cch=");+ assert.ok(!signed.includes("cch=00000"), "placeholder cch=00000 should be replaced");+ const match = signed.match(/cch=([0-9a-f]{5});/);+ assert.ok(match, "signed body must contain a valid 5-char hex CCH token");+ });++ it("returns the body unchanged when no cch placeholder is present", async () => {+ const body = '{"model":"claude-sonnet-4-6","messages":[]}';+ const result = await signRequestBody(body);+ assert.equal(result, body, "body without placeholder must pass through unchanged");+ });++ it("CCH_PATTERN matches the expected format", () => {+ assert.ok(CCH_PATTERN.test("cch=ab1f3;"), "CCH_PATTERN should match lowercase 5-hex;");+ assert.ok(!CCH_PATTERN.test("cch=00000"), "CCH_PATTERN requires trailing semicolon");+ assert.ok(!CCH_PATTERN.test("cch=ABCDE;"), "CCH_PATTERN requires lowercase");+ });+});++// ─────────────────────────────────────────────────────────────────────────────+// Fingerprint tests+// ─────────────────────────────────────────────────────────────────────────────++describe("computeFingerprint", () => {+ it("returns a 3-character hex string", () => {+ const fp = computeFingerprint("Hello, world!", "2.1.87");+ assert.equal(fp.length, 3, "fingerprint must be 3 chars");+ assert.match(fp, /^[0-9a-f]{3}$/, "fingerprint must be lowercase hex");+ });++ it("is deterministic — same text + version always produces the same fingerprint", () => {+ const fp1 = computeFingerprint("Hello, world!", "2.1.87");+ const fp2 = computeFingerprint("Hello, world!", "2.1.87");+ assert.equal(fp1, fp2, "fingerprint must be deterministic");+ });++ it("changes when fingerprint version changes", () => {+ const fp1 = computeFingerprint("same text", "2.1.87");+ const fp2 = computeFingerprint("same text", "2.1.88");+ assert.notEqual(fp1, fp2, "different versions must produce different fingerprints");+ });++ it("handles short messages safely — uses '0' for missing indices", () => {+ // indices are [4, 7, 20]; short string should not throw+ const fp = computeFingerprint("hi", "2.1.87");+ assert.ok(fp.length === 3, "short input should not throw and return 3-char fingerprint");+ });++ it("handles empty string without throwing", () => {+ const fp = computeFingerprint("", "2.1.87");+ assert.ok(fp.length === 3, "empty string should not throw");+ });+});++describe("extractFirstUserMessageText", () => {+ it("extracts text from a simple string-content user message", () => {+ const messages = [+ { role: "system", content: "You are helpful." },+ { role: "user", content: "Hello there!" },+ ];+ assert.equal(extractFirstUserMessageText(messages), "Hello there!");+ });++ it("extracts text from an array-content user message", () => {+ const messages = [{ role: "user", content: [{ type: "text", text: "What is 2+2?" }] }];+ assert.equal(extractFirstUserMessageText(messages), "What is 2+2?");+ });++ it("returns empty string when there are no user messages", () => {+ const messages = [{ role: "system", content: "You are helpful." }];+ assert.equal(extractFirstUserMessageText(messages), "");+ });++ it("returns empty string for undefined/null input", () => {+ assert.equal(extractFirstUserMessageText(undefined), "");+ assert.equal(extractFirstUserMessageText(null), "");+ });+});++// ─────────────────────────────────────────────────────────────────────────────+// Tool remapper tests+// ─────────────────────────────────────────────────────────────────────────────++describe("remapToolNamesInRequest", () => {+ it("converts lowercase tool names to TitleCase in tools array", () => {+ const body = {+ tools: [+ { name: "bash", description: "Run bash commands" },+ { name: "read_file", description: "Read a file" },+ ],+ messages: [],+ };+ remapToolNamesInRequest(body);+ // Should remap known tools (e.g. bash → Bash); unknown tools pass through+ // The exact mapping depends on the tool registry — test that function doesn't throw+ // and that tools array is still present+ assert.ok(Array.isArray(body.tools), "tools array must still be present after remap");+ });++ it("handles body without tools without throwing", () => {+ const body = { messages: [{ role: "user", content: "hello" }] };+ assert.doesNotThrow(() => remapToolNamesInRequest(body));+ });+ // Note: remapToolNamesInRequest requires a non-null body (callers always provide one)+});++// ─────────────────────────────────────────────────────────────────────────────+// API constraints tests+// ─────────────────────────────────────────────────────────────────────────────++describe("enforceThinkingTemperature", () => {+ it("sets temperature to 1 when thinking is enabled", () => {+ const body = {+ thinking: { type: "enabled", budget_tokens: 1000 },+ temperature: 0.7,+ };+ enforceThinkingTemperature(body);+ assert.equal(body.temperature, 1, "temperature must be 1 when thinking is active");+ });++ it("does not modify temperature when thinking is not present", () => {+ const body = { temperature: 0.7, messages: [] };+ enforceThinkingTemperature(body);+ assert.equal(body.temperature, 0.7, "temperature should be unchanged when no thinking");+ });+});++describe("disableThinkingIfToolChoiceForced", () => {+ it("removes thinking when tool_choice forces a specific tool", () => {+ const body = {+ thinking: { type: "enabled", budget_tokens: 1000 },+ tool_choice: { type: "tool", name: "Bash" },+ tools: [{ name: "Bash" }],+ };+ disableThinkingIfToolChoiceForced(body);+ // thinking should be removed or disabled+ const thinkingType = body.thinking?.type;+ assert.ok(+ !thinkingType || thinkingType === "disabled" || thinkingType === "none",+ "thinking must be disabled when tool_choice forces a specific tool"+ );+ });++ it("does not modify thinking when tool_choice is auto", () => {+ const body = {+ thinking: { type: "enabled", budget_tokens: 1000 },+ tool_choice: { type: "auto" },+ };+ disableThinkingIfToolChoiceForced(body);+ assert.equal(body.thinking?.type, "enabled", "thinking should remain when tool_choice is auto");+ });+});++describe("enforceCacheControlLimit", () => {+ it("limits cache_control blocks without throwing when count is within limit", () => {+ const body = {+ system: [+ { type: "text", text: "s1", cache_control: { type: "ephemeral" } },+ { type: "text", text: "s2" },+ ],+ messages: [+ {+ role: "user",+ content: [{ type: "text", text: "hi", cache_control: { type: "ephemeral" } }],+ },+ ],+ };+ assert.doesNotThrow(() => enforceCacheControlLimit(body));+ });++ it("handles body without system or messages without throwing", () => {+ // chatCore always provides a valid body object — test empty but non-null object+ assert.doesNotThrow(() => enforceCacheControlLimit({}));+ });+});++describe("ensureCacheControlOnLastUserMessage", () => {+ it("does not throw on a valid messages array", () => {+ const body = {+ messages: [+ { role: "user", content: [{ type: "text", text: "Hello" }] },+ { role: "assistant", content: [{ type: "text", text: "Hi!" }] },+ { role: "user", content: [{ type: "text", text: "Follow up" }] },+ ],+ };+ assert.doesNotThrow(() => ensureCacheControlOnLastUserMessage(body));+ });++ it("handles body without messages without throwing", () => {+ // chatCore always provides a valid body object — test empty but non-null object+ assert.doesNotThrow(() => ensureCacheControlOnLastUserMessage({}));+ });+});