diff options
| author | salmonik <salmonik@tuta.io> | 2026-11-19 11:30:47 +0100 |
|---|---|---|
| committer | salmonik <salmonik@tuta.io> | 2026-11-19 11:30:47 +0100 |
| commit | 4d5b584e39cf90a4afaab8af46be2f69dcd914b6 (patch) | |
| tree | 567bd18d2c7ea52129aaca6d3af0e8e8e0e84802 /src/config.rs | |
| parent | docs: refresh KNOWN-ISSUES for 0.11.0; note the yolo git gate (diff) | |
| download | agent686-4d5b584e39cf90a4afaab8af46be2f69dcd914b6.tar.gz agent686-4d5b584e39cf90a4afaab8af46be2f69dcd914b6.zip | |
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
Diffstat (limited to 'src/config.rs')
| -rw-r--r-- | src/config.rs | 195 |
1 files changed, 144 insertions, 51 deletions
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 @@ | |||
| 1 | use std::path::PathBuf; | 1 | use std::path::PathBuf; |
| 2 | 2 | ||
| 3 | pub const DEFAULT_BASE_URL: &str = "https://opencode.ai/inference/go/openai/v1"; | 3 | use serde::Deserialize; |
| 4 | |||
| 4 | pub const DEFAULT_MODEL: &str = "deepseek-v4.1-flash"; | 5 | pub const DEFAULT_MODEL: &str = "deepseek-v4.1-flash"; |
| 5 | 6 | ||
| 6 | /// (name, base_url, default_model, lenient). Local gateways that omit `[DONE]` | 7 | /// What kind of provider this is. Both speak OpenAI-compatible chat |
| 7 | /// or `usage` are marked lenient so a truncated-looking stream is not an error. | 8 | /// completions; OpenCode additionally requires the `x-opencode-session` header. |
| 8 | const PRESETS: &[(&str, &str, &str, bool)] = &[ | 9 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 9 | ("opencode-go", DEFAULT_BASE_URL, DEFAULT_MODEL, false), | 10 | pub enum Provider { |
| 10 | ("openai", "https://api.openai.com/v1", "gpt-4o-mini", false), | 11 | OpenCode, |
| 11 | ("openrouter", "https://openrouter.ai/api/v1", "", false), | 12 | OpenAICompatible, |
| 12 | ("ollama", "http://localhost:11434/v1", "", true), | 13 | } |
| 13 | ("llamacpp", "http://localhost:8080/v1", "", true), | 14 | |
| 14 | ("vllm", "http://localhost:8000/v1", "", true), | 15 | impl Provider { |
| 15 | ]; | 16 | pub fn from_name(name: &str) -> Option<Provider> { |
| 17 | match name { | ||
| 18 | "opencode" | "opencode-go" => Some(Provider::OpenCode), | ||
| 19 | "openai" | "openai-compatible" | "openai_compatible" => { | ||
| 20 | Some(Provider::OpenAICompatible) | ||
| 21 | } | ||
| 22 | _ => None, | ||
| 23 | } | ||
| 24 | } | ||
| 25 | |||
| 26 | pub fn name(self) -> &'static str { | ||
| 27 | match self { | ||
| 28 | Provider::OpenCode => "opencode", | ||
| 29 | Provider::OpenAICompatible => "openai-compatible", | ||
| 30 | } | ||
| 31 | } | ||
| 32 | |||
| 33 | pub fn default_url(self) -> &'static str { | ||
| 34 | match self { | ||
| 35 | Provider::OpenCode => "https://opencode.ai/inference/go/openai/v1", | ||
| 36 | Provider::OpenAICompatible => "", | ||
| 37 | } | ||
| 38 | } | ||
| 39 | |||
| 40 | pub fn needs_session_header(self) -> bool { | ||
| 41 | self == Provider::OpenCode | ||
| 42 | } | ||
| 43 | } | ||
| 44 | |||
| 45 | /// The command-line values that override config and environment. | ||
| 46 | #[derive(Debug, Default)] | ||
| 47 | pub struct Cli { | ||
| 48 | pub model: Option<String>, | ||
| 49 | pub base_url: Option<String>, | ||
| 50 | pub provider: Option<String>, | ||
| 51 | pub yolo: bool, | ||
| 52 | pub lenient: bool, | ||
| 53 | pub system_ca: bool, | ||
| 54 | } | ||
| 16 | 55 | ||
| 17 | #[derive(Debug, Clone, PartialEq, Eq)] | 56 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 18 | pub enum KeySource { | 57 | pub enum KeySource { |
| @@ -26,28 +65,24 @@ pub struct Config { | |||
| 26 | pub base_url: String, | 65 | pub base_url: String, |
| 27 | pub api_key: String, | 66 | pub api_key: String, |
| 28 | pub model: String, | 67 | pub model: String, |
| 29 | /// Session id, sent to the provider as `x-opencode-session`. Filled in once | 68 | /// Session id, sent to the provider as `x-opencode-session` when it is |
| 30 | /// the session exists. | 69 | /// OpenCode. Filled in once the session exists. |
| 31 | pub session: String, | 70 | pub session: String, |
| 71 | pub provider: Provider, | ||
| 32 | pub yolo: bool, | 72 | pub yolo: bool, |
| 33 | /// Tolerate providers that omit `[DONE]`/`usage`/`finish_reason`. | 73 | /// Tolerate providers that omit `[DONE]`/`usage`/`finish_reason`. |
| 34 | pub lenient: bool, | 74 | pub lenient: bool, |
| 35 | /// Verify TLS against the system trust store instead of bundled roots. | 75 | /// Verify TLS against the system trust store instead of bundled roots. |
| 36 | pub system_ca: bool, | 76 | pub system_ca: bool, |
| 77 | /// Permit `--yolo` in `$HOME` or `/`. | ||
| 78 | pub allow_home: bool, | ||
| 79 | /// Permit `--yolo` outside a git repository. | ||
| 80 | pub allow_nogit: bool, | ||
| 37 | } | 81 | } |
| 38 | 82 | ||
| 39 | impl Config { | 83 | impl Config { |
| 40 | /// CLI flags win over environment variables, which win over defaults. | 84 | pub fn resolve(cli: &Cli) -> Result<Config, String> { |
| 41 | #[allow(clippy::too_many_arguments)] | 85 | let config = Self::resolve_lenient(cli); |
| 42 | pub fn resolve( | ||
| 43 | model: Option<String>, | ||
| 44 | base_url: Option<String>, | ||
| 45 | yolo: bool, | ||
| 46 | provider: Option<String>, | ||
| 47 | lenient: bool, | ||
| 48 | system_ca: bool, | ||
| 49 | ) -> Result<Config, String> { | ||
| 50 | let config = Self::resolve_lenient(model, base_url, yolo, provider, lenient, system_ca); | ||
| 51 | if config.api_key.is_empty() { | 86 | if config.api_key.is_empty() { |
| 52 | return Err(format!( | 87 | return Err(format!( |
| 53 | "no API key: set OPENCODE_API_KEY or write one to {}", | 88 | "no API key: set OPENCODE_API_KEY or write one to {}", |
| @@ -56,34 +91,55 @@ impl Config { | |||
| 56 | .unwrap_or_else(|| "~/.config/agent686/key".into()) | 91 | .unwrap_or_else(|| "~/.config/agent686/key".into()) |
| 57 | )); | 92 | )); |
| 58 | } | 93 | } |
| 94 | if config.provider == Provider::OpenAICompatible && config.base_url.is_empty() { | ||
| 95 | return Err( | ||
| 96 | "the openai-compatible provider needs a base URL: set --base-url, \ | ||
| 97 | AGENT686_BASE_URL, or `url` in the config file" | ||
| 98 | .to_string(), | ||
| 99 | ); | ||
| 100 | } | ||
| 59 | Ok(config) | 101 | Ok(config) |
| 60 | } | 102 | } |
| 61 | 103 | ||
| 62 | /// Same as `resolve`, but tolerates a missing key so `--doctor` can report | 104 | /// Same as `resolve`, but tolerates a missing key and URL so `--doctor` can |
| 63 | /// the missing key instead of failing to start. | 105 | /// report them instead of failing to start. |
| 64 | pub fn resolve_lenient( | 106 | pub fn resolve_lenient(cli: &Cli) -> Config { |
| 65 | model: Option<String>, | 107 | let file = load_file(); |
| 66 | base_url: Option<String>, | 108 | |
| 67 | yolo: bool, | 109 | let provider = cli |
| 68 | provider: Option<String>, | 110 | .provider |
| 69 | lenient: bool, | 111 | .clone() |
| 70 | system_ca: bool, | 112 | .or_else(|| env_nonempty("AGENT686_PROVIDER")) |
| 71 | ) -> Config { | 113 | .or(file.provider.clone()) |
| 72 | // Presets fill the base URL and a default model; explicit values win. | 114 | .and_then(|name| Provider::from_name(&name)) |
| 73 | let provider = provider.or_else(|| env_nonempty("AGENT686_PROVIDER")); | 115 | .unwrap_or_else(|| { |
| 74 | let (preset_url, preset_model, preset_lenient) = provider | 116 | // Unknown names fall back to OpenCode, with a note. |
| 75 | .as_deref() | 117 | if let Some(bad) = cli |
| 76 | .and_then(|name| PRESETS.iter().find(|(n, ..)| *n == name)) | 118 | .provider |
| 77 | .map(|(_, url, model, lenient)| (*url, *model, *lenient)) | 119 | .clone() |
| 78 | .unwrap_or((DEFAULT_BASE_URL, "", false)); | 120 | .or_else(|| env_nonempty("AGENT686_PROVIDER")) |
| 79 | 121 | .or(file.provider.clone()) | |
| 80 | let base_url = base_url | 122 | { |
| 123 | eprintln!( | ||
| 124 | "agent686: unknown provider type `{bad}` (expected opencode or \ | ||
| 125 | openai-compatible); using opencode" | ||
| 126 | ); | ||
| 127 | } | ||
| 128 | Provider::OpenCode | ||
| 129 | }); | ||
| 130 | |||
| 131 | let base_url = cli | ||
| 132 | .base_url | ||
| 133 | .clone() | ||
| 81 | .or_else(|| env_nonempty("AGENT686_BASE_URL")) | 134 | .or_else(|| env_nonempty("AGENT686_BASE_URL")) |
| 82 | .unwrap_or_else(|| preset_url.to_string()); | 135 | .or(file.url.clone()) |
| 136 | .unwrap_or_else(|| provider.default_url().to_string()); | ||
| 83 | 137 | ||
| 84 | let model = model | 138 | let model = cli |
| 139 | .model | ||
| 140 | .clone() | ||
| 85 | .or_else(|| env_nonempty("AGENT686_MODEL")) | 141 | .or_else(|| env_nonempty("AGENT686_MODEL")) |
| 86 | .or_else(|| (!preset_model.is_empty()).then(|| preset_model.to_string())) | 142 | .or(file.model.clone()) |
| 87 | .unwrap_or_else(|| DEFAULT_MODEL.to_string()); | 143 | .unwrap_or_else(|| DEFAULT_MODEL.to_string()); |
| 88 | 144 | ||
| 89 | Config { | 145 | Config { |
| @@ -91,9 +147,16 @@ impl Config { | |||
| 91 | base_url, | 147 | base_url, |
| 92 | model, | 148 | model, |
| 93 | session: String::new(), | 149 | session: String::new(), |
| 94 | yolo, | 150 | provider, |
| 95 | lenient: lenient || preset_lenient, | 151 | yolo: cli.yolo || file.yolo.unwrap_or(false), |
| 96 | system_ca: system_ca || env_nonempty("AGENT686_SYSTEM_CA").is_some(), | 152 | lenient: cli.lenient || file.lenient.unwrap_or(false), |
| 153 | system_ca: cli.system_ca | ||
| 154 | || env_nonempty("AGENT686_SYSTEM_CA").is_some() | ||
| 155 | || file.system_ca.unwrap_or(false), | ||
| 156 | allow_home: env_nonempty("AGENT686_ALLOW_HOME").is_some() | ||
| 157 | || file.allow_home.unwrap_or(false), | ||
| 158 | allow_nogit: env_nonempty("AGENT686_ALLOW_NOGIT").is_some() | ||
| 159 | || file.allow_nogit.unwrap_or(false), | ||
| 97 | } | 160 | } |
| 98 | } | 161 | } |
| 99 | 162 | ||
| @@ -102,8 +165,38 @@ impl Config { | |||
| 102 | } | 165 | } |
| 103 | } | 166 | } |
| 104 | 167 | ||
| 105 | pub fn provider_names() -> Vec<&'static str> { | 168 | /// The `~/.config/agent686/config.toml` shape. Every key is optional. |
| 106 | PRESETS.iter().map(|(name, ..)| *name).collect() | 169 | #[derive(Debug, Default, Deserialize)] |
| 170 | struct FileConfig { | ||
| 171 | provider: Option<String>, | ||
| 172 | url: Option<String>, | ||
| 173 | model: Option<String>, | ||
| 174 | lenient: Option<bool>, | ||
| 175 | system_ca: Option<bool>, | ||
| 176 | yolo: Option<bool>, | ||
| 177 | allow_home: Option<bool>, | ||
| 178 | allow_nogit: Option<bool>, | ||
| 179 | } | ||
| 180 | |||
| 181 | fn config_path() -> Option<PathBuf> { | ||
| 182 | if let Some(path) = std::env::var_os("AGENT686_CONFIG") { | ||
| 183 | return Some(PathBuf::from(path)); | ||
| 184 | } | ||
| 185 | let home = std::env::var_os("HOME")?; | ||
| 186 | Some(PathBuf::from(home).join(".config/agent686/config.toml")) | ||
| 187 | } | ||
| 188 | |||
| 189 | fn load_file() -> FileConfig { | ||
| 190 | let Some(path) = config_path() else { | ||
| 191 | return FileConfig::default(); | ||
| 192 | }; | ||
| 193 | match std::fs::read_to_string(&path) { | ||
| 194 | Ok(text) => toml::from_str(&text).unwrap_or_else(|error| { | ||
| 195 | eprintln!("agent686: {}: {error}", path.display()); | ||
| 196 | FileConfig::default() | ||
| 197 | }), | ||
| 198 | Err(_) => FileConfig::default(), | ||
| 199 | } | ||
| 107 | } | 200 | } |
| 108 | 201 | ||
| 109 | fn env_nonempty(key: &str) -> Option<String> { | 202 | fn env_nonempty(key: &str) -> Option<String> { |
