replx-agent 2.0.0__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 replx-agent might be problematic. Click here for more details.

Files changed (46) hide show
  1. replx_agent/__init__.py +3 -0
  2. replx_agent/__main__.py +5 -0
  3. replx_agent/commands.py +131 -0
  4. replx_agent/helpers/__init__.py +3 -0
  5. replx_agent/helpers/scanner.py +308 -0
  6. replx_agent/pkg/__init__.py +11 -0
  7. replx_agent/pkg/compiler.py +153 -0
  8. replx_agent/pkg/registry.py +387 -0
  9. replx_agent/pkg/service.py +690 -0
  10. replx_agent/pkg/store.py +83 -0
  11. replx_agent/protocol/__init__.py +11 -0
  12. replx_agent/protocol/repl.py +1029 -0
  13. replx_agent/protocol/storage.py +1018 -0
  14. replx_agent/rpc/__init__.py +3 -0
  15. replx_agent/rpc/protocol.py +220 -0
  16. replx_agent/server/__init__.py +3 -0
  17. replx_agent/server/__main__.py +4 -0
  18. replx_agent/server/command_dispatcher.py +49 -0
  19. replx_agent/server/connection_manager.py +654 -0
  20. replx_agent/server/core.py +1221 -0
  21. replx_agent/server/handlers/__init__.py +24 -0
  22. replx_agent/server/handlers/exec.py +553 -0
  23. replx_agent/server/handlers/filesystem.py +383 -0
  24. replx_agent/server/handlers/firmware.py +118 -0
  25. replx_agent/server/handlers/i2c.py +23 -0
  26. replx_agent/server/handlers/package.py +182 -0
  27. replx_agent/server/handlers/repl.py +188 -0
  28. replx_agent/server/handlers/session.py +205 -0
  29. replx_agent/server/handlers/spi.py +23 -0
  30. replx_agent/server/handlers/transfer.py +600 -0
  31. replx_agent/server/handlers/uart.py +32 -0
  32. replx_agent/server/session_manager.py +391 -0
  33. replx_agent/server/tcp.py +207 -0
  34. replx_agent/terminal.py +566 -0
  35. replx_agent/transport/__init__.py +32 -0
  36. replx_agent/transport/serial.py +220 -0
  37. replx_agent/utils/__init__.py +95 -0
  38. replx_agent/utils/constants.py +78 -0
  39. replx_agent/utils/device_info.py +183 -0
  40. replx_agent/utils/exceptions.py +31 -0
  41. replx_agent-2.0.0.dist-info/METADATA +74 -0
  42. replx_agent-2.0.0.dist-info/RECORD +46 -0
  43. replx_agent-2.0.0.dist-info/WHEEL +5 -0
  44. replx_agent-2.0.0.dist-info/entry_points.txt +2 -0
  45. replx_agent-2.0.0.dist-info/licenses/LICENSE +21 -0
  46. replx_agent-2.0.0.dist-info/top_level.txt +1 -0
@@ -0,0 +1,383 @@
1
+ import logging
2
+ import posixpath
3
+
4
+ from replx_agent.utils.constants import MAX_PAYLOAD_SIZE
5
+ from replx_agent.utils.exceptions import TransportError, FileSystemError
6
+ from ..command_dispatcher import CommandContext
7
+
8
+ logger = logging.getLogger(__name__)
9
+
10
+
11
+ class DisconnectedError(RuntimeError):
12
+ pass
13
+
14
+
15
+ class FilesystemCommandsMixin:
16
+ def _cmd_ls(self, ctx: CommandContext, path: str = "/", detailed: bool = False, recursive: bool = False) -> dict:
17
+ conn = ctx.connection
18
+ if not conn or not conn.file_system:
19
+ raise RuntimeError("Not connected")
20
+
21
+ try:
22
+ real_path = self._to_real_path(path, conn)
23
+
24
+ if detailed or recursive:
25
+ items = conn.file_system.ls_detailed(real_path)
26
+ if recursive:
27
+ all_items = []
28
+ def recurse(p):
29
+ try:
30
+ items = conn.file_system.ls_detailed(p)
31
+ for name, size, is_dir in items:
32
+ full_path = f"{p.rstrip('/')}/{name}"
33
+ all_items.append((full_path, size, is_dir))
34
+ if is_dir:
35
+ recurse(full_path)
36
+ except TransportError:
37
+ raise
38
+ except FileSystemError as e:
39
+ msg = str(e).lower()
40
+ if "enoent" in msg or "enotdir" in msg or "not found" in msg:
41
+ pass
42
+ else:
43
+ logger.warning("Failed to list %s: %s", p, e)
44
+ except Exception as e:
45
+ logger.warning("Unexpected error listing %s: %s", p, e)
46
+ recurse(real_path)
47
+ items = all_items
48
+
49
+ return {
50
+ "items": [{"name": self._to_virtual_path(n, conn), "size": s, "is_dir": d} for n, s, d in items]
51
+ }
52
+ else:
53
+ items = conn.file_system.ls(real_path)
54
+ return {"items": items}
55
+ except FileSystemError as e:
56
+ msg = str(e)
57
+ lower_msg = msg.lower()
58
+ if "enoent" in lower_msg or "no such file" in lower_msg or "not found" in lower_msg:
59
+ raise RuntimeError(f"Path does not exist: {path}")
60
+ if "enotdir" in lower_msg or "not a directory" in lower_msg or "ls failed: 20" in lower_msg:
61
+ if not recursive:
62
+ is_dir = False
63
+ file_exists = None
64
+ if hasattr(conn.file_system, "_file_exists"):
65
+ try:
66
+ file_exists = conn.file_system._file_exists(real_path)
67
+ except Exception:
68
+ file_exists = None
69
+ if file_exists is False:
70
+ raise RuntimeError(f"Path does not exist: {path}")
71
+ size = conn.file_system.state(real_path)
72
+ name = path.rsplit('/', 1)[-1] if '/' in path else path
73
+ return {
74
+ "items": [{"name": name, "size": size, "is_dir": is_dir}]
75
+ }
76
+ raise RuntimeError(f"Path is not a directory: {path}")
77
+ raise RuntimeError(f"ls failed: {msg}")
78
+ except TransportError as e:
79
+ raise DisconnectedError(f"Serial port disconnected: {e}")
80
+ except Exception as e:
81
+ raise RuntimeError(f"ls failed: {e}")
82
+
83
+ def _cmd_ls_recursive(self, ctx: CommandContext, path: str = "/") -> dict:
84
+ conn = ctx.connection
85
+ if not conn or not conn.file_system:
86
+ raise RuntimeError("Not connected")
87
+
88
+ try:
89
+ real_path = self._to_real_path(path, conn)
90
+ items = conn.file_system.ls_recursive(real_path)
91
+ return {
92
+ "items": [{"name": self._to_virtual_path(n, conn), "size": s, "is_dir": d} for n, s, d in items],
93
+ "path": path
94
+ }
95
+ except FileSystemError as e:
96
+ msg = str(e)
97
+ lower_msg = msg.lower()
98
+ if "enoent" in lower_msg or "no such file" in lower_msg or "not found" in lower_msg:
99
+ raise RuntimeError(f"Path does not exist: {path}")
100
+ if "enotdir" in lower_msg or "not a directory" in lower_msg or "ls failed: 20" in lower_msg:
101
+ raise RuntimeError(f"Path is not a directory: {path}")
102
+ raise RuntimeError(f"ls_recursive failed: {msg}")
103
+ except TransportError as e:
104
+ raise DisconnectedError(f"Serial port disconnected: {e}")
105
+ except Exception as e:
106
+ raise RuntimeError(f"ls_recursive failed: {e}")
107
+
108
+ def _cmd_cat(self, ctx: CommandContext, path: str) -> dict:
109
+ conn = ctx.connection
110
+ if not conn or not conn.file_system:
111
+ raise RuntimeError("Not connected")
112
+
113
+ try:
114
+ real_path = self._to_real_path(path, conn)
115
+ content = conn.file_system.get(real_path)
116
+
117
+ if not isinstance(content, (bytes, bytearray)):
118
+ content = content.encode('utf-8') if isinstance(content, str) else bytes(content)
119
+
120
+ if b'\x00' in content:
121
+ text_content = None
122
+ else:
123
+ try:
124
+ text_content = content.decode('utf-8')
125
+ except ValueError:
126
+ text_content = None
127
+
128
+ if text_content is None:
129
+ return {
130
+ "content": content.hex(),
131
+ "is_binary": True,
132
+ "size": len(content)
133
+ }
134
+
135
+ if len(text_content) > MAX_PAYLOAD_SIZE - 1000:
136
+ text_content = text_content[:MAX_PAYLOAD_SIZE - 1100] + "\n... [truncated]"
137
+
138
+ return {"content": text_content, "is_binary": False}
139
+ except TransportError as e:
140
+ raise DisconnectedError(f"Serial port disconnected: {e}")
141
+ except Exception as e:
142
+ raise RuntimeError(f"cat failed: {e}")
143
+
144
+ def _cmd_rm(self, ctx: CommandContext, path: str, recursive: bool = False) -> dict:
145
+ conn = ctx.connection
146
+ if not conn or not conn.file_system:
147
+ raise RuntimeError("Not connected")
148
+
149
+ try:
150
+ real_path = self._to_real_path(path, conn)
151
+ if recursive:
152
+ conn.file_system.rmdir(real_path)
153
+ else:
154
+ conn.file_system.rm(real_path)
155
+ return {"removed": path}
156
+ except TransportError as e:
157
+ raise DisconnectedError(f"Serial port disconnected: {e}")
158
+ except Exception as e:
159
+ raise RuntimeError(f"rm failed: {e}")
160
+
161
+ def _cmd_rmdir(self, ctx: CommandContext, path: str) -> dict:
162
+ conn = ctx.connection
163
+ if not conn or not conn.file_system:
164
+ raise RuntimeError("Not connected")
165
+
166
+ try:
167
+ real_path = self._to_real_path(path, conn)
168
+ conn.file_system.rmdir(real_path)
169
+ return {"removed": path}
170
+ except TransportError as e:
171
+ raise DisconnectedError(f"Serial port disconnected: {e}")
172
+ except Exception as e:
173
+ raise RuntimeError(f"rmdir failed: {e}")
174
+
175
+ def _cmd_mkdir(self, ctx: CommandContext, path: str) -> dict:
176
+ conn = ctx.connection
177
+ if not conn or not conn.file_system:
178
+ raise RuntimeError("Not connected")
179
+
180
+ try:
181
+ real_path = self._to_real_path(path, conn)
182
+ if not conn.file_system.mkdir(real_path):
183
+ raise RuntimeError(f"Could not create directory: {path}")
184
+ return {"created": path}
185
+ except TransportError as e:
186
+ raise DisconnectedError(f"Serial port disconnected: {e}")
187
+ except Exception as e:
188
+ raise RuntimeError(f"mkdir failed: {e}")
189
+
190
+ def _cmd_is_dir(self, ctx: CommandContext, path: str) -> dict:
191
+ conn = ctx.connection
192
+ if not conn or not conn.file_system:
193
+ raise RuntimeError("Not connected")
194
+
195
+ try:
196
+ real_path = self._to_real_path(path, conn)
197
+ result = conn.file_system.is_dir(real_path)
198
+ return {"path": path, "is_dir": result}
199
+ except TransportError as e:
200
+ raise DisconnectedError(f"Serial port disconnected: {e}")
201
+ except Exception as e:
202
+ raise RuntimeError(f"is_dir failed: {e}")
203
+
204
+ def _cmd_touch(self, ctx: CommandContext, path: str) -> dict:
205
+ conn = ctx.connection
206
+ if not conn or not conn.file_system:
207
+ raise RuntimeError("Not connected")
208
+
209
+ try:
210
+ real_path = self._to_real_path(path, conn)
211
+ conn.file_system.touch(real_path, core=conn.core)
212
+ return {"created": path}
213
+ except TransportError as e:
214
+ raise DisconnectedError(f"Serial port disconnected: {e}")
215
+ except Exception as e:
216
+ raise RuntimeError(f"touch failed: {e}")
217
+
218
+ def _cmd_cp(self, ctx: CommandContext, source: str, dest: str, recursive: bool = False) -> dict:
219
+ conn = ctx.connection
220
+ if not conn or not conn.file_system:
221
+ raise RuntimeError("Not connected")
222
+
223
+ try:
224
+ real_source = self._to_real_path(source, conn)
225
+ real_dest = self._to_real_path(dest, conn)
226
+
227
+ is_source_dir = conn.file_system.is_dir(real_source)
228
+
229
+ if is_source_dir and not recursive:
230
+ raise RuntimeError(f"cp: {source} is a directory (use -r to copy recursively)")
231
+
232
+ if is_source_dir:
233
+ dest_exists = False
234
+ try:
235
+ dest_exists = conn.file_system.is_dir(real_dest)
236
+ except TransportError:
237
+ raise
238
+ except Exception:
239
+ pass
240
+
241
+ if dest_exists:
242
+ source_name = posixpath.basename(real_source.rstrip('/'))
243
+ real_dest = posixpath.join(real_dest, source_name)
244
+
245
+ try:
246
+ conn.file_system.mkdir(real_dest)
247
+ except TransportError:
248
+ raise
249
+ except Exception:
250
+ pass
251
+
252
+ success_count = 0
253
+
254
+ def copy_recursive(src_dir, dst_dir):
255
+ nonlocal success_count
256
+ items = conn.file_system.ls_detailed(src_dir)
257
+
258
+ for name, size, is_dir in items:
259
+ src_path = src_dir.rstrip('/') + '/' + name
260
+ dst_path = dst_dir.rstrip('/') + '/' + name
261
+
262
+ if is_dir:
263
+ try:
264
+ conn.file_system.mkdir(dst_path)
265
+ except TransportError:
266
+ raise
267
+ except Exception:
268
+ pass
269
+ copy_recursive(src_path, dst_path)
270
+ else:
271
+ conn.file_system.cp(src_path, dst_path)
272
+ success_count += 1
273
+
274
+ copy_recursive(real_source, real_dest)
275
+
276
+ return {"copied": True, "source": source, "dest": dest, "files_count": success_count}
277
+ else:
278
+ try:
279
+ if conn.file_system.is_dir(real_dest):
280
+ real_dest = posixpath.join(real_dest, posixpath.basename(real_source))
281
+ except TransportError:
282
+ raise
283
+ except Exception:
284
+ pass
285
+
286
+ conn.file_system.cp(real_source, real_dest)
287
+ return {"copied": True, "source": source, "dest": dest}
288
+ except TransportError as e:
289
+ raise DisconnectedError(f"Serial port disconnected: {e}")
290
+ except Exception as e:
291
+ raise RuntimeError(f"cp failed: {e}")
292
+
293
+ def _cmd_mv(self, ctx: CommandContext, source: str, dest: str) -> dict:
294
+ conn = ctx.connection
295
+ if not conn or not conn.file_system:
296
+ raise RuntimeError("Not connected")
297
+
298
+ try:
299
+ real_source = self._to_real_path(source, conn)
300
+ real_dest = self._to_real_path(dest, conn)
301
+
302
+ dest_is_dir = False
303
+ try:
304
+ dest_is_dir = conn.file_system.is_dir(real_dest)
305
+ except TransportError:
306
+ raise
307
+ except Exception:
308
+ pass
309
+
310
+ if dest_is_dir:
311
+ source_name = posixpath.basename(real_source.rstrip('/'))
312
+ real_dest = posixpath.join(real_dest, source_name)
313
+
314
+ conn.file_system.mv(real_source, real_dest)
315
+ return {"moved": True, "source": source, "dest": dest}
316
+ except TransportError as e:
317
+ raise DisconnectedError(f"Serial port disconnected: {e}")
318
+ except Exception as e:
319
+ raise RuntimeError(f"mv failed: {e}")
320
+
321
+ def _cmd_stat(self, ctx: CommandContext, path: str) -> dict:
322
+ conn = ctx.connection
323
+ if not conn or not conn.file_system:
324
+ raise RuntimeError("Not connected")
325
+
326
+ try:
327
+ real_path = self._to_real_path(path, conn)
328
+ size = conn.file_system.state(real_path)
329
+ is_dir = conn.file_system.is_dir(real_path)
330
+ return {
331
+ "path": path,
332
+ "size": size,
333
+ "is_dir": is_dir
334
+ }
335
+ except TransportError as e:
336
+ raise DisconnectedError(f"Serial port disconnected: {e}")
337
+ except Exception as e:
338
+ raise RuntimeError(f"stat failed: {e}")
339
+
340
+ def _cmd_mem(self, ctx: CommandContext) -> dict:
341
+ conn = ctx.connection
342
+ if not conn or not conn.file_system:
343
+ raise RuntimeError("Not connected")
344
+
345
+ try:
346
+ result = conn.file_system.mem()
347
+ return {"mem": result}
348
+ except TransportError as e:
349
+ raise DisconnectedError(f"Serial port disconnected: {e}")
350
+ except Exception as e:
351
+ raise RuntimeError(f"mem failed: {e}")
352
+
353
+ def _cmd_df(self, ctx: CommandContext) -> dict:
354
+ conn = ctx.connection
355
+ if not conn or not conn.file_system:
356
+ raise RuntimeError("Not connected")
357
+
358
+ try:
359
+ result = conn.file_system.df()
360
+ return {
361
+ "total": result[0],
362
+ "used": result[1],
363
+ "free": result[2],
364
+ "percent": result[3]
365
+ }
366
+ except TransportError as e:
367
+ raise DisconnectedError(f"Serial port disconnected: {e}")
368
+ except Exception as e:
369
+ raise RuntimeError(f"df failed: {e}")
370
+
371
+ def _cmd_format(self, ctx: CommandContext) -> dict:
372
+ conn = ctx.connection
373
+ if not conn or not conn.file_system:
374
+ raise RuntimeError("Not connected")
375
+
376
+ try:
377
+ result = conn.file_system.format()
378
+ return {"formatted": result}
379
+ except TransportError as e:
380
+ raise DisconnectedError(f"Serial port disconnected: {e}")
381
+ except Exception as e:
382
+ import traceback
383
+ raise RuntimeError(f"format failed: {e}\n{traceback.format_exc()}")
@@ -0,0 +1,118 @@
1
+ import os
2
+ import subprocess
3
+ import sys
4
+ import time
5
+
6
+ from ..command_dispatcher import CommandContext
7
+
8
+ _FIRMWARE_STAGE_TTL_S = 30 * 60
9
+
10
+
11
+ class FirmwareCommandsMixin:
12
+ def _get_firmware_stage(self, stage_id: str):
13
+ if not stage_id:
14
+ raise ValueError("Firmware stage ID is required")
15
+ with self._firmware_stages_lock:
16
+ stage = self._firmware_stages.get(stage_id)
17
+ if stage and time.monotonic() - stage.created_at > _FIRMWARE_STAGE_TTL_S:
18
+ self._firmware_stages.pop(stage_id, None)
19
+ expired = stage
20
+ stage = None
21
+ else:
22
+ expired = None
23
+ if expired:
24
+ try:
25
+ os.unlink(expired.path)
26
+ except FileNotFoundError:
27
+ pass
28
+ if stage is None:
29
+ raise ValueError("Firmware image staging expired or was not found")
30
+ return stage
31
+
32
+ @staticmethod
33
+ def _run_esptool(arguments: list[str]) -> subprocess.CompletedProcess:
34
+ return subprocess.run(
35
+ [sys.executable, "-m", "esptool", *arguments],
36
+ capture_output=True,
37
+ text=True,
38
+ check=False,
39
+ )
40
+
41
+ def _cmd_firmware_info(self, ctx: CommandContext, stage_id: str) -> dict:
42
+ stage = self._get_firmware_stage(stage_id)
43
+ result = self._run_esptool([
44
+ "--chip", "esp32p4", "image-info", stage.path,
45
+ ])
46
+ output = "\n".join(
47
+ part.strip() for part in (result.stdout, result.stderr) if part.strip()
48
+ )
49
+ if result.returncode:
50
+ raise RuntimeError(
51
+ f"esptool image-info exited with code {result.returncode}.\n{output}"
52
+ )
53
+ return {
54
+ "file_name": stage.file_name,
55
+ "size": stage.size,
56
+ "sha256": stage.digest,
57
+ "output": output,
58
+ }
59
+
60
+ def _cmd_firmware_flash(
61
+ self,
62
+ ctx: CommandContext,
63
+ stage_id: str,
64
+ port: str,
65
+ baud: int = 460800,
66
+ erase: bool = False,
67
+ ) -> dict:
68
+ if not port:
69
+ raise ValueError("Flash port is required")
70
+ if isinstance(baud, bool) or not isinstance(baud, int) or baud <= 0:
71
+ raise ValueError("Baud rate must be a positive integer")
72
+
73
+ stage = self._get_firmware_stage(stage_id)
74
+ self._cmd_disconnect_port(ctx, port=port)
75
+
76
+ arguments = [
77
+ "--chip", "esp32p4", "--port", port, "--baud", str(baud),
78
+ "write-flash",
79
+ ]
80
+ if erase:
81
+ arguments.append("--erase-all")
82
+ arguments.extend(["0x2000", stage.path])
83
+ result = self._run_esptool(arguments)
84
+ output = "\n".join(
85
+ part.strip() for part in (result.stdout, result.stderr) if part.strip()
86
+ )
87
+ if result.returncode:
88
+ raise RuntimeError(
89
+ f"esptool exited with code {result.returncode}. Check the image's chip "
90
+ f"revision, C6 variant and port.\n{output}"
91
+ )
92
+
93
+ with self._firmware_stages_lock:
94
+ self._firmware_stages.pop(stage_id, None)
95
+ try:
96
+ os.unlink(stage.path)
97
+ except FileNotFoundError:
98
+ pass
99
+
100
+ return {
101
+ "flashed": stage.file_name,
102
+ "port": port,
103
+ "baud": baud,
104
+ "erase": erase,
105
+ "output": output,
106
+ }
107
+
108
+ def _cmd_firmware_discard(
109
+ self, ctx: CommandContext, stage_id: str
110
+ ) -> dict:
111
+ with self._firmware_stages_lock:
112
+ stage = self._firmware_stages.pop(stage_id, None)
113
+ if stage:
114
+ try:
115
+ os.unlink(stage.path)
116
+ except FileNotFoundError:
117
+ pass
118
+ return {"discarded": stage is not None}
@@ -0,0 +1,23 @@
1
+ from ..command_dispatcher import CommandContext
2
+
3
+
4
+ class I2cCommandsMixin:
5
+ def _i2c_resolve_port(self, ctx: CommandContext) -> str:
6
+ port = self._resolve_connection_for_session(ctx.ppid, ctx.explicit_port)
7
+ if not port:
8
+ raise ValueError("No active connection. Connect to a board first.")
9
+ return port
10
+
11
+ def _cmd_i2c_bus_set(self, ctx: CommandContext, **kwargs) -> dict:
12
+ port = self._i2c_resolve_port(ctx)
13
+ self._i2c_bus[port] = dict(kwargs)
14
+ return self._i2c_bus[port]
15
+
16
+ def _cmd_i2c_bus_get(self, ctx: CommandContext) -> dict:
17
+ port = self._i2c_resolve_port(ctx)
18
+ return self._i2c_bus.get(port) or {}
19
+
20
+ def _cmd_i2c_bus_clear(self, ctx: CommandContext) -> dict:
21
+ port = self._i2c_resolve_port(ctx)
22
+ self._i2c_bus.pop(port, None)
23
+ return {}