Repository Public
1// Author: Simon-Pierre Boucher — contact@spboucher.ai2// File: Packages/LocalChatUI/Sources/Features/Arena/ArenaScreen.swift3// Description: Model Arena: the same prompt on 2–4 models, live side by side, measured metrics, blind votes, past runs and leaderboard.4import SwiftUI5import LocalChatBenchmarks6import LocalChatCore78public struct ArenaScreen: View {9 @Environment(AppModel.self) private var app10 @Environment(\.horizontalSizeClass) private var sizeClass11 @State private var model = ArenaModel()12 @State private var showPicker = false13 @State private var showSettings = false14 @State private var tab: Tab = .run15 let onOpenModels: (() -> Void)?1617 enum Tab: String, CaseIterable, Identifiable { case run = "Run", history = "History", leaderboard = "Leaderboard"; var id: String { rawValue } }1819 public init(onOpenModels: (() -> Void)? = nil) { self.onOpenModels = onOpenModels }2021 private var compact: Bool { sizeClass == .compact }2223 public var body: some View {24 Group {25 if app.chatModels.count < 2 && model.pastRuns.isEmpty {26 EmptyStateView(symbol: "square.split.2x1", title: "The Arena needs two models",27 message: "Install at least two chat models (or add a remote node) to compare them on the same prompt.") {28 if let onOpenModels { Button("Open Models", action: onOpenModels).buttonStyle(.lcPrimary) }29 }30 } else {31 ScrollView {32 VStack(alignment: .leading, spacing: DS.S.lg) {33 ScreenTitle("Arena", subtitle: "Same prompt, several models, measured on this device.") {34 Picker("View", selection: $tab) { ForEach(Tab.allCases) { Text($0.rawValue).tag($0) } }35 .pickerStyle(.segmented).labelsHidden().frame(maxWidth: 300)36 }37 switch tab {38 case .run: runTab39 case .history: ArenaHistoryView(model: model) { tab = .run }40 case .leaderboard: ArenaLeaderboardView(rows: model.leaderboard)41 }42 }43 .padding(compact ? DS.S.lg : DS.S.xl)44 .frame(maxWidth: 1500, alignment: .leading).frame(maxWidth: .infinity)45 }46 }47 }48 .screenBackground()49 .onAppear { model.reload(app) }50 .sheet(isPresented: $showPicker) {51 NavigationStack {52 ModelPickerView(selectedID: nil, allowAuto: false) { picked in53 if let picked { model.add(picked) }54 showPicker = false55 }56 .navigationTitle("Add a model")57 .toolbar { ToolbarItem(placement: .cancellationAction) { Button("Cancel") { showPicker = false } } }58 }59 #if os(macOS)60 .frame(minWidth: 460, minHeight: 480)61 #endif62 }63 }6465 // MARK: Run tab6667 @ViewBuilder private var runTab: some View {68 setupCard69 if let error = model.errorText { InlineNotice(.error, error) }70 if !model.contestants.isEmpty {71 statusBar72 ArenaColumnsView(model: model, compact: compact)73 if !model.isRunning, model.run != nil {74 voteCard75 ArenaComparisonTable(model: model)76 exportRow77 }78 }79 }8081 private var setupCard: some View {82 VStack(alignment: .leading, spacing: DS.S.md) {83 SectionHeader("Contestants", subtitle: "Pick 2 to \(ArenaModel.maxModels) models.", symbol: "person.2")84 ArenaFlowLayout(spacing: 8) {85 ForEach(model.modelIDs, id: \.self) { id in86 let m = app.model(id)87 HStack(spacing: 6) {88 Text(m?.name ?? id).font(DS.F.callout).foregroundStyle(DS.C.text).lineLimit(1)89 if let m { BackendBadge(m.backend) }90 Button { model.modelIDs.removeAll { $0 == id } } label: { Image(systemName: "xmark.circle.fill").foregroundStyle(DS.C.muted) }91 .buttonStyle(.plain).accessibilityLabel("Remove \(m?.name ?? id)").disabled(model.isRunning)92 }93 .padding(.horizontal, 10).padding(.vertical, 6)94 .background(DS.C.surface2, in: Capsule()).overlay(Capsule().strokeBorder(DS.C.border, lineWidth: 1))95 }96 if model.modelIDs.count < ArenaModel.maxModels {97 Button { showPicker = true } label: { Label("Add model", systemImage: "plus") }98 .buttonStyle(.lcSecondaryCompact).disabled(model.isRunning)99 }100 }101 VStack(alignment: .leading, spacing: 6) {102 Text("PROMPT").font(DS.F.micro).kerning(0.4).foregroundStyle(DS.C.muted)103 TextField("Ask every model the same thing…", text: $model.prompt, axis: .vertical)104 .textFieldStyle(.plain).font(DS.F.body).lineLimit(3...10)105 .padding(10).background(DS.C.surface2, in: RoundedRectangle(cornerRadius: DS.R.control, style: .continuous))106 .overlay(RoundedRectangle(cornerRadius: DS.R.control, style: .continuous).strokeBorder(DS.C.border, lineWidth: 1))107 .accessibilityLabel("Arena prompt")108 }109 DisclosureGroup(isExpanded: $showSettings) { settingsForm.padding(.top, 8) } label: {110 Text("Options").font(DS.F.callout).foregroundStyle(DS.C.text2)111 }112 HStack(spacing: 10) {113 if model.isRunning {114 Button(role: .cancel) { model.cancel() } label: { Label("Cancel", systemImage: "stop.fill") }.buttonStyle(.lcDestructive)115 ThinkingDots()116 } else {117 Button { model.start(app) } label: { Label("Run arena", systemImage: "play.fill") }118 .buttonStyle(.lcPrimary).disabled(!model.canRun).keyboardShortcut(.return, modifiers: .command)119 }120 Spacer(minLength: 0)121 if model.blind { Pill("Blind", symbol: "eye.slash", tone: .accent) }122 if model.fixedSeed { Pill("Seed \(model.seed)", symbol: "dice", tone: .neutral) }123 }124 }125 .card()126 }127128 private var settingsForm: some View {129 VStack(alignment: .leading, spacing: 6) {130 VStack(alignment: .leading, spacing: 6) {131 Text("SYSTEM PROMPT (OPTIONAL)").font(DS.F.micro).kerning(0.4).foregroundStyle(DS.C.muted)132 TextField("System prompt", text: $model.systemPrompt, axis: .vertical).textFieldStyle(.plain).font(DS.F.callout).lineLimit(1...5)133 .padding(8).background(DS.C.surface2, in: RoundedRectangle(cornerRadius: DS.R.control, style: .continuous))134 }135 SettingRow("Preset", detail: model.preset.summary) {136 Picker("Preset", selection: $model.preset) { ForEach(InferencePreset.allCases) { Text($0.title).tag($0) } }.labelsHidden().fixedSize()137 }138 SettingRow("Max tokens", detail: "Upper bound of each answer.") {139 Stepper("\(model.maxTokens)", value: $model.maxTokens, in: 32...8192, step: 64).monospacedDigit().fixedSize()140 }141 SettingRow("Temperature", detail: String(format: "%.2f", model.temperature)) {142 Slider(value: $model.temperature, in: 0...1.5, step: 0.05).frame(width: 150).accessibilityLabel("Temperature")143 }144 SettingRow("Fixed seed", detail: "Same seed for every model, so a run can be repeated.") { Toggle("Fixed seed", isOn: $model.fixedSeed).labelsHidden() }145 if model.fixedSeed {146 SettingRow("Seed") {147 TextField("Seed", value: $model.seed, format: .number.grouping(.never)).textFieldStyle(.roundedBorder).frame(width: 110).multilineTextAlignment(.trailing)148 #if os(iOS)149 .keyboardType(.numberPad)150 #endif151 }152 }153 SettingRow("Repetitions", detail: "Each model answers this many times.") {154 Stepper("\(model.repetitions)", value: $model.repetitions, in: 1...5).monospacedDigit().fixedSize()155 }156 SettingRow("Execution", detail: "Auto runs models together only when they fit in memory.") {157 Picker("Execution", selection: $model.mode) {158 Text("Auto").tag(ArenaExecutionMode.auto); Text("Sequential").tag(ArenaExecutionMode.sequential); Text("Concurrent").tag(ArenaExecutionMode.concurrent)159 }.labelsHidden().fixedSize()160 }161 SettingRow("Blind evaluation", detail: "Names are hidden until you vote.") { Toggle("Blind", isOn: $model.blind).labelsHidden() }162 }163 .disabled(model.isRunning)164 }165166 private var statusBar: some View {167 HStack(alignment: .firstTextBaseline, spacing: 8) {168 if let concurrent = model.ranConcurrently {169 Pill(concurrent ? "Concurrent" : "Sequential", symbol: concurrent ? "arrow.triangle.branch" : "arrow.right", tone: .accent)170 }171 Text(model.modeReason).font(DS.F.caption).foregroundStyle(DS.C.muted).fixedSize(horizontal: false, vertical: true)172 Spacer(minLength: 0)173 if model.run?.wasCancelled == true { Pill("Cancelled", symbol: "stop.circle", tone: .warn) }174 }175 }176177 private var voteCard: some View {178 VStack(alignment: .leading, spacing: DS.S.md) {179 SectionHeader("Your verdict", subtitle: model.revealed ? voteSummary : "Vote to reveal which model wrote each answer.", symbol: "hand.thumbsup")180 ArenaFlowLayout(spacing: 8) {181 ForEach(model.contestants.filter { model.entry($0).error == nil }) { c in182 Button { model.vote(.winner(c.id), app: app) } label: {183 Label(c.displayName(revealed: model.revealed) + (c.repetition > 1 ? " · run \(c.repetition)" : ""), systemImage: "trophy")184 }.buttonStyle(.lcSecondaryCompact)185 }186 Button { model.vote(.tie, app: app) } label: { Label("Tie", systemImage: "equal") }.buttonStyle(.lcSecondaryCompact)187 Button { model.vote(.bothBad, app: app) } label: { Label("All bad", systemImage: "hand.thumbsdown") }.buttonStyle(.lcSecondaryCompact)188 }189 if model.revealed, model.run?.blind == true, let reveal = model.run?.reveal() {190 Text(reveal.sorted { $0.key < $1.key }.map { "\($0.key) = \($0.value)" }.joined(separator: " · "))191 .font(DS.F.caption).foregroundStyle(DS.C.text2).textSelection(.enabled)192 }193 }194 .card()195 }196197 private var voteSummary: String {198 guard let run = model.run, let last = run.votes.last else { return "Pick the best answer; votes feed the leaderboard." }199 switch last.verdict {200 case .winner(let id): return "You picked \(run.entries.first { $0.contestant.id == id }?.contestant.modelName ?? "an answer")."201 case .tie: return "You called it a tie."202 case .bothBad: return "You judged every answer bad (the leaderboard is unchanged)."203 }204 }205206 @ViewBuilder private var exportRow: some View {207 if let run = model.run {208 HStack(spacing: 10) {209 ShareLink(item: run.exportMarkdown(revealed: model.revealed), preview: SharePreview("Arena run (Markdown)")) { Label("Export Markdown", systemImage: "square.and.arrow.up") }210 .buttonStyle(.lcSecondaryCompact)211 if model.revealed, let data = try? run.exportJSON(), let json = String(data: data, encoding: .utf8) {212 ShareLink(item: json, preview: SharePreview("Arena run (JSON)")) { Label("Export JSON", systemImage: "curlybraces") }.buttonStyle(.lcSecondaryCompact)213 }214 Spacer(minLength: 0)215 }216 }217 }218}219220// MARK: Columns221222struct ArenaColumnsView: View {223 @Environment(AppModel.self) private var app224 let model: ArenaModel225 let compact: Bool226227 var body: some View {228 if compact {229 VStack(spacing: DS.S.md) { ForEach(model.contestants) { column($0) } }230 } else if model.contestants.count <= 3 {231 HStack(alignment: .top, spacing: DS.S.md) { ForEach(model.contestants) { column($0).frame(maxWidth: .infinity) } }232 } else {233 ScrollView(.horizontal) {234 HStack(alignment: .top, spacing: DS.S.md) { ForEach(model.contestants) { column($0).frame(width: 380) } }.padding(.bottom, 6)235 }236 }237 }238239 private func column(_ c: ArenaContestant) -> some View {240 let entry = model.entry(c)241 let loadTime = app.snapshot.loaded.first { $0.id == c.modelID }?.info.loadTime242 return ArenaColumn(contestant: c, entry: entry, revealed: model.revealed, running: model.isRunning, loadTime: model.revealed ? loadTime : nil)243 }244}245246struct ArenaColumn: View {247 let contestant: ArenaContestant248 let entry: ArenaModel.Live249 let revealed: Bool250 let running: Bool251 let loadTime: TimeInterval?252 @State private var showReasoning = false253254 var body: some View {255 VStack(alignment: .leading, spacing: 10) {256 HStack(spacing: 6) {257 Text(contestant.displayName(revealed: revealed)).font(DS.F.headline).foregroundStyle(DS.C.text).lineLimit(1)258 if contestant.repetition > 1 { Pill("run \(contestant.repetition)") }259 Spacer(minLength: 4)260 if revealed { BackendBadge(contestant.backend); if let q = contestant.quantization { Pill(q) } }261 if entry.started && !entry.isDone && running { ThinkingDots() }262 }263 if !entry.reasoning.isEmpty {264 DisclosureGroup(isExpanded: $showReasoning) {265 Text(entry.reasoning).font(DS.F.caption).foregroundStyle(DS.C.text2).textSelection(.enabled).frame(maxWidth: .infinity, alignment: .leading).padding(.top, 4)266 } label: { Label("Reasoning", systemImage: "brain").font(DS.F.caption).foregroundStyle(DS.C.muted) }267 }268 if let error = entry.error { InlineNotice(.error, error) }269 if entry.text.isEmpty && entry.error == nil {270 Text(entry.started ? "Waiting for the first token…" : "Queued").font(DS.F.callout).foregroundStyle(DS.C.muted)271 } else if !entry.text.isEmpty {272 MarkdownView(text: entry.text)273 }274 Divider().overlay(DS.C.border)275 let m = entry.metrics276 LazyVGrid(columns: [GridItem(.adaptive(minimum: 92), spacing: 8)], alignment: .leading, spacing: 8) {277 stat("TTFT", MetricFormat.seconds(m?.timeToFirstToken))278 stat("Tokens/s", MetricFormat.rate(m?.tokensPerSecond))279 stat("Prompt tok/s", MetricFormat.rate(m?.promptTokensPerSecond))280 stat("Tokens out", MetricFormat.count(m?.completionTokens))281 stat("Total", MetricFormat.seconds(m?.totalTime))282 stat("Peak memory", MetricFormat.memory(m?.peakMemoryBytes))283 stat("Load time", MetricFormat.seconds(loadTime))284 }285 }286 .card()287 .accessibilityElement(children: .contain)288 .accessibilityLabel("Answer of \(contestant.displayName(revealed: revealed))")289 }290291 private func stat(_ title: String, _ value: String) -> some View {292 VStack(alignment: .leading, spacing: 2) {293 Text(title.uppercased()).font(.system(size: 9.5, weight: .medium)).kerning(0.3).foregroundStyle(DS.C.muted)294 Text(value).font(DS.F.rounded(13)).monospacedDigit().foregroundStyle(value == "—" ? DS.C.muted : DS.C.text)295 }296 .accessibilityElement(children: .combine)297 }298}299300// MARK: Comparison table301302struct ArenaComparisonTable: View {303 let model: ArenaModel304305 private struct Column { let title: String; let value: (GenerationMetrics) -> Double?; let format: (Double) -> String; let lowerIsBetter: Bool }306 private var columns: [Column] {307 [Column(title: "TTFT", value: { $0.timeToFirstToken }, format: { MetricFormat.seconds($0) }, lowerIsBetter: true),308 Column(title: "Tokens/s", value: { $0.tokensPerSecond }, format: { MetricFormat.rate($0) }, lowerIsBetter: false),309 Column(title: "Prompt tok/s", value: { $0.promptTokensPerSecond }, format: { MetricFormat.rate($0) }, lowerIsBetter: false),310 Column(title: "Tokens out", value: { $0.completionTokens.map(Double.init) }, format: { String(Int($0)) }, lowerIsBetter: false),311 Column(title: "Total", value: { $0.totalTime }, format: { MetricFormat.seconds($0) }, lowerIsBetter: true),312 Column(title: "Peak memory", value: { $0.peakMemoryBytes.map(Double.init) }, format: { ByteFormat.memory(Int64($0)) }, lowerIsBetter: true)]313 }314315 var body: some View {316 VStack(alignment: .leading, spacing: DS.S.md) {317 SectionHeader("Comparison", subtitle: "Best measured value per column is highlighted. “—” means the backend does not measure it.", symbol: "tablecells")318 ScrollView(.horizontal) {319 Grid(alignment: .trailing, horizontalSpacing: 18, verticalSpacing: 8) {320 GridRow {321 Text("Model").gridColumnAlignment(.leading)322 ForEach(columns.indices, id: \.self) { Text(columns[$0].title) }323 }324 .font(DS.F.micro).foregroundStyle(DS.C.muted)325 Divider().gridCellUnsizedAxes(.horizontal)326 ForEach(model.contestants) { c in327 let metrics = model.entry(c).metrics328 GridRow {329 Text(c.displayName(revealed: model.revealed) + (c.repetition > 1 ? " #\(c.repetition)" : "")).font(DS.F.callout).foregroundStyle(DS.C.text).lineLimit(1)330 ForEach(columns.indices, id: \.self) { i in331 let col = columns[i]; let v = metrics.flatMap(col.value)332 let best = v != nil && v == bestValue(col)333 Text(v.map(col.format) ?? "—").font(DS.F.rounded(13, weight: best ? .bold : .regular)).monospacedDigit()334 .foregroundStyle(best ? DS.C.ok : (v == nil ? DS.C.muted : DS.C.text))335 }336 }337 }338 }339 }340 }341 .card()342 }343344 private func bestValue(_ col: Column) -> Double? {345 let values = model.contestants.compactMap { model.entry($0).metrics.flatMap(col.value) }346 guard values.count > 1 else { return nil }347 // "tokens out" has no better / worse: do not highlight it348 if col.title == "Tokens out" { return nil }349 return col.lowerIsBetter ? values.min() : values.max()350 }351}352353// MARK: History and leaderboard354355struct ArenaHistoryView: View {356 @Environment(AppModel.self) private var app357 let model: ArenaModel358 let onOpen: () -> Void359360 var body: some View {361 if model.pastRuns.isEmpty {362 EmptyStateView(symbol: "clock.arrow.circlepath", title: "No runs yet", message: "Every arena run is saved here with its answers, metrics and votes.").frame(minHeight: 320)363 } else {364 LazyVStack(spacing: DS.S.sm) {365 ForEach(model.pastRuns) { run in366 let revealed = !run.blind || !run.votes.isEmpty367 HStack(alignment: .top, spacing: 12) {368 VStack(alignment: .leading, spacing: 4) {369 Text(run.prompt).font(DS.F.body).foregroundStyle(DS.C.text).lineLimit(2)370 Text(Array(Set(run.entries.map { $0.contestant.displayName(revealed: revealed) })).sorted().joined(separator: " · "))371 .font(DS.F.caption).foregroundStyle(DS.C.text2).lineLimit(2)372 HStack(spacing: 6) {373 Text(run.createdAt.formatted(date: .abbreviated, time: .shortened)).font(DS.F.micro).foregroundStyle(DS.C.muted)374 Pill(run.ranConcurrently ? "Concurrent" : "Sequential")375 if run.blind { Pill("Blind", symbol: "eye.slash") }376 if !run.votes.isEmpty { Pill("Voted", symbol: "checkmark", tone: .ok) }377 if run.wasCancelled { Pill("Cancelled", tone: .warn) }378 }379 }380 Spacer(minLength: 8)381 Menu {382 Button { model.open(run); onOpen() } label: { Label("Open", systemImage: "arrow.up.right.square") }383 ShareLink(item: run.exportMarkdown(revealed: revealed)) { Label("Export Markdown", systemImage: "square.and.arrow.up") }384 if revealed, let data = try? run.exportJSON(), let json = String(data: data, encoding: .utf8) {385 ShareLink(item: json) { Label("Export JSON", systemImage: "curlybraces") }386 }387 Button(role: .destructive) { model.delete(run, app: app) } label: { Label("Delete", systemImage: "trash") }388 } label: { Image(systemName: "ellipsis.circle").font(.system(size: 16)).foregroundStyle(DS.C.text2).frame(width: 30, height: 30) }389 .menuStyle(.borderlessButton).fixedSize().accessibilityLabel("Run actions")390 }391 .card(padding: DS.S.md)392 .contentShape(Rectangle())393 .onTapGesture { model.open(run); onOpen() }394 }395 }396 }397 }398}399400struct ArenaLeaderboardView: View {401 let rows: [LeaderboardRow]402 var body: some View {403 if rows.isEmpty {404 EmptyStateView(symbol: "trophy", title: "No votes yet", message: "Vote on arena runs to build a leaderboard of the models you actually prefer.").frame(minHeight: 320)405 } else {406 VStack(alignment: .leading, spacing: DS.S.md) {407 SectionHeader("Leaderboard", subtitle: "Elo rating (start \(Int(ArenaLeaderboard.initialRating)), K = \(Int(ArenaLeaderboard.k))) from your votes, with the plain win rate beside it. With few votes both are indicative only.", symbol: "trophy")408 ScrollView(.horizontal) {409 Grid(alignment: .trailing, horizontalSpacing: 20, verticalSpacing: 9) {410 GridRow {411 Text("#"); Text("Model").gridColumnAlignment(.leading); Text("Elo"); Text("Win rate"); Text("W"); Text("L"); Text("T"); Text("Pairs")412 }.font(DS.F.micro).foregroundStyle(DS.C.muted)413 Divider().gridCellUnsizedAxes(.horizontal)414 ForEach(Array(rows.enumerated()), id: \.element.id) { i, row in415 GridRow {416 Text("\(i + 1)").foregroundStyle(DS.C.muted)417 Text(row.modelName).foregroundStyle(DS.C.text).lineLimit(1)418 Text(String(format: "%.0f", row.rating)).font(DS.F.rounded(14, weight: .bold)).foregroundStyle(i == 0 ? DS.C.accent : DS.C.text)419 Text(row.winRate.map { String(format: "%.0f %%", $0 * 100) } ?? "—")420 Text("\(row.wins)"); Text("\(row.losses)"); Text("\(row.ties)"); Text("\(row.comparisons)")421 }.font(DS.F.callout).monospacedDigit()422 }423 }424 }425 }426 .card()427 }428 }429}430431/// Wrapping horizontal layout for chips and buttons.432struct ArenaFlowLayout: Layout {433 var spacing: CGFloat = 8434 func sizeThatFits(proposal: ProposedViewSize, subviews: Subviews, cache: inout ()) -> CGSize {435 arrange(proposal.width ?? .infinity, subviews).size436 }437 func placeSubviews(in bounds: CGRect, proposal: ProposedViewSize, subviews: Subviews, cache: inout ()) {438 for (i, p) in arrange(bounds.width, subviews).points.enumerated() {439 subviews[i].place(at: CGPoint(x: bounds.minX + p.x, y: bounds.minY + p.y), proposal: ProposedViewSize(width: min(bounds.width, subviews[i].sizeThatFits(.unspecified).width), height: nil))440 }441 }442 private func arrange(_ width: CGFloat, _ subviews: Subviews) -> (size: CGSize, points: [CGPoint]) {443 var x: CGFloat = 0, y: CGFloat = 0, rowHeight: CGFloat = 0, maxX: CGFloat = 0; var points: [CGPoint] = []444 for s in subviews {445 let size = s.sizeThatFits(.unspecified)446 if x > 0, x + size.width > width { x = 0; y += rowHeight + spacing; rowHeight = 0 }447 points.append(CGPoint(x: x, y: y)); x += size.width + spacing; rowHeight = max(rowHeight, size.height); maxX = max(maxX, x - spacing)448 }449 return (CGSize(width: min(maxX, width), height: y + rowHeight), points)450 }451}452