Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions crates/cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 4 additions & 0 deletions crates/cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 { .. }) {
Expand Down
86 changes: 86 additions & 0 deletions crates/cli/tests/config_errors.rs
Original file line number Diff line number Diff line change
@@ -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<String> {
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::<String>::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::<String>::new());
}
136 changes: 130 additions & 6 deletions crates/core/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,21 @@ pub struct Config {
/// Model name prefix -> rates; checked before the built-in pricing table.
pub pricing: HashMap<String, ModelPricing>,
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 {
Expand All @@ -26,6 +41,7 @@ impl Default for Config {
valohai_llm: ValohaiLlmConfig::default(),
pricing: HashMap::new(),
upstream: UpstreamConfig::default(),
source: ConfigSource::default(),
}
}
}
Expand Down Expand Up @@ -85,9 +101,10 @@ impl Config {
read_env_var: impl Fn(&str) -> Option<String>,
) -> anyhow::Result<Self> {
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),
}
}
Expand All @@ -98,8 +115,12 @@ impl Config {
) -> anyhow::Result<Self> {
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(
Expand Down Expand Up @@ -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.<name>]` 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`.
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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.<name>]` 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.<name>]` 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.<name>]` section (see conduit.example.toml)."
);
}
}
4 changes: 1 addition & 3 deletions crates/proxy/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()));

Expand Down