-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.py
More file actions
609 lines (510 loc) · 19.6 KB
/
example.py
File metadata and controls
609 lines (510 loc) · 19.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
#!/usr/bin/env python3
"""
Aether Client Example
This example demonstrates the full feature set of the Aether Python client,
including:
- Agent, Task, User, Orchestrator, WorkflowEngine, and MetricsBridge clients
- Message sending with different message types
- KV operations with scope support (global, workspace, user, user-workspace)
- Task creation with assignment modes (self-assign, targeted, pool)
- Task assignment callbacks
- Config snapshot handling
- Context manager support (auto-cleanup on exit)
- Custom exception handling
- TLS/mTLS configuration
- MultiprocessOrchestrator for subprocess management
"""
import json
import time
from typing import Optional
from scitrera_aether_client import (
# Client classes
AgentClient,
TaskClient,
UserClient,
OrchestratorClient,
WorkflowEngineClient,
MetricsBridgeClient,
# Orchestrator classes
MultiprocessOrchestrator,
# Message type constants
CHAT,
CONTROL,
EVENT,
METRIC,
# Task assignment mode constants
SELF_ASSIGN,
TARGETED,
# Custom exceptions
AetherError,
ConnectionError,
AuthenticationError,
DuplicateIdentityError,
ReconnectionError,
TimeoutError,
# Type definitions (for documentation)
ConnectionConfig,
TLSConfig,
# Metric builder
new_metric,
)
def demo_agent_client():
"""Demonstrate the AgentClient with all features."""
print("\n" + "=" * 60)
print("Agent Client Demo")
print("=" * 60)
client = AgentClient(
workspace="default",
implementation="python-demo",
specifier="agent-01"
)
# Set up callbacks
def on_message(msg):
print(f"[Agent] Message from {msg.source_topic}: {msg.payload.decode()}")
def on_config(config):
print(f"[Agent] Config snapshot received:")
print(f" Workspace KV: {dict(config.kv)}")
print(f" Global KV: {dict(config.global_kv)}")
def on_task_assignment(assignment):
print(f"[Agent] Task assigned:")
print(f" Task ID: {assignment.task_id}")
print(f" Task Type: {assignment.task_type}")
print(f" Assigned To: {assignment.assigned_to}")
print(f" Metadata: {dict(assignment.metadata)}")
def on_kv_response(kv):
print(f"[Agent] KV Response: success={kv.success}")
if kv.value:
print(f" Value: {kv.value}")
if kv.keys:
print(f" Keys: {list(kv.keys)}")
def on_error(error):
print(f"[Agent] Error: {error.code} - {error.message}")
client.on_message = on_message
client.on_config = on_config
client.on_task_assignment = on_task_assignment
client.on_kv_response = on_kv_response
client.on_error = on_error
try:
print("Connecting to gateway...")
client.connect("localhost:50051")
print("Connected!")
time.sleep(1)
# Demo: Send a message to ourselves
print("\n--- Sending message to self ---")
client.send_message_to_agent(
workspace="default",
implementation="python-demo",
specifier="agent-01",
payload=b"Hello from Python agent!"
)
time.sleep(0.5)
# Demo: KV operations with different scopes
print("\n--- KV Operations ---")
# Global scope
print("Storing value in global scope...")
client.kv_put(
key="demo/setting",
value=b"global-value",
scope="global"
)
time.sleep(0.3)
# Workspace scope
print("Storing value in workspace scope...")
client.kv_put(
key="demo/workspace-setting",
value=b"workspace-value",
scope="workspace",
workspace="default"
)
time.sleep(0.3)
# Retrieve a value
print("Getting value from global scope...")
client.kv_get(key="demo/setting", scope="global")
time.sleep(0.5)
# Demo: Create a self-assigned task
print("\n--- Task Creation (Self-Assign) ---")
client.create_task(
task_type="data-processing",
workspace="default",
assignment_mode=SELF_ASSIGN,
metadata={"source": "python-demo", "priority": "normal"}
)
time.sleep(0.5)
# Demo: Send events and metrics
print("\n--- Events and Metrics ---")
event_payload = json.dumps({
"event_type": "agent_started",
"agent_id": "python-demo.agent-01",
"timestamp": time.time()
}).encode()
client.send_event(event_payload)
print("Sent event to workflow engine")
metric = new_metric().trace("demo-trace-1").add("messages_processed", "counter", 42.0).tag("agent", "python-demo").build()
client.send_metric(metric)
print("Sent metric to metrics bridge")
time.sleep(1)
print("\nWaiting for messages (Ctrl+C to exit)...")
while True:
time.sleep(1)
except AuthenticationError as e:
print(f"[Agent] Authentication failed: {e}")
except DuplicateIdentityError as e:
print(f"[Agent] Identity already in use: {e}")
except ReconnectionError as e:
print(f"[Agent] Could not reconnect after {e.attempts} attempts")
except ConnectionError as e:
print(f"[Agent] Connection failed: {e}")
except AetherError as e:
print(f"[Agent] Aether error: {e}")
except KeyboardInterrupt:
pass
finally:
client.close()
print("Disconnected.")
def demo_agent_context_manager():
"""Demonstrate using AgentClient as a context manager (new feature)."""
print("\n" + "=" * 60)
print("Agent Client (Context Manager) Demo")
print("=" * 60)
# Using context manager for automatic cleanup
with AgentClient(
workspace="default",
implementation="python-demo",
specifier="context-agent"
) as client:
client.on_message = lambda msg: print(f"[Agent] {msg.payload.decode()}")
try:
client.connect("localhost:50051")
print("Connected via context manager!")
client.send_message_to_agent(
workspace="default",
implementation="python-demo",
specifier="context-agent",
payload=b"Hello via context manager!"
)
time.sleep(1)
print("Exiting context manager (auto-close)...")
except AetherError as e:
print(f"[Agent] Error: {e}")
print("Connection closed automatically.")
def demo_tls_configuration():
"""Demonstrate TLS/mTLS configuration (new feature)."""
print("\n" + "=" * 60)
print("TLS Configuration Demo")
print("=" * 60)
# Example 1: Basic TLS (server verification only)
print("\n--- Basic TLS Configuration ---")
print("Creating client with TLS enabled...")
# Note: These paths are examples - replace with actual certificate paths
_tls_client = AgentClient(
workspace="default",
implementation="python-demo",
specifier="tls-agent",
# Enable TLS
tls_enabled=True,
# Root CA certificate (verify server identity)
tls_root_cert_path="/path/to/ca.crt", # or use tls_root_cert=bytes
)
print(" TLS client created (server verification)")
# Example 2: Mutual TLS (mTLS) with client certificates
print("\n--- mTLS Configuration ---")
print("Creating client with mTLS enabled...")
_mtls_client = AgentClient(
workspace="default",
implementation="python-demo",
specifier="mtls-agent",
# Enable TLS
tls_enabled=True,
# Root CA certificate
tls_root_cert_path="/path/to/ca.crt",
# Client certificate and key for mTLS
tls_client_cert_path="/path/to/client.crt",
tls_client_key_path="/path/to/client.key",
)
print(" mTLS client created (mutual authentication)")
# Example 3: Using in-memory certificates
print("\n--- In-Memory Certificates ---")
print("Certificates can also be provided as bytes:")
print(" tls_root_cert=b'-----BEGIN CERTIFICATE-----...'")
print(" tls_client_cert=b'-----BEGIN CERTIFICATE-----...'")
print(" tls_client_key=b'-----BEGIN PRIVATE KEY-----...'")
print("\nNote: This demo doesn't actually connect (no real certs).")
def demo_orchestrator_client():
"""Demonstrate the OrchestratorClient."""
print("\n" + "=" * 60)
print("Orchestrator Client Demo")
print("=" * 60)
client = OrchestratorClient(
implementation="demo-orchestrator",
supported_profiles=["docker", "kubernetes"]
)
def on_message(msg):
print(f"[Orchestrator] Received from {msg.source_topic}:")
try:
data = json.loads(msg.payload.decode())
print(f" Startup request: {data}")
except Exception:
print(f" Raw: {msg.payload.decode()}")
def on_task_assignment(assignment):
print(f"[Orchestrator] Task assignment received:")
print(f" Task ID: {assignment.task_id}")
print(f" Task Type: {assignment.task_type}")
# Orchestrator would now launch the required compute resources
client.on_message = on_message
client.on_task_assignment = on_task_assignment
try:
print("Connecting as orchestrator...")
client.connect("localhost:50051")
print("Orchestrator connected! Listening for startup requests...")
while True:
time.sleep(1)
except AetherError as e:
print(f"[Orchestrator] Error: {e}")
except KeyboardInterrupt:
pass
finally:
client.close()
print("Orchestrator disconnected.")
def demo_multiprocess_orchestrator():
"""Demonstrate the MultiprocessOrchestrator (new feature)."""
print("\n" + "=" * 60)
print("MultiprocessOrchestrator Demo")
print("=" * 60)
class _DemoOrchestrator(MultiprocessOrchestrator):
"""Example orchestrator that spawns Python scripts."""
def get_implementation(self) -> str:
return "demo-multiprocess"
def get_supported_profiles(self) -> list:
return ["python-script", "python-module"]
def handle_assignment(self, assignment) -> None:
"""Handle task assignment by spawning a subprocess."""
task_type = assignment.task_type
metadata = dict(assignment.metadata)
print(f"[Orchestrator] Handling assignment: {task_type}")
if metadata.get("profile") == "python-script":
# Spawn a Python script
script_path = metadata.get("script", "agent.py")
self.spawn_subprocess(
script_path=script_path,
task_id=assignment.task_id,
args=["--task-id", assignment.task_id],
env={"AETHER_WORKSPACE": assignment.workspace}
)
elif metadata.get("profile") == "python-module":
# Run a Python module
module_name = metadata.get("module", "my_agent")
self.spawn_module(
module_name=module_name,
task_id=assignment.task_id,
args=["--task-id", assignment.task_id]
)
else:
print(f"[Orchestrator] Unknown profile: {metadata.get('profile')}")
def on_connect(self) -> None:
"""Called when connected to gateway."""
print("[Orchestrator] Connected and ready for assignments!")
def on_disconnect(self, reason: str) -> None:
"""Called when disconnected."""
print(f"[Orchestrator] Disconnected: {reason}")
print("\nMultiprocessOrchestrator features:")
print(" - Extends BaseOrchestrator for easy subprocess management")
print(" - spawn_subprocess() - Launch Python scripts")
print(" - spawn_module() - Run Python modules via 'python -m'")
print(" - Automatic process tracking and cleanup")
print(" - Output capture with dedicated reader threads")
print(" - Graceful termination (SIGTERM -> SIGKILL fallback)")
print("\nSee the _DemoOrchestrator class above for implementation example.")
def demo_workflow_engine():
"""Demonstrate the WorkflowEngineClient."""
print("\n" + "=" * 60)
print("Workflow Engine Client Demo")
print("=" * 60)
# Using context manager for automatic cleanup
with WorkflowEngineClient() as client:
def on_message(msg):
print(f"[WorkflowEngine] Event from {msg.source_topic}:")
try:
event = json.loads(msg.payload.decode())
print(f" Event type: {event.get('event_type')}")
print(f" Data: {event}")
except Exception:
print(f" Raw: {msg.payload.decode()}")
client.on_message = on_message
try:
print("Connecting as workflow engine...")
client.connect("localhost:50051")
print("Workflow engine connected! Listening for events...")
# Demo: Send a command to an agent
time.sleep(1)
print("\n--- Sending command to agent ---")
command = json.dumps({
"command": "start_processing",
"params": {"batch_size": 100}
}).encode()
client.send_command_to_agent(
workspace="default",
implementation="python-demo",
specifier="agent-01",
payload=command
)
print("Command sent!")
while True:
time.sleep(1)
except AetherError as e:
print(f"[WorkflowEngine] Error: {e}")
except KeyboardInterrupt:
pass
print("Workflow engine disconnected.")
def demo_metrics_bridge():
"""Demonstrate the MetricsBridgeClient."""
print("\n" + "=" * 60)
print("Metrics Bridge Client Demo")
print("=" * 60)
# Using context manager for automatic cleanup
with MetricsBridgeClient() as client:
def on_message(msg):
print(f"[MetricsBridge] Metric from {msg.source_topic}:")
try:
metric = json.loads(msg.payload.decode())
print(f" Name: {metric.get('metric_name')}")
print(f" Value: {metric.get('value')}")
print(f" Tags: {metric.get('tags')}")
except Exception:
print(f" Raw: {msg.payload.decode()}")
client.on_message = on_message
try:
print("Connecting as metrics bridge...")
client.connect("localhost:50051")
print("Metrics bridge connected! Listening for metrics...")
while True:
time.sleep(1)
except AetherError as e:
print(f"[MetricsBridge] Error: {e}")
except KeyboardInterrupt:
pass
print("Metrics bridge disconnected.")
def demo_targeted_task():
"""Demonstrate creating a targeted task (Phase 6 feature)."""
print("\n" + "=" * 60)
print("Targeted Task Creation Demo")
print("=" * 60)
# Using context manager for automatic cleanup
with AgentClient(
workspace="default",
implementation="task-creator",
specifier="01"
) as client:
client.on_message = lambda msg: print(f"[TaskCreator] Message: {msg.payload.decode()}")
try:
print("Connecting...")
client.connect("localhost:50051")
time.sleep(1)
# Create a targeted task that will be assigned to a specific agent
# If the target agent is offline, this will trigger orchestration
print("\n--- Creating targeted task ---")
client.create_task(
task_type="specialized-processing",
workspace="default",
assignment_mode=TARGETED,
target_agent_id="ag::default::worker::specialist-01",
launch_param_overrides={
"memory": "4G",
"gpu": "true"
},
metadata={
"priority": "high",
"deadline": "2024-12-31T23:59:59Z"
}
)
print("Targeted task created! If agent is offline, orchestrator will spin it up.")
time.sleep(2)
except AetherError as e:
print(f"[TaskCreator] Error: {e}")
print("Done.")
def demo_error_handling():
"""Demonstrate the custom exception hierarchy (new feature)."""
print("\n" + "=" * 60)
print("Custom Exception Handling Demo")
print("=" * 60)
print("\nAether provides a structured exception hierarchy:")
print(" AetherError (base)")
print(" ├── ConnectionError")
print(" │ ├── ConnectionClosedError")
print(" │ └── ReconnectionError")
print(" ├── AuthenticationError")
print(" ├── PermissionDeniedError")
print(" ├── DuplicateIdentityError")
print(" ├── TimeoutError")
print(" ├── InvalidArgumentError")
print(" ├── NotFoundError")
print(" ├── NotImplementedError")
print(" ├── MessageError")
print(" ├── KVOperationError")
print(" └── CheckpointError")
print("\nExample error handling pattern:")
print("""
from scitrera_aether_client import (
AgentClient,
AetherError,
ConnectionError,
AuthenticationError,
DuplicateIdentityError,
ReconnectionError,
)
client = AgentClient("ws", "impl", "spec")
try:
client.connect("localhost:50051")
# ... do work ...
except AuthenticationError as e:
print(f"Auth failed: {e}")
except DuplicateIdentityError as e:
print(f"Identity {e.identity} already connected")
except ReconnectionError as e:
print(f"Failed to reconnect after {e.attempts} attempts")
except ConnectionError as e:
print(f"Connection failed: {e}")
except AetherError as e:
print(f"Other Aether error: {e}")
finally:
client.close()
""")
def main():
"""Main entry point - run the agent demo by default."""
import sys
demos = {
"agent": demo_agent_client,
"context": demo_agent_context_manager,
"tls": demo_tls_configuration,
"orchestrator": demo_orchestrator_client,
"multiprocess": demo_multiprocess_orchestrator,
"workflow": demo_workflow_engine,
"metrics": demo_metrics_bridge,
"targeted": demo_targeted_task,
"errors": demo_error_handling,
}
if len(sys.argv) > 1:
demo_name = sys.argv[1].lower()
if demo_name in demos:
demos[demo_name]()
else:
print(f"Unknown demo: {demo_name}")
print(f"Available demos: {', '.join(demos.keys())}")
sys.exit(1)
else:
print("Aether Client Demo")
print("==================")
print("\nUsage: python example.py [demo]")
print("\nAvailable demos:")
print(" agent - Agent client with messaging, KV, and task creation")
print(" context - Agent client using context manager (auto-cleanup)")
print(" tls - TLS/mTLS configuration options")
print(" orchestrator - Orchestrator client for managing agent lifecycle")
print(" multiprocess - MultiprocessOrchestrator for subprocess management")
print(" workflow - Workflow engine for processing events")
print(" metrics - Metrics bridge for collecting telemetry")
print(" targeted - Targeted task creation with orchestration")
print(" errors - Custom exception hierarchy demonstration")
print("\nRunning default 'agent' demo...\n")
demo_agent_client()
if __name__ == "__main__":
main()