opencode-kimi-rotator 1.2.7 → 1.2.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/plugin.bundle.js +50 -14
- package/dist/plugin.d.ts.map +1 -1
- package/dist/plugin.js +51 -15
- package/dist/plugin.js.map +1 -1
- package/package.json +1 -1
- package/src/plugin.ts +64 -17
package/dist/plugin.bundle.js
CHANGED
|
@@ -6272,6 +6272,14 @@ function getRequestUrl(input) {
|
|
|
6272
6272
|
}
|
|
6273
6273
|
return input.url;
|
|
6274
6274
|
}
|
|
6275
|
+
function buildRequestHeaders(input, init) {
|
|
6276
|
+
const headers = new Headers(input instanceof Request ? input.headers : void 0);
|
|
6277
|
+
const initHeaders = new Headers(init?.headers);
|
|
6278
|
+
initHeaders.forEach((value, key) => {
|
|
6279
|
+
headers.set(key, value);
|
|
6280
|
+
});
|
|
6281
|
+
return headers;
|
|
6282
|
+
}
|
|
6275
6283
|
async function showToast(message, variant = "info") {
|
|
6276
6284
|
if (!openCodeClient?.tui?.showToast) {
|
|
6277
6285
|
toastQueue.push({ message, variant });
|
|
@@ -6314,34 +6322,46 @@ var KimiRotatorPlugin = async ({ client }) => {
|
|
|
6314
6322
|
if (!accountManager) {
|
|
6315
6323
|
throw new Error("Account manager not initialized");
|
|
6316
6324
|
}
|
|
6317
|
-
|
|
6318
|
-
|
|
6325
|
+
if (!originalFetch) {
|
|
6326
|
+
throw new Error("Original fetch not available");
|
|
6327
|
+
}
|
|
6328
|
+
const allKeysNow = await accountManager.listKeys();
|
|
6329
|
+
const total = allKeysNow.length;
|
|
6330
|
+
const maxAttempts = Math.max(1, total);
|
|
6331
|
+
let lastResponse = null;
|
|
6332
|
+
for (let attempt = 0; attempt < maxAttempts; attempt++) {
|
|
6333
|
+
const nextAccount = await accountManager.getNextAccount();
|
|
6334
|
+
if (!nextAccount) {
|
|
6335
|
+
break;
|
|
6336
|
+
}
|
|
6319
6337
|
const requestAccountIndex = nextAccount.index;
|
|
6320
6338
|
const keyLabel = nextAccount.account.key.substring(0, 18) + "...";
|
|
6321
6339
|
const position = requestAccountIndex + 1;
|
|
6322
|
-
const
|
|
6323
|
-
const total = allKeysNow.length;
|
|
6324
|
-
await showToast(`\u{1F504} Using key: ${keyLabel} (${String(position)}/${String(total)})`, "info");
|
|
6325
|
-
const headers = new Headers(init?.headers);
|
|
6340
|
+
const headers = buildRequestHeaders(input, init);
|
|
6326
6341
|
headers.set("x-api-key", nextAccount.account.key);
|
|
6327
6342
|
headers.delete("Authorization");
|
|
6343
|
+
await showToast(`\u{1F504} Using key: ${keyLabel} (${String(position)}/${String(total)})`, "info");
|
|
6328
6344
|
const requestStartTime = Date.now();
|
|
6329
6345
|
const today = (/* @__PURE__ */ new Date()).toISOString().split("T")[0];
|
|
6330
|
-
if (!originalFetch) {
|
|
6331
|
-
throw new Error("Original fetch not available");
|
|
6332
|
-
}
|
|
6333
6346
|
try {
|
|
6334
|
-
const
|
|
6347
|
+
const attemptInput = input instanceof Request ? input.clone() : input;
|
|
6348
|
+
const response = await originalFetch(attemptInput, { ...init, headers });
|
|
6335
6349
|
const responseTime = Date.now() - requestStartTime;
|
|
6336
6350
|
if (response.status === 429) {
|
|
6337
6351
|
const retryAfter = response.headers.get("retry-after");
|
|
6338
6352
|
const retryAfterMs = retryAfter ? parseInt(retryAfter, 10) * 1e3 : 6e4;
|
|
6339
6353
|
await accountManager.markAccountRateLimited(requestAccountIndex, retryAfterMs);
|
|
6340
6354
|
await showToast(`\u26A0\uFE0F Key ${String(position)} rate limited`, "warning");
|
|
6355
|
+
if (attempt < maxAttempts - 1) {
|
|
6356
|
+
lastResponse = response;
|
|
6357
|
+
continue;
|
|
6358
|
+
}
|
|
6341
6359
|
} else if (response.ok) {
|
|
6342
6360
|
await accountManager.markAccountSuccess(requestAccountIndex, responseTime, today);
|
|
6361
|
+
return response;
|
|
6343
6362
|
} else if (response.status >= 500) {
|
|
6344
6363
|
await accountManager.markAccountFailure(requestAccountIndex);
|
|
6364
|
+
return response;
|
|
6345
6365
|
} else if (response.status === 400 || response.status === 403) {
|
|
6346
6366
|
const clonedResponse = response.clone();
|
|
6347
6367
|
try {
|
|
@@ -6357,8 +6377,10 @@ var KimiRotatorPlugin = async ({ client }) => {
|
|
|
6357
6377
|
(pattern) => bodyLower.includes(pattern)
|
|
6358
6378
|
);
|
|
6359
6379
|
if (isBillingLimit && accountManager) {
|
|
6380
|
+
let shouldRetry = false;
|
|
6360
6381
|
if (isHardBillingLimit) {
|
|
6361
6382
|
await accountManager.markAccountBillingLimited(requestAccountIndex);
|
|
6383
|
+
shouldRetry = true;
|
|
6362
6384
|
const resetTime = /* @__PURE__ */ new Date();
|
|
6363
6385
|
resetTime.setDate(resetTime.getDate() + 1);
|
|
6364
6386
|
resetTime.setHours(0, 0, 0, 0);
|
|
@@ -6371,6 +6393,7 @@ var KimiRotatorPlugin = async ({ client }) => {
|
|
|
6371
6393
|
);
|
|
6372
6394
|
} else {
|
|
6373
6395
|
const result2 = await accountManager.recordBillingLimitHit(requestAccountIndex);
|
|
6396
|
+
shouldRetry = result2.isConfirmed;
|
|
6374
6397
|
if (result2.isConfirmed) {
|
|
6375
6398
|
const resetTime = /* @__PURE__ */ new Date();
|
|
6376
6399
|
resetTime.setDate(resetTime.getDate() + 1);
|
|
@@ -6388,14 +6411,24 @@ var KimiRotatorPlugin = async ({ client }) => {
|
|
|
6388
6411
|
);
|
|
6389
6412
|
}
|
|
6390
6413
|
}
|
|
6391
|
-
|
|
6392
|
-
|
|
6393
|
-
|
|
6394
|
-
|
|
6414
|
+
if (shouldRetry && attempt < maxAttempts - 1) {
|
|
6415
|
+
await showToast("\u{1F501} Retrying with next key...", "info");
|
|
6416
|
+
lastResponse = response;
|
|
6417
|
+
continue;
|
|
6418
|
+
}
|
|
6419
|
+
return response;
|
|
6395
6420
|
}
|
|
6421
|
+
debugLog(`HTTP ${response.status} but not billing limit for key ${String(position)}`);
|
|
6422
|
+
await accountManager.resetBillingLimitHits(requestAccountIndex);
|
|
6423
|
+
await accountManager.markAccountFailure(requestAccountIndex);
|
|
6424
|
+
return response;
|
|
6396
6425
|
} catch (error) {
|
|
6397
6426
|
debugLog("Error reading response body:", error);
|
|
6427
|
+
return response;
|
|
6398
6428
|
}
|
|
6429
|
+
} else {
|
|
6430
|
+
await accountManager.markAccountFailure(requestAccountIndex);
|
|
6431
|
+
return response;
|
|
6399
6432
|
}
|
|
6400
6433
|
return response;
|
|
6401
6434
|
} catch (error) {
|
|
@@ -6403,6 +6436,9 @@ var KimiRotatorPlugin = async ({ client }) => {
|
|
|
6403
6436
|
throw error;
|
|
6404
6437
|
}
|
|
6405
6438
|
}
|
|
6439
|
+
if (lastResponse) {
|
|
6440
|
+
return lastResponse;
|
|
6441
|
+
}
|
|
6406
6442
|
}
|
|
6407
6443
|
if (!originalFetch) {
|
|
6408
6444
|
throw new Error("Original fetch not available");
|
package/dist/plugin.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"plugin.d.ts","sourceRoot":"","sources":["../src/plugin.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,qBAAqB,CAAC;AAElD,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;
|
|
1
|
+
{"version":3,"file":"plugin.d.ts","sourceRoot":"","sources":["../src/plugin.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,qBAAqB,CAAC;AAElD,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;AAsH9C,eAAO,MAAM,iBAAiB,EAAE,MAsN/B,CAAC;AAEF,eAAe,iBAAiB,CAAC;AACjC,YAAY,EAAE,WAAW,EAAE,CAAC"}
|
package/dist/plugin.js
CHANGED
|
@@ -51,6 +51,14 @@ function getRequestUrl(input) {
|
|
|
51
51
|
}
|
|
52
52
|
return input.url;
|
|
53
53
|
}
|
|
54
|
+
function buildRequestHeaders(input, init) {
|
|
55
|
+
const headers = new Headers(input instanceof Request ? input.headers : undefined);
|
|
56
|
+
const initHeaders = new Headers(init?.headers);
|
|
57
|
+
initHeaders.forEach((value, key) => {
|
|
58
|
+
headers.set(key, value);
|
|
59
|
+
});
|
|
60
|
+
return headers;
|
|
61
|
+
}
|
|
54
62
|
async function showToast(message, variant = 'info') {
|
|
55
63
|
if (!openCodeClient?.tui?.showToast) {
|
|
56
64
|
toastQueue.push({ message, variant });
|
|
@@ -97,36 +105,48 @@ export const KimiRotatorPlugin = async ({ client }) => {
|
|
|
97
105
|
if (!accountManager) {
|
|
98
106
|
throw new Error('Account manager not initialized');
|
|
99
107
|
}
|
|
100
|
-
|
|
101
|
-
|
|
108
|
+
if (!originalFetch) {
|
|
109
|
+
throw new Error('Original fetch not available');
|
|
110
|
+
}
|
|
111
|
+
const allKeysNow = await accountManager.listKeys();
|
|
112
|
+
const total = allKeysNow.length;
|
|
113
|
+
const maxAttempts = Math.max(1, total);
|
|
114
|
+
let lastResponse = null;
|
|
115
|
+
for (let attempt = 0; attempt < maxAttempts; attempt++) {
|
|
116
|
+
const nextAccount = await accountManager.getNextAccount();
|
|
117
|
+
if (!nextAccount) {
|
|
118
|
+
break;
|
|
119
|
+
}
|
|
102
120
|
const requestAccountIndex = nextAccount.index;
|
|
103
121
|
const keyLabel = nextAccount.account.key.substring(0, 18) + '...';
|
|
104
122
|
const position = requestAccountIndex + 1;
|
|
105
|
-
const
|
|
106
|
-
const total = allKeysNow.length;
|
|
107
|
-
await showToast(`🔄 Using key: ${keyLabel} (${String(position)}/${String(total)})`, 'info');
|
|
108
|
-
const headers = new Headers(init?.headers);
|
|
123
|
+
const headers = buildRequestHeaders(input, init);
|
|
109
124
|
headers.set('x-api-key', nextAccount.account.key);
|
|
110
125
|
headers.delete('Authorization');
|
|
126
|
+
await showToast(`🔄 Using key: ${keyLabel} (${String(position)}/${String(total)})`, 'info');
|
|
111
127
|
const requestStartTime = Date.now();
|
|
112
128
|
const today = new Date().toISOString().split('T')[0];
|
|
113
|
-
if (!originalFetch) {
|
|
114
|
-
throw new Error('Original fetch not available');
|
|
115
|
-
}
|
|
116
129
|
try {
|
|
117
|
-
const
|
|
130
|
+
const attemptInput = input instanceof Request ? input.clone() : input;
|
|
131
|
+
const response = await originalFetch(attemptInput, { ...init, headers });
|
|
118
132
|
const responseTime = Date.now() - requestStartTime;
|
|
119
133
|
if (response.status === 429) {
|
|
120
134
|
const retryAfter = response.headers.get('retry-after');
|
|
121
135
|
const retryAfterMs = retryAfter ? parseInt(retryAfter, 10) * 1000 : 60000;
|
|
122
136
|
await accountManager.markAccountRateLimited(requestAccountIndex, retryAfterMs);
|
|
123
137
|
await showToast(`⚠️ Key ${String(position)} rate limited`, 'warning');
|
|
138
|
+
if (attempt < maxAttempts - 1) {
|
|
139
|
+
lastResponse = response;
|
|
140
|
+
continue;
|
|
141
|
+
}
|
|
124
142
|
}
|
|
125
143
|
else if (response.ok) {
|
|
126
144
|
await accountManager.markAccountSuccess(requestAccountIndex, responseTime, today);
|
|
145
|
+
return response;
|
|
127
146
|
}
|
|
128
147
|
else if (response.status >= 500) {
|
|
129
148
|
await accountManager.markAccountFailure(requestAccountIndex);
|
|
149
|
+
return response;
|
|
130
150
|
}
|
|
131
151
|
else if (response.status === 400 || response.status === 403) {
|
|
132
152
|
const clonedResponse = response.clone();
|
|
@@ -139,8 +159,10 @@ export const KimiRotatorPlugin = async ({ client }) => {
|
|
|
139
159
|
const isBillingLimit = BILLING_LIMIT_PATTERNS.some((pattern) => bodyLower.includes(pattern));
|
|
140
160
|
const isHardBillingLimit = HARD_BILLING_LIMIT_PATTERNS.some((pattern) => bodyLower.includes(pattern));
|
|
141
161
|
if (isBillingLimit && accountManager) {
|
|
162
|
+
let shouldRetry = false;
|
|
142
163
|
if (isHardBillingLimit) {
|
|
143
164
|
await accountManager.markAccountBillingLimited(requestAccountIndex);
|
|
165
|
+
shouldRetry = true;
|
|
144
166
|
const resetTime = new Date();
|
|
145
167
|
resetTime.setDate(resetTime.getDate() + 1);
|
|
146
168
|
resetTime.setHours(0, 0, 0, 0);
|
|
@@ -149,6 +171,7 @@ export const KimiRotatorPlugin = async ({ client }) => {
|
|
|
149
171
|
}
|
|
150
172
|
else {
|
|
151
173
|
const result = await accountManager.recordBillingLimitHit(requestAccountIndex);
|
|
174
|
+
shouldRetry = result.isConfirmed;
|
|
152
175
|
if (result.isConfirmed) {
|
|
153
176
|
const resetTime = new Date();
|
|
154
177
|
resetTime.setDate(resetTime.getDate() + 1);
|
|
@@ -160,17 +183,27 @@ export const KimiRotatorPlugin = async ({ client }) => {
|
|
|
160
183
|
debugLog(`Billing limit hit ${2 - result.hitsNeeded}/2 for key ${String(position)} - not confirmed yet`);
|
|
161
184
|
}
|
|
162
185
|
}
|
|
186
|
+
if (shouldRetry && attempt < maxAttempts - 1) {
|
|
187
|
+
await showToast('🔁 Retrying with next key...', 'info');
|
|
188
|
+
lastResponse = response;
|
|
189
|
+
continue;
|
|
190
|
+
}
|
|
191
|
+
return response;
|
|
163
192
|
}
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
}
|
|
193
|
+
debugLog(`HTTP ${response.status} but not billing limit for key ${String(position)}`);
|
|
194
|
+
await accountManager.resetBillingLimitHits(requestAccountIndex);
|
|
195
|
+
await accountManager.markAccountFailure(requestAccountIndex);
|
|
196
|
+
return response;
|
|
169
197
|
}
|
|
170
198
|
catch (error) {
|
|
171
199
|
debugLog('Error reading response body:', error);
|
|
200
|
+
return response;
|
|
172
201
|
}
|
|
173
202
|
}
|
|
203
|
+
else {
|
|
204
|
+
await accountManager.markAccountFailure(requestAccountIndex);
|
|
205
|
+
return response;
|
|
206
|
+
}
|
|
174
207
|
return response;
|
|
175
208
|
}
|
|
176
209
|
catch (error) {
|
|
@@ -178,6 +211,9 @@ export const KimiRotatorPlugin = async ({ client }) => {
|
|
|
178
211
|
throw error;
|
|
179
212
|
}
|
|
180
213
|
}
|
|
214
|
+
if (lastResponse) {
|
|
215
|
+
return lastResponse;
|
|
216
|
+
}
|
|
181
217
|
}
|
|
182
218
|
if (!originalFetch) {
|
|
183
219
|
throw new Error('Original fetch not available');
|
package/dist/plugin.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"plugin.js","sourceRoot":"","sources":["../src/plugin.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,kBAAkB,EAAE,MAAM,eAAe,CAAC;AAQnD,MAAM,sBAAsB,GAAG;IAC7B,wDAAwD;IACxD,eAAe;IACf,sBAAsB;IACtB,gBAAgB;IAChB,sBAAsB;IACtB,kBAAkB;CACnB,CAAC;AAEF,MAAM,2BAA2B,GAAG;IAClC,wDAAwD;IACxD,gDAAgD;IAChD,oBAAoB;CACrB,CAAC;AAEF,MAAM,UAAU,GAAG,OAAO,CAAC,GAAG,CAAC,kBAAkB,KAAK,MAAM,CAAC;AAE7D,IAAI,cAAc,GAA8B,IAAI,CAAC;AACrD,IAAI,aAAa,GAAmC,IAAI,CAAC;AAEzD,SAAS,QAAQ,CAAC,OAAe,EAAE,IAAc;IAC/C,IAAI,UAAU,EAAE,CAAC;QACf,OAAO,CAAC,GAAG,CAAC,uBAAuB,OAAO,EAAE,EAAE,IAAI,IAAI,EAAE,CAAC,CAAC;IAC5D,CAAC;AACH,CAAC;AAED,KAAK,UAAU,iBAAiB;IAC9B,IAAI,CAAC,cAAc,EAAE,CAAC;QACpB,cAAc,GAAG,IAAI,kBAAkB,EAAE,CAAC;QAC1C,MAAM,cAAc,CAAC,IAAI,EAAE,CAAC;IAC9B,CAAC;IACD,OAAO,cAAc,CAAC;AACxB,CAAC;AAED,KAAK,UAAU,OAAO;IACpB,MAAM,OAAO,GAAG,MAAM,iBAAiB,EAAE,CAAC;IAC1C,+DAA+D;IAC/D,qEAAqE;IACrE,MAAM,MAAM,GAAG,MAAM,OAAO,CAAC,iBAAiB,EAAE,CAAC;IAEjD,IAAI,CAAC,MAAM,EAAE,CAAC;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,MAAM,IAAI,GAAG,MAAM,OAAO,EAAE,CAAC;gBAC7B,IAAI,CAAC,IAAI,EAAE,CAAC;oBACV,MAAM,IAAI,KAAK,CAAC,yDAAyD,CAAC,CAAC;gBAC7E,CAAC;gBAED,MAAM,WAAW,GAAG,gCAAgC,CAAC;gBAErD,OAAO;oBACL,MAAM,EAAE,IAAI,CAAC,GAAG;oBAChB,OAAO,EAAE,WAAW;oBACpB,KAAK,CAAC,KAAK,CAAC,KAA6B,EAAE,IAAkB;wBAC3D,OAAO,UAAU,CAAC,KAAK,CAAC,KAAK,EAAE,IAAI,CAAC,CAAC;oBACvC,CAAC;iBACF,CAAC;YACJ,CAAC;SACF;QACD,OAAO,EAAE,GAAG,EAAE;YACZ,IAAI,aAAa,EAAE,CAAC;gBAClB,UAAU,CAAC,KAAK,GAAG,aAAa,CAAC;gBACjC,aAAa,GAAG,IAAI,CAAC;YACvB,CAAC;YACD,cAAc,GAAG,IAAI,CAAC;YACtB,cAAc,GAAG,IAAI,CAAC;YACtB,UAAU,GAAG,EAAE,CAAC;QAClB,CAAC;KACF,CAAC;IAEF,OAAO,YAAY,CAAC;AACtB,CAAC,CAAC;AAEF,eAAe,iBAAiB,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "opencode-kimi-rotator",
|
|
3
|
-
"version": "1.2.
|
|
3
|
+
"version": "1.2.8",
|
|
4
4
|
"description": "Kimi API Key Rotator for OpenCode - Automatically rotate between multiple Kimi API keys to handle rate limits",
|
|
5
5
|
"main": "./dist/plugin.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
package/src/plugin.ts
CHANGED
|
@@ -79,6 +79,17 @@ function getRequestUrl(input: string | Request | URL): string {
|
|
|
79
79
|
return input.url;
|
|
80
80
|
}
|
|
81
81
|
|
|
82
|
+
function buildRequestHeaders(input: string | Request | URL, init?: RequestInit): Headers {
|
|
83
|
+
const headers = new Headers(input instanceof Request ? input.headers : undefined);
|
|
84
|
+
const initHeaders = new Headers(init?.headers);
|
|
85
|
+
|
|
86
|
+
initHeaders.forEach((value, key) => {
|
|
87
|
+
headers.set(key, value);
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
return headers;
|
|
91
|
+
}
|
|
92
|
+
|
|
82
93
|
async function showToast(message: string, variant: 'info' | 'warning' | 'error' = 'info') {
|
|
83
94
|
if (!openCodeClient?.tui?.showToast) {
|
|
84
95
|
toastQueue.push({ message, variant });
|
|
@@ -132,29 +143,37 @@ export const KimiRotatorPlugin: Plugin = async ({ client }) => {
|
|
|
132
143
|
if (!accountManager) {
|
|
133
144
|
throw new Error('Account manager not initialized');
|
|
134
145
|
}
|
|
135
|
-
|
|
136
|
-
if (
|
|
146
|
+
|
|
147
|
+
if (!originalFetch) {
|
|
148
|
+
throw new Error('Original fetch not available');
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
const allKeysNow = await accountManager.listKeys();
|
|
152
|
+
const total = allKeysNow.length;
|
|
153
|
+
const maxAttempts = Math.max(1, total);
|
|
154
|
+
let lastResponse: Response | null = null;
|
|
155
|
+
|
|
156
|
+
for (let attempt = 0; attempt < maxAttempts; attempt++) {
|
|
157
|
+
const nextAccount = await accountManager.getNextAccount();
|
|
158
|
+
if (!nextAccount) {
|
|
159
|
+
break;
|
|
160
|
+
}
|
|
161
|
+
|
|
137
162
|
const requestAccountIndex = nextAccount.index;
|
|
138
163
|
const keyLabel = nextAccount.account.key.substring(0, 18) + '...';
|
|
139
164
|
const position = requestAccountIndex + 1;
|
|
140
|
-
const
|
|
141
|
-
const total = allKeysNow.length;
|
|
142
|
-
|
|
143
|
-
await showToast(`🔄 Using key: ${keyLabel} (${String(position)}/${String(total)})`, 'info');
|
|
144
|
-
|
|
145
|
-
const headers = new Headers(init?.headers);
|
|
165
|
+
const headers = buildRequestHeaders(input, init);
|
|
146
166
|
headers.set('x-api-key', nextAccount.account.key);
|
|
147
167
|
headers.delete('Authorization');
|
|
148
168
|
|
|
169
|
+
await showToast(`🔄 Using key: ${keyLabel} (${String(position)}/${String(total)})`, 'info');
|
|
170
|
+
|
|
149
171
|
const requestStartTime = Date.now();
|
|
150
172
|
const today = new Date().toISOString().split('T')[0];
|
|
151
173
|
|
|
152
|
-
if (!originalFetch) {
|
|
153
|
-
throw new Error('Original fetch not available');
|
|
154
|
-
}
|
|
155
|
-
|
|
156
174
|
try {
|
|
157
|
-
const
|
|
175
|
+
const attemptInput = input instanceof Request ? input.clone() : input;
|
|
176
|
+
const response = await originalFetch(attemptInput, { ...init, headers });
|
|
158
177
|
const responseTime = Date.now() - requestStartTime;
|
|
159
178
|
|
|
160
179
|
if (response.status === 429) {
|
|
@@ -162,10 +181,17 @@ export const KimiRotatorPlugin: Plugin = async ({ client }) => {
|
|
|
162
181
|
const retryAfterMs = retryAfter ? parseInt(retryAfter, 10) * 1000 : 60000;
|
|
163
182
|
await accountManager.markAccountRateLimited(requestAccountIndex, retryAfterMs);
|
|
164
183
|
await showToast(`⚠️ Key ${String(position)} rate limited`, 'warning');
|
|
184
|
+
|
|
185
|
+
if (attempt < maxAttempts - 1) {
|
|
186
|
+
lastResponse = response;
|
|
187
|
+
continue;
|
|
188
|
+
}
|
|
165
189
|
} else if (response.ok) {
|
|
166
190
|
await accountManager.markAccountSuccess(requestAccountIndex, responseTime, today);
|
|
191
|
+
return response;
|
|
167
192
|
} else if (response.status >= 500) {
|
|
168
193
|
await accountManager.markAccountFailure(requestAccountIndex);
|
|
194
|
+
return response;
|
|
169
195
|
} else if (response.status === 400 || response.status === 403) {
|
|
170
196
|
const clonedResponse = response.clone();
|
|
171
197
|
try {
|
|
@@ -184,8 +210,11 @@ export const KimiRotatorPlugin: Plugin = async ({ client }) => {
|
|
|
184
210
|
);
|
|
185
211
|
|
|
186
212
|
if (isBillingLimit && accountManager) {
|
|
213
|
+
let shouldRetry = false;
|
|
214
|
+
|
|
187
215
|
if (isHardBillingLimit) {
|
|
188
216
|
await accountManager.markAccountBillingLimited(requestAccountIndex);
|
|
217
|
+
shouldRetry = true;
|
|
189
218
|
const resetTime = new Date();
|
|
190
219
|
resetTime.setDate(resetTime.getDate() + 1);
|
|
191
220
|
resetTime.setHours(0, 0, 0, 0);
|
|
@@ -198,6 +227,7 @@ export const KimiRotatorPlugin: Plugin = async ({ client }) => {
|
|
|
198
227
|
);
|
|
199
228
|
} else {
|
|
200
229
|
const result = await accountManager.recordBillingLimitHit(requestAccountIndex);
|
|
230
|
+
shouldRetry = result.isConfirmed;
|
|
201
231
|
if (result.isConfirmed) {
|
|
202
232
|
const resetTime = new Date();
|
|
203
233
|
resetTime.setDate(resetTime.getDate() + 1);
|
|
@@ -215,14 +245,27 @@ export const KimiRotatorPlugin: Plugin = async ({ client }) => {
|
|
|
215
245
|
);
|
|
216
246
|
}
|
|
217
247
|
}
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
248
|
+
|
|
249
|
+
if (shouldRetry && attempt < maxAttempts - 1) {
|
|
250
|
+
await showToast('🔁 Retrying with next key...', 'info');
|
|
251
|
+
lastResponse = response;
|
|
252
|
+
continue;
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
return response;
|
|
222
256
|
}
|
|
257
|
+
|
|
258
|
+
debugLog(`HTTP ${response.status} but not billing limit for key ${String(position)}`);
|
|
259
|
+
await accountManager.resetBillingLimitHits(requestAccountIndex);
|
|
260
|
+
await accountManager.markAccountFailure(requestAccountIndex);
|
|
261
|
+
return response;
|
|
223
262
|
} catch (error) {
|
|
224
263
|
debugLog('Error reading response body:', error);
|
|
264
|
+
return response;
|
|
225
265
|
}
|
|
266
|
+
} else {
|
|
267
|
+
await accountManager.markAccountFailure(requestAccountIndex);
|
|
268
|
+
return response;
|
|
226
269
|
}
|
|
227
270
|
|
|
228
271
|
return response;
|
|
@@ -231,6 +274,10 @@ export const KimiRotatorPlugin: Plugin = async ({ client }) => {
|
|
|
231
274
|
throw error;
|
|
232
275
|
}
|
|
233
276
|
}
|
|
277
|
+
|
|
278
|
+
if (lastResponse) {
|
|
279
|
+
return lastResponse;
|
|
280
|
+
}
|
|
234
281
|
}
|
|
235
282
|
|
|
236
283
|
if (!originalFetch) {
|