opencode-kimi-rotator 1.2.6 → 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 +60 -15
- package/dist/plugin.d.ts.map +1 -1
- package/dist/plugin.js +61 -16
- package/dist/plugin.js.map +1 -1
- package/package.json +1 -1
- package/src/plugin.ts +77 -18
package/dist/plugin.bundle.js
CHANGED
|
@@ -6263,6 +6263,23 @@ async function getAuth() {
|
|
|
6263
6263
|
}
|
|
6264
6264
|
var openCodeClient = null;
|
|
6265
6265
|
var toastQueue = [];
|
|
6266
|
+
function getRequestUrl(input) {
|
|
6267
|
+
if (typeof input === "string") {
|
|
6268
|
+
return input;
|
|
6269
|
+
}
|
|
6270
|
+
if (input instanceof URL) {
|
|
6271
|
+
return input.toString();
|
|
6272
|
+
}
|
|
6273
|
+
return input.url;
|
|
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
|
+
}
|
|
6266
6283
|
async function showToast(message, variant = "info") {
|
|
6267
6284
|
if (!openCodeClient?.tui?.showToast) {
|
|
6268
6285
|
toastQueue.push({ message, variant });
|
|
@@ -6300,39 +6317,51 @@ var KimiRotatorPlugin = async ({ client }) => {
|
|
|
6300
6317
|
}
|
|
6301
6318
|
originalFetch = globalThis.fetch;
|
|
6302
6319
|
globalThis.fetch = async (input, init) => {
|
|
6303
|
-
const url =
|
|
6320
|
+
const url = getRequestUrl(input);
|
|
6304
6321
|
if (url.includes("api.kimi.com")) {
|
|
6305
6322
|
if (!accountManager) {
|
|
6306
6323
|
throw new Error("Account manager not initialized");
|
|
6307
6324
|
}
|
|
6308
|
-
|
|
6309
|
-
|
|
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
|
+
}
|
|
6310
6337
|
const requestAccountIndex = nextAccount.index;
|
|
6311
6338
|
const keyLabel = nextAccount.account.key.substring(0, 18) + "...";
|
|
6312
6339
|
const position = requestAccountIndex + 1;
|
|
6313
|
-
const
|
|
6314
|
-
const total = allKeysNow.length;
|
|
6315
|
-
await showToast(`\u{1F504} Using key: ${keyLabel} (${String(position)}/${String(total)})`, "info");
|
|
6316
|
-
const headers = new Headers(init?.headers);
|
|
6340
|
+
const headers = buildRequestHeaders(input, init);
|
|
6317
6341
|
headers.set("x-api-key", nextAccount.account.key);
|
|
6318
6342
|
headers.delete("Authorization");
|
|
6343
|
+
await showToast(`\u{1F504} Using key: ${keyLabel} (${String(position)}/${String(total)})`, "info");
|
|
6319
6344
|
const requestStartTime = Date.now();
|
|
6320
6345
|
const today = (/* @__PURE__ */ new Date()).toISOString().split("T")[0];
|
|
6321
|
-
if (!originalFetch) {
|
|
6322
|
-
throw new Error("Original fetch not available");
|
|
6323
|
-
}
|
|
6324
6346
|
try {
|
|
6325
|
-
const
|
|
6347
|
+
const attemptInput = input instanceof Request ? input.clone() : input;
|
|
6348
|
+
const response = await originalFetch(attemptInput, { ...init, headers });
|
|
6326
6349
|
const responseTime = Date.now() - requestStartTime;
|
|
6327
6350
|
if (response.status === 429) {
|
|
6328
6351
|
const retryAfter = response.headers.get("retry-after");
|
|
6329
6352
|
const retryAfterMs = retryAfter ? parseInt(retryAfter, 10) * 1e3 : 6e4;
|
|
6330
6353
|
await accountManager.markAccountRateLimited(requestAccountIndex, retryAfterMs);
|
|
6331
6354
|
await showToast(`\u26A0\uFE0F Key ${String(position)} rate limited`, "warning");
|
|
6355
|
+
if (attempt < maxAttempts - 1) {
|
|
6356
|
+
lastResponse = response;
|
|
6357
|
+
continue;
|
|
6358
|
+
}
|
|
6332
6359
|
} else if (response.ok) {
|
|
6333
6360
|
await accountManager.markAccountSuccess(requestAccountIndex, responseTime, today);
|
|
6361
|
+
return response;
|
|
6334
6362
|
} else if (response.status >= 500) {
|
|
6335
6363
|
await accountManager.markAccountFailure(requestAccountIndex);
|
|
6364
|
+
return response;
|
|
6336
6365
|
} else if (response.status === 400 || response.status === 403) {
|
|
6337
6366
|
const clonedResponse = response.clone();
|
|
6338
6367
|
try {
|
|
@@ -6348,8 +6377,10 @@ var KimiRotatorPlugin = async ({ client }) => {
|
|
|
6348
6377
|
(pattern) => bodyLower.includes(pattern)
|
|
6349
6378
|
);
|
|
6350
6379
|
if (isBillingLimit && accountManager) {
|
|
6380
|
+
let shouldRetry = false;
|
|
6351
6381
|
if (isHardBillingLimit) {
|
|
6352
6382
|
await accountManager.markAccountBillingLimited(requestAccountIndex);
|
|
6383
|
+
shouldRetry = true;
|
|
6353
6384
|
const resetTime = /* @__PURE__ */ new Date();
|
|
6354
6385
|
resetTime.setDate(resetTime.getDate() + 1);
|
|
6355
6386
|
resetTime.setHours(0, 0, 0, 0);
|
|
@@ -6362,6 +6393,7 @@ var KimiRotatorPlugin = async ({ client }) => {
|
|
|
6362
6393
|
);
|
|
6363
6394
|
} else {
|
|
6364
6395
|
const result2 = await accountManager.recordBillingLimitHit(requestAccountIndex);
|
|
6396
|
+
shouldRetry = result2.isConfirmed;
|
|
6365
6397
|
if (result2.isConfirmed) {
|
|
6366
6398
|
const resetTime = /* @__PURE__ */ new Date();
|
|
6367
6399
|
resetTime.setDate(resetTime.getDate() + 1);
|
|
@@ -6379,14 +6411,24 @@ var KimiRotatorPlugin = async ({ client }) => {
|
|
|
6379
6411
|
);
|
|
6380
6412
|
}
|
|
6381
6413
|
}
|
|
6382
|
-
|
|
6383
|
-
|
|
6384
|
-
|
|
6385
|
-
|
|
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;
|
|
6386
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;
|
|
6387
6425
|
} catch (error) {
|
|
6388
6426
|
debugLog("Error reading response body:", error);
|
|
6427
|
+
return response;
|
|
6389
6428
|
}
|
|
6429
|
+
} else {
|
|
6430
|
+
await accountManager.markAccountFailure(requestAccountIndex);
|
|
6431
|
+
return response;
|
|
6390
6432
|
}
|
|
6391
6433
|
return response;
|
|
6392
6434
|
} catch (error) {
|
|
@@ -6394,6 +6436,9 @@ var KimiRotatorPlugin = async ({ client }) => {
|
|
|
6394
6436
|
throw error;
|
|
6395
6437
|
}
|
|
6396
6438
|
}
|
|
6439
|
+
if (lastResponse) {
|
|
6440
|
+
return lastResponse;
|
|
6441
|
+
}
|
|
6397
6442
|
}
|
|
6398
6443
|
if (!originalFetch) {
|
|
6399
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
|
@@ -42,6 +42,23 @@ async function getAuth() {
|
|
|
42
42
|
}
|
|
43
43
|
let openCodeClient = null;
|
|
44
44
|
let toastQueue = [];
|
|
45
|
+
function getRequestUrl(input) {
|
|
46
|
+
if (typeof input === 'string') {
|
|
47
|
+
return input;
|
|
48
|
+
}
|
|
49
|
+
if (input instanceof URL) {
|
|
50
|
+
return input.toString();
|
|
51
|
+
}
|
|
52
|
+
return input.url;
|
|
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
|
+
}
|
|
45
62
|
async function showToast(message, variant = 'info') {
|
|
46
63
|
if (!openCodeClient?.tui?.showToast) {
|
|
47
64
|
toastQueue.push({ message, variant });
|
|
@@ -83,41 +100,53 @@ export const KimiRotatorPlugin = async ({ client }) => {
|
|
|
83
100
|
}
|
|
84
101
|
originalFetch = globalThis.fetch;
|
|
85
102
|
globalThis.fetch = async (input, init) => {
|
|
86
|
-
const url =
|
|
103
|
+
const url = getRequestUrl(input);
|
|
87
104
|
if (url.includes('api.kimi.com')) {
|
|
88
105
|
if (!accountManager) {
|
|
89
106
|
throw new Error('Account manager not initialized');
|
|
90
107
|
}
|
|
91
|
-
|
|
92
|
-
|
|
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
|
+
}
|
|
93
120
|
const requestAccountIndex = nextAccount.index;
|
|
94
121
|
const keyLabel = nextAccount.account.key.substring(0, 18) + '...';
|
|
95
122
|
const position = requestAccountIndex + 1;
|
|
96
|
-
const
|
|
97
|
-
const total = allKeysNow.length;
|
|
98
|
-
await showToast(`🔄 Using key: ${keyLabel} (${String(position)}/${String(total)})`, 'info');
|
|
99
|
-
const headers = new Headers(init?.headers);
|
|
123
|
+
const headers = buildRequestHeaders(input, init);
|
|
100
124
|
headers.set('x-api-key', nextAccount.account.key);
|
|
101
125
|
headers.delete('Authorization');
|
|
126
|
+
await showToast(`🔄 Using key: ${keyLabel} (${String(position)}/${String(total)})`, 'info');
|
|
102
127
|
const requestStartTime = Date.now();
|
|
103
128
|
const today = new Date().toISOString().split('T')[0];
|
|
104
|
-
if (!originalFetch) {
|
|
105
|
-
throw new Error('Original fetch not available');
|
|
106
|
-
}
|
|
107
129
|
try {
|
|
108
|
-
const
|
|
130
|
+
const attemptInput = input instanceof Request ? input.clone() : input;
|
|
131
|
+
const response = await originalFetch(attemptInput, { ...init, headers });
|
|
109
132
|
const responseTime = Date.now() - requestStartTime;
|
|
110
133
|
if (response.status === 429) {
|
|
111
134
|
const retryAfter = response.headers.get('retry-after');
|
|
112
135
|
const retryAfterMs = retryAfter ? parseInt(retryAfter, 10) * 1000 : 60000;
|
|
113
136
|
await accountManager.markAccountRateLimited(requestAccountIndex, retryAfterMs);
|
|
114
137
|
await showToast(`⚠️ Key ${String(position)} rate limited`, 'warning');
|
|
138
|
+
if (attempt < maxAttempts - 1) {
|
|
139
|
+
lastResponse = response;
|
|
140
|
+
continue;
|
|
141
|
+
}
|
|
115
142
|
}
|
|
116
143
|
else if (response.ok) {
|
|
117
144
|
await accountManager.markAccountSuccess(requestAccountIndex, responseTime, today);
|
|
145
|
+
return response;
|
|
118
146
|
}
|
|
119
147
|
else if (response.status >= 500) {
|
|
120
148
|
await accountManager.markAccountFailure(requestAccountIndex);
|
|
149
|
+
return response;
|
|
121
150
|
}
|
|
122
151
|
else if (response.status === 400 || response.status === 403) {
|
|
123
152
|
const clonedResponse = response.clone();
|
|
@@ -130,8 +159,10 @@ export const KimiRotatorPlugin = async ({ client }) => {
|
|
|
130
159
|
const isBillingLimit = BILLING_LIMIT_PATTERNS.some((pattern) => bodyLower.includes(pattern));
|
|
131
160
|
const isHardBillingLimit = HARD_BILLING_LIMIT_PATTERNS.some((pattern) => bodyLower.includes(pattern));
|
|
132
161
|
if (isBillingLimit && accountManager) {
|
|
162
|
+
let shouldRetry = false;
|
|
133
163
|
if (isHardBillingLimit) {
|
|
134
164
|
await accountManager.markAccountBillingLimited(requestAccountIndex);
|
|
165
|
+
shouldRetry = true;
|
|
135
166
|
const resetTime = new Date();
|
|
136
167
|
resetTime.setDate(resetTime.getDate() + 1);
|
|
137
168
|
resetTime.setHours(0, 0, 0, 0);
|
|
@@ -140,6 +171,7 @@ export const KimiRotatorPlugin = async ({ client }) => {
|
|
|
140
171
|
}
|
|
141
172
|
else {
|
|
142
173
|
const result = await accountManager.recordBillingLimitHit(requestAccountIndex);
|
|
174
|
+
shouldRetry = result.isConfirmed;
|
|
143
175
|
if (result.isConfirmed) {
|
|
144
176
|
const resetTime = new Date();
|
|
145
177
|
resetTime.setDate(resetTime.getDate() + 1);
|
|
@@ -151,17 +183,27 @@ export const KimiRotatorPlugin = async ({ client }) => {
|
|
|
151
183
|
debugLog(`Billing limit hit ${2 - result.hitsNeeded}/2 for key ${String(position)} - not confirmed yet`);
|
|
152
184
|
}
|
|
153
185
|
}
|
|
186
|
+
if (shouldRetry && attempt < maxAttempts - 1) {
|
|
187
|
+
await showToast('🔁 Retrying with next key...', 'info');
|
|
188
|
+
lastResponse = response;
|
|
189
|
+
continue;
|
|
190
|
+
}
|
|
191
|
+
return response;
|
|
154
192
|
}
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
}
|
|
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;
|
|
160
197
|
}
|
|
161
198
|
catch (error) {
|
|
162
199
|
debugLog('Error reading response body:', error);
|
|
200
|
+
return response;
|
|
163
201
|
}
|
|
164
202
|
}
|
|
203
|
+
else {
|
|
204
|
+
await accountManager.markAccountFailure(requestAccountIndex);
|
|
205
|
+
return response;
|
|
206
|
+
}
|
|
165
207
|
return response;
|
|
166
208
|
}
|
|
167
209
|
catch (error) {
|
|
@@ -169,6 +211,9 @@ export const KimiRotatorPlugin = async ({ client }) => {
|
|
|
169
211
|
throw error;
|
|
170
212
|
}
|
|
171
213
|
}
|
|
214
|
+
if (lastResponse) {
|
|
215
|
+
return lastResponse;
|
|
216
|
+
}
|
|
172
217
|
}
|
|
173
218
|
if (!originalFetch) {
|
|
174
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,UA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C;IAEF,gBAAgB,EAAE,CAAC,KAAK,CAAC,GAAG,EAAE,CAAC,SAAS,CAAC,CAAC;IAE1C,MAAM,YAAY,GAAG;QACnB,IAAI,EAAE;YACJ,QAAQ,EAAE,cAAc;YACxB,OAAO,EAAE;gBACP;oBACE,IAAI,EAAE,KAAc;oBACpB,KAAK,EAAE,cAAc;iBACtB;aACF;YACD,MAAM,EAAE,KAAK,IAAI,EAAE;gBACjB,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
|
@@ -67,6 +67,29 @@ let openCodeClient: {
|
|
|
67
67
|
|
|
68
68
|
let toastQueue: { message: string; variant: 'info' | 'warning' | 'error' }[] = [];
|
|
69
69
|
|
|
70
|
+
function getRequestUrl(input: string | Request | URL): string {
|
|
71
|
+
if (typeof input === 'string') {
|
|
72
|
+
return input;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
if (input instanceof URL) {
|
|
76
|
+
return input.toString();
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
return input.url;
|
|
80
|
+
}
|
|
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
|
+
|
|
70
93
|
async function showToast(message: string, variant: 'info' | 'warning' | 'error' = 'info') {
|
|
71
94
|
if (!openCodeClient?.tui?.showToast) {
|
|
72
95
|
toastQueue.push({ message, variant });
|
|
@@ -114,35 +137,43 @@ export const KimiRotatorPlugin: Plugin = async ({ client }) => {
|
|
|
114
137
|
input: string | Request | URL,
|
|
115
138
|
init?: RequestInit
|
|
116
139
|
): Promise<Response> => {
|
|
117
|
-
const url =
|
|
140
|
+
const url = getRequestUrl(input);
|
|
118
141
|
|
|
119
142
|
if (url.includes('api.kimi.com')) {
|
|
120
143
|
if (!accountManager) {
|
|
121
144
|
throw new Error('Account manager not initialized');
|
|
122
145
|
}
|
|
123
|
-
|
|
124
|
-
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
|
+
|
|
125
162
|
const requestAccountIndex = nextAccount.index;
|
|
126
163
|
const keyLabel = nextAccount.account.key.substring(0, 18) + '...';
|
|
127
164
|
const position = requestAccountIndex + 1;
|
|
128
|
-
const
|
|
129
|
-
const total = allKeysNow.length;
|
|
130
|
-
|
|
131
|
-
await showToast(`🔄 Using key: ${keyLabel} (${String(position)}/${String(total)})`, 'info');
|
|
132
|
-
|
|
133
|
-
const headers = new Headers(init?.headers);
|
|
165
|
+
const headers = buildRequestHeaders(input, init);
|
|
134
166
|
headers.set('x-api-key', nextAccount.account.key);
|
|
135
167
|
headers.delete('Authorization');
|
|
136
168
|
|
|
169
|
+
await showToast(`🔄 Using key: ${keyLabel} (${String(position)}/${String(total)})`, 'info');
|
|
170
|
+
|
|
137
171
|
const requestStartTime = Date.now();
|
|
138
172
|
const today = new Date().toISOString().split('T')[0];
|
|
139
173
|
|
|
140
|
-
if (!originalFetch) {
|
|
141
|
-
throw new Error('Original fetch not available');
|
|
142
|
-
}
|
|
143
|
-
|
|
144
174
|
try {
|
|
145
|
-
const
|
|
175
|
+
const attemptInput = input instanceof Request ? input.clone() : input;
|
|
176
|
+
const response = await originalFetch(attemptInput, { ...init, headers });
|
|
146
177
|
const responseTime = Date.now() - requestStartTime;
|
|
147
178
|
|
|
148
179
|
if (response.status === 429) {
|
|
@@ -150,10 +181,17 @@ export const KimiRotatorPlugin: Plugin = async ({ client }) => {
|
|
|
150
181
|
const retryAfterMs = retryAfter ? parseInt(retryAfter, 10) * 1000 : 60000;
|
|
151
182
|
await accountManager.markAccountRateLimited(requestAccountIndex, retryAfterMs);
|
|
152
183
|
await showToast(`⚠️ Key ${String(position)} rate limited`, 'warning');
|
|
184
|
+
|
|
185
|
+
if (attempt < maxAttempts - 1) {
|
|
186
|
+
lastResponse = response;
|
|
187
|
+
continue;
|
|
188
|
+
}
|
|
153
189
|
} else if (response.ok) {
|
|
154
190
|
await accountManager.markAccountSuccess(requestAccountIndex, responseTime, today);
|
|
191
|
+
return response;
|
|
155
192
|
} else if (response.status >= 500) {
|
|
156
193
|
await accountManager.markAccountFailure(requestAccountIndex);
|
|
194
|
+
return response;
|
|
157
195
|
} else if (response.status === 400 || response.status === 403) {
|
|
158
196
|
const clonedResponse = response.clone();
|
|
159
197
|
try {
|
|
@@ -172,8 +210,11 @@ export const KimiRotatorPlugin: Plugin = async ({ client }) => {
|
|
|
172
210
|
);
|
|
173
211
|
|
|
174
212
|
if (isBillingLimit && accountManager) {
|
|
213
|
+
let shouldRetry = false;
|
|
214
|
+
|
|
175
215
|
if (isHardBillingLimit) {
|
|
176
216
|
await accountManager.markAccountBillingLimited(requestAccountIndex);
|
|
217
|
+
shouldRetry = true;
|
|
177
218
|
const resetTime = new Date();
|
|
178
219
|
resetTime.setDate(resetTime.getDate() + 1);
|
|
179
220
|
resetTime.setHours(0, 0, 0, 0);
|
|
@@ -186,6 +227,7 @@ export const KimiRotatorPlugin: Plugin = async ({ client }) => {
|
|
|
186
227
|
);
|
|
187
228
|
} else {
|
|
188
229
|
const result = await accountManager.recordBillingLimitHit(requestAccountIndex);
|
|
230
|
+
shouldRetry = result.isConfirmed;
|
|
189
231
|
if (result.isConfirmed) {
|
|
190
232
|
const resetTime = new Date();
|
|
191
233
|
resetTime.setDate(resetTime.getDate() + 1);
|
|
@@ -203,14 +245,27 @@ export const KimiRotatorPlugin: Plugin = async ({ client }) => {
|
|
|
203
245
|
);
|
|
204
246
|
}
|
|
205
247
|
}
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
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;
|
|
210
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;
|
|
211
262
|
} catch (error) {
|
|
212
263
|
debugLog('Error reading response body:', error);
|
|
264
|
+
return response;
|
|
213
265
|
}
|
|
266
|
+
} else {
|
|
267
|
+
await accountManager.markAccountFailure(requestAccountIndex);
|
|
268
|
+
return response;
|
|
214
269
|
}
|
|
215
270
|
|
|
216
271
|
return response;
|
|
@@ -219,6 +274,10 @@ export const KimiRotatorPlugin: Plugin = async ({ client }) => {
|
|
|
219
274
|
throw error;
|
|
220
275
|
}
|
|
221
276
|
}
|
|
277
|
+
|
|
278
|
+
if (lastResponse) {
|
|
279
|
+
return lastResponse;
|
|
280
|
+
}
|
|
222
281
|
}
|
|
223
282
|
|
|
224
283
|
if (!originalFetch) {
|