Skip to content

Commit 99f6aa2

Browse files
committed
utilize get_artifact_text, get_message_text
1 parent 3ac014a commit 99f6aa2

3 files changed

Lines changed: 29 additions & 22 deletions

File tree

samples/cli.py

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,16 @@
1111

1212
from a2a.client import A2ACardResolver, ClientConfig, create_client
1313
from a2a.types import Message, Part, Role, SendMessageRequest, TaskState
14+
from a2a.utils import get_artifact_text, get_message_text
1415
from a2a.utils.agent_card import display_agent_card
1516

1617

17-
async def _handle_stream( # noqa: PLR0912
18+
async def _handle_stream(
1819
stream: Any, current_task_id: str | None
1920
) -> str | None:
2021
async for event in stream:
2122
if event.HasField('message'):
22-
print('Message:', end=' ')
23-
for part in event.message.parts:
24-
if part.text:
25-
print(part.text, end=' ')
26-
print()
23+
print('Message:', get_message_text(event.message, delimiter=' '))
2724
return None
2825

2926
if not current_task_id:
@@ -36,12 +33,15 @@ async def _handle_stream( # noqa: PLR0912
3633

3734
if event.HasField('status_update'):
3835
state_name = TaskState.Name(event.status_update.status.state)
39-
print(f'TaskStatusUpdate [state={state_name}]:', end=' ')
40-
if event.status_update.status.HasField('message'):
41-
for part in event.status_update.status.message.parts:
42-
if part.text:
43-
print(part.text, end=' ')
44-
print()
36+
message_text = (
37+
': '
38+
+ get_message_text(
39+
event.status_update.status.message, delimiter=' '
40+
)
41+
if event.status_update.status.HasField('message')
42+
else ''
43+
)
44+
print(f'TaskStatusUpdate [state={state_name}]{message_text}')
4545
if state_name in (
4646
'TASK_STATE_COMPLETED',
4747
'TASK_STATE_FAILED',
@@ -53,12 +53,10 @@ async def _handle_stream( # noqa: PLR0912
5353
elif event.HasField('artifact_update'):
5454
print(
5555
f'TaskArtifactUpdate [name={event.artifact_update.artifact.name}]:',
56-
end=' ',
56+
get_artifact_text(
57+
event.artifact_update.artifact, delimiter=' '
58+
),
5759
)
58-
for part in event.artifact_update.artifact.parts:
59-
if part.text:
60-
print(part.text, end=' ')
61-
print()
6260
return current_task_id
6361

6462

src/a2a/utils/agent_card.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,8 @@ def display_agent_card(card: AgentCard) -> None:
2626
if card.icon_url:
2727
lines.append(f'Icon URL : {card.icon_url}')
2828
if card.HasField('provider'):
29-
lines.append(
30-
f'Provider : {card.provider.organization} ({card.provider.url})'
31-
)
29+
url_suffix = f' ({card.provider.url})' if card.provider.url else ''
30+
lines.append(f'Provider : {card.provider.organization}{url_suffix}')
3231

3332
lines += ['', '--- Interfaces ---']
3433
for i, iface in enumerate(card.supported_interfaces):

tests/utils/test_agent_card_display.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,10 +167,10 @@ def test_interface_without_binding_or_version_has_no_parentheses(
167167
display_agent_card(card)
168168
assert ' [0] 127.0.0.1:50051\n' in capsys.readouterr().out
169169

170-
def test_provider_both_fields_always_shown(
170+
def test_provider_with_url(
171171
self, capsys: pytest.CaptureFixture[str]
172172
) -> None:
173-
"""Both organization and url are shown when provider is set (both are required fields)."""
173+
"""Provider shows organization and URL in parentheses when both are set."""
174174
card = AgentCard(
175175
provider=AgentProvider(
176176
organization='Example Org',
@@ -182,3 +182,13 @@ def test_provider_both_fields_always_shown(
182182
'Provider : Example Org (https://example.com)'
183183
in capsys.readouterr().out
184184
)
185+
186+
def test_provider_without_url_has_no_empty_parentheses(
187+
self, capsys: pytest.CaptureFixture[str]
188+
) -> None:
189+
"""No empty parentheses when provider URL is not set."""
190+
card = AgentCard(provider=AgentProvider(organization='Example Org'))
191+
display_agent_card(card)
192+
out = capsys.readouterr().out
193+
assert 'Provider : Example Org' in out
194+
assert '()' not in out

0 commit comments

Comments
 (0)