Spaces:
Sleeping
Sleeping
Update index.js
Browse files
index.js
CHANGED
|
@@ -129,7 +129,7 @@ class Utils {
|
|
| 129 |
}
|
| 130 |
static async handleError(error, res, originalRequest = null) {
|
| 131 |
// 如果是500错误且提供了原始请求函数,尝试重新获取签名并重试
|
| 132 |
-
if (error.status
|
| 133 |
try {
|
| 134 |
const result = await Utils.retryRequestWithNewSignature(originalRequest);
|
| 135 |
return result;
|
|
@@ -490,63 +490,82 @@ async function handleStreamResponse(response, model, res) {
|
|
| 490 |
res.setHeader('Connection', 'keep-alive');
|
| 491 |
|
| 492 |
try {
|
| 493 |
-
|
| 494 |
-
|
| 495 |
-
|
| 496 |
-
|
| 497 |
-
|
| 498 |
-
|
| 499 |
-
|
| 500 |
-
|
| 501 |
-
|
| 502 |
-
|
| 503 |
-
|
| 504 |
-
|
| 505 |
-
|
| 506 |
-
|
| 507 |
-
|
| 508 |
-
|
| 509 |
-
console.log("认证信息已删除");
|
| 510 |
-
res.write(`data: ${JSON.stringify(responseData)}\n\n`);
|
| 511 |
-
return res.end();
|
| 512 |
-
}
|
| 513 |
-
switch (model) {
|
| 514 |
-
case 'grok-latest-image':
|
| 515 |
-
if (linejosn.response === "modelResponse" && linejosn?.modelResponse?.generatedImageUrls) {
|
| 516 |
-
const dataImage = await handleImageResponse(linejosn.modelResponse.generatedImageUrls);
|
| 517 |
-
const responseData = MessageProcessor.createChatResponse(dataImage, model, true);
|
| 518 |
-
res.write(`data: ${JSON.stringify(responseData)}\n\n`);
|
| 519 |
}
|
| 520 |
-
|
| 521 |
-
|
| 522 |
-
|
| 523 |
-
|
| 524 |
-
|
| 525 |
-
|
|
|
|
|
|
|
| 526 |
res.write(`data: ${JSON.stringify(responseData)}\n\n`);
|
|
|
|
| 527 |
}
|
| 528 |
-
|
| 529 |
-
|
| 530 |
-
|
| 531 |
-
|
| 532 |
-
|
| 533 |
-
|
| 534 |
-
|
| 535 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 536 |
}
|
|
|
|
|
|
|
| 537 |
}
|
| 538 |
-
|
| 539 |
-
const searchResults = await Utils.organizeSearchResults(linejosn.webSearchResults);
|
| 540 |
-
const responseData = MessageProcessor.createChatResponse(`<thinking>\r\n${searchResults}\r\n</thinking>\r\n`, model, true);
|
| 541 |
-
res.write(`data: ${JSON.stringify(responseData)}\n\n`);
|
| 542 |
-
}
|
| 543 |
-
break;
|
| 544 |
}
|
| 545 |
-
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 546 |
|
| 547 |
-
res.end();
|
| 548 |
} catch (error) {
|
| 549 |
-
|
|
|
|
|
|
|
| 550 |
}
|
| 551 |
}
|
| 552 |
|
|
|
|
| 129 |
}
|
| 130 |
static async handleError(error, res, originalRequest = null) {
|
| 131 |
// 如果是500错误且提供了原始请求函数,尝试重新获取签名并重试
|
| 132 |
+
if (String(error).includes("status: 500") && originalRequest) {
|
| 133 |
try {
|
| 134 |
const result = await Utils.retryRequestWithNewSignature(originalRequest);
|
| 135 |
return result;
|
|
|
|
| 490 |
res.setHeader('Connection', 'keep-alive');
|
| 491 |
|
| 492 |
try {
|
| 493 |
+
const stream = response.body;
|
| 494 |
+
let buffer = '';
|
| 495 |
+
|
| 496 |
+
stream.on('data', async (chunk) => {
|
| 497 |
+
buffer += chunk.toString();
|
| 498 |
+
const lines = buffer.split('\n');
|
| 499 |
+
buffer = lines.pop() || '';
|
| 500 |
+
|
| 501 |
+
for (const line of lines) {
|
| 502 |
+
if (!line.startsWith('data: ')) continue;
|
| 503 |
+
const trimmedLine = line.trim();
|
| 504 |
+
if (trimmedLine.startsWith('data: ')) {
|
| 505 |
+
const content = trimmedLine.substring(6);
|
| 506 |
+
if (content === '[DONE]') {
|
| 507 |
+
res.write('data: [DONE]\n\n');
|
| 508 |
+
continue;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 509 |
}
|
| 510 |
+
|
| 511 |
+
try {
|
| 512 |
+
const linejosn = JSON.parse(content);
|
| 513 |
+
if (linejosn?.error?.name === "RateLimitError") {
|
| 514 |
+
var responseData = MessageProcessor.createChatResponse(`${linejosn.error.name},请重新对话`, model, true);
|
| 515 |
+
fs.unlinkSync(path.resolve(process.cwd(), 'signature.json'));
|
| 516 |
+
CONFIG.API.SIGNATURE_COOKIE = null;
|
| 517 |
+
console.log("认证信息已删除");
|
| 518 |
res.write(`data: ${JSON.stringify(responseData)}\n\n`);
|
| 519 |
+
return res.end();
|
| 520 |
}
|
| 521 |
+
switch (model) {
|
| 522 |
+
case 'grok-latest-image':
|
| 523 |
+
if (linejosn.response === "modelResponse" && linejosn?.modelResponse?.generatedImageUrls) {
|
| 524 |
+
const dataImage = await handleImageResponse(linejosn.modelResponse.generatedImageUrls);
|
| 525 |
+
const responseData = MessageProcessor.createChatResponse(dataImage, model, true);
|
| 526 |
+
res.write(`data: ${JSON.stringify(responseData)}\n\n`);
|
| 527 |
+
}
|
| 528 |
+
break;
|
| 529 |
+
case 'grok-latest':
|
| 530 |
+
if (linejosn.response === "token") {
|
| 531 |
+
const token = linejosn.token;
|
| 532 |
+
if (token && token.length > 0) {
|
| 533 |
+
const responseData = MessageProcessor.createChatResponse(token, model, true);
|
| 534 |
+
res.write(`data: ${JSON.stringify(responseData)}\n\n`);
|
| 535 |
+
}
|
| 536 |
+
}
|
| 537 |
+
break;
|
| 538 |
+
case 'grok-latest-search':
|
| 539 |
+
if (linejosn.response === "token") {
|
| 540 |
+
const token = linejosn.token;
|
| 541 |
+
if (token && token.length > 0) {
|
| 542 |
+
const responseData = MessageProcessor.createChatResponse(token, model, true);
|
| 543 |
+
res.write(`data: ${JSON.stringify(responseData)}\n\n`);
|
| 544 |
+
}
|
| 545 |
+
}
|
| 546 |
+
if (linejosn.response === "webSearchResults" && CONFIG.ISSHOW_SEARCH_RESULTS) {
|
| 547 |
+
const searchResults = await Utils.organizeSearchResults(linejosn.webSearchResults);
|
| 548 |
+
const responseData = MessageProcessor.createChatResponse(`<thinking>\r\n${searchResults}\r\n</thinking>\r\n`, model, true);
|
| 549 |
+
res.write(`data: ${JSON.stringify(responseData)}\n\n`);
|
| 550 |
+
}
|
| 551 |
+
break;
|
| 552 |
}
|
| 553 |
+
} catch (error) {
|
| 554 |
+
console.error('JSON解析错误:', error);
|
| 555 |
}
|
| 556 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 557 |
}
|
| 558 |
+
});
|
| 559 |
+
stream.on('error', (error) => {
|
| 560 |
+
console.error('流处理错误:', error);
|
| 561 |
+
res.write('data: [DONE]\n\n');
|
| 562 |
+
res.end();
|
| 563 |
+
});
|
| 564 |
|
|
|
|
| 565 |
} catch (error) {
|
| 566 |
+
console.error('处理响应错误:', error);
|
| 567 |
+
res.write('data: [DONE]\n\n');
|
| 568 |
+
res.end();
|
| 569 |
}
|
| 570 |
}
|
| 571 |
|