Skip to content

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

refactor(session): replace inline JSON-RPC helpers with shared library

Steven Enamakel4613cfc5964d
diff --git a/crates/openhuman-app/Cargo.lock b/crates/openhuman-app/Cargo.lock
index e843294c9b..a9155767e4 100644
--- a/crates/openhuman-app/Cargo.lock
+++ b/crates/openhuman-app/Cargo.lock
@@ -4325,6 +4325,7 @@ dependencies = [
"tinymemory-bus",
"tinymemory-sources",
"tinyruntime-bus",
+ "tinysearch-bus",
"tinyskills",
"tinytools 0.4.1",
"tinytools-agent 0.4.1",
@@ -7691,6 +7692,14 @@ dependencies = [
"serde_json",
]
+[[package]]
+name = "tinysearch-bus"
+version = "0.2.1"
+dependencies = [
+ "serde",
+ "serde_json",
+]
+
[[package]]
name = "tinyskills"
version = "0.2.2"
diff --git a/crates/openhuman-app/src/local_data_reset.rs b/crates/openhuman-app/src/local_data_reset.rs
index b11b12dc2a..f363f02045 100644
--- a/crates/openhuman-app/src/local_data_reset.rs
+++ b/crates/openhuman-app/src/local_data_reset.rs
@@ -304,12 +304,11 @@ async fn fetch_data_paths(user_id: Option<String>) -> Result<ResolvedDataPaths,
if let Some(id) = user_id {
params.insert("user_id".to_string(), serde_json::Value::String(id));
}
- let body = serde_json::json!({
- "jsonrpc": "2.0",
- "id": 1,
- "method": "openhuman.config_get_data_paths",
- "params": serde_json::Value::Object(params),
- });
+ let body = openhuman_rpc::request_body(
+ 1,
+ "openhuman.config_get_data_paths",
+ serde_json::Value::Object(params),
+ );
let client = reqwest::Client::builder()
.timeout(std::time::Duration::from_secs(10))
.build()
diff --git a/crates/openhuman-app/src/session/link.rs b/crates/openhuman-app/src/session/link.rs
index f6819d4b68..af98df65d8 100644
--- a/crates/openhuman-app/src/session/link.rs
+++ b/crates/openhuman-app/src/session/link.rs
@@ -6,7 +6,7 @@
use async_trait::async_trait;
use openhuman_tinyhumans::CoreLink;
-use serde_json::{json, Value};
+use serde_json::Value;
use crate::core_process::CoreProcessHandle;
@@ -20,46 +20,17 @@ impl HttpCoreLink {
}
}
-/// Decode a JSON-RPC 2.0 response body into its `result`, or the error
-/// message.
-pub(crate) fn decode_rpc_response(status: u16, body: &str) -> Result<Value, String> {
- let parsed: Value = serde_json::from_str(body)
- .map_err(|e| format!("core rpc returned a non-JSON body (http {status}): {e}"))?;
- if let Some(error) = parsed.get("error").filter(|e| !e.is_null()) {
- let message = error
- .get("message")
- .and_then(Value::as_str)
- .map(str::to_string)
- .unwrap_or_else(|| error.to_string());
- return Err(message);
- }
- if !(200..300).contains(&status) {
- return Err(format!("core rpc failed with http {status}"));
- }
- Ok(parsed.get("result").cloned().unwrap_or(Value::Null))
-}
-
#[async_trait]
impl CoreLink for HttpCoreLink {
async fn invoke(&self, method: &str, params: Value) -> Result<Value, String> {
let (url, token) = crate::active_rpc_endpoint(&self.desktop).await;
- let body = json!({
- "jsonrpc": "2.0",
- "id": 1,
- "method": method,
- "params": params,
- })
- .to_string();
+ let body = openhuman_rpc::request_body(1, method, params).to_string();
log::debug!(
"[session][link] {method} -> {}",
openhuman_rpc::redact_url_for_log(&url)
);
let token = (!token.is_empty()).then_some(token);
let response = crate::core_rpc::post_json_rpc(&url, token.as_deref(), body).await?;
- decode_rpc_response(response.status, &response.body)
+ openhuman_rpc::decode_response(response.status, &response.body)
}
}
-
-#[cfg(test)]
-#[path = "link_tests.rs"]
-mod tests;
diff --git a/crates/openhuman-app/src/session/link_tests.rs b/crates/openhuman-app/src/session/link_tests.rs
deleted file mode 100644
index 3933a3c59f..0000000000
--- a/crates/openhuman-app/src/session/link_tests.rs
+++ /dev/null
@@ -1,25 +0,0 @@
-use super::decode_rpc_response;
-use serde_json::json;
-
-#[test]
-fn decodes_result_and_error_envelopes() {
- let ok = decode_rpc_response(
- 200,
- r#"{"jsonrpc":"2.0","id":1,"result":{"isAuthenticated":true}}"#,
- )
- .unwrap();
- assert_eq!(ok, json!({ "isAuthenticated": true }));
-
- let err = decode_rpc_response(
- 200,
- r#"{"jsonrpc":"2.0","id":1,"error":{"code":-32000,"message":"token is required"}}"#,
- )
- .unwrap_err();
- assert_eq!(err, "token is required");
-
- let http = decode_rpc_response(401, r#"{"jsonrpc":"2.0","id":1}"#).unwrap_err();
- assert!(http.contains("401"), "{http}");
-
- let garbage = decode_rpc_response(200, "<html>").unwrap_err();
- assert!(garbage.contains("non-JSON"), "{garbage}");
-}