Skip to main content

Hooks

Struct Hooks 

Source
pub struct Hooks {
    pub pre_tool_use: Vec<Arc<dyn Fn(PreToolUseEvent) -> Pin<Box<dyn Future<Output = Option<HookDecision>> + Send>> + Send + Sync>>,
    pub post_tool_use: Vec<Arc<dyn Fn(PostToolUseEvent) -> Pin<Box<dyn Future<Output = Option<HookDecision>> + Send>> + Send + Sync>>,
    pub user_prompt_submit: Vec<Arc<dyn Fn(UserPromptSubmitEvent) -> Pin<Box<dyn Future<Output = Option<HookDecision>> + Send>> + Send + Sync>>,
}
Expand description

An ordered registry of asynchronous lifecycle hooks.

The first handler returning Some(HookDecision) determines the result and skips later handlers. Return None to let subsequent handlers run. Clones share handlers through Arc; the three vectors also permit direct registration.

use open_agent::{Hooks, HookDecision};

let hooks = Hooks::new()
    .add_pre_tool_use(|event| async move {
        if event.tool_name == "delete_file" {
            return Some(HookDecision::block("Deletion is disabled"));
        }
        None
    })
    .add_post_tool_use(|event| async move {
        println!("Completed {}", event.tool_name);
        None
    })
    .add_user_prompt_submit(|event| async move {
        let prompt = format!("{}\nAnswer in JSON.", event.prompt);
        Some(HookDecision::modify_prompt(prompt, "Requested output format"))
    });

Fields§

§pre_tool_use: Vec<Arc<dyn Fn(PreToolUseEvent) -> Pin<Box<dyn Future<Output = Option<HookDecision>> + Send>> + Send + Sync>>

Collection of PreToolUse hook handlers, executed in registration order

§post_tool_use: Vec<Arc<dyn Fn(PostToolUseEvent) -> Pin<Box<dyn Future<Output = Option<HookDecision>> + Send>> + Send + Sync>>

Collection of PostToolUse hook handlers, executed in registration order

§user_prompt_submit: Vec<Arc<dyn Fn(UserPromptSubmitEvent) -> Pin<Box<dyn Future<Output = Option<HookDecision>> + Send>> + Send + Sync>>

Collection of UserPromptSubmit hook handlers, executed in registration order

Implementations§

Source§

impl Hooks

Source

pub fn new() -> Self

Creates an empty hook registry.

Source

pub fn add_pre_tool_use<F, Fut>(self, handler: F) -> Self
where F: Fn(PreToolUseEvent) -> Fut + Send + Sync + 'static, Fut: Future<Output = Option<HookDecision>> + Send + 'static,

Appends a pre-tool handler. See Hooks for ordering and an example.

Source

pub fn add_post_tool_use<F, Fut>(self, handler: F) -> Self
where F: Fn(PostToolUseEvent) -> Fut + Send + Sync + 'static, Fut: Future<Output = Option<HookDecision>> + Send + 'static,

Appends a post-tool handler. Return None from observers so later hooks run.

Source

pub fn add_user_prompt_submit<F, Fut>(self, handler: F) -> Self
where F: Fn(UserPromptSubmitEvent) -> Fut + Send + Sync + 'static, Fut: Future<Output = Option<HookDecision>> + Send + 'static,

Appends a prompt handler. Return a decision to block or replace the prompt.

Source

pub async fn execute_pre_tool_use( &self, event: PreToolUseEvent, ) -> Option<HookDecision>

Runs handlers in registration order, returning the first Some decision.

Each handler receives the same event snapshot. Returns None if no handler decides, including when the registry is empty.

Source

pub async fn execute_post_tool_use( &self, event: PostToolUseEvent, ) -> Option<HookDecision>

Runs post-tool handlers with the same first-decision behavior as execute_pre_tool_use.

Source

pub async fn execute_user_prompt_submit( &self, event: UserPromptSubmitEvent, ) -> Option<HookDecision>

Executes all registered UserPromptSubmit hooks in order and returns the first decision.

Identical in behavior to Self::execute_pre_tool_use but for UserPromptSubmit events. See that method for detailed documentation of the execution model.

Trait Implementations§

Source§

impl Clone for Hooks

Source§

fn clone(&self) -> Hooks

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Hooks

Shows handler counts, omitting the closures.

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Hooks

Source§

fn default() -> Hooks

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

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

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

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> 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