From fa5894ea5740852c37eba3937c6ad255574ccfd3 Mon Sep 17 00:00:00 2001 From: salmonik Date: Sun, 15 Nov 2026 15:26:21 +0100 Subject: 0.3.0: --version and --doctor doctor reports key source, writable dirs, provider, model, then checks the key against GET /models and confirms the model id is listed. Non-zero exit on failure, so it can gate a deployment. --- src/config.rs | 62 ++++++++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 44 insertions(+), 18 deletions(-) (limited to 'src/config.rs') diff --git a/src/config.rs b/src/config.rs index a1d7c42..77bdc52 100644 --- a/src/config.rs +++ b/src/config.rs @@ -3,6 +3,13 @@ use std::path::PathBuf; pub const DEFAULT_BASE_URL: &str = "https://opencode.ai/inference/go/openai/v1"; pub const DEFAULT_MODEL: &str = "deepseek-v4.1-flash"; +#[derive(Debug, Clone, PartialEq, Eq)] +pub enum KeySource { + Env, + File(PathBuf), + Missing, +} + #[derive(Debug, Clone)] pub struct Config { pub base_url: String, @@ -21,8 +28,23 @@ impl Config { base_url: Option, yolo: bool, ) -> Result { - Ok(Config { - api_key: api_key()?, + let config = Self::resolve_lenient(model, base_url, yolo); + if config.api_key.is_empty() { + return Err(format!( + "no API key: set OPENCODE_API_KEY or write one to {}", + key_file() + .map(|path| path.display().to_string()) + .unwrap_or_else(|| "~/.config/agent686/key".into()) + )); + } + Ok(config) + } + + /// Same as `resolve`, but tolerates a missing key so `--doctor` can report + /// the missing key instead of failing to start. + pub fn resolve_lenient(model: Option, base_url: Option, yolo: bool) -> Config { + Config { + api_key: api_key().unwrap_or_default(), base_url: base_url .or_else(|| env_nonempty("AGENT686_BASE_URL")) .unwrap_or_else(|| DEFAULT_BASE_URL.to_string()), @@ -31,7 +53,7 @@ impl Config { .unwrap_or_else(|| DEFAULT_MODEL.to_string()), session: String::new(), yolo, - }) + } } pub fn endpoint(&self) -> String { @@ -43,26 +65,30 @@ fn env_nonempty(key: &str) -> Option { std::env::var(key).ok().filter(|v| !v.trim().is_empty()) } -/// The key comes from the environment first, then from a file, so a key never -/// has to live in shell history. -fn api_key() -> Result { - if let Some(k) = env_nonempty("OPENCODE_API_KEY") { - return Ok(k.trim().to_string()); +pub fn key_source() -> KeySource { + if env_nonempty("OPENCODE_API_KEY").is_some() { + return KeySource::Env; } if let Some(path) = key_file() { - if let Ok(k) = std::fs::read_to_string(&path) { - let k = k.trim().to_string(); - if !k.is_empty() { - return Ok(k); + if let Ok(key) = std::fs::read_to_string(&path) { + if !key.trim().is_empty() { + return KeySource::File(path); } } } - Err(format!( - "no API key: set OPENCODE_API_KEY or write one to {}", - key_file() - .map(|p| p.display().to_string()) - .unwrap_or_else(|| "~/.config/agent686/key".into()) - )) + KeySource::Missing +} + +/// The key comes from the environment first, then from a file, so a key never +/// has to live in shell history. +fn api_key() -> Result { + match key_source() { + KeySource::Env => Ok(env_nonempty("OPENCODE_API_KEY").unwrap_or_default()), + KeySource::File(path) => Ok(std::fs::read_to_string(path) + .map(|key| key.trim().to_string()) + .unwrap_or_default()), + KeySource::Missing => Err("no API key".into()), + } } fn key_file() -> Option { -- cgit v1.2.3