forked from themanojdesai/python-a2a
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patha2a_client.py
More file actions
359 lines (305 loc) · 11.1 KB
/
Copy patha2a_client.py
File metadata and controls
359 lines (305 loc) · 11.1 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
"""
Enhanced A2A client with protocol support.
"""
import requests
import uuid
from typing import Dict, Any, Optional, List, Union
from ..models.agent import AgentCard, AgentSkill
from ..models.task import Task, TaskStatus, TaskState
from ..models.message import Message, MessageRole
from ..models.conversation import Conversation
from ..models.content import TextContent
from .base import BaseA2AClient
from ..exceptions import A2AConnectionError
class A2AClient(BaseA2AClient):
"""
Enhanced A2A client with protocol support
"""
def __init__(self, url, headers=None):
"""
Initialize a client for an A2A agent
Args:
url: The base URL of the agent
headers: Optional HTTP headers to include in requests
"""
self.url = url.rstrip("/")
self.headers = headers or {}
if "Content-Type" not in self.headers:
self.headers["Content-Type"] = "application/json"
# Try to fetch the agent card
try:
self.agent_card = self._fetch_agent_card()
except Exception as e:
print(f"Warning: Could not fetch agent card: {e}")
# Create a default agent card
self.agent_card = AgentCard(
name="Unknown Agent",
description="Agent card not available",
url=self.url,
version="unknown"
)
def _fetch_agent_card(self):
"""Fetch the agent card from the well-known URL"""
# Try standard A2A endpoint first
try:
card_url = f"{self.url}/agent.json"
response = requests.get(card_url, headers=self.headers)
response.raise_for_status()
card_data = response.json()
except Exception:
# Try alternate endpoint
card_url = f"{self.url}/a2a/agent.json"
response = requests.get(card_url, headers=self.headers)
response.raise_for_status()
card_data = response.json()
# Create AgentSkill objects from data
skills = []
for skill_data in card_data.get("skills", []):
skills.append(AgentSkill(
id=skill_data.get("id", str(uuid.uuid4())),
name=skill_data.get("name", "Unknown Skill"),
description=skill_data.get("description", ""),
tags=skill_data.get("tags", []),
examples=skill_data.get("examples", [])
))
# Create AgentCard object
return AgentCard(
name=card_data.get("name", "Unknown Agent"),
description=card_data.get("description", ""),
url=self.url,
version=card_data.get("version", "unknown"),
authentication=card_data.get("authentication"),
capabilities=card_data.get("capabilities", {}),
skills=skills,
provider=card_data.get("provider"),
documentation_url=card_data.get("documentationUrl")
)
def send_message(self, message: Message) -> Message:
"""
Send a message to the A2A agent (required for BaseA2AClient)
Args:
message: The message to send
Returns:
The agent's response
"""
# Create a task from the message
task = self.create_task(message)
# Send the task
result = self.send_task(task)
# Convert the task result back to a message
if result.artifacts and len(result.artifacts) > 0:
for part in result.artifacts[0].get("parts", []):
if part.get("type") == "text":
return Message(
content=TextContent(text=part.get("text", "")),
role=MessageRole.AGENT,
parent_message_id=message.message_id,
conversation_id=message.conversation_id
)
# If no artifacts found, create a default response
return Message(
content=TextContent(text="No response content"),
role=MessageRole.AGENT,
parent_message_id=message.message_id,
conversation_id=message.conversation_id
)
def send_conversation(self, conversation: Conversation) -> Conversation:
"""
Send a conversation to the A2A agent (required for BaseA2AClient)
Args:
conversation: The conversation to send
Returns:
The updated conversation
"""
if not conversation.messages:
return conversation
# Get the last message
last_message = conversation.messages[-1]
# Send it to the agent
response = self.send_message(last_message)
# Add the response to the conversation
conversation.add_message(response)
return conversation
def ask(self, message_text):
"""
Simple helper for text-based queries
Args:
message_text: Text message to send
Returns:
Text response from the agent
"""
# Check if message is already a Message object
if isinstance(message_text, str):
message = Message(
content=TextContent(text=message_text),
role=MessageRole.USER
)
else:
message = message_text
# Send message
response = self.send_message(message)
# Extract text from response
if hasattr(response.content, "text"):
return response.content.text
return "No text response"
def create_task(self, message):
"""
Create a new task with a message
Args:
message: Message object or text
Returns:
A new Task object
"""
# Convert string to Message if needed
if isinstance(message, str):
message = Message(
content=TextContent(text=message),
role=MessageRole.USER
)
# Create a task
return Task(
id=str(uuid.uuid4()),
message=message.to_dict() if isinstance(message, Message) else message
)
def send_task(self, task):
"""
Send a task to the agent
Args:
task: The task to send
Returns:
The updated task with the agent's response
"""
# Prepare JSON-RPC request
request_data = {
"jsonrpc": "2.0",
"id": 1,
"method": "tasks/send",
"params": task.to_dict()
}
try:
# Try the standard endpoint first
try:
response = requests.post(
f"{self.url}/tasks/send",
json=request_data,
headers=self.headers
)
response.raise_for_status()
except Exception:
# Try the alternate endpoint
response = requests.post(
f"{self.url}/a2a/tasks/send",
json=request_data,
headers=self.headers
)
response.raise_for_status()
# Parse the response
response_data = response.json()
result = response_data.get("result", {})
# Convert to Task object
return Task.from_dict(result)
except Exception as e:
# Create an error task
task.status = TaskStatus(
state=TaskState.FAILED,
message={"error": str(e)}
)
return task
def get_task(self, task_id, history_length=0):
"""
Get a task by ID
Args:
task_id: ID of the task to retrieve
history_length: Number of history messages to include
Returns:
The task with current status and results
"""
# Prepare JSON-RPC request
request_data = {
"jsonrpc": "2.0",
"id": 1,
"method": "tasks/get",
"params": {
"id": task_id,
"historyLength": history_length
}
}
try:
# Try the standard endpoint first
try:
response = requests.post(
f"{self.url}/tasks/get",
json=request_data,
headers=self.headers
)
response.raise_for_status()
except Exception:
# Try the alternate endpoint
response = requests.post(
f"{self.url}/a2a/tasks/get",
json=request_data,
headers=self.headers
)
response.raise_for_status()
# Parse the response
response_data = response.json()
result = response_data.get("result", {})
# Convert to Task object
return Task.from_dict(result)
except Exception as e:
# Create an error task
return Task(
id=task_id,
status=TaskStatus(
state=TaskState.FAILED,
message={"error": str(e)}
)
)
def cancel_task(self, task_id):
"""
Cancel a task
Args:
task_id: ID of the task to cancel
Returns:
The canceled task
"""
# Prepare JSON-RPC request
request_data = {
"jsonrpc": "2.0",
"id": 1,
"method": "tasks/cancel",
"params": {
"id": task_id
}
}
try:
# Try the standard endpoint first
try:
response = requests.post(
f"{self.url}/tasks/cancel",
json=request_data,
headers=self.headers
)
response.raise_for_status()
except Exception:
# Try the alternate endpoint
response = requests.post(
f"{self.url}/a2a/tasks/cancel",
json=request_data,
headers=self.headers
)
response.raise_for_status()
# Parse the response
response_data = response.json()
result = response_data.get("result", {})
# Convert to Task object
return Task.from_dict(result)
except Exception as e:
# Create an error task
return Task(
id=task_id,
status=TaskStatus(
state=TaskState.CANCELED,
message={"error": str(e)}
)
)