litellm-rs 0.1.1

A high-performance AI Gateway written in Rust, providing OpenAI-compatible APIs with intelligent routing, load balancing, and enterprise features
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
//! Configuration builder for type-safe configuration construction
//!
//! This module provides a builder pattern for creating configurations
//! with compile-time validation and better ergonomics.

#![allow(dead_code)] // Builder module - functions may be used in the future

use super::{AuthConfig, Config, GatewayConfig, ProviderConfig, ServerConfig, StorageConfig};
use crate::utils::error::{GatewayError, Result};
use crate::utils::type_utils::{Builder, NonEmptyString, PositiveF64};
use std::collections::HashMap;
use std::time::Duration;

/// Builder for creating gateway configurations
#[derive(Debug, Clone)]
pub struct ConfigBuilder {
    server: Option<ServerConfig>,
    auth: Option<AuthConfig>,
    storage: Option<StorageConfig>,
    providers: Vec<ProviderConfig>,
    features: HashMap<String, bool>,
}

impl ConfigBuilder {
    /// Create a new configuration builder
    pub fn new() -> Self {
        Self {
            server: None,
            auth: None,
            storage: None,
            providers: Vec::new(),
            features: HashMap::new(),
        }
    }

    /// Set the server configuration
    pub fn with_server(mut self, config: ServerConfig) -> Self {
        self.server = Some(config);
        self
    }

    /// Set the authentication configuration
    pub fn with_auth(mut self, config: AuthConfig) -> Self {
        self.auth = Some(config);
        self
    }

    /// Set the storage configuration
    pub fn with_storage(mut self, config: StorageConfig) -> Self {
        self.storage = Some(config);
        self
    }

    /// Add a provider configuration
    pub fn add_provider(mut self, config: ProviderConfig) -> Self {
        self.providers.push(config);
        self
    }

    /// Add multiple provider configurations
    pub fn add_providers(mut self, configs: Vec<ProviderConfig>) -> Self {
        self.providers.extend(configs);
        self
    }

    /// Enable a feature
    pub fn enable_feature(mut self, feature: impl Into<String>) -> Self {
        self.features.insert(feature.into(), true);
        self
    }

    /// Disable a feature
    pub fn disable_feature(mut self, feature: impl Into<String>) -> Self {
        self.features.insert(feature.into(), false);
        self
    }

    /// Build the configuration with validation
    pub fn build(self) -> Result<Config> {
        let gateway = GatewayConfig {
            server: self.server.unwrap_or_default(),
            auth: self.auth.unwrap_or_default(),
            storage: self.storage.unwrap_or_default(),
            providers: self.providers,
            router: super::RouterConfig::default(),
            monitoring: super::MonitoringConfig::default(),
            cache: super::CacheConfig::default(),
            rate_limit: super::RateLimitConfig::default(),
            enterprise: super::EnterpriseConfig::default(),
        };

        let config = Config { gateway };

        // Validate the configuration
        if let Err(e) = config.gateway.validate() {
            return Err(GatewayError::Config(e));
        }

        Ok(config)
    }

    /// Build the configuration or panic with a descriptive message
    pub fn build_or_panic(self) -> Config {
        self.build().unwrap_or_else(|e| {
            panic!("Failed to build configuration: {}", e);
        })
    }
}

impl Default for ConfigBuilder {
    fn default() -> Self {
        Self::new()
    }
}

impl Builder<Config> for ConfigBuilder {
    fn build(self) -> Config {
        self.build().expect("Configuration validation failed")
    }
}

/// Builder for server configuration
#[derive(Debug, Clone)]
pub struct ServerConfigBuilder {
    host: Option<String>,
    port: Option<u16>,
    workers: Option<usize>,
    timeout: Option<Duration>,
    max_connections: Option<usize>,
    enable_cors: bool,
    cors_origins: Vec<String>,
}

impl ServerConfigBuilder {
    /// Create a new server configuration builder
    pub fn new() -> Self {
        Self {
            host: None,
            port: None,
            workers: None,
            timeout: None,
            max_connections: None,
            enable_cors: false,
            cors_origins: Vec::new(),
        }
    }

    /// Set the host
    pub fn host(mut self, host: impl Into<String>) -> Self {
        self.host = Some(host.into());
        self
    }

    /// Set the port
    pub fn port(mut self, port: u16) -> Self {
        self.port = Some(port);
        self
    }

    /// Set the number of workers
    pub fn workers(mut self, workers: usize) -> Self {
        self.workers = Some(workers);
        self
    }

    /// Set the request timeout
    pub fn timeout(mut self, timeout: Duration) -> Self {
        self.timeout = Some(timeout);
        self
    }

    /// Set the maximum number of connections
    pub fn max_connections(mut self, max_connections: usize) -> Self {
        self.max_connections = Some(max_connections);
        self
    }

    /// Enable CORS
    pub fn enable_cors(mut self) -> Self {
        self.enable_cors = true;
        self
    }

    /// Add CORS origin
    pub fn add_cors_origin(mut self, origin: impl Into<String>) -> Self {
        self.cors_origins.push(origin.into());
        self
    }

    /// Build the server configuration
    pub fn build(self) -> ServerConfig {
        ServerConfig {
            host: self.host.unwrap_or_else(|| "127.0.0.1".to_string()),
            port: self.port.unwrap_or(8080),
            workers: self.workers,
            timeout: self.timeout.map(|d| d.as_secs()).unwrap_or(30),
            max_body_size: 1024 * 1024, // 1MB default
            dev_mode: false,
            tls: None,
            cors: super::CorsConfig {
                enabled: self.enable_cors,
                allowed_origins: if self.cors_origins.is_empty() {
                    vec!["*".to_string()]
                } else {
                    self.cors_origins
                },
                allowed_methods: vec!["GET".to_string(), "POST".to_string(), "OPTIONS".to_string()],
                allowed_headers: vec!["Content-Type".to_string(), "Authorization".to_string()],
                max_age: 3600,
                allow_credentials: false,
            },
        }
    }
}

impl Default for ServerConfigBuilder {
    fn default() -> Self {
        Self::new()
    }
}

impl Builder<ServerConfig> for ServerConfigBuilder {
    fn build(self) -> ServerConfig {
        self.build()
    }
}

/// Builder for provider configuration
#[derive(Debug, Clone)]
pub struct ProviderConfigBuilder {
    name: Option<NonEmptyString>,
    provider_type: Option<NonEmptyString>,
    api_key: Option<String>,
    base_url: Option<String>,
    models: Vec<String>,
    max_requests_per_minute: Option<u32>,
    timeout: Option<Duration>,
    enabled: bool,
    weight: Option<PositiveF64>,
}

impl ProviderConfigBuilder {
    /// Create a new provider configuration builder
    pub fn new() -> Self {
        Self {
            name: None,
            provider_type: None,
            api_key: None,
            base_url: None,
            models: Vec::new(),
            max_requests_per_minute: None,
            timeout: None,
            enabled: true,
            weight: None,
        }
    }

    /// Set the provider name
    pub fn name(mut self, name: impl TryInto<NonEmptyString>) -> Result<Self> {
        self.name = Some(
            name.try_into()
                .map_err(|_| GatewayError::Config("Provider name cannot be empty".to_string()))?,
        );
        Ok(self)
    }

    /// Set the provider type
    pub fn provider_type(mut self, provider_type: impl TryInto<NonEmptyString>) -> Result<Self> {
        self.provider_type = Some(
            provider_type
                .try_into()
                .map_err(|_| GatewayError::Config("Provider type cannot be empty".to_string()))?,
        );
        Ok(self)
    }

    /// Set the API key
    pub fn api_key(mut self, api_key: impl Into<String>) -> Self {
        self.api_key = Some(api_key.into());
        self
    }

    /// Set the base URL
    pub fn base_url(mut self, base_url: impl Into<String>) -> Self {
        self.base_url = Some(base_url.into());
        self
    }

    /// Add a supported model
    pub fn add_model(mut self, model: impl Into<String>) -> Self {
        self.models.push(model.into());
        self
    }

    /// Set the rate limit
    pub fn rate_limit(mut self, requests_per_minute: u32) -> Self {
        self.max_requests_per_minute = Some(requests_per_minute);
        self
    }

    /// Set the timeout
    pub fn timeout(mut self, timeout: Duration) -> Self {
        self.timeout = Some(timeout);
        self
    }

    /// Enable the provider
    pub fn enable(mut self) -> Self {
        self.enabled = true;
        self
    }

    /// Disable the provider
    pub fn disable(mut self) -> Self {
        self.enabled = false;
        self
    }

    /// Set the provider weight for load balancing
    pub fn weight(mut self, weight: f64) -> Result<Self> {
        self.weight =
            Some(PositiveF64::new(weight).map_err(|_| {
                GatewayError::Config("Provider weight must be positive".to_string())
            })?);
        Ok(self)
    }

    /// Build the provider configuration
    pub fn build(self) -> Result<ProviderConfig> {
        let name = self
            .name
            .ok_or_else(|| GatewayError::Config("Provider name is required".to_string()))?;

        let provider_type = self
            .provider_type
            .ok_or_else(|| GatewayError::Config("Provider type is required".to_string()))?;

        Ok(ProviderConfig {
            name: name.into_string(),
            provider_type: provider_type.into_string(),
            api_key: self.api_key.unwrap_or_default(),
            base_url: self.base_url,
            api_version: None,
            organization: None,
            project: None,
            weight: self.weight.map(|w| w.get() as f32).unwrap_or(1.0),
            rpm: self.max_requests_per_minute.unwrap_or(1000),
            tpm: 100000, // Default TPM
            max_concurrent_requests: 10,
            timeout: self.timeout.map(|d| d.as_secs()).unwrap_or(30),
            max_retries: 3,
            retry: super::RetryConfig::default(),
            health_check: super::HealthCheckConfig::default(),
            settings: std::collections::HashMap::new(),
            models: self.models,
            enabled: self.enabled,
            tags: Vec::new(),
        })
    }
}

impl Default for ProviderConfigBuilder {
    fn default() -> Self {
        Self::new()
    }
}

/// Convenience functions for common configurations
pub mod presets {
    use super::*;

    /// Create a development server configuration
    pub fn dev_server() -> ServerConfigBuilder {
        ServerConfigBuilder::new()
            .host("127.0.0.1")
            .port(8080)
            .workers(1)
            .enable_cors()
            .add_cors_origin("*")
    }

    /// Create a production server configuration
    pub fn prod_server() -> ServerConfigBuilder {
        ServerConfigBuilder::new()
            .host("0.0.0.0")
            .port(8080)
            .workers(num_cpus::get())
            .max_connections(10000)
            .timeout(Duration::from_secs(60))
    }

    /// Create an OpenAI provider configuration
    pub fn openai_provider(name: &str, api_key: &str) -> Result<ProviderConfigBuilder> {
        Ok(ProviderConfigBuilder::new()
            .name(name)?
            .provider_type("openai")?
            .api_key(api_key)
            .add_model("gpt-3.5-turbo")
            .add_model("gpt-4")
            .rate_limit(3000))
    }

    /// Create an Anthropic provider configuration
    pub fn anthropic_provider(name: &str, api_key: &str) -> Result<ProviderConfigBuilder> {
        Ok(ProviderConfigBuilder::new()
            .name(name)?
            .provider_type("anthropic")?
            .api_key(api_key)
            .add_model("claude-3-sonnet")
            .add_model("claude-3-haiku")
            .rate_limit(1000))
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_config_builder() {
        let config = ConfigBuilder::new()
            .with_server(presets::dev_server().build())
            .add_provider(
                presets::openai_provider("openai", "test-key")
                    .unwrap()
                    .build()
                    .unwrap(),
            )
            .enable_feature("metrics")
            .build();

        assert!(config.is_ok());
        let config = config.unwrap();
        assert_eq!(config.gateway.server.port, 8080);
        assert_eq!(config.gateway.providers.len(), 1);
    }

    #[test]
    fn test_provider_builder() {
        let provider = ProviderConfigBuilder::new()
            .name("test-provider")
            .unwrap()
            .provider_type("openai")
            .unwrap()
            .api_key("test-key")
            .add_model("gpt-4")
            .weight(2.0)
            .unwrap()
            .build();

        assert!(provider.is_ok());
        let provider = provider.unwrap();
        assert_eq!(provider.name, "test-provider");
        assert_eq!(provider.weight, 2.0);
    }
}