Improves model selection fallback handling

Adds null check when selecting fallback model to prevent potential errors

Ensures graceful handling when the current model becomes unavailable by verifying a fallback model exists before setting it
This commit is contained in:
AlexKucera committed 2025-10-24 11:27:50 +02:00
1 parent e06b07eb26
commit e8443a878d
1 file changed
+8 -5
+8 -5
View File
@@ -245,15 +245,18 @@ export class FreewritingPromptsSettingTab extends PluginSettingTab {
const modelExists = this.availableModels.some(m => m.id === currentModel);
if (modelExists) {
this.modelDropdown.setValue(currentModel);
} else if (this.availableModels.length > 0) {
} else {
// Fall back to first available model if current one is gone
this.plugin.settings.model = this.availableModels[0].id;
this.modelDropdown.setValue(this.availableModels[0].id);
new Notice('Your selected model is no longer available. Defaulting to ' + this.availableModels[0].displayName);
const fallbackModel = this.availableModels[0];
if (fallbackModel) {
this.modelDropdown.setValue(fallbackModel.id);
this.plugin.settings.model = fallbackModel.id;
new Notice('Your selected model is no longer available. Defaulting to ' + fallbackModel.displayName);
}
}
}
// Save updated cache
// Persist any selection changes
await this.plugin.saveSettings();
} catch (error) {
console.error('Error refreshing models:', error);