Skip to main content

InterventionHandler

Trait InterventionHandler 

Source
pub trait InterventionHandler:
    Send
    + Sync
    + 'static {
    // Required method
    fn name(&self) -> &str;

    // Provided methods
    fn on_error(&self) -> OnError { ... }
    fn before_invocation<'a, 'life0, 'life1, 'async_trait>(
        &'life0 self,
        _event: &'life1 mut BeforeInvocationEvent<'a>,
    ) -> Pin<Box<dyn Future<Output = InterventionResult<BeforeInvocationEvent<'a>>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'a: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait { ... }
    fn before_tool_call<'life0, 'life1, 'async_trait>(
        &'life0 self,
        _event: &'life1 mut BeforeToolCallEvent,
    ) -> Pin<Box<dyn Future<Output = InterventionResult<BeforeToolCallEvent>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait { ... }
    fn after_tool_call<'life0, 'life1, 'async_trait>(
        &'life0 self,
        _event: &'life1 mut AfterToolCallEvent,
    ) -> Pin<Box<dyn Future<Output = InterventionResult<AfterToolCallEvent>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait { ... }
    fn before_model_call<'a, 'life0, 'life1, 'async_trait>(
        &'life0 self,
        _event: &'life1 mut BeforeModelCallEvent<'a>,
    ) -> Pin<Box<dyn Future<Output = InterventionResult<BeforeModelCallEvent<'a>>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'a: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait { ... }
    fn after_model_call<'a, 'life0, 'life1, 'async_trait>(
        &'life0 self,
        _event: &'life1 mut AfterModelCallEvent<'a>,
    ) -> Pin<Box<dyn Future<Output = InterventionResult<AfterModelCallEvent<'a>>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'a: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait { ... }
}
Expand description

A policy evaluated at the agent’s lifecycle points (InterventionHandler).

Override the methods you care about; the others return Proceed. Handlers run in registration order, after the agent’s other hooks on Before* events (HookOrder::INTERVENTION_INPUT) and before them on After* events (HookOrder::INTERVENTION_OUTPUT). Tool calls made with ToolAgent::call_tool are gated too. Methods on events that borrow the agent name the lifetime, as in async fn before_model_call<'a>(&self, event: &mut BeforeModelCallEvent<'a>) -> InterventionResult<BeforeModelCallEvent<'a>>.

use strands::hooks::BeforeToolCallEvent;
use strands::interventions::{InterventionAction, InterventionHandler, InterventionResult};

struct NoShell;

#[strands::async_trait]
impl InterventionHandler for NoShell {
    fn name(&self) -> &str {
        "no-shell"
    }

    async fn before_tool_call(&self, event: &mut BeforeToolCallEvent) -> InterventionResult<BeforeToolCallEvent> {
        if event.tool_use().name == "shell" {
            return Ok(InterventionAction::deny("shell is disabled"));
        }
        Ok(InterventionAction::proceed())
    }
}

let agent = strands::Agent::builder()
    .model(strands::testing::MockModel::new())
    .interventions([NoShell])
    .printer(false)
    .build()?;

Required Methods§

Source

fn name(&self) -> &str

The handler’s name, unique among an agent’s handlers; also the name of the interrupt a Confirm raises.

Provided Methods§

Source

fn on_error(&self) -> OnError

What to do when this handler fails. Defaults to OnError::Throw.

Source

fn before_invocation<'a, 'life0, 'life1, 'async_trait>( &'life0 self, _event: &'life1 mut BeforeInvocationEvent<'a>, ) -> Pin<Box<dyn Future<Output = InterventionResult<BeforeInvocationEvent<'a>>> + Send + 'async_trait>>
where Self: 'async_trait, 'a: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Evaluates the invocation before it starts.

§Errors

Any error is resolved by on_error.

Source

fn before_tool_call<'life0, 'life1, 'async_trait>( &'life0 self, _event: &'life1 mut BeforeToolCallEvent, ) -> Pin<Box<dyn Future<Output = InterventionResult<BeforeToolCallEvent>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Evaluates a tool call before it runs.

§Errors

Any error is resolved by on_error.

Source

fn after_tool_call<'life0, 'life1, 'async_trait>( &'life0 self, _event: &'life1 mut AfterToolCallEvent, ) -> Pin<Box<dyn Future<Output = InterventionResult<AfterToolCallEvent>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Evaluates a tool result.

§Errors

Any error is resolved by on_error.

Source

fn before_model_call<'a, 'life0, 'life1, 'async_trait>( &'life0 self, _event: &'life1 mut BeforeModelCallEvent<'a>, ) -> Pin<Box<dyn Future<Output = InterventionResult<BeforeModelCallEvent<'a>>> + Send + 'async_trait>>
where Self: 'async_trait, 'a: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Evaluates a model call before it is made.

§Errors

Any error is resolved by on_error.

Source

fn after_model_call<'a, 'life0, 'life1, 'async_trait>( &'life0 self, _event: &'life1 mut AfterModelCallEvent<'a>, ) -> Pin<Box<dyn Future<Output = InterventionResult<AfterModelCallEvent<'a>>> + Send + 'async_trait>>
where Self: 'async_trait, 'a: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Evaluates a model response.

§Errors

Any error is resolved by on_error.

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementations on Foreign Types§

Source§

impl<H: InterventionHandler + ?Sized> InterventionHandler for Arc<H>

A shared handler is a handler, so Arc<dyn InterventionHandler> and Arc<H> go wherever handlers do.

Source§

fn name(&self) -> &str

Source§

fn on_error(&self) -> OnError

Source§

fn before_invocation<'a, 'life0, 'life1, 'async_trait>( &'life0 self, event: &'life1 mut BeforeInvocationEvent<'a>, ) -> Pin<Box<dyn Future<Output = InterventionResult<BeforeInvocationEvent<'a>>> + Send + 'async_trait>>
where Self: 'async_trait, 'a: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Source§

fn before_tool_call<'life0, 'life1, 'async_trait>( &'life0 self, event: &'life1 mut BeforeToolCallEvent, ) -> Pin<Box<dyn Future<Output = InterventionResult<BeforeToolCallEvent>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Source§

fn after_tool_call<'life0, 'life1, 'async_trait>( &'life0 self, event: &'life1 mut AfterToolCallEvent, ) -> Pin<Box<dyn Future<Output = InterventionResult<AfterToolCallEvent>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Source§

fn before_model_call<'a, 'life0, 'life1, 'async_trait>( &'life0 self, event: &'life1 mut BeforeModelCallEvent<'a>, ) -> Pin<Box<dyn Future<Output = InterventionResult<BeforeModelCallEvent<'a>>> + Send + 'async_trait>>
where Self: 'async_trait, 'a: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Source§

fn after_model_call<'a, 'life0, 'life1, 'async_trait>( &'life0 self, event: &'life1 mut AfterModelCallEvent<'a>, ) -> Pin<Box<dyn Future<Output = InterventionResult<AfterModelCallEvent<'a>>> + Send + 'async_trait>>
where Self: 'async_trait, 'a: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Implementors§

Source§

impl InterventionHandler for CedarAuthorization

Available on crate feature cedar only.
Source§

impl InterventionHandler for DogwoodAuthorization

Available on crate feature dogwood only.
Source§

impl InterventionHandler for HumanInTheLoop