Skip to content

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

feat(threads): scope conversation store to a dispatch context

Steven Enamakel38622e23135d
diff --git a/crates/openhuman-core/src/core/runtime/dispatch_ctx.rs b/crates/openhuman-core/src/core/runtime/dispatch_ctx.rs
index bffb1f0324..3e77b91e02 100644
--- a/crates/openhuman-core/src/core/runtime/dispatch_ctx.rs
+++ b/crates/openhuman-core/src/core/runtime/dispatch_ctx.rs
@@ -93,6 +93,13 @@ impl DispatchCtx {
}
}
+ /// The tenant this call acts for: the resolved context's profile and
+ /// agent. Outside SaaS an unresolved context is the default tenant; in SaaS
+ /// it is [`NoTenant`](super::NoTenant), so the caller fails closed.
+ pub fn tenant(&self) -> Result<super::Tenant, super::NoTenant> {
+ super::tenant_in(super::is_saas(), self.resolved().as_deref())
+ }
+
/// The configuration an embedder supplied for the context this call runs
/// under, if any. See [`CoreContext::embedder_config`].
pub fn embedder_config(&self) -> Option<crate::config::Config> {
diff --git a/crates/openhuman-core/src/threads/store/backend.rs b/crates/openhuman-core/src/threads/store/backend.rs
index ae2af23354..538533e668 100644
--- a/crates/openhuman-core/src/threads/store/backend.rs
+++ b/crates/openhuman-core/src/threads/store/backend.rs
@@ -25,6 +25,7 @@ use tinyagents_session::threads::{
ThreadMessagePatch,
};
+use crate::core::runtime::DispatchCtx;
use crate::storage::documents::Repo;
use crate::storage::{self, Scope, StorageError};
use tinystoragedrivers::ErrorKind;
@@ -34,8 +35,8 @@ const LOG_PREFIX: &str = "[conversations]";
/// The storage scope conversations live under: the calling tenant's profile,
/// else `local`; refused in SaaS mode with no profile.
-fn conversation_scope() -> Result<Scope, String> {
- let tenant = crate::core::runtime::current_tenant().map_err(|e| e.to_string())?;
+fn conversation_scope(ctx: &DispatchCtx) -> Result<Scope, String> {
+ let tenant = ctx.tenant().map_err(|e| e.to_string())?;
storage::scope_from(
tenant.profile.as_deref(),
None,
@@ -93,19 +94,21 @@ fn describe(error: &ConversationError, subject: Subject<'_>) -> String {
/// A conversation store bound to one workspace's storage backend.
#[derive(Clone)]
pub struct ConversationStore {
+ ctx: DispatchCtx,
workspace_dir: PathBuf,
}
impl ConversationStore {
- /// A store for `workspace_dir`. Nothing is opened until the first call.
- pub fn new(workspace_dir: PathBuf) -> Self {
- Self { workspace_dir }
+ /// A store for `workspace_dir`, acting for the tenant `ctx` serves.
+ /// Nothing is opened until the first call.
+ pub fn new(ctx: DispatchCtx, workspace_dir: PathBuf) -> Self {
+ Self { ctx, workspace_dir }
}
/// The driver store for this call, its schema declared once per backend
/// and scope, and the legacy JSONL conversations imported once.
fn open(&self) -> Result<DriverConversationStore, String> {
- let scope = conversation_scope()?;
+ let scope = conversation_scope(&self.ctx)?;
let backend = storage::local::backend_for(&self.workspace_dir)
.map_err(|e| format!("{LOG_PREFIX} open the storage backend: {e:#}"))?;
let scoped = backend
@@ -296,98 +299,106 @@ impl ConversationStore {
}
}
-fn store(workspace_dir: PathBuf) -> ConversationStore {
- ConversationStore::new(workspace_dir)
+fn store(ctx: &DispatchCtx, workspace_dir: PathBuf) -> ConversationStore {
+ ConversationStore::new(ctx.clone(), workspace_dir)
}
/// Free-function shim around [`ConversationStore::ensure_thread`].
pub fn ensure_thread(
+ ctx: &DispatchCtx,
workspace_dir: PathBuf,
request: CreateConversationThread,
) -> Result<ConversationThread, String> {
- store(workspace_dir).ensure_thread(request)
+ store(ctx, workspace_dir).ensure_thread(request)
}
/// Free-function shim around [`ConversationStore::list_threads`].
-pub fn list_threads(workspace_dir: PathBuf) -> Result<Vec<ConversationThread>, String> {
- store(workspace_dir).list_threads()
+pub fn list_threads(ctx: &DispatchCtx, workspace_dir: PathBuf) -> Result<Vec<ConversationThread>, String> {
+ store(ctx, workspace_dir).list_threads()
}
/// Free-function shim around [`ConversationStore::get_messages`].
-pub fn get_messages(workspace_dir: PathBuf, thread_id: &str) -> Result<Vec<ThreadMessage>, String> {
- store(workspace_dir).get_messages(thread_id)
+pub fn get_messages(ctx: &DispatchCtx, workspace_dir: PathBuf, thread_id: &str) -> Result<Vec<ThreadMessage>, String> {
+ store(ctx, workspace_dir).get_messages(thread_id)
}
/// Free-function shim around [`ConversationStore::append_message`].
pub fn append_message(
+ ctx: &DispatchCtx,
workspace_dir: PathBuf,
thread_id: &str,
message: ThreadMessage,
) -> Result<ThreadMessage, String> {
- store(workspace_dir).append_message(thread_id, message)
+ store(ctx, workspace_dir).append_message(thread_id, message)
}
/// Free-function shim around [`ConversationStore::update_thread_title`].
pub fn update_thread_title(
+ ctx: &DispatchCtx,
workspace_dir: PathBuf,
thread_id: &str,
title: &str,
updated_at: &str,
) -> Result<ConversationThread, String> {
- store(workspace_dir).update_thread_title(thread_id, title, updated_at)
+ store(ctx, workspace_dir).update_thread_title(thread_id, title, updated_at)
}
/// Free-function shim around [`ConversationStore::update_thread_labels`].
pub fn update_thread_labels(
+ ctx: &DispatchCtx,
workspace_dir: PathBuf,
thread_id: &str,
labels: Vec<String>,
updated_at: &str,
) -> Result<ConversationThread, String> {
- store(workspace_dir).update_thread_labels(thread_id, labels, updated_at)
+ store(ctx, workspace_dir).update_thread_labels(thread_id, labels, updated_at)
}
/// Free-function shim around [`ConversationStore::update_thread_working_dir`].
pub fn update_thread_working_dir(
+ ctx: &DispatchCtx,
workspace_dir: PathBuf,
thread_id: &str,
working_dir: Option<String>,
updated_at: &str,
) -> Result<ConversationThread, String> {
- store(workspace_dir).update_thread_working_dir(thread_id, working_dir, updated_at)
+ store(ctx, workspace_dir).update_thread_working_dir(thread_id, working_dir, updated_at)
}
/// Free-function shim around [`ConversationStore::update_message`].
pub fn update_message(
+ ctx: &DispatchCtx,
workspace_dir: PathBuf,
thread_id: &str,
message_id: &str,
patch: ThreadMessagePatch,
) -> Result<ThreadMessage, String> {
- store(workspace_dir).update_message(thread_id, message_id, patch)
+ store(ctx, workspace_dir).update_message(thread_id, message_id, patch)
}
/// Free-function shim around [`ConversationStore::delete_messages_from`].
pub fn delete_messages_from(
+ ctx: &DispatchCtx,
workspace_dir: PathBuf,
thread_id: &str,
message_id: &str,
) -> Result<Option<usize>, String> {
- store(workspace_dir).delete_messages_from(thread_id, message_id)
+ store(ctx, workspace_dir).delete_messages_from(thread_id, message_id)
}
/// Free-function shim around [`ConversationStore::purge_threads`].
-pub fn purge_threads(workspace_dir: PathBuf) -> Result<ConversationPurgeStats, String> {
- store(workspace_dir).purge_threads()
+pub fn purge_threads(ctx: &DispatchCtx, workspace_dir: PathBuf) -> Result<ConversationPurgeStats, String> {
+ store(ctx, workspace_dir).purge_threads()
}
/// Free-function shim around [`ConversationStore::delete_thread`].
pub fn delete_thread(
+ ctx: &DispatchCtx,
workspace_dir: PathBuf,
thread_id: &str,
deleted_at: &str,
) -> Result<bool, String> {
- store(workspace_dir).delete_thread(thread_id, deleted_at)
+ store(ctx, workspace_dir).delete_thread(thread_id, deleted_at)
}
/// Whether `path` is the legacy conversation root of `workspace_dir`.
diff --git a/crates/openhuman-core/src/threads/store/blocking.rs b/crates/openhuman-core/src/threads/store/blocking.rs
index 673f6674ce..8a64961451 100644
--- a/crates/openhuman-core/src/threads/store/blocking.rs
+++ b/crates/openhuman-core/src/threads/store/blocking.rs
@@ -51,6 +51,7 @@
use std::path::PathBuf;
+use crate::core::runtime::DispatchCtx;
use crate::threads::store;
use super::{
@@ -86,122 +87,131 @@ where
/// [`store::ensure_thread`] on the blocking pool.
pub async fn ensure_thread(
+ ctx: DispatchCtx,
workspace_dir: PathBuf,
request: CreateConversationThread,
) -> Result<ConversationThread, String> {
run("ensure_thread", move || {
- store::ensure_thread(workspace_dir, request)
+ store::ensure_thread(&ctx, workspace_dir, request)
})
.await
}
/// [`store::list_threads`] on the blocking pool.
-pub async fn list_threads(workspace_dir: PathBuf) -> Result<Vec<ConversationThread>, String> {
- run("list_threads", move || store::list_threads(workspace_dir)).await
+pub async fn list_threads(ctx: DispatchCtx, workspace_dir: PathBuf) -> Result<Vec<ConversationThread>, String> {
+ run("list_threads", move || store::list_threads(&ctx, workspace_dir)).await
}
/// [`store::get_messages`] on the blocking pool.
pub async fn get_messages(
+ ctx: DispatchCtx,
workspace_dir: PathBuf,
thread_id: String,
) -> Result<Vec<ConversationMessage>, String> {
run("get_messages", move || {
- store::get_messages(workspace_dir, &thread_id)
+ store::get_messages(&ctx, workspace_dir, &thread_id)
})
.await
}
/// [`store::append_message`] on the blocking pool.
pub async fn append_message(
+ ctx: DispatchCtx,
workspace_dir: PathBuf,
thread_id: String,
message: ConversationMessage,
) -> Result<ConversationMessage, String> {
run("append_message", move || {
- store::append_message(workspace_dir, &thread_id, message)
+ store::append_message(&ctx, workspace_dir, &thread_id, message)
})
.await
}
/// [`store::update_message`] on the blocking pool.
pub async fn update_message(
+ ctx: DispatchCtx,
workspace_dir: PathBuf,
thread_id: String,
message_id: String,
patch: ConversationMessagePatch,
) -> Result<ConversationMessage, String> {
run("update_message", move || {
- store::update_message(workspace_dir, &thread_id, &message_id, patch)
+ store::update_message(&ctx, workspace_dir, &thread_id, &message_id, patch)
})
.await
}
/// [`store::delete_messages_from`] on the blocking pool.
pub async fn delete_messages_from(
+ ctx: DispatchCtx,
workspace_dir: PathBuf,
thread_id: String,
message_id: String,
) -> Result<Option<usize>, String> {
run("delete_messages_from", move || {
- store::delete_messages_from(workspace_dir, &thread_id, &message_id)
+ store::delete_messages_from(&ctx, workspace_dir, &thread_id, &message_id)
})
.await
}
/// [`store::update_thread_title`] on the blocking pool.
pub async fn update_thread_title(
+ ctx: DispatchCtx,
workspace_dir: PathBuf,
thread_id: String,
title: String,
updated_at: String,
) -> Result<ConversationThread, String> {
run("update_thread_title", move || {
- store::update_thread_title(workspace_dir, &thread_id, &title, &updated_at)
+ store::update_thread_title(&ctx, workspace_dir, &thread_id, &title, &updated_at)
})
.await
}
/// [`store::update_thread_working_dir`] on the blocking pool.
pub async fn update_thread_working_dir(
+ ctx: DispatchCtx,
workspace_dir: PathBuf,
thread_id: String,
working_dir: Option<String>,
updated_at: String,
) -> Result<ConversationThread, String> {
run("update_thread_working_dir", move || {
- store::update_thread_working_dir(workspace_dir, &thread_id, working_dir, &updated_at)
+ store::update_thread_working_dir(&ctx, workspace_dir, &thread_id, working_dir, &updated_at)
})
.await
}
/// [`store::update_thread_labels`] on the blocking pool.
pub async fn update_thread_labels(
+ ctx: DispatchCtx,
workspace_dir: PathBuf,
thread_id: String,
labels: Vec<String>,
updated_at: String,
) -> Result<ConversationThread, String> {
run("update_thread_labels", move || {
- store::update_thread_labels(workspace_dir, &thread_id, labels, &updated_at)
+ store::update_thread_labels(&ctx, workspace_dir, &thread_id, labels, &updated_at)
})
.await
}
/// [`store::delete_thread`] on the blocking pool.
pub async fn delete_thread(
+ ctx: DispatchCtx,
workspace_dir: PathBuf,
thread_id: String,
deleted_at: String,
) -> Result<bool, String> {
run("delete_thread", move || {
- store::delete_thread(workspace_dir, &thread_id, &deleted_at)
+ store::delete_thread(&ctx, workspace_dir, &thread_id, &deleted_at)
})
.await
}
/// [`store::purge_threads`] on the blocking pool.
-pub async fn purge_threads(workspace_dir: PathBuf) -> Result<ConversationPurgeStats, String> {
- run("purge_threads", move || store::purge_threads(workspace_dir)).await
+pub async fn purge_threads(ctx: DispatchCtx, workspace_dir: PathBuf) -> Result<ConversationPurgeStats, String> {
+ run("purge_threads", move || store::purge_threads(&ctx, workspace_dir)).await
}
/// [`ConversationStore::search_cross_thread_messages`] on the blocking pool.
@@ -209,13 +219,14 @@ pub async fn purge_threads(workspace_dir: PathBuf) -> Result<ConversationPurgeSt
/// The heaviest operation in the family: a cold inverted index reads every
/// thread's transcript before the search runs.
pub async fn search_cross_thread_messages(
+ ctx: DispatchCtx,
workspace_dir: PathBuf,
query: String,
limit: usize,
exclude_thread_id: Option<String>,
) -> Result<Vec<CrossThreadHit>, String> {
run("search_cross_thread_messages", move || {
- ConversationStore::new(workspace_dir).search_cross_thread_messages(
+ ConversationStore::new(ctx, workspace_dir).search_cross_thread_messages(
&query,
limit,
exclude_thread_id.as_deref(),