@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,329 +0,0 @@
1
- import Foundation
2
- import SQLite3
3
-
4
- // MARK: - Search Result
5
-
6
- struct OcrSearchResult {
7
- let id: Int64
8
- let wid: UInt32
9
- let app: String
10
- let title: String
11
- let frame: WindowFrame
12
- let fullText: String
13
- let snippet: String
14
- let timestamp: Date
15
- let source: TextSource
16
- }
17
-
18
- // MARK: - SQLite OCR Store
19
-
20
- final class OcrStore {
21
- static let shared = OcrStore()
22
-
23
- private var db: OpaquePointer?
24
- private let queue = DispatchQueue(label: "com.arach.lattices.ocrstore", qos: .background)
25
-
26
- // Cached prepared statements
27
- private var insertStmt: OpaquePointer?
28
- private var cleanupStmt: OpaquePointer?
29
-
30
- private let sqliteTransient = unsafeBitCast(-1, to: sqlite3_destructor_type.self)
31
-
32
- // MARK: - Open / Schema
33
-
34
- func open() {
35
- queue.sync {
36
- guard db == nil else { return }
37
-
38
- let dir = NSHomeDirectory() + "/.lattices"
39
- try? FileManager.default.createDirectory(atPath: dir, withIntermediateDirectories: true)
40
- let path = dir + "/ocr.db"
41
-
42
- guard sqlite3_open_v2(path, &db, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE | SQLITE_OPEN_FULLMUTEX, nil) == SQLITE_OK else {
43
- DiagnosticLog.shared.error("OcrStore: failed to open \(path)")
44
- return
45
- }
46
-
47
- // WAL mode for concurrent reads/writes
48
- exec("PRAGMA journal_mode=WAL")
49
- exec("PRAGMA synchronous=NORMAL")
50
-
51
- // Main table
52
- exec("""
53
- CREATE TABLE IF NOT EXISTS ocr_entry (
54
- id INTEGER PRIMARY KEY AUTOINCREMENT,
55
- wid INTEGER NOT NULL,
56
- app TEXT NOT NULL,
57
- title TEXT NOT NULL,
58
- frame_x REAL,
59
- frame_y REAL,
60
- frame_w REAL,
61
- frame_h REAL,
62
- full_text TEXT NOT NULL,
63
- timestamp REAL NOT NULL
64
- )
65
- """)
66
- exec("CREATE INDEX IF NOT EXISTS idx_ocr_entry_timestamp ON ocr_entry(timestamp)")
67
- exec("CREATE INDEX IF NOT EXISTS idx_ocr_entry_wid ON ocr_entry(wid)")
68
-
69
- // FTS5 content-sync table
70
- exec("""
71
- CREATE VIRTUAL TABLE IF NOT EXISTS ocr_fts USING fts5(
72
- full_text, app, title,
73
- content='ocr_entry', content_rowid='id'
74
- )
75
- """)
76
-
77
- // Triggers to keep FTS in sync
78
- exec("""
79
- CREATE TRIGGER IF NOT EXISTS ocr_fts_ai AFTER INSERT ON ocr_entry BEGIN
80
- INSERT INTO ocr_fts(rowid, full_text, app, title)
81
- VALUES (new.id, new.full_text, new.app, new.title);
82
- END
83
- """)
84
- exec("""
85
- CREATE TRIGGER IF NOT EXISTS ocr_fts_ad AFTER DELETE ON ocr_entry BEGIN
86
- INSERT INTO ocr_fts(ocr_fts, rowid, full_text, app, title)
87
- VALUES ('delete', old.id, old.full_text, old.app, old.title);
88
- END
89
- """)
90
- exec("""
91
- CREATE TRIGGER IF NOT EXISTS ocr_fts_au AFTER UPDATE ON ocr_entry BEGIN
92
- INSERT INTO ocr_fts(ocr_fts, rowid, full_text, app, title)
93
- VALUES ('delete', old.id, old.full_text, old.app, old.title);
94
- INSERT INTO ocr_fts(rowid, full_text, app, title)
95
- VALUES (new.id, new.full_text, new.app, new.title);
96
- END
97
- """)
98
-
99
- // Migration: add source column if not present
100
- migrateAddSourceColumn()
101
-
102
- // Prepare cached statements
103
- prepareInsert()
104
- prepareCleanup()
105
-
106
- // Run cleanup on open
107
- cleanupSync(olderThanDays: 3)
108
-
109
- DiagnosticLog.shared.info("OcrStore: opened \(path)")
110
- }
111
- }
112
-
113
- // MARK: - Insert (batch, async)
114
-
115
- func insert(results: [OcrWindowResult]) {
116
- guard !results.isEmpty else { return }
117
- queue.async { [weak self] in
118
- guard let self, let db = self.db, let stmt = self.insertStmt else { return }
119
-
120
- sqlite3_exec(db, "BEGIN TRANSACTION", nil, nil, nil)
121
-
122
- for r in results {
123
- let ts = r.timestamp.timeIntervalSince1970
124
- sqlite3_bind_int(stmt, 1, Int32(r.wid))
125
- self.bindText(stmt, 2, r.app)
126
- self.bindText(stmt, 3, r.title)
127
- sqlite3_bind_double(stmt, 4, r.frame.x)
128
- sqlite3_bind_double(stmt, 5, r.frame.y)
129
- sqlite3_bind_double(stmt, 6, r.frame.w)
130
- sqlite3_bind_double(stmt, 7, r.frame.h)
131
- self.bindText(stmt, 8, r.fullText)
132
- sqlite3_bind_double(stmt, 9, ts)
133
- self.bindText(stmt, 10, r.source.rawValue)
134
- sqlite3_step(stmt)
135
- sqlite3_reset(stmt)
136
- sqlite3_clear_bindings(stmt)
137
- }
138
-
139
- sqlite3_exec(db, "COMMIT", nil, nil, nil)
140
- }
141
- }
142
-
143
- // MARK: - Search (FTS5, synchronous)
144
-
145
- func search(query: String, app: String? = nil, limit: Int = 50) -> [OcrSearchResult] {
146
- guard let db else { return [] }
147
-
148
- var sql = """
149
- SELECT e.id, e.wid, e.app, e.title,
150
- e.frame_x, e.frame_y, e.frame_w, e.frame_h,
151
- e.full_text, e.timestamp,
152
- snippet(ocr_fts, 0, '»', '«', '…', 32) AS snip,
153
- COALESCE(e.source, 'ocr') AS source
154
- FROM ocr_fts f
155
- JOIN ocr_entry e ON e.id = f.rowid
156
- WHERE ocr_fts MATCH ?1
157
- """
158
- if app != nil { sql += " AND e.app = ?2" }
159
- sql += " ORDER BY rank LIMIT ?3"
160
-
161
- var stmt: OpaquePointer?
162
- guard sqlite3_prepare_v2(db, sql, -1, &stmt, nil) == SQLITE_OK else { return [] }
163
- defer { sqlite3_finalize(stmt) }
164
-
165
- bindText(stmt!, 1, query)
166
- if let app { bindText(stmt!, 2, app) }
167
- sqlite3_bind_int(stmt!, 3, Int32(limit))
168
-
169
- var results: [OcrSearchResult] = []
170
- while sqlite3_step(stmt) == SQLITE_ROW {
171
- results.append(rowToSearchResult(stmt!))
172
- }
173
- return results
174
- }
175
-
176
- // MARK: - History (per-window, synchronous)
177
-
178
- func history(wid: UInt32, limit: Int = 50) -> [OcrSearchResult] {
179
- guard let db else { return [] }
180
-
181
- let sql = """
182
- SELECT id, wid, app, title,
183
- frame_x, frame_y, frame_w, frame_h,
184
- full_text, timestamp, '' AS snip,
185
- COALESCE(source, 'ocr') AS source
186
- FROM ocr_entry
187
- WHERE wid = ?1
188
- ORDER BY timestamp DESC
189
- LIMIT ?2
190
- """
191
-
192
- var stmt: OpaquePointer?
193
- guard sqlite3_prepare_v2(db, sql, -1, &stmt, nil) == SQLITE_OK else { return [] }
194
- defer { sqlite3_finalize(stmt) }
195
-
196
- sqlite3_bind_int(stmt!, 1, Int32(wid))
197
- sqlite3_bind_int(stmt!, 2, Int32(limit))
198
-
199
- var results: [OcrSearchResult] = []
200
- while sqlite3_step(stmt) == SQLITE_ROW {
201
- results.append(rowToSearchResult(stmt!))
202
- }
203
- return results
204
- }
205
-
206
- // MARK: - Recent (chronological, synchronous)
207
-
208
- func recent(limit: Int = 50) -> [OcrSearchResult] {
209
- guard let db else { return [] }
210
-
211
- let sql = """
212
- SELECT id, wid, app, title,
213
- frame_x, frame_y, frame_w, frame_h,
214
- full_text, timestamp, '' AS snip,
215
- COALESCE(source, 'ocr') AS source
216
- FROM ocr_entry
217
- ORDER BY timestamp DESC
218
- LIMIT ?1
219
- """
220
-
221
- var stmt: OpaquePointer?
222
- guard sqlite3_prepare_v2(db, sql, -1, &stmt, nil) == SQLITE_OK else { return [] }
223
- defer { sqlite3_finalize(stmt) }
224
-
225
- sqlite3_bind_int(stmt!, 1, Int32(limit))
226
-
227
- var results: [OcrSearchResult] = []
228
- while sqlite3_step(stmt) == SQLITE_ROW {
229
- results.append(rowToSearchResult(stmt!))
230
- }
231
- return results
232
- }
233
-
234
- // MARK: - Cleanup
235
-
236
- private func cleanupSync(olderThanDays days: Int) {
237
- guard let db, let stmt = cleanupStmt else { return }
238
- let cutoff = Date().timeIntervalSince1970 - Double(days * 86400)
239
- sqlite3_bind_double(stmt, 1, cutoff)
240
- sqlite3_step(stmt)
241
- sqlite3_reset(stmt)
242
- sqlite3_clear_bindings(stmt)
243
-
244
- let deleted = sqlite3_changes(db)
245
- if deleted > 0 {
246
- DiagnosticLog.shared.info("OcrStore: cleaned up \(deleted) entries older than \(days) days")
247
- }
248
- }
249
-
250
- // MARK: - Helpers
251
-
252
- private func exec(_ sql: String) {
253
- guard let db else { return }
254
- var err: UnsafeMutablePointer<CChar>?
255
- if sqlite3_exec(db, sql, nil, nil, &err) != SQLITE_OK {
256
- let msg = err.map { String(cString: $0) } ?? "unknown"
257
- DiagnosticLog.shared.error("OcrStore SQL error: \(msg)")
258
- sqlite3_free(err)
259
- }
260
- }
261
-
262
- private func bindText(_ stmt: OpaquePointer, _ index: Int32, _ value: String) {
263
- sqlite3_bind_text(stmt, index, value, -1, sqliteTransient)
264
- }
265
-
266
- private func prepareInsert() {
267
- let sql = """
268
- INSERT INTO ocr_entry (wid, app, title, frame_x, frame_y, frame_w, frame_h, full_text, timestamp, source)
269
- VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10)
270
- """
271
- sqlite3_prepare_v2(db, sql, -1, &insertStmt, nil)
272
- }
273
-
274
- private func migrateAddSourceColumn() {
275
- // Check if source column exists
276
- var tableInfoStmt: OpaquePointer?
277
- let sql = "PRAGMA table_info(ocr_entry)"
278
- guard sqlite3_prepare_v2(db, sql, -1, &tableInfoStmt, nil) == SQLITE_OK else { return }
279
- defer { sqlite3_finalize(tableInfoStmt) }
280
-
281
- var hasSource = false
282
- while sqlite3_step(tableInfoStmt) == SQLITE_ROW {
283
- if let name = sqlite3_column_text(tableInfoStmt, 1) {
284
- if String(cString: name) == "source" {
285
- hasSource = true
286
- break
287
- }
288
- }
289
- }
290
-
291
- if !hasSource {
292
- exec("ALTER TABLE ocr_entry ADD COLUMN source TEXT DEFAULT 'ocr'")
293
- DiagnosticLog.shared.info("OcrStore: migrated — added source column")
294
- }
295
- }
296
-
297
- private func prepareCleanup() {
298
- let sql = "DELETE FROM ocr_entry WHERE timestamp < ?1"
299
- sqlite3_prepare_v2(db, sql, -1, &cleanupStmt, nil)
300
- }
301
-
302
- private func columnText(_ stmt: OpaquePointer, _ index: Int32) -> String {
303
- if let cStr = sqlite3_column_text(stmt, index) {
304
- return String(cString: cStr)
305
- }
306
- return ""
307
- }
308
-
309
- private func rowToSearchResult(_ stmt: OpaquePointer) -> OcrSearchResult {
310
- let sourceStr = columnText(stmt, 11)
311
- let source = TextSource(rawValue: sourceStr) ?? .ocr
312
- return OcrSearchResult(
313
- id: sqlite3_column_int64(stmt, 0),
314
- wid: UInt32(sqlite3_column_int(stmt, 1)),
315
- app: columnText(stmt, 2),
316
- title: columnText(stmt, 3),
317
- frame: WindowFrame(
318
- x: sqlite3_column_double(stmt, 4),
319
- y: sqlite3_column_double(stmt, 5),
320
- w: sqlite3_column_double(stmt, 6),
321
- h: sqlite3_column_double(stmt, 7)
322
- ),
323
- fullText: columnText(stmt, 8),
324
- snippet: columnText(stmt, 10),
325
- timestamp: Date(timeIntervalSince1970: sqlite3_column_double(stmt, 9)),
326
- source: source
327
- )
328
- }
329
- }
@@ -1,195 +0,0 @@
1
- import CoreGraphics
2
- import Foundation
3
-
4
- struct GridPlacement: Equatable {
5
- let columns: Int
6
- let rows: Int
7
- let column: Int
8
- let row: Int
9
-
10
- init?(columns: Int, rows: Int, column: Int, row: Int) {
11
- guard columns > 0, rows > 0,
12
- column >= 0, column < columns,
13
- row >= 0, row < rows else { return nil }
14
- self.columns = columns
15
- self.rows = rows
16
- self.column = column
17
- self.row = row
18
- }
19
-
20
- static func parse(_ str: String) -> GridPlacement? {
21
- let parts = str.split(separator: ":")
22
- guard parts.count == 3, parts[0] == "grid" else { return nil }
23
- let dims = parts[1].split(separator: "x")
24
- let coords = parts[2].split(separator: ",")
25
- guard dims.count == 2, coords.count == 2,
26
- let columns = Int(dims[0]), let rows = Int(dims[1]),
27
- let column = Int(coords[0]), let row = Int(coords[1]) else {
28
- return nil
29
- }
30
- return GridPlacement(columns: columns, rows: rows, column: column, row: row)
31
- }
32
-
33
- var fractions: (CGFloat, CGFloat, CGFloat, CGFloat) {
34
- let w = 1.0 / CGFloat(columns)
35
- let h = 1.0 / CGFloat(rows)
36
- return (CGFloat(column) * w, CGFloat(row) * h, w, h)
37
- }
38
-
39
- var wireValue: String {
40
- "grid:\(columns)x\(rows):\(column),\(row)"
41
- }
42
- }
43
-
44
- struct FractionalPlacement: Equatable {
45
- let x: CGFloat
46
- let y: CGFloat
47
- let w: CGFloat
48
- let h: CGFloat
49
-
50
- init?(x: CGFloat, y: CGFloat, w: CGFloat, h: CGFloat) {
51
- guard x >= 0, y >= 0, w > 0, h > 0,
52
- x + w <= 1.0001, y + h <= 1.0001 else { return nil }
53
- self.x = x
54
- self.y = y
55
- self.w = w
56
- self.h = h
57
- }
58
-
59
- var fractions: (CGFloat, CGFloat, CGFloat, CGFloat) {
60
- (x, y, w, h)
61
- }
62
- }
63
-
64
- enum PlacementSpec: Equatable {
65
- case tile(TilePosition)
66
- case grid(GridPlacement)
67
- case fractions(FractionalPlacement)
68
-
69
- init?(string: String) {
70
- let normalized = PlacementSpec.normalize(string)
71
- if let position = TilePosition(rawValue: normalized) {
72
- self = .tile(position)
73
- return
74
- }
75
- if let alias = PlacementSpec.aliases[normalized], let position = TilePosition(rawValue: alias) {
76
- self = .tile(position)
77
- return
78
- }
79
- if let grid = GridPlacement.parse(normalized) {
80
- self = .grid(grid)
81
- return
82
- }
83
- return nil
84
- }
85
-
86
- init?(json: JSON?) {
87
- guard let json else { return nil }
88
-
89
- if let string = json.stringValue {
90
- self.init(string: string)
91
- return
92
- }
93
-
94
- guard case .object(let obj) = json,
95
- let kind = obj["kind"]?.stringValue?.lowercased() else {
96
- return nil
97
- }
98
-
99
- switch kind {
100
- case "tile", "named", "position":
101
- guard let value = obj["value"]?.stringValue else { return nil }
102
- self.init(string: value)
103
- case "grid":
104
- guard let columns = obj["columns"]?.intValue,
105
- let rows = obj["rows"]?.intValue,
106
- let column = obj["column"]?.intValue,
107
- let row = obj["row"]?.intValue,
108
- let grid = GridPlacement(columns: columns, rows: rows, column: column, row: row) else {
109
- return nil
110
- }
111
- self = .grid(grid)
112
- case "fractions":
113
- guard let x = obj["x"]?.numericDouble,
114
- let y = obj["y"]?.numericDouble,
115
- let w = obj["w"]?.numericDouble,
116
- let h = obj["h"]?.numericDouble,
117
- let placement = FractionalPlacement(
118
- x: CGFloat(x),
119
- y: CGFloat(y),
120
- w: CGFloat(w),
121
- h: CGFloat(h)
122
- ) else {
123
- return nil
124
- }
125
- self = .fractions(placement)
126
- default:
127
- return nil
128
- }
129
- }
130
-
131
- var fractions: (CGFloat, CGFloat, CGFloat, CGFloat) {
132
- switch self {
133
- case .tile(let position):
134
- return position.rect
135
- case .grid(let grid):
136
- return grid.fractions
137
- case .fractions(let placement):
138
- return placement.fractions
139
- }
140
- }
141
-
142
- var wireValue: String {
143
- switch self {
144
- case .tile(let position):
145
- return position.rawValue
146
- case .grid(let grid):
147
- return grid.wireValue
148
- case .fractions(let placement):
149
- return "fractions:\(placement.x),\(placement.y),\(placement.w),\(placement.h)"
150
- }
151
- }
152
-
153
- var jsonValue: JSON {
154
- switch self {
155
- case .tile(let position):
156
- return .object([
157
- "kind": .string("tile"),
158
- "value": .string(position.rawValue),
159
- ])
160
- case .grid(let grid):
161
- return .object([
162
- "kind": .string("grid"),
163
- "columns": .int(grid.columns),
164
- "rows": .int(grid.rows),
165
- "column": .int(grid.column),
166
- "row": .int(grid.row),
167
- ])
168
- case .fractions(let placement):
169
- return .object([
170
- "kind": .string("fractions"),
171
- "x": .double(Double(placement.x)),
172
- "y": .double(Double(placement.y)),
173
- "w": .double(Double(placement.w)),
174
- "h": .double(Double(placement.h)),
175
- ])
176
- }
177
- }
178
-
179
- private static func normalize(_ string: String) -> String {
180
- string
181
- .trimmingCharacters(in: .whitespacesAndNewlines)
182
- .lowercased()
183
- .replacingOccurrences(of: "_", with: "-")
184
- .replacingOccurrences(of: " ", with: "-")
185
- }
186
-
187
- private static let aliases: [String: String] = [
188
- "upper-third": "top-third",
189
- "lower-third": "bottom-third",
190
- "left-quarter": "left-quarter",
191
- "right-quarter": "right-quarter",
192
- "top-quarter": "top-quarter",
193
- "bottom-quarter": "bottom-quarter",
194
- ]
195
- }
@@ -1,139 +0,0 @@
1
- import AppKit
2
- import ApplicationServices
3
- import CoreGraphics
4
-
5
- struct LocatedWindow {
6
- let wid: UInt32
7
- let pid: pid_t
8
- }
9
-
10
- enum SessionWindowLocator {
11
- static func tag(for session: String) -> String {
12
- Terminal.windowTag(for: session)
13
- }
14
-
15
- static func extractSessionName(from title: String) -> String? {
16
- guard let range = title.range(of: #"\[lattices:([^\]]+)\]"#, options: .regularExpression) else {
17
- return nil
18
- }
19
- let match = String(title[range])
20
- return String(match.dropFirst(10).dropLast(1))
21
- }
22
-
23
- static func matches(session: String, title: String, extractedSessionName: String? = nil) -> Bool {
24
- if extractedSessionName == session {
25
- return true
26
- }
27
- return title.contains(tag(for: session))
28
- }
29
-
30
- static func cachedWindow(forSession session: String, in windows: [UInt32: WindowEntry]) -> WindowEntry? {
31
- windows.values.first { entry in
32
- matches(session: session, title: entry.title, extractedSessionName: entry.latticesSession)
33
- }
34
- }
35
-
36
- static func cachedWindow(forSession session: String, desktopModel: DesktopModel = .shared) -> WindowEntry? {
37
- cachedWindow(forSession: session, in: desktopModel.windows)
38
- }
39
-
40
- static func findCGWindow(tag: String) -> LocatedWindow? {
41
- guard let windowList = CGWindowListCopyWindowInfo([.optionAll, .excludeDesktopElements], kCGNullWindowID) as? [[String: Any]] else {
42
- return nil
43
- }
44
-
45
- for info in windowList {
46
- if let name = info[kCGWindowName as String] as? String,
47
- name.contains(tag),
48
- let wid = info[kCGWindowNumber as String] as? UInt32,
49
- let pid = info[kCGWindowOwnerPID as String] as? pid_t {
50
- return LocatedWindow(wid: wid, pid: pid)
51
- }
52
- }
53
- return nil
54
- }
55
-
56
- static func findWindow(session: String, terminal: Terminal) -> LocatedWindow? {
57
- findWindow(tag: tag(for: session), terminal: terminal)
58
- }
59
-
60
- static func findWindow(tag: String, terminal: Terminal) -> LocatedWindow? {
61
- if let match = findCGWindow(tag: tag) {
62
- return match
63
- }
64
-
65
- if let ax = findAXWindow(terminal: terminal, tag: tag),
66
- let wid = matchCGWindow(pid: ax.pid, axWindow: ax.window) {
67
- return LocatedWindow(wid: wid, pid: ax.pid)
68
- }
69
-
70
- return nil
71
- }
72
-
73
- static func findAXWindow(terminal: Terminal, tag: String) -> (pid: pid_t, window: AXUIElement)? {
74
- let diag = DiagnosticLog.shared
75
- guard let app = NSWorkspace.shared.runningApplications.first(where: {
76
- $0.bundleIdentifier == terminal.bundleId
77
- }) else {
78
- diag.error("SessionWindowLocator.findAXWindow: \(terminal.rawValue) (\(terminal.bundleId)) not running")
79
- return nil
80
- }
81
-
82
- let pid = app.processIdentifier
83
- let appRef = AXUIElementCreateApplication(pid)
84
- var windowsRef: CFTypeRef?
85
- let err = AXUIElementCopyAttributeValue(appRef, kAXWindowsAttribute as CFString, &windowsRef)
86
- guard err == .success, let windows = windowsRef as? [AXUIElement] else {
87
- diag.error("SessionWindowLocator.findAXWindow: AX error \(err.rawValue) — Accessibility not granted?")
88
- return nil
89
- }
90
-
91
- diag.info("SessionWindowLocator.findAXWindow: \(windows.count) windows for \(terminal.rawValue), searching for \(tag)")
92
- for win in windows {
93
- var titleRef: CFTypeRef?
94
- AXUIElementCopyAttributeValue(win, kAXTitleAttribute as CFString, &titleRef)
95
- let title = titleRef as? String ?? "<no title>"
96
- if title.contains(tag) {
97
- diag.success("SessionWindowLocator.findAXWindow: matched \"\(title)\"")
98
- return (pid, win)
99
- } else {
100
- diag.info(" skip: \"\(title)\"")
101
- }
102
- }
103
-
104
- diag.warn("SessionWindowLocator.findAXWindow: no window matched tag \(tag)")
105
- return nil
106
- }
107
-
108
- static func matchCGWindow(pid: pid_t, axWindow: AXUIElement) -> UInt32? {
109
- var posRef: CFTypeRef?
110
- var sizeRef: CFTypeRef?
111
- AXUIElementCopyAttributeValue(axWindow, kAXPositionAttribute as CFString, &posRef)
112
- AXUIElementCopyAttributeValue(axWindow, kAXSizeAttribute as CFString, &sizeRef)
113
- guard let pv = posRef, let sv = sizeRef else { return nil }
114
-
115
- var pos = CGPoint.zero
116
- var size = CGSize.zero
117
- AXValueGetValue(pv as! AXValue, .cgPoint, &pos)
118
- AXValueGetValue(sv as! AXValue, .cgSize, &size)
119
-
120
- guard let windowList = CGWindowListCopyWindowInfo([.optionAll, .excludeDesktopElements], kCGNullWindowID) as? [[String: Any]] else {
121
- return nil
122
- }
123
-
124
- for info in windowList {
125
- guard let wPid = info[kCGWindowOwnerPID as String] as? pid_t,
126
- wPid == pid,
127
- let wid = info[kCGWindowNumber as String] as? UInt32,
128
- let boundsDict = info[kCGWindowBounds as String] as? NSDictionary else { continue }
129
- var rect = CGRect.zero
130
- if CGRectMakeWithDictionaryRepresentation(boundsDict, &rect) {
131
- if abs(rect.origin.x - pos.x) < 2 && abs(rect.origin.y - pos.y) < 2 &&
132
- abs(rect.width - size.width) < 2 && abs(rect.height - size.height) < 2 {
133
- return wid
134
- }
135
- }
136
- }
137
- return nil
138
- }
139
- }