modelmux 1.0.0

ModelMux - high-performance Rust gateway that translates OpenAI-compatible API requests to Vertex AI (Claude), with streaming, tool calling, and production-grade reliability.
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
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
//!
//! Configuration loading system for ModelMux.
//!
//! This module implements a multi-layered configuration loading system following
//! industry best practices:
//! 1. CLI arguments (highest priority)
//! 2. Environment variables
//! 3. User config file (~/.config/modelmux/config.toml)
//! 4. System config file (/etc/modelmux/config.toml)
//! 5. Built-in defaults (lowest priority)
//!
//! Follows the Builder pattern (Open/Closed Principle) and Single Responsibility
//! Principle - handles only configuration loading concerns.
//!
//! Authors:
//!   Jaro <yarenty@gmail.com>
//!
//! Copyright (c) 2026 SkyCorp

/* --- uses ------------------------------------------------------------------------------------ */

use crate::config::paths;
use crate::config::{AuthConfig, Config, LogLevel, ServerConfig, StreamingConfig, StreamingMode};
use crate::error::{ProxyError, Result};

use std::collections::HashMap;
use std::env;
use std::path::Path;

/* --- types ----------------------------------------------------------------------------------- */

///
/// Configuration loader implementing the Builder pattern.
///
/// Provides a fluent interface for building configuration from multiple sources
/// in the correct precedence order. Each method returns self for chaining.
pub struct ConfigLoader {
    /// Current configuration being built
    config: Config,
    /// Environment variable overrides collected
    env_overrides: HashMap<String, String>,
    /// Whether defaults have been applied
    defaults_applied: bool,
}

/* --- implementations --------------------------------------------------------------------- */

impl ConfigLoader {
    /// Create a new configuration loader
    ///
    /// Initializes an empty loader ready to build configuration from multiple sources.
    ///
    /// # Returns
    /// * ConfigLoader instance ready for configuration building
    ///
    /// # Examples
    /// ```rust,no_run
    /// use modelmux::config::loader::ConfigLoader;
    ///
    /// # fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// let config = ConfigLoader::new()
    ///     .with_defaults()
    ///     .with_user_config()?
    ///     .with_env_vars()?
    ///     .build_base()?;
    /// # Ok(())
    /// # }
    /// ```
    pub fn new() -> Self {
        Self { config: Config::default(), env_overrides: HashMap::new(), defaults_applied: false }
    }

    /// Apply built-in default values
    ///
    /// Sets sensible defaults for all configuration values. This should
    /// typically be called first in the configuration loading chain.
    ///
    /// # Returns
    /// * Self for method chaining
    pub fn with_defaults(mut self) -> Self {
        self.config = Config::default();
        self.defaults_applied = true;
        self
    }

    /// Load system-wide configuration file
    ///
    /// Attempts to load configuration from the system config file:
    /// - Linux: /etc/modelmux/config.toml
    /// - macOS: /Library/Preferences/modelmux/config.toml
    /// - Windows: %PROGRAMDATA%/modelmux/config.toml
    ///
    /// If the file doesn't exist, this is not considered an error.
    ///
    /// # Returns
    /// * `Ok(Self)` - System config loaded or skipped (file not found)
    /// * `Err(ProxyError)` - System config exists but failed to load
    pub fn with_system_config(mut self) -> Result<Self> {
        let system_config_path = paths::system_config_file()?;

        if system_config_path.exists() {
            tracing::debug!("Loading system config from: {}", system_config_path.display());
            self.load_config_file(&system_config_path)?;
        } else {
            tracing::debug!("System config not found at: {}", system_config_path.display());
        }

        Ok(self)
    }

    /// Load user configuration file
    ///
    /// Attempts to load configuration from the user config file:
    /// - Linux: ~/.config/modelmux/config.toml
    /// - macOS: ~/Library/Application Support/modelmux/config.toml
    /// - Windows: %APPDATA%/modelmux/config.toml
    ///
    /// If the file doesn't exist, this is not considered an error.
    ///
    /// # Returns
    /// * `Ok(Self)` - User config loaded or skipped (file not found)
    /// * `Err(ProxyError)` - User config exists but failed to load
    pub fn with_user_config(mut self) -> Result<Self> {
        let user_config_path = paths::user_config_file()?;

        if user_config_path.exists() {
            tracing::debug!("Loading user config from: {}", user_config_path.display());
            self.load_config_file(&user_config_path)?;
        } else {
            tracing::debug!("User config not found at: {}", user_config_path.display());
        }

        Ok(self)
    }

    /// Load configuration from specific file path
    ///
    /// Loads configuration from a custom file path. Useful for testing
    /// or when users want to specify a custom config location.
    ///
    /// # Arguments
    /// * `path` - Path to configuration file to load
    ///
    /// # Returns
    /// * `Ok(Self)` - Config loaded successfully
    /// * `Err(ProxyError)` - Failed to load or parse config file
    #[allow(dead_code)]
    pub fn with_config_file<P: AsRef<Path>>(mut self, path: P) -> Result<Self> {
        let path = path.as_ref();
        tracing::debug!("Loading custom config from: {}", path.display());
        self.load_config_file(path)?;
        Ok(self)
    }

    /// Apply environment variable overrides
    ///
    /// Loads configuration values from environment variables using the
    /// MODELMUX_ prefix. Environment variables take precedence over config files.
    ///
    /// Supported environment variables:
    /// - MODELMUX_SERVER_PORT
    /// - MODELMUX_SERVER_LOG_LEVEL
    /// - MODELMUX_AUTH_SERVICE_ACCOUNT_FILE
    /// - MODELMUX_LLM_PROVIDER_PROJECT_ID
    /// - ... and more
    ///
    /// # Returns
    /// * `Ok(Self)` - Environment variables applied
    /// * `Err(ProxyError)` - Invalid environment variable values
    pub fn with_env_vars(mut self) -> Result<Self> {
        tracing::debug!("Loading configuration from environment variables");

        // Collect all MODELMUX_ environment variables
        for (key, value) in env::vars() {
            if key.starts_with("MODELMUX_") {
                self.env_overrides.insert(key, value);
            }
        }

        // Apply environment variable overrides
        self.apply_env_overrides()?;

        Ok(self)
    }

    /// Build the final configuration
    ///
    /// Validates the final configuration and returns it. This should be
    /// called last in the configuration loading chain.
    ///
    /// # Returns
    /// * `Ok(Config)` - Valid, fully-loaded configuration
    /// * `Err(ProxyError)` - Configuration validation failed
    #[allow(dead_code)]
    pub fn build(self) -> Result<Config> {
        if !self.defaults_applied {
            return Err(ProxyError::Config(
                "Configuration loader must call with_defaults() before build()".to_string(),
            ));
        }

        // Validate the final configuration
        self.config.validate()?;

        tracing::info!("Configuration loaded successfully");
        tracing::debug!(
            "Final config: server.port={}, server.log_level={:?}, streaming.mode={:?}",
            self.config.server.port,
            self.config.server.log_level,
            self.config.streaming.mode
        );

        Ok(self.config)
    }

    /// Build configuration without provider validation (for use with external provider loading)
    ///
    /// This method builds the configuration but skips provider validation,
    /// allowing the provider to be loaded separately using the existing system.
    ///
    /// # Returns
    /// * `Ok(Config)` - Configuration with basic validation
    /// * `Err(ProxyError)` - Configuration loading failed
    pub fn build_base(self) -> Result<Config> {
        if !self.defaults_applied {
            return Err(ProxyError::Config(
                "Configuration loader must call with_defaults() before build()".to_string(),
            ));
        }

        tracing::info!("Base configuration loaded successfully");
        tracing::debug!(
            "Base config: server.port={}, server.log_level={:?}, streaming.mode={:?}",
            self.config.server.port,
            self.config.server.log_level,
            self.config.streaming.mode
        );

        Ok(self.config)
    }

    /* --- private methods ----------------------------------------------------------------- */

    /// Load and merge configuration from a TOML file
    fn load_config_file<P: AsRef<Path>>(&mut self, path: P) -> Result<()> {
        let path = path.as_ref();

        // Validate file exists and is readable
        paths::validate_config_file(path)?;

        // Read file contents
        let contents = std::fs::read_to_string(path).map_err(|e| {
            ProxyError::Config(format!(
                "Failed to read configuration file '{}': {}",
                path.display(),
                e
            ))
        })?;

        // Parse TOML
        let file_config: Config = toml::from_str(&contents).map_err(|e| {
            ProxyError::Config(format!(
                "Failed to parse TOML configuration file '{}': {}\n\
                 \n\
                 Please check the syntax of your configuration file.\n\
                 Common issues:\n\
                 1. Missing quotes around string values\n\
                 2. Invalid TOML syntax\n\
                 3. Incorrect section names or field names\n\
                 \n\
                 You can validate your TOML syntax at: https://www.toml-lint.com/\n\
                 \n\
                 Run 'modelmux config validate' for more details.",
                path.display(),
                e
            ))
        })?;

        // Merge configuration (file config overrides current config)
        self.merge_config(file_config);

        tracing::debug!("Successfully loaded config from: {}", path.display());
        Ok(())
    }

    /// Merge another config into the current config
    fn merge_config(&mut self, other: Config) {
        // Merge server config
        self.merge_server_config(other.server);

        // Merge LLM provider config if present
        if other.llm_provider.is_some() {
            self.config.llm_provider = other.llm_provider;
        }

        // Merge vertex config if present
        if other.vertex.is_some() {
            self.config.vertex = other.vertex;
        }

        // Merge auth config
        self.merge_auth_config(other.auth);

        // Merge streaming config
        self.merge_streaming_config(other.streaming);
    }

    /// Merge server configuration
    fn merge_server_config(&mut self, other: ServerConfig) {
        // Only override if the value is explicitly set (not default)
        if other.port != ServerConfig::default().port {
            self.config.server.port = other.port;
        }

        // For enums, we need to check if they're different from default
        // Since we can't easily detect "explicitly set", we always merge
        self.config.server.log_level = other.log_level;
        self.config.server.enable_retries = other.enable_retries;

        if other.max_retry_attempts != ServerConfig::default().max_retry_attempts {
            self.config.server.max_retry_attempts = other.max_retry_attempts;
        }
    }

    /// Merge authentication configuration
    fn merge_auth_config(&mut self, other: AuthConfig) {
        if other.service_account_file.is_some() {
            self.config.auth.service_account_file = other.service_account_file;
        }

        if other.service_account_json.is_some() {
            self.config.auth.service_account_json = other.service_account_json;
        }

        // Always merge strategy
        self.config.auth.strategy = other.strategy;
    }

    /// Merge streaming configuration
    fn merge_streaming_config(&mut self, other: StreamingConfig) {
        self.config.streaming.mode = other.mode;

        if other.buffer_size != StreamingConfig::default().buffer_size {
            self.config.streaming.buffer_size = other.buffer_size;
        }

        if other.chunk_timeout_ms != StreamingConfig::default().chunk_timeout_ms {
            self.config.streaming.chunk_timeout_ms = other.chunk_timeout_ms;
        }
    }

    /// Apply environment variable overrides to current configuration
    fn apply_env_overrides(&mut self) -> Result<()> {
        for (key, value) in &self.env_overrides {
            match key.as_str() {
                // Server configuration
                "MODELMUX_SERVER_PORT" => {
                    self.config.server.port = value.parse().map_err(|e| {
                        ProxyError::Config(format!(
                            "Invalid MODELMUX_SERVER_PORT value '{}': {}\n\
                             Port must be a number between 1 and 65535.",
                            value, e
                        ))
                    })?;
                }
                "MODELMUX_SERVER_LOG_LEVEL" => {
                    self.config.server.log_level = LogLevel::from_str(value)?;
                }
                "MODELMUX_SERVER_ENABLE_RETRIES" => {
                    self.config.server.enable_retries = parse_bool_env(value, key)?;
                }
                "MODELMUX_SERVER_MAX_RETRY_ATTEMPTS" => {
                    self.config.server.max_retry_attempts = value.parse().map_err(|e| {
                        ProxyError::Config(format!(
                            "Invalid MODELMUX_SERVER_MAX_RETRY_ATTEMPTS value '{}': {}",
                            value, e
                        ))
                    })?;
                }

                // Authentication configuration
                "MODELMUX_AUTH_SERVICE_ACCOUNT_FILE" => {
                    self.config.auth.service_account_file = Some(value.clone());
                }
                "MODELMUX_AUTH_SERVICE_ACCOUNT_JSON" => {
                    self.config.auth.service_account_json = Some(value.clone());
                }

                // Streaming configuration
                "MODELMUX_STREAMING_MODE" => {
                    self.config.streaming.mode = StreamingMode::from_str(value)?;
                }
                "MODELMUX_STREAMING_BUFFER_SIZE" => {
                    self.config.streaming.buffer_size = value.parse().map_err(|e| {
                        ProxyError::Config(format!(
                            "Invalid MODELMUX_STREAMING_BUFFER_SIZE value '{}': {}",
                            value, e
                        ))
                    })?;
                }
                "MODELMUX_STREAMING_CHUNK_TIMEOUT_MS" => {
                    self.config.streaming.chunk_timeout_ms = value.parse().map_err(|e| {
                        ProxyError::Config(format!(
                            "Invalid MODELMUX_STREAMING_CHUNK_TIMEOUT_MS value '{}': {}",
                            value, e
                        ))
                    })?;
                }

                // LLM Provider configuration (delegate to provider)
                key if key.starts_with("MODELMUX_LLM_PROVIDER_") => {
                    // Let the LlmProviderConfig handle its own env vars
                    // This will be handled when LlmProviderConfig::from_env() is called
                    tracing::debug!("LLM provider env var will be handled by provider: {}", key);
                }

                // Legacy environment variables for backward compatibility
                "GCP_SERVICE_ACCOUNT_KEY" => {
                    tracing::warn!(
                        "GCP_SERVICE_ACCOUNT_KEY is deprecated. Please use MODELMUX_AUTH_SERVICE_ACCOUNT_JSON or config file."
                    );
                    self.config.auth.service_account_json = Some(value.clone());
                }
                "PORT" => {
                    tracing::warn!(
                        "PORT environment variable is deprecated. Please use MODELMUX_SERVER_PORT."
                    );
                    self.config.server.port = value.parse().map_err(|e| {
                        ProxyError::Config(format!("Invalid PORT value '{}': {}", value, e))
                    })?;
                }

                // Unknown environment variable
                _ => {
                    tracing::debug!("Ignoring unknown environment variable: {}", key);
                }
            }
        }

        Ok(())
    }
}

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

/* --- utility functions ------------------------------------------------------------------- */

/// Parse boolean value from environment variable
fn parse_bool_env(value: &str, var_name: &str) -> Result<bool> {
    match value.to_lowercase().as_str() {
        "true" | "yes" | "1" | "on" | "enabled" => Ok(true),
        "false" | "no" | "0" | "off" | "disabled" => Ok(false),
        _ => Err(ProxyError::Config(format!(
            "Invalid boolean value for {}: '{}'\n\
             Valid values: true/false, yes/no, 1/0, on/off, enabled/disabled",
            var_name, value
        ))),
    }
}

/* --- tests ------------------------------------------------------------------------------- */

#[cfg(test)]
mod tests {
    use super::*;
    use std::fs;
    use tempfile::TempDir;

    #[test]
    fn test_config_loader_defaults() {
        let config =
            ConfigLoader::new().with_defaults().build_base().expect("Should build with defaults");

        assert_eq!(config.server.port, 3000);
        assert!(matches!(config.server.log_level, LogLevel::Info));
        assert!(config.server.enable_retries);
        assert_eq!(config.server.max_retry_attempts, 3);
    }

    #[test]
    fn test_config_loader_with_file() {
        let temp_dir = TempDir::new().unwrap();
        let config_file = temp_dir.path().join("config.toml");

        let config_content = r#"
[server]
port = 8080
log_level = "debug"

[auth]
service_account_json = '{"type":"service_account","project_id":"test","private_key_id":"test","private_key":"-----BEGIN PRIVATE KEY-----\ntest\n-----END PRIVATE KEY-----","client_email":"test@test.com","client_id":"test","auth_uri":"https://accounts.google.com/o/oauth2/auth","token_uri":"https://oauth2.googleapis.com/token","auth_provider_x509_cert_url":"https://www.googleapis.com/oauth2/v1/certs","client_x509_cert_url":"test"}'

[streaming]
mode = "standard"
"#;

        fs::write(&config_file, config_content).unwrap();

        let config = ConfigLoader::new()
            .with_defaults()
            .with_config_file(&config_file)
            .expect("Should create loader")
            .build_base()
            .expect("Should load custom config file");

        assert_eq!(config.server.port, 8080);
        assert!(matches!(config.server.log_level, LogLevel::Debug));
        assert!(matches!(config.streaming.mode, StreamingMode::Standard));
    }

    #[test]
    fn test_env_var_overrides() {
        temp_env::with_vars(
            [
                ("MODELMUX_SERVER_PORT", Some("9090")),
                ("MODELMUX_SERVER_LOG_LEVEL", Some("error")),
                ("MODELMUX_STREAMING_MODE", Some("never")),
                (
                    "MODELMUX_AUTH_SERVICE_ACCOUNT_JSON",
                    Some(
                        r#"{"type":"service_account","project_id":"test","private_key_id":"test","private_key":"-----BEGIN PRIVATE KEY-----\ntest\n-----END PRIVATE KEY-----","client_email":"test@test.com","client_id":"test","auth_uri":"https://accounts.google.com/o/oauth2/auth","token_uri":"https://oauth2.googleapis.com/token","auth_provider_x509_cert_url":"https://www.googleapis.com/oauth2/v1/certs","client_x509_cert_url":"test"}"#,
                    ),
                ),
            ],
            || {
                let config = ConfigLoader::new()
                    .with_defaults()
                    .with_env_vars()
                    .expect("Should apply env vars")
                    .build_base()
                    .expect("Should build with env vars");

                assert_eq!(config.server.port, 9090);
                assert!(matches!(config.server.log_level, LogLevel::Error));
                assert!(matches!(config.streaming.mode, StreamingMode::Never));
            },
        );
    }

    #[test]
    fn test_precedence_order() {
        let temp_dir = TempDir::new().unwrap();
        let config_file = temp_dir.path().join("config.toml");

        // Config file sets port to 7070
        let config_content = r#"
[server]
port = 7070

[auth]
service_account_json = '{"type":"service_account","project_id":"test","private_key_id":"test","private_key":"-----BEGIN PRIVATE KEY-----\ntest\n-----END PRIVATE KEY-----","client_email":"test@test.com","client_id":"test","auth_uri":"https://accounts.google.com/o/oauth2/auth","token_uri":"https://oauth2.googleapis.com/token","auth_provider_x509_cert_url":"https://www.googleapis.com/oauth2/v1/certs","client_x509_cert_url":"test"}'

[streaming]
mode = "auto"
"#;
        fs::write(&config_file, config_content).unwrap();

        // Environment variable should override config file
        temp_env::with_vars([("MODELMUX_SERVER_PORT", Some("8080"))], || {
            let config = ConfigLoader::new()
                .with_defaults()
                .with_config_file(&config_file)
                .expect("Should create loader")
                .with_env_vars()
                .expect("Should apply env vars")
                .build_base()
                .expect("Should build with precedence");

            // Env var should win over config file
            assert_eq!(config.server.port, 8080);
        });
    }

    #[test]
    fn test_invalid_toml_error() {
        let temp_dir = TempDir::new().unwrap();
        let config_file = temp_dir.path().join("config.toml");

        // Invalid TOML content
        let invalid_content = r#"
[server
port = 8080
"#;
        fs::write(&config_file, invalid_content).unwrap();

        let result = ConfigLoader::new()
            .with_defaults()
            .with_config_file(&config_file)
            .and_then(|loader| loader.build_base());

        assert!(result.is_err());
        let error_msg = format!("{}", result.unwrap_err());
        assert!(error_msg.contains("Failed to parse TOML"));
    }

    #[test]
    fn test_boolean_env_parsing() {
        assert!(parse_bool_env("true", "TEST").unwrap());
        assert!(parse_bool_env("yes", "TEST").unwrap());
        assert!(parse_bool_env("1", "TEST").unwrap());
        assert!(parse_bool_env("on", "TEST").unwrap());
        assert!(parse_bool_env("enabled", "TEST").unwrap());

        assert!(!parse_bool_env("false", "TEST").unwrap());
        assert!(!parse_bool_env("no", "TEST").unwrap());
        assert!(!parse_bool_env("0", "TEST").unwrap());
        assert!(!parse_bool_env("off", "TEST").unwrap());
        assert!(!parse_bool_env("disabled", "TEST").unwrap());

        assert!(parse_bool_env("invalid", "TEST").is_err());
    }
}