-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_a2a_hitl.py
More file actions
69 lines (58 loc) · 2.1 KB
/
Copy pathtest_a2a_hitl.py
File metadata and controls
69 lines (58 loc) · 2.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
"""A2A example: human-in-the-loop email approval flow.
Sends a message that triggers your_send_email_tool (which is configured with
HumanInTheLoopMiddleware), waits for the interrupt, then resumes by approving
the tool call.
Usage:
export LANGSMITH_API_KEY=lsv2_...
uv run python test_a2a_hitl.py
"""
import asyncio
import os
import httpx
from a2a.client import ClientConfig, ClientFactory
from a2a.types import DataPart, Message, Part, Role, TextPart
AGENT_URL = "https://<deployment-url>/a2a/<assistant-id>"
HEADERS = {"X-Api-Key": os.environ["LANGSMITH_API_KEY"]}
async def main():
async with httpx.AsyncClient(headers=HEADERS) as httpx_client:
client = await ClientFactory.connect(
AGENT_URL,
client_config=ClientConfig(streaming=False, httpx_client=httpx_client),
resolver_http_kwargs={"headers": HEADERS},
)
# Step 1: Send a message that triggers the email tool (will interrupt)
async for task, event in client.send_message(
Message(
message_id="send-email",
parts=[
Part(
root=TextPart(
text="Send an email to bob@example.com with subject 'Hello' and body 'Hi Bob!'"
)
)
],
role=Role.user,
)
):
print("Interrupted — waiting for approval")
print(task)
print()
# Step 2: Resume by approving the interrupted tool call
async for task, event in client.send_message(
Message(
message_id="approve-email",
context_id=task.context_id,
task_id=task.id,
parts=[
Part(
root=DataPart(
data={"resume": {"decisions": [{"type": "approve"}]}}
)
)
],
role=Role.user,
)
):
print("Approved — task completed")
print(task)
asyncio.run(main())