Skip to content

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

refactor(threads): simplify thread edit operation

Steven Enamakeled8ccb36f899
diff --git a/crates/openhuman-core/src/threads/ops/edit.rs b/crates/openhuman-core/src/threads/ops/edit.rs
index 5b507341ce..adf010a31e 100644
--- a/crates/openhuman-core/src/threads/ops/edit.rs
+++ b/crates/openhuman-core/src/threads/ops/edit.rs
@@ -50,7 +50,7 @@ use crate::core::Outcome;
use crate::threads::store::{self as conversations, reply_run_id, run_reply_message_id};
use crate::threads::ThreadsError;
-use super::support::workspace_dir;
+use super::support::{transcript_store, workspace_dir, TranscriptStore};
#[derive(Debug, Clone, Deserialize)]
#[serde(deny_unknown_fields)]
@@ -87,7 +87,9 @@ pub async fn edit_message(
let client_id = request.client_id.unwrap_or_else(|| "system".to_string());
let thread_id = request.thread_id;
let dir = workspace_dir().await.map_err(ThreadsError::Message)?;
- let store_key = transcript_store_key().await;
+ // The request's own tenant picks the store key, so a SaaS profile edits
+ // the transcript its turns were written to.
+ let store = transcript_store(ctx).await.map_err(ThreadsError::Message)?;
crate::web_chat::cancel_chat_scoped(&client_id, &thread_id, None)
.await
@@ -101,17 +103,11 @@ pub async fn edit_message(
.map_err(ThreadsError::Message)?;
if let Some(cut_request_id) = &cut_request_id {
- let dir_for_blocking = dir.clone();
- let store_key_owned = store_key.clone();
+ let store_owned = store.clone();
let thread_id_owned = thread_id.clone();
let cut_request_id_owned = cut_request_id.clone();
tokio::task::spawn_blocking(move || {
- truncate_transcript_before_turn(
- &dir_for_blocking,
- &store_key_owned,
- &thread_id_owned,
- &cut_request_id_owned,
- )
+ truncate_transcript_before_turn(&store_owned, &thread_id_owned, &cut_request_id_owned)
})
.await
.map_err(|e| ThreadsError::Message(format!("truncate transcript task: {e}")))?
@@ -158,7 +154,9 @@ pub async fn regenerate(
let client_id = request.client_id.unwrap_or_else(|| "system".to_string());
let thread_id = request.thread_id;
let dir = workspace_dir().await.map_err(ThreadsError::Message)?;
- let store_key = transcript_store_key().await;
+ // The request's own tenant picks the store key, so a SaaS profile edits
+ // the transcript its turns were written to.
+ let store = transcript_store(ctx).await.map_err(ThreadsError::Message)?;
crate::web_chat::cancel_chat_scoped(&client_id, &thread_id, None)
.await
@@ -175,14 +173,12 @@ pub async fn regenerate(
None => None,
};
- let dir_for_blocking = dir.clone();
- let store_key_for_blocking = store_key.clone();
+ let store_for_blocking = store.clone();
let thread_id_for_blocking = thread_id.clone();
let target_for_blocking = target_request_id.clone();
let (prompt, cut_request_id) = tokio::task::spawn_blocking(move || {
truncate_transcript_for_regenerate(
- &dir_for_blocking,
- &store_key_for_blocking,
+ &store_for_blocking,
&thread_id_for_blocking,
target_for_blocking.as_deref(),
)
@@ -251,28 +247,13 @@ async fn next_reply_request_id_after(
/// order (`tinyagents_session::transcript::view`'s resolver module doc explains why that
/// is unsafe: `.g1` sorts before the un-suffixed root).
fn resolve_head_transcript(
- workspace_dir: &std::path::Path,
- store_key: &str,
+ store: &TranscriptStore,
thread_id: &str,
) -> Result<HeadTranscript, String> {
- find_head_transcript(workspace_dir, store_key, thread_id)?
+ find_head_transcript(store, thread_id)?
.ok_or_else(|| format!("thread {thread_id} has no session transcript"))
}
-/// The key the thread's session transcripts are stored under: the configured
-/// chat agent's id, derived from the config alone. It deliberately reads no
-/// ambient tenant or context (those accessors are being removed); the key is
-/// passed down explicitly to every helper below.
-async fn transcript_store_key() -> String {
- match crate::config::Config::load_or_init().await {
- Ok(config) => crate::web_chat::pick_target_agent_id(&config),
- Err(error) => {
- log::warn!("[threads][edit] config unavailable for the chat agent id: {error}");
- "orchestrator".to_string()
- }
- }
-}
-
type HeadTranscript = (
SessionRef,
std::sync::Arc<dyn TranscriptLocator>,
@@ -282,14 +263,13 @@ type HeadTranscript = (
/// [`resolve_head_transcript`], answering `Ok(None)` when the thread has never
/// written a session transcript. Read and resolve failures still error.
fn find_head_transcript(
- workspace_dir: &std::path::Path,
- store_key: &str,
+ store: &TranscriptStore,
thread_id: &str,
) -> Result<Option<HeadTranscript>, String> {
// The same store the session host writes the thread's turns to: the
- // installed provider, else the workspace's driver-backed store. No
- // transcript file is named here.
- let locator = crate::agent::session_store::transcripts_or_files(store_key, workspace_dir);
+ // installed provider, else the workspace's driver-backed store, under the
+ // key the caller resolved from the request. No transcript file is named.
+ let locator = store.locator();
let Some(root) = locator.root_for_thread(thread_id) else {
log::debug!("[threads][edit] thread {thread_id} has no session transcript");
return Ok(None);
@@ -331,12 +311,11 @@ fn truncation_seed(head: &SessionRef, current: &TranscriptMeta) -> TranscriptMet
/// everything after — backs `edit_message`'s correlation-through-next-reply
/// path.
fn truncate_transcript_before_turn(
- workspace_dir: &std::path::Path,
- store_key: &str,
+ store: &TranscriptStore,
thread_id: &str,
request_id: &str,
) -> Result<(), String> {
- let (head, locator, transcript) = resolve_head_transcript(workspace_dir, store_key, thread_id)?;
+ let (head, locator, transcript) = resolve_head_transcript(store, thread_id)?;
let cut_index = transcript
.messages
.iter()
@@ -356,14 +335,13 @@ fn truncate_transcript_before_turn(
/// dropped turn's `request_id`, or `Ok(None)` when the thread has no turn to
/// regenerate (no transcript yet, or an empty one).
fn truncate_transcript_for_regenerate(
- workspace_dir: &std::path::Path,
- store_key: &str,
+ store: &TranscriptStore,
thread_id: &str,
target_request_id: Option<&str>,
) -> Result<Option<(String, String)>, String> {
// Sentry TAURI-REACT-AK: a thread that never wrote a transcript has no turn
// to regenerate — the caller turns `None` into that friendly error.
- let Some((head, locator, transcript)) = find_head_transcript(workspace_dir, store_key, thread_id)? else {
+ let Some((head, locator, transcript)) = find_head_transcript(store, thread_id)? else {
return Ok(None);
};
let cut = match target_request_id {