Skip to main content

Hooks

Struct Hooks 

Source
pub struct Hooks { /* private fields */ }
Expand description

Stores and executes registered hook callbacks.

Callbacks at the same HookPoint fire in the order they were registered.

§Example

Fluent builder pattern (recommended):

use agy_bridge::hooks::{HookResult, Hooks, PreToolCallDecideContext, PreTurnContext};

let hooks = Hooks::new()
    .with_pre_turn("logger", |ctx: &PreTurnContext| {
        println!("Turn {} prompt: {}", ctx.turn_number, ctx.prompt);
    })
    .with_pre_tool_call_decide("gate", |ctx: &PreToolCallDecideContext| {
        if ctx.tool_name == "dangerous_tool" {
            HookResult::deny("blocked by policy")
        } else {
            HookResult::allow()
        }
    });

hooks.run_pre_turn(&PreTurnContext {
    prompt: "hi".into(),
    turn_number: 1,
});
let result = hooks.run_pre_tool_call_decide(&PreToolCallDecideContext {
    tool_name: "safe_tool".into(),
    tool_args: serde_json::Value::Null,
});
assert!(result.allow);

For conditional or loop-based registration, use the on_*(&mut self) methods:

let mut hooks = Hooks::new();
hooks.on_pre_turn("logger", |ctx| {
    println!("Turn {}", ctx.turn_number);
});

Implementations§

Source§

impl Hooks

Source

pub const fn new() -> Self

Create an empty hook runner.

Source

pub fn register( &mut self, name: impl Into<String>, callback: HookCallback, ) -> &mut Self

Register a named callback.

The HookPoint is derived automatically from the callback variant. If a callback with the same name AND hook point already exists, it is replaced and a warning is logged. Returns &mut Self for chaining.

Source

pub fn run_pre_turn(&self, ctx: &PreTurnContext)

Run all HookPoint::PreTurn callbacks in registration order.

Source

pub fn run_post_turn(&self, ctx: &PostTurnContext)

Run all HookPoint::PostTurn callbacks in registration order.

Source

pub fn run_pre_tool_call_decide( &self, ctx: &PreToolCallDecideContext, ) -> HookResult

Run all HookPoint::PreToolCallDecide callbacks in registration order.

If any callback returns HookResult with allow: false, execution short-circuits and that deny result is returned immediately. Otherwise returns HookResult::allow().

If a callback panics, the tool call is denied as a safe default.

Source

pub fn run_post_tool_call(&self, ctx: &PostToolCallContext)

Run all HookPoint::PostToolCall callbacks in registration order.

Source

pub fn run_on_tool_error(&self, ctx: &OnToolErrorContext)

Run all HookPoint::OnToolError callbacks in registration order.

Source

pub fn run_on_session_start(&self, ctx: &OnSessionStartContext)

Run all HookPoint::OnSessionStart callbacks in registration order.

Source

pub fn run_on_session_end(&self, ctx: &OnSessionEndContext)

Run all HookPoint::OnSessionEnd callbacks in registration order.

Source

pub fn run_on_compaction(&self, ctx: &OnCompactionContext)

Run all HookPoint::OnCompaction callbacks in registration order.

Source

pub fn run_on_interaction(&self, ctx: &OnInteractionContext) -> HookResult

Run all HookPoint::OnInteraction callbacks in registration order.

If a callback panics, the panic is logged and execution continues (the interaction is not blocked).

Source

pub fn run_transform_tool_input(&self, ctx: &PreToolCallDecideContext) -> Value

Run all TransformToolInput callbacks in registration order, threading the (possibly modified) tool arguments through each transform.

Returns the final tool arguments after all transforms have been applied. If no transform returns Some, the original arguments are returned unchanged.

Panicking transforms are logged and skipped (original args kept).

Source

pub fn on_pre_turn( &mut self, name: impl Into<String>, f: impl Fn(&PreTurnContext) + Send + Sync + 'static, ) -> &mut Self

Register a HookPoint::PreTurn callback.

Convenience wrapper matching the Python SDK’s @on_pre_turn decorator.

Source

pub fn on_post_turn( &mut self, name: impl Into<String>, f: impl Fn(&PostTurnContext) + Send + Sync + 'static, ) -> &mut Self

Register a HookPoint::PostTurn callback.

Convenience wrapper matching the Python SDK’s @on_post_turn decorator.

Source

pub fn on_pre_tool_call_decide( &mut self, name: impl Into<String>, f: impl Fn(&PreToolCallDecideContext) -> HookResult + Send + Sync + 'static, ) -> &mut Self

Register a HookPoint::PreToolCallDecide callback.

Convenience wrapper matching the Python SDK’s @on_pre_tool_call_decide decorator.

Source

pub fn on_post_tool_call( &mut self, name: impl Into<String>, f: impl Fn(&PostToolCallContext) + Send + Sync + 'static, ) -> &mut Self

Register a HookPoint::PostToolCall callback.

Convenience wrapper matching the Python SDK’s @on_post_tool_call decorator.

Source

pub fn on_tool_error( &mut self, name: impl Into<String>, f: impl Fn(&OnToolErrorContext) + Send + Sync + 'static, ) -> &mut Self

Register a HookPoint::OnToolError callback.

Convenience wrapper matching the Python SDK’s @on_tool_error decorator.

Source

pub fn on_compaction( &mut self, name: impl Into<String>, f: impl Fn(&OnCompactionContext) + Send + Sync + 'static, ) -> &mut Self

Register a HookPoint::OnCompaction callback.

Convenience wrapper matching the Python SDK’s @on_compaction decorator.

Source

pub fn on_interaction( &mut self, name: impl Into<String>, f: impl Fn(&OnInteractionContext) -> HookResult + Send + Sync + 'static, ) -> &mut Self

Register a HookPoint::OnInteraction callback.

Convenience wrapper matching the Python SDK’s @on_interaction decorator.

Source

pub fn on_session_start( &mut self, name: impl Into<String>, f: impl Fn(&OnSessionStartContext) + Send + Sync + 'static, ) -> &mut Self

Register a HookPoint::OnSessionStart callback.

Convenience wrapper matching the Python SDK’s @on_session_start decorator.

Source

pub fn on_session_end( &mut self, name: impl Into<String>, f: impl Fn(&OnSessionEndContext) + Send + Sync + 'static, ) -> &mut Self

Register a HookPoint::OnSessionEnd callback.

Convenience wrapper matching the Python SDK’s @on_session_end decorator.

Source

pub fn on_transform_tool_input( &mut self, name: impl Into<String>, f: impl Fn(&PreToolCallDecideContext) -> Option<Value> + Send + Sync + 'static, ) -> &mut Self

Register a TransformToolInput callback.

The closure receives the pre-tool-call context and may return Some(new_args) to replace tool arguments, or None to leave them unchanged.

Source

pub fn with_pre_turn( self, name: impl Into<String>, f: impl Fn(&PreTurnContext) + Send + Sync + 'static, ) -> Self

Register a HookPoint::PreTurn callback, returning self for chaining.

This is the owned-self variant of on_pre_turn.

Source

pub fn with_post_turn( self, name: impl Into<String>, f: impl Fn(&PostTurnContext) + Send + Sync + 'static, ) -> Self

Register a HookPoint::PostTurn callback, returning self for chaining.

This is the owned-self variant of on_post_turn.

Source

pub fn with_pre_tool_call_decide( self, name: impl Into<String>, f: impl Fn(&PreToolCallDecideContext) -> HookResult + Send + Sync + 'static, ) -> Self

Register a HookPoint::PreToolCallDecide callback, returning self for chaining.

This is the owned-self variant of on_pre_tool_call_decide.

Source

pub fn with_post_tool_call( self, name: impl Into<String>, f: impl Fn(&PostToolCallContext) + Send + Sync + 'static, ) -> Self

Register a HookPoint::PostToolCall callback, returning self for chaining.

This is the owned-self variant of on_post_tool_call.

Source

pub fn with_tool_error( self, name: impl Into<String>, f: impl Fn(&OnToolErrorContext) + Send + Sync + 'static, ) -> Self

Register a HookPoint::OnToolError callback, returning self for chaining.

This is the owned-self variant of on_tool_error.

Source

pub fn with_compaction( self, name: impl Into<String>, f: impl Fn(&OnCompactionContext) + Send + Sync + 'static, ) -> Self

Register a HookPoint::OnCompaction callback, returning self for chaining.

This is the owned-self variant of on_compaction.

Source

pub fn with_interaction( self, name: impl Into<String>, f: impl Fn(&OnInteractionContext) -> HookResult + Send + Sync + 'static, ) -> Self

Register a HookPoint::OnInteraction callback, returning self for chaining.

This is the owned-self variant of on_interaction.

Source

pub fn with_session_start( self, name: impl Into<String>, f: impl Fn(&OnSessionStartContext) + Send + Sync + 'static, ) -> Self

Register a HookPoint::OnSessionStart callback, returning self for chaining.

This is the owned-self variant of on_session_start.

Source

pub fn with_session_end( self, name: impl Into<String>, f: impl Fn(&OnSessionEndContext) + Send + Sync + 'static, ) -> Self

Register a HookPoint::OnSessionEnd callback, returning self for chaining.

This is the owned-self variant of on_session_end.

Source

pub fn with_transform_tool_input( self, name: impl Into<String>, f: impl Fn(&PreToolCallDecideContext) -> Option<Value> + Send + Sync + 'static, ) -> Self

Register a TransformToolInput callback, returning self for chaining.

This is the owned-self variant of on_transform_tool_input.

Source

pub fn entries(&self) -> Vec<HookEntry>

Extract a list of HookEntry objects corresponding to the registered callbacks.

This allows the AgentBuilder to automatically populate the agent’s configuration with the necessary entries to connect the Python SDK’s hook dispatcher back to the Rust runner.

Trait Implementations§

Source§

impl Default for Hooks

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl !RefUnwindSafe for Hooks

§

impl !UnwindSafe for Hooks

§

impl Freeze for Hooks

§

impl Send for Hooks

§

impl Sync for Hooks

§

impl Unpin for Hooks

§

impl UnsafeUnpin for Hooks

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> Ungil for T
where T: Send,

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more