Skip to content

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

refactor(memory): replace handler match with lookup table

Steven Enamakel64f8d08d650c
diff --git a/crates/openhuman-core/src/memory/schemas.rs b/crates/openhuman-core/src/memory/schemas.rs
index e55ddac282..894da379b9 100644
--- a/crates/openhuman-core/src/memory/schemas.rs
+++ b/crates/openhuman-core/src/memory/schemas.rs
@@ -8,6 +8,51 @@ use crate::core::ControllerSchema;
pub use defs::{schema, FUNCTIONS};
+/// Every memory function paired with the handler it dispatches to.
+const HANDLERS: &[(&str, crate::core::all::ControllerHandler)] = &[
+ ("engines_list", handlers::engines_list),
+ ("engine_get", handlers::engine_get),
+ ("engine_set", handlers::engine_set),
+ ("policy_get", handlers::policy_get),
+ ("policy_set", handlers::policy_set),
+ ("pack_preview", handlers::pack_preview),
+ ("recall", handlers::recall),
+ ("fetch", handlers::fetch),
+ ("learn", handlers::learn),
+ ("forget", handlers::forget),
+ ("erase_all", handlers::erase_all),
+ ("items_list", handlers::items_list),
+ ("explore", handlers::explore),
+ ("items_get", handlers::items_get),
+ ("agents_list", handlers::agents_list),
+ (
+ "conversations_backfill_status",
+ handlers::conversations_backfill_status,
+ ),
+ (
+ "conversations_backfill_start",
+ handlers::conversations_backfill_start,
+ ),
+ ("brain_sources", handlers::brain_sources),
+ ("brain_search", handlers::brain_search),
+ ("brain_ingest", handlers::brain_ingest),
+ ("brain_forget", handlers::brain_forget),
+ ("sources_list", handlers::sources_list),
+ ("sources_add", handlers::sources_add),
+ ("sources_remove", handlers::sources_remove),
+ ("sources_sync", handlers::sources_sync),
+ ("jobs_list", handlers::jobs_list),
+ ("jobs_run", handlers::jobs_run),
+ ("import_scan", handlers::import_scan),
+ ("import_start", handlers::import_start),
+ ("import_status", handlers::import_status),
+ ("import_retry_failed", handlers::import_retry_failed),
+ ("migration_scan", handlers::migration_scan),
+ ("migration_start", handlers::migration_start),
+ ("migration_status", handlers::migration_status),
+ ("migration_retry", handlers::migration_retry),
+];
+
/// Every `memory` controller schema.
#[must_use]
pub fn all_controller_schemas() -> Vec<ControllerSchema> {
@@ -21,49 +66,16 @@ pub fn all_registered_controllers() -> Vec<RegisteredController> {
.iter()
.map(|function| RegisteredController {
schema: schema(function),
- handler: handler_for(function),
+ handler: handler_for(function)
+ .expect("every registered memory function has a handler"),
})
.collect()
}
-fn handler_for(function: &str) -> crate::core::all::ControllerHandler {
- match function {
- "engines_list" => handlers::engines_list,
- "engine_get" => handlers::engine_get,
- "engine_set" => handlers::engine_set,
- "policy_get" => handlers::policy_get,
- "policy_set" => handlers::policy_set,
- "pack_preview" => handlers::pack_preview,
- "recall" => handlers::recall,
- "fetch" => handlers::fetch,
- "learn" => handlers::learn,
- "forget" => handlers::forget,
- "erase_all" => handlers::erase_all,
- "items_list" => handlers::items_list,
- "explore" => handlers::explore,
- "items_get" => handlers::items_get,
- "agents_list" => handlers::agents_list,
- "conversations_backfill_status" => handlers::conversations_backfill_status,
- "conversations_backfill_start" => handlers::conversations_backfill_start,
- "brain_sources" => handlers::brain_sources,
- "brain_search" => handlers::brain_search,
- "brain_ingest" => handlers::brain_ingest,
- "brain_forget" => handlers::brain_forget,
- "sources_list" => handlers::sources_list,
- "sources_add" => handlers::sources_add,
- "sources_remove" => handlers::sources_remove,
- "sources_sync" => handlers::sources_sync,
- "jobs_list" => handlers::jobs_list,
- "jobs_run" => handlers::jobs_run,
- "import_scan" => handlers::import_scan,
- "import_start" => handlers::import_start,
- "import_retry_failed" => handlers::import_retry_failed,
- "migration_scan" => handlers::migration_scan,
- "migration_start" => handlers::migration_start,
- "migration_status" => handlers::migration_status,
- "migration_retry" => handlers::migration_retry,
- _ => handlers::import_status,
- }
+fn handler_for(function: &str) -> Option<crate::core::all::ControllerHandler> {
+ HANDLERS
+ .iter()
+ .find_map(|(name, handler)| (*name == function).then_some(*handler))
}
#[cfg(test)]
diff --git a/crates/openhuman-core/src/memory/schemas_tests.rs b/crates/openhuman-core/src/memory/schemas_tests.rs
index 85b37d8453..f55d5617a2 100644
--- a/crates/openhuman-core/src/memory/schemas_tests.rs
+++ b/crates/openhuman-core/src/memory/schemas_tests.rs
@@ -55,7 +55,7 @@ fn object(value: Value) -> Map<String, Value> {
/// Runs `function`'s handler against `config` (as the embedder-supplied
/// config, so no on-disk config is read).
async fn call(config: &Config, function: &str, params: Value) -> Result<Value, String> {
- let handler = handler_for(function);
+ let handler = handler_for(function).expect("test function has a handler");
let ctx = CoreContext::for_test_with_config(DomainSet::full(), config.clone());
CoreContext::scope(ctx.clone(), async move {
handler(DispatchCtx::new(ctx.clone()), object(params)).await