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

View file

@ -1,9 +1,9 @@
import { getProviderCredentials, markAccountUnavailable, clearAccountError } from "../services/auth.js";
import { getModelInfo, getComboModels } from "../services/model.js";
import { handleChatCore } from "open-sse/handlers/chatCore.js";
import { errorResponse } from "open-sse/utils/error.js";
import { checkFallbackError } from "open-sse/services/accountFallback.js";
import { errorResponse, unavailableResponse } from "open-sse/utils/error.js";
import { handleComboChat } from "open-sse/services/combo.js";
import { HTTP_STATUS } from "open-sse/config/constants.js";
import * as log from "../utils/logger.js";
import { updateProviderCredentials, checkAndRefreshToken } from "../services/tokenRefresh.js";
@ -18,7 +18,7 @@ export async function handleChat(request, clientRawRequest = null) {
body = await request.json();
} catch {
log.warn("CHAT", "Invalid JSON body");
return errorResponse(400, "Invalid JSON body");
return errorResponse(HTTP_STATUS.BAD_REQUEST, "Invalid JSON body");
}
// Build clientRawRequest for logging (if not provided)
@ -52,7 +52,7 @@ export async function handleChat(request, clientRawRequest = null) {
if (!modelStr) {
log.warn("CHAT", "Missing model");
return errorResponse(400, "Missing model");
return errorResponse(HTTP_STATUS.BAD_REQUEST, "Missing model");
}
// Check if model is a combo (has multiple models with fallback)
@ -78,7 +78,7 @@ async function handleSingleModelChat(body, modelStr, clientRawRequest = null, re
const modelInfo = await getModelInfo(modelStr);
if (!modelInfo.provider) {
log.warn("CHAT", "Invalid model format", { model: modelStr });
return errorResponse(400, "Invalid model format");
return errorResponse(HTTP_STATUS.BAD_REQUEST, "Invalid model format");
}
const { provider, model } = modelInfo;
@ -96,19 +96,25 @@ async function handleSingleModelChat(body, modelStr, clientRawRequest = null, re
// Try with available accounts (fallback on errors)
let excludeConnectionId = null;
let lastError = null;
let lastStatus = null;
while (true) {
const credentials = await getProviderCredentials(provider, excludeConnectionId);
if (!credentials) {
// All accounts unavailable
if (!credentials || credentials.allRateLimited) {
if (credentials?.allRateLimited) {
const errorMsg = lastError || credentials.lastError || "Unavailable";
const status = lastStatus || Number(credentials.lastErrorCode) || HTTP_STATUS.SERVICE_UNAVAILABLE;
log.warn("CHAT", `[${provider}/${model}] ${errorMsg} (${credentials.retryAfterHuman})`);
return unavailableResponse(status, `[${provider}/${model}] ${errorMsg}`, credentials.retryAfter, credentials.retryAfterHuman);
}
if (!excludeConnectionId) {
log.error("AUTH", `No credentials for provider: ${provider}`);
return errorResponse(400, `No credentials for provider: ${provider}`);
return errorResponse(HTTP_STATUS.BAD_REQUEST, `No credentials for provider: ${provider}`);
}
log.warn("CHAT", "No more accounts available", { provider });
return new Response(
JSON.stringify({ error: lastError || "All accounts unavailable" }),
{ status: 503, headers: { "Content-Type": "application/json" } }
);
return errorResponse(lastStatus || HTTP_STATUS.SERVICE_UNAVAILABLE, lastError || "All accounts unavailable");
}
// Log account selection
@ -135,22 +141,20 @@ async function handleSingleModelChat(body, modelStr, clientRawRequest = null, re
});
},
onRequestSuccess: async () => {
// Clear error status only if currently has error (optimization)
await clearAccountError(credentials.connectionId, credentials);
}
});
if (result.success) return result.response;
// Check if should fallback to next account
const { shouldFallback, cooldownMs } = checkFallbackError(result.status, result.error);
// Mark account unavailable (auto-calculates cooldown with exponential backoff)
const { shouldFallback } = await markAccountUnavailable(credentials.connectionId, result.status, result.error, provider);
if (shouldFallback) {
const accountId = credentials.connectionId.slice(0, 8);
log.warn("AUTH", `Account ${accountId}... unavailable (status: ${result.status}), trying fallback`);
await markAccountUnavailable(credentials.connectionId, cooldownMs, result.error?.slice(0, 100), result.status, provider);
log.warn("AUTH", `Account ${accountId}... unavailable (${result.status}), trying fallback`);
excludeConnectionId = credentials.connectionId;
lastError = result.error;
lastStatus = result.status;
continue;
}

View file

@ -1,5 +1,5 @@
import { getProviderConnections, validateApiKey, updateProviderConnection, getSettings } from "@/lib/localDb";
import { isAccountUnavailable, getUnavailableUntil } from "open-sse/services/accountFallback.js";
import { isAccountUnavailable, getUnavailableUntil, getEarliestRateLimitedUntil, formatRetryAfter, checkFallbackError } from "open-sse/services/accountFallback.js";
import * as log from "../utils/logger.js";
// Mutex to prevent race conditions during account selection
@ -21,8 +21,23 @@ export async function getProviderCredentials(provider, excludeConnectionId = nul
await currentMutex;
const connections = await getProviderConnections({ provider, isActive: true });
log.debug("AUTH", `${provider} | total connections: ${connections.length}, excludeId: ${excludeConnectionId || "none"}`);
if (connections.length === 0) {
// Check all connections (including inactive) to see if rate limited
const allConnections = await getProviderConnections({ provider });
log.debug("AUTH", `${provider} | all connections (incl inactive): ${allConnections.length}`);
if (allConnections.length > 0) {
const earliest = getEarliestRateLimitedUntil(allConnections);
if (earliest) {
log.warn("AUTH", `${provider} | all ${allConnections.length} accounts rate limited (${formatRetryAfter(earliest)})`);
return { allRateLimited: true, retryAfter: earliest, retryAfterHuman: formatRetryAfter(earliest) };
}
log.warn("AUTH", `${provider} | ${allConnections.length} accounts found but none active`);
allConnections.forEach(c => {
log.debug("AUTH", `${c.id?.slice(0, 8)} | isActive=${c.isActive} | rateLimitedUntil=${c.rateLimitedUntil || "none"} | testStatus=${c.testStatus}`);
});
}
log.warn("AUTH", `No credentials for ${provider}`);
return null;
}
@ -34,8 +49,31 @@ export async function getProviderCredentials(provider, excludeConnectionId = nul
return true;
});
log.debug("AUTH", `${provider} | available: ${availableConnections.length}/${connections.length}`);
connections.forEach(c => {
const excluded = excludeConnectionId && c.id === excludeConnectionId;
const rateLimited = isAccountUnavailable(c.rateLimitedUntil);
if (excluded || rateLimited) {
log.debug("AUTH", `${c.id?.slice(0, 8)} | ${excluded ? "excluded" : ""} ${rateLimited ? `rateLimited until ${c.rateLimitedUntil}` : ""}`);
}
});
if (availableConnections.length === 0) {
log.warn("AUTH", `All ${connections.length} accounts for ${provider} unavailable`);
const earliest = getEarliestRateLimitedUntil(connections);
if (earliest) {
// Find the connection with the earliest rateLimitedUntil to get its error info
const rateLimitedConns = connections.filter(c => c.rateLimitedUntil && new Date(c.rateLimitedUntil).getTime() > Date.now());
const earliestConn = rateLimitedConns.sort((a, b) => new Date(a.rateLimitedUntil) - new Date(b.rateLimitedUntil))[0];
log.warn("AUTH", `${provider} | all ${connections.length} active accounts rate limited (${formatRetryAfter(earliest)}) | lastErrorCode=${earliestConn?.errorCode}, lastError=${earliestConn?.lastError?.slice(0, 50)}`);
return {
allRateLimited: true,
retryAfter: earliest,
retryAfterHuman: formatRetryAfter(earliest),
lastError: earliestConn?.lastError || null,
lastErrorCode: earliestConn?.errorCode || null
};
}
log.warn("AUTH", `${provider} | all ${connections.length} accounts unavailable`);
return null;
}
@ -106,23 +144,35 @@ export async function getProviderCredentials(provider, excludeConnectionId = nul
}
/**
* Mark account as unavailable with cooldown
* Mark account as unavailable reads backoffLevel from DB, calculates cooldown with exponential backoff, saves new level
* @returns {{ shouldFallback: boolean, cooldownMs: number }}
*/
export async function markAccountUnavailable(connectionId, cooldownMs, reason = "Provider error", errorCode = null, provider = null) {
export async function markAccountUnavailable(connectionId, status, errorText, provider = null) {
// Read current connection to get backoffLevel
const connections = await getProviderConnections({ provider });
const conn = connections.find(c => c.id === connectionId);
const backoffLevel = conn?.backoffLevel || 0;
const { shouldFallback, cooldownMs, newBackoffLevel } = checkFallbackError(status, errorText, backoffLevel);
if (!shouldFallback) return { shouldFallback: false, cooldownMs: 0 };
const rateLimitedUntil = getUnavailableUntil(cooldownMs);
const reason = typeof errorText === "string" ? errorText.slice(0, 100) : "Provider error";
await updateProviderConnection(connectionId, {
rateLimitedUntil,
testStatus: "unavailable",
lastError: reason,
errorCode,
lastErrorAt: new Date().toISOString()
errorCode: status,
lastErrorAt: new Date().toISOString(),
backoffLevel: newBackoffLevel ?? backoffLevel
});
// log.warn("AUTH", `Account ${connectionId.slice(0,8)} unavailable until ${rateLimitedUntil}`);
// Log to stderr for CLI to display
if (provider && errorCode && reason) {
console.error(`${provider} [${errorCode}]: ${reason}`);
if (provider && status && reason) {
console.error(`${provider} [${status}]: ${reason}`);
}
return { shouldFallback: true, cooldownMs };
}
/**
@ -141,7 +191,8 @@ export async function clearAccountError(connectionId, currentConnection) {
testStatus: "active",
lastError: null,
lastErrorAt: null,
rateLimitedUntil: null
rateLimitedUntil: null,
backoffLevel: 0
});
log.info("AUTH", `Account ${connectionId.slice(0,8)} error cleared`);
}