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
This commit is contained in:
1 file changed
+19
-16
+19
-16
@@ -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<ModelOption[]> {
|
||||
// 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
|
||||
*
|
||||
|
||||
Reference in new issue
Block a user