strands/hooks/mod.rs
1//! Hooks: the 17 hook events, the typed [`HookRegistry`] and the stream records
2//! ([`AgentStreamEvent`]).
3//!
4//! Register hooks on an agent, a [`LocalAgent`](crate::agent::LocalAgent) or a registry, in three
5//! forms:
6//!
7//! - [`add_hook`](HookRegistry::add_hook) takes a plain closure; annotate its argument so the
8//! event type is inferred.
9//! - [`add_async_hook::<E>`](HookRegistry::add_async_hook) takes a closure that returns
10//! `Box::pin(async move { .. })`, for hooks that await while borrowing the event.
11//! - [`add_hook_impl`](HookRegistry::add_hook_impl) takes a [`Hook`] implementation.
12//!
13//! Each returns a [`HookCleanup`]; the `_with` variants take [`HookCallbackOptions`] with the
14//! [`HookOrder`].
15//!
16//! # Errors and panics
17//!
18//! A hook returns `()` or `Result<(), E>` for any `E: Into<BoxError>` ([`HookOutput`]); a
19//! [`Hook`] implementation returns `Result<(), BoxError>`. An error fails the invocation with it:
20//! the callbacks after it do not run, and in a tool call it stops the
21//! batch once `AfterToolsEvent` has fired. An [`InterruptError`](crate::InterruptError), such as
22//! the one `BeforeToolCallEvent::interrupt` returns, is not a failure: the agent stops with
23//! `StopReason::Interrupt` once every callback has run.
24//!
25//! A panicking hook is a bug and propagates to the caller of `invoke` or the stream's consumer,
26//! also when it runs on a tool call's task. Only a panic inside a tool's body becomes an error
27//! result the model sees.
28//!
29//! [`BoxError`]: crate::BoxError
30//!
31//! ```
32//! use strands::hooks::{AfterToolCallEvent, BeforeToolCallEvent, HookCallbackOptions, HookOrder, HookRegistry};
33//!
34//! let hooks = HookRegistry::new();
35//! hooks.add_hook(|event: &mut BeforeToolCallEvent| {
36//! if event.tool_use().name == "shell" {
37//! event.set_cancel("shell is disabled");
38//! }
39//! });
40//! hooks.add_hook_with(
41//! |event: &mut AfterToolCallEvent| event.set_retry(event.error().is_some()),
42//! HookCallbackOptions { order: HookOrder::SDK_LAST },
43//! );
44//! assert_eq!(hooks.len(), 2);
45//! ```
46
47mod events;
48mod registry;
49mod stream_events;
50mod types;
51
52pub use events::{
53 AfterInvocationEvent, AfterModelCallEvent, AfterToolCallEvent, AfterToolsEvent, AgentResultEvent,
54 BeforeInvocationEvent, BeforeModelCallEvent, BeforeToolCallEvent, BeforeToolsEvent, Cancel, ContentBlockEvent,
55 EndTurn, InitializedEvent, InterruptEvent, MessageAddedEvent, ModelMessageEvent, ModelStopResponse,
56 ModelStreamUpdateEvent, ToolResultEvent, ToolStreamUpdateEvent, ToolUseData,
57};
58pub use registry::{EventKind, Hook, HookEvent, HookOutput, HookRegistry, IntoAsyncHook, IntoHookImpl, IntoSyncHook};
59pub use stream_events::{
60 AfterInvocationEventData, AfterModelCallEventData, AfterToolCallEventData, AfterToolsEventData, AgentStreamEvent,
61 BeforeInvocationEventData, BeforeModelCallEventData, BeforeToolCallEventData, BeforeToolsEventData,
62 ContentBlockEventData, ErrorData, InitializedEventData, InterruptEventData, MessageAddedEventData,
63 ModelMessageEventData, ModelStreamUpdateEventData, ToolResultEventData, ToolStreamUpdateEventData,
64};
65pub use types::{HookCallbackOptions, HookCleanup, HookOrder};
66
67pub use crate::models::Redaction;