From ecb57d794ce9e74c70d4ae66839d48ca95a07b3d Mon Sep 17 00:00:00 2001 From: Alexander Kucera Date: Fri, 24 Oct 2025 15:43:37 +0200 Subject: [PATCH] Refactors model fallback retrieval with new helper method Extracts common fallback model caching logic into a new private method `cacheFallbackModels()` Reduces code duplication in model retrieval methods by centralizing fallback model caching Improves code readability and maintainability by consolidating repetitive logic Adds descriptive docstring to explain the purpose of the new helper method --- services/modelService.ts | 35 +++++++++++++++++++---------------- 1 file changed, 19 insertions(+), 16 deletions(-) diff --git a/services/modelService.ts b/services/modelService.ts index cce5c73..fd6cebd 100644 --- a/services/modelService.ts +++ b/services/modelService.ts @@ -77,10 +77,7 @@ export class ModelService { // If no API key, immediately return and cache fallback (no Notice needed) if (!this.anthropicClient.validateApiKey()) { - const fallback = this.getFallbackModels(); - // Cache fallback models as ModelInfo for consistency - this.updateCache(fallback.map(f => this.toModelInfo(f))); - return fallback; + return this.cacheFallbackModels(); } // Try to fetch from API @@ -91,10 +88,7 @@ export class ModelService { } catch (error) { console.error('Failed to fetch models from API:', error); this.showThrottledErrorNotice(); - const fallback = this.getFallbackModels(); - // Cache fallback models as ModelInfo for consistency - this.updateCache(fallback.map(f => this.toModelInfo(f))); - return fallback; + return this.cacheFallbackModels(); } } @@ -110,10 +104,7 @@ export class ModelService { async refreshModels(): Promise { // If no API key, immediately return and cache fallback if (!this.anthropicClient.validateApiKey()) { - const fallback = this.getFallbackModels(); - // Cache fallback models as ModelInfo for consistency - this.updateCache(fallback.map(f => this.toModelInfo(f))); - return fallback; + return this.cacheFallbackModels(); } try { @@ -123,10 +114,7 @@ export class ModelService { } catch (error) { console.error('Failed to refresh models from API:', error); this.showThrottledErrorNotice(); - const fallback = this.getFallbackModels(); - // Cache fallback models as ModelInfo for consistency - this.updateCache(fallback.map(f => this.toModelInfo(f))); - return fallback; + return this.cacheFallbackModels(); } } @@ -175,6 +163,21 @@ export class ModelService { // MARK: - Private Methods + /** + * Caches fallback models as ModelInfo for consistency + * + * Helper that retrieves fallback models and updates the cache with them + * converted to ModelInfo format. This consolidates the common pattern of + * falling back to hardcoded models when the API is unavailable. + * + * @returns Array of fallback model options + */ + private cacheFallbackModels(): ModelOption[] { + const fallback = this.getFallbackModels(); + this.updateCache(fallback.map(f => this.toModelInfo(f))); + return fallback; + } + /** * Shows throttled error notice for API failures *