Skip to content

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

feat(memory): add brain source sync

Steven Enamakel608f9574d819
diff --git a/crates/openhuman-core/src/memory/brain.rs b/crates/openhuman-core/src/memory/brain.rs
index 41d67b8921..55b82cf0c6 100644
--- a/crates/openhuman-core/src/memory/brain.rs
+++ b/crates/openhuman-core/src/memory/brain.rs
@@ -13,6 +13,7 @@
use serde::{Deserialize, Serialize};
use tinymemory_api::{ExploreRequest, Facet, Hit, Namespace, StoreItem, WriteOptions};
use tinymemory_integrations::documents::RawDocument;
+use tinymemory_tools::layout::placement::{brain_node_with, files_source};
use tinymemory_tools::{Brain, BrainSource, MemoryLayout};
use crate::config::schema::MemorySourceKind;
@@ -26,14 +27,6 @@ use super::scope;
/// Largest file `memory_brain_ingest` reads, in bytes.
pub const MAX_INGEST_BYTES: u64 = 25 * 1024 * 1024;
-/// The brain source of local files and uploads, of every format.
-// TODO(tinymemory#214): `BrainSource::Files` once the pin carries it; the
-// node (`source:files`) is the same.
-#[must_use]
-pub fn files_source() -> BrainSource {
- BrainSource::Other("files".to_string())
-}
-
/// The brain source a synced item belongs to: the connector it came from,
/// so removing it erases one source. GitHub to `github`, links and feeds to
/// `web`, and local files, whatever their format, to `files`.
@@ -46,82 +39,6 @@ pub fn brain_source(kind: MemorySourceKind) -> BrainSource {
}
}
-/// Canonical form of a connector slug found on a legacy brain node
-/// (`google_drive` becomes `googledrive`), so documents filed by the removed
-/// Composio sync still migrate to the node they were filed under.
-fn legacy_connector_slug(slug: &str) -> String {
- let key = slug.trim().to_ascii_lowercase();
- match key.as_str() {
- "feishu" | "lark" => "larksuite".to_string(),
- "google_calendar" => "googlecalendar".to_string(),
- "google_drive" => "googledrive".to_string(),
- "google_sheets" => "googlesheets".to_string(),
- _ => key,
- }
-}
-
-/// The brain source a document filed under the old per-type layout belongs
-/// to now, for the migration that moves it: `old_source_id` is the id of
-/// the `source:<id>` node it sits at, `item` the document itself.
-///
-/// - Per-format nodes (`pdf`, `markdown`, `docx`, `xlsx`, `pptx`, `code`,
-/// `other`) held local files: `files`.
-/// - `web` held links and feeds, but also HTML files from a folder or an
-/// upload; a document with a file kind or a file path is a file.
-/// - Any other node was a connector: its canonical slug (`google_drive`
-/// becomes `googledrive`), so `notion`, `github` and `gmail` stay put.
-#[must_use]
-pub fn legacy_brain_node(old_source_id: &str, item: &StoreItem) -> BrainSource {
- let id = legacy_connector_slug(old_source_id);
- match id.as_str() {
- "files" | "pdf" | "markdown" | "md" | "docx" | "xlsx" | "pptx" | "code" | "other" => {
- files_source()
- }
- "web" => {
- let meta = item.meta();
- let file = matches!(
- meta.source.kind,
- tinymemory_api::SourceKind::File | tinymemory_api::SourceKind::Folder
- ) || meta.file_path.is_some();
- if file {
- files_source()
- } else {
- BrainSource::Web
- }
- }
- _ => id.parse().unwrap_or_else(|_| files_source()),
- }
-}
-
-/// The repository a GitHub document belongs to, as the collection id
-/// `<owner>--<repo>` (lowercase, as GitHub's names are case-insensitive):
-/// from its `repo` (`owner/name` or a URL), else its URL. An owner name
-/// never holds `--` nor ends in `-`, so the first `--` always splits the
-/// two and no two repositories share an id (`foo-bar/repo` and
-/// `foo/bar-repo` stay apart).
-#[must_use]
-pub fn github_collection(item: &StoreItem) -> Option<String> {
- let meta = item.meta();
- [meta.repo.as_deref(), meta.url.as_deref()]
- .into_iter()
- .flatten()
- .find_map(|raw| {
- let path = raw
- .trim()
- .trim_start_matches("https://")
- .trim_start_matches("http://")
- .trim_start_matches("www.")
- .trim_start_matches("github.com/");
- // A URL's query or fragment is not part of the path.
- let path = path.split(['?', '#']).next().unwrap_or_default();
- let mut parts = path.split('/').filter(|part| !part.is_empty());
- let (owner, repo) = (parts.next()?, parts.next()?);
- let repo = repo.trim_end_matches(".git");
- (!owner.contains('.') && !repo.is_empty())
- .then(|| format!("{owner}--{repo}").to_ascii_lowercase())
- })
-}
-
/// The node `item` of `source` is filed at: the source's node, or, for
/// GitHub with `[memory] split_github_by_repo` on, its repository's
/// collection below it.
@@ -134,32 +51,6 @@ pub fn brain_node(
brain_node_with(config.memory.split_github_by_repo, layout, source, item)
}
-/// [`brain_node`] with `[memory] split_github_by_repo` given, for a caller
-/// that holds the setting rather than the config (the layout migration).
-pub fn brain_node_with(
- split_github_by_repo: bool,
- layout: &MemoryLayout,
- source: &BrainSource,
- item: &StoreItem,
-) -> MemoryResult<Namespace> {
- if split_github_by_repo && *source == BrainSource::Github {
- if let Some(repo) = github_collection(item) {
- return Ok(layout.brain_collection(source, &repo)?);
- }
- }
- Ok(layout.brain(source)?)
-}
-
-/// `item` placed in the brain at `node`, with no agent id (the brain
-/// belongs to every agent).
-#[must_use]
-pub fn file_into(node: Namespace, mut item: StoreItem) -> StoreItem {
- let meta = item.meta_mut();
- meta.namespace = node;
- meta.agent_id = None;
- item
-}
-
/// The layout the brain RPCs act on: the in-scope identity's.
fn layout(config: &Config) -> MemoryLayout {
scope::resolve_current(config).layout
diff --git a/crates/openhuman-core/src/memory/brain_tests.rs b/crates/openhuman-core/src/memory/brain_tests.rs
index a6ad641bb4..41f90d970b 100644
--- a/crates/openhuman-core/src/memory/brain_tests.rs
+++ b/crates/openhuman-core/src/memory/brain_tests.rs
@@ -31,43 +31,6 @@ fn synced_items_are_filed_by_their_connector() {
assert_eq!(files_source().to_string(), "files");
}
-#[test]
-fn a_github_document_names_its_repository() {
- let with = |repo: Option<&str>, url: Option<&str>| {
- let mut item = document(None, None);
- item.meta_mut().repo = repo.map(str::to_string);
- item.meta_mut().url = url.map(str::to_string);
- github_collection(&item)
- };
- assert_eq!(with(Some("Acme/API"), None).as_deref(), Some("acme--api"));
- assert_eq!(
- with(Some("https://github.com/acme/api.git"), None).as_deref(),
- Some("acme--api")
- );
- // A Composio issue carries only its URL.
- assert_eq!(
- with(
- None,
- Some("https://github.com/tinyhumansai/openhuman/issues/12")
- )
- .as_deref(),
- Some("tinyhumansai--openhuman")
- );
- assert_eq!(with(None, Some("https://example.com/a/b")), None);
- assert_eq!(with(Some("acme"), None), None);
- assert_eq!(with(None, None), None);
- // A query or fragment is not part of the repository.
- assert_eq!(
- with(None, Some("https://github.com/acme/api?tab=readme#top")).as_deref(),
- Some("acme--api")
- );
- // Hyphens on either side never make two repositories one collection.
- assert_ne!(
- with(Some("foo-bar/repo"), None),
- with(Some("foo/bar-repo"), None)
- );
-}
-
#[tokio::test]
async fn a_sources_collections_count_and_forget_with_it() {
use tinymemory_api::MemoryEngine as _;
@@ -288,52 +251,6 @@ async fn an_ingest_queues_a_belief_build_only_for_an_engine_that_waits_for_one()
}
}
-#[test]
-fn legacy_nodes_map_to_their_connector() {
- use tinymemory_api::{SourceKind, SourceRef};
- let item = |kind: SourceKind, path: Option<&str>| {
- let mut item = document(None, path);
- item.meta_mut().source = SourceRef { kind, id: None };
- item
- };
- let link = item(SourceKind::Link, None);
- for old in [
- "pdf", "markdown", "md", "docx", "xlsx", "pptx", "code", "other", "files",
- ] {
- assert_eq!(legacy_brain_node(old, &link), files_source(), "{old}");
- }
- // `web` held links and feeds, and HTML files that were filed beside them.
- assert_eq!(legacy_brain_node("web", &link), BrainSource::Web);
- assert_eq!(
- legacy_brain_node("web", &item(SourceKind::Rss, None)),
- BrainSource::Web
- );
- assert_eq!(
- legacy_brain_node("web", &item(SourceKind::Folder, None)),
- files_source()
- );
- assert_eq!(
- legacy_brain_node("web", &item(SourceKind::File, None)),
- files_source()
- );
- // An upload carried the `Link` kind `web` implies, but kept its path.
- assert_eq!(
- legacy_brain_node("web", &item(SourceKind::Link, Some("/u/pricing.html"))),
- files_source()
- );
- // Connectors keep their node, under the slug Composio uses.
- assert_eq!(legacy_brain_node("notion", &link), BrainSource::Notion);
- assert_eq!(legacy_brain_node("github", &link), BrainSource::Github);
- assert_eq!(
- legacy_brain_node("gmail", &link),
- BrainSource::Other("gmail".into())
- );
- assert_eq!(
- legacy_brain_node("google_drive", &link),
- BrainSource::Other("googledrive".into())
- );
-}
-
fn path_params(path: String) -> BrainIngestParams {
BrainIngestParams {
path: Some(path),
diff --git a/crates/openhuman-core/src/memory/sources/sync.rs b/crates/openhuman-core/src/memory/sources/sync.rs
index 08994c27dd..78ae2b15c2 100644
--- a/crates/openhuman-core/src/memory/sources/sync.rs
+++ b/crates/openhuman-core/src/memory/sources/sync.rs
@@ -124,7 +124,7 @@ pub(crate) async fn store_all(
let mut placed = Vec::with_capacity(items.len());
for item in items {
let node = crate::memory::brain::brain_node(config, layout, &brain_source, &item)?;
- let item = crate::memory::brain::file_into(node.clone(), item);
+ let item = tinymemory_tools::layout::placement::file_into(node.clone(), item);
placed.push((node, item));
}