forked from BasedHardware/omi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDesktopHomeView.swift
More file actions
1303 lines (1227 loc) · 56.1 KB
/
Copy pathDesktopHomeView.swift
File metadata and controls
1303 lines (1227 loc) · 56.1 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
private struct AnySendableBox: @unchecked Sendable { let value: Any? }
/// Decides whether a persisted capture intent needs its runtime service restored.
///
/// Intent is stored independently from the running services. A fresh launch and a
/// settings sync therefore both need to reconcile the two states instead of using
/// a one-time readiness check as the source of truth.
enum PersistedCaptureLaunchPolicy {
static func shouldStartTranscription(
intentEnabled: Bool,
isTranscribing: Bool,
micPermissionAuthorized: Bool
) -> Bool {
// Restores run on launch/reactivation/key-load/sync; without a mic grant an
// attempted start would raise the TCC sheet (the skip-mic loop) or bounce a
// denied alert. The intent waits for an explicit Listen/Grant action instead.
intentEnabled && !isTranscribing && micPermissionAuthorized
}
static func shouldStartScreenAnalysis(intentEnabled: Bool, isMonitoring: Bool) -> Bool {
intentEnabled && !isMonitoring
}
}
// MARK: - NSHostingView sizingOptions access
/// Protocol to access sizingOptions on any NSHostingView<Content> regardless of the generic parameter.
/// NSHostingView is generic so we can't cast to it without knowing Content.
/// This protocol + extension lets us access sizingOptions through existential dispatch.
@MainActor
private protocol HostingSizingConfigurable: AnyObject {
var sizingOptions: NSHostingSizingOptions { get set }
}
extension NSHostingView: HostingSizingConfigurable {}
struct DesktopHomeView: View {
private static let pageNavigationAnimation = Animation.easeOut(duration: 0.08)
@EnvironmentObject private var appState: AppState
@StateObject private var viewModelContainer = ViewModelContainer()
/// The Chat-first shell owns typed navigation at the root, never through legacy
/// sidebar indices. It persists only route/collapse state, not enrollment.
/// The one navigation owner, shared with the auxiliary Chat surfaces (task
/// panel, floating/notch) so a content block tapped there routes *this* shell
/// rather than a second instance nothing renders.
@ObservedObject private var chatFirstNavigation = ChatFirstShellNavigation.shared
@ObservedObject private var authState = AuthState.shared
@ObservedObject private var apiKeyService = APIKeyService.shared
@ObservedObject private var updatePolicyManager = DesktopUpdatePolicyManager.shared
@ObservedObject private var accountCutoverControl = AccountCutoverControlManager.shared
@ObservedObject private var automationPresentationCoordinator =
DesktopAutomationPresentationCoordinator.shared
@AppStorage("currentTierLevel") private var currentTierLevel = 0
@AppStorage("onboardingStep") private var onboardingStep = 0
@AppStorage("onboardingFurthestStep") private var onboardingFurthestStep = 0
@AppStorage("onboardingJustCompleted") private var onboardingJustCompleted = false
@AppStorage(MemoryHubDestination.storageKey) private var memoryDestinationRawValue =
MemoryHubDestination.memories.rawValue
/// Reference instant for the top bar's "new since you were last here" counts —
/// updated to now whenever Omi resigns front (see the didResignActive handler).
@AppStorage("topBarNewSince") private var topBarNewSinceRaw: Double = 0
// Settings sidebar state
@State private var selectedSettingsSection: SettingsContentView.SettingsSection = .general
@State private var highlightedSettingId: String? = nil
@State private var showTryAskingPopup = false
@State private var logoPulse = false
@State private var lastActivationRefresh = Date.distantPast
@State private var didScheduleAgentVMProvisioning = false
@State private var proactiveMonitoringStartGate = RetryableDelayedStartGate()
@State private var isWaitingForScreenAnalysisKeys = false
// Anchor for the proactive-monitoring warmup budget. Captured at view
// creation (≈ launch) so the delay is spent once per session, not once per
// trigger — see StartupWarmupPolicy.remainingProactiveAssistantsStartDelay.
@State private var proactiveMonitoringWarmupAnchor = Date()
@State private var didScheduleConversationWarmup = false
@State private var initialFileIndexingBackfill = DelayedFileIndexingBackfillState()
@State private var automationPresentationReadinessGate =
DesktopAutomationPresentationReadinessGate()
/// Server-authoritative capability for the one shell. It never decides which
/// shell mounts — only whether the capability-gated kernel features engage.
@State private var chatFirstCapability = ChatFirstCapabilitySample()
// Pre-loaded hero logo to avoid NSImage init crashes during SwiftUI body evaluation
private static let heroLogoImage: NSImage? = {
guard let url = Bundle.resourceBundle.url(forResource: "herologo", withExtension: "png"),
let data = try? Data(contentsOf: url)
else { return nil }
return NSImage(data: data)
}()
private var shouldShowAuthEntryShell: Bool {
authState.isRestoringAuth || authState.sessionPhase == .recoveryRequired || !authState.isSignedIn
|| !hasCompletedOnboardingAtAuthorityRead
}
@ViewBuilder
private var authEntryShell: some View {
if authState.isRestoringAuth {
TransparentWindowStatusPanel {
VStack(spacing: OmiSpacing.md) {
ProgressView()
.controlSize(.small)
.tint(Ink.secondary)
Text("Restoring your session…")
.inkStyle(.prose, color: Ink.secondary)
}
}
.onAppear {
log("DesktopHomeView: Showing auth loading splash")
}
} else if authState.sessionPhase == .recoveryRequired {
SessionRecoveryView()
.onAppear {
log("DesktopHomeView: Showing recoverable auth state")
}
} else if !authState.isSignedIn {
SignInView(authState: authState)
.onAppear {
log("DesktopHomeView: Showing SignInView (not signed in)")
}
} else if shouldSkipOnboarding() {
TransparentWindowStatusPanel {
VStack(spacing: OmiSpacing.md) {
ProgressView()
.controlSize(.small)
.tint(Ink.secondary)
Text("Finishing setup…")
.inkStyle(.prose, color: Ink.secondary)
}
}
.onAppear {
log("DesktopHomeView: --skip-onboarding flag detected, skipping onboarding")
appState.hasCompletedOnboarding = true
}
} else {
SBOnboardingView(
appState: appState,
chatProvider: viewModelContainer.chatProvider,
importConnectorStatusStore: viewModelContainer.homeStatusStore.connectorStatusStore,
onComplete: nil
)
.onAppear {
log("DesktopHomeView: Showing SBOnboardingView (signed in, not onboarded)")
}
}
}
// State 3: Signed in and onboarded.
private var mainContentPresentation: some View {
mainContent
.opacity(viewModelContainer.isInitialLoadComplete ? 1 : 0)
.overlay {
if appState.showUsageLimitPopup {
UsageLimitPopupView(
reason: appState.usageLimitReason,
onUpgrade: {
appState.showUsageLimitPopup = false
selectedSettingsSection = .planUsage
// Plan and Usage now lives below Account on the merged
// "Account & Plan" page — scroll straight to the plan card.
highlightedSettingId = "planusage.current"
OmiMotion.withGated(Self.pageNavigationAnimation) {
navigateToLegacyDestination(.settings)
}
},
onDismiss: {
appState.showUsageLimitPopup = false
},
onBringYourOwnKeys: {
appState.showUsageLimitPopup = false
selectedSettingsSection = .advanced
OmiMotion.withGated(Self.pageNavigationAnimation) {
navigateToLegacyDestination(.settings)
}
}
)
}
}
.overlay(alignment: .top) {
if let policy = updatePolicyManager.visiblePolicy, !policy.isRequired {
DesktopUpdatePolicyBanner(
policy: policy,
onDownload: { updatePolicyManager.openDownload(policy) },
onDismiss: { updatePolicyManager.dismiss(policy) }
)
.padding(.top, OmiSpacing.md)
.padding(.horizontal, OmiSpacing.xl)
.transition(.move(edge: .top).combined(with: .opacity))
}
}
}
private var mainContentStartupLifecycle: some View {
mainContentPresentation
.onReceive(NotificationCenter.default.publisher(for: .showUsageLimitPopup)) { notification in
let reason = notification.userInfo?["reason"] as? String ?? ""
appState.triggerUsageLimitPopup(reason: reason)
}
.onAppear {
log("DesktopHomeView: Showing mainContent (signed in and onboarded)")
// The first-use popup is armed by the same guidance policy for every
// account now that there is one shell.
if PostOnboardingPromptSuggestions.shouldArmPopup() {
showTryAskingPopup = true
}
updatePolicyManager.refresh(force: true)
// Permissions and update policy remain reachable while cutover fences
// product traffic.
appState.checkAllPermissions()
}
.task(id: accountCutoverControl.productShellAdmissionToken) {
await DesktopHomeSignedInStartup.runProductServicesIfAdmitted(
appState: appState,
chatProvider: viewModelContainer.chatProvider,
scheduleInitialFileIndexing: scheduleInitialFileIndexing,
restorePersistedCaptureServices: restorePersistedCaptureServices(reason:)
)
await DesktopHomeSignedInStartup.loadDataIfAdmitted(
loadAllData: { await viewModelContainer.loadAllData() },
scheduleConversationWarmup: scheduleConversationWarmup,
scheduleAgentVMProvisioning: scheduleAgentVMProvisioning
)
}
// Refresh conversations when app becomes active (e.g. switching back from another app)
.onReceive(
NotificationCenter.default.publisher(for: NSApplication.didBecomeActiveNotification)
) { _ in
updatePolicyManager.refresh()
guard AccountCutoverControlManager.shared.isProductShellAdmitted else { return }
// Cooldown: only refresh conversations if last activation was 60+ seconds ago
let now = Date()
if PollingConfig.shouldAllowActivationRefresh(now: now, lastRefresh: lastActivationRefresh) {
lastActivationRefresh = now
Task { await appState.refreshConversations() }
}
// Reconcile persisted intent after returning from System Settings or
// after a runtime service stopped while the app was inactive.
restorePersistedCaptureServices(reason: "app active")
}
.onChange(of: apiKeyService.isLoaded) { _, loaded in
guard loaded, AccountCutoverControlManager.shared.isProductShellAdmitted else { return }
log("DesktopHomeView: API keys loaded — retrying deferred services")
restorePersistedCaptureServices(reason: "key load")
}
.onReceive(NotificationCenter.default.publisher(for: .assistantSettingsDidSyncFromServer)) { _ in
guard AccountCutoverControlManager.shared.isProductShellAdmitted else { return }
reconcileCaptureServicesAfterSettingsSync()
}
// Cmd+R: refresh all data (conversations, chat, tasks, memories)
.onReceive(NotificationCenter.default.publisher(for: .refreshAllData)) { _ in
guard AccountCutoverControlManager.shared.isProductShellAdmitted else { return }
Task { await appState.refreshConversations() }
}
}
private var mainContentWithLifecycle: some View {
mainContentStartupLifecycle
// On sign-out: reset @AppStorage-backed onboarding flag and stop transcription.
// hasCompletedOnboarding must be set here (in a View) because @AppStorage
// on ObservableObject caches internally and ignores UserDefaults.removeObject().
// Stopping transcription here prevents FOREIGN KEY errors from an old
// transcription session writing to a new user's database.
.onReceive(NotificationCenter.default.publisher(for: .userDidSignOut)) { _ in
log(
"DesktopHomeView: userDidSignOut — resetting hasCompletedOnboarding and stopping transcription"
)
chatFirstCapability.ownerDidChange(to: nil)
resetSessionScopedStartupWarmups()
appState.conversationRepository.reset()
appState.folders = []
appState.selectedFolderId = nil
appState.selectedDateFilter = nil
appState.showStarredOnly = false
appState.totalConversationsCount = nil
appState.conversationsError = nil
appState.isLoadingConversations = false
appState.isLoadingFolders = false
appState.hasCompletedOnboarding = false
appState.stopTranscription()
}
.onReceive(NotificationCenter.default.publisher(for: .resetOnboardingRequested)) { _ in
log(
"DesktopHomeView: resetOnboardingRequested — clearing live onboarding state for current app"
)
resetSessionScopedStartupWarmups()
appState.hasCompletedOnboarding = false
onboardingStep = 0
onboardingFurthestStep = 0
onboardingJustCompleted = false
appState.stopTranscription()
}
.onReceive(NotificationCenter.default.publisher(for: NSApplication.willTerminateNotification)) { _ in
log("DesktopHomeView: app terminating — cancelling startup warmups")
resetSessionScopedStartupWarmups()
}
// Periodic file re-scan (every 3 hours)
.task {
while !Task.isCancelled {
try? await Task.sleep(for: .seconds(3 * 60 * 60))
guard !Task.isCancelled else { break }
guard !AppBuild.usesLazyDevPermissions else { continue }
guard UserDefaults.standard.bool(forKey: .hasCompletedFileIndexing) else {
continue
}
log("DesktopHomeView: Triggering background file rescan")
await FileIndexerService.shared.backgroundRescan(
fullDiskAccessGranted: await appState.refreshFullDiskAccess())
}
}
}
var body: some View {
Group {
if shouldShowAuthEntryShell {
authEntryShell
} else {
ZStack {
// After onboarding completes, navigate to Tasks page
Color.clear
.frame(width: 0, height: 0)
.onAppear {
if UserDefaults.standard.bool(forKey: "onboardingJustCompleted") {
UserDefaults.standard.removeObject(forKey: "onboardingJustCompleted")
navigateAfterOnboarding()
}
}
mainContentWithLifecycle
// The shell mounts immediately; the capability resolves alongside it and
// only decides whether capability-gated features engage.
.task(id: RuntimeOwnerIdentity.currentOwnerId() ?? "missing-owner") {
await resolveChatFirstCapabilityIfNeeded()
}
if !viewModelContainer.isInitialLoadComplete {
TransparentWindowStatusPanel {
VStack(spacing: OmiSpacing.xxl) {
if let nsImage = Self.heroLogoImage {
Image(nsImage: nsImage)
.resizable()
.scaledToFit()
.frame(width: 72, height: 72)
.scaleEffect(logoPulse ? 1.08 : 1.0)
.opacity(logoPulse ? 1.0 : 0.7)
.omiAnimation(
.easeInOut(duration: 1.2).repeatForever(autoreverses: true),
value: logoPulse
)
.onAppear { logoPulse = true }
}
Text(viewModelContainer.initStatusMessage)
.inkStyle(.prose, color: Ink.secondary)
ProgressView()
.scaleEffect(0.8)
.tint(Ink.secondary)
}
}
.transition(.opacity.animation(OmiMotion.gated(.easeOut(duration: 0.3))))
}
if let policy = updatePolicyManager.visiblePolicy, policy.isRequired {
// The heaviest dim in the app, and the one that most needed bounding: mounted here it is
// over the whole (invisible) window, so full-bleed it was a black rectangle on the
// wallpaper. No dismiss gesture — a required update is required — but it still blocks.
ShellModalScrim(opacity: ShellModalScrimLayout.blocking)
.zIndex(20)
DesktopRequiredUpdatePrompt(
policy: policy,
onDownload: { updatePolicyManager.openDownload(policy) }
)
.zIndex(21)
} else {
AccountCutoverBlockingOverlay.host(onOpenDownload: { updatePolicyManager.openDownload($0) })
}
}
}
}
.environment(\.colorScheme, .light) // No window ground since `ShellWindowChrome`; each panel is its own glass.
.background(ShellWindowAttachment().frame(width: 0, height: 0))
.frame(minWidth: DesktopWindowLayoutPolicy.width, minHeight: DesktopWindowLayoutPolicy.height)
.preferredColorScheme(.light) // Glass is pinned light — see `InkGlass`. Deliberate, not a bug.
.tint(Ink.accent)
.onAppear {
reconcileOnboardingCompletionOwner()
log(
"DesktopHomeView: View appeared - isSignedIn=\(authState.isSignedIn), hasCompletedOnboarding=\(appState.hasCompletedOnboarding)"
)
// Drive the notch "moments" (live receipts + conversation-end) off real state.
NotchMomentsCoordinator.shared.start(appState: appState)
// By default, every @Published change triggers
// updateWindowContentSizeExtremaIfNecessary() → minSize() → sizeThatFits()
// which traverses the ENTIRE view tree (~200 samples per window per trigger).
// Removing .minSize from sizingOptions prevents this full-tree traversal.
// The window's min size is enforced at the AppKit level instead.
enforceMainWindowMinimumSize()
// SwiftUI's automatic resizability later re-derives the window min from content
// extrema and resets our pin, after which the window can be dragged small enough
// to hide content. Re-pin on every live resize so AppKit keeps clamping the drag.
installMinimumSizeGuardIfNeeded()
reportAutomationState()
handleAutomationPresentationReadinessChange(viewModelContainer.isInitialLoadComplete)
}
.onChange(of: currentTierLevel) { _, _ in
reportAutomationState()
}
.onChange(of: chatFirstNavigation.route) { _, _ in
// Route nav recreates the content hosting view with default sizingOptions, which
// resets the window min — re-pin + re-disable to hold the minimum.
enforceMainWindowMinimumSize()
}
.onChange(of: automationPresentationCoordinator.activeCommand?.generation) { _, _ in
guard
let command = automationPresentationReadinessGate.commandForConsumption(
automationPresentationCoordinator.activeCommand)
else { return }
handleAutomationPresentationCommand(command)
}
.onChange(of: viewModelContainer.isInitialLoadComplete) { _, isReady in
handleAutomationPresentationReadinessChange(isReady)
}
.onChange(of: selectedSettingsSection) { _, _ in reportAutomationState() }
.onChange(of: highlightedSettingId) { _, _ in reportAutomationState() }
.onChange(of: authState.isSignedIn) { _, _ in reportAutomationState() }
.onChange(of: authState.isRestoringAuth) { _, _ in reportAutomationState() }
.onChange(of: appState.hasCompletedOnboarding) { _, _ in reportAutomationState() }
.onReceive(NotificationCenter.default.publisher(for: .runtimeOwnerDidChange)) { _ in
reconcileOnboardingCompletionOwner()
chatFirstCapability.ownerDidChange(to: RuntimeOwnerIdentity.currentOwnerId())
// The provider's owner-bound gate rejects the previous sample for this
// owner; no replacement sample is persisted or inferred locally.
reportAutomationState()
}
.onReceive(NotificationCenter.default.publisher(for: NSApplication.didBecomeActiveNotification)) { _ in
enforceMainWindowMinimumSize()
reportAutomationState()
// First-run seed so the counter doesn't count the entire backlog as "new".
seedTopBarNewSinceIfNeeded()
}
.onReceive(NotificationCenter.default.publisher(for: NSApplication.didResignActiveNotification)) { _ in
reportAutomationState()
// Mark the moment Omi went to the background; anything created after this
// shows in the top bar's "new since you were last here" counter.
topBarNewSinceRaw = Date().timeIntervalSince1970
}
.onReceive(NotificationCenter.default.publisher(for: .desktopAutomationNavigateRequested)) {
notification in
handleAutomationNavigation(notification)
}
.onReceive(NotificationCenter.default.publisher(for: .navigateToChat)) { _ in
// The global shortcut / notch "Ask Omi" opens the continuous chat, which
// is the shell's Chat route. Selecting it is idempotent, so no re-emit
// loop guard is needed.
chatFirstNavigation.selectPrimary(.chat)
}
// "Continue in Omi" from the floating bar. The one shell has no second chat
// panel to consume the pending request on its behalf.
.onReceive(NotificationCenter.default.publisher(for: .openMainChatRequested)) { _ in
handleMainChatRequest()
}
}
private func reconcileOnboardingCompletionOwner() {
guard
OnboardingFlow.reconcileCompletionOwner(
currentOwnerID: RuntimeOwnerIdentity.currentOwnerId()) == .resetForDifferentOwner
else { return }
onboardingStep = 0
onboardingFurthestStep = 0
onboardingJustCompleted = false
appState.hasCompletedOnboarding = false
}
private func handleMainChatRequest() {
guard MainChatNavigationRequestStore.shared.consume() else { return }
chatFirstNavigation.selectPrimary(.chat, origin: .chatDeeplink)
}
private func enforceMainWindowMinimumSize() {
DispatchQueue.main.async {
// Appearance belongs to `WindowGlass.wear(_:as:)`; stamping one here unpinned the glass.
for window in NSApp.windows where window.title.lowercased().hasPrefix("omi") {
Self.pinShellWindowSizeLimits(window)
Self.disableMinSizeComputation(in: window)
}
}
}
/// Re-pin the window min and max on every live resize. SwiftUI's `.automatic` window
/// resizability periodically recomputes content-size extrema and overwrites the
/// one-shot pin from `enforceMainWindowMinimumSize()`, after which the window can be
/// dragged small enough to hide content or wide enough to recreate the invisible
/// click border. Observing `didResize` and re-pinning keeps AppKit clamping the live
/// drag. Installed once for the app's lifetime.
private static var minimumSizeGuardInstalled = false
private func installMinimumSizeGuardIfNeeded() {
guard !Self.minimumSizeGuardInstalled else { return }
Self.minimumSizeGuardInstalled = true
NotificationCenter.default.addObserver(
forName: NSWindow.didResizeNotification, object: nil, queue: .main
) { notification in
let objectBox = AnySendableBox(value: notification.object)
MainActor.assumeIsolated {
guard let window = objectBox.value as? NSWindow,
window.title.lowercased().hasPrefix("omi")
else { return }
Self.pinShellWindowSizeLimits(window, resizeFrame: false)
}
}
}
/// Pins the shell between the destination minimum and the visible display frame.
private static func pinShellWindowSizeLimits(_ window: NSWindow, resizeFrame: Bool = true) {
let minimumContentSize = DesktopWindowLayoutPolicy.minimumContentSize
let maximumContentSize = DesktopWindowLayoutPolicy.maximumContentSize(for: window)
window.contentMinSize = minimumContentSize
window.minSize = window.frameRect(forContentRect: NSRect(origin: .zero, size: minimumContentSize)).size
window.contentMaxSize = maximumContentSize
window.maxSize = window.frameRect(forContentRect: NSRect(origin: .zero, size: maximumContentSize)).size
guard resizeFrame else { return }
let currentContentSize = window.contentView?.bounds.size ?? window.contentLayoutRect.size
var frame = window.frame
var changed = false
let widthDelta = max(0, minimumContentSize.width - currentContentSize.width)
let heightDelta = max(0, minimumContentSize.height - currentContentSize.height)
if widthDelta > 0 || heightDelta > 0 {
frame.size.width += widthDelta
frame.size.height += heightDelta
frame.origin.y -= heightDelta
changed = true
}
let maxFrameWidth = window.frameRect(
forContentRect: NSRect(
origin: .zero,
size: NSSize(width: maximumContentSize.width, height: currentContentSize.height))
).width
if frame.size.width > maxFrameWidth {
let extra = frame.size.width - maxFrameWidth
frame.origin.x += extra / 2
frame.size.width = maxFrameWidth
changed = true
}
if changed {
window.setFrame(frame, display: true, animate: false)
}
}
/// Recursively find all NSHostingViews in a window and set sizingOptions to [],
/// disabling ALL size computations to prevent full-tree sizeThatFits() traversals.
/// Window min/max sizes are enforced at the AppKit level via NSWindow.minSize instead.
/// NOTE: ClickThroughHostingView is excluded because it wraps the sidebar and needs
/// intrinsicContentSize for SwiftUI's .fixedSize() layout to compute the correct width.
private static func disableMinSizeComputation(in window: NSWindow) {
func visit(_ view: NSView) {
if let hosting = view as? any HostingSizingConfigurable {
// Skip ClickThroughHostingView — it's an NSViewRepresentable boundary
// that needs intrinsicContentSize for the sidebar's .fixedSize() to work.
let typeName = String(describing: type(of: view))
guard !typeName.contains("ClickThroughHostingView") else {
// Still visit children
for subview in view.subviews { visit(subview) }
return
}
let before = hosting.sizingOptions
if before != [] {
hosting.sizingOptions = []
}
}
for subview in view.subviews {
visit(subview)
}
}
if let contentView = window.contentView {
visit(contentView)
}
}
private func seedTopBarNewSinceIfNeeded() {
let currentValue = topBarNewSinceRaw
guard currentValue == 0 else { return }
topBarNewSinceRaw = Date().timeIntervalSince1970
}
private var currentAppStateLabel: String {
if authState.isRestoringAuth { return "restoring_auth" }
if authState.sessionPhase == .recoveryRequired { return "auth_recovery" }
if !authState.isSignedIn { return "signed_out" }
if !appState.hasCompletedOnboarding { return "onboarding" }
return "main"
}
/// Preserve the existing AppStorage winner while observing disagreement at
/// the actual product/onboarding gate. This read still runs when the completed
/// flag prevents SBOnboardingModel from mounting.
private var hasCompletedOnboardingAtAuthorityRead: Bool {
let completed = appState.hasCompletedOnboarding
guard completed else { return false }
let savedRaw = UserDefaults.standard.integer(forKey: SBOnboardingModel.resumeStepKey)
if savedRaw > SBOnboardingModel.Step.promise.rawValue,
SBOnboardingModel.Step(rawValue: savedRaw) != nil
{
DesktopDiagnosticsManager.shared.recordStateAuthoritySignal(
seam: .onboardingSetupState,
from: "completed_flag",
to: "persisted_resume",
direction: "completed_flag_with_resume_state")
}
if viewModelContainer.chatProvider.isOnboarding {
DesktopDiagnosticsManager.shared.recordStateAuthoritySignal(
seam: .onboardingSetupState,
from: "completed_flag",
to: "setup_journal",
direction: "completed_flag_with_active_journal")
}
return true
}
private func reportAutomationState() {
guard DesktopAutomationLaunchOptions.isEnabled else { return }
let currentWindow = NSApp.windows.first(where: {
$0.title.lowercased().hasPrefix("omi") && $0.isVisible
})
let chatFirstRoute = chatFirstNavigation.route
let snapshot = DesktopAutomationSnapshot(
bridgeEnabled: true,
bridgePort: DesktopAutomationLaunchOptions.port,
bundleIdentifier: Bundle.main.bundleIdentifier ?? "unknown",
appState: currentAppStateLabel,
selectedTab: chatFirstRoute.title,
selectedTabIndex: nil,
selectedSettingsSection: chatFirstRoute == .more(.settings)
? selectedSettingsSection.rawValue : nil,
highlightedSettingId: highlightedSettingId,
// There is one shell and it renders no Home stage: `DashboardPage` was its
// only writer and no longer exists. `nil` says exactly that — never a
// plausible-looking default a flow could wait on forever.
homeMode: nil,
// Pinned: the app has one shell. Retained in the snapshot because e2e
// flows and the navigation-visibility policy read it.
shellVariant: DesktopAutomationSnapshot.singleShellVariant,
chatFirstRoute: chatFirstRoute.stableName,
visibleChatFirstRoute: chatFirstNavigation.visibleRoute?.stableName,
pendingFocusKind: chatFirstNavigation.pendingFocus?.stableName,
acknowledgedFocusKind: chatFirstNavigation.lastAcknowledgedFocusKind,
focusedEntityID: chatFirstNavigation.focusedEntityID,
isFocusedEntityAcknowledged: chatFirstNavigation.isFocusedEntityAcknowledged,
showsPrimarySidebar: false,
isSidebarCollapsed: chatFirstNavigation.isSidebarCollapsed,
hasCompletedOnboarding: appState.hasCompletedOnboarding,
isSignedIn: authState.isSignedIn,
isRestoringAuth: authState.isRestoringAuth,
isAppActive: NSApp.isActive,
mainWindowTitle: currentWindow?.title,
floatingBarVisible: FloatingControlBarManager.shared.automationState.isVisible,
askOmiOpen: OpenAskOmiAutomation.isComposerPresented(chatFirstNavigation),
askOmiFocused: OpenAskOmiAutomation.isComposerFocused(chatFirstNavigation),
floatingBarFrame: FloatingControlBarManager.shared.automationState.frame,
floatingBarVoiceListening: FloatingControlBarManager.shared.automationState.isVoiceListening,
floatingBarVoiceDictating: FloatingControlBarManager.shared.automationState.isVoiceDictating,
floatingBarVoiceResponseActive: FloatingControlBarManager.shared.automationState.isVoiceResponseActive,
floatingBarUsesNotchIsland: FloatingControlBarManager.shared.automationState.usesNotchIsland,
updatedAt: ISO8601DateFormatter().string(from: Date())
)
DesktopAutomationStateStore.shared.update(snapshot)
}
private func handleAutomationNavigation(_ notification: Notification) {
guard DesktopAutomationLaunchOptions.isEnabled else { return }
guard let target = notification.userInfo?["target"] as? String else { return }
let settingsSectionRaw = notification.userInfo?["settingsSection"] as? String
let settingId = notification.userInfo?["highlightedSettingId"] as? String
let activateApp = notification.userInfo?["activateApp"] as? Bool ?? false
if activateApp {
NSApp.activate()
if let window = NSApp.windows.first(where: { $0.title.lowercased().hasPrefix("omi") }) {
window.makeKeyAndOrderFront(nil)
}
}
if let sectionRaw = settingsSectionRaw {
// Tolerant match (SET-01): omi-ctl sends the caller's casing verbatim (docs use
// lowercase, raw values are Title Case), so a strict rawValue init silently left
// navigation on General for every sub-section command.
if let section = SettingsContentView.SettingsSection.automationMatch(sectionRaw) {
selectedSettingsSection = section
} else {
log("AutomationNavigation: unknown settings section '\(sectionRaw)'")
}
}
highlightedSettingId = settingId
if target.lowercased().replacingOccurrences(of: "-", with: "_") == "rewind" {
navigateToLegacyDestination(.rewind)
reportAutomationState()
return
}
// `navigate help` used to name a "Help from Founder" page that no shell has
// mounted for a long time, so the bridge resolved a title and then timed out
// waiting for it. Settings → About is where getting help from a person
// actually lives (the Community / Join Discord card), so the name now lands
// on a destination that exists.
if ChatFirstRoute.isHelpAutomationTarget(target), settingsSectionRaw == nil {
selectedSettingsSection = .about
}
if let route = ChatFirstRoute.automationVisibilityDestination(named: target) {
switch route {
case .more(let page):
chatFirstNavigation.selectMore(page)
default:
chatFirstNavigation.selectPrimary(route)
}
}
reportAutomationState()
}
private func handleAutomationPresentationCommand(
_ command: DesktopAutomationPresentationCommand
) {
if DesktopAutomationWindowPresentation.currentMode != .quiet {
NSApp.activate()
if let window = NSApp.windows.first(where: { $0.title.lowercased().hasPrefix("omi") }) {
window.makeKeyAndOrderFront(nil)
}
}
navigateToLegacyDestination(.apps)
reportAutomationState()
}
private func handleAutomationPresentationReadinessChange(_ isReady: Bool) {
guard
let command = automationPresentationReadinessGate.transition(
to: isReady,
activeCommand: automationPresentationCoordinator.activeCommand)
else { return }
handleAutomationPresentationCommand(command)
}
/// Update store auto-refresh based on which page is visible
/// On launch, if the user quit with the task chat panel open, macOS restores the
/// expanded window frame but the chat panel itself is not shown. Shrink the window
/// back to its pre-chat width so the layout isn't unexpectedly wide.
private func restorePreChatWindowWidth() {
let key = "tasksPreChatWindowWidth"
let saved = UserDefaults.standard.double(forKey: key)
guard saved > 0 else { return }
// Reset the persisted value immediately so TasksPage won't double-shrink
UserDefaults.standard.set(Double(0), forKey: key)
// Delay slightly so the window is fully visible
DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
guard let window = NSApp.windows.first(where: { $0.title.hasPrefix("Omi") && $0.isVisible })
else { return }
var frame = window.frame
frame.size.width = saved
window.setFrame(frame, display: true)
}
}
private func resetSessionScopedStartupWarmups() {
viewModelContainer.resetStartupState()
didScheduleConversationWarmup = false
didScheduleAgentVMProvisioning = false
proactiveMonitoringStartGate.finishAttempt()
initialFileIndexingBackfill.releaseReservation()
}
private func scheduleAgentVMProvisioning() {
guard !didScheduleAgentVMProvisioning else { return }
didScheduleAgentVMProvisioning = true
let scheduled = viewModelContainer.scheduleSessionWarmup(
id: .agentVMProvisioning,
delay: StartupWarmupPolicy.agentVMProvisioningDelay,
onCancel: { didScheduleAgentVMProvisioning = false },
operation: {
await AgentVMService.shared.ensureProvisioned()
})
if !scheduled { didScheduleAgentVMProvisioning = false }
}
private func scheduleConversationWarmup() {
guard !didScheduleConversationWarmup else { return }
didScheduleConversationWarmup = true
let scheduled = viewModelContainer.scheduleSessionWarmup(
id: .conversationWarmup,
delay: StartupWarmupPolicy.conversationWarmupDelay,
onCancel: { didScheduleConversationWarmup = false },
operation: {
async let conversations: Void = loadConversationsIfNeeded()
async let folders: Void = loadFoldersIfNeeded()
// Warm memories + tasks too so the top bar's new-item counter has data
// even before those tabs are visited.
async let memories: Void = viewModelContainer.memoriesViewModel.loadMemoriesIfNeeded()
async let tasks: Void = viewModelContainer.tasksStore.loadTasksIfNeeded()
_ = await (conversations, folders, memories, tasks)
})
if !scheduled { didScheduleConversationWarmup = false }
}
private func loadConversationsIfNeeded() async {
guard appState.conversations.isEmpty else { return }
await appState.loadConversations()
}
private func loadFoldersIfNeeded() async {
guard appState.folders.isEmpty else { return }
await appState.loadFolders()
}
private func scheduleInitialFileIndexing() {
guard
initialFileIndexingBackfill.reserveIfNeeded(
hasCompletedBackfill: UserDefaults.standard.bool(forKey: "hasCompletedFileIndexing"))
else { return }
let sessionScope = StartupWarmupSessionScope(
userId: UserDefaults.standard.string(forKey: .authUserId))
let scheduled = viewModelContainer.scheduleSessionWarmup(
id: .initialFileIndexing,
delay: StartupWarmupPolicy.initialFileIndexingDelay,
onCancel: { initialFileIndexingBackfill.releaseReservation() },
operation: {
log("DesktopHomeView: Running delayed background file scan for existing user")
await FileIndexerService.shared.backgroundRescan(
fullDiskAccessGranted: await appState.refreshFullDiskAccess())
guard !Task.isCancelled,
sessionScope.matches(
currentUserId: UserDefaults.standard.string(forKey: .authUserId),
isSignedIn: AuthState.shared.isSignedIn)
else {
initialFileIndexingBackfill.releaseReservation()
return
}
initialFileIndexingBackfill.markScanCompleted()
if initialFileIndexingBackfill.shouldMarkComplete {
UserDefaults.standard.set(true, forKey: .hasCompletedFileIndexing)
log(
"DesktopHomeView: Marked existing-user file indexing backfill complete after background scan returned"
)
}
})
if !scheduled { initialFileIndexingBackfill.releaseReservation() }
}
private func scheduleProactiveMonitoringStart(reason: String) {
guard proactiveMonitoringStartGate.reserve() else { return }
let delay = StartupWarmupPolicy.remainingProactiveAssistantsStartDelay(
elapsedSinceLaunch: Date().timeIntervalSince(proactiveMonitoringWarmupAnchor))
log(
"DesktopHomeView: Scheduling screen analysis start in \(String(format: "%.1f", delay))s (\(reason))"
)
let scheduled = viewModelContainer.scheduleSessionWarmup(
id: .proactiveAssistantsStart,
delay: delay,
onCancel: { proactiveMonitoringStartGate.finishAttempt() },
operation: {
let plugin = ProactiveAssistantsPlugin.shared
guard AssistantSettings.shared.screenAnalysisEnabled, !plugin.isMonitoring else {
proactiveMonitoringStartGate.finishAttempt()
return
}
guard APIKeyService.keysAvailable else {
proactiveMonitoringStartGate.finishAttempt()
log("DesktopHomeView: Screen analysis still deferred after \(reason) — API keys not yet loaded")
return
}
plugin.startMonitoring { success, error in
Task { @MainActor in
proactiveMonitoringStartGate.finishAttempt()
if success {
log("DesktopHomeView: Screen analysis started (\(reason), delayed)")
} else {
log(
"DesktopHomeView: Screen analysis failed to start (\(reason)): \(error ?? "unknown") — setting remains enabled for next launch"
)
}
}
}
})
if !scheduled { proactiveMonitoringStartGate.finishAttempt() }
}
private func restorePersistedCaptureServices(reason: String) {
let settings = AssistantSettings.shared
// Fresh TCC answer before deciding whether an automatic start is allowed.
appState.checkMicrophonePermission()
if PersistedCaptureLaunchPolicy.shouldStartTranscription(
intentEnabled: settings.audioRecordingMode != .off,
isTranscribing: appState.isTranscribing,
micPermissionAuthorized: appState.hasMicrophonePermission
) {
log("DesktopHomeView: Restoring transcription from persisted intent (\(reason))")
// Local transcription does not require remote API keys. AppState owns the
// permission and provider checks, so it remains the single start boundary.
appState.startTranscription(userInitiated: false)
}
let plugin = ProactiveAssistantsPlugin.shared
guard
PersistedCaptureLaunchPolicy.shouldStartScreenAnalysis(
intentEnabled: settings.screenAnalysisEnabled,
isMonitoring: plugin.isMonitoring
)
else { return }
guard APIKeyService.keysAvailable else {
waitForScreenAnalysisKeys(reason: reason)
return
}
plugin.refreshScreenRecordingPermission()
guard plugin.hasScreenRecordingPermission else {
log("DesktopHomeView: Screen recording permission unavailable; retaining capture intent (\(reason))")
return
}
scheduleProactiveMonitoringStart(reason: reason)
}
private func waitForScreenAnalysisKeys(reason: String) {
guard !isWaitingForScreenAnalysisKeys else { return }
isWaitingForScreenAnalysisKeys = true
log("DesktopHomeView: Deferring screen analysis until API keys load (\(reason))")
Task { @MainActor in
await APIKeyService.shared.waitForKeys()
isWaitingForScreenAnalysisKeys = false
guard APIKeyService.keysAvailable else {
log("DesktopHomeView: API keys remain unavailable; retaining capture intent")
return
}
restorePersistedCaptureServices(reason: "key wait completed")
}
}
private func reconcileCaptureServicesAfterSettingsSync() {
let plugin = ProactiveAssistantsPlugin.shared
if !AssistantSettings.shared.screenAnalysisEnabled, plugin.isMonitoring {
log("DesktopHomeView: Stopping screen analysis after server settings sync")
plugin.stopMonitoring(reason: .settingsSync)
}
restorePersistedCaptureServices(reason: "settings sync")
}
private func updateStoreActivityForCurrentShell() {
viewModelContainer.tasksStore.isActive =
chatFirstNavigation.route == .tasks || chatFirstNavigation.route == .more(.dashboard)
viewModelContainer.memoriesViewModel.isActive = chatFirstNavigation.route == .memories
}
/// One fresh server read decides the local runtime projection. It does not
/// decide which shell mounts — there is only one — so the shell is already on
/// screen while this runs. A failed response, missing owner, stale auth
/// snapshot, or owner change resolves capability-off: rich blocks still
/// render, kernel features stay dormant.
private func resolveChatFirstCapabilityIfNeeded() async {
guard !chatFirstCapability.isResolved else { return }
guard let ownerID = RuntimeOwnerIdentity.currentOwnerId(),
let authorization = RuntimeOwnerIdentity.captureAuthorizationSnapshot(expectedOwnerID: ownerID)
else {
chatFirstCapability.resolve(
control: nil,
requestedOwnerID: nil,
ownerIsStillCurrent: false
)
_ = viewModelContainer.chatProvider.configureChatFirstMainChatCapability(nil)
log("DesktopHomeView: chat-first capability off — no owner or authorization snapshot at sample time")
AnalyticsManager.shared.chatFirst(
.capabilityResolution(
outcome: .unavailable,
generationBucket: .none,
errorClass: .ownerChanged
)
)
reportAutomationState()
return
}
var capabilityErrorClass: ChatFirstAnalyticsEvent.CapabilityErrorClass = .none
do {
let control = try await APIClient.shared.getCandidateWorkflowControl(
expectedOwnerId: ownerID,
authorizationSnapshot: authorization
)
let current =
RuntimeOwnerIdentity.isAuthorizationCurrent(authorization)
&& RuntimeOwnerIdentity.currentOwnerId() == ownerID