Repository Public
1// Author: Simon-Pierre Boucher — contact@spboucher.ai2// File: Features/Research/RsReportText.swift3// Description: Native rendering of untrusted text and data that come back from a research job: the AI agent's markdown-like files as plain structured text (headings, lists, code blocks — no web view, no image, no link followed, no markup interpreted beyond layout) and the laboratory's JSON report as an indented key / value outline. The text is shown verbatim.4import SwiftUI5import SPWalletCore67/// A document split into simple blocks by line prefixes only. Inline markup is left as typed: nothing is hidden and no link becomes clickable.8struct RsPlainDocument {9 enum Block: Identifiable {10 case heading(Int, String, level: Int)11 case paragraph(Int, String)12 case bullet(Int, String, indent: Int)13 case code(Int, String)14 case rule(Int)15 var id: Int { switch self { case .heading(let i, _, _), .paragraph(let i, _), .bullet(let i, _, _), .code(let i, _), .rule(let i): i } }16 }17 let blocks: [Block]18 let truncated: Bool1920 static let maxCharacters = 200_0002122 init(_ raw: String) {23 let text = raw.count > Self.maxCharacters ? String(raw.prefix(Self.maxCharacters)) : raw24 truncated = raw.count > Self.maxCharacters25 var out: [Block] = [], paragraph: [String] = [], code: [String]? = nil, n = 026 func flush() { if !paragraph.isEmpty { out.append(.paragraph(n, paragraph.joined(separator: " "))); n += 1; paragraph = [] } }27 for line in text.replacingOccurrences(of: "\r\n", with: "\n").split(separator: "\n", omittingEmptySubsequences: false) {28 let trimmed = line.trimmingCharacters(in: .whitespaces)29 if trimmed.hasPrefix("```") {30 if let c = code { out.append(.code(n, c.joined(separator: "\n"))); n += 1; code = nil } else { flush(); code = [] }31 continue32 }33 if code != nil { code?.append(String(line)); continue }34 if trimmed.isEmpty { flush(); continue }35 let hashes = trimmed.prefix { $0 == "#" }.count36 if (1...6).contains(hashes), trimmed.dropFirst(hashes).hasPrefix(" ") {37 flush(); out.append(.heading(n, trimmed.dropFirst(hashes).trimmingCharacters(in: .whitespaces), level: hashes)); n += 1; continue38 }39 if trimmed.count >= 3, trimmed.allSatisfy({ $0 == "-" || $0 == "*" || $0 == "_" }), Set(trimmed).count == 1 { flush(); out.append(.rule(n)); n += 1; continue }40 let indent = line.prefix { $0 == " " || $0 == "\t" }.reduce(0) { $0 + ($1 == "\t" ? 4 : 1) } / 241 if trimmed.hasPrefix("- ") || trimmed.hasPrefix("* ") || trimmed.hasPrefix("+ ") {42 flush(); out.append(.bullet(n, "• " + trimmed.dropFirst(2), indent: indent)); n += 1; continue43 }44 if let m = trimmed.prefixMatch(of: #/\d{1,3}[.)] /#) {45 flush(); out.append(.bullet(n, String(m.0) + trimmed[m.range.upperBound...], indent: indent)); n += 1; continue46 }47 if trimmed.hasPrefix("|") { flush(); out.append(.code(n, String(line))); n += 1; continue } // table rows: kept aligned, monospaced48 paragraph.append(trimmed)49 }50 if let c = code { out.append(.code(n, c.joined(separator: "\n"))); n += 1 }51 flush()52 blocks = out53 }54}5556struct RsPlainDocumentView: View {57 let document: RsPlainDocument58 init(_ text: String) { document = RsPlainDocument(text) }5960 var body: some View {61 LazyVStack(alignment: .leading, spacing: 7) {62 ForEach(document.blocks) { block in63 switch block {64 case .heading(_, let t, let level): Text(verbatim: t).font(level <= 1 ? .title3.weight(.semibold) : level == 2 ? .headline : .subheadline.weight(.semibold)).padding(.top, 4)65 case .paragraph(_, let t): Text(verbatim: t).fixedSize(horizontal: false, vertical: true)66 case .bullet(_, let t, let indent): Text(verbatim: t).padding(.leading, CGFloat(min(indent, 6)) * 12 + 4).fixedSize(horizontal: false, vertical: true)67 case .code(_, let t):68 Text(verbatim: t).font(Theme.monoSmall).fixedSize(horizontal: false, vertical: true).padding(8).frame(maxWidth: .infinity, alignment: .leading)69 .background(Color.secondary.opacity(0.08), in: RoundedRectangle(cornerRadius: 6))70 case .rule: Divider()71 }72 }73 if document.truncated { Text("Texte tronqué à \(RsPlainDocument.maxCharacters.formatted()) caractères pour l’affichage.").font(.caption).foregroundStyle(Theme.warning) }74 }75 .textSelection(.enabled)76 .frame(maxWidth: .infinity, alignment: .leading)77 }78}7980/// Server JSON as an outline: keys and scalar values, nested objects indented. Long arrays are summarised, never silently cut.81struct RsJSONOutline: View {82 struct Line: Identifiable { let id: Int; let depth: Int; let key: String; let value: String? }83 let lines: [Line]84 let clipped: Bool85 static let maxLines = 4008687 init(_ value: JSONValue) {88 var out: [Line] = [], clippedFlag = false89 func walk(_ v: JSONValue, key: String, depth: Int) {90 guard out.count < Self.maxLines else { clippedFlag = true; return }91 switch v {92 case .object(let o):93 if !key.isEmpty { out.append(Line(id: out.count, depth: depth, key: key, value: o.isEmpty ? "{}" : nil)) }94 for k in o.keys.sorted() { if let child = o[k] { walk(child, key: k, depth: key.isEmpty ? depth : depth + 1) } }95 case .array(let a):96 let scalars = a.allSatisfy { if case .object = $0 { return false }; if case .array = $0 { return false }; return true }97 if scalars && a.count <= 12 { out.append(Line(id: out.count, depth: depth, key: key, value: a.isEmpty ? "[]" : a.map(\.displayText).joined(separator: ", "))) }98 else if a.count > 24 { out.append(Line(id: out.count, depth: depth, key: key, value: "\(a.count) éléments (non détaillés ici)")) }99 else {100 out.append(Line(id: out.count, depth: depth, key: key, value: nil))101 for (i, child) in a.enumerated() { walk(child, key: "[\(i)]", depth: depth + 1) }102 }103 default: out.append(Line(id: out.count, depth: depth, key: key, value: v.displayText))104 }105 }106 walk(value, key: "", depth: 0)107 lines = out; clipped = clippedFlag108 }109110 var body: some View {111 LazyVStack(alignment: .leading, spacing: 2) {112 ForEach(lines) { l in113 HStack(alignment: .firstTextBaseline, spacing: 8) {114 Text(verbatim: l.key).font(Theme.monoSmall).foregroundStyle(.secondary)115 if let v = l.value { Text(verbatim: v).font(Theme.monoSmall).fixedSize(horizontal: false, vertical: true) }116 Spacer(minLength: 0)117 }.padding(.leading, CGFloat(min(l.depth, 8)) * 14)118 }119 if clipped { Text("Affichage limité aux \(Self.maxLines) premières lignes de ce bloc.").font(.caption).foregroundStyle(Theme.warning) }120 }.textSelection(.enabled)121 }122}123124/// The header every result block carries: what kind of thing it is, and who produced it.125struct RsBlockHeader: View {126 let title: String127 let provenance: ResearchProvenance128 var body: some View {129 let (p, text) = RsJobText.provenance(provenance)130 HStack { Text(title).font(.headline); Spacer(); ProvenanceTag(p: p, text: text) }131 }132}133