SPB Git

Repository Public MIT

LabScreen.swift 10.2 KB 239 lines swift

1// =============================================================================2//                  SP BOUCHER | SOFTWARE & RESEARCH3//             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// PROJECT METADATA7// -----------------------------------------------------------------------------8// Project          : ORIX Astra9// File             : Packages/AstraUI/Sources/Lab/LabScreen.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 AstraPersistence9293enum LabSection: String, CaseIterable, Identifiable, Hashable {94    case prompts, usage, benchmarks, diagnostics, explorer, device, memory, settings95    var id: String { rawValue }96    var title: String {97        switch self {98        case .prompts: "Prompt studio"99        case .usage: "Usage and cost"100        case .benchmarks: "Benchmarks"101        case .diagnostics: "Diagnostics"102        case .explorer: "Model explorer"103        case .device: "Device"104        case .memory: "Memory"105        case .settings: "Settings and privacy"106        }107    }108    var detail: String {109        switch self {110        case .prompts: "Templates with variables, version history, presets"111        case .usage: "Tokens and cost per day, provider and model"112        case .benchmarks: "On-device speed, cold and warm; cloud latency"113        case .diagnostics: "Sanitized log of provider requests"114        case .explorer: "Every known fact about a model, with its source"115        case .device: "Memory, thermal state and storage, live"116        case .memory: "What you asked Astra to remember"117        case .settings: "Appearance, app lock, defaults, your data"118        }119    }120    var symbol: String {121        switch self {122        case .prompts: "text.badge.star"123        case .usage: "chart.bar.xaxis"124        case .benchmarks: "speedometer"125        case .diagnostics: "stethoscope"126        case .explorer: "magnifyingglass"127        case .device: "iphone"128        case .memory: "brain.head.profile"129        case .settings: "gearshape"130        }131    }132}133134enum LabRoute: Hashable { case section(LabSection), template(String), about, data }135136/// Astra Lab: the workshop behind the chat. Every figure shown here was measured or reported; nothing is illustrative.137struct LabScreen: View {138    @Environment(AppModel.self) private var app139    @State private var path: [LabRoute] = []140    @State private var total: UsageAggregate?141    @State private var applied = false142143    private let groups: [(title: String, sections: [LabSection])] = [144        ("Build", [.prompts]),145        ("Measure", [.usage, .benchmarks, .diagnostics]),146        ("Understand", [.explorer, .device]),147        ("Yours", [.memory, .settings]),148    ]149150    var body: some View {151        NavigationStack(path: $path) {152            ScrollView {153                VStack(alignment: .leading, spacing: AstraSpace.xl) {154                    summary155                    ForEach(groups, id: \.title) { group in156                        VStack(alignment: .leading, spacing: AstraSpace.md) {157                            SectionTitle(group.title)158                            AstraCard(padding: 0) {159                                VStack(spacing: 0) {160                                    ForEach(Array(group.sections.enumerated()), id: \.element) { index, section in161                                        if index > 0 { LabDivider().padding(.leading, AstraSpace.lg + 28 + AstraSpace.md) }162                                        NavigationLink(value: LabRoute.section(section)) {163                                            LabActionRow(title: section.title, detail: section.detail, symbol: section.symbol)164                                                .padding(.horizontal, AstraSpace.lg).padding(.vertical, AstraSpace.sm)165                                        }166                                        .buttonStyle(.plain)167                                        .accessibilityIdentifier("lab-\(section.rawValue)")168                                    }169                                }170                            }171                        }172                    }173                }174                .padding(.horizontal, AstraSpace.page).padding(.vertical, AstraSpace.lg)175            }176            .background(Starfield(density: 0.5))177            .navigationTitle("Lab")178            .navigationDestination(for: LabRoute.self) { route in179                switch route {180                case .section(let section): destination(section)181                case .template(let id): TemplateEditorView(templateID: id)182                case .about: AboutView()183                case .data: DataControlsView()184                }185            }186            .task {187                applyLaunchScreen()188                for await _ in app.store.changes(in: [.usage]) { total = try? await app.store.usageTotal(from: Calendar.current.date(byAdding: .day, value: -30, to: Date())) }189            }190        }191    }192193    @ViewBuilder private func destination(_ section: LabSection) -> some View {194        switch section {195        case .prompts: PromptStudioView(onOpen: { path.append(.template($0)) })196        case .usage: UsageLabView()197        case .benchmarks: BenchmarksLabView()198        case .diagnostics: DiagnosticsLabView()199        case .explorer: ModelExplorerView()200        case .device: DeviceLabView()201        case .memory: MemoryLabView()202        case .settings: SettingsLabView()203        }204    }205206    private var summary: some View {207        AstraCard {208            VStack(alignment: .leading, spacing: AstraSpace.md) {209                SectionTitle("Last 30 days", detail: "From the usage ledger")210                if let total, total.requests > 0 {211                    HStack(alignment: .top, spacing: AstraSpace.md) {212                        MetricTile("Requests", value: LabFormat.count(total.requests), symbol: "arrow.up.arrow.down")213                        MetricTile("Tokens", value: LabFormat.compact(total.totalTokens), symbol: "number")214                        MetricTile("Known cost", value: total.unpricedRequests == total.requests ? nil : CostEstimate.format(total.costUSD), symbol: "dollarsign.circle")215                    }216                } else if total != nil {217                    Text("No request has been recorded yet. Chat, run a comparison or an agent, and the real figures appear here.")218                        .font(AstraFont.subheadline).foregroundStyle(AstraColor.textSecondary).fixedSize(horizontal: false, vertical: true)219                } else {220                    LabLoading().frame(minHeight: AstraSpace.hit)221                }222            }223        }224    }225226    /// DEBUG launch hook: `-ORIXScreen lab-usage` (or any `lab-<section>`, `lab-about`, `lab-data`) opens that page directly.227    private func applyLaunchScreen() {228        guard !applied else { return }229        applied = true230        #if DEBUG231        guard let screen = app.options.screen, screen.hasPrefix("lab-") else { return }232        let name = String(screen.dropFirst(4))233        if let section = LabSection(rawValue: name) { path = [.section(section)] }234        else if name == "about" { path = [.section(.settings), .about] }235        else if name == "data" { path = [.section(.settings), .data] }236        #endif237    }238}239
Files main
Menu

Theme