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 } } }