Expand description
Hooks: the 17 hook events, the typed HookRegistry and the stream records
(AgentStreamEvent).
Register hooks on an agent, a LocalAgent or a registry, in three
forms:
add_hooktakes a plain closure; annotate its argument so the event type is inferred.add_async_hook::<E>takes a closure that returnsBox::pin(async move { .. }), for hooks that await while borrowing the event.add_hook_impltakes aHookimplementation.
Each returns a HookCleanup; the _with variants take HookCallbackOptions with the
HookOrder.
§Errors and panics
A hook returns () or Result<(), E> for any E: Into<BoxError> (HookOutput); a
Hook implementation returns Result<(), BoxError>. An error fails the invocation with it:
the callbacks after it do not run, and in a tool call it stops the
batch once AfterToolsEvent has fired. An InterruptError, such as
the one BeforeToolCallEvent::interrupt returns, is not a failure: the agent stops with
StopReason::Interrupt once every callback has run.
A panicking hook is a bug and propagates to the caller of invoke or the stream’s consumer,
also when it runs on a tool call’s task. Only a panic inside a tool’s body becomes an error
result the model sees.
use strands::hooks::{AfterToolCallEvent, BeforeToolCallEvent, HookCallbackOptions, HookOrder, HookRegistry};
let hooks = HookRegistry::new();
hooks.add_hook(|event: &mut BeforeToolCallEvent| {
if event.tool_use().name == "shell" {
event.set_cancel("shell is disabled");
}
});
hooks.add_hook_with(
|event: &mut AfterToolCallEvent| event.set_retry(event.error().is_some()),
HookCallbackOptions { order: HookOrder::SDK_LAST },
);
assert_eq!(hooks.len(), 2);Re-exports§
pub use registry::Hook;pub use registry::HookEvent;pub use registry::HookOutput;pub use registry::HookRegistry;pub use registry::IntoAsyncHook;pub use registry::IntoHookImpl;pub use registry::IntoSyncHook;pub use crate::models::Redaction;
Structs§
- After
Invocation Event - The request ended, successfully or not (
AfterInvocationEvent). Callbacks with the same order run in reverse registration order. - After
Invocation Event Data - Record of an
AfterInvocationEvent:{"type":"afterInvocationEvent"}. - After
Model Call Event - A model call finished, successfully or not (
AfterModelCallEvent). Callbacks with the same order run in reverse registration order. - After
Model Call Event Data - Record of an
AfterModelCallEvent. - After
Tool Call Event - A tool call finished, successfully or not (
AfterToolCallEvent). Callbacks with the same order run in reverse registration order. - After
Tool Call Event Data - Record of an
AfterToolCallEvent. - After
Tools Event - Every tool of the batch finished (
AfterToolsEvent). Callbacks with the same order run in reverse registration order. - After
Tools Event Data - Record of an
AfterToolsEvent. - Agent
Result Event - The final event of an invocation, with its result (
AgentResultEvent). - Before
Invocation Event - A new request is starting, before any model call or tool runs (
BeforeInvocationEvent). - Before
Invocation Event Data - Record of a
BeforeInvocationEvent:{"type":"beforeInvocationEvent"}. - Before
Model Call Event - A model call is about to start (
BeforeModelCallEvent). - Before
Model Call Event Data - Record of a
BeforeModelCallEvent. - Before
Tool Call Event - A tool is about to run, after its lookup (
BeforeToolCallEvent). - Before
Tool Call Event Data - Record of a
BeforeToolCallEvent. - Before
Tools Event - The model asked for tools, before any of them runs (
BeforeToolsEvent). - Before
Tools Event Data - Record of a
BeforeToolsEvent. - Content
Block Event - A content block the model finished (
ContentBlockEvent). - Content
Block Event Data - Record of a
ContentBlockEvent. - Error
Data - The message of an error on an event record.
- Hook
Callback Options - Options for registering a hook (
HookCallbackOptions). - Hook
Cleanup - Removes a registered hook (
HookCleanup). - Hook
Registry - Registry of hook callbacks, one ordered list per event type (
HookRegistry). - Initialized
Event - The agent finished construction and initialized its built-in components (
InitializedEvent). - Initialized
Event Data - Record of an
InitializedEvent:{"type":"initializedEvent"}. Not a stream item; the event fires at construction. - Interrupt
Event - The agent stopped for an unanswered interrupt; one event fires per interrupt
(
InterruptEvent). - Interrupt
Event Data - Record of an
InterruptEvent. - Message
Added Event - The framework added a message to the history (
MessageAddedEvent). - Message
Added Event Data - Record of a
MessageAddedEvent. - Model
Message Event - The model finished a message (
ModelMessageEvent). - Model
Message Event Data - Record of a
ModelMessageEvent. - Model
Stop Response - What a model call returned (
ModelStopResponse). - Model
Stream Update Event - One streaming event from the model (
ModelStreamUpdateEvent). - Model
Stream Update Event Data - Record of a
ModelStreamUpdateEvent. - Tool
Result Event - A tool call’s final result is ready (
ToolResultEvent). - Tool
Result Event Data - Record of a
ToolResultEvent. - Tool
Stream Update Event - A tool yielded a progress event (
ToolStreamUpdateEvent). - Tool
Stream Update Event Data - Record of a
ToolStreamUpdateEvent.
Enums§
- Agent
Stream Event - One item of an agent stream (
AgentStreamEvent): the record of a hook event. - Cancel
- A hook’s request to cancel an operation.
- EndTurn
- A hook’s request to end the turn after a tool batch.
- Hook
Order - Presets for hook execution order (
HookOrder). Lower values run first.
Traits§
- Hook
- A reusable or stateful hook (
HookCallbackas an object). - Hook
Event - A hook event type, as the registry keys it (sealed).
- Hook
Output - What a hook returns (sealed):
(), orResult<(), E>for anyE: Into<BoxError>. - Into
Async Hook - An async closure usable as a hook for the event
K: it returnsBox::pin(async move { .. })whose output is aHookOutput. - Into
Hook Impl - A
Hookimplementation usable for the eventK, sealed. - Into
Sync Hook - A plain closure usable as a hook for the event
K(HookCallback), sealed.
Type Aliases§
- Tool
UseData - The tool-use descriptor on tool-call events (
ToolUseData): the shape of the model’s tool use.