mirror of
https://github.com/Nezumi-2711/9router.git
synced 2026-09-22 13:38:31 +00:00
feat(claude-code): spoof TLS fingerprint and stabilize headers for Anthropic
- Add claudeHeaderCache.js to intercept and cache live Claude Code client headers - Forward cached headers dynamically to api.anthropic.com via default.js - Strip first-party identity headers (x-app, claude-code-* beta) for non-Anthropic upstreams - Validate and sanitize tool call IDs to match Anthropic pattern (^[a-zA-Z0-9_-]+$) - Skip thinking blocks when applying cache_control; fix max_tokens buffer (+1024) - Strip cache_control from thinking blocks in openai-to-claude translator - Comment out thoughtSignature in Gemini translator (kept for reference) - Expand .gitignore to match all deploy*.sh variants Co-authored-by: kwanLeeFrmVi <quanle96@outlook.com> Closes #433 Made-with: Cursor
This commit is contained in:
@@ -1,19 +1,31 @@
|
||||
// Tool call helper functions for translator
|
||||
|
||||
// Generate unique tool call ID
|
||||
// Anthropic tool_use.id must match: ^[a-zA-Z0-9_-]+$
|
||||
const TOOL_ID_PATTERN = /^[a-zA-Z0-9_-]+$/;
|
||||
|
||||
// Generate unique tool call ID (always valid for Anthropic)
|
||||
export function generateToolCallId() {
|
||||
return `call_${Date.now().toString(36)}_${Math.random().toString(36).slice(2, 9)}`;
|
||||
}
|
||||
|
||||
// Ensure all tool_calls have id field and arguments is string (some providers require it)
|
||||
// Sanitize ID to match Anthropic pattern: keep only alphanumeric, underscore, hyphen
|
||||
function sanitizeToolId(id) {
|
||||
if (!id || typeof id !== "string") return null;
|
||||
const sanitized = id.replace(/[^a-zA-Z0-9_-]/g, "");
|
||||
return sanitized.length > 0 ? sanitized : null;
|
||||
}
|
||||
|
||||
// Ensure all tool_calls have valid id field and arguments is string (some providers require it)
|
||||
export function ensureToolCallIds(body) {
|
||||
if (!body.messages || !Array.isArray(body.messages)) return body;
|
||||
|
||||
|
||||
for (const msg of body.messages) {
|
||||
if (msg.role === "assistant" && msg.tool_calls && Array.isArray(msg.tool_calls)) {
|
||||
for (const tc of msg.tool_calls) {
|
||||
if (!tc.id) {
|
||||
tc.id = generateToolCallId();
|
||||
// Validate or regenerate ID for Anthropic compatibility
|
||||
if (!tc.id || !TOOL_ID_PATTERN.test(tc.id)) {
|
||||
const sanitized = sanitizeToolId(tc.id);
|
||||
tc.id = sanitized || generateToolCallId();
|
||||
}
|
||||
if (!tc.type) {
|
||||
tc.type = "function";
|
||||
@@ -24,24 +36,45 @@ export function ensureToolCallIds(body) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Validate tool_call_id in tool messages (role: "tool")
|
||||
if (msg.role === "tool" && msg.tool_call_id && !TOOL_ID_PATTERN.test(msg.tool_call_id)) {
|
||||
const sanitized = sanitizeToolId(msg.tool_call_id);
|
||||
msg.tool_call_id = sanitized || generateToolCallId();
|
||||
}
|
||||
|
||||
// Also validate tool_use blocks in content (Claude format)
|
||||
if (Array.isArray(msg.content)) {
|
||||
for (const block of msg.content) {
|
||||
if (block.type === "tool_use" && block.id && !TOOL_ID_PATTERN.test(block.id)) {
|
||||
const sanitized = sanitizeToolId(block.id);
|
||||
block.id = sanitized || generateToolCallId();
|
||||
}
|
||||
// Validate tool_use_id in tool_result blocks
|
||||
if (block.type === "tool_result" && block.tool_use_id && !TOOL_ID_PATTERN.test(block.tool_use_id)) {
|
||||
const sanitized = sanitizeToolId(block.tool_use_id);
|
||||
block.tool_use_id = sanitized || generateToolCallId();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return body;
|
||||
}
|
||||
|
||||
// Get tool_call ids from assistant message (OpenAI format: tool_calls, Claude format: tool_use in content)
|
||||
export function getToolCallIds(msg) {
|
||||
if (msg.role !== "assistant") return [];
|
||||
|
||||
|
||||
const ids = [];
|
||||
|
||||
|
||||
// OpenAI format: tool_calls array
|
||||
if (msg.tool_calls && Array.isArray(msg.tool_calls)) {
|
||||
for (const tc of msg.tool_calls) {
|
||||
if (tc.id) ids.push(tc.id);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Claude format: tool_use blocks in content
|
||||
if (Array.isArray(msg.content)) {
|
||||
for (const block of msg.content) {
|
||||
@@ -50,19 +83,19 @@ export function getToolCallIds(msg) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return ids;
|
||||
}
|
||||
|
||||
// Check if user message has tool_result for given ids (OpenAI format: role=tool, Claude format: tool_result in content)
|
||||
export function hasToolResults(msg, toolCallIds) {
|
||||
if (!msg || !toolCallIds.length) return false;
|
||||
|
||||
|
||||
// OpenAI format: role = "tool" with tool_call_id
|
||||
if (msg.role === "tool" && msg.tool_call_id) {
|
||||
return toolCallIds.includes(msg.tool_call_id);
|
||||
}
|
||||
|
||||
|
||||
// Claude format: tool_result blocks in user message content
|
||||
if (msg.role === "user" && Array.isArray(msg.content)) {
|
||||
for (const block of msg.content) {
|
||||
@@ -71,26 +104,26 @@ export function hasToolResults(msg, toolCallIds) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
// Fix missing tool responses - insert empty tool_result if assistant has tool_use but next message has no tool_result
|
||||
export function fixMissingToolResponses(body) {
|
||||
if (!body.messages || !Array.isArray(body.messages)) return body;
|
||||
|
||||
|
||||
const newMessages = [];
|
||||
|
||||
|
||||
for (let i = 0; i < body.messages.length; i++) {
|
||||
const msg = body.messages[i];
|
||||
const nextMsg = body.messages[i + 1];
|
||||
|
||||
|
||||
newMessages.push(msg);
|
||||
|
||||
|
||||
// Check if this is assistant with tool_calls/tool_use
|
||||
const toolCallIds = getToolCallIds(msg);
|
||||
if (toolCallIds.length === 0) continue;
|
||||
|
||||
|
||||
// Check if next message has tool_result
|
||||
if (nextMsg && !hasToolResults(nextMsg, toolCallIds)) {
|
||||
// Insert tool responses for each tool_call
|
||||
@@ -104,7 +137,7 @@ export function fixMissingToolResponses(body) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
body.messages = newMessages;
|
||||
return body;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user