SPB Git

Repository Public

Status.swift 16.5 KB 409 lines swift

1// =============================================================================2//                  SP BOUCHER | SOFTWARE & RESEARCH3//             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// PROJECT METADATA7// -----------------------------------------------------------------------------8// Project          : Claude Remote / Codex Remote9// File             : Packages/ClaudeRemoteKit/Sources/ClaudeRemoteDesign/Status.swift10// Module           : ClaudeRemoteDesign11// Repository       : https://git.spboucher.ai/claude-remote-apple12// Project Website  : https://claude.spboucher.ai13// Created          : 2026-11-2914// Last Modified    : 2026-11-2915//16// =============================================================================17// AUTHOR & PROFESSIONAL AFFILIATION18// =============================================================================19//20// Author           : Simon-Pierre Boucher21// Position         : Professor of Finance22// Institution      : Université du Québec en Outaouais (UQO)23// Department       : Département des sciences administratives24// Organization     : SP Boucher25//26// Website          : https://spboucher.ai27// Git              : https://git.spboucher.ai28//29// =============================================================================30// OFFICIAL CONTACT DIRECTORY31// =============================================================================32//33// General          : info@spboucher.ai34// Professional     : contact@spboucher.ai35// Direct           : simon@spboucher.ai36// Research         : research@spboucher.ai37// Development      : dev@spboucher.ai38// Support          : support@spboucher.ai39// Security/Admin   : admin@spboucher.ai40// Notifications    : notifications@spboucher.ai41//42// Preferred Contact: support@spboucher.ai43//44// =============================================================================45// COPYRIGHT & INTELLECTUAL PROPERTY46// =============================================================================47//48// Copyright (c) 202649// Simon-Pierre Boucher50//51// All rights reserved where applicable.52//53// This file is governed by the applicable project license,54// copyright notices, institutional agreements and relevant law.55//56// Unless expressly authorized by the applicable license or57// rights holder, copying, redistribution, sublicensing,58// publication and commercial exploitation are not permitted.59//60// Existing third-party rights and notices remain unaffected.61//62// =============================================================================63// CONFIDENTIALITY & SECURITY64// =============================================================================65//66// Classification   : Public67// Distribution     : Public repository; use is governed by the project license68// Security Contact : admin@spboucher.ai69//70// Access to this source does not independently grant permission71// to redistribute or disclose confidential materials.72//73// Do not embed credentials, private keys, access tokens,74// certificates or confidential personal information.75//76// =============================================================================77// LICENSE & MAINTENANCE78// =============================================================================79//80// License          : None specified (no license file in this repository)81// Maintainer       : Simon-Pierre Boucher82// Technical Contact: dev@spboucher.ai83//84// =============================================================================85// END OF SP BOUCHER SOURCE HEADER86// =============================================================================8788import ClaudeRemoteModels89import SwiftUI9091/// Semantic tone of a status; mapped to palette colours.92public enum StatusTone: String, Hashable, Sendable, CaseIterable {93    case neutral, accent, success, warning, error, info9495    /// Text-capable colour token (nil for `.accent`: comes from the theme).96    public var textToken: CRColorToken? {97        switch self {98        case .neutral: CRPalette.muted99        case .accent: nil100        case .success: CRPalette.success101        case .warning: CRPalette.warning102        case .error: CRPalette.error103        case .info: CRPalette.info104        }105    }106107    /// Fill token for dots and bars (nil for `.accent`).108    public var fillToken: CRColorToken? {109        switch self {110        case .neutral: CRPalette.neutralVivid111        case .accent: nil112        case .success: CRPalette.successVivid113        case .warning: CRPalette.warningVivid114        case .error: CRPalette.errorVivid115        case .info: CRPalette.infoVivid116        }117    }118119    /// Text / glyph colour.120    public func color(theme: CRTheme) -> Color { (textToken ?? theme.accent).color }121    /// Fill colour for dots, bars and rings.122    public func fill(theme: CRTheme) -> Color { (fillToken ?? theme.accent).color }123}124125/// How a status is shown: a short label, an SF Symbol and a tone. Pure data, unit-tested.126public struct StatusStyle: Hashable, Sendable {127    public var label: String128    public var symbolName: String129    public var tone: StatusTone130131    public init(label: String, symbolName: String, tone: StatusTone) {132        self.label = label133        self.symbolName = symbolName134        self.tone = tone135    }136137    /// Fallback for a value introduced by a newer server.138    public static func unknown(_ raw: String) -> StatusStyle {139        StatusStyle(label: humanize(raw), symbolName: "questionmark.circle", tone: .neutral)140    }141142    /// "WAITING_FOR_INPUT" → "Waiting for input"; empty → "Unknown".143    public static func humanize(_ raw: String) -> String {144        let words = raw.replacingOccurrences(of: "_", with: " ").replacingOccurrences(of: ".", with: " ")145            .replacingOccurrences(of: "-", with: " ").lowercased().trimmingCharacters(in: .whitespaces)146        guard let first = words.first else { return "Unknown" }147        return first.uppercased() + words.dropFirst()148    }149}150151/// Anything that can be shown as a `StatusPill`.152public protocol StatusPresentable {153    var statusStyle: StatusStyle { get }154}155156extension SessionState: StatusPresentable {157    public var statusStyle: StatusStyle {158        switch self {159        case .created: StatusStyle(label: "Created", symbolName: "circle.dotted", tone: .neutral)160        case .starting: StatusStyle(label: "Starting", symbolName: "arrow.triangle.2.circlepath", tone: .info)161        case .running: StatusStyle(label: "Running", symbolName: "play.circle.fill", tone: .success)162        case .detached: StatusStyle(label: "Detached", symbolName: "pause.circle", tone: .neutral)163        case .waitingForInput: StatusStyle(label: "Needs input", symbolName: "exclamationmark.bubble.fill", tone: .warning)164        case .stopping: StatusStyle(label: "Stopping", symbolName: "stop.circle", tone: .neutral)165        case .stopped: StatusStyle(label: "Stopped", symbolName: "stop.circle.fill", tone: .neutral)166        case .failed: StatusStyle(label: "Failed", symbolName: "xmark.octagon.fill", tone: .error)167        case .recoverable: StatusStyle(label: "Recoverable", symbolName: "arrow.counterclockwise.circle.fill", tone: .warning)168        case .unknown(let raw): .unknown(raw)169        }170    }171}172173extension TaskState: StatusPresentable {174    public var statusStyle: StatusStyle {175        switch self {176        case .queued: StatusStyle(label: "Queued", symbolName: "clock", tone: .neutral)177        case .running: StatusStyle(label: "Running", symbolName: "play.circle.fill", tone: .accent)178        case .succeeded: StatusStyle(label: "Succeeded", symbolName: "checkmark.circle.fill", tone: .success)179        case .failed: StatusStyle(label: "Failed", symbolName: "xmark.octagon.fill", tone: .error)180        case .cancelling: StatusStyle(label: "Cancelling", symbolName: "xmark.circle", tone: .warning)181        case .cancelled: StatusStyle(label: "Cancelled", symbolName: "xmark.circle.fill", tone: .neutral)182        case .timedOut: StatusStyle(label: "Timed out", symbolName: "clock.badge.exclamationmark", tone: .error)183        case .lost: StatusStyle(label: "Lost", symbolName: "questionmark.diamond.fill", tone: .warning)184        case .unknown(let raw): .unknown(raw)185        }186    }187}188189extension Health: StatusPresentable {190    /// Stale / unknown data is neutral, never green.191    public var statusStyle: StatusStyle {192        switch self {193        case .online: StatusStyle(label: "Online", symbolName: "checkmark.circle.fill", tone: .success)194        case .offline: StatusStyle(label: "Offline", symbolName: "xmark.circle.fill", tone: .error)195        case .degraded: StatusStyle(label: "Degraded", symbolName: "exclamationmark.triangle.fill", tone: .warning)196        case .unknown(let raw): .unknown(raw)197        }198    }199}200201extension ClusterAppState: StatusPresentable {202    public var statusStyle: StatusStyle {203        switch self {204        case .up: StatusStyle(label: "Up", symbolName: "checkmark.circle.fill", tone: .success)205        case .degraded: StatusStyle(label: "Degraded", symbolName: "exclamationmark.triangle.fill", tone: .warning)206        case .down: StatusStyle(label: "Down", symbolName: "xmark.circle.fill", tone: .error)207        case .unknown(let raw): .unknown(raw)208        }209    }210}211212extension IncidentSeverity: StatusPresentable {213    public var statusStyle: StatusStyle {214        switch self {215        case .critical: StatusStyle(label: "Critical", symbolName: "exclamationmark.octagon.fill", tone: .error)216        case .warning: StatusStyle(label: "Warning", symbolName: "exclamationmark.triangle.fill", tone: .warning)217        case .info: StatusStyle(label: "Info", symbolName: "info.circle.fill", tone: .info)218        case .unknown(let raw): .unknown(raw)219        }220    }221}222223extension McpStatus: StatusPresentable {224    public var statusStyle: StatusStyle {225        switch self {226        case .connected: StatusStyle(label: "Connected", symbolName: "checkmark.circle.fill", tone: .success)227        case .failed: StatusStyle(label: "Failed", symbolName: "xmark.octagon.fill", tone: .error)228        case .needsAuth: StatusStyle(label: "Needs sign-in", symbolName: "key.fill", tone: .warning)229        case .notChecked: StatusStyle(label: "Not checked", symbolName: "circle.dashed", tone: .neutral)230        case .unknown(let raw): .unknown(raw)231        }232    }233}234235extension NotificationKind: StatusPresentable {236    public var statusStyle: StatusStyle {237        switch self {238        case .sessionWaiting: StatusStyle(label: "Needs input", symbolName: "exclamationmark.bubble.fill", tone: .warning)239        case .sessionExited: StatusStyle(label: "Session ended", symbolName: "terminal", tone: .neutral)240        case .taskSucceeded: StatusStyle(label: "Task succeeded", symbolName: "checkmark.circle.fill", tone: .success)241        case .taskFailed: StatusStyle(label: "Task failed", symbolName: "xmark.octagon.fill", tone: .error)242        case .nodeOffline: StatusStyle(label: "Node offline", symbolName: "wifi.slash", tone: .error)243        case .storageLow: StatusStyle(label: "Storage low", symbolName: "externaldrive.badge.exclamationmark", tone: .warning)244        case .system: StatusStyle(label: "System", symbolName: "gearshape.fill", tone: .info)245        case .unknown(let raw): .unknown(raw)246        }247    }248}249250extension DeviceKind: StatusPresentable {251    public var statusStyle: StatusStyle {252        switch self {253        case .passkey: StatusStyle(label: "Passkey", symbolName: "person.badge.key.fill", tone: .accent)254        case .session: StatusStyle(label: "Browser session", symbolName: "globe", tone: .neutral)255        case .native: StatusStyle(label: "App", symbolName: "laptopcomputer.and.iphone", tone: .neutral)256        case .unknown(let raw): .unknown(raw)257        }258    }259}260261extension SessionKind {262    /// SF Symbol of a session kind (`profile` supplies the agent's symbol).263    public func symbolName(profile: AgentProfile) -> String {264        switch self {265        case .agent: profile.symbolName266        case .shell: "terminal"267        case .unknown: "questionmark.square.dashed"268        }269    }270    /// "Claude", "Codex" or "Shell".271    public func label(profile: AgentProfile) -> String {272        switch self {273        case .agent: profile.shortName274        case .shell: "Shell"275        case .unknown(let raw): StatusStyle.humanize(raw)276        }277    }278}279280/// Capsule showing a status: icon + label, or a dot only when `compact`.281public struct StatusPill: View {282    @Environment(\.crTheme) private var theme283    private let style: StatusStyle284    private let compact: Bool285286    public init(_ style: StatusStyle, compact: Bool = false) {287        self.style = style288        self.compact = compact289    }290    public init(_ status: some StatusPresentable, compact: Bool = false) {291        self.init(status.statusStyle, compact: compact)292    }293294    public var body: some View {295        Group {296            if compact {297                Circle().fill(style.tone.fill(theme: theme)).frame(width: 8, height: 8)298            } else {299                Label {300                    Text(style.label).lineLimit(1)301                } icon: {302                    Image(systemName: style.symbolName)303                }304                .labelStyle(.titleAndIcon)305                .font(.caption.weight(.medium))306                .foregroundStyle(style.tone.color(theme: theme))307                .padding(.horizontal, CRSpacing.sm)308                .padding(.vertical, 3)309                .background(style.tone.fill(theme: theme).opacity(0.14), in: Capsule())310            }311        }312        .accessibilityElement(children: .ignore)313        .accessibilityLabel(Text("Status: \(style.label)"))314    }315}316317/// Small coloured dot for a `Health` value (label exposed to accessibility).318public struct HealthDot: View {319    @Environment(\.crTheme) private var theme320    private let health: Health321    private let size: CGFloat322323    public init(_ health: Health, size: CGFloat = 8) {324        self.health = health325        self.size = size326    }327328    public var body: some View {329        Circle()330            .fill(health.statusStyle.tone.fill(theme: theme))331            .frame(width: size, height: size)332            .accessibilityElement()333            .accessibilityLabel(Text(health.statusStyle.label))334    }335}336337/// Connection state of the live link to the server, with measured latency.338public struct ConnectionIndicator: View {339    /// Design-side mirror of `ConnectionState` (ClaudeRemoteCore); map one to the other in the app.340    public enum Status: Hashable, Sendable {341        case online(latencyMs: Int?)342        case connecting343        case reconnecting(attempt: Int, nextRetry: Date?)344        case offline345        case unauthorized346347        public var label: String {348            switch self {349            case .online: "Online"350            case .connecting: "Connecting"351            case .reconnecting(let attempt, _): attempt > 1 ? "Reconnecting (attempt \(attempt))" : "Reconnecting"352            case .offline: "Offline"353            case .unauthorized: "Signed out"354            }355        }356        public var symbolName: String {357            switch self {358            case .online: "wifi"359            case .connecting: "antenna.radiowaves.left.and.right"360            case .reconnecting: "arrow.triangle.2.circlepath"361            case .offline: "wifi.slash"362            case .unauthorized: "lock.fill"363            }364        }365        public var tone: StatusTone {366            switch self {367            case .online: .success368            case .connecting: .info369            case .reconnecting: .warning370            case .offline: .error371            case .unauthorized: .error372            }373        }374        /// "23 ms" when online and measured.375        public var detail: String? {376            if case .online(let ms?) = self { return CRFormat.latency(ms: Double(ms)) }377            return nil378        }379        public var accessibilityText: String {380            detail.map { "\(label), latency \($0)" } ?? label381        }382    }383384    @Environment(\.crTheme) private var theme385    private let status: Status386    private let showsLabel: Bool387388    public init(_ status: Status, showsLabel: Bool = true) {389        self.status = status390        self.showsLabel = showsLabel391    }392393    public var body: some View {394        HStack(spacing: CRSpacing.xs) {395            Image(systemName: status.symbolName)396                .foregroundStyle(status.tone.color(theme: theme))397            if showsLabel {398                Text(status.label).foregroundStyle(CRPalette.text2.color)399                if let detail = status.detail {400                    Text(detail).monospacedDigit().foregroundStyle(CRPalette.muted.color)401                }402            }403        }404        .font(.caption)405        .accessibilityElement(children: .ignore)406        .accessibilityLabel(Text("Connection: \(status.accessibilityText)"))407    }408}409
Files main
Menu

Theme