diff --git a/open-sse/config/constants.js b/open-sse/config/constants.js index 78609d4..4973962 100644 --- a/open-sse/config/constants.js +++ b/open-sse/config/constants.js @@ -219,6 +219,48 @@ export const DEFAULT_MAX_TOKENS = 64000; // Minimum max tokens for tool calling (to prevent truncated arguments) export const DEFAULT_MIN_TOKENS = 32000; +// HTTP status codes +export const HTTP_STATUS = { + BAD_REQUEST: 400, + UNAUTHORIZED: 401, + PAYMENT_REQUIRED: 402, + FORBIDDEN: 403, + NOT_FOUND: 404, + NOT_ACCEPTABLE: 406, + REQUEST_TIMEOUT: 408, + RATE_LIMITED: 429, + SERVER_ERROR: 500, + BAD_GATEWAY: 502, + SERVICE_UNAVAILABLE: 503, + GATEWAY_TIMEOUT: 504 +}; + +// OpenAI-compatible error types mapping +export const ERROR_TYPES = { + [HTTP_STATUS.BAD_REQUEST]: { type: "invalid_request_error", code: "bad_request" }, + [HTTP_STATUS.UNAUTHORIZED]: { type: "authentication_error", code: "invalid_api_key" }, + [HTTP_STATUS.FORBIDDEN]: { type: "permission_error", code: "insufficient_quota" }, + [HTTP_STATUS.NOT_FOUND]: { type: "invalid_request_error", code: "model_not_found" }, + [HTTP_STATUS.RATE_LIMITED]: { type: "rate_limit_error", code: "rate_limit_exceeded" }, + [HTTP_STATUS.SERVER_ERROR]: { type: "server_error", code: "internal_server_error" }, + [HTTP_STATUS.BAD_GATEWAY]: { type: "server_error", code: "bad_gateway" }, + [HTTP_STATUS.SERVICE_UNAVAILABLE]: { type: "server_error", code: "service_unavailable" }, + [HTTP_STATUS.GATEWAY_TIMEOUT]: { type: "server_error", code: "gateway_timeout" } +}; + +// Default error messages per status code +export const DEFAULT_ERROR_MESSAGES = { + [HTTP_STATUS.BAD_REQUEST]: "Bad request", + [HTTP_STATUS.UNAUTHORIZED]: "Invalid API key provided", + [HTTP_STATUS.FORBIDDEN]: "You exceeded your current quota", + [HTTP_STATUS.NOT_FOUND]: "Model not found", + [HTTP_STATUS.RATE_LIMITED]: "Rate limit exceeded", + [HTTP_STATUS.SERVER_ERROR]: "Internal server error", + [HTTP_STATUS.BAD_GATEWAY]: "Bad gateway - upstream provider error", + [HTTP_STATUS.SERVICE_UNAVAILABLE]: "Service temporarily unavailable", + [HTTP_STATUS.GATEWAY_TIMEOUT]: "Gateway timeout" +}; + // Exponential backoff config for rate limits (like CLIProxyAPI) export const BACKOFF_CONFIG = { base: 1000, // 1 second base diff --git a/open-sse/config/providerModels.js b/open-sse/config/providerModels.js index b9a8da6..803d646 100644 --- a/open-sse/config/providerModels.js +++ b/open-sse/config/providerModels.js @@ -54,7 +54,7 @@ export const PROVIDER_MODELS = { { id: "glm-4.7", name: "GLM 4.7" }, ], ag: [ // Antigravity - special case: models call different backends - // { id: "claude-opus-4-6", name: "Claude Opus 4.6" }, + { id: "claude-opus-4-6-thinking", name: "Claude Opus 4.6 Thinking" }, { id: "claude-opus-4-5-thinking", name: "Claude Opus 4.5 Thinking" }, { id: "claude-sonnet-4-5-thinking", name: "Claude Sonnet 4.5 Thinking" }, { id: "claude-sonnet-4-5", name: "Claude Sonnet 4.5" }, diff --git a/open-sse/executors/antigravity.js b/open-sse/executors/antigravity.js index 92dc727..e16d3b7 100644 --- a/open-sse/executors/antigravity.js +++ b/open-sse/executors/antigravity.js @@ -1,6 +1,6 @@ import crypto from "crypto"; import { BaseExecutor } from "./base.js"; -import { PROVIDERS, OAUTH_ENDPOINTS } from "../config/constants.js"; +import { PROVIDERS, OAUTH_ENDPOINTS, HTTP_STATUS } from "../config/constants.js"; const MAX_RETRY_AFTER_MS = 10000; @@ -162,7 +162,7 @@ export class AntigravityExecutor extends BaseExecutor { signal }); - if (response.status === 429 || response.status === 503) { + if (response.status === HTTP_STATUS.RATE_LIMITED || response.status === HTTP_STATUS.SERVICE_UNAVAILABLE) { // Try to get retry time from headers first let retryMs = this.parseRetryHeaders(response.headers); @@ -186,7 +186,7 @@ export class AntigravityExecutor extends BaseExecutor { } // Auto retry only for 429 when retryMs is 0 or undefined - if (response.status === 429 && (!retryMs || retryMs === 0) && retryAttemptsByUrl[urlIndex] < MAX_AUTO_RETRIES) { + if (response.status === HTTP_STATUS.RATE_LIMITED && (!retryMs || retryMs === 0) && retryAttemptsByUrl[urlIndex] < MAX_AUTO_RETRIES) { retryAttemptsByUrl[urlIndex]++; // Exponential backoff: 2s, 4s, 8s... const backoffMs = Math.min(1000 * (2 ** retryAttemptsByUrl[urlIndex]), MAX_RETRY_AFTER_MS); diff --git a/open-sse/executors/base.js b/open-sse/executors/base.js index 7286077..0cbcdd3 100644 --- a/open-sse/executors/base.js +++ b/open-sse/executors/base.js @@ -1,3 +1,5 @@ +import { HTTP_STATUS } from "../config/constants.js"; + /** * BaseExecutor - Base class for provider executors */ @@ -55,7 +57,7 @@ export class BaseExecutor { } shouldRetry(status, urlIndex) { - return status === 429 && urlIndex + 1 < this.getFallbackCount(); + return status === HTTP_STATUS.RATE_LIMITED && urlIndex + 1 < this.getFallbackCount(); } // Override in subclass for provider-specific refresh diff --git a/open-sse/executors/cursor.js b/open-sse/executors/cursor.js index 190429d..1bcefce 100644 --- a/open-sse/executors/cursor.js +++ b/open-sse/executors/cursor.js @@ -1,5 +1,5 @@ import { BaseExecutor } from "./base.js"; -import { PROVIDERS } from "../config/constants.js"; +import { PROVIDERS, HTTP_STATUS } from "../config/constants.js"; import { generateCursorBody, parseConnectRPCFrame, @@ -77,7 +77,7 @@ function createErrorResponse(jsonError) { code: jsonError?.error?.details?.[0]?.debug?.error || "unknown" } }), { - status: isRateLimit ? 429 : 400, + status: isRateLimit ? HTTP_STATUS.RATE_LIMITED : HTTP_STATUS.BAD_REQUEST, headers: { "Content-Type": "application/json" } }); } @@ -275,7 +275,7 @@ export class CursorExecutor extends BaseExecutor { code: "" } }), { - status: 500, + status: HTTP_STATUS.SERVER_ERROR, headers: { "Content-Type": "application/json" } }); return { response: errorResponse, url, headers, transformedBody: body }; @@ -338,7 +338,7 @@ export class CursorExecutor extends BaseExecutor { code: "rate_limited" } }), { - status: 429, + status: HTTP_STATUS.RATE_LIMITED, headers: { "Content-Type": "application/json" } }); } @@ -480,7 +480,7 @@ export class CursorExecutor extends BaseExecutor { code: "rate_limited" } }), { - status: 429, + status: HTTP_STATUS.RATE_LIMITED, headers: { "Content-Type": "application/json" } }); } diff --git a/open-sse/handlers/chatCore.js b/open-sse/handlers/chatCore.js index 14c36f4..bd9d274 100644 --- a/open-sse/handlers/chatCore.js +++ b/open-sse/handlers/chatCore.js @@ -8,6 +8,7 @@ import { refreshWithRetry } from "../services/tokenRefresh.js"; import { createRequestLogger } from "../utils/requestLogger.js"; import { getModelTargetFormat, PROVIDER_ID_TO_ALIAS } from "../config/providerModels.js"; import { createErrorResult, parseUpstreamError, formatProviderError } from "../utils/error.js"; +import { HTTP_STATUS } from "../config/constants.js"; import { handleBypassRequest } from "../utils/bypassHandler.js"; import { saveRequestUsage, trackPendingRequest, appendRequestLog } from "@/lib/usageDb.js"; import { getExecutor } from "../executors/index.js"; @@ -330,18 +331,18 @@ export async function handleChatCore({ body, modelInfo, credentials, log, onCred } catch (error) { trackPendingRequest(model, provider, connectionId, false); - appendRequestLog({ model, provider, connectionId, status: `FAILED ${error.name === "AbortError" ? 499 : 502}` }).catch(() => { }); + appendRequestLog({ model, provider, connectionId, status: `FAILED ${error.name === "AbortError" ? 499 : HTTP_STATUS.BAD_GATEWAY}` }).catch(() => { }); if (error.name === "AbortError") { streamController.handleError(error); return createErrorResult(499, "Request aborted"); } - const errMsg = formatProviderError(error, provider, model, 502); + const errMsg = formatProviderError(error, provider, model, HTTP_STATUS.BAD_GATEWAY); console.log(`${COLORS.red}[ERROR] ${errMsg}${COLORS.reset}`); - return createErrorResult(502, errMsg); + return createErrorResult(HTTP_STATUS.BAD_GATEWAY, errMsg); } // Handle 401/403 - try token refresh using executor - if (providerResponse.status === 401 || providerResponse.status === 403) { + if (providerResponse.status === HTTP_STATUS.UNAUTHORIZED || providerResponse.status === HTTP_STATUS.FORBIDDEN) { const newCredentials = await refreshWithRetry( () => executor.refreshCredentials(credentials, log), 3, diff --git a/open-sse/services/accountFallback.js b/open-sse/services/accountFallback.js index a23281a..276cb82 100644 --- a/open-sse/services/accountFallback.js +++ b/open-sse/services/accountFallback.js @@ -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) */ diff --git a/open-sse/services/combo.js b/open-sse/services/combo.js index 011b484..b3bb993 100644 --- a/open-sse/services/combo.js +++ b/open-sse/services/combo.js @@ -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" } } ); } - diff --git a/open-sse/services/usage.js b/open-sse/services/usage.js index f62851e..1766a85 100644 --- a/open-sse/services/usage.js +++ b/open-sse/services/usage.js @@ -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', diff --git a/open-sse/utils/error.js b/open-sse/utils/error.js index 262fc75..323f3ae 100644 --- a/open-sse/utils/error.js +++ b/open-sse/utils/error.js @@ -1,15 +1,4 @@ -// OpenAI-compatible error types mapping -const ERROR_TYPES = { - 400: { type: "invalid_request_error", code: "bad_request" }, - 401: { type: "authentication_error", code: "invalid_api_key" }, - 403: { type: "permission_error", code: "insufficient_quota" }, - 404: { type: "invalid_request_error", code: "model_not_found" }, - 429: { type: "rate_limit_error", code: "rate_limit_exceeded" }, - 500: { type: "server_error", code: "internal_server_error" }, - 502: { type: "server_error", code: "bad_gateway" }, - 503: { type: "server_error", code: "service_unavailable" }, - 504: { type: "server_error", code: "gateway_timeout" } -}; +import { ERROR_TYPES, DEFAULT_ERROR_MESSAGES } from "../config/constants.js"; /** * Build OpenAI-compatible error response body @@ -25,31 +14,13 @@ export function buildErrorBody(statusCode, message) { return { error: { - message: message || getDefaultMessage(statusCode), + message: message || DEFAULT_ERROR_MESSAGES[statusCode] || "An error occurred", type: errorInfo.type, code: errorInfo.code } }; } -/** - * Get default error message for status code - */ -function getDefaultMessage(statusCode) { - const messages = { - 400: "Bad request", - 401: "Invalid API key provided", - 403: "You exceeded your current quota", - 404: "Model not found", - 429: "Rate limit exceeded", - 500: "Internal server error", - 502: "Bad gateway - upstream provider error", - 503: "Service temporarily unavailable", - 504: "Gateway timeout" - }; - return messages[statusCode] || "An error occurred"; -} - /** * Create error Response object (for non-streaming) * @param {number} statusCode - HTTP status code @@ -175,6 +146,29 @@ export function createErrorResult(statusCode, message, retryAfterMs = null) { return result; } +/** + * Create unavailable response when all accounts are rate limited + * @param {number} statusCode - Original error status code + * @param {string} message - Error message (without retry info) + * @param {string} retryAfter - ISO timestamp when earliest account becomes available + * @param {string} retryAfterHuman - Human-readable retry info e.g. "reset after 30s" + * @returns {Response} + */ +export function unavailableResponse(statusCode, message, retryAfter, retryAfterHuman) { + const retryAfterSec = Math.max(Math.ceil((new Date(retryAfter).getTime() - Date.now()) / 1000), 1); + const msg = `${message} (${retryAfterHuman})`; + return new Response( + JSON.stringify({ error: { message: msg } }), + { + status: statusCode, + headers: { + "Content-Type": "application/json", + "Retry-After": String(retryAfterSec) + } + } + ); +} + /** * Format provider error with context * @param {Error} error - Original error diff --git a/src/app/(dashboard)/dashboard/providers/page.js b/src/app/(dashboard)/dashboard/providers/page.js index bdd9838..0ff2031 100644 --- a/src/app/(dashboard)/dashboard/providers/page.js +++ b/src/app/(dashboard)/dashboard/providers/page.js @@ -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() { + {/* Free Providers */} +