generated from amazon-archives/__template_Apache-2.0
-
Notifications
You must be signed in to change notification settings - Fork 449
feat: add multiagent hooks, add serialize & deserialize function to multiagent base & agent result #1070
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
Merged
feat: add multiagent hooks, add serialize & deserialize function to multiagent base & agent result #1070
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| """Multi-agent hook events and utilities. | ||
|
|
||
| Provides event classes for hooking into multi-agent orchestrator lifecycle. | ||
| """ | ||
|
|
||
| from .events import ( | ||
| AfterMultiAgentInvocationEvent, | ||
| AfterNodeCallEvent, | ||
| BeforeMultiAgentInvocationEvent, | ||
| BeforeNodeCallEvent, | ||
| MultiAgentInitializedEvent, | ||
| ) | ||
|
|
||
| __all__ = [ | ||
| "AfterMultiAgentInvocationEvent", | ||
| "AfterNodeCallEvent", | ||
| "BeforeMultiAgentInvocationEvent", | ||
| "BeforeNodeCallEvent", | ||
| "MultiAgentInitializedEvent", | ||
| ] |
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,93 @@ | ||
| """Multi-agent execution lifecycle events for hook system integration. | ||
|
|
||
| These events are fired by orchestrators (Graph/Swarm) at key points so | ||
| hooks can persist, monitor, or debug execution. No intermediate state model | ||
| is used—hooks read from the orchestrator directly. | ||
| """ | ||
|
|
||
| from dataclasses import dataclass | ||
| from typing import TYPE_CHECKING, Any | ||
|
|
||
| from ....hooks import BaseHookEvent | ||
|
|
||
| if TYPE_CHECKING: | ||
| from ....multiagent.base import MultiAgentBase | ||
|
|
||
|
|
||
| @dataclass | ||
| class MultiAgentInitializedEvent(BaseHookEvent): | ||
| """Event triggered when multi-agent orchestrator initialized. | ||
|
|
||
| Attributes: | ||
| source: The multi-agent orchestrator instance | ||
| invocation_state: Configuration that user passes in | ||
| """ | ||
|
|
||
| source: "MultiAgentBase" | ||
| invocation_state: dict[str, Any] | None = None | ||
|
|
||
|
|
||
| @dataclass | ||
| class BeforeNodeCallEvent(BaseHookEvent): | ||
| """Event triggered before individual node execution starts. | ||
|
|
||
| Attributes: | ||
| source: The multi-agent orchestrator instance | ||
| node_id: ID of the node about to execute | ||
| invocation_state: Configuration that user passes in | ||
| """ | ||
|
|
||
| source: "MultiAgentBase" | ||
| node_id: str | ||
| invocation_state: dict[str, Any] | None = None | ||
|
|
||
|
|
||
| @dataclass | ||
| class AfterNodeCallEvent(BaseHookEvent): | ||
| """Event triggered after individual node execution completes. | ||
|
|
||
| Attributes: | ||
| source: The multi-agent orchestrator instance | ||
| node_id: ID of the node that just completed execution | ||
| invocation_state: Configuration that user passes in | ||
| """ | ||
|
|
||
| source: "MultiAgentBase" | ||
| node_id: str | ||
| invocation_state: dict[str, Any] | None = None | ||
|
|
||
| @property | ||
| def should_reverse_callbacks(self) -> bool: | ||
| """True to invoke callbacks in reverse order.""" | ||
| return True | ||
|
|
||
|
|
||
| @dataclass | ||
| class BeforeMultiAgentInvocationEvent(BaseHookEvent): | ||
| """Event triggered before orchestrator execution starts. | ||
|
|
||
| Attributes: | ||
| source: The multi-agent orchestrator instance | ||
| invocation_state: Configuration that user passes in | ||
| """ | ||
|
|
||
| source: "MultiAgentBase" | ||
| invocation_state: dict[str, Any] | None = None | ||
|
|
||
|
|
||
| @dataclass | ||
| class AfterMultiAgentInvocationEvent(BaseHookEvent): | ||
| """Event triggered after orchestrator execution completes. | ||
|
|
||
| Attributes: | ||
| source: The multi-agent orchestrator instance | ||
| invocation_state: Configuration that user passes in | ||
| """ | ||
|
|
||
| source: "MultiAgentBase" | ||
| invocation_state: dict[str, Any] | None = None | ||
|
|
||
| @property | ||
| def should_reverse_callbacks(self) -> bool: | ||
| """True to invoke callbacks in reverse order.""" | ||
| return True |
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,41 @@ | ||
| from typing import Iterator, Literal, Tuple, Type | ||
|
|
||
| from strands.experimental.hooks.multiagent.events import ( | ||
| AfterMultiAgentInvocationEvent, | ||
| AfterNodeCallEvent, | ||
| BeforeNodeCallEvent, | ||
| MultiAgentInitializedEvent, | ||
| ) | ||
| from strands.hooks import ( | ||
| HookEvent, | ||
| HookProvider, | ||
| HookRegistry, | ||
| ) | ||
|
|
||
|
|
||
| class MockMultiAgentHookProvider(HookProvider): | ||
| def __init__(self, event_types: list[Type] | Literal["all"]): | ||
| if event_types == "all": | ||
| event_types = [ | ||
| MultiAgentInitializedEvent, | ||
| BeforeNodeCallEvent, | ||
| AfterNodeCallEvent, | ||
| AfterMultiAgentInvocationEvent, | ||
| ] | ||
|
|
||
| self.events_received = [] | ||
| self.events_types = event_types | ||
|
|
||
| @property | ||
| def event_types_received(self): | ||
| return [type(event) for event in self.events_received] | ||
|
|
||
| def get_events(self) -> Tuple[int, Iterator[HookEvent]]: | ||
| return len(self.events_received), iter(self.events_received) | ||
|
|
||
| def register_hooks(self, registry: HookRegistry) -> None: | ||
| for event_type in self.events_types: | ||
| registry.add_callback(event_type, self.add_event) | ||
|
|
||
| def add_event(self, event: HookEvent) -> None: | ||
| self.events_received.append(event) |
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
Empty file.
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.