The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.
Rust agent-orchestrationai-agentsai-assistantdesktopllmlocal-firstmcppersonal-aiprivacyrustsecond-braintauri
feat(core): add envelope type for structured message handling
Steven Enamakel1c48e73b1ddf
diff --git a/crates/openhuman-core/src/core/envelope.rs b/crates/openhuman-core/src/core/envelope.rsnew file mode 100644index 0000000000..2c52c55beb--- /dev/null+++ b/crates/openhuman-core/src/core/envelope.rs@@ -0,0 +1,107 @@+//! The `{data, error, meta}` response envelope a few JSON-RPC surfaces share+//! (`threads`, desktop provider surfaces), and the helpers that build it.++use std::collections::BTreeMap;++use serde::{Deserialize, Serialize};++use crate::core::Outcome;++/// Standard error structure for API responses.+#[derive(Debug, Clone, Serialize, Deserialize)]+pub struct ApiError {+ /// A machine-readable error code.+ pub code: String,+ /// A human-readable error message.+ pub message: String,+ /// Optional additional error details.+ #[serde(skip_serializing_if = "Option::is_none")]+ pub details: Option<serde_json::Value>,+}++/// Pagination metadata for list-based responses.+#[derive(Debug, Clone, Serialize, Deserialize)]+pub struct PaginationMeta {+ /// Maximum number of items requested.+ pub limit: usize,+ /// Number of items skipped.+ pub offset: usize,+ /// Total number of items available in the backend.+ pub count: usize,+}++/// General metadata included in all API envelopes.+#[derive(Debug, Clone, Serialize, Deserialize)]+pub struct ApiMeta {+ /// Unique identifier for the request.+ pub request_id: String,+ /// Time taken to process the request in seconds.+ #[serde(skip_serializing_if = "Option::is_none")]+ pub latency_seconds: Option<f64>,+ /// Whether the response was served from a cache.+ #[serde(skip_serializing_if = "Option::is_none")]+ pub cached: Option<bool>,+ /// Optional counts of various items (e.g., by category).+ #[serde(skip_serializing_if = "Option::is_none")]+ pub counts: Option<BTreeMap<String, usize>>,+ /// Optional pagination information.+ #[serde(skip_serializing_if = "Option::is_none")]+ pub pagination: Option<PaginationMeta>,+}++/// Generic envelope for all API responses.+#[derive(Debug, Clone, Serialize, Deserialize)]+pub struct ApiEnvelope<T> {+ /// The actual payload of the response.+ #[serde(skip_serializing_if = "Option::is_none")]+ pub data: Option<T>,+ /// Error information if the request failed.+ #[serde(skip_serializing_if = "Option::is_none")]+ pub error: Option<ApiError>,+ /// Metadata about the request and response.+ pub meta: ApiMeta,+}++/// An empty request body for methods that don't require parameters.+#[derive(Debug, Clone, Serialize, Deserialize)]+#[serde(deny_unknown_fields)]+pub struct EmptyRequest {}++/// A fresh request id for an envelope's `meta`.+pub(crate) fn request_id() -> String {+ uuid::Uuid::new_v4().to_string()+}++/// Named counts for an envelope's `meta.counts`.+pub(crate) fn counts(entries: impl IntoIterator<Item = (&'static str, usize)>) -> BTreeMap<String, usize> {+ entries+ .into_iter()+ .map(|(key, value)| (key.to_string(), value))+ .collect()+}++/// Wraps `data` in a successful envelope.+pub(crate) fn envelope<T: Serialize>(+ data: T,+ counts: Option<BTreeMap<String, usize>>,+ pagination: Option<PaginationMeta>,+) -> Outcome<ApiEnvelope<T>> {+ Outcome::new(+ ApiEnvelope {+ data: Some(data),+ error: None,+ meta: ApiMeta {+ request_id: request_id(),+ latency_seconds: None,+ cached: None,+ counts,+ pagination,+ },+ },+ vec![],+ )+}++#[cfg(test)]+#[path = "envelope_tests.rs"]+mod tests;diff --git a/crates/openhuman-core/src/core/envelope_tests.rs b/crates/openhuman-core/src/core/envelope_tests.rsnew file mode 100644index 0000000000..7665bb4f7e--- /dev/null+++ b/crates/openhuman-core/src/core/envelope_tests.rs@@ -0,0 +1,35 @@+use super::*;++#[test]+fn counts_collects_named_entries() {+ let counts = counts([("documents", 2), ("entities", 5)]);+ assert_eq!(counts.get("documents"), Some(&2));+ assert_eq!(counts.get("entities"), Some(&5));+}++#[test]+fn envelope_wraps_data_with_meta() {+ let outcome = envelope(serde_json::json!({"ok": true}), None, None);+ let value = outcome.into_cli_compatible_json().unwrap();+ assert_eq!(value["data"]["ok"], true);+ assert!(value["meta"]["request_id"].as_str().is_some());+ assert!(value["error"].is_null());+}++#[test]+fn envelope_propagates_counts_and_pagination() {+ let pagination = PaginationMeta {+ limit: 10,+ offset: 0,+ count: 7,+ };+ let value = envelope(+ serde_json::json!({"v": 42}),+ Some(counts([("num_messages", 7)])),+ Some(pagination),+ )+ .into_cli_compatible_json()+ .unwrap();+ assert_eq!(value["meta"]["counts"]["num_messages"], 7);+ assert_eq!(value["meta"]["pagination"]["count"], 7);+}diff --git a/crates/openhuman-core/src/core/mod.rs b/crates/openhuman-core/src/core/mod.rsindex 431e0daaae..0145d3cf02 100644--- a/crates/openhuman-core/src/core/mod.rs+++ b/crates/openhuman-core/src/core/mod.rs@@ -14,6 +14,7 @@ pub mod bus_testing;pub mod cli;pub mod cli_capability;pub mod dispatch;+pub mod envelope;pub mod event_bind_tokens;pub mod events;// Ungated compile-time marker for the `http-server` gate (#5048) — the desktopdiff --git a/crates/openhuman-core/src/threads/rpc_models.rs b/crates/openhuman-core/src/threads/rpc_models.rsindex 5d3ed37084..564077ac30 100644--- a/crates/openhuman-core/src/threads/rpc_models.rs+++ b/crates/openhuman-core/src/threads/rpc_models.rs@@ -6,67 +6,8 @@//! messages, which are thread persistence (`threads::store`), not memory.use serde::{Deserialize, Serialize};-use std::collections::BTreeMap;-/// Standard error structure for API responses.-#[derive(Debug, Clone, Serialize, Deserialize)]-pub struct ApiError {- /// A machine-readable error code.- pub code: String,- /// A human-readable error message.- pub message: String,- /// Optional additional error details.- #[serde(skip_serializing_if = "Option::is_none")]- pub details: Option<serde_json::Value>,-}--/// Pagination metadata for list-based responses.-#[derive(Debug, Clone, Serialize, Deserialize)]-pub struct PaginationMeta {- /// Maximum number of items requested.- pub limit: usize,- /// Number of items skipped.- pub offset: usize,- /// Total number of items available in the backend.- pub count: usize,-}--/// General metadata included in all API envelopes.-#[derive(Debug, Clone, Serialize, Deserialize)]-pub struct ApiMeta {- /// Unique identifier for the request.- pub request_id: String,- /// Time taken to process the request in seconds.- #[serde(skip_serializing_if = "Option::is_none")]- pub latency_seconds: Option<f64>,- /// Whether the response was served from a cache.- #[serde(skip_serializing_if = "Option::is_none")]- pub cached: Option<bool>,- /// Optional counts of various items (e.g., by category).- #[serde(skip_serializing_if = "Option::is_none")]- pub counts: Option<BTreeMap<String, usize>>,- /// Optional pagination information.- #[serde(skip_serializing_if = "Option::is_none")]- pub pagination: Option<PaginationMeta>,-}--/// Generic envelope for all API responses.-#[derive(Debug, Clone, Serialize, Deserialize)]-pub struct ApiEnvelope<T> {- /// The actual payload of the response.- #[serde(skip_serializing_if = "Option::is_none")]- pub data: Option<T>,- /// Error information if the request failed.- #[serde(skip_serializing_if = "Option::is_none")]- pub error: Option<ApiError>,- /// Metadata about the request and response.- pub meta: ApiMeta,-}--/// An empty request body for methods that don't require parameters.-#[derive(Debug, Clone, Serialize, Deserialize)]-#[serde(deny_unknown_fields)]-pub struct EmptyRequest {}+pub use crate::core::envelope::{ApiEnvelope, ApiError, ApiMeta, EmptyRequest, PaginationMeta};/// Request to create a new conversation thread.#[derive(Debug, Clone, Serialize, Deserialize)]