import Foundation import Observation import SprachmittlerEngines import SprachmittlerOllama /// Settings presentation model. It never stores source text or translations. @MainActor @Observable public final class SettingsViewModel { /// The editable draft. public var draft: AppSettings /// A settings validation or save error. public private(set) var saveError: String? /// A launch-at-login error; the other settings still saved. public private(set) var launchError: String? /// Installed Hy-MT3 tags, loaded on demand. public private(set) var installedModels: [InstalledModel] = [] /// True while refreshing the model list. public private(set) var isRefreshingModels = false private let store: SettingsStore private let catalogProvider: @Sendable (AppSettings) throws -> any OllamaCatalog private let launchAtLogin: any LaunchAtLoginControlling /// Creates the settings model. public init( store: SettingsStore, catalogProvider: @escaping @Sendable (AppSettings) throws -> any OllamaCatalog, launchAtLogin: any LaunchAtLoginControlling ) { self.store = store self.catalogProvider = catalogProvider self.launchAtLogin = launchAtLogin draft = store.current() draft.launchAtLogin = launchAtLogin.isEnabled } /// Reloads the draft from storage. public func reload() { draft = store.current() draft.launchAtLogin = launchAtLogin.isEnabled saveError = nil } /// Validates and saves the draft. The remote endpoint policy is enforced here. public func save() { draft.sanitize() saveError = nil launchError = nil do { _ = try draft.validatedEndpoint() try store.save(draft) } catch let rejection as EndpointRejection { saveError = SprachmittlerError.endpointRejected(reason: rejection).userMessage return } catch { saveError = "Settings could not be saved." return } do { try launchAtLogin.setEnabled(draft.launchAtLogin) } catch { launchError = "Launch at login could not be changed." } } /// Resets stored settings and the login item. public func reset() { draft = store.reset() try? launchAtLogin.setEnabled(false) saveError = nil launchError = nil } /// Refreshes the installed model list using the draft endpoint. public func refreshModels() async { isRefreshingModels = true defer { isRefreshingModels = false } do { let catalog = try catalogProvider(draft) installedModels = try await catalog.installedModels() .filter { HyMT3ModelPreset.isHyMT3($0.name) } saveError = nil } catch let rejection as EndpointRejection { saveError = SprachmittlerError.endpointRejected(reason: rejection).userMessage } catch let error as OllamaError { saveError = SprachmittlerError(ollama: error).userMessage } catch let error as SprachmittlerError { saveError = error.userMessage } catch { saveError = "Models could not be loaded." } } /// The model tags offered to the picker, including the current values. public var modelChoices: [String] { var tags = Set(installedModels.map(\.name)) tags.insert(draft.model) if let fast = draft.fastModel { tags.insert(fast) } return tags.sorted() } /// The licence text from the model card, not the Ollama field. public var licenceNote: String { "Hy-MT3 is Apache-2.0 per the model card. Sprachmittler bundles no weights." } /// The selected model digest, when installed. public var selectedDigest: String? { installedModels.first { $0.name == draft.model }?.digest } /// The installed Fast model candidate, if any. public var detectedFastModel: String? { installedModels.first { HyMT3ModelPreset.isFastModel($0.name) }?.name } }