pub trait Middleware:
Send
+ Sync
+ 'static {
// Required method
fn name(&self) -> &str;
// Provided methods
fn before_agent<'life0, 'life1, 'async_trait>(
&'life0 self,
_ctx: &'life1 InvocationContext,
) -> Pin<Box<dyn Future<Output = Result<(), AgentError>> + Send + 'async_trait>>
where Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait { ... }
fn after_agent<'life0, 'life1, 'async_trait>(
&'life0 self,
_ctx: &'life1 InvocationContext,
) -> Pin<Box<dyn Future<Output = Result<(), AgentError>> + Send + 'async_trait>>
where Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait { ... }
fn before_tool<'life0, 'life1, 'async_trait>(
&'life0 self,
_call: &'life1 FunctionCall,
) -> Pin<Box<dyn Future<Output = Result<(), AgentError>> + Send + 'async_trait>>
where Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait { ... }
fn after_tool<'life0, 'life1, 'life2, 'async_trait>(
&'life0 self,
_call: &'life1 FunctionCall,
_result: &'life2 Value,
) -> Pin<Box<dyn Future<Output = Result<(), AgentError>> + Send + 'async_trait>>
where Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait { ... }
fn on_tool_error<'life0, 'life1, 'life2, 'async_trait>(
&'life0 self,
_call: &'life1 FunctionCall,
_err: &'life2 ToolError,
) -> Pin<Box<dyn Future<Output = Result<(), AgentError>> + Send + 'async_trait>>
where Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait { ... }
fn on_event<'life0, 'life1, 'async_trait>(
&'life0 self,
_event: &'life1 AgentEvent,
) -> Pin<Box<dyn Future<Output = Result<(), AgentError>> + Send + 'async_trait>>
where Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait { ... }
fn on_error<'life0, 'life1, 'async_trait>(
&'life0 self,
_err: &'life1 AgentError,
) -> Pin<Box<dyn Future<Output = Result<(), AgentError>> + Send + 'async_trait>>
where Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait { ... }
fn before_model<'life0, 'life1, 'async_trait>(
&'life0 self,
_request: &'life1 LlmRequest,
) -> Pin<Box<dyn Future<Output = Result<Option<LlmResponse>, AgentError>> + Send + 'async_trait>>
where Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait { ... }
fn after_model<'life0, 'life1, 'life2, 'async_trait>(
&'life0 self,
_request: &'life1 LlmRequest,
_response: &'life2 LlmResponse,
) -> Pin<Box<dyn Future<Output = Result<Option<LlmResponse>, AgentError>> + Send + 'async_trait>>
where Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait { ... }
fn transform_request<'life0, 'life1, 'async_trait>(
&'life0 self,
_request: &'life1 mut LlmRequest,
) -> Pin<Box<dyn Future<Output = Result<(), AgentError>> + Send + 'async_trait>>
where Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait { ... }
fn timeout(&self) -> Option<Duration> { ... }
}Expand description
Middleware hooks — all optional, implement only what you need.
§Examples
use async_trait::async_trait;
use gemini_adk_rs::middleware::Middleware;
use gemini_adk_rs::error::AgentError;
use gemini_genai_rs::prelude::FunctionCall;
struct AuditMiddleware;
#[async_trait]
impl Middleware for AuditMiddleware {
fn name(&self) -> &str { "audit" }
async fn before_tool(&self, call: &FunctionCall) -> Result<(), AgentError> {
println!("Calling tool: {}", call.name);
Ok(())
}
}Required Methods§
Provided Methods§
Sourcefn before_agent<'life0, 'life1, 'async_trait>(
&'life0 self,
_ctx: &'life1 InvocationContext,
) -> Pin<Box<dyn Future<Output = Result<(), AgentError>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
fn before_agent<'life0, 'life1, 'async_trait>(
&'life0 self,
_ctx: &'life1 InvocationContext,
) -> Pin<Box<dyn Future<Output = Result<(), AgentError>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
Called before an agent begins execution.
Sourcefn after_agent<'life0, 'life1, 'async_trait>(
&'life0 self,
_ctx: &'life1 InvocationContext,
) -> Pin<Box<dyn Future<Output = Result<(), AgentError>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
fn after_agent<'life0, 'life1, 'async_trait>(
&'life0 self,
_ctx: &'life1 InvocationContext,
) -> Pin<Box<dyn Future<Output = Result<(), AgentError>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
Called after an agent completes execution.
Sourcefn before_tool<'life0, 'life1, 'async_trait>(
&'life0 self,
_call: &'life1 FunctionCall,
) -> Pin<Box<dyn Future<Output = Result<(), AgentError>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
fn before_tool<'life0, 'life1, 'async_trait>(
&'life0 self,
_call: &'life1 FunctionCall,
) -> Pin<Box<dyn Future<Output = Result<(), AgentError>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
Called before a tool is invoked.
Sourcefn after_tool<'life0, 'life1, 'life2, 'async_trait>(
&'life0 self,
_call: &'life1 FunctionCall,
_result: &'life2 Value,
) -> Pin<Box<dyn Future<Output = Result<(), AgentError>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
fn after_tool<'life0, 'life1, 'life2, 'async_trait>(
&'life0 self,
_call: &'life1 FunctionCall,
_result: &'life2 Value,
) -> Pin<Box<dyn Future<Output = Result<(), AgentError>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
Called after a tool completes successfully.
Sourcefn on_tool_error<'life0, 'life1, 'life2, 'async_trait>(
&'life0 self,
_call: &'life1 FunctionCall,
_err: &'life2 ToolError,
) -> Pin<Box<dyn Future<Output = Result<(), AgentError>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
fn on_tool_error<'life0, 'life1, 'life2, 'async_trait>(
&'life0 self,
_call: &'life1 FunctionCall,
_err: &'life2 ToolError,
) -> Pin<Box<dyn Future<Output = Result<(), AgentError>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
Called when a tool execution fails.
Sourcefn on_event<'life0, 'life1, 'async_trait>(
&'life0 self,
_event: &'life1 AgentEvent,
) -> Pin<Box<dyn Future<Output = Result<(), AgentError>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
fn on_event<'life0, 'life1, 'async_trait>(
&'life0 self,
_event: &'life1 AgentEvent,
) -> Pin<Box<dyn Future<Output = Result<(), AgentError>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
Called when an agent event is emitted.
Sourcefn on_error<'life0, 'life1, 'async_trait>(
&'life0 self,
_err: &'life1 AgentError,
) -> Pin<Box<dyn Future<Output = Result<(), AgentError>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
fn on_error<'life0, 'life1, 'async_trait>(
&'life0 self,
_err: &'life1 AgentError,
) -> Pin<Box<dyn Future<Output = Result<(), AgentError>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
Called when an agent error occurs.
Sourcefn before_model<'life0, 'life1, 'async_trait>(
&'life0 self,
_request: &'life1 LlmRequest,
) -> Pin<Box<dyn Future<Output = Result<Option<LlmResponse>, AgentError>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
fn before_model<'life0, 'life1, 'async_trait>(
&'life0 self,
_request: &'life1 LlmRequest,
) -> Pin<Box<dyn Future<Output = Result<Option<LlmResponse>, AgentError>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
Called before an LLM model call is made. Return Some(LlmResponse) to skip the LLM call
and use the returned response instead (e.g., for caching, guardrails). Return None to proceed.
Sourcefn after_model<'life0, 'life1, 'life2, 'async_trait>(
&'life0 self,
_request: &'life1 LlmRequest,
_response: &'life2 LlmResponse,
) -> Pin<Box<dyn Future<Output = Result<Option<LlmResponse>, AgentError>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
fn after_model<'life0, 'life1, 'life2, 'async_trait>(
&'life0 self,
_request: &'life1 LlmRequest,
_response: &'life2 LlmResponse,
) -> Pin<Box<dyn Future<Output = Result<Option<LlmResponse>, AgentError>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
Called after an LLM model call completes. Return Some(LlmResponse) to replace the model’s
response (e.g., for output filtering, safety). Return None to use the original response.
Sourcefn transform_request<'life0, 'life1, 'async_trait>(
&'life0 self,
_request: &'life1 mut LlmRequest,
) -> Pin<Box<dyn Future<Output = Result<(), AgentError>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
fn transform_request<'life0, 'life1, 'async_trait>(
&'life0 self,
_request: &'life1 mut LlmRequest,
) -> Pin<Box<dyn Future<Output = Result<(), AgentError>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
Called with the fully-built request before it is sent to the model,
allowing in-place mutation (e.g. trimming or rewriting conversation
history). Runs ahead of Middleware::before_model. This mirrors the
mutable before_model_callback request hook in the ADK Python SDK.