Skip to content

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

fix(ops): handle missing referral code in referral ops

Steven Enamakel814fcbe8cc41
diff --git a/crates/openhuman-tinyhumans/src/hosted/referral/ops.rs b/crates/openhuman-tinyhumans/src/hosted/referral/ops.rs
index 0cfc079978..0abd081085 100644
--- a/crates/openhuman-tinyhumans/src/hosted/referral/ops.rs
+++ b/crates/openhuman-tinyhumans/src/hosted/referral/ops.rs
@@ -1,38 +1,23 @@
-//! Referral program — authenticated calls to the hosted API (`/referral/*`).
+//! Referral program — authenticated calls to the hosted API (`/referral/*`)
+//! through the SDK's typed `referral()` client.
//!
-//! The desktop WebView `fetch` to the backend can fail with a generic "Load failed"
-//! (CORS / TLS / WebKit). These ops reuse the same `reqwest` path as billing.
+//! The desktop WebView `fetch` to the backend can fail with a generic "Load
+//! failed" (CORS / TLS / WebKit), so these run in-process like billing.
-use reqwest::Method;
-use serde_json::{json, Map, Value};
+use serde_json::Value;
+use tinyhumans_sdk::api::types::ClaimReferralRequest;
-use openhuman_core::api::config::effective_backend_api_url;
-use openhuman_core::api::jwt::get_session_token;
-use openhuman_core::api::BackendOAuthClient;
use openhuman_core::config::Config;
use openhuman_core::rpc::RpcOutcome;
-fn require_token(config: &Config) -> Result<String, String> {
- get_session_token(config)?
- .and_then(|v| {
- let t = v.trim().to_string();
- if t.is_empty() {
- None
- } else {
- Some(t)
- }
- })
- .ok_or_else(|| "no backend session token; run auth_store_session first".to_string())
-}
+use crate::hosted::client::HostedClient;
pub async fn get_stats(config: &Config) -> Result<RpcOutcome<Value>, String> {
- let token = require_token(config)?;
- let api_url = effective_backend_api_url(&config.api_url);
- let client = BackendOAuthClient::new(&api_url).map_err(|e| e.to_string())?;
- let data = client
- .authed_json(&token, Method::GET, "/referral/stats", None)
- .await
- .map_err(|e| e.to_string())?;
+ let client = HostedClient::from_config(config)?;
+ let data = client.finish_value(
+ "GET /referral/stats",
+ client.sdk().referral().get_referral_stats().await,
+ )?;
Ok(RpcOutcome::single_log(
data,
"referral stats fetched from backend GET /referral/stats",
@@ -44,26 +29,18 @@ pub async fn claim_referral(
code: &str,
device_fingerprint: Option<&str>,
) -> Result<RpcOutcome<Value>, String> {
- let token = require_token(config)?;
- let api_url = effective_backend_api_url(&config.api_url);
- let client = BackendOAuthClient::new(&api_url).map_err(|e| e.to_string())?;
-
- let mut body = Map::new();
- body.insert("code".to_string(), json!(code.trim()));
- if let Some(fp) = device_fingerprint.map(str::trim).filter(|s| !s.is_empty()) {
- body.insert("deviceFingerprint".to_string(), json!(fp));
- }
-
- let data = client
- .authed_json(
- &token,
- Method::POST,
- "/referral/claim",
- Some(Value::Object(body)),
- )
- .await
- .map_err(|e| e.to_string())?;
-
+ let client = HostedClient::from_config(config)?;
+ let request = ClaimReferralRequest {
+ code: code.trim().to_string(),
+ device_fingerprint: device_fingerprint
+ .map(str::trim)
+ .filter(|s| !s.is_empty())
+ .map(str::to_string),
+ };
+ let data = client.finish_value(
+ "POST /referral/claim",
+ client.sdk().referral().claim_referral(&request).await,
+ )?;
Ok(RpcOutcome::single_log(
data,
"referral claim accepted by backend POST /referral/claim",
diff --git a/crates/openhuman-tinyhumans/src/hosted/referral/ops_tests.rs b/crates/openhuman-tinyhumans/src/hosted/referral/ops_tests.rs
index 82eea4725c..afb1bcf03f 100644
--- a/crates/openhuman-tinyhumans/src/hosted/referral/ops_tests.rs
+++ b/crates/openhuman-tinyhumans/src/hosted/referral/ops_tests.rs
@@ -6,6 +6,7 @@ use axum::{
use openhuman_core::security::credentials::{
AuthService, APP_SESSION_PROVIDER, DEFAULT_AUTH_PROFILE_NAME,
};
+use serde_json::json;
use tempfile::TempDir;
fn test_config(tmp: &TempDir) -> Config {
@@ -59,32 +60,36 @@ fn config_with_backend(tmp: &TempDir, base: String) -> Config {
c
}
-// ── require_token (private helper) ────────────────────────────
+// ── credential resolution ────────────────────────────────
#[test]
-fn require_token_errors_without_stored_session() {
+fn hosted_client_errors_without_stored_session() {
let tmp = TempDir::new().unwrap();
let config = test_config(&tmp);
- let err = require_token(&config).unwrap_err();
+ let err = HostedClient::from_config(&config).err().unwrap();
assert!(err.contains("no backend session token"));
}
-#[test]
-fn require_token_trims_stored_value() {
+#[tokio::test]
+async fn get_stats_sends_trimmed_bearer() {
+ let app = Router::new().route(
+ "/referral/stats",
+ get(|headers: axum::http::HeaderMap| async move {
+ Json(json!({
+ "auth": headers
+ .get("authorization")
+ .and_then(|v| v.to_str().ok())
+ .unwrap_or_default()
+ }))
+ }),
+ );
+ let base = spawn_mock(app).await;
let tmp = TempDir::new().unwrap();
- let config = test_config(&tmp);
+ let mut config = test_config(&tmp);
+ config.api_url = Some(base);
store_session_token(&config, " tok ");
- assert_eq!(require_token(&config).unwrap(), "tok");
-}
-
-#[test]
-fn require_token_rejects_whitespace_only_stored_token() {
- let tmp = TempDir::new().unwrap();
- let config = test_config(&tmp);
- store_session_token(&config, " ");
- assert!(require_token(&config)
- .unwrap_err()
- .contains("no backend session token"));
+ let out = get_stats(&config).await.unwrap();
+ assert_eq!(out.value["auth"], json!("Bearer tok"));
}
// ── get_stats ────────────────────────────────────────────────