修复 /v1/messages 端点 x-api-key 认证逻辑:
- handleDirectMessages 读取客户端 x-api-key header - getAnthropicHeaders 优先使用客户端 x-api-key,避免同时设置 x-api-key 和 authorization
This commit is contained in:
@@ -392,10 +392,13 @@ async function handleDirectMessages(req, res) {
|
|||||||
|
|
||||||
logInfo(`Direct forwarding to ${model.type} endpoint: ${endpoint.base_url}`);
|
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;
|
let authHeader;
|
||||||
try {
|
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) {
|
} catch (error) {
|
||||||
logError('Failed to get API key', error);
|
logError('Failed to get API key', error);
|
||||||
return res.status(500).json({
|
return res.status(500).json({
|
||||||
|
|||||||
@@ -163,9 +163,6 @@ export function getAnthropicHeaders(authHeader, clientHeaders = {}, isStreaming
|
|||||||
'accept': 'application/json',
|
'accept': 'application/json',
|
||||||
'content-type': 'application/json',
|
'content-type': 'application/json',
|
||||||
'anthropic-version': clientHeaders['anthropic-version'] || '2023-06-01',
|
'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-api-provider': 'anthropic',
|
||||||
'x-factory-client': 'cli',
|
'x-factory-client': 'cli',
|
||||||
'x-session-id': sessionId,
|
'x-session-id': sessionId,
|
||||||
@@ -175,6 +172,14 @@ export function getAnthropicHeaders(authHeader, clientHeaders = {}, isStreaming
|
|||||||
'connection': 'keep-alive'
|
'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
|
// Handle anthropic-beta header based on reasoning configuration
|
||||||
const reasoningLevel = modelId ? getModelReasoning(modelId) : null;
|
const reasoningLevel = modelId ? getModelReasoning(modelId) : null;
|
||||||
let betaValues = [];
|
let betaValues = [];
|
||||||
|
|||||||
Reference in New Issue
Block a user