@lattices/cli 0.4.14 → 0.6.0

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 (181) hide show
  1. package/README.md +5 -7
  2. package/apps/mac/Info.plist +4 -4
  3. package/apps/mac/Lattices.app/Contents/Info.plist +4 -12
  4. package/apps/mac/Lattices.app/Contents/MacOS/Lattices +0 -0
  5. package/bin/lattices-app.ts +110 -17
  6. package/bin/lattices-build +125 -0
  7. package/bin/lattices-dev +89 -16
  8. package/bin/lattices.ts +977 -16
  9. package/docs/agents.md +81 -4
  10. package/docs/ai-chat-ux-review.md +416 -0
  11. package/docs/api.md +135 -3
  12. package/docs/app.md +30 -8
  13. package/docs/config.md +4 -0
  14. package/docs/mouse-gestures.md +60 -1
  15. package/docs/proposals/LAT-004-interactive-overlay-actors.md +1 -1
  16. package/docs/proposals/LAT-005-action-runtime-product-spine.md +914 -0
  17. package/docs/proposals/LAT-006-mira-in-lattices.md +553 -0
  18. package/docs/proposals/LAT-007-unified-app-shell.md +128 -0
  19. package/docs/reference/dewey.config.ts +2 -2
  20. package/docs/release.md +171 -0
  21. package/docs/repo-structure.md +5 -5
  22. package/docs/voice.md +11 -27
  23. package/package.json +11 -10
  24. package/apps/mac/Package.swift +0 -27
  25. package/apps/mac/Sources/AppShell/App.swift +0 -26
  26. package/apps/mac/Sources/AppShell/AppActivationCoordinator.swift +0 -27
  27. package/apps/mac/Sources/AppShell/AppDelegate.swift +0 -189
  28. package/apps/mac/Sources/AppShell/AppServicesBootstrap.swift +0 -25
  29. package/apps/mac/Sources/AppShell/AppShellView.swift +0 -171
  30. package/apps/mac/Sources/AppShell/AppUpdater.swift +0 -305
  31. package/apps/mac/Sources/AppShell/CliActionLauncher.swift +0 -50
  32. package/apps/mac/Sources/AppShell/HomeDashboardView.swift +0 -133
  33. package/apps/mac/Sources/AppShell/HotkeyBootstrap.swift +0 -87
  34. package/apps/mac/Sources/AppShell/KeyRecorderView.swift +0 -210
  35. package/apps/mac/Sources/AppShell/LatticesRuntime.swift +0 -104
  36. package/apps/mac/Sources/AppShell/MainView.swift +0 -847
  37. package/apps/mac/Sources/AppShell/MainWindow.swift +0 -83
  38. package/apps/mac/Sources/AppShell/MenuBarController.swift +0 -177
  39. package/apps/mac/Sources/AppShell/OnboardingView.swift +0 -483
  40. package/apps/mac/Sources/AppShell/PermissionsAssistantView.swift +0 -366
  41. package/apps/mac/Sources/AppShell/PermissionsAssistantWindow.swift +0 -70
  42. package/apps/mac/Sources/AppShell/Preferences.swift +0 -297
  43. package/apps/mac/Sources/AppShell/SettingsView.swift +0 -3163
  44. package/apps/mac/Sources/AppShell/SettingsWindow.swift +0 -34
  45. package/apps/mac/Sources/AppShell/WorkspaceInspectorPresenter.swift +0 -13
  46. package/apps/mac/Sources/Core/Actions/HotkeyManager.swift +0 -256
  47. package/apps/mac/Sources/Core/Actions/HotkeyStore.swift +0 -399
  48. package/apps/mac/Sources/Core/Actions/IntentEngine.swift +0 -988
  49. package/apps/mac/Sources/Core/Actions/IntentSchema.swift +0 -94
  50. package/apps/mac/Sources/Core/Actions/Intents/CreateLayerIntent.swift +0 -54
  51. package/apps/mac/Sources/Core/Actions/Intents/DistributeIntent.swift +0 -56
  52. package/apps/mac/Sources/Core/Actions/Intents/FocusIntent.swift +0 -69
  53. package/apps/mac/Sources/Core/Actions/Intents/HelpIntent.swift +0 -41
  54. package/apps/mac/Sources/Core/Actions/Intents/KillIntent.swift +0 -47
  55. package/apps/mac/Sources/Core/Actions/Intents/LatticeIntent.swift +0 -53
  56. package/apps/mac/Sources/Core/Actions/Intents/LaunchIntent.swift +0 -67
  57. package/apps/mac/Sources/Core/Actions/Intents/ListSessionsIntent.swift +0 -32
  58. package/apps/mac/Sources/Core/Actions/Intents/ListWindowsIntent.swift +0 -30
  59. package/apps/mac/Sources/Core/Actions/Intents/ScanIntent.swift +0 -52
  60. package/apps/mac/Sources/Core/Actions/Intents/SearchIntent.swift +0 -190
  61. package/apps/mac/Sources/Core/Actions/Intents/SwitchLayerIntent.swift +0 -50
  62. package/apps/mac/Sources/Core/Actions/Intents/TileIntent.swift +0 -61
  63. package/apps/mac/Sources/Core/Actions/PaletteCommand.swift +0 -439
  64. package/apps/mac/Sources/Core/Actions/VoiceIntentResolver.swift +0 -713
  65. package/apps/mac/Sources/Core/Companion/CompanionActivityLog.swift +0 -70
  66. package/apps/mac/Sources/Core/Companion/CompanionKeyboardController.swift +0 -141
  67. package/apps/mac/Sources/Core/Companion/LatticesCompanionBridgeServer.swift +0 -454
  68. package/apps/mac/Sources/Core/Companion/LatticesCompanionCockpit.swift +0 -555
  69. package/apps/mac/Sources/Core/Companion/LatticesCompanionSecurityCoordinator.swift +0 -629
  70. package/apps/mac/Sources/Core/Companion/LatticesCompanionTrackpadController.swift +0 -204
  71. package/apps/mac/Sources/Core/Companion/LatticesDeckHost.swift +0 -1463
  72. package/apps/mac/Sources/Core/Daemon/DaemonProtocol.swift +0 -114
  73. package/apps/mac/Sources/Core/Daemon/DaemonServer.swift +0 -427
  74. package/apps/mac/Sources/Core/Daemon/LatticesApi.swift +0 -2965
  75. package/apps/mac/Sources/Core/Desktop/AccessibilityTextExtractor.swift +0 -111
  76. package/apps/mac/Sources/Core/Desktop/AppTypeClassifier.swift +0 -106
  77. package/apps/mac/Sources/Core/Desktop/DesktopModel.swift +0 -331
  78. package/apps/mac/Sources/Core/Desktop/DesktopModelTypes.swift +0 -73
  79. package/apps/mac/Sources/Core/Desktop/InventoryManager.swift +0 -35
  80. package/apps/mac/Sources/Core/Desktop/InventoryPath.swift +0 -43
  81. package/apps/mac/Sources/Core/Desktop/MouseFinder.swift +0 -527
  82. package/apps/mac/Sources/Core/Desktop/OcrModel.swift +0 -467
  83. package/apps/mac/Sources/Core/Desktop/OcrStore.swift +0 -329
  84. package/apps/mac/Sources/Core/Desktop/PlacementSpec.swift +0 -195
  85. package/apps/mac/Sources/Core/Desktop/SessionWindowLocator.swift +0 -139
  86. package/apps/mac/Sources/Core/Desktop/TilePickerView.swift +0 -209
  87. package/apps/mac/Sources/Core/Desktop/WindowCapture.swift +0 -33
  88. package/apps/mac/Sources/Core/Desktop/WindowDragSnapController.swift +0 -429
  89. package/apps/mac/Sources/Core/Desktop/WindowPreviewCard.swift +0 -100
  90. package/apps/mac/Sources/Core/Desktop/WindowPreviewStore.swift +0 -112
  91. package/apps/mac/Sources/Core/Desktop/WindowSelectionStore.swift +0 -76
  92. package/apps/mac/Sources/Core/Desktop/WindowTiler.swift +0 -2222
  93. package/apps/mac/Sources/Core/Input/EventTapBreaker.swift +0 -124
  94. package/apps/mac/Sources/Core/Input/EventTapThread.swift +0 -54
  95. package/apps/mac/Sources/Core/Input/InputCaptureResetCenter.swift +0 -20
  96. package/apps/mac/Sources/Core/Input/KeyboardRemapConfig.swift +0 -69
  97. package/apps/mac/Sources/Core/Input/KeyboardRemapController.swift +0 -346
  98. package/apps/mac/Sources/Core/Input/KeyboardRemapStore.swift +0 -141
  99. package/apps/mac/Sources/Core/Input/MouseGestureConfig.swift +0 -499
  100. package/apps/mac/Sources/Core/Input/MouseGestureController.swift +0 -2583
  101. package/apps/mac/Sources/Core/Input/MouseInputDeviceStore.swift +0 -98
  102. package/apps/mac/Sources/Core/Input/MouseInputEventViewer.swift +0 -272
  103. package/apps/mac/Sources/Core/Input/MouseShortcutStore.swift +0 -170
  104. package/apps/mac/Sources/Core/Input/SecureEventInputMonitor.swift +0 -39
  105. package/apps/mac/Sources/Core/Input/ShapeRecognizer.swift +0 -624
  106. package/apps/mac/Sources/Core/Input/TapBudgetMeter.swift +0 -56
  107. package/apps/mac/Sources/Core/Overlays/AppWindowShell.swift +0 -63
  108. package/apps/mac/Sources/Core/Overlays/CommandMode/CommandModeState.swift +0 -1566
  109. package/apps/mac/Sources/Core/Overlays/CommandMode/CommandModeView.swift +0 -1927
  110. package/apps/mac/Sources/Core/Overlays/CommandMode/CommandModeWindow.swift +0 -196
  111. package/apps/mac/Sources/Core/Overlays/CommandPalette/CommandPaletteView.swift +0 -307
  112. package/apps/mac/Sources/Core/Overlays/CommandPalette/CommandPaletteWindow.swift +0 -67
  113. package/apps/mac/Sources/Core/Overlays/HUD/CheatSheetHUD.swift +0 -576
  114. package/apps/mac/Sources/Core/Overlays/HUD/HUDBottomBar.swift +0 -279
  115. package/apps/mac/Sources/Core/Overlays/HUD/HUDController.swift +0 -1158
  116. package/apps/mac/Sources/Core/Overlays/HUD/HUDLeftBar.swift +0 -849
  117. package/apps/mac/Sources/Core/Overlays/HUD/HUDMinimap.swift +0 -179
  118. package/apps/mac/Sources/Core/Overlays/HUD/HUDRightBar.swift +0 -596
  119. package/apps/mac/Sources/Core/Overlays/HUD/HUDState.swift +0 -367
  120. package/apps/mac/Sources/Core/Overlays/HUD/HUDTopBar.swift +0 -243
  121. package/apps/mac/Sources/Core/Overlays/HUD/LauncherHUD.swift +0 -334
  122. package/apps/mac/Sources/Core/Overlays/HUD/LayerBezel.swift +0 -203
  123. package/apps/mac/Sources/Core/Overlays/OmniSearch/OmniSearchState.swift +0 -280
  124. package/apps/mac/Sources/Core/Overlays/OmniSearch/OmniSearchView.swift +0 -422
  125. package/apps/mac/Sources/Core/Overlays/OmniSearch/OmniSearchWindow.swift +0 -94
  126. package/apps/mac/Sources/Core/Overlays/OverlayPanelShell.swift +0 -241
  127. package/apps/mac/Sources/Core/Overlays/ScreenMap/ScreenMapState.swift +0 -3135
  128. package/apps/mac/Sources/Core/Overlays/ScreenMap/ScreenMapView.swift +0 -3977
  129. package/apps/mac/Sources/Core/Overlays/ScreenMap/ScreenMapWindowController.swift +0 -119
  130. package/apps/mac/Sources/Core/Overlays/ScreenOverlayCanvasController.swift +0 -1217
  131. package/apps/mac/Sources/Core/Overlays/Voice/VoiceCommandWindow.swift +0 -1575
  132. package/apps/mac/Sources/Core/Pi/PiAuthNextStepCard.swift +0 -148
  133. package/apps/mac/Sources/Core/Pi/PiAuthPromptCard.swift +0 -90
  134. package/apps/mac/Sources/Core/Pi/PiChatDock.swift +0 -564
  135. package/apps/mac/Sources/Core/Pi/PiChatSession.swift +0 -1948
  136. package/apps/mac/Sources/Core/Pi/PiInstallCallout.swift +0 -86
  137. package/apps/mac/Sources/Core/Pi/PiProviderSetupCallout.swift +0 -99
  138. package/apps/mac/Sources/Core/Pi/PiWorkspaceView.swift +0 -510
  139. package/apps/mac/Sources/Core/System/Capability.swift +0 -79
  140. package/apps/mac/Sources/Core/System/DiagnosticLog.swift +0 -373
  141. package/apps/mac/Sources/Core/System/EventBus.swift +0 -31
  142. package/apps/mac/Sources/Core/System/PermissionChecker.swift +0 -224
  143. package/apps/mac/Sources/Core/System/ProcessModel.swift +0 -199
  144. package/apps/mac/Sources/Core/System/ProcessQuery.swift +0 -151
  145. package/apps/mac/Sources/Core/System/SystemTelemetryMonitor.swift +0 -273
  146. package/apps/mac/Sources/Core/Voice/AdvisorLearningStore.swift +0 -90
  147. package/apps/mac/Sources/Core/Voice/AgentSession.swift +0 -377
  148. package/apps/mac/Sources/Core/Voice/AudioProvider.swift +0 -555
  149. package/apps/mac/Sources/Core/Voice/HandsOffSession.swift +0 -839
  150. package/apps/mac/Sources/Core/Voice/VoiceChatView.swift +0 -192
  151. package/apps/mac/Sources/Core/Voice/VoxClient.swift +0 -454
  152. package/apps/mac/Sources/Core/Workspace/Project.swift +0 -28
  153. package/apps/mac/Sources/Core/Workspace/ProjectScanner.swift +0 -141
  154. package/apps/mac/Sources/Core/Workspace/SessionLayerStore.swift +0 -285
  155. package/apps/mac/Sources/Core/Workspace/SessionManager.swift +0 -75
  156. package/apps/mac/Sources/Core/Workspace/Terminal/Terminal.swift +0 -259
  157. package/apps/mac/Sources/Core/Workspace/Terminal/TerminalQuery.swift +0 -156
  158. package/apps/mac/Sources/Core/Workspace/Terminal/TerminalSynthesizer.swift +0 -200
  159. package/apps/mac/Sources/Core/Workspace/Tmux/TmuxModel.swift +0 -60
  160. package/apps/mac/Sources/Core/Workspace/Tmux/TmuxQuery.swift +0 -105
  161. package/apps/mac/Sources/Core/Workspace/WorkspaceManager.swift +0 -1027
  162. package/apps/mac/Sources/UI/ActionRow.swift +0 -78
  163. package/apps/mac/Sources/UI/OrphanRow.swift +0 -129
  164. package/apps/mac/Sources/UI/ProjectRow.swift +0 -368
  165. package/apps/mac/Sources/UI/TabGroupRow.swift +0 -178
  166. package/apps/mac/Sources/UI/Theme.swift +0 -164
  167. package/apps/mac/Tests/StageDragTests.swift +0 -333
  168. package/apps/mac/Tests/StageJoinTests.swift +0 -313
  169. package/apps/mac/Tests/StageManagerTests.swift +0 -280
  170. package/apps/mac/Tests/StageTileTests.swift +0 -353
  171. package/swift/Package.swift +0 -20
  172. package/swift/Sources/DeckKit/DeckAction.swift +0 -51
  173. package/swift/Sources/DeckKit/DeckBridgeSecurity.swift +0 -152
  174. package/swift/Sources/DeckKit/DeckCockpit.swift +0 -82
  175. package/swift/Sources/DeckKit/DeckHost.swift +0 -7
  176. package/swift/Sources/DeckKit/DeckManifest.swift +0 -145
  177. package/swift/Sources/DeckKit/DeckRuntimeSnapshot.swift +0 -533
  178. package/swift/Sources/DeckKit/DeckTrackpad.swift +0 -63
  179. package/swift/Sources/DeckKit/DeckValue.swift +0 -93
  180. package/swift/Sources/DeckKit/DeckVoiceError.swift +0 -88
  181. package/swift/Tests/DeckKitTests/DeckKitTests.swift +0 -286
@@ -1,377 +0,0 @@
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
- }