Skip to content

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

refactor(memory): extract shared rpc helper for schema handlers

Steven Enamakel8a4d8a632214
diff --git a/crates/openhuman-core/src/memory/schemas/handlers.rs b/crates/openhuman-core/src/memory/schemas/handlers.rs
index aa3e025270..dd20059147 100644
--- a/crates/openhuman-core/src/memory/schemas/handlers.rs
+++ b/crates/openhuman-core/src/memory/schemas/handlers.rs
@@ -1,13 +1,11 @@
//! Thin JSON-RPC handlers: parse params, load config, delegate to the ops,
//! and persist config for the setters.
-use crate::core::runtime::DispatchCtx;
-use serde::de::DeserializeOwned;
-use serde::Serialize;
-use serde_json::{json, Map, Value};
+use std::future::Future;
use crate::config::Config;
use crate::core::all::ControllerFuture;
+use crate::core::runtime::DispatchCtx;
use crate::core::Outcome;
use crate::memory::error::{MemoryError, MemoryResult};
use crate::memory::explore::{ExploreParams, ItemsGetParams};
@@ -20,6 +18,45 @@ use crate::memory::types::{
SourcesSyncView,
};
use crate::memory::{backfill, brain, confine, engine, import, layout_migration, ops, sources};
+use serde::de::DeserializeOwned;
+use serde::Serialize;
+use serde_json::{json, Map, Value};
+
+/// The operation run after the common request parsing and config load.
+trait RpcOperation<P, R>: Send + 'static {
+ type Future: Future<Output = MemoryResult<R>> + Send + 'static;
+
+ fn call(self, ctx: DispatchCtx, config: Config, params: P) -> Self::Future;
+}
+
+impl<P, R, F, Fut> RpcOperation<P, R> for F
+where
+ F: FnOnce(DispatchCtx, Config, P) -> Fut + Send + 'static,
+ Fut: Future<Output = MemoryResult<R>> + Send + 'static,
+{
+ type Future = Fut;
+
+ fn call(self, ctx: DispatchCtx, config: Config, params: P) -> Self::Future {
+ self(ctx, config, params)
+ }
+}
+
+/// Runs the memory RPC's shared parse → load → call → finish sequence.
+fn rpc<P, R>(
+ ctx: DispatchCtx,
+ params: Map<String, Value>,
+ operation: impl RpcOperation<P, R>,
+) -> ControllerFuture
+where
+ P: DeserializeOwned + Send + 'static,
+ R: Serialize + Send + 'static,
+{
+ Box::pin(async move {
+ let params = parse::<P>(params)?;
+ let config = load(&ctx).await?;
+ finish(operation.call(ctx, config, params).await)
+ })
+}
fn parse<T: DeserializeOwned>(params: Map<String, Value>) -> Result<T, String> {
serde_json::from_value(Value::Object(params))
@@ -46,118 +83,97 @@ async fn save(config: &Config) -> MemoryResult<()> {
}
pub(super) fn engines_list(ctx: DispatchCtx, params: Map<String, Value>) -> ControllerFuture {
- Box::pin(async move {
- parse::<EmptyParams>(params)?;
- to_json(ops::engines_list(&load(&ctx).await?))
+ rpc::<EmptyParams, _>(ctx, params, |_, config, _| async move {
+ Ok(ops::engines_list(&config))
})
}
pub(super) fn engine_get(ctx: DispatchCtx, params: Map<String, Value>) -> ControllerFuture {
- Box::pin(async move {
- parse::<EmptyParams>(params)?;
- to_json(ops::engine_get(&load(&ctx).await?).await)
+ rpc::<EmptyParams, _>(ctx, params, |_, config, _| async move {
+ Ok(ops::engine_get(&config).await)
})
}
pub(super) fn engine_set(ctx: DispatchCtx, params: Map<String, Value>) -> ControllerFuture {
- Box::pin(async move {
- let params = parse::<EngineSetParams>(params)?;
- let mut config = load(&ctx).await?;
- let result = async {
- ops::apply_engine_set(&mut config, &params)?;
- save(&config).await
- }
- .await;
- result.map_err(String::from)?;
+ rpc::<EngineSetParams, _>(ctx, params, |_, mut config, params| async move {
+ ops::apply_engine_set(&mut config, &params)?;
+ save(&config).await?;
engine::invalidate();
- to_json(ops::engine_get(&config).await)
+ Ok(ops::engine_get(&config).await)
})
}
pub(super) fn recall(ctx: DispatchCtx, params: Map<String, Value>) -> ControllerFuture {
- Box::pin(async move {
- let params = parse::<RecallParams>(params)?;
- finish(confine::recall(&load(&ctx).await?, &MemoryIdentity::root(), params).await)
+ rpc::<RecallParams, _>(ctx, params, |_, config, params| async move {
+ confine::recall(&config, &MemoryIdentity::root(), params).await
})
}
pub(super) fn fetch(ctx: DispatchCtx, params: Map<String, Value>) -> ControllerFuture {
- Box::pin(async move {
- let params = parse::<FetchParams>(params)?;
- finish(confine::fetch(&load(&ctx).await?, &MemoryIdentity::root(), params).await)
+ rpc::<FetchParams, _>(ctx, params, |_, config, params| async move {
+ confine::fetch(&config, &MemoryIdentity::root(), params).await
})
}
pub(super) fn learn(ctx: DispatchCtx, params: Map<String, Value>) -> ControllerFuture {
- Box::pin(async move {
- let params = parse::<LearnParams>(params)?;
- finish(confine::learn(&load(&ctx).await?, &MemoryIdentity::root(), params).await)
+ rpc::<LearnParams, _>(ctx, params, |_, config, params| async move {
+ confine::learn(&config, &MemoryIdentity::root(), params).await
})
}
pub(super) fn forget(ctx: DispatchCtx, params: Map<String, Value>) -> ControllerFuture {
- Box::pin(async move {
- let params = parse::<ForgetParams>(params)?;
- finish(confine::forget(&load(&ctx).await?, &MemoryIdentity::root(), params).await)
+ rpc::<ForgetParams, _>(ctx, params, |_, config, params| async move {
+ confine::forget(&config, &MemoryIdentity::root(), params).await
})
}
pub(super) fn erase_all(ctx: DispatchCtx, params: Map<String, Value>) -> ControllerFuture {
- Box::pin(async move {
- let params = parse::<EraseAllParams>(params)?;
- finish(ops::erase_all(&ctx, &load(&ctx).await?, params).await)
+ rpc::<EraseAllParams, _>(ctx, params, |ctx, config, params| async move {
+ ops::erase_all(&ctx, &config, params).await
})
}
pub(super) fn items_list(ctx: DispatchCtx, params: Map<String, Value>) -> ControllerFuture {
- Box::pin(async move {
- let params = parse::<ItemsListParams>(params)?;
- finish(confine::items_list(&load(&ctx).await?, &MemoryIdentity::root(), params).await)
+ rpc::<ItemsListParams, _>(ctx, params, |_, config, params| async move {
+ confine::items_list(&config, &MemoryIdentity::root(), params).await
})
}
pub(super) fn explore(ctx: DispatchCtx, params: Map<String, Value>) -> ControllerFuture {
- Box::pin(async move {
- let params = parse::<ExploreParams>(params)?;
- finish(confine::explore(&load(&ctx).await?, &MemoryIdentity::root(), params).await)
+ rpc::<ExploreParams, _>(ctx, params, |_, config, params| async move {
+ confine::explore(&config, &MemoryIdentity::root(), params).await
})
}
pub(super) fn items_get(ctx: DispatchCtx, params: Map<String, Value>) -> ControllerFuture {
- Box::pin(async move {
- let params = parse::<ItemsGetParams>(params)?;
- finish(confine::items_get(&load(&ctx).await?, &MemoryIdentity::root(), params).await)
+ rpc::<ItemsGetParams, _>(ctx, params, |_, config, params| async move {
+ confine::items_get(&config, &MemoryIdentity::root(), params).await
})
}
pub(super) fn policy_get(ctx: DispatchCtx, params: Map<String, Value>) -> ControllerFuture {
- Box::pin(async move {
- parse::<EmptyParams>(params)?;
- to_json(views::policy_view(&load(&ctx).await?))
+ rpc::<EmptyParams, _>(ctx, params, |_, config, _| async move {
+ Ok(views::policy_view(&config))
})
}
pub(super) fn policy_set(ctx: DispatchCtx, params: Map<String, Value>) -> ControllerFuture {
- Box::pin(async move {
- let params = parse::<PolicySetParams>(params)?;
- let mut config = load(&ctx).await?;
+ rpc::<PolicySetParams, _>(ctx, params, |_, mut config, params| async move {
views::apply_policy_set(&mut config, &params).map_err(String::from)?;
- save(&config).await.map_err(String::from)?;
- to_json(views::policy_view(&config))
+ save(&config).await?;
+ Ok(views::policy_view(&config))
})
}
pub(super) fn pack_preview(ctx: DispatchCtx, params: Map<String, Value>) -> ControllerFuture {
- Box::pin(async move {
- let params = parse::<PackPreviewParams>(params)?;
- finish(views::pack_preview(&load(&ctx).await?, &MemoryIdentity::root(), params).await)
+ rpc::<PackPreviewParams, _>(ctx, params, |_, config, params| async move {
+ views::pack_preview(&config, &MemoryIdentity::root(), params).await
})
}
pub(super) fn agents_list(ctx: DispatchCtx, params: Map<String, Value>) -> ControllerFuture {
- Box::pin(async move {
- parse::<EmptyParams>(params)?;
- finish(views::agents_list(&load(&ctx).await?, &MemoryIdentity::root()).await)
+ rpc::<EmptyParams, _>(ctx, params, |_, config, _| async move {
+ views::agents_list(&config, &MemoryIdentity::root()).await
})
}
@@ -165,9 +181,8 @@ pub(super) fn conversations_backfill_status(
ctx: DispatchCtx,
params: Map<String, Value>,
) -> ControllerFuture {
- Box::pin(async move {
- parse::<EmptyParams>(params)?;
- finish(backfill::status(&ctx, &load(&ctx).await?).await)
+ rpc::<EmptyParams, _>(ctx, params, |ctx, config, _| async move {
+ backfill::status(&ctx, &config).await
})
}
@@ -175,127 +190,103 @@ pub(super) fn conversations_backfill_start(
ctx: DispatchCtx,
params: Map<String, Value>,
) -> ControllerFuture {
- Box::pin(async move {
- let params = parse::<backfill::BackfillStartParams>(params)?;
- finish(backfill::start(&ctx, &load(&ctx).await?, params).await)
+ rpc::<backfill::BackfillStartParams, _>(ctx, params, |ctx, config, params| async move {
+ backfill::start(&ctx, &config, params).await
})
}
pub(super) fn brain_sources(ctx: DispatchCtx, params: Map<String, Value>) -> ControllerFuture {
- Box::pin(async move {
- parse::<EmptyParams>(params)?;
- finish(brain::sources(&load(&ctx).await?, &MemoryIdentity::root()).await)
+ rpc::<EmptyParams, _>(ctx, params, |_, config, _| async move {
+ brain::sources(&config, &MemoryIdentity::root()).await
})
}
pub(super) fn brain_search(ctx: DispatchCtx, params: Map<String, Value>) -> ControllerFuture {
- Box::pin(async move {
- let params = parse::<brain::BrainSearchParams>(params)?;
- finish(brain::search(&load(&ctx).await?, &MemoryIdentity::root(), params).await)
+ rpc::<brain::BrainSearchParams, _>(ctx, params, |_, config, params| async move {
+ brain::search(&config, &MemoryIdentity::root(), params).await
})
}
pub(super) fn brain_ingest(ctx: DispatchCtx, params: Map<String, Value>) -> ControllerFuture {
- Box::pin(async move {
- let params = parse::<brain::BrainIngestParams>(params)?;
- finish(brain::ingest(&load(&ctx).await?, &MemoryIdentity::root(), params).await)
+ rpc::<brain::BrainIngestParams, _>(ctx, params, |_, config, params| async move {
+ brain::ingest(&config, &MemoryIdentity::root(), params).await
})
}
pub(super) fn brain_forget(ctx: DispatchCtx, params: Map<String, Value>) -> ControllerFuture {
- Box::pin(async move {
- let params = parse::<brain::BrainForgetParams>(params)?;
- finish(brain::forget(&load(&ctx).await?, &MemoryIdentity::root(), params).await)
+ rpc::<brain::BrainForgetParams, _>(ctx, params, |_, config, params| async move {
+ brain::forget(&config, &MemoryIdentity::root(), params).await
})
}
pub(super) fn jobs_list(ctx: DispatchCtx, params: Map<String, Value>) -> ControllerFuture {
- Box::pin(async move {
- parse::<EmptyParams>(params)?;
- to_json(views::jobs_list(&load(&ctx).await?).await)
+ rpc::<EmptyParams, _>(ctx, params, |_, config, _| async move {
+ Ok(views::jobs_list(&config).await)
})
}
pub(super) fn jobs_run(ctx: DispatchCtx, params: Map<String, Value>) -> ControllerFuture {
- Box::pin(async move {
- let params = parse::<JobsRunParams>(params)?;
- finish(views::jobs_run(&load(&ctx).await?, params).await)
+ rpc::<JobsRunParams, _>(ctx, params, |_, config, params| async move {
+ views::jobs_run(&config, params).await
})
}
pub(super) fn sources_list(ctx: DispatchCtx, params: Map<String, Value>) -> ControllerFuture {
- Box::pin(async move {
- parse::<EmptyParams>(params)?;
- to_json(SourcesListView {
- sources: sources::list(&load(&ctx).await?),
+ rpc::<EmptyParams, _>(ctx, params, |_, config, _| async move {
+ Ok(SourcesListView {
+ sources: sources::list(&config),
})
})
}
pub(super) fn sources_add(ctx: DispatchCtx, params: Map<String, Value>) -> ControllerFuture {
- Box::pin(async move {
- let params = parse::<SourcesAddParams>(params)?;
- let mut config = load(&ctx).await?;
- let source = sources::apply_add(&mut config, &params).map_err(String::from)?;
- save(&config).await.map_err(String::from)?;
- to_json(SourceAddedView {
+ rpc::<SourcesAddParams, _>(ctx, params, |_, mut config, params| async move {
+ let source = sources::apply_add(&mut config, &params)?;
+ save(&config).await?;
+ Ok(SourceAddedView {
source: sources::view(&source, None),
})
})
}
pub(super) fn sources_remove(ctx: DispatchCtx, params: Map<String, Value>) -> ControllerFuture {
- Box::pin(async move {
- let params = parse::<SourcesRemoveParams>(params)?;
- let mut config = load(&ctx).await?;
+ rpc::<SourcesRemoveParams, _>(ctx, params, |_, mut config, params| async move {
let Some(removed) = sources::apply_remove(&mut config, &params.id) else {
- return to_json(SourceRemovedView { removed: false });
+ return Ok(SourceRemovedView { removed: false });
};
- save(&config).await.map_err(String::from)?;
+ save(&config).await?;
sources::state::remove(&config.workspace_dir, &removed.id);
if params.forget_items.unwrap_or(false) {
- sources::forget_items(&config, &removed.id)
- .await
- .map_err(String::from)?;
+ sources::forget_items(&config, &removed.id).await?;
}
- to_json(SourceRemovedView { removed: true })
+ Ok(SourceRemovedView { removed: true })
})
}
pub(super) fn sources_sync(ctx: DispatchCtx, params: Map<String, Value>) -> ControllerFuture {
- Box::pin(async move {
- let params = parse::<SourcesSyncParams>(params)?;
- let config = load(&ctx).await?;
- let started =
- sources::start_sync(&ctx, &config, params.id.as_deref()).map_err(String::from)?;
- to_json(SourcesSyncView { started })
+ rpc::<SourcesSyncParams, _>(ctx, params, |ctx, config, params| async move {
+ let started = sources::start_sync(&ctx, &config, params.id.as_deref())?;
+ Ok(SourcesSyncView { started })
})
}
pub(super) fn import_scan(ctx: DispatchCtx, params: Map<String, Value>) -> ControllerFuture {
- Box::pin(async move {
- parse::<EmptyParams>(params)?;
- finish(import::scan(&load(&ctx).await?).await)
+ rpc::<EmptyParams, _>(ctx, params, |_, config, _| async move {
+ import::scan(&config).await
})
}
pub(super) fn import_start(ctx: DispatchCtx, params: Map<String, Value>) -> ControllerFuture {
- Box::pin(async move {
- let params = parse::<ImportStartParams>(params)?;
- let config = load(&ctx).await?;
- finish(
- import::start(&ctx, &config, params.consent)
- .await
- .map(|state| ImportStateView { state }),
- )
+ rpc::<ImportStartParams, _>(ctx, params, |ctx, config, params| async move {
+ import::start(&ctx, &config, params.consent)
+ .await
+ .map(|state| ImportStateView { state })
})
}
pub(super) fn import_status(ctx: DispatchCtx, params: Map<String, Value>) -> ControllerFuture {
- Box::pin(async move {
- parse::<EmptyParams>(params)?;
- let config = load(&ctx).await?;
- to_json(json!(ImportStateView {
+ rpc::<EmptyParams, _>(ctx, params, |_, config, _| async move {
+ Ok(json!(ImportStateView {
state: import::status(&config)
}))
})
@@ -305,28 +296,21 @@ pub(super) fn import_retry_failed(
ctx: DispatchCtx,
params: Map<String, Value>,
) -> ControllerFuture {
- Box::pin(async move {
- parse::<EmptyParams>(params)?;
- let config = load(&ctx).await?;
- finish(
- import::retry_failed(&ctx, &config)
- .await
- .map(|state| ImportStateView { state }),
- )
+ rpc::<EmptyParams, _>(ctx, params, |ctx, config, _| async move {
+ import::retry_failed(&ctx, &config)
+ .await
+ .map(|state| ImportStateView { state })
})
}
pub(super) fn migration_scan(ctx: DispatchCtx, params: Map<String, Value>) -> ControllerFuture {
- Box::pin(async move {
- parse::<EmptyParams>(params)?;
- finish(layout_migration::scan(&layout_migration::AppHost::new(load(&ctx).await?)).await)
+ rpc::<EmptyParams, _>(ctx, params, |_, config, _| async move {
+ layout_migration::scan(&layout_migration::AppHost::new(config)).await
})
}
pub(super) fn migration_start(ctx: DispatchCtx, params: Map<String, Value>) -> ControllerFuture {
- Box::pin(async move {
- let params = parse::<layout_migration::StartParams>(params)?;
- let config = load(&ctx).await?;
+ rpc::<layout_migration::StartParams, _>(ctx, params, |ctx, config, params| async move {
let started = layout_migration::start(
&ctx,
config.clone(),
@@ -337,20 +321,18 @@ pub(super) fn migration_start(ctx: DispatchCtx, params: Map<String, Value>) -> C
std::sync::Arc::new(import::scheduler_paused),
);
tracing::debug!(started, "[memory:layout_migration] start requested");
- finish(layout_migration::status(&config))
+ layout_migration::status(&config)
})
}
pub(super) fn migration_status(ctx: DispatchCtx, params: Map<String, Value>) -> ControllerFuture {
- Box::pin(async move {
- parse::<EmptyParams>(params)?;
- finish(layout_migration::status(&load(&ctx).await?))
+ rpc::<EmptyParams, _>(ctx, params, |_, config, _| async move {
+ layout_migration::status(&config)
})
}
pub(super) fn migration_retry(ctx: DispatchCtx, params: Map<String, Value>) -> ControllerFuture {
- Box::pin(async move {
- parse::<EmptyParams>(params)?;
- finish(layout_migration::retry(&load(&ctx).await?))
+ rpc::<EmptyParams, _>(ctx, params, |_, config, _| async move {
+ layout_migration::retry(&config)
})
}