-
Notifications
You must be signed in to change notification settings - Fork 851
Expand file tree
/
Copy path__init__.py
More file actions
74 lines (65 loc) · 2.3 KB
/
__init__.py
File metadata and controls
74 lines (65 loc) · 2.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
"""Typed hook system for extending agent functionality.
This module provides a composable mechanism for building objects that can hook
into specific events during the agent lifecycle. The hook system enables both
built-in SDK components and user code to react to or modify agent behavior
through strongly-typed event callbacks.
Example Usage:
```python
from strands.hooks import HookProvider, HookRegistry
from strands.hooks.events import BeforeInvocationEvent, AfterInvocationEvent
class LoggingHooks(HookProvider):
def register_hooks(self, registry: HookRegistry) -> None:
registry.add_callback(BeforeInvocationEvent, self.log_start)
registry.add_callback(AfterInvocationEvent, self.log_end)
def log_start(self, event: BeforeInvocationEvent) -> None:
print(f"Request started for {event.agent.name}")
def log_end(self, event: AfterInvocationEvent) -> None:
print(f"Request completed for {event.agent.name}")
# Use with agent
agent = Agent(hooks=[LoggingHooks()])
```
This replaces the older callback_handler approach with a more composable,
type-safe system that supports multiple subscribers per event type.
"""
from .events import (
AfterInvocationEvent,
AfterModelCallEvent,
# Multiagent hook events
AfterMultiAgentInvocationEvent,
AfterNodeCallEvent,
AfterReduceContextEvent,
AfterToolCallEvent,
AgentInitializedEvent,
BeforeInvocationEvent,
BeforeModelCallEvent,
BeforeMultiAgentInvocationEvent,
BeforeNodeCallEvent,
BeforeReduceContextEvent,
BeforeToolCallEvent,
MessageAddedEvent,
MultiAgentInitializedEvent,
)
from .registry import BaseHookEvent, HookCallback, HookEvent, HookProvider, HookRegistry
__all__ = [
"AgentInitializedEvent",
"BeforeInvocationEvent",
"BeforeToolCallEvent",
"AfterToolCallEvent",
"BeforeModelCallEvent",
"AfterModelCallEvent",
"AfterInvocationEvent",
"BeforeReduceContextEvent",
"AfterReduceContextEvent",
"MessageAddedEvent",
"HookEvent",
"HookProvider",
"HookCallback",
"HookRegistry",
"HookEvent",
"BaseHookEvent",
"AfterMultiAgentInvocationEvent",
"AfterNodeCallEvent",
"BeforeMultiAgentInvocationEvent",
"BeforeNodeCallEvent",
"MultiAgentInitializedEvent",
]