Repository Public
1// Author: Simon-Pierre Boucher — contact@spboucher.ai2// File: Packages/LocalChatUI/Sources/Features/Models/ModelUIHelpers.swift3// Description: Small shared pieces of the Models UI: capability chips, descriptor facts line, compact number formatting, app-level helpers.4import SwiftUI5import LocalChatCore6import LocalChatModelHub78struct CapabilityChips: View {9 let capabilities: Set<ModelCapability>10 var body: some View {11 HStack(spacing: 4) {12 ForEach(ModelCapability.allCases.filter { capabilities.contains($0) }, id: \.self) { c in Pill(c.label, symbol: c.symbol) }13 }14 }15}1617enum ModelFacts {18 static func parameters(_ m: ModelDescriptor) -> String? {19 guard let label = m.parameterLabel else { return nil }20 switch m.parameterProvenance {21 case .inferred: return "≈\(label) params (from name)"22 case .declared: return "\(label) params"23 case .measured: return "\(label) params (measured)"24 case .unknown: return "\(label) params"25 }26 }27 /// Facts known about a model, in reading order. Unknown values are left out, never guessed.28 static func line(_ m: ModelDescriptor) -> [String] {29 var out: [String] = []30 if let a = m.architecture { out.append(a) }31 if let p = parameters(m) { out.append(p) }32 if let q = m.quantization { out.append(q) }33 if let c = m.contextLength { out.append("\(compact(c)) ctx") }34 if let s = m.sizeBytes { out.append(ByteFormat.string(s)) }35 return out36 }37 static func compact(_ n: Int) -> String {38 if n >= 1_000_000 { return String(format: "%.1fM", Double(n) / 1e6) }39 if n >= 1000 { return String(format: n % 1000 == 0 ? "%.0fk" : "%.1fk", Double(n) / 1000) }40 return "\(n)"41 }42 static func relative(_ date: Date?) -> String? {43 guard let date else { return nil }44 let f = RelativeDateTimeFormatter(); f.unitsStyle = .abbreviated45 return f.localizedString(for: date, relativeTo: Date())46 }47}4849struct FactsLine: View {50 let facts: [String]51 var body: some View {52 Text(facts.joined(separator: " · ")).font(DS.F.caption).foregroundStyle(DS.C.text2).lineLimit(2).fixedSize(horizontal: false, vertical: true)53 }54}5556extension AppModel {57 var hubResolver: ModelMetadataResolver { ModelMetadataResolver() }5859 func isDownloading(_ id: String) -> DownloadState? { downloadStates.first { $0.id == id && !$0.phase.isFinished } }6061 /// Queues a hub candidate for download. Returns the error text when it cannot be queued.62 func startDownload(_ candidate: ModelCandidate) async -> String? {63 guard let request = DownloadRequest(candidate: candidate) else { return "This repository has no file this app can use." }64 do { try await downloads.enqueue(request); Haptics.play(.light); return nil }65 catch { return error.localizedDescription }66 }6768 var hasHubToken: Bool { ((try? keychain.string(for: "huggingface.token")) ?? nil)?.isEmpty == false }69}7071func hubErrorText(_ error: Error) -> String {72 if let e = error as? HubError { return e.errorDescription ?? "Hugging Face request failed." }73 if (error as? URLError)?.code == .notConnectedToInternet { return "You appear to be offline. Installed models keep working." }74 return error.localizedDescription75}76