-
Notifications
You must be signed in to change notification settings - Fork 265
Expand file tree
/
Copy pathAppDelegate.swift
More file actions
1288 lines (1132 loc) · 51.2 KB
/
AppDelegate.swift
File metadata and controls
1288 lines (1132 loc) · 51.2 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
//
// AppDelegate.swift
// osaurus
//
// Created by Terence on 8/17/25.
//
import AppKit
import Combine
import QuartzCore
import SwiftUI
@MainActor
public final class AppDelegate: NSObject, NSApplicationDelegate, NSPopoverDelegate {
public static weak var shared: AppDelegate?
let serverController = ServerController()
private var statusItem: NSStatusItem?
private var popover: NSPopover?
private var cancellables: Set<AnyCancellable> = []
let updater = UpdaterViewModel()
private var activityDot: NSView?
private var vadDot: NSView?
private var pendingPopoverAction: (@MainActor () -> Void)?
public func applicationDidFinishLaunching(_ notification: Notification) {
AppDelegate.shared = self
// Detect repeated startup crashes and enter safe mode if needed
LaunchGuard.checkOnLaunch()
// CRITICAL SEQUENCING: run the at-rest encryption migrator
// BEFORE any database opens. Without this gate
// `MemoryDatabase.shared.open()` below would try SQLCipher
// against still-plaintext files and fail key verification,
// leaving the app in a degraded state on first launch after
// upgrade. We block the launch flow synchronously while the
// overlay shows progress; the run loop is pumped so SwiftUI
// updates keep painting.
StorageMigrationCoordinator.blockingAwaitReady()
// Wire up the periodic SQLite maintenance ticker (PRAGMA
// optimize / wal_checkpoint / VACUUM at sensible intervals).
// Idempotent — safe even if some DBs aren't open yet, the
// ticker only touches handles that are currently registered.
Task.detached(priority: .background) {
await StorageMaintenance.shared.start()
}
// vmlx-swift-lm DSV4 cache-mode default. Process-wide env var read
// by `LLMModelFactory.dispatchDeepseekV4` at model-load time.
//
// DSV4-Flash's stock default is `RotatingKVCache(maxSize: 128)` per
// layer — fine for FIM / short Q&A but loses prompt visibility on
// any decode > 128 tokens, which means any chat conversation /
// reasoning-mode trace / multi-turn response drifts off-topic
// (live-confirmed 2026-04-25 on DSV4-Flash JANGTQ: thinking traces
// produced random SQL queries because the original prompt scrolled
// out of attention).
//
// Setting `DSV4_KV_MODE=full` switches new caches to `KVCacheSimple`
// — full attention across the entire prompt + decode. Memory cost
// ~360 MB at 8K output (vs. ~6 MB rotating), which is a non-issue
// on any machine that can load DSV4 in the first place (79.5 GB+
// bundles).
//
// No effect on non-DSV4 models — vmlx ignores the var unless the
// factory dispatch hits the `deepseek_v4` model_type. Setting this
// unconditionally at launch is the recommended osaurus-side
// operating point per vmlx
// `Libraries/MLXLMCommon/BatchEngine/OSAURUS-INTEGRATION.md`
// §"DeepSeek-V4 — runtime knobs" (2026-04-25 update). Users who
// want the rotating-window memory savings can override by exporting
// a different value before launching osaurus.
if ProcessInfo.processInfo.environment["DSV4_KV_MODE"] == nil {
setenv("DSV4_KV_MODE", "full", 1)
}
// Configure as regular app (show Dock icon) by default, or accessory if hidden
let hideDockIcon = ServerConfigurationStore.load()?.hideDockIcon ?? false
if hideDockIcon {
NSApp.setActivationPolicy(.accessory)
} else {
NSApp.setActivationPolicy(.regular)
}
// App has launched
NSLog("Osaurus server app launched")
// Configure local notifications
NotificationService.shared.configureOnLaunch()
// If PocketTTS models are already on disk, preload them so the first
// speaker tap plays immediately without routing to settings.
TTSService.shared.refreshModelState()
// Set up observers for server state changes
setupObservers()
// Set up distributed control listeners (local-only management)
setupControlNotifications()
// Apply saved Start at Login preference on launch
let launchedByCLI = ProcessInfo.processInfo.arguments.contains("--launched-by-cli")
if !launchedByCLI {
LoginItemService.shared.applyStartAtLogin(serverController.configuration.startAtLogin)
}
// Create status bar item and attach click handler
let item = NSStatusBar.system.statusItem(withLength: NSStatusItem.variableLength)
if let button = item.button {
if let image = NSImage(named: "osaurus") {
image.size = NSSize(width: 18, height: 18)
image.isTemplate = true
button.image = image
} else {
button.title = "Osaurus"
}
button.toolTip = L("Osaurus Server")
button.target = self
button.action = #selector(togglePopover(_:))
// Add a small green blinking dot at the bottom-right of the status bar button
let dot = NSView()
dot.wantsLayer = true
dot.translatesAutoresizingMaskIntoConstraints = false
dot.isHidden = true
button.addSubview(dot)
NSLayoutConstraint.activate([
dot.trailingAnchor.constraint(equalTo: button.trailingAnchor, constant: -3),
dot.bottomAnchor.constraint(equalTo: button.bottomAnchor, constant: -3),
dot.widthAnchor.constraint(equalToConstant: 7),
dot.heightAnchor.constraint(equalToConstant: 7),
])
if let layer = dot.layer {
layer.backgroundColor = NSColor.systemGreen.cgColor
layer.cornerRadius = 3.5
layer.borderWidth = 1
layer.borderColor = NSColor.white.withAlphaComponent(0.9).cgColor
}
activityDot = dot
// Add a VAD status dot at the top-right of the status bar button (blue/purple for VAD listening)
let vDot = NSView()
vDot.wantsLayer = true
vDot.translatesAutoresizingMaskIntoConstraints = false
vDot.isHidden = true
button.addSubview(vDot)
NSLayoutConstraint.activate([
vDot.trailingAnchor.constraint(equalTo: button.trailingAnchor, constant: -3),
vDot.topAnchor.constraint(equalTo: button.topAnchor, constant: 3),
vDot.widthAnchor.constraint(equalToConstant: 7),
vDot.heightAnchor.constraint(equalToConstant: 7),
])
if let layer = vDot.layer {
layer.backgroundColor = NSColor.systemBlue.cgColor
layer.cornerRadius = 3.5
layer.borderWidth = 1
layer.borderColor = NSColor.white.withAlphaComponent(0.9).cgColor
}
vadDot = vDot
}
statusItem = item
updateStatusItemAndMenu()
// Start main thread watchdog in debug builds to detect UI hangs
#if DEBUG
MainThreadWatchdog.shared.start()
#endif
// Initialize directory access early so security-scoped bookmark is active
let _ = DirectoryPickerService.shared
if LaunchGuard.isSafeMode {
NotificationService.shared.postSafeModeActive()
LaunchGuard.markStartupComplete()
} else {
// Load external tool plugins at launch (after core is initialized)
Task { @MainActor in
await PluginManager.shared.loadAll()
LaunchGuard.markStartupComplete()
}
// Start plugin repository background refresh for update checking
PluginRepositoryService.shared.startBackgroundRefresh()
}
// Pre-warm caches immediately for instant first window (no async deps).
// The unified prewarm builds the picker with whatever is currently
// available; once remote providers finish connecting below they post
// .remoteProviderModelsChanged and the cache rebuilds automatically.
_ = SpeechConfigurationStore.load()
ModelPickerItemCache.shared.prewarm()
// Auto-connect to enabled providers, then update model cache with remote models
Task { @MainActor in
await MCPProviderManager.shared.connectEnabledProviders()
await RemoteProviderManager.shared.connectEnabledProviders()
await ModelPickerItemCache.shared.prewarmModelCache()
}
// VecturaKit inits run sequentially. Memory DB opens first because
// MemorySearchService.initialize() needs it for reverse maps.
// MetalGate serializes CoreML/MLX at runtime; this task is only held
// for startup sequencing of orphan recovery + activity tracking below.
//
// The `blockingAwaitReady()` call above already gated the
// launch flow on the storage migrator, so by the time this
// Task runs the migrator is guaranteed done. Each
// `*Database.shared.open()` also calls the gate
// defensively (no-op fast path) for the plugin/HTTP entry
// points that don't go through this Task.
let embeddingInitTask = Task {
var memoryDBOpened = false
for attempt in 1 ... 3 {
do {
try MemoryDatabase.shared.open()
memoryDBOpened = true
break
} catch {
MemoryLogger.database.error("Memory database open attempt \(attempt)/3 failed: \(error)")
if attempt < 3 {
try? await Task.sleep(nanoseconds: UInt64(attempt) * 500_000_000)
}
}
}
if memoryDBOpened {
await MemorySearchService.shared.initialize()
} else {
MemoryLogger.database.error("Memory system disabled — database failed to open after 3 attempts")
}
try? MethodDatabase.shared.open()
await MethodSearchService.shared.initialize()
try? ToolDatabase.shared.open()
await ToolSearchService.shared.initialize()
await SkillSearchService.shared.initialize()
await ToolIndexService.shared.syncFromRegistry()
await SkillSearchService.shared.rebuildIndex()
await MethodSearchService.shared.rebuildIndex()
}
// Start activity tracking, drain any pending sessions left over from
// the previous launch, and arm the periodic consolidator.
Task { @MainActor in
await embeddingInitTask.value
if MemoryDatabase.shared.isOpen {
ActivityTracker.shared.start()
await MemoryService.shared.recoverOrphanedSignals()
await MemoryConsolidator.shared.start()
}
}
// Auto-start server on app launch
Task { @MainActor in
await serverController.startServer()
}
// Setup global hotkey for Chat overlay (configured)
applyChatHotkey()
// Auto-load speech model if voice features are enabled
Task { @MainActor in
await SpeechService.shared.autoLoadIfNeeded()
}
// Initialize VAD service if enabled
initializeVADService()
// Setup VAD detection notification listener
setupVADNotifications()
// Initialize Transcription Mode service
initializeTranscriptionModeService()
// Setup global toast notification system
ToastWindowController.shared.setup()
// Setup notch background task indicator
NotchWindowController.shared.setup()
// Initialize ScheduleManager to start scheduled tasks
_ = ScheduleManager.shared
// Initialize WatcherManager to start file system watchers
_ = WatcherManager.shared
// Start sandbox tool registrar. Internally awaits container
// auto-start before the initial `registerTools` call, so the first
// compose for the active agent sees real sandbox tools instead of
// the placeholder. (Replaces a separate `Task.detached` startContainer
// call that used to race the registrar's first registration.)
SandboxToolRegistrar.shared.start()
// Show onboarding for first-time users
if OnboardingService.shared.shouldShowOnboarding {
// Slight delay to let the app finish launching
Task { @MainActor in
try? await Task.sleep(nanoseconds: 300_000_000) // 300ms
showOnboardingWindow()
}
} else {
// Fresh launch from terminated state: explicitly activate and show window
Task { @MainActor in
// Delay slightly to ensure services are ready
try? await Task.sleep(nanoseconds: 300_000_000) // 300ms
// Ensure app is unhidden and active
NSApp.unhide(nil)
if #available(macOS 14.0, *) {
_ = NSRunningApplication.current.activate(options: .activateAllWindows)
} else {
_ = NSRunningApplication.current.activate(options: [
.activateAllWindows, .activateIgnoringOtherApps,
])
}
if ChatWindowManager.shared.windowCount > 0 {
ChatWindowManager.shared.focusAllWindows()
} else if WindowManager.shared.isVisible(.management) {
WindowManager.shared.show(.management, center: false)
} else {
showChatOverlay()
}
}
}
}
// MARK: - VAD Service
private func initializeVADService() {
// Auto-start VAD if enabled (with delay to wait for model loading)
let vadConfig = VADConfigurationStore.load()
if vadConfig.vadModeEnabled && !vadConfig.enabledAgentIds.isEmpty {
Task { @MainActor in
// Wait for speech model to be loaded (up to 30 seconds)
let speechService = SpeechService.shared
var attempts = 0
while !speechService.isModelLoaded && attempts < 60 {
try? await Task.sleep(nanoseconds: 500_000_000) // 500ms
attempts += 1
}
if speechService.isModelLoaded {
do {
try await VADService.shared.start()
print("[AppDelegate] VAD service started successfully on app launch")
} catch {
print("[AppDelegate] Failed to start VAD service: \(error)")
}
} else {
print("[AppDelegate] VAD service not started - model not loaded after 30 seconds")
}
}
}
}
// MARK: - Transcription Mode Service
private func initializeTranscriptionModeService() {
// Initialize the transcription mode service and register hotkey if enabled
TranscriptionModeService.shared.initialize()
print("[AppDelegate] Transcription mode service initialized")
}
private func setupVADNotifications() {
// Listen for agent detection from VAD service
NotificationCenter.default.addObserver(
self,
selector: #selector(handleVADAgentDetected(_:)),
name: .vadAgentDetected,
object: nil
)
// Listen for requests to show main window
NotificationCenter.default.addObserver(
self,
selector: #selector(handleShowMainWindow(_:)),
name: NSNotification.Name("ShowMainWindow"),
object: nil
)
// Listen for requests to show voice settings
NotificationCenter.default.addObserver(
self,
selector: #selector(handleShowVoiceSettings(_:)),
name: NSNotification.Name("ShowVoiceSettings"),
object: nil
)
// Listen for requests to show management window
NotificationCenter.default.addObserver(
self,
selector: #selector(handleShowManagement(_:)),
name: NSNotification.Name("ShowManagement"),
object: nil
)
// Route "user tapped speaker but model isn't ready" to the TTS settings tab.
NotificationCenter.default.addObserver(
self,
selector: #selector(handleOpenTTSSettings(_:)),
name: .openTTSSettingsRequested,
object: nil
)
// Listen for chat view closed to resume VAD
NotificationCenter.default.addObserver(
self,
selector: #selector(handleChatViewClosed(_:)),
name: .chatViewClosed,
object: nil
)
// Listen for requests to close chat overlay (from silence timeout)
NotificationCenter.default.addObserver(
self,
selector: #selector(handleCloseChatOverlay(_:)),
name: .closeChatOverlay,
object: nil
)
}
@objc private func handleChatViewClosed(_ notification: Notification) {
print("[AppDelegate] Chat view closed, checking if VAD should resume...")
Task { @MainActor in
// Resume VAD if it was paused
await VADService.shared.resumeAfterChat()
}
}
@objc private func handleCloseChatOverlay(_ notification: Notification) {
print("[AppDelegate] Close chat overlay requested (silence timeout)")
Task { @MainActor in
closeChatOverlay()
}
}
@objc private func handleVADAgentDetected(_ notification: Notification) {
guard let detection = notification.object as? VADDetectionResult else { return }
Task { @MainActor in
print("[AppDelegate] VAD detected agent: \(detection.agentName)")
// Check if a window for this agent already exists
let existingWindows = ChatWindowManager.shared.findWindows(byAgentId: detection.agentId)
let targetWindowId: UUID
if let existing = existingWindows.first {
// Focus existing window for this agent
print("[AppDelegate] Found existing window for agent, focusing...")
ChatWindowManager.shared.showWindow(id: existing.id)
targetWindowId = existing.id
} else {
// Create a new chat window for the detected agent
print("[AppDelegate] Creating new chat window for agent...")
targetWindowId = ChatWindowManager.shared.createWindow(agentId: detection.agentId)
}
print(
"[AppDelegate] VAD target window: \(targetWindowId), window count: \(ChatWindowManager.shared.windowCount)"
)
// Pause VAD when handling voice input
await VADService.shared.pause()
// Start voice input in chat after a delay (let VAD stop and UI settle)
let vadConfig = VADConfigurationStore.load()
if vadConfig.autoStartVoiceInput {
try? await Task.sleep(nanoseconds: 200_000_000) // 200ms - fast handoff
print("[AppDelegate] Triggering voice input in chat for window \(targetWindowId)")
NotificationCenter.default.post(
name: .startVoiceInputInChat,
object: targetWindowId // Target specific window
)
}
NotificationCenter.default.post(name: .chatOverlayActivated, object: nil)
}
}
@objc private func handleShowMainWindow(_ notification: Notification) {
Task { @MainActor in
showChatOverlay()
}
}
@objc private func handleShowVoiceSettings(_ notification: Notification) {
Task { @MainActor in
showManagementWindow(initialTab: .voice)
}
}
@objc private func handleShowManagement(_ notification: Notification) {
Task { @MainActor in
showManagementWindow()
}
}
@objc private func handleOpenTTSSettings(_ notification: Notification) {
Task { @MainActor in
ManagementStateManager.shared.voiceSubTabRequest = "TTS"
showManagementWindow(initialTab: .voice)
}
}
public func application(_ application: NSApplication, open urls: [URL]) {
for url in urls {
handleDeepLink(url)
}
}
public func applicationShouldHandleReopen(_ sender: NSApplication, hasVisibleWindows flag: Bool) -> Bool {
Task { @MainActor in
// Show onboarding if not completed (mandatory step)
if OnboardingService.shared.shouldShowOnboarding {
self.showOnboardingWindow()
return
}
if ChatWindowManager.shared.windowCount > 0 {
ChatWindowManager.shared.focusAllWindows()
} else if WindowManager.shared.isVisible(.management) {
WindowManager.shared.show(.management, center: false)
} else {
self.showChatOverlay()
}
}
return true
}
// MARK: - Dock Menu
public func applicationDockMenu(_ sender: NSApplication) -> NSMenu? {
let menu = NSMenu()
menu.addItem(NSMenuItem(title: "New Chat", action: #selector(dockNewChat), keyEquivalent: ""))
menu.addItem(NSMenuItem(title: "Agents", action: #selector(dockShowAgents), keyEquivalent: ""))
menu.addItem(NSMenuItem(title: "Settings", action: #selector(dockShowSettings), keyEquivalent: ""))
#if DEBUG
menu.addItem(NSMenuItem.separator())
menu.addItem(
NSMenuItem(title: "Reset Onboarding", action: #selector(dockResetOnboarding), keyEquivalent: "")
)
#endif
return menu
}
@objc private func dockNewChat() {
showChatOverlay()
}
@objc private func dockShowAgents() {
showManagementWindow(initialTab: .agents)
}
@objc private func dockShowSettings() {
showManagementWindow(initialTab: nil)
}
#if DEBUG
@objc private func dockResetOnboarding() {
OnboardingService.shared.resetOnboarding()
showOnboardingWindow(forceShowIdentity: true)
}
#endif
public func applicationShouldTerminate(_ sender: NSApplication) -> NSApplication.TerminateReply {
// Defer termination so in-flight inference tasks and MLX GPU resources are
// released before exit() triggers C++ static destructors.
//
// Issue #860: the previous version guarded the server shutdown on
// `serverController.isRunning`. That flag can be false while the
// underlying NIO `MultiThreadedEventLoopGroup` is still alive
// (e.g. mid-partial-start, mid-shutdown, or Sparkle-triggered
// quit racing against server cleanup). When the EL group is
// still non-nil at `exit()`, NIO's destructor hits
// `preconditionFailure("EventLoopGroup is still running")` —
// EXC_BREAKPOINT at `NIO-ELT-3` as reported. `ensureShutdown()`
// itself is a no-op if everything is already nil, so always
// call it.
//
// We also always stop the sandbox (which in turn stops the
// HostAPIBridgeServer) so its 2-thread EL group can't leak
// past quit even when no sandbox container was started.
Task { @MainActor in
ChatWindowManager.shared.stopAllSessions()
BackgroundTaskManager.shared.cancelAllTasks()
MCPProviderManager.shared.disconnectAll()
RemoteProviderManager.shared.disconnectAll()
// Unconditional: ensureShutdown is idempotent when already clean.
await serverController.ensureShutdown()
await MCPServerManager.shared.stopAll()
await ModelRuntime.shared.clearAll()
do {
try await SandboxManager.shared.stopContainer()
} catch {
NSLog("[Osaurus] Sandbox stop failed: \(error)")
}
// Belt-and-suspenders: if the sandbox was never provisioned,
// `stopContainer` still stops the bridge, but if the bridge
// was started through some other path in a future refactor
// we want its EL group torn down regardless.
await HostAPIBridgeServer.shared.stop()
NSApp.reply(toApplicationShouldTerminate: true)
}
return .terminateLater
}
public func applicationWillTerminate(_ notification: Notification) {
NSLog("Osaurus server app terminating")
PluginRepositoryService.shared.stopBackgroundRefresh()
ToastWindowController.shared.teardown()
NotchWindowController.shared.teardown()
SharedConfigurationService.shared.remove()
}
// MARK: Status Item / Menu
private func setupObservers() {
cancellables.removeAll()
serverController.$serverHealth
.receive(on: RunLoop.main)
.sink { [weak self] _ in
self?.updateStatusItemAndMenu()
}
.store(in: &cancellables)
serverController.$isRunning
.receive(on: RunLoop.main)
.sink { [weak self] _ in
self?.updateStatusItemAndMenu()
}
.store(in: &cancellables)
serverController.$configuration
.receive(on: RunLoop.main)
.sink { [weak self] _ in
self?.updateStatusItemAndMenu()
}
.store(in: &cancellables)
serverController.$activeRequestCount
.receive(on: RunLoop.main)
.sink { [weak self] _ in
self?.updateStatusItemAndMenu()
}
.store(in: &cancellables)
// Observe VAD service state for menu bar indicator
VADService.shared.$state
.receive(on: RunLoop.main)
.sink { [weak self] _ in
self?.updateStatusItemAndMenu()
}
.store(in: &cancellables)
// Publish shared configuration on state/config/address changes
Publishers.CombineLatest3(
serverController.$serverHealth,
serverController.$configuration,
serverController.$localNetworkAddress
)
.receive(on: RunLoop.main)
.sink { health, config, address in
SharedConfigurationService.shared.update(
health: health,
configuration: config,
localAddress: address
)
}
.store(in: &cancellables)
}
private func updateStatusItemAndMenu() {
guard let statusItem else { return }
// Ensure no NSMenu is attached so button action is triggered
statusItem.menu = nil
if let button = statusItem.button {
// Update status bar icon
if let image = NSImage(named: "osaurus") {
image.size = NSSize(width: 18, height: 18)
image.isTemplate = true
button.image = image
}
// Toggle green blinking dot overlay
let isGenerating = serverController.activeRequestCount > 0
if let dot = activityDot {
if isGenerating {
dot.isHidden = false
if let layer = dot.layer, layer.animation(forKey: "blink") == nil {
let anim = CABasicAnimation(keyPath: "opacity")
anim.fromValue = 1.0
anim.toValue = 0.2
anim.duration = 0.8
anim.autoreverses = true
anim.repeatCount = .infinity
layer.add(anim, forKey: "blink")
}
} else {
if let layer = dot.layer {
layer.removeAnimation(forKey: "blink")
}
dot.isHidden = true
}
}
var tooltip: String
switch serverController.serverHealth {
case .stopped:
tooltip =
serverController.isRestarting ? "Osaurus — Restarting…" : "Osaurus — Ready to start"
case .starting:
tooltip = "Osaurus — Starting…"
case .restarting:
tooltip = "Osaurus — Restarting…"
case .running:
tooltip = "Osaurus — Running on port \(serverController.port)"
case .stopping:
tooltip = "Osaurus — Stopping…"
case .error(let message):
tooltip = "Osaurus — Error: \(message)"
}
if serverController.activeRequestCount > 0 {
tooltip += " — Generating…"
}
// Update VAD status dot
let vadState = VADService.shared.state
if let vDot = vadDot {
switch vadState {
case .listening:
vDot.isHidden = false
if let layer = vDot.layer {
layer.backgroundColor = NSColor.systemBlue.cgColor
// Add pulse animation for listening state
if layer.animation(forKey: "vadPulse") == nil {
let anim = CABasicAnimation(keyPath: "opacity")
anim.fromValue = 1.0
anim.toValue = 0.4
anim.duration = 1.2
anim.autoreverses = true
anim.repeatCount = .infinity
layer.add(anim, forKey: "vadPulse")
}
}
tooltip += " — Voice: Listening"
case .error:
vDot.isHidden = false
if let layer = vDot.layer {
layer.backgroundColor = NSColor.systemRed.cgColor
layer.removeAnimation(forKey: "vadPulse")
}
tooltip += " — Voice: Error"
default:
if let layer = vDot.layer {
layer.removeAnimation(forKey: "vadPulse")
}
vDot.isHidden = true
}
}
// Advertise MCP HTTP endpoints on the same port
tooltip += " — MCP: /mcp/*"
button.toolTip = tooltip
}
}
// MARK: - Actions
@objc private func togglePopover(_ sender: Any?) {
if let popover, popover.isShown {
popover.performClose(sender)
return
}
showPopover()
}
// Expose a method to show the popover programmatically (e.g., for Cmd+,)
public func showPopover() {
guard let statusButton = statusItem?.button else { return }
if let popover, popover.isShown {
// Already visible; bring app to front
NSApp.activate(ignoringOtherApps: true)
return
}
let popover = NSPopover()
popover.behavior = .transient
popover.animates = true
popover.delegate = self
let themeManager = ThemeManager.shared
let statusPanel = StatusPanelView()
.environmentObject(serverController)
.environment(\.theme, themeManager.currentTheme)
.environmentObject(updater)
popover.contentViewController = NSHostingController(rootView: statusPanel)
self.popover = popover
popover.show(relativeTo: statusButton.bounds, of: statusButton, preferredEdge: .minY)
// ensure popover window can join all spaces and appear over full screen apps
if let popoverWindow = popover.contentViewController?.view.window {
popoverWindow.collectionBehavior = [.canJoinAllSpaces, .fullScreenAuxiliary]
}
NSApp.activate(ignoringOtherApps: true)
}
// MARK: - NSPopoverDelegate
public func popoverDidClose(_ notification: Notification) {
print("[AppDelegate] Popover closed, posting chatViewClosed notification")
// Post notification so VAD can resume
NotificationCenter.default.post(name: .chatViewClosed, object: nil)
if let action = pendingPopoverAction {
pendingPopoverAction = nil
Task { @MainActor in
action()
}
}
}
}
// MARK: - Distributed Control (Local Only)
extension AppDelegate {
fileprivate static let controlToolsReloadNotification = Notification.Name(
"com.dinoki.osaurus.control.toolsReload"
)
fileprivate static let controlServeNotification = Notification.Name(
"com.dinoki.osaurus.control.serve"
)
fileprivate static let controlStopNotification = Notification.Name(
"com.dinoki.osaurus.control.stop"
)
fileprivate static let controlShowUINotification = Notification.Name(
"com.dinoki.osaurus.control.ui"
)
private func setupControlNotifications() {
let center = DistributedNotificationCenter.default()
center.addObserver(
self,
selector: #selector(handleServeCommand(_:)),
name: Self.controlServeNotification,
object: nil
)
center.addObserver(
self,
selector: #selector(handleStopCommand(_:)),
name: Self.controlStopNotification,
object: nil
)
center.addObserver(
self,
selector: #selector(handleShowUICommand(_:)),
name: Self.controlShowUINotification,
object: nil
)
center.addObserver(
self,
selector: #selector(handleToolsReloadCommand(_:)),
name: Self.controlToolsReloadNotification,
object: nil
)
}
@objc private func handleServeCommand(_ note: Notification) {
var desiredPort: Int? = nil
var exposeFlag: Bool = false
if let ui = note.userInfo {
if let p = ui["port"] as? Int {
desiredPort = p
} else if let s = ui["port"] as? String, let p = Int(s) {
desiredPort = p
}
if let e = ui["expose"] as? Bool {
exposeFlag = e
} else if let es = ui["expose"] as? String {
let v = es.trimmingCharacters(in: .whitespacesAndNewlines).lowercased()
exposeFlag = (v == "1" || v == "true" || v == "yes" || v == "y")
}
}
// Apply defaults if not provided
let targetPort = desiredPort ?? (ServerConfigurationStore.load()?.port ?? 1337)
guard (1 ..< 65536).contains(targetPort) else { return }
// Apply exposure policy based on request (default localhost-only)
serverController.configuration.exposeToNetwork = exposeFlag
serverController.port = targetPort
serverController.saveConfiguration()
Task { @MainActor in
await serverController.startServer()
}
}
@objc private func handleStopCommand(_ note: Notification) {
Task { @MainActor in
await serverController.stopServer()
}
}
@objc private func handleShowUICommand(_ note: Notification) {
Task { @MainActor in
self.showPopover()
}
}
@objc private func handleToolsReloadCommand(_ note: Notification) {
Task { @MainActor in
await PluginManager.shared.loadAll(forceReload: true)
}
}
}
// MARK: Deep Link Handling
extension AppDelegate {
func applyChatHotkey() {
let cfg = ChatConfigurationStore.load()
HotKeyManager.shared.register(hotkey: cfg.hotkey) { [weak self] in
Task { @MainActor in
// if opening (about to be shown), and clipboard monitoring is enabled, trigger a selection grab before showing Osaurus
// to capture content from the currently active application.
if !ChatWindowManager.shared.hasVisibleWindows && cfg.enableClipboardMonitoring {
// start grabbing selection in the background before we take focus
Task {
_ = await ClipboardService.shared.grabSelection()
}
// small yield to allow Cmd+C to be posted before toggle takes focus
// 50ms
try? await Task.sleep(nanoseconds: 50_000_000)
}
self?.toggleChatOverlay()
}
}
}
fileprivate func handleDeepLink(_ url: URL) {
let scheme = url.scheme?.lowercased() ?? ""
switch scheme {
case "osaurus":
handleOsaurusDeepLink(url)
case "huggingface":
handleHuggingFaceDeepLink(url)
default:
return
}
}
/// `osaurus://<addr>?pair=<base64url(invite)>` — incoming agent share link.
/// Stages the decoded invite for `IncomingPairSheet` to present.
fileprivate func handleOsaurusDeepLink(_ url: URL) {
Task { @MainActor in
// Make sure SOMETHING is on screen so the approval panel doesn't
// open behind a hidden app. Bring the management window forward
// as the anchor — it doesn't matter which tab is selected; the
// approval is presented as its own NSPanel via PairingPromptService.
NSApp.activate(ignoringOtherApps: true)
showManagementWindow(initialTab: .agents)
_ = PairingDeepLinkRouter.handle(url)
}
}
fileprivate func handleHuggingFaceDeepLink(_ url: URL) {
guard let components = URLComponents(url: url, resolvingAgainstBaseURL: false) else { return }
let items = components.queryItems ?? []
let modelId = items.first(where: { $0.name.lowercased() == "model" })?.value?
.trimmingCharacters(in: .whitespacesAndNewlines)
let file = items.first(where: { $0.name.lowercased() == "file" })?.value?.trimmingCharacters(
in: .whitespacesAndNewlines
)
guard let modelId, !modelId.isEmpty else {
// No model id provided; ignore silently
return
}
// Resolve to ensure it appears in the UI; enforce MLX-only via metadata
Task { @MainActor in
if await ModelManager.shared.resolveModelIfMLXCompatible(byRepoId: modelId) == nil {
let alert = NSAlert()
alert.messageText = "Unsupported model"
alert.informativeText = "Osaurus only supports MLX-compatible Hugging Face repositories."
alert.alertStyle = .warning
alert.addButton(withTitle: "OK")
alert.runModal()
return
}
// Open Model Manager in its own window for deeplinks
showManagementWindow(initialTab: .models, deeplinkModelId: modelId, deeplinkFile: file)
}
}
}