siumai 0.10.3

A unified LLM interface library for Rust
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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
//! Retry Mechanism Module
//!
//! This module provides comprehensive retry functionality for LLM API calls,
//! including exponential backoff, jitter, and provider-specific retry policies.

use rand::Rng;
use std::time::Duration;
use tokio::time::sleep;

use crate::error::LlmError;
use crate::types::ProviderType;

/// Retry policy configuration
#[derive(Debug, Clone)]
pub struct RetryPolicy {
    /// Maximum number of retry attempts
    pub max_attempts: u32,
    /// Initial delay between retries
    pub initial_delay: Duration,
    /// Maximum delay between retries
    pub max_delay: Duration,
    /// Backoff multiplier (for exponential backoff)
    pub backoff_multiplier: f64,
    /// Whether to add jitter to delays
    pub use_jitter: bool,
    /// Maximum jitter percentage (0.0 to 1.0)
    pub jitter_factor: f64,
    /// Custom retry condition function
    pub retry_condition: Option<fn(&LlmError) -> bool>,
}

impl Default for RetryPolicy {
    fn default() -> Self {
        Self {
            max_attempts: 3,
            initial_delay: Duration::from_millis(1000),
            max_delay: Duration::from_secs(60),
            backoff_multiplier: 2.0,
            use_jitter: true,
            jitter_factor: 0.1,
            retry_condition: None,
        }
    }
}

impl RetryPolicy {
    /// Create a new retry policy
    pub fn new() -> Self {
        Self::default()
    }

    /// Set maximum attempts
    pub const fn with_max_attempts(mut self, max_attempts: u32) -> Self {
        self.max_attempts = max_attempts;
        self
    }

    /// Set initial delay
    pub const fn with_initial_delay(mut self, delay: Duration) -> Self {
        self.initial_delay = delay;
        self
    }

    /// Set maximum delay
    pub const fn with_max_delay(mut self, delay: Duration) -> Self {
        self.max_delay = delay;
        self
    }

    /// Set backoff multiplier
    pub const fn with_backoff_multiplier(mut self, multiplier: f64) -> Self {
        self.backoff_multiplier = multiplier;
        self
    }

    /// Enable or disable jitter
    pub const fn with_jitter(mut self, use_jitter: bool) -> Self {
        self.use_jitter = use_jitter;
        self
    }

    /// Set jitter factor
    pub const fn with_jitter_factor(mut self, factor: f64) -> Self {
        self.jitter_factor = factor.clamp(0.0, 1.0);
        self
    }

    /// Set custom retry condition
    pub fn with_retry_condition(mut self, condition: fn(&LlmError) -> bool) -> Self {
        self.retry_condition = Some(condition);
        self
    }

    /// Check if an error should be retried
    pub fn should_retry(&self, error: &LlmError) -> bool {
        if let Some(condition) = self.retry_condition {
            condition(error)
        } else {
            error.is_retryable()
        }
    }

    /// Calculate delay for a given attempt
    pub fn calculate_delay(&self, attempt: u32) -> Duration {
        let base_delay =
            self.initial_delay.as_millis() as f64 * self.backoff_multiplier.powi(attempt as i32);

        let delay = Duration::from_millis(base_delay as u64).min(self.max_delay);

        if self.use_jitter {
            self.add_jitter(delay)
        } else {
            delay
        }
    }

    /// Add jitter to a delay
    fn add_jitter(&self, delay: Duration) -> Duration {
        let mut rng = rand::thread_rng();
        let jitter_range = delay.as_millis() as f64 * self.jitter_factor;
        let jitter = rng.gen_range(-jitter_range..=jitter_range);

        let new_delay = delay.as_millis() as f64 + jitter;
        Duration::from_millis(new_delay.max(0.0) as u64)
    }

    /// Create a provider-specific retry policy
    pub fn for_provider(provider: &ProviderType) -> Self {
        match provider {
            ProviderType::OpenAi => Self::openai_policy(),
            ProviderType::Anthropic => Self::anthropic_policy(),
            ProviderType::Gemini => Self::gemini_policy(),
            ProviderType::XAI => Self::xai_policy(),
            ProviderType::Ollama => Self::ollama_policy(),
            ProviderType::Custom(_) => Self::default(),
            ProviderType::Groq => Self::default(),
        }
    }

    /// OpenAI-specific retry policy
    pub fn openai_policy() -> Self {
        Self {
            max_attempts: 3,
            initial_delay: Duration::from_millis(1000),
            max_delay: Duration::from_secs(60),
            backoff_multiplier: 2.0,
            use_jitter: true,
            jitter_factor: 0.1,
            retry_condition: Some(|error| {
                match error {
                    LlmError::ApiError { code, .. } => {
                        // OpenAI specific: retry on 429, 500, 502, 503, 504
                        matches!(*code, 429 | 500 | 502 | 503 | 504)
                    }
                    _ => error.is_retryable(),
                }
            }),
        }
    }

    /// Anthropic-specific retry policy
    pub fn anthropic_policy() -> Self {
        Self {
            max_attempts: 3,
            initial_delay: Duration::from_millis(1500),
            max_delay: Duration::from_secs(120),
            backoff_multiplier: 2.0,
            use_jitter: true,
            jitter_factor: 0.15,
            retry_condition: Some(|error| {
                match error {
                    LlmError::ApiError { code, .. } => {
                        // Anthropic specific: retry on 429, 500, 502, 503, 529
                        matches!(*code, 429 | 500 | 502 | 503 | 529)
                    }
                    _ => error.is_retryable(),
                }
            }),
        }
    }

    /// Gemini-specific retry policy
    pub fn gemini_policy() -> Self {
        Self {
            max_attempts: 3,
            initial_delay: Duration::from_millis(1000),
            max_delay: Duration::from_secs(60),
            backoff_multiplier: 1.5,
            use_jitter: true,
            jitter_factor: 0.1,
            retry_condition: Some(|error| {
                match error {
                    LlmError::ApiError { code, .. } => {
                        // Google specific: retry on 429, 500, 502, 503
                        matches!(*code, 429 | 500 | 502 | 503)
                    }
                    _ => error.is_retryable(),
                }
            }),
        }
    }

    /// xAI-specific retry policy
    pub fn xai_policy() -> Self {
        Self {
            max_attempts: 3,
            initial_delay: Duration::from_millis(1000),
            max_delay: Duration::from_secs(60),
            backoff_multiplier: 2.0,
            use_jitter: true,
            jitter_factor: 0.1,
            retry_condition: Some(|error| {
                match error {
                    LlmError::ApiError { code, .. } => {
                        // xAI uses OpenAI-compatible API, so similar retry logic
                        matches!(*code, 429 | 500 | 502 | 503 | 504)
                    }
                    _ => error.is_retryable(),
                }
            }),
        }
    }

    /// Ollama-specific retry policy
    pub fn ollama_policy() -> Self {
        Self {
            max_attempts: 3,
            initial_delay: Duration::from_millis(500),
            max_delay: Duration::from_secs(30),
            backoff_multiplier: 1.5,
            use_jitter: true,
            jitter_factor: 0.1,
            retry_condition: Some(|error| {
                match error {
                    LlmError::ApiError { code, .. } => {
                        // Ollama specific: retry on 429, 500, 502, 503, 504
                        matches!(*code, 429 | 500 | 502 | 503 | 504)
                    }
                    LlmError::HttpError(_) => true, // Retry on HTTP errors (connection issues)
                    _ => error.is_retryable(),
                }
            }),
        }
    }
}

/// Retry executor that handles the actual retry logic
pub struct RetryExecutor {
    policy: RetryPolicy,
}

impl RetryExecutor {
    /// Create a new retry executor
    pub const fn new(policy: RetryPolicy) -> Self {
        Self { policy }
    }

    /// Execute a function with retry logic
    pub async fn execute<F, Fut, T>(&self, mut operation: F) -> Result<T, LlmError>
    where
        F: FnMut() -> Fut,
        Fut: std::future::Future<Output = Result<T, LlmError>>,
    {
        let mut last_error = None;

        for attempt in 0..self.policy.max_attempts {
            match operation().await {
                Ok(result) => return Ok(result),
                Err(error) => {
                    last_error = Some(error.clone());

                    // Check if we should retry
                    if !self.policy.should_retry(&error) {
                        return Err(error);
                    }

                    // If this is the last attempt, don't wait
                    if attempt == self.policy.max_attempts - 1 {
                        break;
                    }

                    // Calculate and apply delay
                    let delay = self.policy.calculate_delay(attempt);
                    sleep(delay).await;
                }
            }
        }

        // Return the last error if all attempts failed
        Err(last_error.unwrap_or_else(|| {
            LlmError::InternalError("Retry executor failed without error".to_string())
        }))
    }

    /// Execute with custom error handling
    pub async fn execute_with_handler<F, Fut, T, H>(
        &self,
        mut operation: F,
        mut error_handler: H,
    ) -> Result<T, LlmError>
    where
        F: FnMut() -> Fut,
        Fut: std::future::Future<Output = Result<T, LlmError>>,
        H: FnMut(&LlmError, u32) -> bool, // Returns true to continue retrying
    {
        let mut last_error = None;

        for attempt in 0..self.policy.max_attempts {
            match operation().await {
                Ok(result) => return Ok(result),
                Err(error) => {
                    last_error = Some(error.clone());

                    // Check with custom error handler
                    if !error_handler(&error, attempt) {
                        return Err(error);
                    }

                    // Check if we should retry according to policy
                    if !self.policy.should_retry(&error) {
                        return Err(error);
                    }

                    // If this is the last attempt, don't wait
                    if attempt == self.policy.max_attempts - 1 {
                        break;
                    }

                    // Calculate and apply delay
                    let delay = self.policy.calculate_delay(attempt);
                    sleep(delay).await;
                }
            }
        }

        // Return the last error if all attempts failed
        Err(last_error.unwrap_or_else(|| {
            LlmError::InternalError("Retry executor failed without error".to_string())
        }))
    }
}

/// Convenience function to retry an operation with default policy
pub async fn retry_with_default<F, Fut, T>(operation: F) -> Result<T, LlmError>
where
    F: FnMut() -> Fut,
    Fut: std::future::Future<Output = Result<T, LlmError>>,
{
    let executor = RetryExecutor::new(RetryPolicy::default());
    executor.execute(operation).await
}

/// Convenience function to retry an operation with provider-specific policy
pub async fn retry_for_provider<F, Fut, T>(
    provider: &ProviderType,
    operation: F,
) -> Result<T, LlmError>
where
    F: FnMut() -> Fut,
    Fut: std::future::Future<Output = Result<T, LlmError>>,
{
    let policy = RetryPolicy::for_provider(provider);
    let executor = RetryExecutor::new(policy);
    executor.execute(operation).await
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::sync::Arc;
    use std::sync::atomic::{AtomicU32, Ordering};

    #[tokio::test]
    async fn test_retry_success_on_second_attempt() {
        let counter = Arc::new(AtomicU32::new(0));
        let counter_clone = counter.clone();

        let policy = RetryPolicy::new().with_max_attempts(3);
        let executor = RetryExecutor::new(policy);

        let result = executor
            .execute(|| {
                let counter = counter_clone.clone();
                async move {
                    let count = counter.fetch_add(1, Ordering::SeqCst);
                    if count == 0 {
                        Err(LlmError::ApiError {
                            code: 500,
                            message: "Server error".to_string(),
                            details: None,
                        })
                    } else {
                        Ok("success")
                    }
                }
            })
            .await;

        assert!(result.is_ok());
        assert_eq!(result.unwrap(), "success");
        assert_eq!(counter.load(Ordering::SeqCst), 2);
    }

    #[tokio::test]
    async fn test_retry_exhaustion() {
        let counter = Arc::new(AtomicU32::new(0));
        let counter_clone = counter.clone();

        let policy = RetryPolicy::new().with_max_attempts(2);
        let executor = RetryExecutor::new(policy);

        let result: Result<(), LlmError> = executor
            .execute(|| {
                let counter = counter_clone.clone();
                async move {
                    counter.fetch_add(1, Ordering::SeqCst);
                    Err(LlmError::ApiError {
                        code: 500,
                        message: "Server error".to_string(),
                        details: None,
                    })
                }
            })
            .await;

        assert!(result.is_err());
        assert_eq!(counter.load(Ordering::SeqCst), 2);
    }

    #[test]
    fn test_delay_calculation() {
        let policy = RetryPolicy::new()
            .with_initial_delay(Duration::from_millis(100))
            .with_backoff_multiplier(2.0)
            .with_jitter(false);

        assert_eq!(policy.calculate_delay(0), Duration::from_millis(100));
        assert_eq!(policy.calculate_delay(1), Duration::from_millis(200));
        assert_eq!(policy.calculate_delay(2), Duration::from_millis(400));
    }

    #[test]
    fn test_provider_specific_policies() {
        let openai_policy = RetryPolicy::for_provider(&ProviderType::OpenAi);
        let anthropic_policy = RetryPolicy::for_provider(&ProviderType::Anthropic);

        assert_eq!(openai_policy.max_attempts, 3);
        assert_eq!(anthropic_policy.initial_delay, Duration::from_millis(1500));
    }
}