|
3 | 3 | This module defines the events that are emitted as Agents run through the lifecycle of a request.
|
4 | 4 | """
|
5 | 5 |
|
6 |
| -from dataclasses import dataclass |
7 |
| -from typing import Any, Optional |
8 |
| - |
9 |
| -from ...hooks import HookEvent |
10 |
| -from ...types.content import Message |
11 |
| -from ...types.streaming import StopReason |
12 |
| -from ...types.tools import AgentTool, ToolResult, ToolUse |
13 |
| - |
14 |
| - |
15 |
| -@dataclass |
16 |
| -class BeforeToolInvocationEvent(HookEvent): |
17 |
| - """Event triggered before a tool is invoked. |
18 |
| -
|
19 |
| - This event is fired just before the agent executes a tool, allowing hook |
20 |
| - providers to inspect, modify, or replace the tool that will be executed. |
21 |
| - The selected_tool can be modified by hook callbacks to change which tool |
22 |
| - gets executed. |
23 |
| -
|
24 |
| - Attributes: |
25 |
| - selected_tool: The tool that will be invoked. Can be modified by hooks |
26 |
| - to change which tool gets executed. This may be None if tool lookup failed. |
27 |
| - tool_use: The tool parameters that will be passed to selected_tool. |
28 |
| - invocation_state: Keyword arguments that will be passed to the tool. |
29 |
| - """ |
30 |
| - |
31 |
| - selected_tool: Optional[AgentTool] |
32 |
| - tool_use: ToolUse |
33 |
| - invocation_state: dict[str, Any] |
34 |
| - |
35 |
| - def _can_write(self, name: str) -> bool: |
36 |
| - return name in ["selected_tool", "tool_use"] |
37 |
| - |
38 |
| - |
39 |
| -@dataclass |
40 |
| -class AfterToolInvocationEvent(HookEvent): |
41 |
| - """Event triggered after a tool invocation completes. |
42 |
| -
|
43 |
| - This event is fired after the agent has finished executing a tool, |
44 |
| - regardless of whether the execution was successful or resulted in an error. |
45 |
| - Hook providers can use this event for cleanup, logging, or post-processing. |
46 |
| -
|
47 |
| - Note: This event uses reverse callback ordering, meaning callbacks registered |
48 |
| - later will be invoked first during cleanup. |
49 |
| -
|
50 |
| - Attributes: |
51 |
| - selected_tool: The tool that was invoked. It may be None if tool lookup failed. |
52 |
| - tool_use: The tool parameters that were passed to the tool invoked. |
53 |
| - invocation_state: Keyword arguments that were passed to the tool |
54 |
| - result: The result of the tool invocation. Either a ToolResult on success |
55 |
| - or an Exception if the tool execution failed. |
56 |
| - """ |
57 |
| - |
58 |
| - selected_tool: Optional[AgentTool] |
59 |
| - tool_use: ToolUse |
60 |
| - invocation_state: dict[str, Any] |
61 |
| - result: ToolResult |
62 |
| - exception: Optional[Exception] = None |
63 |
| - |
64 |
| - def _can_write(self, name: str) -> bool: |
65 |
| - return name == "result" |
66 |
| - |
67 |
| - @property |
68 |
| - def should_reverse_callbacks(self) -> bool: |
69 |
| - """True to invoke callbacks in reverse order.""" |
70 |
| - return True |
71 |
| - |
72 |
| - |
73 |
| -@dataclass |
74 |
| -class BeforeModelInvocationEvent(HookEvent): |
75 |
| - """Event triggered before the model is invoked. |
76 |
| -
|
77 |
| - This event is fired just before the agent calls the model for inference, |
78 |
| - allowing hook providers to inspect or modify the messages and configuration |
79 |
| - that will be sent to the model. |
80 |
| -
|
81 |
| - Note: This event is not fired for invocations to structured_output. |
82 |
| - """ |
83 |
| - |
84 |
| - pass |
85 |
| - |
86 |
| - |
87 |
| -@dataclass |
88 |
| -class AfterModelInvocationEvent(HookEvent): |
89 |
| - """Event triggered after the model invocation completes. |
90 |
| -
|
91 |
| - This event is fired after the agent has finished calling the model, |
92 |
| - regardless of whether the invocation was successful or resulted in an error. |
93 |
| - Hook providers can use this event for cleanup, logging, or post-processing. |
94 |
| -
|
95 |
| - Note: This event uses reverse callback ordering, meaning callbacks registered |
96 |
| - later will be invoked first during cleanup. |
97 |
| -
|
98 |
| - Note: This event is not fired for invocations to structured_output. |
99 |
| -
|
100 |
| - Attributes: |
101 |
| - stop_response: The model response data if invocation was successful, None if failed. |
102 |
| - exception: Exception if the model invocation failed, None if successful. |
103 |
| - """ |
104 |
| - |
105 |
| - @dataclass |
106 |
| - class ModelStopResponse: |
107 |
| - """Model response data from successful invocation. |
108 |
| -
|
109 |
| - Attributes: |
110 |
| - stop_reason: The reason the model stopped generating. |
111 |
| - message: The generated message from the model. |
112 |
| - """ |
113 |
| - |
114 |
| - message: Message |
115 |
| - stop_reason: StopReason |
116 |
| - |
117 |
| - stop_response: Optional[ModelStopResponse] = None |
118 |
| - exception: Optional[Exception] = None |
119 |
| - |
120 |
| - @property |
121 |
| - def should_reverse_callbacks(self) -> bool: |
122 |
| - """True to invoke callbacks in reverse order.""" |
123 |
| - return True |
| 6 | +import warnings |
| 7 | +from typing import TypeAlias |
| 8 | + |
| 9 | +from ...hooks.events import AfterModelCallEvent, AfterToolCallEvent, BeforeModelCallEvent, BeforeToolCallEvent |
| 10 | + |
| 11 | +warnings.warn( |
| 12 | + "These events have been moved to production with updated names. Use BeforeModelCallEvent, " |
| 13 | + "AfterModelCallEvent, BeforeToolCallEvent, and AfterToolCallEvent from strands.hooks instead.", |
| 14 | + DeprecationWarning, |
| 15 | + stacklevel=2, |
| 16 | +) |
| 17 | + |
| 18 | +BeforeToolInvocationEvent: TypeAlias = BeforeToolCallEvent |
| 19 | +AfterToolInvocationEvent: TypeAlias = AfterToolCallEvent |
| 20 | +BeforeModelInvocationEvent: TypeAlias = BeforeModelCallEvent |
| 21 | +AfterModelInvocationEvent: TypeAlias = AfterModelCallEvent |
0 commit comments