File size: 6,170 Bytes
6dfd5ec |
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 176 177 178 |
import { logDebug } from '../logger.js';
import { getSystemPrompt, getModelReasoning, getUserAgent } from '../config.js';
export function transformToOpenAI(openaiRequest) {
logDebug('Transforming OpenAI request to target OpenAI format');
const targetRequest = {
model: openaiRequest.model,
input: [],
store: false
};
// Only add stream parameter if explicitly provided by client
if (openaiRequest.stream !== undefined) {
targetRequest.stream = openaiRequest.stream;
}
// Transform max_tokens to 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
});
} else if (Array.isArray(msg.content)) {
for (const part of msg.content) {
if (part.type === 'text') {
inputMsg.content.push({
type: textType,
text: part.text
});
} 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;
}
// Handle reasoning field based on model configuration
const reasoningLevel = getModelReasoning(openaiRequest.model);
if (reasoningLevel === 'auto') {
// Auto mode: preserve original request's reasoning field exactly as-is
if (openaiRequest.reasoning !== undefined) {
targetRequest.reasoning = openaiRequest.reasoning;
}
// If original request has no reasoning field, don't add one
} else if (reasoningLevel && ['low', 'medium', 'high'].includes(reasoningLevel)) {
// Specific level: override with model configuration
targetRequest.reasoning = {
effort: reasoningLevel,
summary: 'auto'
};
} else {
// Off or invalid: explicitly remove reasoning field
// This ensures any reasoning field from the original request is deleted
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;
}
logDebug('Transformed target OpenAI request', targetRequest);
return targetRequest;
}
export function getOpenAIHeaders(authHeader, clientHeaders = {}) {
// Generate unique IDs if not provided
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);
});
}
|