SPB Git

Repository Public MIT

ModelLibrary.swift 14.8 KB 315 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/ModelLibrary.swift10// Module           : App11// 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 AstraPersistence92import AstraProviders9394/// What the app knows about one keyed cloud provider.95public struct ProviderState: Sendable, Hashable {96    public var hasKey = false97    public var maskedKey: String?98    public var models: [ModelInfo] = []99    public var status: ConnectionStatus?100    public var isLoading = false101    public var error: AstraError?102    public var modelsFetchedAt: Date?103}104105/// The single source of truth for "which models can I pick": cloud providers, custom endpoints and on-device models.106/// Model lists are cached in the local store so the pickers work offline; a cached list always shows when it was fetched.107@MainActor @Observable public final class ModelLibrary {108    public private(set) var providers: [ProviderKind: ProviderState] = [:]109    public private(set) var endpoints: [EndpointProfile] = []110    public private(set) var endpointModels: [String: [ModelInfo]] = [:]111    public private(set) var endpointErrors: [String: AstraError] = [:]112    /// On-device models that can be chatted with right now (installed MLX models, the system model when available).113    public var localModels: [ModelInfo] = []114    public private(set) var favorites: Set<String> = []115    public private(set) var recents: [String] = []116117    private let store: AstraStore118    private let credentials: any CredentialStore119    private let resolve: @Sendable (ModelRef) async throws -> any ChatProvider120121    public init(store: AstraStore, credentials: any CredentialStore, resolve: @escaping @Sendable (ModelRef) async throws -> any ChatProvider) {122        self.store = store; self.credentials = credentials; self.resolve = resolve123        for kind in ProviderKind.cloudKeyed { providers[kind] = ProviderState() }124    }125126    private struct CachedModels: Codable, Sendable { var fetchedAt: Date; var models: [ModelInfo] }127128    /// Loads keys' presence, cached lists, endpoints, favourites. No network.129    public func load() async {130        for kind in ProviderKind.cloudKeyed {131            var state = providers[kind] ?? ProviderState()132            let key = try? credentials.secret(for: CredentialAccount.provider(kind))133            state.hasKey = key != nil134            state.maskedKey = key.map(Redactor.mask(key:))135            if let cached: CachedModels = try? await store.setting("models.cache.\(kind.rawValue)") {136                state.models = cached.models; state.modelsFetchedAt = cached.fetchedAt137            }138            providers[kind] = state139        }140        endpoints = (try? await store.endpoints()) ?? []141        for e in endpoints {142            if let cached: CachedModels = try? await store.setting("models.cache.endpoint.\(e.id)") { endpointModels[e.id] = cached.models }143        }144        favorites = Set((try? await store.setting("models.favorites", default: [String]())) ?? [])145        recents = (try? await store.setting("models.recents", default: [String]())) ?? []146    }147148    // MARK: Keys149150    /// Saves a key in the Keychain, then proves it works. The key itself is never kept in observable state.151    @discardableResult public func setKey(_ key: String, for kind: ProviderKind) async -> ConnectionStatus? {152        do { try credentials.setSecret(key, for: CredentialAccount.provider(kind)) }153        catch { providers[kind]?.error = .local(error.localizedDescription); return nil }154        providers[kind]?.hasKey = true155        providers[kind]?.maskedKey = Redactor.mask(key: key)156        return await refresh(kind)157    }158159    public func removeKey(for kind: ProviderKind) async {160        try? credentials.removeSecret(for: CredentialAccount.provider(kind))161        try? await store.removeSetting("models.cache.\(kind.rawValue)")162        providers[kind] = ProviderState()163    }164165    /// Verifies the key and refreshes the model list from the provider.166    @discardableResult public func refresh(_ kind: ProviderKind) async -> ConnectionStatus? {167        guard providers[kind]?.hasKey == true else { return nil }168        providers[kind]?.isLoading = true; providers[kind]?.error = nil169        defer { providers[kind]?.isLoading = false }170        do {171            let provider = try await resolve(ModelRef(provider: kind, modelID: "-"))172            let status = try await provider.verify()173            providers[kind]?.status = status174            let models = try await provider.listModels()175            providers[kind]?.models = models176            providers[kind]?.modelsFetchedAt = Date()177            try? await store.setSetting("models.cache.\(kind.rawValue)", CachedModels(fetchedAt: Date(), models: models))178            return status179        } catch {180            let mapped = AstraError.wrap(error)181            providers[kind]?.error = mapped182            providers[kind]?.status = ConnectionStatus(ok: false, detail: mapped.message)183            return providers[kind]?.status184        }185    }186187    public func refreshAllKeyed() async {188        await withTaskGroup(of: Void.self) { group in189            for kind in ProviderKind.cloudKeyed where providers[kind]?.hasKey == true { group.addTask { await self.refresh(kind) } }190        }191    }192193    // MARK: Custom endpoints194195    /// Validates and saves a profile; the secret (optional) goes to the Keychain under the profile's own account.196    public func saveEndpoint(_ profile: EndpointProfile, key: String?) async throws {197        _ = try profile.validated()198        try await store.saveEndpoint(profile)199        if let key, !key.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty { try credentials.setSecret(key, for: CredentialAccount.endpoint(profile.id)) }200        endpoints = (try? await store.endpoints()) ?? endpoints201        await refreshEndpoint(profile.id)202    }203204    public func deleteEndpoint(_ id: String) async {205        try? await store.deleteEndpoint(id: id)206        try? credentials.removeSecret(for: CredentialAccount.endpoint(id))207        try? await store.removeSetting("models.cache.endpoint.\(id)")208        endpoints.removeAll { $0.id == id }; endpointModels[id] = nil; endpointErrors[id] = nil209    }210211    public func endpointHasKey(_ id: String) -> Bool { ((try? credentials.secret(for: CredentialAccount.endpoint(id))) ?? nil) != nil }212213    @discardableResult public func refreshEndpoint(_ id: String) async -> Bool {214        guard let profile = endpoints.first(where: { $0.id == id }) else { return false }215        do {216            let provider = try await resolve(ModelRef(provider: .custom, modelID: profile.defaultModelID ?? "-", endpointID: id))217            var models = try await provider.listModels()218            if models.isEmpty, let fallback = profile.defaultModelID, !fallback.isEmpty {219                models = [ModelInfo(ref: ModelRef(provider: .custom, modelID: fallback, endpointID: id), displayName: fallback, source: .user)]220            }221            endpointModels[id] = models; endpointErrors[id] = nil222            try? await store.setSetting("models.cache.endpoint.\(id)", CachedModels(fetchedAt: Date(), models: models))223            return true224        } catch {225            endpointErrors[id] = AstraError.wrap(error)226            if endpointModels[id] == nil, let fallback = profile.defaultModelID, !fallback.isEmpty {227                endpointModels[id] = [ModelInfo(ref: ModelRef(provider: .custom, modelID: fallback, endpointID: id), displayName: fallback, source: .user)]228            }229            return false230        }231    }232233    // MARK: Lookup234235    public func models(for kind: ProviderKind) -> [ModelInfo] {236        switch kind {237        case .mlx, .appleFoundation: localModels.filter { $0.ref.provider == kind }238        case .custom: endpoints.flatMap { endpointModels[$0.id] ?? [] }239        default: providers[kind]?.models ?? []240        }241    }242243    /// Every model that can be selected right now.244    public var selectable: [ModelInfo] {245        localModels + ProviderKind.cloudKeyed.flatMap { providers[$0]?.hasKey == true ? (providers[$0]?.models ?? []) : [] }246            + endpoints.flatMap { endpointModels[$0.id] ?? [] }247    }248249    public func info(for ref: ModelRef) -> ModelInfo? {250        if ref.provider.isLocal { return localModels.first { $0.ref == ref } }251        if ref.provider == .custom { return endpointModels[ref.endpointID ?? ""]?.first { $0.ref == ref } }252        // Before the provider's list has loaded (first launch, offline), fall back to the dated curated facts.253        return providers[ref.provider]?.models.first { $0.ref == ref } ?? CuratedCatalog.info(for: ref)254    }255256    public func displayName(for ref: ModelRef) -> String { info(for: ref)?.displayName ?? ref.modelID }257258    public func endpointName(for ref: ModelRef) -> String? { endpoints.first { $0.id == ref.endpointID }?.name }259260    /// Whether a model can be used right now, and if not, why.261    public func availability(of ref: ModelRef) -> AstraError? {262        switch ref.provider {263        case .mlx, .appleFoundation: return localModels.contains { $0.ref == ref } ? nil : .local("This on-device model is not installed or not available on this device.")264        case .custom: return endpoints.contains { $0.id == ref.endpointID } ? nil : .invalidRequest("This custom endpoint was removed.")265        default: return providers[ref.provider]?.hasKey == true ? nil : .missingCredential(ref.provider)266        }267    }268269    public var hasAnyUsableModel: Bool { !selectable.isEmpty }270271    /// A sensible default: the most recent model still available, else the first local model, else the first cloud model.272    public func defaultModel() -> ModelRef? {273        for id in recents { if let ref = ModelRef(id: id), availability(of: ref) == nil, info(for: ref) != nil { return ref } }274        return localModels.first?.ref ?? selectable.first?.ref275    }276277    // MARK: Favourites and recents278279    public func isFavorite(_ ref: ModelRef) -> Bool { favorites.contains(ref.id) }280281    public func toggleFavorite(_ ref: ModelRef) {282        if favorites.contains(ref.id) { favorites.remove(ref.id) } else { favorites.insert(ref.id) }283        let snapshot = favorites.sorted()284        Task { try? await store.setSetting("models.favorites", snapshot) }285    }286287    public func noteUsed(_ ref: ModelRef) {288        recents.removeAll { $0 == ref.id }; recents.insert(ref.id, at: 0)289        if recents.count > 8 { recents = Array(recents.prefix(8)) }290        let snapshot = recents291        Task { try? await store.setSetting("models.recents", snapshot) }292    }293294    public var recentModels: [ModelInfo] { recents.compactMap { ModelRef(id: $0) }.compactMap { info(for: $0) }.filter { availability(of: $0.ref) == nil } }295    public var favoriteModels: [ModelInfo] { selectable.filter { favorites.contains($0.id) } }296}297298public extension ModelInfo {299    /// "128K", "1M": compact context length, or nil when unknown.300    var contextLabel: String? {301        guard let n = contextLength, n > 0 else { return nil }302        if n >= 1_000_000 { return n % 1_000_000 == 0 ? "\(n / 1_000_000)M" : String(format: "%.1fM", Double(n) / 1_000_000) }303        if n >= 1_000 { return "\(Int((Double(n) / 1000).rounded()))K" }304        return "\(n)"305    }306307    /// "$2 / $10 per 1M" with its date, "Free", or nil when no price is known.308    var priceLabel: String? {309        guard let p = pricing else { return nil }310        if p.isFree { return "Free" }311        func f(_ v: Double) -> String { v >= 1 ? (v.rounded() == v ? String(format: "$%.0f", v) : String(format: "$%.2f", v)) : String(format: "$%.3g", v) }312        return "\(f(p.inputPerMTok)) in · \(f(p.outputPerMTok)) out / 1M"313    }314}315
Files main
Menu

Theme