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§
Provided Methods§
Sourcefn on_error(&self) -> OnError
fn on_error(&self) -> OnError
What to do when this handler fails. Defaults to OnError::Throw.
Sourcefn 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_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,
Sourcefn 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 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,
Sourcefn 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 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,
Sourcefn 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 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,
Sourcefn 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,
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,
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.
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.
fn name(&self) -> &str
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,
Implementors§
impl InterventionHandler for CedarAuthorization
cedar only.impl InterventionHandler for DogwoodAuthorization
dogwood only.