( currentModel: string, isFastModeActive?: boolean, )
| 40 | * @param isFastModeActive Whether fast mode is currently active (for fast-mode-only mocks) |
| 41 | */ |
| 42 | export function checkMockRateLimitError( |
| 43 | currentModel: string, |
| 44 | isFastModeActive?: boolean, |
| 45 | ): APIError | null { |
| 46 | if (!shouldProcessMockLimits()) { |
| 47 | return null |
| 48 | } |
| 49 | |
| 50 | const headerlessMessage = getMockHeaderless429Message() |
| 51 | if (headerlessMessage) { |
| 52 | return new APIError( |
| 53 | 429, |
| 54 | { error: { type: 'rate_limit_error', message: headerlessMessage } }, |
| 55 | headerlessMessage, |
| 56 | // eslint-disable-next-line eslint-plugin-n/no-unsupported-features/node-builtins |
| 57 | new globalThis.Headers(), |
| 58 | ) |
| 59 | } |
| 60 | |
| 61 | const mockHeaders = getMockHeaders() |
| 62 | if (!mockHeaders) { |
| 63 | return null |
| 64 | } |
| 65 | |
| 66 | // Check if we should throw a 429 error |
| 67 | // Only throw if: |
| 68 | // 1. Status is rejected AND |
| 69 | // 2. Either no overage headers OR overage is also rejected |
| 70 | // 3. For Opus-specific limits, only throw if actually using an Opus model |
| 71 | const status = mockHeaders['anthropic-ratelimit-unified-status'] |
| 72 | const overageStatus = |
| 73 | mockHeaders['anthropic-ratelimit-unified-overage-status'] |
| 74 | const rateLimitType = |
| 75 | mockHeaders['anthropic-ratelimit-unified-representative-claim'] |
| 76 | |
| 77 | // Check if this is an Opus-specific rate limit |
| 78 | const isOpusLimit = rateLimitType === 'seven_day_opus' |
| 79 | |
| 80 | // Check if current model is an Opus model (handles all variants including aliases) |
| 81 | const isUsingOpus = currentModel.includes('opus') |
| 82 | |
| 83 | // For Opus limits, only throw 429 if actually using Opus |
| 84 | // This simulates the real API behavior where fallback to Sonnet succeeds |
| 85 | if (isOpusLimit && !isUsingOpus) { |
| 86 | return null |
| 87 | } |
| 88 | |
| 89 | // Check for mock fast mode rate limits (handles expiry, countdown, etc.) |
| 90 | if (isMockFastModeRateLimitScenario()) { |
| 91 | const fastModeHeaders = checkMockFastModeRateLimit(isFastModeActive) |
| 92 | if (fastModeHeaders === null) { |
| 93 | return null |
| 94 | } |
| 95 | // Create a mock 429 error with the fast mode headers |
| 96 | const error = new APIError( |
| 97 | 429, |
| 98 | { error: { type: 'rate_limit_error', message: 'Rate limit exceeded' } }, |
| 99 | 'Rate limit exceeded', |
no test coverage detected