From 27fdb7e1574bba81821a5454648f8212f396dbbf Mon Sep 17 00:00:00 2001 From: 1e0n Date: Thu, 9 Oct 2025 15:05:30 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=20/v1/messages=20=E7=AB=AF?= =?UTF-8?q?=E7=82=B9=20x-api-key=20=E8=AE=A4=E8=AF=81=E9=80=BB=E8=BE=91?= =?UTF-8?q?=EF=BC=9A=20-=20handleDirectMessages=20=E8=AF=BB=E5=8F=96?= =?UTF-8?q?=E5=AE=A2=E6=88=B7=E7=AB=AF=20x-api-key=20header=20-=20getAnthr?= =?UTF-8?q?opicHeaders=20=E4=BC=98=E5=85=88=E4=BD=BF=E7=94=A8=E5=AE=A2?= =?UTF-8?q?=E6=88=B7=E7=AB=AF=20x-api-key=EF=BC=8C=E9=81=BF=E5=85=8D?= =?UTF-8?q?=E5=90=8C=E6=97=B6=E8=AE=BE=E7=BD=AE=20x-api-key=20=E5=92=8C=20?= =?UTF-8?q?authorization?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- routes.js | 7 +++++-- transformers/request-anthropic.js | 11 ++++++++--- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/routes.js b/routes.js index b994e0d..d5cbe46 100644 --- a/routes.js +++ b/routes.js @@ -392,10 +392,13 @@ async function handleDirectMessages(req, res) { logInfo(`Direct forwarding to ${model.type} endpoint: ${endpoint.base_url}`); - // Get API key + // Get API key - support client x-api-key for anthropic endpoint let authHeader; try { - authHeader = await getApiKey(req.headers.authorization); + const clientAuthFromXApiKey = req.headers['x-api-key'] + ? `Bearer ${req.headers['x-api-key']}` + : null; + authHeader = await getApiKey(req.headers.authorization || clientAuthFromXApiKey); } catch (error) { logError('Failed to get API key', error); return res.status(500).json({ diff --git a/transformers/request-anthropic.js b/transformers/request-anthropic.js index 003edf9..0c88377 100644 --- a/transformers/request-anthropic.js +++ b/transformers/request-anthropic.js @@ -163,9 +163,6 @@ export function getAnthropicHeaders(authHeader, clientHeaders = {}, isStreaming 'accept': 'application/json', 'content-type': 'application/json', 'anthropic-version': clientHeaders['anthropic-version'] || '2023-06-01', - // Prefer client-provided x-api-key for anthropic endpoint format - ...(clientHeaders['x-api-key'] ? { 'x-api-key': clientHeaders['x-api-key'] } : {}), - 'authorization': authHeader || '', 'x-api-provider': 'anthropic', 'x-factory-client': 'cli', 'x-session-id': sessionId, @@ -175,6 +172,14 @@ export function getAnthropicHeaders(authHeader, clientHeaders = {}, isStreaming 'connection': 'keep-alive' }; + // Prefer client-provided x-api-key for anthropic endpoint format + if (clientHeaders['x-api-key']) { + headers['x-api-key'] = clientHeaders['x-api-key']; + } else if (authHeader) { + // If no client x-api-key, use authorization header (from FACTORY_API_KEY or refresh token) + headers['authorization'] = authHeader; + } + // Handle anthropic-beta header based on reasoning configuration const reasoningLevel = modelId ? getModelReasoning(modelId) : null; let betaValues = [];