Skip to content

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

feat(memory): add lifecycle views for brain memory

Steven Enamakelf1fd15e802b8
diff --git a/crates/openhuman-core/src/memory/brain.rs b/crates/openhuman-core/src/memory/brain.rs
index f05d80a8ca..7464dd1803 100644
--- a/crates/openhuman-core/src/memory/brain.rs
+++ b/crates/openhuman-core/src/memory/brain.rs
@@ -22,7 +22,7 @@ use crate::config::Config;
use super::engine;
use super::error::{MemoryError, MemoryResult};
use super::lifecycle::jobs;
-use super::scope;
+use super::scope::MemoryIdentity;
/// Largest file `memory_brain_ingest` reads, in bytes.
pub const MAX_INGEST_BYTES: u64 = 25 * 1024 * 1024;
@@ -56,14 +56,14 @@ pub fn brain_node(
)?)
}
-/// The layout the brain RPCs act on: the in-scope identity's.
-fn layout(config: &Config) -> MemoryLayout {
- scope::resolve_current(config).layout
+/// The layout the brain RPCs act on: `identity`'s.
+fn layout(config: &Config, identity: &MemoryIdentity) -> MemoryLayout {
+ identity.resolve(config).layout
}
-fn brain(config: &Config) -> MemoryResult<Brain> {
+fn brain(config: &Config, identity: &MemoryIdentity) -> MemoryResult<Brain> {
let bound = engine::resolve(config).engine()?;
- Ok(Brain::new(bound.engine, layout(config)))
+ Ok(Brain::new(bound.engine, layout(config, identity)))
}
fn parse_source(raw: &str) -> MemoryResult<BrainSource> {
@@ -92,9 +92,12 @@ pub struct BrainSourcesView {
}
/// `memory_brain_sources`: the brain's sources and their sizes.
-pub async fn sources(config: &Config) -> MemoryResult<BrainSourcesView> {
+pub async fn sources(
+ config: &Config,
+ identity: &MemoryIdentity,
+) -> MemoryResult<BrainSourcesView> {
let bound = engine::resolve(config).engine()?;
- let layout = layout(config);
+ let layout = layout(config, identity);
let page = bound
.engine
.explore(ExploreRequest {
@@ -152,14 +155,18 @@ pub struct BrainSearchView {
}
/// `memory_brain_search`.
-pub async fn search(config: &Config, params: BrainSearchParams) -> MemoryResult<BrainSearchView> {
+pub async fn search(
+ config: &Config,
+ identity: &MemoryIdentity,
+ params: BrainSearchParams,
+) -> MemoryResult<BrainSearchView> {
let query = params.query.trim();
if query.is_empty() {
return Err(MemoryError::invalid("the query is blank"));
}
let source = params.source.as_deref().map(parse_source).transpose()?;
let limit = super::types::clamp_limit(params.limit);
- let hits = brain(config)?.search(query, source.as_ref(), limit).await?;
+ let hits = brain(config, identity)?.search(query, source.as_ref(), limit).await?;
tracing::debug!(hits = hits.len(), "[memory:brain] search");
Ok(BrainSearchView { hits })
}
@@ -215,7 +222,11 @@ async fn ingest_path(config: &Config, path: &str) -> MemoryResult<std::path::Pat
/// `memory_brain_ingest`: files a document in the brain and queues its
/// source's belief build. The write waits only for the engine to accept it.
-pub async fn ingest(config: &Config, params: BrainIngestParams) -> MemoryResult<BrainIngestView> {
+pub async fn ingest(
+ config: &Config,
+ identity: &MemoryIdentity,
+ params: BrainIngestParams,
+) -> MemoryResult<BrainIngestView> {
let source = params.source.as_deref().map(parse_source).transpose()?;
let mut document = match (params.path.as_deref(), params.text.as_deref()) {
(Some(path), None) => {
@@ -256,12 +267,12 @@ pub async fn ingest(config: &Config, params: BrainIngestParams) -> MemoryResult<
document = document.titled(title);
}
let filed = document.source.to_string();
- let ingested = brain(config)?
+ let ingested = brain(config, identity)?
.ingest_with(document, WriteOptions::accepted())
.await?;
jobs::enqueue(
config,
- layout(config).root(),
+ layout(config, identity).root(),
ingested.job.into_iter().collect(),
)
.await;
@@ -288,9 +299,13 @@ pub struct BrainForgetView {
}
/// `memory_brain_forget`: forgets every document of one source.
-pub async fn forget(config: &Config, params: BrainForgetParams) -> MemoryResult<BrainForgetView> {
+pub async fn forget(
+ config: &Config,
+ identity: &MemoryIdentity,
+ params: BrainForgetParams,
+) -> MemoryResult<BrainForgetView> {
let source = parse_source(&params.source)?;
- let report = brain(config)?.forget(&source).await?;
+ let report = brain(config, identity)?.forget(&source).await?;
tracing::info!(source = %source, forgotten = report.forgotten, "[memory:brain] source forgotten");
Ok(BrainForgetView {
forgotten: report.forgotten,
diff --git a/crates/openhuman-core/src/memory/lifecycle/mod.rs b/crates/openhuman-core/src/memory/lifecycle/mod.rs
index f9757f03cd..3a48f5838d 100644
--- a/crates/openhuman-core/src/memory/lifecycle/mod.rs
+++ b/crates/openhuman-core/src/memory/lifecycle/mod.rs
@@ -80,20 +80,24 @@ pub fn agent_memory(config: &Config, identity: &ResolvedIdentity) -> MemoryResul
agent_memory_on(bound.engine, config, identity)
}
-/// The memory of the identity in scope (the default agent outside one).
-pub fn current_agent_memory(config: &Config) -> MemoryResult<(AgentMemory, ResolvedIdentity)> {
- let identity = super::scope::resolve_current(config);
+/// The memory of `identity` (the default agent for the root identity).
+pub fn current_agent_memory(
+ config: &Config,
+ identity: &super::scope::MemoryIdentity,
+) -> MemoryResult<(AgentMemory, ResolvedIdentity)> {
+ let identity = identity.resolve(config);
let memory = agent_memory(config, &identity)?;
Ok((memory, identity))
}
-/// An agent id the caller named, resolved under the in-scope root; the
-/// identity in scope when none is named.
+/// An agent id the caller named, resolved under `identity`'s root; `identity`
+/// itself when none is named.
pub fn named_agent_memory(
config: &Config,
+ identity: &super::scope::MemoryIdentity,
agent_id: Option<&str>,
) -> MemoryResult<(AgentMemory, ResolvedIdentity)> {
- let mut identity = super::scope::resolve_current(config);
+ let mut identity = identity.resolve(config);
if let Some(agent_id) = agent_id.map(str::trim).filter(|id| !id.is_empty()) {
identity.agent_id = agent_id.to_string();
}
diff --git a/crates/openhuman-core/src/memory/lifecycle/views.rs b/crates/openhuman-core/src/memory/lifecycle/views.rs
index 48f4747b67..400b4e6979 100644
--- a/crates/openhuman-core/src/memory/lifecycle/views.rs
+++ b/crates/openhuman-core/src/memory/lifecycle/views.rs
@@ -163,9 +163,11 @@ pub struct PackPreviewView {
/// (without one) would be given. Reads only; nothing is logged.
pub async fn pack_preview(
config: &Config,
+ identity: &MemoryIdentity,
params: PackPreviewParams,
) -> MemoryResult<PackPreviewView> {
- let (memory, identity) = super::named_agent_memory(config, params.agent_id.as_deref())?;
+ let (memory, identity) =
+ super::named_agent_memory(config, identity, params.agent_id.as_deref())?;
let query = params.query.filter(|query| !query.trim().is_empty());
let (mode, pack) = match query {
Some(query) => ("turn", memory.recall(&query).await?),
@@ -207,8 +209,8 @@ pub struct AgentsView {
}
/// `memory_agents_list`: the agents with conversations under the root.
-pub async fn agents_list(config: &Config) -> MemoryResult<AgentsView> {
- let (memory, identity) = super::current_agent_memory(config)?;
+pub async fn agents_list(config: &Config, identity: &MemoryIdentity) -> MemoryResult<AgentsView> {
+ let (memory, identity) = super::current_agent_memory(config, identity)?;
let page = memory
.engine()
.explore(ExploreRequest {
diff --git a/crates/openhuman-core/src/memory/schemas/handlers.rs b/crates/openhuman-core/src/memory/schemas/handlers.rs
index 500e5df525..1870e8e1bc 100644
--- a/crates/openhuman-core/src/memory/schemas/handlers.rs
+++ b/crates/openhuman-core/src/memory/schemas/handlers.rs
@@ -17,6 +17,7 @@ use crate::memory::types::{
SourceRemovedView, SourcesAddParams, SourcesListView, SourcesRemoveParams, SourcesSyncParams,
SourcesSyncView,
};
+use crate::memory::scope::MemoryIdentity;
use crate::memory::{backfill, brain, confine, engine, import, layout_migration, ops, sources};
fn parse<T: DeserializeOwned>(params: Map<String, Value>) -> Result<T, String> {
@@ -176,28 +177,28 @@ pub(super) fn conversations_backfill_start(params: Map<String, Value>) -> Contro
pub(super) fn brain_sources(params: Map<String, Value>) -> ControllerFuture {
Box::pin(async move {
parse::<EmptyParams>(params)?;
- finish(brain::sources(&load().await?).await)
+ finish(brain::sources(&load().await?, &MemoryIdentity::root()).await)
})
}
pub(super) fn brain_search(params: Map<String, Value>) -> ControllerFuture {
Box::pin(async move {
let params = parse::<brain::BrainSearchParams>(params)?;
- finish(brain::search(&load().await?, params).await)
+ finish(brain::search(&load().await?, &MemoryIdentity::root(), params).await)
})
}
pub(super) fn brain_ingest(params: Map<String, Value>) -> ControllerFuture {
Box::pin(async move {
let params = parse::<brain::BrainIngestParams>(params)?;
- finish(brain::ingest(&load().await?, params).await)
+ finish(brain::ingest(&load().await?, &MemoryIdentity::root(), params).await)
})
}
pub(super) fn brain_forget(params: Map<String, Value>) -> ControllerFuture {
Box::pin(async move {
let params = parse::<brain::BrainForgetParams>(params)?;
- finish(brain::forget(&load().await?, params).await)
+ finish(brain::forget(&load().await?, &MemoryIdentity::root(), params).await)
})
}