Skip to content

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

refactor(embed): move profile handle into its own module

Steven Enamakel7e82684c0e8a
diff --git a/crates/openhuman-core/src/agent/session_host/runtime_session.rs b/crates/openhuman-core/src/agent/session_host/runtime_session.rs
index 5df931b859..d039d00b79 100644
--- a/crates/openhuman-core/src/agent/session_host/runtime_session.rs
+++ b/crates/openhuman-core/src/agent/session_host/runtime_session.rs
@@ -48,7 +48,6 @@ mod progress;
/// resume cache, or persistence handle. Those are exclusively `Session` state.
#[derive(Default)]
pub(super) struct OpenHumanSessionState {
- last_commit: Option<CommitReceipt<OpenHumanRunContext>>,
terminals: Vec<SessionTerminal>,
pub(super) last_turn_hit_cap: bool,
pub(super) last_turn_usage: Option<crate::agent::tinyagents::host::LastTurnUsage>,
@@ -1286,10 +1285,14 @@ impl OpenHumanSessionHost {
let _ =
progress::send_receipt_progress(&receipt, &input, &output, iterations)
.await;
- state
- .lock()
- .unwrap_or_else(|poisoned| poisoned.into_inner())
- .last_commit = Some(receipt);
+ // Drop the receipt here rather than retaining it on
+ // the session: its run context carries this turn's
+ // progress sender (`progress` and the attached
+ // parent's `on_progress`), and a cached session that
+ // kept it would hold the caller's progress channel
+ // open after `set_on_progress(None)` — the web
+ // channel's bridge task then never exits.
+ drop(receipt);
{
let mut state = state
.lock()
diff --git a/crates/openhuman-embed/examples/profiles.rs b/crates/openhuman-embed/examples/profiles.rs
index fc50c6a75d..33b62795d5 100644
--- a/crates/openhuman-embed/examples/profiles.rs
+++ b/crates/openhuman-embed/examples/profiles.rs
@@ -110,6 +110,9 @@ impl Respond for Echo {
.and_then(|m| m.iter().rev().find(|m| m["role"] == "user"))
.and_then(|m| m["content"].as_str())
.unwrap_or_default();
+ // The core prefixes the turn's message with context lines (the date);
+ // the user's own text is the last line.
+ let said = said.lines().rev().find(|l| !l.trim().is_empty()).unwrap_or_default();
let reply = format!("echo: {said}");
if body["stream"] == true {
let chunk = json!({ "id": "c", "object": "chat.completion.chunk", "created": 0,
diff --git a/crates/openhuman-embed/src/profiles/handle.rs b/crates/openhuman-embed/src/profiles/handle.rs
index 5376ccec9c..81ddfdb496 100644
--- a/crates/openhuman-embed/src/profiles/handle.rs
+++ b/crates/openhuman-embed/src/profiles/handle.rs
@@ -8,7 +8,8 @@ use openhuman_core::core::envelope::ApiEnvelope;
use openhuman_core::core::runtime::CoreContext;
use openhuman_core::profiles::Profile;
use openhuman_core::threads::{
- ConversationMessagesRequest, ConversationMessagesResponse, ConversationThreadsListResponse,
+ AppendConversationMessageRequest, ConversationMessagesRequest, ConversationMessagesResponse,
+ ConversationThreadsListResponse,
};
use serde::de::DeserializeOwned;
use serde::{Deserialize, Serialize};
@@ -22,6 +23,7 @@ use crate::call::call_in;
use crate::error::CoreError;
const THREADS_UPSERT: &str = "openhuman.threads_upsert";
+const THREADS_MESSAGE_APPEND: &str = "openhuman.threads_message_append";
const THREADS_LIST: &str = "openhuman.threads_list";
const THREADS_MESSAGES_LIST: &str = "openhuman.threads_messages_list";
const CHANNEL_WEB_CHAT: &str = "openhuman.channel_web_chat";
@@ -195,11 +197,16 @@ impl ProfileHandle {
/// for the turn's final reply. A thread id is unique per profile: two
/// users' `t1` are two threads.
///
+ /// Like the web app, it records the user's message on the thread itself
+ /// (`threads_message_append`) before starting the turn; the core records
+ /// the reply.
+ ///
/// Waits for as long as the turn runs; wrap it in
/// `tokio::time::timeout` to bound it. For live progress, read
/// [`events`](Self::events) alongside.
pub async fn chat(&self, thread_id: &str, text: &str) -> Result<ChatReply, ProfileError> {
self.ensure_thread(thread_id).await?;
+ self.append_user_message(thread_id, text).await?;
let mut events = self.events();
let accepted: WebChatAccepted = self
.call(
@@ -292,6 +299,27 @@ impl ProfileHandle {
Ok(listed.data.map(|data| data.messages).unwrap_or_default())
}
+ /// Record `text` as the user's message on `thread_id`.
+ async fn append_user_message(&self, thread_id: &str, text: &str) -> Result<(), ProfileError> {
+ let _appended: ApiEnvelope<ConversationMessageRecord> = self
+ .call(
+ THREADS_MESSAGE_APPEND,
+ AppendConversationMessageRequest {
+ thread_id: thread_id.to_string(),
+ message: ConversationMessageRecord {
+ id: format!("msg_{}", uuid::Uuid::new_v4()),
+ content: text.to_string(),
+ message_type: "text".to_string(),
+ extra_metadata: serde_json::json!({}),
+ sender: "user".to_string(),
+ created_at: chrono::Utc::now().to_rfc3339(),
+ },
+ },
+ )
+ .await?;
+ Ok(())
+ }
+
/// Create `thread_id` on this profile unless it exists (`threads_upsert`
/// keeps an existing thread as it is).
async fn ensure_thread(&self, thread_id: &str) -> Result<(), ProfileError> {