Repository Public
1// Author: Simon-Pierre Boucher — contact@spboucher.ai2// File: Packages/LocalChatUI/Sources/App/AppModel.swift3// Description: The application model shared by the macOS and iOS apps: owns every service (stores, engine, hub, knowledge, nodes) and the observable state the views read.4import Foundation5import Observation6import SwiftUI7import LocalChatAgents8import LocalChatBenchmarks9import LocalChatConversations10import LocalChatCore11import LocalChatInference12import LocalChatKnowledge13import LocalChatModelHub14import LocalChatSecurity15import RemoteInference1617@MainActor @Observable18public final class AppModel {19 // MARK: Services (constant after init)20 public let paths: AppPaths21 public let store: ConversationStore22 public let attachments: AttachmentStore23 public let metrics: MetricsStore24 public let knowledgeStore: KnowledgeStore25 public let indexer: KnowledgeIndexer26 public let knowledge: KnowledgeEngine27 public let library: ModelLibrary28 public let downloads: DownloadManager29 public let hub: HuggingFaceAPI30 /// Live MLX catalog (mlx-community) used to propose models per device class.31 public let deviceCatalog: DeviceModelCatalog32 public let engine: InferenceEngine33 public let nodes: RemoteNodeRegistry34 public let keychain: KeychainStore35 public let providers: EmbeddingProviders36 public let isEphemeral: Bool3738 // MARK: Observable state39 public var preferences = AppPreferences()40 public var device = DeviceProfile.current()41 public var installedModels: [ModelDescriptor] = []42 public var remoteModels: [ModelDescriptor] = []43 public var systemModel: ModelDescriptor?44 public var nodeStatuses: [RemoteNodeStatus] = []45 public var backendIssues: [BackendKind: String] = [:] // backend → why it cannot run here46 public var snapshot = EngineSnapshot()47 public var downloadStates: [DownloadState] = []48 public var conversations: [Conversation] = []49 public var folders: [ChatFolder] = []50 public var tags: [TagCount] = []51 public var templates: [PromptTemplate] = []52 public var workspaces: [ModelWorkspace] = []53 public var agents: [AgentConfiguration] = []54 public var collections: [KnowledgeCollection] = []55 public var measuredSpeed: [String: Double] = [:]56 public var lastError: String?57 public var isReady = false5859 /// Biometric / passcode lock of the app (optional).60 public let lock: AppLock61 @ObservationIgnored private var serverStorage: ServerController?62 @ObservationIgnored private var syncStorage: SyncController?63 /// Local API server (macOS). Created on first use.64 public var server: ServerController { if let s = serverStorage { return s }; let s = ServerController(app: self); serverStorage = s; return s }65 /// Optional encrypted synchronization. Created on first use.66 public var sync: SyncController { if let s = syncStorage { return s }; let s = SyncController(app: self); syncStorage = s; return s }6768 @ObservationIgnored private var controllers: [UUID: ChatController] = [:]69 @ObservationIgnored private var tasks: [Task<Void, Never>] = []7071 public static let keychainService = "ai.spboucher.localchat"7273 /// `ephemeral` builds everything in a temporary folder (previews, UI tests, screenshots).74 public init(ephemeral: Bool = false, root: URL? = nil) throws {75 isEphemeral = ephemeral76 if let root { paths = AppPaths(root: root) }77 else if ephemeral { paths = AppPaths(root: FileManager.default.temporaryDirectory.appendingPathComponent("LocalChat-\(UUID().uuidString)")) }78 else { paths = .standard() }79 try FileManager.default.createDirectory(at: paths.root, withIntermediateDirectories: true)80 paths.excludeModelsFromBackup()8182 store = try ConversationStore(url: paths.database)83 lock = AppLock(isEnabled: ((try? store.preferences()) ?? AppPreferences()).appLockEnabled && !ephemeral)84 attachments = AttachmentStore(paths: paths)85 metrics = try MetricsStore(url: paths.metricsDatabase)86 knowledgeStore = try KnowledgeStore(url: paths.knowledgeDatabase)87 indexer = KnowledgeIndexer(store: knowledgeStore)88 library = ModelLibrary(paths: paths)89 let keychain = KeychainStore(service: Self.keychainService)90 self.keychain = keychain91 let hubToken: HuggingFaceAPI.TokenProvider = { try? keychain.string(for: "huggingface.token") }92 downloads = DownloadManager(paths: paths, library: library, tokenProvider: hubToken)93 hub = HuggingFaceAPI(tokenProvider: hubToken)94 deviceCatalog = DeviceModelCatalog(api: hub, cacheURL: paths.root.appendingPathComponent("mlx-catalog.json"))95 let apiKey: @Sendable (RemoteNode) -> String? = { node in try? keychain.string(for: node.keychainAccount) }96 nodes = RemoteNodeRegistry(fileURL: paths.root.appendingPathComponent("nodes.json"), apiKey: apiKey)97 let device = DeviceProfile.current()98 MLXSupport.configureMemory(for: device)99 engine = InferenceEngine(backends: [MLXBackend(), GGUFBackend(), AppleFoundationBackend(), RemoteBackend(registry: nodes, apiKey: apiKey)],100 modelsRoot: paths.models, device: device)101 let providers = EmbeddingProviders(engine: engine)102 self.providers = providers103 knowledge = KnowledgeEngine(store: knowledgeStore) { identifier in await providers.provider(for: identifier) }104 }105106 // MARK: Lifecycle107108 public func bootstrap() async {109 guard !isReady else { return }110 preferences = (try? store.preferences()) ?? AppPreferences()111 Haptics.enabled = preferences.hapticsEnabled112 _ = await library.reconcile()113 await downloads.restore()114 systemModel = AppleFoundationBackend().descriptor115 for kind in BackendKind.allCases { if let reason = await engine.unavailableReason(kind) { backendIssues[kind] = reason } }116 await refreshModels()117 await engine.setCatalog { [weak self] in await self?.allModels ?? [] }118 reloadLists()119 refreshMeasuredSpeed()120 observe()121 isReady = true122 Task { await refreshNodes() }123 }124125 private func observe() {126 let engine = engine, library = library, downloads = downloads, store = store127 tasks.append(Task { [weak self] in for await s in await engine.snapshots() { self?.snapshot = s } })128 tasks.append(Task { [weak self] in for await done in await engine.completions() { self?.record(done) } })129 tasks.append(Task { [weak self] in for await models in await library.changes() { self?.installedModels = models; await self?.providers.setModels(models) } })130 tasks.append(Task { [weak self] in131 for await states in await downloads.updates() {132 guard let self else { return }133 let finished = states.contains { s in s.installed != nil && !self.installedModels.contains { $0.id == s.installed?.id } }134 self.downloadStates = states135 if finished { await self.refreshModels(); Haptics.play(.success) }136 }137 })138 tasks.append(Task { [weak self] in for await list in store.observeConversations(ConversationFilter(archived: nil)) { self?.conversations = list; self?.reloadSecondaryLists() } })139 }140141 public func refreshModels() async {142 installedModels = await library.all()143 await providers.setModels(installedModels)144 }145146 public func refreshNodes() async {147 nodeStatuses = await nodes.refresh()148 remoteModels = await nodes.availableModels()149 }150151 public func reloadLists() {152 conversations = (try? store.conversations(ConversationFilter(archived: nil))) ?? []153 reloadSecondaryLists()154 templates = (try? store.templates()) ?? []155 workspaces = (try? store.workspaces()) ?? []156 agents = (try? store.agents()) ?? []157 collections = (try? knowledgeStore.collections()) ?? []158 }159 private func reloadSecondaryLists() {160 folders = (try? store.folders()) ?? []161 tags = (try? store.tags()) ?? []162 }163164 public func refreshDevice() { device = .current() }165166 public func refreshMeasuredSpeed() { measuredSpeed = (try? metrics.measuredSpeed(device: device.machine)) ?? [:] }167168 private func record(_ done: CompletedGeneration) {169 guard done.purpose == "chat" || done.purpose == "agent", done.metrics.stopReason != .error else { return }170 let m = done.descriptor171 try? metrics.insert(BenchmarkRecord(modelID: m.id, modelName: m.name, backend: m.backend, quantization: m.quantization, device: device.machine,172 kind: "chat", metrics: done.metrics, loadTime: done.loadTime, contextLength: done.contextLength))173 Task { await library.touch(id: m.id) }174 refreshMeasuredSpeed()175 if preferences.keepModelLoadedMinutes == 0, m.backend == .mlx || m.backend == .gguf {176 Task { try? await Task.sleep(for: .seconds(2)); await engine.unloadIdle(olderThan: 1) }177 }178 }179180 // MARK: Models181182 /// Every model the user can pick: installed, the system model, and models on trusted nodes.183 public var allModels: [ModelDescriptor] { installedModels + (systemModel.map { [$0] } ?? []) + remoteModels }184 public var chatModels: [ModelDescriptor] { allModels.filter { $0.capabilities.contains(.chat) && !$0.isEmbeddingOnly } }185 public var embeddingModels: [ModelDescriptor] { allModels.filter { $0.capabilities.contains(.embeddings) } }186 public func model(_ id: String?) -> ModelDescriptor? { id.flatMap { id in allModels.first { $0.id == id } } }187188 public func compatibility(_ model: ModelDescriptor, context: Int = 4096) -> CompatibilityVerdict {189 device.compatibility(of: model, backendAvailable: { backendIssues[$0] }, context: context)190 }191192 public var recommendations: [ModelRecommendation] {193 ModelRouter.recommendations(models: allModels, device: device, backendAvailable: { backendIssues[$0] }, measuredSpeed: measuredSpeed)194 }195196 /// The model a new conversation starts with: the user's default, else the best everyday fit, else anything usable.197 public var defaultModelID: String? {198 if let id = preferences.defaultModelID, model(id) != nil { return id }199 if let best = recommendations.first(where: { $0.category == .everyday }) { return best.model.id }200 return chatModels.first { compatibility($0).canAttemptLoad }?.id201 }202203 public func nodeName(for model: ModelDescriptor) -> String? {204 guard let id = model.remoteNodeID else { return nil }205 return nodeStatuses.first { $0.node.id == id }?.node.name206 }207208 public func deleteModel(_ model: ModelDescriptor) async {209 await engine.unload(model.id)210 do { try await library.delete(id: model.id) } catch { lastError = error.localizedDescription }211 try? metrics.deleteRecords(modelID: model.id)212 await refreshModels()213 }214215 // MARK: Preferences216217 public func updatePreferences(_ change: (inout AppPreferences) -> Void) {218 var p = preferences; change(&p)219 guard p != preferences else { return }220 preferences = (try? store.savePreferences(p)) ?? p221 Haptics.enabled = preferences.hapticsEnabled222 }223224 public var colorScheme: ColorScheme? {225 switch preferences.appearance { case "light": .light; case "dark": .dark; default: nil }226 }227228 // MARK: Conversations229230 @discardableResult231 public func newConversation(folder: UUID? = nil, workspace: ModelWorkspace? = nil, agent: AgentConfiguration? = nil) -> Conversation {232 var c = Conversation(folderID: folder, systemPrompt: preferences.defaultSystemPrompt, modelID: defaultModelID, preset: preferences.defaultPreset)233 c.settings = preferences.defaultPreset.settings(maxContext: 32768)234 if let workspace {235 c.workspaceID = workspace.id; c.modelID = workspace.modelID ?? c.modelID; c.systemPrompt = workspace.systemPrompt236 c.settings = workspace.settings; c.preset = workspace.preset; c.collectionIDs = workspace.collectionIDs; c.agentID = workspace.agentID237 }238 if let agent { c.agentID = agent.id; c.modelID = agent.modelID ?? c.modelID; c.collectionIDs = agent.collectionIDs; c.title = agent.name }239 return (try? store.saveConversation(c)) ?? c240 }241242 public func controller(for conversationID: UUID) -> ChatController? {243 if let live = controllers[conversationID] { return live }244 guard let conversation = try? store.conversation(conversationID) else { return nil }245 let controller = ChatController(app: self, conversation: conversation)246 controllers[conversationID] = controller247 // keep a handful of controllers; never drop one that is generating248 if controllers.count > 8, let victim = controllers.first(where: { $0.key != conversationID && !$0.value.isGenerating })?.key { controllers[victim] = nil }249 return controller250 }251252 /// True when that conversation is generating right now (never creates a controller).253 public func isGenerating(_ conversationID: UUID) -> Bool { controllers[conversationID]?.isGenerating ?? false }254255 public func deleteConversation(_ id: UUID) {256 controllers[id]?.stop(); controllers[id] = nil257 try? store.deleteConversation(id)258 Task { await engine.resetSession(modelID: "", key: id.uuidString) }259 }260261 public func update(_ conversation: Conversation) {262 _ = try? store.saveConversation(conversation)263 }264265 public var isAnyGenerationRunning: Bool { !snapshot.active.isEmpty }266267 // MARK: Agents268269 public var agentLogs: AgentLogStore { AgentLogStore(directory: paths.root.appendingPathComponent("AgentRuns", isDirectory: true)) }270271 /// Device-local agent permissions (never synchronized): folders the user granted and whether shell commands may be proposed.272 public var agentSettings: AgentLocalSettings {273 get { AgentLocalSettings.load(from: paths.root.appendingPathComponent("agent-settings.json")) }274 set { newValue.save(to: paths.root.appendingPathComponent("agent-settings.json")) }275 }276277 public var isShellPermitted: Bool {278 #if os(macOS)279 true280 #else281 false282 #endif283 }284285 public func makeAgentRunner(for agent: AgentConfiguration) -> (runner: AgentRunner, permissions: ToolPermissions, options: AgentRunOptions) {286 let local = agentSettings287 let registry = ToolRegistry.builtIn(shellPermitted: isShellPermitted && local.allowShell)288 let permissions = ToolPermissions(enabledTools: Set(agent.enabledTools), grantedFolders: local.resolvedFolders(), allowShell: isShellPermitted && local.allowShell,289 approval: agent.approval)290 return (AgentRunner(generator: engine, registry: registry, knowledge: knowledge), permissions, AgentRunOptions(allowTextToolCalls: local.allowTextToolCalls))291 }292}293294public struct AgentLocalSettings: Codable, Sendable, Hashable {295 public var folderBookmarks: [Data] = []296 public var folderPaths: [String] = [] // display only297 public var allowShell = false298 public var allowTextToolCalls = true299 public init() {}300301 static func load(from url: URL) -> AgentLocalSettings {302 (try? Data(contentsOf: url)).flatMap { try? JSONDecoder().decode(AgentLocalSettings.self, from: $0) } ?? AgentLocalSettings()303 }304 func save(to url: URL) { try? JSONEncoder().encode(self).write(to: url, options: .atomic) }305306 public mutating func grant(_ url: URL) {307 #if os(macOS)308 let options: URL.BookmarkCreationOptions = [.withSecurityScope]309 #else310 let options: URL.BookmarkCreationOptions = []311 #endif312 let scoped = url.startAccessingSecurityScopedResource()313 defer { if scoped { url.stopAccessingSecurityScopedResource() } }314 guard let data = try? url.bookmarkData(options: options, includingResourceValuesForKeys: nil, relativeTo: nil), !folderPaths.contains(url.path) else { return }315 folderBookmarks.append(data); folderPaths.append(url.path)316 }317 public mutating func revoke(at index: Int) {318 guard folderBookmarks.indices.contains(index) else { return }319 folderBookmarks.remove(at: index); folderPaths.remove(at: index)320 }321 /// Resolves the bookmarks and starts security-scoped access (kept for the life of the process).322 public func resolvedFolders() -> [URL] {323 folderBookmarks.compactMap { data in324 var stale = false325 #if os(macOS)326 let options: URL.BookmarkResolutionOptions = [.withSecurityScope]327 #else328 let options: URL.BookmarkResolutionOptions = []329 #endif330 guard let url = try? URL(resolvingBookmarkData: data, options: options, relativeTo: nil, bookmarkDataIsStale: &stale) else { return nil }331 _ = url.startAccessingSecurityScopedResource()332 return url333 }334 }335}336337/// Resolves the embedding provider of a knowledge collection from its identifier (system sentence embeddings or an installed embedding model).338public actor EmbeddingProviders {339 private let engine: InferenceEngine340 private var models: [ModelDescriptor] = []341 private var cache: [String: any EmbeddingProvider] = [:]342 init(engine: InferenceEngine) { self.engine = engine }343 func setModels(_ models: [ModelDescriptor]) { self.models = models }344345 public func provider(for identifier: String) async -> (any EmbeddingProvider)? {346 if let hit = cache[identifier] { return hit }347 var made: (any EmbeddingProvider)?348 if identifier.hasPrefix("apple.nl") { made = AppleNLEmbeddingProvider(identifier: identifier) }349 else {350 var model = models.first { $0.id == identifier }351 if model == nil { model = await engine.descriptor(for: identifier) }352 if let model { made = try? await EngineEmbeddingProvider.make(engine: engine, model: model) }353 }354 if let made { cache[identifier] = made }355 return made356 }357358 /// Identifier + display name of every embedder that can be used right now.359 public func available(sample: String = "") async -> [(id: String, name: String, remote: Bool)] {360 var out: [(String, String, Bool)] = AppleNLEmbeddingProvider.availableLanguages().compactMap { language in361 AppleNLEmbeddingProvider(language: language).map { ($0.identifier, "Apple sentence embeddings (\(language.rawValue), \($0.dimension)d)", false) }362 }363 for m in models where m.capabilities.contains(.embeddings) { out.append((m.id, m.name, m.backend == .remote)) }364 return out.map { (id: $0.0, name: $0.1, remote: $0.2) }365 }366}367