ai-lib 0.4.0

A unified AI SDK for Rust providing a single interface for multiple AI providers with hybrid architecture
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
//! Circuit breaker implementation

use crate::circuit_breaker::{CircuitBreakerConfig, CircuitState};
use crate::metrics::Metrics;
use crate::types::AiLibError;
use futures::Future;
use serde::{Deserialize, Serialize};
use std::sync::atomic::{AtomicU32, AtomicU64};
use std::sync::{Arc, Mutex};
use std::time::{Duration, Instant};
use tokio::time::timeout;

/// Circuit breaker error types
#[derive(Debug, thiserror::Error)]
pub enum CircuitBreakerError {
    #[error("Circuit breaker is open: {0}")]
    CircuitOpen(String),
    #[error("Request timeout: {0}")]
    RequestTimeout(String),
    #[error("Underlying error: {0}")]
    Underlying(#[from] AiLibError),
    #[error("Circuit breaker is disabled")]
    Disabled,
}

/// Circuit breaker metrics for monitoring
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CircuitBreakerMetrics {
    pub state: CircuitState,
    pub total_requests: u64,
    pub successful_requests: u64,
    pub failed_requests: u64,
    pub timeout_requests: u64,
    pub circuit_open_count: u64,
    pub circuit_close_count: u64,
    pub current_failure_count: u32,
    pub current_success_count: u32,
    #[serde(skip)]
    pub last_failure_time: Option<Instant>,
    #[serde(skip)]
    pub uptime: Duration,
}

/// Circuit breaker implementation
pub struct CircuitBreaker {
    state: Arc<Mutex<CircuitState>>,
    config: CircuitBreakerConfig,
    failure_count: Arc<AtomicU32>,
    success_count: Arc<AtomicU32>,
    last_failure_time: Arc<Mutex<Option<Instant>>>,
    // Metrics
    total_requests: Arc<AtomicU64>,
    successful_requests: Arc<AtomicU64>,
    failed_requests: Arc<AtomicU64>,
    timeout_requests: Arc<AtomicU64>,
    circuit_open_count: Arc<AtomicU64>,
    circuit_close_count: Arc<AtomicU64>,
    start_time: Instant,
    // Optional metrics collector
    metrics: Option<Arc<dyn Metrics>>,
    // Circuit breaker enabled flag
    enabled: bool,
}

impl CircuitBreaker {
    /// Create a new circuit breaker with the given configuration
    pub fn new(config: CircuitBreakerConfig) -> Self {
        Self {
            state: Arc::new(Mutex::new(CircuitState::Closed)),
            config,
            failure_count: Arc::new(AtomicU32::new(0)),
            success_count: Arc::new(AtomicU32::new(0)),
            last_failure_time: Arc::new(Mutex::new(None)),
            total_requests: Arc::new(AtomicU64::new(0)),
            successful_requests: Arc::new(AtomicU64::new(0)),
            failed_requests: Arc::new(AtomicU64::new(0)),
            timeout_requests: Arc::new(AtomicU64::new(0)),
            circuit_open_count: Arc::new(AtomicU64::new(0)),
            circuit_close_count: Arc::new(AtomicU64::new(0)),
            start_time: Instant::now(),
            metrics: None,
            enabled: true,
        }
    }

    /// Create a new circuit breaker with metrics collection
    pub fn with_metrics(config: CircuitBreakerConfig, metrics: Arc<dyn Metrics>) -> Self {
        Self {
            state: Arc::new(Mutex::new(CircuitState::Closed)),
            config,
            failure_count: Arc::new(AtomicU32::new(0)),
            success_count: Arc::new(AtomicU32::new(0)),
            last_failure_time: Arc::new(Mutex::new(None)),
            total_requests: Arc::new(AtomicU64::new(0)),
            successful_requests: Arc::new(AtomicU64::new(0)),
            failed_requests: Arc::new(AtomicU64::new(0)),
            timeout_requests: Arc::new(AtomicU64::new(0)),
            circuit_open_count: Arc::new(AtomicU64::new(0)),
            circuit_close_count: Arc::new(AtomicU64::new(0)),
            start_time: Instant::now(),
            metrics: Some(metrics),
            enabled: true,
        }
    }

    /// Enable or disable the circuit breaker
    pub fn set_enabled(&mut self, enabled: bool) {
        self.enabled = enabled;
    }

    /// Execute a function with circuit breaker protection
    pub async fn call<F, T>(&self, f: F) -> Result<T, CircuitBreakerError>
    where
        F: Future<Output = Result<T, AiLibError>>,
    {
        // Check if circuit breaker is enabled
        if !self.enabled {
            return f.await.map_err(CircuitBreakerError::Underlying);
        }

        // Increment total requests counter
        self.total_requests
            .fetch_add(1, std::sync::atomic::Ordering::Relaxed);

        // Check if we should allow the request
        if !self.should_allow_request().await {
            return Err(CircuitBreakerError::CircuitOpen(
                "Circuit breaker is open".to_string(),
            ));
        }

        // Execute the request with timeout
        let result = timeout(self.config.request_timeout, f).await;

        match result {
            Ok(Ok(response)) => {
                self.on_success().await;
                Ok(response)
            }
            Ok(Err(error)) => {
                self.on_failure().await;
                Err(CircuitBreakerError::Underlying(error))
            }
            Err(_) => {
                self.on_timeout().await;
                Err(CircuitBreakerError::RequestTimeout(
                    "Request timed out".to_string(),
                ))
            }
        }
    }

    /// Check if the circuit breaker should allow a request
    async fn should_allow_request(&self) -> bool {
        let state = *self.state.lock().unwrap();

        match state {
            CircuitState::Closed => true,
            CircuitState::Open => {
                // Check if enough time has passed to try half-open
                let allow_half_open = {
                    let last = self.last_failure_time.lock().unwrap();
                    last.and_then(|t| Some(t.elapsed() >= self.config.recovery_timeout))
                        .unwrap_or(false)
                };
                if allow_half_open {
                    self.transition_to_half_open().await;
                    true
                } else {
                    false
                }
            }
            CircuitState::HalfOpen => true,
        }
    }

    /// Handle successful request
    async fn on_success(&self) {
        self.successful_requests
            .fetch_add(1, std::sync::atomic::Ordering::Relaxed);

        let mut record_closed_metric = false;
        {
            let mut state = self.state.lock().unwrap();
            match *state {
                CircuitState::Closed => {
                    // Reset failure count on success
                    self.failure_count
                        .store(0, std::sync::atomic::Ordering::Relaxed);
                }
                CircuitState::HalfOpen => {
                    let success_count = self
                        .success_count
                        .fetch_add(1, std::sync::atomic::Ordering::Relaxed)
                        + 1;
                    if success_count >= self.config.success_threshold {
                        *state = CircuitState::Closed;
                        self.success_count
                            .store(0, std::sync::atomic::Ordering::Relaxed);
                        self.circuit_close_count
                            .fetch_add(1, std::sync::atomic::Ordering::Relaxed);
                        record_closed_metric = true;
                    }
                }
                CircuitState::Open => {
                    // This shouldn't happen, but handle gracefully
                }
            }
        }
        if record_closed_metric {
            if let Some(metrics) = &self.metrics {
                metrics.incr_counter("circuit_breaker.closed", 1).await;
            }
        }
    }

    /// Handle failed request
    async fn on_failure(&self) {
        self.failed_requests
            .fetch_add(1, std::sync::atomic::Ordering::Relaxed);

        let failure_count = self
            .failure_count
            .fetch_add(1, std::sync::atomic::Ordering::Relaxed)
            + 1;

        // Record failure time
        *self.last_failure_time.lock().unwrap() = Some(Instant::now());

        // Check if we should open the circuit
        if failure_count >= self.config.failure_threshold {
            {
                let mut state = self.state.lock().unwrap();
                *state = CircuitState::Open;
            }
            self.circuit_open_count
                .fetch_add(1, std::sync::atomic::Ordering::Relaxed);

            // Record metrics
            if let Some(metrics) = &self.metrics {
                let m = metrics.clone();
                m.incr_counter("circuit_breaker.opened", 1).await;
            }
        }
    }

    /// Handle timeout request
    async fn on_timeout(&self) {
        self.timeout_requests
            .fetch_add(1, std::sync::atomic::Ordering::Relaxed);
        self.failed_requests
            .fetch_add(1, std::sync::atomic::Ordering::Relaxed);

        let failure_count = self
            .failure_count
            .fetch_add(1, std::sync::atomic::Ordering::Relaxed)
            + 1;

        // Record failure time
        *self.last_failure_time.lock().unwrap() = Some(Instant::now());

        // Check if we should open the circuit
        if failure_count >= self.config.failure_threshold {
            {
                let mut state = self.state.lock().unwrap();
                *state = CircuitState::Open;
            }
            self.circuit_open_count
                .fetch_add(1, std::sync::atomic::Ordering::Relaxed);

            // Record metrics
            if let Some(metrics) = &self.metrics {
                let m = metrics.clone();
                m.incr_counter("circuit_breaker.opened", 1).await;
            }
        }
    }

    /// Transition to half-open state
    async fn transition_to_half_open(&self) {
        let mut state = self.state.lock().unwrap();
        *state = CircuitState::HalfOpen;
        self.success_count
            .store(0, std::sync::atomic::Ordering::Relaxed);
    }

    /// Get current circuit state
    pub fn state(&self) -> CircuitState {
        *self.state.lock().unwrap()
    }

    /// Get current failure count
    pub fn failure_count(&self) -> u32 {
        self.failure_count
            .load(std::sync::atomic::Ordering::Relaxed)
    }

    /// Get current success count
    pub fn success_count(&self) -> u32 {
        self.success_count
            .load(std::sync::atomic::Ordering::Relaxed)
    }

    /// Get comprehensive metrics
    pub fn get_metrics(&self) -> CircuitBreakerMetrics {
        CircuitBreakerMetrics {
            state: self.state(),
            total_requests: self
                .total_requests
                .load(std::sync::atomic::Ordering::Relaxed),
            successful_requests: self
                .successful_requests
                .load(std::sync::atomic::Ordering::Relaxed),
            failed_requests: self
                .failed_requests
                .load(std::sync::atomic::Ordering::Relaxed),
            timeout_requests: self
                .timeout_requests
                .load(std::sync::atomic::Ordering::Relaxed),
            circuit_open_count: self
                .circuit_open_count
                .load(std::sync::atomic::Ordering::Relaxed),
            circuit_close_count: self
                .circuit_close_count
                .load(std::sync::atomic::Ordering::Relaxed),
            current_failure_count: self.failure_count(),
            current_success_count: self.success_count(),
            last_failure_time: *self.last_failure_time.lock().unwrap(),
            uptime: self.start_time.elapsed(),
        }
    }

    /// Get success rate as a percentage
    pub fn success_rate(&self) -> f64 {
        let total = self
            .total_requests
            .load(std::sync::atomic::Ordering::Relaxed);
        if total == 0 {
            return 100.0;
        }
        let successful = self
            .successful_requests
            .load(std::sync::atomic::Ordering::Relaxed);
        (successful as f64 / total as f64) * 100.0
    }

    /// Get failure rate as a percentage
    pub fn failure_rate(&self) -> f64 {
        let total = self
            .total_requests
            .load(std::sync::atomic::Ordering::Relaxed);
        if total == 0 {
            return 0.0;
        }
        let failed = self
            .failed_requests
            .load(std::sync::atomic::Ordering::Relaxed);
        (failed as f64 / total as f64) * 100.0
    }

    /// Check if circuit breaker is healthy
    pub fn is_healthy(&self) -> bool {
        self.state() == CircuitState::Closed && self.failure_rate() < 50.0
    }

    /// Reset all counters and state
    pub fn reset(&self) {
        self.failure_count
            .store(0, std::sync::atomic::Ordering::Relaxed);
        self.success_count
            .store(0, std::sync::atomic::Ordering::Relaxed);
        self.total_requests
            .store(0, std::sync::atomic::Ordering::Relaxed);
        self.successful_requests
            .store(0, std::sync::atomic::Ordering::Relaxed);
        self.failed_requests
            .store(0, std::sync::atomic::Ordering::Relaxed);
        self.timeout_requests
            .store(0, std::sync::atomic::Ordering::Relaxed);
        self.circuit_open_count
            .store(0, std::sync::atomic::Ordering::Relaxed);
        self.circuit_close_count
            .store(0, std::sync::atomic::Ordering::Relaxed);

        let mut state = self.state.lock().unwrap();
        *state = CircuitState::Closed;

        let mut last_failure = self.last_failure_time.lock().unwrap();
        *last_failure = None;
    }

    /// Force circuit breaker to open state
    pub fn force_open(&self) {
        let mut state = self.state.lock().unwrap();
        *state = CircuitState::Open;
        self.circuit_open_count
            .fetch_add(1, std::sync::atomic::Ordering::Relaxed);
    }

    /// Force circuit breaker to closed state
    pub fn force_close(&self) {
        let mut state = self.state.lock().unwrap();
        *state = CircuitState::Closed;
        self.failure_count
            .store(0, std::sync::atomic::Ordering::Relaxed);
        self.success_count
            .store(0, std::sync::atomic::Ordering::Relaxed);
        self.circuit_close_count
            .fetch_add(1, std::sync::atomic::Ordering::Relaxed);
    }
}