Skip to content

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

feat(controls): delegate auth operations to the new session module

Steven Enamakelf5729b92d052
diff --git a/Cargo.lock b/Cargo.lock
index d1d522bf93..74d0540f8b 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -4267,6 +4267,7 @@ name = "openhuman-tui"
version = "0.63.28"
dependencies = [
"anyhow",
+ "async-trait",
"base64 0.22.1",
"chrono",
"crossterm",
@@ -4274,6 +4275,7 @@ dependencies = [
"log",
"openhuman",
"openhuman-rpc",
+ "openhuman-session",
"ratatui",
"sentry",
"serde_json",
diff --git a/crates/openhuman-tui/Cargo.toml b/crates/openhuman-tui/Cargo.toml
index 73a853cb9a..5e323bf4bf 100644
--- a/crates/openhuman-tui/Cargo.toml
+++ b/crates/openhuman-tui/Cargo.toml
@@ -18,6 +18,7 @@ path = "src/main.rs"
[dependencies]
openhuman-core.workspace = true
openhuman-rpc.workspace = true
+openhuman-session.workspace = true
anyhow = "1"
base64 = "0.22"
chrono = "0.4"
@@ -27,6 +28,7 @@ log = "0.4"
ratatui = "0.30"
serde_json = "1"
sentry = { version = "0.47.0", default-features = false, optional = true, features = ["backtrace", "contexts", "panic", "tracing", "debug-images", "httpdate"] }
+async-trait = "0.1"
tokio = { version = "1", features = ["rt-multi-thread", "sync", "time"] }
unicode-width = "0.2"
uuid = { version = "1", features = ["v4"] }
diff --git a/crates/openhuman-tui/src/controls.rs b/crates/openhuman-tui/src/controls.rs
index c13a657814..ebdbc48ec2 100644
--- a/crates/openhuman-tui/src/controls.rs
+++ b/crates/openhuman-tui/src/controls.rs
@@ -168,11 +168,21 @@ pub async fn refresh_auth(runtime: &Arc<CoreRuntime>, ui: &mut UiState) {
async fn view_account(runtime: &Arc<CoreRuntime>, ui: &mut UiState) {
ui.settings_status = "Refreshing account…".to_string();
- match runtime.invoke("openhuman.auth_get_me", json!({})).await {
- Ok(value) => {
- let user = rpc_payload(&value);
- ui.account_detail = account_detail(user);
- ui.settings_status = "Account refreshed.".to_string();
+ match crate::session::session_manager(runtime)
+ .current_user(true)
+ .await
+ {
+ Ok(current) => {
+ ui.account_detail = current
+ .user
+ .as_ref()
+ .map(account_detail)
+ .unwrap_or_else(|| "No account details available.".to_string());
+ ui.settings_status = if current.stale {
+ "Account refreshed from the stored copy (backend unreachable).".to_string()
+ } else {
+ "Account refreshed.".to_string()
+ };
refresh_auth(runtime, ui).await;
}
Err(err) => ui.settings_status = format!("Account refresh failed: {err}"),
@@ -187,45 +197,25 @@ async fn login_with_token(runtime: &Arc<CoreRuntime>, ui: &mut UiState) {
return;
}
ui.settings_status = "Signing in…".to_string();
- let consumed = runtime
- .invoke(
- "openhuman.auth_consume_login_token",
- json!({"loginToken": token.trim()}),
- )
+ // Exchange, validate against `/auth/me`, and install in the core — all
+ // owned by `openhuman-session`; the JWT never passes through this module.
+ let result = crate::session::session_manager(runtime)
+ .login_with_token(token.trim())
.await;
token.zeroize();
- let result = match consumed {
- Ok(value) => value,
- Err(err) => {
- ui.settings_status = format!("Login failed: {err}");
- return;
- }
- };
- let mut jwt = string_at(rpc_payload(&result), &["jwtToken"]);
- if jwt.is_empty() {
- ui.settings_status = "Login failed: backend returned no session token.".to_string();
- return;
- }
- let stored = runtime
- .invoke("openhuman.auth_store_session", json!({"token": jwt}))
- .await;
- jwt.zeroize();
- match stored {
+ match result {
Ok(_) => {
ui.settings_status = "Signed in.".to_string();
refresh_auth(runtime, ui).await;
ui.identity_changed = true;
}
- Err(err) => ui.settings_status = format!("Could not store session: {err}"),
+ Err(err) => ui.settings_status = format!("Login failed: {err}"),
}
}
async fn logout(runtime: &Arc<CoreRuntime>, ui: &mut UiState) {
ui.logout_confirm = false;
- match runtime
- .invoke("openhuman.auth_clear_session", json!({}))
- .await
- {
+ match crate::session::session_manager(runtime).logout().await {
Ok(_) => {
ui.settings_status = "Signed out.".to_string();
refresh_auth(runtime, ui).await;
diff --git a/crates/openhuman-tui/src/lib.rs b/crates/openhuman-tui/src/lib.rs
index 916dbf1db5..582b7d4c97 100644
--- a/crates/openhuman-tui/src/lib.rs
+++ b/crates/openhuman-tui/src/lib.rs
@@ -35,6 +35,7 @@ mod controls;
mod crash_reporting;
mod render;
mod runner;
+mod session;
mod state;
mod terminal;
mod ui_state;
diff --git a/crates/openhuman-tui/src/session.rs b/crates/openhuman-tui/src/session.rs
new file mode 100644
index 0000000000..f9297e0341
--- /dev/null
+++ b/crates/openhuman-tui/src/session.rs
@@ -0,0 +1,33 @@
+//! The TUI's session owner: `openhuman-session` over an in-process
+//! [`CoreRuntime`]. Login-token exchange and `/auth/me` happen here, in the
+//! TUI process; the core only receives the resulting credential through
+//! `auth.set_credential`.
+
+use std::sync::{Arc, OnceLock};
+
+use async_trait::async_trait;
+use openhuman_core::core::runtime::CoreRuntime;
+use openhuman_session::{ClientHeaders, CoreLink, SessionManager};
+
+/// `CoreLink` over `CoreRuntime::invoke` — no HTTP, no bearer.
+pub struct InProcessLink(pub Arc<CoreRuntime>);
+
+#[async_trait]
+impl CoreLink for InProcessLink {
+ async fn invoke(&self, method: &str, params: serde_json::Value) -> Result<serde_json::Value, String> {
+ self.0.invoke(method, params).await
+ }
+}
+
+static MANAGER: OnceLock<Arc<SessionManager<InProcessLink>>> = OnceLock::new();
+
+/// The one session manager for this TUI process, bound to `runtime` on first
+/// use. A TUI process hosts exactly one runtime, so later callers get the
+/// same manager regardless of the handle they pass.
+pub fn session_manager(runtime: &Arc<CoreRuntime>) -> Arc<SessionManager<InProcessLink>> {
+ Arc::clone(MANAGER.get_or_init(|| {
+ let headers = ClientHeaders::new(openhuman_core::api::product_identity().as_str())
+ .with_core_version(env!("CARGO_PKG_VERSION"));
+ SessionManager::new(Arc::new(InProcessLink(Arc::clone(runtime))), headers)
+ }))
+}