Skip to main content

RuntimeLayer

Trait RuntimeLayer 

Source
pub trait RuntimeLayer: Send + Sync {
    // Provided methods
    fn before_chat<'a>(
        &'a self,
        _ctx: &'a LayerContext<'_>,
        _messages: &'a [Message],
        _tools: &'a [ToolDefinition],
    ) -> Pin<Box<dyn Future<Output = Option<ChatResponse>> + Send + 'a>> { ... }
    fn after_chat<'a>(
        &'a self,
        _ctx: &'a LayerContext<'_>,
        _response: &'a ChatResponse,
    ) -> Pin<Box<dyn Future<Output = ()> + Send + 'a>> { ... }
    fn before_tool<'a>(
        &'a self,
        _ctx: &'a LayerContext<'_>,
        _call: &'a ToolCall,
    ) -> Pin<Box<dyn Future<Output = BeforeToolResult> + Send + 'a>> { ... }
    fn after_tool<'a>(
        &'a self,
        _ctx: &'a LayerContext<'_>,
        _call: &'a ToolCall,
        _result: &'a Result<Option<ToolOutput>, ToolError>,
    ) -> Pin<Box<dyn Future<Output = ()> + Send + 'a>> { ... }
}
Expand description

Middleware layer that wraps LLM calls and tool dispatch.

Layers are composed in a stack; each layer may inspect, modify, or short-circuit the request before passing it to the next layer (or the real executor).

All methods have default implementations that are no-ops, so implementors only need to override the hooks they care about.

§Short-circuiting

Returning Some(result) from before_chat or before_tool bypasses the actual LLM call or tool execution. Subsequent layers are still called with after_chat / after_tool using the short-circuit result.

Provided Methods§

Source

fn before_chat<'a>( &'a self, _ctx: &'a LayerContext<'_>, _messages: &'a [Message], _tools: &'a [ToolDefinition], ) -> Pin<Box<dyn Future<Output = Option<ChatResponse>> + Send + 'a>>

Called before an LLM chat call.

Return Some(response) to short-circuit the actual LLM call. Return None to proceed normally.

Source

fn after_chat<'a>( &'a self, _ctx: &'a LayerContext<'_>, _response: &'a ChatResponse, ) -> Pin<Box<dyn Future<Output = ()> + Send + 'a>>

Called after an LLM chat call completes (or was short-circuited).

Source

fn before_tool<'a>( &'a self, _ctx: &'a LayerContext<'_>, _call: &'a ToolCall, ) -> Pin<Box<dyn Future<Output = BeforeToolResult> + Send + 'a>>

Called before tool execution.

Return Some(result) to short-circuit the actual tool execution. Return None to proceed normally.

Source

fn after_tool<'a>( &'a self, _ctx: &'a LayerContext<'_>, _call: &'a ToolCall, _result: &'a Result<Option<ToolOutput>, ToolError>, ) -> Pin<Box<dyn Future<Output = ()> + Send + 'a>>

Called after tool execution completes (or was short-circuited).

Implementors§