portacode 0.3.4.dev0__py3-none-any.whl → 1.4.11.dev0__py3-none-any.whl

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.

Potentially problematic release.


This version of portacode might be problematic. Click here for more details.

Files changed (93) hide show
  1. portacode/_version.py +16 -3
  2. portacode/cli.py +155 -19
  3. portacode/connection/client.py +152 -12
  4. portacode/connection/handlers/WEBSOCKET_PROTOCOL.md +1577 -0
  5. portacode/connection/handlers/__init__.py +43 -1
  6. portacode/connection/handlers/base.py +122 -18
  7. portacode/connection/handlers/chunked_content.py +244 -0
  8. portacode/connection/handlers/diff_handlers.py +603 -0
  9. portacode/connection/handlers/file_handlers.py +902 -17
  10. portacode/connection/handlers/project_aware_file_handlers.py +226 -0
  11. portacode/connection/handlers/project_state/README.md +312 -0
  12. portacode/connection/handlers/project_state/__init__.py +92 -0
  13. portacode/connection/handlers/project_state/file_system_watcher.py +179 -0
  14. portacode/connection/handlers/project_state/git_manager.py +1502 -0
  15. portacode/connection/handlers/project_state/handlers.py +875 -0
  16. portacode/connection/handlers/project_state/manager.py +1331 -0
  17. portacode/connection/handlers/project_state/models.py +108 -0
  18. portacode/connection/handlers/project_state/utils.py +50 -0
  19. portacode/connection/handlers/project_state_handlers.py +45 -0
  20. portacode/connection/handlers/proxmox_infra.py +307 -0
  21. portacode/connection/handlers/registry.py +53 -10
  22. portacode/connection/handlers/session.py +705 -53
  23. portacode/connection/handlers/system_handlers.py +142 -8
  24. portacode/connection/handlers/tab_factory.py +389 -0
  25. portacode/connection/handlers/terminal_handlers.py +150 -11
  26. portacode/connection/handlers/update_handler.py +61 -0
  27. portacode/connection/multiplex.py +60 -2
  28. portacode/connection/terminal.py +695 -28
  29. portacode/keypair.py +63 -1
  30. portacode/link_capture/__init__.py +38 -0
  31. portacode/link_capture/__pycache__/__init__.cpython-311.pyc +0 -0
  32. portacode/link_capture/bin/__pycache__/link_capture_wrapper.cpython-311.pyc +0 -0
  33. portacode/link_capture/bin/elinks +3 -0
  34. portacode/link_capture/bin/gio-open +3 -0
  35. portacode/link_capture/bin/gnome-open +3 -0
  36. portacode/link_capture/bin/gvfs-open +3 -0
  37. portacode/link_capture/bin/kde-open +3 -0
  38. portacode/link_capture/bin/kfmclient +3 -0
  39. portacode/link_capture/bin/link_capture_exec.sh +11 -0
  40. portacode/link_capture/bin/link_capture_wrapper.py +75 -0
  41. portacode/link_capture/bin/links +3 -0
  42. portacode/link_capture/bin/links2 +3 -0
  43. portacode/link_capture/bin/lynx +3 -0
  44. portacode/link_capture/bin/mate-open +3 -0
  45. portacode/link_capture/bin/netsurf +3 -0
  46. portacode/link_capture/bin/sensible-browser +3 -0
  47. portacode/link_capture/bin/w3m +3 -0
  48. portacode/link_capture/bin/x-www-browser +3 -0
  49. portacode/link_capture/bin/xdg-open +3 -0
  50. portacode/logging_categories.py +140 -0
  51. portacode/pairing.py +103 -0
  52. portacode/service.py +6 -0
  53. portacode/static/js/test-ntp-clock.html +63 -0
  54. portacode/static/js/utils/ntp-clock.js +232 -0
  55. portacode/utils/NTP_ARCHITECTURE.md +136 -0
  56. portacode/utils/__init__.py +1 -0
  57. portacode/utils/diff_apply.py +456 -0
  58. portacode/utils/diff_renderer.py +371 -0
  59. portacode/utils/ntp_clock.py +65 -0
  60. portacode-1.4.11.dev0.dist-info/METADATA +298 -0
  61. portacode-1.4.11.dev0.dist-info/RECORD +97 -0
  62. {portacode-0.3.4.dev0.dist-info → portacode-1.4.11.dev0.dist-info}/WHEEL +1 -1
  63. portacode-1.4.11.dev0.dist-info/top_level.txt +3 -0
  64. test_modules/README.md +296 -0
  65. test_modules/__init__.py +1 -0
  66. test_modules/test_device_online.py +44 -0
  67. test_modules/test_file_operations.py +743 -0
  68. test_modules/test_git_status_ui.py +370 -0
  69. test_modules/test_login_flow.py +50 -0
  70. test_modules/test_navigate_testing_folder.py +361 -0
  71. test_modules/test_play_store_screenshots.py +294 -0
  72. test_modules/test_terminal_buffer_performance.py +261 -0
  73. test_modules/test_terminal_interaction.py +80 -0
  74. test_modules/test_terminal_loading_race_condition.py +95 -0
  75. test_modules/test_terminal_start.py +56 -0
  76. testing_framework/.env.example +21 -0
  77. testing_framework/README.md +334 -0
  78. testing_framework/__init__.py +17 -0
  79. testing_framework/cli.py +326 -0
  80. testing_framework/core/__init__.py +1 -0
  81. testing_framework/core/base_test.py +336 -0
  82. testing_framework/core/cli_manager.py +177 -0
  83. testing_framework/core/hierarchical_runner.py +577 -0
  84. testing_framework/core/playwright_manager.py +520 -0
  85. testing_framework/core/runner.py +447 -0
  86. testing_framework/core/shared_cli_manager.py +234 -0
  87. testing_framework/core/test_discovery.py +112 -0
  88. testing_framework/requirements.txt +12 -0
  89. portacode-0.3.4.dev0.dist-info/METADATA +0 -236
  90. portacode-0.3.4.dev0.dist-info/RECORD +0 -27
  91. portacode-0.3.4.dev0.dist-info/top_level.txt +0 -1
  92. {portacode-0.3.4.dev0.dist-info → portacode-1.4.11.dev0.dist-info}/entry_points.txt +0 -0
  93. {portacode-0.3.4.dev0.dist-info → portacode-1.4.11.dev0.dist-info/licenses}/LICENSE +0 -0
@@ -0,0 +1,1577 @@
1
+ # WebSocket Communication Protocol
2
+
3
+ This document outlines the WebSocket communication protocol used in Portacode. The protocol involves three main participants: client sessions, the Portacode server, and devices.
4
+
5
+ ## Architecture Overview
6
+
7
+ ```
8
+ ┌─────────────┐ ┌──────────────────┐ ┌─────────────────────────┐
9
+ │ Client │ │ Portacode │ │ Device │
10
+ │ Session │◄────────►│ Server │◄────────►│ (Portacode CLI or │
11
+ │ │ │ │ │ Python package) │
12
+ └─────────────┘ └──────────────────┘ └─────────────────────────┘
13
+ │ │ │
14
+ │ │ │
15
+ Client-Side Acts as middleman Device-Side
16
+ Protocol - Routes messages Protocol
17
+ - Manages sessions
18
+ ```
19
+
20
+ The Portacode server acts as a **routing middleman** between client sessions and devices. It manages routing fields that are included in messages to specify routing destinations but are removed or transformed before reaching the final recipient:
21
+
22
+ **Routing Fields Behavior:**
23
+
24
+ - **`device_id`** (Client → Server): Client includes this to specify which device to route to. Server uses it for routing, then **removes it** before forwarding to the device (the device knows the message is for them). Server **adds it** when routing device responses back to clients (so clients know which device the message came from).
25
+
26
+ - **`client_sessions`** (Device → Server): Device includes this to specify which client session(s) to route to. Server uses it for routing, then **removes it** before forwarding to clients (clients just receive the message without seeing routing metadata).
27
+
28
+ - **`source_client_session`** (Server → Device): Server **adds this** when forwarding client commands to devices (so device knows which client sent the command and can target responses back). Clients never include this field.
29
+
30
+ This document describes the complete protocol for communicating with devices through the server, guiding app developers on how to get their client sessions to communicate with devices.
31
+
32
+ ## Table of Contents
33
+
34
+ - [Raw Message Format On Device Side](#raw-message-format-on-device-side)
35
+ - [Raw Message Format On Client Side](#raw-message-format-on-client-side)
36
+ - [Actions](#actions)
37
+ - [Terminal Actions](#terminal-actions)
38
+ - [`terminal_start`](#terminal_start)
39
+ - [`terminal_send`](#terminal_send)
40
+ - [`terminal_stop`](#terminal_stop)
41
+ - [`terminal_list`](#terminal_list)
42
+ - [System Actions](#system-actions)
43
+ - [`system_info`](#system_info)
44
+ - [`update_portacode_cli`](#update_portacode_cli)
45
+ - [`clock_sync_request`](#clock_sync_request)
46
+ - [File Actions](#file-actions)
47
+ - [`file_read`](#file_read)
48
+ - [`file_search`](#file_search)
49
+ - [`file_write`](#file_write)
50
+ - [`file_apply_diff`](#file_apply_diff)
51
+ - [`file_preview_diff`](#file_preview_diff)
52
+ - [`directory_list`](#directory_list)
53
+ - [`file_info`](#file_info)
54
+ - [`file_delete`](#file_delete)
55
+ - [`file_create`](#file_create)
56
+ - [`folder_create`](#folder_create)
57
+ - [`file_rename`](#file_rename)
58
+ - [`content_request`](#content_request)
59
+ - [Project State Actions](#project-state-actions)
60
+ - [`project_state_folder_expand`](#project_state_folder_expand)
61
+ - [`project_state_folder_collapse`](#project_state_folder_collapse)
62
+ - [`project_state_file_open`](#project_state_file_open)
63
+ - [`project_state_tab_close`](#project_state_tab_close)
64
+ - [`project_state_set_active_tab`](#project_state_set_active_tab)
65
+ - [`project_state_diff_open`](#project_state_diff_open)
66
+ - [`project_state_diff_content_request`](#project_state_diff_content_request)
67
+ - [`project_state_git_stage`](#project_state_git_stage)
68
+ - [`project_state_git_unstage`](#project_state_git_unstage)
69
+ - [`project_state_git_revert`](#project_state_git_revert)
70
+ - [`project_state_git_commit`](#project_state_git_commit)
71
+ - [Client Session Management](#client-session-management)
72
+ - [`client_sessions_update`](#client_sessions_update)
73
+ - [Events](#events)
74
+ - [Error Events](#error-events)
75
+ - [`error`](#error)
76
+ - [Terminal Events](#terminal-events)
77
+ - [`terminal_started`](#terminal_started)
78
+ - [`terminal_data`](#terminal_data)
79
+ - [`terminal_exit`](#terminal_exit)
80
+ - [`terminal_send_ack`](#terminal_send_ack)
81
+ - [`terminal_stopped`](#terminal_stopped)
82
+ - [`terminal_stop_completed`](#terminal_stop_completed)
83
+ - [`terminal_list`](#terminal_list-event)
84
+ - [System Events](#system-events)
85
+ - [`system_info`](#system_info-event)
86
+ - [`update_portacode_response`](#update_portacode_response)
87
+ - [`clock_sync_response`](#clock_sync_response)
88
+ - [File Events](#file-events)
89
+ - [`file_read_response`](#file_read_response)
90
+ - [`file_search_response`](#file_search_response)
91
+ - [`file_write_response`](#file_write_response)
92
+ - [`file_apply_diff_response`](#file_apply_diff_response)
93
+ - [`file_preview_diff_response`](#file_preview_diff_response)
94
+ - [`directory_list_response`](#directory_list_response)
95
+ - [`file_info_response`](#file_info_response)
96
+ - [`file_delete_response`](#file_delete_response)
97
+ - [`file_create_response`](#file_create_response)
98
+ - [`folder_create_response`](#folder_create_response)
99
+ - [`file_rename_response`](#file_rename_response)
100
+ - [`content_response`](#content_response)
101
+ - [Project State Events](#project-state-events)
102
+ - [`project_state_initialized`](#project_state_initialized)
103
+ - [`project_state_update`](#project_state_update)
104
+ - [`project_state_folder_expand_response`](#project_state_folder_expand_response)
105
+ - [`project_state_folder_collapse_response`](#project_state_folder_collapse_response)
106
+ - [`project_state_file_open_response`](#project_state_file_open_response)
107
+ - [`project_state_tab_close_response`](#project_state_tab_close_response)
108
+ - [`project_state_set_active_tab_response`](#project_state_set_active_tab_response)
109
+ - [`project_state_diff_open_response`](#project_state_diff_open_response)
110
+ - [`project_state_diff_content_response`](#project_state_diff_content_response)
111
+ - [`project_state_git_stage_response`](#project_state_git_stage_response)
112
+ - [`project_state_git_unstage_response`](#project_state_git_unstage_response)
113
+ - [`project_state_git_revert_response`](#project_state_git_revert_response)
114
+ - [`project_state_git_commit_response`](#project_state_git_commit_response)
115
+ - [Client Session Events](#client-session-events)
116
+ - [`request_client_sessions`](#request_client_sessions)
117
+ - [Terminal Data](#terminal-data)
118
+ - [Terminal I/O Data](#terminal_data)
119
+ - [Server-Side Events](#server-side-events)
120
+ - [`device_status`](#device_status)
121
+ - [`devices`](#devices)
122
+
123
+ ## Raw Message Format On Device Side
124
+
125
+ Communication between the server and devices uses a [multiplexer](./multiplex.py) that wraps every message in a JSON object with a `channel` and a `payload`. This allows for multiple virtual communication channels over a single WebSocket connection.
126
+
127
+ **Device-Side Message Structure:**
128
+
129
+ ```json
130
+ {
131
+ "channel": "<channel_id>",
132
+ "payload": {
133
+ // This is where the Action or Event object goes
134
+ }
135
+ }
136
+ ```
137
+
138
+ **Field Descriptions:**
139
+
140
+ * `channel` (string|integer, mandatory): Identifies the virtual channel the message is for. When sending control commands to the device, they should be sent to channel 0 and when the device responds to such control commands or sends system events, they will also be sent on the zero channel. When a terminal session is created in the device, it is assigned a uuid, the uuid becomes the channel for communicating to that specific terminal.
141
+ * `payload` (object, mandatory): The content of the message, which will be either an [Action](#actions) or an [Event](#events) object.
142
+
143
+ **Channel Types:**
144
+ - **Channel 0** (control channel): Used for system commands, terminal management, file operations, and project state management
145
+ - **Channel UUID** (terminal channel): Used for terminal I/O to a specific terminal session
146
+
147
+ ---
148
+
149
+ ## Raw Message Format On Client Side
150
+
151
+ Client sessions communicate with the server using a unified message format with the same field names as the device protocol, plus routing information.
152
+
153
+ **Client-Side Message Structure (Client → Server):**
154
+
155
+ ```json
156
+ {
157
+ "device_id": <number>,
158
+ "channel": <number|string>,
159
+ "payload": {
160
+ "cmd": "<command_name>",
161
+ ...command-specific fields
162
+ }
163
+ }
164
+ ```
165
+
166
+ **Field Descriptions:**
167
+
168
+ * `device_id` (number, mandatory): Routing field - specifies which device to send the message to. The server validates that the client has access to this device before forwarding.
169
+ * `channel` (number|string, mandatory): Same as device protocol - the target channel (0 for control, UUID for terminal). Uses the same field name for consistency.
170
+ * `payload` (object, mandatory): Same as device protocol - the command payload. Uses the same field name for consistency.
171
+
172
+ **Server Transformation (Client → Device):**
173
+
174
+ When the server receives a client message, it:
175
+ 1. Validates client has access to the specified `device_id`
176
+ 2. **Removes** `device_id` from the message (device doesn't need to be told "this is for you")
177
+ 3. **Adds** `source_client_session` to the payload (so device knows which client to respond to)
178
+ 4. Forwards to device: `{channel, payload: {...payload, source_client_session}}`
179
+
180
+ **Server Transformation (Device → Client):**
181
+
182
+ When the server receives a device response, it:
183
+ 1. **Adds** `device_id` to the message (so client knows which device it came from, based on authenticated device connection)
184
+ 2. **Removes** `client_sessions` routing metadata (clients don't need to see routing info)
185
+ 3. Routes to appropriate client session(s)
186
+
187
+ **Server Response Format (Server → Client):**
188
+
189
+ ```json
190
+ {
191
+ "event": "<event_name>",
192
+ "device_id": <number>,
193
+ ...event-specific fields
194
+ }
195
+ ```
196
+
197
+ **Field Descriptions:**
198
+
199
+ * `event` (string, mandatory): The name of the event being sent.
200
+ * `device_id` (number, mandatory): Authenticated field - identifies which device the event came from (added by server based on authenticated device connection).
201
+ * Additional fields depend on the specific event type.
202
+
203
+ **Example Client Message:**
204
+ ```json
205
+ {
206
+ "device_id": 42,
207
+ "channel": 0,
208
+ "payload": {
209
+ "cmd": "terminal_start",
210
+ "shell": "bash",
211
+ "cwd": "/home/user/project"
212
+ }
213
+ }
214
+ ```
215
+
216
+ **Example Server Response:**
217
+ ```json
218
+ {
219
+ "event": "terminal_started",
220
+ "device_id": 42,
221
+ "terminal_id": "uuid-1234-5678",
222
+ "channel": "uuid-1234-5678",
223
+ "pid": 12345
224
+ }
225
+ ```
226
+
227
+ **Note:** The server acts as a translator between the client-side and device-side protocols:
228
+ - When a client sends a command, the server transforms it from the client format to the device format
229
+ - When a device sends an event, the server adds the `device_id` and routes it to the appropriate client sessions
230
+
231
+ ---
232
+
233
+ ## Actions
234
+
235
+ Actions are messages sent from the server to the device, placed within the `payload` of a raw message. They instruct the device to perform a specific operation and are handled by the [`BaseHandler`](./base.py) and its subclasses.
236
+
237
+ **Action Structure (inside the `payload`):**
238
+
239
+ ```json
240
+ {
241
+ "cmd": "<command_name>",
242
+ "arg1": "value1",
243
+ "arg2": "value2",
244
+ "source_client_session": "channel.abc123"
245
+ }
246
+ ```
247
+
248
+ **Field Descriptions:**
249
+
250
+ * `cmd` (string, mandatory): The name of the action to be executed (e.g., `terminal_start`, `file_read`, `system_info`).
251
+ * `source_client_session` (string, mandatory): The channel name of the client session that originated this command. This field is automatically added by the server and allows devices to identify which specific client sent the command.
252
+ * Additional fields depend on the specific command (see individual command documentation below).
253
+
254
+ **Note**: Actions do not require targeting information - responses are automatically routed using the client session management system.
255
+
256
+ ### `terminal_start`
257
+
258
+ Initiates a new terminal session on the device. Handled by [`terminal_start`](./terminal_handlers.py).
259
+
260
+ **Payload Fields:**
261
+
262
+ * `shell` (string, optional): The shell to use (e.g., `/bin/bash`). Defaults to the system's default shell.
263
+ * `cwd` (string, optional): The working directory to start the terminal in. Defaults to the user's home directory.
264
+ * `project_id` (string, optional): The ID of the project this terminal is associated with.
265
+
266
+ **Responses:**
267
+
268
+ * On success, the device will respond with a [`terminal_started`](#terminal_started) event.
269
+ * On error, a generic [`error`](#error) event is sent.
270
+
271
+ ### `terminal_send`
272
+
273
+ Sends input data to a running terminal session. Handled by [`terminal_send`](./terminal_handlers.py).
274
+
275
+ **Payload Fields:**
276
+
277
+ * `terminal_id` (string, mandatory): The ID of the terminal session to send data to.
278
+ * `data` (string, mandatory): The data to write to the terminal's standard input.
279
+
280
+ **Responses:**
281
+
282
+ * On success, the device will respond with a [`terminal_send_ack`](#terminal_send_ack) event.
283
+ * On error, a generic [`error`](#error) event is sent.
284
+
285
+ ### `terminal_stop`
286
+
287
+ Terminates a running terminal session. Handled by [`terminal_stop`](./terminal_handlers.py).
288
+
289
+ **Payload Fields:**
290
+
291
+ * `terminal_id` (string, mandatory): The ID of the terminal session to stop.
292
+
293
+ **Responses:**
294
+
295
+ * The device immediately responds with a [`terminal_stopped`](#terminal_stopped) event to acknowledge the request.
296
+ * Once the terminal is successfully stopped, a [`terminal_stop_completed`](#terminal_stop_completed) event is sent.
297
+ * If the terminal is not found, a [`terminal_stop_completed`](#terminal_stop_completed) with a "not_found" status is sent.
298
+
299
+ ### `terminal_list`
300
+
301
+ Requests a list of all active terminal sessions. Handled by [`terminal_list`](./terminal_handlers.py).
302
+
303
+ **Payload Fields:**
304
+
305
+ * `project_id` (string, optional): If provided, filters terminals by this project ID. If "all", lists all terminals.
306
+
307
+ **Responses:**
308
+
309
+ * On success, the device will respond with a [`terminal_list`](#terminal_list-event) event.
310
+
311
+ ### `system_info`
312
+
313
+ Requests system information from the device. Handled by [`system_info`](./system_handlers.py).
314
+
315
+ **Payload Fields:**
316
+
317
+ This action does not require any payload fields.
318
+
319
+ **Responses:**
320
+
321
+ * On success, the device will respond with a [`system_info`](#system_info-event) event.
322
+
323
+ ### `setup_proxmox_infra`
324
+
325
+ Configures a Proxmox node for Portacode infrastructure usage (API token storage, default storage selection, and bridge setup). Handled by [`ConfigureProxmoxInfraHandler`](./proxmox_infra.py).
326
+
327
+ **Payload Fields:**
328
+
329
+ * `token_identifier` (string, required): API token identifier in the form `user@realm!tokenid`.
330
+ * `token_value` (string, required): Secret value associated with the token.
331
+ * `verify_ssl` (boolean, optional): When true, the handler verifies SSL certificates; defaults to `false`.
332
+
333
+ **Responses:**
334
+
335
+ * On success, the device will emit a [`proxmox_infra_configured`](#proxmox_infra_configured-event) event with the persisted infra snapshot.
336
+ * On failure, the device will emit an [`error`](#error) event with details (e.g., permission issues, missing proxmoxer/dnsmasq, or missing root privileges).
337
+
338
+ ### `clock_sync_request`
339
+
340
+ Internal event that devices send to the gateway to request the authoritative server timestamp (used for adjusting `portacode.utils.ntp_clock`). The gateway responds immediately with [`clock_sync_response`](#clock_sync_response).
341
+
342
+ **Payload Fields:**
343
+
344
+ * `request_id` (string, optional): Correlates the response with the request.
345
+
346
+ **Responses:**
347
+
348
+ * The gateway responds with [`clock_sync_response`](#clock_sync_response) that includes the authoritative `server_time` (plus the optional `server_time_iso` mirror).
349
+
350
+ ### `update_portacode_cli`
351
+
352
+ Updates the Portacode CLI package and restarts the process. Handled by [`update_portacode_cli`](./update_handler.py).
353
+
354
+ **Payload Fields:**
355
+
356
+ This action does not require any payload fields.
357
+
358
+ **Responses:**
359
+
360
+ * On success, the device will respond with an `update_portacode_response` event and then exit with code 42 to trigger restart.
361
+ * On error, an `update_portacode_response` event with error details is sent.
362
+
363
+ ### `file_read`
364
+
365
+ Reads the content of a file. Handled by [`file_read`](./file_handlers.py).
366
+
367
+ **Payload Fields:**
368
+
369
+ * `path` (string, mandatory): The absolute path to the file to read.
370
+ * `start_line` (integer, optional): 1-based line number to start reading from. Defaults to `1`.
371
+ * `end_line` (integer, optional): 1-based line number to stop reading at (inclusive). When provided, limits the response to the range between `start_line` and `end_line`.
372
+ * `max_lines` (integer, optional): Maximum number of lines to return (capped at 2000). Useful for pagination when `end_line` is not specified.
373
+ * `encoding` (string, optional): Text encoding to use when reading the file. Defaults to `utf-8` with replacement for invalid bytes.
374
+
375
+ **Responses:**
376
+
377
+ * On success, the device will respond with a [`file_read_response`](#file_read_response) event.
378
+ * On error, a generic [`error`](#error) event is sent.
379
+
380
+ ### `file_search`
381
+
382
+ Searches for text matches within files beneath a given root directory. Handled by [`file_search`](./file_handlers.py).
383
+
384
+ **Payload Fields:**
385
+
386
+ * `root_path` (string, mandatory): The absolute path that acts as the search root (typically a project folder).
387
+ * `query` (string, mandatory): The search query. Treated as plain text unless `regex=true`.
388
+ * `match_case` (boolean, optional): When `true`, performs a case-sensitive search. Defaults to `false`.
389
+ * `regex` (boolean, optional): When `true`, interprets `query` as a regular expression. Defaults to `false`.
390
+ * `whole_word` (boolean, optional): When `true`, matches only whole words. Works with both plain text and regex queries.
391
+ * `include_patterns` (array[string], optional): Glob patterns that files must match to be included (e.g., `["src/**/*.py"]`).
392
+ * `exclude_patterns` (array[string], optional): Glob patterns for files/directories to skip (e.g., `["**/tests/**"]`).
393
+ * `include_hidden` (boolean, optional): When `true`, includes hidden files and folders. Defaults to `false`.
394
+ * `max_results` (integer, optional): Maximum number of match entries to return (capped at 500). Defaults to `40`.
395
+ * `max_matches_per_file` (integer, optional): Maximum number of matches to return per file (capped at 50). Defaults to `5`.
396
+ * `max_file_size` (integer, optional): Maximum file size in bytes to scan (defaults to 1 MiB).
397
+ * `max_line_length` (integer, optional): Maximum number of characters to return per matching line (defaults to `200`).
398
+
399
+ **Default Behaviour:**
400
+
401
+ * Binary files and large vendor/static directories (e.g., `node_modules`, `dist`, `static`) are skipped automatically unless custom `exclude_patterns` are provided.
402
+ * Only common source/text extensions are scanned by default (override with `include_patterns` to widen the scope).
403
+ * Searches stop after 10 seconds, respecting both per-file and global match limits to avoid oversized responses.
404
+
405
+ **Responses:**
406
+
407
+ * On success, the device will respond with a [`file_search_response`](#file_search_response) event containing the matches.
408
+ * On error, a generic [`error`](#error) event is sent.
409
+
410
+ ### `file_write`
411
+
412
+ Writes content to a file. Handled by [`file_write`](./file_handlers.py).
413
+
414
+ **Payload Fields:**
415
+
416
+ * `path` (string, mandatory): The absolute path to the file to write to.
417
+ * `content` (string, mandatory): The content to write to the file.
418
+
419
+ **Responses:**
420
+
421
+ * On success, the device will respond with a [`file_write_response`](#file_write_response) event.
422
+
423
+ ### `file_apply_diff`
424
+
425
+ Apply one or more unified diff hunks to local files. Handled by [`file_apply_diff`](./diff_handlers.py).
426
+
427
+ **Request Payload:**
428
+
429
+ ```json
430
+ {
431
+ "cmd": "file_apply_diff",
432
+ "diff": "<unified diff string>",
433
+ "base_path": "<optional base path for relative diff entries>",
434
+ "project_id": "<server project UUID>",
435
+ "source_client_session": "<originating session/channel>"
436
+ }
437
+ ```
438
+
439
+ **Behavior:**
440
+
441
+ * `diff` must be standard unified diff text (like `git diff` output). Multiple files per diff are supported.
442
+ * If `base_path` is omitted the handler will attempt to derive the active project root from `source_client_session`, falling back to the device working directory.
443
+ * Each file hunk is validated before writing; context mismatches or missing files return per-file errors without aborting the rest.
444
+ * `/dev/null` entries are interpreted as file creations/deletions.
445
+ * Inline directives are also supported on their own lines. Use `@@delete:relative/path.py@@` to delete a file directly or `@@move:old/path.py -> new/path.py@@` (alias `@@rename:...@@`) to move/rename a file without crafting a diff. Directives are evaluated before the diff hunks and must point to files inside the project base.
446
+
447
+ * On completion the device responds with [`file_apply_diff_response`](#file_apply_diff_response).
448
+ * On error, a generic [`error`](#error) event is sent.
449
+
450
+ ### `file_preview_diff`
451
+
452
+ Validate one or more unified diff hunks and render an HTML preview without mutating any files. Handled by [`file_preview_diff`](./diff_handlers.py).
453
+
454
+ **Request Payload:**
455
+
456
+ ```json
457
+ {
458
+ "cmd": "file_preview_diff",
459
+ "diff": "<unified diff string>",
460
+ "base_path": "<optional base path for relative diff entries>",
461
+ "request_id": "req_123456"
462
+ }
463
+ ```
464
+
465
+ **Behavior:**
466
+
467
+ * Reuses the same parser as `file_apply_diff`, so invalid hunks surface the same errors.
468
+ * Produces HTML snippets per file using the device-side renderer. No files are modified.
469
+ * Inline directives (`@@delete:...@@`, `@@move:src -> dest@@`) use the same syntax as `file_apply_diff`. The handler validates them up front and includes them in the preview output so the user can see deletions or moves before clicking “Apply”.
470
+ * Returns immediately with an error payload if preview generation fails.
471
+
472
+ * On completion the device responds with [`file_preview_diff_response`](#file_preview_diff_response).
473
+ * On error, a generic [`error`](#error) event is sent.
474
+
475
+ ### `directory_list`
476
+
477
+ Lists the contents of a directory. Handled by [`directory_list`](./file_handlers.py).
478
+
479
+ **Payload Fields:**
480
+
481
+ * `path` (string, optional): The path to the directory to list. Defaults to the current directory.
482
+ * `show_hidden` (boolean, optional): Whether to include hidden files in the listing. Defaults to `false`.
483
+ * `limit` (integer, optional): Maximum number of entries to return (defaults to “all”). Values above 1000 are clamped to 1000.
484
+ * `offset` (integer, optional): Number of entries to skip before collecting results (defaults to `0`).
485
+
486
+ **Responses:**
487
+
488
+ * On success, the device will respond with a [`directory_list_response`](#directory_list_response) event.
489
+ * On error, a generic [`error`](#error) event is sent.
490
+
491
+ ### `file_info`
492
+
493
+ Gets information about a file or directory. Handled by [`file_info`](./file_handlers.py).
494
+
495
+ **Payload Fields:**
496
+
497
+ * `path` (string, mandatory): The path to the file or directory.
498
+
499
+ **Responses:**
500
+
501
+ * On success, the device will respond with a [`file_info_response`](#file_info_response) event.
502
+
503
+ ### `file_delete`
504
+
505
+ Deletes a file or directory. Handled by [`file_delete`](./file_handlers.py).
506
+
507
+ **Payload Fields:**
508
+
509
+ * `path` (string, mandatory): The path to the file or directory to delete.
510
+ * `recursive` (boolean, optional): If `true`, recursively deletes a directory and its contents. Defaults to `false`.
511
+
512
+ **Responses:**
513
+
514
+ * On success, the device will respond with a [`file_delete_response`](#file_delete_response) event.
515
+ * On error, a generic [`error`](#error) event is sent.
516
+
517
+ ### `file_create`
518
+
519
+ Creates a new file. Handled by [`file_create`](./file_handlers.py).
520
+
521
+ **Payload Fields:**
522
+
523
+ * `parent_path` (string, mandatory): The absolute path to the parent directory where the file should be created.
524
+ * `file_name` (string, mandatory): The name of the file to create. Must not contain path separators or be special directories (`.`, `..`).
525
+ * `content` (string, optional): The initial content for the file. Defaults to empty string.
526
+
527
+ **Responses:**
528
+
529
+ * On success, the device will respond with a [`file_create_response`](#file_create_response) event.
530
+ * On error, a generic [`error`](#error) event is sent.
531
+
532
+ ### `folder_create`
533
+
534
+ Creates a new folder/directory. Handled by [`folder_create`](./file_handlers.py).
535
+
536
+ **Payload Fields:**
537
+
538
+ * `parent_path` (string, mandatory): The absolute path to the parent directory where the folder should be created.
539
+ * `folder_name` (string, mandatory): The name of the folder to create. Must not contain path separators or be special directories (`.`, `..`).
540
+
541
+ **Responses:**
542
+
543
+ * On success, the device will respond with a [`folder_create_response`](#folder_create_response) event.
544
+ * On error, a generic [`error`](#error) event is sent.
545
+
546
+ ### `file_rename`
547
+
548
+ Renames a file or folder. Handled by [`file_rename`](./file_handlers.py).
549
+
550
+ **Payload Fields:**
551
+
552
+ * `old_path` (string, mandatory): The absolute path to the file or folder to rename.
553
+ * `new_name` (string, mandatory): The new name (not full path) for the item. Must not contain path separators or be special directories (`.`, `..`).
554
+
555
+ **Responses:**
556
+
557
+ * On success, the device will respond with a [`file_rename_response`](#file_rename_response) event.
558
+ * On error, a generic [`error`](#error) event is sent.
559
+
560
+ ### `content_request`
561
+
562
+ Requests cached content by SHA-256 hash. This action is used to implement content caching for performance optimization, allowing clients to request large content (such as file content, HTML diffs, etc.) by hash instead of receiving it in every WebSocket message. For large content (>200KB), the response will be automatically chunked into multiple messages for reliable transmission. Handled by [`content_request`](./file_handlers.py).
563
+
564
+ **Payload Fields:**
565
+
566
+ * `content_hash` (string, mandatory): The SHA-256 hash of the content to retrieve (with "sha256:" prefix).
567
+ * `request_id` (string, mandatory): A unique identifier for this request, used to match with the response.
568
+
569
+ **Responses:**
570
+
571
+ * On success, the device will respond with one or more [`content_response`](#content_response) events containing the cached content. Large content is automatically chunked.
572
+ * On error (content not found), a [`content_response`](#content_response) event with `success: false` is sent.
573
+
574
+ ## Project State Actions
575
+
576
+ Project state actions manage the state of project folders, including file structures, Git metadata, open files, and folder expansion states. These actions provide real-time synchronization between the client and server for project management functionality.
577
+
578
+ **Note:** Project state is automatically initialized when a client session connects with a `project_folder_path` property. No manual initialization command is required.
579
+
580
+ **Tab Management:** Open tabs are internally managed using a dictionary structure with unique keys to prevent duplicates and race conditions:
581
+ - File tabs use `file_path` as the unique key
582
+ - Diff tabs use a composite key: `diff:{file_path}:{from_ref}:{to_ref}:{from_hash}:{to_hash}`
583
+ - Untitled tabs use their `tab_id` as the unique key
584
+
585
+ This ensures that sending the same command multiple times (e.g., `project_state_diff_open` with identical parameters) will not create duplicate tabs but will instead activate the existing tab.
586
+
587
+ ### `project_state_folder_expand`
588
+
589
+ Expands a folder in the project tree, loading its contents and enabling monitoring for that folder level. When a folder is expanded, the system proactively loads one level down for all subdirectories to enable immediate expansion in the UI. This action also scans items in the expanded folder and preloads content for any non-empty subdirectories.
590
+
591
+ **Payload Fields:**
592
+
593
+ * `project_id` (string, mandatory): The project ID from the initialized project state.
594
+ * `folder_path` (string, mandatory): The absolute path to the folder to expand.
595
+
596
+ **Responses:**
597
+
598
+ * On success, the device will respond with a [`project_state_folder_expand_response`](#project_state_folder_expand_response) event, followed by a [`project_state_update`](#project_state_update) event containing the updated file structure with preloaded subdirectory contents.
599
+ * On error, a generic [`error`](#error) event is sent.
600
+
601
+ ### `project_state_folder_collapse`
602
+
603
+ Collapses a folder in the project tree, stopping monitoring for that folder level.
604
+
605
+ **Payload Fields:**
606
+
607
+ * `project_id` (string, mandatory): The project ID from the initialized project state.
608
+ * `folder_path` (string, mandatory): The absolute path to the folder to collapse.
609
+
610
+ **Responses:**
611
+
612
+ * On success, the device will respond with a [`project_state_folder_collapse_response`](#project_state_folder_collapse_response) event, followed by a [`project_state_update`](#project_state_update) event.
613
+ * On error, a generic [`error`](#error) event is sent.
614
+
615
+ ### `project_state_file_open`
616
+
617
+ Marks a file as open in the project state, tracking it as part of the current editing session.
618
+
619
+ **Duplicate Prevention:** This action prevents creating duplicate file tabs by using the `file_path` as a unique key. If a file tab with the same path already exists, it will be activated instead of creating a new one.
620
+
621
+ **Payload Fields:**
622
+
623
+ * `project_id` (string, mandatory): The project ID from the initialized project state.
624
+ * `file_path` (string, mandatory): The absolute path to the file to open.
625
+ * `set_active` (boolean, optional): Whether to set this file as the active file. Defaults to `true`.
626
+
627
+ **Responses:**
628
+
629
+ * On success, the device will respond with a [`project_state_file_open_response`](#project_state_file_open_response) event, followed by a [`project_state_update`](#project_state_update) event.
630
+ * On error, a generic [`error`](#error) event is sent.
631
+
632
+ ### `project_state_tab_close`
633
+
634
+ Closes a tab in the project state, removing it from the current editing session.
635
+
636
+ **Payload Fields:**
637
+
638
+ * `project_id` (string, mandatory): The project ID from the initialized project state.
639
+ * `tab_id` (string, mandatory): The unique ID of the tab to close.
640
+
641
+ **Responses:**
642
+
643
+ * On success, the device will respond with a [`project_state_tab_close_response`](#project_state_tab_close_response) event, followed by a [`project_state_update`](#project_state_update) event.
644
+ * On error, a generic [`error`](#error) event is sent.
645
+
646
+ ### `project_state_set_active_tab`
647
+
648
+ Sets the currently active tab in the project state. Only one tab can be active at a time.
649
+
650
+ **Payload Fields:**
651
+
652
+ * `project_id` (string, mandatory): The project ID from the initialized project state.
653
+ * `tab_id` (string, optional): The unique ID of the tab to set as active. If `null` or omitted, clears the active tab.
654
+
655
+ **Responses:**
656
+
657
+ * On success, the device will respond with a [`project_state_set_active_tab_response`](#project_state_set_active_tab_response) event, followed by a [`project_state_update`](#project_state_update) event.
658
+ * On error, a generic [`error`](#error) event is sent.
659
+
660
+ ### `project_state_diff_open`
661
+
662
+ Opens a diff tab for comparing file versions at different points in the git timeline. This replaces the previous `project_state_create_diff_tab` action with a more efficient approach that doesn't require the client to provide file content, instead using git timeline references.
663
+
664
+ **Duplicate Prevention:** This action prevents creating duplicate diff tabs by using a unique key based on `file_path`, `from_ref`, `to_ref`, `from_hash`, and `to_hash`. If a diff tab with the same parameters already exists, it will be activated instead of creating a new one.
665
+
666
+ **Payload Fields:**
667
+
668
+ * `project_id` (string, mandatory): The project ID from the initialized project state.
669
+ * `file_path` (string, mandatory): The absolute path to the file to create a diff for.
670
+ * `from_ref` (string, mandatory): The source reference point. Must be one of:
671
+ - `"head"`: Content from the HEAD commit
672
+ - `"staged"`: Content from the staging area
673
+ - `"working"`: Current working directory content
674
+ - `"commit"`: Content from a specific commit (requires `from_hash`)
675
+ * `to_ref` (string, mandatory): The target reference point. Same options as `from_ref`.
676
+ * `from_hash` (string, optional): Required when `from_ref` is `"commit"`. The commit hash to get content from.
677
+ * `to_hash` (string, optional): Required when `to_ref` is `"commit"`. The commit hash to get content from.
678
+
679
+ **Responses:**
680
+
681
+ * On success, the device will respond with a [`project_state_diff_open_response`](#project_state_diff_open_response) event, followed by a [`project_state_update`](#project_state_update) event.
682
+ * On error, a generic [`error`](#error) event is sent.
683
+
684
+ ### `project_state_diff_content_request`
685
+
686
+ Requests the content for a specific diff tab identified by its diff parameters. This action is used to load the actual file content (original and modified) as well as HTML diff data for diff tabs after they have been created by [`project_state_diff_open`](#project_state_diff_open). For large content (>200KB), the response will be automatically chunked into multiple messages for reliable transmission.
687
+
688
+ **Content Types:** This action can request content for a diff:
689
+ - `original`: The original (from) content of the diff
690
+ - `modified`: The modified (to) content of the diff
691
+ - `html_diff`: The HTML diff versions for rich visual display
692
+ - `all`: All content types returned as a single JSON object (recommended for efficiency)
693
+
694
+ **Payload Fields:**
695
+
696
+ * `project_id` (string, mandatory): The project ID from the initialized project state.
697
+ * `file_path` (string, mandatory): The absolute path to the file the diff is for.
698
+ * `from_ref` (string, mandatory): The source reference point used in the diff. Must match the diff tab parameters.
699
+ * `to_ref` (string, mandatory): The target reference point used in the diff. Must match the diff tab parameters.
700
+ * `from_hash` (string, optional): The commit hash for `from_ref` if it was `"commit"`. Must match the diff tab parameters.
701
+ * `to_hash` (string, optional): The commit hash for `to_ref` if it was `"commit"`. Must match the diff tab parameters.
702
+ * `content_type` (string, mandatory): The type of content to request. Must be one of:
703
+ - `"original"`: Request the original (from) content
704
+ - `"modified"`: Request the modified (to) content
705
+ - `"html_diff"`: Request the HTML diff versions for visual display
706
+ - `"all"`: Request all content types as a single JSON object
707
+ * `request_id` (string, mandatory): Unique identifier for this request to match with the response.
708
+
709
+ **Responses:**
710
+
711
+ * On success, the device will respond with one or more [`project_state_diff_content_response`](#project_state_diff_content_response) events. Large content is automatically chunked.
712
+ * On error, a generic [`error`](#error) event is sent.
713
+
714
+ ### `project_state_git_stage`
715
+
716
+ Stages file(s) for commit in the project's git repository. Supports both single file and bulk operations. Handled by [`project_state_git_stage`](./project_state_handlers.py).
717
+
718
+ **Payload Fields:**
719
+
720
+ * `project_id` (string, mandatory): The project ID from the initialized project state.
721
+ * `file_path` (string, optional): The absolute path to a single file to stage. Used for backward compatibility.
722
+ * `file_paths` (array of strings, optional): Array of absolute paths to files to stage. Used for bulk operations.
723
+ * `stage_all` (boolean, optional): If true, stages all unstaged changes in the repository. Takes precedence over file_path/file_paths.
724
+
725
+ **Operation Modes:**
726
+ - Single file: Provide `file_path`
727
+ - Bulk operation: Provide `file_paths` array
728
+ - Stage all: Set `stage_all` to true
729
+
730
+ **Responses:**
731
+
732
+ * On success, the device will respond with a [`project_state_git_stage_response`](#project_state_git_stage_response) event, followed by a [`project_state_update`](#project_state_update) event with updated git status.
733
+ * On error, a generic [`error`](#error) event is sent.
734
+
735
+ ### `project_state_git_unstage`
736
+
737
+ Unstages file(s) (removes from staging area) in the project's git repository. Supports both single file and bulk operations. Handled by [`project_state_git_unstage`](./project_state_handlers.py).
738
+
739
+ **Payload Fields:**
740
+
741
+ * `project_id` (string, mandatory): The project ID from the initialized project state.
742
+ * `file_path` (string, optional): The absolute path to a single file to unstage. Used for backward compatibility.
743
+ * `file_paths` (array of strings, optional): Array of absolute paths to files to unstage. Used for bulk operations.
744
+ * `unstage_all` (boolean, optional): If true, unstages all staged changes in the repository. Takes precedence over file_path/file_paths.
745
+
746
+ **Operation Modes:**
747
+ - Single file: Provide `file_path`
748
+ - Bulk operation: Provide `file_paths` array
749
+ - Unstage all: Set `unstage_all` to true
750
+
751
+ **Responses:**
752
+
753
+ * On success, the device will respond with a [`project_state_git_unstage_response`](#project_state_git_unstage_response) event, followed by a [`project_state_update`](#project_state_update) event with updated git status.
754
+ * On error, a generic [`error`](#error) event is sent.
755
+
756
+ ### `project_state_git_revert`
757
+
758
+ Reverts file(s) to their HEAD version, discarding local changes in the project's git repository. Supports both single file and bulk operations. Handled by [`project_state_git_revert`](./project_state_handlers.py).
759
+
760
+ **Payload Fields:**
761
+
762
+ * `project_id` (string, mandatory): The project ID from the initialized project state.
763
+ * `file_path` (string, optional): The absolute path to a single file to revert. Used for backward compatibility.
764
+ * `file_paths` (array of strings, optional): Array of absolute paths to files to revert. Used for bulk operations.
765
+ * `revert_all` (boolean, optional): If true, reverts all unstaged changes in the repository. Takes precedence over file_path/file_paths.
766
+
767
+ **Operation Modes:**
768
+ - Single file: Provide `file_path`
769
+ - Bulk operation: Provide `file_paths` array
770
+ - Revert all: Set `revert_all` to true
771
+
772
+ **Responses:**
773
+
774
+ * On success, the device will respond with a [`project_state_git_revert_response`](#project_state_git_revert_response) event, followed by a [`project_state_update`](#project_state_update) event with updated git status.
775
+ * On error, a generic [`error`](#error) event is sent.
776
+
777
+ ### `project_state_git_commit`
778
+
779
+ Commits staged changes with a commit message in the project's git repository. Handled by [`project_state_git_commit`](./project_state_handlers.py).
780
+
781
+ **Payload Fields:**
782
+
783
+ * `project_id` (string, mandatory): The project ID from the initialized project state.
784
+ * `commit_message` (string, mandatory): The commit message for the changes being committed.
785
+
786
+ **Responses:**
787
+
788
+ * On success, the device will respond with a [`project_state_git_commit_response`](#project_state_git_commit_response) event, followed by a [`project_state_update`](#project_state_update) event with updated git status.
789
+ * On error, a generic [`error`](#error) event is sent.
790
+
791
+ ### Client Session Management
792
+
793
+ ### `client_sessions_update`
794
+
795
+ Sends updated client session information to the device. This is a special internal action used by the server to inform devices about connected client sessions.
796
+
797
+ **Payload Fields:**
798
+
799
+ * `sessions` (array, mandatory): Array of client session objects containing connection information.
800
+
801
+ **Responses:**
802
+
803
+ This action does not generate a response event.
804
+
805
+ ---
806
+
807
+ ## Events
808
+
809
+ Events are messages sent from the device to the server, placed within the `payload` of a raw message. They are sent in response to an action or to notify the server of a state change.
810
+
811
+ **Event Structure (inside the `payload`):**
812
+
813
+ ```json
814
+ {
815
+ "event": "<event_name>",
816
+ // Event-specific fields...
817
+ "device_id": 123,
818
+ "project_id": "<project_uuid>",
819
+ "client_sessions": ["channel.abc123", "channel.def456"]
820
+ }
821
+ ```
822
+
823
+ **Standard Fields (automatically added by the system):**
824
+
825
+ * `event` (string, mandatory): The name of the event being sent (e.g., `terminal_started`).
826
+ * `device_id` (integer, mandatory): The ID of the authenticated device that generated this event. **Added by the server based on the authenticated connection for security** - devices cannot self-identify.
827
+ * `project_id` (string, optional): The project UUID associated with this event, used for project-scoped filtering. Sent by the device.
828
+ * `client_sessions` (array, optional): Array of client session channel names that should receive this event. **Added by the device's terminal manager** based on interested client sessions. When present, the event is sent only to these specific sessions. When absent, the event is broadcast to all sessions for the device owner.
829
+
830
+ **Event-Specific Fields:**
831
+
832
+ Each event type includes additional fields specific to its purpose, documented in the individual event sections below.
833
+
834
+ ### <a name="error"></a>`error`
835
+
836
+ A generic event sent when an error occurs during the execution of an action.
837
+
838
+ **Event Fields:**
839
+
840
+ * `message` (string, mandatory): A description of the error that occurred.
841
+
842
+ ### <a name="terminal_started"></a>`terminal_started`
843
+
844
+ Confirms that a new terminal session has been successfully started. Triggered by a `terminal_start` action. Handled by [`terminal_start`](./terminal_handlers.py).
845
+
846
+ **Event Fields:**
847
+
848
+ * `terminal_id` (string, mandatory): The unique ID of the newly created terminal session.
849
+ * `channel` (string, mandatory): The channel name for terminal I/O.
850
+ * `pid` (integer, mandatory): The process ID (PID) of the terminal process.
851
+ * `shell` (string, optional): The shell that was used to start the terminal.
852
+ * `cwd` (string, optional): The working directory where the terminal was started.
853
+ * `project_id` (string, optional): The project ID associated with the terminal.
854
+
855
+ ### <a name="terminal_data"></a>`terminal_data`
856
+
857
+ Streams real-time terminal output data from a running terminal session to connected clients. This event is automatically generated whenever the terminal process produces output (stdout/stderr). Generated by [`TerminalSession`](./session.py) through the terminal manager's session-aware messaging system.
858
+
859
+ **Event-Specific Fields:**
860
+
861
+ * `channel` (string, mandatory): The terminal UUID identifying which terminal session produced this output. This matches the `terminal_id` from the corresponding `terminal_started` event.
862
+ * `data` (string, mandatory): The raw terminal output data. **See detailed description below.**
863
+
864
+ **The `data` Field - Detailed Specification:**
865
+
866
+ The `data` field contains the exact bytes output by the terminal process, decoded as a UTF-8 string with error handling:
867
+
868
+ * **Encoding**: UTF-8 with `errors="ignore"` - invalid UTF-8 sequences are silently dropped
869
+ * **Content**: Raw terminal output including:
870
+ - Regular command output (stdout)
871
+ - Error messages (stderr) - combined with stdout in PTY mode
872
+ - ANSI escape sequences for colors, cursor positioning, screen clearing, etc.
873
+ - Control characters (newlines, tabs, backspace, etc.)
874
+ - Shell prompts and interactive application output
875
+ * **Buffering**: Data is read in 1024-byte chunks from the terminal process and sent immediately (no line buffering)
876
+ * **Binary Safety**: Binary data is handled via UTF-8 decoding with error tolerance
877
+ * **Size**: Individual chunks are typically ≤1024 characters, but can be smaller for real-time responsiveness
878
+
879
+ **Examples of `data` content:**
880
+ ```
881
+ "Hello, World!\n" // Simple command output
882
+ "\u001b[32mSuccess\u001b[0m\n" // ANSI colored text
883
+ "user@host:~/project$ " // Shell prompt
884
+ "\u001b[2J\u001b[H" // Clear screen escape sequence
885
+ "Progress: [████████████████████] 100%\r" // Progress bar with carriage return
886
+ ```
887
+
888
+ **Security Note**: The `device_id` field is automatically injected by the server based on the authenticated connection - the device cannot and should not specify its own ID. The `project_id` and `client_sessions` fields are added by the device's terminal manager for proper routing and filtering.
889
+
890
+ ### <a name="terminal_link_request"></a>`terminal_link_request`
891
+
892
+ Signals that the active terminal session attempted to open an external URL (e.g., via `xdg-open`). The terminal environment is instrumented with the `portacode/link_capture` helper, so CLI programs that try to open a browser are captured and forwarded to connected clients for confirmation.
893
+
894
+ **Event Fields:**
895
+
896
+ * `terminal_id` (string, mandatory): The UUID of the terminal session that triggered the request.
897
+ * `channel` (string, mandatory): Same as `terminal_id` (included for backward compatibility with raw channel routing).
898
+ * `url` (string, mandatory): The full URL the terminal tried to open. Clients must surface this text directly so users can verify it.
899
+ * `command` (string, optional): The command that attempted the navigation (e.g., `xdg-open`).
900
+ * `args` (array[string], optional): Arguments passed to the command, which may include safely-encoded paths or flags.
901
+ * `timestamp` (number, optional): UNIX epoch seconds when the capture occurred.
902
+ * `project_id` (string, optional): The project UUID in whose context the attempt was made.
903
+
904
+ Clients receiving this event should pause and ask the user for confirmation before opening the URL, and may throttle or suppress repeated events to prevent modal storms if a CLI tool loops on the same link.
905
+
906
+ ### <a name="terminal_exit"></a>`terminal_exit`
907
+
908
+ Notifies the server that a terminal session has terminated. This can be due to the process ending or the session being stopped. Handled by [`terminal_start`](./terminal_handlers.py).
909
+
910
+ **Event Fields:**
911
+
912
+ * `terminal_id` (string, mandatory): The ID of the terminal session that has exited.
913
+ * `returncode` (integer, mandatory): The exit code of the terminal process.
914
+ * `project_id` (string, optional): The project ID associated with the terminal.
915
+
916
+ ### <a name="terminal_send_ack"></a>`terminal_send_ack`
917
+
918
+ Acknowledges the receipt of a `terminal_send` action. Handled by [`terminal_send`](./terminal_handlers.py). This event carries no extra fields.
919
+
920
+ ### <a name="terminal_stopped"></a>`terminal_stopped`
921
+
922
+ Acknowledges that a `terminal_stop` request has been received and is being processed. Handled by [`terminal_stop`](./terminal_handlers.py).
923
+
924
+ **Event Fields:**
925
+
926
+ * `terminal_id` (string, mandatory): The ID of the terminal being stopped.
927
+ * `status` (string, mandatory): The status of the stop operation (e.g., "stopping", "not_found").
928
+ * `message` (string, mandatory): A descriptive message about the status.
929
+ * `project_id` (string, optional): The project ID associated with the terminal.
930
+
931
+ ### <a name="terminal_stop_completed"></a>`terminal_stop_completed`
932
+
933
+ Confirms that a terminal session has been successfully stopped. Handled by [`terminal_stop`](./terminal_handlers.py).
934
+
935
+ **Event Fields:**
936
+
937
+ * `terminal_id` (string, mandatory): The ID of the stopped terminal.
938
+ * `status` (string, mandatory): The final status ("success", "timeout", "error", "not_found").
939
+ * `message` (string, mandatory): A descriptive message.
940
+ * `project_id` (string, optional): The project ID associated with the terminal.
941
+
942
+ ### <a name="terminal_list-event"></a>`terminal_list`
943
+
944
+ Provides the list of active terminal sessions in response to a `terminal_list` action. Handled by [`terminal_list`](./terminal_handlers.py).
945
+
946
+ **Event Fields:**
947
+
948
+ * `sessions` (array, mandatory): A list of active terminal session objects.
949
+ * `project_id` (string, optional): The project ID that was used to filter the list.
950
+
951
+ ### <a name="system_info-event"></a>`system_info`
952
+
953
+ Provides system information in response to a `system_info` action. Handled by [`system_info`](./system_handlers.py).
954
+
955
+ **Event Fields:**
956
+
957
+ * `info` (object, mandatory): An object containing system information, including:
958
+ * `cpu_percent` (float): CPU usage percentage.
959
+ * `memory` (object): Memory usage statistics.
960
+ * `disk` (object): Disk usage statistics.
961
+ * `os_info` (object): Operating system details, including `os_type`, `os_version`, `architecture`, `default_shell`, and `default_cwd`.
962
+ * `user_context` (object): Information about the user running the CLI, including:
963
+ * `username` (string): Resolved username (via `os.getlogin` or fallback).
964
+ * `username_source` (string): Which API resolved the username.
965
+ * `home` (string): Home directory detected for the CLI user.
966
+ * `uid` (integer|null): POSIX UID when available.
967
+ * `euid` (integer|null): Effective UID when available.
968
+ * `is_root` (boolean|null): True when running as root/administrator.
969
+ * `has_sudo` (boolean): Whether a `sudo` binary exists on the host.
970
+ * `sudo_user` (string|null): Value of `SUDO_USER` when set.
971
+ * `is_sudo_session` (boolean): True when the CLI was started via `sudo`.
972
+ * `playwright` (object): Optional Playwright runtime metadata when Playwright is installed:
973
+ * `installed` (boolean): True if Playwright is importable on the device.
974
+ * `version` (string|null): Exact package version when available.
975
+ * `browsers` (object): Browser-specific data keyed by Playwright browser names:
976
+ * `<browser>` (object): Per-browser info (variants: `chromium`, `firefox`, `webkit`).
977
+ * `available` (boolean): True when Playwright knows an executable path.
978
+ * `executable_path` (string|null): Absolute path to the browser binary when known.
979
+ * `error` (string|null): Any warning message captured while probing Playwright.
980
+ * `proxmox` (object): Detection hints for Proxmox VE nodes:
981
+ * `is_proxmox_node` (boolean): True when Proxmox artifacts (e.g., `/etc/proxmox-release`) exist.
982
+ * `version` (string|null): Raw contents of `/etc/proxmox-release` when readable.
983
+ * `infra` (object): Portacode infrastructure configuration snapshot:
984
+ * `configured` (boolean): True when `setup_proxmox_infra` stored an API token.
985
+ * `host` (string|null): Hostname used for the API client (usually `localhost`).
986
+ * `node` (string|null): Proxmox node name that was targeted.
987
+ * `user` (string|null): API token owner (e.g., `root@pam`).
988
+ * `token_name` (string|null): API token identifier.
989
+ * `default_storage` (string|null): Storage pool chosen for future containers.
990
+ * `templates` (array[string]): Cached list of available LXC templates.
991
+ * `last_verified` (string|null): ISO timestamp when the token was last validated.
992
+ * `network` (object):
993
+ * `applied` (boolean): True when the bridge/NAT services were successfully configured.
994
+ * `message` (string|null): Informational text about the network setup attempt.
995
+ * `bridge` (string): The bridge interface configured (typically `vmbr1`).
996
+ * `node_status` (object|null): Status response returned by the Proxmox API when validating the token.
997
+ * `portacode_version` (string): Installed CLI version returned by `portacode.__version__`.
998
+
999
+ ### `proxmox_infra_configured`
1000
+
1001
+ Emitted after a successful `setup_proxmox_infra` action. The event reports the stored API token metadata, template list, and network setup status.
1002
+
1003
+ **Event Fields:**
1004
+
1005
+ * `success` (boolean): True when the configuration completed.
1006
+ * `message` (string): User-facing summary (e.g., "Proxmox infrastructure configured").
1007
+ * `infra` (object): Same snapshot described under [`system_info`](#system_info-event) `proxmox.infra`.
1008
+
1009
+ ### <a name="clock_sync_response"></a>`clock_sync_response`
1010
+
1011
+ Reply sent by the gateway immediately after receiving a `clock_sync_request`. Devices use this event plus the measured round-trip time to keep their local `ntp_clock` offset accurate.
1012
+
1013
+ **Event Fields:**
1014
+
1015
+ * `event` (string): Always `clock_sync_response`.
1016
+ * `server_time` (integer): Server time in milliseconds.
1017
+ * `server_time_iso` (string, optional): ISO 8601 representation of `server_time`, useful for UI dashboards.
1018
+ * `server_receive_time` (integer, optional): Timestamp when the gateway received the sync request.
1019
+ * `server_send_time` (integer, optional): Timestamp when the gateway replied; used to compute a midpoint for latency compensation.
1020
+ * `request_id` (string, optional): Mirrors the request's `request_id`.
1021
+
1022
+ ### `update_portacode_response`
1023
+
1024
+ Reports the result of an `update_portacode_cli` action. Handled by [`update_portacode_cli`](./update_handler.py).
1025
+
1026
+ **Event Fields:**
1027
+
1028
+ * `success` (boolean, mandatory): Whether the update operation was successful.
1029
+ * `message` (string, optional): Success message when update completes.
1030
+ * `error` (string, optional): Error message when update fails.
1031
+ * `restart_required` (boolean, optional): Indicates if process restart is required (always true for successful updates).
1032
+
1033
+ ### <a name="file_read_response"></a>`file_read_response`
1034
+
1035
+ Returns the content of a file in response to a `file_read` action. Handled by [`file_read`](./file_handlers.py).
1036
+
1037
+ **Event Fields:**
1038
+
1039
+ * `path` (string, mandatory): The path of the file that was read.
1040
+ * `content` (string, mandatory): The file content returned (may be a slice when pagination parameters are used).
1041
+ * `size` (integer, mandatory): The total size of the file in bytes.
1042
+ * `total_lines` (integer, optional): Total number of lines detected in the file.
1043
+ * `returned_lines` (integer, optional): Number of lines included in `content`.
1044
+ * `start_line` (integer, optional): The first line number included in the response (if any lines were returned).
1045
+ * `requested_start_line` (integer, optional): The requested starting line supplied in the command.
1046
+ * `end_line` (integer, optional): The last line number included in the response.
1047
+ * `has_more_before` (boolean, optional): Whether there is additional content before the returned range.
1048
+ * `has_more_after` (boolean, optional): Whether there is additional content after the returned range.
1049
+ * `encoding` (string, optional): Encoding that was used while reading the file.
1050
+
1051
+ ### <a name="file_search_response"></a>`file_search_response`
1052
+
1053
+ Returns aggregated search results in response to a `file_search` action. Handled by [`file_search`](./file_handlers.py).
1054
+
1055
+ **Event Fields:**
1056
+
1057
+ * `root_path` (string, mandatory): The root directory that was searched.
1058
+ * `query` (string, mandatory): The query string that was used.
1059
+ * `match_case` (boolean, mandatory): Indicates if the search was case sensitive.
1060
+ * `regex` (boolean, mandatory): Indicates if the query was interpreted as a regular expression.
1061
+ * `whole_word` (boolean, mandatory): Indicates if the search matched whole words only.
1062
+ * `include_patterns` (array[string], mandatory): Effective include glob patterns.
1063
+ * `exclude_patterns` (array[string], mandatory): Effective exclude glob patterns.
1064
+ * `matches` (array, mandatory): List of match objects containing `relative_path`, `path`, `line_number`, `line`, `match_spans` `[start, end]`, `match_count`, and `line_truncated` (boolean).
1065
+ * `matches_returned` (integer, mandatory): Number of match entries returned (length of `matches`).
1066
+ * `total_matches` (integer, mandatory): Total number of matches found while scanning.
1067
+ * `files_scanned` (integer, mandatory): Count of files inspected.
1068
+ * `truncated` (boolean, mandatory): Indicates if additional matches exist beyond those returned.
1069
+ * `truncated_count` (integer, optional): Number of matches that were omitted due to truncation limits.
1070
+ * `max_results` (integer, mandatory): Maximum number of matches requested.
1071
+ * `max_matches_per_file` (integer, mandatory): Maximum matches requested per file.
1072
+ * `errors` (array[string], optional): Non-fatal errors encountered during scanning (e.g., unreadable files).
1073
+
1074
+ ### <a name="file_write_response"></a>`file_write_response`
1075
+
1076
+ Confirms that a file has been written successfully in response to a `file_write` action. Handled by [`file_write`](./file_handlers.py).
1077
+
1078
+ **Event Fields:**
1079
+
1080
+ * `path` (string, mandatory): The path of the file that was written.
1081
+ * `bytes_written` (integer, mandatory): The number of bytes written to the file.
1082
+ * `success` (boolean, mandatory): Indicates whether the write operation was successful.
1083
+
1084
+ ### <a name="file_apply_diff_response"></a>`file_apply_diff_response`
1085
+
1086
+ Reports the outcome of a [`file_apply_diff`](#file_apply_diff) action.
1087
+
1088
+ **Event Fields:**
1089
+
1090
+ * `event`: Always `"file_apply_diff_response"`.
1091
+ * `success`: Boolean indicating whether all hunks succeeded.
1092
+ * `status`: `"success"`, `"partial_failure"`, or `"failed"`.
1093
+ * `base_path`: Absolute base path used for relative diff entries.
1094
+ * `files_changed`: Number of files successfully updated.
1095
+ * `results`: Array containing one object per file with:
1096
+ * `path`: Absolute path on the device.
1097
+ * `status`: `"applied"` or `"error"`.
1098
+ * `action`: `"created"`, `"modified"`, or `"deleted"` (present for successes).
1099
+ * `bytes_written`: Bytes written for the file (0 for deletes).
1100
+ * `error`: Error text when the patch failed for that file.
1101
+ * `line`: Optional line number hint for mismatches.
1102
+
1103
+ The response is emitted even if some files fail so the caller can retry with corrected diffs.
1104
+
1105
+ ### <a name="file_preview_diff_response"></a>`file_preview_diff_response`
1106
+
1107
+ Reports the outcome of a [`file_preview_diff`](#file_preview_diff) action.
1108
+
1109
+ **Event Fields:**
1110
+
1111
+ * `event`: Always `"file_preview_diff_response"`.
1112
+ * `success`: Boolean indicating whether all previews rendered successfully.
1113
+ * `status`: `"success"`, `"partial_failure"`, or `"failed"`.
1114
+ * `base_path`: Absolute base path used for relative paths.
1115
+ * `previews`: Array containing one entry per file with:
1116
+ * `path`: Absolute path hint (used for syntax highlighting).
1117
+ * `relative_path`: Relative project path if known.
1118
+ * `status`: `"ready"` or `"error"`.
1119
+ * `html`: Rendered diff snippet (when status is `"ready"`).
1120
+ * `error`: Error text (when status is `"error"`).
1121
+ * `error`: Optional top-level error string when the entire preview failed (e.g., diff parse error).
1122
+
1123
+ ### <a name="directory_list_response"></a>`directory_list_response`
1124
+
1125
+ Returns the contents of a directory in response to a `directory_list` action. Handled by [`directory_list`](./file_handlers.py).
1126
+
1127
+ **Event Fields:**
1128
+
1129
+ * `path` (string, mandatory): The path of the directory that was listed.
1130
+ * `items` (array, mandatory): A list of objects, each representing a file or directory in the listed directory.
1131
+ * `count` (integer, mandatory): The number of items returned in this response (honours `limit`/`offset`).
1132
+ * `total_count` (integer, mandatory): Total number of entries in the directory before pagination.
1133
+ * `offset` (integer, optional): Offset that was applied.
1134
+ * `limit` (integer, optional): Limit that was applied (or `null` if none).
1135
+ * `has_more` (boolean, optional): Indicates whether additional items remain beyond the returned slice.
1136
+
1137
+ ### <a name="file_info_response"></a>`file_info_response`
1138
+
1139
+ Returns information about a file or directory in response to a `file_info` action. Handled by [`file_info`](./file_handlers.py).
1140
+
1141
+ **Event Fields:**
1142
+
1143
+ * `path` (string, mandatory): The path of the file or directory.
1144
+ * `exists` (boolean, mandatory): Indicates whether the file or directory exists.
1145
+ * `is_file` (boolean, optional): Indicates if the path is a file.
1146
+ * `is_dir` (boolean, optional): Indicates if the path is a directory.
1147
+ * `is_symlink` (boolean, optional): Indicates if the path is a symbolic link.
1148
+ * `size` (integer, optional): The size of the file in bytes.
1149
+ * `modified` (float, optional): The last modification time (timestamp).
1150
+ * `accessed` (float, optional): The last access time (timestamp).
1151
+ * `created` (float, optional): The creation time (timestamp).
1152
+ * `permissions` (string, optional): The file permissions in octal format.
1153
+ * `owner_uid` (integer, optional): The user ID of the owner.
1154
+ * `group_gid` (integer, optional): The group ID of the owner.
1155
+
1156
+ ### <a name="file_delete_response"></a>`file_delete_response`
1157
+
1158
+ Confirms that a file or directory has been deleted in response to a `file_delete` action. Handled by [`file_delete`](./file_handlers.py).
1159
+
1160
+ **Event Fields:**
1161
+
1162
+ * `path` (string, mandatory): The path of the deleted file or directory.
1163
+ * `deleted_type` (string, mandatory): The type of the deleted item ("file" or "directory").
1164
+ * `success` (boolean, mandatory): Indicates whether the deletion was successful.
1165
+
1166
+ ### <a name="file_create_response"></a>`file_create_response`
1167
+
1168
+ Confirms that a file has been created successfully in response to a `file_create` action. Handled by [`file_create`](./file_handlers.py).
1169
+
1170
+ **Event Fields:**
1171
+
1172
+ * `parent_path` (string, mandatory): The path of the parent directory where the file was created.
1173
+ * `file_name` (string, mandatory): The name of the created file.
1174
+ * `file_path` (string, mandatory): The full absolute path to the created file.
1175
+ * `success` (boolean, mandatory): Indicates whether the creation was successful.
1176
+
1177
+ ### <a name="folder_create_response"></a>`folder_create_response`
1178
+
1179
+ Confirms that a folder has been created successfully in response to a `folder_create` action. Handled by [`folder_create`](./file_handlers.py).
1180
+
1181
+ **Event Fields:**
1182
+
1183
+ * `parent_path` (string, mandatory): The path of the parent directory where the folder was created.
1184
+ * `folder_name` (string, mandatory): The name of the created folder.
1185
+ * `folder_path` (string, mandatory): The full absolute path to the created folder.
1186
+ * `success` (boolean, mandatory): Indicates whether the creation was successful.
1187
+
1188
+ ### <a name="file_rename_response"></a>`file_rename_response`
1189
+
1190
+ Confirms that a file or folder has been renamed successfully in response to a `file_rename` action. Handled by [`file_rename`](./file_handlers.py).
1191
+
1192
+ **Event Fields:**
1193
+
1194
+ * `old_path` (string, mandatory): The original path of the renamed item.
1195
+ * `new_path` (string, mandatory): The new path of the renamed item.
1196
+ * `new_name` (string, mandatory): The new name of the item.
1197
+ * `is_directory` (boolean, mandatory): Indicates whether the renamed item is a directory.
1198
+ * `success` (boolean, mandatory): Indicates whether the rename was successful.
1199
+
1200
+ ### <a name="content_response"></a>`content_response`
1201
+
1202
+ Returns cached content in response to a `content_request` action. This is part of the content caching system used for performance optimization. For large content (>200KB), the response is automatically chunked into multiple messages to ensure reliable transmission over WebSocket connections. Handled by [`content_request`](./file_handlers.py).
1203
+
1204
+ **Event Fields:**
1205
+
1206
+ * `request_id` (string, mandatory): The unique identifier from the corresponding request, used to match request and response.
1207
+ * `content_hash` (string, mandatory): The SHA-256 hash that was requested.
1208
+ * `content` (string, optional): The cached content or chunk content if found and `success` is true. Null if content was not found.
1209
+ * `success` (boolean, mandatory): Indicates whether the content was found and returned successfully.
1210
+ * `error` (string, optional): Error message if `success` is false (e.g., "Content not found in cache").
1211
+ * `chunked` (boolean, mandatory): Indicates whether this response is part of a chunked transfer. False for single responses, true for chunked responses.
1212
+
1213
+ **Chunked Transfer Fields (when `chunked` is true):**
1214
+
1215
+ * `transfer_id` (string, mandatory): Unique identifier for the chunked transfer session.
1216
+ * `chunk_index` (integer, mandatory): Zero-based index of this chunk in the sequence.
1217
+ * `chunk_count` (integer, mandatory): Total number of chunks in the transfer.
1218
+ * `chunk_size` (integer, mandatory): Size of this chunk in bytes.
1219
+ * `total_size` (integer, mandatory): Total size of the complete content in bytes.
1220
+ * `chunk_hash` (string, mandatory): SHA-256 hash of this chunk for verification.
1221
+ * `is_final_chunk` (boolean, mandatory): Indicates if this is the last chunk in the sequence.
1222
+
1223
+ **Chunked Transfer Process:**
1224
+
1225
+ 1. **Size Check**: Content >200KB is automatically chunked into 64KB chunks
1226
+ 2. **Sequential Delivery**: Chunks are sent in order with increasing `chunk_index`
1227
+ 3. **Client Assembly**: Client collects all chunks and verifies integrity using hashes
1228
+ 4. **Hash Verification**: Both individual chunk hashes and final content hash are verified
1229
+ 5. **Error Handling**: Missing chunks or hash mismatches trigger request failure
1230
+
1231
+ **Example Non-Chunked Response:**
1232
+ ```json
1233
+ {
1234
+ "event": "content_response",
1235
+ "request_id": "req_abc123",
1236
+ "content_hash": "sha256:...",
1237
+ "content": "Small content here",
1238
+ "success": true,
1239
+ "chunked": false
1240
+ }
1241
+ ```
1242
+
1243
+ **Example Chunked Response (first chunk):**
1244
+ ```json
1245
+ {
1246
+ "event": "content_response",
1247
+ "request_id": "req_abc123",
1248
+ "content_hash": "sha256:...",
1249
+ "content": "First chunk content...",
1250
+ "success": true,
1251
+ "chunked": true,
1252
+ "transfer_id": "transfer_xyz789",
1253
+ "chunk_index": 0,
1254
+ "chunk_count": 5,
1255
+ "chunk_size": 65536,
1256
+ "total_size": 300000,
1257
+ "chunk_hash": "chunk_sha256:...",
1258
+ "is_final_chunk": false
1259
+ }
1260
+ ```
1261
+
1262
+ ### Project State Events
1263
+
1264
+ ### <a name="project_state_initialized"></a>`project_state_initialized`
1265
+
1266
+ Confirms that project state has been successfully initialized for a client session. Contains the complete initial project state including file structure and Git metadata.
1267
+
1268
+ **Event Fields:**
1269
+
1270
+ * `project_id` (string, mandatory): The project ID for the initialized project state.
1271
+ * `project_folder_path` (string, mandatory): The absolute path to the project folder.
1272
+ * `is_git_repo` (boolean, mandatory): Whether the project folder is a Git repository.
1273
+ * `git_branch` (string, optional): The current Git branch name if available.
1274
+ * `git_status_summary` (object, optional): Summary of Git status counts (modified, added, deleted, untracked files).
1275
+ * `git_detailed_status` (object, optional): Detailed Git status with comprehensive file change information and content hashes. Contains:
1276
+ * `head_commit_hash` (string, optional): SHA hash of the HEAD commit.
1277
+ * `staged_changes` (array, optional): Array of staged file changes. Each change contains:
1278
+ * `file_repo_path` (string): Relative path from repository root.
1279
+ * `file_name` (string): Just the filename (basename).
1280
+ * `file_abs_path` (string): Absolute path to the file.
1281
+ * `change_type` (string): Type of change following git's native types ('added', 'modified', 'deleted', 'untracked'). Note: renames appear as separate 'deleted' and 'added' entries unless git detects them as modifications.
1282
+ * `content_hash` (string, optional): SHA256 hash of current file content. Null for deleted files.
1283
+ * `is_staged` (boolean): Always true for staged changes.
1284
+ * `diff_details` (object, optional): Per-character diff information computed using diff-match-patch algorithm. Contains:
1285
+ * `diffs` (array): Array of diff operations, each containing:
1286
+ * `operation` (integer): Diff operation type (-1 = delete, 0 = equal, 1 = insert).
1287
+ * `text` (string): The text content for this operation.
1288
+ * `stats` (object): Statistics about the diff:
1289
+ * `char_additions` (integer): Number of characters added.
1290
+ * `char_deletions` (integer): Number of characters deleted.
1291
+ * `char_unchanged` (integer): Number of characters unchanged.
1292
+ * `total_changes` (integer): Total number of character changes (additions + deletions).
1293
+ * `algorithm` (string): Always "diff-match-patch" indicating the algorithm used.
1294
+ * `unstaged_changes` (array, optional): Array of unstaged file changes with same structure as staged_changes but `is_staged` is always false.
1295
+ * `untracked_files` (array, optional): Array of untracked files with same structure as staged_changes but `is_staged` is always false and `change_type` is always 'untracked'.
1296
+ * `open_tabs` (array, mandatory): Array of tab objects currently open. Internally stored as a dictionary with unique keys to prevent duplicates, but serialized as an array for API responses. Each tab object contains:
1297
+ * `tab_id` (string, mandatory): Unique identifier for the tab.
1298
+ * `tab_type` (string, mandatory): Type of tab ("file", "diff", "untitled", "image", "audio", "video").
1299
+ * `title` (string, mandatory): Display title for the tab.
1300
+ * `file_path` (string, optional): Path for file-based tabs.
1301
+ * `content` (string, optional): Text content or base64 for media. When content caching is enabled, this field may be excluded from project state events if the content is available via `content_hash`.
1302
+ * `original_content` (string, optional): For diff tabs - original content. When content caching is enabled, this field may be excluded from project state events if the content is available via `original_content_hash`.
1303
+ * `modified_content` (string, optional): For diff tabs - modified content. When content caching is enabled, this field may be excluded from project state events if the content is available via `modified_content_hash`.
1304
+ * `is_dirty` (boolean, mandatory): Whether the tab has unsaved changes.
1305
+ * `mime_type` (string, optional): MIME type for media files.
1306
+ * `encoding` (string, optional): Content encoding (base64, utf-8, etc.).
1307
+ * `metadata` (object, optional): Additional metadata. When content caching is enabled, large metadata such as `html_diff_versions` may be excluded from project state events if available via `html_diff_hash`.
1308
+ * `content_hash` (string, optional): SHA-256 hash of the tab content for content caching optimization. When present, the content can be retrieved via [`content_request`](#content_request) action.
1309
+ * `original_content_hash` (string, optional): SHA-256 hash of the original content for diff tabs. When present, the original content can be retrieved via [`content_request`](#content_request) action.
1310
+ * `modified_content_hash` (string, optional): SHA-256 hash of the modified content for diff tabs. When present, the modified content can be retrieved via [`content_request`](#content_request) action.
1311
+ * `html_diff_hash` (string, optional): SHA-256 hash of the HTML diff versions JSON for diff tabs. When present, the HTML diff data can be retrieved via [`content_request`](#content_request) action as a JSON string.
1312
+ * `active_tab` (object, optional): The currently active tab object, or null if no tab is active.
1313
+ * `items` (array, mandatory): Flattened array of all visible file/folder items. Always includes root level items and one level down from the project root (since the project root is treated as expanded by default). Also includes items within explicitly expanded folders and one level down from each expanded folder. Each item object contains the following fields:
1314
+ * `name` (string, mandatory): The file or directory name.
1315
+ * `path` (string, mandatory): The absolute path to the file or directory.
1316
+ * `is_directory` (boolean, mandatory): Whether this item is a directory.
1317
+ * `parent_path` (string, mandatory): The absolute path to the parent directory.
1318
+ * `size` (integer, optional): File size in bytes. Only present for files, not directories.
1319
+ * `modified_time` (float, optional): Last modification time as Unix timestamp.
1320
+ * `is_git_tracked` (boolean, optional): Whether the file is tracked by Git. Only present if project is a Git repository.
1321
+ * `git_status` (string, optional): Git status of the file ("clean", "modified", "untracked", "ignored"). Only present if project is a Git repository.
1322
+ * `is_hidden` (boolean, mandatory): Whether the file/directory name starts with a dot (hidden file).
1323
+ * `is_ignored` (boolean, mandatory): Whether the file is ignored by Git. Only meaningful if project is a Git repository.
1324
+ * `children` (array, optional): Array of child FileItem objects for directories. Usually null in flattened structure as children are included as separate items.
1325
+ * `is_expanded` (boolean, mandatory): Whether this directory is expanded in the project tree. Only meaningful for directories.
1326
+ * `is_loaded` (boolean, mandatory): Whether the directory contents have been loaded and are available. Always true for files. For directories, true indicates that the directory is being monitored (in monitored_folders) and its contents are loaded and available in the items list, enabling immediate expansion when requested.
1327
+ * `timestamp` (float, mandatory): Unix timestamp of when the state was generated.
1328
+
1329
+ ### <a name="project_state_update"></a>`project_state_update`
1330
+
1331
+ Sent automatically when project state changes due to file system modifications, Git changes, or user actions. Contains the complete updated project state.
1332
+
1333
+ **Event Fields:**
1334
+
1335
+ * `project_id` (string, mandatory): The project ID this update applies to.
1336
+ * `project_folder_path` (string, mandatory): The absolute path to the project folder.
1337
+ * `is_git_repo` (boolean, mandatory): Whether the project folder is a Git repository.
1338
+ * `git_branch` (string, optional): The current Git branch name if available.
1339
+ * `git_status_summary` (object, optional): Updated summary of Git status counts.
1340
+ * `git_detailed_status` (object, optional): Updated detailed Git status with comprehensive file change information, content hashes, and per-character diff details (same structure as in `project_state_initialized`).
1341
+ * `open_tabs` (array, mandatory): Updated array of tab objects currently open. Internally stored as a dictionary with unique keys to prevent duplicates, but serialized as an array for API responses.
1342
+ * `active_tab` (object, optional): Updated active tab object.
1343
+ * `items` (array, mandatory): Updated flattened array of all visible file/folder items. Always includes root level items and one level down from the project root (since the project root is treated as expanded by default). Also includes items within explicitly expanded folders and one level down from each expanded folder. Each item object contains the following fields:
1344
+ * `name` (string, mandatory): The file or directory name.
1345
+ * `path` (string, mandatory): The absolute path to the file or directory.
1346
+ * `is_directory` (boolean, mandatory): Whether this item is a directory.
1347
+ * `parent_path` (string, mandatory): The absolute path to the parent directory.
1348
+ * `size` (integer, optional): File size in bytes. Only present for files, not directories.
1349
+ * `modified_time` (float, optional): Last modification time as Unix timestamp.
1350
+ * `is_git_tracked` (boolean, optional): Whether the file is tracked by Git. Only present if project is a Git repository.
1351
+ * `git_status` (string, optional): Git status of the file ("clean", "modified", "untracked", "ignored"). Only present if project is a Git repository.
1352
+ * `is_hidden` (boolean, mandatory): Whether the file/directory name starts with a dot (hidden file).
1353
+ * `is_ignored` (boolean, mandatory): Whether the file is ignored by Git. Only meaningful if project is a Git repository.
1354
+ * `children` (array, optional): Array of child FileItem objects for directories. Usually null in flattened structure as children are included as separate items.
1355
+ * `is_expanded` (boolean, mandatory): Whether this directory is expanded in the project tree. Only meaningful for directories.
1356
+ * `is_loaded` (boolean, mandatory): Whether the directory contents have been loaded and are available. Always true for files. For directories, true indicates that the directory is being monitored (in monitored_folders) and its contents are loaded and available in the items list, enabling immediate expansion when requested.
1357
+ * `timestamp` (float, mandatory): Unix timestamp of when the update was generated.
1358
+
1359
+ ### <a name="project_state_folder_expand_response"></a>`project_state_folder_expand_response`
1360
+
1361
+ Confirms the result of a folder expand operation.
1362
+
1363
+ **Event Fields:**
1364
+
1365
+ * `project_id` (string, mandatory): The project ID the operation was performed on.
1366
+ * `folder_path` (string, mandatory): The path to the folder that was expanded.
1367
+ * `success` (boolean, mandatory): Whether the expand operation was successful.
1368
+
1369
+ ### <a name="project_state_folder_collapse_response"></a>`project_state_folder_collapse_response`
1370
+
1371
+ Confirms the result of a folder collapse operation.
1372
+
1373
+ **Event Fields:**
1374
+
1375
+ * `project_id` (string, mandatory): The project ID the operation was performed on.
1376
+ * `folder_path` (string, mandatory): The path to the folder that was collapsed.
1377
+ * `success` (boolean, mandatory): Whether the collapse operation was successful.
1378
+
1379
+ ### <a name="project_state_file_open_response"></a>`project_state_file_open_response`
1380
+
1381
+ Confirms the result of a file open operation.
1382
+
1383
+ **Event Fields:**
1384
+
1385
+ * `project_id` (string, mandatory): The project ID the operation was performed on.
1386
+ * `file_path` (string, mandatory): The path to the file that was opened.
1387
+ * `success` (boolean, mandatory): Whether the file open operation was successful.
1388
+ * `set_active` (boolean, mandatory): Whether the file was also set as the active file.
1389
+
1390
+ ### <a name="project_state_tab_close_response"></a>`project_state_tab_close_response`
1391
+
1392
+ Confirms the result of a tab close operation.
1393
+
1394
+ **Event Fields:**
1395
+
1396
+ * `project_id` (string, mandatory): The project ID the operation was performed on.
1397
+ * `tab_id` (string, mandatory): The ID of the tab that was closed.
1398
+ * `success` (boolean, mandatory): Whether the tab close operation was successful.
1399
+
1400
+ ### <a name="project_state_set_active_tab_response"></a>`project_state_set_active_tab_response`
1401
+
1402
+ Confirms the result of setting an active tab.
1403
+
1404
+ **Event Fields:**
1405
+
1406
+ * `project_id` (string, mandatory): The project ID the operation was performed on.
1407
+ * `tab_id` (string, optional): The ID of the tab that was set as active (null if cleared).
1408
+ * `success` (boolean, mandatory): Whether the operation was successful.
1409
+
1410
+ ### <a name="project_state_diff_open_response"></a>`project_state_diff_open_response`
1411
+
1412
+ Confirms the result of opening a diff tab with git timeline references.
1413
+
1414
+ **Event Fields:**
1415
+
1416
+ * `project_id` (string, mandatory): The project ID the operation was performed on.
1417
+ * `file_path` (string, mandatory): The path to the file the diff tab was created for.
1418
+ * `from_ref` (string, mandatory): The source reference point that was used.
1419
+ * `to_ref` (string, mandatory): The target reference point that was used.
1420
+ * `from_hash` (string, optional): The commit hash used for `from_ref` if it was `"commit"`.
1421
+ * `to_hash` (string, optional): The commit hash used for `to_ref` if it was `"commit"`.
1422
+ * `success` (boolean, mandatory): Whether the diff tab creation was successful.
1423
+ * `error` (string, optional): Error message if the operation failed.
1424
+
1425
+ ### <a name="project_state_diff_content_response"></a>`project_state_diff_content_response`
1426
+
1427
+ Returns the requested content for a specific diff tab, sent in response to a [`project_state_diff_content_request`](#project_state_diff_content_request) action. For large content (>200KB), the response is automatically chunked into multiple messages to ensure reliable transmission over WebSocket connections.
1428
+
1429
+ **Event Fields:**
1430
+
1431
+ * `project_id` (string, mandatory): The project ID the operation was performed on.
1432
+ * `file_path` (string, mandatory): The path to the file the diff content is for.
1433
+ * `from_ref` (string, mandatory): The source reference point used in the diff.
1434
+ * `to_ref` (string, mandatory): The target reference point used in the diff.
1435
+ * `from_hash` (string, optional): The commit hash used for `from_ref` if it was `"commit"`.
1436
+ * `to_hash` (string, optional): The commit hash used for `to_ref` if it was `"commit"`.
1437
+ * `content_type` (string, mandatory): The type of content being returned (`"original"`, `"modified"`, `"html_diff"`, or `"all"`).
1438
+ * `request_id` (string, mandatory): The unique identifier from the request to match response with request.
1439
+ * `success` (boolean, mandatory): Whether the content retrieval was successful.
1440
+ * `content` (string, optional): The requested content or chunk content. For `html_diff` type, this is a JSON string containing the HTML diff versions object. For `all` type, this is a JSON string containing an object with `original_content`, `modified_content`, and `html_diff_versions` fields.
1441
+ * `error` (string, optional): Error message if the operation failed.
1442
+ * `chunked` (boolean, mandatory): Indicates whether this response is part of a chunked transfer. False for single responses, true for chunked responses.
1443
+
1444
+ **Chunked Transfer Fields (when `chunked` is true):**
1445
+
1446
+ * `transfer_id` (string, mandatory): Unique identifier for the chunked transfer session.
1447
+ * `chunk_index` (integer, mandatory): Zero-based index of this chunk in the sequence.
1448
+ * `chunk_count` (integer, mandatory): Total number of chunks in the transfer.
1449
+ * `chunk_size` (integer, mandatory): Size of this chunk in bytes.
1450
+ * `total_size` (integer, mandatory): Total size of the complete content in bytes.
1451
+ * `chunk_hash` (string, mandatory): SHA-256 hash of this chunk for verification.
1452
+ * `is_final_chunk` (boolean, mandatory): Indicates if this is the last chunk in the sequence.
1453
+
1454
+ **Note:** The chunked transfer process follows the same pattern as described in [`content_response`](#content_response), with content >200KB automatically split into 64KB chunks for reliable transmission.
1455
+
1456
+ ### <a name="project_state_git_stage_response"></a>`project_state_git_stage_response`
1457
+
1458
+ Confirms the result of a git stage operation. Supports responses for both single file and bulk operations.
1459
+
1460
+ **Event Fields:**
1461
+
1462
+ * `project_id` (string, mandatory): The project ID the operation was performed on.
1463
+ * `file_path` (string, optional): The path to the file that was staged (for single file operations).
1464
+ * `file_paths` (array of strings, optional): Array of paths to files that were staged (for bulk operations).
1465
+ * `stage_all` (boolean, optional): Present if the operation was a "stage all" operation.
1466
+ * `success` (boolean, mandatory): Whether the stage operation was successful.
1467
+ * `error` (string, optional): Error message if the operation failed.
1468
+
1469
+ ### <a name="project_state_git_unstage_response"></a>`project_state_git_unstage_response`
1470
+
1471
+ Confirms the result of a git unstage operation. Supports responses for both single file and bulk operations.
1472
+
1473
+ **Event Fields:**
1474
+
1475
+ * `project_id` (string, mandatory): The project ID the operation was performed on.
1476
+ * `file_path` (string, optional): The path to the file that was unstaged (for single file operations).
1477
+ * `file_paths` (array of strings, optional): Array of paths to files that were unstaged (for bulk operations).
1478
+ * `unstage_all` (boolean, optional): Present if the operation was an "unstage all" operation.
1479
+ * `success` (boolean, mandatory): Whether the unstage operation was successful.
1480
+ * `error` (string, optional): Error message if the operation failed.
1481
+
1482
+ ### <a name="project_state_git_revert_response"></a>`project_state_git_revert_response`
1483
+
1484
+ Confirms the result of a git revert operation. Supports responses for both single file and bulk operations.
1485
+
1486
+ **Event Fields:**
1487
+
1488
+ * `project_id` (string, mandatory): The project ID the operation was performed on.
1489
+ * `file_path` (string, optional): The path to the file that was reverted (for single file operations).
1490
+ * `file_paths` (array of strings, optional): Array of paths to files that were reverted (for bulk operations).
1491
+ * `revert_all` (boolean, optional): Present if the operation was a "revert all" operation.
1492
+ * `success` (boolean, mandatory): Whether the revert operation was successful.
1493
+ * `error` (string, optional): Error message if the operation failed.
1494
+
1495
+ ### <a name="project_state_git_commit_response"></a>`project_state_git_commit_response`
1496
+
1497
+ Confirms the result of a git commit operation.
1498
+
1499
+ **Event Fields:**
1500
+
1501
+ * `project_id` (string, mandatory): The project ID the operation was performed on.
1502
+ * `commit_message` (string, mandatory): The commit message that was used.
1503
+ * `success` (boolean, mandatory): Whether the commit operation was successful.
1504
+ * `error` (string, optional): Error message if the operation failed.
1505
+ * `commit_hash` (string, optional): The SHA hash of the new commit if successful.
1506
+
1507
+ ### Client Session Events
1508
+
1509
+ ### <a name="request_client_sessions"></a>`request_client_sessions`
1510
+
1511
+ Sent by the device to request the current list of connected client sessions from the server. This is an internal event used during device initialization and reconnection.
1512
+
1513
+ **Event Fields:**
1514
+
1515
+ This event carries no additional fields.
1516
+
1517
+ ### Terminal Data
1518
+
1519
+ ### Terminal I/O Data Formats
1520
+
1521
+ Terminal I/O data can be sent in two formats depending on the implementation:
1522
+
1523
+ #### Modern Format (Recommended)
1524
+
1525
+ Terminal data is sent as a proper [`terminal_data`](#terminal_data) event on the control channel (channel 0) with client session targeting support:
1526
+
1527
+ ```json
1528
+ {
1529
+ "channel": 0,
1530
+ "payload": {
1531
+ "event": "terminal_data",
1532
+ "channel": "<terminal_uuid>",
1533
+ "data": "<terminal_output_string>",
1534
+ "device_id": 123,
1535
+ "project_id": "<project_uuid>",
1536
+ "client_sessions": ["channel.abc123", "channel.def456"]
1537
+ }
1538
+ }
1539
+ ```
1540
+
1541
+ This format follows the standard event structure with automatic system field injection (device_id, project_id, client_sessions) for proper routing and security.
1542
+
1543
+ #### Legacy Format (Deprecated)
1544
+
1545
+ Terminal data sent directly on terminal channels (not on the control channel):
1546
+
1547
+ ```json
1548
+ {
1549
+ "channel": "<terminal_uuid>",
1550
+ "payload": "<terminal_output_string>"
1551
+ }
1552
+ ```
1553
+
1554
+ * Terminal output is sent as raw string data in the payload
1555
+ * Input to terminals is sent the same way but in the opposite direction
1556
+ * No event wrapper or client targeting is used
1557
+ * This format broadcasts to all sessions for the device owner
1558
+
1559
+ ### Server-Side Events
1560
+
1561
+ ### <a name="device_status"></a>`device_status`
1562
+
1563
+ Sent by the server to clients to indicate device online/offline status changes.
1564
+
1565
+ **Event Fields:**
1566
+
1567
+ * `device` (object, mandatory): Device status information
1568
+ * `id` (integer, mandatory): Device ID
1569
+ * `online` (boolean, mandatory): Whether the device is online
1570
+
1571
+ ### <a name="devices"></a>`devices`
1572
+
1573
+ Sent by the server to clients to provide initial device list snapshot.
1574
+
1575
+ **Event Fields:**
1576
+
1577
+ * `devices` (array, mandatory): Array of device objects with status information