Repository Public
1// Author: Simon-Pierre Boucher — contact@spboucher.ai2// File: SPChat/Features/Workspace/ExplorerCompare.swift3// Description: Comparison table of the Explorer compare tray: the published facts of 2–4 models side by side, with a hand-off to the Arena.4import SwiftUI5import SPChatCore67/// One line of the comparison: a label and one value per model. `best` marks the cells that hold the extreme value8/// of a numeric fact (largest context, lowest price) — a reading aid computed from the same published figures.9struct CompareLine: Identifiable, Equatable {10 let label: String11 let values: [String]12 var best: Set<Int> = []13 var mono = false14 var id: String { label }15}1617enum CompareTable {18 /// Facts compared, in reading order. A line whose value is unknown for every model is left out.19 static func lines(for models: [AIModel], defaultModelId: String?) -> [CompareLine] {20 func line(_ label: String, mono: Bool = false, _ value: (AIModel) -> String?) -> CompareLine? {21 let values = models.map(value)22 return values.allSatisfy({ $0 == nil }) ? nil : CompareLine(label: label, values: values.map { $0 ?? "—" }, mono: mono)23 }24 func numeric(_ label: String, lowest: Bool, _ number: (AIModel) -> Double?, _ format: (Double) -> String) -> CompareLine? {25 let numbers = models.map(number)26 guard numbers.contains(where: { $0 != nil }) else { return nil }27 let comparable = numbers.compactMap { $0 }.filter { $0 >= 0 }28 let extreme = lowest ? comparable.min() : comparable.max()29 // nothing stands out when every model has the same value30 let distinct = Set(comparable).count > 131 let best = Set(numbers.indices.filter { distinct && numbers[$0] != nil && numbers[$0] == extreme })32 return CompareLine(label: label, values: numbers.map { $0.map(format) ?? "—" }, best: best)33 }34 let lines: [CompareLine?] = [35 line("Fabricant") { $0.creatorName },36 line("Route") { ExplorerText.route($0.route) },37 line("Exécuté par") { Provider.label($0.provider) },38 line("Statut") { ExplorerText.status($0).label + ($0.id == defaultModelId ? " · défaut" : "") },39 line("Type") { ExplorerText.kind($0.kind) },40 numeric("Fenêtre de contexte", lowest: false, { $0.contextWindow.map(Double.init) }, { Format.tokens(Int($0)) }),41 numeric("Sortie maximale", lowest: false, { $0.maxOutputTokens.map(Double.init) }, { Format.tokens(Int($0)) }),42 numeric("Prix d'entrée / M", lowest: true, { $0.hasVariablePrice ? -1 : $0.inputPrice }, { ExplorerText.price($0) }),43 numeric("Prix de sortie / M", lowest: true, { $0.hasVariablePrice ? -1 : $0.outputPrice }, { ExplorerText.price($0) }),44 numeric("Entrée en cache / M", lowest: true, { $0.cachedInputPrice }, { ExplorerText.price($0) }),45 line("Paliers de prix") { m in (m.pricing?["tiers"]?.arrayValue).flatMap { $0.isEmpty ? nil : "\($0.count) palier\($0.count > 1 ? "s" : "") selon la taille du prompt" } },46 line("Capacités") { m in47 let c = (ModelFacet.capabilities + ["audioOutput"]).filter { m.has($0) }.map(ModelFacet.capabilityLabel)48 return c.isEmpty ? nil : c.joined(separator: ", ")49 },50 line("Catégories") { $0.categories.isEmpty ? nil : $0.categories.map(ModelFacet.label).joined(separator: ", ") },51 line("Entrées") { m in (m.catalog?.inputModalities).flatMap { $0.isEmpty ? nil : $0.map(ExplorerText.modality).joined(separator: ", ") } },52 line("Sorties") { m in (m.catalog?.outputModalities).flatMap { $0.isEmpty ? nil : $0.map(ExplorerText.modality).joined(separator: ", ") } },53 line("Efforts de raisonnement") { m in (m.catalog?.reasoning?.efforts ?? m.effortLevels).flatMap { $0.isEmpty ? nil : $0.map(ExplorerText.effort).joined(separator: ", ") } },54 line("Banc local · tests") { m in m.benchmark.map { "\($0.passed) / \($0.tests) réussis" } ?? (m.isCatalogOnly ? "non testé par le banc" : nil) },55 numeric("Banc local · qualité", lowest: false, { $0.benchmark?.qualityScore }, { "\(Int($0.rounded())) %" }),56 numeric("Banc local · premier jeton (p50)", lowest: true, { $0.benchmark?.p50TtftMs }, { Format.duration(ms: Int($0.rounded())) }),57 numeric("Banc local · durée (p50)", lowest: true, { $0.benchmark?.p50LatencyMs }, { Format.duration(ms: Int($0.rounded())) }),58 line("Bancs tiers disponibles") { m in (m.catalog?.benchmarkSources).flatMap { $0.isEmpty ? nil : $0.map(ExplorerText.sourceLabel).joined(separator: ", ") } },59 line("Identifiant", mono: true) { $0.id },60 ]61 return lines.compactMap { $0 }62 }63}6465struct ModelCompareView: View {66 let models: [AIModel]67 var defaultModelId: String?68 var categoryRules: [String: String] = [:]69 let close: () -> Void70 let arena: () -> Void7172 private var lines: [CompareLine] { CompareTable.lines(for: models, defaultModelId: defaultModelId) }73 private let labelWidth: CGFloat = 17874 private let columnWidth: CGFloat = 1967576 var body: some View {77 VStack(spacing: 0) {78 HStack(spacing: 10) {79 Text("Comparaison").font(DS.F.heading).foregroundStyle(DS.C.text)80 Text("\(models.count) modèles").font(DS.F.caption).foregroundStyle(DS.C.muted)81 Spacer()82 Button(action: arena) { Label("Ouvrir dans l'Arena", systemImage: "rectangle.split.2x1") }83 .buttonStyle(.borderedProminent).tint(DS.C.accent).disabled(models.filter(\.isSelectableForChat).count < 2)84 .help("Envoie le même prompt à ces modèles, côte à côte").accessibilityIdentifier("compare-arena")85 Button("Fermer", action: close).keyboardShortcut(.cancelAction)86 }87 .padding(.horizontal, 20).frame(height: 52).background(DS.C.bgElev)88 Rectangle().fill(DS.C.border).frame(height: 1)89 ScrollView([.vertical, .horizontal]) {90 Grid(alignment: .topLeading, horizontalSpacing: 0, verticalSpacing: 0) {91 GridRow {92 Color.clear.frame(width: labelWidth, height: 1)93 ForEach(models) { m in header(m) }94 }95 ForEach(lines) { l in96 Rectangle().fill(DS.C.border.opacity(0.7)).frame(height: 1).gridCellColumns(models.count + 1)97 GridRow {98 Text(l.label).font(.system(size: 12)).foregroundStyle(DS.C.muted).frame(width: labelWidth, alignment: .leading).padding(.vertical, 8)99 ForEach(Array(l.values.enumerated()), id: \.offset) { i, v in100 Text(v).font(l.mono ? DS.F.mono(11) : .system(size: 12, weight: l.best.contains(i) ? .semibold : .regular).monospacedDigit())101 .foregroundStyle(v == "—" ? DS.C.muted : (l.best.contains(i) ? DS.C.accent : DS.C.text))102 .textSelection(.enabled).fixedSize(horizontal: false, vertical: true)103 .frame(width: columnWidth - 16, alignment: .leading).padding(.vertical, 8).padding(.trailing, 16)104 .accessibilityLabel("\(l.label), \(models[i].displayName) : \(v)")105 }106 }107 }108 }109 .padding(.horizontal, 24).padding(.vertical, 16)110 }111 Rectangle().fill(DS.C.border).frame(height: 1)112 Text("Faits publiés par le registre de SP Chat et le catalogue OpenRouter ; prix en $ US par million de jetons. En iris : la valeur la plus favorable de la ligne (contexte le plus grand, prix ou délai le plus bas).")113 .font(.system(size: 10.5)).foregroundStyle(DS.C.muted).frame(maxWidth: .infinity, alignment: .leading).padding(.horizontal, 24).padding(.vertical, 10)114 }115 .frame(width: min(CGFloat(models.count) * columnWidth + labelWidth + 48, 1010), height: 640)116 .background(DS.C.bg)117 }118119 private func header(_ m: AIModel) -> some View {120 VStack(alignment: .leading, spacing: 6) {121 ModelMark(m, size: 24)122 Text(m.displayName).font(DS.F.ui).foregroundStyle(DS.C.text).lineLimit(2).fixedSize(horizontal: false, vertical: true)123 if m.isOpenRouter { RouteBadge(route: m.route) } else { Tag(text: "direct") }124 if m.isCatalogOnly { CatalogNotice() }125 }126 .frame(width: columnWidth - 16, alignment: .leading).padding(.trailing, 16).padding(.bottom, 12)127 .accessibilityElement(children: .combine)128 }129}130