Spaces:
Running
Running
Update index.js
Browse files
index.js
CHANGED
@@ -26,7 +26,7 @@ const CONFIG = {
|
|
26 |
BODY_LIMIT: '5mb'
|
27 |
},
|
28 |
RETRY: {
|
29 |
-
MAX_ATTEMPTS:
|
30 |
DELAY_BASE: 1000 // 基础延迟时间(毫秒)
|
31 |
},
|
32 |
ISSHOW_SEARCH_RESULTS: process.env.ISSHOW_SEARCH_RESULTS === 'true',//是否显示搜索结果,默认关闭
|
@@ -508,10 +508,14 @@ async function handleStreamResponse(response, model, res) {
|
|
508 |
const trimmedLine = line.trim();
|
509 |
if (trimmedLine.startsWith('data: ')) {
|
510 |
const data = trimmedLine.substring(6);
|
|
|
|
|
|
|
|
|
|
|
511 |
try {
|
512 |
-
|
513 |
-
|
514 |
-
const linejosn = JSON.parse(content);
|
515 |
if (linejosn?.error?.name === "RateLimitError") {
|
516 |
var responseData = MessageProcessor.createChatResponse(`${linejosn.error.name},请重新对话`, model, true);
|
517 |
fs.unlinkSync(path.resolve(process.cwd(), 'signature.json'));
|
@@ -526,7 +530,7 @@ async function handleStreamResponse(response, model, res) {
|
|
526 |
if (linejosn.response === "modelResponse" && linejosn?.modelResponse?.generatedImageUrls) {
|
527 |
const dataImage = await handleImageResponse(linejosn.modelResponse.generatedImageUrls);
|
528 |
const responseData = MessageProcessor.createChatResponse(dataImage, model, true);
|
529 |
-
res.write(`data: ${JSON.stringify(responseData)}\n\n`);
|
530 |
}
|
531 |
break;
|
532 |
case 'grok-latest':
|
@@ -559,10 +563,6 @@ async function handleStreamResponse(response, model, res) {
|
|
559 |
}
|
560 |
}
|
561 |
});
|
562 |
-
stream.on('end', () => {
|
563 |
-
res.write('data: [DONE]\n\n');
|
564 |
-
res.end();
|
565 |
-
});
|
566 |
stream.on('error', (error) => {
|
567 |
console.error('流处理错误:', error);
|
568 |
res.write('data: [DONE]\n\n');
|
@@ -638,7 +638,7 @@ async function handleImageResponse(imageUrl) {
|
|
638 |
const MAX_RETRIES = 3;
|
639 |
let retryCount = 0;
|
640 |
let imageBase64Response;
|
641 |
-
|
642 |
while (retryCount < MAX_RETRIES) {
|
643 |
try {
|
644 |
//发送图片请求获取图片
|
@@ -691,7 +691,6 @@ async function handleImageResponse(imageUrl) {
|
|
691 |
body: formData
|
692 |
});
|
693 |
if (!responseURL.ok) {
|
694 |
-
console.error("上传图床失败");
|
695 |
return "生图失败,请查看图床密钥是否设置正确"
|
696 |
} else {
|
697 |
const result = await responseURL.json();
|
|
|
26 |
BODY_LIMIT: '5mb'
|
27 |
},
|
28 |
RETRY: {
|
29 |
+
MAX_ATTEMPTS: 1,//重试次数
|
30 |
DELAY_BASE: 1000 // 基础延迟时间(毫秒)
|
31 |
},
|
32 |
ISSHOW_SEARCH_RESULTS: process.env.ISSHOW_SEARCH_RESULTS === 'true',//是否显示搜索结果,默认关闭
|
|
|
508 |
const trimmedLine = line.trim();
|
509 |
if (trimmedLine.startsWith('data: ')) {
|
510 |
const data = trimmedLine.substring(6);
|
511 |
+
if(data === "[DONE]"){
|
512 |
+
console.log("流结束");
|
513 |
+
res.write('data: [DONE]\n\n');
|
514 |
+
return res.end();
|
515 |
+
};
|
516 |
try {
|
517 |
+
if(!data.trim())continue;
|
518 |
+
const linejosn = JSON.parse(data);
|
|
|
519 |
if (linejosn?.error?.name === "RateLimitError") {
|
520 |
var responseData = MessageProcessor.createChatResponse(`${linejosn.error.name},请重新对话`, model, true);
|
521 |
fs.unlinkSync(path.resolve(process.cwd(), 'signature.json'));
|
|
|
530 |
if (linejosn.response === "modelResponse" && linejosn?.modelResponse?.generatedImageUrls) {
|
531 |
const dataImage = await handleImageResponse(linejosn.modelResponse.generatedImageUrls);
|
532 |
const responseData = MessageProcessor.createChatResponse(dataImage, model, true);
|
533 |
+
res.write(`data: ${JSON.stringify(responseData)}\n\n`);
|
534 |
}
|
535 |
break;
|
536 |
case 'grok-latest':
|
|
|
563 |
}
|
564 |
}
|
565 |
});
|
|
|
|
|
|
|
|
|
566 |
stream.on('error', (error) => {
|
567 |
console.error('流处理错误:', error);
|
568 |
res.write('data: [DONE]\n\n');
|
|
|
638 |
const MAX_RETRIES = 3;
|
639 |
let retryCount = 0;
|
640 |
let imageBase64Response;
|
641 |
+
|
642 |
while (retryCount < MAX_RETRIES) {
|
643 |
try {
|
644 |
//发送图片请求获取图片
|
|
|
691 |
body: formData
|
692 |
});
|
693 |
if (!responseURL.ok) {
|
|
|
694 |
return "生图失败,请查看图床密钥是否设置正确"
|
695 |
} else {
|
696 |
const result = await responseURL.json();
|