-
Notifications
You must be signed in to change notification settings - Fork 279
feat: update create_text_message_object() to accept other input parameters
#519
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 10 commits
ff8b93d
b7906f8
8a0196c
b236530
4c2b78f
e6d806e
1211d73
275f124
54f3af9
85d0c9e
3d0288d
1a35a16
dc44d96
5d60e93
00ab922
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,22 +1,32 @@ | ||
| """Helper functions for the A2A client.""" | ||
|
|
||
| from typing import Any | ||
| from uuid import uuid4 | ||
|
|
||
| from a2a.types import Message, Part, Role, TextPart | ||
|
|
||
|
|
||
| def create_text_message_object( | ||
| role: Role = Role.user, content: str = '' | ||
| role: Role = Role.user, | ||
| content: str = '', | ||
| extensions: list[str] | None = None, | ||
| metadata: dict[str, Any] | None = None, | ||
| ) -> Message: | ||
| """Create a Message object containing a single TextPart. | ||
|
|
||
| Args: | ||
| role: The role of the message sender (user or agent). Defaults to Role.user. | ||
| content: The text content of the message. Defaults to an empty string. | ||
| extensions: The extensions of the message. Defaults to an empty list. | ||
| metadata: The metadata of the message. Defaults to an empty dictionary. | ||
|
|
||
| Returns: | ||
| A `Message` object with a new UUID message_id. | ||
| """ | ||
| return Message( | ||
| role=role, parts=[Part(TextPart(text=content))], message_id=str(uuid4()) | ||
| role=role, | ||
| parts=[Part(TextPart(text=content))], | ||
| message_id=str(uuid4()), | ||
| extensions=extensions or [], | ||
| metadata=metadata or {}, | ||
| ) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -378,6 +378,64 @@ async def test_send_message_success( | |
| assert isinstance(response, Message) | ||
| assert response.model_dump() == success_response.model_dump() | ||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_send_message_success_with_extensions( | ||
| self, mock_httpx_client: AsyncMock, mock_agent_card: MagicMock | ||
| ): | ||
| client = JsonRpcTransport( | ||
| httpx_client=mock_httpx_client, agent_card=mock_agent_card | ||
| ) | ||
| params = MessageSendParams( | ||
| message=create_text_message_object( | ||
| content='Hello', extensions=['test'] | ||
| ) | ||
| ) | ||
| success_response = create_text_message_object( | ||
| role=Role.agent, content='Hi there!', extensions=['test'] | ||
| ) | ||
| rpc_response = SendMessageSuccessResponse( | ||
| id='123', jsonrpc='2.0', result=success_response | ||
| ) | ||
| response = httpx.Response( | ||
| 200, json=rpc_response.model_dump(mode='json') | ||
| ) | ||
| response.request = httpx.Request('POST', 'http://agent.example.com/api') | ||
| mock_httpx_client.post.return_value = response | ||
|
|
||
| response = await client.send_message(request=params) | ||
|
|
||
| assert isinstance(response, Message) | ||
| assert response.model_dump() == success_response.model_dump() | ||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_send_message_success_with_metadata( | ||
| self, mock_httpx_client: AsyncMock, mock_agent_card: MagicMock | ||
| ): | ||
| client = JsonRpcTransport( | ||
| httpx_client=mock_httpx_client, agent_card=mock_agent_card | ||
| ) | ||
| params = MessageSendParams( | ||
| message=create_text_message_object( | ||
| content='Hello', metadata={'test': 'test'} | ||
| ) | ||
| ) | ||
| success_response = create_text_message_object( | ||
| role=Role.agent, content='Hi there!', metadata={'test': 'test'} | ||
| ) | ||
| rpc_response = SendMessageSuccessResponse( | ||
| id='123', jsonrpc='2.0', result=success_response | ||
| ) | ||
| response = httpx.Response( | ||
| 200, json=rpc_response.model_dump(mode='json') | ||
| ) | ||
| response.request = httpx.Request('POST', 'http://agent.example.com/api') | ||
| mock_httpx_client.post.return_value = response | ||
|
|
||
| response = await client.send_message(request=params) | ||
|
|
||
| assert isinstance(response, Message) | ||
| assert response.model_dump() == success_response.model_dump() | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think there is much value in adding additional tests for the
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. hi will add it over weekend, thanks |
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_send_message_error_response( | ||
| self, mock_httpx_client: AsyncMock, mock_agent_card: MagicMock | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.