yxmiler commited on
Commit
259996c
·
verified ·
1 Parent(s): 54bf756

Update index.js

Browse files
Files changed (1) hide show
  1. index.js +171 -136
index.js CHANGED
@@ -23,6 +23,7 @@ const CONFIG = {
23
  "grok-3-reasoning": "grok-3"
24
  },
25
  API: {
 
26
  IS_TEMP_GROK2: process.env.IS_TEMP_GROK2 == undefined ? true : process.env.IS_TEMP_GROK2 == 'true',
27
  GROK2_CONCURRENCY_LEVEL: process.env.GROK2_CONCURRENCY_LEVEL || 4,
28
  IS_CUSTOM_SSO: process.env.IS_CUSTOM_SSO == undefined ? false : process.env.IS_CUSTOM_SSO == 'true',
@@ -44,7 +45,6 @@ const CONFIG = {
44
  IS_THINKING: false,
45
  IS_IMG_GEN: false,
46
  IS_IMG_GEN2: false,
47
- SSO_INDEX: 0,//sso的索引
48
  TEMP_COOKIE_INDEX: 0,//临时cookie的下标
49
  ISSHOW_SEARCH_RESULTS: process.env.ISSHOW_SEARCH_RESULTS == undefined ? true : process.env.ISSHOW_SEARCH_RESULTS == 'true',//是否显示搜索结果
50
  CHROME_PATH: process.env.CHROME_PATH || null
@@ -90,8 +90,8 @@ async function initialization() {
90
  ssoArray.forEach((sso) => {
91
  tokenManager.addToken(`sso-rw=${sso};sso=${sso}`);
92
  });
93
- console.log(JSON.stringify(tokenManager.getActiveTokens(), null, 2));
94
- Logger.info(`令牌加载完成,共加载: ${tokenManager.getTokenCount()}个令牌`, 'Server');
95
  if (CONFIG.API.IS_TEMP_GROK2) {
96
  await tempCookieManager.ensureCookies();
97
  CONFIG.API.TEMP_COOKIE = tempCookieManager.cookies[tempCookieManager.currentIndex];
@@ -99,144 +99,186 @@ async function initialization() {
99
  Logger.info("初始化完成", 'Server');
100
  }
101
 
102
-
103
  class AuthTokenManager {
104
  constructor() {
105
- this.activeTokens = [];
106
- this.expiredTokens = new Map();
107
- this.tokenModelFrequency = new Map();
108
- if (CONFIG.API.IS_TEMP_GROK2) {
109
- this.modelRateLimit = {
110
- "grok-3": { RequestFrequency: 20 },
111
- "grok-3-deepsearch": { RequestFrequency: 10 },
112
- "grok-3-reasoning": { RequestFrequency: 10 }
113
- };
114
- this.modelInitFrequency = {
115
- "grok-3": 0,
116
- "grok-3-deepsearch": 0,
117
- "grok-3-reasoning": 0
118
- };
119
- } else {
120
- this.modelRateLimit = {
121
- "grok-2": { RequestFrequency: 20 },
122
- "grok-3": { RequestFrequency: 20 },
123
- "grok-3-deepsearch": { RequestFrequency: 5 },
124
- "grok-3-reasoning": { RequestFrequency: 5 }
125
- };
126
- this.modelInitFrequency = {
127
- "grok-2": 0,
128
- "grok-3": 0,
129
- "grok-3-deepsearch": 0,
130
- "grok-3-reasoning": 0
131
- };
132
- }
133
-
134
  }
135
 
136
  addToken(token) {
137
- if (!this.activeTokens.includes(token)) {
138
- this.activeTokens.push(token);
139
- this.tokenModelFrequency.set(token, this.modelInitFrequency);
140
- }
 
 
 
 
 
 
 
 
 
 
141
  }
 
 
142
  setToken(token) {
143
- this.activeTokens = [token];
144
- this.tokenModelFrequency.set(token, this.modelInitFrequency);
 
 
 
 
 
 
 
 
145
  }
146
 
147
- getTokenByIndex(index, model) {
148
- if (this.activeTokens.length === 0) {
 
 
149
  return null;
150
  }
151
- const token = this.activeTokens[index];
152
- this.recordModelRequest(token, model);
153
- return token;
154
- }
 
 
 
 
 
155
 
156
- recordModelRequest(token, model) {
157
- if (model.startsWith('grok-') && (model.includes('search') || model.includes('imageGen'))) {
158
- model = model.split('-')[0] + '-' + model.split('-')[1];
159
  }
160
 
161
- if (!this.modelRateLimit[model]) return;
162
- const tokenFrequency = this.tokenModelFrequency.get(token);
163
- if (tokenFrequency && tokenFrequency[model] !== undefined) {
164
- tokenFrequency[model]++;
165
- }
166
- this.checkAndRemoveTokenIfLimitReached(token);
167
- }
168
- setModelLimit(index, model) {
169
- if (model.startsWith('grok-') && (model.includes('search') || model.includes('imageGen'))) {
170
- model = model.split('-')[0] + '-' + model.split('-')[1];
171
- }
172
- if (!this.modelRateLimit[model]) return;
173
- const tokenFrequency = this.tokenModelFrequency.get(this.activeTokens[index]);
174
- tokenFrequency[model] = 9999;
175
  }
176
- isTokenModelLimitReached(index, model) {
177
- if (model.startsWith('grok-') && (model.includes('search') || model.includes('imageGen'))) {
178
- model = model.split('-')[0] + '-' + model.split('-')[1];
179
- }
180
- if (!this.modelRateLimit[model]) return;
181
- const token = this.activeTokens[index];
182
- const tokenFrequency = this.tokenModelFrequency.get(token);
183
 
184
- if (!tokenFrequency) {
 
 
 
 
185
  return false;
186
  }
187
- return tokenFrequency[model] >= this.modelRateLimit[model].RequestFrequency;
188
- }
189
- checkAndRemoveTokenIfLimitReached(token) {
190
- const tokenFrequency = this.tokenModelFrequency.get(token);
191
- if (!tokenFrequency) return;
192
-
193
- const isLimitReached = Object.keys(tokenFrequency).every(model =>
194
- tokenFrequency[model] >= this.modelRateLimit[model].RequestFrequency
195
- );
196
-
197
- if (isLimitReached) {
198
- const tokenIndex = this.activeTokens.indexOf(token);
199
- if (tokenIndex !== -1) {
200
- this.removeTokenByIndex(tokenIndex);
201
  }
 
 
202
  }
 
 
 
 
 
 
 
203
  }
204
 
205
- removeTokenByIndex(index) {
206
- if (!this.isRecoveryProcess) {
207
- this.startTokenRecoveryProcess();
208
  }
209
- const token = this.activeTokens[index];
210
- this.expiredTokens.set(token, Date.now());
211
- this.activeTokens.splice(index, 1);
212
- this.tokenModelFrequency.delete(token);
213
- Logger.info(`令牌${token}已达到上限,已移除`, 'TokenManager');
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
214
  }
215
 
216
- startTokenRecoveryProcess() {
217
- if (CONFIG.API.IS_CUSTOM_SSO) return;
218
  setInterval(() => {
219
  const now = Date.now();
220
- for (const [token, expiredTime] of this.expiredTokens.entries()) {
221
- if (now - expiredTime >= 2 * 60 * 60 * 1000) {
222
- this.tokenModelFrequency.set(token, this.modelInitFrequency);
223
- this.activeTokens.push(token);
224
- this.expiredTokens.delete(token);
225
- Logger.info(`令牌${token}已恢复,已添加到可用令牌列表`, 'TokenManager');
 
 
 
 
 
 
226
  }
227
- }
228
- }, 2 * 60 * 60 * 1000);
229
- }
230
-
231
- getTokenCount() {
232
- return this.activeTokens.length || 0;
233
  }
234
 
235
- getActiveTokens() {
236
- return [...this.activeTokens];
 
 
 
 
237
  }
238
  }
239
 
 
240
  class Utils {
241
  static delay(time) {
242
  return new Promise(function (resolve) {
@@ -261,7 +303,7 @@ class Utils {
261
  return formattedResults.join('\n\n');
262
  }
263
  static async createAuthHeaders(model) {
264
- return await tokenManager.getTokenByIndex(CONFIG.SSO_INDEX, model);
265
  }
266
  }
267
  class GrokTempCookieManager {
@@ -554,6 +596,7 @@ class GrokApiClient {
554
  }
555
 
556
  return {
 
557
  modelName: this.modelId,
558
  message: messages.trim(),
559
  fileAttachments: fileAttachments.slice(0, 4),
@@ -901,9 +944,10 @@ app.post('/hf/v1/chat/completions', async (req, res) => {
901
  } else if (authToken !== CONFIG.API.API_KEY) {
902
  return res.status(401).json({ error: 'Unauthorized' });
903
  }
904
- let isTempCookie = req.body.model.includes("grok-2") && CONFIG.API.IS_TEMP_GROK2;
 
905
  let retryCount = 0;
906
- const grokClient = new GrokApiClient(req.body.model);
907
  const requestPayload = await grokClient.prepareChatRequest(req.body);
908
  Logger.info(`请求体: ${JSON.stringify(requestPayload, null, 2)}`, 'Server');
909
 
@@ -913,13 +957,13 @@ app.post('/hf/v1/chat/completions', async (req, res) => {
913
  CONFIG.API.SIGNATURE_COOKIE = CONFIG.API.TEMP_COOKIE;
914
  Logger.info(`已切换为临时令牌`, 'Server');
915
  } else {
916
- CONFIG.API.SIGNATURE_COOKIE = await Utils.createAuthHeaders(req.body.model);
917
  }
918
  if (!CONFIG.API.SIGNATURE_COOKIE) {
919
  throw new Error('该模型无可用令牌');
920
  }
921
- Logger.info(`当前令牌索引: ${CONFIG.SSO_INDEX}`, 'Server');
922
  Logger.info(`当前令牌: ${JSON.stringify(CONFIG.API.SIGNATURE_COOKIE, null, 2)}`, 'Server');
 
923
  const response = await fetch(`${CONFIG.API.BASE_URL}/rest/app-chat/conversations/new`, {
924
  method: 'POST',
925
  headers: {
@@ -934,10 +978,9 @@ app.post('/hf/v1/chat/completions', async (req, res) => {
934
 
935
  if (response.ok) {
936
  Logger.info(`请求成功`, 'Server');
937
- CONFIG.SSO_INDEX = (CONFIG.SSO_INDEX + 1) % tokenManager.getTokenCount();
938
- Logger.info(`当前剩余可用令牌数: ${tokenManager.getTokenCount()}`, 'Server');
939
  try {
940
- await handleResponse(response, req.body.model, res, req.body.stream);
941
  Logger.info(`请求结束`, 'Server');
942
  return;
943
  } catch (error) {
@@ -959,14 +1002,10 @@ app.post('/hf/v1/chat/completions', async (req, res) => {
959
  }
960
  }
961
  } else {
962
- tokenManager.setModelLimit(CONFIG.SSO_INDEX, req.body.model);
963
- for (let i = 1; i <= tokenManager.getTokenCount(); i++) {
964
- CONFIG.SSO_INDEX = (CONFIG.SSO_INDEX + 1) % tokenManager.getTokenCount();
965
- if (!tokenManager.isTokenModelLimitReached(CONFIG.SSO_INDEX, req.body.model)) {
966
- break;
967
- } else if (i >= tokenManager.getTokenCount()) {
968
- throw new Error(`${req.body.model} 次数已达上限,请切换其他模型或者重新对话`);
969
- }
970
  }
971
  }
972
  }
@@ -989,14 +1028,10 @@ app.post('/hf/v1/chat/completions', async (req, res) => {
989
  }
990
  }
991
  } else {
992
- tokenManager.setModelLimit(CONFIG.SSO_INDEX, req.body.model);
993
- for (let i = 1; i <= tokenManager.getTokenCount(); i++) {
994
- CONFIG.SSO_INDEX = (CONFIG.SSO_INDEX + 1) % tokenManager.getTokenCount();
995
- if (!tokenManager.isTokenModelLimitReached(CONFIG.SSO_INDEX, req.body.model)) {
996
- break;
997
- } else if (i >= tokenManager.getTokenCount()) {
998
- throw new Error(`${req.body.model} 次数已达上限,请切换其他模型或者重新对话`);
999
- }
1000
  }
1001
  }
1002
  } else {
@@ -1018,10 +1053,10 @@ app.post('/hf/v1/chat/completions', async (req, res) => {
1018
  }
1019
  }
1020
  } else {
1021
- Logger.error(`令牌异常错误状态!status: ${response.status}, 已移除当前令牌${CONFIG.SSO_INDEX.cookie}`, 'Server');
1022
- tokenManager.removeTokenByIndex(CONFIG.SSO_INDEX);
1023
- Logger.info(`当前剩余可用令牌数: ${tokenManager.getTokenCount()}`, 'Server');
1024
- CONFIG.SSO_INDEX = (CONFIG.SSO_INDEX + 1) % tokenManager.getTokenCount();
1025
  }
1026
  }
1027
  }
 
23
  "grok-3-reasoning": "grok-3"
24
  },
25
  API: {
26
+ IS_TEMP_CONVERSATION: process.env.IS_TEMP_CONVERSATION == undefined ? false : process.env.IS_TEMP_CONVERSATION == 'true',
27
  IS_TEMP_GROK2: process.env.IS_TEMP_GROK2 == undefined ? true : process.env.IS_TEMP_GROK2 == 'true',
28
  GROK2_CONCURRENCY_LEVEL: process.env.GROK2_CONCURRENCY_LEVEL || 4,
29
  IS_CUSTOM_SSO: process.env.IS_CUSTOM_SSO == undefined ? false : process.env.IS_CUSTOM_SSO == 'true',
 
45
  IS_THINKING: false,
46
  IS_IMG_GEN: false,
47
  IS_IMG_GEN2: false,
 
48
  TEMP_COOKIE_INDEX: 0,//临时cookie的下标
49
  ISSHOW_SEARCH_RESULTS: process.env.ISSHOW_SEARCH_RESULTS == undefined ? true : process.env.ISSHOW_SEARCH_RESULTS == 'true',//是否显示搜索结果
50
  CHROME_PATH: process.env.CHROME_PATH || null
 
90
  ssoArray.forEach((sso) => {
91
  tokenManager.addToken(`sso-rw=${sso};sso=${sso}`);
92
  });
93
+ Logger.info(`成功加载令牌: ${JSON.stringify(tokenManager.getAllTokens(), null, 2)}`, 'Server');
94
+ Logger.info(`令牌加载完成,共加载: ${tokenManager.getAllTokens().length}个令牌`, 'Server');
95
  if (CONFIG.API.IS_TEMP_GROK2) {
96
  await tempCookieManager.ensureCookies();
97
  CONFIG.API.TEMP_COOKIE = tempCookieManager.cookies[tempCookieManager.currentIndex];
 
99
  Logger.info("初始化完成", 'Server');
100
  }
101
 
 
102
  class AuthTokenManager {
103
  constructor() {
104
+ this.tokenModelMap = {};
105
+ this.expiredTokens = new Set();
106
+
107
+ // 定义模型请求频率限制和过期时间
108
+ this.modelConfig = {
109
+ "grok-2": {
110
+ RequestFrequency: 20,
111
+ ExpirationTime: 2 * 60 * 60 * 1000 // 2小时
112
+ },
113
+ "grok-3": {
114
+ RequestFrequency: 20,
115
+ ExpirationTime: 2 * 60 * 60 * 1000 // 2小时
116
+ },
117
+ "grok-3-deepsearch": {
118
+ RequestFrequency: 10,
119
+ ExpirationTime: 24 * 60 * 60 * 1000 // 24小时
120
+ },
121
+ "grok-3-reasoning": {
122
+ RequestFrequency: 10,
123
+ ExpirationTime: 24 * 60 * 60 * 1000 // 24小时
124
+ }
125
+ };
126
+ this.tokenResetSwitch = false;
 
 
 
 
 
 
127
  }
128
 
129
  addToken(token) {
130
+ Object.keys(this.modelConfig).forEach(model => {
131
+ if (!this.tokenModelMap[model]) {
132
+ this.tokenModelMap[model] = [];
133
+ }
134
+ const existingTokenEntry = this.tokenModelMap[model].find(entry => entry.token === token);
135
+
136
+ if (!existingTokenEntry) {
137
+ this.tokenModelMap[model].push({
138
+ token: token,
139
+ RequestCount: 0,
140
+ AddedTime: Date.now()
141
+ });
142
+ }
143
+ });
144
  }
145
+
146
+ //直接设置token,仅适用于自己设置轮询而不是使用AuthTokenManager管理
147
  setToken(token) {
148
+ const models = ["grok-2", "grok-3", "grok-3-reasoning", "grok-3-deepsearch"];
149
+
150
+ this.tokenModelMap = models.reduce((map, model) => {
151
+ map[model] = [{
152
+ token,
153
+ RequestCount: 0,
154
+ AddedTime: Date.now()
155
+ }];
156
+ return map;
157
+ }, {});
158
  }
159
 
160
+ getNextTokenForModel(modelId) {
161
+ const normalizedModel = this.normalizeModelName(modelId);
162
+
163
+ if (!this.tokenModelMap[normalizedModel] || this.tokenModelMap[normalizedModel].length === 0) {
164
  return null;
165
  }
166
+ const tokenEntry = this.tokenModelMap[normalizedModel][0];
167
+
168
+ if (tokenEntry) {
169
+ tokenEntry.RequestCount++;
170
+ if (tokenEntry.RequestCount > this.modelConfig[normalizedModel].RequestFrequency) {
171
+ this.removeTokenFromModel(normalizedModel, tokenEntry.token);
172
+ const nextTokenEntry = this.tokenModelMap[normalizedModel][0];
173
+ return nextTokenEntry ? nextTokenEntry.token : null;
174
+ }
175
 
176
+ return tokenEntry.token;
 
 
177
  }
178
 
179
+ return null;
 
 
 
 
 
 
 
 
 
 
 
 
 
180
  }
 
 
 
 
 
 
 
181
 
182
+ removeTokenFromModel(modelId, token) {
183
+ const normalizedModel = this.normalizeModelName(modelId);
184
+
185
+ if (!this.tokenModelMap[normalizedModel]) {
186
+ Logger.error(`模型 ${normalizedModel} 不存在`, 'TokenManager');
187
  return false;
188
  }
189
+
190
+ const modelTokens = this.tokenModelMap[normalizedModel];
191
+ const tokenIndex = modelTokens.findIndex(entry => entry.token === token);
192
+
193
+ if (tokenIndex !== -1) {
194
+ const removedTokenEntry = modelTokens.splice(tokenIndex, 1)[0];
195
+ this.expiredTokens.add({
196
+ token: removedTokenEntry.token,
197
+ model: normalizedModel,
198
+ expiredTime: Date.now()
199
+ });
200
+ if(!this.tokenResetSwitch){
201
+ this.startTokenResetProcess();
202
+ this.tokenResetSwitch = true;
203
  }
204
+ Logger.info(`模型${modelId}的令牌已失效,已成功移除令牌: ${token}`, 'TokenManager');
205
+ return true;
206
  }
207
+
208
+ Logger.error(`在模型 ${normalizedModel} 中未找到 token: ${token}`, 'TokenManager');
209
+ return false;
210
+ }
211
+
212
+ getExpiredTokens() {
213
+ return Array.from(this.expiredTokens);
214
  }
215
 
216
+ normalizeModelName(model) {
217
+ if (model.startsWith('grok-') && !model.includes('deepsearch') && !model.includes('reasoning')) {
218
+ return model.split('-').slice(0, 2).join('-');
219
  }
220
+ return model;
221
+ }
222
+
223
+ getTokenCountForModel(modelId) {
224
+ const normalizedModel = this.normalizeModelName(modelId);
225
+ return this.tokenModelMap[normalizedModel]?.length || 0;
226
+ }
227
+ getRemainingTokenRequestCapacity() {
228
+ const remainingCapacityMap = {};
229
+
230
+ Object.keys(this.modelConfig).forEach(model => {
231
+ const modelTokens = this.tokenModelMap[model] || [];
232
+
233
+ const modelRequestFrequency = this.modelConfig[model].RequestFrequency;
234
+
235
+ const totalUsedRequests = modelTokens.reduce((sum, tokenEntry) => {
236
+ return sum + (tokenEntry.RequestCount || 0);
237
+ }, 0);
238
+
239
+ // 计算剩余可用请求数量
240
+ const remainingCapacity = (modelTokens.length * modelRequestFrequency) - totalUsedRequests;
241
+ remainingCapacityMap[model] = Math.max(0, remainingCapacity);
242
+ });
243
+
244
+ return remainingCapacityMap;
245
+ }
246
+
247
+ getTokenArrayForModel(modelId) {
248
+ const normalizedModel = this.normalizeModelName(modelId);
249
+ return this.tokenModelMap[normalizedModel] || [];
250
  }
251
 
252
+ startTokenResetProcess() {
 
253
  setInterval(() => {
254
  const now = Date.now();
255
+ this.expiredTokens.forEach(expiredTokenInfo => {
256
+ const { token, model, expiredTime } = expiredTokenInfo;
257
+ const expirationTime = this.modelConfig[model].ExpirationTime;
258
+ if (now - expiredTime >= expirationTime) {
259
+ if (!this.tokenModelMap[model].some(entry => entry.token === token)) {
260
+ this.tokenModelMap[model].push({
261
+ token: token,
262
+ RequestCount: 0,
263
+ AddedTime: now
264
+ });
265
+ }
266
+ this.expiredTokens.delete(expiredTokenInfo);
267
  }
268
+ });
269
+ }, 2 * 60 * 60 * 1000); // 每两小时运行一次
 
 
 
 
270
  }
271
 
272
+ getAllTokens() {
273
+ const allTokens = new Set();
274
+ Object.values(this.tokenModelMap).forEach(modelTokens => {
275
+ modelTokens.forEach(entry => allTokens.add(entry.token));
276
+ });
277
+ return Array.from(allTokens);
278
  }
279
  }
280
 
281
+
282
  class Utils {
283
  static delay(time) {
284
  return new Promise(function (resolve) {
 
303
  return formattedResults.join('\n\n');
304
  }
305
  static async createAuthHeaders(model) {
306
+ return await tokenManager.getNextTokenForModel(model);
307
  }
308
  }
309
  class GrokTempCookieManager {
 
596
  }
597
 
598
  return {
599
+ temporary: CONFIG.API.IS_TEMP_CONVERSATION,
600
  modelName: this.modelId,
601
  message: messages.trim(),
602
  fileAttachments: fileAttachments.slice(0, 4),
 
944
  } else if (authToken !== CONFIG.API.API_KEY) {
945
  return res.status(401).json({ error: 'Unauthorized' });
946
  }
947
+ const { model, stream } = req.body;
948
+ let isTempCookie = model.includes("grok-2") && CONFIG.API.IS_TEMP_GROK2;
949
  let retryCount = 0;
950
+ const grokClient = new GrokApiClient(model);
951
  const requestPayload = await grokClient.prepareChatRequest(req.body);
952
  Logger.info(`请求体: ${JSON.stringify(requestPayload, null, 2)}`, 'Server');
953
 
 
957
  CONFIG.API.SIGNATURE_COOKIE = CONFIG.API.TEMP_COOKIE;
958
  Logger.info(`已切换为临时令牌`, 'Server');
959
  } else {
960
+ CONFIG.API.SIGNATURE_COOKIE = await Utils.createAuthHeaders(model);
961
  }
962
  if (!CONFIG.API.SIGNATURE_COOKIE) {
963
  throw new Error('该模型无可用令牌');
964
  }
 
965
  Logger.info(`当前令牌: ${JSON.stringify(CONFIG.API.SIGNATURE_COOKIE, null, 2)}`, 'Server');
966
+ Logger.info(`当前可用模型的全部可用数量: ${JSON.stringify(tokenManager.getRemainingTokenRequestCapacity(), null, 2)}`, 'Server');
967
  const response = await fetch(`${CONFIG.API.BASE_URL}/rest/app-chat/conversations/new`, {
968
  method: 'POST',
969
  headers: {
 
978
 
979
  if (response.ok) {
980
  Logger.info(`请求成功`, 'Server');
981
+ Logger.info(`当前${model}剩余可用令牌数: ${tokenManager.getTokenCountForModel(model)}`, 'Server');
 
982
  try {
983
+ await handleResponse(response, model, res, stream);
984
  Logger.info(`请求结束`, 'Server');
985
  return;
986
  } catch (error) {
 
1002
  }
1003
  }
1004
  } else {
1005
+ if(CONFIG.API.IS_CUSTOM_SSO) throw new Error(`自定义SSO令牌当前模型${model}的请求次数已失效`);
1006
+ tokenManager.removeTokenFromModel(model, CONFIG.API.SIGNATURE_COOKIE.cookie);
1007
+ if(tokenManager.getTokenCountForModel(model) === 0){
1008
+ throw new Error(`${model} 次数已达上限,请切换其他模型或者重新对话`);
 
 
 
 
1009
  }
1010
  }
1011
  }
 
1028
  }
1029
  }
1030
  } else {
1031
+ if(CONFIG.API.IS_CUSTOM_SSO) throw new Error(`自定义SSO令牌当前模型${model}的请求次数已失效`);
1032
+ tokenManager.removeTokenFromModel(model, CONFIG.API.SIGNATURE_COOKIE.cookie);
1033
+ if(tokenManager.getTokenCountForModel(model) === 0){
1034
+ throw new Error(`${model} 次数已达上限,请切换其他模型或者重新对话`);
 
 
 
 
1035
  }
1036
  }
1037
  } else {
 
1053
  }
1054
  }
1055
  } else {
1056
+ if(CONFIG.API.IS_CUSTOM_SSO) throw new Error(`自定义SSO令牌当前模型${model}的请求次数已失效`);
1057
+ Logger.error(`令牌异常错误状态!status: ${response.status}`, 'Server');
1058
+ tokenManager.removeTokenFromModel(model, CONFIG.API.SIGNATURE_COOKIE.cookie);
1059
+ Logger.info(`当前${model}剩余可用令牌数: ${tokenManager.getTokenCountForModel(model)}`, 'Server');
1060
  }
1061
  }
1062
  }