Repository Public MIT
1// =============================================================================2// SP BOUCHER | SOFTWARE & RESEARCH3// ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// PROJECT METADATA7// -----------------------------------------------------------------------------8// Project : ORIX Astra9// File : Packages/AstraUI/Sources/Models/ModelsScreen.swift10// Module : Models11// 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 SwiftUI89import AstraCore90import AstraDesign91import AstraLocal9293/// The Models tab: what runs on this iPhone, which cloud providers are connected, and custom endpoints.94struct ModelsScreen: View {95 @Environment(AppModel.self) private var app96 @State private var keyEditor: ProviderKind?97 @State private var endpointEditor: EndpointDraft?98 @State private var detail: LocalModelCard?99100 var body: some View {101 NavigationStack {102 ScrollView {103 VStack(alignment: .leading, spacing: AstraSpace.xl) {104 onDeviceSection105 cloudSection106 endpointSection107 }108 .padding(.horizontal, AstraSpace.page).padding(.vertical, AstraSpace.lg)109 }110 .background(Starfield(density: 0.5))111 .navigationTitle("Models")112 .refreshable { await app.local.refreshCatalog(); await app.library.refreshAllKeyed(); await app.local.refreshInstalled() }113 .sheet(item: $keyEditor) { kind in ProviderKeySheet(kind: kind) }114 .sheet(item: $endpointEditor) { draft in EndpointSheet(draft: draft) }115 .sheet(item: $detail) { card in LocalModelDetailSheet(card: card) }116 .navigationDestination(for: ProviderKind.self) { ProviderModelsList(kind: $0) }117 .task { if app.local.catalog.entries.isEmpty || app.local.catalogNote == nil { await app.local.refreshCatalog() } }118 }119 }120121 // MARK: On device122123 private var onDeviceSection: some View {124 VStack(alignment: .leading, spacing: AstraSpace.md) {125 SectionTitle("On this iPhone", detail: storageLine)126 appleCard127 if let reason = app.local.mlxUnavailableReason {128 NoticeCard(.info, title: "MLX models need a real device", message: reason)129 }130 if let warning = app.local.snapshot?.warning { NoticeCard(.warning, title: "Device is warm", message: warning) }131 if let note = app.local.catalogNote { NoticeCard(.info, title: "Catalogue", message: note) }132 ForEach(app.local.catalog.entries) { entry in133 LocalModelCell(card: entry.card, origin: entry.origin) { detail = entry.card }134 }135 if app.local.catalog.entries.isEmpty {136 AstraCard { Text("The model catalogue is empty.").font(AstraFont.subheadline).foregroundStyle(AstraColor.textSecondary) }137 }138 Text("Every model in this list was loaded and run with MLX before being listed. Speeds shown on a model's page were measured on the machine named there, not on your iPhone.")139 .font(AstraFont.caption).foregroundStyle(AstraColor.textTertiary)140 }141 }142143 private var storageLine: String? {144 let used = app.local.totalBytes145 guard used > 0 || app.local.freeBytes != nil else { return nil }146 var s = "\(MemoryGovernor.format(used)) used"147 if let free = app.local.freeBytes { s += " · \(MemoryGovernor.format(free)) free" }148 return s149 }150151 private var appleCard: some View {152 let availability = app.local.appleAvailability153 return AstraCard {154 HStack(alignment: .top, spacing: 12) {155 ProviderLogo(provider: .appleFoundation, size: 38)156 VStack(alignment: .leading, spacing: 4) {157 Text("Apple Intelligence").font(AstraFont.bodyEmphasis).foregroundStyle(AstraColor.textPrimary)158 Text(availability.isAvailable ? "The system's on-device model. Nothing to download; it is part of iOS and managed by Apple."159 : (availability.reason ?? "Not available on this device."))160 .font(AstraFont.footnote).foregroundStyle(AstraColor.textSecondary).fixedSize(horizontal: false, vertical: true)161 AstraBadge(availability.isAvailable ? "Ready" : "Unavailable", symbol: availability.isAvailable ? "checkmark" : "minus.circle", tone: availability.isAvailable ? .success : .neutral)162 }163 Spacer(minLength: 0)164 }165 }166 .accessibilityElement(children: .combine)167 }168169 // MARK: Cloud170171 private var cloudSection: some View {172 VStack(alignment: .leading, spacing: AstraSpace.md) {173 SectionTitle("Cloud providers", detail: "Your own keys")174 ForEach(ProviderKind.cloudKeyed) { kind in175 let state = app.library.providers[kind] ?? ProviderState()176 AstraCard(padding: 0) {177 VStack(spacing: 0) {178 Button { keyEditor = kind } label: {179 HStack(spacing: 12) {180 ProviderLogo(provider: kind, size: 38)181 VStack(alignment: .leading, spacing: 3) {182 Text(kind.displayName).font(AstraFont.bodyEmphasis).foregroundStyle(AstraColor.textPrimary)183 Text(statusLine(state)).font(AstraFont.caption).foregroundStyle(statusColor(state)).lineLimit(2)184 }185 Spacer(minLength: 0)186 if state.isLoading { ProgressView() }187 else { Text(state.hasKey ? "Manage" : "Add key").font(AstraFont.subheadline.weight(.semibold)).foregroundStyle(AstraColor.violet) }188 }189 .padding(AstraSpace.lg).contentShape(Rectangle())190 }191 .buttonStyle(.plain)192 .accessibilityIdentifier("provider-\(kind.rawValue)")193 if state.hasKey, !state.models.isEmpty {194 Divider().overlay(AstraColor.stroke)195 NavigationLink(value: kind) {196 HStack {197 Text("\(state.models.count) models").font(AstraFont.subheadline).foregroundStyle(AstraColor.textSecondary)198 Spacer()199 Image(systemName: "chevron.right").font(.caption.weight(.semibold)).foregroundStyle(AstraColor.textTertiary)200 }201 .padding(.horizontal, AstraSpace.lg).frame(minHeight: AstraSpace.hit).contentShape(Rectangle())202 }203 .buttonStyle(.plain)204 }205 }206 }207 }208 }209 }210211 private func statusLine(_ s: ProviderState) -> String {212 if !s.hasKey { return "Not connected" }213 if let e = s.error { return e.message }214 if let status = s.status { return status.ok ? "Connected · \(s.maskedKey ?? "")" : status.detail }215 return "Key saved · \(s.maskedKey ?? "")" + (s.modelsFetchedAt == nil ? "" : " · list cached")216 }217 private func statusColor(_ s: ProviderState) -> Color {218 if !s.hasKey { return AstraColor.textTertiary }219 if s.error != nil || s.status?.ok == false { return AstraColor.danger }220 return s.status?.ok == true ? AstraColor.success : AstraColor.textSecondary221 }222223 // MARK: Endpoints224225 private var endpointSection: some View {226 VStack(alignment: .leading, spacing: AstraSpace.md) {227 SectionTitle("Custom endpoints", detail: "OpenAI-compatible")228 ForEach(app.library.endpoints) { endpoint in229 AstraCard {230 Button { endpointEditor = EndpointDraft(profile: endpoint, isNew: false) } label: {231 HStack(spacing: 12) {232 ProviderLogo(provider: .custom, size: 38)233 VStack(alignment: .leading, spacing: 3) {234 Text(endpoint.name).font(AstraFont.bodyEmphasis).foregroundStyle(AstraColor.textPrimary)235 Text(endpoint.baseURL).font(AstraFont.caption).foregroundStyle(AstraColor.textSecondary).lineLimit(1)236 if let e = app.library.endpointErrors[endpoint.id] { Text(e.message).font(AstraFont.caption).foregroundStyle(AstraColor.danger).lineLimit(2) }237 else { Text("\(app.library.endpointModels[endpoint.id]?.count ?? 0) models").font(AstraFont.caption).foregroundStyle(AstraColor.textTertiary) }238 }239 Spacer(minLength: 0)240 if endpoint.allowInsecureHTTP { AstraBadge("HTTP", symbol: "lock.open", tone: .warning) }241 }242 .contentShape(Rectangle())243 }244 .buttonStyle(.plain)245 }246 }247 Button { endpointEditor = EndpointDraft(profile: EndpointProfile(name: "", baseURL: "https://"), isNew: true) } label: {248 Label("Add an endpoint", systemImage: "plus")249 }250 .buttonStyle(AstraSecondaryButtonStyle()).accessibilityIdentifier("add-endpoint")251 Text("Use this for your own inference server or any service that speaks the OpenAI chat API. Nothing is preconfigured.")252 .font(AstraFont.caption).foregroundStyle(AstraColor.textTertiary)253 }254 }255}256257// MARK: - Local model cell258259struct LocalModelCell: View {260 @Environment(AppModel.self) private var app261 let card: LocalModelCard262 let origin: ModelCatalog.Origin263 let openDetail: () -> Void264 @State private var confirmRemove = false265 @State private var confirmCellular = false266267 private var status: DownloadStatus? { app.local.statuses[card.id] }268 private var installed: Bool { app.local.isInstalled(card.id) }269 private var loaded: Bool { app.local.snapshot?.isLoaded(card.id) ?? false }270271 var body: some View {272 let verdict = app.local.verdict(card)273 AstraCard {274 VStack(alignment: .leading, spacing: 10) {275 Button(action: openDetail) {276 HStack(alignment: .top, spacing: 12) {277 ProviderLogo(ref: ModelRef(provider: .mlx, modelID: card.id), vendor: card.publisher, size: 38)278 VStack(alignment: .leading, spacing: 4) {279 Text(card.displayName).font(AstraFont.bodyEmphasis).foregroundStyle(AstraColor.textPrimary)280 Text("\(card.publisher) · \(card.quantization) · \(MemoryGovernor.format(card.totalBytes))")281 .font(AstraFont.caption).foregroundStyle(AstraColor.textSecondary).lineLimit(1)282 FlowLayout(spacing: 4) {283 fitBadge(verdict)284 AstraBadge(card.licenseID, symbol: "doc.text")285 if installed { AstraBadge(loaded ? "In memory" : "Downloaded", symbol: loaded ? "bolt.fill" : "checkmark", tone: .success) }286 if origin == .onlineCatalog { AstraBadge(origin.label, tone: .cyan) }287 }288 }289 Spacer(minLength: 0)290 Image(systemName: "chevron.right").font(.caption.weight(.semibold)).foregroundStyle(AstraColor.textTertiary).padding(.top, 4)291 }292 .contentShape(Rectangle())293 }294 .buttonStyle(.plain)295 .accessibilityHint("Shows the model card")296297 if let status, !installed || status.phase.isActive { progress(status) }298 actions(verdict)299 }300 }301 .confirmationDialog("Remove \(card.displayName)?", isPresented: $confirmRemove, titleVisibility: .visible) {302 Button("Remove and free \(MemoryGovernor.format(card.totalBytes))", role: .destructive) {303 Task { let freed = await app.local.remove(card.id); app.show("Removed. \(MemoryGovernor.format(freed)) freed.", .success) }304 }305 Button("Cancel", role: .cancel) {}306 } message: { Text("The weights, partial files and caches of this model are deleted from this iPhone. Chats that used it are kept.") }307 }308309 private func fitBadge(_ v: FitVerdict) -> some View {310 switch v.state {311 case .comfortable: AstraBadge("Fits this device", symbol: "checkmark.seal", tone: .success)312 case .tight: AstraBadge("Tight fit", symbol: "exclamationmark.triangle", tone: .warning)313 case .unlikely: AstraBadge("Unlikely to fit", symbol: "exclamationmark.triangle", tone: .danger)314 case .tooLarge: AstraBadge("Too large", symbol: "xmark.octagon", tone: .danger)315 }316 }317318 @ViewBuilder private func progress(_ s: DownloadStatus) -> some View {319 VStack(alignment: .leading, spacing: 6) {320 AstraProgressBar(s.fraction)321 HStack {322 Text(phaseLabel(s)).font(AstraFont.caption).foregroundStyle({ if case .failed = s.phase { return AstraColor.danger }; return AstraColor.textSecondary }())323 .fixedSize(horizontal: false, vertical: true)324 Spacer()325 Text("\(MemoryGovernor.format(s.bytesDone)) of \(MemoryGovernor.format(s.bytesTotal))").font(AstraFont.micro.monospacedDigit()).foregroundStyle(AstraColor.textTertiary)326 }327 }328 .accessibilityElement(children: .combine)329 }330331 private func phaseLabel(_ s: DownloadStatus) -> String {332 switch s.phase {333 case .preparing: return "Preparing…"334 case .downloading:335 var t = s.usedFallback ? "Downloading from Hugging Face" : "Downloading"336 if let speed = s.bytesPerSecond, speed > 0 { t += " · \(MemoryGovernor.format(Int64(speed)))/s" }337 if let eta = s.etaSeconds, eta.isFinite, eta > 0 { t += " · \(Self.duration(eta)) left" }338 return t339 case .verifying: return "Verifying checksums…"340 case .paused: return "Paused"341 case .failed(let e): return e.errorDescription ?? "The download failed."342 case .completed: return "Verified"343 case .cancelled: return "Cancelled"344 }345 }346347 static func duration(_ seconds: Double) -> String {348 let s = Int(seconds)349 return s >= 3600 ? "\(s / 3600) h \(s % 3600 / 60) min" : s >= 60 ? "\(s / 60) min" : "\(s) s"350 }351352 @ViewBuilder private func actions(_ verdict: FitVerdict) -> some View {353 HStack(spacing: 8) {354 if installed {355 Button("Chat") { Task { await app.newConversation(model: ModelRef(provider: .mlx, modelID: card.id)) } }356 .buttonStyle(AstraSecondaryButtonStyle(compact: true)).disabled(app.local.mlxUnavailableReason != nil)357 if loaded { Button("Unload") { Task { await app.local.unload(card.id) } }.buttonStyle(AstraSecondaryButtonStyle(compact: true)) }358 Spacer()359 Button(role: .destructive) { confirmRemove = true } label: { Image(systemName: "trash").frame(width: AstraSpace.hit, height: AstraSpace.hit) }360 .accessibilityLabel("Remove \(card.displayName)")361 } else if let status {362 switch status.phase {363 case .preparing, .downloading, .verifying:364 Button("Pause") { Task { await app.local.pause(card.id) } }.buttonStyle(AstraSecondaryButtonStyle(compact: true))365 Button("Cancel") { Task { await app.local.cancel(card.id) } }.buttonStyle(AstraSecondaryButtonStyle(compact: true))366 case .paused, .failed:367 Button({ if case .failed = status.phase { return "Retry" }; return "Resume" }()) {368 Task { if let e = await app.local.resume(card.id) { app.show(e.message, .error) } }369 }.buttonStyle(AstraSecondaryButtonStyle(compact: true))370 Button("Cancel") { Task { await app.local.cancel(card.id) } }.buttonStyle(AstraSecondaryButtonStyle(compact: true))371 case .completed, .cancelled: downloadButton(verdict)372 }373 Spacer()374 } else {375 downloadButton(verdict)376 Spacer()377 }378 }379 }380381 private func downloadButton(_ verdict: FitVerdict) -> some View {382 Button {383 Task { if let e = await app.local.download(card) { app.show(e.message, .error) } }384 } label: { Label("Download \(MemoryGovernor.format(card.totalBytes))", systemImage: "arrow.down.circle") }385 .buttonStyle(AstraSecondaryButtonStyle(compact: true))386 .disabled(verdict.state == .tooLarge)387 .accessibilityIdentifier("download-\(card.id)")388 }389}390391/// The full model card: provenance, licence, files, memory estimate and verification record.392struct LocalModelDetailSheet: View {393 @Environment(AppModel.self) private var app394 @Environment(\.dismiss) private var dismiss395 let card: LocalModelCard396397 var body: some View {398 let verdict = app.local.verdict(card)399 NavigationStack {400 List {401 Section("Model") {402 row("Family", card.family); row("Publisher", card.publisher); row("Architecture", card.architecture)403 row("Quantization", card.quantization)404 if let p = card.parameterCount { row("Parameters", Self.count(p)) }405 row("Context", "\(card.contextLength) tokens")406 row("Tasks", card.supportedTasks.joined(separator: ", "))407 }408 Section {409 row("Download", MemoryGovernor.format(card.totalBytes))410 row("Estimated memory in use", MemoryGovernor.format(verdict.estimatedNeedBytes))411 row("This device", MemoryGovernor.format(verdict.physicalBytes) + " RAM")412 Text(verdict.explanation).font(AstraFont.footnote).foregroundStyle(AstraColor.textSecondary)413 } header: { Text("Size and memory") } footer: { Text("Memory figures are conservative estimates, not guarantees: what is available depends on what else your iPhone is doing.") }414 Section {415 row("Repository", card.repo)416 row("Revision", String(card.revision.prefix(12)))417 row("Licence", card.licenseID)418 if let url = URL(string: card.licenseURL) { Link("Read the licence", destination: url) }419 row("Downloaded from", card.redistributable && card.baseURL != nil ? "orixlab.dev, then Hugging Face as fallback" : "Hugging Face")420 row("Integrity", "SHA-256 of all \(card.files.count) files checked after download")421 } header: { Text("Provenance") }422 Section {423 if let v = card.verified {424 row("Verified on", v.date); row("Machine", "\(v.machine) (\(v.platform) \(v.osVersion))")425 row("Decode speed", String(format: "%.1f tok/s", v.tokensPerSecond)); row("First token", String(format: "%.2f s", v.timeToFirstToken))426 if let peak = v.peakMemoryBytes { row("Peak memory", MemoryGovernor.format(peak)) }427 } else { Text("No verification record.").foregroundStyle(AstraColor.textSecondary) }428 } header: { Text("Verification") } footer: {429 Text("These numbers were measured on the machine named above. Speed and memory on your iPhone will differ; run the benchmark in Lab to measure them here.")430 }431 if let notes = card.notes, !notes.isEmpty { Section("Notes") { Text(notes).font(AstraFont.footnote) } }432 }433 .scrollContentBackground(.hidden).background(AstraColor.background)434 .navigationTitle(card.displayName).navigationBarTitleDisplayMode(.inline)435 .toolbar { ToolbarItem(placement: .confirmationAction) { Button("Done") { dismiss() } } }436 }437 }438439 private func row(_ label: String, _ value: String) -> some View {440 LabeledContent(label) { Text(value).multilineTextAlignment(.trailing).textSelection(.enabled) }.font(AstraFont.subheadline)441 }442 static func count(_ n: Int64) -> String { n >= 1_000_000_000 ? String(format: "%.1f B", Double(n) / 1e9) : String(format: "%.0f M", Double(n) / 1e6) }443}444445/// All models of one provider, searchable, with the facts the provider reports.446struct ProviderModelsList: View {447 @Environment(AppModel.self) private var app448 let kind: ProviderKind449 @State private var query = ""450 var body: some View {451 let all = app.library.models(for: kind)452 let q = query.lowercased()453 let models = q.isEmpty ? all : all.filter { $0.displayName.lowercased().contains(q) || $0.ref.modelID.lowercased().contains(q) }454 List {455 Section {456 ForEach(models) { model in457 ModelRow(model: model, isFavorite: app.library.isFavorite(model.ref))458 .listRowBackground(AstraColor.surface)459 .swipeActions(edge: .trailing) {460 Button { app.library.toggleFavorite(model.ref) } label: { Label("Favourite", systemImage: "star") }.tint(AstraColor.warning)461 Button { Task { await app.newConversation(model: model.ref) } } label: { Label("Chat", systemImage: "bubble.left") }.tint(AstraColor.violet)462 }463 }464 } footer: {465 if let fetched = app.library.providers[kind]?.modelsFetchedAt {466 Text("List fetched \(fetched.formatted(date: .abbreviated, time: .shortened)). Context sizes, prices and capability badges appear only when \(kind.displayName) or its documentation states them; prices are per million tokens and dated.")467 }468 }469 }470 .listStyle(.insetGrouped).scrollContentBackground(.hidden).background(AstraColor.background)471 .searchable(text: $query, prompt: "Search \(kind.displayName) models")472 .navigationTitle(kind.displayName).navigationBarTitleDisplayMode(.inline)473 .refreshable { await app.library.refresh(kind) }474 }475}476