Skip to content

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

feat(memory): add backfill API for storing past conversations

Steven Enamakel122c371f7370
diff --git a/crates/openhuman-core/src/memory/conversations/backfill_tests.rs b/crates/openhuman-core/src/memory/conversations/backfill_tests.rs
new file mode 100644
index 0000000000..179adb7b4b
--- /dev/null
+++ b/crates/openhuman-core/src/memory/conversations/backfill_tests.rs
@@ -0,0 +1 @@
+//! placeholder
diff --git a/crates/openhuman-core/src/memory/conversations/mod.rs b/crates/openhuman-core/src/memory/conversations/mod.rs
index 6bbf5edebd..bb43c15232 100644
--- a/crates/openhuman-core/src/memory/conversations/mod.rs
+++ b/crates/openhuman-core/src/memory/conversations/mod.rs
@@ -10,6 +10,7 @@
//! numbers `meta.turns`, and the latest stored batches the UI lists — lives in
//! `<workspace>/memory/conversations_state.json`. Turn text never touches it.
+pub mod backfill;
pub mod buffer;
use std::collections::{BTreeMap, HashMap};
@@ -141,6 +142,19 @@ fn remember_stored(workspace_dir: &Path, batch: &Batch, stored_at: DateTime<Utc>
write_state(workspace_dir, &state);
}
+/// How many turns of `thread_id` live ingestion has taken (stored or still
+/// buffered): the thread's most recent turns, which the backfill leaves alone.
+#[must_use]
+pub fn live_turns_taken(workspace_dir: &Path, thread_id: &str) -> u32 {
+ let _guard = STATE_LOCK
+ .lock()
+ .unwrap_or_else(std::sync::PoisonError::into_inner);
+ read_state(workspace_dir)
+ .threads
+ .get(thread_id)
+ .map_or(0, |counter| counter.next)
+}
+
/// The latest stored batches, newest first.
#[must_use]
pub fn recent(workspace_dir: &Path) -> Vec<RecentConversation> {
diff --git a/crates/openhuman-core/src/memory/schemas.rs b/crates/openhuman-core/src/memory/schemas.rs
index 034fa5ae5b..6ec75ffe79 100644
--- a/crates/openhuman-core/src/memory/schemas.rs
+++ b/crates/openhuman-core/src/memory/schemas.rs
@@ -40,6 +40,8 @@ fn handler_for(function: &str) -> crate::core::all::ControllerHandler {
"items_get" => handlers::items_get,
"conversations_get" => handlers::conversations_get,
"conversations_set" => handlers::conversations_set,
+ "conversations_backfill_status" => handlers::conversations_backfill_status,
+ "conversations_backfill_start" => handlers::conversations_backfill_start,
"sources_list" => handlers::sources_list,
"sources_add" => handlers::sources_add,
"sources_remove" => handlers::sources_remove,
diff --git a/crates/openhuman-core/src/memory/schemas/defs.rs b/crates/openhuman-core/src/memory/schemas/defs.rs
index c2ba3313d9..e25841cc36 100644
--- a/crates/openhuman-core/src/memory/schemas/defs.rs
+++ b/crates/openhuman-core/src/memory/schemas/defs.rs
@@ -4,7 +4,7 @@
use crate::core::{ControllerSchema, FieldSchema, TypeSchema};
/// Every function of the namespace, in spec order.
-pub const FUNCTIONS: [&str; 22] = [
+pub const FUNCTIONS: [&str; 24] = [
"engines_list",
"engine_get",
"engine_set",
@@ -17,6 +17,8 @@ pub const FUNCTIONS: [&str; 22] = [
"items_get",
"conversations_get",
"conversations_set",
+ "conversations_backfill_status",
+ "conversations_backfill_start",
"sources_list",
"sources_add",
"sources_remove",
@@ -199,6 +201,20 @@ pub fn schema(function: &str) -> ControllerSchema {
],
outputs: out("Same as memory_conversations_get."),
},
+ "conversations_backfill_status" => ControllerSchema {
+ namespace: "memory",
+ function: "conversations_backfill_status",
+ description: "Progress of storing past chats, and how many threads and turns from before automatic ingestion are still unstored.",
+ inputs: vec![],
+ outputs: out("{state: {phase, threads_total, threads_done, turns_stored, items_stored, error?, finished_at?}, pending_threads, pending_turns}"),
+ },
+ "conversations_backfill_start" => ControllerSchema {
+ namespace: "memory",
+ function: "conversations_backfill_start",
+ description: "Store past chats (turns from before automatic ingestion) as conversation memory, in the background. Uploads chat history to the selected engine, so it requires consent: true.",
+ inputs: vec![req("consent", TypeSchema::Bool, "The user agreed to upload past chats to the engine.")],
+ outputs: out("Same as memory_conversations_backfill_status."),
+ },
"sources_list" => ControllerSchema {
namespace: "memory",
function: "sources_list",
diff --git a/crates/openhuman-core/src/memory/schemas/handlers.rs b/crates/openhuman-core/src/memory/schemas/handlers.rs
index d9614c5e8e..713782d306 100644
--- a/crates/openhuman-core/src/memory/schemas/handlers.rs
+++ b/crates/openhuman-core/src/memory/schemas/handlers.rs
@@ -120,6 +120,20 @@ pub(super) fn items_get(params: Map<String, Value>) -> ControllerFuture {
})
}
+pub(super) fn conversations_backfill_status(params: Map<String, Value>) -> ControllerFuture {
+ Box::pin(async move {
+ parse::<EmptyParams>(params)?;
+ finish(conversations::backfill::status(&load().await?).await)
+ })
+}
+
+pub(super) fn conversations_backfill_start(params: Map<String, Value>) -> ControllerFuture {
+ Box::pin(async move {
+ let params = parse::<conversations::backfill::BackfillStartParams>(params)?;
+ finish(conversations::backfill::start(&load().await?, params).await)
+ })
+}
+
pub(super) fn conversations_get(params: Map<String, Value>) -> ControllerFuture {
Box::pin(async move {
parse::<EmptyParams>(params)?;