Fix Combo

This commit is contained in:
decolua
2026-01-14 14:55:47 +07:00
parent f9ef718fc6
commit c39eca6d4e
5 changed files with 113 additions and 26 deletions

View File

@@ -2,7 +2,7 @@ import { COOLDOWN_MS, BACKOFF_CONFIG } from "../config/constants.js";
/**
* Calculate exponential backoff cooldown for rate limits (429)
* Level 0: 1s, Level 1: 2s, Level 2: 4s... → max 30 min
* Level 0: 1s, Level 1: 2s, Level 2: 4s... → max 2 min
* @param {number} backoffLevel - Current backoff level
* @returns {number} Cooldown in milliseconds
*/
@@ -22,12 +22,12 @@ export function checkFallbackError(status, errorText, backoffLevel = 0) {
// Check error message FIRST - specific patterns take priority over status codes
if (errorText) {
const lowerError = errorText.toLowerCase();
// "Request not allowed" - short cooldown (5s), takes priority over status code
if (lowerError.includes("request not allowed")) {
return { shouldFallback: true, cooldownMs: COOLDOWN_MS.requestNotAllowed };
}
// Rate limit keywords - exponential backoff
if (
lowerError.includes("rate limit") ||
@@ -37,8 +37,8 @@ export function checkFallbackError(status, errorText, backoffLevel = 0) {
lowerError.includes("overloaded")
) {
const newLevel = Math.min(backoffLevel + 1, BACKOFF_CONFIG.maxLevel);
return {
shouldFallback: true,
return {
shouldFallback: true,
cooldownMs: getQuotaCooldown(backoffLevel),
newBackoffLevel: newLevel
};
@@ -63,8 +63,8 @@ export function checkFallbackError(status, errorText, backoffLevel = 0) {
// 429 - Rate limit with exponential backoff
if (status === 429) {
const newLevel = Math.min(backoffLevel + 1, BACKOFF_CONFIG.maxLevel);
return {
shouldFallback: true,
return {
shouldFallback: true,
cooldownMs: getQuotaCooldown(backoffLevel),
newBackoffLevel: newLevel
};
@@ -134,10 +134,10 @@ export function resetAccountState(account) {
*/
export function applyErrorState(account, status, errorText) {
if (!account) return account;
const backoffLevel = account.backoffLevel || 0;
const { cooldownMs, newBackoffLevel } = checkFallbackError(status, errorText, backoffLevel);
return {
...account,
rateLimitedUntil: cooldownMs > 0 ? getUnavailableUntil(cooldownMs) : null,

View File

@@ -2,6 +2,8 @@
* Shared combo (model combo) handling with fallback support
*/
import { checkFallbackError } from "./accountFallback.js";
/**
* Get combo models from combos data
* @param {string} modelStr - Model string to check
@@ -42,20 +44,34 @@ export async function handleComboChat({ body, models, handleSingleModel, log })
// Success (2xx) - return response
if (result.ok) {
log.info("COMBO", `Model ${modelStr} succeeded`);
return result;
}
// 401 unauthorized - return immediately (auth error)
if (result.status === 401) {
// Extract error message from response
let errorText = result.statusText || "";
try {
const errorBody = await result.clone().json();
errorText = errorBody.error || errorBody.message || errorText;
} catch {
// Ignore JSON parse errors
}
// Check if should fallback to next model
const { shouldFallback } = checkFallbackError(result.status, errorText);
if (!shouldFallback) {
// Don't fallback - return error immediately (e.g. 401 auth errors)
log.warn("COMBO", `Model ${modelStr} failed (no fallback)`, { status: result.status });
return result;
}
// 4xx/5xx - try next model
lastError = `${modelStr}: ${result.statusText || result.status}`;
log.warn("COMBO", `Model failed, trying next`, { model: modelStr, status: result.status });
// Fallback to next model
lastError = `${modelStr}: ${errorText || result.status}`;
log.warn("COMBO", `Model ${modelStr} failed, trying next`, { status: result.status, error: errorText.slice(0, 100) });
}
log.warn("COMBO", "All models failed");
log.warn("COMBO", "All combo models failed");
// Return 503 with last error
return new Response(