summaryrefslogtreecommitdiffstats
path: root/src/config.rs
diff options
context:
space:
mode:
authorsalmonik <salmonik@tuta.io>2026-11-15 15:26:21 +0100
committersalmonik <salmonik@tuta.io>2026-11-15 15:26:21 +0100
commitfa5894ea5740852c37eba3937c6ad255574ccfd3 (patch)
tree82d09308bbe8afe3ce92c4df31a03b77d3add110 /src/config.rs
parent0.2.0: streaming line-range reads (diff)
downloadagent686-fa5894ea5740852c37eba3937c6ad255574ccfd3.tar.gz
agent686-fa5894ea5740852c37eba3937c6ad255574ccfd3.zip
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.
Diffstat (limited to '')
-rw-r--r--src/config.rs62
1 files changed, 44 insertions, 18 deletions
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;
3pub const DEFAULT_BASE_URL: &str = "https://opencode.ai/inference/go/openai/v1"; 3pub const DEFAULT_BASE_URL: &str = "https://opencode.ai/inference/go/openai/v1";
4pub const DEFAULT_MODEL: &str = "deepseek-v4.1-flash"; 4pub const DEFAULT_MODEL: &str = "deepseek-v4.1-flash";
5 5
6#[derive(Debug, Clone, PartialEq, Eq)]
7pub enum KeySource {
8 Env,
9 File(PathBuf),
10 Missing,
11}
12
6#[derive(Debug, Clone)] 13#[derive(Debug, Clone)]
7pub struct Config { 14pub struct Config {
8 pub base_url: String, 15 pub base_url: String,
@@ -21,8 +28,23 @@ impl Config {
21 base_url: Option<String>, 28 base_url: Option<String>,
22 yolo: bool, 29 yolo: bool,
23 ) -> Result<Config, String> { 30 ) -> Result<Config, String> {
24 Ok(Config { 31 let config = Self::resolve_lenient(model, base_url, yolo);
25 api_key: api_key()?, 32 if config.api_key.is_empty() {
33 return Err(format!(
34 "no API key: set OPENCODE_API_KEY or write one to {}",
35 key_file()
36 .map(|path| path.display().to_string())
37 .unwrap_or_else(|| "~/.config/agent686/key".into())
38 ));
39 }
40 Ok(config)
41 }
42
43 /// Same as `resolve`, but tolerates a missing key so `--doctor` can report
44 /// the missing key instead of failing to start.
45 pub fn resolve_lenient(model: Option<String>, base_url: Option<String>, yolo: bool) -> Config {
46 Config {
47 api_key: api_key().unwrap_or_default(),
26 base_url: base_url 48 base_url: base_url
27 .or_else(|| env_nonempty("AGENT686_BASE_URL")) 49 .or_else(|| env_nonempty("AGENT686_BASE_URL"))
28 .unwrap_or_else(|| DEFAULT_BASE_URL.to_string()), 50 .unwrap_or_else(|| DEFAULT_BASE_URL.to_string()),
@@ -31,7 +53,7 @@ impl Config {
31 .unwrap_or_else(|| DEFAULT_MODEL.to_string()), 53 .unwrap_or_else(|| DEFAULT_MODEL.to_string()),
32 session: String::new(), 54 session: String::new(),
33 yolo, 55 yolo,
34 }) 56 }
35 } 57 }
36 58
37 pub fn endpoint(&self) -> String { 59 pub fn endpoint(&self) -> String {
@@ -43,26 +65,30 @@ fn env_nonempty(key: &str) -> Option<String> {
43 std::env::var(key).ok().filter(|v| !v.trim().is_empty()) 65 std::env::var(key).ok().filter(|v| !v.trim().is_empty())
44} 66}
45 67
46/// The key comes from the environment first, then from a file, so a key never 68pub fn key_source() -> KeySource {
47/// has to live in shell history. 69 if env_nonempty("OPENCODE_API_KEY").is_some() {
48fn api_key() -> Result<String, String> { 70 return KeySource::Env;
49 if let Some(k) = env_nonempty("OPENCODE_API_KEY") {
50 return Ok(k.trim().to_string());
51 } 71 }
52 if let Some(path) = key_file() { 72 if let Some(path) = key_file() {
53 if let Ok(k) = std::fs::read_to_string(&path) { 73 if let Ok(key) = std::fs::read_to_string(&path) {
54 let k = k.trim().to_string(); 74 if !key.trim().is_empty() {
55 if !k.is_empty() { 75 return KeySource::File(path);
56 return Ok(k);
57 } 76 }
58 } 77 }
59 } 78 }
60 Err(format!( 79 KeySource::Missing
61 "no API key: set OPENCODE_API_KEY or write one to {}", 80}
62 key_file() 81
63 .map(|p| p.display().to_string()) 82/// The key comes from the environment first, then from a file, so a key never
64 .unwrap_or_else(|| "~/.config/agent686/key".into()) 83/// has to live in shell history.
65 )) 84fn api_key() -> Result<String, String> {
85 match key_source() {
86 KeySource::Env => Ok(env_nonempty("OPENCODE_API_KEY").unwrap_or_default()),
87 KeySource::File(path) => Ok(std::fs::read_to_string(path)
88 .map(|key| key.trim().to_string())
89 .unwrap_or_default()),
90 KeySource::Missing => Err("no API key".into()),
91 }
66} 92}
67 93
68fn key_file() -> Option<PathBuf> { 94fn key_file() -> Option<PathBuf> {