-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy patha2a_tool.py
More file actions
313 lines (262 loc) · 9.9 KB
/
a2a_tool.py
File metadata and controls
313 lines (262 loc) · 9.9 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
"""A2A singleton tool — one tool per remote agent.
Each tool maintains conversation context (task_id/context_id) across calls
using deterministic persistence via LangGraph graph state (tools_storage).
Authentication uses the UiPath SDK Bearer token, resolved lazily on first call.
"""
from __future__ import annotations
import asyncio
import json
from logging import getLogger
from uuid import uuid4
import httpx
from a2a.client import Client
from a2a.types import (
AgentCard,
Message,
Part,
Role,
Task,
TaskArtifactUpdateEvent,
TaskState,
TextPart,
)
from langchain_core.messages import ToolCall, ToolMessage
from langchain_core.tools import BaseTool
from langgraph.types import Command
from pydantic import BaseModel, Field
from uipath._utils._ssl_context import get_httpx_client_kwargs
from uipath.agent.models.agent import AgentA2aResourceConfig
from uipath_langchain.agent.react.types import AgentGraphState
from uipath_langchain.agent.tools.base_uipath_structured_tool import (
BaseUiPathStructuredTool,
)
from uipath_langchain.agent.tools.tool_node import (
ToolWrapperMixin,
ToolWrapperReturnType,
)
from uipath_langchain.agent.tools.utils import sanitize_tool_name
logger = getLogger(__name__)
class A2aToolInput(BaseModel):
"""Input schema for A2A agent tool."""
message: str = Field(description="The message to send to the remote agent.")
class A2aStructuredToolWithWrapper(BaseUiPathStructuredTool, ToolWrapperMixin):
pass
def _extract_text(obj: Task | Message) -> str:
"""Extract text content from a Task or Message response."""
parts: list[Part] = []
if isinstance(obj, Message):
parts = obj.parts or []
elif isinstance(obj, Task):
if obj.status and obj.status.state == TaskState.input_required:
if obj.status.message:
parts = obj.status.message.parts or []
else:
if obj.artifacts:
for artifact in obj.artifacts:
parts.extend(artifact.parts or [])
if not parts and obj.status and obj.status.message:
parts = obj.status.message.parts or []
if not parts and obj.history:
for msg in reversed(obj.history):
if msg.role == Role.agent:
parts = msg.parts or []
break
texts = []
for part in parts:
if isinstance(part.root, TextPart):
texts.append(part.root.text)
return "\n".join(texts) if texts else ""
def _format_response(text: str, state: str) -> str:
"""Build a structured tool response the LLM can act on."""
return json.dumps({"agent_response": text, "task_state": state})
def _build_description(card: AgentCard) -> str:
"""Build a tool description from an agent card."""
parts = []
if card.description:
parts.append(card.description)
if card.skills:
for skill in card.skills:
skill_desc = skill.name or ""
if skill.description:
skill_desc += f": {skill.description}"
if skill_desc:
parts.append(f"Skill: {skill_desc}")
return " | ".join(parts) if parts else f"Remote A2A agent at {card.url}"
def _resolve_a2a_url(config: AgentA2aResourceConfig) -> str:
"""Resolve the A2A endpoint URL from the cached agent card."""
if config.cached_agent_card and "url" in config.cached_agent_card:
return config.cached_agent_card["url"]
return ""
async def create_a2a_agent_tools(
resources: list[AgentA2aResourceConfig],
) -> list[BaseTool]:
"""Create A2A tools from a list of A2A resource configurations.
Each enabled A2A resource becomes a single tool representing the remote agent.
Conversation context (task_id/context_id) is persisted in LangGraph graph state.
Args:
resources: List of A2A resource configurations from agent.json.
Returns:
List of BaseTool instances, one per enabled A2A resource.
"""
tools: list[BaseTool] = []
for resource in resources:
if resource.is_enabled is False:
logger.info("Skipping disabled A2A resource '%s'", resource.name)
continue
logger.info("Creating A2A tool for resource '%s'", resource.name)
tool = _create_a2a_tool(resource)
tools.append(tool)
return tools
async def _send_a2a_message(
client: Client,
a2a_url: str,
*,
message: str,
task_id: str | None,
context_id: str | None,
) -> tuple[str, str, str | None, str | None]:
"""Send a message to a remote A2A agent and return the response.
Returns:
Tuple of (response_text, task_state, new_task_id, new_context_id).
"""
if task_id or context_id:
logger.info(
"A2A continue task=%s context=%s to %s", task_id, context_id, a2a_url
)
else:
logger.info("A2A new message to %s", a2a_url)
a2a_message = Message(
role=Role.user,
parts=[Part(root=TextPart(text=message))],
message_id=str(uuid4()),
task_id=task_id,
context_id=context_id,
)
try:
text = ""
state = "unknown"
new_task_id = task_id
new_context_id = context_id
async for event in client.send_message(a2a_message):
if isinstance(event, Message):
text = _extract_text(event)
new_context_id = event.context_id
state = "completed"
break
else:
task, update = event
new_task_id = task.id
new_context_id = task.context_id
state = task.status.state.value if task.status else "unknown"
if update is None:
text = _extract_text(task)
break
elif isinstance(update, TaskArtifactUpdateEvent):
for part in update.artifact.parts or []:
if isinstance(part.root, TextPart):
text += part.root.text
return (text or "No response received.", state, new_task_id, new_context_id)
except Exception as e:
logger.exception("A2A request to %s failed", a2a_url)
return (f"Error: {e}", "error", task_id, context_id)
def _create_a2a_tool(config: AgentA2aResourceConfig) -> BaseTool:
"""Create a single LangChain tool for A2A communication.
Conversation context (task_id/context_id) is persisted deterministically
in LangGraph's graph state via tools_storage, ensuring reliable
multi-turn conversations with the remote agent.
"""
if config.cached_agent_card:
agent_card = AgentCard(**config.cached_agent_card)
else:
agent_card = AgentCard(
url="",
name=config.name,
description=config.description or "",
version="1.0.0",
skills=[],
capabilities={},
default_input_modes=["text/plain"],
default_output_modes=["text/plain"],
)
raw_name = agent_card.name or config.name
tool_name = sanitize_tool_name(raw_name)
tool_description = _build_description(agent_card)
a2a_url = _resolve_a2a_url(config)
_lock = asyncio.Lock()
_client: Client | None = None
_http_client: httpx.AsyncClient | None = None
async def _ensure_client() -> Client:
nonlocal _client, _http_client
if _client is None:
async with _lock:
if _client is None:
from a2a.client import ClientConfig, ClientFactory
from uipath.platform import UiPath
sdk = UiPath()
client_kwargs = get_httpx_client_kwargs(
headers={"Authorization": f"Bearer {sdk._config.secret}"},
)
_http_client = httpx.AsyncClient(**client_kwargs)
_client = await ClientFactory.connect(
a2a_url,
client_config=ClientConfig(
httpx_client=_http_client,
streaming=False,
),
)
return _client
metadata = {
"tool_type": "a2a",
"display_name": raw_name,
"slug": config.slug,
}
async def _send(*, message: str) -> str:
client = await _ensure_client()
text, state, _, _ = await _send_a2a_message(
client, a2a_url, message=message, task_id=None, context_id=None
)
return _format_response(text, state)
async def _a2a_wrapper(
tool: BaseTool,
call: ToolCall,
state: AgentGraphState,
) -> ToolWrapperReturnType:
prior = state.inner_state.tools_storage.get(tool.name) or {}
task_id = prior.get("task_id")
context_id = prior.get("context_id")
client = await _ensure_client()
text, task_state, new_task_id, new_context_id = await _send_a2a_message(
client,
a2a_url,
message=call["args"]["message"],
task_id=task_id,
context_id=context_id,
)
return Command(
update={
"messages": [
ToolMessage(
content=_format_response(text, task_state),
name=call["name"],
tool_call_id=call["id"],
)
],
"inner_state": {
"tools_storage": {
tool.name: {
"task_id": new_task_id,
"context_id": new_context_id,
}
}
},
}
)
tool = A2aStructuredToolWithWrapper(
name=tool_name,
description=tool_description,
coroutine=_send,
args_schema=A2aToolInput,
metadata=metadata,
)
tool.set_tool_wrappers(awrapper=_a2a_wrapper)
return tool