RetryMiddleware

Struct RetryMiddleware 

Source
pub struct RetryMiddleware { /* private fields */ }
Expand description

Advisory middleware that tracks errors and provides retry guidance.

The Middleware trait hooks are lifecycle callbacks, not control-flow points. RetryMiddleware counts errors via Middleware::on_error and exposes a RetryMiddleware::should_retry method the caller can query to decide whether to re-invoke the agent.

§Examples

use std::sync::Arc;
use gemini_adk_rs::middleware::RetryMiddleware;

let retry = Arc::new(RetryMiddleware::new(3));
// ... run agent, on_error is called by the middleware chain ...
while retry.should_retry() {
    retry.record_attempt();
    // re-run the agent
}

Implementations§

Source§

impl RetryMiddleware

Source

pub fn new(max_retries: u32) -> Self

Create a new retry middleware with the given maximum retry count.

Source

pub fn should_retry(&self) -> bool

Returns true if the number of attempts is below max_retries and at least one error has been recorded since the last reset.

Source

pub fn record_attempt(&self)

Record that a retry attempt is being made. Call this before re-invoking the agent.

Source

pub fn attempts(&self) -> u32

Returns the current attempt count (0-based).

Source

pub fn max_retries(&self) -> u32

Returns the configured maximum number of retries.

Source

pub fn reset(&self)

Reset all counters, allowing the middleware to be reused.

Trait Implementations§

Source§

impl Middleware for RetryMiddleware

Source§

fn name(&self) -> &str

Unique name for this middleware (used in logging/debugging).
Source§

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.
Source§

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.
Source§

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.
Source§

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.
Source§

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.
Source§

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.
Source§

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.
Source§

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.
Source§

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.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

§

impl<T> PolicyExt for T
where T: ?Sized,

§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns [Action::Follow] only if self and other return Action::Follow. Read more
§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns [Action::Follow] if either self or other returns Action::Follow. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V

§

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more