-
Notifications
You must be signed in to change notification settings - Fork 422
feat: Enforce Task or Message as an initial response for Send Streaming Message
#964
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
Closed
Closed
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
9b6f0ee
fix: update `samples/cli.py`
sokoliva b23c112
fix readme
sokoliva 35310ac
enforce first streaming event to be Task
sokoliva 5d9ba84
update `cli.py`
sokoliva c4346ed
comment
sokoliva 49d2831
remove warnings import
sokoliva 4f1d78e
change launch.json
sokoliva File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| # A2A Python SDK — Samples | ||
|
|
||
| This directory contains runnable examples demonstrating how to build and interact with an A2A-compliant agent using the Python SDK. | ||
|
|
||
| ## Contents | ||
|
|
||
| | File | Role | Description | | ||
| |---|---|---| | ||
| | `hello_world_agent.py` | **Server** | A2A agent server | | ||
| | `cli.py` | **Client** | Interactive terminal client | | ||
|
|
||
| The samples are designed to work together out of the box: the agent listens on `http://127.0.0.1:41241`, which is the default URL used by the client. | ||
| --- | ||
|
|
||
| ## `hello_world_agent.py` — Agent Server | ||
|
|
||
| Implements an A2A agent that responds to simple greeting messages (e.g., "hello", "how are you", "bye") with text replies, simulating a 1-second processing delay. | ||
|
|
||
| Demonstrates: | ||
| - Subclassing `AgentExecutor` and implementing `execute()` / `cancel()` | ||
| - Publishing streaming status updates and artifacts via `TaskUpdater` | ||
| - Exposing all three transports in both protocol versions (v1.0 and v0.3 compat) simultaneously: | ||
| - **JSON-RPC** (v1.0 and v0.3) at `http://127.0.0.1:41241/a2a/jsonrpc` | ||
| - **HTTP+JSON (REST)** (v1.0 and v0.3) at `http://127.0.0.1:41241/a2a/rest` | ||
| - **gRPC v1.0** on port `50051` | ||
| - **gRPC v0.3 (compat)** on port `50052` | ||
| - Serving the agent card at `http://127.0.0.1:41241/.well-known/agent-card.json` | ||
|
|
||
| **Run:** | ||
|
|
||
| ```bash | ||
| uv run python samples/hello_world_agent.py | ||
| ``` | ||
|
|
||
| --- | ||
|
|
||
| ## `cli.py` — Client | ||
|
|
||
| An interactive terminal client with full visibility into the streaming event flow. Each `TaskStatusUpdate` and `TaskArtifactUpdate` event is printed as it arrives. | ||
|
|
||
| Features: | ||
| - Transport selection via `--transport` flag (`JSONRPC`, `HTTP+JSON`, `GRPC`) | ||
| - Session management (`context_id` persisted across messages, `task_id` per task) | ||
| - Graceful error handling for HTTP and gRPC failures | ||
|
|
||
| **Run:** | ||
|
|
||
| ```bash | ||
| # Connect to the local hello_world_agent (default): | ||
| uv run python samples/cli.py | ||
|
|
||
| # Connect to a different URL, using gRPC: | ||
| uv run python samples/cli.py --url http://192.168.1.10:41241 --transport GRPC | ||
| ``` | ||
|
|
||
| Then type a message like `hello` and press Enter. | ||
|
|
||
| Type `/quit` or `/exit` to stop, or press `Ctrl+C`. | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||
|---|---|---|---|---|
|
|
@@ -37,6 +37,8 @@ | |||
| SubscribeToTaskRequest, | ||||
| Task, | ||||
| TaskPushNotificationConfig, | ||||
| TaskState, | ||||
| TaskStatus, | ||||
| TaskStatusUpdateEvent, | ||||
| ) | ||||
| from a2a.utils.errors import ( | ||||
|
|
@@ -302,16 +304,35 @@ async def on_message_send_stream( # noqa: D102 | |||
| params: SendMessageRequest, | ||||
| context: ServerCallContext, | ||||
| ) -> AsyncGenerator[Event, None]: | ||||
| is_new_task = not params.message.task_id | ||||
|
|
||||
| active_task, request_context = await self._setup_active_task( | ||||
| params, context | ||||
| ) | ||||
|
|
||||
| task_id = cast('str', request_context.task_id) | ||||
| context_id = cast('str', request_context.context_id) | ||||
| first_event = True | ||||
|
|
||||
| async for event in active_task.subscribe( | ||||
| request=request_context, | ||||
| include_initial_task=False, | ||||
| ): | ||||
| if ( | ||||
| first_event | ||||
| and is_new_task | ||||
| and not isinstance(event, (Task, Message)) | ||||
| ): | ||||
| # Agent didn't emit a Task/Message first. | ||||
| # The stream MUST begin with a Task or Message. | ||||
| submitted_task = Task( | ||||
| id=task_id, | ||||
| context_id=context_id, | ||||
| status=TaskStatus(state=TaskState.TASK_STATE_SUBMITTED), | ||||
| history=[params.message], | ||||
| ) | ||||
|
Comment on lines
+327
to
+332
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. The task_manager is already covering this use case, we can use the task created and saved on the task store
|
||||
| yield apply_history_length(submitted_task, params.configuration) | ||||
| first_event = False | ||||
|
|
||||
| if isinstance(event, Task): | ||||
| self._validate_task_id_match(task_id, event.id) | ||||
| yield apply_history_length(event, params.configuration) | ||||
|
|
||||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.