File size: 16,262 Bytes
b5569e9 | 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 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 | // completionHandler.js
import fetch from "node-fetch";
import { PassThrough } from "stream";
import { log } from '../config/logger.js';
import { logApiCall, generateId, getFileExtension, getFileType } from "./utils.js";
// 上传文件到 Dify,获取文件 ID
async function uploadFileToDify(base64Data, config, userId) {
try {
// 解析 base64 数据 URL,提取 contentType 和 base64 字符串
const matches = base64Data.match(/^data:(.+);base64,(.+)$/);
if (!matches || matches.length !== 3) {
throw new Error("Invalid base64 data");
}
let contentType = matches[1];
const base64String = matches[2];
let fileData = Buffer.from(base64String, "base64");
// 如果 contentType 是 'image/jpg',将其调整为 'image/jpeg'
if (contentType === "image/jpg") {
contentType = "image/jpeg";
}
// 从 contentType 确定文件扩展名
const fileExtension = contentType.split("/")[1]; // 例如 'jpeg'、'png'、'gif'
// 使用扩展名创建文件名
const filename = `image.${fileExtension}`;
// 创建 FormData 并包含 'user' 字段
const form = new FormData();
form.append("file", fileData, {
filename: filename,
contentType: contentType,
});
form.append("user", userId); // 使用提供的用户标识符
// 记录文件上传请求的详细信息
log("info", "正在上传文件到 Dify", {
url: `${config.DIFY_API_URL}/files/upload`,
headers: {
Authorization: `Bearer ${config.API_KEY}`,
...form.getHeaders(),
},
formData: "<<FILE DATA>>", // 出于安全考虑,不记录实际文件数据
});
// 发送上传请求
const response = await fetch(`${config.DIFY_API_URL}/files/upload`, {
method: "POST",
headers: {
Authorization: `Bearer ${config.API_KEY}`,
...form.getHeaders(),
},
body: form,
});
// 记录文件上传响应的详细信息
log("info", "文件上传响应", {
status: response.status,
statusText: response.statusText,
});
if (!response.ok) {
const errorBody = await response.text();
log("error", "文件上传失败", {
status: response.status,
statusText: response.statusText,
errorBody: errorBody,
});
throw new Error(
`文件上传失败: ${response.status} ${response.statusText}: ${errorBody}`
);
}
const result = await response.json();
log("info", "文件上传成功", { fileId: result.id });
return result.id; // 返回文件 ID
} catch (error) {
console.error("上传文件出错:", error);
throw error;
}
}
// 处理 Completion 类型请求
async function handleRequest(req, res, config, requestId, startTime) {
try {
const apiPath = "/completion-messages";
const data = req.body;
const messages = data.messages;
let queryString = "";
let files = [];
// 记录收到的请求头和请求体
log("info", "收到请求", {
requestId,
headers: req.headers,
body: data,
});
const userId = "apiuser"; // 如果可用,替换为实际的用户 ID
// 第一步:先扫描所有消息中的图片内容
log("info", "开始扫描所有消息中的图片", { requestId, messageCount: messages.length });
for (const message of messages) {
if (Array.isArray(message.content)) {
for (const content of message.content) {
if (content.type === "image_url" && content.image_url && content.image_url.url) {
const imageUrl = content.image_url.url;
// 检查URL是否为base64数据
if (imageUrl.startsWith('data:')) {
// 是base64数据,需要上传
const fileExt = getFileExtension(imageUrl);
const fileType = getFileType(fileExt);
log("info", "检测到base64数据,准备上传", { requestId, fileType, fileExt });
const fileId = await uploadFileToDify(
imageUrl,
config,
userId
);
files.push({
type: fileType,
transfer_method: "local_file",
upload_file_id: fileId,
});
} else {
// 是真正的URL,直接使用remote_url方式
const fileExt = getFileExtension(imageUrl);
const fileType = getFileType(fileExt);
log("info", "检测到远程文件URL", { requestId, url: imageUrl.substring(0, 30) + '...', fileType, fileExt });
files.push({
type: fileType,
transfer_method: "remote_url",
url: imageUrl,
});
}
}
}
}
}
// 第二步:从最后一条消息中提取查询文本
const lastMessage = messages[messages.length - 1];
if (Array.isArray(lastMessage.content)) {
for (const content of lastMessage.content) {
// 处理字符串类型的内容(OpenAI格式)
if (typeof content === "string") {
queryString += content + "\n";
}
// 处理对象类型的内容
else if (content.type === "text") {
queryString += content.text + "\n";
}
// 注意:这里不再重复处理image_url,因为已经在上面处理过了
}
queryString = queryString.trim(); // 去除末尾的换行符
} else {
queryString = lastMessage.content;
}
// 日志记录
log("info", "处理 Completion 类型消息", {
requestId,
contentLength: queryString.length,
queryString,
files,
});
const stream = data.stream !== undefined ? data.stream : false;
let requestBody;
// 如果指定了 INPUT_VARIABLE,则使用它作为键,否则默认使用 'query'
const inputKey = config.INPUT_VARIABLE || "query";
// 构建请求体
requestBody = {
inputs: { [inputKey]: queryString },
response_mode: "streaming",
user: userId,
files: files,
};
// 记录将要发送到 Dify 的请求载荷
log("info", "发送请求到 Dify", {
requestId,
url: config.DIFY_API_URL + apiPath,
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${config.API_KEY}`,
},
body: requestBody,
});
// 发送请求到 Dify
const resp = await fetch(config.DIFY_API_URL + apiPath, {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${config.API_KEY}`,
},
body: JSON.stringify(requestBody),
});
// 记录 API 调用时间
const apiCallDuration = Date.now() - startTime;
logApiCall(requestId, config, apiPath, apiCallDuration);
// 记录 Dify 的响应状态
log("info", "收到 Dify 响应", {
requestId,
status: resp.status,
statusText: resp.statusText,
});
if (!resp.ok) {
const errorBody = await resp.text();
log("error", "Dify API 请求失败", {
requestId,
status: resp.status,
statusText: resp.statusText,
errorBody: errorBody,
});
res.status(resp.status).send(errorBody);
return;
}
let isResponseEnded = false;
if (stream) {
res.setHeader("Content-Type", "text/event-stream");
let buffer = "";
const responseStream = resp.body
.pipe(new PassThrough())
.on("data", (chunk) => {
buffer += chunk.toString();
let lines = buffer.split("\n");
for (let i = 0; i < lines.length - 1; i++) {
let line = lines[i].trim();
if (!line.startsWith("data:")) continue;
line = line.slice(5).trim();
let chunkObj;
try {
if (line.startsWith("{")) {
chunkObj = JSON.parse(line);
} else {
continue;
}
} catch (error) {
console.error("解析 chunk 出错:", error);
continue;
}
// 记录每个 chunk 的内容
log("debug", "处理 chunk", {
requestId,
chunkObj,
});
if (
chunkObj.event === "message" ||
chunkObj.event === "agent_message" ||
chunkObj.event === "text_chunk"
) {
let chunkContent;
if (chunkObj.event === "text_chunk") {
chunkContent = chunkObj.data.text;
} else {
chunkContent = chunkObj.answer;
}
if (chunkContent !== "") {
const chunkId = `chatcmpl-${Date.now()}`;
const chunkCreated = chunkObj.created_at;
if (!isResponseEnded) {
res.write(
"data: " +
JSON.stringify({
id: chunkId,
object: "chat.completion.chunk",
created: chunkCreated,
model: data.model,
choices: [
{
index: 0,
delta: {
content: chunkContent,
},
finish_reason: null,
},
],
}) +
"\n\n"
);
}
}
} else if (
chunkObj.event === "workflow_finished" ||
chunkObj.event === "message_end"
) {
const chunkId = `chatcmpl-${Date.now()}`;
const chunkCreated = chunkObj.created_at;
if (!isResponseEnded) {
res.write(
"data: " +
JSON.stringify({
id: chunkId,
object: "chat.completion.chunk",
created: chunkCreated,
model: data.model,
choices: [
{
index: 0,
delta: {},
finish_reason: "stop",
},
],
}) +
"\n\n"
);
}
if (!isResponseEnded) {
res.write("data: [DONE]\n\n");
}
res.end();
isResponseEnded = true;
} else if (chunkObj.event === "agent_thought") {
// 如果需要,处理 agent_thought 事件
} else if (chunkObj.event === "ping") {
// 如果需要,处理 ping 事件
} else if (chunkObj.event === "error") {
console.error(`Error: ${chunkObj.code}, ${chunkObj.message}`);
res
.status(500)
.write(
`data: ${JSON.stringify({ error: chunkObj.message })}\n\n`
);
if (!isResponseEnded) {
res.write("data: [DONE]\n\n");
}
res.end();
isResponseEnded = true;
}
}
buffer = lines[lines.length - 1];
});
// 记录响应结束
responseStream.on("end", () => {
log("info", "响应结束", { requestId });
});
} else {
let result = "";
let usageData = "";
let buffer = "";
let hasError = false;
// 记录普通响应开始
log("info", "开始处理普通响应", {
requestId,
timestamp: new Date().toISOString(),
});
const responseStream = resp.body;
responseStream.on("data", (chunk) => {
buffer += chunk.toString();
let lines = buffer.split("\n");
for (let i = 0; i < lines.length - 1; i++) {
const line = lines[i].trim();
if (line === "") continue;
let chunkObj;
try {
const cleanedLine = line.replace(/^data: /, "").trim();
if (cleanedLine.startsWith("{") && cleanedLine.endsWith("}")) {
chunkObj = JSON.parse(cleanedLine);
} else {
continue;
}
} catch (error) {
console.error("解析 JSON 出错:", error);
continue;
}
// 记录每个 chunk 的内容
log("debug", "处理 chunk", {
requestId,
chunkObj,
});
if (
chunkObj.event === "message" ||
chunkObj.event === "agent_message"
) {
result += chunkObj.answer;
} else if (chunkObj.event === "message_end") {
usageData = {
prompt_tokens: chunkObj.metadata.usage.prompt_tokens || 100,
completion_tokens:
chunkObj.metadata.usage.completion_tokens || 10,
total_tokens: chunkObj.metadata.usage.total_tokens || 110,
};
} else if (chunkObj.event === "workflow_finished") {
const outputs = chunkObj.data.outputs;
if (config.OUTPUT_VARIABLE) {
result = outputs[config.OUTPUT_VARIABLE];
} else {
result = outputs;
}
result = String(result);
usageData = {
prompt_tokens: chunkObj.metadata?.usage?.prompt_tokens || 100,
completion_tokens:
chunkObj.metadata?.usage?.completion_tokens || 10,
total_tokens: chunkObj.data.total_tokens || 110,
};
} else if (chunkObj.event === "agent_thought") {
// 如果需要,处理 agent_thought 事件
} else if (chunkObj.event === "ping") {
// 如果需要,处理 ping 事件
} else if (chunkObj.event === "error") {
hasError = true;
console.error(`Error: ${chunkObj.code}, ${chunkObj.message}`);
break;
}
}
buffer = lines[lines.length - 1];
});
responseStream.on("end", () => {
if (hasError) {
res
.status(500)
.json({ error: "An error occurred while processing the request." });
} else {
const formattedResponse = {
id: `chatcmpl-${generateId()}`,
object: "chat.completion",
created: Math.floor(Date.now() / 1000),
model: data.model,
choices: [
{
index: 0,
message: {
role: "assistant",
content: result.trim(),
},
logprobs: null,
finish_reason: "stop",
},
],
usage: usageData,
system_fingerprint: "fp_2f57f81c11",
};
const jsonResponse = JSON.stringify(formattedResponse, null, 2);
// 记录发送的响应
log("info", "发送响应", {
requestId,
response: formattedResponse,
});
res.set("Content-Type", "application/json");
res.send(jsonResponse);
}
});
}
} catch (error) {
console.error("处理 Completion 请求时发生错误:", error);
// 记录错误
log("error", "处理 Completion 请求时发生错误", {
requestId,
error: error.message,
stack: error.stack,
});
res.status(500).json({ error: error.message });
}
}
export default {
handleRequest,
};
|