pub struct M;Expand description
The M namespace — static factory methods for middleware.
Implementations§
Source§impl M
impl M
Sourcepub fn log() -> MiddlewareComposite
pub fn log() -> MiddlewareComposite
Add logging middleware.
Sourcepub fn latency() -> MiddlewareComposite
pub fn latency() -> MiddlewareComposite
Add latency tracking middleware.
Sourcepub fn timeout(duration: Duration) -> MiddlewareComposite
pub fn timeout(duration: Duration) -> MiddlewareComposite
Bound the agent run to duration. The text agent enforces the tightest
timeout across its middleware chain by wrapping the whole run; on elapse
it emits AgentEvent::Timeout and returns an error.
Sourcepub fn retry(max_retries: u32) -> MiddlewareComposite
pub fn retry(max_retries: u32) -> MiddlewareComposite
Add retry middleware — tracks errors and advises on retry.
Sourcepub fn tap(
f: impl Fn(&AgentEvent) + Send + Sync + 'static,
) -> MiddlewareComposite
pub fn tap( f: impl Fn(&AgentEvent) + Send + Sync + 'static, ) -> MiddlewareComposite
Add a custom event observer — called on every agent event.
Sourcepub fn before_tool(
f: impl Fn(&FunctionCall) -> Result<(), String> + Send + Sync + 'static,
) -> MiddlewareComposite
pub fn before_tool( f: impl Fn(&FunctionCall) -> Result<(), String> + Send + Sync + 'static, ) -> MiddlewareComposite
Add a custom before-tool filter — called before every tool invocation.
Sourcepub fn after_tool(
f: impl Fn(&FunctionCall, &Value) -> Result<(), String> + Send + Sync + 'static,
) -> MiddlewareComposite
pub fn after_tool( f: impl Fn(&FunctionCall, &Value) -> Result<(), String> + Send + Sync + 'static, ) -> MiddlewareComposite
Add a custom after-tool hook — called after every successful tool invocation.
Sourcepub fn on_error(
f: impl Fn(&AgentError) -> Result<(), String> + Send + Sync + 'static,
) -> MiddlewareComposite
pub fn on_error( f: impl Fn(&AgentError) -> Result<(), String> + Send + Sync + 'static, ) -> MiddlewareComposite
Add a custom error observer — called when an agent-level error occurs.
Sourcepub fn cost() -> MiddlewareComposite
pub fn cost() -> MiddlewareComposite
Add cost tracking middleware — records token usage estimates.
Sourcepub fn rate_limit(rps: u32) -> MiddlewareComposite
pub fn rate_limit(rps: u32) -> MiddlewareComposite
Add rate-limiting middleware — spaces tool calls to at most rps per
second by delaying before_tool (concurrent calls queue rather than
burst).
Sourcepub fn circuit_breaker(threshold: u32) -> MiddlewareComposite
pub fn circuit_breaker(threshold: u32) -> MiddlewareComposite
Add circuit breaker middleware — opens after consecutive failures.
Sourcepub fn trace() -> MiddlewareComposite
pub fn trace() -> MiddlewareComposite
Add tracing span middleware — creates spans for distributed tracing.
Sourcepub fn audit() -> MiddlewareComposite
pub fn audit() -> MiddlewareComposite
Add audit middleware — records all tool calls for review.
Sourcepub fn structured_log() -> MiddlewareComposite
pub fn structured_log() -> MiddlewareComposite
Structured logging middleware — logs agent events as structured JSON.
Sourcepub fn dispatch_log() -> MiddlewareComposite
pub fn dispatch_log() -> MiddlewareComposite
Dispatch logging middleware — logs dispatch/join events.
Sourcepub fn topology_log() -> MiddlewareComposite
pub fn topology_log() -> MiddlewareComposite
Topology logging middleware — logs agent topology events.
Sourcepub fn validate(
f: impl Fn(&FunctionCall) -> Result<(), String> + Send + Sync + 'static,
) -> MiddlewareComposite
pub fn validate( f: impl Fn(&FunctionCall) -> Result<(), String> + Send + Sync + 'static, ) -> MiddlewareComposite
Add a tool input validator middleware.
Sourcepub fn cache() -> MiddlewareComposite
pub fn cache() -> MiddlewareComposite
Response caching middleware — caches model responses to avoid redundant calls.
Sourcepub fn dedup() -> MiddlewareComposite
pub fn dedup() -> MiddlewareComposite
Deduplicate consecutive identical requests.
Sourcepub fn sample(rate: f64) -> MiddlewareComposite
pub fn sample(rate: f64) -> MiddlewareComposite
Sample/pass-through a fraction of requests (0.0–1.0).
Sourcepub fn metrics() -> MiddlewareComposite
pub fn metrics() -> MiddlewareComposite
Metrics collection middleware — tracks request counts, error counts, and latencies.
Sourcepub fn before_agent(
f: impl Fn(&InvocationContext) -> Result<(), String> + Send + Sync + 'static,
) -> MiddlewareComposite
pub fn before_agent( f: impl Fn(&InvocationContext) -> Result<(), String> + Send + Sync + 'static, ) -> MiddlewareComposite
Shortcut for a before-agent hook.
Sourcepub fn after_agent(
f: impl Fn(&InvocationContext) -> Result<(), String> + Send + Sync + 'static,
) -> MiddlewareComposite
pub fn after_agent( f: impl Fn(&InvocationContext) -> Result<(), String> + Send + Sync + 'static, ) -> MiddlewareComposite
Shortcut for an after-agent hook.
Sourcepub fn before_model(
f: impl Fn(&LlmRequest) -> Result<(), String> + Send + Sync + 'static,
) -> MiddlewareComposite
pub fn before_model( f: impl Fn(&LlmRequest) -> Result<(), String> + Send + Sync + 'static, ) -> MiddlewareComposite
Shortcut for a before-model hook.
Sourcepub fn after_model(
f: impl Fn(&LlmRequest, &LlmResponse) -> Result<(), String> + Send + Sync + 'static,
) -> MiddlewareComposite
pub fn after_model( f: impl Fn(&LlmRequest, &LlmResponse) -> Result<(), String> + Send + Sync + 'static, ) -> MiddlewareComposite
Shortcut for an after-model hook.
Sourcepub fn on_loop(f: impl Fn(u32) + Send + Sync + 'static) -> MiddlewareComposite
pub fn on_loop(f: impl Fn(u32) + Send + Sync + 'static) -> MiddlewareComposite
Loop iteration event hook — called on each iteration of a loop agent.
Sourcepub fn on_timeout(f: impl Fn() + Send + Sync + 'static) -> MiddlewareComposite
pub fn on_timeout(f: impl Fn() + Send + Sync + 'static) -> MiddlewareComposite
Timeout event hook — called when an agent times out.
Sourcepub fn on_route(f: impl Fn(&str) + Send + Sync + 'static) -> MiddlewareComposite
pub fn on_route(f: impl Fn(&str) + Send + Sync + 'static) -> MiddlewareComposite
Route decision event hook — called when a route agent selects a branch.
Sourcepub fn on_fallback(
f: impl Fn(&str) + Send + Sync + 'static,
) -> MiddlewareComposite
pub fn on_fallback( f: impl Fn(&str) + Send + Sync + 'static, ) -> MiddlewareComposite
Fallback event hook — called when a fallback agent activates.
Auto Trait Implementations§
impl Freeze for M
impl RefUnwindSafe for M
impl Send for M
impl Sync for M
impl Unpin for M
impl UnwindSafe for M
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
§impl<S> FromSample<S> for S
impl<S> FromSample<S> for S
fn from_sample_(s: S) -> S
§impl<T> FutureExt for T
impl<T> FutureExt for T
§fn with_context(self, otel_cx: Context) -> WithContext<Self>
fn with_context(self, otel_cx: Context) -> WithContext<Self>
§fn with_current_context(self) -> WithContext<Self>
fn with_current_context(self) -> WithContext<Self>
§impl<T> FutureExt for T
impl<T> FutureExt for T
§fn with_context(self, otel_cx: Context) -> WithContext<Self>
fn with_context(self, otel_cx: Context) -> WithContext<Self>
§fn with_current_context(self) -> WithContext<Self>
fn with_current_context(self) -> WithContext<Self>
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
T in a tonic::Request§impl<L> LayerExt<L> for L
impl<L> LayerExt<L> for L
§fn named_layer<S>(&self, service: S) -> Layered<<L as Layer<S>>::Service, S>where
L: Layer<S>,
fn named_layer<S>(&self, service: S) -> Layered<<L as Layer<S>>::Service, S>where
L: Layer<S>,
Layered].