SPB Git

Repository Public MIT

ChatController.swift 17.7 KB 356 lines swift

1// =============================================================================2//                  SP BOUCHER | SOFTWARE & RESEARCH3//             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// PROJECT METADATA7// -----------------------------------------------------------------------------8// Project          : ORIX Astra9// File             : Packages/AstraUI/Sources/Chat/ChatController.swift10// Module           : Chat11// Repository       : https://git.spboucher.ai/orix-astra12// Created          : 2026-11-2113// Last Modified    : 2026-11-2214//15// =============================================================================16// AUTHOR & PROFESSIONAL AFFILIATION17// =============================================================================18//19// Author           : Simon-Pierre Boucher20// Position         : Professor of Finance21// Institution      : Université du Québec en Outaouais (UQO)22// Department       : Département des sciences administratives23// Organization     : SP Boucher24//25// Website          : https://spboucher.ai26// Git              : https://git.spboucher.ai27//28// =============================================================================29// OFFICIAL CONTACT DIRECTORY30// =============================================================================31//32// General          : info@spboucher.ai33// Professional     : contact@spboucher.ai34// Direct           : simon@spboucher.ai35// Research         : research@spboucher.ai36// Development      : dev@spboucher.ai37// Support          : support@spboucher.ai38// Security/Admin   : admin@spboucher.ai39// Notifications    : notifications@spboucher.ai40//41// Preferred Contact: support@spboucher.ai42//43// =============================================================================44// COPYRIGHT & INTELLECTUAL PROPERTY45// =============================================================================46//47// Copyright (c) 202648// Simon-Pierre Boucher49//50// All rights reserved where applicable.51//52// This file is governed by the applicable project license,53// copyright notices, institutional agreements and relevant law.54//55// Unless expressly authorized by the applicable license or56// rights holder, copying, redistribution, sublicensing,57// publication and commercial exploitation are not permitted.58//59// Existing third-party rights and notices remain unaffected.60//61// =============================================================================62// CONFIDENTIALITY & SECURITY63// =============================================================================64//65// Classification   : Public66// Distribution     : Public repository; use is governed by the project license67// Security Contact : admin@spboucher.ai68//69// Access to this source does not independently grant permission70// to redistribute or disclose confidential materials.71//72// Do not embed credentials, private keys, access tokens,73// certificates or confidential personal information.74//75// =============================================================================76// LICENSE & MAINTENANCE77// =============================================================================78//79// License          : Proprietary, all rights reserved (see license file)80// License File     : LICENSE81// Maintainer       : Simon-Pierre Boucher82// Technical Contact: dev@spboucher.ai83//84// =============================================================================85// END OF SP BOUCHER SOURCE HEADER86// =============================================================================8788import Foundation89import Observation90import AstraCore91import AstraPersistence9293/// One conversation on screen: the active branch, the live generation, and every action on messages.94/// The database is the source of truth; this object mirrors the active path and holds only the text that is streaming right now.95@MainActor @Observable public final class ChatController {96    public private(set) var conversation: ConversationRecord97    public private(set) var messages: [MessageRecord] = []98    public private(set) var attachments: [String: [AttachmentRecord]] = [:]99    public private(set) var branches: [String: BranchPosition] = [:]100101    /// The assistant message being generated, if any. Its text lives in `liveText` until it is finalised.102    public private(set) var streamingID: String?103    public private(set) var liveText = ""104    public private(set) var liveReasoning = ""105    public private(set) var liveStart: MessageStart?106    public private(set) var isLoadingModel = false107    /// What the last request did to the context: how full the window is and how many turns were left out.108    public private(set) var lastPlan: ContextBudget.Plan?109    public private(set) var negotiationNotes: [String] = []110    public var banner: AstraError?111112    public var isGenerating: Bool { streamingID != nil }113114    /// How many of the most recent messages of the branch are on screen.115    public private(set) var window = 60116    public private(set) var hasEarlier = false117118    public func loadEarlier() async { window += 60; await reload() }119120    private let store: AstraStore121    private let library: ModelLibrary122    private let orchestrator: InferenceOrchestrator123    private var task: Task<Void, Never>?124    private let onFinished: @MainActor (MessageRecord) -> Void125126    public init(conversation: ConversationRecord, store: AstraStore, library: ModelLibrary, orchestrator: InferenceOrchestrator,127                onFinished: @escaping @MainActor (MessageRecord) -> Void = { _ in }) {128        self.conversation = conversation; self.store = store; self.library = library; self.orchestrator = orchestrator; self.onFinished = onFinished129    }130131    // MARK: Loading132133    public func reload() async {134        guard let fresh = try? await store.conversation(id: conversation.id) else { return }135        conversation = fresh136        // Only a window of the most recent turns is kept on screen; earlier ones are paged in on demand. The prompt sent to137        // the model is built from the full branch, not from this window.138        let page = (try? await store.activePath(conversationID: conversation.id, last: window + 1)) ?? []139        hasEarlier = page.count > window140        messages = hasEarlier ? Array(page.suffix(window)) : page141        attachments = (try? await store.attachments(conversationID: conversation.id)) ?? [:]142        branches = (try? await store.branchPositions(conversationID: conversation.id)) ?? [:]143    }144145    public var modelInfo: ModelInfo? { library.info(for: conversation.model) }146147    /// Tokens of the model's window that the conversation may use, after the person's own cap.148    public var effectiveWindow: Int {149        let modelWindow = modelInfo?.contextLength ?? (conversation.model.provider.isLocal ? 4_096 : 32_000)150        return conversation.contextWindowCap.map { min($0, modelWindow) } ?? modelWindow151    }152153    // MARK: Conversation settings154155    public func update(_ change: @escaping @Sendable (inout ConversationRecord) -> Void) async {156        _ = try? await store.updateConversation(id: conversation.id, change)157        if let fresh = try? await store.conversation(id: conversation.id) { conversation = fresh }158    }159160    /// Switches model mid-conversation. The whole active branch is sent to the new model on the next turn; anything the new161    /// model cannot take (images, a longer window) is reported when that turn is sent, not silently dropped.162    public func switchModel(to ref: ModelRef) async {163        guard ref != conversation.model else { return }164        await update { $0.model = ref }165        library.noteUsed(ref)166        lastPlan = nil; negotiationNotes = []167    }168169    // MARK: Sending170171    /// True when sending these attachments would upload them to a cloud provider.172    public func wouldUpload(_ drafts: [AttachmentDraft]) -> Bool { !drafts.isEmpty && !conversation.model.provider.isLocal }173174    public func send(_ text: String, attachments drafts: [AttachmentDraft] = []) async {175        let trimmed = text.trimmingCharacters(in: .whitespacesAndNewlines)176        guard !isGenerating, !trimmed.isEmpty || !drafts.isEmpty else { return }177        if let unavailable = library.availability(of: conversation.model) { banner = unavailable; return }178        do {179            _ = try await store.appendUserMessage(conversationID: conversation.id, text: trimmed, attachments: drafts)180            _ = try? await store.autoTitleIfNeeded(conversationID: conversation.id)181            await reload()182            await generate()183        } catch { banner = AstraError.wrap(error) }184    }185186    /// Replaces a user turn with an edited version on a new branch and answers it.187    public func editAndResend(_ messageID: String, newText: String) async {188        guard !isGenerating else { return }189        do {190            _ = try await store.editAndResend(messageID: messageID, newText: newText.trimmingCharacters(in: .whitespacesAndNewlines))191            await reload()192            await generate()193        } catch { banner = AstraError.wrap(error) }194    }195196    /// Answers the same prompt again on a sibling branch, optionally with another model.197    public func regenerate(_ assistantMessageID: String, with model: ModelRef? = nil) async {198        guard !isGenerating else { return }199        if let unavailable = library.availability(of: model ?? conversation.model) { banner = unavailable; return }200        do {201            let fresh = try await store.regenerate(assistantMessageID: assistantMessageID, model: model ?? conversation.model)202            await reload()203            await run(assistant: fresh)204        } catch { banner = AstraError.wrap(error) }205    }206207    /// Answers the last user turn again after a failure.208    public func retryLast() async {209        guard !isGenerating, let last = messages.last else { return }210        if last.role == .assistant { await regenerate(last.id) } else { await generate() }211    }212213    public func stop() { task?.cancel() }214215    private func generate() async {216        do {217            let assistant = try await store.beginAssistantMessage(conversationID: conversation.id, model: conversation.model)218            await reload()219            await run(assistant: assistant)220        } catch { banner = AstraError.wrap(error) }221    }222223    // MARK: The generation itself224225    private func run(assistant: MessageRecord) async {226        let model = assistant.model ?? conversation.model227        let info = library.info(for: model)228        streamingID = assistant.id; liveText = ""; liveReasoning = ""; liveStart = nil; banner = nil229        library.noteUsed(model)230231        // History = the active path up to (not including) the assistant turn being written.232        let fullPath = (try? await store.activePath(conversationID: conversation.id)) ?? messages233        let history = fullPath.filter { $0.id != assistant.id && ($0.status == .complete || $0.status == .stopped || $0.role == .user) && !($0.role == .assistant && $0.text.isEmpty && $0.toolCalls.isEmpty) }234        var prompt: [PromptMessage] = []235        var hasImages = false236        for m in history {237            var p = m.promptMessage238            for a in attachments[m.id] ?? [] {239                switch a.kind {240                case .image:241                    if info?.capabilities.contains(.vision) ?? false, let data = try? await store.attachmentData(a) { p.images.append(ImageInput(data: data, mimeType: a.mimeType)) }242                    hasImages = true243                default:244                    if let extracted = a.extractedText, !extracted.isEmpty {245                        p.text += "\n\n<attached-file name=\"\(a.filename.replacingOccurrences(of: "\"", with: "'"))\">\n\(extracted)\n</attached-file>"246                    }247                }248            }249            prompt.append(p)250        }251252        var system = conversation.systemPrompt.trimmingCharacters(in: .whitespacesAndNewlines)253        if conversation.memoryEnabled, let memories = try? await store.memories(enabledOnly: true), !memories.isEmpty {254            let block = memories.prefix(40).map { "- " + $0.text }.joined(separator: "\n")255            system += (system.isEmpty ? "" : "\n\n") + "Things the user asked you to remember:\n" + block256        }257258        var parameters = conversation.parameters259        var notes: [String] = []260        if let info {261            let negotiated = ParameterNegotiator.negotiate(parameters, tools: [], images: hasImages, for: info)262            parameters = negotiated.parameters; notes = negotiated.notes263        }264        let reserve = min(parameters.maxOutputTokens ?? info?.maxOutputTokens.map { min($0, 4_096) } ?? 1_024, max(256, effectiveWindow / 2))265        let budget = ContextBudget(window: effectiveWindow, reservedForOutput: reserve, strategy: conversation.truncationStrategy)266        let plan = budget.plan(system: system.isEmpty ? nil : system, messages: prompt)267        lastPlan = plan; negotiationNotes = notes268269        guard plan.fits else {270            await finish(assistant.id, MessageCompletion(text: "", status: .failed, error: .contextTooLong(271                plan.droppedCount == 0 && conversation.truncationStrategy == .strict272                    ? "This conversation no longer fits in the context window and truncation is turned off. Raise the context budget, allow truncation, or start a new chat."273                    : "The last message alone is larger than this model's context window."), stopReason: .error), model: model, info: info)274            return275        }276        if model.provider.isLocal { parameters.contextLength = effectiveWindow }277278        let request = GenerationRequest(model: model, system: system.isEmpty ? nil : system, messages: plan.messages, parameters: parameters,279                                        routing: conversation.routing, sessionKey: conversation.id)280        let orchestrator = orchestrator, store = store, id = assistant.id281        isLoadingModel = model.provider == .mlx282        task = Task { [weak self] in283            var outcome = GenerationOutcome()284            var lastPersist = Date.distantPast, lastPublish = Date.distantPast285            for await event in orchestrator.stream(request) {286                outcome.apply(event)287                let now = Date()288                switch event {289                case .messageStarted(let start): self?.liveStart = start290                case .textDelta, .reasoningSummaryDelta:291                    // Publish to the UI at most ~25 times a second: the renderer re-parses only the last block, and the292                    // layout settles between frames instead of thrashing on every token.293                    if now.timeIntervalSince(lastPublish) > 0.04 {294                        lastPublish = now295                        self?.isLoadingModel = false296                        self?.liveText = outcome.text; self?.liveReasoning = outcome.reasoningSummary297                    }298                    // Persist the partial answer a few times a second so a crash or a kill loses almost nothing.299                    if now.timeIntervalSince(lastPersist) > 0.4 {300                        lastPersist = now301                        try? await store.updateStreaming(id: id, text: outcome.text, reasoning: outcome.reasoningSummary)302                    }303                default: break304                }305            }306            if outcome.error == nil, outcome.stopReason == .unknown, Task.isCancelled { outcome.apply(.error(.cancelled)) }307            let cost = UsageAccounting.cost(usage: outcome.usage, pricing: info?.pricing, provider: model.provider)308            await self?.finish(id, MessageCompletion(outcome: outcome, cost: cost), model: model, info: info)309        }310        await task?.value311    }312313    private func finish(_ id: String, _ completion: MessageCompletion, model: ModelRef, info: ModelInfo?) async {314        var completion = completion315        // A stopped answer keeps whatever text arrived; an empty failed one keeps its error card.316        if completion.status == .stopped, completion.text.isEmpty, completion.reasoningSummary.isEmpty { completion.stopReason = .cancelled }317        let saved = try? await store.finalizeAssistantMessage(id: id, completion)318        streamingID = nil; liveText = ""; liveReasoning = ""; liveStart = nil; isLoadingModel = false; task = nil319        await reload()320        if let saved { onFinished(saved) }321    }322323    // MARK: Branches and message actions324325    /// Moves to the previous or next sibling of a message (another version of the same turn).326    public func switchBranch(of messageID: String, by delta: Int) async {327        guard !isGenerating, let position = branches[messageID] else { return }328        let target = position.index + delta329        guard position.siblingIDs.indices.contains(target) else { return }330        _ = try? await store.selectBranch(messageID: position.siblingIDs[target])331        await reload()332    }333334    public func toggleBookmark(_ messageID: String) async {335        _ = try? await store.toggleBookmark(messageID: messageID)336        await reload()337    }338339    public func deleteFrom(_ messageID: String) async {340        guard !isGenerating else { return }341        _ = try? await store.deleteMessageSubtree(messageID: messageID)342        await reload()343    }344345    /// The text shown for a message: live text while it streams, stored text otherwise.346    public func displayText(_ message: MessageRecord) -> String { message.id == streamingID ? liveText : message.text }347    public func displayReasoning(_ message: MessageRecord) -> String { message.id == streamingID ? liveReasoning : message.reasoningSummary }348349    /// Estimated share of the input budget used by the conversation as it stands (before the next message).350    public var contextFraction: Double {351        if let plan = lastPlan { return plan.fraction }352        let used = messages.reduce(0) { $0 + TokenEstimator.estimate($1.promptMessage) } + TokenEstimator.estimate(conversation.systemPrompt)353        return min(1, Double(used) / Double(max(1, effectiveWindow)))354    }355}356
Files main
Menu

Theme