pub trait SessionHooks:
Send
+ Sync
+ 'static {
// Provided methods
fn on_hook<'life0, 'async_trait>(
&'life0 self,
event: HookEvent,
) -> Pin<Box<dyn Future<Output = HookOutput> + Send + 'async_trait>>
where Self: 'async_trait,
'life0: 'async_trait { ... }
fn on_pre_tool_use<'life0, 'async_trait>(
&'life0 self,
_input: PreToolUseInput,
_ctx: HookContext,
) -> Pin<Box<dyn Future<Output = Option<PreToolUseOutput>> + Send + 'async_trait>>
where Self: 'async_trait,
'life0: 'async_trait { ... }
fn on_post_tool_use<'life0, 'async_trait>(
&'life0 self,
_input: PostToolUseInput,
_ctx: HookContext,
) -> Pin<Box<dyn Future<Output = Option<PostToolUseOutput>> + Send + 'async_trait>>
where Self: 'async_trait,
'life0: 'async_trait { ... }
fn on_user_prompt_submitted<'life0, 'async_trait>(
&'life0 self,
_input: UserPromptSubmittedInput,
_ctx: HookContext,
) -> Pin<Box<dyn Future<Output = Option<UserPromptSubmittedOutput>> + Send + 'async_trait>>
where Self: 'async_trait,
'life0: 'async_trait { ... }
fn on_session_start<'life0, 'async_trait>(
&'life0 self,
_input: SessionStartInput,
_ctx: HookContext,
) -> Pin<Box<dyn Future<Output = Option<SessionStartOutput>> + Send + 'async_trait>>
where Self: 'async_trait,
'life0: 'async_trait { ... }
fn on_session_end<'life0, 'async_trait>(
&'life0 self,
_input: SessionEndInput,
_ctx: HookContext,
) -> Pin<Box<dyn Future<Output = Option<SessionEndOutput>> + Send + 'async_trait>>
where Self: 'async_trait,
'life0: 'async_trait { ... }
fn on_error_occurred<'life0, 'async_trait>(
&'life0 self,
_input: ErrorOccurredInput,
_ctx: HookContext,
) -> Pin<Box<dyn Future<Output = Option<ErrorOccurredOutput>> + Send + 'async_trait>>
where Self: 'async_trait,
'life0: 'async_trait { ... }
}Expand description
Callback trait for session hooks — invoked by the CLI at key lifecycle points (tool use, prompt submission, session start/end, errors).
Implement this trait to intercept and modify CLI behavior at hook points. There are two styles of implementation — pick whichever fits:
- Per-hook methods (recommended). Override the specific
on_*hook methods you care about; every hook has a default that returnsNone(meaning “no hook registered, use CLI default behavior”). - Single
on_hookmethod. Override this one andmatchonHookEventyourself — useful for logging middleware or shared dispatch logic.
Hooks only fire when hooks are enabled on the session (via
SessionConfig::hooks = Some(true),
which SessionConfig::with_hooks
sets automatically).
Provided Methods§
Sourcefn on_hook<'life0, 'async_trait>(
&'life0 self,
event: HookEvent,
) -> Pin<Box<dyn Future<Output = HookOutput> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
fn on_hook<'life0, 'async_trait>(
&'life0 self,
event: HookEvent,
) -> Pin<Box<dyn Future<Output = HookOutput> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
Top-level dispatch. The default implementation fans out to the per-hook methods below; override this only if you want a single matching point across all hook types.
Sourcefn on_pre_tool_use<'life0, 'async_trait>(
&'life0 self,
_input: PreToolUseInput,
_ctx: HookContext,
) -> Pin<Box<dyn Future<Output = Option<PreToolUseOutput>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
fn on_pre_tool_use<'life0, 'async_trait>(
&'life0 self,
_input: PreToolUseInput,
_ctx: HookContext,
) -> Pin<Box<dyn Future<Output = Option<PreToolUseOutput>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
Called before a tool executes. Return Some(output) to approve/deny
or modify the call, or None (default) to pass through unchanged.
Sourcefn on_post_tool_use<'life0, 'async_trait>(
&'life0 self,
_input: PostToolUseInput,
_ctx: HookContext,
) -> Pin<Box<dyn Future<Output = Option<PostToolUseOutput>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
fn on_post_tool_use<'life0, 'async_trait>(
&'life0 self,
_input: PostToolUseInput,
_ctx: HookContext,
) -> Pin<Box<dyn Future<Output = Option<PostToolUseOutput>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
Called after a tool executes. Return Some(output) to inject
additional context or signal post-processing decisions; None
(default) means no follow-up.
Sourcefn on_user_prompt_submitted<'life0, 'async_trait>(
&'life0 self,
_input: UserPromptSubmittedInput,
_ctx: HookContext,
) -> Pin<Box<dyn Future<Output = Option<UserPromptSubmittedOutput>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
fn on_user_prompt_submitted<'life0, 'async_trait>(
&'life0 self,
_input: UserPromptSubmittedInput,
_ctx: HookContext,
) -> Pin<Box<dyn Future<Output = Option<UserPromptSubmittedOutput>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
Called when the user submits a prompt. Return Some(output) to
rewrite the prompt or inject extra context; None (default) passes
through unchanged.
Sourcefn on_session_start<'life0, 'async_trait>(
&'life0 self,
_input: SessionStartInput,
_ctx: HookContext,
) -> Pin<Box<dyn Future<Output = Option<SessionStartOutput>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
fn on_session_start<'life0, 'async_trait>(
&'life0 self,
_input: SessionStartInput,
_ctx: HookContext,
) -> Pin<Box<dyn Future<Output = Option<SessionStartOutput>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
Called at session creation or resume. Return Some(output) to
inject startup context.
Sourcefn on_session_end<'life0, 'async_trait>(
&'life0 self,
_input: SessionEndInput,
_ctx: HookContext,
) -> Pin<Box<dyn Future<Output = Option<SessionEndOutput>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
fn on_session_end<'life0, 'async_trait>(
&'life0 self,
_input: SessionEndInput,
_ctx: HookContext,
) -> Pin<Box<dyn Future<Output = Option<SessionEndOutput>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
Called when the session ends. Return Some(output) if your hook
needs to signal cleanup behavior.
Sourcefn on_error_occurred<'life0, 'async_trait>(
&'life0 self,
_input: ErrorOccurredInput,
_ctx: HookContext,
) -> Pin<Box<dyn Future<Output = Option<ErrorOccurredOutput>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
fn on_error_occurred<'life0, 'async_trait>(
&'life0 self,
_input: ErrorOccurredInput,
_ctx: HookContext,
) -> Pin<Box<dyn Future<Output = Option<ErrorOccurredOutput>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
Called when the CLI reports an error. Return Some(output) to
influence retry behavior or surface a user-facing notification.