Repository Public
1//2// SP Chat iOS3// File: SPChat-iOS/Features/Chat/MessageSheets.swift4// Description: Sheets opened from a message: edit and resend, select text, answer details, artefact viewer.5//6// Author: Simon-Pierre Boucher7// Contact: contact@spboucher.ai8//910import SwiftUI11import UIKit1213struct EditMessageSheet: View {14 let message: ChatMessage15 let onSubmit: (String) -> Void16 @Environment(\.dismiss) private var dismiss17 @State private var text = ""18 @FocusState private var focused: Bool1920 var body: some View {21 NavigationStack {22 TextEditor(text: $text)23 .font(.body).focused($focused).padding(.horizontal, 12)24 .scrollContentBackground(.hidden).background(Color.appBackground)25 .navigationTitle("Modifier le message").navigationBarTitleDisplayMode(.inline)26 .toolbar {27 ToolbarItem(placement: .cancellationAction) { Button("Annuler") { dismiss() } }28 ToolbarItem(placement: .confirmationAction) {29 Button("Renvoyer") { onSubmit(text); dismiss() }.fontWeight(.semibold)30 .disabled(text.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty || text == message.text)31 }32 }33 .safeAreaInset(edge: .bottom) {34 Text("L’ancienne version reste accessible avec les flèches ‹ › sous le message.").font(.footnote).foregroundStyle(Color.muted).padding(.horizontal, 16).padding(.bottom, 8).frame(maxWidth: .infinity, alignment: .leading)35 }36 }37 .onAppear { text = message.text; focused = true }38 }39}4041struct SelectTextSheet: View {42 let text: String43 @Environment(\.dismiss) private var dismiss44 var body: some View {45 NavigationStack {46 SelectableText(text: text)47 .background(Color.appBackground)48 .navigationTitle("Sélectionner le texte").navigationBarTitleDisplayMode(.inline)49 .toolbar {50 ToolbarItem(placement: .cancellationAction) { Button("Fermer") { dismiss() } }51 ToolbarItem(placement: .primaryAction) { Button { UIPasteboard.general.string = text; dismiss() } label: { Label("Tout copier", systemImage: "square.on.square") } }52 }53 }54 }55}5657struct MessageDetailsSheet: View {58 let message: ChatMessage59 let model: AIModel?60 @Environment(\.dismiss) private var dismiss6162 var body: some View {63 NavigationStack {64 List {65 Section("Modèle") {66 HStack { Text("Modèle"); Spacer(minLength: 12); ModelChip(model: model, fallback: message.modelId ?? "—") }67 LabeledContent("Identifiant", value: message.modelId ?? "—")68 if let model { LabeledContent("Créateur", value: model.creatorName) }69 LabeledContent("Route", value: AIModel.routeLabel(model?.route ?? AIModel.impliedRoute(id: message.modelId ?? "", provider: message.provider)))70 if model?.isCatalogOnly == true { Text(model?.isLocal == true ? "Modèle local MacLustr · non testé par le banc" : "Catalogue OpenRouter · non testé par le banc").font(.footnote).foregroundStyle(Color.muted) }71 if let r = message.routing {72 if let mode = r.mode { LabeledContent("Routage", value: mode) }73 if let reason = r.reason { Text(reason).font(.footnote).foregroundStyle(Color.textSecondary) }74 if let served = r.servedBy { LabeledContent("Servi par", value: served) }75 }76 LabeledContent("État", value: message.status.rawValue)77 }78 Section("Mesures") {79 if let u = message.usage {80 LabeledContent("Jetons en entrée", value: u.input.formatted())81 LabeledContent("Jetons en sortie", value: u.output.formatted())82 if let c = u.cached, c > 0 { LabeledContent("Entrée en cache", value: c.formatted()) }83 if let r = u.reasoning, r > 0 { LabeledContent("Raisonnement", value: r.formatted()) }84 }85 if AIModel.impliedRoute(id: message.modelId ?? "", provider: message.provider) == "local" { LabeledContent("Coût", value: String(localized: "local · 0 $")) }86 else { LabeledContent(message.exactCost ? "Coût facturé" : "Coût estimé", value: Format.usd(message.costUsd)) }87 LabeledContent("Premier jeton", value: Format.duration(ms: message.ttftMs))88 LabeledContent("Durée totale", value: Format.duration(ms: message.latencyMs))89 }90 if let a = message.applied, !a.applied.isEmpty || !a.ignored.isEmpty {91 Section {92 ForEach(a.applied.keys.sorted(), id: \.self) { k in LabeledContent(k, value: a.applied[k]?.prettyPrinted(limit: 80) ?? "") }93 ForEach(a.ignored, id: \.param) { i in94 VStack(alignment: .leading, spacing: 2) {95 Text(i.param).strikethrough().foregroundStyle(Color.textSecondary)96 Text(i.reason).font(.footnote).foregroundStyle(Color.muted)97 }98 }99 } header: { Text("Paramètres réellement envoyés") } footer: { Text("Les paramètres barrés ont été demandés mais ce modèle ne les accepte pas. « Facturé » = montant rapporté par le fournisseur ; « estimé » = jetons × prix catalogue.") }100 }101 if let e = message.error { Section("Erreur") { Text(e).foregroundStyle(Color.danger); if let c = message.errorCode { LabeledContent("Code", value: c) } } }102 }103 .navigationTitle("Détails").navigationBarTitleDisplayMode(.inline)104 .toolbar { ToolbarItem(placement: .confirmationAction) { Button("OK") { dismiss() } } }105 }106 }107}108109/// Source of an artefact (HTML, SVG, React, Python…) with syntax colouring, copy and export as a file.110struct ArtifactSheet: View {111 let artifactId: String112 @Environment(AppEnvironment.self) private var env113 @Environment(\.dismiss) private var dismiss114 @State private var artifact: Artifact?115 @State private var error: String?116 @State private var shareURL: URL?117118 var body: some View {119 NavigationStack {120 Group {121 if let a = artifact, let content = a.content {122 ScrollView { CodeBlockView(language: a.language ?? a.type, code: content).padding(12) }123 } else if let error {124 ContentUnavailableView { Label("Artefact indisponible", systemImage: "doc.questionmark") } description: { Text(error) }125 } else { ProgressView() }126 }127 .frame(maxWidth: .infinity, maxHeight: .infinity).background(Color.appBackground)128 .navigationTitle(artifact?.title ?? String(localized: "Artefact")).navigationBarTitleDisplayMode(.inline)129 .toolbar {130 ToolbarItem(placement: .cancellationAction) { Button("Fermer") { dismiss() } }131 ToolbarItem(placement: .primaryAction) {132 if let shareURL { ShareLink(item: shareURL) { Image(systemName: "square.and.arrow.up") }.accessibilityLabel(String(localized: "Partager le fichier")) }133 }134 }135 }136 .task {137 do {138 let a = try await env.api.artifact(artifactId)139 artifact = a140 if let content = a.content {141 let dir = FileManager.default.temporaryDirectory.appendingPathComponent("spchat-preview/artifacts", isDirectory: true)142 try? FileManager.default.createDirectory(at: dir, withIntermediateDirectories: true)143 let url = dir.appendingPathComponent("\(a.title.replacingOccurrences(of: "/", with: "-").prefix(80)).\(a.fileExtension)")144 if (try? Data(content.utf8).write(to: url, options: [.atomic, .completeFileProtectionUnlessOpen])) != nil { shareURL = url }145 }146 } catch { self.error = (error as? APIError)?.message ?? String(localized: "Chargement impossible.") }147 }148 }149}150