From 3435dfe5e3bff613037395c184f1abb24e3a8c39 Mon Sep 17 00:00:00 2001 From: Phantom <59059173+EurFelux@users.noreply.github.com> Date: Thu, 7 Aug 2025 12:33:25 +0800 Subject: [PATCH] fix(settings): reading undefined caused crash (#8901) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix(settings): 修复OpenAI推理条件中provider未定义的判断问题 修复useProvider钩子中provider可能为undefined时的模型获取逻辑 * feat(provider): 添加供应商不存在时的默认回退逻辑 当供应商不存在时,自动回退到默认供应商并显示警告信息 * feat(i18n): 添加缺失供应商的警告信息翻译 * fix(SettingsTab): 移除对provider的冗余检查 --- src/renderer/src/hooks/useProvider.ts | 13 +++++++++++-- src/renderer/src/i18n/locales/en-us.json | 3 +++ src/renderer/src/i18n/locales/ja-jp.json | 3 +++ src/renderer/src/i18n/locales/ru-ru.json | 3 +++ src/renderer/src/i18n/locales/zh-cn.json | 3 +++ src/renderer/src/i18n/locales/zh-tw.json | 3 +++ 6 files changed, 26 insertions(+), 2 deletions(-) diff --git a/src/renderer/src/hooks/useProvider.ts b/src/renderer/src/hooks/useProvider.ts index e02bfa2dcf..fa25821c3e 100644 --- a/src/renderer/src/hooks/useProvider.ts +++ b/src/renderer/src/hooks/useProvider.ts @@ -1,5 +1,6 @@ import { createSelector } from '@reduxjs/toolkit' import { isSystemProvider } from '@renderer/config/providers' +import { getDefaultProvider } from '@renderer/services/AssistantService' import { useAppDispatch, useAppSelector } from '@renderer/store' import { addModel, @@ -11,6 +12,8 @@ import { updateProviders } from '@renderer/store/llm' import { Assistant, Model, Provider } from '@renderer/types' +import { getFancyProviderName } from '@renderer/utils' +import { useTranslation } from 'react-i18next' import { useDefaultModel } from './useAssistant' @@ -45,12 +48,18 @@ export function useAllProviders() { } export function useProvider(id: string) { - const provider = useAppSelector((state) => state.llm.providers.find((p) => p.id === id) as Provider) + let provider = useAppSelector((state) => state.llm.providers.find((p) => p.id === id)) const dispatch = useAppDispatch() + const { t } = useTranslation() + + if (!provider) { + provider = getDefaultProvider() + window.message.warning(t('warning.missing_provider', { provider: getFancyProviderName(provider) })) + } return { provider, - models: provider?.models || [], + models: provider?.models ?? [], updateProvider: (updates: Partial) => dispatch(updateProvider({ id, ...updates })), addModel: (model: Model) => dispatch(addModel({ providerId: id, model })), removeModel: (model: Model) => dispatch(removeModel({ providerId: id, model })), diff --git a/src/renderer/src/i18n/locales/en-us.json b/src/renderer/src/i18n/locales/en-us.json index 6c477ad4ca..13857b0703 100644 --- a/src/renderer/src/i18n/locales/en-us.json +++ b/src/renderer/src/i18n/locales/en-us.json @@ -3565,6 +3565,9 @@ "noReleaseNotes": "No release notes", "title": "Update" }, + "warning": { + "missing_provider": "The supplier does not exist; reverted to the default supplier {{provider}}. This may cause issues." + }, "words": { "knowledgeGraph": "Knowledge Graph", "quit": "Quit", diff --git a/src/renderer/src/i18n/locales/ja-jp.json b/src/renderer/src/i18n/locales/ja-jp.json index c7e3498b08..edd6df5641 100644 --- a/src/renderer/src/i18n/locales/ja-jp.json +++ b/src/renderer/src/i18n/locales/ja-jp.json @@ -3565,6 +3565,9 @@ "noReleaseNotes": "暫無更新日誌", "title": "更新" }, + "warning": { + "missing_provider": "サプライヤーが存在しないため、デフォルトのサプライヤー {{provider}} にロールバックされました。これにより問題が発生する可能性があります。" + }, "words": { "knowledgeGraph": "ナレッジグラフ", "quit": "終了", diff --git a/src/renderer/src/i18n/locales/ru-ru.json b/src/renderer/src/i18n/locales/ru-ru.json index 41d2ee01fc..19832f22ef 100644 --- a/src/renderer/src/i18n/locales/ru-ru.json +++ b/src/renderer/src/i18n/locales/ru-ru.json @@ -3565,6 +3565,9 @@ "noReleaseNotes": "Нет заметок об обновлении", "title": "Обновление" }, + "warning": { + "missing_provider": "Поставщик не существует, возвращение к поставщику по умолчанию {{provider}}. Это может привести к проблемам." + }, "words": { "knowledgeGraph": "Граф знаний", "quit": "Выйти", diff --git a/src/renderer/src/i18n/locales/zh-cn.json b/src/renderer/src/i18n/locales/zh-cn.json index ce49fe7b2e..5a1c49187d 100644 --- a/src/renderer/src/i18n/locales/zh-cn.json +++ b/src/renderer/src/i18n/locales/zh-cn.json @@ -3565,6 +3565,9 @@ "noReleaseNotes": "暂无更新日志", "title": "更新提示" }, + "warning": { + "missing_provider": "供应商不存在,已回退到默认供应商 {{provider}}。这可能导致问题。" + }, "words": { "knowledgeGraph": "知识图谱", "quit": "退出", diff --git a/src/renderer/src/i18n/locales/zh-tw.json b/src/renderer/src/i18n/locales/zh-tw.json index 3b4511338f..6b74f9cf05 100644 --- a/src/renderer/src/i18n/locales/zh-tw.json +++ b/src/renderer/src/i18n/locales/zh-tw.json @@ -3565,6 +3565,9 @@ "noReleaseNotes": "暫無更新日誌", "title": "更新提示" }, + "warning": { + "missing_provider": "供應商不存在,已回退到預設供應商 {{provider}}。這可能導致問題。" + }, "words": { "knowledgeGraph": "知識圖譜", "quit": "結束",