Skip to content

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

feat(memory): file the brain by connector, not by file type

M3gA-Mindc7523c06e6e2
diff --git a/crates/openhuman-core/src/memory/brain.rs b/crates/openhuman-core/src/memory/brain.rs
index 5c023e4800..cb0a71363b 100644
--- a/crates/openhuman-core/src/memory/brain.rs
+++ b/crates/openhuman-core/src/memory/brain.rs
@@ -1,10 +1,10 @@
-//! The brain: documents every agent shares, filed by source type.
+//! The brain: documents every agent shares, filed by the connector they came
+//! from.
//!
-//! TinyMemory's layout keeps documents at `source:<kind>` nodes below the
-//! layout root (`tinymemory_tools::MemoryLayout::brain`), with no agent id.
-//! Synced sources ([`super::sources`]) are filed there by what they read
-//! ([`brain_source`]); a file ingested from the UI by its format
-//! (`tinymemory_integrations::brain::brain_document`). Every ingest queues a
+//! TinyMemory's layout keeps documents at `source:<connector>` nodes below
+//! the layout root (`tinymemory_tools::MemoryLayout::brain`), with no agent
+//! id. Synced sources ([`super::sources`]) are filed there by what they read
+//! ([`brain_source`]); a file ingested from the UI under `files`. Every ingest queues a
//! belief build of the source's scope (`lifecycle::jobs`).
//!
//! The `memory_brain_*` RPCs read and forget per source, under the root of
@@ -26,34 +26,30 @@ use super::scope;
/// Largest file `memory_brain_ingest` reads, in bytes.
pub const MAX_INGEST_BYTES: u64 = 25 * 1024 * 1024;
-/// The brain source a synced item belongs to: GitHub repos to `github`,
-/// links and feeds to `web`, a Composio toolkit to its own source
-/// (`notion` is the known one), and local files by their type — PDFs to
-/// `pdf`, HTML to `web`, everything else textual to `markdown`.
+/// 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 brain_source(kind: MemorySourceKind, target: &str, item: &StoreItem) -> BrainSource {
+pub fn files_source() -> BrainSource {
+ BrainSource::Other("files".to_string())
+}
+
+/// The brain source a synced item belongs to: the connector it came from,
+/// so disconnecting or removing it erases one source. GitHub (the reader
+/// and the Composio toolkit alike) to `github`, links and feeds to `web`, a
+/// Composio toolkit to its canonical slug (`gmail`, `googledrive`), and
+/// local files, whatever their format, to `files`.
+#[must_use]
+pub fn brain_source(kind: MemorySourceKind, target: &str) -> BrainSource {
match kind {
MemorySourceKind::Github => BrainSource::Github,
MemorySourceKind::Link | MemorySourceKind::Rss => BrainSource::Web,
- MemorySourceKind::Composio => target
- .trim()
- .to_ascii_lowercase()
- .parse()
- .unwrap_or_else(|_| BrainSource::Other("composio".to_string())),
- MemorySourceKind::Folder | MemorySourceKind::File => {
- let mime = match item {
- StoreItem::Document { mime, .. } => mime.as_deref().unwrap_or_default(),
- _ => "",
- };
- let path = item.meta().file_path.as_deref().unwrap_or_default();
- if mime.contains("pdf") || path.to_ascii_lowercase().ends_with(".pdf") {
- BrainSource::Pdf
- } else if mime.contains("html") {
- BrainSource::Web
- } else {
- BrainSource::Markdown
- }
+ MemorySourceKind::Composio => {
+ crate::integrations::composio::tools::canonicalize_toolkit_slug(target)
+ .parse()
+ .unwrap_or_else(|_| BrainSource::Other("composio".to_string()))
}
+ MemorySourceKind::Folder | MemorySourceKind::File => files_source(),
}
}
@@ -186,8 +182,7 @@ pub struct BrainIngestParams {
/// Text to file directly.
#[serde(default)]
pub text: Option<String>,
- /// The source to file under; unset picks it from the file's format
- /// (`markdown` for text).
+ /// The source to file under; unset files it under `files`.
#[serde(default)]
pub source: Option<String>,
/// A title.
@@ -233,14 +228,14 @@ pub async fn ingest(config: &Config, params: BrainIngestParams) -> MemoryResult<
tinymemory_integrations::brain::brain_document(
super::convert::converter(),
&raw,
- source,
+ Some(source.unwrap_or_else(files_source)),
meta,
)
.await
.map_err(|error| MemoryError::invalid(format!("cannot convert the file: {error}")))?
}
(None, Some(text)) => {
- tinymemory_tools::BrainDocument::new(source.unwrap_or(BrainSource::Markdown), text)
+ tinymemory_tools::BrainDocument::new(source.unwrap_or_else(files_source), text)
}
_ => return Err(MemoryError::invalid("pass exactly one of `path` or `text`")),
};
diff --git a/crates/openhuman-core/src/memory/brain_tests.rs b/crates/openhuman-core/src/memory/brain_tests.rs
index 6a50eb6c02..154a7af44c 100644
--- a/crates/openhuman-core/src/memory/brain_tests.rs
+++ b/crates/openhuman-core/src/memory/brain_tests.rs
@@ -17,48 +17,32 @@ fn document(mime: Option<&str>, path: Option<&str>) -> StoreItem {
}
#[test]
-fn synced_items_are_filed_by_what_they_read() {
- let plain = document(None, Some("notes/a.md"));
- assert_eq!(
- brain_source(MemorySourceKind::Github, "o/r", &plain),
- BrainSource::Github
- );
- assert_eq!(
- brain_source(MemorySourceKind::Link, "https://x", &plain),
- BrainSource::Web
- );
- assert_eq!(
- brain_source(MemorySourceKind::Rss, "https://x/feed", &plain),
- BrainSource::Web
- );
- assert_eq!(
- brain_source(MemorySourceKind::Composio, "Notion", &plain),
- BrainSource::Notion
- );
- assert_eq!(
- brain_source(MemorySourceKind::Composio, "gmail", &plain),
- BrainSource::Other("gmail".into())
- );
- assert_eq!(
- brain_source(MemorySourceKind::Folder, "/n", &plain),
- BrainSource::Markdown
- );
- assert_eq!(
- brain_source(
- MemorySourceKind::File,
- "/n",
- &document(None, Some("deck/Q3.PDF"))
+fn synced_items_are_filed_by_their_connector() {
+ for (kind, target, want) in [
+ (MemorySourceKind::Github, "o/r", BrainSource::Github),
+ (MemorySourceKind::Link, "https://x", BrainSource::Web),
+ (MemorySourceKind::Rss, "https://x/feed", BrainSource::Web),
+ (MemorySourceKind::Composio, "Notion", BrainSource::Notion),
+ // The Composio toolkit and the GitHub reader share one source.
+ (MemorySourceKind::Composio, "github", BrainSource::Github),
+ (
+ MemorySourceKind::Composio,
+ "gmail",
+ BrainSource::Other("gmail".into()),
),
- BrainSource::Pdf
- );
- assert_eq!(
- brain_source(
- MemorySourceKind::Folder,
- "/n",
- &document(Some("text/html"), None)
+ // An alias files under the slug Composio itself uses.
+ (
+ MemorySourceKind::Composio,
+ "google_drive",
+ BrainSource::Other("googledrive".into()),
),
- BrainSource::Web
- );
+ // Local files, whatever their format, share one source.
+ (MemorySourceKind::Folder, "/n", files_source()),
+ (MemorySourceKind::File, "/n/deck/Q3.PDF", files_source()),
+ ] {
+ assert_eq!(brain_source(kind, target), want, "{kind:?} {target}");
+ }
+ assert_eq!(files_source().to_string(), "files");
}
#[test]
@@ -184,7 +168,7 @@ async fn ingest_refuses_bad_input() {
}
#[tokio::test]
-async fn a_file_is_converted_and_filed_by_its_format() {
+async fn a_file_is_converted_and_filed_under_files() {
let tmp = tempfile::tempdir().unwrap();
let config = config_in(&tmp);
let engine = bind_reference(&config);
@@ -201,9 +185,9 @@ async fn a_file_is_converted_and_filed_by_its_format() {
)
.await
.unwrap();
- assert_eq!(ingested.source, "markdown");
+ assert_eq!(ingested.source, "files");
let docs = stored(&engine, MetaFilter::kinds([ItemKind::Document])).await;
- assert_eq!(docs[0].meta.namespace.to_string(), "source:markdown");
+ assert_eq!(docs[0].meta.namespace.to_string(), "source:files");
}
#[tokio::test]
diff --git a/crates/openhuman-core/src/memory/sources/sync.rs b/crates/openhuman-core/src/memory/sources/sync.rs
index 15cdc91e6d..07415a391b 100644
--- a/crates/openhuman-core/src/memory/sources/sync.rs
+++ b/crates/openhuman-core/src/memory/sources/sync.rs
@@ -116,7 +116,7 @@ pub(crate) async fn store_all(
let mut last_error = None;
let mut touched = std::collections::BTreeSet::new();
for item in items {
- let brain_source = crate::memory::brain::brain_source(kind, target, &item);
+ let brain_source = crate::memory::brain::brain_source(kind, target);
let item = crate::memory::brain::file_into(layout, &brain_source, item)?;
match store_on(bound, item).await {
Ok(_) => {