forked from a2aproject/a2a-python
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtext_client_cli.py
More file actions
70 lines (54 loc) · 2.04 KB
/
text_client_cli.py
File metadata and controls
70 lines (54 loc) · 2.04 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
import argparse
import asyncio
import grpc
import httpx
from a2a.client import A2ACardResolver, ClientConfig, create_text_client
async def main() -> None:
"""Run the simple A2A terminal client using TextClient."""
parser = argparse.ArgumentParser(description='A2A Simple Text Client')
parser.add_argument(
'--url', default='http://127.0.0.1:41241', help='Agent base URL'
)
parser.add_argument(
'--transport',
default=None,
help='Preferred transport (JSONRPC, HTTP+JSON, GRPC)',
)
args = parser.parse_args()
config = ClientConfig()
if args.transport:
config.supported_protocol_bindings = [args.transport]
if args.transport == 'GRPC':
config.grpc_channel_factory = grpc.aio.insecure_channel
print(
f'Connecting to {args.url} (preferred transport: {args.transport or "Any"})'
)
async with httpx.AsyncClient() as httpx_client:
resolver = A2ACardResolver(httpx_client, args.url)
card = await resolver.get_agent_card()
print('\n✓ Agent Card Found:')
print(f' Name: {card.name}')
text_client = await create_text_client(card, client_config=config)
actual_transport = getattr(
text_client.client, '_transport', text_client.client
)
print(f' Picked Transport: {actual_transport.__class__.__name__}')
print('\nConnected! Send a message or type /quit to exit.')
while True:
try:
loop = asyncio.get_running_loop()
user_input = await loop.run_in_executor(None, input, 'You: ')
except KeyboardInterrupt:
break
if user_input.lower() in ('/quit', '/exit'):
break
if not user_input.strip():
continue
try:
response = await text_client.send_text_message(user_input)
print(f'Agent: {response}')
except (httpx.RequestError, grpc.RpcError) as e:
print(f'Error communicating with agent: {e}')
await text_client.close()
if __name__ == '__main__':
asyncio.run(main())