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).awaitmultiple times - Wrap output: call
next, then transform the result
§Implementing
ⓘ
use llm_stack_core::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
})
}
}