-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathadapter_copy.py
More file actions
101 lines (87 loc) · 3.57 KB
/
adapter_copy.py
File metadata and controls
101 lines (87 loc) · 3.57 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
#!/usr/bin/env python3
"""
Simple NANDA Adapter - Clean Agent-to-Agent Communication
Simple, clean adapter focused on A2A communication without complexity.
8-10 lines to deploy an agent.
"""
import os
import requests
from typing import Optional, Callable
from python_a2a import run_server
from .agent_bridge import SimpleAgentBridge
class NANDA:
"""Simple NANDA class for clean agent deployment"""
def __init__(self,
agent_id: str,
agent_logic: Callable[[str, str], str],
port: int = 6000,
registry_url: Optional[str] = None,
public_url: Optional[str] = None,
host: str = "0.0.0.0",
enable_telemetry: bool = True):
"""
Create a simple NANDA agent
Args:
agent_id: Unique agent identifier
agent_logic: Function that takes (message: str, conversation_id: str) -> response: str
port: Port to run on
registry_url: Optional registry URL for agent discovery
public_url: Public URL for agent registration (e.g., https://yourdomain.com:6000)
host: Host to bind to
enable_telemetry: Enable telemetry logging (optional)
"""
self.agent_id = agent_id
self.agent_logic = agent_logic
self.port = port
self.registry_url = registry_url
self.public_url = public_url
self.host = host
self.enable_telemetry = enable_telemetry
# Initialize telemetry if enabled
self.telemetry = None
if enable_telemetry:
try:
from ..telemetry.telemetry_system import TelemetrySystem
self.telemetry = TelemetrySystem(agent_id)
print(f"📊 Telemetry enabled for {agent_id}")
except ImportError:
print(f"⚠️ Telemetry requested but module not available")
# Create the bridge with optional features
self.bridge = SimpleAgentBridge(
agent_id=agent_id,
agent_logic=agent_logic,
registry_url=registry_url,
telemetry=self.telemetry
)
print(f"🤖 NANDA Agent '{agent_id}' created")
if registry_url:
print(f"🌐 Registry: {registry_url}")
if public_url:
print(f"🔗 Public URL: {public_url}")
def start(self, register: bool = True):
"""Start the agent server"""
# Register with registry if provided
if register and self.registry_url and self.public_url:
self._register()
print(f"🚀 Starting agent '{self.agent_id}' on {self.host}:{self.port}")
# Start the A2A server
run_server(self.bridge, host=self.host, port=self.port)
def _register(self):
"""Register agent with registry"""
try:
data = {
"agent_id": self.agent_id,
"agent_url": self.public_url
}
response = requests.post(f"{self.registry_url}/register", json=data, timeout=10)
if response.status_code == 200:
print(f"✅ Agent '{self.agent_id}' registered successfully")
else:
print(f"⚠️ Failed to register agent: HTTP {response.status_code}")
except Exception as e:
print(f"⚠️ Registration error: {e}")
def stop(self):
"""Stop the agent and cleanup telemetry"""
if self.telemetry:
self.telemetry.stop()
print(f"🛑 Stopping agent '{self.agent_id}'")