use std::env;
pub use terraphim_config::llm_router::{LlmRouterConfig, RouterMode, RouterStrategy};
#[derive(Debug, Clone, Default)]
pub struct MergedRouterConfig {
pub enabled: bool,
pub mode: RouterMode,
pub proxy_url: Option<String>,
pub taxonomy_path: Option<String>,
pub cost_optimization_enabled: bool,
pub performance_optimization_enabled: bool,
pub strategy: RouterStrategy,
}
impl MergedRouterConfig {
pub fn from_role_and_env(role_config: Option<&LlmRouterConfig>) -> Self {
let mut config = role_config.cloned().unwrap_or_default();
if let Ok(url) = env::var("LLM_PROXY_URL") {
config.proxy_url = Some(url);
}
if let Ok(path) = env::var("LLM_TAXONOMY_PATH") {
config.taxonomy_path = Some(path);
}
if let Ok(val) = env::var("LLM_COST_OPTIMIZATION") {
config.cost_optimization_enabled = val.parse().unwrap_or(false);
}
if let Ok(val) = env::var("LLM_PERFORMANCE_OPTIMIZATION") {
config.performance_optimization_enabled = val.parse().unwrap_or(false);
}
if let Ok(val) = env::var("LLM_ROUTING_STRATEGY") {
config.strategy = serde_json::from_str(&val).unwrap_or(RouterStrategy::Balanced);
}
config.into()
}
pub fn is_enabled(&self) -> bool {
self.enabled
}
pub fn get_proxy_url(&self) -> String {
self.proxy_url.clone().unwrap_or_else(|| match self.mode {
RouterMode::Service => "http://127.0.0.1:3456".to_string(),
RouterMode::Library => panic!("Library mode should not use proxy URL"),
})
}
}
impl From<LlmRouterConfig> for MergedRouterConfig {
fn from(config: LlmRouterConfig) -> Self {
Self {
enabled: config.enabled,
mode: config.mode,
proxy_url: config.proxy_url,
taxonomy_path: config.taxonomy_path,
cost_optimization_enabled: config.cost_optimization_enabled,
performance_optimization_enabled: config.performance_optimization_enabled,
strategy: config.strategy,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_merged_config_defaults() {
let config = MergedRouterConfig::default();
assert!(!config.enabled);
assert_eq!(config.mode, RouterMode::Library);
assert!(config.proxy_url.is_none());
assert_eq!(config.strategy, RouterStrategy::Balanced);
}
#[test]
fn test_merged_config_from_role() {
let role_config = LlmRouterConfig {
enabled: true,
mode: RouterMode::Service,
proxy_url: Some("http://custom-proxy:8080".to_string()),
strategy: RouterStrategy::CostFirst,
..Default::default()
};
let merged = MergedRouterConfig::from_role_and_env(Some(&role_config));
assert!(merged.enabled);
assert_eq!(merged.mode, RouterMode::Service);
assert_eq!(
merged.proxy_url,
Some("http://custom-proxy:8080".to_string())
);
assert_eq!(merged.strategy, RouterStrategy::CostFirst);
}
#[test]
fn test_env_overrides() {
unsafe {
env::set_var("LLM_PROXY_URL", "http://env-proxy:9999");
}
let role_config = LlmRouterConfig {
enabled: true,
mode: RouterMode::Service,
strategy: RouterStrategy::Balanced,
..Default::default()
};
let merged = MergedRouterConfig::from_role_and_env(Some(&role_config));
assert_eq!(merged.proxy_url, Some("http://env-proxy:9999".to_string()));
unsafe {
env::remove_var("LLM_PROXY_URL");
}
}
}