* Handles falling back to Flash model when persistent 429 errors occur for OAuth users. * Uses a fallback handler if provided by the config; otherwise, returns null.
(
authType?: string,
error?: unknown,
)
| 868 | * Uses a fallback handler if provided by the config; otherwise, returns null. |
| 869 | */ |
| 870 | private async handleFlashFallback( |
| 871 | authType?: string, |
| 872 | error?: unknown, |
| 873 | ): Promise<string | null> { |
| 874 | // Only handle fallback for OAuth users |
| 875 | if (authType !== AuthType.LOGIN_WITH_GOOGLE) { |
| 876 | return null; |
| 877 | } |
| 878 | |
| 879 | const currentModel = this.config.getModel(); |
| 880 | const fallbackModel = DEFAULT_FALLBACK_MODEL; |
| 881 | |
| 882 | // Don't fallback if already using Flash model |
| 883 | if (currentModel === fallbackModel) { |
| 884 | return null; |
| 885 | } |
| 886 | |
| 887 | // Check if config has a fallback handler (set by CLI package) |
| 888 | const fallbackHandler = this.config.flashFallbackHandler; |
| 889 | if (typeof fallbackHandler === 'function') { |
| 890 | try { |
| 891 | const accepted = await fallbackHandler( |
| 892 | currentModel, |
| 893 | fallbackModel, |
| 894 | error, |
| 895 | ); |
| 896 | if (accepted !== false && accepted !== null) { |
| 897 | this.config.setModel(fallbackModel); |
| 898 | this.config.setFallbackMode(true); |
| 899 | return fallbackModel; |
| 900 | } |
| 901 | // Check if the model was switched manually in the handler |
| 902 | if (this.config.getModel() === fallbackModel) { |
| 903 | return null; // Model was switched but don't continue with current prompt |
| 904 | } |
| 905 | } catch (error) { |
| 906 | console.warn('Flash fallback handler failed:', error); |
| 907 | } |
| 908 | } |
| 909 | |
| 910 | return null; |
| 911 | } |
| 912 | } |
| 913 | |
| 914 | export const TEST_ONLY = { |
no test coverage detected