generated from amazon-archives/__template_Apache-2.0
-
Notifications
You must be signed in to change notification settings - Fork 499
hooks - before node call - cancel node #1203
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
Merged
+243
−32
Merged
Changes from all commits
Commits
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
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
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
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 |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| import pytest | ||
|
|
||
| from strands import Agent | ||
| from strands.experimental.hooks.multiagent import BeforeNodeCallEvent | ||
| from strands.hooks import HookProvider | ||
| from strands.multiagent import GraphBuilder, Swarm | ||
| from strands.multiagent.base import Status | ||
| from strands.types._events import MultiAgentNodeCancelEvent | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def cancel_hook(): | ||
| class Hook(HookProvider): | ||
| def register_hooks(self, registry): | ||
| registry.add_callback(BeforeNodeCallEvent, self.cancel) | ||
|
|
||
| def cancel(self, event): | ||
| if event.node_id == "weather": | ||
| event.cancel_node = "test cancel" | ||
|
|
||
| return Hook() | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def info_agent(): | ||
| return Agent(name="info") | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def weather_agent(): | ||
| return Agent(name="weather") | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def swarm(cancel_hook, info_agent, weather_agent): | ||
| return Swarm([info_agent, weather_agent], hooks=[cancel_hook]) | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def graph(cancel_hook, info_agent, weather_agent): | ||
| builder = GraphBuilder() | ||
| builder.add_node(info_agent, "info") | ||
| builder.add_node(weather_agent, "weather") | ||
| builder.add_edge("info", "weather") | ||
| builder.set_entry_point("info") | ||
| builder.set_hook_providers([cancel_hook]) | ||
|
|
||
| return builder.build() | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_swarm_cancel_node(swarm): | ||
| tru_cancel_event = None | ||
| async for event in swarm.stream_async("What is the weather"): | ||
| if event.get("type") == "multiagent_node_cancel": | ||
| tru_cancel_event = event | ||
|
|
||
| multiagent_result = event["result"] | ||
|
|
||
| exp_cancel_event = MultiAgentNodeCancelEvent(node_id="weather", message="test cancel") | ||
| assert tru_cancel_event == exp_cancel_event | ||
|
|
||
| tru_status = multiagent_result.status | ||
| exp_status = Status.FAILED | ||
| assert tru_status == exp_status | ||
|
|
||
| assert len(multiagent_result.node_history) == 1 | ||
| tru_node_id = multiagent_result.node_history[0].node_id | ||
| exp_node_id = "info" | ||
| assert tru_node_id == exp_node_id | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_graph_cancel_node(graph): | ||
| tru_cancel_event = None | ||
| with pytest.raises(RuntimeError, match="test cancel"): | ||
| async for event in graph.stream_async("What is the weather"): | ||
| if event.get("type") == "multiagent_node_cancel": | ||
| tru_cancel_event = event | ||
|
|
||
| exp_cancel_event = MultiAgentNodeCancelEvent(node_id="weather", message="test cancel") | ||
| assert tru_cancel_event == exp_cancel_event | ||
|
|
||
| state = graph.state | ||
|
|
||
| tru_status = state.status | ||
| exp_status = Status.FAILED | ||
| assert tru_status == exp_status |
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.