Skip to main content

wrap_model_call

Attribute Macro wrap_model_call 

Source
#[wrap_model_call]
Expand description

Middleware: wrap the model call with custom logic.

The decorated async function must accept ModelRequest and &dyn ModelCaller, returning Result<ModelResponse, SynapticError>. This enables retry, fallback, and other wrapping patterns.

§Example

use synaptic_macros::wrap_model_call;
use synaptic_middleware::{ModelRequest, ModelResponse, ModelCaller};
use synaptic_core::SynapticError;

#[wrap_model_call]
async fn retry_model(request: ModelRequest, next: &dyn ModelCaller) -> Result<ModelResponse, SynapticError> {
    match next.call(request.clone()).await {
        Ok(r) => Ok(r),
        Err(_) => next.call(request).await,
    }
}

let mw = retry_model(); // Arc<dyn AgentMiddleware>