pub trait AgentMiddleware: Send + Sync {
// Required methods
fn id(&self) -> &'static str;
fn modify_model_request<'life0, 'life1, 'life2, 'async_trait>(
&'life0 self,
ctx: &'life1 mut MiddlewareContext<'life2>,
) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>
where Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait;
// Provided methods
fn tools(&self) -> Vec<ToolBox> ⓘ { ... }
fn before_tool_execution<'life0, 'life1, 'life2, 'life3, 'async_trait>(
&'life0 self,
_tool_name: &'life1 str,
_tool_args: &'life2 Value,
_call_id: &'life3 str,
) -> Pin<Box<dyn Future<Output = Result<Option<AgentInterrupt>>> + Send + 'async_trait>>
where Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
'life3: 'async_trait { ... }
}
Expand description
Middleware hook that can register additional tools and mutate the model request prior to execution. Mirrors the Python AgentMiddleware contracts but keeps the interface async-first for future network calls.
Required Methods§
Sourcefn modify_model_request<'life0, 'life1, 'life2, 'async_trait>(
&'life0 self,
ctx: &'life1 mut MiddlewareContext<'life2>,
) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
fn modify_model_request<'life0, 'life1, 'life2, 'async_trait>(
&'life0 self,
ctx: &'life1 mut MiddlewareContext<'life2>,
) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
Apply middleware-specific mutations to the pending model request.
Provided Methods§
Sourcefn before_tool_execution<'life0, 'life1, 'life2, 'life3, 'async_trait>(
&'life0 self,
_tool_name: &'life1 str,
_tool_args: &'life2 Value,
_call_id: &'life3 str,
) -> Pin<Box<dyn Future<Output = Result<Option<AgentInterrupt>>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
'life3: 'async_trait,
fn before_tool_execution<'life0, 'life1, 'life2, 'life3, 'async_trait>(
&'life0 self,
_tool_name: &'life1 str,
_tool_args: &'life2 Value,
_call_id: &'life3 str,
) -> Pin<Box<dyn Future<Output = Result<Option<AgentInterrupt>>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
'life3: 'async_trait,
Hook called before tool execution - can return an interrupt to pause execution.
This hook is invoked for each tool call before it executes, allowing middleware to intercept and pause execution for human review. If an interrupt is returned, the agent will save its state and wait for human approval before continuing.
§Arguments
tool_name
- Name of the tool about to be executedtool_args
- Arguments that will be passed to the toolcall_id
- Unique identifier for this tool call
§Returns
Ok(Some(interrupt))
- Pause execution and wait for human responseOk(None)
- Continue with tool execution normallyErr(e)
- Error occurred during interrupt check