Update index.js
Browse files607 unmodified lines60860961061161261361461561623 unmodified lines64064164264364464515 unmodified lines6616626636646656666676686696706711 unmodified line67367467567667767867920 unmodified lines70070170270370470556 unmodified lines76276376476576676776876977077177277312 unmodified lines7867877887897907917926 unmodified lines79980080180280380480574 unmodified lines88088188288388488588688712 unmodified lines9009019029039049059064 unmodified lines911912913914915916917607 unmodified linesif (!data.trim()) continue;if (data === "[DONE]") continue;const linejosn = JSON.parse(data);Logger.error(JSON.stringify(linejosn, null, 2), 'Server');if (linejosn?.error) {Logger.error(JSON.stringify(linejosn, null, 2), 'Server');stream.destroy();reject(new Error("RateLimitError"));return;23 unmodified lines})();dataPromises.push(processPromise);} catch (error) {continue;}}15 unmodified linesCONFIG.IS_IMG_GEN2 = false;resolve();} catch (error) {reject(error);}});stream.on('error', (error) => {reject(error);});});1 unmodified lineLogger.error(error, 'Server');CONFIG.IS_IMG_GEN = false;CONFIG.IS_IMG_GEN2 = false;throw error;}}20 unmodified linesawait new Promise(resolve => setTimeout(resolve, CONFIG.API.RETRY_TIME * retryCount));} catch (error) {retryCount++;if (retryCount === MAX_RETRIES) {throw error;56 unmodified lines} else {Logger.info("生图成功", 'Server');const result = await responseURL.json();try {var url = result.data.links.urlreturn ``} catch (error) {return error}}}}12 unmodified linesallowedHeaders: ['Content-Type', 'Authorization']}));app.get('/hf/v1/models', (req, res) => {res.json({object: "list",data: Object.keys(CONFIG.MODELS).map((model, index) => ({6 unmodified lines});app.post('/hf/v1/chat/completions', async (req, res) => {try {const authToken = req.headers.authorization?.replace('Bearer ', '');if (CONFIG.API.IS_CUSTOM_SSO) {74 unmodified linesLogger.info(`当前剩余可用令牌数: ${tokenManager.getTokenCount()}`, 'Server');try {await handleResponse(response, req.body.model, res, req.body.stream);return;} catch (error) {if (isTempCookie) {await Utils.get_signature();} else {12 unmodified linesif (response.status === 429) {if (isTempCookie) {await Utils.get_signature();} else {tokenManager.setModelLimit(CONFIG.SSO_INDEX, req.body.model);for (let i = 1; i <= tokenManager.getTokenCount(); i++) {CONFIG.SSO_INDEX = (CONFIG.SSO_INDEX + 1) % tokenManager.getTokenCount();4 unmodified lines}}}} else {// 非429错误直接抛出if (isTempCookie) {await Utils.get_signature();607 unmodified lines60860961061161261361461561661761823 unmodified lines64264364464564664764815 unmodified lines6646656666676686696706716726736746756761 unmodified line67867968068168268368420 unmodified lines70570670770870971071156 unmodified lines76876977077177277377412 unmodified lines7877887897907917927936 unmodified lines80080180280380480580674 unmodified lines88188288388488588688788888989012 unmodified lines9039049059069079089094 unmodified lines914915916917918919920607 unmodified linesif (!data.trim()) continue;if (data === "[DONE]") continue;const linejosn = JSON.parse(data);if (linejosn?.error) {Logger.error(JSON.stringify(linejosn, null, 2), 'Server');if(linejosn.error?.name === "RateLimitError"){CONFIG.API.TEMP_COOKIE = null;}stream.destroy();reject(new Error("RateLimitError"));return;23 unmodified lines})();dataPromises.push(processPromise);} catch (error) {Logger.error(error, 'Server');continue;}}15 unmodified linesCONFIG.IS_IMG_GEN2 = false;resolve();} catch (error) {Logger.error(error, 'Server');reject(error);}});stream.on('error', (error) => {Logger.error(error, 'Server');reject(error);});});1 unmodified lineLogger.error(error, 'Server');CONFIG.IS_IMG_GEN = false;CONFIG.IS_IMG_GEN2 = false;throw new Error(error);}}20 unmodified linesawait new Promise(resolve => setTimeout(resolve, CONFIG.API.RETRY_TIME * retryCount));} catch (error) {Logger.error(error, 'Server');retryCount++;if (retryCount === MAX_RETRIES) {throw error;56 unmodified lines} else {Logger.info("生图成功", 'Server');const result = await responseURL.json();return ``}}}12 unmodified linesallowedHeaders: ['Content-Type', 'Authorization']}));app.get('/v1/models', (req, res) => {res.json({object: "list",data: Object.keys(CONFIG.MODELS).map((model, index) => ({6 unmodified lines});app.post('/v1/chat/completions', async (req, res) => {try {const authToken = req.headers.authorization?.replace('Bearer ', '');if (CONFIG.API.IS_CUSTOM_SSO) {74 unmodified linesLogger.info(`当前剩余可用令牌数: ${tokenManager.getTokenCount()}`, 'Server');try {await handleResponse(response, req.body.model, res, req.body.stream);Logger.info(`请求结束`, 'Server');return;} catch (error) {Logger.error(error, 'Server');if (isTempCookie) {await Utils.get_signature();} else {12 unmodified linesif (response.status === 429) {if (isTempCookie) {await Utils.get_signature();} else {tokenManager.setModelLimit(CONFIG.SSO_INDEX, req.body.model);for (let i = 1; i <= tokenManager.getTokenCount(); i++) {CONFIG.SSO_INDEX = (CONFIG.SSO_INDEX + 1) % tokenManager.getTokenCount();4 unmodified lines}}}} else {// 非429错误直接抛出if (isTempCookie) {await Utils.get_signature();
index.js+16-13