diff --git a/Cargo.lock b/Cargo.lock index d894122..987230c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2916,6 +2916,7 @@ version = "0.3.0" dependencies = [ "anyhow", "clap", + "tempfile", "tokio", "tracing", "tracing-subscriber", diff --git a/crates/cli/Cargo.toml b/crates/cli/Cargo.toml index d46ecce..8a23065 100644 --- a/crates/cli/Cargo.toml +++ b/crates/cli/Cargo.toml @@ -28,3 +28,6 @@ valohai-conduit-core.workspace = true valohai-conduit-proxy.workspace = true valohai-conduit-storage.workspace = true valohai-conduit-tui.workspace = true + +[dev-dependencies] +tempfile.workspace = true diff --git a/crates/cli/src/main.rs b/crates/cli/src/main.rs index a1d8728..fceda75 100644 --- a/crates/cli/src/main.rs +++ b/crates/cli/src/main.rs @@ -75,6 +75,10 @@ async fn main() -> anyhow::Result<()> { let config = conduit_core::Config::load(cli.config_path)?; tracing::debug!(?config, "config loaded"); + if matches!(cmd, Commands::Proxy) { + // before opening the database, which creates it, so a bad config leaves no files behind + config.ensure_providers()?; + } conduit_core::cost::set_custom_pricing(&config.pricing); let transit_storage = if matches!(cmd, Commands::Dashboard { .. }) { diff --git a/crates/cli/tests/config_errors.rs b/crates/cli/tests/config_errors.rs new file mode 100644 index 0000000..a44cf0b --- /dev/null +++ b/crates/cli/tests/config_errors.rs @@ -0,0 +1,86 @@ +use std::path::Path; +use std::process::{Command, Output}; + +use conduit_core::config::{CONFIG_ENV_VAR, DATABASE_ENV_VAR}; + +/// Runs `conduit` in `dir`, ignoring any config or database set in the developer's shell. +fn conduit_in(dir: &Path, args: &[&str]) -> Output { + Command::new(env!("CARGO_BIN_EXE_conduit")) + .args(args) + .current_dir(dir) + .env_remove(CONFIG_ENV_VAR) + .env_remove(DATABASE_ENV_VAR) + .output() + .expect("failed to run conduit") +} + +fn files_in(dir: &Path) -> Vec { + let mut names: Vec<_> = std::fs::read_dir(dir) + .expect("failed to list directory") + .map(|entry| entry.unwrap().file_name().to_string_lossy().into_owned()) + .collect(); + names.sort(); + names +} + +#[test] +fn proxy_without_a_config_leaves_the_directory_empty() { + let dir = tempfile::tempdir().unwrap(); + + let output = conduit_in(dir.path(), &[]); + + assert!(!output.status.success()); + assert!( + String::from_utf8_lossy(&output.stderr) + .contains("No providers configured: there is no config file at ./conduit.toml."), + "{output:?}" + ); + assert_eq!(files_in(dir.path()), Vec::::new()); +} + +#[test] +fn proxy_with_an_empty_config_creates_no_database() { + let dir = tempfile::tempdir().unwrap(); + std::fs::write(dir.path().join("conduit.toml"), "").unwrap(); + + let output = conduit_in(dir.path(), &["proxy"]); + + assert!(!output.status.success()); + assert!( + String::from_utf8_lossy(&output.stderr) + .contains("No providers configured in conduit.toml."), + "{output:?}" + ); + assert_eq!(files_in(dir.path()), ["conduit.toml"]); +} + +#[test] +fn proxy_names_the_config_file_given_with_config() { + let dir = tempfile::tempdir().unwrap(); + std::fs::write(dir.path().join("other.toml"), "listen = \":0\"").unwrap(); + + let output = conduit_in(dir.path(), &["--config", "other.toml"]); + + assert!(!output.status.success()); + let stderr = String::from_utf8_lossy(&output.stderr); + assert!( + stderr.contains("No providers configured in other.toml."), + "{output:?}" + ); + assert!(!stderr.contains("./conduit.toml"), "{output:?}"); + assert_eq!(files_in(dir.path()), ["other.toml"]); +} + +#[test] +fn dashboard_does_not_require_providers() { + let dir = tempfile::tempdir().unwrap(); + + let output = conduit_in(dir.path(), &["dashboard"]); + + // it gets as far as the database, which doesn't exist yet + assert!(!output.status.success()); + let stderr = String::from_utf8_lossy(&output.stderr); + assert!(stderr.contains("failed to open database"), "{output:?}"); + assert!(!stderr.contains("No providers"), "{output:?}"); + assert_eq!(files_in(dir.path()), Vec::::new()); +} diff --git a/crates/core/src/config.rs b/crates/core/src/config.rs index 7555959..13b7318 100644 --- a/crates/core/src/config.rs +++ b/crates/core/src/config.rs @@ -15,6 +15,21 @@ pub struct Config { /// Model name prefix -> rates; checked before the built-in pricing table. pub pricing: HashMap, pub upstream: UpstreamConfig, + /// Where this config was read from, for error messages; not a config key. + #[serde(skip)] + pub source: ConfigSource, +} + +/// Where a [`Config`] came from, so errors can point at the file to fix. +#[derive(Clone, Debug, Default, PartialEq)] +pub enum ConfigSource { + /// not loaded from disk, e.g. `Config::default()` or a config built in code + #[default] + NotLoaded, + /// no config file was given and there was none at this default path + MissingDefault(PathBuf), + /// read from this file + File(PathBuf), } impl Default for Config { @@ -26,6 +41,7 @@ impl Default for Config { valohai_llm: ValohaiLlmConfig::default(), pricing: HashMap::new(), upstream: UpstreamConfig::default(), + source: ConfigSource::default(), } } } @@ -85,9 +101,10 @@ impl Config { read_env_var: impl Fn(&str) -> Option, ) -> anyhow::Result { match std::fs::read_to_string(path) { - Err(e) if e.kind() == std::io::ErrorKind::NotFound => { - Ok(Self::from_toml_str("", read_env_var)?) - } + Err(e) if e.kind() == std::io::ErrorKind::NotFound => Ok(Self { + source: ConfigSource::MissingDefault(path.to_path_buf()), + ..Self::from_toml_str("", read_env_var)? + }), _ => Self::from_path(path, read_env_var), } } @@ -98,8 +115,12 @@ impl Config { ) -> anyhow::Result { let contents = std::fs::read_to_string(path) .with_context(|| format!("failed to read {}", path.display()))?; - Self::from_toml_str(&contents, read_env_var) - .with_context(|| format!("failed to parse {}", path.display())) + let config = Self::from_toml_str(&contents, read_env_var) + .with_context(|| format!("failed to parse {}", path.display()))?; + Ok(Self { + source: ConfigSource::File(path.to_path_buf()), + ..config + }) } fn from_toml_str( @@ -138,6 +159,29 @@ impl Config { self.valohai_llm.api_key = llm_api_key; } } + + /// The proxy has nothing to route without providers; the dashboard doesn't need any. + pub fn ensure_providers(&self) -> anyhow::Result<()> { + anyhow::ensure!(!self.providers.is_empty(), self.no_providers_message()); + Ok(()) + } + + fn no_providers_message(&self) -> String { + let section = "a `[providers.]` section (see conduit.example.toml)"; + let elsewhere = format!("use --config or {CONFIG_ENV_VAR} to point elsewhere"); + match &self.source { + ConfigSource::NotLoaded => format!("No providers configured. Add {section}."), + // `./conduit.toml` makes it clear the working directory was searched + ConfigSource::MissingDefault(path) => format!( + "No providers configured: there is no config file at {}. Create one with {section}, or {elsewhere}.", + Path::new(".").join(path).display() + ), + ConfigSource::File(path) => format!( + "No providers configured in {}. Add {section}, or {elsewhere}.", + path.display() + ), + } + } } /// USD per million tokens, the same shape as an entry in `pricing.json`. @@ -314,7 +358,13 @@ upstream = "https://api.anthropic.com" let path = dir.path().join("conduit.toml"); let config = Config::from_path_if_exists(&path, |_| None)?; - assert_eq!(config, Config::default()); + assert_eq!( + config, + Config { + source: ConfigSource::MissingDefault(path.clone()), + ..Config::default() + } + ); std::fs::write(&path, "listen = [[[invalid")?; assert!(Config::from_path_if_exists(&path, |_| None).is_err()); @@ -432,4 +482,78 @@ output = 0.0 std::fs::remove_dir_all(&dir)?; Ok(()) } + + #[test] + fn ensure_providers_requires_at_least_one_provider() -> anyhow::Result<()> { + let config = Config::from_toml_str("", |_| None)?; + assert!(config.ensure_providers().is_err()); + + let config = Config::from_toml_str("[providers.openai]\nupstream = \"x\"", |_| None)?; + config.ensure_providers()?; + Ok(()) + } + + #[test] + fn loaded_config_remembers_which_file_it_came_from() -> anyhow::Result<()> { + let dir = tempfile::tempdir()?; + let path = dir.path().join("conduit.toml"); + std::fs::write(&path, "")?; + + let config = Config::load_with_env(Some(path.clone()), |_| None)?; + assert_eq!(config.source, ConfigSource::File(path.clone())); + + let config = Config::from_path_if_exists(&path, |_| None)?; + assert_eq!(config.source, ConfigSource::File(path)); + Ok(()) + } + + #[test] + fn source_cannot_be_set_from_the_config_file() -> anyhow::Result<()> { + let config = Config::from_toml_str(r#"source = "elsewhere.toml""#, |_| None)?; + assert_eq!(config.source, ConfigSource::NotLoaded); + Ok(()) + } + + #[test] + fn no_providers_error_says_no_config_file_was_found() { + let config = Config { + source: ConfigSource::MissingDefault(PathBuf::from("conduit.toml")), + ..Config::default() + }; + let err = config.ensure_providers().unwrap_err().to_string(); + assert_eq!( + err, + "No providers configured: there is no config file at ./conduit.toml. \ + Create one with a `[providers.]` section (see conduit.example.toml), \ + or use --config or CONDUIT_CONFIG to point elsewhere." + ); + } + + #[test] + fn no_providers_error_names_the_config_file_that_was_read() { + let config = Config { + source: ConfigSource::File(PathBuf::from("configs/other.toml")), + ..Config::default() + }; + let err = config.ensure_providers().unwrap_err().to_string(); + assert_eq!( + err, + "No providers configured in configs/other.toml. \ + Add a `[providers.]` section (see conduit.example.toml), \ + or use --config or CONDUIT_CONFIG to point elsewhere." + ); + assert!(!err.contains("./conduit.toml")); + } + + #[test] + fn no_providers_error_for_a_config_built_in_code_mentions_no_file() { + let err = Config::default() + .ensure_providers() + .unwrap_err() + .to_string(); + assert_eq!( + err, + "No providers configured. Add a `[providers.]` section (see conduit.example.toml)." + ); + } } diff --git a/crates/proxy/src/lib.rs b/crates/proxy/src/lib.rs index b7051fb..342abda 100644 --- a/crates/proxy/src/lib.rs +++ b/crates/proxy/src/lib.rs @@ -47,9 +47,7 @@ struct ProviderContext { } pub async fn start(config: Config, storages: Storages) -> anyhow::Result<()> { - if config.providers.is_empty() { - anyhow::bail!("No providers specified, gateway will not be functional."); - } + config.ensure_providers()?; let (report_tx, report_rx) = mpsc::unbounded_channel(); tokio::spawn(report_processor(report_rx, storages.clone()));