Enhances code documentation and structure for plugin #3
+40
-26
@@ -27,6 +27,8 @@ export class ModelService {
|
|||||||
private anthropicClient: AnthropicClient;
|
private anthropicClient: AnthropicClient;
|
||||||
private modelCache: ModelCache | null = null;
|
private modelCache: ModelCache | null = null;
|
||||||
private readonly CACHE_TTL_MS = 24 * 60 * 60 * 1000; // 24 hours
|
private readonly CACHE_TTL_MS = 24 * 60 * 60 * 1000; // 24 hours
|
||||||
|
private lastErrorNoticeTime = 0;
|
||||||
|
private readonly ERROR_NOTICE_THROTTLE_MS = 60 * 1000; // 1 minute
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a new model service
|
* Creates a new model service
|
||||||
@@ -37,6 +39,24 @@ export class ModelService {
|
|||||||
this.anthropicClient = anthropicClient;
|
this.anthropicClient = anthropicClient;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Converts ModelOption to ModelInfo format
|
||||||
|
*
|
||||||
|
* Helper to maintain consistent shape when caching fallback models.
|
||||||
|
* Keeps the mapping logic in one place for easier maintenance.
|
||||||
|
*
|
||||||
|
* @param opt - Model option to convert
|
||||||
|
* @returns ModelInfo with empty metadata fields
|
||||||
|
*/
|
||||||
|
private toModelInfo(opt: ModelOption): ModelInfo {
|
||||||
|
return {
|
||||||
|
id: opt.id,
|
||||||
|
display_name: opt.displayName,
|
||||||
|
created_at: '',
|
||||||
|
type: 'model'
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
// MARK: - Public Methods
|
// MARK: - Public Methods
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -59,12 +79,7 @@ export class ModelService {
|
|||||||
if (!this.anthropicClient.validateApiKey()) {
|
if (!this.anthropicClient.validateApiKey()) {
|
||||||
const fallback = this.getFallbackModels();
|
const fallback = this.getFallbackModels();
|
||||||
// Cache fallback models as ModelInfo for consistency
|
// Cache fallback models as ModelInfo for consistency
|
||||||
this.updateCache(fallback.map(f => ({
|
this.updateCache(fallback.map(f => this.toModelInfo(f)));
|
||||||
id: f.id,
|
|
||||||
display_name: f.displayName,
|
|
||||||
created_at: '',
|
|
||||||
type: 'model'
|
|
||||||
} as ModelInfo)));
|
|
||||||
return fallback;
|
return fallback;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -75,15 +90,10 @@ export class ModelService {
|
|||||||
return this.formatModels(response.data);
|
return this.formatModels(response.data);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Failed to fetch models from API:', error);
|
console.error('Failed to fetch models from API:', error);
|
||||||
new Notice('Fetching current models failed. Using hardcoded fallback list.');
|
this.showThrottledErrorNotice();
|
||||||
const fallback = this.getFallbackModels();
|
const fallback = this.getFallbackModels();
|
||||||
// Cache fallback models as ModelInfo for consistency
|
// Cache fallback models as ModelInfo for consistency
|
||||||
this.updateCache(fallback.map(f => ({
|
this.updateCache(fallback.map(f => this.toModelInfo(f)));
|
||||||
id: f.id,
|
|
||||||
display_name: f.displayName,
|
|
||||||
created_at: '',
|
|
||||||
type: 'model'
|
|
||||||
} as ModelInfo)));
|
|
||||||
return fallback;
|
return fallback;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -102,12 +112,7 @@ export class ModelService {
|
|||||||
if (!this.anthropicClient.validateApiKey()) {
|
if (!this.anthropicClient.validateApiKey()) {
|
||||||
const fallback = this.getFallbackModels();
|
const fallback = this.getFallbackModels();
|
||||||
// Cache fallback models as ModelInfo for consistency
|
// Cache fallback models as ModelInfo for consistency
|
||||||
this.updateCache(fallback.map(f => ({
|
this.updateCache(fallback.map(f => this.toModelInfo(f)));
|
||||||
id: f.id,
|
|
||||||
display_name: f.displayName,
|
|
||||||
created_at: '',
|
|
||||||
type: 'model'
|
|
||||||
} as ModelInfo)));
|
|
||||||
return fallback;
|
return fallback;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -117,15 +122,10 @@ export class ModelService {
|
|||||||
return this.formatModels(response.data);
|
return this.formatModels(response.data);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Failed to refresh models from API:', error);
|
console.error('Failed to refresh models from API:', error);
|
||||||
new Notice('Fetching current models failed. Using hardcoded fallback list.');
|
this.showThrottledErrorNotice();
|
||||||
const fallback = this.getFallbackModels();
|
const fallback = this.getFallbackModels();
|
||||||
// Cache fallback models as ModelInfo for consistency
|
// Cache fallback models as ModelInfo for consistency
|
||||||
this.updateCache(fallback.map(f => ({
|
this.updateCache(fallback.map(f => this.toModelInfo(f)));
|
||||||
id: f.id,
|
|
||||||
display_name: f.displayName,
|
|
||||||
created_at: '',
|
|
||||||
type: 'model'
|
|
||||||
} as ModelInfo)));
|
|
||||||
return fallback;
|
return fallback;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -175,6 +175,20 @@ export class ModelService {
|
|||||||
|
|
||||||
// MARK: - Private Methods
|
// MARK: - Private Methods
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Shows throttled error notice for API failures
|
||||||
|
*
|
||||||
|
* Prevents spamming user with error notices when API is persistently down.
|
||||||
|
* Only shows notice once per minute to avoid notification fatigue.
|
||||||
|
*/
|
||||||
|
private showThrottledErrorNotice(): void {
|
||||||
|
const now = Date.now();
|
||||||
|
if (now - this.lastErrorNoticeTime > this.ERROR_NOTICE_THROTTLE_MS) {
|
||||||
|
new Notice('Fetching current models failed. Using hardcoded fallback list.');
|
||||||
|
this.lastErrorNoticeTime = now;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Checks if the current cache is still valid
|
* Checks if the current cache is still valid
|
||||||
*
|
*
|
||||||
|
|||||||
Reference in new issue
Block a user