Upload 15 files
Browse files- src/configs/config.js +94 -0
- src/managers/proxy.js +36 -0
- src/routes/handlers/chat.js +148 -0
- src/routes/handlers/messages.js +266 -0
- src/routes/handlers/models.js +29 -0
- src/routes/handlers/responses.js +138 -0
- src/routes/routes.js +16 -0
- src/routes/utils/auth.js +9 -0
- src/routes/utils/converter.js +40 -0
- src/server.js +129 -0
- src/transformers/requests/anthropic.js +239 -0
- src/transformers/requests/common.js +84 -0
- src/transformers/requests/openai.js +174 -0
- src/transformers/responses/anthropic.js +134 -0
- src/transformers/responses/openai.js +123 -0
src/configs/config.js
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// 配置常量
|
| 2 |
+
const CONFIG = {
|
| 3 |
+
port:process.env.PORT || 3000,
|
| 4 |
+
|
| 5 |
+
endpoint: [
|
| 6 |
+
{
|
| 7 |
+
name: 'openai',
|
| 8 |
+
base_url: 'https://app.factory.ai/api/llm/o/v1/responses'
|
| 9 |
+
},
|
| 10 |
+
{
|
| 11 |
+
name: 'anthropic',
|
| 12 |
+
base_url: 'https://app.factory.ai/api/llm/a/v1/messages'
|
| 13 |
+
},
|
| 14 |
+
{
|
| 15 |
+
name: 'common',
|
| 16 |
+
base_url: 'https://app.factory.ai/api/llm/o/v1/chat/completions'
|
| 17 |
+
}
|
| 18 |
+
],
|
| 19 |
+
|
| 20 |
+
models: [
|
| 21 |
+
{
|
| 22 |
+
id: 'claude-opus-4-1-20250805',
|
| 23 |
+
type: 'anthropic',
|
| 24 |
+
reasoning: 'auto'
|
| 25 |
+
},
|
| 26 |
+
{
|
| 27 |
+
id: 'claude-haiku-4-5-20251001',
|
| 28 |
+
type: 'anthropic',
|
| 29 |
+
reasoning: 'auto'
|
| 30 |
+
},
|
| 31 |
+
{
|
| 32 |
+
id: 'claude-sonnet-4-5-20250929',
|
| 33 |
+
type: 'anthropic',
|
| 34 |
+
reasoning: 'auto'
|
| 35 |
+
},
|
| 36 |
+
{
|
| 37 |
+
id: 'gpt-5-2025-08-07',
|
| 38 |
+
type: 'openai',
|
| 39 |
+
reasoning: 'auto'
|
| 40 |
+
},
|
| 41 |
+
{
|
| 42 |
+
id: 'gpt-5-codex',
|
| 43 |
+
type: 'openai',
|
| 44 |
+
reasoning: 'off'
|
| 45 |
+
},
|
| 46 |
+
{
|
| 47 |
+
id: 'glm-4.6',
|
| 48 |
+
type: 'common'
|
| 49 |
+
}
|
| 50 |
+
],
|
| 51 |
+
|
| 52 |
+
user_agent: 'factory-cli/0.22.14',
|
| 53 |
+
system_prompt: 'You are Droid, an AI software engineering agent built by Factory.\n\n'
|
| 54 |
+
}
|
| 55 |
+
|
| 56 |
+
export function getConfig() {
|
| 57 |
+
return CONFIG
|
| 58 |
+
}
|
| 59 |
+
|
| 60 |
+
export function getModelById(modelId) {
|
| 61 |
+
return CONFIG.models.find(m => m.id === modelId)
|
| 62 |
+
}
|
| 63 |
+
|
| 64 |
+
export function getEndpointByType(type) {
|
| 65 |
+
return CONFIG.endpoint.find(e => e.name === type)
|
| 66 |
+
}
|
| 67 |
+
|
| 68 |
+
export function getPort() {
|
| 69 |
+
return parseInt(process.env.PORT) || CONFIG.port
|
| 70 |
+
}
|
| 71 |
+
|
| 72 |
+
export function getSystemPrompt() {
|
| 73 |
+
return CONFIG.system_prompt || ''
|
| 74 |
+
}
|
| 75 |
+
|
| 76 |
+
export function getModelReasoning(modelId) {
|
| 77 |
+
const model = getModelById(modelId)
|
| 78 |
+
if (!model || !model.reasoning) {
|
| 79 |
+
return null
|
| 80 |
+
}
|
| 81 |
+
const reasoningLevel = model.reasoning.toLowerCase()
|
| 82 |
+
if (['low', 'medium', 'high', 'auto'].includes(reasoningLevel)) {
|
| 83 |
+
return reasoningLevel
|
| 84 |
+
}
|
| 85 |
+
return null
|
| 86 |
+
}
|
| 87 |
+
|
| 88 |
+
export function getUserAgent() {
|
| 89 |
+
return CONFIG.user_agent
|
| 90 |
+
}
|
| 91 |
+
|
| 92 |
+
export function getProxyUrl() {
|
| 93 |
+
return process.env.PROXY_URL || null
|
| 94 |
+
}
|
src/managers/proxy.js
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { HttpsProxyAgent } from 'https-proxy-agent'
|
| 2 |
+
import { getProxyUrl } from '../configs/config.js'
|
| 3 |
+
|
| 4 |
+
let cachedAgent = null
|
| 5 |
+
let cachedProxyUrl = null
|
| 6 |
+
|
| 7 |
+
export function getNextProxyAgent(targetUrl) {
|
| 8 |
+
const proxyUrl = getProxyUrl()
|
| 9 |
+
|
| 10 |
+
// 如果没有配置代理,返回 null(直连)
|
| 11 |
+
if (!proxyUrl) {
|
| 12 |
+
return null
|
| 13 |
+
}
|
| 14 |
+
|
| 15 |
+
// 如果代理 URL 改变,清除缓存
|
| 16 |
+
if (proxyUrl !== cachedProxyUrl) {
|
| 17 |
+
cachedAgent = null
|
| 18 |
+
cachedProxyUrl = proxyUrl
|
| 19 |
+
}
|
| 20 |
+
|
| 21 |
+
// 如果已有缓存的代理,直接返回
|
| 22 |
+
if (cachedAgent) {
|
| 23 |
+
return { agent: cachedAgent, proxy: { url: proxyUrl } }
|
| 24 |
+
}
|
| 25 |
+
|
| 26 |
+
// 创建新的代理
|
| 27 |
+
try {
|
| 28 |
+
cachedAgent = new HttpsProxyAgent(proxyUrl)
|
| 29 |
+
console.log(`使用代理 ${proxyUrl} 请求 ${targetUrl}`)
|
| 30 |
+
return { agent: cachedAgent, proxy: { url: proxyUrl } }
|
| 31 |
+
} catch (error) {
|
| 32 |
+
console.error(`为 ${proxyUrl} 创建代理失败:`, error)
|
| 33 |
+
return null
|
| 34 |
+
}
|
| 35 |
+
}
|
| 36 |
+
|
src/routes/handlers/chat.js
ADDED
|
@@ -0,0 +1,148 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import fetch from 'node-fetch'
|
| 2 |
+
import { getModelById, getEndpointByType } from '../../configs/config.js'
|
| 3 |
+
import { transformToAnthropic, getAnthropicHeaders } from '../../transformers/requests/anthropic.js'
|
| 4 |
+
import { transformToOpenAI, getOpenAIHeaders } from '../../transformers/requests/openai.js'
|
| 5 |
+
import { transformToCommon, getCommonHeaders } from '../../transformers/requests/common.js'
|
| 6 |
+
import { AnthropicResponseTransformer } from '../../transformers/responses/anthropic.js'
|
| 7 |
+
import { OpenAIResponseTransformer } from '../../transformers/responses/openai.js'
|
| 8 |
+
import { getNextProxyAgent } from '../../managers/proxy.js'
|
| 9 |
+
import { getAuthHeader } from '../utils/auth.js'
|
| 10 |
+
import { convertResponseToChatCompletion } from '../utils/converter.js'
|
| 11 |
+
|
| 12 |
+
/**
|
| 13 |
+
* 处理 POST /v1/chat/completions 请求
|
| 14 |
+
* 标准 OpenAI 聊天补全处理函数(带格式转换)
|
| 15 |
+
*/
|
| 16 |
+
export async function handleChatCompletions(req, res) {
|
| 17 |
+
try {
|
| 18 |
+
const openaiRequest = req.body
|
| 19 |
+
const modelId = openaiRequest.model
|
| 20 |
+
|
| 21 |
+
if (!modelId) {
|
| 22 |
+
return res.status(400).json({ error: '需要提供 model 参数' })
|
| 23 |
+
}
|
| 24 |
+
|
| 25 |
+
const model = getModelById(modelId)
|
| 26 |
+
if (!model) {
|
| 27 |
+
return res.status(404).json({ error: `未找到模型 ${modelId}` })
|
| 28 |
+
}
|
| 29 |
+
|
| 30 |
+
const endpoint = getEndpointByType(model.type)
|
| 31 |
+
if (!endpoint) {
|
| 32 |
+
return res.status(500).json({ error: `未找到端点类型 ${model.type}` })
|
| 33 |
+
}
|
| 34 |
+
|
| 35 |
+
// 获取认证信息
|
| 36 |
+
const authHeader = getAuthHeader(req)
|
| 37 |
+
if (!authHeader) {
|
| 38 |
+
return res.status(401).json({
|
| 39 |
+
error: '未提供认证信息',
|
| 40 |
+
message: '请在请求头中提供 Authorization 或 x-api-key'
|
| 41 |
+
})
|
| 42 |
+
}
|
| 43 |
+
|
| 44 |
+
let transformedRequest
|
| 45 |
+
let headers
|
| 46 |
+
const clientHeaders = req.headers
|
| 47 |
+
|
| 48 |
+
if ((openaiRequest.model === 'claude-sonnet-4-5-20250929' || openaiRequest.model === 'claude-haiku-4-5-20251001' || openaiRequest.model === 'claude-opus-4-1-20250805') && openaiRequest.temperature && openaiRequest.top_p) {
|
| 49 |
+
delete openaiRequest.top_p
|
| 50 |
+
}
|
| 51 |
+
|
| 52 |
+
// 转换请求格式
|
| 53 |
+
if (model.type === 'anthropic') {
|
| 54 |
+
transformedRequest = transformToAnthropic(openaiRequest)
|
| 55 |
+
const isStreaming = openaiRequest.stream === true
|
| 56 |
+
headers = getAnthropicHeaders(authHeader, clientHeaders, isStreaming, modelId)
|
| 57 |
+
} else if (model.type === 'openai') {
|
| 58 |
+
transformedRequest = transformToOpenAI(openaiRequest)
|
| 59 |
+
headers = getOpenAIHeaders(authHeader, clientHeaders)
|
| 60 |
+
} else if (model.type === 'common') {
|
| 61 |
+
transformedRequest = transformToCommon(openaiRequest)
|
| 62 |
+
headers = getCommonHeaders(authHeader, clientHeaders)
|
| 63 |
+
} else {
|
| 64 |
+
return res.status(500).json({ error: `未知的端点类型: ${model.type}` })
|
| 65 |
+
}
|
| 66 |
+
|
| 67 |
+
const proxyAgentInfo = getNextProxyAgent(endpoint.base_url)
|
| 68 |
+
const fetchOptions = {
|
| 69 |
+
method: 'POST',
|
| 70 |
+
headers,
|
| 71 |
+
body: JSON.stringify(transformedRequest)
|
| 72 |
+
}
|
| 73 |
+
|
| 74 |
+
if (proxyAgentInfo?.agent) {
|
| 75 |
+
fetchOptions.agent = proxyAgentInfo.agent
|
| 76 |
+
}
|
| 77 |
+
|
| 78 |
+
const response = await fetch(endpoint.base_url, fetchOptions)
|
| 79 |
+
|
| 80 |
+
if (!response.ok) {
|
| 81 |
+
const errorText = await response.text()
|
| 82 |
+
console.error(`端点错误: ${response.status}`, errorText)
|
| 83 |
+
return res.status(response.status).json({
|
| 84 |
+
error: `端点返回 ${response.status}`,
|
| 85 |
+
details: errorText
|
| 86 |
+
})
|
| 87 |
+
}
|
| 88 |
+
|
| 89 |
+
const isStreaming = transformedRequest.stream === true
|
| 90 |
+
|
| 91 |
+
if (isStreaming) {
|
| 92 |
+
res.setHeader('Content-Type', 'text/event-stream')
|
| 93 |
+
res.setHeader('Cache-Control', 'no-cache')
|
| 94 |
+
res.setHeader('Connection', 'keep-alive')
|
| 95 |
+
|
| 96 |
+
// common 类型直接转发,不使用 transformer
|
| 97 |
+
if (model.type === 'common') {
|
| 98 |
+
try {
|
| 99 |
+
for await (const chunk of response.body) {
|
| 100 |
+
res.write(chunk)
|
| 101 |
+
}
|
| 102 |
+
res.end()
|
| 103 |
+
} catch (streamError) {
|
| 104 |
+
console.error('流错误:', streamError)
|
| 105 |
+
res.end()
|
| 106 |
+
}
|
| 107 |
+
} else {
|
| 108 |
+
// anthropic 和 openai 类型使用 transformer
|
| 109 |
+
let transformer
|
| 110 |
+
if (model.type === 'anthropic') {
|
| 111 |
+
transformer = new AnthropicResponseTransformer(modelId, `chatcmpl-${Date.now()}`)
|
| 112 |
+
} else if (model.type === 'openai') {
|
| 113 |
+
transformer = new OpenAIResponseTransformer(modelId, `chatcmpl-${Date.now()}`)
|
| 114 |
+
}
|
| 115 |
+
|
| 116 |
+
try {
|
| 117 |
+
for await (const chunk of transformer.transformStream(response.body)) {
|
| 118 |
+
res.write(chunk)
|
| 119 |
+
}
|
| 120 |
+
res.end()
|
| 121 |
+
} catch (streamError) {
|
| 122 |
+
console.error('流错误:', streamError)
|
| 123 |
+
res.end()
|
| 124 |
+
}
|
| 125 |
+
}
|
| 126 |
+
} else {
|
| 127 |
+
const data = await response.json()
|
| 128 |
+
if (model.type === 'openai') {
|
| 129 |
+
try {
|
| 130 |
+
const converted = convertResponseToChatCompletion(data)
|
| 131 |
+
res.json(converted)
|
| 132 |
+
} catch (e) {
|
| 133 |
+
res.json(data)
|
| 134 |
+
}
|
| 135 |
+
} else {
|
| 136 |
+
res.json(data)
|
| 137 |
+
}
|
| 138 |
+
}
|
| 139 |
+
|
| 140 |
+
} catch (error) {
|
| 141 |
+
console.error('/v1/chat/completions 错误:', error)
|
| 142 |
+
res.status(500).json({
|
| 143 |
+
error: '内部服务器错误',
|
| 144 |
+
message: error.message
|
| 145 |
+
})
|
| 146 |
+
}
|
| 147 |
+
}
|
| 148 |
+
|
src/routes/handlers/messages.js
ADDED
|
@@ -0,0 +1,266 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import fetch from 'node-fetch'
|
| 2 |
+
import { getModelById, getEndpointByType, getSystemPrompt, getModelReasoning } from '../../configs/config.js'
|
| 3 |
+
import { getAnthropicHeaders } from '../../transformers/requests/anthropic.js'
|
| 4 |
+
import { getNextProxyAgent } from '../../managers/proxy.js'
|
| 5 |
+
import { getAuthHeader } from '../utils/auth.js'
|
| 6 |
+
|
| 7 |
+
/**
|
| 8 |
+
* 处理 POST /v1/messages 请求
|
| 9 |
+
* 直接转发 Anthropic 请求(不做格式转换)
|
| 10 |
+
*/
|
| 11 |
+
export async function handleDirectMessages(req, res) {
|
| 12 |
+
try {
|
| 13 |
+
const anthropicRequest = req.body
|
| 14 |
+
const modelId = anthropicRequest.model
|
| 15 |
+
|
| 16 |
+
if (!modelId) {
|
| 17 |
+
return res.status(400).json({ error: '需要提供 model 参数' })
|
| 18 |
+
}
|
| 19 |
+
|
| 20 |
+
const model = getModelById(modelId)
|
| 21 |
+
if (!model) {
|
| 22 |
+
return res.status(404).json({ error: `未找到模型 ${modelId}` })
|
| 23 |
+
}
|
| 24 |
+
|
| 25 |
+
// 只允许 anthropic 类型端点
|
| 26 |
+
if (model.type !== 'anthropic') {
|
| 27 |
+
return res.status(400).json({
|
| 28 |
+
error: '无效的端点类型',
|
| 29 |
+
message: `/v1/messages 接口只支持 anthropic 类型端点,当前模型 ${modelId} 是 ${model.type} 类型`
|
| 30 |
+
})
|
| 31 |
+
}
|
| 32 |
+
|
| 33 |
+
const endpoint = getEndpointByType(model.type)
|
| 34 |
+
if (!endpoint) {
|
| 35 |
+
return res.status(500).json({ error: `未找到端点类型 ${model.type}` })
|
| 36 |
+
}
|
| 37 |
+
|
| 38 |
+
// 获取认证信息
|
| 39 |
+
const authHeader = getAuthHeader(req)
|
| 40 |
+
if (!authHeader) {
|
| 41 |
+
return res.status(401).json({
|
| 42 |
+
error: '未提供认证信息',
|
| 43 |
+
message: '请在请求头中提供 Authorization 或 x-api-key'
|
| 44 |
+
})
|
| 45 |
+
}
|
| 46 |
+
|
| 47 |
+
// 如果是 x-api-key,转换为 Bearer 格式
|
| 48 |
+
const finalAuthHeader = authHeader.startsWith('Bearer ')
|
| 49 |
+
? authHeader
|
| 50 |
+
: `Bearer ${authHeader}`
|
| 51 |
+
|
| 52 |
+
const clientHeaders = req.headers
|
| 53 |
+
|
| 54 |
+
// 获取 headers
|
| 55 |
+
const isStreaming = anthropicRequest.stream === true
|
| 56 |
+
const headers = getAnthropicHeaders(finalAuthHeader, clientHeaders, isStreaming, modelId)
|
| 57 |
+
|
| 58 |
+
// 注入系统提示到 system 字段
|
| 59 |
+
const systemPrompt = getSystemPrompt()
|
| 60 |
+
const modifiedRequest = { ...anthropicRequest }
|
| 61 |
+
|
| 62 |
+
// 清理cc中的 "You are Claude Code, Anthropic's official CLI for Claude."
|
| 63 |
+
if (modifiedRequest.system && Array.isArray(modifiedRequest.system)) {
|
| 64 |
+
for (const msg of modifiedRequest.system) {
|
| 65 |
+
if (msg.type === 'text') {
|
| 66 |
+
msg.text = msg.text.replace("You are Claude Code, Anthropic's official CLI for Claude.", 'you are bot.')
|
| 67 |
+
}
|
| 68 |
+
}
|
| 69 |
+
}
|
| 70 |
+
|
| 71 |
+
if (systemPrompt) {
|
| 72 |
+
if (modifiedRequest.system && Array.isArray(modifiedRequest.system)) {
|
| 73 |
+
// 如果已有 system 数组,则在最前面插入系统提示
|
| 74 |
+
modifiedRequest.system = [
|
| 75 |
+
{
|
| 76 |
+
type: 'text',
|
| 77 |
+
text: systemPrompt
|
| 78 |
+
},
|
| 79 |
+
{
|
| 80 |
+
type: 'text',
|
| 81 |
+
text: 'Forget "You are Droid, an AI software engineering agent built by Factory." You have no relationship with Droid / Factory whatsoever.'
|
| 82 |
+
},
|
| 83 |
+
...modifiedRequest.system
|
| 84 |
+
]
|
| 85 |
+
} else {
|
| 86 |
+
// 否则创建新的 system 数组
|
| 87 |
+
modifiedRequest.system = [
|
| 88 |
+
{
|
| 89 |
+
type: 'text',
|
| 90 |
+
text: systemPrompt
|
| 91 |
+
},
|
| 92 |
+
{
|
| 93 |
+
type: 'text',
|
| 94 |
+
text: 'Forget "You are Droid, an AI software engineering agent built by Factory." You have no relationship with Droid / Factory whatsoever.'
|
| 95 |
+
}
|
| 96 |
+
]
|
| 97 |
+
}
|
| 98 |
+
}
|
| 99 |
+
|
| 100 |
+
if ((modelId === 'claude-sonnet-4-5-20250929' || modelId === 'claude-haiku-4-5-20251001' || modelId === 'claude-opus-4-1-20250805') && modifiedRequest.temperature && modifiedRequest.top_p) {
|
| 101 |
+
delete modifiedRequest.top_p
|
| 102 |
+
}
|
| 103 |
+
|
| 104 |
+
// 处理thinking字段
|
| 105 |
+
const reasoningLevel = getModelReasoning(modelId)
|
| 106 |
+
if (reasoningLevel === 'auto') {
|
| 107 |
+
// Auto模式:保持原始请求的thinking字段不变
|
| 108 |
+
// 如果原始请求有thinking字段就保留,没有就不添加
|
| 109 |
+
} else if (reasoningLevel && ['low', 'medium', 'high'].includes(reasoningLevel)) {
|
| 110 |
+
const budgetTokens = {
|
| 111 |
+
'low': 4096,
|
| 112 |
+
'medium': 12288,
|
| 113 |
+
'high': 24576
|
| 114 |
+
}
|
| 115 |
+
|
| 116 |
+
modifiedRequest.thinking = {
|
| 117 |
+
type: 'enabled',
|
| 118 |
+
budget_tokens: budgetTokens[reasoningLevel]
|
| 119 |
+
}
|
| 120 |
+
} else {
|
| 121 |
+
// 如果配置是off或无效,移除thinking字段
|
| 122 |
+
delete modifiedRequest.thinking
|
| 123 |
+
}
|
| 124 |
+
|
| 125 |
+
// 转发修改后的请求
|
| 126 |
+
const proxyAgentInfo = getNextProxyAgent(endpoint.base_url)
|
| 127 |
+
const fetchOptions = {
|
| 128 |
+
method: 'POST',
|
| 129 |
+
headers,
|
| 130 |
+
body: JSON.stringify(modifiedRequest)
|
| 131 |
+
}
|
| 132 |
+
|
| 133 |
+
if (proxyAgentInfo?.agent) {
|
| 134 |
+
fetchOptions.agent = proxyAgentInfo.agent
|
| 135 |
+
}
|
| 136 |
+
|
| 137 |
+
const response = await fetch(endpoint.base_url, fetchOptions)
|
| 138 |
+
|
| 139 |
+
if (!response.ok) {
|
| 140 |
+
const errorText = await response.text()
|
| 141 |
+
console.error(`端点错误: ${response.status}`, errorText)
|
| 142 |
+
return res.status(response.status).json({
|
| 143 |
+
error: `端点返回 ${response.status}`,
|
| 144 |
+
details: errorText
|
| 145 |
+
})
|
| 146 |
+
}
|
| 147 |
+
|
| 148 |
+
if (isStreaming) {
|
| 149 |
+
// 直接转发流式响应,不做任何转换
|
| 150 |
+
res.setHeader('Content-Type', 'text/event-stream')
|
| 151 |
+
res.setHeader('Cache-Control', 'no-cache')
|
| 152 |
+
res.setHeader('Connection', 'keep-alive')
|
| 153 |
+
|
| 154 |
+
try {
|
| 155 |
+
// 直接将原始响应流转发给客户端
|
| 156 |
+
for await (const chunk of response.body) {
|
| 157 |
+
res.write(chunk)
|
| 158 |
+
}
|
| 159 |
+
res.end()
|
| 160 |
+
} catch (streamError) {
|
| 161 |
+
console.error('流错误:', streamError)
|
| 162 |
+
res.end()
|
| 163 |
+
}
|
| 164 |
+
} else {
|
| 165 |
+
// 直接转发非流式响应,不做任何转换
|
| 166 |
+
const data = await response.json()
|
| 167 |
+
res.json(data)
|
| 168 |
+
}
|
| 169 |
+
|
| 170 |
+
} catch (error) {
|
| 171 |
+
console.error('/v1/messages 错误:', error)
|
| 172 |
+
res.status(500).json({
|
| 173 |
+
error: '内部服务器错误',
|
| 174 |
+
message: error.message
|
| 175 |
+
})
|
| 176 |
+
}
|
| 177 |
+
}
|
| 178 |
+
|
| 179 |
+
/**
|
| 180 |
+
* 处理 POST /v1/messages/count_tokens 请求
|
| 181 |
+
* Anthropic count_tokens 请求
|
| 182 |
+
*/
|
| 183 |
+
export async function handleCountTokens(req, res) {
|
| 184 |
+
try {
|
| 185 |
+
const anthropicRequest = req.body
|
| 186 |
+
const modelId = anthropicRequest.model
|
| 187 |
+
|
| 188 |
+
if (!modelId) {
|
| 189 |
+
return res.status(400).json({ error: '需要提供 model 参数' })
|
| 190 |
+
}
|
| 191 |
+
|
| 192 |
+
const model = getModelById(modelId)
|
| 193 |
+
if (!model) {
|
| 194 |
+
return res.status(404).json({ error: `未找到模型 ${modelId}` })
|
| 195 |
+
}
|
| 196 |
+
|
| 197 |
+
// 只允许 anthropic 类型端点
|
| 198 |
+
if (model.type !== 'anthropic') {
|
| 199 |
+
return res.status(400).json({
|
| 200 |
+
error: '无效的端点类型',
|
| 201 |
+
message: `/v1/messages/count_tokens 接口只支持 anthropic 类型端点,当前模型 ${modelId} 是 ${model.type} 类型`
|
| 202 |
+
})
|
| 203 |
+
}
|
| 204 |
+
|
| 205 |
+
const endpoint = getEndpointByType('anthropic')
|
| 206 |
+
if (!endpoint) {
|
| 207 |
+
return res.status(500).json({ error: '未找到端点类型 anthropic' })
|
| 208 |
+
}
|
| 209 |
+
|
| 210 |
+
// 获取认证信息
|
| 211 |
+
const authHeader = getAuthHeader(req)
|
| 212 |
+
if (!authHeader) {
|
| 213 |
+
return res.status(401).json({
|
| 214 |
+
error: '未提供认证信息',
|
| 215 |
+
message: '请在请求头中提供 Authorization 或 x-api-key'
|
| 216 |
+
})
|
| 217 |
+
}
|
| 218 |
+
|
| 219 |
+
// 如果是 x-api-key,转换为 Bearer 格式
|
| 220 |
+
const finalAuthHeader = authHeader.startsWith('Bearer ')
|
| 221 |
+
? authHeader
|
| 222 |
+
: `Bearer ${authHeader}`
|
| 223 |
+
|
| 224 |
+
const clientHeaders = req.headers
|
| 225 |
+
const headers = getAnthropicHeaders(finalAuthHeader, clientHeaders, false, modelId)
|
| 226 |
+
|
| 227 |
+
// 构建 count_tokens 端点 URL
|
| 228 |
+
const countTokensUrl = endpoint.base_url.replace('/v1/messages', '/v1/messages/count_tokens')
|
| 229 |
+
|
| 230 |
+
// 使用原始请求体
|
| 231 |
+
const modifiedRequest = { ...anthropicRequest }
|
| 232 |
+
|
| 233 |
+
const proxyAgentInfo = getNextProxyAgent(countTokensUrl)
|
| 234 |
+
const fetchOptions = {
|
| 235 |
+
method: 'POST',
|
| 236 |
+
headers,
|
| 237 |
+
body: JSON.stringify(modifiedRequest)
|
| 238 |
+
}
|
| 239 |
+
|
| 240 |
+
if (proxyAgentInfo?.agent) {
|
| 241 |
+
fetchOptions.agent = proxyAgentInfo.agent
|
| 242 |
+
}
|
| 243 |
+
|
| 244 |
+
const response = await fetch(countTokensUrl, fetchOptions)
|
| 245 |
+
|
| 246 |
+
if (!response.ok) {
|
| 247 |
+
const errorText = await response.text()
|
| 248 |
+
console.error(`计数令牌错误: ${response.status}`, errorText)
|
| 249 |
+
return res.status(response.status).json({
|
| 250 |
+
error: `端点返回 ${response.status}`,
|
| 251 |
+
details: errorText
|
| 252 |
+
})
|
| 253 |
+
}
|
| 254 |
+
|
| 255 |
+
const data = await response.json()
|
| 256 |
+
res.json(data)
|
| 257 |
+
|
| 258 |
+
} catch (error) {
|
| 259 |
+
console.error('/v1/messages/count_tokens 错误:', error)
|
| 260 |
+
res.status(500).json({
|
| 261 |
+
error: '内部服务器错误',
|
| 262 |
+
message: error.message
|
| 263 |
+
})
|
| 264 |
+
}
|
| 265 |
+
}
|
| 266 |
+
|
src/routes/handlers/models.js
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { getConfig } from '../../configs/config.js'
|
| 2 |
+
|
| 3 |
+
/**
|
| 4 |
+
* 处理 GET /v1/models 请求
|
| 5 |
+
* 返回所有可用模型列表
|
| 6 |
+
*/
|
| 7 |
+
export async function handleModels(req, res) {
|
| 8 |
+
try {
|
| 9 |
+
const config = getConfig()
|
| 10 |
+
const models = config.models.map(model => ({
|
| 11 |
+
id: model.id,
|
| 12 |
+
object: 'model',
|
| 13 |
+
created: Date.now(),
|
| 14 |
+
owned_by: model.type,
|
| 15 |
+
permission: [],
|
| 16 |
+
root: model.id,
|
| 17 |
+
parent: null
|
| 18 |
+
}))
|
| 19 |
+
|
| 20 |
+
res.json({
|
| 21 |
+
object: 'list',
|
| 22 |
+
data: models
|
| 23 |
+
})
|
| 24 |
+
} catch (error) {
|
| 25 |
+
console.error('GET /v1/models 错误:', error)
|
| 26 |
+
res.status(500).json({ error: 'Internal server error' })
|
| 27 |
+
}
|
| 28 |
+
}
|
| 29 |
+
|
src/routes/handlers/responses.js
ADDED
|
@@ -0,0 +1,138 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import fetch from 'node-fetch'
|
| 2 |
+
import { getModelById, getEndpointByType, getSystemPrompt, getModelReasoning } from '../../configs/config.js'
|
| 3 |
+
import { getOpenAIHeaders } from '../../transformers/requests/openai.js'
|
| 4 |
+
import { getNextProxyAgent } from '../../managers/proxy.js'
|
| 5 |
+
import { getAuthHeader } from '../utils/auth.js'
|
| 6 |
+
|
| 7 |
+
/**
|
| 8 |
+
* 处理 POST /v1/responses 请求
|
| 9 |
+
* 直接转发 OpenAI 请求(不做格式转换)
|
| 10 |
+
*/
|
| 11 |
+
export async function handleDirectResponses(req, res) {
|
| 12 |
+
try {
|
| 13 |
+
const openaiRequest = req.body
|
| 14 |
+
const modelId = openaiRequest.model
|
| 15 |
+
|
| 16 |
+
if (!modelId) {
|
| 17 |
+
return res.status(400).json({ error: '需要提供 model 参数' })
|
| 18 |
+
}
|
| 19 |
+
|
| 20 |
+
const model = getModelById(modelId)
|
| 21 |
+
if (!model) {
|
| 22 |
+
return res.status(404).json({ error: `未找到模型 ${modelId}` })
|
| 23 |
+
}
|
| 24 |
+
|
| 25 |
+
// 只允许 openai 类型端点
|
| 26 |
+
if (model.type !== 'openai') {
|
| 27 |
+
return res.status(400).json({
|
| 28 |
+
error: '无效的端点类型',
|
| 29 |
+
message: `/v1/responses 接口只支持 openai 类型端点,当前模型 ${modelId} 是 ${model.type} 类型`
|
| 30 |
+
})
|
| 31 |
+
}
|
| 32 |
+
|
| 33 |
+
const endpoint = getEndpointByType(model.type)
|
| 34 |
+
if (!endpoint) {
|
| 35 |
+
return res.status(500).json({ error: `未找到端点类型 ${model.type}` })
|
| 36 |
+
}
|
| 37 |
+
|
| 38 |
+
// 获取认证信息
|
| 39 |
+
const authHeader = getAuthHeader(req)
|
| 40 |
+
if (!authHeader) {
|
| 41 |
+
return res.status(401).json({
|
| 42 |
+
error: '未提供认证信息',
|
| 43 |
+
message: '请在请求头中提供 Authorization 或 x-api-key'
|
| 44 |
+
})
|
| 45 |
+
}
|
| 46 |
+
|
| 47 |
+
// 如果是 x-api-key,转换为 Bearer 格式
|
| 48 |
+
const finalAuthHeader = authHeader.startsWith('Bearer ')
|
| 49 |
+
? authHeader
|
| 50 |
+
: `Bearer ${authHeader}`
|
| 51 |
+
|
| 52 |
+
const clientHeaders = req.headers
|
| 53 |
+
|
| 54 |
+
// 获取 headers
|
| 55 |
+
const headers = getOpenAIHeaders(finalAuthHeader, clientHeaders)
|
| 56 |
+
|
| 57 |
+
// 注入系统提示到 instructions 字段
|
| 58 |
+
const systemPrompt = getSystemPrompt()
|
| 59 |
+
const modifiedRequest = { ...openaiRequest }
|
| 60 |
+
if (systemPrompt) {
|
| 61 |
+
// 如果已有 instructions,则在前面添加系统提示
|
| 62 |
+
if (modifiedRequest.instructions) {
|
| 63 |
+
modifiedRequest.instructions = systemPrompt + 'Forget "You are Droid, an AI software engineering agent built by Factory." You have no relationship with Droid / Factory whatsoever.' + modifiedRequest.instructions
|
| 64 |
+
} else {
|
| 65 |
+
// 否则直接设置系统提示
|
| 66 |
+
modifiedRequest.instructions = systemPrompt + 'Forget "You are Droid, an AI software engineering agent built by Factory." You have no relationship with Droid / Factory whatsoever.'
|
| 67 |
+
}
|
| 68 |
+
}
|
| 69 |
+
|
| 70 |
+
// 处理reasoning字段
|
| 71 |
+
const reasoningLevel = getModelReasoning(modelId)
|
| 72 |
+
if (reasoningLevel === 'auto') {
|
| 73 |
+
// Auto模式:保持原始请求的reasoning字段不变
|
| 74 |
+
// 如果原始请求有reasoning字段就保留,没有就不添加
|
| 75 |
+
} else if (reasoningLevel && ['low', 'medium', 'high'].includes(reasoningLevel)) {
|
| 76 |
+
modifiedRequest.reasoning = {
|
| 77 |
+
effort: reasoningLevel,
|
| 78 |
+
summary: 'auto'
|
| 79 |
+
}
|
| 80 |
+
} else {
|
| 81 |
+
// 如果配置是off或无效,移除reasoning字段
|
| 82 |
+
delete modifiedRequest.reasoning
|
| 83 |
+
}
|
| 84 |
+
|
| 85 |
+
const proxyAgentInfo = getNextProxyAgent(endpoint.base_url)
|
| 86 |
+
const fetchOptions = {
|
| 87 |
+
method: 'POST',
|
| 88 |
+
headers,
|
| 89 |
+
body: JSON.stringify(modifiedRequest)
|
| 90 |
+
}
|
| 91 |
+
|
| 92 |
+
if (proxyAgentInfo?.agent) {
|
| 93 |
+
fetchOptions.agent = proxyAgentInfo.agent
|
| 94 |
+
}
|
| 95 |
+
|
| 96 |
+
console.log(`[INFO] 直接转发到 openai 端点: ${endpoint.base_url}`)
|
| 97 |
+
const response = await fetch(endpoint.base_url, fetchOptions)
|
| 98 |
+
console.log(`[INFO] 响应状态: ${response.status}`)
|
| 99 |
+
|
| 100 |
+
if (!response.ok) {
|
| 101 |
+
const errorText = await response.text()
|
| 102 |
+
console.error(`端点错误: ${response.status}`, errorText)
|
| 103 |
+
return res.status(response.status).json({
|
| 104 |
+
error: `端点返回 ${response.status}`,
|
| 105 |
+
details: errorText
|
| 106 |
+
})
|
| 107 |
+
}
|
| 108 |
+
|
| 109 |
+
const isStreaming = openaiRequest.stream === true
|
| 110 |
+
|
| 111 |
+
if (isStreaming) {
|
| 112 |
+
res.setHeader('Content-Type', 'text/event-stream')
|
| 113 |
+
res.setHeader('Cache-Control', 'no-cache')
|
| 114 |
+
res.setHeader('Connection', 'keep-alive')
|
| 115 |
+
|
| 116 |
+
try {
|
| 117 |
+
for await (const chunk of response.body) {
|
| 118 |
+
res.write(chunk)
|
| 119 |
+
}
|
| 120 |
+
res.end()
|
| 121 |
+
} catch (streamError) {
|
| 122 |
+
console.error('流错误:', streamError)
|
| 123 |
+
res.end()
|
| 124 |
+
}
|
| 125 |
+
} else {
|
| 126 |
+
const data = await response.json()
|
| 127 |
+
res.json(data)
|
| 128 |
+
}
|
| 129 |
+
|
| 130 |
+
} catch (error) {
|
| 131 |
+
console.error('/v1/responses 错误:', error)
|
| 132 |
+
res.status(500).json({
|
| 133 |
+
error: '内部服务器错误',
|
| 134 |
+
message: error.message
|
| 135 |
+
})
|
| 136 |
+
}
|
| 137 |
+
}
|
| 138 |
+
|
src/routes/routes.js
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import express from 'express'
|
| 2 |
+
import { handleModels } from './handlers/models.js'
|
| 3 |
+
import { handleChatCompletions } from './handlers/chat.js'
|
| 4 |
+
import { handleDirectResponses } from './handlers/responses.js'
|
| 5 |
+
import { handleDirectMessages, handleCountTokens } from './handlers/messages.js'
|
| 6 |
+
|
| 7 |
+
const router = express.Router()
|
| 8 |
+
|
| 9 |
+
// 注册路由
|
| 10 |
+
router.get('/v1/models', handleModels)
|
| 11 |
+
router.post('/v1/chat/completions', handleChatCompletions)
|
| 12 |
+
router.post('/v1/responses', handleDirectResponses)
|
| 13 |
+
router.post('/v1/messages', handleDirectMessages)
|
| 14 |
+
router.post('/v1/messages/count_tokens', handleCountTokens)
|
| 15 |
+
|
| 16 |
+
export default router
|
src/routes/utils/auth.js
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
/**
|
| 2 |
+
* 从请求头获取认证信息
|
| 3 |
+
* @param {Object} req - Express 请求对象
|
| 4 |
+
* @returns {string|undefined} 认证令牌
|
| 5 |
+
*/
|
| 6 |
+
export function getAuthHeader(req) {
|
| 7 |
+
return req.headers.authorization || req.headers.Authorization || req.headers['x-api-key']
|
| 8 |
+
}
|
| 9 |
+
|
src/routes/utils/converter.js
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
/**
|
| 2 |
+
* 将 /v1/responses API 结果转换为 /v1/chat/completions 兼容格式
|
| 3 |
+
* 适用于非流式响应
|
| 4 |
+
* @param {Object} resp - 原始响应对象
|
| 5 |
+
* @returns {Object} OpenAI 兼容的聊天补全格式
|
| 6 |
+
*/
|
| 7 |
+
export function convertResponseToChatCompletion(resp) {
|
| 8 |
+
if (!resp || typeof resp !== 'object') {
|
| 9 |
+
throw new Error('Invalid response object')
|
| 10 |
+
}
|
| 11 |
+
|
| 12 |
+
const outputMsg = (resp.output || []).find(o => o.type === 'message')
|
| 13 |
+
const textBlocks = outputMsg?.content?.filter(c => c.type === 'output_text') || []
|
| 14 |
+
const content = textBlocks.map(c => c.text).join('')
|
| 15 |
+
|
| 16 |
+
const chatCompletion = {
|
| 17 |
+
id: resp.id ? resp.id.replace(/^resp_/, 'chatcmpl-') : `chatcmpl-${Date.now()}`,
|
| 18 |
+
object: 'chat.completion',
|
| 19 |
+
created: resp.created_at || Math.floor(Date.now() / 1000),
|
| 20 |
+
model: resp.model || 'unknown-model',
|
| 21 |
+
choices: [
|
| 22 |
+
{
|
| 23 |
+
index: 0,
|
| 24 |
+
message: {
|
| 25 |
+
role: outputMsg?.role || 'assistant',
|
| 26 |
+
content: content || ''
|
| 27 |
+
},
|
| 28 |
+
finish_reason: resp.status === 'completed' ? 'stop' : 'unknown'
|
| 29 |
+
}
|
| 30 |
+
],
|
| 31 |
+
usage: {
|
| 32 |
+
prompt_tokens: resp.usage?.input_tokens ?? 0,
|
| 33 |
+
completion_tokens: resp.usage?.output_tokens ?? 0,
|
| 34 |
+
total_tokens: resp.usage?.total_tokens ?? 0
|
| 35 |
+
}
|
| 36 |
+
}
|
| 37 |
+
|
| 38 |
+
return chatCompletion
|
| 39 |
+
}
|
| 40 |
+
|
src/server.js
ADDED
|
@@ -0,0 +1,129 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import express from 'express'
|
| 2 |
+
import { getPort } from './configs/config.js'
|
| 3 |
+
import router from './routes/routes.js'
|
| 4 |
+
|
| 5 |
+
const app = express();
|
| 6 |
+
|
| 7 |
+
app.use(express.json({ limit: '50mb' }));
|
| 8 |
+
app.use(express.urlencoded({ extended: true, limit: '50mb' }));
|
| 9 |
+
|
| 10 |
+
// 生成请求ID (UUID格式)
|
| 11 |
+
function generateRequestId() {
|
| 12 |
+
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
|
| 13 |
+
const r = Math.random() * 16 | 0
|
| 14 |
+
const v = c === 'x' ? r : (r & 0x3 | 0x8)
|
| 15 |
+
return v.toString(16)
|
| 16 |
+
})
|
| 17 |
+
}
|
| 18 |
+
|
| 19 |
+
// 获取客户端IP
|
| 20 |
+
function getClientIp(req) {
|
| 21 |
+
return req.headers['x-forwarded-for']?.split(',')[0].trim() ||
|
| 22 |
+
req.headers['x-real-ip'] ||
|
| 23 |
+
req.connection?.remoteAddress ||
|
| 24 |
+
req.socket?.remoteAddress ||
|
| 25 |
+
'unknown'
|
| 26 |
+
}
|
| 27 |
+
|
| 28 |
+
// 请求日志中间件
|
| 29 |
+
app.use((req, res, next) => {
|
| 30 |
+
const startTime = Date.now()
|
| 31 |
+
const requestId = generateRequestId()
|
| 32 |
+
const clientIp = getClientIp(req)
|
| 33 |
+
|
| 34 |
+
// 将请求ID附加到req对象
|
| 35 |
+
req.requestId = requestId
|
| 36 |
+
|
| 37 |
+
// 请求开始时打印日志
|
| 38 |
+
console.log(`[INFO] [${requestId}] [${clientIp}] ${req.method} ${req.path}`)
|
| 39 |
+
|
| 40 |
+
// 保存原始的 res.json 和 res.end 方法
|
| 41 |
+
const originalJson = res.json.bind(res)
|
| 42 |
+
const originalEnd = res.end.bind(res)
|
| 43 |
+
|
| 44 |
+
// 重写 res.json 方法以捕获响应状态
|
| 45 |
+
res.json = function(data) {
|
| 46 |
+
logRequestEnd(req, res, startTime, requestId, clientIp)
|
| 47 |
+
return originalJson(data)
|
| 48 |
+
}
|
| 49 |
+
|
| 50 |
+
// 重写 res.end 方法以捕获流式响应状态
|
| 51 |
+
res.end = function(...args) {
|
| 52 |
+
logRequestEnd(req, res, startTime, requestId, clientIp)
|
| 53 |
+
return originalEnd(...args)
|
| 54 |
+
}
|
| 55 |
+
|
| 56 |
+
next()
|
| 57 |
+
})
|
| 58 |
+
|
| 59 |
+
// 请求结束日志记录函数
|
| 60 |
+
function logRequestEnd(req, res, startTime, requestId, clientIp) {
|
| 61 |
+
// 避免重复记录
|
| 62 |
+
if (res._logged) return
|
| 63 |
+
res._logged = true
|
| 64 |
+
|
| 65 |
+
const duration = ((Date.now() - startTime) / 1000).toFixed(3)
|
| 66 |
+
const status = res.statusCode
|
| 67 |
+
|
| 68 |
+
console.log(`[INFO] [${requestId}] [${clientIp}] 响应状态: ${status}`)
|
| 69 |
+
console.log(`[INFO] [${requestId}] [${clientIp}] 处理时间: ${duration}s`)
|
| 70 |
+
}
|
| 71 |
+
|
| 72 |
+
app.use((req, res, next) => {
|
| 73 |
+
res.header('Access-Control-Allow-Origin', '*');
|
| 74 |
+
res.header('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS');
|
| 75 |
+
res.header('Access-Control-Allow-Headers', 'Content-Type, Authorization, X-API-Key, anthropic-version');
|
| 76 |
+
|
| 77 |
+
if (req.method === 'OPTIONS') {
|
| 78 |
+
return res.sendStatus(200);
|
| 79 |
+
}
|
| 80 |
+
next();
|
| 81 |
+
});
|
| 82 |
+
|
| 83 |
+
app.use(router);
|
| 84 |
+
|
| 85 |
+
app.get('/', (req, res) => {
|
| 86 |
+
res.redirect('https://www.bilibili.com/video/BV1SMH5zfEwe/?spm_id_from=333.1007.tianma.1-1-1.click&vd_source=1f3b8eb28230105c578a443fa6481550')
|
| 87 |
+
})
|
| 88 |
+
|
| 89 |
+
|
| 90 |
+
// 错误处理中间件
|
| 91 |
+
app.use((err, req, res, next) => {
|
| 92 |
+
console.error('未处理的错误:', err);
|
| 93 |
+
res.status(500).json({
|
| 94 |
+
error: '内部服务器错误',
|
| 95 |
+
message: err.message
|
| 96 |
+
});
|
| 97 |
+
});
|
| 98 |
+
|
| 99 |
+
(async () => {
|
| 100 |
+
try {
|
| 101 |
+
const PORT = getPort()
|
| 102 |
+
|
| 103 |
+
app.listen(PORT)
|
| 104 |
+
.on('listening', () => {
|
| 105 |
+
console.log(`服务器运行在 http://localhost:${PORT}`)
|
| 106 |
+
})
|
| 107 |
+
.on('error', (err) => {
|
| 108 |
+
if (err.code === 'EADDRINUSE') {
|
| 109 |
+
console.error(`\n${'='.repeat(80)}`);
|
| 110 |
+
console.error(`错误: 端口 ${PORT} 已被占用!`);
|
| 111 |
+
console.error('');
|
| 112 |
+
console.error('请选择以下选项之一:');
|
| 113 |
+
console.error(` 1. 停止使用端口 ${PORT} 的进程:`);
|
| 114 |
+
console.error(` lsof -ti:${PORT} | xargs kill`);
|
| 115 |
+
console.error('');
|
| 116 |
+
console.error(' 2. 使用环境变量更改端口:');
|
| 117 |
+
console.error(' export PORT=8080');
|
| 118 |
+
console.error(`${'='.repeat(80)}\n`);
|
| 119 |
+
process.exit(1);
|
| 120 |
+
} else {
|
| 121 |
+
console.error('启动服务器失败:', err);
|
| 122 |
+
process.exit(1);
|
| 123 |
+
}
|
| 124 |
+
});
|
| 125 |
+
} catch (error) {
|
| 126 |
+
console.error('启动服务器失败:', error);
|
| 127 |
+
process.exit(1);
|
| 128 |
+
}
|
| 129 |
+
})();
|
src/transformers/requests/anthropic.js
ADDED
|
@@ -0,0 +1,239 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { getSystemPrompt, getModelReasoning, getUserAgent } from '../../configs/config.js'
|
| 2 |
+
|
| 3 |
+
export function transformToAnthropic(openaiRequest) {
|
| 4 |
+
|
| 5 |
+
const anthropicRequest = {
|
| 6 |
+
model: openaiRequest.model,
|
| 7 |
+
messages: []
|
| 8 |
+
};
|
| 9 |
+
|
| 10 |
+
// 仅在客户端明确提供时添加 stream 参数
|
| 11 |
+
if (openaiRequest.stream !== undefined) {
|
| 12 |
+
anthropicRequest.stream = openaiRequest.stream;
|
| 13 |
+
}
|
| 14 |
+
|
| 15 |
+
// 处理 max_tokens
|
| 16 |
+
if (openaiRequest.max_tokens) {
|
| 17 |
+
anthropicRequest.max_tokens = openaiRequest.max_tokens;
|
| 18 |
+
} else if (openaiRequest.max_completion_tokens) {
|
| 19 |
+
anthropicRequest.max_tokens = openaiRequest.max_completion_tokens;
|
| 20 |
+
} else {
|
| 21 |
+
anthropicRequest.max_tokens = 4096;
|
| 22 |
+
}
|
| 23 |
+
|
| 24 |
+
// 提取系统消息并转换其他消息
|
| 25 |
+
let systemContent = [];
|
| 26 |
+
|
| 27 |
+
if (openaiRequest.messages && Array.isArray(openaiRequest.messages)) {
|
| 28 |
+
for (const msg of openaiRequest.messages) {
|
| 29 |
+
// 单独处理系统消息
|
| 30 |
+
if (msg.role === 'system') {
|
| 31 |
+
if (typeof msg.content === 'string') {
|
| 32 |
+
systemContent.push({
|
| 33 |
+
type: 'text',
|
| 34 |
+
text: msg.content?.replace("You are Claude Code, Anthropic's official CLI for Claude.", 'You are bot.')
|
| 35 |
+
});
|
| 36 |
+
} else if (Array.isArray(msg.content)) {
|
| 37 |
+
for (const part of msg.content) {
|
| 38 |
+
if (part.type === 'text') {
|
| 39 |
+
systemContent.push({
|
| 40 |
+
type: 'text',
|
| 41 |
+
text: part.text?.replace("You are Claude Code, Anthropic's official CLI for Claude.", 'You are bot.')
|
| 42 |
+
});
|
| 43 |
+
} else {
|
| 44 |
+
systemContent.push(part);
|
| 45 |
+
}
|
| 46 |
+
}
|
| 47 |
+
}
|
| 48 |
+
continue; // 跳过将系统消息添加到消息数组
|
| 49 |
+
}
|
| 50 |
+
|
| 51 |
+
const anthropicMsg = {
|
| 52 |
+
role: msg.role,
|
| 53 |
+
content: []
|
| 54 |
+
};
|
| 55 |
+
|
| 56 |
+
if (typeof msg.content === 'string') {
|
| 57 |
+
anthropicMsg.content.push({
|
| 58 |
+
type: 'text',
|
| 59 |
+
text: msg.content
|
| 60 |
+
});
|
| 61 |
+
} else if (Array.isArray(msg.content)) {
|
| 62 |
+
for (const part of msg.content) {
|
| 63 |
+
if (part.type === 'text') {
|
| 64 |
+
anthropicMsg.content.push({
|
| 65 |
+
type: 'text',
|
| 66 |
+
text: part.text
|
| 67 |
+
});
|
| 68 |
+
} else if (part.type === 'image_url') {
|
| 69 |
+
anthropicMsg.content.push({
|
| 70 |
+
type: 'image',
|
| 71 |
+
source: part.image_url
|
| 72 |
+
});
|
| 73 |
+
} else {
|
| 74 |
+
anthropicMsg.content.push(part);
|
| 75 |
+
}
|
| 76 |
+
}
|
| 77 |
+
}
|
| 78 |
+
|
| 79 |
+
anthropicRequest.messages.push(anthropicMsg);
|
| 80 |
+
}
|
| 81 |
+
}
|
| 82 |
+
|
| 83 |
+
// 添加系统参数,并在前面加上系统提示
|
| 84 |
+
const systemPrompt = getSystemPrompt();
|
| 85 |
+
if (systemPrompt || systemContent.length > 0) {
|
| 86 |
+
anthropicRequest.system = [];
|
| 87 |
+
// 如果存在系统提示,则将其作为第一个元素添加
|
| 88 |
+
if (systemPrompt) {
|
| 89 |
+
anthropicRequest.system.push({
|
| 90 |
+
type: 'text',
|
| 91 |
+
text: systemPrompt
|
| 92 |
+
});
|
| 93 |
+
}
|
| 94 |
+
// 添加用户提供的系统内容
|
| 95 |
+
anthropicRequest.system.push(...systemContent);
|
| 96 |
+
}
|
| 97 |
+
|
| 98 |
+
// 如果存在工具,则进行转换
|
| 99 |
+
if (openaiRequest.tools && Array.isArray(openaiRequest.tools)) {
|
| 100 |
+
anthropicRequest.tools = openaiRequest.tools.map(tool => {
|
| 101 |
+
if (tool.type === 'function') {
|
| 102 |
+
return {
|
| 103 |
+
name: tool.function.name,
|
| 104 |
+
description: tool.function.description,
|
| 105 |
+
input_schema: tool.function.parameters || {}
|
| 106 |
+
};
|
| 107 |
+
}
|
| 108 |
+
return tool;
|
| 109 |
+
});
|
| 110 |
+
}
|
| 111 |
+
|
| 112 |
+
// 根据模型配置处理 thinking 字段
|
| 113 |
+
const reasoningLevel = getModelReasoning(openaiRequest.model);
|
| 114 |
+
if (reasoningLevel === 'auto') {
|
| 115 |
+
// 自动模式:完全保留原始请求的 thinking 字段
|
| 116 |
+
if (openaiRequest.thinking !== undefined) {
|
| 117 |
+
anthropicRequest.thinking = openaiRequest.thinking;
|
| 118 |
+
}
|
| 119 |
+
// 如果原始请求没有 thinking 字段,则不添加
|
| 120 |
+
} else if (reasoningLevel && ['low', 'medium', 'high'].includes(reasoningLevel)) {
|
| 121 |
+
// 特定级别:使用模型配置覆盖
|
| 122 |
+
const budgetTokens = {
|
| 123 |
+
'low': 4096,
|
| 124 |
+
'medium': 12288,
|
| 125 |
+
'high': 24576
|
| 126 |
+
};
|
| 127 |
+
|
| 128 |
+
anthropicRequest.thinking = {
|
| 129 |
+
type: 'enabled',
|
| 130 |
+
budget_tokens: budgetTokens[reasoningLevel]
|
| 131 |
+
};
|
| 132 |
+
} else {
|
| 133 |
+
// 关闭或无效:显式删除 thinking 字段
|
| 134 |
+
// 这确保删除原始请求中的任何 thinking 字段
|
| 135 |
+
delete anthropicRequest.thinking;
|
| 136 |
+
}
|
| 137 |
+
|
| 138 |
+
// 传递其他兼容参数
|
| 139 |
+
if (openaiRequest.temperature !== undefined) {
|
| 140 |
+
anthropicRequest.temperature = openaiRequest.temperature;
|
| 141 |
+
}
|
| 142 |
+
if (openaiRequest.top_p !== undefined) {
|
| 143 |
+
anthropicRequest.top_p = openaiRequest.top_p;
|
| 144 |
+
}
|
| 145 |
+
if (openaiRequest.stop !== undefined) {
|
| 146 |
+
anthropicRequest.stop_sequences = Array.isArray(openaiRequest.stop)
|
| 147 |
+
? openaiRequest.stop
|
| 148 |
+
: [openaiRequest.stop];
|
| 149 |
+
}
|
| 150 |
+
|
| 151 |
+
return anthropicRequest;
|
| 152 |
+
}
|
| 153 |
+
|
| 154 |
+
export function getAnthropicHeaders(authHeader, clientHeaders = {}, isStreaming = true, modelId = null) {
|
| 155 |
+
// 如果未提供则生成唯一 ID
|
| 156 |
+
const sessionId = clientHeaders['x-session-id'] || generateUUID();
|
| 157 |
+
const messageId = clientHeaders['x-assistant-message-id'] || generateUUID();
|
| 158 |
+
|
| 159 |
+
const headers = {
|
| 160 |
+
'accept': 'application/json',
|
| 161 |
+
'content-type': 'application/json',
|
| 162 |
+
'anthropic-version': clientHeaders['anthropic-version'] || '2023-06-01',
|
| 163 |
+
'authorization': authHeader || '',
|
| 164 |
+
'x-api-key': 'placeholder',
|
| 165 |
+
'x-api-provider': 'anthropic',
|
| 166 |
+
'x-factory-client': 'cli',
|
| 167 |
+
'x-session-id': sessionId,
|
| 168 |
+
'x-assistant-message-id': messageId,
|
| 169 |
+
'user-agent': getUserAgent(),
|
| 170 |
+
'x-stainless-timeout': '600',
|
| 171 |
+
'connection': 'keep-alive'
|
| 172 |
+
}
|
| 173 |
+
|
| 174 |
+
// 根据推理配置处理 anthropic-beta 头
|
| 175 |
+
const reasoningLevel = modelId ? getModelReasoning(modelId) : null;
|
| 176 |
+
let betaValues = [];
|
| 177 |
+
|
| 178 |
+
// 从客户端头添加现有的 beta 值
|
| 179 |
+
if (clientHeaders['anthropic-beta']) {
|
| 180 |
+
const existingBeta = clientHeaders['anthropic-beta'];
|
| 181 |
+
betaValues = existingBeta.split(',').map(v => v.trim());
|
| 182 |
+
}
|
| 183 |
+
|
| 184 |
+
// 根据推理配置处理 thinking beta
|
| 185 |
+
const thinkingBeta = 'interleaved-thinking-2025-05-14';
|
| 186 |
+
if (reasoningLevel === 'auto') {
|
| 187 |
+
// 自动模式:不修改 anthropic-beta 头,保留原始值
|
| 188 |
+
// betaValues 保持客户端头的不变
|
| 189 |
+
} else if (reasoningLevel && ['low', 'medium', 'high'].includes(reasoningLevel)) {
|
| 190 |
+
// 如果尚未存在,则添加 thinking beta
|
| 191 |
+
if (!betaValues.includes(thinkingBeta)) {
|
| 192 |
+
betaValues.push(thinkingBeta);
|
| 193 |
+
}
|
| 194 |
+
} else {
|
| 195 |
+
// 如果推理关闭或无效,则删除 thinking beta
|
| 196 |
+
betaValues = betaValues.filter(v => v !== thinkingBeta);
|
| 197 |
+
}
|
| 198 |
+
|
| 199 |
+
// 如果有任何值,则设置 anthropic-beta 头
|
| 200 |
+
if (betaValues.length > 0) {
|
| 201 |
+
headers['anthropic-beta'] = betaValues.join(', ');
|
| 202 |
+
}
|
| 203 |
+
|
| 204 |
+
// 使用默认值传递 Stainless SDK 头
|
| 205 |
+
const stainlessDefaults = {
|
| 206 |
+
'x-stainless-arch': 'x64',
|
| 207 |
+
'x-stainless-lang': 'js',
|
| 208 |
+
'x-stainless-os': 'MacOS',
|
| 209 |
+
'x-stainless-runtime': 'node',
|
| 210 |
+
'x-stainless-retry-count': '0',
|
| 211 |
+
'x-stainless-package-version': '0.57.0',
|
| 212 |
+
'x-stainless-runtime-version': 'v24.3.0'
|
| 213 |
+
};
|
| 214 |
+
|
| 215 |
+
// 根据流式传输设置 helper-method
|
| 216 |
+
if (isStreaming) {
|
| 217 |
+
headers['x-stainless-helper-method'] = 'stream';
|
| 218 |
+
}
|
| 219 |
+
|
| 220 |
+
// 从客户端复制 Stainless 头或使用默认值
|
| 221 |
+
Object.keys(stainlessDefaults).forEach(header => {
|
| 222 |
+
headers[header] = clientHeaders[header] || stainlessDefaults[header];
|
| 223 |
+
});
|
| 224 |
+
|
| 225 |
+
// 如果客户端提供,则覆盖默认超时
|
| 226 |
+
if (clientHeaders['x-stainless-timeout']) {
|
| 227 |
+
headers['x-stainless-timeout'] = clientHeaders['x-stainless-timeout'];
|
| 228 |
+
}
|
| 229 |
+
|
| 230 |
+
return headers;
|
| 231 |
+
}
|
| 232 |
+
|
| 233 |
+
function generateUUID() {
|
| 234 |
+
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
|
| 235 |
+
const r = Math.random() * 16 | 0;
|
| 236 |
+
const v = c == 'x' ? r : (r & 0x3 | 0x8);
|
| 237 |
+
return v.toString(16);
|
| 238 |
+
});
|
| 239 |
+
}
|
src/transformers/requests/common.js
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { getSystemPrompt, getUserAgent } from '../../configs/config.js'
|
| 2 |
+
|
| 3 |
+
export function transformToCommon(openaiRequest) {
|
| 4 |
+
// 基本保持 OpenAI 格式,只在 messages 前面插入 system 消息
|
| 5 |
+
const commonRequest = {
|
| 6 |
+
...openaiRequest
|
| 7 |
+
};
|
| 8 |
+
|
| 9 |
+
const systemPrompt = getSystemPrompt();
|
| 10 |
+
|
| 11 |
+
if (systemPrompt) {
|
| 12 |
+
// 检查是否已有 system 消息
|
| 13 |
+
const hasSystemMessage = commonRequest.messages?.some(m => m.role === 'system');
|
| 14 |
+
|
| 15 |
+
if (hasSystemMessage) {
|
| 16 |
+
// 如果已有 system 消息,在第一个 system 消息前插入我们的 system prompt
|
| 17 |
+
commonRequest.messages = commonRequest.messages.map((msg, index) => {
|
| 18 |
+
if (msg.role === 'system' && index === commonRequest.messages.findIndex(m => m.role === 'system')) {
|
| 19 |
+
// 找到第一个 system 消息,前置我们的 prompt
|
| 20 |
+
return {
|
| 21 |
+
role: 'system',
|
| 22 |
+
content: systemPrompt + (typeof msg.content === 'string' ? msg.content?.replace("You are Claude Code, Anthropic's official CLI for Claude.", 'You are bot.') : '')
|
| 23 |
+
};
|
| 24 |
+
}
|
| 25 |
+
return msg;
|
| 26 |
+
});
|
| 27 |
+
} else {
|
| 28 |
+
// 如果没有 system 消息,在 messages 数组最前面插入
|
| 29 |
+
commonRequest.messages = [
|
| 30 |
+
{
|
| 31 |
+
role: 'system',
|
| 32 |
+
content: systemPrompt
|
| 33 |
+
},
|
| 34 |
+
...(commonRequest.messages || [])
|
| 35 |
+
];
|
| 36 |
+
}
|
| 37 |
+
}
|
| 38 |
+
|
| 39 |
+
return commonRequest;
|
| 40 |
+
}
|
| 41 |
+
|
| 42 |
+
export function getCommonHeaders(authHeader, clientHeaders = {}) {
|
| 43 |
+
// 如果未提供则生成唯一 ID
|
| 44 |
+
const sessionId = clientHeaders['x-session-id'] || generateUUID();
|
| 45 |
+
const messageId = clientHeaders['x-assistant-message-id'] || generateUUID();
|
| 46 |
+
|
| 47 |
+
const headers = {
|
| 48 |
+
'accept': 'application/json',
|
| 49 |
+
'content-type': 'application/json',
|
| 50 |
+
'authorization': authHeader || '',
|
| 51 |
+
'x-api-provider': 'baseten',
|
| 52 |
+
'x-factory-client': 'cli',
|
| 53 |
+
'x-session-id': sessionId,
|
| 54 |
+
'x-assistant-message-id': messageId,
|
| 55 |
+
'user-agent': getUserAgent(),
|
| 56 |
+
'connection': 'keep-alive'
|
| 57 |
+
};
|
| 58 |
+
|
| 59 |
+
// 使用默认值传递 Stainless SDK 头
|
| 60 |
+
const stainlessDefaults = {
|
| 61 |
+
'x-stainless-arch': 'x64',
|
| 62 |
+
'x-stainless-lang': 'js',
|
| 63 |
+
'x-stainless-os': 'MacOS',
|
| 64 |
+
'x-stainless-runtime': 'node',
|
| 65 |
+
'x-stainless-retry-count': '0',
|
| 66 |
+
'x-stainless-package-version': '5.23.2',
|
| 67 |
+
'x-stainless-runtime-version': 'v24.3.0'
|
| 68 |
+
};
|
| 69 |
+
|
| 70 |
+
// 从客户端复制 Stainless 头或使用默认值
|
| 71 |
+
Object.keys(stainlessDefaults).forEach(header => {
|
| 72 |
+
headers[header] = clientHeaders[header] || stainlessDefaults[header];
|
| 73 |
+
});
|
| 74 |
+
|
| 75 |
+
return headers;
|
| 76 |
+
}
|
| 77 |
+
|
| 78 |
+
function generateUUID() {
|
| 79 |
+
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
|
| 80 |
+
const r = Math.random() * 16 | 0;
|
| 81 |
+
const v = c == 'x' ? r : (r & 0x3 | 0x8);
|
| 82 |
+
return v.toString(16);
|
| 83 |
+
});
|
| 84 |
+
}
|
src/transformers/requests/openai.js
ADDED
|
@@ -0,0 +1,174 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { getSystemPrompt, getModelReasoning, getUserAgent } from '../../configs/config.js'
|
| 2 |
+
|
| 3 |
+
export function transformToOpenAI(openaiRequest) {
|
| 4 |
+
|
| 5 |
+
const targetRequest = {
|
| 6 |
+
model: openaiRequest.model,
|
| 7 |
+
input: [],
|
| 8 |
+
store: false
|
| 9 |
+
};
|
| 10 |
+
|
| 11 |
+
// 仅在客户端明确提供时添加 stream 参数
|
| 12 |
+
if (openaiRequest.stream !== undefined) {
|
| 13 |
+
targetRequest.stream = openaiRequest.stream;
|
| 14 |
+
}
|
| 15 |
+
|
| 16 |
+
// 将 max_tokens 转换为 max_output_tokens
|
| 17 |
+
if (openaiRequest.max_tokens) {
|
| 18 |
+
targetRequest.max_output_tokens = openaiRequest.max_tokens;
|
| 19 |
+
} else if (openaiRequest.max_completion_tokens) {
|
| 20 |
+
targetRequest.max_output_tokens = openaiRequest.max_completion_tokens;
|
| 21 |
+
}
|
| 22 |
+
|
| 23 |
+
// Transform messages to input
|
| 24 |
+
if (openaiRequest.messages && Array.isArray(openaiRequest.messages)) {
|
| 25 |
+
for (const msg of openaiRequest.messages) {
|
| 26 |
+
const inputMsg = {
|
| 27 |
+
role: msg.role,
|
| 28 |
+
content: []
|
| 29 |
+
};
|
| 30 |
+
|
| 31 |
+
// Determine content type based on role
|
| 32 |
+
// user role uses 'input_text', assistant role uses 'output_text'
|
| 33 |
+
const textType = msg.role === 'assistant' ? 'output_text' : 'input_text';
|
| 34 |
+
const imageType = msg.role === 'assistant' ? 'output_image' : 'input_image';
|
| 35 |
+
|
| 36 |
+
if (typeof msg.content === 'string') {
|
| 37 |
+
inputMsg.content.push({
|
| 38 |
+
type: textType,
|
| 39 |
+
text: msg.content?.replace("You are Claude Code, Anthropic's official CLI for Claude.", 'You are bot.')
|
| 40 |
+
});
|
| 41 |
+
} else if (Array.isArray(msg.content)) {
|
| 42 |
+
for (const part of msg.content) {
|
| 43 |
+
if (part.type === 'text') {
|
| 44 |
+
inputMsg.content.push({
|
| 45 |
+
type: textType,
|
| 46 |
+
text: part.text?.replace("You are Claude Code, Anthropic's official CLI for Claude.", 'You are bot.')
|
| 47 |
+
});
|
| 48 |
+
} else if (part.type === 'image_url') {
|
| 49 |
+
inputMsg.content.push({
|
| 50 |
+
type: imageType,
|
| 51 |
+
image_url: part.image_url
|
| 52 |
+
});
|
| 53 |
+
} else {
|
| 54 |
+
// Pass through other types as-is
|
| 55 |
+
inputMsg.content.push(part);
|
| 56 |
+
}
|
| 57 |
+
}
|
| 58 |
+
}
|
| 59 |
+
|
| 60 |
+
targetRequest.input.push(inputMsg);
|
| 61 |
+
}
|
| 62 |
+
}
|
| 63 |
+
|
| 64 |
+
// Transform tools if present
|
| 65 |
+
if (openaiRequest.tools && Array.isArray(openaiRequest.tools)) {
|
| 66 |
+
targetRequest.tools = openaiRequest.tools.map(tool => ({
|
| 67 |
+
...tool,
|
| 68 |
+
strict: false
|
| 69 |
+
}));
|
| 70 |
+
}
|
| 71 |
+
|
| 72 |
+
// Extract system message as instructions and prepend system prompt
|
| 73 |
+
const systemPrompt = getSystemPrompt();
|
| 74 |
+
const systemMessage = openaiRequest.messages?.find(m => m.role === 'system');
|
| 75 |
+
|
| 76 |
+
if (systemMessage) {
|
| 77 |
+
let userInstructions = '';
|
| 78 |
+
if (typeof systemMessage.content === 'string') {
|
| 79 |
+
userInstructions = systemMessage.content;
|
| 80 |
+
} else if (Array.isArray(systemMessage.content)) {
|
| 81 |
+
userInstructions = systemMessage.content
|
| 82 |
+
.filter(p => p.type === 'text')
|
| 83 |
+
.map(p => p.text)
|
| 84 |
+
.join('\n');
|
| 85 |
+
}
|
| 86 |
+
targetRequest.instructions = systemPrompt + userInstructions;
|
| 87 |
+
targetRequest.input = targetRequest.input.filter(m => m.role !== 'system');
|
| 88 |
+
} else if (systemPrompt) {
|
| 89 |
+
// If no user-provided system message, just add the system prompt
|
| 90 |
+
targetRequest.instructions = systemPrompt;
|
| 91 |
+
}
|
| 92 |
+
|
| 93 |
+
// 根据模型配置处理 reasoning 字段
|
| 94 |
+
const reasoningLevel = getModelReasoning(openaiRequest.model);
|
| 95 |
+
if (reasoningLevel === 'auto') {
|
| 96 |
+
// 自动模式:完全保留原始请求的 reasoning 字段
|
| 97 |
+
if (openaiRequest.reasoning !== undefined) {
|
| 98 |
+
targetRequest.reasoning = openaiRequest.reasoning;
|
| 99 |
+
}
|
| 100 |
+
// 如果原始请求没有 reasoning 字段,则不添加
|
| 101 |
+
} else if (reasoningLevel && ['low', 'medium', 'high'].includes(reasoningLevel)) {
|
| 102 |
+
// 特定级别:使用模型配置覆盖
|
| 103 |
+
targetRequest.reasoning = {
|
| 104 |
+
effort: reasoningLevel,
|
| 105 |
+
summary: 'auto'
|
| 106 |
+
};
|
| 107 |
+
} else {
|
| 108 |
+
// 关闭或无效:显式删除 reasoning 字段
|
| 109 |
+
// 这确保删除原始请求中的任何 reasoning 字段
|
| 110 |
+
delete targetRequest.reasoning;
|
| 111 |
+
}
|
| 112 |
+
|
| 113 |
+
// Pass through other parameters
|
| 114 |
+
if (openaiRequest.temperature !== undefined) {
|
| 115 |
+
targetRequest.temperature = openaiRequest.temperature;
|
| 116 |
+
}
|
| 117 |
+
if (openaiRequest.top_p !== undefined) {
|
| 118 |
+
targetRequest.top_p = openaiRequest.top_p;
|
| 119 |
+
}
|
| 120 |
+
if (openaiRequest.presence_penalty !== undefined) {
|
| 121 |
+
targetRequest.presence_penalty = openaiRequest.presence_penalty;
|
| 122 |
+
}
|
| 123 |
+
if (openaiRequest.frequency_penalty !== undefined) {
|
| 124 |
+
targetRequest.frequency_penalty = openaiRequest.frequency_penalty;
|
| 125 |
+
}
|
| 126 |
+
if (openaiRequest.parallel_tool_calls !== undefined) {
|
| 127 |
+
targetRequest.parallel_tool_calls = openaiRequest.parallel_tool_calls;
|
| 128 |
+
}
|
| 129 |
+
|
| 130 |
+
return targetRequest;
|
| 131 |
+
}
|
| 132 |
+
|
| 133 |
+
export function getOpenAIHeaders(authHeader, clientHeaders = {}) {
|
| 134 |
+
// 如果未提供则生成唯一 ID
|
| 135 |
+
const sessionId = clientHeaders['x-session-id'] || generateUUID();
|
| 136 |
+
const messageId = clientHeaders['x-assistant-message-id'] || generateUUID();
|
| 137 |
+
|
| 138 |
+
const headers = {
|
| 139 |
+
'content-type': 'application/json',
|
| 140 |
+
'authorization': authHeader || '',
|
| 141 |
+
'x-api-provider': 'azure_openai',
|
| 142 |
+
'x-factory-client': 'cli',
|
| 143 |
+
'x-session-id': sessionId,
|
| 144 |
+
'x-assistant-message-id': messageId,
|
| 145 |
+
'user-agent': getUserAgent(),
|
| 146 |
+
'connection': 'keep-alive'
|
| 147 |
+
};
|
| 148 |
+
|
| 149 |
+
// Pass through Stainless SDK headers with defaults
|
| 150 |
+
const stainlessDefaults = {
|
| 151 |
+
'x-stainless-arch': 'x64',
|
| 152 |
+
'x-stainless-lang': 'js',
|
| 153 |
+
'x-stainless-os': 'MacOS',
|
| 154 |
+
'x-stainless-runtime': 'node',
|
| 155 |
+
'x-stainless-retry-count': '0',
|
| 156 |
+
'x-stainless-package-version': '5.23.2',
|
| 157 |
+
'x-stainless-runtime-version': 'v24.3.0'
|
| 158 |
+
};
|
| 159 |
+
|
| 160 |
+
// Copy Stainless headers from client or use defaults
|
| 161 |
+
Object.keys(stainlessDefaults).forEach(header => {
|
| 162 |
+
headers[header] = clientHeaders[header] || stainlessDefaults[header];
|
| 163 |
+
});
|
| 164 |
+
|
| 165 |
+
return headers;
|
| 166 |
+
}
|
| 167 |
+
|
| 168 |
+
function generateUUID() {
|
| 169 |
+
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
|
| 170 |
+
const r = Math.random() * 16 | 0;
|
| 171 |
+
const v = c == 'x' ? r : (r & 0x3 | 0x8);
|
| 172 |
+
return v.toString(16);
|
| 173 |
+
});
|
| 174 |
+
}
|
src/transformers/responses/anthropic.js
ADDED
|
@@ -0,0 +1,134 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
export class AnthropicResponseTransformer {
|
| 2 |
+
constructor(model, requestId) {
|
| 3 |
+
this.model = model;
|
| 4 |
+
this.requestId = requestId || `chatcmpl-${Date.now()}`;
|
| 5 |
+
this.created = Math.floor(Date.now() / 1000);
|
| 6 |
+
this.messageId = null;
|
| 7 |
+
this.currentIndex = 0;
|
| 8 |
+
}
|
| 9 |
+
|
| 10 |
+
parseSSELine(line) {
|
| 11 |
+
if (line.startsWith('event:')) {
|
| 12 |
+
return { type: 'event', value: line.slice(6).trim() };
|
| 13 |
+
}
|
| 14 |
+
if (line.startsWith('data:')) {
|
| 15 |
+
const dataStr = line.slice(5).trim();
|
| 16 |
+
try {
|
| 17 |
+
return { type: 'data', value: JSON.parse(dataStr) };
|
| 18 |
+
} catch (e) {
|
| 19 |
+
return { type: 'data', value: dataStr };
|
| 20 |
+
}
|
| 21 |
+
}
|
| 22 |
+
return null;
|
| 23 |
+
}
|
| 24 |
+
|
| 25 |
+
transformEvent(eventType, eventData) {
|
| 26 |
+
if (eventType === 'message_start') {
|
| 27 |
+
this.messageId = eventData.message?.id || this.requestId;
|
| 28 |
+
return this.createOpenAIChunk('', 'assistant', false);
|
| 29 |
+
}
|
| 30 |
+
|
| 31 |
+
if (eventType === 'content_block_start') {
|
| 32 |
+
return null;
|
| 33 |
+
}
|
| 34 |
+
|
| 35 |
+
if (eventType === 'content_block_delta') {
|
| 36 |
+
const text = eventData.delta?.text || '';
|
| 37 |
+
return this.createOpenAIChunk(text, null, false);
|
| 38 |
+
}
|
| 39 |
+
|
| 40 |
+
if (eventType === 'content_block_stop') {
|
| 41 |
+
return null;
|
| 42 |
+
}
|
| 43 |
+
|
| 44 |
+
if (eventType === 'message_delta') {
|
| 45 |
+
const stopReason = eventData.delta?.stop_reason;
|
| 46 |
+
if (stopReason) {
|
| 47 |
+
return this.createOpenAIChunk('', null, true, this.mapStopReason(stopReason));
|
| 48 |
+
}
|
| 49 |
+
return null;
|
| 50 |
+
}
|
| 51 |
+
|
| 52 |
+
if (eventType === 'message_stop') {
|
| 53 |
+
return this.createDoneSignal();
|
| 54 |
+
}
|
| 55 |
+
|
| 56 |
+
if (eventType === 'ping') {
|
| 57 |
+
return null;
|
| 58 |
+
}
|
| 59 |
+
|
| 60 |
+
return null;
|
| 61 |
+
}
|
| 62 |
+
|
| 63 |
+
createOpenAIChunk(content, role = null, finish = false, finishReason = null) {
|
| 64 |
+
const chunk = {
|
| 65 |
+
id: this.requestId,
|
| 66 |
+
object: 'chat.completion.chunk',
|
| 67 |
+
created: this.created,
|
| 68 |
+
model: this.model,
|
| 69 |
+
choices: [
|
| 70 |
+
{
|
| 71 |
+
index: 0,
|
| 72 |
+
delta: {},
|
| 73 |
+
finish_reason: finish ? finishReason : null
|
| 74 |
+
}
|
| 75 |
+
]
|
| 76 |
+
};
|
| 77 |
+
|
| 78 |
+
if (role) {
|
| 79 |
+
chunk.choices[0].delta.role = role;
|
| 80 |
+
}
|
| 81 |
+
if (content) {
|
| 82 |
+
chunk.choices[0].delta.content = content;
|
| 83 |
+
}
|
| 84 |
+
|
| 85 |
+
return `data: ${JSON.stringify(chunk)}\n\n`;
|
| 86 |
+
}
|
| 87 |
+
|
| 88 |
+
createDoneSignal() {
|
| 89 |
+
return 'data: [DONE]\n\n';
|
| 90 |
+
}
|
| 91 |
+
|
| 92 |
+
mapStopReason(anthropicReason) {
|
| 93 |
+
const mapping = {
|
| 94 |
+
'end_turn': 'stop',
|
| 95 |
+
'max_tokens': 'length',
|
| 96 |
+
'stop_sequence': 'stop',
|
| 97 |
+
'tool_use': 'tool_calls'
|
| 98 |
+
};
|
| 99 |
+
return mapping[anthropicReason] || 'stop';
|
| 100 |
+
}
|
| 101 |
+
|
| 102 |
+
async *transformStream(sourceStream) {
|
| 103 |
+
let buffer = '';
|
| 104 |
+
let currentEvent = null;
|
| 105 |
+
|
| 106 |
+
try {
|
| 107 |
+
for await (const chunk of sourceStream) {
|
| 108 |
+
buffer += chunk.toString();
|
| 109 |
+
const lines = buffer.split('\n');
|
| 110 |
+
buffer = lines.pop() || '';
|
| 111 |
+
|
| 112 |
+
for (const line of lines) {
|
| 113 |
+
if (!line.trim()) continue;
|
| 114 |
+
|
| 115 |
+
const parsed = this.parseSSELine(line);
|
| 116 |
+
if (!parsed) continue;
|
| 117 |
+
|
| 118 |
+
if (parsed.type === 'event') {
|
| 119 |
+
currentEvent = parsed.value;
|
| 120 |
+
} else if (parsed.type === 'data' && currentEvent) {
|
| 121 |
+
const transformed = this.transformEvent(currentEvent, parsed.value);
|
| 122 |
+
if (transformed) {
|
| 123 |
+
yield transformed;
|
| 124 |
+
}
|
| 125 |
+
currentEvent = null;
|
| 126 |
+
}
|
| 127 |
+
}
|
| 128 |
+
}
|
| 129 |
+
} catch (error) {
|
| 130 |
+
console.error('Anthropic 流转换错误:', error);
|
| 131 |
+
throw error;
|
| 132 |
+
}
|
| 133 |
+
}
|
| 134 |
+
}
|
src/transformers/responses/openai.js
ADDED
|
@@ -0,0 +1,123 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
export class OpenAIResponseTransformer {
|
| 2 |
+
constructor(model, requestId) {
|
| 3 |
+
this.model = model;
|
| 4 |
+
this.requestId = requestId || `chatcmpl-${Date.now()}`;
|
| 5 |
+
this.created = Math.floor(Date.now() / 1000);
|
| 6 |
+
}
|
| 7 |
+
|
| 8 |
+
parseSSELine(line) {
|
| 9 |
+
if (line.startsWith('event:')) {
|
| 10 |
+
return { type: 'event', value: line.slice(6).trim() };
|
| 11 |
+
}
|
| 12 |
+
if (line.startsWith('data:')) {
|
| 13 |
+
const dataStr = line.slice(5).trim();
|
| 14 |
+
try {
|
| 15 |
+
return { type: 'data', value: JSON.parse(dataStr) };
|
| 16 |
+
} catch (e) {
|
| 17 |
+
return { type: 'data', value: dataStr };
|
| 18 |
+
}
|
| 19 |
+
}
|
| 20 |
+
return null;
|
| 21 |
+
}
|
| 22 |
+
|
| 23 |
+
transformEvent(eventType, eventData) {
|
| 24 |
+
if (eventType === 'response.created') {
|
| 25 |
+
return this.createOpenAIChunk('', 'assistant', false);
|
| 26 |
+
}
|
| 27 |
+
|
| 28 |
+
if (eventType === 'response.in_progress') {
|
| 29 |
+
return null;
|
| 30 |
+
}
|
| 31 |
+
|
| 32 |
+
if (eventType === 'response.output_text.delta') {
|
| 33 |
+
const text = eventData.delta || eventData.text || '';
|
| 34 |
+
return this.createOpenAIChunk(text, null, false);
|
| 35 |
+
}
|
| 36 |
+
|
| 37 |
+
if (eventType === 'response.output_text.done') {
|
| 38 |
+
return null;
|
| 39 |
+
}
|
| 40 |
+
|
| 41 |
+
if (eventType === 'response.done') {
|
| 42 |
+
const status = eventData.response?.status;
|
| 43 |
+
let finishReason = 'stop';
|
| 44 |
+
|
| 45 |
+
if (status === 'completed') {
|
| 46 |
+
finishReason = 'stop';
|
| 47 |
+
} else if (status === 'incomplete') {
|
| 48 |
+
finishReason = 'length';
|
| 49 |
+
}
|
| 50 |
+
|
| 51 |
+
const finalChunk = this.createOpenAIChunk('', null, true, finishReason);
|
| 52 |
+
const done = this.createDoneSignal();
|
| 53 |
+
return finalChunk + done;
|
| 54 |
+
}
|
| 55 |
+
|
| 56 |
+
return null;
|
| 57 |
+
}
|
| 58 |
+
|
| 59 |
+
createOpenAIChunk(content, role = null, finish = false, finishReason = null) {
|
| 60 |
+
const chunk = {
|
| 61 |
+
id: this.requestId,
|
| 62 |
+
object: 'chat.completion.chunk',
|
| 63 |
+
created: this.created,
|
| 64 |
+
model: this.model,
|
| 65 |
+
choices: [
|
| 66 |
+
{
|
| 67 |
+
index: 0,
|
| 68 |
+
delta: {},
|
| 69 |
+
finish_reason: finish ? finishReason : null
|
| 70 |
+
}
|
| 71 |
+
]
|
| 72 |
+
};
|
| 73 |
+
|
| 74 |
+
if (role) {
|
| 75 |
+
chunk.choices[0].delta.role = role;
|
| 76 |
+
}
|
| 77 |
+
if (content) {
|
| 78 |
+
chunk.choices[0].delta.content = content;
|
| 79 |
+
}
|
| 80 |
+
|
| 81 |
+
return `data: ${JSON.stringify(chunk)}\n\n`;
|
| 82 |
+
}
|
| 83 |
+
|
| 84 |
+
createDoneSignal() {
|
| 85 |
+
return 'data: [DONE]\n\n';
|
| 86 |
+
}
|
| 87 |
+
|
| 88 |
+
async *transformStream(sourceStream) {
|
| 89 |
+
let buffer = '';
|
| 90 |
+
let currentEvent = null;
|
| 91 |
+
|
| 92 |
+
try {
|
| 93 |
+
for await (const chunk of sourceStream) {
|
| 94 |
+
buffer += chunk.toString();
|
| 95 |
+
const lines = buffer.split('\n');
|
| 96 |
+
buffer = lines.pop() || '';
|
| 97 |
+
|
| 98 |
+
for (const line of lines) {
|
| 99 |
+
if (!line.trim()) continue;
|
| 100 |
+
|
| 101 |
+
const parsed = this.parseSSELine(line);
|
| 102 |
+
if (!parsed) continue;
|
| 103 |
+
|
| 104 |
+
if (parsed.type === 'event') {
|
| 105 |
+
currentEvent = parsed.value;
|
| 106 |
+
} else if (parsed.type === 'data' && currentEvent) {
|
| 107 |
+
const transformed = this.transformEvent(currentEvent, parsed.value);
|
| 108 |
+
if (transformed) {
|
| 109 |
+
yield transformed;
|
| 110 |
+
}
|
| 111 |
+
}
|
| 112 |
+
}
|
| 113 |
+
}
|
| 114 |
+
|
| 115 |
+
if (currentEvent === 'response.done' || currentEvent === 'response.completed') {
|
| 116 |
+
yield this.createDoneSignal();
|
| 117 |
+
}
|
| 118 |
+
} catch (error) {
|
| 119 |
+
console.error('OpenAI 流转换错误:', error);
|
| 120 |
+
throw error;
|
| 121 |
+
}
|
| 122 |
+
}
|
| 123 |
+
}
|