SPB Git

Repository Public

ObservatoryScreen.swift 23.7 KB 386 lines swift

1// Author: Simon-Pierre Boucher — contact@spboucher.ai2// File: Packages/LocalChatUI/Sources/Features/Observatory/ObservatoryScreen.swift3// Description: Inference Observatory: live generations, loaded models, memory and device panels, measured history charts, aggregates, comparison report and benchmarks.4import SwiftUI5import Charts6import LocalChatBenchmarks7import LocalChatCore8import LocalChatInference910public struct ObservatoryScreen: View {11    @Environment(AppModel.self) private var app12    @Environment(\.horizontalSizeClass) private var sizeClass13    @State private var model = ObservatoryModel()14    let onOpenModels: (() -> Void)?1516    public init(onOpenModels: (() -> Void)? = nil) { self.onOpenModels = onOpenModels }1718    private var compact: Bool { sizeClass == .compact }1920    public var body: some View {21        ScrollView {22            VStack(alignment: .leading, spacing: DS.S.lg) {23                ScreenTitle("Observatory", subtitle: "What the engine is doing right now, and what it measured before.") {24                    if app.isAnyGenerationRunning { Pill("Generating", symbol: "waveform", tone: .ok) } else { Pill("Idle", symbol: "moon.zzz") }25                }26                if app.allModels.isEmpty {27                    EmptyStateView(symbol: "gauge.with.dots.needle.67percent", title: "Nothing to observe yet",28                                   message: "Install a model or add a remote node; live telemetry and measured history appear here.") {29                        if let onOpenModels { Button("Open Models", action: onOpenModels).buttonStyle(.lcPrimary) }30                    }.frame(minHeight: 300)31                }32                livePanel33                adaptivePair { loadedPanel } second: { memoryPanel }34                adaptivePair { devicePanel } second: { lastCompletedPanel }35                benchmarkPanel36                historyPanel37                aggregatesPanel38            }39            .padding(compact ? DS.S.lg : DS.S.xl)40            .frame(maxWidth: 1400, alignment: .leading).frame(maxWidth: .infinity)41        }42        .screenBackground()43        .onAppear { model.reload(app); app.refreshDevice() }44        .task(id: app.snapshot.loaded.map(\.id)) { await model.refreshCache(app) }45        .onChange(of: app.snapshot.lastCompleted?.id) { _, _ in46            model.reload(app); app.refreshDevice()47            Task { await model.refreshCache(app) }48        }49        .onChange(of: model.deviceFilter) { _, _ in model.reload(app) }50    }5152    @ViewBuilder private func adaptivePair<A: View, B: View>(@ViewBuilder _ first: () -> A, @ViewBuilder second: () -> B) -> some View {53        if compact { first(); second() }54        else { HStack(alignment: .top, spacing: DS.S.lg) { first().frame(maxWidth: .infinity, alignment: .top); second().frame(maxWidth: .infinity, alignment: .top) } }55    }5657    // MARK: Live5859    private var livePanel: some View {60        VStack(alignment: .leading, spacing: DS.S.md) {61            SectionHeader("Live", subtitle: app.snapshot.queued > 0 ? "\(app.snapshot.queued) request\(app.snapshot.queued == 1 ? "" : "s") waiting for the on-device lane." : "Active generations.", symbol: "waveform.path.ecg")62            if !app.snapshot.loading.isEmpty {63                ForEach(app.snapshot.loading.keys.sorted(), id: \.self) { id in64                    let p = app.snapshot.loading[id]65                    VStack(alignment: .leading, spacing: 5) {66                        HStack { Text("Loading \(app.model(id)?.name ?? id)").font(DS.F.callout).foregroundStyle(DS.C.text); Spacer(); Text(p?.stage.rawValue ?? "").font(DS.F.caption).foregroundStyle(DS.C.muted) }67                        if let f = p?.fraction { ProgressBar(f) } else { ProgressView().controlSize(.small) }68                    }69                }70            }71            if app.snapshot.active.isEmpty {72                Text("No generation is running.").font(DS.F.callout).foregroundStyle(DS.C.muted).padding(.vertical, 4)73            } else {74                TimelineView(.periodic(from: .now, by: 0.5)) { timeline in75                    VStack(spacing: DS.S.md) { ForEach(app.snapshot.active) { ActiveGenerationRow(generation: $0, descriptor: app.model($0.modelID), now: timeline.date) } }76                }77            }78        }79        .card()80    }8182    // MARK: Loaded models8384    private var loadedPanel: some View {85        VStack(alignment: .leading, spacing: DS.S.md) {86            SectionHeader("Loaded models", subtitle: "Weights memory and load time are measured by the backend.", symbol: "memorychip")87            if app.snapshot.loaded.isEmpty { Text("No model is in memory.").font(DS.F.callout).foregroundStyle(DS.C.muted) }88            ForEach(app.snapshot.loaded) { loaded in89                VStack(alignment: .leading, spacing: 6) {90                    HStack(spacing: 6) {91                        Text(loaded.descriptor.name).font(DS.F.body.weight(.medium)).foregroundStyle(DS.C.text).lineLimit(1)92                        BackendBadge(loaded.descriptor.backend)93                        if let q = loaded.descriptor.quantization { Pill(q) }94                        Spacer(minLength: 4)95                        Button("Unload") { Task { await app.engine.unload(loaded.id) } }96                            .buttonStyle(.lcSecondaryCompact).disabled(app.snapshot.active.contains { $0.modelID == loaded.id })97                            .accessibilityLabel("Unload \(loaded.descriptor.name)")98                    }99                    Text(loadedDetail(loaded)).font(DS.F.caption).foregroundStyle(DS.C.text2).fixedSize(horizontal: false, vertical: true)100                }101                .padding(10).background(DS.C.surface2, in: RoundedRectangle(cornerRadius: DS.R.control, style: .continuous))102            }103        }104        .card()105    }106107    private func loadedDetail(_ loaded: LoadedModel) -> String {108        var parts = ["Weights \(MetricFormat.memory(loaded.info.weightsMemoryBytes))", "loaded in \(MetricFormat.seconds(loaded.info.loadTime))"]109        if let c = loaded.info.contextLength { parts.append("context \(c.formatted())") }110        parts.append("last used \(loaded.lastUsedAt.formatted(date: .omitted, time: .standard))")111        if let cache = model.cache[loaded.id] {112            parts.append("KV cache \(cache.cachedTokens.formatted()) tokens" + (cache.bytes.map { " (\(ByteFormat.memory($0)))" } ?? ""))113        }114        return parts.joined(separator: " · ")115    }116117    // MARK: Memory118119    private var memoryPanel: some View {120        let s = app.snapshot, d = app.device121        let footprint = s.processFootprintBytes122        return VStack(alignment: .leading, spacing: DS.S.md) {123            SectionHeader("Memory", subtitle: "Process footprint against what this device can give to models.", symbol: "chart.bar.doc.horizontal")124            if let footprint {125                VStack(alignment: .leading, spacing: 5) {126                    ProgressBar(Double(footprint) / Double(max(1, d.usableModelMemoryBytes)), tint: footprint > d.usableModelMemoryBytes ? DS.C.danger : DS.C.accent, height: 8)127                        .accessibilityLabel("Memory used by Local Chat").accessibilityValue("\(ByteFormat.memory(footprint)) of \(ByteFormat.memory(d.usableModelMemoryBytes))")128                    Text("\(ByteFormat.memory(footprint)) used by Local Chat · \(ByteFormat.memory(d.usableModelMemoryBytes)) usable for models (estimate from physical memory)")129                        .font(DS.F.caption).foregroundStyle(DS.C.text2).fixedSize(horizontal: false, vertical: true)130                }131            }132            TileGrid(minimum: 130) {133                MetricTile("Process", value: MetricFormat.memory(footprint), symbol: "app", footnote: "phys_footprint")134                if let v = s.mlxActiveBytes { MetricTile("MLX active", value: ByteFormat.memory(v), symbol: "cpu", tint: DS.C.accent2) }135                if let v = s.mlxCacheBytes { MetricTile("MLX cache", value: ByteFormat.memory(v), symbol: "tray.2", tint: DS.C.accent3) }136                MetricTile("Physical", value: ByteFormat.memory(d.physicalMemoryBytes), symbol: "memorychip", tint: DS.C.muted)137                if let v = d.availableMemoryBytes { MetricTile("Available", value: ByteFormat.memory(v), symbol: "arrow.down.circle", tint: DS.C.ok, footnote: "at last refresh") }138            }139        }140        .card()141    }142143    // MARK: Device144145    private var devicePanel: some View {146        let d = app.device147        return VStack(alignment: .leading, spacing: DS.S.md) {148            HStack {149                SectionHeader("Device", subtitle: "Only what the system reports.", symbol: "desktopcomputer")150                IconButton("arrow.clockwise", label: "Refresh device information") { app.refreshDevice() }151            }152            VStack(spacing: 0) {153                row("Machine", d.machine + (d.isSimulator ? " (simulator)" : ""))154                if let chip = d.chip { row("Chip", chip) }155                if let p = d.performanceCores { row("Cores", "\(p) performance" + (d.efficiencyCores.map { " · \($0) efficiency" } ?? "")) }156                row("Thermal state", d.thermal.rawValue.capitalized, tint: d.thermal == .nominal ? DS.C.ok : d.thermal == .fair ? DS.C.warn : DS.C.danger)157                row("Low Power Mode", d.lowPowerMode ? "On" : "Off", tint: d.lowPowerMode ? DS.C.warn : nil)158                row("System", d.osVersion)159                if let free = d.storageFreeBytes { row("Storage", "\(ByteFormat.string(free)) free" + (d.storageTotalBytes.map { " of \(ByteFormat.string($0))" } ?? "")) }160            }161            if !app.backendIssues.isEmpty {162                ForEach(app.backendIssues.keys.sorted { $0.rawValue < $1.rawValue }, id: \.self) { kind in163                    InlineNotice(.info, "\(kind.displayName): \(app.backendIssues[kind] ?? "")")164                }165            }166        }167        .card()168    }169170    private func row(_ title: String, _ value: String, tint: Color? = nil) -> some View {171        HStack(alignment: .firstTextBaseline) {172            Text(title).font(DS.F.callout).foregroundStyle(DS.C.muted)173            Spacer(minLength: 12)174            Text(value).font(DS.F.callout).foregroundStyle(tint ?? DS.C.text).multilineTextAlignment(.trailing).textSelection(.enabled)175        }176        .padding(.vertical, 5)177        .accessibilityElement(children: .combine)178    }179180    // MARK: Last completed181182    private var lastCompletedPanel: some View {183        VStack(alignment: .leading, spacing: DS.S.md) {184            SectionHeader("Last generation", subtitle: "Final figures reported by the backend.", symbol: "checkmark.seal")185            if let done = app.snapshot.lastCompleted {186                let m = done.metrics187                HStack(spacing: 6) {188                    Text(done.descriptor.name).font(DS.F.body.weight(.medium)).foregroundStyle(DS.C.text).lineLimit(1)189                    BackendBadge(done.descriptor.backend)190                    if let q = done.descriptor.quantization { Pill(q) }191                    Pill(done.purpose)192                    Spacer(minLength: 0)193                    Text(done.finishedAt.formatted(date: .omitted, time: .standard)).font(DS.F.caption).foregroundStyle(DS.C.muted)194                }195                TileGrid(minimum: 120) {196                    MetricTile("Tokens/s", value: MetricFormat.rate(m.tokensPerSecond), symbol: "speedometer")197                    MetricTile("TTFT", value: MetricFormat.seconds(m.timeToFirstToken), symbol: "timer", tint: DS.C.accent3)198                    MetricTile("Prompt tok/s", value: MetricFormat.rate(m.promptTokensPerSecond), symbol: "text.insert", tint: DS.C.accent2)199                    MetricTile("Prompt", value: MetricFormat.count(m.promptTokens), unit: "tok", footnote: m.cachedPromptTokens.map { "\($0.formatted()) from cache" })200                    MetricTile("Output", value: MetricFormat.count(m.completionTokens), unit: "tok", footnote: m.reasoningTokens.map { "\($0.formatted()) reasoning" })201                    MetricTile("Total", value: MetricFormat.seconds(m.totalTime), symbol: "clock", tint: DS.C.muted)202                    MetricTile("Peak memory", value: MetricFormat.memory(m.peakMemoryBytes), symbol: "memorychip", tint: DS.C.warn)203                    MetricTile("Load time", value: MetricFormat.seconds(done.loadTime), symbol: "arrow.down.to.line", tint: DS.C.muted)204                }205                contextBar(used: (m.promptTokens ?? 0) + (m.completionTokens ?? 0), known: m.promptTokens != nil, length: done.contextLength)206                Text("Stopped: \(m.stopReason.rawValue)").font(DS.F.micro).foregroundStyle(DS.C.muted)207            } else {208                Text("Nothing generated since launch.").font(DS.F.callout).foregroundStyle(DS.C.muted)209            }210        }211        .card()212    }213214    @ViewBuilder private func contextBar(used: Int, known: Bool, length: Int) -> some View {215        if known, length > 0 {216            VStack(alignment: .leading, spacing: 4) {217                ProgressBar(Double(used) / Double(length), height: 6).accessibilityLabel("Context utilization").accessibilityValue("\(used) of \(length) tokens")218                Text("Context \(used.formatted()) / \(length.formatted()) tokens").font(DS.F.micro).foregroundStyle(DS.C.muted)219            }220        }221    }222223    // MARK: Benchmark224225    private var benchmarkPanel: some View {226        VStack(alignment: .leading, spacing: DS.S.md) {227            SectionHeader("Benchmark", subtitle: "\(BenchmarkSuite.standard.name) v\(BenchmarkSuite.standard.version): \(BenchmarkSuite.standard.prompts.count) fixed prompts with fixed seeds. Results are stored in the history.", symbol: "stopwatch")228            HStack(spacing: 10) {229                Picker("Model", selection: Binding(get: { model.benchmarkModelID ?? app.defaultModelID ?? "" }, set: { model.benchmarkModelID = $0 })) {230                    ForEach(app.chatModels) { Text($0.name).tag($0.id) }231                }232                .labelsHidden().frame(maxWidth: 320).disabled(model.benchmarkRunning || app.chatModels.isEmpty).accessibilityLabel("Model to benchmark")233                if model.benchmarkRunning {234                    Button { model.cancelBenchmark() } label: { Label("Cancel", systemImage: "stop.fill") }.buttonStyle(.lcDestructive)235                } else {236                    Button { model.startBenchmark(app) } label: { Label("Run benchmark", systemImage: "play.fill") }237                        .buttonStyle(.lcPrimaryCompact).disabled(app.chatModels.isEmpty || app.isAnyGenerationRunning)238                }239                Spacer(minLength: 0)240            }241            if model.benchmarkRunning {242                VStack(alignment: .leading, spacing: 5) {243                    ProgressBar(model.benchmarkTotal > 0 ? Double(model.benchmarkDone) / Double(model.benchmarkTotal) : 0)244                    Text(model.benchmarkStep).font(DS.F.caption).foregroundStyle(DS.C.text2)245                }246            }247            if let s = model.benchmarkSummary {248                TileGrid(minimum: 130) {249                    MetricTile("Median tokens/s", value: MetricFormat.rate(s.medianTokensPerSecond), symbol: "speedometer")250                    MetricTile("Median TTFT", value: MetricFormat.seconds(s.medianTimeToFirstToken), symbol: "timer", tint: DS.C.accent3)251                    MetricTile("Prompt tok/s", value: MetricFormat.rate(s.medianPromptTokensPerSecond), symbol: "text.insert", tint: DS.C.accent2)252                    MetricTile("Peak memory", value: MetricFormat.memory(s.peakMemoryBytes), symbol: "memorychip", tint: DS.C.warn)253                    MetricTile("Duration", value: MetricFormat.duration(s.duration), symbol: "clock", tint: DS.C.muted,254                               footnote: "\(s.records.count) of \(BenchmarkSuite.standard.prompts.count) prompts" + (s.wasCancelled ? " · cancelled" : ""))255                }256            }257            ForEach(model.benchmarkFailures, id: \.self) { InlineNotice(.error, $0) }258        }259        .card()260    }261262    // MARK: History263264    private var historyPanel: some View {265        VStack(alignment: .leading, spacing: DS.S.md) {266            SectionHeader("History", subtitle: "\(model.records.count.formatted()) measured generations (chat, arena, benchmark).", symbol: "chart.xyaxis.line")267            ArenaFlowLayout(spacing: 8) {268                Picker("Series", selection: $model.series) { ForEach(ObservatoryModel.Series.allCases) { Text($0.rawValue).tag($0) } }269                    .pickerStyle(.segmented).labelsHidden().fixedSize()270                Picker("Model", selection: $model.modelFilter) {271                    Text("All models").tag(String?.none)272                    ForEach(model.modelsInHistory, id: \.id) { Text($0.name).tag(String?.some($0.id)) }273                }.labelsHidden().fixedSize().accessibilityLabel("Model filter")274                if model.devices.count > 1 {275                    Picker("Device", selection: $model.deviceFilter) {276                        Text("All devices").tag(String?.none)277                        ForEach(model.devices, id: \.self) { Text($0).tag(String?.some($0)) }278                    }.labelsHidden().fixedSize().accessibilityLabel("Device filter")279                }280            }281            let points = model.points282            if points.isEmpty {283                Text("No measurement for this selection yet. Chat with a model or run a benchmark.").font(DS.F.callout).foregroundStyle(DS.C.muted).padding(.vertical, 20)284            } else {285                Chart(points) { p in286                    LineMark(x: .value("Time", p.date), y: .value(model.series.rawValue, p.value), series: .value("Model", p.model))287                        .foregroundStyle(by: .value("Model", p.model)).interpolationMethod(.monotone).lineStyle(StrokeStyle(lineWidth: 1.6))288                    PointMark(x: .value("Time", p.date), y: .value(model.series.rawValue, p.value))289                        .foregroundStyle(by: .value("Model", p.model)).symbolSize(22)290                }291                .chartForegroundStyleScale(range: DS.C.series)292                .chartYAxisLabel(model.series.unit)293                .chartLegend(position: .bottom, alignment: .leading)294                .frame(height: compact ? 220 : 280)295                .accessibilityLabel("\(model.series.rawValue) over time")296            }297        }298        .card()299    }300301    // MARK: Aggregates and report302303    private var aggregatesPanel: some View {304        VStack(alignment: .leading, spacing: DS.S.md) {305            SectionHeader("Model comparison", subtitle: "Medians over every measured run" + (model.deviceFilter.map { " on \($0)" } ?? " on all devices") + ". Fastest first.", symbol: "tablecells")306            let rows = model.report?.rows ?? model.aggregates307            if rows.isEmpty {308                Text("No aggregate yet.").font(DS.F.callout).foregroundStyle(DS.C.muted)309            } else {310                ScrollView(.horizontal) {311                    Grid(alignment: .trailing, horizontalSpacing: 18, verticalSpacing: 8) {312                        GridRow {313                            Text("Model").gridColumnAlignment(.leading); Text("Backend").gridColumnAlignment(.leading); Text("Runs"); Text("Tok/s median"); Text("p10–p90")314                            Text("TTFT"); Text("Prompt tok/s"); Text("Load"); Text("Peak memory")315                        }.font(DS.F.micro).foregroundStyle(DS.C.muted)316                        Divider().gridCellUnsizedAxes(.horizontal)317                        ForEach(rows) { r in318                            GridRow {319                                Text(r.modelName + (r.quantization.map { " · \($0)" } ?? "")).foregroundStyle(DS.C.text).lineLimit(1)320                                Text(r.backend.displayName).foregroundStyle(DS.C.text2)321                                Text("\(r.runCount)")322                                Text(MetricFormat.rate(r.medianTokensPerSecond)).font(DS.F.rounded(13, weight: .bold)).foregroundStyle(DS.C.accent)323                                Text(r.p10TokensPerSecond != nil && r.p90TokensPerSecond != nil ? "\(MetricFormat.rate(r.p10TokensPerSecond))–\(MetricFormat.rate(r.p90TokensPerSecond))" : "—")324                                Text(MetricFormat.seconds(r.medianTimeToFirstToken))325                                Text(MetricFormat.rate(r.medianPromptTokensPerSecond))326                                Text(MetricFormat.seconds(r.medianLoadTime))327                                Text(MetricFormat.memory(r.peakMemoryBytes))328                            }.font(DS.F.callout).monospacedDigit()329                        }330                    }331                }332                if let report = model.report {333                    ArenaFlowLayout(spacing: 8) {334                        ShareLink(item: report.csv(), preview: SharePreview("Model comparison (CSV)")) { Label("CSV", systemImage: "tablecells") }.buttonStyle(.lcSecondaryCompact)335                        ShareLink(item: report.markdown(), preview: SharePreview("Model comparison (Markdown)")) { Label("Markdown", systemImage: "doc.plaintext") }.buttonStyle(.lcSecondaryCompact)336                        if let data = try? report.json(), let json = String(data: data, encoding: .utf8) {337                            ShareLink(item: json, preview: SharePreview("Model comparison (JSON)")) { Label("JSON", systemImage: "curlybraces") }.buttonStyle(.lcSecondaryCompact)338                        }339                        if let csv = try? app.metrics.exportRecordsCSV(device: model.deviceFilter) {340                            ShareLink(item: csv, preview: SharePreview("All records (CSV)")) { Label("All records", systemImage: "square.and.arrow.up") }.buttonStyle(.lcSecondaryCompact)341                        }342                    }343                }344            }345        }346        .card()347    }348}349350struct ActiveGenerationRow: View {351    let generation: ActiveGeneration352    let descriptor: ModelDescriptor?353    let now: Date354355    var body: some View {356        VStack(alignment: .leading, spacing: 8) {357            HStack(spacing: 6) {358                Text(generation.modelName).font(DS.F.body.weight(.medium)).foregroundStyle(DS.C.text).lineLimit(1)359                BackendBadge(generation.backend)360                if let q = descriptor?.quantization { Pill(q) }361                Pill(generation.purpose)362                Spacer(minLength: 0)363                ThinkingDots()364            }365            TileGrid(minimum: 120) {366                MetricTile("Live rate", value: MetricFormat.rate(generation.liveRate), unit: "ev/s", symbol: "bolt", footnote: "live · stream events per second until final metrics")367                MetricTile("Streamed", value: generation.generatedTokens.formatted(), unit: "events", symbol: "text.word.spacing", tint: DS.C.accent2)368                MetricTile("Elapsed", value: MetricFormat.duration(now.timeIntervalSince(generation.startedAt)), symbol: "clock", tint: DS.C.muted)369                MetricTile("First token", value: MetricFormat.seconds(generation.firstTokenAt.map { $0.timeIntervalSince(generation.startedAt) }), symbol: "timer", tint: DS.C.accent3)370            }371            if generation.contextLength > 0 {372                let used = (generation.promptTokens ?? 0) + generation.generatedTokens373                VStack(alignment: .leading, spacing: 4) {374                    ProgressBar(Double(used) / Double(generation.contextLength), height: 6).accessibilityLabel("Context utilization")375                    Text(generation.promptTokens != nil376                         ? "Context ≈ \(used.formatted()) / \(generation.contextLength.formatted()) tokens"377                         : "Generated ≈ \(generation.generatedTokens.formatted()) of a \(generation.contextLength.formatted())-token window (prompt size is reported when the generation ends)")378                        .font(DS.F.micro).foregroundStyle(DS.C.muted).fixedSize(horizontal: false, vertical: true)379                }380            }381        }382        .padding(12).background(DS.C.surface2, in: RoundedRectangle(cornerRadius: DS.R.control, style: .continuous))383        .accessibilityElement(children: .contain).accessibilityLabel("Generation with \(generation.modelName)")384    }385}386
Files main
Menu

Theme