Files
Sprachmittler/Packages/SprachmittlerKit/Sources/SprachmittlerCore/OnboardingViewModel.swift
T
schihei a78572adc3 feat(m4-m6): session, panel, Replace, settings and onboarding
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.
2026-11-26 14:03:16 +01:00

136 lines
4.7 KiB
Swift

import Foundation
import Observation
import SprachmittlerEngines
import SprachmittlerOllama
/// One onboarding check.
public enum OnboardingCheck: Sendable, Equatable {
/// Not checked yet.
case idle
/// A check is running.
case checking
/// The check passed; the value is a short detail.
case ok(String)
/// The check failed; the value is a user-facing message.
case failed(String)
}
/// First-run onboarding state and actions (`docs/IMPLEMENTATION_PLAN.md` T6.2).
@MainActor
@Observable
public final class OnboardingViewModel {
/// Ollama reachability and version.
public private(set) var ollama: OnboardingCheck = .idle
/// Hy-MT3 installation.
public private(set) var model: OnboardingCheck = .idle
/// Accessibility trust (optional, for Replace).
public private(set) var accessibilityTrusted = false
/// The selected target.
public private(set) var target: ModelLanguage
/// The installed Hy-MT3 tags.
public private(set) var installedModels: [String] = []
/// True while any check is running.
public private(set) var isChecking = false
private let settingsStore: SettingsStore
private let catalogProvider: @Sendable (AppSettings) throws -> any OllamaCatalog
private let accessibility: any AccessibilityPermissionChecking
/// Creates the onboarding model.
public init(
settingsStore: SettingsStore,
catalogProvider: @escaping @Sendable (AppSettings) throws -> any OllamaCatalog,
accessibility: any AccessibilityPermissionChecking
) {
self.settingsStore = settingsStore
self.catalogProvider = catalogProvider
self.accessibility = accessibility
let settings = settingsStore.current()
target = settings.targetLanguageModel
accessibilityTrusted = accessibility.isTrusted
}
/// The command shown when the model is missing.
public var pullCommand: String {
"ollama pull \(settingsStore.current().model)"
}
/// True when the essential checks passed; the app can be used.
public var canFinish: Bool {
if case .ok = ollama, case .ok = model {
return true
}
return false
}
/// Runs all checks.
public func refresh() async {
isChecking = true
ollama = .checking
model = .checking
accessibilityTrusted = accessibility.isTrusted
do {
let settings = settingsStore.current()
let catalog = try catalogProvider(settings)
let version = try await catalog.serverVersion()
ollama = .ok(version)
let models = try await catalog.installedModels()
let tags = models.map(\.name).filter { HyMT3ModelPreset.isHyMT3($0) }
installedModels = tags
if tags.isEmpty {
model = .failed(SprachmittlerError.modelNotInstalled(model: settings.model).userMessage)
} else {
if !tags.contains(settings.model), let first = tags.first {
try? settingsStore.update { $0.model = first }
}
model = .ok(tags.joined(separator: ", "))
}
} catch let error as OllamaError {
let domain = SprachmittlerError(ollama: error)
ollama = .failed(domain.userMessage)
model = .failed(domain.userMessage)
} catch let error as SprachmittlerError {
ollama = .failed(error.userMessage)
model = .failed(error.userMessage)
} catch let rejection as EndpointRejection {
let message = SprachmittlerError.endpointRejected(reason: rejection).userMessage
ollama = .failed(message)
model = .failed(message)
} catch {
let message = "Ollama could not be reached."
ollama = .failed(message)
model = .failed(message)
}
isChecking = false
}
/// Saves the chosen target.
public func saveTarget() throws {
try settingsStore.update { $0.noteTarget(target) }
}
/// Sets the target; the caller saves.
public func setTarget(_ language: ModelLanguage) {
target = language
}
/// Requests Accessibility trust. Only call from onboarding.
public func requestAccessibility() {
accessibility.requestTrust()
accessibilityTrusted = accessibility.isTrusted
}
/// Opens the Accessibility settings page.
public func openAccessibilitySettings() {
accessibility.openSettings()
}
/// Marks onboarding complete after a successful setup.
public func complete() throws {
try saveTarget()
try settingsStore.update { $0.hasCompletedOnboarding = true }
}
}