|
| 1 | +import AppKit |
| 2 | +import Foundation |
| 3 | +import LoopndrollCore |
| 4 | +import Observation |
| 5 | + |
| 6 | +@MainActor |
| 7 | +@Observable |
| 8 | +final class AppModel { |
| 9 | + var state: PersistedState = .defaultValue |
| 10 | + var installationSummary = "Checking installation..." |
| 11 | + var installationHealthy = false |
| 12 | + var busy = false |
| 13 | + var errorMessage: String? |
| 14 | + |
| 15 | + private let paths: LoopndrollPaths |
| 16 | + private let executableURL: URL |
| 17 | + private var hasStarted = false |
| 18 | + |
| 19 | + init( |
| 20 | + paths: LoopndrollPaths = .live(), |
| 21 | + executableURL: URL = Bundle.main.executableURL ?? URL(fileURLWithPath: CommandLine.arguments[0]) |
| 22 | + ) { |
| 23 | + self.paths = paths |
| 24 | + self.executableURL = executableURL |
| 25 | + } |
| 26 | + |
| 27 | + func startIfNeeded() { |
| 28 | + guard !hasStarted else { return } |
| 29 | + hasStarted = true |
| 30 | + |
| 31 | + Task { |
| 32 | + await refreshState() |
| 33 | + await repairInstallation() |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + func setEnabled(_ enabled: Bool) { |
| 38 | + Task { |
| 39 | + await updateState { state in |
| 40 | + state.config.enabled = enabled |
| 41 | + state.startNewActivation() |
| 42 | + } |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + func setMode(_ mode: LoopMode) { |
| 47 | + Task { |
| 48 | + await updateState { state in |
| 49 | + state.config.mode = mode |
| 50 | + state.startNewActivation() |
| 51 | + } |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + func setMaxTurns(_ value: Int) { |
| 56 | + Task { |
| 57 | + await updateState { state in |
| 58 | + state.config.maxTurns = max(1, value) |
| 59 | + state.startNewActivation() |
| 60 | + } |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + func setPromptTemplate(_ promptTemplate: String) { |
| 65 | + Task { |
| 66 | + await updateState { state in |
| 67 | + state.config.promptTemplate = promptTemplate |
| 68 | + state.touch() |
| 69 | + } |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + func resetBudgets() { |
| 74 | + Task { |
| 75 | + await updateState { state in |
| 76 | + state.startNewActivation() |
| 77 | + } |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + func repairNow() { |
| 82 | + Task { |
| 83 | + await repairInstallation() |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + func openCodexFolder() { |
| 88 | + NSWorkspace.shared.open(paths.codexDirectoryURL) |
| 89 | + } |
| 90 | + |
| 91 | + func openAppSupportFolder() { |
| 92 | + NSWorkspace.shared.open(paths.appDirectoryURL) |
| 93 | + } |
| 94 | + |
| 95 | + func quit() { |
| 96 | + NSApplication.shared.terminate(nil) |
| 97 | + } |
| 98 | + |
| 99 | + private func refreshState() async { |
| 100 | + let stateStore = StateStore(stateURL: paths.stateURL, lockURL: paths.lockURL) |
| 101 | + |
| 102 | + do { |
| 103 | + let loadedState = try await runIO { |
| 104 | + try stateStore.load() |
| 105 | + } |
| 106 | + state = loadedState |
| 107 | + errorMessage = nil |
| 108 | + } catch { |
| 109 | + errorMessage = "Failed to load Loopndroll state: \(error.localizedDescription)" |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + private func repairInstallation() async { |
| 114 | + busy = true |
| 115 | + defer { busy = false } |
| 116 | + |
| 117 | + do { |
| 118 | + let paths = self.paths |
| 119 | + let executableURL = self.executableURL |
| 120 | + let report = try await runIO { |
| 121 | + try InstallationManager(paths: paths).repair(using: executableURL) |
| 122 | + } |
| 123 | + |
| 124 | + installationHealthy = report.health.isHealthy |
| 125 | + if report.health.isHealthy { |
| 126 | + if report.didUpdateExecutable || report.didUpdateConfig || report.didUpdateHooks || report.didCreateState { |
| 127 | + installationSummary = "Installed and repaired." |
| 128 | + } else { |
| 129 | + installationSummary = "Installed and healthy." |
| 130 | + } |
| 131 | + } else { |
| 132 | + installationSummary = report.health.issues.joined(separator: " ") |
| 133 | + } |
| 134 | + |
| 135 | + if report.replacedMalformedHooks { |
| 136 | + errorMessage = "hooks.json was malformed and was replaced with a repaired file." |
| 137 | + } else { |
| 138 | + errorMessage = nil |
| 139 | + } |
| 140 | + |
| 141 | + await refreshState() |
| 142 | + } catch { |
| 143 | + installationHealthy = false |
| 144 | + installationSummary = "Installation needs repair." |
| 145 | + errorMessage = "Repair failed: \(error.localizedDescription)" |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + private func updateState(_ mutate: @escaping @Sendable (inout PersistedState) -> Void) async { |
| 150 | + busy = true |
| 151 | + defer { busy = false } |
| 152 | + |
| 153 | + let stateStore = StateStore(stateURL: paths.stateURL, lockURL: paths.lockURL) |
| 154 | + |
| 155 | + do { |
| 156 | + let updatedState = try await runIO { |
| 157 | + let (state, _) = try stateStore.mutate { state in |
| 158 | + mutate(&state) |
| 159 | + } |
| 160 | + return state |
| 161 | + } |
| 162 | + |
| 163 | + state = updatedState |
| 164 | + errorMessage = nil |
| 165 | + } catch { |
| 166 | + errorMessage = "Failed to save Loopndroll state: \(error.localizedDescription)" |
| 167 | + } |
| 168 | + } |
| 169 | + |
| 170 | + private func runIO<T: Sendable>(_ operation: @escaping @Sendable () throws -> T) async throws -> T { |
| 171 | + try await Task.detached(priority: .userInitiated, operation: operation).value |
| 172 | + } |
| 173 | +} |
0 commit comments