litellm-rs 0.4.16

A high-performance AI Gateway written in Rust, providing OpenAI-compatible APIs with intelligent routing, load balancing, and enterprise features
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
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
//! Team billing models

use serde::{Deserialize, Serialize};

/// Team billing information
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TeamBilling {
    /// Billing plan
    pub plan: BillingPlan,
    /// Billing status
    pub status: BillingStatus,
    /// Monthly budget limit
    pub monthly_budget: Option<f64>,
    /// Current month usage
    pub current_usage: f64,
    /// Billing cycle start
    pub cycle_start: chrono::DateTime<chrono::Utc>,
    /// Billing cycle end
    pub cycle_end: chrono::DateTime<chrono::Utc>,
    /// Payment method
    pub payment_method: Option<PaymentMethod>,
    /// Billing address
    pub billing_address: Option<BillingAddress>,
}

/// Billing plan
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum BillingPlan {
    /// Free plan
    Free,
    /// Starter plan
    Starter,
    /// Professional plan
    Professional,
    /// Enterprise plan
    Enterprise,
    /// Custom plan
    Custom,
}

/// Billing status
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum BillingStatus {
    /// Active billing status
    Active,
    /// Past due billing status
    PastDue,
    /// Cancelled billing status
    Cancelled,
    /// Suspended billing status
    Suspended,
    /// Trial billing status
    Trial,
}

/// Payment method
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PaymentMethod {
    /// Payment method type
    pub method_type: PaymentMethodType,
    /// Last 4 digits (for cards)
    pub last_four: Option<String>,
    /// Expiry month (for cards)
    pub expiry_month: Option<u32>,
    /// Expiry year (for cards)
    pub expiry_year: Option<u32>,
    /// Brand (for cards)
    pub brand: Option<String>,
}

/// Payment method type
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum PaymentMethodType {
    /// Credit card payment
    CreditCard,
    /// Debit card payment
    DebitCard,
    /// Bank transfer payment
    BankTransfer,
    /// PayPal payment
    PayPal,
    /// Stripe payment
    Stripe,
}

/// Billing address
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BillingAddress {
    /// Company name
    pub company: Option<String>,
    /// Address line 1
    pub line1: String,
    /// Address line 2
    pub line2: Option<String>,
    /// City
    pub city: String,
    /// State/Province
    pub state: Option<String>,
    /// Postal code
    pub postal_code: String,
    /// Country
    pub country: String,
    /// Tax ID
    pub tax_id: Option<String>,
}

#[cfg(test)]
mod tests {
    use super::*;
    use chrono::{Duration, Utc};

    // ==================== BillingPlan Tests ====================

    #[test]
    fn test_billing_plan_free() {
        let plan = BillingPlan::Free;
        let json = serde_json::to_string(&plan).unwrap();
        assert_eq!(json, "\"free\"");
    }

    #[test]
    fn test_billing_plan_starter() {
        let plan = BillingPlan::Starter;
        let json = serde_json::to_string(&plan).unwrap();
        assert_eq!(json, "\"starter\"");
    }

    #[test]
    fn test_billing_plan_professional() {
        let plan = BillingPlan::Professional;
        let json = serde_json::to_string(&plan).unwrap();
        assert_eq!(json, "\"professional\"");
    }

    #[test]
    fn test_billing_plan_enterprise() {
        let plan = BillingPlan::Enterprise;
        let json = serde_json::to_string(&plan).unwrap();
        assert_eq!(json, "\"enterprise\"");
    }

    #[test]
    fn test_billing_plan_custom() {
        let plan = BillingPlan::Custom;
        let json = serde_json::to_string(&plan).unwrap();
        assert_eq!(json, "\"custom\"");
    }

    #[test]
    fn test_billing_plan_deserialize() {
        let plan: BillingPlan = serde_json::from_str("\"enterprise\"").unwrap();
        assert!(matches!(plan, BillingPlan::Enterprise));
    }

    #[test]
    fn test_billing_plan_clone() {
        let original = BillingPlan::Professional;
        let cloned = original.clone();
        let json1 = serde_json::to_string(&original).unwrap();
        let json2 = serde_json::to_string(&cloned).unwrap();
        assert_eq!(json1, json2);
    }

    // ==================== BillingStatus Tests ====================

    #[test]
    fn test_billing_status_active() {
        let status = BillingStatus::Active;
        let json = serde_json::to_string(&status).unwrap();
        assert_eq!(json, "\"active\"");
    }

    #[test]
    fn test_billing_status_past_due() {
        let status = BillingStatus::PastDue;
        let json = serde_json::to_string(&status).unwrap();
        assert_eq!(json, "\"past_due\"");
    }

    #[test]
    fn test_billing_status_cancelled() {
        let status = BillingStatus::Cancelled;
        let json = serde_json::to_string(&status).unwrap();
        assert_eq!(json, "\"cancelled\"");
    }

    #[test]
    fn test_billing_status_suspended() {
        let status = BillingStatus::Suspended;
        let json = serde_json::to_string(&status).unwrap();
        assert_eq!(json, "\"suspended\"");
    }

    #[test]
    fn test_billing_status_trial() {
        let status = BillingStatus::Trial;
        let json = serde_json::to_string(&status).unwrap();
        assert_eq!(json, "\"trial\"");
    }

    #[test]
    fn test_billing_status_deserialize() {
        let status: BillingStatus = serde_json::from_str("\"past_due\"").unwrap();
        assert!(matches!(status, BillingStatus::PastDue));
    }

    // ==================== PaymentMethodType Tests ====================

    #[test]
    fn test_payment_method_type_credit_card() {
        let t = PaymentMethodType::CreditCard;
        let json = serde_json::to_string(&t).unwrap();
        assert_eq!(json, "\"credit_card\"");
    }

    #[test]
    fn test_payment_method_type_debit_card() {
        let t = PaymentMethodType::DebitCard;
        let json = serde_json::to_string(&t).unwrap();
        assert_eq!(json, "\"debit_card\"");
    }

    #[test]
    fn test_payment_method_type_bank_transfer() {
        let t = PaymentMethodType::BankTransfer;
        let json = serde_json::to_string(&t).unwrap();
        assert_eq!(json, "\"bank_transfer\"");
    }

    #[test]
    fn test_payment_method_type_paypal() {
        let t = PaymentMethodType::PayPal;
        let json = serde_json::to_string(&t).unwrap();
        assert_eq!(json, "\"pay_pal\"");
    }

    #[test]
    fn test_payment_method_type_stripe() {
        let t = PaymentMethodType::Stripe;
        let json = serde_json::to_string(&t).unwrap();
        assert_eq!(json, "\"stripe\"");
    }

    // ==================== PaymentMethod Tests ====================

    #[test]
    fn test_payment_method_credit_card() {
        let pm = PaymentMethod {
            method_type: PaymentMethodType::CreditCard,
            last_four: Some("4242".to_string()),
            expiry_month: Some(12),
            expiry_year: Some(2025),
            brand: Some("Visa".to_string()),
        };

        assert_eq!(pm.last_four, Some("4242".to_string()));
        assert_eq!(pm.brand, Some("Visa".to_string()));
    }

    #[test]
    fn test_payment_method_bank_transfer() {
        let pm = PaymentMethod {
            method_type: PaymentMethodType::BankTransfer,
            last_four: None,
            expiry_month: None,
            expiry_year: None,
            brand: None,
        };

        assert!(matches!(pm.method_type, PaymentMethodType::BankTransfer));
        assert!(pm.last_four.is_none());
    }

    #[test]
    fn test_payment_method_serialize() {
        let pm = PaymentMethod {
            method_type: PaymentMethodType::CreditCard,
            last_four: Some("1234".to_string()),
            expiry_month: Some(6),
            expiry_year: Some(2026),
            brand: Some("MasterCard".to_string()),
        };

        let json = serde_json::to_string(&pm).unwrap();
        assert!(json.contains("credit_card"));
        assert!(json.contains("1234"));
        assert!(json.contains("MasterCard"));
    }

    #[test]
    fn test_payment_method_clone() {
        let original = PaymentMethod {
            method_type: PaymentMethodType::Stripe,
            last_four: Some("9999".to_string()),
            expiry_month: None,
            expiry_year: None,
            brand: None,
        };

        let cloned = original.clone();
        assert_eq!(original.last_four, cloned.last_four);
    }

    // ==================== BillingAddress Tests ====================

    #[test]
    fn test_billing_address_full() {
        let addr = BillingAddress {
            company: Some("Acme Corp".to_string()),
            line1: "123 Main St".to_string(),
            line2: Some("Suite 100".to_string()),
            city: "San Francisco".to_string(),
            state: Some("CA".to_string()),
            postal_code: "94102".to_string(),
            country: "US".to_string(),
            tax_id: Some("US123456789".to_string()),
        };

        assert_eq!(addr.company, Some("Acme Corp".to_string()));
        assert_eq!(addr.city, "San Francisco");
        assert_eq!(addr.country, "US");
    }

    #[test]
    fn test_billing_address_minimal() {
        let addr = BillingAddress {
            company: None,
            line1: "456 Oak Ave".to_string(),
            line2: None,
            city: "New York".to_string(),
            state: None,
            postal_code: "10001".to_string(),
            country: "US".to_string(),
            tax_id: None,
        };

        assert!(addr.company.is_none());
        assert!(addr.line2.is_none());
    }

    #[test]
    fn test_billing_address_serialize() {
        let addr = BillingAddress {
            company: Some("Test Inc".to_string()),
            line1: "789 Elm St".to_string(),
            line2: None,
            city: "Chicago".to_string(),
            state: Some("IL".to_string()),
            postal_code: "60601".to_string(),
            country: "US".to_string(),
            tax_id: None,
        };

        let json = serde_json::to_string(&addr).unwrap();
        assert!(json.contains("Test Inc"));
        assert!(json.contains("Chicago"));
    }

    #[test]
    fn test_billing_address_clone() {
        let original = BillingAddress {
            company: None,
            line1: "Test St".to_string(),
            line2: None,
            city: "Boston".to_string(),
            state: Some("MA".to_string()),
            postal_code: "02101".to_string(),
            country: "US".to_string(),
            tax_id: None,
        };

        let cloned = original.clone();
        assert_eq!(original.city, cloned.city);
    }

    // ==================== TeamBilling Tests ====================

    #[test]
    fn test_team_billing_creation() {
        let now = Utc::now();
        let billing = TeamBilling {
            plan: BillingPlan::Professional,
            status: BillingStatus::Active,
            monthly_budget: Some(1000.0),
            current_usage: 250.0,
            cycle_start: now,
            cycle_end: now + Duration::days(30),
            payment_method: None,
            billing_address: None,
        };

        assert!(matches!(billing.plan, BillingPlan::Professional));
        assert!(matches!(billing.status, BillingStatus::Active));
        assert_eq!(billing.monthly_budget, Some(1000.0));
        assert_eq!(billing.current_usage, 250.0);
    }

    #[test]
    fn test_team_billing_with_payment_method() {
        let billing = TeamBilling {
            plan: BillingPlan::Enterprise,
            status: BillingStatus::Active,
            monthly_budget: Some(10000.0),
            current_usage: 0.0,
            cycle_start: Utc::now(),
            cycle_end: Utc::now() + Duration::days(30),
            payment_method: Some(PaymentMethod {
                method_type: PaymentMethodType::CreditCard,
                last_four: Some("4242".to_string()),
                expiry_month: Some(12),
                expiry_year: Some(2025),
                brand: Some("Visa".to_string()),
            }),
            billing_address: None,
        };

        assert!(billing.payment_method.is_some());
    }

    #[test]
    fn test_team_billing_with_address() {
        let billing = TeamBilling {
            plan: BillingPlan::Starter,
            status: BillingStatus::Trial,
            monthly_budget: None,
            current_usage: 0.0,
            cycle_start: Utc::now(),
            cycle_end: Utc::now() + Duration::days(14),
            payment_method: None,
            billing_address: Some(BillingAddress {
                company: Some("Startup Inc".to_string()),
                line1: "100 Tech St".to_string(),
                line2: None,
                city: "Austin".to_string(),
                state: Some("TX".to_string()),
                postal_code: "78701".to_string(),
                country: "US".to_string(),
                tax_id: None,
            }),
        };

        assert!(billing.billing_address.is_some());
    }

    #[test]
    fn test_team_billing_serialize() {
        let billing = TeamBilling {
            plan: BillingPlan::Free,
            status: BillingStatus::Active,
            monthly_budget: None,
            current_usage: 0.0,
            cycle_start: Utc::now(),
            cycle_end: Utc::now() + Duration::days(30),
            payment_method: None,
            billing_address: None,
        };

        let json = serde_json::to_string(&billing).unwrap();
        assert!(json.contains("\"plan\":\"free\""));
        assert!(json.contains("\"status\":\"active\""));
    }

    #[test]
    fn test_team_billing_clone() {
        let billing = TeamBilling {
            plan: BillingPlan::Professional,
            status: BillingStatus::Active,
            monthly_budget: Some(500.0),
            current_usage: 100.0,
            cycle_start: Utc::now(),
            cycle_end: Utc::now() + Duration::days(30),
            payment_method: None,
            billing_address: None,
        };

        let cloned = billing.clone();
        assert_eq!(billing.monthly_budget, cloned.monthly_budget);
        assert_eq!(billing.current_usage, cloned.current_usage);
    }

    #[test]
    fn test_team_billing_debug() {
        let billing = TeamBilling {
            plan: BillingPlan::Custom,
            status: BillingStatus::Suspended,
            monthly_budget: None,
            current_usage: 0.0,
            cycle_start: Utc::now(),
            cycle_end: Utc::now() + Duration::days(30),
            payment_method: None,
            billing_address: None,
        };

        let debug_str = format!("{:?}", billing);
        assert!(debug_str.contains("TeamBilling"));
        assert!(debug_str.contains("Custom"));
    }
}