SPB Git

Repository Public MIT

LocalModels.swift 10.2 KB 222 lines swift

1// =============================================================================2//                  SP BOUCHER | SOFTWARE & RESEARCH3//             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// PROJECT METADATA7// -----------------------------------------------------------------------------8// Project          : ORIX Astra9// File             : Packages/AstraUI/Sources/App/LocalModels.swift10// Module           : App11// Repository       : https://git.spboucher.ai/orix-astra12// Created          : 2026-11-2113// Last Modified    : 2026-11-2114//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 AstraLocal92import AstraPersistence9394/// On-device models as the UI sees them: the catalogue, what is installed, what is downloading, what is loaded.95@MainActor @Observable public final class LocalModels {96    public let modelStore: ModelStore97    public nonisolated let engine: LocalEngine98    public let downloads: DownloadCoordinator99    private let transport: any HTTPTransport100    private let settings: AstraStore101102    public private(set) var catalog = ModelCatalog()103    public private(set) var catalogNote: String?104    public private(set) var installed: [InstalledModel] = []105    public private(set) var statuses: [String: DownloadStatus] = [:]106    public private(set) var snapshot: LocalEngine.Snapshot?107    public private(set) var appleAvailability: AppleFoundationAvailability = .current()108    public private(set) var isRefreshingCatalog = false109    public private(set) var totalBytes: Int64 = 0110    public private(set) var freeBytes: Int64?111    public let device = DeviceFacts.current()112113    private weak var library: ModelLibrary?114    private var observers: [Task<Void, Never>] = []115116    public init(transport: any HTTPTransport, settings: AstraStore) throws {117        let root = try ModelStore.defaultRoot()118        modelStore = ModelStore(root: root)119        try modelStore.prepare()120        engine = LocalEngine(store: modelStore)121        downloads = DownloadCoordinator(store: modelStore)122        self.transport = transport; self.settings = settings123    }124125    /// Why MLX cannot run here (the iOS Simulator has no usable GPU), or nil when it can.126    public var mlxUnavailableReason: String? { MLXAvailability.unavailableReason }127128    public func start(library: ModelLibrary) async {129        self.library = library130        if let url = Bundle.module.url(forResource: "catalog", withExtension: "json"), let data = try? Data(contentsOf: url),131           let bundled = try? ModelCatalog.decode(data, origin: .bundled) { catalog = bundled }132        if let cached: Data = try? await settings.setting("local.catalog.online"), let online = try? ModelCatalog.decode(cached, origin: .onlineCatalog) {133            catalog = catalog.merging(online: online).catalog134        }135        for status in await downloads.restore() { statuses[status.id] = status }136        await engine.startMonitoring()137        await refreshInstalled()138139        let downloads = downloads, engine = engine140        observers.append(Task { [weak self] in141            for await list in await downloads.updates() {142                guard let self else { return }143                let before = self.statuses144                self.statuses = Dictionary(uniqueKeysWithValues: list.map { ($0.id, $0) })145                if list.contains(where: { $0.phase == .completed && before[$0.id]?.phase != .completed }) { await self.refreshInstalled() }146            }147        })148        observers.append(Task { [weak self] in149            for await snap in await engine.snapshots() { self?.snapshot = snap }150        })151    }152153    /// Re-reads the disk and publishes the list of chat-ready on-device models to the library.154    public func refreshInstalled() async {155        let store = modelStore156        let (models, bytes, free) = await Task.detached { (store.installed(), store.totalBytesOnDisk(), store.availableDiskBytes()) }.value157        installed = models; totalBytes = bytes; freeBytes = free158        appleAvailability = .current()159        var infos: [ModelInfo] = []160        if appleAvailability.isAvailable, let system = try? await AppleFoundationProvider().listModels() { infos += system }161        if mlxUnavailableReason == nil, let mlx = try? await MLXProvider(engine: engine).listModels() { infos += mlx }162        library?.localModels = infos163    }164165    /// Refreshes the catalogue from the owner's site. A card for a known model is accepted only when its file hashes match166    /// the ones shipped with the app.167    public func refreshCatalog() async {168        isRefreshingCatalog = true; defer { isRefreshingCatalog = false }169        guard let url = Bundle.module.url(forResource: "catalog", withExtension: "json"), let data = try? Data(contentsOf: url),170              let bundled = try? ModelCatalog.decode(data, origin: .bundled) else { return }171        let client = CatalogClient(transport: transport)172        let result = await client.refreshed(bundled: bundled)173        catalog = result.catalog174        if let failure = result.failure { catalogNote = "Showing the catalogue shipped with the app. \(failure.message)" }175        else if !result.rejected.isEmpty { catalogNote = "\(result.rejected.count) online entr\(result.rejected.count == 1 ? "y was" : "ies were") ignored because the file hashes did not match." }176        else {177            catalogNote = nil178            if let online = try? await client.fetch(), let encoded = try? online.encoded() { try? await settings.setSetting("local.catalog.online", encoded) }179        }180    }181182    public func card(_ id: String) -> LocalModelCard? { catalog.card(id: id) ?? modelStore.card(id: id) }183    public func isInstalled(_ id: String) -> Bool { installed.contains { $0.id == id } }184    public func verdict(_ card: LocalModelCard) -> FitVerdict { engine.verdict(for: card) }185186    // MARK: Downloads187188    public func download(_ card: LocalModelCard) async -> AstraError? {189        do { statuses[card.id] = try await downloads.start(DownloadPlan(card: card)); return nil }190        catch let e as DownloadError { return e.astraError }191        catch { return AstraError.wrap(error) }192    }193194    public func pause(_ id: String) async { await downloads.pause(id: id) }195    public func resume(_ id: String) async -> AstraError? {196        do { try await downloads.resume(id: id); return nil }197        catch let e as DownloadError { return e.astraError }198        catch { return AstraError.wrap(error) }199    }200    public func cancel(_ id: String) async { await downloads.cancel(id: id); statuses[id] = nil }201202    /// Removes a model completely: weights, partial files and caches. Returns the bytes freed.203    @discardableResult public func remove(_ id: String) async -> Int64 {204        await downloads.cancel(id: id)205        await downloads.forget(id: id)206        await engine.unload(id)207        let store = modelStore208        let freed = await Task.detached { (try? store.remove(id: id)) ?? 0 }.value209        statuses[id] = nil210        await refreshInstalled()211        return freed212    }213214    // MARK: Memory215216    public func load(_ id: String, force: Bool = false) async -> AstraError? {217        do { _ = try await engine.load(id, force: force); return nil } catch { return AstraError.wrap(error) }218    }219    public func unload(_ id: String) async { await engine.unload(id) }220    public func unloadAll() async { await engine.unloadAll() }221}222
Files main
Menu

Theme