150 lines · 6.9 KB · SwiftRaw
// AN ASSISTANT'S ASK (ROUTES-AUDIT finding 8; AUDIT C 5, 28 Nov 2026): with token scopes on, an assistant's token that// sends mail from the owner's address, changes a forward or invites guests waits, and the phone shows what it would do// (`asks.card`): the core's heading and lines, a mail's text (with how much is not shown) and its attachments, then// Allow and Not now (`asks.allow` / `asks.refuse`, the object runs or drops the kept request). Every word is the// core's. It stands over the app as the unlock card does, read at the same moments, and every `pollSeconds` while one// waits.import CottonKitimport SwiftUI/// One waiting ask as the core says it.struct AssistantAskCard: Equatable { var askId: String var heading: String var lines: [String] var text: String? var more: String? var attachments: [String] var allow: String var refuse: String init?(_ value: JSONValue?) { guard case .object(let card)? = value, let id = card["askId"]?.string, let heading = card["heading"]?.string else { return nil } askId = id self.heading = heading lines = (card["lines"]?.strings ?? []).filter { !$0.isEmpty } // A mail's words as the object kept them, and "N more characters" when cut (both null otherwise). text = card["text"]?.string more = card["more"]?.string attachments = card["attachments"]?.strings ?? [] allow = card["allow"]?["label"]?.string ?? "Allow" refuse = card["refuse"]?["label"]?.string ?? "Not now" }}@MainActor@Observablefinal class AssistantAsks { private(set) var card: AssistantAskCard? private(set) var busy = false @ObservationIgnored private let core: any CottonCalling @ObservationIgnored private let say: @MainActor (String, CottonToast.Tone) -> Void @ObservationIgnored private var poll: Task<Void, Never>? init(core: any CottonCalling, say: @escaping @MainActor (String, CottonToast.Tone) -> Void) { self.core = core self.say = say } /// Read the oldest waiting ask again. func read() async { do { let answer = try await core.callJSON("asks.card") card = AssistantAskCard(answer["card"]) keepPolling(answer["pollSeconds"].flatMap { if case .number(let n) = $0 { n } else { nil } }) } catch { Log.write("asks: the card could not be read — \(error)") } } /// Allow (the object runs the kept request once) or Not now (dropped, never run); the core's words said after. func answer(allow: Bool) async { guard let card, !busy else { return } busy = true defer { busy = false } do { let answer = try await core.callJSON(allow ? "asks.allow" : "asks.refuse", ["askId": card.askId]) if let said = answer["said"]?.string, !said.isEmpty { say(said, allow ? .success : .notice) } } catch { Log.write("asks: \(allow ? "allow" : "refuse") was not taken — \(error)") } await read() } private func keepPolling(_ seconds: Double?) { poll?.cancel() poll = nil guard card != nil, let seconds, seconds > 0 else { return } poll = Task { @MainActor [weak self] in try? await Task.sleep(for: .seconds(seconds)) guard let self, !Task.isCancelled else { return } await self.read() } }}/// The ask over the app: a veil, the card's words on paper, a mail's text in its own box, then Not now and Allow.struct AssistantAskLayer: View { let asks: AssistantAsks var body: some View { if let card = asks.card { ZStack { Color.black.opacity(UnlockAsksKnobs.veil).ignoresSafeArea().accessibilityHidden(true) VStack(alignment: .leading, spacing: 14) { Text(verbatim: card.heading) .font(.title3.weight(.semibold)) .fixedSize(horizontal: false, vertical: true) .accessibilityAddTraits(.isHeader) .accessibilityIdentifier("asks-heading") ScrollView { VStack(alignment: .leading, spacing: 8) { ForEach(Array(card.lines.enumerated()), id: \.offset) { at, line in Text(verbatim: line).font(.body).fixedSize(horizontal: false, vertical: true) .accessibilityIdentifier("asks-line-\(at)") } if let text = card.text { Text(verbatim: text) .font(.callout) .fixedSize(horizontal: false, vertical: true) .padding(10) .frame(maxWidth: .infinity, alignment: .leading) .background(Color.primary.opacity(0.05), in: RoundedRectangle(cornerRadius: 10)) .accessibilityIdentifier("asks-text") if let more = card.more { Text(verbatim: more).font(.footnote).foregroundStyle(.secondary).accessibilityIdentifier("asks-more") } } ForEach(Array(card.attachments.enumerated()), id: \.offset) { at, line in Label { Text(verbatim: line).fixedSize(horizontal: false, vertical: true) } icon: { Image(systemName: "paperclip") } .font(.callout) .accessibilityIdentifier("asks-attachment-\(at)") } } .frame(maxWidth: .infinity, alignment: .leading) } .frame(maxHeight: 420) HStack(spacing: 28) { Spacer() PaperTypedButton(card.refuse, size: UnlockAsksKnobs.actSize) { Task { await asks.answer(allow: false) } } .disabled(asks.busy) .accessibilityIdentifier("asks-refuse") PaperTypedButton(card.allow, size: UnlockAsksKnobs.actSize) { Task { await asks.answer(allow: true) } } .disabled(asks.busy) .accessibilityIdentifier("asks-allow") } } .foregroundStyle(Color.black.opacity(0.85)) .padding(22) .frame(maxWidth: UnlockAsksKnobs.cardWidth) .background(PaperDye.cream.fill, in: RoundedRectangle(cornerRadius: 22)) .padding(UnlockAsksKnobs.gutter) .accessibilityElement(children: .contain) .accessibilityIdentifier("asks-card") } .transition(.opacity) } }}