Skip to content

Open source Grok Bot alternative, Bring your own agent and subscription: message Claude Code, Codex, Gemini CLI, Cursor, Copilot and 40+ AI agents as bots on your own computer, from iPhone, Mac, Windows, Linux or terminal. Agent-agnostic and free. Also a Muse and Dots alternative. Built in Rust.

feat(host): let bots read their chat back from a message and check a memory's source

leepokai57796b9a026f
diff --git a/host/src/chat/memory/tools.rs b/host/src/chat/memory/tools.rs
index 03a21a4f..f9ca2277 100644
--- a/host/src/chat/memory/tools.rs
+++ b/host/src/chat/memory/tools.rs
@@ -56,7 +56,7 @@ pub fn tools() -> Value {
},
{
"name": "search_memory",
- "description": "Search your memory, including archived entries, for text containing the query (case-insensitive).",
+ "description": "Search your memory, including archived entries, for text containing the query (case-insensitive). Each result's source lists the messages it was learned from: pass one to search_history as from to read it.",
"inputSchema": {
"type": "object",
"properties": {"query": {"type": "string"}},
@@ -66,14 +66,14 @@ pub fn tools() -> Value {
},
{
"name": "search_history",
- "description": "Search your full chat history with the user for messages containing every given word (case-insensitive). Returns dated messages, newest first.",
+ "description": "Read your full chat history with the user, newest first: messages containing every given word (case-insensitive), or without a query, every message. Returns dated messages with ids, and next: pass it as from for the page before.",
"inputSchema": {
"type": "object",
"properties": {
"query": {"type": "string", "description": "Words that must all appear, separated by spaces."},
+ "from": {"type": "string", "description": "A message id (from a result, next, or a memory's source): start at that message and go back."},
"limit": {"type": "integer", "description": "Maximum results (default 10, at most 30)."},
},
- "required": ["query"],
},
"annotations": {"readOnlyHint": true},
},
@@ -129,7 +129,14 @@ async fn search_memory(hub: &Arc<Hub>, cfg: &BotConfig, query: &str) -> Result<V
.into_iter()
.filter(|e| shared || e.bot.is_some())
.map(|e| {
- json!({"id": e.id, "text": e.text, "state": e.state, "shared": e.bot.is_none(), "date": ymd(e.updated_at)})
+ json!({
+ "id": e.id,
+ "text": e.text,
+ "state": e.state,
+ "shared": e.bot.is_none(),
+ "date": ymd(e.updated_at),
+ "source": e.evidence,
+ })
})
.collect();
Ok(json!({"results": results}))
@@ -138,17 +145,32 @@ async fn search_memory(hub: &Arc<Hub>, cfg: &BotConfig, query: &str) -> Result<V
async fn search_history(hub: &Arc<Hub>, cfg: &BotConfig, args: &Value) -> Result<Value> {
let terms: Vec<String> =
args["query"].as_str().unwrap_or_default().split_whitespace().take(SEARCH_TERMS).map(str::to_owned).collect();
- if terms.is_empty() {
- bail!("query must contain at least one word");
- }
+ let from = args["from"].as_str().map(str::to_owned);
let limit = args["limit"].as_i64().unwrap_or(SEARCH_DEFAULT).clamp(1, SEARCH_MAX);
let hub = hub.clone();
- let id = cfg.id.clone();
- let rows = tokio::task::spawn_blocking(move || hub.store.search_messages(&id, &terms, limit)).await??;
+ let bot = cfg.id.clone();
+ let (rows, next) = tokio::task::spawn_blocking(move || -> Result<_> {
+ let from = match from {
+ Some(id) => Some(
+ hub.store
+ .entry(&id)
+ .filter(|e| e.bot_id == bot)
+ .ok_or_else(|| anyhow!("no message {id} in your chat"))?
+ .seq,
+ ),
+ None => None,
+ };
+ // One more than asked tells whether there is a page before.
+ let mut rows = hub.store.search_messages(&bot, &terms, from, limit + 1)?;
+ let next = if rows.len() > usize::try_from(limit)? { rows.pop().map(|e| e.id) } else { None };
+ Ok((rows, next))
+ })
+ .await??;
let results: Vec<Value> = rows
.iter()
.map(|e| {
json!({
+ "id": e.id,
"date": ymd(e.created_at),
"from": if e.kind == crate::store::EntryKind::User.as_str() { "user" } else { "you" },
"inThread": e.thread_id.is_some(),
@@ -156,5 +178,5 @@ async fn search_history(hub: &Arc<Hub>, cfg: &BotConfig, args: &Value) -> Result
})
})
.collect();
- Ok(json!({"results": results}))
+ Ok(json!({"results": results, "next": next}))
}
diff --git a/host/src/store/entries.rs b/host/src/store/entries.rs
index 5ffe14ad..91ef34ac 100644
--- a/host/src/store/entries.rs
+++ b/host/src/store/entries.rs
@@ -153,18 +153,20 @@ impl Store {
}
/// Chat-visible messages (the user's, final replies) in any of `bot_id`'s lanes that
- /// contain every term (case-insensitive for ASCII), newest first.
- // ponytail: substring scan, no index (CJK needs no tokenizer this way); an FTS5 trigram table if it gets slow.
- pub fn search_messages(&self, bot_id: &str, terms: &[String], limit: i64) -> Result<Vec<Entry>> {
+ /// contain every term (case-insensitive for ASCII), newest first, from `from` (a `seq`) back.
+ // ponytail: substring scan, no index (CJK needs no tokenizer this way): 0.6 s over 180k messages of one bot.
+ // An FTS5 trigram table once a bot's search passes a second.
+ pub fn search_messages(&self, bot_id: &str, terms: &[String], from: Option<i64>, limit: i64) -> Result<Vec<Entry>> {
let mut sql = format!(
- "SELECT {ENTRY_COLS} FROM entries WHERE bot_id = ?1 AND
+ "SELECT {ENTRY_COLS} FROM entries WHERE bot_id = ?1 AND seq <= ?3 AND
(kind = 'user' OR (kind = 'agent' AND json_extract(data, '$.final') = 1))"
);
for i in 0..terms.len() {
- let _ = write!(sql, " AND instr(lower(json_extract(data, '$.text')), ?{}) > 0", i + 3);
+ let _ = write!(sql, " AND instr(lower(json_extract(data, '$.text')), ?{}) > 0", i + 4);
}
sql.push_str(" ORDER BY seq DESC LIMIT ?2");
- let mut args: Vec<rusqlite::types::Value> = vec![bot_id.to_owned().into(), limit.into()];
+ let mut args: Vec<rusqlite::types::Value> =
+ vec![bot_id.to_owned().into(), limit.into(), from.unwrap_or(i64::MAX).into()];
args.extend(terms.iter().map(|t| t.to_lowercase().into()));
let c = self.db.locked();
let mut st = c.prepare(&sql)?;
@@ -333,7 +335,7 @@ mod tests {
s.insert_entry(&Lane::main("b2"), EntryKind::User, 1, &json!({"text": "Swift 6 elsewhere"})).unwrap();
let texts = |terms: &[&str]| -> Vec<String> {
let terms: Vec<String> = terms.iter().map(|t| (*t).to_owned()).collect();
- s.search_messages("b1", &terms, 10)
+ s.search_messages("b1", &terms, None, 10)
.unwrap()
.into_iter()
.map(|e| e.data["text"].as_str().unwrap().to_owned())
@@ -344,6 +346,22 @@ mod tests {
assert_eq!(texts(&["narration"]).len(), 0);
}
+ #[test]
+ fn history_search_reads_back_from_a_message() {
+ let s = temp_store();
+ let main = Lane::main("b1");
+ let seqs: Vec<i64> = (1..=4)
+ .map(|i| s.insert_entry(&main, EntryKind::User, 1, &json!({"text": format!("note {i}")})).unwrap().seq)
+ .collect();
+ let texts = |terms: &[String], from| -> Vec<String> {
+ let rows = s.search_messages("b1", terms, from, 2).unwrap();
+ rows.into_iter().map(|e| e.data["text"].as_str().unwrap().to_owned()).collect()
+ };
+ assert_eq!(texts(&[], Some(seqs[2])), ["note 3", "note 2"], "starts at the message itself");
+ assert_eq!(texts(&["1".to_owned()], Some(seqs[2])), ["note 1"]);
+ assert_eq!(texts(&[], None), ["note 4", "note 3"]);
+ }
+
#[test]
fn restart_expires_pending_cards_and_queued_sends() {
let s = temp_store();
diff --git a/host/tests/memory_e2e.rs b/host/tests/memory_e2e.rs
index 140f1437..b1c5229e 100644
--- a/host/tests/memory_e2e.rs
+++ b/host/tests/memory_e2e.rs
@@ -257,6 +257,18 @@ async fn dreaming_keeps_only_what_the_user_typed_and_can_be_undone() {
assert_eq!(entry["evidence"], json!([message]), "it cites the message it learned from");
assert!(!view.to_string().contains("durian"), "pasted text isn't the user's own words");
+ // The bot can go from a memory to the message it came from, and page back through the chat.
+ let found = host.tool(&bot, "search_memory", json!({"query": "concise"})).await;
+ assert_eq!(found["results"][0]["source"], json!([message]));
+ let page = host.tool(&bot, "search_history", json!({"limit": 1})).await;
+ assert_eq!(page["results"][0]["from"], "you");
+ assert_eq!(page["next"], json!(message));
+ let page = host.tool(&bot, "search_history", json!({"from": message})).await;
+ assert_eq!(page["results"][0]["id"], json!(message));
+ assert_eq!(page["next"], Value::Null);
+ let other = host.bot("Other", "auto").await;
+ assert!(host.tool_refused(&other, "search_history", json!({"from": message})).await.contains("no message"));
+
let dream = view["dreams"][0]["id"].as_i64().unwrap();
host.call("undoDream", json!({"botId": bot, "id": dream})).await;
let view = host.memory(&bot).await;