Spaces:
Running
Running
Update index.js
Browse files
index.js
CHANGED
@@ -504,15 +504,10 @@ async function handleStreamResponse(response, model, res) {
|
|
504 |
buffer = lines.pop() || '';
|
505 |
|
506 |
for (const line of lines) {
|
507 |
-
if (!line.
|
508 |
const trimmedLine = line.trim();
|
509 |
if (trimmedLine.startsWith('data: ')) {
|
510 |
const content = trimmedLine.substring(6);
|
511 |
-
if (content === '[DONE]') {
|
512 |
-
res.write('data: [DONE]\n\n');
|
513 |
-
continue;
|
514 |
-
}
|
515 |
-
|
516 |
try {
|
517 |
const linejosn = JSON.parse(content);
|
518 |
if (linejosn?.error?.name === "RateLimitError") {
|
@@ -521,6 +516,7 @@ async function handleStreamResponse(response, model, res) {
|
|
521 |
CONFIG.API.SIGNATURE_COOKIE = null;
|
522 |
console.log("认证信息已删除");
|
523 |
res.write(`data: ${JSON.stringify(responseData)}\n\n`);
|
|
|
524 |
return res.end();
|
525 |
}
|
526 |
switch (model) {
|
@@ -561,6 +557,10 @@ async function handleStreamResponse(response, model, res) {
|
|
561 |
}
|
562 |
}
|
563 |
});
|
|
|
|
|
|
|
|
|
564 |
stream.on('error', (error) => {
|
565 |
console.error('流处理错误:', error);
|
566 |
res.write('data: [DONE]\n\n');
|
@@ -583,15 +583,13 @@ async function handleNormalResponse(response, model, res) {
|
|
583 |
const lines = responseText.split('\n');
|
584 |
|
585 |
for (const line of lines) {
|
586 |
-
if (!line.
|
587 |
-
|
588 |
const data = line.slice(6);
|
589 |
if (data === '[DONE]') continue;
|
590 |
let linejosn = JSON.parse(data);
|
591 |
if (linejosn?.error?.name === "RateLimitError") {
|
592 |
fullResponse = `${linejosn.error.name},请重新对话`;
|
593 |
CONFIG.API.SIGNATURE_COOKIE = null;
|
594 |
-
console.log("认证信息已删除");
|
595 |
return res.json(MessageProcessor.createChatResponse(fullResponse, model));
|
596 |
}
|
597 |
switch (model) {
|
@@ -701,7 +699,7 @@ async function handleImageResponse(imageUrl) {
|
|
701 |
|
702 |
// 404处理
|
703 |
app.use((req, res) => {
|
704 |
-
res.status(
|
705 |
});
|
706 |
|
707 |
// 启动服务器
|
|
|
504 |
buffer = lines.pop() || '';
|
505 |
|
506 |
for (const line of lines) {
|
507 |
+
if (!line.trim()) continue;
|
508 |
const trimmedLine = line.trim();
|
509 |
if (trimmedLine.startsWith('data: ')) {
|
510 |
const content = trimmedLine.substring(6);
|
|
|
|
|
|
|
|
|
|
|
511 |
try {
|
512 |
const linejosn = JSON.parse(content);
|
513 |
if (linejosn?.error?.name === "RateLimitError") {
|
|
|
516 |
CONFIG.API.SIGNATURE_COOKIE = null;
|
517 |
console.log("认证信息已删除");
|
518 |
res.write(`data: ${JSON.stringify(responseData)}\n\n`);
|
519 |
+
res.write('data: [DONE]\n\n');
|
520 |
return res.end();
|
521 |
}
|
522 |
switch (model) {
|
|
|
557 |
}
|
558 |
}
|
559 |
});
|
560 |
+
stream.on('end', () => {
|
561 |
+
res.write('data: [DONE]\n\n');
|
562 |
+
res.end();
|
563 |
+
});
|
564 |
stream.on('error', (error) => {
|
565 |
console.error('流处理错误:', error);
|
566 |
res.write('data: [DONE]\n\n');
|
|
|
583 |
const lines = responseText.split('\n');
|
584 |
|
585 |
for (const line of lines) {
|
586 |
+
if (!line.trim()) continue;
|
|
|
587 |
const data = line.slice(6);
|
588 |
if (data === '[DONE]') continue;
|
589 |
let linejosn = JSON.parse(data);
|
590 |
if (linejosn?.error?.name === "RateLimitError") {
|
591 |
fullResponse = `${linejosn.error.name},请重新对话`;
|
592 |
CONFIG.API.SIGNATURE_COOKIE = null;
|
|
|
593 |
return res.json(MessageProcessor.createChatResponse(fullResponse, model));
|
594 |
}
|
595 |
switch (model) {
|
|
|
699 |
|
700 |
// 404处理
|
701 |
app.use((req, res) => {
|
702 |
+
res.status(200).send('api运行正常');
|
703 |
});
|
704 |
|
705 |
// 启动服务器
|