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 <noreply@anthropic.com>
This commit is contained in:
2026-10-02 18:53:12 +02:00
co-authored by Claude Opus 5.5
parent b2a700571f
commit ed2ce8fafc
3 changed files with 145 additions and 21 deletions
+91 -10
View File
@@ -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<String> {
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<Message> {
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<String>,
) -> Result<String> {
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<String> {
#[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() {