puda-comms 0.0.2__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.
- puda_comms/__init__.py +5 -0
- puda_comms/command_service.py +635 -0
- puda_comms/execution_state.py +89 -0
- puda_comms/machine_client.py +771 -0
- puda_comms/models.py +88 -0
- puda_comms-0.0.2.dist-info/METADATA +310 -0
- puda_comms-0.0.2.dist-info/RECORD +8 -0
- puda_comms-0.0.2.dist-info/WHEEL +4 -0
puda_comms/models.py
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Models for Puda Comms.
|
|
3
|
+
"""
|
|
4
|
+
|
|
5
|
+
from enum import Enum
|
|
6
|
+
from typing import Optional, Dict, Any
|
|
7
|
+
from datetime import datetime, timezone
|
|
8
|
+
from pydantic import BaseModel, Field
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
class CommandResponseStatus(str, Enum):
|
|
12
|
+
"""Status of a command response."""
|
|
13
|
+
SUCCESS = 'success'
|
|
14
|
+
ERROR = 'error'
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
class CommandResponseCode(str, Enum):
|
|
18
|
+
"""Error codes for command responses."""
|
|
19
|
+
COMMAND_CANCELLED = 'COMMAND_CANCELLED'
|
|
20
|
+
JSON_DECODE_ERROR = 'JSON_DECODE_ERROR'
|
|
21
|
+
EXECUTION_ERROR = 'EXECUTION_ERROR'
|
|
22
|
+
EXECUTION_LOCKED = 'EXECUTION_LOCKED'
|
|
23
|
+
UNKNOWN_COMMAND = 'UNKNOWN_COMMAND'
|
|
24
|
+
PAUSE_ERROR = 'PAUSE_ERROR'
|
|
25
|
+
RESUME_ERROR = 'RESUME_ERROR'
|
|
26
|
+
NO_EXECUTION = 'NO_EXECUTION'
|
|
27
|
+
RUN_ID_MISMATCH = 'RUN_ID_MISMATCH'
|
|
28
|
+
CANCEL_ERROR = 'CANCEL_ERROR'
|
|
29
|
+
MACHINE_PAUSED = 'MACHINE_PAUSED'
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
class MessageType(str, Enum):
|
|
33
|
+
"""Type of NATS message."""
|
|
34
|
+
COMMAND = 'command'
|
|
35
|
+
RESPONSE = 'response'
|
|
36
|
+
LOG = 'log'
|
|
37
|
+
ALERT = 'alert'
|
|
38
|
+
MEDIA = 'media'
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
class ImmediateCommand(str, Enum):
|
|
42
|
+
"""Command names for immediate commands."""
|
|
43
|
+
PAUSE = 'pause'
|
|
44
|
+
RESUME = 'resume'
|
|
45
|
+
CANCEL = 'cancel'
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
def _get_current_timestamp() -> str:
|
|
49
|
+
"""Get current timestamp in ISO 8601 UTC format."""
|
|
50
|
+
return datetime.now(timezone.utc).strftime('%Y-%m-%dT%H:%M:%SZ')
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
class CommandRequest(BaseModel):
|
|
54
|
+
"""Command request data for NATS messages."""
|
|
55
|
+
name: str = Field(description="The command name (string) to send to the machine.")
|
|
56
|
+
params: Dict[str, Any] = Field(default_factory=dict, description="The parameters to send to the machine.")
|
|
57
|
+
step_number: int = Field(description="Execution step number (integer). Used to track the progress of a command.")
|
|
58
|
+
version: str = Field(default="1.0", description="Command version.")
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
class CommandResponse(BaseModel):
|
|
62
|
+
"""Result data in a command response."""
|
|
63
|
+
status: CommandResponseStatus = Field(description="Status of the command response.")
|
|
64
|
+
completed_at: str = Field(default_factory=_get_current_timestamp, description="ISO format timestamp (auto-set on creation)")
|
|
65
|
+
code: Optional[CommandResponseCode] = Field(default=None, description="Error code")
|
|
66
|
+
message: Optional[str] = Field(default=None, description="Error message (human-readable description)")
|
|
67
|
+
data: Optional[Dict[str, Any]] = Field(default=None, description="Optional output data from the command handler")
|
|
68
|
+
|
|
69
|
+
class MessageHeader(BaseModel):
|
|
70
|
+
"""Header for NATS messages."""
|
|
71
|
+
message_type: MessageType = Field(description="Type of message")
|
|
72
|
+
version: str = Field(default="1.0", description="Message version")
|
|
73
|
+
timestamp: str = Field(default_factory=_get_current_timestamp, description="ISO format timestamp (auto-set on creation)")
|
|
74
|
+
machine_id: str = Field(description="Machine ID")
|
|
75
|
+
run_id: Optional[str] = Field(default=None, description="Unique identifier (uuid) for the run/workflow")
|
|
76
|
+
|
|
77
|
+
class NATSMessage(BaseModel):
|
|
78
|
+
"""
|
|
79
|
+
Complete NATS message structure.
|
|
80
|
+
|
|
81
|
+
Structure:
|
|
82
|
+
- header: MessageHeader with message_type, version, timestamp, machine_id, run_id
|
|
83
|
+
- command: Optional CommandRequest (for command messages)
|
|
84
|
+
- response: Optional CommandResponse data (for response messages)
|
|
85
|
+
"""
|
|
86
|
+
header: MessageHeader = Field(description="Header of the NATS message.")
|
|
87
|
+
command: Optional[CommandRequest] = Field(default=None, description="Command request (for command messages)")
|
|
88
|
+
response: Optional[CommandResponse] = Field(default=None, description="Command response (for response messages)")
|
|
@@ -0,0 +1,310 @@
|
|
|
1
|
+
Metadata-Version: 2.3
|
|
2
|
+
Name: puda-comms
|
|
3
|
+
Version: 0.0.2
|
|
4
|
+
Summary: Communication library for the PUDA platform.
|
|
5
|
+
Author: zhao
|
|
6
|
+
Author-email: zhao <20024592+agentzhao@users.noreply.github.com>
|
|
7
|
+
Requires-Dist: nats-py>=2.12.0
|
|
8
|
+
Requires-Dist: puda-drivers
|
|
9
|
+
Requires-Dist: pydantic>=2.12.5
|
|
10
|
+
Requires-Python: >=3.14
|
|
11
|
+
Description-Content-Type: text/markdown
|
|
12
|
+
|
|
13
|
+
# Puda Comms
|
|
14
|
+
|
|
15
|
+
A Python module for communication between machines and command services via NATS messaging. Provides client-side services for sending commands, machine-side clients for receiving commands, and data models for structured message exchange.
|
|
16
|
+
|
|
17
|
+
## Overview
|
|
18
|
+
|
|
19
|
+
The `puda_comms` module enables asynchronous, reliable communication between command services and machines using NATS (NATS JetStream for guaranteed delivery). It handles:
|
|
20
|
+
|
|
21
|
+
- **Command execution**: Send commands to machines and receive responses
|
|
22
|
+
- **Message routing**: Queue commands (sequential execution) and immediate commands (control operations)
|
|
23
|
+
- **State management**: Thread-safe execution state tracking for cancellation and locking
|
|
24
|
+
- **Connection management**: Automatic NATS connection handling with async context managers
|
|
25
|
+
|
|
26
|
+
## Components
|
|
27
|
+
|
|
28
|
+
The module consists of four main components:
|
|
29
|
+
|
|
30
|
+
### 1. Models (`models.py`)
|
|
31
|
+
|
|
32
|
+
Data models for structured message exchange. All models use Pydantic for validation and serialization.
|
|
33
|
+
|
|
34
|
+
#### Enums
|
|
35
|
+
|
|
36
|
+
##### `CommandResponseStatus`
|
|
37
|
+
Status of a command response:
|
|
38
|
+
- `SUCCESS`: Command executed successfully
|
|
39
|
+
- `ERROR`: Command execution failed
|
|
40
|
+
|
|
41
|
+
##### `CommandResponseCode`
|
|
42
|
+
Error codes for command responses:
|
|
43
|
+
- `COMMAND_CANCELLED`: Command was cancelled before completion
|
|
44
|
+
- `JSON_DECODE_ERROR`: Failed to decode JSON payload
|
|
45
|
+
- `EXECUTION_ERROR`: General execution error
|
|
46
|
+
- `EXECUTION_LOCKED`: Execution is locked (another command is running)
|
|
47
|
+
- `UNKNOWN_COMMAND`: Command name not recognized
|
|
48
|
+
- `PAUSE_ERROR`: Error occurred while pausing execution
|
|
49
|
+
- `RESUME_ERROR`: Error occurred while resuming execution
|
|
50
|
+
- `NO_EXECUTION`: No execution found
|
|
51
|
+
- `RUN_ID_MISMATCH`: Run ID doesn't match current execution
|
|
52
|
+
- `CANCEL_ERROR`: Error occurred while cancelling execution
|
|
53
|
+
- `MACHINE_PAUSED`: Machine is currently paused
|
|
54
|
+
|
|
55
|
+
##### `MessageType`
|
|
56
|
+
Type of NATS message:
|
|
57
|
+
- `COMMAND`: Command message sent to machine
|
|
58
|
+
- `RESPONSE`: Response message from machine
|
|
59
|
+
- `LOG`: Log message
|
|
60
|
+
- `ALERT`: Alert message
|
|
61
|
+
- `MEDIA`: Media message
|
|
62
|
+
|
|
63
|
+
##### `ImmediateCommand`
|
|
64
|
+
Command names for immediate/control commands:
|
|
65
|
+
- `PAUSE`: Pause the current execution
|
|
66
|
+
- `RESUME`: Resume a paused execution
|
|
67
|
+
- `CANCEL`: Cancel the current execution
|
|
68
|
+
|
|
69
|
+
#### Data Models
|
|
70
|
+
|
|
71
|
+
##### `CommandRequest`
|
|
72
|
+
Represents a command to be sent to a machine.
|
|
73
|
+
|
|
74
|
+
**Fields:**
|
|
75
|
+
- `name` (str): The command name to execute
|
|
76
|
+
- `params` (Dict[str, Any]): Command parameters (default: empty dict)
|
|
77
|
+
- `step_number` (int): Execution step number for tracking progress
|
|
78
|
+
- `version` (str): Command version (default: "1.0")
|
|
79
|
+
|
|
80
|
+
**Example:**
|
|
81
|
+
```python
|
|
82
|
+
command = CommandRequest(
|
|
83
|
+
name="attach_tip",
|
|
84
|
+
params={"slot": "A3", "well": "G8"},
|
|
85
|
+
step_number=2,
|
|
86
|
+
version="1.0"
|
|
87
|
+
)
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
##### `CommandResponse`
|
|
91
|
+
Represents the result of a command execution.
|
|
92
|
+
|
|
93
|
+
**Fields:**
|
|
94
|
+
- `status` (CommandResponseStatus): Status of the command response (SUCCESS or ERROR)
|
|
95
|
+
- `completed_at` (str): ISO 8601 UTC timestamp (auto-generated)
|
|
96
|
+
- `code` (Optional[str]): Error code if status is ERROR
|
|
97
|
+
- `message` (Optional[str]): Human-readable error message
|
|
98
|
+
|
|
99
|
+
**Example:**
|
|
100
|
+
```python
|
|
101
|
+
response = CommandResponse(
|
|
102
|
+
status=CommandResponseStatus.SUCCESS,
|
|
103
|
+
completed_at="2026-01-20T02:00:46Z"
|
|
104
|
+
)
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
**Error Example:**
|
|
108
|
+
```python
|
|
109
|
+
error_response = CommandResponse(
|
|
110
|
+
status=CommandResponseStatus.ERROR,
|
|
111
|
+
code="EXECUTION_ERROR",
|
|
112
|
+
message="Failed to attach tip: slot A3 not found",
|
|
113
|
+
completed_at="2026-01-20T02:00:46Z"
|
|
114
|
+
)
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
##### `MessageHeader`
|
|
118
|
+
Header metadata for NATS messages.
|
|
119
|
+
|
|
120
|
+
**Fields:**
|
|
121
|
+
- `message_type` (MessageType): Type of message (COMMAND, RESPONSE, LOG, etc.)
|
|
122
|
+
- `version` (str): Message version (default: "1.0")
|
|
123
|
+
- `timestamp` (str): ISO 8601 UTC timestamp (auto-generated)
|
|
124
|
+
- `machine_id` (str): Identifier for the target machine
|
|
125
|
+
- `run_id` (Optional[str]): Unique identifier (UUID) for the run/workflow
|
|
126
|
+
|
|
127
|
+
**Example:**
|
|
128
|
+
```python
|
|
129
|
+
header = MessageHeader(
|
|
130
|
+
message_type=MessageType.RESPONSE,
|
|
131
|
+
version="1.0",
|
|
132
|
+
timestamp="2026-01-20T02:00:46Z",
|
|
133
|
+
machine_id="first",
|
|
134
|
+
run_id="092073e6-13d0-4756-8d99-eff1612a5a72"
|
|
135
|
+
)
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
##### `NATSMessage`
|
|
139
|
+
Complete NATS message structure combining header with optional command or response data.
|
|
140
|
+
|
|
141
|
+
**Fields:**
|
|
142
|
+
- `header` (MessageHeader): Message header (required)
|
|
143
|
+
- `command` (Optional[CommandRequest]): Command request (for command messages)
|
|
144
|
+
- `response` (Optional[CommandResponse]): Command response (for response messages)
|
|
145
|
+
|
|
146
|
+
**Structure:**
|
|
147
|
+
- For command messages: include `header` with `message_type=COMMAND` and `command` field
|
|
148
|
+
- For response messages: include `header` with `message_type=RESPONSE` and `response` field
|
|
149
|
+
|
|
150
|
+
**Complete Message Example:**
|
|
151
|
+
```json
|
|
152
|
+
{
|
|
153
|
+
"header": {
|
|
154
|
+
"message_type": "response",
|
|
155
|
+
"version": "1.0",
|
|
156
|
+
"timestamp": "2026-01-20T02:00:46Z",
|
|
157
|
+
"machine_id": "first",
|
|
158
|
+
"run_id": "092073e6-13d0-4756-8d99-eff1612a5a72"
|
|
159
|
+
},
|
|
160
|
+
"command": {
|
|
161
|
+
"name": "attach_tip",
|
|
162
|
+
"params": {
|
|
163
|
+
"slot": "A3",
|
|
164
|
+
"well": "G8"
|
|
165
|
+
},
|
|
166
|
+
"step_number": 2,
|
|
167
|
+
"version": "1.0"
|
|
168
|
+
},
|
|
169
|
+
"response": {
|
|
170
|
+
"status": "success",
|
|
171
|
+
"completed_at": "2026-01-20T02:00:46Z",
|
|
172
|
+
"code": null,
|
|
173
|
+
"message": null
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
```
|
|
177
|
+
|
|
178
|
+
### 2. CommandService (`command_service.py`)
|
|
179
|
+
|
|
180
|
+
Client-side service for sending commands to machines via NATS. Handles:
|
|
181
|
+
- Connecting to NATS servers
|
|
182
|
+
- Sending commands to machines (queue or immediate)
|
|
183
|
+
- Waiting for and handling responses
|
|
184
|
+
- Managing command lifecycle (run_id, step_number, etc.)
|
|
185
|
+
- Automatic connection cleanup via async context manager
|
|
186
|
+
|
|
187
|
+
See [Sending Commands](#sending-commands) section for usage examples.
|
|
188
|
+
|
|
189
|
+
### 3. MachineClient (`machine_client.py`)
|
|
190
|
+
|
|
191
|
+
Basic default NATS client for generic machines. Handles commands, telemetry, and events following the `puda.{machine_id}.{category}.{sub_category}` pattern. Provides:
|
|
192
|
+
- Subscribing to command streams (queue and immediate) via JetStream with exactly-once delivery
|
|
193
|
+
- Processing incoming commands and sending command responses
|
|
194
|
+
- Publishing telemetry (core NATS, no JetStream)
|
|
195
|
+
- Publishing events (core NATS, fire-and-forget)
|
|
196
|
+
- Connection management and reconnection handling
|
|
197
|
+
|
|
198
|
+
**Note:** This is a generic client. Machine-specific methods should be implemented in the machine-edge client.
|
|
199
|
+
|
|
200
|
+
### 4. ExecutionState (`execution_state.py`)
|
|
201
|
+
|
|
202
|
+
Thread-safe state management for command execution. Provides:
|
|
203
|
+
- Execution lock to prevent concurrent commands
|
|
204
|
+
- Current task tracking for cancellation
|
|
205
|
+
- Run ID matching for cancel operations
|
|
206
|
+
- Thread-safe access to execution state
|
|
207
|
+
|
|
208
|
+
## Sending Commands
|
|
209
|
+
|
|
210
|
+
The `CommandService` provides a high-level interface for sending commands to machines via NATS. See [`tests/commands.py`](tests/commands.py) and [`tests/batch_commands.py`](tests/batch_commands.py) for complete examples.
|
|
211
|
+
|
|
212
|
+
### Recommended Usage: Async Context Manager
|
|
213
|
+
|
|
214
|
+
The recommended way to use `CommandService` is with an async context manager, which automatically handles connection and disconnection. See [`tests/commands.py`](tests/commands.py) for complete examples.
|
|
215
|
+
|
|
216
|
+
### Command Types
|
|
217
|
+
|
|
218
|
+
#### Queue Commands
|
|
219
|
+
|
|
220
|
+
Queue commands are regular commands that are executed in sequence. Use `send_queue_command()` for machine-specific operations.
|
|
221
|
+
|
|
222
|
+
**Note:** Available commands depend on the machine you are controlling. Different machines support different command sets. See [`puda_drivers`](../drivers/README.md) for information about available commands for each machine type (e.g., `first` machine supports commands like `load_deck`, `attach_tip`, `aspirate_from`, `dispense_to`, `drop_tip`, etc.).
|
|
223
|
+
|
|
224
|
+
Both `send_queue_command()`, `send_queue_commands()`, and `send_immediate_command()` accept an optional `timeout` parameter (default: 120 seconds):
|
|
225
|
+
|
|
226
|
+
```python
|
|
227
|
+
# Single command
|
|
228
|
+
reply = await service.send_queue_command(
|
|
229
|
+
request=request,
|
|
230
|
+
machine_id="first",
|
|
231
|
+
run_id=run_id,
|
|
232
|
+
timeout=60 # Wait up to 60 seconds
|
|
233
|
+
)
|
|
234
|
+
|
|
235
|
+
# Multiple commands (timeout applies to each command)
|
|
236
|
+
reply = await service.send_queue_commands(
|
|
237
|
+
requests=commands,
|
|
238
|
+
machine_id="first",
|
|
239
|
+
run_id=run_id,
|
|
240
|
+
timeout=60 # Wait up to 60 seconds per command
|
|
241
|
+
)
|
|
242
|
+
```
|
|
243
|
+
|
|
244
|
+
**Examples:**
|
|
245
|
+
|
|
246
|
+
See [`tests/commands.py`](tests/commands.py) for complete examples.
|
|
247
|
+
|
|
248
|
+
#### Immediate Commands
|
|
249
|
+
|
|
250
|
+
Immediate commands are control commands that interrupt or modify execution. Use `send_immediate_command()` for:
|
|
251
|
+
- `pause`: Pause the current execution
|
|
252
|
+
- `resume`: Resume a paused execution
|
|
253
|
+
- `cancel`: Cancel the current execution
|
|
254
|
+
|
|
255
|
+
**Examples:**
|
|
256
|
+
|
|
257
|
+
See [`tests/commands.py`](tests/commands.py) for complete examples.
|
|
258
|
+
|
|
259
|
+
|
|
260
|
+
|
|
261
|
+
### Sending Command Sequences
|
|
262
|
+
|
|
263
|
+
You can send multiple commands in sequence using `send_queue_commands()`, which sends commands one by one and waits for each response before sending the next. If any command fails or times out, it stops immediately and returns the error response.
|
|
264
|
+
|
|
265
|
+
**Loading Commands from JSON (Recommended for LLM-generated commands):**
|
|
266
|
+
|
|
267
|
+
When generating commands from an LLM or loading from external sources, you can store commands in a JSON file and load them. See [`tests/batch_commands.py`](tests/batch_commands.py) for a complete example.
|
|
268
|
+
|
|
269
|
+
### Error Handling
|
|
270
|
+
|
|
271
|
+
Always check the response status and handle errors appropriately:
|
|
272
|
+
|
|
273
|
+
```python
|
|
274
|
+
reply: NATSMessage = await service.send_queue_command(
|
|
275
|
+
request=request,
|
|
276
|
+
machine_id="first",
|
|
277
|
+
run_id=run_id
|
|
278
|
+
)
|
|
279
|
+
|
|
280
|
+
if reply is None:
|
|
281
|
+
# Command timed out or failed to send
|
|
282
|
+
logger.error("Command failed or timed out")
|
|
283
|
+
elif reply.response is not None and reply.response.status == CommandResponseStatus.SUCCESS:
|
|
284
|
+
# Command succeeded
|
|
285
|
+
logger.info("Command completed successfully")
|
|
286
|
+
else:
|
|
287
|
+
# Command failed with error
|
|
288
|
+
logger.error("Command failed with code: %s, message: %s",
|
|
289
|
+
reply.response.code if reply.response else None,
|
|
290
|
+
reply.response.message if reply.response else None)
|
|
291
|
+
```
|
|
292
|
+
|
|
293
|
+
### Configuration
|
|
294
|
+
|
|
295
|
+
The `CommandService` reads NATS server URLs from the `NATS_SERVERS` environment variable, or defaults to:
|
|
296
|
+
```
|
|
297
|
+
nats://192.168.50.201:4222,nats://192.168.50.201:4223,nats://192.168.50.201:4224
|
|
298
|
+
```
|
|
299
|
+
|
|
300
|
+
You can also specify servers explicitly:
|
|
301
|
+
```python
|
|
302
|
+
service = CommandService(servers=["nats://localhost:4222"])
|
|
303
|
+
```
|
|
304
|
+
## Validation
|
|
305
|
+
|
|
306
|
+
All models use Pydantic for validation, ensuring:
|
|
307
|
+
- Type checking for all fields
|
|
308
|
+
- Required fields are present
|
|
309
|
+
- Default values are applied correctly
|
|
310
|
+
- JSON serialization/deserialization works correctly
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
puda_comms/__init__.py,sha256=lntvVFJJez_rv5lZy5mYj4_43B9Y3NRNzxWfBuSAQ1M,194
|
|
2
|
+
puda_comms/command_service.py,sha256=B4fKiQNF0slvGS1fXVoh5UZax_-xk4IS-KT96teSRfg,23272
|
|
3
|
+
puda_comms/execution_state.py,sha256=aTaejCnJgg1y_FP-ymIC1GQzqC81FIWo0RZ18XzAQnA,2881
|
|
4
|
+
puda_comms/machine_client.py,sha256=F2i0BYBuOLjKAnfZAblNrb3Lzs0yhEO1d4XA-k_dkIU,33039
|
|
5
|
+
puda_comms/models.py,sha256=cVH5uKzyLmjzPeBcm3RIJMTkoynmxqe_P26GtZwlIN8,3500
|
|
6
|
+
puda_comms-0.0.2.dist-info/WHEEL,sha256=ZyFSCYkV2BrxH6-HRVRg3R9Fo7MALzer9KiPYqNxSbo,79
|
|
7
|
+
puda_comms-0.0.2.dist-info/METADATA,sha256=jHHcSSmdWOykobTsieX2bqDeRtqSaqdUd-xZeeWxJZ8,10585
|
|
8
|
+
puda_comms-0.0.2.dist-info/RECORD,,
|