From ee9c4cb23b9bc416a4f86b3f2e4c283ca2a536f5 Mon Sep 17 00:00:00 2001 From: perf3ct Date: Mon, 31 Mar 2025 18:28:21 +0000 Subject: [PATCH] fix the model selection setting to not be overridden by loading available model list --- .../options/ai_settings/providers.ts | 85 +++++++++++-------- 1 file changed, 49 insertions(+), 36 deletions(-) diff --git a/src/public/app/widgets/type_widgets/options/ai_settings/providers.ts b/src/public/app/widgets/type_widgets/options/ai_settings/providers.ts index c5dfc71d3..2488f9ed3 100644 --- a/src/public/app/widgets/type_widgets/options/ai_settings/providers.ts +++ b/src/public/app/widgets/type_widgets/options/ai_settings/providers.ts @@ -1,10 +1,53 @@ import server from "../../../../services/server.js"; import toastService from "../../../../services/toast.js"; import { t } from "../../../../services/i18n.js"; +import options from "../../../../services/options.js"; import { OpenAIModelResponse, AnthropicModelResponse, OllamaModelResponse } from "./interfaces.js"; export class ProviderService { - constructor(private $widget: JQuery) {} + constructor(private $widget: JQuery) { + // Initialize Voyage models (since they don't have a dynamic refresh yet) + this.initializeVoyageModels(); + } + + /** + * Initialize Voyage models with default values and ensure proper selection + */ + private initializeVoyageModels() { + setTimeout(() => { + const $voyageModelSelect = this.$widget.find('.voyage-embedding-model'); + if ($voyageModelSelect.length > 0) { + const currentValue = $voyageModelSelect.val(); + this.ensureSelectedValue($voyageModelSelect, currentValue, 'voyageEmbeddingModel'); + } + }, 100); // Small delay to ensure the widget is fully initialized + } + + /** + * Ensures the dropdown has the correct value set, prioritizing: + * 1. Current UI value if present + * 2. Value from database options if available + * 3. Falling back to first option if neither is available + */ + private ensureSelectedValue($select: JQuery, currentValue: string | number | string[] | undefined | null, optionName: string) { + if (currentValue) { + $select.val(currentValue); + // If the value doesn't exist anymore, select the first option + if (!$select.val()) { + $select.prop('selectedIndex', 0); + } + } else { + // If no current value exists in the dropdown but there's a default in the database + const savedModel = options.get(optionName); + if (savedModel) { + $select.val(savedModel); + // If the saved model isn't in the dropdown, select the first option + if (!$select.val()) { + $select.prop('selectedIndex', 0); + } + } + } + } /** * Refreshes the list of OpenAI models @@ -49,13 +92,7 @@ export class ProviderService { }); // Try to restore the previously selected value - if (currentChatValue) { - $chatModelSelect.val(currentChatValue); - // If the value doesn't exist anymore, select the first option - if (!$chatModelSelect.val()) { - $chatModelSelect.prop('selectedIndex', 0); - } - } + this.ensureSelectedValue($chatModelSelect, currentChatValue, 'openaiDefaultModel'); } // Update the embedding models dropdown @@ -75,13 +112,7 @@ export class ProviderService { }); // Try to restore the previously selected value - if (currentEmbedValue) { - $embedModelSelect.val(currentEmbedValue); - // If the value doesn't exist anymore, select the first option - if (!$embedModelSelect.val()) { - $embedModelSelect.prop('selectedIndex', 0); - } - } + this.ensureSelectedValue($embedModelSelect, currentEmbedValue, 'openaiEmbeddingModel'); } if (showLoading) { @@ -153,13 +184,7 @@ export class ProviderService { }); // Try to restore the previously selected value - if (currentChatValue) { - $chatModelSelect.val(currentChatValue); - // If the value doesn't exist anymore, select the first option - if (!$chatModelSelect.val()) { - $chatModelSelect.prop('selectedIndex', 0); - } - } + this.ensureSelectedValue($chatModelSelect, currentChatValue, 'anthropicDefaultModel'); } // Handle embedding models if they exist @@ -247,13 +272,7 @@ export class ProviderService { }); // Try to restore the previously selected value - if (currentValue) { - $embedModelSelect.val(currentValue); - // If the value doesn't exist anymore, select the first option - if (!$embedModelSelect.val()) { - $embedModelSelect.prop('selectedIndex', 0); - } - } + this.ensureSelectedValue($embedModelSelect, currentValue, 'ollamaEmbeddingModel'); // Also update the LLM model dropdown const $modelSelect = this.$widget.find('.ollama-default-model'); @@ -271,13 +290,7 @@ export class ProviderService { }); // Try to restore the previously selected value - if (currentModelValue) { - $modelSelect.val(currentModelValue); - // If the value doesn't exist anymore, select the first option - if (!$modelSelect.val()) { - $modelSelect.prop('selectedIndex', 0); - } - } + this.ensureSelectedValue($modelSelect, currentModelValue, 'ollamaDefaultModel'); if (showLoading) { toastService.showMessage(`${response.models.length} Ollama models found.`);