Skip to content

The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.

fix(provider): correct catalog listing to use download client

Steven Enamakeld9ebc7e7bee3
diff --git a/crates/openhuman-core/src/inference/provider/ops/models/catalog_listing.rs b/crates/openhuman-core/src/inference/provider/ops/models/catalog_listing.rs
new file mode 100644
index 0000000000..ef34de2561
--- /dev/null
+++ b/crates/openhuman-core/src/inference/provider/ops/models/catalog_listing.rs
@@ -0,0 +1,383 @@
+//! The `list_configured_models` RPC entry point: resolves a provider id to a
+//! `cloud_providers` entry (falling back to synthesized local-runtime /
+//! managed entries), builds an authenticated `/models` request for it, and
+//! parses the response.
+
+use super::*;
+
+/// Resolve the API key used to probe a provider's `/models` endpoint.
+///
+/// Cloud providers store their key in auth-profiles.json (via `lookup_key_for_slug`),
+/// but the OMLX local-runtime chip persists its Bearer key in
+/// `config.local_ai.api_key`. When the slug-scoped lookup comes back empty for omlx,
+/// fall back to that local-runtime key so the reachability probe still sends
+/// `Authorization: Bearer` (otherwise the OMLX server returns 401).
+pub(super) fn resolve_local_runtime_key(
+ slug: &str,
+ looked_up: String,
+ config: &crate::config::Config,
+) -> String {
+ let looked_up = looked_up.trim().to_string();
+ if !looked_up.is_empty() {
+ return looked_up;
+ }
+ if slug == "omlx" {
+ return config
+ .local_ai
+ .api_key
+ .clone()
+ .unwrap_or_default()
+ .trim()
+ .to_string();
+ }
+ looked_up
+}
+
+pub fn append_query_param(url: &str, key: &str, value: &str) -> String {
+ if let Ok(mut parsed) = reqwest::Url::parse(url) {
+ parsed.query_pairs_mut().append_pair(key, value);
+ return parsed.to_string();
+ }
+
+ let separator = if url.contains('?') { '&' } else { '?' };
+ format!("{url}{separator}{key}={value}")
+}
+
+pub async fn list_configured_models(
+ provider_id: &str,
+) -> Result<crate::rpc::RpcOutcome<serde_json::Value>, String> {
+ let config = crate::config::Config::load_or_init()
+ .await
+ .map_err(|e| e.to_string())?;
+
+ list_configured_models_from_config(provider_id, &config).await
+}
+
+pub async fn list_configured_models_from_config(
+ provider_id: &str,
+ config: &crate::config::Config,
+) -> Result<crate::rpc::RpcOutcome<serde_json::Value>, String> {
+ let provider_id = provider_id.trim().to_string();
+ if provider_id.is_empty() {
+ return Err("provider_id must not be empty".to_string());
+ }
+
+ log::debug!("[providers][list_models] provider_id={}", provider_id);
+
+ // Explicit `cloud_providers` entry wins (e.g. a user-pointed remote
+ // ollama box at https://ollama.example.com/v1). Falling back to the
+ // local-runtime synthesis below only happens when no entry matches.
+ let entry = config
+ .cloud_providers
+ .iter()
+ .find(|e| e.id == provider_id || e.slug == provider_id)
+ .cloned()
+ .or_else(|| synthesize_local_runtime_entry(&provider_id, config))
+ .or_else(|| synthesize_managed_entry(&provider_id))
+ .ok_or_else(|| format!("no cloud provider with id or slug '{}' found", provider_id))?;
+
+ let looked_up = crate::inference::provider::factory::lookup_key_for_slug(&entry.slug, config)
+ .unwrap_or_default();
+ let api_key = resolve_local_runtime_key(&entry.slug, looked_up, config);
+
+ let bearer_is_oauth = entry.slug == "openai"
+ && crate::inference::provider::factory::openai_bearer_is_oauth(config);
+ let routing =
+ resolve_openai_codex_routing(config, &entry.slug, &entry.endpoint, &api_key, bearer_is_oauth)
+ .unwrap_or_else(|err| {
+ log::warn!(
+ "[providers][list_models] openai codex routing unavailable; continuing with configured endpoint: {err}"
+ );
+ OpenAiCodexRouting::standard(&entry.endpoint)
+ });
+
+ let mut models_url = format!("{}/models", routing.endpoint);
+ let codex_client_version = routing.using_oauth.then(openai_codex_client_version);
+ if let Some(client_version) = codex_client_version.as_deref() {
+ models_url = append_query_param(&models_url, "client_version", client_version);
+ }
+
+ log::debug!(
+ "[providers][list_models] fetching url={} slug={} codex_oauth={} account_id_header={}",
+ models_url,
+ entry.slug,
+ routing.using_oauth,
+ routing.account_id.is_some()
+ );
+
+ let client =
+ crate::config::build_runtime_proxy_client_with_timeouts("providers.list_models", 30, 10);
+
+ use crate::config::schema::cloud_providers::AuthStyle;
+
+ // Managed backend (`openhuman`) needs a different URL *and* a different
+ // credential than every BYOK provider above, so neither `entry.endpoint`
+ // nor `lookup_key_for_slug` is usable here:
+ //
+ // * the seeded entry's endpoint is the *chat* base
+ // (`https://api.openhuman.ai/v1`), so `{endpoint}/models` would probe
+ // the wrong host entirely — the hosted API is resolved by
+ // `effective_backend_api_url`, which also ignores an `api_url`
+ // override pointing at a local/third-party inference host.
+ // * the session JWT lives in the `app-session` auth profile, not
+ // `provider:openhuman`, so `lookup_key_for_slug` returns "" and the
+ // request would go out unauthenticated (401).
+ //
+ // `?catalog=openrouter` is required: without it the backend returns only
+ // the curated tier list (chat-v1, reasoning-v1, ...), which is deliberately
+ // byte-identical to the legacy payload. The catalog listing is gated
+ // server-side by OPENROUTER_PASSTHROUGH_ENABLED and returns an empty set
+ // when the passthrough is off, so this degrades to "no models" rather than
+ // an error on a backend that has not enabled it.
+ let mut managed_token = String::new();
+ if entry.auth_style == AuthStyle::OpenhumanJwt {
+ // Do NOT propagate a missing session: a self-hosted entry may carry a
+ // provider-scoped key instead, and the auth arm below documents that
+ // fallback. Only fail when neither credential exists, so the error the
+ // caller sees names the real problem.
+ //
+ // Classify the session directly rather than going through
+ // `require_live_session_token`, which flattens "signed out" and "could
+ // not read the credential store" into one opaque Err. A lock timeout or
+ // filesystem error is a recoverable fault the picker should surface —
+ // swallowing it into a successful empty catalog hides it (review, #6206).
+ // A store error still propagates; only a genuinely absent/expired
+ // session degrades to an empty list.
+ use crate::security::credentials::session_support::{
+ classify_session_token, load_app_session_profile, publish_local_session_expiry,
+ SessionTokenCheck,
+ };
+ let profile = load_app_session_profile(config)?;
+ match classify_session_token(profile.as_ref(), chrono::Utc::now()) {
+ SessionTokenCheck::Live(token) => managed_token = token,
+ // Signed out is not a provider failure. The managed catalog has
+ // nothing to offer until there is a session, and managed stays
+ // selectable on its automatic routing — so return an empty list
+ // rather than surfacing "could not load models".
+ check if api_key.is_empty() => {
+ let reason = match check {
+ SessionTokenCheck::Expired => {
+ // Still announce the expiry: `require_live_session_token`
+ // did this for us before, and without it an expired token
+ // stays in the store with nothing prompting a re-auth.
+ publish_local_session_expiry("list_configured_models");
+ "session expired"
+ }
+ _ => "no session",
+ };
+ log::info!(
+ "[providers][list_models] managed catalog unavailable — {reason}; returning an empty list"
+ );
+ return Ok(crate::rpc::RpcOutcome::new(
+ serde_json::json!({ "models": Vec::<ModelInfo>::new() }),
+ vec![format!("{reason}; managed catalog is empty")],
+ ));
+ }
+ check => {
+ if matches!(check, SessionTokenCheck::Expired) {
+ publish_local_session_expiry("list_configured_models");
+ }
+ log::debug!(
+ "[providers][list_models] no live session; falling back to the provider-scoped key"
+ );
+ }
+ }
+ let base = crate::api::config::effective_backend_api_url(&config.api_url);
+ models_url = append_query_param(
+ &crate::api::config::api_url(&base, "/openai/v1/models"),
+ "catalog",
+ "openrouter",
+ );
+ log::debug!(
+ "[providers][list_models] managed catalog url={}",
+ models_url
+ );
+ }
+
+ if is_openrouter_provider(&entry) {
+ validate_openrouter_api_key(&client, &routing.endpoint, &api_key).await?;
+ }
+
+ // Whether the app session token actually made it onto the wire. It is not
+ // the same question as "is `managed_token` non-empty": the credential-safety
+ // guard below can decline to attach it, and a 401 on an unauthenticated
+ // request must not be read as a stale session (review, #6206).
+ let mut managed_session_attached = false;
+ let mut request = client.get(&models_url);
+ if routing.using_oauth {
+ request = request
+ .header(reqwest::header::USER_AGENT, openai_codex_user_agent())
+ .header(OPENAI_CODEX_ORIGINATOR_HEADER, OPENAI_CODEX_ORIGINATOR);
+ }
+
+ request = match entry.auth_style {
+ AuthStyle::Bearer => {
+ if !api_key.is_empty() {
+ let mut r = request.header("Authorization", format!("Bearer {}", api_key));
+ if let Some(account_id) = routing.account_id.as_deref() {
+ r = r.header(OPENAI_CODEX_ACCOUNT_HEADER, account_id);
+ }
+ r
+ } else {
+ request
+ }
+ }
+ AuthStyle::Anthropic => {
+ let mut r = request.header("anthropic-version", "2023-06-01");
+ if !api_key.is_empty() {
+ r = r.header("x-api-key", &api_key);
+ }
+ r
+ }
+ AuthStyle::OpenhumanJwt => {
+ // Prefer the live session JWT resolved above; fall back to a
+ // provider-scoped key so a self-hosted entry that stores one still
+ // authenticates.
+ let token = if !managed_token.is_empty() {
+ managed_token.as_str()
+ } else {
+ api_key.as_str()
+ };
+ managed_session_attached = managed_session_attaches(&managed_token, &models_url);
+ // Never put a bearer credential on the wire in clear text. `https`
+ // or a loopback host only — loopback stays allowed so a local
+ // backend (BACKEND_URL=http://127.0.0.1:...) still works in dev.
+ if !token.is_empty() && url_is_credential_safe(&models_url) {
+ // Managed traffic is attributed per embedding product
+ // (OpenCompany / Medulla / desktop); the generic provider client
+ // does not carry it, so attach it explicitly.
+ let (name, value) = crate::api::product::product_identity_header();
+ request
+ .header("Authorization", format!("Bearer {}", token))
+ .header(name, value)
+ } else if !token.is_empty() {
+ log::warn!(
+ "[providers][list_models] refusing to send a bearer token to a non-https, non-loopback URL"
+ );
+ request
+ } else {
+ request
+ }
+ }
+ AuthStyle::None => request,
+ };
+
+ let response = request
+ .send()
+ .await
+ .map_err(|e| format!("[providers][list_models] HTTP request failed: {}", e))?;
+
+ let status = response.status();
+ if !status.is_success() {
+ // A 401 from the MANAGED catalog means the caller is not signed in —
+ // the stored session was rejected server-side even though its local
+ // `exp` was still valid, so `require_live_session_token` handed us a
+ // token the backend no longer honours. That is a signed-out state, not
+ // a provider failure, and rendering it as "could not load models" put a
+ // red error under managed for a user whose only problem is a stale
+ // session. Managed stays selectable on its automatic routing, so return
+ // an empty catalog and let the app's normal auth surfaces prompt for
+ // re-authentication.
+ //
+ // Scoped to the managed provider on purpose: for a BYOK provider a 401
+ // IS the actionable error (a wrong or revoked API key), and hiding it
+ // would strand the user with a silently empty dropdown.
+ if managed_401_means_signed_out(status.as_u16(), entry.auth_style, managed_session_attached)
+ {
+ log::info!(
+ "[providers][list_models] managed catalog unavailable — backend rejected the session token (401); returning an empty list"
+ );
+ return Ok(crate::rpc::RpcOutcome::new(
+ serde_json::json!({ "models": Vec::<ModelInfo>::new() }),
+ vec!["session not accepted; managed catalog is empty".to_string()],
+ ));
+ }
+ let body = response.text().await.unwrap_or_default();
+ let sanitized = sanitize_api_error(&body);
+ let truncated = crate::util::truncate_with_ellipsis(&sanitized, 300);
+ // TAURI-RUST-8X3: a 404 from the `<base>/models` probe means the
+ // configured base URL does not host an OpenAI-compatible `/models`
+ // listing (wrong base, a model-only proxy, or a missing `/v1`
+ // suffix). Append an actionable hint so the model-dropdown probe
+ // surfaces *recovery guidance* inline instead of the bare Go-style
+ // `404 page not found`. The `provider returned 404` prefix is kept
+ // verbatim so the `is_provider_user_state_message` classifier anchor
+ // (which demotes this preventable user-state case out of Sentry)
+ // still matches — see `crates/openhuman-core/src/core/observability.rs`.
+ if status.as_u16() == 404 {
+ return Err(format!(
+ "provider returned 404: {} — the configured base URL does not expose a `/models` endpoint; check the provider's base URL (it usually ends in `/v1`)",
+ truncated
+ ));
+ }
+ return Err(format!(
+ "provider returned {}: {}",
+ status.as_u16(),
+ truncated
+ ));
+ }
+
+ // TAURI-RUST-12: `response.json()` discards the body when decoding fails,
+ // so Sentry just sees `error decoding response body` with no clue what the
+ // server actually sent. In practice the offending body is HTML from a
+ // captive portal / corporate proxy login page, an upstream load-balancer
+ // 502 served as HTML with a `200 OK`, or a JSON parser tripping on a
+ // wrong-path endpoint. Read the body as text first, then parse, and
+ // surface a sanitized + truncated snippet so the failure is diagnosable
+ // from the error string alone.
+ let raw_body = response.text().await.map_err(|e| {
+ format!(
+ "[providers][list_models] failed to read response body: {}",
+ e
+ )
+ })?;
+ let body: serde_json::Value = serde_json::from_str(&raw_body).map_err(|e| {
+ let sanitized = sanitize_api_error(&raw_body);
+ let snippet = crate::util::truncate_with_ellipsis(&sanitized, 300);
+ format!(
+ "[providers][list_models] failed to parse JSON: {} (body: {})",
+ e, snippet
+ )
+ })?;
+
+ // OpenAI-compatible servers occasionally return HTTP 200 with an error
+ // payload instead of a 4xx (LM Studio does this for unknown paths like
+ // `/v11/models` — body `{"error":"Unexpected endpoint or method..."}`).
+ // Treat any top-level `error` field as a failure so the AI-panel probe
+ // doesn't silently accept a typo'd endpoint.
+ if let Some(err_field) = body.get("error") {
+ let msg = err_field
+ .as_str()
+ .map(|s| s.to_string())
+ .or_else(|| {
+ err_field
+ .get("message")
+ .and_then(|m| m.as_str())
+ .map(|s| s.to_string())
+ })
+ .unwrap_or_else(|| err_field.to_string());
+ let sanitized = sanitize_api_error(&msg);
+ return Err(format!("provider returned error payload: {}", sanitized));
+ }
+
+ // Parse the OpenAI-compatible `/models` envelope into typed model
+ // entries. See `parse_models_response` for the distinct error shapes
+ // returned for "missing field" vs "field present but wrong type"
+ // (TAURI-RUST-4Y). The ChatGPT Codex backend uses a sibling `models`
+ // array keyed by `slug`, so that shape is accepted here too.
+ let mut models = parse_models_response(&body)?;
+ if routing.using_oauth {
+ merge_openai_codex_model_hints(&mut models);
+ }
+
+ log::info!(
+ "[providers][list_models] slug={} fetched {} models",
+ entry.slug,
+ models.len()
+ );
+
+ Ok(crate::rpc::RpcOutcome::new(
+ serde_json::json!({ "models": models }),
+ vec![format!("fetched {} models", models.len())],
+ ))
+}
diff --git a/crates/openhuman-core/src/integrations/client/download.rs b/crates/openhuman-core/src/integrations/client/download.rs
new file mode 100644
index 0000000000..53695f3fed
--- /dev/null
+++ b/crates/openhuman-core/src/integrations/client/download.rs
@@ -0,0 +1,113 @@
+//! Raw-bytes download support for [`IntegrationClient`]: the file-storage
+//! download route bypasses the JSON envelope and needs `Content-Type` /
+//! `Content-Disposition` metadata the SDK's binary primitive doesn't expose.
+
+use tinyhumans_sdk::Error as SdkError;
+
+use super::construct::IntegrationClient;
+use super::requests::{emit_backend_egress, enforce_backend_egress};
+
+/// Extract the `filename` (or RFC 5987 `filename*`) parameter from a
+/// `Content-Disposition` header value, e.g.
+/// `attachment; filename="report.pdf"` → `Some("report.pdf")`.
+/// Best-effort: unparseable values yield `None` and callers fall back to
+/// their own naming scheme.
+fn parse_content_disposition_filename(value: &str) -> Option<String> {
+ for part in value.split(';') {
+ let part = part.trim();
+ let lower = part.to_ascii_lowercase();
+ if let Some(rest) = lower
+ .starts_with("filename*=")
+ .then(|| &part["filename*=".len()..])
+ {
+ // RFC 5987: filename*=UTF-8''percent-encoded — keep the tail
+ // after the last `'` and leave percent-decoding to callers who
+ // care (the raw form is still a usable, safe name).
+ let tail = rest.rsplit('\'').next().unwrap_or(rest);
+ let trimmed = tail.trim_matches('"').trim();
+ if !trimmed.is_empty() {
+ return Some(trimmed.to_string());
+ }
+ } else if let Some(rest) = lower
+ .starts_with("filename=")
+ .then(|| &part["filename=".len()..])
+ {
+ let trimmed = rest.trim().trim_matches('"').trim();
+ if !trimmed.is_empty() {
+ return Some(trimmed.to_string());
+ }
+ }
+ }
+ None
+}
+
+impl IntegrationClient {
+ /// Authenticated GET returning the raw response body plus content-type and
+ /// any `Content-Disposition` filename. Used for backend download routes
+ /// that `302`-redirect to a presigned S3 URL: reqwest follows redirects by
+ /// default and its redirect policy strips sensitive headers (including
+ /// `Authorization`) on cross-host hops, so the bearer token never leaks to
+ /// S3 while the presigned URL still authorizes the fetch.
+ pub async fn get_bytes(
+ &self,
+ path: &str,
+ ) -> anyhow::Result<(bytes::Bytes, Option<String>, Option<String>)> {
+ enforce_backend_egress(path)?;
+ emit_backend_egress(path);
+ self.ensure_budget_available(path).await?;
+ let route = path.split('?').next().unwrap_or(path);
+ let segments = route.trim_matches('/').split('/').collect::<Vec<_>>();
+ let is_file_download = matches!(
+ segments.as_slice(),
+ ["agent-integrations", "file-storage", "files", _, "download"]
+ );
+ if !is_file_download {
+ anyhow::bail!("route is intentionally not exposed by the SDK: GET {route}");
+ }
+ let url = crate::api::config::api_url(&self.backend_url, path);
+ tracing::debug!("[integrations] GET(bytes) {}", url);
+
+ let resp = self
+ .download_client
+ .get(&url)
+ .header("Authorization", format!("Bearer {}", self.auth_token))
+ .send()
+ .await
+ .map_err(|error| Self::report_transport_error(error, "get_bytes", path, &url))?;
+ let status = resp.status();
+ if !status.is_success() {
+ let body_text = resp.text().await.unwrap_or_default();
+ let body =
+ serde_json::from_str(&body_text).unwrap_or(serde_json::Value::String(body_text));
+ return Err(Self::map_sdk_error(
+ SdkError::Status {
+ status: status.as_u16(),
+ body,
+ },
+ "get_bytes",
+ path,
+ &url,
+ ));
+ }
+ let content_type = resp
+ .headers()
+ .get(reqwest::header::CONTENT_TYPE)
+ .and_then(|value| value.to_str().ok())
+ .map(str::to_owned);
+ let filename = resp
+ .headers()
+ .get(reqwest::header::CONTENT_DISPOSITION)
+ .and_then(|value| value.to_str().ok())
+ .and_then(parse_content_disposition_filename);
+ let body = resp.bytes().await.map_err(|error| {
+ anyhow::anyhow!("failed to read response body for GET {url}: {error}")
+ })?;
+ tracing::debug!(
+ "[integrations] GET(bytes) {} → {} bytes (content_type={:?})",
+ url,
+ body.len(),
+ content_type
+ );
+ Ok((body, content_type, filename))
+ }
+}