Skip to main content

Module hooks

Module hooks 

Source
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_hook takes a plain closure; annotate its argument so the event type is inferred.
  • add_async_hook::<E> takes a closure that returns Box::pin(async move { .. }), for hooks that await while borrowing the event.
  • add_hook_impl takes a Hook implementation.

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§

AfterInvocationEvent
The request ended, successfully or not (AfterInvocationEvent). Callbacks with the same order run in reverse registration order.
AfterInvocationEventData
Record of an AfterInvocationEvent: {"type":"afterInvocationEvent"}.
AfterModelCallEvent
A model call finished, successfully or not (AfterModelCallEvent). Callbacks with the same order run in reverse registration order.
AfterModelCallEventData
Record of an AfterModelCallEvent.
AfterToolCallEvent
A tool call finished, successfully or not (AfterToolCallEvent). Callbacks with the same order run in reverse registration order.
AfterToolCallEventData
Record of an AfterToolCallEvent.
AfterToolsEvent
Every tool of the batch finished (AfterToolsEvent). Callbacks with the same order run in reverse registration order.
AfterToolsEventData
Record of an AfterToolsEvent.
AgentResultEvent
The final event of an invocation, with its result (AgentResultEvent).
BeforeInvocationEvent
A new request is starting, before any model call or tool runs (BeforeInvocationEvent).
BeforeInvocationEventData
Record of a BeforeInvocationEvent: {"type":"beforeInvocationEvent"}.
BeforeModelCallEvent
A model call is about to start (BeforeModelCallEvent).
BeforeModelCallEventData
Record of a BeforeModelCallEvent.
BeforeToolCallEvent
A tool is about to run, after its lookup (BeforeToolCallEvent).
BeforeToolCallEventData
Record of a BeforeToolCallEvent.
BeforeToolsEvent
The model asked for tools, before any of them runs (BeforeToolsEvent).
BeforeToolsEventData
Record of a BeforeToolsEvent.
ContentBlockEvent
A content block the model finished (ContentBlockEvent).
ContentBlockEventData
Record of a ContentBlockEvent.
ErrorData
The message of an error on an event record.
HookCallbackOptions
Options for registering a hook (HookCallbackOptions).
HookCleanup
Removes a registered hook (HookCleanup).
HookRegistry
Registry of hook callbacks, one ordered list per event type (HookRegistry).
InitializedEvent
The agent finished construction and initialized its built-in components (InitializedEvent).
InitializedEventData
Record of an InitializedEvent: {"type":"initializedEvent"}. Not a stream item; the event fires at construction.
InterruptEvent
The agent stopped for an unanswered interrupt; one event fires per interrupt (InterruptEvent).
InterruptEventData
Record of an InterruptEvent.
MessageAddedEvent
The framework added a message to the history (MessageAddedEvent).
MessageAddedEventData
Record of a MessageAddedEvent.
ModelMessageEvent
The model finished a message (ModelMessageEvent).
ModelMessageEventData
Record of a ModelMessageEvent.
ModelStopResponse
What a model call returned (ModelStopResponse).
ModelStreamUpdateEvent
One streaming event from the model (ModelStreamUpdateEvent).
ModelStreamUpdateEventData
Record of a ModelStreamUpdateEvent.
ToolResultEvent
A tool call’s final result is ready (ToolResultEvent).
ToolResultEventData
Record of a ToolResultEvent.
ToolStreamUpdateEvent
A tool yielded a progress event (ToolStreamUpdateEvent).
ToolStreamUpdateEventData
Record of a ToolStreamUpdateEvent.

Enums§

AgentStreamEvent
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.
HookOrder
Presets for hook execution order (HookOrder). Lower values run first.

Traits§

Hook
A reusable or stateful hook (HookCallback as an object).
HookEvent
A hook event type, as the registry keys it (sealed).
HookOutput
What a hook returns (sealed): (), or Result<(), E> for any E: Into<BoxError>.
IntoAsyncHook
An async closure usable as a hook for the event K: it returns Box::pin(async move { .. }) whose output is a HookOutput.
IntoHookImpl
A Hook implementation usable for the event K, sealed.
IntoSyncHook
A plain closure usable as a hook for the event K (HookCallback), sealed.

Type Aliases§

ToolUseData
The tool-use descriptor on tool-call events (ToolUseData): the shape of the model’s tool use.