From ed2ce8fafc11b70c4f4db434845b28f9f93e5737 Mon Sep 17 00:00:00 2001 From: Erik Simon Date: Fri, 2 Oct 2026 18:53:12 +0200 Subject: [PATCH] feat(terra): stored Hermes sessions for luna's voice - Each daemon run creates a Hermes session (source "desktop") on its first turn, so voice chats show in the dashboard's default Chats view. Hermes only allows a fixed set of sources; unknown ones become the hidden api_server. - Turns carry X-Hermes-Session-Id (Hermes keeps the transcript) and X-Hermes-Session-Key: voice (one Honcho memory session for voice). - With a stored session only the base prompt and what's new since her last reply are sent; falls back to full client-side history if the session can't be created. Co-Authored-By: Claude Opus 5.5 --- pkgs/luna-voice/src/brain.rs | 101 ++++++++++++++++++++++++++++++---- pkgs/luna-voice/src/config.rs | 11 ++++ pkgs/luna-voice/src/main.rs | 54 ++++++++++++++---- 3 files changed, 145 insertions(+), 21 deletions(-) diff --git a/pkgs/luna-voice/src/brain.rs b/pkgs/luna-voice/src/brain.rs index 870625d..9c75080 100644 --- a/pkgs/luna-voice/src/brain.rs +++ b/pkgs/luna-voice/src/brain.rs @@ -21,20 +21,68 @@ impl Message { } } +/// Where a turn lands in Hermes. `id` makes Hermes keep the history (and list +/// the session on the dashboard); `key` scopes long-term memory per channel. +#[derive(Clone, Copy, Default)] +pub struct Session<'a> { + pub id: Option<&'a str>, + pub key: Option<&'a str>, +} + +/// Creates a stored Hermes session. The source is only a dashboard label — +/// Hermes allowlists it, and anything unknown becomes the hidden `api_server`. +pub async fn create_session( + client: &reqwest::Client, + base_url: &str, + key: &str, + source: &str, +) -> Result { + let resp = client + .post(format!("{}/api/sessions", base_url.trim_end_matches('/'))) + .bearer_auth(key) + .json(&serde_json::json!({ "source": source })) + .send() + .await?; + if !resp.status().is_success() { + let status = resp.status(); + let text = resp.text().await.unwrap_or_default(); + bail!("hermes session create {status}: {}", text.trim()); + } + let v: serde_json::Value = resp.json().await?; + match v.pointer("/session/id").and_then(|id| id.as_str()) { + Some(id) => Ok(id.to_string()), + None => bail!("hermes session create: no id in response"), + } +} + +/// What to send for the next turn. With a stored session Hermes loads the +/// history itself and folds every system message into the prompt, so only the +/// base prompt plus what's new since her last reply goes out — resending old +/// notes would repeat them every turn. +pub fn outgoing(history: &[Message], stored: bool) -> Vec { + let Some(last) = history.iter().rposition(|m| m.role == "assistant") else { + return history.to_vec(); + }; + if !stored { + return history.to_vec(); + } + let base = history.first().filter(|m| m.role == "system").cloned(); + base.into_iter().chain(history[last + 1..].iter().cloned()).collect() +} + /// Streams a reply from Hermes' OpenAI-compatible API, pushing text deltas /// into `tx` as they arrive. /// -/// /v1/chat/completions with client-side history, not /v1/responses with -/// previous_response_id: the Responses API keeps the transcript server-side, -/// which is nicer, but this shape is the one every OpenAI client exercises so -/// it is the one least likely to differ between Hermes versions. Her memory -/// and skills are unaffected either way — those are not conversation state. +/// /v1/chat/completions, not /v1/responses: this shape is the one every OpenAI +/// client exercises, so it is the least likely to differ between Hermes +/// versions. With `session.id` set Hermes keeps the transcript itself. pub async fn stream_reply( client: &reqwest::Client, base_url: &str, key: &str, model: &str, history: &[Message], + session: Session<'_>, tx: mpsc::Sender, ) -> Result { let body = serde_json::json!({ @@ -43,12 +91,17 @@ pub async fn stream_reply( "stream": true, }); - let resp = client + let mut req = client .post(format!("{}/v1/chat/completions", base_url.trim_end_matches('/'))) .bearer_auth(key) - .json(&body) - .send() - .await?; + .json(&body); + if let Some(id) = session.id { + req = req.header("X-Hermes-Session-Id", id); + } + if let Some(k) = session.key { + req = req.header("X-Hermes-Session-Key", k); + } + let resp = req.send().await?; if !resp.status().is_success() { let status = resp.status(); @@ -124,7 +177,35 @@ fn parse_delta(data: &str) -> Option { #[cfg(test)] mod tests { - use super::{nonempty, parse_delta, parse_error}; + use super::{nonempty, outgoing, parse_delta, parse_error, Message}; + + fn roles(msgs: &[Message]) -> Vec<(&str, &str)> { + msgs.iter().map(|m| (m.role.as_str(), m.content.as_str())).collect() + } + + #[test] + fn stored_session_sends_prompt_and_only_whats_new() { + let h = vec![ + Message::system("prompt"), + Message::user("one"), + Message::assistant("reply"), + Message::system("[interrupted]"), + Message::user("two"), + ]; + assert_eq!( + roles(&outgoing(&h, true)), + [("system", "prompt"), ("system", "[interrupted]"), ("user", "two")] + ); + assert_eq!(outgoing(&h, false).len(), 5); + } + + #[test] + fn first_turn_sends_everything_either_way() { + let h = vec![Message::system("prompt"), Message::user("one")]; + assert_eq!(outgoing(&h, true).len(), 2); + let bare = vec![Message::user("one")]; + assert_eq!(roles(&outgoing(&bare, true)), [("user", "one")]); + } #[test] fn error_frame_carries_its_message() { diff --git a/pkgs/luna-voice/src/config.rs b/pkgs/luna-voice/src/config.rs index 99923f4..60cc044 100644 --- a/pkgs/luna-voice/src/config.rs +++ b/pkgs/luna-voice/src/config.rs @@ -16,6 +16,10 @@ pub struct Config { /// system prompt is this client's opinion, while the marker survives into /// whatever history Hermes keeps and still identifies the channel later. pub voice_marker: Option, + /// Dashboard label for her stored voice session; empty = no stored session. + pub session_source: String, + /// Memory scope (one Honcho session for all of voice); empty = none. + pub session_key: Option, pub eleven_key: String, pub voice_id: String, @@ -66,6 +70,13 @@ impl Config { m if m.is_empty() => None, m => Some(m), }, + // desktop, not a "voice" source: Hermes only allows a fixed set, and + // its default dashboard view hides api_server. + session_source: env_or("LUNA_SESSION_SOURCE", "desktop"), + session_key: match env_or("LUNA_SESSION_KEY", "voice") { + k if k.is_empty() => None, + k => Some(k), + }, eleven_key: secret("LUNA_ELEVENLABS_KEY_FILE")?, voice_id: env("LUNA_VOICE_ID")?, diff --git a/pkgs/luna-voice/src/main.rs b/pkgs/luna-voice/src/main.rs index c2fb57f..e0d1138 100644 --- a/pkgs/luna-voice/src/main.rs +++ b/pkgs/luna-voice/src/main.rs @@ -191,9 +191,9 @@ async fn daemon() -> Result<()> { .timeout(Duration::from_secs(300)) .build()?; - let mut history: Vec = Vec::new(); + let mut conv = Conversation::default(); if let Some(prompt) = &cfg.system_prompt { - history.push(brain::Message::system(prompt.clone())); + conv.history.push(brain::Message::system(prompt.clone())); } ipc.emit(Event::State { value: "idle" }); @@ -210,7 +210,9 @@ async fn daemon() -> Result<()> { let (tx, mut rx) = mpsc::channel(4); tokio::spawn(async move { while rx.recv().await.is_some() {} }); let warm = [brain::Message::user("hi")]; - match brain::stream_reply(&http, &url, &key, &model, &warm, tx).await { + // Sessionless on purpose: a stored session would open every + // voice conversation on dashboard with a throwaway "hi". + match brain::stream_reply(&http, &url, &key, &model, &warm, Default::default(), tx).await { Ok(_) => eprintln!("luna-voice: warmed up"), Err(e) => eprintln!("luna-voice: warmup skipped: {e}"), } @@ -274,7 +276,7 @@ async fn daemon() -> Result<()> { Ok(heard) if !heard.is_empty() => { run_turn( &cfg, &http, &ipc, &capture, &playback, &mut cmd_rx, - &requeue, &mut history, heard, + &requeue, &mut conv, heard, ) .await; } @@ -356,12 +358,12 @@ async fn daemon() -> Result<()> { continue; } - run_turn(&cfg, &http, &ipc, &capture, &playback, &mut cmd_rx, &requeue, &mut history, heard) + run_turn(&cfg, &http, &ipc, &capture, &playback, &mut cmd_rx, &requeue, &mut conv, heard) .await; } Command::Say { text } => { ipc.emit(Event::State { value: "thinking" }); - run_turn(&cfg, &http, &ipc, &capture, &playback, &mut cmd_rx, &requeue, &mut history, text) + run_turn(&cfg, &http, &ipc, &capture, &playback, &mut cmd_rx, &requeue, &mut conv, text) .await; } Command::Cancel => { @@ -416,6 +418,16 @@ mod tests { } #[allow(clippy::too_many_arguments)] + +/// One daemon run's conversation. `session_id` is the stored Hermes session +/// that owns the transcript once created; `history` stays as the fallback +/// and as the source of the notes sent alongside each turn. +#[derive(Default)] +struct Conversation { + history: Vec, + session_id: Option, +} + async fn run_turn( cfg: &Config, http: &reqwest::Client, @@ -424,10 +436,10 @@ async fn run_turn( playback: &Arc, cmd_rx: &mut mpsc::Receiver, requeue: &mpsc::Sender, - history: &mut Vec, + conv: &mut Conversation, heard: String, ) { - if let Err(e) = turn(cfg, http, ipc, capture, playback, cmd_rx, requeue, history, heard).await { + if let Err(e) = turn(cfg, http, ipc, capture, playback, cmd_rx, requeue, conv, heard).await { eprintln!("luna-voice: {e:#}"); ipc.emit(Event::Error { message: e.to_string() }); notify(&e.to_string()); @@ -444,9 +456,10 @@ async fn turn( playback: &Arc, cmd_rx: &mut mpsc::Receiver, requeue: &mpsc::Sender, - history: &mut Vec, + conv: &mut Conversation, heard: String, ) -> Result<()> { + let history = &mut conv.history; eprintln!("luna-voice: heard {heard:?}"); // The caption shows what you SAID; the marker is only for her. ipc.emit(Event::Caption { speaker: "user", text: heard.clone() }); @@ -503,12 +516,31 @@ async fn turn( } }); + // Retried every turn until it works; meanwhile she still answers off the + // client-side history, just without a dashboard entry. + if conv.session_id.is_none() && !cfg.session_source.is_empty() { + match brain::create_session(http, &cfg.hermes_url, &cfg.hermes_key, &cfg.session_source).await { + Ok(id) => { + eprintln!("luna-voice: hermes session {id}"); + conv.session_id = Some(id); + } + Err(e) => eprintln!("luna-voice: no stored session, sending full history: {e:#}"), + } + } + let (delta_tx, mut delta_rx) = mpsc::channel::(64); let reply = tokio::spawn({ let http = http.clone(); let (url, key, model) = (cfg.hermes_url.clone(), cfg.hermes_key.clone(), cfg.model.clone()); - let history = history.clone(); - async move { brain::stream_reply(&http, &url, &key, &model, &history, delta_tx).await } + let messages = brain::outgoing(history, conv.session_id.is_some()); + let (session_id, session_key) = (conv.session_id.clone(), cfg.session_key.clone()); + async move { + let session = brain::Session { + id: session_id.as_deref(), + key: session_key.as_deref(), + }; + brain::stream_reply(&http, &url, &key, &model, &messages, session, delta_tx).await + } }); let mut splitter = tts::SentenceSplitter::new(keep_tags);