Skip to content

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

feat(threads): generate thread titles from first message

Steven Enamakeld9f576d8925d
diff --git a/crates/openhuman-core/src/threads/ops/title_generation.rs b/crates/openhuman-core/src/threads/ops/title_generation.rs
index c9c1925325..6d26b465c1 100644
--- a/crates/openhuman-core/src/threads/ops/title_generation.rs
+++ b/crates/openhuman-core/src/threads/ops/title_generation.rs
@@ -23,15 +23,23 @@ use tinyagents_harness::title::{
/// is written the moment the user sends — before any reply exists to summarize
/// — so without this the real summary that runs on the finished reply found a
/// non-placeholder title and never ran. Anything else was typed by the user
-/// and is left alone. A user who renames a thread to exactly the interim title
-/// gets it summarized once more, which is harmless.
-pub(crate) fn is_replaceable_title(title: &str, first_user_message: Option<&str>) -> bool {
+/// and is left alone.
+///
+/// The interim title is only replaceable during the first exchange
+/// (`agent_replies <= 1`). A summary that happens to equal the interim title
+/// would otherwise look interim forever and be re-summarized on every turn.
+pub(crate) fn is_replaceable_title(
+ title: &str,
+ first_user_message: Option<&str>,
+ agent_replies: usize,
+) -> bool {
if is_auto_generated_thread_title(title) {
return true;
}
- first_user_message
- .and_then(title_from_user_message)
- .is_some_and(|interim| interim == title.trim())
+ agent_replies <= 1
+ && first_user_message
+ .and_then(title_from_user_message)
+ .is_some_and(|interim| interim == title.trim())
}
/// Generates a durable thread title from the first user message and assistant reply.
@@ -57,7 +65,11 @@ pub async fn thread_generate_title(
.find(|message| message.sender == "user" && !message.content.trim().is_empty())
.map(|message| message.content.trim().to_string());
- if !is_replaceable_title(&thread.title, first_user_message.as_deref()) {
+ let agent_replies = messages
+ .iter()
+ .filter(|message| message.sender == "agent")
+ .count();
+ if !is_replaceable_title(&thread.title, first_user_message.as_deref(), agent_replies) {
tracing::debug!(
thread_id = %request.thread_id,
title_len = thread.title.chars().count(),
@@ -208,3 +220,7 @@ pub async fn thread_generate_title(
None,
))
}
+
+#[cfg(test)]
+#[path = "title_generation_tests.rs"]
+mod tests;
diff --git a/crates/openhuman-core/src/threads/ops/title_generation_tests.rs b/crates/openhuman-core/src/threads/ops/title_generation_tests.rs
new file mode 100644
index 0000000000..cced55e131
--- /dev/null
+++ b/crates/openhuman-core/src/threads/ops/title_generation_tests.rs
@@ -0,0 +1,29 @@
+use super::*;
+
+#[test]
+fn placeholder_title_is_always_replaceable() {
+ assert!(is_replaceable_title("Chat Oct 6 3:04 PM", None, 0));
+ assert!(is_replaceable_title("Chat Oct 6 3:04 PM", Some("hi"), 5));
+}
+
+#[test]
+fn interim_title_from_first_message_is_replaceable_during_first_exchange() {
+ let message = "Can you retrieve my latest 5 emails and summarize them?";
+ let interim = title_from_user_message(message).expect("interim title");
+ assert!(is_replaceable_title(&interim, Some(message), 0));
+ assert!(is_replaceable_title(&interim, Some(message), 1));
+}
+
+#[test]
+fn interim_title_is_kept_after_the_first_exchange() {
+ let message = "Can you retrieve my latest 5 emails and summarize them?";
+ let interim = title_from_user_message(message).expect("interim title");
+ assert!(!is_replaceable_title(&interim, Some(message), 2));
+}
+
+#[test]
+fn user_chosen_title_is_never_replaced() {
+ let message = "Can you retrieve my latest 5 emails and summarize them?";
+ assert!(!is_replaceable_title("Inbox triage", Some(message), 0));
+ assert!(!is_replaceable_title("Inbox triage", None, 0));
+}