@lattices/cli 0.3.0 → 0.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (111) hide show
  1. package/README.md +85 -9
  2. package/app/Info.plist +30 -0
  3. package/app/Lattices.app/Contents/Info.plist +8 -2
  4. package/app/Lattices.app/Contents/MacOS/Lattices +0 -0
  5. package/app/Lattices.app/Contents/Resources/AppIcon.icns +0 -0
  6. package/app/Lattices.app/Contents/Resources/tap.wav +0 -0
  7. package/app/Lattices.app/Contents/_CodeSignature/CodeResources +139 -0
  8. package/app/Lattices.entitlements +15 -0
  9. package/app/Package.swift +8 -1
  10. package/app/Resources/tap.wav +0 -0
  11. package/app/Sources/AdvisorLearningStore.swift +90 -0
  12. package/app/Sources/AgentSession.swift +377 -0
  13. package/app/Sources/AppDelegate.swift +45 -12
  14. package/app/Sources/AppShellView.swift +81 -8
  15. package/app/Sources/AudioProvider.swift +386 -0
  16. package/app/Sources/CheatSheetHUD.swift +261 -19
  17. package/app/Sources/DaemonProtocol.swift +13 -0
  18. package/app/Sources/DaemonServer.swift +8 -0
  19. package/app/Sources/DesktopModel.swift +189 -6
  20. package/app/Sources/DesktopModelTypes.swift +2 -0
  21. package/app/Sources/DiagnosticLog.swift +104 -2
  22. package/app/Sources/EventBus.swift +1 -0
  23. package/app/Sources/HUDBottomBar.swift +279 -0
  24. package/app/Sources/HUDController.swift +1158 -0
  25. package/app/Sources/HUDLeftBar.swift +849 -0
  26. package/app/Sources/HUDMinimap.swift +179 -0
  27. package/app/Sources/HUDRightBar.swift +774 -0
  28. package/app/Sources/HUDState.swift +367 -0
  29. package/app/Sources/HUDTopBar.swift +243 -0
  30. package/app/Sources/HandsOffSession.swift +802 -0
  31. package/app/Sources/HomeDashboardView.swift +125 -0
  32. package/app/Sources/HotkeyManager.swift +2 -0
  33. package/app/Sources/HotkeyStore.swift +49 -9
  34. package/app/Sources/IntentEngine.swift +962 -0
  35. package/app/Sources/Intents/CreateLayerIntent.swift +54 -0
  36. package/app/Sources/Intents/DistributeIntent.swift +56 -0
  37. package/app/Sources/Intents/FocusIntent.swift +69 -0
  38. package/app/Sources/Intents/HelpIntent.swift +41 -0
  39. package/app/Sources/Intents/KillIntent.swift +47 -0
  40. package/app/Sources/Intents/LatticeIntent.swift +78 -0
  41. package/app/Sources/Intents/LaunchIntent.swift +67 -0
  42. package/app/Sources/Intents/ListSessionsIntent.swift +32 -0
  43. package/app/Sources/Intents/ListWindowsIntent.swift +30 -0
  44. package/app/Sources/Intents/ScanIntent.swift +52 -0
  45. package/app/Sources/Intents/SearchIntent.swift +190 -0
  46. package/app/Sources/Intents/SwitchLayerIntent.swift +50 -0
  47. package/app/Sources/Intents/TileIntent.swift +61 -0
  48. package/app/Sources/LatticesApi.swift +1275 -30
  49. package/app/Sources/LauncherHUD.swift +348 -0
  50. package/app/Sources/MainView.swift +147 -44
  51. package/app/Sources/MouseFinder.swift +222 -0
  52. package/app/Sources/OcrModel.swift +34 -1
  53. package/app/Sources/OmniSearchState.swift +99 -102
  54. package/app/Sources/OnboardingView.swift +457 -0
  55. package/app/Sources/PermissionChecker.swift +2 -12
  56. package/app/Sources/PiChatDock.swift +454 -0
  57. package/app/Sources/PiChatSession.swift +815 -0
  58. package/app/Sources/PiWorkspaceView.swift +364 -0
  59. package/app/Sources/PlacementSpec.swift +195 -0
  60. package/app/Sources/Preferences.swift +59 -0
  61. package/app/Sources/ProjectScanner.swift +58 -45
  62. package/app/Sources/ScreenMapState.swift +701 -55
  63. package/app/Sources/ScreenMapView.swift +843 -103
  64. package/app/Sources/ScreenMapWindowController.swift +22 -0
  65. package/app/Sources/SessionLayerStore.swift +285 -0
  66. package/app/Sources/SessionManager.swift +4 -1
  67. package/app/Sources/SettingsView.swift +186 -3
  68. package/app/Sources/Theme.swift +9 -8
  69. package/app/Sources/TmuxModel.swift +7 -0
  70. package/app/Sources/TmuxQuery.swift +27 -3
  71. package/app/Sources/VoiceChatView.swift +192 -0
  72. package/app/Sources/VoiceCommandWindow.swift +1594 -0
  73. package/app/Sources/VoiceIntentResolver.swift +671 -0
  74. package/app/Sources/VoxClient.swift +454 -0
  75. package/app/Sources/WindowTiler.swift +348 -87
  76. package/app/Sources/WorkspaceManager.swift +127 -18
  77. package/app/Tests/StageDragTests.swift +333 -0
  78. package/app/Tests/StageJoinTests.swift +313 -0
  79. package/app/Tests/StageManagerTests.swift +280 -0
  80. package/app/Tests/StageTileTests.swift +353 -0
  81. package/assets/AppIcon.icns +0 -0
  82. package/bin/client.ts +16 -0
  83. package/bin/{daemon-client.js → daemon-client.ts} +49 -30
  84. package/bin/handsoff-infer.ts +280 -0
  85. package/bin/handsoff-worker.ts +740 -0
  86. package/bin/lattices-app.ts +338 -0
  87. package/bin/lattices-dev +208 -0
  88. package/bin/{lattices.js → lattices.ts} +777 -140
  89. package/bin/project-twin.ts +645 -0
  90. package/docs/agent-execution-plan.md +562 -0
  91. package/docs/agent-layer-guide.md +207 -0
  92. package/docs/agents.md +142 -0
  93. package/docs/api.md +153 -34
  94. package/docs/app.md +29 -1
  95. package/docs/config.md +5 -1
  96. package/docs/handsoff-test-scenarios.md +84 -0
  97. package/docs/layers.md +20 -20
  98. package/docs/ocr.md +14 -5
  99. package/docs/overview.md +5 -1
  100. package/docs/presentation-execution-review.md +491 -0
  101. package/docs/prompts/hands-off-system.md +374 -0
  102. package/docs/prompts/hands-off-turn.md +30 -0
  103. package/docs/prompts/voice-advisor.md +31 -0
  104. package/docs/prompts/voice-fallback.md +23 -0
  105. package/docs/tiling-reference.md +167 -0
  106. package/docs/twins.md +138 -0
  107. package/docs/voice-command-protocol.md +278 -0
  108. package/docs/voice.md +219 -0
  109. package/package.json +29 -11
  110. package/bin/client.js +0 -4
  111. package/bin/lattices-app.js +0 -221
@@ -0,0 +1,377 @@
1
+ import Foundation
2
+
3
+ // MARK: - Persistent Claude CLI Agent
4
+
5
+ /// Manages a persistent Claude conversation via `--session-id` + `--resume`.
6
+ /// Each query spawns a `claude -p` process that resumes the same session.
7
+ /// Uses `--output-format stream-json` for structured response parsing.
8
+ /// The conversation context carries over between calls via session persistence.
9
+
10
+ final class AgentSession: ObservableObject {
11
+ let model: String
12
+ let label: String
13
+ private(set) var sessionId: UUID
14
+
15
+ @Published var isReady = false
16
+ @Published var lastResponse: AgentResponse?
17
+ @Published var sessionStats: SessionStats = .empty
18
+
19
+ struct SessionStats {
20
+ let inputTokens: Int
21
+ let outputTokens: Int
22
+ let cacheReadTokens: Int
23
+ let cacheCreationTokens: Int
24
+ let contextWindow: Int
25
+ let costUSD: Double
26
+ let numTurns: Int
27
+
28
+ static let empty = SessionStats(inputTokens: 0, outputTokens: 0, cacheReadTokens: 0, cacheCreationTokens: 0, contextWindow: 0, costUSD: 0, numTurns: 0)
29
+
30
+ /// How full is the context? 0.0–1.0
31
+ var contextUsage: Double {
32
+ guard contextWindow > 0 else { return 0 }
33
+ let totalInput = inputTokens + cacheReadTokens + cacheCreationTokens
34
+ return Double(totalInput) / Double(contextWindow)
35
+ }
36
+ }
37
+
38
+ private var claudePath: String?
39
+ private let queue = DispatchQueue(label: "agent-session", qos: .userInitiated)
40
+ private var callCount = 0
41
+ private var busy = false
42
+
43
+ /// Optional override for the system prompt. If set, used instead of the default advisor prompt.
44
+ var customSystemPrompt: (() -> String)?
45
+
46
+ init(model: String, label: String) {
47
+ self.model = model
48
+ self.label = label
49
+ self.sessionId = UUID()
50
+ }
51
+
52
+ // MARK: - Lifecycle
53
+
54
+ func start() {
55
+ guard let resolved = Preferences.resolveClaudePath() else {
56
+ DiagnosticLog.shared.warn("AgentSession[\(label)]: claude CLI not found")
57
+ return
58
+ }
59
+ claudePath = resolved
60
+ DiagnosticLog.shared.info("AgentSession[\(label)]: ready (model=\(model), claude=\(resolved), session=\(sessionId.uuidString.prefix(8)))")
61
+ DispatchQueue.main.async { self.isReady = true }
62
+ }
63
+
64
+ func stop() {
65
+ DispatchQueue.main.async {
66
+ self.isReady = false
67
+ self.callCount = 0
68
+ }
69
+ }
70
+
71
+ // MARK: - Communication
72
+
73
+ /// Send a message and get a response via callback (main thread).
74
+ func send(message: String, callback: @escaping (AgentResponse?) -> Void) {
75
+ guard isReady else {
76
+ callback(nil)
77
+ return
78
+ }
79
+ guard !busy else {
80
+ DiagnosticLog.shared.info("AgentSession[\(label)]: busy, skipping")
81
+ callback(nil)
82
+ return
83
+ }
84
+
85
+ queue.async { [weak self] in
86
+ guard let self = self else { return }
87
+ self.busy = true
88
+ let response = self.call(prompt: message)
89
+ self.busy = false
90
+
91
+ DispatchQueue.main.async {
92
+ self.lastResponse = response
93
+ callback(response)
94
+ }
95
+ }
96
+ }
97
+
98
+ // MARK: - Claude CLI call
99
+
100
+ private func call(prompt: String) -> AgentResponse? {
101
+ let timer = DiagnosticLog.shared.startTimed("AgentSession[\(label)] call")
102
+
103
+ guard let claudePath = claudePath else { return nil }
104
+
105
+ let proc = Process()
106
+ proc.executableURL = URL(fileURLWithPath: claudePath)
107
+
108
+ var args = [
109
+ "-p", prompt,
110
+ "--model", model,
111
+ "--output-format", "stream-json",
112
+ "--max-budget-usd", String(format: "%.2f", Preferences.shared.advisorBudgetUSD),
113
+ "--permission-mode", "plan",
114
+ "--no-chrome",
115
+ ]
116
+
117
+ if callCount == 0 {
118
+ // First call: create session with system prompt
119
+ args.append(contentsOf: [
120
+ "--session-id", sessionId.uuidString,
121
+ "--system-prompt", customSystemPrompt?() ?? buildSystemPrompt(),
122
+ ])
123
+ } else {
124
+ // Subsequent calls: resume existing session (context carries over)
125
+ args.append(contentsOf: ["--resume", sessionId.uuidString])
126
+ }
127
+
128
+ proc.arguments = args
129
+
130
+ var env = ProcessInfo.processInfo.environment
131
+ env.removeValue(forKey: "CLAUDECODE")
132
+ proc.environment = env
133
+
134
+ let outPipe = Pipe()
135
+ let errPipe = Pipe()
136
+ proc.standardOutput = outPipe
137
+ proc.standardError = errPipe
138
+
139
+ do {
140
+ try proc.run()
141
+ } catch {
142
+ DiagnosticLog.shared.warn("AgentSession[\(label)]: launch failed — \(error)")
143
+ DiagnosticLog.shared.finish(timer)
144
+ return nil
145
+ }
146
+
147
+ proc.waitUntilExit()
148
+ DiagnosticLog.shared.finish(timer)
149
+
150
+ let output = String(data: outPipe.fileHandleForReading.readDataToEndOfFile(), encoding: .utf8)?
151
+ .trimmingCharacters(in: .whitespacesAndNewlines) ?? ""
152
+ let stderr = String(data: errPipe.fileHandleForReading.readDataToEndOfFile(), encoding: .utf8)?
153
+ .trimmingCharacters(in: .whitespacesAndNewlines) ?? ""
154
+
155
+ if !stderr.isEmpty {
156
+ DiagnosticLog.shared.info("AgentSession[\(label)] stderr: \(stderr.prefix(200))")
157
+ }
158
+
159
+ guard !output.isEmpty else {
160
+ DiagnosticLog.shared.info("AgentSession[\(label)]: empty response")
161
+ return nil
162
+ }
163
+
164
+ // Parse stream-json output — extract text and stats
165
+ let parsed = parseStreamJSON(output)
166
+
167
+ // Update session stats
168
+ let stats = parsed.stats
169
+ DispatchQueue.main.async {
170
+ self.sessionStats = stats
171
+ }
172
+
173
+ if stats.contextWindow > 0 {
174
+ let pct = Int(stats.contextUsage * 100)
175
+ DiagnosticLog.shared.info("AgentSession[\(label)]: context \(pct)% (\(stats.inputTokens + stats.cacheReadTokens + stats.cacheCreationTokens)/\(stats.contextWindow)) cost=$\(String(format: "%.4f", stats.costUSD))")
176
+ }
177
+
178
+ guard let text = parsed.text, !text.isEmpty else {
179
+ DiagnosticLog.shared.info("AgentSession[\(label)]: no text in response")
180
+ return nil
181
+ }
182
+
183
+ // Auto-reset session if context usage > 75%
184
+ if stats.contextUsage > 0.75 {
185
+ DiagnosticLog.shared.warn("AgentSession[\(label)]: context at \(Int(stats.contextUsage * 100))%, resetting session")
186
+ sessionId = UUID() // Fresh session ID
187
+ callCount = 0 // Next call will create a fresh session
188
+ } else {
189
+ callCount += 1
190
+ }
191
+
192
+ DiagnosticLog.shared.info("AgentSession[\(label)]: \(text.prefix(120))")
193
+ return AgentResponse.parse(text: text)
194
+ }
195
+
196
+ struct ParsedResponse {
197
+ let text: String?
198
+ let stats: SessionStats
199
+ }
200
+
201
+ /// Parse stream-json output lines, extract text and session stats from the result line.
202
+ private func parseStreamJSON(_ output: String) -> ParsedResponse {
203
+ let lines = output.components(separatedBy: "\n")
204
+ var resultText: String?
205
+ var stats = SessionStats.empty
206
+
207
+ for line in lines {
208
+ guard let data = line.data(using: .utf8),
209
+ let json = try? JSONSerialization.jsonObject(with: data) as? [String: Any] else { continue }
210
+
211
+ let type = json["type"] as? String
212
+
213
+ if type == "result" {
214
+ resultText = json["result"] as? String
215
+ let numTurns = json["num_turns"] as? Int ?? 0
216
+ let costUSD = json["total_cost_usd"] as? Double ?? 0
217
+
218
+ // Usage stats
219
+ let usage = json["usage"] as? [String: Any] ?? [:]
220
+ let inputTokens = usage["input_tokens"] as? Int ?? 0
221
+ let outputTokens = usage["output_tokens"] as? Int ?? 0
222
+ let cacheRead = usage["cache_read_input_tokens"] as? Int ?? 0
223
+ let cacheCreation = usage["cache_creation_input_tokens"] as? Int ?? 0
224
+
225
+ // Context window from modelUsage
226
+ var contextWindow = 0
227
+ if let modelUsage = json["modelUsage"] as? [String: Any] {
228
+ for (_, v) in modelUsage {
229
+ if let m = v as? [String: Any], let cw = m["contextWindow"] as? Int {
230
+ contextWindow = cw
231
+ }
232
+ }
233
+ }
234
+
235
+ stats = SessionStats(
236
+ inputTokens: inputTokens,
237
+ outputTokens: outputTokens,
238
+ cacheReadTokens: cacheRead,
239
+ cacheCreationTokens: cacheCreation,
240
+ contextWindow: contextWindow,
241
+ costUSD: costUSD,
242
+ numTurns: numTurns
243
+ )
244
+ }
245
+
246
+ // Fallback: accumulate text from assistant content blocks
247
+ if resultText == nil, type == "assistant",
248
+ let message = json["message"] as? [String: Any],
249
+ let content = message["content"] as? [[String: Any]] {
250
+ var text = ""
251
+ for block in content {
252
+ if block["type"] as? String == "text",
253
+ let t = block["text"] as? String {
254
+ text += t
255
+ }
256
+ }
257
+ if !text.isEmpty { resultText = text }
258
+ }
259
+ }
260
+
261
+ return ParsedResponse(text: resultText, stats: stats)
262
+ }
263
+
264
+ // MARK: - System prompt
265
+
266
+ private func buildSystemPrompt() -> String {
267
+ let windowSummary = DesktopModel.shared.windows.values
268
+ .prefix(20)
269
+ .map { "\($0.app): \($0.title)" }
270
+ .joined(separator: "\n")
271
+
272
+ let intentList = PhraseMatcher.shared.catalog()
273
+ var intentSummary = ""
274
+ if case .array(let intents) = intentList {
275
+ intentSummary = intents.compactMap { intent -> String? in
276
+ guard let name = intent["intent"]?.stringValue else { return nil }
277
+ var slotNames: [String] = []
278
+ if case .array(let slots) = intent["slots"] {
279
+ slotNames = slots.compactMap { $0["name"]?.stringValue }
280
+ }
281
+ let s = slotNames.isEmpty ? "" : "(\(slotNames.joined(separator: ", ")))"
282
+ return "\(name)\(s)"
283
+ }.joined(separator: ", ")
284
+ }
285
+
286
+ return """
287
+ You are an advisor for Lattices, a macOS workspace manager. You run alongside voice commands, providing commentary and follow-up suggestions.
288
+
289
+ Available commands: \(intentSummary)
290
+
291
+ Current windows:
292
+ \(windowSummary)
293
+
294
+ For each user message, you receive a voice transcript and what command was matched.
295
+
296
+ Respond with ONLY a JSON object:
297
+ {"commentary": "short observation or null", "suggestion": {"label": "button text", "intent": "intent_name", "slots": {"key": "value"}} or null}
298
+
299
+ Rules:
300
+ - commentary: 1 sentence max. null if the matched command fully covers the request.
301
+ - suggestion: a follow-up action. null if none needed.
302
+ - Never suggest what was already executed.
303
+ - Suggestions MUST include all required slots. e.g. search requires {"query": "..."}.
304
+ - Be terse and useful, not chatty.
305
+ """
306
+ }
307
+ }
308
+
309
+ // MARK: - Response types
310
+
311
+ struct AgentResponse {
312
+ let commentary: String?
313
+ let suggestion: AgentSuggestion?
314
+ let raw: String
315
+
316
+ struct AgentSuggestion {
317
+ let label: String
318
+ let intent: String
319
+ let slots: [String: String]
320
+ }
321
+
322
+ static func parse(text: String) -> AgentResponse {
323
+ guard let jsonStr = extractJSON(from: text),
324
+ let data = jsonStr.data(using: .utf8),
325
+ let json = try? JSONSerialization.jsonObject(with: data) as? [String: Any] else {
326
+ return AgentResponse(commentary: text, suggestion: nil, raw: text)
327
+ }
328
+
329
+ let commentary = json["commentary"] as? String
330
+
331
+ var suggestion: AgentSuggestion?
332
+ if let s = json["suggestion"] as? [String: Any],
333
+ let label = s["label"] as? String,
334
+ let intent = s["intent"] as? String {
335
+ let slots = (s["slots"] as? [String: String]) ?? [:]
336
+ suggestion = AgentSuggestion(label: label, intent: intent, slots: slots)
337
+ }
338
+
339
+ return AgentResponse(commentary: commentary, suggestion: suggestion, raw: text)
340
+ }
341
+
342
+ private static func extractJSON(from text: String) -> String? {
343
+ let cleaned = text
344
+ .replacingOccurrences(of: "```json", with: "")
345
+ .replacingOccurrences(of: "```", with: "")
346
+ .trimmingCharacters(in: .whitespacesAndNewlines)
347
+ guard let start = cleaned.firstIndex(of: "{"),
348
+ let end = cleaned.lastIndex(of: "}") else { return nil }
349
+ return String(cleaned[start...end])
350
+ }
351
+ }
352
+
353
+ // MARK: - Agent Pool
354
+
355
+ /// Manages the Haiku (fast advisor) and Sonnet (deep thinker) agent sessions.
356
+ final class AgentPool {
357
+ static let shared = AgentPool()
358
+
359
+ let haiku = AgentSession(model: "haiku", label: "haiku")
360
+ let sonnet = AgentSession(model: "sonnet", label: "sonnet")
361
+
362
+ private init() {}
363
+
364
+ func start() {
365
+ DiagnosticLog.shared.info("AgentPool: starting haiku + sonnet sessions")
366
+ haiku.start()
367
+ // Stagger sonnet start
368
+ DispatchQueue.global().asyncAfter(deadline: .now() + 5) {
369
+ self.sonnet.start()
370
+ }
371
+ }
372
+
373
+ func stop() {
374
+ haiku.stop()
375
+ sonnet.stop()
376
+ }
377
+ }
@@ -81,19 +81,48 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSPopoverDelegate {
81
81
 
82
82
  let store = HotkeyStore.shared
83
83
  store.register(action: .palette) { CommandPaletteWindow.shared.toggle() }
84
- store.register(action: .screenMap) { ScreenMapWindowController.shared.toggle() }
84
+ store.register(action: .unifiedWindow) { ScreenMapWindowController.shared.toggle() }
85
85
  store.register(action: .bezel) { WindowBezel.showBezelForFrontmostWindow() }
86
86
  store.register(action: .cheatSheet) { CheatSheetHUD.shared.toggle() }
87
- store.register(action: .desktopInventory) { CommandModeWindow.shared.toggle() }
87
+ store.register(action: .voiceCommand) {
88
+ DiagnosticLog.shared.info("Hotkey: voiceCommand triggered")
89
+ VoiceCommandWindow.shared.toggle()
90
+ }
91
+ store.register(action: .handsOff) {
92
+ DiagnosticLog.shared.info("Hotkey: handsOff triggered")
93
+ HandsOffSession.shared.toggle()
94
+ // Show voice bar when starting, hide when stopping
95
+ if HandsOffSession.shared.state != .idle {
96
+ HUDController.shared.showVoiceBar()
97
+ } else {
98
+ HUDController.shared.hideVoiceBar()
99
+ }
100
+ }
101
+ store.register(action: .hud) { HUDController.shared.toggle() }
102
+ store.register(action: .mouseFinder) { MouseFinder.shared.find() }
103
+
104
+ // Pre-render HUD panels off-screen for instant first open
105
+ DispatchQueue.main.async { HUDController.shared.warmUp() }
88
106
  store.register(action: .omniSearch) { OmniSearchWindow.shared.toggle() }
89
107
 
90
- // Layer-switching hotkeys
108
+ // Session layer cycling
109
+ store.register(action: .layerNext) { SessionLayerStore.shared.cycleNext() }
110
+ store.register(action: .layerPrev) { SessionLayerStore.shared.cyclePrev() }
111
+ store.register(action: .layerTag) { SessionLayerStore.shared.tagFrontmostWindow() }
112
+
113
+ // Layer-switching hotkeys (1-9): session layers take priority
91
114
  let workspace = WorkspaceManager.shared
92
- let layerCount = (workspace.config?.layers ?? []).count
93
- for (i, action) in HotkeyAction.layerActions.prefix(layerCount).enumerated() {
115
+ let configLayerCount = (workspace.config?.layers ?? []).count
116
+ let maxLayers = max(configLayerCount, 9)
117
+ for (i, action) in HotkeyAction.layerActions.prefix(maxLayers).enumerated() {
94
118
  let index = i
95
119
  store.register(action: action) {
96
- workspace.tileLayer(index: index, launch: true, force: true)
120
+ let session = SessionLayerStore.shared
121
+ if !session.layers.isEmpty && index < session.layers.count {
122
+ session.switchTo(index: index)
123
+ } else {
124
+ workspace.focusLayer(index: index)
125
+ }
97
126
  EventBus.shared.post(.layerSwitched(index: index))
98
127
  }
99
128
  }
@@ -113,8 +142,10 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSPopoverDelegate {
113
142
  }
114
143
  store.register(action: .tileDistribute) { WindowTiler.distributeVisible() }
115
144
 
116
- // Check macOS permissions (Accessibility, Screen Recording)
117
- PermissionChecker.shared.check()
145
+ // Onboarding on first launch; otherwise just check permissions
146
+ if !OnboardingWindowController.shared.showIfNeeded() {
147
+ PermissionChecker.shared.check()
148
+ }
118
149
 
119
150
  // Start daemon services
120
151
  let diag = DiagnosticLog.shared
@@ -126,6 +157,8 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSPopoverDelegate {
126
157
  ProcessModel.shared.start()
127
158
  LatticesApi.setup()
128
159
  DaemonServer.shared.start()
160
+ AgentPool.shared.start()
161
+ HandsOffSession.shared.start()
129
162
  diag.finish(tBoot)
130
163
 
131
164
  // --diagnostics flag: auto-open diagnostics panel on launch
@@ -175,7 +208,7 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSPopoverDelegate {
175
208
  let p = NSPopover()
176
209
  p.contentViewController = NSHostingController(rootView: MainView(scanner: ProjectScanner.shared))
177
210
  p.behavior = .transient
178
- p.contentSize = NSSize(width: 380, height: 520)
211
+ p.contentSize = NSSize(width: 380, height: 560)
179
212
  p.appearance = NSAppearance(named: .darkAqua)
180
213
  p.delegate = self
181
214
  popover = p
@@ -196,8 +229,8 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSPopoverDelegate {
196
229
 
197
230
  let actions: [(String, String, Selector)] = [
198
231
  ("Command Palette", "⌘⇧M", #selector(menuCommandPalette)),
199
- ("Screen Map", "", #selector(menuScreenMap)),
200
- ("Desktop Inventory", "", #selector(menuDesktopInventory)),
232
+ ("Unified Window", "", #selector(menuScreenMap)),
233
+ ("HUD", "", #selector(menuHUD)),
201
234
  ("Window Bezel", "", #selector(menuWindowBezel)),
202
235
  ("Cheat Sheet", "", #selector(menuCheatSheet)),
203
236
  ("Omni Search", "", #selector(menuOmniSearch)),
@@ -232,7 +265,7 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSPopoverDelegate {
232
265
 
233
266
  @objc private func menuCommandPalette() { CommandPaletteWindow.shared.toggle() }
234
267
  @objc private func menuScreenMap() { ScreenMapWindowController.shared.toggle() }
235
- @objc private func menuDesktopInventory() { CommandModeWindow.shared.toggle() }
268
+ @objc private func menuHUD() { HUDController.shared.toggle() }
236
269
  @objc private func menuWindowBezel() { WindowBezel.showBezelForFrontmostWindow() }
237
270
  @objc private func menuCheatSheet() { CheatSheetHUD.shared.toggle() }
238
271
  @objc private func menuOmniSearch() { OmniSearchWindow.shared.toggle() }
@@ -3,25 +3,37 @@ import SwiftUI
3
3
  // MARK: - Navigation Pages
4
4
 
5
5
  enum AppPage: String, CaseIterable {
6
+ case home
6
7
  case screenMap
8
+ case desktopInventory
9
+ case pi
7
10
  case settings
8
11
  case docs
9
12
 
10
13
  var label: String {
11
14
  switch self {
12
- case .screenMap: return "Screen Map"
13
- case .settings: return "Settings"
14
- case .docs: return "Docs"
15
+ case .home: return "Home"
16
+ case .screenMap: return "Screen Map"
17
+ case .desktopInventory: return "Desktop Inventory"
18
+ case .pi: return "Pi"
19
+ case .settings: return "Settings"
20
+ case .docs: return "Docs"
15
21
  }
16
22
  }
17
23
 
18
24
  var icon: String {
19
25
  switch self {
20
- case .screenMap: return "rectangle.3.group"
21
- case .settings: return "gearshape"
22
- case .docs: return "book"
26
+ case .home: return "house"
27
+ case .screenMap: return "rectangle.3.group"
28
+ case .desktopInventory: return "macwindow.on.rectangle"
29
+ case .pi: return "terminal"
30
+ case .settings: return "gearshape"
31
+ case .docs: return "book"
23
32
  }
24
33
  }
34
+
35
+ /// Pages shown as primary tabs in the unified window
36
+ static var primaryTabs: [AppPage] { [.home, .screenMap, .desktopInventory, .pi] }
25
37
  }
26
38
 
27
39
  // MARK: - App Shell View
@@ -29,10 +41,61 @@ enum AppPage: String, CaseIterable {
29
41
  struct AppShellView: View {
30
42
  @ObservedObject var controller: ScreenMapController
31
43
  @ObservedObject var windowController = ScreenMapWindowController.shared
44
+ @StateObject private var commandState = CommandModeState()
32
45
 
33
46
  var body: some View {
34
- contentArea
35
- .background(Palette.bg)
47
+ VStack(spacing: 0) {
48
+ // Tab bar (only on primary pages)
49
+ if AppPage.primaryTabs.contains(windowController.activePage) {
50
+ tabBar
51
+ Rectangle().fill(Palette.border).frame(height: 0.5)
52
+ }
53
+
54
+ contentArea
55
+ }
56
+ .background(Palette.bg)
57
+ .onAppear {
58
+ commandState.onDismiss = { windowController.activePage = .home }
59
+ }
60
+ }
61
+
62
+ // MARK: - Tab Bar
63
+
64
+ private var tabBar: some View {
65
+ HStack(spacing: 0) {
66
+ ForEach(AppPage.primaryTabs, id: \.rawValue) { tab in
67
+ tabButton(tab)
68
+ }
69
+ Spacer()
70
+ }
71
+ .padding(.horizontal, 12)
72
+ .padding(.top, 8)
73
+ .padding(.bottom, 4)
74
+ }
75
+
76
+ private func tabButton(_ tab: AppPage) -> some View {
77
+ let isActive = windowController.activePage == tab
78
+
79
+ return Button {
80
+ windowController.activePage = tab
81
+ if tab == .screenMap { controller.enter() }
82
+ if tab == .desktopInventory { commandState.enter() }
83
+ } label: {
84
+ HStack(spacing: 5) {
85
+ Image(systemName: tab.icon)
86
+ .font(.system(size: 10))
87
+ Text(tab.label)
88
+ .font(Typo.monoBold(11))
89
+ }
90
+ .foregroundColor(isActive ? Palette.text : Palette.textMuted)
91
+ .padding(.horizontal, 12)
92
+ .padding(.vertical, 6)
93
+ .background(
94
+ RoundedRectangle(cornerRadius: 6)
95
+ .fill(isActive ? Palette.surfaceHov : Color.clear)
96
+ )
97
+ }
98
+ .buttonStyle(.plain)
36
99
  }
37
100
 
38
101
  // MARK: - Content Area
@@ -40,10 +103,20 @@ struct AppShellView: View {
40
103
  @ViewBuilder
41
104
  private var contentArea: some View {
42
105
  switch windowController.activePage {
106
+ case .home:
107
+ HomeDashboardView(onNavigate: { page in
108
+ windowController.activePage = page
109
+ if page == .screenMap { controller.enter() }
110
+ if page == .desktopInventory { commandState.enter() }
111
+ })
43
112
  case .screenMap:
44
113
  ScreenMapView(controller: controller, onNavigate: { page in
45
114
  windowController.activePage = page
46
115
  })
116
+ case .desktopInventory:
117
+ CommandModeView(state: commandState)
118
+ case .pi:
119
+ PiWorkspaceView()
47
120
  case .settings:
48
121
  SettingsContentView(
49
122
  prefs: Preferences.shared,