diff --git a/Easydict/Swift/Feature/Configuration/Configuration+Defaults.swift b/Easydict/Swift/Feature/Configuration/Configuration+Defaults.swift index 02003148e..752cb596d 100644 --- a/Easydict/Swift/Feature/Configuration/Configuration+Defaults.swift +++ b/Easydict/Swift/Feature/Configuration/Configuration+Defaults.swift @@ -206,6 +206,10 @@ extension Defaults.Keys { EZOpenAIAvailableModelsKey, default: OpenAIModel.allCases.map { $0.rawValue }.joined(separator: ",") ) + static let openAIVaildModels = Key( + "EZOpenAIValidModelsKey", + default: OpenAIModel.allCases.map { $0.rawValue } + ) // Custom OpenAI static let customOpenAINameKey = Key( @@ -223,6 +227,10 @@ extension Defaults.Keys { static let customOpenAIEndPoint = Key(EZCustomOpenAIEndPointKey, default: "") static let customOpenAIModel = Key(EZCustomOpenAIModelKey, default: "") static let customOpenAIModelsAvailable = Key(EZCustomOpenAIModelssAvailableKey, default: "") + static let customOpenAIVaildModels = Key( + "EZCustomOpenAIValidModelsKey", + default: [""] + ) // DeepL static let deepLAuth = Key(EZDeepLAuthKey) diff --git a/Easydict/Swift/Service/CustomOpenAI/CustomOpenAIService.swift b/Easydict/Swift/Service/CustomOpenAI/CustomOpenAIService.swift index abafaa320..eea956b6f 100644 --- a/Easydict/Swift/Service/CustomOpenAI/CustomOpenAIService.swift +++ b/Easydict/Swift/Service/CustomOpenAI/CustomOpenAIService.swift @@ -50,9 +50,7 @@ class CustomOpenAIService: BaseOpenAIService { } override var availableModels: [String] { - let models = Defaults[.customOpenAIModelsAvailable] - guard let models, !models.isEmpty else { return [] } - return models.components(separatedBy: ",").filter { !$0.isEmpty } + Defaults[.customOpenAIVaildModels] } override func serviceType() -> ServiceType { diff --git a/Easydict/Swift/Service/OpenAI/BaseOpenAIService.swift b/Easydict/Swift/Service/OpenAI/BaseOpenAIService.swift index 688fe5b67..11d7f4e35 100644 --- a/Easydict/Swift/Service/OpenAI/BaseOpenAIService.swift +++ b/Easydict/Swift/Service/OpenAI/BaseOpenAIService.swift @@ -124,10 +124,7 @@ public class BaseOpenAIService: QueryService { // MARK: Internal var availableModels: [String] { - let models = Defaults[.openAIAvailableModels] - guard let models, !models.isEmpty else { return [] } - return models.components(separatedBy: ",").map { $0.trimmingCharacters(in: .whitespacesAndNewlines) } - .filter { !$0.isEmpty } + Defaults[.openAIVaildModels] } var model: String { diff --git a/Easydict/Swift/View/SettingView/Tabs/ServiceConfigurationView/QueryService+ConfigurableService/CustomOpenAIService+ConfigurableService.swift b/Easydict/Swift/View/SettingView/Tabs/ServiceConfigurationView/QueryService+ConfigurableService/CustomOpenAIService+ConfigurableService.swift index 67a2ddc18..7bc42dd88 100644 --- a/Easydict/Swift/View/SettingView/Tabs/ServiceConfigurationView/QueryService+ConfigurableService/CustomOpenAIService+ConfigurableService.swift +++ b/Easydict/Swift/View/SettingView/Tabs/ServiceConfigurationView/QueryService+ConfigurableService/CustomOpenAIService+ConfigurableService.swift @@ -158,10 +158,13 @@ private class CustomOpenAIViewModel: ObservableObject { validModels = [] return } - validModels = availableModels.components(separatedBy: ",").filter { !$0.isEmpty } + validModels = availableModels.components(separatedBy: ",") + .map { $0.trimmingCharacters(in: .whitespacesAndNewlines) }.filter { !$0.isEmpty } if validModels.count == 1 || !validModels.contains(model) { model = validModels[0] } + + Defaults[.customOpenAIVaildModels] = validModels } private func serviceConfigChanged() { diff --git a/Easydict/Swift/View/SettingView/Tabs/ServiceConfigurationView/QueryService+ConfigurableService/OpenAIService+ConfigurableService.swift b/Easydict/Swift/View/SettingView/Tabs/ServiceConfigurationView/QueryService+ConfigurableService/OpenAIService+ConfigurableService.swift index b81972cfa..aa5837f03 100644 --- a/Easydict/Swift/View/SettingView/Tabs/ServiceConfigurationView/QueryService+ConfigurableService/OpenAIService+ConfigurableService.swift +++ b/Easydict/Swift/View/SettingView/Tabs/ServiceConfigurationView/QueryService+ConfigurableService/OpenAIService+ConfigurableService.swift @@ -149,6 +149,8 @@ private class OpenAIServiceViewModel: ObservableObject { if validModels.count == 1 || !validModels.contains(model) { model = validModels[0] } + + Defaults[.openAIVaildModels] = validModels } private func serviceConfigChanged() {