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
//! API Key Repository
//!
//! This module provides the storage abstraction for API keys.

use super::types::{KeyStatus, ManagedApiKey, UpdateKeyConfig};
use crate::utils::error::gateway_error::{GatewayError, Result};
use async_trait::async_trait;
use chrono::Utc;
use dashmap::DashMap;
use std::sync::Arc;
use uuid::Uuid;

/// Repository trait for API key storage operations
#[async_trait]
pub trait KeyRepository: Send + Sync {
    /// Store a new API key
    async fn create(&self, key: ManagedApiKey) -> Result<ManagedApiKey>;

    /// Find a key by ID
    async fn find_by_id(&self, id: Uuid) -> Result<Option<ManagedApiKey>>;

    /// Find a key by its hash
    async fn find_by_hash(&self, key_hash: &str) -> Result<Option<ManagedApiKey>>;

    /// Update a key
    async fn update(&self, id: Uuid, config: UpdateKeyConfig) -> Result<ManagedApiKey>;

    /// Update key status
    async fn update_status(&self, id: Uuid, status: KeyStatus) -> Result<()>;

    /// Update last used timestamp
    async fn update_last_used(&self, id: Uuid) -> Result<()>;

    /// Update usage statistics
    async fn update_usage(&self, id: Uuid, tokens: u64, cost: f64) -> Result<()>;

    /// List keys by user ID
    async fn list_by_user(&self, user_id: Uuid) -> Result<Vec<ManagedApiKey>>;

    /// List keys by team ID
    async fn list_by_team(&self, team_id: Uuid) -> Result<Vec<ManagedApiKey>>;

    /// List all keys with optional filters
    async fn list_all(
        &self,
        status: Option<KeyStatus>,
        limit: Option<usize>,
        offset: Option<usize>,
    ) -> Result<Vec<ManagedApiKey>>;

    /// Delete a key permanently
    async fn delete(&self, id: Uuid) -> Result<()>;

    /// Delete expired keys
    async fn delete_expired(&self) -> Result<u64>;

    /// Count total keys
    async fn count(&self, status: Option<KeyStatus>) -> Result<u64>;
}

/// In-memory implementation of KeyRepository for testing and development
#[derive(Debug, Default)]
pub struct InMemoryKeyRepository {
    /// Storage for keys by ID
    keys_by_id: DashMap<Uuid, ManagedApiKey>,
    /// Index for looking up keys by hash
    hash_index: DashMap<String, Uuid>,
}

impl InMemoryKeyRepository {
    /// Create a new in-memory repository
    pub fn new() -> Self {
        Self {
            keys_by_id: DashMap::new(),
            hash_index: DashMap::new(),
        }
    }

    /// Create a new in-memory repository wrapped in Arc
    pub fn new_arc() -> Arc<Self> {
        Arc::new(Self::new())
    }
}

#[async_trait]
impl KeyRepository for InMemoryKeyRepository {
    async fn create(&self, key: ManagedApiKey) -> Result<ManagedApiKey> {
        // Check if hash already exists
        if self.hash_index.contains_key(&key.key_hash) {
            return Err(GatewayError::conflict("API key already exists"));
        }

        // Store the key
        self.hash_index.insert(key.key_hash.clone(), key.id);
        self.keys_by_id.insert(key.id, key.clone());

        Ok(key)
    }

    async fn find_by_id(&self, id: Uuid) -> Result<Option<ManagedApiKey>> {
        Ok(self.keys_by_id.get(&id).map(|r| r.value().clone()))
    }

    async fn find_by_hash(&self, key_hash: &str) -> Result<Option<ManagedApiKey>> {
        if let Some(id) = self.hash_index.get(key_hash) {
            return self.find_by_id(*id).await;
        }
        Ok(None)
    }

    async fn update(&self, id: Uuid, config: UpdateKeyConfig) -> Result<ManagedApiKey> {
        let mut entry = self
            .keys_by_id
            .get_mut(&id)
            .ok_or_else(|| GatewayError::not_found("API key not found"))?;

        let key = entry.value_mut();

        if let Some(name) = config.name {
            key.name = name;
        }
        if let Some(description) = config.description {
            key.description = description;
        }
        if let Some(permissions) = config.permissions {
            key.permissions = permissions;
        }
        if let Some(rate_limits) = config.rate_limits {
            key.rate_limits = rate_limits;
        }
        if let Some(budget_id) = config.budget_id {
            key.budget_id = budget_id;
        }
        if let Some(expires_at) = config.expires_at {
            key.expires_at = expires_at;
        }
        if let Some(metadata) = config.metadata {
            key.metadata = metadata;
        }

        key.updated_at = Utc::now();

        Ok(key.clone())
    }

    async fn update_status(&self, id: Uuid, status: KeyStatus) -> Result<()> {
        let mut entry = self
            .keys_by_id
            .get_mut(&id)
            .ok_or_else(|| GatewayError::not_found("API key not found"))?;

        entry.value_mut().status = status;
        entry.value_mut().updated_at = Utc::now();

        Ok(())
    }

    async fn update_last_used(&self, id: Uuid) -> Result<()> {
        if let Some(mut entry) = self.keys_by_id.get_mut(&id) {
            entry.value_mut().last_used_at = Some(Utc::now());
        }
        Ok(())
    }

    async fn update_usage(&self, id: Uuid, tokens: u64, cost: f64) -> Result<()> {
        if let Some(mut entry) = self.keys_by_id.get_mut(&id) {
            let key = entry.value_mut();
            key.usage_stats.reset_daily_if_needed();
            key.usage_stats.record_usage(tokens, cost);
            key.last_used_at = Some(Utc::now());
        }
        Ok(())
    }

    async fn list_by_user(&self, user_id: Uuid) -> Result<Vec<ManagedApiKey>> {
        let keys: Vec<ManagedApiKey> = self
            .keys_by_id
            .iter()
            .filter(|r| r.value().user_id == Some(user_id))
            .map(|r| r.value().clone())
            .collect();
        Ok(keys)
    }

    async fn list_by_team(&self, team_id: Uuid) -> Result<Vec<ManagedApiKey>> {
        let keys: Vec<ManagedApiKey> = self
            .keys_by_id
            .iter()
            .filter(|r| r.value().team_id == Some(team_id))
            .map(|r| r.value().clone())
            .collect();
        Ok(keys)
    }

    async fn list_all(
        &self,
        status: Option<KeyStatus>,
        limit: Option<usize>,
        offset: Option<usize>,
    ) -> Result<Vec<ManagedApiKey>> {
        let mut keys: Vec<ManagedApiKey> = self
            .keys_by_id
            .iter()
            .filter(|r| {
                if let Some(s) = status {
                    r.value().effective_status() == s
                } else {
                    true
                }
            })
            .map(|r| r.value().clone())
            .collect();

        // Sort by created_at descending
        keys.sort_by(|a, b| b.created_at.cmp(&a.created_at));

        // Apply pagination
        let offset = offset.unwrap_or(0);
        let limit = limit.unwrap_or(100);

        Ok(keys.into_iter().skip(offset).take(limit).collect())
    }

    async fn delete(&self, id: Uuid) -> Result<()> {
        if let Some((_, key)) = self.keys_by_id.remove(&id) {
            self.hash_index.remove(&key.key_hash);
        }
        Ok(())
    }

    async fn delete_expired(&self) -> Result<u64> {
        let now = Utc::now();
        let mut deleted = 0u64;

        let expired_ids: Vec<Uuid> = self
            .keys_by_id
            .iter()
            .filter(|r| {
                if let Some(expires_at) = r.value().expires_at {
                    expires_at < now
                } else {
                    false
                }
            })
            .map(|r| *r.key())
            .collect();

        for id in expired_ids {
            if let Some((_, key)) = self.keys_by_id.remove(&id) {
                self.hash_index.remove(&key.key_hash);
                deleted += 1;
            }
        }

        Ok(deleted)
    }

    async fn count(&self, status: Option<KeyStatus>) -> Result<u64> {
        let count = self
            .keys_by_id
            .iter()
            .filter(|r| {
                if let Some(s) = status {
                    r.value().effective_status() == s
                } else {
                    true
                }
            })
            .count();
        Ok(count as u64)
    }
}

#[cfg(test)]
mod repository_tests {
    use super::*;
    use crate::core::keys::types::{KeyPermissions, KeyRateLimits, KeyUsageStats};

    fn create_test_key() -> ManagedApiKey {
        ManagedApiKey {
            id: Uuid::new_v4(),
            key_hash: format!("hash_{}", Uuid::new_v4()),
            key_prefix: "gw-test...1234".to_string(),
            name: "Test Key".to_string(),
            description: None,
            user_id: None,
            team_id: None,
            budget_id: None,
            permissions: KeyPermissions::default(),
            rate_limits: KeyRateLimits::default(),
            status: KeyStatus::Active,
            expires_at: None,
            created_at: Utc::now(),
            updated_at: Utc::now(),
            last_used_at: None,
            usage_stats: KeyUsageStats::new(),
            metadata: serde_json::Value::Null,
        }
    }

    #[tokio::test]
    async fn test_create_key() {
        let repo = InMemoryKeyRepository::new();
        let key = create_test_key();
        let id = key.id;

        let created = repo.create(key).await.unwrap();
        assert_eq!(created.id, id);

        let found = repo.find_by_id(id).await.unwrap();
        assert!(found.is_some());
    }

    #[tokio::test]
    async fn test_find_by_hash() {
        let repo = InMemoryKeyRepository::new();
        let key = create_test_key();
        let hash = key.key_hash.clone();

        repo.create(key).await.unwrap();

        let found = repo.find_by_hash(&hash).await.unwrap();
        assert!(found.is_some());
        assert_eq!(found.unwrap().key_hash, hash);
    }

    #[tokio::test]
    async fn test_update_key() {
        let repo = InMemoryKeyRepository::new();
        let key = create_test_key();
        let id = key.id;

        repo.create(key).await.unwrap();

        let config = UpdateKeyConfig {
            name: Some("Updated Name".to_string()),
            ..Default::default()
        };

        let updated = repo.update(id, config).await.unwrap();
        assert_eq!(updated.name, "Updated Name");
    }

    #[tokio::test]
    async fn test_update_status() {
        let repo = InMemoryKeyRepository::new();
        let key = create_test_key();
        let id = key.id;

        repo.create(key).await.unwrap();
        repo.update_status(id, KeyStatus::Revoked).await.unwrap();

        let found = repo.find_by_id(id).await.unwrap().unwrap();
        assert_eq!(found.status, KeyStatus::Revoked);
    }

    #[tokio::test]
    async fn test_list_by_user() {
        let repo = InMemoryKeyRepository::new();
        let user_id = Uuid::new_v4();

        let mut key1 = create_test_key();
        key1.user_id = Some(user_id);
        repo.create(key1).await.unwrap();

        let mut key2 = create_test_key();
        key2.user_id = Some(user_id);
        repo.create(key2).await.unwrap();

        let mut key3 = create_test_key();
        key3.user_id = Some(Uuid::new_v4());
        repo.create(key3).await.unwrap();

        let keys = repo.list_by_user(user_id).await.unwrap();
        assert_eq!(keys.len(), 2);
    }

    #[tokio::test]
    async fn test_delete_key() {
        let repo = InMemoryKeyRepository::new();
        let key = create_test_key();
        let id = key.id;
        let hash = key.key_hash.clone();

        repo.create(key).await.unwrap();
        repo.delete(id).await.unwrap();

        assert!(repo.find_by_id(id).await.unwrap().is_none());
        assert!(repo.find_by_hash(&hash).await.unwrap().is_none());
    }

    #[tokio::test]
    async fn test_delete_expired() {
        let repo = InMemoryKeyRepository::new();

        // Create an expired key
        let mut key1 = create_test_key();
        key1.expires_at = Some(Utc::now() - chrono::Duration::hours(1));
        repo.create(key1).await.unwrap();

        // Create a valid key
        let key2 = create_test_key();
        let valid_id = key2.id;
        repo.create(key2).await.unwrap();

        let deleted = repo.delete_expired().await.unwrap();
        assert_eq!(deleted, 1);

        // Valid key should still exist
        assert!(repo.find_by_id(valid_id).await.unwrap().is_some());
    }

    #[tokio::test]
    async fn test_count() {
        let repo = InMemoryKeyRepository::new();

        repo.create(create_test_key()).await.unwrap();
        repo.create(create_test_key()).await.unwrap();

        let count = repo.count(None).await.unwrap();
        assert_eq!(count, 2);

        let active_count = repo.count(Some(KeyStatus::Active)).await.unwrap();
        assert_eq!(active_count, 2);
    }

    #[tokio::test]
    async fn test_update_usage() {
        let repo = InMemoryKeyRepository::new();
        let key = create_test_key();
        let id = key.id;

        repo.create(key).await.unwrap();
        repo.update_usage(id, 100, 0.01).await.unwrap();

        let found = repo.find_by_id(id).await.unwrap().unwrap();
        assert_eq!(found.usage_stats.total_requests, 1);
        assert_eq!(found.usage_stats.total_tokens, 100);
    }

    #[tokio::test]
    async fn test_duplicate_hash_rejected() {
        let repo = InMemoryKeyRepository::new();
        let mut key1 = create_test_key();
        let hash = "same_hash".to_string();
        key1.key_hash = hash.clone();

        repo.create(key1).await.unwrap();

        let mut key2 = create_test_key();
        key2.key_hash = hash;

        let result = repo.create(key2).await;
        assert!(result.is_err());
    }
}