@azumag/opencode-rate-limit-fallback 1.0.19 → 1.0.21
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/index.js +17 -14
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -214,7 +214,7 @@ export const RateLimitFallback = async ({ client, directory }) => {
|
|
|
214
214
|
}
|
|
215
215
|
return null;
|
|
216
216
|
}
|
|
217
|
-
async function handleRateLimitFallback(sessionID, currentProviderID, currentModelID) {
|
|
217
|
+
async function handleRateLimitFallback(sessionID, currentProviderID, currentModelID, skipAbort = false) {
|
|
218
218
|
try {
|
|
219
219
|
// Prevent duplicate fallback processing within 5 seconds
|
|
220
220
|
const lastFallback = fallbackInProgress.get(sessionID);
|
|
@@ -316,21 +316,22 @@ export const RateLimitFallback = async ({ client, directory }) => {
|
|
|
316
316
|
parts: parts,
|
|
317
317
|
model: { providerID: nextModel.providerID, modelID: nextModel.modelID },
|
|
318
318
|
};
|
|
319
|
-
// abort → promptAsync:
|
|
320
|
-
//
|
|
319
|
+
// abort → promptAsync: abort the old request first, then send fallback prompt
|
|
320
|
+
// This prevents the abort from killing the new fallback prompt.
|
|
321
321
|
//
|
|
322
|
-
//
|
|
323
|
-
//
|
|
324
|
-
// dispose sequence interrupts the new prompt before it can complete.
|
|
322
|
+
// For session.error events, the request is already in error state, so we
|
|
323
|
+
// only send promptAsync (skipAbort = true). For retry status, we send both.
|
|
325
324
|
//
|
|
326
325
|
// promptAsync: HTTP POST /session/{id}/prompt_async → 204 (SDK sdk.gen.js).
|
|
327
326
|
const t0 = Date.now();
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
327
|
+
if (!skipAbort) {
|
|
328
|
+
try {
|
|
329
|
+
await client.session.abort({ path: { id: sessionID } });
|
|
330
|
+
logToFile(`abort succeeded for session ${sessionID} (${Date.now() - t0}ms)`);
|
|
331
|
+
}
|
|
332
|
+
catch (abortErr) {
|
|
333
|
+
logToFile(`abort failed (${Date.now() - t0}ms): ${abortErr}`);
|
|
334
|
+
}
|
|
334
335
|
}
|
|
335
336
|
const t1 = Date.now();
|
|
336
337
|
await client.session.promptAsync({
|
|
@@ -399,7 +400,7 @@ export const RateLimitFallback = async ({ client, directory }) => {
|
|
|
399
400
|
catch {
|
|
400
401
|
console.log("[rate-limit-fallback] Rate limit error detected, attempting fallback");
|
|
401
402
|
}
|
|
402
|
-
await handleRateLimitFallback(sessionID, "", "");
|
|
403
|
+
await handleRateLimitFallback(sessionID, "", "", true); // skipAbort = true (already in error state)
|
|
403
404
|
}
|
|
404
405
|
}
|
|
405
406
|
if (event.type === "message.updated") {
|
|
@@ -438,7 +439,7 @@ export const RateLimitFallback = async ({ client, directory }) => {
|
|
|
438
439
|
catch {
|
|
439
440
|
console.log("[rate-limit-fallback] Rate limit error in message, attempting fallback");
|
|
440
441
|
}
|
|
441
|
-
await handleRateLimitFallback(info.sessionID, info.providerID || "", info.modelID || "");
|
|
442
|
+
await handleRateLimitFallback(info.sessionID, info.providerID || "", info.modelID || "", true); // skipAbort = true (already in error state)
|
|
442
443
|
}
|
|
443
444
|
}
|
|
444
445
|
if (event.type === "session.status") {
|
|
@@ -457,6 +458,8 @@ export const RateLimitFallback = async ({ client, directory }) => {
|
|
|
457
458
|
catch {
|
|
458
459
|
console.log("[rate-limit-fallback] session.status:", status);
|
|
459
460
|
}
|
|
461
|
+
// Note: interrupted status is handled by the fallback sequence (abort → promptAsync).
|
|
462
|
+
// We don't need to handle it separately here as it would cause duplicate prompt sends.
|
|
460
463
|
if (status?.type === "retry" && status?.message) {
|
|
461
464
|
const message = status.message.toLowerCase();
|
|
462
465
|
const isRateLimitRetry = message.includes("usage limit") ||
|
package/package.json
CHANGED