aidale-layer

Crates.ioaidale-layer
lib.rsaidale-layer
version0.1.0
created_at2025-11-01 04:15:10.020081+00
updated_at2025-11-01 04:15:10.020081+00
descriptionBuilt-in layers for Aidale (logging, retry, caching, etc.)
homepagehttps://github.com/hanxuanliang/aidale
repositoryhttps://github.com/hanxuanliang/aidale
max_upload_size
id1911679
size60,187
hanhotfox (hanxuanliang)

documentation

https://docs.rs/aidale

README

aidale-layer

Built-in middleware layers for Aidale (logging, retry, caching, etc.).

Crates.io Documentation License

Overview

aidale-layer provides composable middleware layers following the AOP (Aspect-Oriented Programming) pattern:

  • LoggingLayer: Request/response logging with timing
  • RetryLayer: Exponential backoff retry with jitter
  • More layers coming soon (caching, rate limiting, etc.)

Available Layers

LoggingLayer

Logs all requests and responses with timing information:

use aidale_layer::LoggingLayer;

let executor = RuntimeExecutor::builder(provider)
    .layer(LoggingLayer::new())
    .finish();

Output example:

[AI Request] model=gpt-3.5-turbo messages=2
[AI Response] duration=1.2s tokens=150

RetryLayer

Automatic retry with exponential backoff:

use aidale_layer::RetryLayer;
use std::time::Duration;

let executor = RuntimeExecutor::builder(provider)
    .layer(RetryLayer::new()
        .with_max_retries(3)
        .with_initial_delay(Duration::from_millis(100))
        .with_max_delay(Duration::from_secs(10)))
    .finish();

Features:

  • Configurable max retries
  • Exponential backoff with jitter
  • Configurable delay bounds
  • Only retries on transient errors (5xx, network errors)

Composition

Layers are composed in order from outermost to innermost:

let executor = RuntimeExecutor::builder(provider)
    .layer(LoggingLayer::new())      // Executes first (outer)
    .layer(RetryLayer::new()          // Executes second
        .with_max_retries(3))
    .finish();

Execution flow:

Request  → LoggingLayer → RetryLayer → Provider
Response ← LoggingLayer ← RetryLayer ← Provider

Zero-Cost Abstraction

Layers use compile-time composition with static dispatch:

  • No virtual dispatch (no dyn Trait)
  • No heap allocation for layer chain
  • All composition resolved at compile time
  • Type-level recursion for layer nesting

This means zero runtime overhead compared to manual implementation!

Usage

Via the main aidale crate:

[dependencies]
aidale = { version = "0.1", features = ["layers"] }

Directly:

[dependencies]
aidale-layer = "0.1"
aidale-core = "0.1"

Custom Layers

Implement the Layer trait from aidale-core:

use aidale_core::{Layer, Provider, ChatCompletionParams, ChatCompletionResponse};
use async_trait::async_trait;

pub struct MyCustomLayer {
    // Your fields
}

#[async_trait]
impl<P: Provider> Layer<P> for MyCustomLayer {
    async fn call(
        &self,
        provider: &P,
        model: &str,
        params: ChatCompletionParams,
    ) -> Result<ChatCompletionResponse> {
        // Pre-processing
        println!("Before request");

        // Call next layer or provider
        let response = provider.chat_completion(model, params).await?;

        // Post-processing
        println!("After request");

        Ok(response)
    }
}

Planned Layers

  • CachingLayer: Response caching with TTL
  • RateLimitLayer: Request rate limiting
  • CircuitBreakerLayer: Circuit breaker pattern
  • MetricsLayer: Prometheus metrics export
  • TracingLayer: OpenTelemetry distributed tracing

Related Crates

License

MIT OR Apache-2.0

Commit count: 0

cargo fmt