feat(provider): add free providers and enhance error handling

This commit is contained in:
decolua
2026-02-07 11:17:06 +07:00
parent 53a5f43993
commit bdbe8162e7
16 changed files with 285 additions and 120 deletions
+52 -13
View File
@@ -1,4 +1,4 @@
import { COOLDOWN_MS, BACKOFF_CONFIG } from "../config/constants.js";
import { COOLDOWN_MS, BACKOFF_CONFIG, HTTP_STATUS } from "../config/constants.js";
/**
* Calculate exponential backoff cooldown for rate limits (429)
@@ -24,12 +24,10 @@ export function checkFallbackError(status, errorText, backoffLevel = 0) {
const errorStr = typeof errorText === "string" ? errorText : JSON.stringify(errorText);
const lowerError = errorStr.toLowerCase();
// "No credentials" - should fallback to next model in combo
if (lowerError.includes("no credentials")) {
return { shouldFallback: true, cooldownMs: COOLDOWN_MS.notFound };
}
// "Request not allowed" - short cooldown (5s), takes priority over status code
if (lowerError.includes("request not allowed")) {
return { shouldFallback: true, cooldownMs: COOLDOWN_MS.requestNotAllowed };
}
@@ -51,23 +49,20 @@ export function checkFallbackError(status, errorText, backoffLevel = 0) {
}
}
// 401 - Authentication error (token expired/invalid)
if (status === 401) {
if (status === HTTP_STATUS.UNAUTHORIZED) {
return { shouldFallback: true, cooldownMs: COOLDOWN_MS.unauthorized };
}
// 402/403 - Payment required / Forbidden (quota/permission)
if (status === 402 || status === 403) {
if (status === HTTP_STATUS.PAYMENT_REQUIRED || status === HTTP_STATUS.FORBIDDEN) {
return { shouldFallback: true, cooldownMs: COOLDOWN_MS.paymentRequired };
}
// 404 - Model not found (long cooldown)
if (status === 404) {
if (status === HTTP_STATUS.NOT_FOUND) {
return { shouldFallback: true, cooldownMs: COOLDOWN_MS.notFound };
}
// 429 - Rate limit with exponential backoff
if (status === 429) {
if (status === HTTP_STATUS.RATE_LIMITED) {
const newLevel = Math.min(backoffLevel + 1, BACKOFF_CONFIG.maxLevel);
return {
shouldFallback: true,
@@ -76,12 +71,18 @@ export function checkFallbackError(status, errorText, backoffLevel = 0) {
};
}
// 408/500/502/503/504 - Transient errors (short cooldown)
if (status === 408 || status === 500 || status === 502 || status === 503 || status === 504) {
// Transient errors
const transientStatuses = [
HTTP_STATUS.NOT_ACCEPTABLE, HTTP_STATUS.REQUEST_TIMEOUT,
HTTP_STATUS.SERVER_ERROR, HTTP_STATUS.BAD_GATEWAY,
HTTP_STATUS.SERVICE_UNAVAILABLE, HTTP_STATUS.GATEWAY_TIMEOUT
];
if (transientStatuses.includes(status)) {
return { shouldFallback: true, cooldownMs: COOLDOWN_MS.transient };
}
return { shouldFallback: false, cooldownMs: 0 };
// All other errors - fallback with transient cooldown
return { shouldFallback: true, cooldownMs: COOLDOWN_MS.transient };
}
/**
@@ -99,6 +100,44 @@ export function getUnavailableUntil(cooldownMs) {
return new Date(Date.now() + cooldownMs).toISOString();
}
/**
* Get the earliest rateLimitedUntil from a list of accounts
* @param {Array} accounts - Array of account objects with rateLimitedUntil
* @returns {string|null} Earliest rateLimitedUntil ISO string, or null
*/
export function getEarliestRateLimitedUntil(accounts) {
let earliest = null;
const now = Date.now();
for (const acc of accounts) {
if (!acc.rateLimitedUntil) continue;
const until = new Date(acc.rateLimitedUntil).getTime();
if (until <= now) continue;
if (!earliest || until < earliest) earliest = until;
}
if (!earliest) return null;
return new Date(earliest).toISOString();
}
/**
* Format rateLimitedUntil to human-readable "reset after Xm Ys"
* @param {string} rateLimitedUntil - ISO timestamp
* @returns {string} e.g. "reset after 2m 30s"
*/
export function formatRetryAfter(rateLimitedUntil) {
if (!rateLimitedUntil) return "";
const diffMs = new Date(rateLimitedUntil).getTime() - Date.now();
if (diffMs <= 0) return "reset after 0s";
const totalSec = Math.ceil(diffMs / 1000);
const h = Math.floor(totalSec / 3600);
const m = Math.floor((totalSec % 3600) / 60);
const s = totalSec % 60;
const parts = [];
if (h > 0) parts.push(`${h}h`);
if (m > 0) parts.push(`${m}m`);
if (s > 0 || parts.length === 0) parts.push(`${s}s`);
return `reset after ${parts.join(" ")}`;
}
/**
* Filter available accounts (not in cooldown)
*/
+30 -20
View File
@@ -2,7 +2,8 @@
* Shared combo (model combo) handling with fallback support
*/
import { checkFallbackError } from "./accountFallback.js";
import { checkFallbackError, formatRetryAfter } from "./accountFallback.js";
import { unavailableResponse } from "../utils/error.js";
/**
* Get combo models from combos data
@@ -35,6 +36,8 @@ export function getComboModelsFromData(modelStr, combosData) {
*/
export async function handleComboChat({ body, models, handleSingleModel, log }) {
let lastError = null;
let earliestRetryAfter = null;
let lastStatus = null;
for (let i = 0; i < models.length; i++) {
const modelStr = models[i];
@@ -48,47 +51,54 @@ export async function handleComboChat({ body, models, handleSingleModel, log })
return result;
}
// Extract error message from response
// Extract error info from response
let errorText = result.statusText || "";
let retryAfter = null;
try {
const errorBody = await result.clone().json();
errorText = errorBody?.error ?? errorBody?.message ?? errorText;
errorText = errorBody?.error?.message || errorBody?.error || errorBody?.message || errorText;
retryAfter = errorBody?.retryAfter || null;
} catch {
// Ignore JSON parse errors
}
// Track earliest retryAfter across all combo models
if (retryAfter && (!earliestRetryAfter || new Date(retryAfter) < new Date(earliestRetryAfter))) {
earliestRetryAfter = retryAfter;
}
// Normalize error text to string (Worker-safe)
if (typeof errorText !== "string") {
try {
errorText = JSON.stringify(errorText);
} catch {
errorText = String(errorText);
}
try { errorText = JSON.stringify(errorText); } catch { errorText = String(errorText); }
}
// 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;
}
// 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) });
lastError = errorText || String(result.status);
if (!lastStatus) lastStatus = result.status;
log.warn("COMBO", `Model ${modelStr} failed, trying next`, { status: result.status });
}
log.warn("COMBO", "All combo models failed");
// Return 503 with last error
// All models failed
const status = 406;
const msg = lastError || "All combo models unavailable";
if (earliestRetryAfter) {
const retryHuman = formatRetryAfter(earliestRetryAfter);
log.warn("COMBO", `All models failed | ${msg} (${retryHuman})`);
return unavailableResponse(status, msg, earliestRetryAfter, retryHuman);
}
log.warn("COMBO", `All models failed | ${msg}`);
return new Response(
JSON.stringify({ error: lastError || "All combo models unavailable" }),
{
status: 503,
headers: { "Content-Type": "application/json" }
}
JSON.stringify({ error: { message: msg } }),
{ status, headers: { "Content-Type": "application/json" } }
);
}
+1
View File
@@ -238,6 +238,7 @@ async function getAntigravityUsage(accessToken, providerSpecificData) {
if (data.models) {
// Filter only recommended/important models (must match PROVIDER_MODELS ag ids)
const importantModels = [
'claude-opus-4-6-thinking',
'claude-opus-4-5-thinking',
'claude-opus-4-5',
'claude-sonnet-4-5-thinking',