Skip to main content

Middleware

Trait Middleware 

Source
pub trait Middleware: Send + Sync {
    // Required method
    fn name(&self) -> &str;

    // Provided methods
    fn before_model<'life0, 'life1, 'life2, 'life3, 'async_trait>(
        &'life0 self,
        _ctx: &'life1 MiddlewareCtx<'life2>,
        _req: &'life3 mut LLMRequest,
    ) -> Pin<Box<dyn Future<Output = Result<(), String>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait,
             'life2: 'async_trait,
             'life3: 'async_trait { ... }
    fn after_model<'life0, 'life1, 'life2, 'life3, 'async_trait>(
        &'life0 self,
        _ctx: &'life1 MiddlewareCtx<'life2>,
        _msg: &'life3 mut Message,
    ) -> Pin<Box<dyn Future<Output = Result<(), String>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait,
             'life2: 'async_trait,
             'life3: 'async_trait { ... }
    fn before_tool<'life0, 'life1, 'life2, 'life3, 'async_trait>(
        &'life0 self,
        _ctx: &'life1 MiddlewareCtx<'life2>,
        _call: &'life3 ToolCall,
    ) -> Pin<Box<dyn Future<Output = Result<ToolDecision, String>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait,
             'life2: 'async_trait,
             'life3: 'async_trait { ... }
    fn after_tool<'life0, 'life1, 'life2, 'life3, 'life4, 'async_trait>(
        &'life0 self,
        _ctx: &'life1 MiddlewareCtx<'life2>,
        _call: &'life3 ToolCall,
        _result: &'life4 mut ContentBlock,
    ) -> Pin<Box<dyn Future<Output = Result<(), String>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait,
             'life2: 'async_trait,
             'life3: 'async_trait,
             'life4: 'async_trait { ... }
}
Expand description

中间件钩子。所有方法都有默认空实现,按需覆盖。

返回 Err 表示中间件自身出错,会被运行时当作一次失败(kind = "middleware")处理。

Required Methods§

Source

fn name(&self) -> &str

Provided Methods§

Source

fn before_model<'life0, 'life1, 'life2, 'life3, 'async_trait>( &'life0 self, _ctx: &'life1 MiddlewareCtx<'life2>, _req: &'life3 mut LLMRequest, ) -> Pin<Box<dyn Future<Output = Result<(), String>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait, 'life3: 'async_trait,

模型调用前:可改写即将发给 provider 的请求。

Source

fn after_model<'life0, 'life1, 'life2, 'life3, 'async_trait>( &'life0 self, _ctx: &'life1 MiddlewareCtx<'life2>, _msg: &'life3 mut Message, ) -> Pin<Box<dyn Future<Output = Result<(), String>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait, 'life3: 'async_trait,

模型产出 assistant 消息后、落库前:可改写该消息。

Source

fn before_tool<'life0, 'life1, 'life2, 'life3, 'async_trait>( &'life0 self, _ctx: &'life1 MiddlewareCtx<'life2>, _call: &'life3 ToolCall, ) -> Pin<Box<dyn Future<Output = Result<ToolDecision, String>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait, 'life3: 'async_trait,

工具执行前:放行或短路。

Source

fn after_tool<'life0, 'life1, 'life2, 'life3, 'life4, 'async_trait>( &'life0 self, _ctx: &'life1 MiddlewareCtx<'life2>, _call: &'life3 ToolCall, _result: &'life4 mut ContentBlock, ) -> Pin<Box<dyn Future<Output = Result<(), String>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait, 'life3: 'async_trait, 'life4: 'async_trait,

工具执行后、结果落库前:可改写该 ToolResult 块。

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§