forked from a2aproject/a2a-python
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtext_client.py
More file actions
64 lines (53 loc) · 2.11 KB
/
text_client.py
File metadata and controls
64 lines (53 loc) · 2.11 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
import uuid
from a2a.client.client import Client, ClientCallContext
from a2a.types import Message, Part, Role, SendMessageRequest
class TextClient:
"""A facade around Client that simplifies text-based communication.
Wraps an underlying Client instance and exposes a simplified interface
for sending plain-text messages and receiving aggregated text responses.
For full Client API access, use the underlying client directly via
the `client` property.
"""
def __init__(self, client: Client):
self._client = client
@property
def client(self) -> Client:
"""Returns the underlying Client instance for full API access."""
return self._client
async def send_text_message(
self,
text: str,
*,
context: ClientCallContext | None = None,
) -> str:
"""Sends a text message and returns the aggregated text response."""
request = SendMessageRequest(
message=Message(
role=Role.ROLE_USER,
message_id=str(uuid.uuid4()),
parts=[Part(text=text)],
)
)
response_parts: list[str] = []
async for event in self._client.send_message(request, context=context):
if event.HasField('message'):
response_parts.extend(
part.text for part in event.message.parts if part.text
)
elif event.HasField('status_update'):
if event.status_update.status.HasField('message'):
response_parts.extend(
part.text
for part in event.status_update.status.message.parts
if part.text
)
elif event.HasField('artifact_update'):
response_parts.extend(
part.text
for part in event.artifact_update.artifact.parts
if part.text
)
return ' '.join(response_parts)
async def close(self) -> None:
"""Closes the underlying client."""
await self._client.close()