From e06b07eb26985d52783dcfb55e51de365d0e60f8 Mon Sep 17 00:00:00 2001 From: Alexander Kucera Date: Fri, 24 Oct 2025 11:26:48 +0200 Subject: [PATCH] Adds API key validation and model sorting Improves model retrieval by adding API key validation check for both model fetching methods Adds fallback to predefined models when no API key is present Sorts model lists alphabetically by display name for consistent presentation Enhances robustness of model service handling --- services/modelService.ts | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/services/modelService.ts b/services/modelService.ts index 29166f6..9fe2743 100644 --- a/services/modelService.ts +++ b/services/modelService.ts @@ -32,6 +32,11 @@ export class ModelService { return this.formatModels(this.modelCache.models); } + // If no API key, quietly return fallback + if (!this.anthropicClient.validateApiKey()) { + return this.getFallbackModels(); + } + // Try to fetch from API try { const response = await this.anthropicClient.fetchModels(); @@ -49,6 +54,11 @@ export class ModelService { * Used when user changes API key */ async refreshModels(): Promise { + // If no API key, quietly return fallback + if (!this.anthropicClient.validateApiKey()) { + return this.getFallbackModels(); + } + try { const response = await this.anthropicClient.fetchModels(); this.updateCache(response.data); @@ -114,7 +124,7 @@ export class ModelService { return models.map(model => ({ id: model.id, displayName: model.display_name || model.id - })); + })).sort((a, b) => a.displayName.localeCompare(b.displayName)); } /** @@ -124,6 +134,6 @@ export class ModelService { return ANTHROPIC_MODELS.map(id => ({ id, displayName: id - })); + })).sort((a, b) => a.displayName.localeCompare(b.displayName)); } }