Skip to content

armgit / kwant

Public Imported from GitHub
0

Your own team of AI bots in one self-hosted binary: spaces, every account you have, handoffs, approvals and routines. Open-source, self-hosted alternative to Grok Bot, Claude Duet and OpenAI dots. Rust + React.

Send the person's current message to the model once

Lin (kwant bot)0e60c022a0b2
diff --git a/src/drivers/mod.rs b/src/drivers/mod.rs
index b252bc3e..12d5afc4 100644
--- a/src/drivers/mod.rs
+++ b/src/drivers/mod.rs
@@ -84,6 +84,16 @@ pub struct Teammate {
}
/// Models only know their training cutoff, so every turn is told the real date and how to use it.
+/// Drops the newest history message when it is the person's message this turn answers: callers
+/// save it before reading history and then pass it again as the prompt, so the model saw it twice.
+pub fn drop_current_message(history: &mut Vec<crate::db::models::Message>, prompt: &str) {
+ let Some(last) = history.last() else { return };
+ let text = last.text.trim();
+ if last.sender_role == "user" && !text.is_empty() && prompt.trim_start().starts_with(text) {
+ history.pop();
+ }
+}
+
/// Frames text another bot wrote, wherever it reaches a bot (history, handoffs, delegated tasks
/// and their results), so it is never mistaken for the person's own words or authority.
/// The frame's markers can't be forged from inside: any "[Message from" / "[End of message" in the
@@ -585,6 +595,44 @@ pub fn create_driver_for_kwant(kwant: &Kwant) -> Box<dyn Driver> {
#[cfg(test)]
mod tests {
+ fn said(role: &str, text: &str) -> crate::db::models::Message {
+ crate::db::models::Message {
+ id: text.into(),
+ channel_id: None,
+ direct_kwant_id: None,
+ sender_role: role.into(),
+ sender_id: "u".into(),
+ text: text.into(),
+ reasoning_content: None,
+ tool_calls: None,
+ tokens_used: 0,
+ created_at: None,
+ thread_id: None,
+ meta: None,
+ }
+ }
+
+ #[test]
+ fn the_current_message_is_not_repeated_in_history() {
+ let mut h = vec![said("user", "hi"), said("kwant", "hello"), said("user", "plan my week")];
+ drop_current_message(&mut h, "plan my week\n\n[Files attached: a.pdf]");
+ assert_eq!(h.iter().map(|m| m.text.as_str()).collect::<Vec<_>>(), ["hi", "hello"]);
+ // Only the newest, only the person's, only when the prompt starts with it
+ for (last, prompt) in [("kwant", "hello again"), ("user", "something else")] {
+ let mut h = vec![said("user", "hi"), said(last, "hello")];
+ if last == "user" {
+ h[1].text = "plan".into();
+ }
+ drop_current_message(&mut h, prompt);
+ assert_eq!(h.len(), 2, "{last} {prompt}");
+ }
+ let mut h = vec![said("user", " ")];
+ drop_current_message(&mut h, "anything");
+ assert_eq!(h.len(), 1);
+ let mut empty = Vec::new();
+ drop_current_message(&mut empty, "x");
+ }
+
use super::*;
fn input() -> TurnInput {
diff --git a/src/server/routes.rs b/src/server/routes.rs
index 0669ffe1..ed609eeb 100644
--- a/src/server/routes.rs
+++ b/src/server/routes.rs
@@ -1623,7 +1623,8 @@ async fn run_kwant_turn_inner(
let context_id = tag.context_id.clone();
- let history = db.list_messages(&context_id, 20).await.unwrap_or_default();
+ let mut history = db.list_messages(&context_id, 20).await.unwrap_or_default();
+ crate::drivers::drop_current_message(&mut history, &prompt);
let configured_key = get_configured_key(&db, &target_model).await;
let endpoint = get_configured_endpoint(&db, &target_model).await;
diff --git a/tests/current_message_test.rs b/tests/current_message_test.rs
new file mode 100644
index 00000000..a59c05c7
--- /dev/null
+++ b/tests/current_message_test.rs
@@ -0,0 +1,23 @@
+//! The person's message reaches the model once: as this turn's request, not also as the last line
+//! of the history before it.
+
+mod common;
+
+use common::*;
+use serde_json::json;
+
+#[tokio::test]
+async fn the_current_message_reaches_the_model_once() {
+ let env = setup().await;
+ send_direct(&env, &env.a, "First ask EARLIER_ASK_41 REPLYTAG:dup1", json!({})).await;
+ wait_reply(&env, &env.a, "answer dup1").await;
+ send_direct(&env, &env.a, "Second ask CURRENT_ASK_42 REPLYTAG:dup2", json!({})).await;
+ wait_reply(&env, &env.a, "answer dup2").await;
+
+ let prompts = invocations_with("CURRENT_ASK_42");
+ assert_eq!(prompts.len(), 1, "{prompts:?}");
+ let p = &prompts[0];
+ assert_eq!(p.matches("CURRENT_ASK_42").count(), 1, "{p}");
+ // The earlier exchange is still there as history
+ assert!(p.contains("EARLIER_ASK_41") && p.contains("answer dup1"), "{p}");
+}