Skip to content

Commit eef11cc

Browse files
feat: Create a new HookEvent for Multiagent (#925)
* add a base class example * feat:add BaseHookEvent for multiagent use
1 parent 99cd49b commit eef11cc

File tree

2 files changed

+18
-11
lines changed

2 files changed

+18
-11
lines changed

src/strands/hooks/__init__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ def log_end(self, event: AfterInvocationEvent) -> None:
3939
BeforeToolCallEvent,
4040
MessageAddedEvent,
4141
)
42-
from .registry import HookCallback, HookEvent, HookProvider, HookRegistry
42+
from .registry import BaseHookEvent, HookCallback, HookEvent, HookProvider, HookRegistry
4343

4444
__all__ = [
4545
"AgentInitializedEvent",
@@ -54,4 +54,6 @@ def log_end(self, event: AfterInvocationEvent) -> None:
5454
"HookProvider",
5555
"HookCallback",
5656
"HookRegistry",
57+
"HookEvent",
58+
"BaseHookEvent",
5759
]

src/strands/hooks/registry.py

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,8 @@
1515

1616

1717
@dataclass
18-
class HookEvent:
19-
"""Base class for all hook events.
20-
21-
Attributes:
22-
agent: The agent instance that triggered this event.
23-
"""
24-
25-
agent: "Agent"
18+
class BaseHookEvent:
19+
"""Base class for all hook events."""
2620

2721
@property
2822
def should_reverse_callbacks(self) -> bool:
@@ -66,10 +60,21 @@ def __setattr__(self, name: str, value: Any) -> None:
6660
raise AttributeError(f"Property {name} is not writable")
6761

6862

69-
TEvent = TypeVar("TEvent", bound=HookEvent, contravariant=True)
63+
@dataclass
64+
class HookEvent(BaseHookEvent):
65+
"""Base class for single agent hook events.
66+
67+
Attributes:
68+
agent: The agent instance that triggered this event.
69+
"""
70+
71+
agent: "Agent"
72+
73+
74+
TEvent = TypeVar("TEvent", bound=BaseHookEvent, contravariant=True)
7075
"""Generic for adding callback handlers - contravariant to allow adding handlers which take in base classes."""
7176

72-
TInvokeEvent = TypeVar("TInvokeEvent", bound=HookEvent)
77+
TInvokeEvent = TypeVar("TInvokeEvent", bound=BaseHookEvent)
7378
"""Generic for invoking events - non-contravariant to enable returning events."""
7479

7580

0 commit comments

Comments
 (0)