M4: TranslationSession actor with epochs/one-in-flight streaming, PanelViewModel, non-activating NSPanel, streaming panel UI, searchable/recent target languages, warnings/errors and Copy/Retry/Replace actions. M5: InsertionCoordinator with target capture, AX direct replacement, secure-field/own-PID refusal, frontmost and selected-text verification, snapshot/⌘V/restore fallback, cancellation-safe restore and blocking-warning gate. M6: versioned AppSettings/SettingsStore, onboarding for Ollama/model/target/Accessibility, General/Model/Translation/Privacy/Diagnostics settings, launch-at-login via SMAppService, status menu (target, copy/replace last, setup, diagnostics, about), text-free diagnostics and first-run setup. Docs: PROGRESS M4–M6, ARCHITECTURE §10–§12, ADR-019/020, SELECTION_AND_CLIPBOARD and TESTING. Manual 10-app matrices, real AX/paste delivery, VoiceOver and focus checks remain open. Verified: make verify green (format, strict lint, scripts, privacy, Debug+Release signatures, 217 package tests); app launches and a two-copy clipboard smoke test opens the panel.
120 lines
4.1 KiB
Swift
120 lines
4.1 KiB
Swift
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
|
|
}
|
|
}
|