Skip to content

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

feat(memory): add namespace support for memory sources

Steven Enamakel0c76e1b8356a
diff --git a/crates/openhuman-core/src/integrations/composio/ops/sync.rs b/crates/openhuman-core/src/integrations/composio/ops/sync.rs
index 43de4bc94d..27d8e50cc1 100644
--- a/crates/openhuman-core/src/integrations/composio/ops/sync.rs
+++ b/crates/openhuman-core/src/integrations/composio/ops/sync.rs
@@ -244,6 +244,7 @@ pub async fn run_sync_pass(
toolkit,
connection_id,
source_id,
+ &crate::memory::sources::namespace_of(config, source_id),
&response.batch.records,
)
.await
diff --git a/crates/openhuman-core/src/memory/sources/composio.rs b/crates/openhuman-core/src/memory/sources/composio.rs
index aaccdd2336..7b15563957 100644
--- a/crates/openhuman-core/src/memory/sources/composio.rs
+++ b/crates/openhuman-core/src/memory/sources/composio.rs
@@ -67,19 +67,21 @@ pub fn record_item(
})
}
-/// Stores every non-empty record. Returns how many were stored.
+/// Stores every non-empty record at `namespace`. Returns how many were
+/// stored.
pub async fn store_records(
bound: &BoundEngine,
toolkit: &str,
connection_id: &str,
source_id: &str,
+ namespace: &tinymemory::Namespace,
records: &[ConnectorRecord],
) -> MemoryResult<u64> {
let items: Vec<StoreItem> = records
.iter()
.filter_map(|record| record_item(toolkit, connection_id, source_id, record))
.collect();
- super::sync::store_all(bound, items, source_id).await
+ super::sync::store_all(bound, items, source_id, namespace).await
}
/// Syncs every active connection of the source's toolkit.
diff --git a/crates/openhuman-core/src/memory/sources/mod.rs b/crates/openhuman-core/src/memory/sources/mod.rs
index e810b24e58..2b93553cef 100644
--- a/crates/openhuman-core/src/memory/sources/mod.rs
+++ b/crates/openhuman-core/src/memory/sources/mod.rs
@@ -135,13 +135,21 @@ pub fn apply_add(
.map(str::trim)
.filter(|label| !label.is_empty())
.map_or_else(|| target.clone(), str::to_string);
+ let namespace = match params.namespace.as_deref().map(str::trim) {
+ None | Some("") => None,
+ Some(raw) => Some(
+ raw.parse::<tinymemory::Namespace>()
+ .map_err(|error| MemoryError::invalid(error.to_string()))?
+ .to_string(),
+ ),
+ };
let source = MemorySourceConfig {
id: format!("src-{}", uuid::Uuid::new_v4().simple()),
kind,
target,
label,
schedule_mins: params.schedule_mins,
- namespace: None,
+ namespace,
};
sync::reader_entry(&source)?;
config.memory.sources.push(source.clone());
@@ -195,6 +203,32 @@ pub fn is_due(
}
}
+/// The memory node `source` stores at: its configured `namespace`, else the
+/// root. An invalid one (hand-edited config) is logged and stored at the
+/// root.
+#[must_use]
+pub fn namespace_of_source(source: &MemorySourceConfig) -> tinymemory::Namespace {
+ let Some(raw) = source.namespace.as_deref() else {
+ return tinymemory::Namespace::ROOT;
+ };
+ raw.parse().unwrap_or_else(|error| {
+ tracing::warn!(id = %source.id, %error, "[memory:sources] invalid namespace; storing at the root");
+ tinymemory::Namespace::ROOT
+ })
+}
+
+/// The memory node the configured source `source_id` stores at; the root
+/// for an unknown id.
+#[must_use]
+pub fn namespace_of(config: &Config, source_id: &str) -> tinymemory::Namespace {
+ config
+ .memory
+ .sources
+ .iter()
+ .find(|source| source.id == source_id)
+ .map_or(tinymemory::Namespace::ROOT, namespace_of_source)
+}
+
#[cfg(test)]
#[path = "mod_tests.rs"]
mod tests;
diff --git a/crates/openhuman-core/src/memory/sources/sync.rs b/crates/openhuman-core/src/memory/sources/sync.rs
index f4198bbf9c..ab1caa8462 100644
--- a/crates/openhuman-core/src/memory/sources/sync.rs
+++ b/crates/openhuman-core/src/memory/sources/sync.rs
@@ -76,17 +76,26 @@ pub async fn sync_one(config: &Config, source: &MemorySourceConfig) -> MemoryRes
"[memory:sources] some items could not be read"
);
}
- store_all(&bound, collected.items, &source.id).await
+ store_all(
+ &bound,
+ collected.items,
+ &source.id,
+ &super::namespace_of_source(source),
+ )
+ .await
}
+/// Stores `items` at `namespace`, the source's memory node.
pub(super) async fn store_all(
bound: &BoundEngine,
items: Vec<tinymemory::StoreItem>,
source_id: &str,
+ namespace: &tinymemory::Namespace,
) -> MemoryResult<u64> {
let mut stored = 0u64;
let mut last_error = None;
- for item in items {
+ for mut item in items {
+ item.meta_mut().namespace = namespace.clone();
match store_on(bound, item).await {
Ok(_) => stored += 1,
Err(error @ (MemoryError::Unauthorized(_) | MemoryError::Off(_))) => return Err(error),
diff --git a/crates/openhuman-core/src/memory/types.rs b/crates/openhuman-core/src/memory/types.rs
index f0eb1f16e4..eded39e105 100644
--- a/crates/openhuman-core/src/memory/types.rs
+++ b/crates/openhuman-core/src/memory/types.rs
@@ -303,6 +303,10 @@ pub struct SourcesAddParams {
/// Minutes between scheduled syncs.
#[serde(default)]
pub schedule_mins: Option<u32>,
+ /// The memory node to store the source's documents at
+ /// (`agent:researcher`); unset stores them at the root.
+ #[serde(default)]
+ pub namespace: Option<String>,
}
/// `memory_sources_add` result.