forked from themanojdesai/python-a2a
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathagent.py
More file actions
401 lines (327 loc) · 12.8 KB
/
Copy pathagent.py
File metadata and controls
401 lines (327 loc) · 12.8 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
"""
Agent models for representing and connecting to A2A agents.
"""
import json
import uuid
from datetime import datetime
from enum import Enum, auto
from typing import Dict, List, Optional, Set, Any, Union, Tuple
from python_a2a import (
AgentCard, AgentSkill, A2AClient,
Message, TextContent, MessageRole
)
class AgentSource(Enum):
"""Source types for agents."""
LOCAL = auto() # Local agent (part of this system)
REMOTE = auto() # Remote A2A agent
LLM = auto() # Language model powered agent
CUSTOM = auto() # Custom agent implementation
class AgentStatus(Enum):
"""Status of an agent connection."""
CONNECTED = auto()
DISCONNECTED = auto()
ERROR = auto()
class AgentDefinition:
"""
Definition of an agent to be used in workflows.
This represents a specific agent instance with its configuration,
capabilities, and connection details.
"""
def __init__(
self,
id: Optional[str] = None,
name: str = "Unnamed Agent",
description: str = "",
url: str = "",
agent_source: AgentSource = AgentSource.REMOTE,
agent_type: str = "a2a",
config: Optional[Dict[str, Any]] = None,
created_at: Optional[datetime] = None,
updated_at: Optional[datetime] = None,
metadata: Optional[Dict[str, Any]] = None
):
"""
Initialize an agent definition.
Args:
id: Unique identifier (generated if not provided)
name: Human-readable name
description: Description of the agent's purpose
url: URL where the agent is accessible
agent_source: Source type of the agent
agent_type: Type of the agent (a2a, langchain, mcp, etc.)
config: Configuration parameters for the agent
created_at: Creation timestamp
updated_at: Last update timestamp
metadata: Additional metadata
"""
self.id = id or str(uuid.uuid4())
self.name = name
self.description = description
self.url = url
self.agent_source = agent_source
self.agent_type = agent_type
self.config = config or {}
self.created_at = created_at or datetime.now()
self.updated_at = updated_at or datetime.now()
self.metadata = metadata or {}
self.skills: List[AgentSkill] = []
self.status = AgentStatus.DISCONNECTED
self.agent_card: Optional[AgentCard] = None
self.client: Optional[A2AClient] = None
self.error_message: Optional[str] = None
def connect(self) -> bool:
"""
Connect to the agent and fetch its capabilities.
Returns:
True if connection was successful, False otherwise
"""
try:
# Initialize client
self.client = A2AClient(self.url)
# Fetch agent card
self.agent_card = self.client.get_agent_card()
# Update properties from agent card
if self.agent_card:
self.name = self.agent_card.name or self.name
self.description = self.agent_card.description or self.description
if hasattr(self.agent_card, 'skills'):
self.skills = self.agent_card.skills
self.status = AgentStatus.CONNECTED
self.error_message = None
return True
except Exception as e:
self.status = AgentStatus.ERROR
self.error_message = str(e)
return False
def disconnect(self) -> None:
"""Disconnect from the agent."""
self.client = None
self.status = AgentStatus.DISCONNECTED
def send_message(self, text: str) -> Optional[str]:
"""
Send a message to the agent and return the response.
Args:
text: Message text to send
Returns:
Response text if successful, None on error
"""
if not self.client or self.status != AgentStatus.CONNECTED:
self.error_message = "Agent not connected"
return None
try:
response = self.client.ask(text)
return response
except Exception as e:
self.error_message = str(e)
return None
def to_dict(self) -> Dict[str, Any]:
"""
Convert the agent definition to a dictionary.
Returns:
Dictionary representation of the agent definition
"""
return {
"id": self.id,
"name": self.name,
"description": self.description,
"url": self.url,
"agent_source": self.agent_source.name,
"agent_type": self.agent_type,
"config": self.config,
"created_at": self.created_at.isoformat(),
"updated_at": self.updated_at.isoformat(),
"metadata": self.metadata,
"skills": [skill.to_dict() for skill in self.skills],
"status": self.status.name
}
@classmethod
def from_dict(cls, data: Dict[str, Any]) -> 'AgentDefinition':
"""
Create an agent definition from a dictionary.
Args:
data: Dictionary representation of the agent definition
Returns:
AgentDefinition instance
"""
# Parse timestamps
created_at = data.get("created_at")
if isinstance(created_at, str):
created_at = datetime.fromisoformat(created_at)
updated_at = data.get("updated_at")
if isinstance(updated_at, str):
updated_at = datetime.fromisoformat(updated_at)
# Create agent definition
agent_def = cls(
id=data.get("id"),
name=data.get("name", "Unnamed Agent"),
description=data.get("description", ""),
url=data.get("url", ""),
agent_source=AgentSource[data.get("agent_source", "REMOTE")],
agent_type=data.get("agent_type", "a2a"),
config=data.get("config", {}),
created_at=created_at,
updated_at=updated_at,
metadata=data.get("metadata", {})
)
# Add skills
for skill_data in data.get("skills", []):
if isinstance(skill_data, dict):
skill = AgentSkill(
name=skill_data.get("name", ""),
description=skill_data.get("description", ""),
tags=skill_data.get("tags", []),
examples=skill_data.get("examples", [])
)
agent_def.skills.append(skill)
# Set status
status_str = data.get("status", "DISCONNECTED")
agent_def.status = AgentStatus[status_str]
return agent_def
class AgentRegistry:
"""
Registry for managing agent definitions.
The registry maintains a collection of available agents that can be
used in workflows, providing lookup, registration, and connection
management.
"""
def __init__(self):
"""Initialize the agent registry."""
self.agents: Dict[str, AgentDefinition] = {}
def register(self, agent: AgentDefinition) -> None:
"""
Register an agent in the registry.
Args:
agent: Agent definition to register
"""
self.agents[agent.id] = agent
def unregister(self, agent_id: str) -> bool:
"""
Remove an agent from the registry.
Args:
agent_id: ID of the agent to remove
Returns:
True if removed, False if not found
"""
if agent_id in self.agents:
# Disconnect if connected
agent = self.agents[agent_id]
if agent.status == AgentStatus.CONNECTED:
agent.disconnect()
# Remove from registry
del self.agents[agent_id]
return True
return False
def get(self, agent_id: str) -> Optional[AgentDefinition]:
"""
Get an agent by ID.
Args:
agent_id: ID of the agent to get
Returns:
AgentDefinition if found, None otherwise
"""
return self.agents.get(agent_id)
def list_agents(self) -> List[AgentDefinition]:
"""
Get all registered agents.
Returns:
List of all agent definitions
"""
return list(self.agents.values())
def connect_all(self) -> Tuple[int, int]:
"""
Connect to all agents in the registry.
Returns:
Tuple of (successful, failed) connection counts
"""
successful = 0
failed = 0
for agent in self.agents.values():
if agent.connect():
successful += 1
else:
failed += 1
return successful, failed
def disconnect_all(self) -> None:
"""Disconnect from all agents in the registry."""
for agent in self.agents.values():
agent.disconnect()
def discover_agents(self, base_url: str, port_range: Tuple[int, int]) -> List[AgentDefinition]:
"""
Discover agents running on local ports.
Args:
base_url: Base URL for discovery (e.g., "http://localhost")
port_range: Range of ports to scan (start, end)
Returns:
List of discovered agent definitions
"""
import requests
from concurrent.futures import ThreadPoolExecutor
discovered: List[AgentDefinition] = []
start_port, end_port = port_range
def check_port(port: int) -> Optional[AgentDefinition]:
"""Check a single port for an A2A agent."""
url = f"{base_url}:{port}"
agent_url = f"{url}/agent.json"
try:
response = requests.get(agent_url, timeout=0.5)
if response.status_code == 200:
data = response.json()
if isinstance(data, dict) and "name" in data and "description" in data:
# Create agent definition
agent = AgentDefinition(
name=data.get("name", f"Agent on port {port}"),
description=data.get("description", ""),
url=url,
agent_source=AgentSource.REMOTE,
agent_type="a2a"
)
# Add skills if available
if "skills" in data and isinstance(data["skills"], list):
for skill_data in data["skills"]:
if isinstance(skill_data, dict):
skill = AgentSkill(
name=skill_data.get("name", ""),
description=skill_data.get("description", ""),
tags=skill_data.get("tags", []),
examples=skill_data.get("examples", [])
)
agent.skills.append(skill)
return agent
except:
pass
return None
# Use ThreadPoolExecutor to scan ports in parallel
with ThreadPoolExecutor(max_workers=10) as executor:
results = list(executor.map(
check_port,
range(start_port, end_port + 1)
))
# Filter out None results
discovered = [agent for agent in results if agent is not None]
# Add discovered agents to registry
for agent in discovered:
self.register(agent)
return discovered
def to_dict(self) -> Dict[str, Any]:
"""
Convert the registry to a dictionary.
Returns:
Dictionary representation of the registry
"""
return {
"agents": [agent.to_dict() for agent in self.agents.values()]
}
@classmethod
def from_dict(cls, data: Dict[str, Any]) -> 'AgentRegistry':
"""
Create a registry from a dictionary.
Args:
data: Dictionary representation of the registry
Returns:
AgentRegistry instance
"""
registry = cls()
for agent_data in data.get("agents", []):
agent = AgentDefinition.from_dict(agent_data)
registry.register(agent)
return registry