@arach/lattices 0.2.0 → 0.6.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 (143) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +172 -86
  3. package/apps/mac/Info.plist +43 -0
  4. package/apps/mac/Lattices.app/Contents/Info.plist +43 -0
  5. package/apps/mac/Lattices.app/Contents/MacOS/Lattices +0 -0
  6. package/apps/mac/Lattices.app/Contents/Resources/AppIcon.icns +0 -0
  7. package/apps/mac/Lattices.app/Contents/Resources/docs/assistant-knowledge.md +130 -0
  8. package/apps/mac/Lattices.app/Contents/Resources/tap.wav +0 -0
  9. package/apps/mac/Lattices.app/Contents/_CodeSignature/CodeResources +150 -0
  10. package/apps/mac/Lattices.entitlements +21 -0
  11. package/apps/mac/Resources/Pets/assistant-spark/pet.json +62 -0
  12. package/apps/mac/Resources/Pets/assistant-spark/spritesheet.webp +0 -0
  13. package/apps/mac/Resources/Pets/scout-ranger/pet.json +6 -0
  14. package/apps/mac/Resources/Pets/scout-ranger/spritesheet.webp +0 -0
  15. package/apps/mac/Resources/tap.wav +0 -0
  16. package/assets/AppIcon.icns +0 -0
  17. package/bin/assistant-intelligence.ts +912 -0
  18. package/bin/cli/capture.ts +252 -0
  19. package/bin/cli/daemon.ts +22 -0
  20. package/bin/cli/helpers.ts +105 -0
  21. package/bin/cli/layer.ts +178 -0
  22. package/bin/cli/runs.ts +43 -0
  23. package/bin/cli/search.ts +141 -0
  24. package/bin/cli/session.ts +32 -0
  25. package/bin/client.ts +17 -0
  26. package/bin/cua.ts +26 -0
  27. package/bin/{daemon-client.js → daemon-client.ts} +49 -30
  28. package/bin/handsoff-infer.ts +96 -0
  29. package/bin/handsoff-worker.ts +531 -0
  30. package/bin/infer.ts +424 -0
  31. package/bin/keychain.ts +75 -0
  32. package/bin/lattices-app.ts +655 -0
  33. package/bin/lattices-build +125 -0
  34. package/bin/lattices-build-env.ts +77 -0
  35. package/bin/lattices-dev +362 -0
  36. package/bin/lattices.ts +3260 -0
  37. package/bin/project-twin.ts +645 -0
  38. package/docs/agent-execution-plan.md +562 -0
  39. package/docs/agent-layer-guide.md +207 -0
  40. package/docs/agents.md +233 -0
  41. package/docs/ai-chat-ux-review.md +416 -0
  42. package/docs/api.md +1041 -47
  43. package/docs/app.md +96 -13
  44. package/docs/assistant-knowledge.md +130 -0
  45. package/docs/companion-deck.md +209 -0
  46. package/docs/component-extraction-roadmap.md +392 -0
  47. package/docs/concepts.md +13 -12
  48. package/docs/config.md +83 -10
  49. package/docs/gesture-customization-proposal.md +520 -0
  50. package/docs/handsoff-test-scenarios.md +84 -0
  51. package/docs/hyperspace-grid-snappiness.md +210 -0
  52. package/docs/layers.md +176 -28
  53. package/docs/mouse-gestures.md +244 -0
  54. package/docs/ocr.md +21 -9
  55. package/docs/overview.md +42 -23
  56. package/docs/presentation-execution-review.md +491 -0
  57. package/docs/prompts/hands-off-system.md +382 -0
  58. package/docs/prompts/hands-off-turn.md +30 -0
  59. package/docs/prompts/voice-advisor.md +31 -0
  60. package/docs/prompts/voice-fallback.md +23 -0
  61. package/docs/proposals/LAT-001-gesture-visual-customization.md +522 -0
  62. package/docs/proposals/LAT-002-shared-overlay-canvas.md +353 -0
  63. package/docs/proposals/LAT-003-menu-bar-controller-architecture.md +291 -0
  64. package/docs/proposals/LAT-004-interactive-overlay-actors.md +534 -0
  65. package/docs/proposals/LAT-005-action-runtime-product-spine.md +914 -0
  66. package/docs/proposals/LAT-006-followup-gaps.md +103 -0
  67. package/docs/proposals/LAT-006-runs-and-capture-in-lattices.md +566 -0
  68. package/docs/proposals/LAT-007-unified-app-shell.md +128 -0
  69. package/docs/quickstart.md +8 -12
  70. package/docs/reference/dewey.config.ts +74 -0
  71. package/docs/reference/install-agent.md +79 -0
  72. package/docs/release.md +172 -0
  73. package/docs/repo-structure.md +100 -0
  74. package/docs/terminal-kit.md +87 -0
  75. package/docs/tiling-reference.md +224 -0
  76. package/docs/twins.md +138 -0
  77. package/docs/voice-command-protocol.md +278 -0
  78. package/docs/voice-error-model.md +73 -0
  79. package/docs/voice.md +221 -0
  80. package/package.json +69 -16
  81. package/packages/npm/sdk/cua.d.mts +1 -0
  82. package/packages/npm/sdk/cua.d.ts +188 -0
  83. package/packages/npm/sdk/cua.mjs +376 -0
  84. package/app/Lattices.app/Contents/Info.plist +0 -24
  85. package/app/Package.swift +0 -13
  86. package/app/Sources/ActionRow.swift +0 -61
  87. package/app/Sources/App.swift +0 -10
  88. package/app/Sources/AppDelegate.swift +0 -234
  89. package/app/Sources/AppShellView.swift +0 -62
  90. package/app/Sources/AppTypeClassifier.swift +0 -70
  91. package/app/Sources/AppWindowShell.swift +0 -63
  92. package/app/Sources/CheatSheetHUD.swift +0 -332
  93. package/app/Sources/CommandModeState.swift +0 -1362
  94. package/app/Sources/CommandModeView.swift +0 -1405
  95. package/app/Sources/CommandModeWindow.swift +0 -192
  96. package/app/Sources/CommandPaletteView.swift +0 -307
  97. package/app/Sources/CommandPaletteWindow.swift +0 -134
  98. package/app/Sources/DaemonProtocol.swift +0 -101
  99. package/app/Sources/DaemonServer.swift +0 -414
  100. package/app/Sources/DesktopModel.swift +0 -121
  101. package/app/Sources/DesktopModelTypes.swift +0 -71
  102. package/app/Sources/DiagnosticLog.swift +0 -271
  103. package/app/Sources/EventBus.swift +0 -30
  104. package/app/Sources/HotkeyManager.swift +0 -250
  105. package/app/Sources/HotkeyStore.swift +0 -338
  106. package/app/Sources/InventoryManager.swift +0 -35
  107. package/app/Sources/InventoryPath.swift +0 -43
  108. package/app/Sources/KeyRecorderView.swift +0 -210
  109. package/app/Sources/LatticesApi.swift +0 -1125
  110. package/app/Sources/MainView.swift +0 -467
  111. package/app/Sources/MainWindow.swift +0 -83
  112. package/app/Sources/OcrModel.swift +0 -309
  113. package/app/Sources/OcrStore.swift +0 -295
  114. package/app/Sources/OmniSearchState.swift +0 -283
  115. package/app/Sources/OmniSearchView.swift +0 -288
  116. package/app/Sources/OmniSearchWindow.swift +0 -105
  117. package/app/Sources/OrphanRow.swift +0 -129
  118. package/app/Sources/PaletteCommand.swift +0 -419
  119. package/app/Sources/PermissionChecker.swift +0 -125
  120. package/app/Sources/Preferences.swift +0 -92
  121. package/app/Sources/ProcessModel.swift +0 -199
  122. package/app/Sources/ProcessQuery.swift +0 -151
  123. package/app/Sources/Project.swift +0 -28
  124. package/app/Sources/ProjectRow.swift +0 -368
  125. package/app/Sources/ProjectScanner.swift +0 -121
  126. package/app/Sources/ScreenMapState.swift +0 -2387
  127. package/app/Sources/ScreenMapView.swift +0 -2820
  128. package/app/Sources/ScreenMapWindowController.swift +0 -89
  129. package/app/Sources/SessionManager.swift +0 -72
  130. package/app/Sources/SettingsView.swift +0 -1053
  131. package/app/Sources/SettingsWindow.swift +0 -20
  132. package/app/Sources/TabGroupRow.swift +0 -178
  133. package/app/Sources/Terminal.swift +0 -259
  134. package/app/Sources/TerminalQuery.swift +0 -156
  135. package/app/Sources/TerminalSynthesizer.swift +0 -200
  136. package/app/Sources/Theme.swift +0 -163
  137. package/app/Sources/TilePickerView.swift +0 -209
  138. package/app/Sources/TmuxModel.swift +0 -53
  139. package/app/Sources/TmuxQuery.swift +0 -81
  140. package/app/Sources/WindowTiler.swift +0 -1755
  141. package/app/Sources/WorkspaceManager.swift +0 -434
  142. package/bin/lattices-app.js +0 -221
  143. package/bin/lattices.js +0 -1418
@@ -1,101 +0,0 @@
1
- import Foundation
2
-
3
- // MARK: - Wire Format
4
-
5
- struct DaemonRequest: Codable {
6
- let id: String
7
- let method: String
8
- let params: JSON?
9
- }
10
-
11
- struct DaemonResponse: Codable {
12
- let id: String
13
- let result: JSON?
14
- let error: String?
15
- }
16
-
17
- struct DaemonEvent: Codable {
18
- let event: String
19
- let data: JSON
20
- }
21
-
22
- // MARK: - Dynamic JSON
23
-
24
- enum JSON: Codable, Equatable {
25
- case string(String)
26
- case int(Int)
27
- case double(Double)
28
- case bool(Bool)
29
- case array([JSON])
30
- case object([String: JSON])
31
- case null
32
-
33
- // MARK: Codable
34
-
35
- init(from decoder: Decoder) throws {
36
- let container = try decoder.singleValueContainer()
37
-
38
- if container.decodeNil() {
39
- self = .null
40
- } else if let b = try? container.decode(Bool.self) {
41
- self = .bool(b)
42
- } else if let i = try? container.decode(Int.self) {
43
- self = .int(i)
44
- } else if let d = try? container.decode(Double.self) {
45
- self = .double(d)
46
- } else if let s = try? container.decode(String.self) {
47
- self = .string(s)
48
- } else if let arr = try? container.decode([JSON].self) {
49
- self = .array(arr)
50
- } else if let obj = try? container.decode([String: JSON].self) {
51
- self = .object(obj)
52
- } else {
53
- throw DecodingError.dataCorruptedError(in: container, debugDescription: "Cannot decode JSON value")
54
- }
55
- }
56
-
57
- func encode(to encoder: Encoder) throws {
58
- var container = encoder.singleValueContainer()
59
- switch self {
60
- case .string(let s): try container.encode(s)
61
- case .int(let i): try container.encode(i)
62
- case .double(let d): try container.encode(d)
63
- case .bool(let b): try container.encode(b)
64
- case .array(let a): try container.encode(a)
65
- case .object(let o): try container.encode(o)
66
- case .null: try container.encodeNil()
67
- }
68
- }
69
-
70
- // MARK: Subscript helpers
71
-
72
- subscript(key: String) -> JSON? {
73
- guard case .object(let dict) = self else { return nil }
74
- return dict[key]
75
- }
76
-
77
- subscript(index: Int) -> JSON? {
78
- guard case .array(let arr) = self, index >= 0, index < arr.count else { return nil }
79
- return arr[index]
80
- }
81
-
82
- var stringValue: String? {
83
- guard case .string(let s) = self else { return nil }
84
- return s
85
- }
86
-
87
- var intValue: Int? {
88
- guard case .int(let i) = self else { return nil }
89
- return i
90
- }
91
-
92
- var uint32Value: UInt32? {
93
- guard case .int(let i) = self else { return nil }
94
- return UInt32(i)
95
- }
96
-
97
- var boolValue: Bool? {
98
- guard case .bool(let b) = self else { return nil }
99
- return b
100
- }
101
- }
@@ -1,414 +0,0 @@
1
- import Foundation
2
- import CommonCrypto
3
-
4
- // MARK: - POSIX WebSocket Server
5
- // NWListener is broken on macOS 26 (Tahoe) — EINVAL on any listener creation.
6
- // This is a minimal POSIX-socket WebSocket server on 127.0.0.1:9399.
7
-
8
- final class DaemonServer: ObservableObject {
9
- static let shared = DaemonServer()
10
-
11
- @Published var clientCount: Int = 0
12
- @Published var isListening: Bool = false
13
-
14
- private var serverFd: Int32 = -1
15
- private var clients: [UUID: WebSocketClient] = [:]
16
- private let lock = NSLock()
17
- private let queue = DispatchQueue(label: "lattices.daemon", qos: .userInitiated)
18
- private let encoder = JSONEncoder()
19
- private let decoder = JSONDecoder()
20
- private var acceptSource: DispatchSourceRead?
21
-
22
- func start() {
23
- let diag = DiagnosticLog.shared
24
-
25
- // 1. Create TCP socket
26
- serverFd = socket(AF_INET, SOCK_STREAM, 0)
27
- guard serverFd >= 0 else {
28
- diag.error("DaemonServer: socket() failed — errno \(errno)")
29
- return
30
- }
31
-
32
- // SO_REUSEADDR so we can restart quickly
33
- var yes: Int32 = 1
34
- setsockopt(serverFd, SOL_SOCKET, SO_REUSEADDR, &yes, socklen_t(MemoryLayout<Int32>.size))
35
-
36
- // 2. Bind to 127.0.0.1:9399
37
- var addr = sockaddr_in()
38
- addr.sin_len = UInt8(MemoryLayout<sockaddr_in>.size)
39
- addr.sin_family = sa_family_t(AF_INET)
40
- addr.sin_port = UInt16(9399).bigEndian
41
- addr.sin_addr.s_addr = UInt32(0x7f000001).bigEndian // 127.0.0.1
42
-
43
- let bindResult = withUnsafePointer(to: &addr) {
44
- $0.withMemoryRebound(to: sockaddr.self, capacity: 1) {
45
- bind(serverFd, $0, socklen_t(MemoryLayout<sockaddr_in>.size))
46
- }
47
- }
48
- guard bindResult == 0 else {
49
- diag.error("DaemonServer: bind() failed — errno \(errno)")
50
- close(serverFd)
51
- serverFd = -1
52
- return
53
- }
54
-
55
- // 3. Listen
56
- guard listen(serverFd, 8) == 0 else {
57
- diag.error("DaemonServer: listen() failed — errno \(errno)")
58
- close(serverFd)
59
- serverFd = -1
60
- return
61
- }
62
-
63
- // Non-blocking
64
- let flags = fcntl(serverFd, F_GETFL)
65
- _ = fcntl(serverFd, F_SETFL, flags | O_NONBLOCK)
66
-
67
- // 4. GCD dispatch source for accepting connections
68
- let source = DispatchSource.makeReadSource(fileDescriptor: serverFd, queue: queue)
69
- source.setEventHandler { [weak self] in self?.acceptConnection() }
70
- source.setCancelHandler { [weak self] in
71
- if let fd = self?.serverFd, fd >= 0 { close(fd) }
72
- self?.serverFd = -1
73
- }
74
- source.resume()
75
- acceptSource = source
76
-
77
- DispatchQueue.main.async { self.isListening = true }
78
- diag.success("DaemonServer: listening on ws://127.0.0.1:9399")
79
-
80
- // Subscribe to EventBus for broadcasting
81
- EventBus.shared.subscribe { [weak self] event in
82
- self?.broadcastEvent(event)
83
- }
84
- }
85
-
86
- func stop() {
87
- acceptSource?.cancel()
88
- acceptSource = nil
89
- lock.lock()
90
- for (_, client) in clients {
91
- close(client.fd)
92
- }
93
- clients.removeAll()
94
- lock.unlock()
95
- DispatchQueue.main.async {
96
- self.clientCount = 0
97
- self.isListening = false
98
- }
99
- }
100
-
101
- func broadcast(_ event: DaemonEvent) {
102
- guard let data = try? encoder.encode(event),
103
- let text = String(data: data, encoding: .utf8) else { return }
104
- lock.lock()
105
- let snapshot = clients
106
- lock.unlock()
107
- for (_, client) in snapshot {
108
- sendWebSocketText(text, to: client)
109
- }
110
- }
111
-
112
- // MARK: - Accept
113
-
114
- private func acceptConnection() {
115
- var clientAddr = sockaddr_in()
116
- var addrLen = socklen_t(MemoryLayout<sockaddr_in>.size)
117
- let clientFd = withUnsafeMutablePointer(to: &clientAddr) {
118
- $0.withMemoryRebound(to: sockaddr.self, capacity: 1) {
119
- accept(serverFd, $0, &addrLen)
120
- }
121
- }
122
- guard clientFd >= 0 else { return }
123
-
124
- let id = UUID()
125
- let client = WebSocketClient(id: id, fd: clientFd)
126
-
127
- // Read the HTTP upgrade request
128
- queue.async { [weak self] in
129
- self?.performHandshake(client: client)
130
- }
131
- }
132
-
133
- // MARK: - WebSocket Handshake
134
-
135
- private func performHandshake(client: WebSocketClient) {
136
- let diag = DiagnosticLog.shared
137
-
138
- // Ensure blocking mode for handshake read
139
- let curFlags = fcntl(client.fd, F_GETFL)
140
- if curFlags & O_NONBLOCK != 0 {
141
- _ = fcntl(client.fd, F_SETFL, curFlags & ~O_NONBLOCK)
142
- }
143
-
144
- // Read HTTP request (up to 4KB)
145
- var buf = [UInt8](repeating: 0, count: 4096)
146
- let n = read(client.fd, &buf, buf.count)
147
- guard n > 0 else {
148
- close(client.fd)
149
- return
150
- }
151
-
152
- let request = String(bytes: buf[..<n], encoding: .utf8) ?? ""
153
-
154
- // Extract Sec-WebSocket-Key
155
- guard let keyLine = request.split(separator: "\r\n").first(where: {
156
- $0.lowercased().hasPrefix("sec-websocket-key:")
157
- }) else {
158
- close(client.fd)
159
- return
160
- }
161
- let key = keyLine.split(separator: ":", maxSplits: 1)[1].trimmingCharacters(in: .whitespaces)
162
-
163
- // Compute accept key: Base64(SHA1(key + magic))
164
- let magic = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"
165
- let combined = key + magic
166
- let acceptKey = sha1Base64(combined)
167
-
168
- // Send HTTP 101 response
169
- let response = "HTTP/1.1 101 Switching Protocols\r\n" +
170
- "Upgrade: websocket\r\n" +
171
- "Connection: Upgrade\r\n" +
172
- "Sec-WebSocket-Accept: \(acceptKey)\r\n\r\n"
173
- let responseBytes = Array(response.utf8)
174
- responseBytes.withUnsafeBufferPointer { ptr in
175
- _ = write(client.fd, ptr.baseAddress!, ptr.count)
176
- }
177
-
178
- // Register client
179
- lock.lock()
180
- clients[client.id] = client
181
- let count = clients.count
182
- lock.unlock()
183
- DispatchQueue.main.async { self.clientCount = count }
184
- diag.info("DaemonServer: client connected (\(count) total)")
185
-
186
- // Start read loop
187
- readLoop(client: client)
188
- }
189
-
190
- // MARK: - WebSocket Frame I/O
191
-
192
- private func readLoop(client: WebSocketClient) {
193
- // Make non-blocking and use a dispatch source
194
- let flags = fcntl(client.fd, F_GETFL)
195
- _ = fcntl(client.fd, F_SETFL, flags | O_NONBLOCK)
196
-
197
- let source = DispatchSource.makeReadSource(fileDescriptor: client.fd, queue: queue)
198
- client.readSource = source
199
- source.setEventHandler { [weak self] in
200
- self?.readFrame(client: client)
201
- }
202
- source.setCancelHandler { [weak self] in
203
- self?.removeClient(client)
204
- }
205
- source.resume()
206
- }
207
-
208
- private func readFrame(client: WebSocketClient) {
209
- // Read available data into client buffer
210
- var buf = [UInt8](repeating: 0, count: 65536)
211
- let n = read(client.fd, &buf, buf.count)
212
- if n <= 0 {
213
- client.readSource?.cancel()
214
- return
215
- }
216
- client.buffer.append(contentsOf: buf[..<n])
217
-
218
- // Process complete frames
219
- while let frame = parseFrame(&client.buffer) {
220
- switch frame.opcode {
221
- case 0x1: // Text
222
- if let text = String(bytes: frame.payload, encoding: .utf8),
223
- let data = text.data(using: .utf8) {
224
- handleMessage(data, client: client)
225
- }
226
- case 0x8: // Close
227
- // Send close frame back
228
- sendFrame(opcode: 0x8, payload: [], to: client)
229
- client.readSource?.cancel()
230
- return
231
- case 0x9: // Ping → Pong
232
- sendFrame(opcode: 0xA, payload: frame.payload, to: client)
233
- case 0xA: // Pong — ignore
234
- break
235
- default:
236
- break
237
- }
238
- }
239
- }
240
-
241
- private struct WSFrame {
242
- let opcode: UInt8
243
- let payload: [UInt8]
244
- }
245
-
246
- private func parseFrame(_ buffer: inout [UInt8]) -> WSFrame? {
247
- guard buffer.count >= 2 else { return nil }
248
-
249
- let byte0 = buffer[0]
250
- let byte1 = buffer[1]
251
- let opcode = byte0 & 0x0F
252
- let masked = (byte1 & 0x80) != 0
253
- var payloadLen = UInt64(byte1 & 0x7F)
254
- var offset = 2
255
-
256
- if payloadLen == 126 {
257
- guard buffer.count >= 4 else { return nil }
258
- payloadLen = UInt64(buffer[2]) << 8 | UInt64(buffer[3])
259
- offset = 4
260
- } else if payloadLen == 127 {
261
- guard buffer.count >= 10 else { return nil }
262
- payloadLen = 0
263
- for i in 0..<8 { payloadLen = payloadLen << 8 | UInt64(buffer[2 + i]) }
264
- offset = 10
265
- }
266
-
267
- let maskSize = masked ? 4 : 0
268
- let totalNeeded = offset + maskSize + Int(payloadLen)
269
- guard buffer.count >= totalNeeded else { return nil }
270
-
271
- var payload: [UInt8]
272
- if masked {
273
- let mask = Array(buffer[offset..<(offset + 4)])
274
- let dataStart = offset + 4
275
- payload = Array(buffer[dataStart..<(dataStart + Int(payloadLen))])
276
- for i in 0..<payload.count {
277
- payload[i] ^= mask[i % 4]
278
- }
279
- } else {
280
- payload = Array(buffer[offset..<(offset + Int(payloadLen))])
281
- }
282
-
283
- buffer.removeFirst(totalNeeded)
284
- return WSFrame(opcode: opcode, payload: payload)
285
- }
286
-
287
- private func sendFrame(opcode: UInt8, payload: [UInt8], to client: WebSocketClient) {
288
- var frame: [UInt8] = [0x80 | opcode] // FIN + opcode
289
-
290
- if payload.count < 126 {
291
- frame.append(UInt8(payload.count))
292
- } else if payload.count < 65536 {
293
- frame.append(126)
294
- frame.append(UInt8((payload.count >> 8) & 0xFF))
295
- frame.append(UInt8(payload.count & 0xFF))
296
- } else {
297
- frame.append(127)
298
- for i in (0..<8).reversed() {
299
- frame.append(UInt8((payload.count >> (i * 8)) & 0xFF))
300
- }
301
- }
302
- frame.append(contentsOf: payload)
303
-
304
- frame.withUnsafeBufferPointer { ptr in
305
- _ = write(client.fd, ptr.baseAddress!, ptr.count)
306
- }
307
- }
308
-
309
- private func sendWebSocketText(_ text: String, to client: WebSocketClient) {
310
- let payload = Array(text.utf8)
311
- sendFrame(opcode: 0x1, payload: payload, to: client)
312
- }
313
-
314
- // MARK: - Message Handling
315
-
316
- private func handleMessage(_ data: Data, client: WebSocketClient) {
317
- guard let request = try? decoder.decode(DaemonRequest.self, from: data) else {
318
- let errResponse = DaemonResponse(id: "?", result: nil, error: "Invalid request JSON")
319
- sendResponse(errResponse, to: client)
320
- return
321
- }
322
-
323
- let response = LatticesApi.shared.handle(request)
324
- sendResponse(response, to: client)
325
- }
326
-
327
- private func sendResponse(_ response: DaemonResponse, to client: WebSocketClient) {
328
- guard let data = try? encoder.encode(response),
329
- let text = String(data: data, encoding: .utf8) else { return }
330
- sendWebSocketText(text, to: client)
331
- }
332
-
333
- // MARK: - Client Management
334
-
335
- private func removeClient(_ client: WebSocketClient) {
336
- close(client.fd)
337
- lock.lock()
338
- clients.removeValue(forKey: client.id)
339
- let count = clients.count
340
- lock.unlock()
341
- DispatchQueue.main.async { self.clientCount = count }
342
- DiagnosticLog.shared.info("DaemonServer: client disconnected (\(count) total)")
343
- }
344
-
345
- // MARK: - Crypto Helper
346
-
347
- private func sha1Base64(_ string: String) -> String {
348
- let data = Array(string.utf8)
349
- var hash = [UInt8](repeating: 0, count: Int(CC_SHA1_DIGEST_LENGTH))
350
- CC_SHA1(data, CC_LONG(data.count), &hash)
351
- return Data(hash).base64EncodedString()
352
- }
353
-
354
- // MARK: - Event Broadcasting
355
-
356
- private func broadcastEvent(_ event: ModelEvent) {
357
- let daemonEvent: DaemonEvent
358
- switch event {
359
- case .windowsChanged(let windows, let added, let removed):
360
- daemonEvent = DaemonEvent(
361
- event: "windows.changed",
362
- data: .object([
363
- "windowCount": .int(windows.count),
364
- "added": .array(added.map { .int(Int($0)) }),
365
- "removed": .array(removed.map { .int(Int($0)) })
366
- ])
367
- )
368
- case .tmuxChanged(let sessions):
369
- daemonEvent = DaemonEvent(
370
- event: "tmux.changed",
371
- data: .object([
372
- "sessionCount": .int(sessions.count),
373
- "sessions": .array(sessions.map { .string($0.name) })
374
- ])
375
- )
376
- case .layerSwitched(let index):
377
- daemonEvent = DaemonEvent(
378
- event: "layer.switched",
379
- data: .object(["index": .int(index)])
380
- )
381
- case .processesChanged(let interesting):
382
- daemonEvent = DaemonEvent(
383
- event: "processes.changed",
384
- data: .object([
385
- "interestingCount": .int(interesting.count),
386
- "pids": .array(interesting.map { .int($0) })
387
- ])
388
- )
389
- case .ocrScanComplete(let windowCount, let totalBlocks):
390
- daemonEvent = DaemonEvent(
391
- event: "ocr.scanComplete",
392
- data: .object([
393
- "windowCount": .int(windowCount),
394
- "totalBlocks": .int(totalBlocks)
395
- ])
396
- )
397
- }
398
- broadcast(daemonEvent)
399
- }
400
- }
401
-
402
- // MARK: - Client State
403
-
404
- final class WebSocketClient {
405
- let id: UUID
406
- let fd: Int32
407
- var buffer: [UInt8] = []
408
- var readSource: DispatchSourceRead?
409
-
410
- init(id: UUID, fd: Int32) {
411
- self.id = id
412
- self.fd = fd
413
- }
414
- }
@@ -1,121 +0,0 @@
1
- import AppKit
2
- import CoreGraphics
3
-
4
- final class DesktopModel: ObservableObject {
5
- static let shared = DesktopModel()
6
-
7
- @Published private(set) var windows: [UInt32: WindowEntry] = [:]
8
- private var timer: Timer?
9
-
10
- func start(interval: TimeInterval = 1.5) {
11
- guard timer == nil else { return }
12
- DiagnosticLog.shared.info("DesktopModel: starting (interval=\(interval)s)")
13
- poll()
14
- timer = Timer.scheduledTimer(withTimeInterval: interval, repeats: true) { [weak self] _ in
15
- self?.poll()
16
- }
17
- }
18
-
19
- func stop() {
20
- timer?.invalidate()
21
- timer = nil
22
- }
23
-
24
- func allWindows() -> [WindowEntry] {
25
- Array(windows.values).sorted { $0.wid < $1.wid }
26
- }
27
-
28
- func windowForSession(_ session: String) -> WindowEntry? {
29
- let tag = Terminal.windowTag(for: session)
30
- return windows.values.first { $0.title.contains(tag) }
31
- }
32
-
33
- // MARK: - Polling
34
-
35
- func poll() {
36
- guard let list = CGWindowListCopyWindowInfo(
37
- [.optionOnScreenOnly, .excludeDesktopElements],
38
- kCGNullWindowID
39
- ) as? [[String: Any]] else { return }
40
-
41
- var fresh: [UInt32: WindowEntry] = [:]
42
-
43
- for info in list {
44
- guard let wid = info[kCGWindowNumber as String] as? UInt32,
45
- let ownerName = info[kCGWindowOwnerName as String] as? String,
46
- let pid = info[kCGWindowOwnerPID as String] as? Int32,
47
- let boundsDict = info[kCGWindowBounds as String] as? NSDictionary
48
- else { continue }
49
-
50
- // Skip tiny windows (menu extras, status items)
51
- var rect = CGRect.zero
52
- guard CGRectMakeWithDictionaryRepresentation(boundsDict, &rect),
53
- rect.width >= 50, rect.height >= 50 else { continue }
54
-
55
- let title = info[kCGWindowName as String] as? String ?? ""
56
- let layer = info[kCGWindowLayer as String] as? Int ?? 0
57
- let isOnScreen = info[kCGWindowIsOnscreen as String] as? Bool ?? true
58
-
59
- // Skip non-standard layers (menus, overlays)
60
- guard layer == 0 else { continue }
61
-
62
- let frame = WindowFrame(
63
- x: Double(rect.origin.x),
64
- y: Double(rect.origin.y),
65
- w: Double(rect.width),
66
- h: Double(rect.height)
67
- )
68
-
69
- let spaceIds = WindowTiler.getSpacesForWindow(wid)
70
-
71
- // Extract lattices session tag from title: [lattices:session-name]
72
- var latticesSession: String?
73
- if let range = title.range(of: #"\[lattices:([^\]]+)\]"#, options: .regularExpression) {
74
- let match = String(title[range])
75
- latticesSession = String(match.dropFirst(9).dropLast(1)) // drop "[lattices:" and "]"
76
- }
77
-
78
- fresh[wid] = WindowEntry(
79
- wid: wid,
80
- app: ownerName,
81
- pid: pid,
82
- title: title,
83
- frame: frame,
84
- spaceIds: spaceIds,
85
- isOnScreen: isOnScreen,
86
- latticesSession: latticesSession
87
- )
88
- }
89
-
90
- // Diff
91
- let oldKeys = Set(windows.keys)
92
- let newKeys = Set(fresh.keys)
93
- let added = Array(newKeys.subtracting(oldKeys))
94
- let removed = Array(oldKeys.subtracting(newKeys))
95
-
96
- let changed = added.count > 0 || removed.count > 0 || windowsContentChanged(old: windows, new: fresh)
97
-
98
- DispatchQueue.main.async {
99
- self.windows = fresh
100
- }
101
-
102
- if changed {
103
- EventBus.shared.post(.windowsChanged(
104
- windows: Array(fresh.values),
105
- added: added,
106
- removed: removed
107
- ))
108
- }
109
- }
110
-
111
- private func windowsContentChanged(old: [UInt32: WindowEntry], new: [UInt32: WindowEntry]) -> Bool {
112
- // Quick check: if titles or frames changed for any existing window
113
- for (wid, newEntry) in new {
114
- guard let oldEntry = old[wid] else { continue }
115
- if oldEntry.title != newEntry.title || oldEntry.frame != newEntry.frame {
116
- return true
117
- }
118
- }
119
- return false
120
- }
121
- }
@@ -1,71 +0,0 @@
1
- import Foundation
2
-
3
- struct WindowEntry: Codable, Identifiable {
4
- let wid: UInt32
5
- let app: String
6
- let pid: Int32
7
- let title: String
8
- let frame: WindowFrame
9
- let spaceIds: [Int]
10
- let isOnScreen: Bool
11
- let latticesSession: String?
12
-
13
- var id: UInt32 { wid }
14
- }
15
-
16
- struct WindowFrame: Codable, Equatable {
17
- let x: Double
18
- let y: Double
19
- let w: Double
20
- let h: Double
21
- }
22
-
23
- // MARK: - Desktop Inventory Snapshot
24
-
25
- struct DesktopInventorySnapshot {
26
- let displays: [DisplayInfo]
27
- let timestamp: Date
28
-
29
- struct DisplayInfo: Identifiable {
30
- let id: String // display UUID or index
31
- let name: String // e.g. "Built-in Retina", "LG UltraFine"
32
- let resolution: (w: Int, h: Int)
33
- let visibleFrame: (w: Int, h: Int)
34
- let isMain: Bool
35
- let spaceCount: Int
36
- let currentSpaceIndex: Int
37
- let spaces: [SpaceGroup]
38
- }
39
-
40
- struct SpaceGroup: Identifiable {
41
- let id: Int // CGS space ID
42
- let index: Int // 1-based index within display
43
- let isCurrent: Bool
44
- let apps: [AppGroup]
45
- }
46
-
47
- struct AppGroup: Identifiable {
48
- let id: String // unique key (spaceId-appName)
49
- let appName: String
50
- let windows: [InventoryWindowInfo]
51
- }
52
-
53
- struct InventoryWindowInfo: Identifiable {
54
- let id: UInt32 // CGWindowID
55
- let pid: Int32 // owner PID for AX operations
56
- let title: String
57
- let frame: WindowFrame
58
- let tilePosition: TilePosition?
59
- let isLattices: Bool
60
- let latticesSession: String?
61
- let spaceIndex: Int? // 1-based space index within display
62
- let isOnScreen: Bool // on current space
63
- var inventoryPath: InventoryPath?
64
- var appName: String? // owner app name for filtering
65
- }
66
-
67
- /// Flat list of all windows across all displays/spaces/apps
68
- var allWindows: [InventoryWindowInfo] {
69
- displays.flatMap { $0.spaces.flatMap { $0.apps.flatMap { $0.windows } } }
70
- }
71
- }