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/Lab/ModelExplorer.swift10// Module : Lab11// 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 SwiftUI89import AstraCore90import AstraDesign91import AstraLocal92import AstraPersistence9394/// Everything Astra knows about one model, each fact with where it comes from, and what a request to it may contain.95struct ModelExplorerView: View {96 @Environment(AppModel.self) private var app97 @State private var model: ModelRef?98 @State private var showPicker = false99100 private enum Verdict { case yes, no, undeclared }101102 var body: some View {103 LabPage(title: "Model explorer") {104 AstraCard { ModelChoiceRow(ref: model, placeholder: "Choose a model to inspect") { showPicker = true }.accessibilityIdentifier("explorer-model") }105 if let model {106 if let info = app.library.info(for: model) { page(info) }107 else { NoticeCard(.warning, title: "No metadata for this model", message: "It is no longer in the list its provider returned. Refresh the provider in Models.") }108 } else if !app.library.hasAnyUsableModel {109 EmptyStateView(symbol: "square.stack.3d.up.slash", title: "No model yet", message: "Download an on-device model or add a provider key in Models, then inspect it here.") {110 Button("Open Models") { app.tab = .models }.buttonStyle(AstraPrimaryButtonStyle())111 }112 .frame(maxWidth: .infinity)113 }114 }115 .task { if model == nil { model = app.library.defaultModel() } }116 .sheet(isPresented: $showPicker) { ModelPickerSheet(selected: model, title: "Inspect a model") { model = $0 } }117 }118119 private func sourceDetail(_ source: MetadataSource) -> String {120 switch source {121 case .providerAPI(let fetchedAt): "Returned by the provider's API on \(LabFormat.dateTime(fetchedAt))."122 case .documentation(let asOf, let url): "Curated from the provider's documentation on \(asOf): \(url)"123 case .runtime: "Read from the model files and the runtime on this device."124 case .user: "Entered by you for a custom endpoint."125 case .unknown: "No source: these fields are unverified and may be wrong."126 }127 }128129 @ViewBuilder private func page(_ info: ModelInfo) -> some View {130 let source = info.source.label131 VStack(alignment: .leading, spacing: AstraSpace.md) {132 SectionTitle("Identity")133 AstraCard {134 VStack(alignment: .leading, spacing: 0) {135 HStack(spacing: AstraSpace.sm) {136 AstraBadge(source, symbol: info.source == .unknown ? "questionmark.circle" : "checkmark.seal", tone: info.source == .unknown ? .warning : .accent)137 if let unavailable = app.library.availability(of: info.ref) { AstraBadge(unavailable.title, symbol: "exclamationmark.triangle", tone: .danger) }138 else { AstraBadge("Usable now", symbol: "checkmark", tone: .success) }139 }140 .padding(.bottom, AstraSpace.sm)141 Text(sourceDetail(info.source)).font(AstraFont.caption).foregroundStyle(AstraColor.textSecondary).fixedSize(horizontal: false, vertical: true).textSelection(.enabled)142 .padding(.bottom, AstraSpace.sm)143 FactRow(label: "Name", value: info.displayName, source: source)144 FactRow(label: "Model id", value: info.ref.modelID, source: "Sent to the provider as is", mono: true)145 FactRow(label: "Runs at", value: app.library.endpointName(for: info.ref) ?? info.ref.provider.displayName)146 FactRow(label: "Vendor", value: info.vendor, source: info.vendor == nil ? nil : source)147 FactRow(label: "Released", value: info.createdAt.map { $0.formatted(date: .abbreviated, time: .omitted) }, source: info.createdAt == nil ? nil : source)148 if let summary = info.summary, !summary.isEmpty {149 Text(summary).font(AstraFont.footnote).foregroundStyle(AstraColor.textSecondary).fixedSize(horizontal: false, vertical: true).padding(.top, AstraSpace.sm)150 }151 }152 }153 }154 VStack(alignment: .leading, spacing: AstraSpace.md) {155 SectionTitle("Limits")156 AstraCard {157 VStack(alignment: .leading, spacing: 0) {158 FactRow(label: "Context window", value: info.contextLength.map { "\($0.formatted(.number)) tokens" }, source: info.contextLength == nil ? "Not published" : source)159 FactRow(label: "Maximum output", value: info.maxOutputTokens.map { "\($0.formatted(.number)) tokens" }, source: info.maxOutputTokens == nil ? "Not published" : source)160 }161 }162 }163 capabilities(info)164 compatibility(info)165 parameters(info)166 pricing(info)167 if info.ref.provider == .mlx, let card = app.local.card(info.ref.modelID) { local(card) }168 }169170 private func capabilities(_ info: ModelInfo) -> some View {171 VStack(alignment: .leading, spacing: AstraSpace.md) {172 SectionTitle("Capabilities", detail: info.source.label)173 AstraCard {174 VStack(alignment: .leading, spacing: 0) {175 ForEach(Capability.allCases, id: \.self) { capability in176 let has = info.capabilities.contains(capability)177 HStack(spacing: AstraSpace.md) {178 Image(systemName: capability.symbol).foregroundStyle(has ? AstraColor.violet : AstraColor.textTertiary).frame(width: 24).accessibilityHidden(true)179 Text(capability.label).font(AstraFont.subheadline).foregroundStyle(has ? AstraColor.textPrimary : AstraColor.textTertiary)180 Spacer(minLength: AstraSpace.sm)181 Label(has ? "Declared" : "Not declared", systemImage: has ? "checkmark.circle.fill" : "minus.circle")182 .font(AstraFont.caption).foregroundStyle(has ? AstraColor.success : AstraColor.textTertiary)183 }184 .frame(minHeight: 36)185 .accessibilityElement(children: .combine)186 }187 if info.source == .unknown {188 Text("These capabilities have no source. Astra shows no capability badge for this model elsewhere in the app.")189 .font(AstraFont.caption).foregroundStyle(AstraColor.warning).fixedSize(horizontal: false, vertical: true).padding(.top, AstraSpace.sm)190 }191 }192 }193 }194 }195196 /// Runs the same negotiation every request goes through, with everything switched on, and reports what survives.197 private func compatibility(_ info: ModelInfo) -> some View {198 let probeTool = ToolDefinition(name: "probe", summary: "Compatibility probe", parameters: ["type": "object"])199 let asked = GenerationParameters(temperature: 0.7, topP: 0.9, maxOutputTokens: nil, reasoning: .high, seed: 1, stop: ["END"])200 let result = ParameterNegotiator.negotiate(asked, tools: [probeTool], images: true, for: info)201 let declared = info.supportedParameters != nil202 func param(_ kept: Bool) -> Verdict { declared ? (kept ? .yes : .no) : .undeclared }203 let rows: [(String, Verdict)] = [204 ("Tools (function calling)", result.tools.isEmpty ? .no : .yes),205 ("Images in the prompt", info.capabilities.contains(.vision) ? .yes : .no),206 ("Temperature", param(result.parameters.temperature != nil)),207 ("Top-p", param(result.parameters.topP != nil)),208 ("Seed", param(result.parameters.seed != nil)),209 ("Stop sequences", param(!result.parameters.stop.isEmpty)),210 ("Reasoning effort", result.parameters.reasoning != nil ? .yes : .no),211 ("Structured output", info.capabilities.contains(.structuredOutput) ? .yes : .no),212 ("Prompt caching", info.capabilities.contains(.promptCaching) ? .yes : .no),213 ]214 return VStack(alignment: .leading, spacing: AstraSpace.md) {215 SectionTitle("Compatibility check", detail: "What a request may contain")216 AstraCard {217 VStack(alignment: .leading, spacing: 0) {218 ForEach(rows, id: \.0) { row in219 HStack(spacing: AstraSpace.md) {220 Text(row.0).font(AstraFont.subheadline).foregroundStyle(AstraColor.textPrimary)221 Spacer(minLength: AstraSpace.sm)222 switch row.1 {223 case .yes: Label("Accepted", systemImage: "checkmark.circle.fill").font(AstraFont.caption).foregroundStyle(AstraColor.success)224 case .no: Label("Dropped", systemImage: "xmark.circle.fill").font(AstraFont.caption).foregroundStyle(AstraColor.danger)225 case .undeclared: Label("Sent as is", systemImage: "questionmark.circle").font(AstraFont.caption).foregroundStyle(AstraColor.warning)226 }227 }228 .frame(minHeight: 36)229 .accessibilityElement(children: .combine)230 }231 Text(declared232 ? "Computed by the negotiation that prepares every request: anything marked Dropped is removed before sending and you are told in the chat."233 : "The provider publishes no parameter list for this model. Parameters marked “Sent as is” are sent when you set them; if the provider rejects one, its error is shown.")234 .font(AstraFont.caption).foregroundStyle(AstraColor.textTertiary).fixedSize(horizontal: false, vertical: true).padding(.top, AstraSpace.sm)235 }236 }237 }238 .accessibilityIdentifier("explorer-compatibility")239 }240241 private func parameters(_ info: ModelInfo) -> some View {242 VStack(alignment: .leading, spacing: AstraSpace.md) {243 SectionTitle("Supported parameters", detail: info.supportedParameters.map { "\($0.count)" })244 AstraCard {245 if let supported = info.supportedParameters {246 if supported.isEmpty { Text("The provider lists none.").font(AstraFont.subheadline).foregroundStyle(AstraColor.textSecondary) }247 else {248 VStack(alignment: .leading, spacing: AstraSpace.sm) {249 FlowLayout(spacing: AstraSpace.xs) { ForEach(supported.sorted(), id: \.self) { AstraBadge($0) } }250 Text("As listed by the provider. Source: \(info.source.label).").font(AstraFont.micro).foregroundStyle(AstraColor.textTertiary)251 }252 }253 } else {254 Text("Not published for this model.").font(AstraFont.subheadline).foregroundStyle(AstraColor.textSecondary)255 }256 }257 }258 }259260 private func price(_ value: Double?) -> String? { value.map { $0 == 0 ? "$0" : ($0 >= 1 ? String(format: "$%.2f", $0) : String(format: "$%.4g", $0)) + " per 1M tokens" } }261262 private func pricing(_ info: ModelInfo) -> some View {263 VStack(alignment: .leading, spacing: AstraSpace.md) {264 SectionTitle("Pricing", detail: info.pricing.map { "As of \($0.asOf)" })265 AstraCard {266 if info.ref.provider.isLocal {267 Text("Runs on this iPhone. There is no per-token charge; it uses battery and memory instead.").font(AstraFont.subheadline).foregroundStyle(AstraColor.textSecondary)268 .fixedSize(horizontal: false, vertical: true)269 } else if let p = info.pricing {270 VStack(alignment: .leading, spacing: 0) {271 FactRow(label: "Input", value: price(p.inputPerMTok))272 FactRow(label: "Output", value: price(p.outputPerMTok))273 if p.cachedInputPerMTok != nil { FactRow(label: "Cached input", value: price(p.cachedInputPerMTok)) }274 if p.cacheWritePerMTok != nil { FactRow(label: "Cache write", value: price(p.cacheWritePerMTok)) }275 FactRow(label: "Price date", value: p.asOf)276 if let text = p.sourceURL, let url = URL(string: text) {277 Link(destination: url) { Label("Price source", systemImage: "arrow.up.right.square").font(AstraFont.footnote.weight(.semibold)) }278 .foregroundStyle(AstraColor.violet).frame(minHeight: AstraSpace.hit)279 }280 Text("US dollars. Used only to estimate cost from the tokens a provider reports; the provider's invoice is the reference.")281 .font(AstraFont.caption).foregroundStyle(AstraColor.textTertiary).fixedSize(horizontal: false, vertical: true)282 }283 } else {284 Text("No published price is known for this model, so its cost is shown as unavailable rather than guessed.")285 .font(AstraFont.subheadline).foregroundStyle(AstraColor.textSecondary).fixedSize(horizontal: false, vertical: true)286 }287 }288 }289 }290291 private func local(_ card: LocalModelCard) -> some View {292 let verdict = app.local.verdict(card)293 let installed = app.local.installed.first { $0.id == card.id }294 return VStack(alignment: .leading, spacing: AstraSpace.md) {295 SectionTitle("On-device details")296 AstraCard {297 VStack(alignment: .leading, spacing: 0) {298 FactRow(label: "Publisher", value: card.publisher, source: "Model card")299 FactRow(label: "Repository", value: card.repo, source: "Model card", mono: true)300 FactRow(label: "Revision", value: String(card.revision.prefix(12)), source: "Commit the file hashes were taken from", mono: true)301 FactRow(label: "Architecture", value: card.architecture, source: "Model card")302 FactRow(label: "Quantization", value: card.quantization, source: "Model card")303 FactRow(label: "Parameters", value: card.parameterCount.map { LabFormat.compact(Int($0)) }, source: card.parameterCount == nil ? nil : "Model card")304 FactRow(label: "License", value: card.licenseID, source: "Model card")305 FactRow(label: "Size on disk", value: installed.map { LabFormat.fileBytes($0.bytesOnDisk) }, source: installed == nil ? "Not installed" : "Measured on device")306 if let v = card.verified {307 FactRow(label: "Verified speed", value: String(format: "%.1f tok/s", v.tokensPerSecond), source: "Measured on \(v.machine), \(v.platform) \(v.osVersion), \(v.date). Not this iPhone.")308 }309 Text(verdict.explanation).font(AstraFont.caption).foregroundStyle(AstraColor.textSecondary).fixedSize(horizontal: false, vertical: true).padding(.top, AstraSpace.sm)310 }311 }312 }313 }314}315316// MARK: - Device317318/// The device as the system reports it right now: memory, heat, storage. Readings refresh while the page is open.319struct DeviceLabView: View {320 @Environment(AppModel.self) private var app321 @State private var reading = MemoryReading.current()322 @State private var mlx: MLXMemoryFigures?323 @State private var thermal = ThermalLevel.current()324 @State private var lowPower = ProcessInfo.processInfo.isLowPowerModeEnabled325 @State private var storage: StorageInfo?326 @State private var isCompacting = false327 @State private var isUnloading = false328329 struct StorageInfo: Equatable { var database: Int64; var attachments: Int64; var attachmentFiles: Int }330331 var body: some View {332 let device = app.local.device333 LabPage(title: "Device") {334 VStack(alignment: .leading, spacing: AstraSpace.md) {335 SectionTitle("This device", detail: "Read from the system")336 AstraCard {337 VStack(alignment: .leading, spacing: 0) {338 FactRow(label: "Model", value: device.modelIdentifier, mono: true)339 FactRow(label: "System", value: (device.platform == .simulator ? "iOS Simulator " : "iOS ") + device.osVersion)340 FactRow(label: "Physical memory", value: LabFormat.bytes(device.physicalMemoryBytes))341 FactRow(label: "Low Power Mode", value: lowPower ? "On" : "Off")342 }343 }344 }345 VStack(alignment: .leading, spacing: AstraSpace.md) {346 SectionTitle("Memory", detail: "Live")347 AstraCard {348 VStack(alignment: .leading, spacing: AstraSpace.md) {349 LazyVGrid(columns: [GridItem(.adaptive(minimum: 120), spacing: AstraSpace.md, alignment: .topLeading)], alignment: .leading, spacing: AstraSpace.md) {350 MetricTile("Available to Astra", value: LabFormat.bytes(reading.availableBytes), symbol: "memorychip")351 MetricTile("Used by Astra", value: LabFormat.bytes(reading.footprintBytes), symbol: "square.stack.3d.down.right")352 MetricTile("MLX active", value: LabFormat.bytes(mlx?.activeBytes), symbol: "cpu")353 MetricTile("MLX cache", value: LabFormat.bytes(mlx?.cacheBytes), symbol: "tray.full")354 MetricTile("MLX peak", value: LabFormat.bytes(mlx?.peakBytes), symbol: "arrow.up.to.line")355 }356 if reading.availableBytes == nil {357 Text("This system does not report how much memory an app may still use, so that figure is unavailable here.")358 .font(AstraFont.caption).foregroundStyle(AstraColor.textSecondary).fixedSize(horizontal: false, vertical: true)359 }360 if mlx == nil, let reason = app.local.mlxUnavailableReason {361 Text(reason).font(AstraFont.caption).foregroundStyle(AstraColor.textSecondary).fixedSize(horizontal: false, vertical: true)362 }363 if let loaded = app.local.snapshot?.loaded, !loaded.isEmpty {364 LabDivider()365 ForEach(loaded, id: \.modelID) { item in366 FactRow(label: "In memory", value: app.local.card(item.modelID)?.displayName ?? item.modelID,367 source: LabFormat.bytes(item.weightsMemoryBytes).map { "Weights: \($0), as MLX reports" })368 }369 Button { unload() } label: {370 HStack(spacing: AstraSpace.sm) { if isUnloading { ProgressView() }; Text("Unload from memory") }371 }372 .buttonStyle(AstraSecondaryButtonStyle()).disabled(isUnloading).accessibilityIdentifier("device-unload")373 }374 }375 }376 }377 VStack(alignment: .leading, spacing: AstraSpace.md) {378 SectionTitle("Thermal state", detail: "Live")379 AstraCard {380 VStack(alignment: .leading, spacing: AstraSpace.sm) {381 HStack(spacing: AstraSpace.sm) {382 AstraBadge(thermal.rawValue.capitalized, symbol: thermal >= .serious ? "thermometer.high" : "thermometer.medium",383 tone: thermal == .nominal ? .success : thermal == .fair ? .neutral : thermal == .serious ? .warning : .danger)384 Spacer(minLength: 0)385 }386 .accessibilityElement(children: .ignore).accessibilityLabel("Thermal state").accessibilityValue(thermal.rawValue)387 Text(thermalText).font(AstraFont.footnote).foregroundStyle(AstraColor.textSecondary).fixedSize(horizontal: false, vertical: true)388 }389 }390 .accessibilityIdentifier("device-thermal")391 }392 VStack(alignment: .leading, spacing: AstraSpace.md) {393 SectionTitle("Storage used by Astra")394 AstraCard {395 VStack(alignment: .leading, spacing: 0) {396 FactRow(label: "Downloaded models", value: LabFormat.fileBytes(app.local.totalBytes), source: "\(app.local.installed.count) installed")397 FactRow(label: "Database", value: storage.map { LabFormat.fileBytes($0.database) }, source: "Conversations, usage, settings")398 FactRow(label: "Attachments", value: storage.map { LabFormat.fileBytes($0.attachments) }, source: storage.map { "\($0.attachmentFiles) file\($0.attachmentFiles == 1 ? "" : "s")" })399 FactRow(label: "Free on this iPhone", value: app.local.freeBytes.map { LabFormat.fileBytes($0) })400 Button { compact() } label: {401 HStack(spacing: AstraSpace.sm) { if isCompacting { ProgressView() }; Text("Compact the database") }402 }403 .buttonStyle(AstraSecondaryButtonStyle()).disabled(isCompacting).padding(.top, AstraSpace.sm).accessibilityIdentifier("device-compact")404 }405 }406 }407 NoticeCard(.info, title: "Battery and heat",408 message: "Running a model on device uses the processor and graphics chip intensively, so the iPhone warms up and the battery drains faster than during ordinary use. How much depends on the model, the length of the answer, the ambient temperature and the age of the battery; Astra cannot predict it. When the system reports a serious thermal state, Astra shortens answers, and at critical it unloads the model.")409 }410 .task {411 await refreshStorage()412 await app.local.refreshInstalled()413 while !Task.isCancelled {414 reading = MemoryReading.current(); mlx = MLXMemoryFigures.read(); lowPower = ProcessInfo.processInfo.isLowPowerModeEnabled415 try? await Task.sleep(nanoseconds: 2_000_000_000)416 }417 }418 .task { for await level in ThermalGovernor.levels() { thermal = level } }419 }420421 private var thermalText: String {422 if let warning = ThermalGovernor().decision(for: thermal).warning { return warning }423 return thermal == .nominal ? "The device is at its normal temperature. Nothing is limited." : "The device is slightly warm. Nothing is limited yet."424 }425426 private func refreshStorage() async {427 if let usage = try? await app.store.storageUsage() {428 storage = StorageInfo(database: usage.databaseBytes, attachments: usage.attachmentBytes, attachmentFiles: usage.attachmentFileCount)429 }430 }431432 private func compact() {433 isCompacting = true434 let store = app.store435 Task {436 defer { isCompacting = false }437 do {438 let freed = try await store.compact()439 await refreshStorage()440 app.show(freed > 0 ? "\(LabFormat.fileBytes(freed)) returned to the system" : "The database was already compact", .success)441 } catch { app.show(AstraError.wrap(error).message, .error) }442 }443 }444445 private func unload() {446 isUnloading = true447 Task {448 await app.local.unloadAll()449 isUnloading = false450 app.show("Models unloaded from memory", .success)451 }452 }453}454