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
impl Hooks
Sourcepub fn add_pre_tool_use<F, Fut>(self, handler: F) -> Selfwhere
F: Fn(PreToolUseEvent) -> Fut + Send + Sync + 'static,
Fut: Future<Output = Option<HookDecision>> + Send + 'static,
pub fn add_pre_tool_use<F, Fut>(self, handler: F) -> Selfwhere
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.
Sourcepub fn add_post_tool_use<F, Fut>(self, handler: F) -> Selfwhere
F: Fn(PostToolUseEvent) -> Fut + Send + Sync + 'static,
Fut: Future<Output = Option<HookDecision>> + Send + 'static,
pub fn add_post_tool_use<F, Fut>(self, handler: F) -> Selfwhere
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.
Sourcepub fn add_user_prompt_submit<F, Fut>(self, handler: F) -> Selfwhere
F: Fn(UserPromptSubmitEvent) -> Fut + Send + Sync + 'static,
Fut: Future<Output = Option<HookDecision>> + Send + 'static,
pub fn add_user_prompt_submit<F, Fut>(self, handler: F) -> Selfwhere
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.
Sourcepub async fn execute_pre_tool_use(
&self,
event: PreToolUseEvent,
) -> Option<HookDecision>
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.
Sourcepub async fn execute_post_tool_use(
&self,
event: PostToolUseEvent,
) -> Option<HookDecision>
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.
Sourcepub async fn execute_user_prompt_submit(
&self,
event: UserPromptSubmitEvent,
) -> Option<HookDecision>
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.