Skip to main content

Interceptor

Trait Interceptor 

Source
pub trait Interceptor<T: Interceptable>: Send + Sync {
    // Required method
    fn intercept<'a>(
        &'a self,
        input: &'a T::Input,
        next: Next<'a, T>,
    ) -> Pin<Box<dyn Future<Output = T::Output> + Send + 'a>>;
}
Expand description

Wraps an interceptable operation.

Interceptors form a chain. Each interceptor receives the input and a Next handle. It can:

  • Pass through: call next.run(input).await
  • Modify input: transform input, then call next.run(&modified).await
  • Short-circuit: return early without calling next
  • Retry: call next.clone().run(input).await multiple times
  • Wrap output: call next, then transform the result

§Implementing

use llm_stack::intercept::{Interceptor, Interceptable, Next};
use std::future::Future;
use std::pin::Pin;

struct MyInterceptor;

impl<T: Interceptable> Interceptor<T> for MyInterceptor
where
    T::Input: Sync,
{
    fn intercept<'a>(
        &'a self,
        input: &'a T::Input,
        next: Next<'a, T>,
    ) -> Pin<Box<dyn Future<Output = T::Output> + Send + 'a>> {
        Box::pin(async move {
            // Do something before
            let result = next.run(input).await;
            // Do something after
            result
        })
    }
}

Required Methods§

Source

fn intercept<'a>( &'a self, input: &'a T::Input, next: Next<'a, T>, ) -> Pin<Box<dyn Future<Output = T::Output> + Send + 'a>>

Intercept the operation.

Call next.run(input) to continue the chain, or return early to short-circuit.

Implementors§

Source§

impl<Ctx, F> Interceptor<ToolExec<Ctx>> for Approval<F>
where Ctx: Send + Sync + 'static, F: Fn(&ToolRequest) -> ApprovalDecision + Send + Sync,

Source§

impl<T> Interceptor<T> for Logging

Available on crate feature tracing only.
Source§

impl<T> Interceptor<T> for NoOp
where T: Interceptable, T::Input: Sync,

Source§

impl<T> Interceptor<T> for Retry

Source§

impl<T> Interceptor<T> for Timeout