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/provider.rs | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) (limited to 'src/provider.rs') diff --git a/src/provider.rs b/src/provider.rs index ba36b70..16c9013 100644 --- a/src/provider.rs +++ b/src/provider.rs @@ -448,3 +448,52 @@ struct FunctionDelta { #[serde(default)] arguments: Option, } + +/// Lists the models the provider offers for this key, for `--doctor`. +pub fn models(cfg: &Config) -> Result, Error> { + let agent: ureq::Agent = ureq::Agent::config_builder() + .http_status_as_error(false) + .timeout_connect(Some(Duration::from_secs(10))) + .timeout_recv_response(Some(Duration::from_secs(20))) + .timeout_per_read(Some(Duration::from_secs(20))) + .build() + .into(); + + let url = format!("{}/models", cfg.base_url.trim_end_matches('/')); + let mut response = agent + .get(&url) + .header("Authorization", format!("Bearer {}", cfg.api_key)) + .header("User-Agent", concat!("agent686/", env!("CARGO_PKG_VERSION"))) + .header("x-opencode-session", &cfg.session) + .call() + .map_err(|e| Error::Transport(e.to_string()))?; + + let status = response.status().as_u16(); + let body = response + .body_mut() + .with_config() + .limit(8 * 1024 * 1024) + .read_to_string() + .unwrap_or_default(); + + if !(200..300).contains(&status) { + return Err(Error::Status { + code: status, + message: explain(status, &error_message(&body)), + }); + } + + let value: Value = serde_json::from_str(&body) + .map_err(|e| Error::Transport(format!("could not parse the model list: {e}")))?; + Ok(value + .get("data") + .and_then(|data| data.as_array()) + .map(|models| { + models + .iter() + .filter_map(|model| model.get("id").and_then(|id| id.as_str())) + .map(|id| id.to_string()) + .collect() + }) + .unwrap_or_default()) +} -- cgit v1.2.3