From 063b79477f5f264d59384b7cf4bdedc64a89c66a Mon Sep 17 00:00:00 2001 From: misyaguziya <53165965+misyaguziya@users.noreply.github.com> Date: Fri, 26 Dec 2025 05:30:37 +0900 Subject: [PATCH] [Fix] Error Handling: Enhance authentication checks and add support for Groq and OpenRouter models. --- .../translation/translation_openrouter.py | 18 +++---- src-ui/logics/_useBackendErrorHandling.js | 52 +++++++++++++++++++ 2 files changed, 58 insertions(+), 12 deletions(-) diff --git a/src-python/models/translation/translation_openrouter.py b/src-python/models/translation/translation_openrouter.py index 20746bbf..5df2ec44 100644 --- a/src-python/models/translation/translation_openrouter.py +++ b/src-python/models/translation/translation_openrouter.py @@ -13,26 +13,20 @@ except Exception: from translation_utils import loadTranslatePromptConfig translation_lang = loadTranslationLanguages(path=".", force=True) -def _authentication_check(api_key: str) -> bool: +def _authentication_check(api_key: str, base_url: str | None = None) -> bool: """Check if the provided API key is valid by attempting to list models. """ try: - client = OpenAI( - api_key=api_key, - base_url="https://openrouter.ai/api/v1", - ) + client = OpenAI(api_key=api_key, base_url=base_url) client.models.list() return True except Exception: return False -def _get_available_text_models(api_key: str) -> list[str]: +def _get_available_text_models(api_key: str, base_url: str | None = None) -> list[str]: """Extract only OpenRouter models suitable for translation and chat applications. """ - client = OpenAI( - api_key=api_key, - base_url="https://openrouter.ai/api/v1", - ) + client = OpenAI(api_key=api_key, base_url=base_url) res = client.models.list() allowed_models = [] @@ -90,13 +84,13 @@ class OpenRouterClient: self.openrouter_llm = None def getModelList(self) -> list[str]: - return _get_available_text_models(self.api_key) if self.api_key else [] + return _get_available_text_models(self.api_key, self.base_url) if self.api_key else [] def getAuthKey(self) -> str: return self.api_key def setAuthKey(self, api_key: str) -> bool: - result = _authentication_check(api_key) + result = _authentication_check(api_key, self.base_url) if result: self.api_key = api_key return result diff --git a/src-ui/logics/_useBackendErrorHandling.js b/src-ui/logics/_useBackendErrorHandling.js index 4d494fe9..fce938f3 100644 --- a/src-ui/logics/_useBackendErrorHandling.js +++ b/src-ui/logics/_useBackendErrorHandling.js @@ -48,6 +48,12 @@ export const _useBackendErrorHandling = () => { updateOpenAIAuthKey, updateSelectedOpenAIModel, + updateGroqAuthKey, + updateSelectedGroqModel, + + updateOpenRouterAuthKey, + updateSelectedOpenRouterModel, + updateLMStudioURL, updateSelectedLMStudioModel, @@ -220,6 +226,52 @@ export const _useBackendErrorHandling = () => { } return; + case "/set/data/groq_auth_key": + if (message === "Groq auth key is not valid") { + updateGroqAuthKey(data); + showNotification_Error(message, { category_id: "groq_auth_key" }); + } else if (message === "Authentication failure of Groq auth key") { + updateGroqAuthKey(data); + showNotification_Error(message, { category_id: "groq_auth_key" }); + } else { + updateGroqAuthKey(data); + showNotification_Error(message, { category_id: "groq_auth_key" }); + } + return; + + case "/set/data/selected_groq_model": + if (message === "Groq model is not valid") { + updateSelectedGroqModel(data); + showNotification_Error(message, { category_id: "selected_groq_model" }); + } else { + updateSelectedGroqModel(data); + showNotification_Error(message, { category_id: "selected_groq_model" }); + } + return; + + case "/set/data/openrouter_auth_key": + if (message === "OpenRouter auth key is not valid") { + updateOpenRouterAuthKey(data); + showNotification_Error(message, { category_id: "openrouter_auth_key" }); + } else if (message === "Authentication failure of OpenRouter auth key") { + updateOpenRouterAuthKey(data); + showNotification_Error(message, { category_id: "openrouter_auth_key" }); + } else { + updateOpenRouterAuthKey(data); + showNotification_Error(message, { category_id: "openrouter_auth_key" }); + } + return; + + case "/set/data/selected_openrouter_model": + if (message === "OpenRouter model is not valid") { + updateSelectedOpenRouterModel(data); + showNotification_Error(message, { category_id: "selected_openrouter_model" }); + } else { + updateSelectedOpenRouterModel(data); + showNotification_Error(message, { category_id: "selected_openrouter_model" }); + } + return; + case "/set/data/lmstudio_url": if (message === "LMStudio URL is not valid") { updateLMStudioURL(data);