From 2dfb87eac6af02bc9199e112b264994948abde60 Mon Sep 17 00:00:00 2001 From: MythEclipse Date: Wed, 10 Jun 2026 23:57:24 +0700 Subject: [PATCH] fix: retry AI proxy with next proxy on HTTP errors too Previously only retried on network errors (fetch exceptions). Now also rotates to next proxy when upstream returns non-2xx (429 rate limit, 5xx, etc). Applies to both OpenAI and Anthropic endpoints. Co-Authored-By: Claude Fable 5 --- src/lib/ai-proxy.ts | 18 ++++++++++++++---- src/lib/anthropic-proxy.ts | 24 ++++++++++++++---------- 2 files changed, 28 insertions(+), 14 deletions(-) diff --git a/src/lib/ai-proxy.ts b/src/lib/ai-proxy.ts index b812d0d..8653580 100644 --- a/src/lib/ai-proxy.ts +++ b/src/lib/ai-proxy.ts @@ -327,12 +327,22 @@ export async function handleChatCompletion( try { response = await fetch(url, init); - if (response.ok && proxyPool && proxyPool.size > 0 && init.proxy && attempt > 0) { - proxyPool.markSuccess(); + if (response.ok) { + // Success — reset proxy failure if we used one + if (proxyPool && proxyPool.size > 0 && init.proxy && attempt > 0) { + proxyPool.markSuccess(); + } + break; + } + // Non-2xx — mark proxy as failed so next attempt rotates + if (proxyPool && proxyPool.size > 0 && init.proxy) { + proxyPool.markFailed(); } - if (response.ok || response.status < 500) break; // non-5xx = no retry } catch { - // fall through to next attempt + // Network error — mark proxy as failed, retry + if (proxyPool && proxyPool.size > 0 && init.proxy) { + proxyPool.markFailed(); + } } } diff --git a/src/lib/anthropic-proxy.ts b/src/lib/anthropic-proxy.ts index bb58681..ebdf19d 100644 --- a/src/lib/anthropic-proxy.ts +++ b/src/lib/anthropic-proxy.ts @@ -417,18 +417,22 @@ export async function handleAnthropicMessages( try { response = await fetch(url, init); - if ( - response.ok && - proxyPool && - proxyPool.size > 0 && - init.proxy && - attempt > 0 - ) { - proxyPool.markSuccess(); + if (response.ok) { + // Success — reset proxy failure if we used one + if (proxyPool && proxyPool.size > 0 && init.proxy && attempt > 0) { + proxyPool.markSuccess(); + } + break; + } + // Non-2xx — mark proxy as failed so next attempt rotates + if (proxyPool && proxyPool.size > 0 && init.proxy) { + proxyPool.markFailed(); } - if (response.ok || response.status < 500) break; } catch { - // fall through to next attempt + // Network error — mark proxy as failed, retry + if (proxyPool && proxyPool.size > 0 && init.proxy) { + proxyPool.markFailed(); + } } }