SPB Git

Repository Public

ModelsScreen.swift 7.6 KB 135 lines swift

1import MCCCore2import SwiftUI34enum ModelsLogic {5    /// What is shown as the name of an inference server: the loaded model, else the kind and port.6    static func title(_ s: API.InferenceServer) -> String { s.model ?? "\(s.kind) :\(s.port)" }78    static func sorted(_ servers: [API.InferenceServer]) -> [API.InferenceServer] {9        servers.sorted {10            let c = $0.nodeAlias.localizedStandardCompare($1.nodeAlias)11            return c == .orderedSame ? $0.port < $1.port : c == .orderedAscending12        }13    }1415    /// The five metric families of `metrics_support`, in display order, with their title keys.16    static func supportRows(_ m: API.InferenceMetricsSupport) -> [(key: String, support: API.MetricSupport)] {17        [("models.metric.requests", m.requests), ("models.metric.tokens_per_s", m.tokensPerS), ("models.metric.ttft", m.ttft),18         ("models.metric.latency", m.latency), ("models.metric.queue", m.queue)]19    }20}2122struct ModelsScreen: View {23    @Environment(\.mccTheme) private var theme24    @Session private var session2526    var body: some View {27        ScreenStack {28            ResourceSection(resource: session.models.list) { servers in29                VStack(alignment: .leading, spacing: Tokens.Space.s3) {30                    if servers.isEmpty {31                        EmptyStateView(tr("models.empty"), message: tr("models.empty.message"), systemImage: "cpu")32                    }33                    ForEach(ModelsLogic.sorted(servers), id: \.id) { s in34                        RouteRow(route: .model(s.id), identifier: "model-row-" + s.nodeAlias + "-\(s.port)") {35                            VStack(alignment: .leading, spacing: Tokens.Space.s2) {36                                AdaptiveLine(spacing: Tokens.Space.s3) {37                                    Text(ModelsLogic.title(s)).font(.headline).foregroundStyle(theme.textPrimary)38                                    StatusBadge(s.health.label, tone: s.health.tone, compact: true)39                                    if s.goneAt != nil { StatusBadge(tr("models.gone"), tone: .neutral, compact: true) }40                                }41                                Text([s.nodeAlias, s.kind, tr("models.port", String(s.port)), s.quantization].compactMap { $0 }.joined(separator: " · "))42                                    .font(.footnote).foregroundStyle(theme.textSecondary)43                                FreshnessLine(s.freshness, observedAt: s.observedAt ?? s.lastSeenAt).font(.footnote)44                            }45                        }46                    }47                }48            }49        }50        .screen("models", title: tr("section.models"), load: { await session.models.list.loadIfNeeded() }, reload: { await session.models.list.load() })51    }52}5354struct ModelDetailScreen: View {55    @Environment(Navigator.self) private var nav56    @Environment(\.mccTheme) private var theme57    @Session private var session58    let serverID: String5960    private var detail: Resource<API.InferenceServer> { session.models.detail[serverID] }6162    var body: some View {63        ScreenStack {64            ResourceSection(resource: detail) { s in65                VStack(alignment: .leading, spacing: Tokens.Space.s5) {66                    Card {67                        VStack(alignment: .leading, spacing: Tokens.Space.s3) {68                            Text(ModelsLogic.title(s)).font(.title2.weight(.semibold)).foregroundStyle(theme.textPrimary).accessibilityAddTraits(.isHeader)69                            AdaptiveLine(spacing: Tokens.Space.s3) {70                                StatusBadge(s.health.label, tone: s.health.tone).accessibilityIdentifier("model-health")71                                if let at = s.healthAt { Text(tr("models.health_at", Format.relative(at))).font(.footnote).foregroundStyle(theme.textTertiary) }72                            }73                            FreshnessLine(s.freshness, observedAt: s.observedAt ?? s.lastSeenAt).font(.callout)74                            if let gone = s.goneAt {75                                InfoBanner(tone: .neutral, title: tr("models.gone"), message: Format.dateTime(gone))76                            }77                            MutationButton(title: tr("models.actions"), systemImage: "bolt", identifier: "model-actions") {78                                nav.sheet = .actionPicker(target: .init(kind: .inferenceServer, id: s.id), targetLabel: ModelsLogic.title(s))79                            }80                        }81                    }82                    SectionCard(tr("models.server")) {83                        InfoRow(tr("models.kind"), s.kind)84                        InfoRow(tr("models.port_label"), String(s.port), monospaced: true)85                        InfoRow(tr("models.model"), s.model)86                        InfoRow(tr("models.quantization"), s.quantization)87                        InfoRow(tr("services.pid"), s.pid.map(String.init), monospaced: true)88                        InfoRow(tr("models.rss"), s.rssBytes.map { Format.bytes($0, binary: true) } ?? tr("metric.not_measured"))89                        InfoRow(tr("models.disk"), s.diskBytes.map { Format.bytes($0) } ?? tr("metric.not_measured"))90                        InfoRow(tr("services.uptime"), s.uptimeS.map { Format.duration(seconds: Double($0)) } ?? tr("metric.not_measured"))91                        InfoRow(tr("models.health_latency"), s.healthLatencyMs.map { Format.milliseconds($0) } ?? tr("metric.not_measured"))92                        InfoRow(tr("models.listed"), s.listedModels.isEmpty ? tr("common.none") : s.listedModels.joined(separator: ", "))93                    }94                    // Throughput, latency, queue: only what the server says is instrumented. No number is invented.95                    SectionCard(tr("models.metrics")) {96                        Text(tr("models.metrics.note")).font(.footnote).foregroundStyle(theme.textSecondary)97                        ForEach(ModelsLogic.supportRows(s.metricsSupport), id: \.key) { row in98                            ModelSupportRow(title: trDynamic(row.key), support: row.support)99                        }100                    }101                    .accessibilityIdentifier("model-metrics-support")102                    RouteRow(route: .node(s.nodeId), identifier: "model-node") {103                        Label(tr("services.node", s.nodeAlias), systemImage: "desktopcomputer").foregroundStyle(theme.textPrimary)104                    }105                }106            }107        }108        .screen("model-detail", title: detail.value.map(ModelsLogic.title) ?? tr("models.model"), load: { await detail.loadIfNeeded() }, reload: { await detail.load() })109        .navigationBarTitleDisplayMode(.inline)110        .onDisappear { detail.cancelLoad() }111    }112}113114/// `MetricSupportRow` of the core, stacked vertically at accessibility text sizes.115struct ModelSupportRow: View {116    @Environment(\.mccTheme) private var theme117    @Environment(\.dynamicTypeSize) private var size118    let title: String119    let support: API.MetricSupport120    var body: some View {121        if size.isAccessibilitySize {122            VStack(alignment: .leading, spacing: Tokens.Space.s1) {123                Text(title).font(.callout).foregroundStyle(theme.textSecondary)124                StatusLabel(support.availability == .available ? tr("metric.instrumented") : tr("metric.not_instrumented", support.availability.label),125                            tone: support.availability.tone)126                if let r = support.reason, !r.isEmpty { Text(r).font(.caption).foregroundStyle(theme.textTertiary) }127            }128            .frame(maxWidth: .infinity, alignment: .leading)129            .accessibilityElement(children: .combine)130        } else {131            MetricSupportRow(title, support: support)132        }133    }134}135
Files main
Menu

Theme