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/LabRunController.swift10// Module : Lab11// Repository : https://git.spboucher.ai/orix-astra12// Created : 2026-11-2113// Last Modified : 2026-11-2214//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 Foundation89import Observation90import SwiftUI91import AstraCore92import AstraDesign93import AstraPersistence9495/// Streams one answer from one model for the prompt studio and the latency probe, and keeps what was measured.96@MainActor @Observable final class LabRunController {97 enum Phase: Equatable { case idle, running, done, stopped, failed }9899 private(set) var phase: Phase = .idle100 private(set) var text = ""101 private(set) var model: ModelRef?102 private(set) var start: MessageStart?103 private(set) var usage: TokenUsage?104 private(set) var timing: GenerationTiming?105 private(set) var stopReason: StopReason?106 private(set) var cost: CostEstimate?107 private(set) var error: AstraError?108 private(set) var notes: [String] = []109 private(set) var startedAt: Date?110 /// Time to response headers of this request, read back from the diagnostics log (cloud models only).111 private(set) var timeToHeaders: TimeInterval?112 private(set) var httpStatus: Int?113 private(set) var isLoadingModel = false114115 private let store: AstraStore116 private let library: ModelLibrary117 private let orchestrator: InferenceOrchestrator118 private var task: Task<Void, Never>?119 private var runID = UUID()120121 init(store: AstraStore, library: ModelLibrary, orchestrator: InferenceOrchestrator) {122 self.store = store; self.library = library; self.orchestrator = orchestrator123 }124125 var isRunning: Bool { phase == .running }126 var tokensPerSecond: Double? { timing?.tokensPerSecond(outputTokens: usage?.outputTokens, reasoningTokens: usage?.reasoningTokens) }127128 func stop() { task?.cancel() }129130 func reset() {131 task?.cancel(); runID = UUID()132 phase = .idle; text = ""; start = nil; usage = nil; timing = nil; stopReason = nil; cost = nil; error = nil; notes = []; timeToHeaders = nil; httpStatus = nil133 }134135 /// Sends one prompt. The usage is recorded in the ledger under `origin`.136 func run(model: ModelRef, system: String, prompt: String, parameters: GenerationParameters, origin: UsageOrigin) {137 reset()138 let id = runID139 self.model = model140 phase = .running; startedAt = Date()141 if let unavailable = library.availability(of: model) { error = unavailable; phase = .failed; return }142 let info = library.info(for: model)143 var negotiated = parameters144 if let info {145 let result = ParameterNegotiator.negotiate(parameters, tools: [], images: false, for: info)146 negotiated = result.parameters; notes = result.notes147 }148 let cleanSystem = system.trimmingCharacters(in: .whitespacesAndNewlines)149 let messages = [PromptMessage.user(prompt)]150 let window = info?.contextLength ?? (model.provider.isLocal ? 4_096 : 32_000)151 let reserve = min(negotiated.maxOutputTokens ?? info?.maxOutputTokens ?? 1_024, max(256, window / 2))152 let plan = ContextBudget(window: window, reservedForOutput: reserve, strategy: .strict).plan(system: cleanSystem.isEmpty ? nil : cleanSystem, messages: messages)153 guard plan.fits else {154 error = .contextTooLong("The prompt is estimated at \(plan.estimatedInputTokens) tokens, more than the \(plan.inputBudget) this model can take once the answer is reserved.")155 phase = .failed; return156 }157 library.noteUsed(model)158 isLoadingModel = model.provider == .mlx159 let request = GenerationRequest(model: model, system: cleanSystem.isEmpty ? nil : cleanSystem, messages: messages, parameters: negotiated)160 let orchestrator = orchestrator, store = store161 let began = Date()162 task = Task { [weak self] in163 var outcome = GenerationOutcome()164 var lastPublish = Date.distantPast165 for await event in orchestrator.stream(request) {166 outcome.apply(event)167 switch event {168 case .messageStarted(let s): if self?.runID == id { self?.start = s }169 case .textDelta, .reasoningSummaryDelta:170 let now = Date()171 if now.timeIntervalSince(lastPublish) > 0.04, self?.runID == id {172 lastPublish = now173 self?.text = outcome.text174 self?.isLoadingModel = false175 self?.timing = outcome.timing176 }177 default: break178 }179 }180 if outcome.error == nil, outcome.stopReason == .unknown, Task.isCancelled { outcome.apply(.error(.cancelled)) }181 let cost = outcome.usage == nil ? nil : UsageAccounting.cost(usage: outcome.usage, pricing: info?.pricing, provider: model.provider)182 if outcome.usage != nil {183 try? await store.recordUsage(UsageRecord(model: model, usage: outcome.usage, cost: cost, origin: origin))184 }185 // The transport logs one sanitized record per HTTP response; the newest one for this model since the start is ours.186 var headers: TimeInterval?, status: Int?187 if !model.provider.isLocal, let recent = try? await store.diagnostics(limit: 20),188 let mine = recent.first(where: { $0.provider == model.provider && $0.modelID == model.modelID && $0.startedAt >= began.addingTimeInterval(-0.5) }) {189 headers = mine.timeToHeaders; status = mine.httpStatus190 }191 self?.complete(id, outcome, cost: cost, headers: headers, status: status)192 }193 }194195 private func complete(_ id: UUID, _ outcome: GenerationOutcome, cost: CostEstimate?, headers: TimeInterval?, status: Int?) {196 guard runID == id else { return }197 text = outcome.text; start = outcome.start; usage = outcome.usage; timing = outcome.timing; stopReason = outcome.stopReason198 self.cost = cost; timeToHeaders = headers; httpStatus = status; isLoadingModel = false199 if let e = outcome.error {200 if e == .cancelled { phase = .stopped; error = nil } else { phase = .failed; error = e }201 } else { phase = .done }202 task = nil203 }204}205206/// The result of a `LabRunController`: live text, state, error and the measured figures.207struct LabRunResultView: View {208 @Environment(AppModel.self) private var app209 let run: LabRunController210 var showText = true211 var showHeaders = false212 @ScaledMetric(relativeTo: .body) private var fontSize: CGFloat = 16213214 var body: some View {215 VStack(alignment: .leading, spacing: AstraSpace.md) {216 if !run.notes.isEmpty {217 NoticeCard(.warning, title: "Incompatible parameters were dropped", message: run.notes.joined(separator: " "))218 }219 if showText {220 if !run.text.isEmpty {221 MarkdownView(text: run.text).foregroundStyle(AstraColor.textPrimary).textSelection(.enabled)222 .environment(\.transcriptFontSize, fontSize)223 } else if run.isRunning {224 HStack(spacing: AstraSpace.sm) {225 OrbitActivity(size: 22)226 Text(run.isLoadingModel ? "Loading the model into memory…" : "Waiting for the first token…").font(AstraFont.footnote).foregroundStyle(AstraColor.textSecondary)227 }228 } else if run.phase == .done || run.phase == .stopped {229 Text(run.phase == .stopped ? "Stopped before any text arrived." : "The model returned no text.").font(AstraFont.footnote).foregroundStyle(AstraColor.textSecondary)230 }231 } else if run.isRunning {232 HStack(spacing: AstraSpace.sm) { OrbitActivity(size: 22); Text("Measuring…").font(AstraFont.footnote).foregroundStyle(AstraColor.textSecondary) }233 }234 if let error = run.error {235 let needsKey: Bool = { if case .missingCredential = error { return true }; if case .unauthorized = error { return true }; return false }()236 NoticeCard(.error, title: error.title, message: error.message, actionTitle: needsKey ? "Open Models" : nil, action: needsKey ? { app.tab = .models } : nil)237 }238 if run.isRunning || run.timing != nil || run.usage != nil { metrics }239 }240 .frame(maxWidth: .infinity, alignment: .leading)241 }242243 private var metrics: some View {244 VStack(alignment: .leading, spacing: AstraSpace.md) {245 LabDivider()246 let columns = [GridItem(.adaptive(minimum: 96), spacing: AstraSpace.md, alignment: .topLeading)]247 LazyVGrid(columns: columns, alignment: .leading, spacing: AstraSpace.md) {248 if showHeaders { MetricTile("Headers", value: LabFormat.seconds(run.timeToHeaders), unit: "s", symbol: "arrow.down.left.and.arrow.up.right") }249 MetricTile("First token", value: LabFormat.seconds(run.timing?.timeToFirstToken), unit: "s", symbol: "bolt")250 MetricTile("Total", value: LabFormat.seconds(run.timing?.totalTime), unit: "s", symbol: "clock")251 MetricTile("Speed", value: LabFormat.rate(run.tokensPerSecond), unit: "tok/s", symbol: "speedometer")252 MetricTile("Input", value: LabFormat.count(run.usage?.inputTokens), unit: "tok", symbol: "arrow.up")253 MetricTile("Output", value: LabFormat.count(run.usage?.outputTokens), unit: "tok", symbol: "arrow.down")254 MetricTile("Cost", value: run.cost?.label, symbol: "dollarsign.circle")255 }256 if !run.isRunning {257 VStack(alignment: .leading, spacing: 0) {258 FactRow(label: "Model reported by the provider", value: run.start?.reportedModel, mono: true)259 if run.model?.provider == .openRouter || run.start?.servedBy != nil { FactRow(label: "Served by", value: run.start?.servedBy) }260 FactRow(label: "Stop reason", value: run.stopReason?.label)261 FactRow(label: "Cost basis", value: run.cost.map { LabFormat.basis($0.basis) }, source: run.cost?.pricingAsOf.map { "Prices as of \($0)" })262 if showHeaders { FactRow(label: "HTTP status", value: run.httpStatus.map(String.init)) }263 }264 }265 }266 }267}268