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

@ -5,7 +5,7 @@ import Image from "next/image";
import PropTypes from "prop-types";
import { Card, CardSkeleton, Badge, Button, Input, Modal, Select } from "@/shared/components";
import { OAUTH_PROVIDERS, APIKEY_PROVIDERS } from "@/shared/constants/config";
import { OPENAI_COMPATIBLE_PREFIX, ANTHROPIC_COMPATIBLE_PREFIX } from "@/shared/constants/providers";
import { FREE_PROVIDERS, OPENAI_COMPATIBLE_PREFIX, ANTHROPIC_COMPATIBLE_PREFIX } from "@/shared/constants/providers";
import Link from "next/link";
import { getErrorCode, getRelativeTime } from "@/shared/utils";
@ -151,6 +151,21 @@ export default function ProvidersPage() {
</div>
</div>
{/* Free Providers */}
<div className="flex flex-col gap-4">
<h2 className="text-xl font-semibold">Free Providers</h2>
<div className="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-3 xl:grid-cols-4 gap-4">
{Object.entries(FREE_PROVIDERS).map(([key, info]) => (
<ProviderCard
key={key}
providerId={key}
provider={info}
stats={getProviderStats(key, "oauth")}
/>
))}
</div>
</div>
{/* API Key Providers */}
<div className="flex flex-col gap-4">
<div className="flex items-center justify-between">
@ -207,7 +222,7 @@ function ProviderCard({ providerId, provider, stats }) {
return (
<Link href={`/dashboard/providers/${providerId}`} className="group">
<Card padding="sm" className="h-full hover:bg-black/[0.01] dark:hover:bg-white/[0.01] transition-colors cursor-pointer">
<Card padding="xs" className="h-full hover:bg-black/[0.01] dark:hover:bg-white/[0.01] transition-colors cursor-pointer">
<div className="flex items-center justify-between">
<div className="flex items-center gap-3">
<div
@ -225,8 +240,8 @@ function ProviderCard({ providerId, provider, stats }) {
<Image
src={`/providers/${provider.id}.png`}
alt={provider.name}
width={32}
height={32}
width={30}
height={30}
className="object-contain rounded-lg max-w-[32px] max-h-[32px]"
sizes="32px"
onError={() => setImgError(true)}
@ -286,7 +301,7 @@ function ApiKeyProviderCard({ providerId, provider, stats }) {
return (
<Link href={`/dashboard/providers/${providerId}`} className="group">
<Card padding="sm" className="h-full hover:bg-black/[0.01] dark:hover:bg-white/[0.01] transition-colors cursor-pointer">
<Card padding="xs" className="h-full hover:bg-black/[0.01] dark:hover:bg-white/[0.01] transition-colors cursor-pointer">
<div className="flex items-center justify-between">
<div className="flex items-center gap-3">
<div
@ -304,10 +319,10 @@ function ApiKeyProviderCard({ providerId, provider, stats }) {
<Image
src={getIconPath()}
alt={provider.name}
width={32}
height={32}
className="object-contain rounded-lg max-w-[32px] max-h-[32px]"
sizes="32px"
width={30}
height={30}
className="object-contain rounded-lg max-w-[30px] max-h-[30px]"
sizes="30px"
onError={() => setImgError(true)}
/>
)}

View file

@ -15,6 +15,7 @@ export default function Card({
}) {
const paddings = {
none: "",
xs: "p-3",
sm: "p-4",
md: "p-6",
lg: "p-8",

View file

@ -43,6 +43,7 @@ export const PROVIDER_ENDPOINTS = {
// Re-export from providers.js for backward compatibility
export {
FREE_PROVIDERS,
OAUTH_PROVIDERS,
APIKEY_PROVIDERS,
AI_PROVIDERS,

View file

@ -1,12 +1,16 @@
// Provider definitions
// Free Providers
export const FREE_PROVIDERS = {
iflow: { id: "iflow", alias: "if", name: "iFlow AI", icon: "water_drop", color: "#6366F1" },
qwen: { id: "qwen", alias: "qw", name: "Qwen Code", icon: "psychology", color: "#10B981" },
};
// OAuth Providers
export const OAUTH_PROVIDERS = {
claude: { id: "claude", alias: "cc", name: "Claude Code", icon: "smart_toy", color: "#D97757" },
antigravity: { id: "antigravity", alias: "ag", name: "Antigravity", icon: "rocket_launch", color: "#F59E0B" },
codex: { id: "codex", alias: "cx", name: "OpenAI Codex", icon: "code", color: "#3B82F6" },
iflow: { id: "iflow", alias: "if", name: "iFlow AI", icon: "water_drop", color: "#6366F1" },
qwen: { id: "qwen", alias: "qw", name: "Qwen Code", icon: "psychology", color: "#10B981" },
"gemini-cli": { id: "gemini-cli", alias: "gc", name: "Gemini CLI", icon: "terminal", color: "#4285F4" },
github: { id: "github", alias: "gh", name: "GitHub Copilot", icon: "code", color: "#333333" },
kiro: { id: "kiro", alias: "kr", name: "Kiro AI", icon: "psychology_alt", color: "#FF6B35" },
@ -18,7 +22,7 @@ export const APIKEY_PROVIDERS = {
glm: { id: "glm", alias: "glm", name: "GLM Coding", icon: "code", color: "#2563EB", textIcon: "GL" },
kimi: { id: "kimi", alias: "kimi", name: "Kimi Coding", icon: "psychology", color: "#1E3A8A", textIcon: "KM" },
minimax: { id: "minimax", alias: "minimax", name: "Minimax Coding", icon: "memory", color: "#7C3AED", textIcon: "MM" },
"minimax-cn": { id: "minimax-cn", alias: "minimax-cn", name: "Minimax Coding (China)", icon: "memory", color: "#DC2626", textIcon: "MC" },
"minimax-cn": { id: "minimax-cn", alias: "minimax-cn", name: "Minimax (China)", icon: "memory", color: "#DC2626", textIcon: "MC" },
openai: { id: "openai", alias: "openai", name: "OpenAI", icon: "auto_awesome", color: "#10A37F", textIcon: "OA" },
anthropic: { id: "anthropic", alias: "anthropic", name: "Anthropic", icon: "smart_toy", color: "#D97757", textIcon: "AN" },
gemini: { id: "gemini", alias: "gemini", name: "Gemini", icon: "diamond", color: "#4285F4", textIcon: "GE" },
@ -36,7 +40,7 @@ export function isAnthropicCompatibleProvider(providerId) {
}
// All providers (combined)
export const AI_PROVIDERS = { ...OAUTH_PROVIDERS, ...APIKEY_PROVIDERS };
export const AI_PROVIDERS = { ...FREE_PROVIDERS, ...OAUTH_PROVIDERS, ...APIKEY_PROVIDERS };
// Auth methods
export const AUTH_METHODS = {

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`);
}