diff options
| author | salmonik <salmonik@tuta.io> | 2026-11-18 12:45:05 +0100 |
|---|---|---|
| committer | salmonik <salmonik@tuta.io> | 2026-11-18 12:45:05 +0100 |
| commit | 226c44dc770dd0ea42e322af4d0edd6c14ce14db (patch) | |
| tree | e41318cd028770a1d16408712a5549ef59e36622 /src/config.rs | |
| parent | 0.10.0: --json, /models /model /forget, git-repo gate for --yolo (diff) | |
| download | agent686-226c44dc770dd0ea42e322af4d0edd6c14ce14db.tar.gz agent686-226c44dc770dd0ea42e322af4d0edd6c14ce14db.zip | |
0.11.0: /undo via git checkpoints, /compact, provider presets, leniency, system CA
- /undo resets to a git checkpoint taken before each turn
- /compact summarises and rewrites the transcript (ADR 0003, the one sanctioned rewrite)
- --provider presets for six OpenAI-compatible gateways, local ones lenient by default
- --lenient and --system-ca
- proxies already honoured via ureq; documented
Diffstat (limited to 'src/config.rs')
| -rw-r--r-- | src/config.rs | 61 |
1 files changed, 53 insertions, 8 deletions
diff --git a/src/config.rs b/src/config.rs index 77bdc52..1d259c7 100644 --- a/src/config.rs +++ b/src/config.rs | |||
| @@ -3,6 +3,17 @@ use std::path::PathBuf; | |||
| 3 | pub const DEFAULT_BASE_URL: &str = "https://opencode.ai/inference/go/openai/v1"; | 3 | pub const DEFAULT_BASE_URL: &str = "https://opencode.ai/inference/go/openai/v1"; |
| 4 | pub const DEFAULT_MODEL: &str = "deepseek-v4.1-flash"; | 4 | pub const DEFAULT_MODEL: &str = "deepseek-v4.1-flash"; |
| 5 | 5 | ||
| 6 | /// (name, base_url, default_model, lenient). Local gateways that omit `[DONE]` | ||
| 7 | /// or `usage` are marked lenient so a truncated-looking stream is not an error. | ||
| 8 | const PRESETS: &[(&str, &str, &str, bool)] = &[ | ||
| 9 | ("opencode-go", DEFAULT_BASE_URL, DEFAULT_MODEL, false), | ||
| 10 | ("openai", "https://api.openai.com/v1", "gpt-4o-mini", false), | ||
| 11 | ("openrouter", "https://openrouter.ai/api/v1", "", false), | ||
| 12 | ("ollama", "http://localhost:11434/v1", "", true), | ||
| 13 | ("llamacpp", "http://localhost:8080/v1", "", true), | ||
| 14 | ("vllm", "http://localhost:8000/v1", "", true), | ||
| 15 | ]; | ||
| 16 | |||
| 6 | #[derive(Debug, Clone, PartialEq, Eq)] | 17 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 7 | pub enum KeySource { | 18 | pub enum KeySource { |
| 8 | Env, | 19 | Env, |
| @@ -19,16 +30,24 @@ pub struct Config { | |||
| 19 | /// the session exists. | 30 | /// the session exists. |
| 20 | pub session: String, | 31 | pub session: String, |
| 21 | pub yolo: bool, | 32 | pub yolo: bool, |
| 33 | /// Tolerate providers that omit `[DONE]`/`usage`/`finish_reason`. | ||
| 34 | pub lenient: bool, | ||
| 35 | /// Verify TLS against the system trust store instead of bundled roots. | ||
| 36 | pub system_ca: bool, | ||
| 22 | } | 37 | } |
| 23 | 38 | ||
| 24 | impl Config { | 39 | impl Config { |
| 25 | /// CLI flags win over environment variables, which win over defaults. | 40 | /// CLI flags win over environment variables, which win over defaults. |
| 41 | #[allow(clippy::too_many_arguments)] | ||
| 26 | pub fn resolve( | 42 | pub fn resolve( |
| 27 | model: Option<String>, | 43 | model: Option<String>, |
| 28 | base_url: Option<String>, | 44 | base_url: Option<String>, |
| 29 | yolo: bool, | 45 | yolo: bool, |
| 46 | provider: Option<String>, | ||
| 47 | lenient: bool, | ||
| 48 | system_ca: bool, | ||
| 30 | ) -> Result<Config, String> { | 49 | ) -> Result<Config, String> { |
| 31 | let config = Self::resolve_lenient(model, base_url, yolo); | 50 | let config = Self::resolve_lenient(model, base_url, yolo, provider, lenient, system_ca); |
| 32 | if config.api_key.is_empty() { | 51 | if config.api_key.is_empty() { |
| 33 | return Err(format!( | 52 | return Err(format!( |
| 34 | "no API key: set OPENCODE_API_KEY or write one to {}", | 53 | "no API key: set OPENCODE_API_KEY or write one to {}", |
| @@ -42,17 +61,39 @@ impl Config { | |||
| 42 | 61 | ||
| 43 | /// Same as `resolve`, but tolerates a missing key so `--doctor` can report | 62 | /// Same as `resolve`, but tolerates a missing key so `--doctor` can report |
| 44 | /// the missing key instead of failing to start. | 63 | /// the missing key instead of failing to start. |
| 45 | pub fn resolve_lenient(model: Option<String>, base_url: Option<String>, yolo: bool) -> Config { | 64 | pub fn resolve_lenient( |
| 65 | model: Option<String>, | ||
| 66 | base_url: Option<String>, | ||
| 67 | yolo: bool, | ||
| 68 | provider: Option<String>, | ||
| 69 | lenient: bool, | ||
| 70 | system_ca: bool, | ||
| 71 | ) -> Config { | ||
| 72 | // Presets fill the base URL and a default model; explicit values win. | ||
| 73 | let provider = provider.or_else(|| env_nonempty("AGENT686_PROVIDER")); | ||
| 74 | let (preset_url, preset_model, preset_lenient) = provider | ||
| 75 | .as_deref() | ||
| 76 | .and_then(|name| PRESETS.iter().find(|(n, ..)| *n == name)) | ||
| 77 | .map(|(_, url, model, lenient)| (*url, *model, *lenient)) | ||
| 78 | .unwrap_or((DEFAULT_BASE_URL, "", false)); | ||
| 79 | |||
| 80 | let base_url = base_url | ||
| 81 | .or_else(|| env_nonempty("AGENT686_BASE_URL")) | ||
| 82 | .unwrap_or_else(|| preset_url.to_string()); | ||
| 83 | |||
| 84 | let model = model | ||
| 85 | .or_else(|| env_nonempty("AGENT686_MODEL")) | ||
| 86 | .or_else(|| (!preset_model.is_empty()).then(|| preset_model.to_string())) | ||
| 87 | .unwrap_or_else(|| DEFAULT_MODEL.to_string()); | ||
| 88 | |||
| 46 | Config { | 89 | Config { |
| 47 | api_key: api_key().unwrap_or_default(), | 90 | api_key: api_key().unwrap_or_default(), |
| 48 | base_url: base_url | 91 | base_url, |
| 49 | .or_else(|| env_nonempty("AGENT686_BASE_URL")) | 92 | model, |
| 50 | .unwrap_or_else(|| DEFAULT_BASE_URL.to_string()), | ||
| 51 | model: model | ||
| 52 | .or_else(|| env_nonempty("AGENT686_MODEL")) | ||
| 53 | .unwrap_or_else(|| DEFAULT_MODEL.to_string()), | ||
| 54 | session: String::new(), | 93 | session: String::new(), |
| 55 | yolo, | 94 | yolo, |
| 95 | lenient: lenient || preset_lenient, | ||
| 96 | system_ca: system_ca || env_nonempty("AGENT686_SYSTEM_CA").is_some(), | ||
| 56 | } | 97 | } |
| 57 | } | 98 | } |
| 58 | 99 | ||
| @@ -61,6 +102,10 @@ impl Config { | |||
| 61 | } | 102 | } |
| 62 | } | 103 | } |
| 63 | 104 | ||
| 105 | pub fn provider_names() -> Vec<&'static str> { | ||
| 106 | PRESETS.iter().map(|(name, ..)| *name).collect() | ||
| 107 | } | ||
| 108 | |||
| 64 | fn env_nonempty(key: &str) -> Option<String> { | 109 | fn env_nonempty(key: &str) -> Option<String> { |
| 65 | std::env::var(key).ok().filter(|v| !v.trim().is_empty()) | 110 | std::env::var(key).ok().filter(|v| !v.trim().is_empty()) |
| 66 | } | 111 | } |
