forked from BasedHardware/omi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChatFirstContentBlockViews.swift
More file actions
1126 lines (1045 loc) · 40.8 KB
/
Copy pathChatFirstContentBlockViews.swift
File metadata and controls
1126 lines (1045 loc) · 40.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import AppKit
import OmiTheme
import SwiftUI
// MARK: - Question card
/// Choices are controls only while the kernel-backed parent is the completed
/// tail of Main Chat. The runtime remains authoritative at selection time;
/// this view's gate simply avoids presenting obsolete choices as actionable.
/// Whether a question card's options are pressable, dimmed, or gone.
///
/// Three different situations used to collapse into one boolean, and the losing
/// two both rendered as "no options at all": a question already answered (right),
/// a question whose turn is no longer the tail (right), and a question on an
/// account whose capability has not resolved (wrong — that reader saw a question
/// with no visible answers and no explanation).
enum ChatFirstQuestionCardOptionsPolicy: Equatable {
case hidden
case enabled
case disabled
static func presentation(
isActionable: Bool,
isCapabilityAvailable: Bool,
hasSelection: Bool,
hasOptions: Bool
) -> Self {
guard hasOptions, !hasSelection else { return .hidden }
if isActionable { return .enabled }
// Capability-off is the only reason to show unpressable options: the
// question is live, we simply cannot answer it yet.
return isCapabilityAvailable ? .hidden : .disabled
}
var isVisible: Bool { self != .hidden }
var isPressable: Bool { self == .enabled }
}
struct QuestionCardView: View {
private struct Option: Identifiable {
let id: String
let label: String
let isDeferral: Bool
init?(_ dictionary: [String: Any]) {
guard let id = dictionary["optionId"] as? String,
!id.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty,
let label = dictionary["label"] as? String,
!label.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty
else { return nil }
self.id = id
self.label = label
self.isDeferral = dictionary["defer"] as? Bool ?? false
}
}
let questionID: String
let text: String
let options: [[String: Any]]
let selectedOptionID: String?
let isActionable: Bool
/// False while the server-owned capability has not resolved, or for an account
/// it does not cover. The options still render — a question with its answers
/// hidden reads as a question nobody asked — but they cannot be pressed.
let isCapabilityAvailable: Bool
let onSelect: (String, Bool) -> Void
private var validOptions: [Option] { options.compactMap(Option.init) }
var body: some View {
VStack(alignment: .leading, spacing: OmiSpacing.sm) {
Label("Question", systemImage: "questionmark.circle")
.scaledFont(size: OmiType.caption, weight: .semibold)
.foregroundStyle(Ink.secondary)
Text(text)
.scaledFont(size: OmiType.body, weight: .medium)
.foregroundStyle(Ink.primary)
.fixedSize(horizontal: false, vertical: true)
// A completed question remains useful transcript context, but its
// suggestions disappear as soon as an answer exists or another bubble
// has taken the tail. We never leave stale chips that look tappable.
//
// Capability-off is the one case that shows the chips *without* making
// them pressable: the question is real and its answers are the only thing
// that explains it, so they are dimmed rather than deleted.
let optionsPresentation = ChatFirstQuestionCardOptionsPolicy.presentation(
isActionable: isActionable,
isCapabilityAvailable: isCapabilityAvailable,
hasSelection: selectedOptionID != nil,
hasOptions: !validOptions.isEmpty
)
if optionsPresentation.isVisible {
FlowLayout(spacing: OmiSpacing.sm) {
ForEach(validOptions) { option in
Button {
onSelect(option.id, option.isDeferral)
} label: {
Text(option.label)
.scaledFont(size: OmiType.caption, weight: .medium)
.foregroundStyle(Ink.secondary)
.padding(.horizontal, OmiSpacing.md)
.padding(.vertical, OmiSpacing.sm)
.glassChip()
}
.buttonStyle(.plain)
.disabled(!optionsPresentation.isPressable)
.opacity(optionsPresentation.isPressable ? 1 : 0.45)
.accessibilityLabel("Send suggestion: \(option.label)")
.accessibilityIdentifier("chat-first-question-\(questionID)-option-\(option.id)")
}
}
if !optionsPresentation.isPressable {
Text("Answering is unavailable right now")
.scaledFont(size: OmiType.caption)
.foregroundStyle(Ink.secondary)
.accessibilityIdentifier("chat-first-question-\(questionID)-unavailable")
}
}
}
.padding(.horizontal, OmiSpacing.md)
.padding(.vertical, OmiSpacing.sm)
.frame(maxWidth: .infinity, alignment: .leading)
.background(Ink.rowFill)
.clipShape(RoundedRectangle(cornerRadius: PageGlass.rowRadius, style: .continuous))
.overlay(
RoundedRectangle(cornerRadius: PageGlass.rowRadius, style: .continuous)
.stroke(Ink.glassEdge, lineWidth: 1)
)
.accessibilityElement(children: .contain)
.accessibilityIdentifier("chat-first-question-\(questionID)")
.onAppear {
AnalyticsManager.shared.chatFirst(
.richBlock(kind: .questionCard, outcome: .rendered, action: .none)
)
AnalyticsManager.shared.chatFirst(.question(lifecycle: .shown))
}
.onChange(of: isActionable) { wasActionable, nowActionable in
guard wasActionable, !nowActionable, selectedOptionID == nil else { return }
AnalyticsManager.shared.chatFirst(.question(lifecycle: .retiredUnseen))
}
}
}
// MARK: - Task card
struct TaskCardView: View {
let taskID: String
@ObservedObject private var tasksStore: TasksStore
let navigation: ChatFirstShellNavigation
@State private var isToggling = false
@State private var showCompletionAcknowledgement = false
@State private var hydrationFinished = false
@State private var retainedCompletedTask: TaskActionItem?
/// The completion the reader performed on this card, kept whatever the store
/// says afterwards. See `ChatFirstTaskCardPresentation.displayTask`.
@State private var locallyCompletedTask: TaskActionItem?
init(taskID: String, tasksStore: TasksStore, navigation: ChatFirstShellNavigation) {
self.taskID = taskID
_tasksStore = ObservedObject(wrappedValue: tasksStore)
self.navigation = navigation
}
private var liveTask: TaskActionItem? {
tasksStore.tasks.first { $0.id == taskID && !$0.isRetired }
}
private var isExplicitlyRetired: Bool {
(tasksStore.tasks + tasksStore.deletedTasks).contains { $0.id == taskID && $0.isRetired }
}
/// The store's row for this card, retired or not.
///
/// `liveTask` is a presentation filter, so it answers nil for a retired row —
/// which made it the wrong thing to reconcile a toggle against. Completing a
/// task whose local row carried a stale tombstone read back as "the mutation
/// did not land", and the card retired itself over the reader's own tick.
private var storeRecord: TaskActionItem? {
tasksStore.tasks.first { $0.id == taskID }
}
private var task: TaskActionItem? {
ChatFirstTaskCardPresentation.displayTask(
liveTask: liveTask,
retainedCompletedTask: retainedCompletedTask?.id == taskID ? retainedCompletedTask : nil,
locallyCompletedTask: locallyCompletedTask?.id == taskID ? locallyCompletedTask : nil
)
}
private var hydrationKey: String {
"\(taskID):\(liveTask == nil)"
}
// The body's branches are extracted so release optimization can type-check
// each in reasonable time; the whole expression as one literal timed out the
// compiler ("unable to type-check this expression in reasonable time").
var body: some View {
Group {
if let task {
renderedCard(task)
} else if hydrationFinished {
unavailableBlock
} else {
ChatFirstLoadingBlockView(entityName: "Task")
}
}
.accessibilityIdentifier("chat-first-task-\(taskID)")
.onChange(of: liveTask) { _, updatedTask in
retainCompletedTaskIfNeeded(updatedTask)
}
.onChange(of: isExplicitlyRetired) { _, retired in
if retired {
retainedCompletedTask = nil
}
}
.task(id: hydrationKey) {
guard liveTask == nil else {
hydrationFinished = true
return
}
if retainedCompletedTask == nil {
hydrationFinished = false
}
let resolvedTask = await tasksStore.resolveCanonicalTask(id: taskID)
switch ChatFirstTaskCardHydration.resolution(
isCancelled: Task.isCancelled, hasLiveTask: liveTask != nil)
{
case .abandon:
return
case .settle:
// The toggle won the race and put the task back in the store. That is
// a better answer than this hydration's, so take it.
retainCompletedTaskIfNeeded(liveTask)
hydrationFinished = true
case .adopt:
if resolvedTask == nil {
log("TaskCardView: \(taskID) hydrated to nothing — the store cannot vouch for this task")
}
// A store that cannot vouch for the row is not the same as a row the
// user retired, and only the second is grounds for taking a card away.
// `.onChange(of: isExplicitlyRetired)` is the one clearer.
retainCompletedTaskIfNeeded(resolvedTask)
hydrationFinished = true
}
}
}
private func renderedCard(_ task: TaskActionItem) -> some View {
card(task)
.onAppear {
retainCompletedTaskIfNeeded(liveTask)
AnalyticsManager.shared.chatFirst(
.richBlock(kind: .taskCard, outcome: .rendered, action: .none)
)
}
}
private var unavailableBlock: some View {
ChatFirstUnavailableBlockView(entityName: "Task")
.onAppear {
log(unavailableDescription)
AnalyticsManager.shared.chatFirst(
.richBlock(kind: .taskCard, outcome: .stalePlaceholder, action: .none)
)
}
}
private var unavailableDescription: String {
"TaskCardView: \(taskID) unavailable"
+ " store=\(tasksStore.tasks.count)"
+ " incomplete=\(tasksStore.incompleteTasks.count)"
+ " completed=\(tasksStore.completedTasks.count)"
+ " deleted=\(tasksStore.deletedTasks.count)"
+ " present=\(tasksStore.tasks.contains { $0.id == taskID })"
+ " retiredHere=\(isExplicitlyRetired)"
+ " retained=\(retainedCompletedTask?.id ?? "none")"
}
@ViewBuilder
private func card(_ task: TaskActionItem) -> some View {
VStack(alignment: .leading, spacing: OmiSpacing.sm) {
HStack(spacing: OmiSpacing.xs) {
Label("Task", systemImage: "checklist")
.scaledFont(size: OmiType.caption, weight: .semibold)
.foregroundStyle(Ink.secondary)
}
HStack(alignment: .center, spacing: OmiSpacing.md) {
Button {
toggle(task)
} label: {
ZStack {
Image(systemName: task.completed ? "checkmark.circle.fill" : "circle")
.scaledFont(size: OmiType.subheading, weight: .medium)
.foregroundStyle(task.completed ? Ink.listeningGreen : Ink.secondary)
if showCompletionAcknowledgement {
Image(systemName: "checkmark")
.scaledFont(size: OmiType.caption, weight: .bold)
.foregroundStyle(Ink.listeningGreen)
.transition(.scale.combined(with: .opacity))
}
}
.frame(width: 24, height: 24)
}
.buttonStyle(.plain)
.disabled(isToggling)
.accessibilityLabel(
task.completed ? "Mark \(task.description) incomplete" : "Mark \(task.description) complete"
)
.accessibilityIdentifier("chat-first-task-\(taskID)-toggle")
VStack(alignment: .leading, spacing: OmiSpacing.sm) {
Text(task.description)
.scaledFont(size: OmiType.body, weight: .medium)
.foregroundStyle(task.completed ? Ink.secondary : Ink.primary)
.strikethrough(task.completed, color: Ink.secondary)
.fixedSize(horizontal: false, vertical: true)
HStack(spacing: OmiSpacing.xs) {
if let goalID = task.goalId, !goalID.isEmpty {
ChatFirstDestinationBadge(
title: "Goal",
systemImage: "target",
accessibilityID: "chat-first-task-\(taskID)-goal-\(goalID)"
) {
navigation.open(focus: .goal(id: goalID))
}
}
if let conversationID = ChatFirstCaptureLinkPolicy.captureID(for: task) {
ChatFirstDestinationBadge(
title: "Capture",
systemImage: "waveform",
accessibilityID: "chat-first-task-\(taskID)-capture-\(conversationID)"
) {
navigation.open(focus: .capture(id: conversationID, momentTs: nil))
}
}
}
}
}
}
.padding(.horizontal, OmiSpacing.md)
.padding(.vertical, OmiSpacing.sm)
.frame(maxWidth: .infinity, alignment: .leading)
.background(Ink.rowFill)
.clipShape(RoundedRectangle(cornerRadius: PageGlass.rowRadius, style: .continuous))
.overlay(
RoundedRectangle(cornerRadius: PageGlass.rowRadius, style: .continuous)
.stroke(Ink.glassEdge, lineWidth: 1)
)
}
private func retainCompletedTaskIfNeeded(_ task: TaskActionItem?) {
guard let task else { return }
retainedCompletedTask = task.completed && !task.isRetired ? task : nil
}
private func toggle(_ task: TaskActionItem) {
guard !isToggling else { return }
let intendedCompletion = !task.completed
isToggling = true
AnalyticsManager.shared.chatFirst(
.richBlock(kind: .taskCard, outcome: .acted, action: .toggle)
)
AnalyticsManager.shared.chatFirst(
.taskMutation(lifecycle: .attempt, mutation: .completion)
)
Task { @MainActor in
await tasksStore.toggleTask(task)
isToggling = false
let reconciledTask = self.storeRecord
// The reader ticked this card and the store took the mutation. That is
// the answer the card shows from here on: a retirement discovered
// afterwards — a stale local tombstone, a lane that cannot vouch for the
// row — is not grounds for erasing a completion they performed.
if intendedCompletion {
locallyCompletedTask = reconciledTask?.completed == true ? reconciledTask : nil
} else {
locallyCompletedTask = nil
}
AnalyticsManager.shared.chatFirst(
.taskMutation(
lifecycle: reconciledTask?.completed == intendedCompletion ? .success : .rollback,
mutation: .completion
)
)
if reconciledTask?.completed != intendedCompletion {
AnalyticsManager.shared.chatFirst(
.richBlock(kind: .taskCard, outcome: .rejected, action: .toggle)
)
}
// `TasksStore` owns local-first mutation and rollback. Acknowledgement
// is derived only from its reconciled record, never from the tap.
guard
ChatFirstTaskCardReconciliation.shouldShowCompletionAcknowledgement(
intendedCompletion: intendedCompletion,
reconciledTask: reconciledTask
)
else { return }
OmiMotion.withGated(.spring(response: 0.26, dampingFraction: 0.72)) {
showCompletionAcknowledgement = true
}
Task { @MainActor in
try? await Task.sleep(nanoseconds: 550_000_000)
guard !Task.isCancelled else { return }
OmiMotion.withGated(.easeOut(duration: 0.16)) {
showCompletionAcknowledgement = false
}
}
}
}
}
/// What a finished hydration is allowed to write back to the card.
///
/// `.task(id:)` cancels the in-flight hydration when its key changes, but Swift
/// cancellation is cooperative: the body keeps running and its `await` still
/// returns. A hydration that started while the card had no task can therefore
/// land *after* the reader has ticked that task, carrying an answer from before
/// the tick — and `resolveCanonicalTask` answers nil for any row it cannot
/// vouch for, including one whose owner lease turned over mid-flight. Applying
/// that late nil cleared the retained task and marked hydration finished, which
/// is exactly the pair that renders "Task is no longer available" under a task
/// the reader had just completed.
///
/// Observed directly: a card visibly showing its task logged
/// `hydrated resolved=nil` from a hydration still in flight behind it.
enum ChatFirstTaskCardHydration {
enum Resolution: Equatable {
/// Nothing newer arrived; the answer is the card's state.
case adopt
/// The card already has a live task, so there is nothing to adopt — but
/// this hydration is genuinely over.
case settle
/// A successor hydration owns the card's state. Write nothing at all:
/// even `hydrationFinished` would flash the unavailable placeholder in
/// the gap before the successor answers.
case abandon
}
static func resolution(isCancelled: Bool, hasLiveTask: Bool) -> Resolution {
if isCancelled { return .abandon }
return hasLiveTask ? .settle : .adopt
}
}
enum ChatFirstTaskCardPresentation {
/// `locallyCompletedTask` is the completion the reader performed on this card
/// and it outranks everything, retirement included.
///
/// Every other input is a projection of store state, and store state can say
/// a task is gone for reasons that have nothing to do with the reader: the
/// Removed lane used to tombstone live rows locally, so ticking one of them
/// swapped their own completed card for "Task is no longer available". A
/// gesture the app accepted is not something a later read gets to deny — the
/// card keeps showing the tick until the reader themselves unticks it.
static func displayTask(
liveTask: TaskActionItem?,
retainedCompletedTask: TaskActionItem?,
locallyCompletedTask: TaskActionItem? = nil
) -> TaskActionItem? {
if let locallyCompletedTask, locallyCompletedTask.completed {
return locallyCompletedTask
}
if let liveTask {
return liveTask.isRetired ? nil : liveTask
}
guard let retainedCompletedTask,
retainedCompletedTask.completed,
!retainedCompletedTask.isRetired
else { return nil }
return retainedCompletedTask
}
}
/// Keeps the card's acknowledgement tied to the owner-safe store's reconciled
/// record. A failed remote mutation leaves the original task in the store, so
/// the view never converts a tap into a false success signal.
enum ChatFirstTaskCardReconciliation {
static func shouldShowCompletionAcknowledgement(
intendedCompletion: Bool,
reconciledTask: TaskActionItem?
) -> Bool {
intendedCompletion && reconciledTask?.completed == true
}
}
// MARK: - Goal and capture links
struct GoalLinkView: View {
let goalID: String
let summary: String
let navigation: ChatFirstShellNavigation
@ObservedObject var goalsStore: CanonicalGoalsStore
@State private var isOpening = false
@State private var isUnavailable = false
var body: some View {
Group {
if isUnavailable {
ChatFirstUnavailableBlockView(entityName: "Goal")
} else {
ChatFirstLinkBlockView(
eyebrow: "Goal",
systemImage: "target",
summary: summary,
actionTitle: "Open in Goals",
isOpening: isOpening,
accessibilityID: "chat-first-goal-\(goalID)-open"
) {
openGoal()
}
}
}
.onAppear {
AnalyticsManager.shared.chatFirst(
.richBlock(kind: .goalLink, outcome: .rendered, action: .none)
)
}
}
private func openGoal() {
guard !isOpening else { return }
isOpening = true
let resolutionGeneration = navigation.beginGoalLinkResolution()
Task { @MainActor in
defer { isOpening = false }
// Validate through the root-owned canonical projection. The actual
// destination remains a typed shell focus, never a display-string URL.
let detail = await goalsStore.loadDetail(goalID: goalID)
guard navigation.isCurrentGoalLinkResolution(resolutionGeneration) else { return }
guard detail != nil else {
isUnavailable = true
AnalyticsManager.shared.chatFirst(
.richBlock(kind: .goalLink, outcome: .stalePlaceholder, action: .open)
)
return
}
AnalyticsManager.shared.chatFirst(
.richBlock(kind: .goalLink, outcome: .acted, action: .open)
)
_ = navigation.completeGoalLinkResolution(goalID: goalID, generation: resolutionGeneration)
}
}
}
struct CaptureLinkView: View {
let conversationID: String
let momentTimestampMs: Int?
let summary: String
let navigation: ChatFirstShellNavigation
@State private var isOpening = false
@State private var isUnavailable = false
var body: some View {
Group {
if isUnavailable {
ChatFirstUnavailableBlockView(entityName: "Conversation")
} else {
ChatFirstLinkBlockView(
eyebrow: "Conversation",
systemImage: "waveform",
summary: summary,
actionTitle: "Open conversation",
isOpening: isOpening,
accessibilityID: "chat-first-capture-\(conversationID)-open"
) {
openCapture()
}
}
}
.onAppear {
AnalyticsManager.shared.chatFirst(
.richBlock(kind: .captureLink, outcome: .rendered, action: .none)
)
}
}
private func openCapture() {
guard !isOpening else { return }
isOpening = true
Task { @MainActor in
defer { isOpening = false }
do {
_ = try await APIClient.shared.getOmiCapture(id: conversationID)
let moment = momentTimestampMs.map { TimeInterval($0) / 1_000 }
AnalyticsManager.shared.chatFirst(
.richBlock(kind: .captureLink, outcome: .acted, action: .open)
)
navigation.open(focus: .capture(id: conversationID, momentTs: moment))
} catch {
isUnavailable = true
AnalyticsManager.shared.chatFirst(
.richBlock(kind: .captureLink, outcome: .stalePlaceholder, action: .open)
)
}
}
}
}
struct ConversationLinkView: View {
let conversationID: String
let summary: String
let recommendedActionItems: [ConversationLinkActionItem]
let navigation: ChatFirstShellNavigation
@State private var isOpening = false
@State private var isUnavailable = false
@State private var isCopyingLink = false
@State private var shareLinkFeedback: ConversationShareLinkFeedback?
@State private var shareLinkFeedbackGeneration = 0
@State private var shareRecipients: [ConversationShareRecipient] = []
@State private var isSendingSummary = false
@State private var summarySendStatus: (message: String, success: Bool)?
private var sendSummaryTitle: String? {
guard let first = shareRecipients.first else { return nil }
let extra = shareRecipients.count - 1
return extra > 0 ? "Send to \(first.shortLabel) +\(extra)" : "Send to \(first.shortLabel)"
}
private var statusLine: (message: String, systemImage: String, color: Color)? {
if let summarySendStatus {
return (
summarySendStatus.message,
summarySendStatus.success ? "checkmark" : "exclamationmark.triangle",
summarySendStatus.success ? Ink.listeningGreen : Ink.errorRed
)
}
if let shareLinkFeedback {
return (
shareLinkFeedback.message,
shareLinkFeedback.systemImage,
shareLinkFeedback == .copied ? Ink.listeningGreen : Ink.errorRed
)
}
return nil
}
var body: some View {
Group {
if isUnavailable {
ChatFirstUnavailableBlockView(entityName: "Conversation")
} else {
ChatFirstLinkBlockView(
eyebrow: "Meeting notes ready",
systemImage: "text.document",
summary: summary,
actionTitle: "Open conversation",
isOpening: isOpening,
accessibilityID: "chat-first-conversation-\(conversationID)-open",
action: { openConversation() },
recommendedActionItems: recommendedActionItems,
recommendedActionItemAction: { item in
guard let taskID = item.taskID else { return }
navigation.open(focus: .task(id: taskID))
},
secondaryActionTitle: "Copy share link",
secondaryActionSystemImage: "link",
isSecondaryBusy: isCopyingLink,
secondaryAccessibilityID: "chat-first-conversation-\(conversationID)-copy-link",
secondaryHelp: "Copy share link — anyone with the link can view",
secondaryAction: { copyShareLink() },
tertiaryActionTitle: sendSummaryTitle,
tertiaryActionSystemImage: "paperplane",
isTertiaryBusy: isSendingSummary,
tertiaryAccessibilityID: "chat-first-conversation-\(conversationID)-send-summary",
tertiaryHelp: shareRecipients.first.map { "Email the summary to \($0.email)" },
tertiaryAction: { sendSummary() },
statusMessage: statusLine?.message,
statusSystemImage: statusLine?.systemImage,
statusColor: statusLine?.color ?? Ink.secondary
)
}
}
.onAppear {
AnalyticsManager.shared.chatFirst(
.richBlock(kind: .conversationLink, outcome: .rendered, action: .none)
)
}
.task {
// Calendar-detected participants make the one-click "Send to …" chip
// appear; no detection (or a fetch failure) just means no chip.
shareRecipients =
(try? await APIClient.shared.getConversationShareRecipients(id: conversationID)) ?? []
}
}
/// One-click email of the summary to the calendar-detected participants.
/// The backend validates recipients and flips visibility to shared, so this
/// discloses the audience in the confirmation just like copying the link.
private func sendSummary() {
guard !isSendingSummary, !shareRecipients.isEmpty else { return }
isSendingSummary = true
Task { @MainActor in
defer { isSendingSummary = false }
do {
let sent = try await APIClient.shared.sendConversationSummaryEmail(
id: conversationID,
recipientEmails: shareRecipients.map(\.email)
)
summarySendStatus = (
"Summary sent to \(sent.joined(separator: ", ")) — anyone with the link can view", true
)
} catch {
summarySendStatus = ("Couldn't send the summary — try again", false)
}
}
}
/// Copies a public share link for the conversation. Minting the link flips
/// the conversation's visibility to shared, so the confirmation discloses
/// the audience. A failure here never touches `isUnavailable` — copy
/// problems must not block "Open conversation".
private func copyShareLink() {
guard !isCopyingLink else { return }
isCopyingLink = true
Task { @MainActor in
defer { isCopyingLink = false }
let feedback = await ConversationShareLinkAction.run(
mintLink: { try await APIClient.shared.getConversationShareLink(id: conversationID) },
copyToPasteboard: { link in
NSPasteboard.general.clearContents()
return NSPasteboard.general.setString(link, forType: .string)
}
)
AnalyticsManager.shared.chatFirst(
.richBlock(
kind: .conversationLink,
outcome: feedback == .copied ? .acted : .rejected,
action: .copyLink
)
)
shareLinkFeedback = feedback
shareLinkFeedbackGeneration += 1
let generation = shareLinkFeedbackGeneration
DispatchQueue.main.asyncAfter(deadline: .now() + ConversationShareLinkFeedback.displaySeconds) {
guard shareLinkFeedbackGeneration == generation else { return }
shareLinkFeedback = nil
}
}
}
private func openConversation() {
guard !isOpening else { return }
isOpening = true
let resolutionGeneration = navigation.beginConversationLinkResolution()
Task { @MainActor in
defer { isOpening = false }
do {
let conversation = try await APIClient.shared.getConversation(id: conversationID)
guard
let conversation = ChatFirstConversationLinkPolicy.validatedConversation(
conversation,
requestedID: conversationID
)
else {
throw URLError(.cannotParseResponse)
}
guard
navigation.completeConversationLinkResolution(
conversation: conversation,
generation: resolutionGeneration)
else { return }
AnalyticsManager.shared.chatFirst(
.richBlock(kind: .conversationLink, outcome: .acted, action: .open)
)
} catch {
isUnavailable = true
AnalyticsManager.shared.chatFirst(
.richBlock(kind: .conversationLink, outcome: .stalePlaceholder, action: .open)
)
}
}
}
}
/// The detail fetch is authoritative for a conversation link. Keep a small
/// pure policy around the ID check so malformed or mismatched responses take
/// the same unavailable path as a failed request instead of opening a nearby
/// paginated row.
enum ChatFirstConversationLinkPolicy {
static func validatedConversation(
_ conversation: ServerConversation?,
requestedID: String
) -> ServerConversation? {
guard let conversation, conversation.id == requestedID else { return nil }
return conversation
}
}
/// Where a chat citation for a conversation opens, decided from the record the
/// server returns for the cited ID.
enum ChatFirstConversationCitationRoute: Equatable {
/// An Omi-device capture. The capture focus routes through the capture
/// archive, which carries the transcript moment into playback.
case captureFocus(momentTs: TimeInterval?)
/// Any other recorded conversation — a desktop or phone session the agent
/// retrieved. Opens as the exact fetched record, which the paginated
/// Conversations list may not currently contain.
case exactRecord
}
extension ChatFirstConversationLinkPolicy {
/// Chat citations name whatever conversation the agent retrieved, but the
/// capture focus resolves only through the archive's strictly source-scoped
/// fetch — routing a non-capture citation there landed the reader on the
/// Conversations list with nothing opened. Let the fetched record's own
/// provenance pick the route instead of the citation's kind alone.
static func citationRoute(
forFetched conversation: ServerConversation?,
requestedID: String,
momentTimestampMs: Int?
) -> ChatFirstConversationCitationRoute? {
guard let conversation = validatedConversation(conversation, requestedID: requestedID) else {
return nil
}
if conversation.isOmiCaptureArchiveRecord {
return .captureFocus(momentTs: momentTimestampMs.map { TimeInterval($0) / 1_000 })
}
return .exactRecord
}
}
struct MemoryLinkView: View {
let memoryID: String
let summary: String
let navigation: ChatFirstShellNavigation
var body: some View {
ChatFirstLinkBlockView(
eyebrow: "Memory",
systemImage: "brain.head.profile",
summary: summary,
actionTitle: "Open in Memories",
isOpening: false,
accessibilityID: "chat-first-memory-\(memoryID)-open"
) {
AnalyticsManager.shared.chatFirst(
.richBlock(kind: .memoryLink, outcome: .acted, action: .open)
)
navigation.open(focus: .memory(id: memoryID))
}
.onAppear {
AnalyticsManager.shared.chatFirst(
.richBlock(kind: .memoryLink, outcome: .rendered, action: .none)
)
}
}
}
// MARK: - Memory review card
/// The `memoryReviewCard` block, rendered as the same rows the daily summary card uses.
///
/// One row view, two arrival paths. The desktop's live path is the summary record — this app has
/// no `day_summary` chat row today — but the block is the contract both shells share, and giving it
/// a second row implementation is how the two surfaces would drift into disagreeing about what a
/// verdict means.
struct MemoryReviewCardView: View {
let summaryID: String
let date: String
let items: [MemoryReviewItem]
var body: some View {
MemoryReviewSection(items: items, source: .chatBlock)
.id("memory-review-block-\(summaryID)-\(date)")
}
}
private struct ChatFirstLinkBlockView: View {
let eyebrow: String
let systemImage: String
let summary: String
let actionTitle: String
let isOpening: Bool
let accessibilityID: String
let action: () -> Void
var recommendedActionItems: [ConversationLinkActionItem] = []
var recommendedActionItemAction: ((ConversationLinkActionItem) -> Void)? = nil
// Optional secondary chip rendered beside the primary destination chip
// (e.g. "Copy share link" beside "Open conversation"). The transient
// status renders on its own caption line under the chips so a long
// confirmation never stretches or clips the chip row.
var secondaryActionTitle: String? = nil
var secondaryActionSystemImage: String = "link"
var isSecondaryBusy: Bool = false
var secondaryAccessibilityID: String = ""
var secondaryHelp: String? = nil
var secondaryAction: (() -> Void)? = nil
// Optional tertiary chip (e.g. "Send to Sarah" beside "Copy share link"),
// same chrome and busy semantics as the secondary chip.
var tertiaryActionTitle: String? = nil
var tertiaryActionSystemImage: String = "paperplane"
var isTertiaryBusy: Bool = false
var tertiaryAccessibilityID: String = ""
var tertiaryHelp: String? = nil
var tertiaryAction: (() -> Void)? = nil
var statusMessage: String? = nil
var statusSystemImage: String? = nil
var statusColor: Color = Ink.secondary
var body: some View {
VStack(alignment: .leading, spacing: OmiSpacing.sm) {
Label(eyebrow, systemImage: systemImage)
.scaledFont(size: OmiType.caption, weight: .semibold)
.foregroundStyle(Ink.secondary)
Text(summary)
.scaledFont(size: OmiType.body, weight: .medium)
.foregroundStyle(Ink.primary)
.fixedSize(horizontal: false, vertical: true)
if !recommendedActionItems.isEmpty {
VStack(alignment: .leading, spacing: OmiSpacing.xs) {
Text("Recommended next steps")
.scaledFont(size: OmiType.micro, weight: .semibold)
.foregroundStyle(Ink.secondary)
ForEach(recommendedActionItems.indices, id: \.self) { index in
recommendedActionItemRow(recommendedActionItems[index])
}
}
}
HStack(spacing: OmiSpacing.sm) {
Button(action: action) {
HStack(spacing: OmiSpacing.xs) {
if isOpening {
ProgressView()
.controlSize(.small)
}
Text(actionTitle)
Image(systemName: "arrow.up.right")
}
.scaledFont(size: OmiType.caption, weight: .semibold)
.foregroundStyle(Ink.primary)
.padding(.horizontal, OmiSpacing.sm)
.padding(.vertical, OmiSpacing.xs)
.glassChip()
}
.buttonStyle(.plain)
.disabled(isOpening)
.accessibilityLabel(actionTitle)
.accessibilityIdentifier(accessibilityID)
if let secondaryActionTitle, let secondaryAction {
Button(action: secondaryAction) {
HStack(spacing: OmiSpacing.xs) {
if isSecondaryBusy {
ProgressView()
.controlSize(.small)
}
Text(secondaryActionTitle)
Image(systemName: secondaryActionSystemImage)
}
.scaledFont(size: OmiType.caption, weight: .semibold)
.foregroundStyle(Ink.primary)
.padding(.horizontal, OmiSpacing.sm)
.padding(.vertical, OmiSpacing.xs)
.glassChip()
}
.buttonStyle(.plain)
.disabled(isSecondaryBusy)
.help(secondaryHelp ?? secondaryActionTitle)
.accessibilityLabel(secondaryHelp ?? secondaryActionTitle)
.accessibilityIdentifier(secondaryAccessibilityID)
}
if let tertiaryActionTitle, let tertiaryAction {
Button(action: tertiaryAction) {
HStack(spacing: OmiSpacing.xs) {
if isTertiaryBusy {
ProgressView()
.controlSize(.small)
}
Text(tertiaryActionTitle)
Image(systemName: tertiaryActionSystemImage)
}
.scaledFont(size: OmiType.caption, weight: .semibold)
.foregroundStyle(Ink.primary)
.padding(.horizontal, OmiSpacing.sm)
.padding(.vertical, OmiSpacing.xs)
.glassChip()
}
.buttonStyle(.plain)
.disabled(isTertiaryBusy)
.help(tertiaryHelp ?? tertiaryActionTitle)
.accessibilityLabel(tertiaryHelp ?? tertiaryActionTitle)
.accessibilityIdentifier(tertiaryAccessibilityID)