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
51 lines (37 loc) · 1.41 KB
/
text_client_cli.py
File metadata and controls
51 lines (37 loc) · 1.41 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
import argparse
import asyncio
import grpc
import httpx
from a2a.client import A2ACardResolver, 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'
)
args = parser.parse_args()
print(f'Connecting to {args.url}')
async with httpx.AsyncClient() as httpx_client:
resolver = A2ACardResolver(httpx_client, args.url)
card = await resolver.get_agent_card()
print(f'\n✓ Agent Card Found: {card.name}')
text_client = await create_text_client(card)
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())