summaryrefslogtreecommitdiffstats
path: root/src/provider.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/provider.rs')
-rw-r--r--src/provider.rs49
1 files changed, 49 insertions, 0 deletions
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 {
448 #[serde(default)] 448 #[serde(default)]
449 arguments: Option<String>, 449 arguments: Option<String>,
450} 450}
451
452/// Lists the models the provider offers for this key, for `--doctor`.
453pub fn models(cfg: &Config) -> Result<Vec<String>, Error> {
454 let agent: ureq::Agent = ureq::Agent::config_builder()
455 .http_status_as_error(false)
456 .timeout_connect(Some(Duration::from_secs(10)))
457 .timeout_recv_response(Some(Duration::from_secs(20)))
458 .timeout_per_read(Some(Duration::from_secs(20)))
459 .build()
460 .into();
461
462 let url = format!("{}/models", cfg.base_url.trim_end_matches('/'));
463 let mut response = agent
464 .get(&url)
465 .header("Authorization", format!("Bearer {}", cfg.api_key))
466 .header("User-Agent", concat!("agent686/", env!("CARGO_PKG_VERSION")))
467 .header("x-opencode-session", &cfg.session)
468 .call()
469 .map_err(|e| Error::Transport(e.to_string()))?;
470
471 let status = response.status().as_u16();
472 let body = response
473 .body_mut()
474 .with_config()
475 .limit(8 * 1024 * 1024)
476 .read_to_string()
477 .unwrap_or_default();
478
479 if !(200..300).contains(&status) {
480 return Err(Error::Status {
481 code: status,
482 message: explain(status, &error_message(&body)),
483 });
484 }
485
486 let value: Value = serde_json::from_str(&body)
487 .map_err(|e| Error::Transport(format!("could not parse the model list: {e}")))?;
488 Ok(value
489 .get("data")
490 .and_then(|data| data.as_array())
491 .map(|models| {
492 models
493 .iter()
494 .filter_map(|model| model.get("id").and_then(|id| id.as_str()))
495 .map(|id| id.to_string())
496 .collect()
497 })
498 .unwrap_or_default())
499}