@blockrun/clawrouter 0.6.1 → 0.6.3
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 +32 -0
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -2320,6 +2320,34 @@ var DEFAULT_REQUEST_TIMEOUT_MS = 18e4;
|
|
|
2320
2320
|
var DEFAULT_PORT = 8402;
|
|
2321
2321
|
var MAX_FALLBACK_ATTEMPTS = 3;
|
|
2322
2322
|
var HEALTH_CHECK_TIMEOUT_MS = 2e3;
|
|
2323
|
+
var RATE_LIMIT_COOLDOWN_MS = 6e4;
|
|
2324
|
+
var rateLimitedModels = /* @__PURE__ */ new Map();
|
|
2325
|
+
function isRateLimited(modelId) {
|
|
2326
|
+
const hitTime = rateLimitedModels.get(modelId);
|
|
2327
|
+
if (!hitTime) return false;
|
|
2328
|
+
const elapsed = Date.now() - hitTime;
|
|
2329
|
+
if (elapsed >= RATE_LIMIT_COOLDOWN_MS) {
|
|
2330
|
+
rateLimitedModels.delete(modelId);
|
|
2331
|
+
return false;
|
|
2332
|
+
}
|
|
2333
|
+
return true;
|
|
2334
|
+
}
|
|
2335
|
+
function markRateLimited(modelId) {
|
|
2336
|
+
rateLimitedModels.set(modelId, Date.now());
|
|
2337
|
+
console.log(`[ClawRouter] Model ${modelId} rate-limited, will deprioritize for 60s`);
|
|
2338
|
+
}
|
|
2339
|
+
function prioritizeNonRateLimited(models) {
|
|
2340
|
+
const available = [];
|
|
2341
|
+
const rateLimited = [];
|
|
2342
|
+
for (const model of models) {
|
|
2343
|
+
if (isRateLimited(model)) {
|
|
2344
|
+
rateLimited.push(model);
|
|
2345
|
+
} else {
|
|
2346
|
+
available.push(model);
|
|
2347
|
+
}
|
|
2348
|
+
}
|
|
2349
|
+
return [...available, ...rateLimited];
|
|
2350
|
+
}
|
|
2323
2351
|
var BALANCE_CHECK_BUFFER = 1.5;
|
|
2324
2352
|
function getProxyPort() {
|
|
2325
2353
|
const envPort = process.env.BLOCKRUN_PROXY_PORT;
|
|
@@ -2909,6 +2937,7 @@ async function proxyRequest(req, res, apiBase, payFetch, options, routerOpts, de
|
|
|
2909
2937
|
);
|
|
2910
2938
|
}
|
|
2911
2939
|
modelsToTry = contextFiltered.slice(0, MAX_FALLBACK_ATTEMPTS);
|
|
2940
|
+
modelsToTry = prioritizeNonRateLimited(modelsToTry);
|
|
2912
2941
|
} else {
|
|
2913
2942
|
modelsToTry = modelId ? [modelId] : [];
|
|
2914
2943
|
}
|
|
@@ -2941,6 +2970,9 @@ async function proxyRequest(req, res, apiBase, payFetch, options, routerOpts, de
|
|
|
2941
2970
|
status: result.errorStatus || 500
|
|
2942
2971
|
};
|
|
2943
2972
|
if (result.isProviderError && !isLastAttempt) {
|
|
2973
|
+
if (result.errorStatus === 429) {
|
|
2974
|
+
markRateLimited(tryModel);
|
|
2975
|
+
}
|
|
2944
2976
|
console.log(
|
|
2945
2977
|
`[ClawRouter] Provider error from ${tryModel}, trying fallback: ${result.errorBody?.slice(0, 100)}`
|
|
2946
2978
|
);
|