Update index.js
Browse files128 unmodified lines129130131132133134135354 unmodified lines490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552128 unmodified lines}static async handleError(error, res, originalRequest = null) {// 如果是500错误且提供了原始请求函数,尝试重新获取签名并重试if (error.status === 500 && originalRequest) {try {const result = await Utils.retryRequestWithNewSignature(originalRequest);return result;354 unmodified linesres.setHeader('Connection', 'keep-alive');try {// 获取响应文本const responseText = await response.text();const lines = responseText.split('\n');for (const line of lines) {if (!line.startsWith('data: ')) continue;const data = line.slice(6);if (data === '[DONE]') {res.write('data: [DONE]\n\n');continue;}let linejosn = JSON.parse(data);if (linejosn?.error?.name === "RateLimitError") {var responseData = MessageProcessor.createChatResponse(`${linejosn.error.name},请重新对话`, model, true);CONFIG.API.SIGNATURE_COOKIE = null;console.log("认证信息已删除");res.write(`data: ${JSON.stringify(responseData)}\n\n`);return res.end();}switch (model) {case 'grok-latest-image':if (linejosn.response === "modelResponse" && linejosn?.modelResponse?.generatedImageUrls) {const dataImage = await handleImageResponse(linejosn.modelResponse.generatedImageUrls);const responseData = MessageProcessor.createChatResponse(dataImage, model, true);res.write(`data: ${JSON.stringify(responseData)}\n\n`);}break;case 'grok-latest':if (linejosn.response === "token") {const token = linejosn.token;if (token && token.length > 0) {const responseData = MessageProcessor.createChatResponse(token, model, true);res.write(`data: ${JSON.stringify(responseData)}\n\n`);}}break;case 'grok-latest-search':if (linejosn.response === "token") {const token = linejosn.token;if (token && token.length > 0) {const responseData = MessageProcessor.createChatResponse(token, model, true);res.write(`data: ${JSON.stringify(responseData)}\n\n`);}}if (linejosn.response === "webSearchResults" && CONFIG.ISSHOW_SEARCH_RESULTS) {const searchResults = await Utils.organizeSearchResults(linejosn.webSearchResults);const responseData = MessageProcessor.createChatResponse(`<thinking>\r\n${searchResults}\r\n</thinking>\r\n`, model, true);res.write(`data: ${JSON.stringify(responseData)}\n\n`);}break;}}res.end();} catch (error) {Utils.handleError(error, res);}}128 unmodified lines129130131132133134135354 unmodified lines490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571128 unmodified lines}static async handleError(error, res, originalRequest = null) {// 如果是500错误且提供了原始请求函数,尝试重新获取签名并重试if (String(error).includes("status: 500") && originalRequest) {try {const result = await Utils.retryRequestWithNewSignature(originalRequest);return result;354 unmodified linesres.setHeader('Connection', 'keep-alive');try {const stream = response.body;let buffer = '';stream.on('data', async (chunk) => {buffer += chunk.toString();const lines = buffer.split('\n');buffer = lines.pop() || '';for (const line of lines) {if (!line.startsWith('data: ')) continue;const trimmedLine = line.trim();if (trimmedLine.startsWith('data: ')) {const content = trimmedLine.substring(6);if (content === '[DONE]') {res.write('data: [DONE]\n\n');continue;}try {const linejosn = JSON.parse(content);if (linejosn?.error?.name === "RateLimitError") {var responseData = MessageProcessor.createChatResponse(`${linejosn.error.name},请重新对话`, model, true);fs.unlinkSync(path.resolve(process.cwd(), 'signature.json'));CONFIG.API.SIGNATURE_COOKIE = null;console.log("认证信息已删除");res.write(`data: ${JSON.stringify(responseData)}\n\n`);return res.end();}switch (model) {case 'grok-latest-image':if (linejosn.response === "modelResponse" && linejosn?.modelResponse?.generatedImageUrls) {const dataImage = await handleImageResponse(linejosn.modelResponse.generatedImageUrls);const responseData = MessageProcessor.createChatResponse(dataImage, model, true);res.write(`data: ${JSON.stringify(responseData)}\n\n`);}break;case 'grok-latest':if (linejosn.response === "token") {const token = linejosn.token;if (token && token.length > 0) {const responseData = MessageProcessor.createChatResponse(token, model, true);res.write(`data: ${JSON.stringify(responseData)}\n\n`);}}break;case 'grok-latest-search':if (linejosn.response === "token") {const token = linejosn.token;if (token && token.length > 0) {const responseData = MessageProcessor.createChatResponse(token, model, true);res.write(`data: ${JSON.stringify(responseData)}\n\n`);}}if (linejosn.response === "webSearchResults" && CONFIG.ISSHOW_SEARCH_RESULTS) {const searchResults = await Utils.organizeSearchResults(linejosn.webSearchResults);const responseData = MessageProcessor.createChatResponse(`<thinking>\r\n${searchResults}\r\n</thinking>\r\n`, model, true);res.write(`data: ${JSON.stringify(responseData)}\n\n`);}break;}} catch (error) {console.error('JSON解析错误:', error);}}}});stream.on('error', (error) => {console.error('流处理错误:', error);res.write('data: [DONE]\n\n');res.end();});} catch (error) {console.error('处理响应错误:', error);res.write('data: [DONE]\n\n');res.end();}}
index.js+69-50