Upload chat.js
Browse files- src/router/chat.js +90 -47
src/router/chat.js
CHANGED
|
@@ -94,74 +94,117 @@ async function convertOpenAIToMammouth(openaiRequest, requestId = null) {
|
|
| 94 |
|
| 95 |
for (const message of regularMessages) {
|
| 96 |
// 处理包含图片的消息
|
| 97 |
-
const imagesData = []
|
| 98 |
let content = message.content
|
|
|
|
| 99 |
|
| 100 |
// 如果是对象数组(多模态内容)
|
| 101 |
if (Array.isArray(message.content)) {
|
| 102 |
const textParts = []
|
|
|
|
| 103 |
|
| 104 |
-
//
|
| 105 |
for (const part of message.content) {
|
| 106 |
if (part.type === 'text') {
|
| 107 |
textParts.push(part.text)
|
| 108 |
} else if (part.type === 'image_url') {
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
if (typeof imageUrl === 'object' && imageUrl.url) {
|
| 113 |
-
imageUrl = imageUrl.url
|
| 114 |
-
}
|
| 115 |
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
if (imageUrl.startsWith('data:image')) {
|
| 119 |
-
uploadedUrls = await imageUploader.uploadFromBase64Smart(
|
| 120 |
-
imageUrl,
|
| 121 |
-
null,
|
| 122 |
-
requestId,
|
| 123 |
-
currentImageIndex
|
| 124 |
-
)
|
| 125 |
-
} else {
|
| 126 |
-
uploadedUrls = await imageUploader.uploadFromUrlSmart(
|
| 127 |
-
imageUrl,
|
| 128 |
-
null,
|
| 129 |
-
requestId,
|
| 130 |
-
currentImageIndex
|
| 131 |
-
)
|
| 132 |
-
}
|
| 133 |
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 137 |
|
| 138 |
-
currentImageIndex++
|
| 139 |
-
} catch (error) {
|
| 140 |
if (requestId) {
|
| 141 |
-
logger.
|
| 142 |
-
imageIndex: currentImageIndex,
|
| 143 |
-
imageUrl: typeof part.image_url === 'string' ? part.image_url.substring(0, 100) : 'object'
|
| 144 |
-
})
|
| 145 |
}
|
| 146 |
-
|
| 147 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 148 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 149 |
}
|
| 150 |
}
|
| 151 |
|
| 152 |
-
//
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 158 |
}
|
| 159 |
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
}))
|
| 165 |
}
|
| 166 |
|
| 167 |
return form
|
|
|
|
| 94 |
|
| 95 |
for (const message of regularMessages) {
|
| 96 |
// 处理包含图片的消息
|
|
|
|
| 97 |
let content = message.content
|
| 98 |
+
let processedMessages = [] // 存储处理后的消息(可能包含多个片段)
|
| 99 |
|
| 100 |
// 如果是对象数组(多模态内容)
|
| 101 |
if (Array.isArray(message.content)) {
|
| 102 |
const textParts = []
|
| 103 |
+
const imageParts = []
|
| 104 |
|
| 105 |
+
// 分离文本和图片部分
|
| 106 |
for (const part of message.content) {
|
| 107 |
if (part.type === 'text') {
|
| 108 |
textParts.push(part.text)
|
| 109 |
} else if (part.type === 'image_url') {
|
| 110 |
+
imageParts.push(part)
|
| 111 |
+
}
|
| 112 |
+
}
|
|
|
|
|
|
|
|
|
|
| 113 |
|
| 114 |
+
// 合并所有文本部分
|
| 115 |
+
const combinedText = textParts.join('\n')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 116 |
|
| 117 |
+
// 处理每个图片
|
| 118 |
+
for (const imagePart of imageParts) {
|
| 119 |
+
try {
|
| 120 |
+
// 获取图片数据
|
| 121 |
+
let imageUrl = imagePart.image_url
|
| 122 |
+
if (typeof imageUrl === 'object' && imageUrl.url) {
|
| 123 |
+
imageUrl = imageUrl.url
|
| 124 |
+
}
|
| 125 |
+
|
| 126 |
+
// 使用智能上传方法处理图片(支持长图)
|
| 127 |
+
let uploadedUrls = []
|
| 128 |
+
if (imageUrl.startsWith('data:image')) {
|
| 129 |
+
uploadedUrls = await imageUploader.uploadFromBase64Smart(
|
| 130 |
+
imageUrl,
|
| 131 |
+
null,
|
| 132 |
+
requestId,
|
| 133 |
+
currentImageIndex
|
| 134 |
+
)
|
| 135 |
+
} else {
|
| 136 |
+
uploadedUrls = await imageUploader.uploadFromUrlSmart(
|
| 137 |
+
imageUrl,
|
| 138 |
+
null,
|
| 139 |
+
requestId,
|
| 140 |
+
currentImageIndex
|
| 141 |
+
)
|
| 142 |
+
}
|
| 143 |
+
|
| 144 |
+
// 如果是长图(多个片段),为每个片段创建单独的消息
|
| 145 |
+
if (uploadedUrls.length > 1) {
|
| 146 |
+
console.log(`[长图处理] 图片${currentImageIndex}被切割为${uploadedUrls.length}个片段,将按顺序发送`)
|
| 147 |
|
|
|
|
|
|
|
| 148 |
if (requestId) {
|
| 149 |
+
logger.logMessageSegmentation(requestId, currentImageIndex, uploadedUrls.length)
|
|
|
|
|
|
|
|
|
|
| 150 |
}
|
| 151 |
+
|
| 152 |
+
uploadedUrls.forEach((url, segmentIndex) => {
|
| 153 |
+
const segmentText = segmentIndex === 0
|
| 154 |
+
? `${combinedText}\n\n[长图片段 ${segmentIndex + 1}/${uploadedUrls.length}]`
|
| 155 |
+
: `[长图片段 ${segmentIndex + 1}/${uploadedUrls.length}]`
|
| 156 |
+
|
| 157 |
+
processedMessages.push({
|
| 158 |
+
content: segmentText || '.',
|
| 159 |
+
imagesData: [url],
|
| 160 |
+
documentsData: []
|
| 161 |
+
})
|
| 162 |
+
|
| 163 |
+
console.log(`[消息生成] 片段${segmentIndex + 1}: "${segmentText.substring(0, 50)}..."`)
|
| 164 |
+
})
|
| 165 |
+
} else {
|
| 166 |
+
// 普通图片,添加到当前消息
|
| 167 |
+
processedMessages.push({
|
| 168 |
+
content: combinedText || '.',
|
| 169 |
+
imagesData: uploadedUrls,
|
| 170 |
+
documentsData: []
|
| 171 |
+
})
|
| 172 |
}
|
| 173 |
+
|
| 174 |
+
currentImageIndex++
|
| 175 |
+
} catch (error) {
|
| 176 |
+
if (requestId) {
|
| 177 |
+
logger.logError(requestId, 'IMAGE_PROCESSING_ERROR', error.message, {
|
| 178 |
+
imageIndex: currentImageIndex,
|
| 179 |
+
imageUrl: typeof imagePart.image_url === 'string' ? imagePart.image_url.substring(0, 100) : 'object'
|
| 180 |
+
})
|
| 181 |
+
}
|
| 182 |
+
console.error('图片处理错误:', error.message)
|
| 183 |
+
currentImageIndex++
|
| 184 |
}
|
| 185 |
}
|
| 186 |
|
| 187 |
+
// 如果没有图片,只有文本
|
| 188 |
+
if (imageParts.length === 0) {
|
| 189 |
+
processedMessages.push({
|
| 190 |
+
content: combinedText || '.',
|
| 191 |
+
imagesData: [],
|
| 192 |
+
documentsData: []
|
| 193 |
+
})
|
| 194 |
+
}
|
| 195 |
+
} else {
|
| 196 |
+
// 纯文本消息
|
| 197 |
+
processedMessages.push({
|
| 198 |
+
content: content || '.',
|
| 199 |
+
imagesData: [],
|
| 200 |
+
documentsData: []
|
| 201 |
+
})
|
| 202 |
}
|
| 203 |
|
| 204 |
+
// 将所有处理后的消息添加到表单
|
| 205 |
+
processedMessages.forEach(msg => {
|
| 206 |
+
form.append('messages', JSON.stringify(msg))
|
| 207 |
+
})
|
|
|
|
| 208 |
}
|
| 209 |
|
| 210 |
return form
|