pub struct HookRegistry { /* private fields */ }Expand description
Registry of hook callbacks, one ordered list per event type (HookRegistry).
Callbacks run in ascending order; within one order they run in registration order,
reversed on After* events. Registration and removal take a short lock; callbacks
run outside it, so a callback may register or remove hooks.
Implementations§
Source§impl HookRegistry
impl HookRegistry
Source§impl HookRegistry
impl HookRegistry
Sourcepub fn add_hook<K: HookEvent>(
&self,
callback: impl IntoSyncHook<K>,
) -> HookCleanup
pub fn add_hook<K: HookEvent>( &self, callback: impl IntoSyncHook<K>, ) -> HookCleanup
Registers a plain closure. Annotate the argument so the event type is
inferred. The closure returns () or a Result (see HookOutput).
use strands::hooks::{BeforeInvocationEvent, BeforeToolCallEvent, HookRegistry};
let registry = HookRegistry::new();
let cleanup = registry.add_hook(|event: &mut BeforeToolCallEvent| {
if event.tool_use().name == "shell" {
event.set_cancel("shell is disabled");
}
});
cleanup.remove();
registry.add_hook(|event: &mut BeforeInvocationEvent<'_>| -> Result<(), strands::Error> {
let limit: Option<u32> = event.agent().app_state().get("limit")?;
if limit == Some(0) {
event.set_cancel("limit reached");
}
Ok(())
});Sourcepub fn add_hook_with<K: HookEvent>(
&self,
callback: impl IntoSyncHook<K>,
options: HookCallbackOptions,
) -> HookCleanup
pub fn add_hook_with<K: HookEvent>( &self, callback: impl IntoSyncHook<K>, options: HookCallbackOptions, ) -> HookCleanup
Registers a plain closure with options such as the order.
Sourcepub fn add_async_hook<K: HookEvent>(
&self,
callback: impl IntoAsyncHook<K>,
) -> HookCleanup
pub fn add_async_hook<K: HookEvent>( &self, callback: impl IntoAsyncHook<K>, ) -> HookCleanup
Registers a closure that awaits while borrowing the event. Name the event type, and return
Box::pin(async move { .. }); the block’s value is () or a Result (see
HookOutput).
use strands::BoxError;
use strands::hooks::{BeforeInvocationEvent, BeforeToolCallEvent, HookRegistry};
let registry = HookRegistry::new();
registry.add_async_hook::<BeforeToolCallEvent>(|event| {
Box::pin(async move {
tokio::task::yield_now().await;
event.set_cancel(true);
})
});
registry.add_async_hook::<BeforeInvocationEvent<'_>>(|event| {
Box::pin(async move {
let turns: Option<u32> = event.agent().app_state().get("turns")?;
if turns.is_some() {
event.agent_mut().messages_mut().clear();
}
Ok::<(), BoxError>(())
})
});Sourcepub fn add_async_hook_with<K: HookEvent>(
&self,
callback: impl IntoAsyncHook<K>,
options: HookCallbackOptions,
) -> HookCleanup
pub fn add_async_hook_with<K: HookEvent>( &self, callback: impl IntoAsyncHook<K>, options: HookCallbackOptions, ) -> HookCleanup
Registers an async closure with options such as the order.
Sourcepub fn add_hook_impl<K: HookEvent>(
&self,
hook: impl IntoHookImpl<K>,
) -> HookCleanup
pub fn add_hook_impl<K: HookEvent>( &self, hook: impl IntoHookImpl<K>, ) -> HookCleanup
Registers a Hook implementation.
Sourcepub fn add_hook_impl_with<K: HookEvent>(
&self,
hook: impl IntoHookImpl<K>,
options: HookCallbackOptions,
) -> HookCleanup
pub fn add_hook_impl_with<K: HookEvent>( &self, hook: impl IntoHookImpl<K>, options: HookCallbackOptions, ) -> HookCleanup
Registers a Hook implementation with options such as the order.
Trait Implementations§
Source§impl Debug for HookRegistry
impl Debug for HookRegistry
Auto Trait Implementations§
impl !Freeze for HookRegistry
impl RefUnwindSafe for HookRegistry
impl Send for HookRegistry
impl Sync for HookRegistry
impl Unpin for HookRegistry
impl UnsafeUnpin for HookRegistry
impl UnwindSafe for HookRegistry
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
§impl<T> FutureExt for T
impl<T> FutureExt for T
§fn with_context(self, otel_cx: Context) -> WithContext<Self> ⓘ
fn with_context(self, otel_cx: Context) -> WithContext<Self> ⓘ
§fn with_current_context(self) -> WithContext<Self> ⓘ
fn with_current_context(self) -> WithContext<Self> ⓘ
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more