From 4d5b584e39cf90a4afaab8af46be2f69dcd914b6 Mon Sep 17 00:00:00 2001 From: salmonik Date: Thu, 19 Nov 2026 11:30:47 +0100 Subject: 0.12.0: provider is a type + URL, plus a config file - --provider opencode|openai-compatible; opencode keeps the session header, openai-compatible needs a base URL and sends none - config file at ~/.config/agent686/config.toml (provider, url, model, flags) - fix the doubled-dash --provider help message --- src/config.rs | 195 +++++++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 144 insertions(+), 51 deletions(-) (limited to 'src/config.rs') diff --git a/src/config.rs b/src/config.rs index 1d259c7..a314f38 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,18 +1,57 @@ use std::path::PathBuf; -pub const DEFAULT_BASE_URL: &str = "https://opencode.ai/inference/go/openai/v1"; +use serde::Deserialize; + pub const DEFAULT_MODEL: &str = "deepseek-v4.1-flash"; -/// (name, base_url, default_model, lenient). Local gateways that omit `[DONE]` -/// or `usage` are marked lenient so a truncated-looking stream is not an error. -const PRESETS: &[(&str, &str, &str, bool)] = &[ - ("opencode-go", DEFAULT_BASE_URL, DEFAULT_MODEL, false), - ("openai", "https://api.openai.com/v1", "gpt-4o-mini", false), - ("openrouter", "https://openrouter.ai/api/v1", "", false), - ("ollama", "http://localhost:11434/v1", "", true), - ("llamacpp", "http://localhost:8080/v1", "", true), - ("vllm", "http://localhost:8000/v1", "", true), -]; +/// What kind of provider this is. Both speak OpenAI-compatible chat +/// completions; OpenCode additionally requires the `x-opencode-session` header. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum Provider { + OpenCode, + OpenAICompatible, +} + +impl Provider { + pub fn from_name(name: &str) -> Option { + match name { + "opencode" | "opencode-go" => Some(Provider::OpenCode), + "openai" | "openai-compatible" | "openai_compatible" => { + Some(Provider::OpenAICompatible) + } + _ => None, + } + } + + pub fn name(self) -> &'static str { + match self { + Provider::OpenCode => "opencode", + Provider::OpenAICompatible => "openai-compatible", + } + } + + pub fn default_url(self) -> &'static str { + match self { + Provider::OpenCode => "https://opencode.ai/inference/go/openai/v1", + Provider::OpenAICompatible => "", + } + } + + pub fn needs_session_header(self) -> bool { + self == Provider::OpenCode + } +} + +/// The command-line values that override config and environment. +#[derive(Debug, Default)] +pub struct Cli { + pub model: Option, + pub base_url: Option, + pub provider: Option, + pub yolo: bool, + pub lenient: bool, + pub system_ca: bool, +} #[derive(Debug, Clone, PartialEq, Eq)] pub enum KeySource { @@ -26,28 +65,24 @@ pub struct Config { pub base_url: String, pub api_key: String, pub model: String, - /// Session id, sent to the provider as `x-opencode-session`. Filled in once - /// the session exists. + /// Session id, sent to the provider as `x-opencode-session` when it is + /// OpenCode. Filled in once the session exists. pub session: String, + pub provider: Provider, pub yolo: bool, /// Tolerate providers that omit `[DONE]`/`usage`/`finish_reason`. pub lenient: bool, /// Verify TLS against the system trust store instead of bundled roots. pub system_ca: bool, + /// Permit `--yolo` in `$HOME` or `/`. + pub allow_home: bool, + /// Permit `--yolo` outside a git repository. + pub allow_nogit: bool, } impl Config { - /// CLI flags win over environment variables, which win over defaults. - #[allow(clippy::too_many_arguments)] - pub fn resolve( - model: Option, - base_url: Option, - yolo: bool, - provider: Option, - lenient: bool, - system_ca: bool, - ) -> Result { - let config = Self::resolve_lenient(model, base_url, yolo, provider, lenient, system_ca); + pub fn resolve(cli: &Cli) -> Result { + let config = Self::resolve_lenient(cli); if config.api_key.is_empty() { return Err(format!( "no API key: set OPENCODE_API_KEY or write one to {}", @@ -56,34 +91,55 @@ impl Config { .unwrap_or_else(|| "~/.config/agent686/key".into()) )); } + if config.provider == Provider::OpenAICompatible && config.base_url.is_empty() { + return Err( + "the openai-compatible provider needs a base URL: set --base-url, \ + AGENT686_BASE_URL, or `url` in the config file" + .to_string(), + ); + } 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, - provider: Option, - lenient: bool, - system_ca: bool, - ) -> Config { - // Presets fill the base URL and a default model; explicit values win. - let provider = provider.or_else(|| env_nonempty("AGENT686_PROVIDER")); - let (preset_url, preset_model, preset_lenient) = provider - .as_deref() - .and_then(|name| PRESETS.iter().find(|(n, ..)| *n == name)) - .map(|(_, url, model, lenient)| (*url, *model, *lenient)) - .unwrap_or((DEFAULT_BASE_URL, "", false)); - - let base_url = base_url + /// Same as `resolve`, but tolerates a missing key and URL so `--doctor` can + /// report them instead of failing to start. + pub fn resolve_lenient(cli: &Cli) -> Config { + let file = load_file(); + + let provider = cli + .provider + .clone() + .or_else(|| env_nonempty("AGENT686_PROVIDER")) + .or(file.provider.clone()) + .and_then(|name| Provider::from_name(&name)) + .unwrap_or_else(|| { + // Unknown names fall back to OpenCode, with a note. + if let Some(bad) = cli + .provider + .clone() + .or_else(|| env_nonempty("AGENT686_PROVIDER")) + .or(file.provider.clone()) + { + eprintln!( + "agent686: unknown provider type `{bad}` (expected opencode or \ + openai-compatible); using opencode" + ); + } + Provider::OpenCode + }); + + let base_url = cli + .base_url + .clone() .or_else(|| env_nonempty("AGENT686_BASE_URL")) - .unwrap_or_else(|| preset_url.to_string()); + .or(file.url.clone()) + .unwrap_or_else(|| provider.default_url().to_string()); - let model = model + let model = cli + .model + .clone() .or_else(|| env_nonempty("AGENT686_MODEL")) - .or_else(|| (!preset_model.is_empty()).then(|| preset_model.to_string())) + .or(file.model.clone()) .unwrap_or_else(|| DEFAULT_MODEL.to_string()); Config { @@ -91,9 +147,16 @@ impl Config { base_url, model, session: String::new(), - yolo, - lenient: lenient || preset_lenient, - system_ca: system_ca || env_nonempty("AGENT686_SYSTEM_CA").is_some(), + provider, + yolo: cli.yolo || file.yolo.unwrap_or(false), + lenient: cli.lenient || file.lenient.unwrap_or(false), + system_ca: cli.system_ca + || env_nonempty("AGENT686_SYSTEM_CA").is_some() + || file.system_ca.unwrap_or(false), + allow_home: env_nonempty("AGENT686_ALLOW_HOME").is_some() + || file.allow_home.unwrap_or(false), + allow_nogit: env_nonempty("AGENT686_ALLOW_NOGIT").is_some() + || file.allow_nogit.unwrap_or(false), } } @@ -102,8 +165,38 @@ impl Config { } } -pub fn provider_names() -> Vec<&'static str> { - PRESETS.iter().map(|(name, ..)| *name).collect() +/// The `~/.config/agent686/config.toml` shape. Every key is optional. +#[derive(Debug, Default, Deserialize)] +struct FileConfig { + provider: Option, + url: Option, + model: Option, + lenient: Option, + system_ca: Option, + yolo: Option, + allow_home: Option, + allow_nogit: Option, +} + +fn config_path() -> Option { + if let Some(path) = std::env::var_os("AGENT686_CONFIG") { + return Some(PathBuf::from(path)); + } + let home = std::env::var_os("HOME")?; + Some(PathBuf::from(home).join(".config/agent686/config.toml")) +} + +fn load_file() -> FileConfig { + let Some(path) = config_path() else { + return FileConfig::default(); + }; + match std::fs::read_to_string(&path) { + Ok(text) => toml::from_str(&text).unwrap_or_else(|error| { + eprintln!("agent686: {}: {error}", path.display()); + FileConfig::default() + }), + Err(_) => FileConfig::default(), + } } fn env_nonempty(key: &str) -> Option { -- cgit v1.2.3