From 7b70ca8899199aea52a4348f858658c63608aadc Mon Sep 17 00:00:00 2001 From: Eureka Date: Wed, 11 Nov 2026 17:36:20 +0000 Subject: [PATCH] chat: huginn chat search CLI and render `huginn chat search [TEXT]... [--room R]... [--after T] [--before T] [--top-k N] [--json]`; T is an RFC 3339 instant or an age (6h, 2d), one parser. The render prints the organ's header line per hit and each message verbatim with its event id, and a truncated listing ends with the --after that continues it. chrono moves from dev-dependency to dependency of eureka-state (already in the lock graph). Co-Authored-By: Claude Sonnet 5.5 --- crates/eureka-state/Cargo.toml | 2 +- crates/eureka-state/src/bin/huginn/chat.rs | 54 +++++++++++++++ crates/eureka-state/src/bin/huginn/main.rs | 70 +++++++++++++++++++- crates/eureka-state/src/bin/huginn/render.rs | 36 +++++++++- crates/eureka-state/tests/cli.rs | 49 +++++++++++++- docs/chat-retrieval-map.md | 13 ++++ 6 files changed, 220 insertions(+), 4 deletions(-) create mode 100644 crates/eureka-state/src/bin/huginn/chat.rs diff --git a/crates/eureka-state/Cargo.toml b/crates/eureka-state/Cargo.toml index b138f56..a84aee6 100644 --- a/crates/eureka-state/Cargo.toml +++ b/crates/eureka-state/Cargo.toml @@ -32,6 +32,7 @@ required-features = ["cli"] [dependencies] anyhow = "1" +chrono = "0.4.44" clap = { version = "4", features = ["derive"], optional = true } cultnet-rs.workspace = true huginn-mind = { path = "../huginn-mind" } @@ -48,6 +49,5 @@ unicode-general-category = "1" # Tests run a real daemon in-process against a temporary mind, and the real # binary against it. [dev-dependencies] -chrono = "0.4.44" huginn-daemon = { path = "../huginn-daemon" } tempfile = "3" diff --git a/crates/eureka-state/src/bin/huginn/chat.rs b/crates/eureka-state/src/bin/huginn/chat.rs new file mode 100644 index 0000000..a8ada81 --- /dev/null +++ b/crates/eureka-state/src/bin/huginn/chat.rs @@ -0,0 +1,54 @@ +//! The `huginn chat` verbs' arguments: one instant parser for `--after` and +//! `--before`. Display and the request are in `main` and `render`. + +use chrono::{DateTime, SecondsFormat, Utc}; + +use crate::trouble::Trouble; + +/// An instant as Unix milliseconds: an RFC 3339 time (`2026-11-11T10:00:00Z`) +/// or an age before `now` (`90s`, `15m`, `6h`, `2d`, `1w`). The one parse of +/// both flags. +pub fn instant_ms(flag: &str, text: &str, now: DateTime) -> Result { + let wrong = || Trouble::invalid(format!("{flag} is an RFC 3339 time or an age such as 6h or 2d, not `{text}`")); + let moment = if let Ok(time) = DateTime::parse_from_rfc3339(text) { + time.with_timezone(&Utc) + } else { + let (digits, unit) = text.split_at(text.len().saturating_sub(1)); + let count: i64 = digits.parse().map_err(|_| wrong())?; + let seconds = match unit { + "s" => 1, + "m" => 60, + "h" => 3600, + "d" => 86_400, + "w" => 604_800, + _ => return Err(wrong()), + }; + now - chrono::Duration::try_seconds(count.checked_mul(seconds).ok_or_else(wrong)?).ok_or_else(wrong)? + }; + u64::try_from(moment.timestamp_millis()).map_err(|_| wrong()) +} + +/// An instant as the CLI prints it. +pub fn shown(ms: u64) -> String { + DateTime::from_timestamp_millis(i64::try_from(ms).unwrap_or(i64::MAX)) + .map_or_else(|| ms.to_string(), |time| time.to_rfc3339_opts(SecondsFormat::Millis, true)) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn an_instant_is_rfc3339_or_an_age_before_now() { + let now = DateTime::parse_from_rfc3339("2026-11-11T12:00:00Z").unwrap().with_timezone(&Utc); + let at = |text: &str| instant_ms("--after", text, now).map(shown); + assert_eq!(at("2026-11-11T10:00:00Z").unwrap(), "2026-11-11T10:00:00.000Z"); + assert_eq!(at("2026-11-11T12:00:00+02:00").unwrap(), "2026-11-11T10:00:00.000Z"); + assert_eq!(at("6h").unwrap(), "2026-11-11T06:00:00.000Z"); + assert_eq!(at("2d").unwrap(), "2026-11-09T12:00:00.000Z"); + assert_eq!(at("90s").unwrap(), "2026-11-11T11:58:30.000Z"); + for bad in ["", "h", "6", "6x", "-2d", "yesterday", "1e3h"] { + assert!(matches!(at(bad), Err(Trouble::InvalidInput(ref detail)) if detail.contains("--after")), "{bad}"); + } + } +} diff --git a/crates/eureka-state/src/bin/huginn/main.rs b/crates/eureka-state/src/bin/huginn/main.rs index 2f7137c..f448e32 100644 --- a/crates/eureka-state/src/bin/huginn/main.rs +++ b/crates/eureka-state/src/bin/huginn/main.rs @@ -17,6 +17,7 @@ //! exits 1 when the campaign is not moved to this mind or this mind holds records of its own under it, and 4 when the history cannot be replayed whole. mod admit_hold; +mod chat; mod config; mod door; mod import; @@ -40,7 +41,7 @@ use huginn_mind::eureka_pipeline::{ Line, PipelineDocument, PipelineRef, Short, Slug, pipeline_key, }; use huginn_mind::{ - BATCH_MAX, Faculty, HuginnMindRequest, HuginnMindResponse, MindRefusal, PipelineAdmissionBatch, PipelineAdmissionOutcome, + BATCH_MAX, ChatQuery, Faculty, HuginnMindRequest, HuginnMindResponse, MindRefusal, PipelineAdmissionBatch, PipelineAdmissionOutcome, PersonaPutOutcome, PipelinePageItems, PipelineProvenance, PipelineSelectionPage, SemanticQuery, SourceQuery, }; use serde_json::Value; @@ -71,6 +72,34 @@ struct Cli { command: Command, } +#[derive(Subcommand, Debug)] +enum ChatCommand { + /// Chunks of the joined rooms (never DMs or Discord), each citing its room, + /// event ids and times. With texts, ranked by meaning; with none, a listing + /// oldest first, at most --top-k, ending in the `--after` to continue from. + Search { + /// Up to eight topics to find; the ranked lists of several are fused. + #[arg(num_args = 0..=8)] + texts: Vec, + /// Only this room: an id (`!abc:gamecult.org`) or a canonical alias + /// (`#swarm:gamecult.org`). Repeat for several. + #[arg(long = "room")] + rooms: Vec, + /// From this instant: an RFC 3339 time, or an age such as 6h or 2d. + #[arg(long)] + after: Option, + /// Until this instant, as --after. + #[arg(long)] + before: Option, + /// How many chunks (default 10, at most 50). + #[arg(long)] + top_k: Option, + /// Print the answer as JSON. + #[arg(long)] + json: bool, + }, +} + #[derive(Subcommand, Debug)] enum Command { /// Who this process acts for and whether the daemon answers. @@ -118,6 +147,11 @@ enum Command { #[arg(long)] json: bool, }, + /// Search the Minds' chat rooms. + Chat { + #[command(subcommand)] + command: ChatCommand, + }, #[command(flatten)] Recipe(Recipe), /// Admit a batch of documents, whole or not at all, and read it back. @@ -284,6 +318,9 @@ fn run(settings: &Settings, command: Command) -> Result { top_k, json, } => sources(settings, texts, repo, front, top_k, json), + Command::Chat { command: ChatCommand::Search { texts, rooms, after, before, top_k, json } } => { + chat_search(settings, texts, rooms, after.as_deref(), before.as_deref(), top_k, json) + } Command::Recipe(Recipe::Blocked { spec_id, page }) => { blocked(settings, &spec_id, page.cursor, page.documents) } @@ -471,6 +508,37 @@ fn sources( } } +fn chat_search( + settings: &Settings, + texts: Vec, + rooms: Vec, + after: Option<&str>, + before: Option<&str>, + top_k: Option, + json: bool, +) -> Result { + let now = chrono::Utc::now(); + let query = ChatQuery { + texts: texts.into_iter().map(Line).collect(), + rooms: rooms.into_iter().map(Short).collect(), + after_ms: after.map(|text| chat::instant_ms("--after", text, now)).transpose()?, + before_ms: before.map(|text| chat::instant_ms("--before", text, now)).transpose()?, + top_k: top_k.unwrap_or(DEFAULT_TOP_K), + }; + match settings.ask(|instance| HuginnMindRequest::SearchChat { instance: instance.clone(), query })? { + HuginnMindResponse::Chat(found) => { + if json { + say(serde_json::to_string_pretty(&found).map_err(|error| Trouble::Internal(error.to_string()))?); + } else { + say(render::chat_hits(&found)); + } + Ok(0) + } + HuginnMindResponse::Refused(refusal) => Ok(refused(&refusal)), + other => Err(unexpected(&other)), + } +} + fn show(page: &PipelineSelectionPage) { match &page.items { PipelinePageItems::Headers(headers) => { diff --git a/crates/eureka-state/src/bin/huginn/render.rs b/crates/eureka-state/src/bin/huginn/render.rs index 68e5f7a..5c5db1c 100644 --- a/crates/eureka-state/src/bin/huginn/render.rs +++ b/crates/eureka-state/src/bin/huginn/render.rs @@ -3,7 +3,7 @@ use std::fmt::Write; use huginn_mind::eureka_pipeline::{PipelineResolution, ResolutionOutcome}; -use huginn_mind::{SourceHits, IndexStatus, PersonaStatus, PipelineDocumentSummary, PipelineDocumentView, PipelineStatus, PipelineStatusSummary}; +use huginn_mind::{ChatHits, SourceHits, IndexStatus, PersonaStatus, PipelineDocumentSummary, PipelineDocumentView, PipelineStatus, PipelineStatusSummary}; use serde::Serialize; use serde_json::Value; @@ -147,6 +147,40 @@ pub fn source_hits(found: &SourceHits) -> String { ") } +/// Chat hits as the CLI prints them. Per hit the organ's own header line, +/// `#alias (!room) first..last time..time score` (the alias and the score only +/// when there are some), then one `time sender event_id body` line per message, +/// the body exactly as the room held it: quoted data under our header, never a +/// message framed as if sent to the reader. A blank line between hits; a +/// truncated listing ends with the `--after` that continues it. +pub fn chat_hits(found: &ChatHits) -> String { + let mut out: Vec = found + .hits + .iter() + .map(|hit| { + let room = hit.room_alias.as_ref().map_or_else(|| hit.room_id.clone(), |alias| format!("{alias} ({})", hit.room_id)); + let score = hit.score.map_or_else(String::new, |score| format!(" {score:.3}")); + let mut text = format!( + "{room} {}..{} {}..{}{score}\n", + hit.first_event_id, + hit.last_event_id, + crate::chat::shown(hit.ts_start_ms), + crate::chat::shown(hit.ts_end_ms) + ); + for message in &hit.messages { + let _ = writeln!(text, "{} {} {} {}", crate::chat::shown(message.ts_ms), message.sender, message.event_id, message.body); + } + text + }) + .collect(); + if found.truncated + && let Some(last) = found.hits.last() + { + out.push(format!("truncated: more follow, continue with --after {}\n", crate::chat::shown(last.ts_end_ms + 1))); + } + out.join("\n") +} + /// A fact as one token: a string, a number, a flag, a reference's id, or a /// list of those joined by commas. Anything deeper is not a header fact. fn scalar(value: &Value) -> Option { diff --git a/crates/eureka-state/tests/cli.rs b/crates/eureka-state/tests/cli.rs index fafa671..0b81629 100644 --- a/crates/eureka-state/tests/cli.rs +++ b/crates/eureka-state/tests/cli.rs @@ -1391,7 +1391,15 @@ fn source_daemon(built_under: &'static str, embeds_under: &'static str) -> Serve chunk("Demo", "README.md", 1, 2, "# Demo"), chunk("Other", "x.rs", 1, 1, "fn other() {}"), ], - chat: Vec::new(), + chat: vec![huginn_daemon::sources::ChatPayload { + room_id: "!r:gamecult.org".into(), + room_alias: Some("#swarm:gamecult.org".into()), + messages: vec![ + huginn_mind::ChatMessage { event_id: "$e1".into(), sender: "@nibu:gamecult.org".into(), ts_ms: 1_700_000_000_000, body: "ship the cut\nthen rest".into() }, + huginn_mind::ChatMessage { event_id: "$e2".into(), sender: "@eureka:gamecult.org".into(), ts_ms: 1_700_000_060_000, body: "done".into() }, + ], + text: "swarm".into(), + }], tickets: 0, done: Vec::new(), }; @@ -2927,3 +2935,42 @@ fn a_malformed_huginn_session_is_refused_naming_the_variable_without_echoing_it( let edge = with_session(live.addr, &"E".repeat(200), &admit_without_session(&file), None); assert_eq!(edge.code, 0, "{}", edge.err); } + +/// `huginn chat search` end to end: a listing through the daemon's worker path +/// prints the organ's header line and each message verbatim with its event id, +/// a room that holds nothing prints nothing, and the flags reach the request as +/// the instants and rooms they name. +#[test] +fn chat_search_prints_cited_messages_and_sends_its_selectors() { + let server = source_daemon("d1", "d1"); + let found = at(server.addr, &["chat", "search", "--room", "#swarm:gamecult.org", "--after", "2023-11-14T22:00:00Z"], None); + assert_eq!(found.code, 0, "{}", found.err); + assert_eq!( + found.out, + "#swarm:gamecult.org (!r:gamecult.org) $e1..$e2 2023-11-14T22:13:20.000Z..2023-11-14T22:14:20.000Z\n\ + 2023-11-14T22:13:20.000Z @nibu:gamecult.org $e1 ship the cut\nthen rest\n\ + 2023-11-14T22:14:20.000Z @eureka:gamecult.org $e2 done\n\n" + ); + let other = at(server.addr, &["chat", "search", "--room", "!elsewhere:gamecult.org"], None); + assert_eq!((other.code, other.out.trim()), (0, "")); + let by_meaning = at(server.addr, &["chat", "search", "who shipped it", "--top-k", "1"], None); + assert!(by_meaning.out.starts_with("#swarm:gamecult.org (!r:gamecult.org) $e1..$e2 ") && by_meaning.out.lines().next().unwrap().ends_with(" 0.900"), "{}", by_meaning.out); + + let (spy, seen) = replying( + "search_chat", + HuginnMindResponse::Chat(huginn_mind::ChatHits { indexed_under: huginn_mind::SourceModel { name: "m".into(), digest: "d".into() }, truncated: false, hits: vec![] }), + ); + let ran = at(spy.addr, &["chat", "search", "a", "b", "--room", "!x:y", "--room", "#z:y", "--after", "2023-11-14T22:13:20Z", "--before", "2023-11-14T22:14:20Z", "--top-k", "3"], None); + assert_eq!(ran.code, 0, "{}", ran.err); + let HuginnMindRequest::SearchChat { query, .. } = seen.lock().unwrap().remove(0) else { panic!("a chat search") }; + let texts: Vec<&str> = query.texts.iter().map(|text| text.0.as_str()).collect(); + let rooms: Vec<&str> = query.rooms.iter().map(|room| room.0.as_str()).collect(); + assert_eq!((texts, rooms, query.after_ms, query.before_ms, query.top_k), (vec!["a", "b"], vec!["!x:y", "#z:y"], Some(1_700_000_000_000), Some(1_700_000_060_000), 3)); + + assert_ne!(at(spy.addr, &["chat", "search", "--after", "yesterday"], None).code, 0); + assert!(seen.lock().unwrap().is_empty(), "a bad instant never left the CLI"); + for bad in [&["chat", "search", "--room", "swarm"][..], &["chat", "search", "--top-k", "0"]] { + let ran = at(server.addr, bad, None); + assert_eq!(ran.code, 1, "{bad:?} is a refusal by the daemon's door: {}", ran.err); + } +} diff --git a/docs/chat-retrieval-map.md b/docs/chat-retrieval-map.md index 3665cae..7758176 100644 --- a/docs/chat-retrieval-map.md +++ b/docs/chat-retrieval-map.md @@ -144,3 +144,16 @@ What changed since the map was written: (`gamecult-chat:ruling:chat-index-minds-rooms`), Discord never. - A Mind that must remember DM content writes a memory citing the chat or room id as its source; the index is not where DM content is kept. + +## Status: read side (cut chat-search) + +Built: `huginn chat search [TEXT]... [--room R]... [--after T] [--before T] [--top-k N] [--json]` +over request `SearchChat` (`sources::search_chat`), listing oldest first without +texts and ranking by meaning with them. The chat point's shape is `ChatPayload` +(`sources/mod.rs`); every point carries `corpus` (`forge` or `chat`) and +`SourcesMeta.corpus` is `forge:GameCult+chat:rooms`. Not built: the ingest job +that writes chat points through `upsert_chat`, and the event-id window. Until the +ingest lands the collection holds no chat points and a chat search answers empty. +In a listing across rooms, the last hit's `ts_end_ms + 1` can repeat messages of +an earlier-starting longer chunk of another room; within one room it continues +exactly.