File size: 6,121 Bytes
0133533 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 |
import { getSystemPrompt, getModelReasoning, getUserAgent } from '../../configs/config.js'
export function transformToOpenAI(openaiRequest) {
const targetRequest = {
model: openaiRequest.model,
input: [],
store: false
};
// 仅在客户端明确提供时添加 stream 参数
if (openaiRequest.stream !== undefined) {
targetRequest.stream = openaiRequest.stream;
}
// 将 max_tokens 转换为 max_output_tokens
if (openaiRequest.max_tokens) {
targetRequest.max_output_tokens = openaiRequest.max_tokens;
} else if (openaiRequest.max_completion_tokens) {
targetRequest.max_output_tokens = openaiRequest.max_completion_tokens;
}
// Transform messages to input
if (openaiRequest.messages && Array.isArray(openaiRequest.messages)) {
for (const msg of openaiRequest.messages) {
const inputMsg = {
role: msg.role,
content: []
};
// Determine content type based on role
// user role uses 'input_text', assistant role uses 'output_text'
const textType = msg.role === 'assistant' ? 'output_text' : 'input_text';
const imageType = msg.role === 'assistant' ? 'output_image' : 'input_image';
if (typeof msg.content === 'string') {
inputMsg.content.push({
type: textType,
text: msg.content?.replace("You are Claude Code, Anthropic's official CLI for Claude.", 'You are bot.')
});
} else if (Array.isArray(msg.content)) {
for (const part of msg.content) {
if (part.type === 'text') {
inputMsg.content.push({
type: textType,
text: part.text?.replace("You are Claude Code, Anthropic's official CLI for Claude.", 'You are bot.')
});
} else if (part.type === 'image_url') {
inputMsg.content.push({
type: imageType,
image_url: part.image_url
});
} else {
// Pass through other types as-is
inputMsg.content.push(part);
}
}
}
targetRequest.input.push(inputMsg);
}
}
// Transform tools if present
if (openaiRequest.tools && Array.isArray(openaiRequest.tools)) {
targetRequest.tools = openaiRequest.tools.map(tool => ({
...tool,
strict: false
}));
}
// Extract system message as instructions and prepend system prompt
const systemPrompt = getSystemPrompt();
const systemMessage = openaiRequest.messages?.find(m => m.role === 'system');
if (systemMessage) {
let userInstructions = '';
if (typeof systemMessage.content === 'string') {
userInstructions = systemMessage.content;
} else if (Array.isArray(systemMessage.content)) {
userInstructions = systemMessage.content
.filter(p => p.type === 'text')
.map(p => p.text)
.join('\n');
}
targetRequest.instructions = systemPrompt + userInstructions;
targetRequest.input = targetRequest.input.filter(m => m.role !== 'system');
} else if (systemPrompt) {
// If no user-provided system message, just add the system prompt
targetRequest.instructions = systemPrompt;
}
// 根据模型配置处理 reasoning 字段
const reasoningLevel = getModelReasoning(openaiRequest.model);
if (reasoningLevel === 'auto') {
// 自动模式:完全保留原始请求的 reasoning 字段
if (openaiRequest.reasoning !== undefined) {
targetRequest.reasoning = openaiRequest.reasoning;
}
// 如果原始请求没有 reasoning 字段,则不添加
} else if (reasoningLevel && ['low', 'medium', 'high'].includes(reasoningLevel)) {
// 特定级别:使用模型配置覆盖
targetRequest.reasoning = {
effort: reasoningLevel,
summary: 'auto'
};
} else {
// 关闭或无效:显式删除 reasoning 字段
// 这确保删除原始请求中的任何 reasoning 字段
delete targetRequest.reasoning;
}
// Pass through other parameters
if (openaiRequest.temperature !== undefined) {
targetRequest.temperature = openaiRequest.temperature;
}
if (openaiRequest.top_p !== undefined) {
targetRequest.top_p = openaiRequest.top_p;
}
if (openaiRequest.presence_penalty !== undefined) {
targetRequest.presence_penalty = openaiRequest.presence_penalty;
}
if (openaiRequest.frequency_penalty !== undefined) {
targetRequest.frequency_penalty = openaiRequest.frequency_penalty;
}
if (openaiRequest.parallel_tool_calls !== undefined) {
targetRequest.parallel_tool_calls = openaiRequest.parallel_tool_calls;
}
return targetRequest;
}
export function getOpenAIHeaders(authHeader, clientHeaders = {}) {
// 如果未提供则生成唯一 ID
const sessionId = clientHeaders['x-session-id'] || generateUUID();
const messageId = clientHeaders['x-assistant-message-id'] || generateUUID();
const headers = {
'content-type': 'application/json',
'authorization': authHeader || '',
'x-api-provider': 'azure_openai',
'x-factory-client': 'cli',
'x-session-id': sessionId,
'x-assistant-message-id': messageId,
'user-agent': getUserAgent(),
'connection': 'keep-alive'
};
// Pass through Stainless SDK headers with defaults
const stainlessDefaults = {
'x-stainless-arch': 'x64',
'x-stainless-lang': 'js',
'x-stainless-os': 'MacOS',
'x-stainless-runtime': 'node',
'x-stainless-retry-count': '0',
'x-stainless-package-version': '5.23.2',
'x-stainless-runtime-version': 'v24.3.0'
};
// Copy Stainless headers from client or use defaults
Object.keys(stainlessDefaults).forEach(header => {
headers[header] = clientHeaders[header] || stainlessDefaults[header];
});
return headers;
}
function generateUUID() {
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
const r = Math.random() * 16 | 0;
const v = c == 'x' ? r : (r & 0x3 | 0x8);
return v.toString(16);
});
}
|