Repository Public
1// Author: Simon-Pierre Boucher — contact@spboucher.ai2// File: Packages/LocalChatUI/Sources/App/ChatController.swift3// Description: Drives one conversation: sending, streaming, stopping, branching (edit / regenerate), retrieval, context assembly and compaction, automatic titles.4import Foundation5import Observation6import SwiftUI7import LocalChatAgents8import LocalChatConversations9import LocalChatCore10import LocalChatInference11import LocalChatKnowledge1213public struct PendingAttachment: Identifiable, Hashable, Sendable {14 public var id = UUID()15 public var fileName: String16 public var mimeType: String17 public var data: Data18 public var kind: Attachment.Kind19 public var extractedText: String?20 public var warning: String?21}2223@MainActor @Observable24public final class ChatController {25 public private(set) var conversation: Conversation26 public private(set) var messages: [ChatMessage] = [] // the active branch, root → leaf27 public private(set) var tree = ConversationTree([])28 public private(set) var isGenerating = false29 public private(set) var streamingMessageID: UUID?30 public private(set) var liveText = ""31 public private(set) var liveReasoning = ""32 public private(set) var status: String? // "Loading model…", "Searching knowledge…"33 public private(set) var context: AssembledContext?34 public private(set) var routing: RoutingDecision?35 public private(set) var pendingApproval: ApprovalRequest?36 public private(set) var liveToolActivity: [ToolActivity] = []37 public var errorText: String?38 public var draft = ""39 public var pendingAttachments: [PendingAttachment] = []4041 public struct ToolActivity: Identifiable, Hashable, Sendable {42 public var id: String43 public var name: String44 public var arguments: String45 public var output: String?46 public var isError = false47 public var isRunning = true48 }4950 @ObservationIgnored private unowned let app: AppModel51 @ObservationIgnored private var generation: Task<Void, Never>?52 @ObservationIgnored private var observers: [Task<Void, Never>] = []53 @ObservationIgnored private var approvalContinuation: CheckedContinuation<ApprovalDecision, Never>?5455 init(app: AppModel, conversation: Conversation) {56 self.app = app; self.conversation = conversation57 reload()58 let store = app.store, id = conversation.id59 observers.append(Task { [weak self] in for await _ in store.observeMessages(in: id) { self?.storeChanged() } })60 observers.append(Task { [weak self] in for await _ in store.observeConversation(id) { self?.storeChanged() } })61 }6263 public var model: ModelDescriptor? { app.model(conversation.modelID) }64 public var agent: AgentConfiguration? { conversation.agentID.flatMap { id in app.agents.first { $0.id == id } } }65 public var canSend: Bool { !isGenerating && (!draft.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty || !pendingAttachments.isEmpty) }6667 // MARK: State sync6869 private func reload() {70 tree = ConversationTree((try? app.store.messages(in: conversation.id)) ?? [])71 messages = tree.path(to: conversation.activeLeafID)72 }73 /// Observation events are only "something changed" signals: they can be delivered late, after newer writes made by this controller.74 /// The current state is therefore always read back from the store, never taken from the event payload.75 private func storeChanged() {76 if let fresh = try? app.store.conversation(conversation.id) { conversation = fresh }77 reload()78 }7980 /// Applies an edit to the conversation record (model, prompt, settings, tags…).81 public func update(_ change: (inout Conversation) -> Void) {82 var c = conversation; change(&c)83 guard c != conversation else { return }84 conversation = (try? app.store.saveConversation(c)) ?? c85 Task { await refreshContext() }86 }8788 public func setPreset(_ preset: InferencePreset) {89 let limit = model?.contextLength ?? 3276890 update { $0.preset = preset; $0.settings = preset.settings(base: $0.settings, maxContext: min(limit, 131072)) }91 }9293 public func setModel(_ id: String) {94 update { $0.modelID = id; $0.autoRoute = false }95 Haptics.play(.selection)96 }9798 // MARK: Sending99100 public func send() {101 let text = draft.trimmingCharacters(in: .whitespacesAndNewlines)102 guard !isGenerating, !text.isEmpty || !pendingAttachments.isEmpty else { return }103 let files = pendingAttachments104 draft = ""; pendingAttachments = []; errorText = nil105 Haptics.play(.light)106 var stored: [Attachment] = []107 for f in files {108 if let a = try? app.attachments.store(data: f.data, fileName: f.fileName, mimeType: f.mimeType, kind: f.kind, extractedText: f.extractedText) { stored.append(a) }109 }110 let user = ChatMessage(conversationID: conversation.id, parentID: conversation.activeLeafID, role: .user, content: text, attachments: stored)111 do { _ = try app.store.appendMessage(user) } catch { errorText = error.localizedDescription; return }112 refreshFromStore()113 startTurn(userText: text)114 }115116 public func regenerate(_ assistantID: UUID) {117 guard !isGenerating, let target = tree.messages[assistantID], let parentID = target.parentID else { return }118 // make the user turn the active leaf: the new answer becomes a sibling of the old one, which is kept119 try? app.store.setActiveLeaf(parentID, for: conversation.id)120 refreshFromStore()121 startTurn(userText: tree.messages[parentID]?.content ?? "")122 }123124 /// Editing a user message creates a sibling branch; the original and its answers stay reachable.125 public func edit(_ userMessageID: UUID, newText: String) {126 guard !isGenerating else { return }127 do { _ = try app.store.editUserMessage(userMessageID, newContent: newText) } catch { errorText = error.localizedDescription; return }128 refreshFromStore()129 startTurn(userText: newText)130 }131132 public func switchBranch(from messageID: UUID, offset: Int) {133 guard !isGenerating else { return }134 _ = try? app.store.switchBranch(from: messageID, offset: offset)135 refreshFromStore(); Haptics.play(.selection)136 Task { await app.engine.resetSession(modelID: conversation.modelID ?? "", key: conversation.id.uuidString); await refreshContext() }137 }138139 public func delete(_ messageID: UUID) {140 guard !isGenerating else { return }141 try? app.store.deleteMessageSubtree(messageID)142 refreshFromStore()143 }144145 public func fork(from messageID: UUID) -> Conversation? {146 try? app.store.forkConversation(from: messageID, title: conversation.title + " (fork)")147 }148149 public func stop() {150 generation?.cancel()151 approvalContinuation?.resume(returning: .deny(reason: "Cancelled by the user.")); approvalContinuation = nil; pendingApproval = nil152 }153154 public func resolveApproval(_ decision: ApprovalDecision) {155 approvalContinuation?.resume(returning: decision); approvalContinuation = nil; pendingApproval = nil156 }157158 private func refreshFromStore() {159 if let c = try? app.store.conversation(conversation.id) { conversation = c }160 reload()161 }162163 // MARK: A turn164165 private func startTurn(userText: String) {166 isGenerating = true; liveText = ""; liveReasoning = ""; liveToolActivity = []; routing = nil167 generation = Task { [weak self] in168 await self?.runTurn(userText: userText)169 self?.finishTurn()170 }171 }172173 private func finishTurn() {174 isGenerating = false; streamingMessageID = nil; status = nil; generation = nil175 refreshFromStore()176 Task { await refreshContext() }177 }178179 private func resolveModel(for prompt: String, hasImages: Bool) -> ModelDescriptor? {180 if conversation.autoRoute {181 let toolsOn = agent != nil182 if let decision = ModelRouter.route(prompt: prompt, hasImages: hasImages, toolsEnabled: toolsOn, candidates: app.chatModels, device: app.device,183 preferences: app.preferences.routing, backendAvailable: { app.backendIssues[$0] }, measuredSpeed: app.measuredSpeed) {184 routing = decision185 return app.model(decision.modelID)186 }187 }188 return app.model(conversation.modelID) ?? app.model(app.defaultModelID)189 }190191 private func runTurn(userText: String) async {192 refreshFromStore() // the branch sent to the model is what the store holds now193 let branch = messages194 let hasImages = branch.last?.attachments.contains { $0.kind == .image } ?? false195 guard let model = resolveModel(for: userText, hasImages: hasImages) else {196 errorText = "No model is available. Download one from the Model Library, or add a remote node."; return197 }198 if conversation.modelID == nil { update { $0.modelID = model.id } }199 let verdict = app.compatibility(model, context: conversation.settings.contextLength)200 if verdict.fit == .unsupported || verdict.fit == .tooLarge {201 errorText = verdict.reasons.first ?? "This model cannot run on this device."; return202 }203204 // 1. retrieval (always local unless the collection's embedder is a remote one the user chose)205 var hits: [KnowledgeHit] = []206 let collectionIDs = conversation.collectionIDs207 if !collectionIDs.isEmpty, !userText.isEmpty {208 status = "Searching knowledge…"209 hits = (try? await app.knowledge.search(userText, collections: collectionIDs, limit: 6)) ?? []210 }211212 // 2. placeholder for the answer213 var answer = ChatMessage(conversationID: conversation.id, parentID: conversation.activeLeafID, role: .assistant, content: "",214 sources: hits.map(\.reference), modelID: model.id, modelName: model.name, backend: model.backend,215 remoteNodeName: app.nodeName(for: model), status: .streaming)216 do { answer = try app.store.appendMessage(answer) } catch { errorText = error.localizedDescription; return }217 streamingMessageID = answer.id218 refreshFromStore()219220 // 3. context assembly inside the token budget221 if !app.snapshot.isLoaded(model.id) { status = model.backend == .remote ? "Contacting \(app.nodeName(for: model) ?? "remote node")…" : "Loading \(model.name)…" }222 let engine = app.engine, attachments = app.attachments223 if model.backend != .remote, model.backend != .appleFoundation { _ = try? await engine.load(model) }224 let loadedInfo = app.snapshot.loaded.first { $0.id == model.id }?.info225 var settings = conversation.settings226 if let limit = loadedInfo?.contextLength ?? model.contextLength, limit > 0 { settings.contextLength = min(settings.contextLength, limit) }227 var working = conversation; working.settings = settings228 let supportsVision = (loadedInfo?.capabilities ?? model.capabilities).contains(.vision)229 let assembled = await ContextAssembler().assemble(230 conversation: working, branch: branch, knowledge: hits,231 attachmentData: { attachments.data(for: $0) }, supportsVision: supportsVision,232 counter: { await engine.countTokens(modelID: model.id, text: $0) })233 context = assembled234 if Task.isCancelled { await finalize(&answer, metrics: nil, cancelled: true); return }235236 // 4. generation (agent loop when the conversation has an agent, plain streaming otherwise)237 status = nil238 var metrics: GenerationMetrics?; var failure: String?239 let request = GenerationRequest(messages: assembled.messages, settings: settings, sessionKey: conversation.id.uuidString)240 var lastFlush = Date()241 func flush(force: Bool = false) {242 guard force || Date().timeIntervalSince(lastFlush) > 0.6 else { return }243 lastFlush = Date()244 try? app.store.updateStreamingMessage(id: answer.id, content: liveText, reasoning: liveReasoning.isEmpty ? nil : liveReasoning)245 }246 if let agent, !agent.enabledTools.isEmpty {247 (metrics, failure) = await runAgent(agent, model: model, request: request, flush: { flush() })248 } else {249 do {250 for try await event in engine.generate(modelID: model.id, request: request, purpose: "chat") {251 switch event {252 case .text(let t): liveText += t; flush()253 case .reasoning(let t): liveReasoning += t; flush()254 case .toolCall: break255 case .finished(let m): metrics = m256 }257 }258 } catch is CancellationError { }259 catch { if !Task.isCancelled { failure = error.localizedDescription } }260 }261 let cancelled = Task.isCancelled || metrics?.stopReason == .cancelled262 if let failure { answer.status = .failed; answer.errorText = failure; Haptics.play(.error) }263 await finalize(&answer, metrics: metrics, cancelled: cancelled)264 if failure == nil, !cancelled { Haptics.play(.success) }265266 // 5. housekeeping that must not block the UI267 if failure == nil {268 await autoTitleIfNeeded(model: model, userText: userText)269 if assembled.needsCompaction { await compact(model: model, assembled: assembled) }270 }271 }272273 private func finalize(_ answer: inout ChatMessage, metrics: GenerationMetrics?, cancelled: Bool) async {274 answer.content = liveText.trimmingCharacters(in: .whitespacesAndNewlines)275 answer.reasoning = liveReasoning.isEmpty ? nil : liveReasoning.trimmingCharacters(in: .whitespacesAndNewlines)276 answer.metrics = metrics277 if answer.status != .failed { answer.status = cancelled ? .cancelled : .complete }278 if answer.status == .failed, answer.content.isEmpty, answer.errorText == nil { answer.errorText = "The model returned nothing." }279 try? app.store.saveMessage(answer, makeActiveLeaf: true)280 }281282 // MARK: Agent mode283284 private func runAgent(_ agent: AgentConfiguration, model: ModelDescriptor, request: GenerationRequest, flush: @escaping () -> Void) async -> (GenerationMetrics?, String?) {285 let setup = app.makeAgentRunner(for: agent)286 var configured = agent287 configured.settings = request.settings; configured.collectionIDs = Array(Set(agent.collectionIDs + conversation.collectionIDs))288 var messages = request.messages289 if messages.first?.role == .system { messages.removeFirst() } // the runner writes its own system prompt from the agent's instructions290 var options = setup.options; options.sessionKey = conversation.id.uuidString291 var metrics: GenerationMetrics?; var failure: String?292 let started = Date()293 let stream = setup.runner.run(agent: configured, modelID: model.id, messages: messages, permissions: setup.permissions, options: options) { [weak self] approval in294 await self?.ask(approval) ?? .deny(reason: "The conversation was closed.")295 }296 for await event in stream {297 switch event {298 case .text(let t): liveText += t; flush()299 case .reasoning(let t): liveReasoning += t; flush()300 case .toolRequested(let call):301 liveToolActivity.append(ToolActivity(id: call.id, name: call.name, arguments: call.arguments.serialized()))302 case .toolDenied(let callID, let reason):303 if let i = liveToolActivity.firstIndex(where: { $0.id == callID }) {304 liveToolActivity[i].output = "Denied: \(reason)"; liveToolActivity[i].isError = true; liveToolActivity[i].isRunning = false305 }306 case .toolFinished(let callID, _, let output, _):307 if let i = liveToolActivity.firstIndex(where: { $0.id == callID }) {308 liveToolActivity[i].output = output.text; liveToolActivity[i].isError = output.isError; liveToolActivity[i].isRunning = false309 }310 case .stepStarted(let n):311 status = n > 1 ? "Step \(n)…" : nil312 if n > 1, !liveText.isEmpty, !liveText.hasSuffix("\n\n") { liveText += "\n\n" }313 case .limitReached(let limit):314 liveText += "\n\n_Stopped: the agent reached its \(limit == .steps ? "step" : limit == .toolCalls ? "tool-call" : "time") limit._"315 case .finished(_, let totals, let log):316 metrics = GenerationMetrics(promptTokens: totals.promptTokens, completionTokens: totals.completionTokens, totalTime: Date().timeIntervalSince(started),317 stopReason: Task.isCancelled ? .cancelled : .endOfTurn)318 try? app.agentLogs.save(log)319 case .failed(let message, let log): failure = message; try? app.agentLogs.save(log)320 case .approvalNeeded, .toolStarted: break321 }322 }323 if !liveToolActivity.isEmpty {324 // keep a compact, human-readable trace of what the agent did with the answer325 let trace = liveToolActivity.map { "- \($0.name) \($0.arguments) → \($0.isError ? "failed" : "ok")" }.joined(separator: "\n")326 liveReasoning += (liveReasoning.isEmpty ? "" : "\n\n") + "Tools used:\n" + trace327 }328 return (metrics, failure)329 }330331 private func ask(_ request: ApprovalRequest) async -> ApprovalDecision {332 Haptics.play(.warning)333 pendingApproval = request334 return await withCheckedContinuation { approvalContinuation = $0 }335 }336337 // MARK: Context338339 public func refreshContext() async {340 guard !isGenerating, let model else { context = nil; return }341 let engine = app.engine, attachments = app.attachments342 let supportsVision = model.capabilities.contains(.vision)343 context = await ContextAssembler().assemble(conversation: conversation, branch: messages, attachmentData: { attachments.data(for: $0) },344 supportsVision: supportsVision, counter: { await engine.countTokens(modelID: model.id, text: $0) })345 }346347 /// Folds the turns that no longer fit into a summary. Messages are never modified: the summary is a separate record.348 private func compact(model: ModelDescriptor, assembled: AssembledContext) async {349 switch conversation.compaction {350 case .off, .truncate: return351 case .summarize: break352 }353 guard let input = ContextAssembler().compactionInput(conversation: conversation, branch: messages, dropped: assembled.droppedMessageIDs) else { return }354 status = "Compacting context…"355 defer { status = nil }356 let request = GenerationRequest(messages: [.system(ContextAssembler.summarizationInstruction), .user(input.text)],357 settings: GenerationSettings(temperature: 0.2, contextLength: conversation.settings.contextLength, maxTokens: 400, reasoningEnabled: false))358 guard let result = try? await app.engine.complete(modelID: model.id, request: request), !result.text.isEmpty else { return }359 let summary = ContextSummary(text: result.text.trimmingCharacters(in: .whitespacesAndNewlines), coversThroughMessageID: input.lastID,360 messageCount: input.count, modelID: model.id)361 try? app.store.setSummary(summary, for: conversation.id)362 refreshFromStore()363 }364365 public func summarizeNow() async -> String? {366 guard let model, !messages.isEmpty else { return nil }367 let text = messages.filter { $0.role == .user || $0.role == .assistant }.map { "\($0.role == .user ? "User" : "Assistant"): \($0.content)" }.joined(separator: "\n\n")368 let request = GenerationRequest(messages: [.system(ContextAssembler.summarizationInstruction), .user(String(text.suffix(24_000)))],369 settings: GenerationSettings(temperature: 0.2, contextLength: conversation.settings.contextLength, maxTokens: 400, reasoningEnabled: false))370 return try? await app.engine.complete(modelID: model.id, request: request).text.trimmingCharacters(in: .whitespacesAndNewlines)371 }372373 public func clearSummary() {374 try? app.store.setSummary(nil, for: conversation.id); refreshFromStore()375 }376377 private func autoTitleIfNeeded(model: ModelDescriptor, userText: String) async {378 guard app.preferences.autoTitle, conversation.title == "New chat", messages.filter({ $0.role == .user }).count == 1, !userText.isEmpty else { return }379 let fallback = String(userText.split(separator: "\n").first?.prefix(48) ?? "New chat")380 var title = fallback381 // titles are generated only by on-device models: a remote node never receives an extra request the user did not ask for382 if model.backend != .remote {383 let request = GenerationRequest(messages: [.system("Write a title of at most 6 words for a conversation that starts with the user's message. Reply with the title only, no quotes, in the user's language."),384 .user(String(userText.prefix(600)))],385 settings: GenerationSettings(temperature: 0.3, contextLength: 2048, maxTokens: 24, reasoningEnabled: false))386 if let out = try? await app.engine.complete(modelID: model.id, request: request).text {387 let cleaned = out.split(separator: "\n").first.map(String.init)?.trimmingCharacters(in: CharacterSet(charactersIn: " \"'«»*#.")) ?? ""388 if cleaned.count >= 3, cleaned.count <= 70 { title = cleaned }389 }390 }391 update { $0.title = title }392 }393394 // MARK: Attachments395396 public func attach(data: Data, fileName: String) {397 pendingAttachments.append(AttachmentImporter.make(data: data, fileName: fileName))398 }399 public func attach(url: URL) {400 let scoped = url.startAccessingSecurityScopedResource()401 defer { if scoped { url.stopAccessingSecurityScopedResource() } }402 guard let data = try? Data(contentsOf: url) else { errorText = "Could not read \(url.lastPathComponent)."; return }403 attach(data: data, fileName: url.lastPathComponent)404 }405406 public func exportMarkdown() -> String { (try? app.store.exportMarkdown(conversation.id)) ?? "" }407 public func exportJSON() -> Data { (try? app.store.exportJSON(conversation.id)) ?? Data() }408}409410/// Turns a dropped / picked file into an attachment the model can use: images stay images, documents are extracted to text on device.411enum AttachmentImporter {412 static let imageTypes: [String: String] = ["png": "image/png", "jpg": "image/jpeg", "jpeg": "image/jpeg", "heic": "image/heic", "gif": "image/gif", "webp": "image/webp", "tiff": "image/tiff"]413 static let maxTextCharacters = 60_000414415 static func make(data: Data, fileName: String) -> PendingAttachment {416 let ext = (fileName as NSString).pathExtension.lowercased()417 if let mime = imageTypes[ext] { return PendingAttachment(fileName: fileName, mimeType: mime, data: data, kind: .image) }418 do {419 let document = try TextExtractor().extract(data: data, fileName: fileName)420 var text = document.sections.map { section in (section.page.map { "[page \($0)]\n" } ?? "") + section.text }.joined(separator: "\n\n")421 var warning = document.warnings.first422 if text.count > maxTextCharacters { text = String(text.prefix(maxTextCharacters)); warning = "Truncated to the first \(maxTextCharacters) characters. Use a knowledge collection for long documents." }423 return PendingAttachment(fileName: fileName, mimeType: "text/plain", data: data, kind: .document, extractedText: text, warning: warning)424 } catch {425 return PendingAttachment(fileName: fileName, mimeType: "application/octet-stream", data: data, kind: .document, extractedText: nil,426 warning: "This file could not be read (\(error.localizedDescription)); the model will not see its content.")427 }428 }429}430