diff options
Diffstat (limited to 'packages/core/src/utils/retry.ts')
| -rw-r--r-- | packages/core/src/utils/retry.ts | 58 |
1 files changed, 56 insertions, 2 deletions
diff --git a/packages/core/src/utils/retry.ts b/packages/core/src/utils/retry.ts index f3f5f2d2..01651950 100644 --- a/packages/core/src/utils/retry.ts +++ b/packages/core/src/utils/retry.ts @@ -5,13 +5,20 @@ */ import { AuthType } from '../core/contentGenerator.js'; +import { + isProQuotaExceededError, + isGenericQuotaExceededError, +} from './quotaErrorDetection.js'; export interface RetryOptions { maxAttempts: number; initialDelayMs: number; maxDelayMs: number; shouldRetry: (error: Error) => boolean; - onPersistent429?: (authType?: string) => Promise<string | null>; + onPersistent429?: ( + authType?: string, + error?: unknown, + ) => Promise<string | null>; authType?: string; } @@ -86,6 +93,53 @@ export async function retryWithBackoff<T>( } catch (error) { const errorStatus = getErrorStatus(error); + // Check for Pro quota exceeded error first - immediate fallback for OAuth users + if ( + errorStatus === 429 && + authType === AuthType.LOGIN_WITH_GOOGLE && + isProQuotaExceededError(error) && + onPersistent429 + ) { + try { + const fallbackModel = await onPersistent429(authType, error); + if (fallbackModel) { + // Reset attempt counter and try with new model + attempt = 0; + consecutive429Count = 0; + currentDelay = initialDelayMs; + // With the model updated, we continue to the next attempt + continue; + } + } catch (fallbackError) { + // If fallback fails, continue with original error + console.warn('Fallback to Flash model failed:', fallbackError); + } + } + + // Check for generic quota exceeded error (but not Pro, which was handled above) - immediate fallback for OAuth users + if ( + errorStatus === 429 && + authType === AuthType.LOGIN_WITH_GOOGLE && + !isProQuotaExceededError(error) && + isGenericQuotaExceededError(error) && + onPersistent429 + ) { + try { + const fallbackModel = await onPersistent429(authType, error); + if (fallbackModel) { + // Reset attempt counter and try with new model + attempt = 0; + consecutive429Count = 0; + currentDelay = initialDelayMs; + // With the model updated, we continue to the next attempt + continue; + } + } catch (fallbackError) { + // If fallback fails, continue with original error + console.warn('Fallback to Flash model failed:', fallbackError); + } + } + // Track consecutive 429 errors if (errorStatus === 429) { consecutive429Count++; @@ -100,7 +154,7 @@ export async function retryWithBackoff<T>( authType === AuthType.LOGIN_WITH_GOOGLE ) { try { - const fallbackModel = await onPersistent429(authType); + const fallbackModel = await onPersistent429(authType, error); if (fallbackModel) { // Reset attempt counter and try with new model attempt = 0; |
