Skip to content

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

refactor(client): split backend client into focused modules

Steven Enamakel466c2b107cab
diff --git a/crates/openhuman-tinyhumans/src/client/backend_client.rs b/crates/openhuman-tinyhumans/src/client/backend_client.rs
index 58b6378046..d76ef1f01b 100644
--- a/crates/openhuman-tinyhumans/src/client/backend_client.rs
+++ b/crates/openhuman-tinyhumans/src/client/backend_client.rs
@@ -4,9 +4,7 @@
use std::sync::{Arc, Mutex};
use anyhow::{Context, Result};
-use openhuman_embed::__host::backend::transport::{
- BackendTransport, BackendTransportError, TransportProfile,
-};
+use openhuman_embed::__host::backend::transport::{BackendTransport, TransportProfile};
use openhuman_embed::__host::config::Config;
use openhuman_embed::__host::security::credentials::session_support::BackendCredential;
use openhuman_embed::BackendRequest;
@@ -169,12 +167,6 @@ impl BackendClient {
}
}
-/// Map a transport `Unavailable` into the typed error; shared with
-/// [`IntegrationsHttp`](super::IntegrationsHttp).
-pub(super) fn is_unavailable(error: &BackendTransportError) -> bool {
- matches!(error, BackendTransportError::Unavailable)
-}
-
#[cfg(test)]
#[path = "backend_client_tests.rs"]
mod tests;
diff --git a/crates/openhuman-tinyhumans/src/client/channel.rs b/crates/openhuman-tinyhumans/src/client/channel.rs
new file mode 100644
index 0000000000..142cbb0f0c
--- /dev/null
+++ b/crates/openhuman-tinyhumans/src/client/channel.rs
@@ -0,0 +1,208 @@
+//! Channel message and thread routes on the authenticated backend client.
+//!
+//! Ported from the core's `backend/client/channel.rs`; the routes are unchanged.
+
+use anyhow::Result;
+use reqwest::Method;
+use serde_json::{json, Value};
+
+use openhuman_embed::__host::security::credentials::session_support::BackendCredential;
+
+use super::backend_client::BackendClient;
+
+impl BackendClient {
+ /// Sends a message to a communication channel.
+ pub async fn send_channel_message(
+ &self,
+ channel: &str,
+ credential: impl Into<BackendCredential>,
+ message_body: Value,
+ ) -> Result<Value> {
+ let channel = channel.trim().trim_matches('/');
+ anyhow::ensure!(!channel.is_empty(), "channel is required");
+ let encoded = urlencoding::encode(channel);
+ self.authed_json(
+ credential,
+ Method::POST,
+ &format!("channels/{encoded}/messages"),
+ Some(message_body),
+ )
+ .await
+ }
+
+ /// Signals "the agent is typing…" on a channel that supports it
+ /// (Telegram's `sendChatAction`, Slack's typing event, …). The backend
+ /// resolves the target chat from the channel integration metadata and
+ /// is responsible for hitting the provider-native API.
+ ///
+ /// Telegram keeps the typing indicator alive for ~5 seconds per call,
+ /// so callers should re-invoke every ~4 s for as long as the turn is
+ /// in flight. Returns `Err` if the backend doesn't support typing for
+ /// this channel — caller should swallow the error silently.
+ pub async fn send_channel_typing(
+ &self,
+ channel: &str,
+ credential: impl Into<BackendCredential>,
+ ) -> Result<Value> {
+ let channel = channel.trim().trim_matches('/');
+ anyhow::ensure!(!channel.is_empty(), "channel is required");
+ let encoded = urlencoding::encode(channel);
+ self.authed_json(
+ credential,
+ Method::POST,
+ &format!("channels/{encoded}/typing"),
+ Some(json!({})),
+ )
+ .await
+ }
+
+ /// Edits an existing channel message. Used by the progressive-edit
+ /// streaming path (Telegram / Slack) to coalesce live deltas into a
+ /// single evolving outbound message rather than spamming the chat
+ /// with one bubble per token.
+ ///
+ /// `message_id` is the backend-returned id of the message that was
+ /// first sent via [`Self::send_channel_message`]. Returns the
+ /// updated message record, or an `Err` if the backend does not
+ /// support editing for this channel (caller should fall back to
+ /// atomic-final delivery).
+ ///
+ /// **The deployed backend does not implement this route yet** (#5230): its
+ /// channel router has `POST /:channel/messages` and `DELETE
+ /// /:channel/messages/:messageId` only, so every call currently returns the
+ /// unmatched-route 404, which [`authed_json`](Self::authed_json) surfaces as
+ /// [`HostedApiError::ChannelEditUnsupported`](super::HostedApiError::ChannelEditUnsupported). Callers must degrade rather
+ /// than treat that as the message having been deleted. Keep this method: it
+ /// starts working unchanged the moment the backend adds the route.
+ pub async fn send_channel_edit(
+ &self,
+ channel: &str,
+ message_id: &str,
+ credential: impl Into<BackendCredential>,
+ edit_body: Value,
+ ) -> Result<Value> {
+ let channel = channel.trim().trim_matches('/');
+ anyhow::ensure!(!channel.is_empty(), "channel is required");
+ anyhow::ensure!(!message_id.is_empty(), "message_id is required");
+ let encoded_channel = urlencoding::encode(channel);
+ let encoded_id = urlencoding::encode(message_id);
+ self.authed_json(
+ credential,
+ Method::PATCH,
+ &format!("channels/{encoded_channel}/messages/{encoded_id}"),
+ Some(edit_body),
+ )
+ .await
+ }
+
+ /// Deletes a message from a communication channel. Used to clean up
+ /// ephemeral messages (e.g. thinking indicators) after the final
+ /// response has been delivered.
+ pub async fn send_channel_delete(
+ &self,
+ channel: &str,
+ message_id: &str,
+ credential: impl Into<BackendCredential>,
+ ) -> Result<Value> {
+ let channel = channel.trim().trim_matches('/');
+ anyhow::ensure!(!channel.is_empty(), "channel is required");
+ anyhow::ensure!(!message_id.is_empty(), "message_id is required");
+ let encoded_channel = urlencoding::encode(channel);
+ let encoded_id = urlencoding::encode(message_id);
+ self.authed_json(
+ credential,
+ Method::DELETE,
+ &format!("channels/{encoded_channel}/messages/{encoded_id}"),
+ None,
+ )
+ .await
+ }
+
+ /// Sends a reaction (e.g. emoji) to a message in a channel.
+ pub async fn send_channel_reaction(
+ &self,
+ channel: &str,
+ credential: impl Into<BackendCredential>,
+ reaction_body: Value,
+ ) -> Result<Value> {
+ let channel = channel.trim().trim_matches('/');
+ anyhow::ensure!(!channel.is_empty(), "channel is required");
+ let encoded = urlencoding::encode(channel);
+ self.authed_json(
+ credential,
+ Method::POST,
+ &format!("channels/{encoded}/reactions"),
+ Some(reaction_body),
+ )
+ .await
+ }
+
+ /// Creates a new thread in a communication channel.
+ pub async fn create_channel_thread(
+ &self,
+ channel: &str,
+ credential: impl Into<BackendCredential>,
+ title: &str,
+ ) -> Result<Value> {
+ let channel = channel.trim().trim_matches('/');
+ anyhow::ensure!(!channel.is_empty(), "channel is required");
+ anyhow::ensure!(!title.trim().is_empty(), "title is required");
+ let encoded = urlencoding::encode(channel);
+ let body = serde_json::json!({ "title": title.trim() });
+ self.authed_json(
+ credential,
+ Method::POST,
+ &format!("channels/{encoded}/threads"),
+ Some(body),
+ )
+ .await
+ }
+
+ /// Updates an existing thread (e.g., closing or reopening it).
+ pub async fn update_channel_thread(
+ &self,
+ channel: &str,
+ credential: impl Into<BackendCredential>,
+ thread_id: &str,
+ action: &str,
+ ) -> Result<Value> {
+ let channel = channel.trim().trim_matches('/');
+ anyhow::ensure!(!channel.is_empty(), "channel is required");
+ anyhow::ensure!(!thread_id.trim().is_empty(), "threadId is required");
+ anyhow::ensure!(
+ action == "close" || action == "reopen",
+ "action must be 'close' or 'reopen'"
+ );
+ let encoded_channel = urlencoding::encode(channel);
+ let encoded_thread = urlencoding::encode(thread_id.trim());
+ let body = serde_json::json!({ "action": action });
+ self.authed_json(
+ credential,
+ Method::PATCH,
+ &format!("channels/{encoded_channel}/threads/{encoded_thread}"),
+ Some(body),
+ )
+ .await
+ }
+
+ /// Lists threads in a communication channel, optionally filtering by status.
+ pub async fn list_channel_threads(
+ &self,
+ channel: &str,
+ credential: impl Into<BackendCredential>,
+ active_filter: Option<bool>,
+ ) -> Result<Value> {
+ let channel = channel.trim().trim_matches('/');
+ anyhow::ensure!(!channel.is_empty(), "channel is required");
+ let encoded = urlencoding::encode(channel);
+ let mut path = format!("channels/{encoded}/threads");
+ if let Some(active) = active_filter {
+ path.push_str(if active {
+ "?active=true"
+ } else {
+ "?active=false"
+ });
+ }
+ self.authed_json(credential, Method::GET, &path, None).await
+ }
+}
diff --git a/crates/openhuman-tinyhumans/src/client/classify.rs b/crates/openhuman-tinyhumans/src/client/classify.rs
index 311cf1b2b9..70b5841c1f 100644
--- a/crates/openhuman-tinyhumans/src/client/classify.rs
+++ b/crates/openhuman-tinyhumans/src/client/classify.rs
@@ -86,7 +86,7 @@ impl BackendClient {
let mut src: Option<&(dyn std::error::Error + 'static)> =
std::error::Error::source(&e);
while let Some(s) = src {
- error_message.push_str(" -> ");
+ error_message.push_str(" → ");
error_message.push_str(&s.to_string());
src = s.source();
}
diff --git a/crates/openhuman-tinyhumans/src/client/integrations_http.rs b/crates/openhuman-tinyhumans/src/client/integrations_http.rs
new file mode 100644
index 0000000000..85e0c3d6e7
--- /dev/null
+++ b/crates/openhuman-tinyhumans/src/client/integrations_http.rs
@@ -0,0 +1,255 @@
+//! [`IntegrationsHttp`]: the transport half of the `/agent-integrations/*`
+//! client: route and credential guards, and the JSON / multipart sends over
+//! [`TransportProfile::Integrations`].
+//!
+//! This is what the core's `IntegrationClient` does between "decide to call"
+//! and "parse the envelope". The pieces that need core policy (egress
+//! disclosure through `__host::egress`, the budget gate, pricing, envelope
+//! parsing and 401 recovery) stay with the integrations domain that moves onto
+//! this helper. Errors come back as the raw
+//! [`BackendTransportError`], unclassified, so that domain can map them.
+
+use std::sync::{Arc, OnceLock};
+use std::time::Duration;
+
+use anyhow::Result;
+use openhuman_embed::__host::backend::transport::{
+ BackendTransport, BackendTransportError, TransportProfile,
+};
+use openhuman_embed::__host::security::credentials::session_support::BackendCredential;
+use openhuman_embed::__host::util::tls::tls_client_builder;
+use openhuman_embed::__host::util::url::{join_url, normalize_backend_api_base_url};
+use openhuman_embed::__host::util::redact_url_for_log;
+use openhuman_embed::BackendRequest;
+use reqwest::header::{HeaderMap, HeaderValue, AUTHORIZATION};
+use reqwest::Method;
+use serde_json::Value;
+
+use super::backend_client::shared_transport;
+use super::endpoint::validate_credential_endpoint;
+
+const LOG_PREFIX: &str = "[tinyhumans-integrations-http]";
+/// The header a TinyHumans API key rides on.
+pub const API_KEY_HEADER: &str = "x-api-key";
+
+/// Strip any inference-style path that snuck into a backend URL (issue #2075,
+/// Sentry `OPENHUMAN-TAURI-H6`/`-HN`): every route is joined onto the field, so
+/// `.../openai/v1/chat/completions` would 404 every integration call.
+/// Idempotent.
+pub fn sanitize_backend_url(backend_url: &str) -> String {
+ let cleaned = normalize_backend_api_base_url(backend_url);
+ let trimmed = backend_url.trim().trim_end_matches('/');
+ if !cleaned.is_empty() && cleaned != trimmed {
+ tracing::warn!(
+ input = %redact_url_for_log(trimmed),
+ cleaned = %redact_url_for_log(&cleaned),
+ "[integrations] backend_url carried an inference / non-root path; \
+ stripping before use (issue #2075)"
+ );
+ }
+ if cleaned.is_empty() {
+ backend_url.to_string()
+ } else {
+ cleaned
+ }
+}
+
+/// Refuse routes the SDK intentionally does not expose to this client
+/// (`webhooks`, `admin`).
+pub fn reject_privileged_backend_path(method: &str, path: &str) -> Result<()> {
+ let route = path.split('?').next().unwrap_or(path);
+ if route.split('/').any(|segment| {
+ segment.eq_ignore_ascii_case("webhooks") || segment.eq_ignore_ascii_case("admin")
+ }) {
+ anyhow::bail!(
+ "route is intentionally not exposed by the SDK: {} {}",
+ method,
+ route
+ );
+ }
+ Ok(())
+}
+
+/// `Authorization: Bearer` for a session, `x-api-key` for an API key. For
+/// requests built outside the transport (binary download, raw DELETE).
+pub fn credential_headers(credential: &BackendCredential) -> Result<HeaderMap> {
+ let secret = credential.secret().trim();
+ let mut headers = HeaderMap::new();
+ match credential {
+ BackendCredential::Session(_) => {
+ headers.insert(
+ AUTHORIZATION,
+ HeaderValue::from_str(&format!("Bearer {secret}"))
+ .map_err(|e| anyhow::anyhow!("invalid backend credential header: {e}"))?,
+ );
+ }
+ BackendCredential::ApiKey(_) => {
+ headers.insert(
+ API_KEY_HEADER,
+ HeaderValue::from_str(secret)
+ .map_err(|e| anyhow::anyhow!("invalid backend credential header: {e}"))?,
+ );
+ }
+ }
+ Ok(headers)
+}
+
+fn download_clients() -> &'static (reqwest::Client, reqwest::Client) {
+ static CLIENTS: OnceLock<(reqwest::Client, reqwest::Client)> = OnceLock::new();
+ CLIENTS.get_or_init(|| {
+ // Neither client carries the product identity: their one caller
+ // fetches a route that can redirect to presigned storage. The first
+ // stops at the redirect, the second follows it without any backend
+ // credential or header.
+ let build = || {
+ tls_client_builder()
+ .http1_only()
+ .redirect(reqwest::redirect::Policy::none())
+ .timeout(Duration::from_secs(15 * 60))
+ .connect_timeout(Duration::from_secs(15))
+ .build()
+ .expect("failed to build integration download HTTP client")
+ };
+ (build(), build())
+ })
+}
+
+/// Authenticated sends to the hosted `/agent-integrations/*` routes.
+#[derive(Clone)]
+pub struct IntegrationsHttp {
+ backend_url: String,
+ credential: BackendCredential,
+ transport: Option<Arc<dyn BackendTransport>>,
+}
+
+impl IntegrationsHttp {
+ /// A helper for `backend_url` (sanitised) authenticating with `credential`.
+ pub fn new(backend_url: &str, credential: BackendCredential) -> Self {
+ Self {
+ backend_url: sanitize_backend_url(backend_url),
+ credential,
+ transport: None,
+ }
+ }
+
+ /// Send through `transport` instead of the shared SDK transport.
+ pub fn with_transport(mut self, transport: Arc<dyn BackendTransport>) -> Self {
+ self.transport = Some(transport);
+ self
+ }
+
+ /// The (sanitised) backend origin routes are joined onto.
+ pub fn backend_url(&self) -> &str {
+ &self.backend_url
+ }
+
+ /// The credential requests carry.
+ pub fn credential(&self) -> &BackendCredential {
+ &self.credential
+ }
+
+ /// Whether the credential is a TinyHumans API key rather than a session.
+ pub fn uses_api_key(&self) -> bool {
+ self.credential.is_api_key()
+ }
+
+ /// The absolute URL for `path` (for logs and error text).
+ pub fn url_for(&self, path: &str) -> String {
+ join_url(&self.backend_url, path)
+ }
+
+ /// Check the credential may be sent to this origin (managed host or
+ /// loopback for a key; HTTPS or loopback for any credential).
+ pub fn validate_credential_endpoint(&self) -> Result<()> {
+ validate_credential_endpoint(&self.backend_url, self.uses_api_key())
+ }
+
+ /// The auth headers for a request built outside the transport.
+ pub fn auth_headers(&self) -> Result<HeaderMap> {
+ self.validate_credential_endpoint()?;
+ credential_headers(&self.credential)
+ }
+
+ /// The no-redirect client for binary downloads (no attribution headers).
+ pub fn download_client(&self) -> reqwest::Client {
+ download_clients().0.clone()
+ }
+
+ /// The client that follows a presigned-storage redirect without any
+ /// backend credential.
+ pub fn presigned_client(&self) -> reqwest::Client {
+ download_clients().1.clone()
+ }
+
+ fn resolve_transport(&self) -> Result<Arc<dyn BackendTransport>, BackendTransportError> {
+ match &self.transport {
+ Some(transport) => Ok(transport.clone()),
+ None => shared_transport()
+ .map(|t| t as Arc<dyn BackendTransport>)
+ .map_err(|error| {
+ log::debug!("{LOG_PREFIX} transport unavailable: {error:#}");
+ BackendTransportError::Unavailable
+ }),
+ }
+ }
+
+ fn request<'a>(
+ &'a self,
+ method: Method,
+ path: &'a str,
+ body: Option<&'a Value>,
+ ) -> BackendRequest<'a> {
+ BackendRequest {
+ profile: TransportProfile::Integrations,
+ base_url: &self.backend_url,
+ method,
+ path,
+ query: &[],
+ body,
+ credential: Some(&self.credential),
+ // The integrations domain parses the `{success,data}` envelope
+ // itself so its error classification sees the raw shape.
+ unwrap_envelope: false,
+ }
+ }
+
+ /// Send one JSON round-trip and return the raw (envelope-intact) body.
+ /// Runs the credential and route guards first; guard failures are `Other`.
+ pub async fn send_json(
+ &self,
+ method: Method,
+ path: &str,
+ body: Option<&Value>,
+ ) -> Result<Value, BackendTransportError> {
+ self.validate_credential_endpoint()
+ .map_err(|e| BackendTransportError::Other(e.to_string()))?;
+ reject_privileged_backend_path(method.as_str(), path)
+ .map_err(|e| BackendTransportError::Other(e.to_string()))?;
+ let transport = self.resolve_transport()?;
+ log::trace!("{LOG_PREFIX} {} {path}", method.as_str());
+ transport
+ .send_json(self.request(method, path, body))
+ .await
+ }
+
+ /// Send a `multipart/form-data` upload and return the response body.
+ pub async fn send_multipart(
+ &self,
+ path: &str,
+ form: reqwest::multipart::Form,
+ ) -> Result<Value, BackendTransportError> {
+ self.validate_credential_endpoint()
+ .map_err(|e| BackendTransportError::Other(e.to_string()))?;
+ reject_privileged_backend_path("POST", path)
+ .map_err(|e| BackendTransportError::Other(e.to_string()))?;
+ let transport = self.resolve_transport()?;
+ log::trace!("{LOG_PREFIX} POST(multipart) {path}");
+ transport
+ .send_multipart(self.request(Method::POST, path, None), form)
+ .await
+ }
+}
+
+#[cfg(test)]
+#[path = "integrations_http_tests.rs"]
+mod tests;