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:
@@ -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
|
/// Streams a reply from Hermes' OpenAI-compatible API, pushing text deltas
|
||||||
/// into `tx` as they arrive.
|
/// into `tx` as they arrive.
|
||||||
///
|
///
|
||||||
/// /v1/chat/completions with client-side history, not /v1/responses with
|
/// /v1/chat/completions, not /v1/responses: this shape is the one every OpenAI
|
||||||
/// previous_response_id: the Responses API keeps the transcript server-side,
|
/// client exercises, so it is the least likely to differ between Hermes
|
||||||
/// which is nicer, but this shape is the one every OpenAI client exercises so
|
/// versions. With `session.id` set Hermes keeps the transcript itself.
|
||||||
/// it is the one least likely to differ between Hermes versions. Her memory
|
|
||||||
/// and skills are unaffected either way — those are not conversation state.
|
|
||||||
pub async fn stream_reply(
|
pub async fn stream_reply(
|
||||||
client: &reqwest::Client,
|
client: &reqwest::Client,
|
||||||
base_url: &str,
|
base_url: &str,
|
||||||
key: &str,
|
key: &str,
|
||||||
model: &str,
|
model: &str,
|
||||||
history: &[Message],
|
history: &[Message],
|
||||||
|
session: Session<'_>,
|
||||||
tx: mpsc::Sender<String>,
|
tx: mpsc::Sender<String>,
|
||||||
) -> Result<String> {
|
) -> Result<String> {
|
||||||
let body = serde_json::json!({
|
let body = serde_json::json!({
|
||||||
@@ -43,12 +91,17 @@ pub async fn stream_reply(
|
|||||||
"stream": true,
|
"stream": true,
|
||||||
});
|
});
|
||||||
|
|
||||||
let resp = client
|
let mut req = client
|
||||||
.post(format!("{}/v1/chat/completions", base_url.trim_end_matches('/')))
|
.post(format!("{}/v1/chat/completions", base_url.trim_end_matches('/')))
|
||||||
.bearer_auth(key)
|
.bearer_auth(key)
|
||||||
.json(&body)
|
.json(&body);
|
||||||
.send()
|
if let Some(id) = session.id {
|
||||||
.await?;
|
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() {
|
if !resp.status().is_success() {
|
||||||
let status = resp.status();
|
let status = resp.status();
|
||||||
@@ -124,7 +177,35 @@ fn parse_delta(data: &str) -> Option<String> {
|
|||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
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]
|
#[test]
|
||||||
fn error_frame_carries_its_message() {
|
fn error_frame_carries_its_message() {
|
||||||
|
|||||||
@@ -16,6 +16,10 @@ pub struct Config {
|
|||||||
/// system prompt is this client's opinion, while the marker survives into
|
/// system prompt is this client's opinion, while the marker survives into
|
||||||
/// whatever history Hermes keeps and still identifies the channel later.
|
/// whatever history Hermes keeps and still identifies the channel later.
|
||||||
pub voice_marker: Option<String>,
|
pub voice_marker: Option<String>,
|
||||||
|
/// 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<String>,
|
||||||
|
|
||||||
pub eleven_key: String,
|
pub eleven_key: String,
|
||||||
pub voice_id: String,
|
pub voice_id: String,
|
||||||
@@ -66,6 +70,13 @@ impl Config {
|
|||||||
m if m.is_empty() => None,
|
m if m.is_empty() => None,
|
||||||
m => Some(m),
|
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")?,
|
eleven_key: secret("LUNA_ELEVENLABS_KEY_FILE")?,
|
||||||
voice_id: env("LUNA_VOICE_ID")?,
|
voice_id: env("LUNA_VOICE_ID")?,
|
||||||
|
|||||||
+43
-11
@@ -191,9 +191,9 @@ async fn daemon() -> Result<()> {
|
|||||||
.timeout(Duration::from_secs(300))
|
.timeout(Duration::from_secs(300))
|
||||||
.build()?;
|
.build()?;
|
||||||
|
|
||||||
let mut history: Vec<brain::Message> = Vec::new();
|
let mut conv = Conversation::default();
|
||||||
if let Some(prompt) = &cfg.system_prompt {
|
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" });
|
ipc.emit(Event::State { value: "idle" });
|
||||||
@@ -210,7 +210,9 @@ async fn daemon() -> Result<()> {
|
|||||||
let (tx, mut rx) = mpsc::channel(4);
|
let (tx, mut rx) = mpsc::channel(4);
|
||||||
tokio::spawn(async move { while rx.recv().await.is_some() {} });
|
tokio::spawn(async move { while rx.recv().await.is_some() {} });
|
||||||
let warm = [brain::Message::user("hi")];
|
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"),
|
Ok(_) => eprintln!("luna-voice: warmed up"),
|
||||||
Err(e) => eprintln!("luna-voice: warmup skipped: {e}"),
|
Err(e) => eprintln!("luna-voice: warmup skipped: {e}"),
|
||||||
}
|
}
|
||||||
@@ -274,7 +276,7 @@ async fn daemon() -> Result<()> {
|
|||||||
Ok(heard) if !heard.is_empty() => {
|
Ok(heard) if !heard.is_empty() => {
|
||||||
run_turn(
|
run_turn(
|
||||||
&cfg, &http, &ipc, &capture, &playback, &mut cmd_rx,
|
&cfg, &http, &ipc, &capture, &playback, &mut cmd_rx,
|
||||||
&requeue, &mut history, heard,
|
&requeue, &mut conv, heard,
|
||||||
)
|
)
|
||||||
.await;
|
.await;
|
||||||
}
|
}
|
||||||
@@ -356,12 +358,12 @@ async fn daemon() -> Result<()> {
|
|||||||
continue;
|
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;
|
.await;
|
||||||
}
|
}
|
||||||
Command::Say { text } => {
|
Command::Say { text } => {
|
||||||
ipc.emit(Event::State { value: "thinking" });
|
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;
|
.await;
|
||||||
}
|
}
|
||||||
Command::Cancel => {
|
Command::Cancel => {
|
||||||
@@ -416,6 +418,16 @@ mod tests {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[allow(clippy::too_many_arguments)]
|
#[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<brain::Message>,
|
||||||
|
session_id: Option<String>,
|
||||||
|
}
|
||||||
|
|
||||||
async fn run_turn(
|
async fn run_turn(
|
||||||
cfg: &Config,
|
cfg: &Config,
|
||||||
http: &reqwest::Client,
|
http: &reqwest::Client,
|
||||||
@@ -424,10 +436,10 @@ async fn run_turn(
|
|||||||
playback: &Arc<audio::Playback>,
|
playback: &Arc<audio::Playback>,
|
||||||
cmd_rx: &mut mpsc::Receiver<Command>,
|
cmd_rx: &mut mpsc::Receiver<Command>,
|
||||||
requeue: &mpsc::Sender<Command>,
|
requeue: &mpsc::Sender<Command>,
|
||||||
history: &mut Vec<brain::Message>,
|
conv: &mut Conversation,
|
||||||
heard: String,
|
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:#}");
|
eprintln!("luna-voice: {e:#}");
|
||||||
ipc.emit(Event::Error { message: e.to_string() });
|
ipc.emit(Event::Error { message: e.to_string() });
|
||||||
notify(&e.to_string());
|
notify(&e.to_string());
|
||||||
@@ -444,9 +456,10 @@ async fn turn(
|
|||||||
playback: &Arc<audio::Playback>,
|
playback: &Arc<audio::Playback>,
|
||||||
cmd_rx: &mut mpsc::Receiver<Command>,
|
cmd_rx: &mut mpsc::Receiver<Command>,
|
||||||
requeue: &mpsc::Sender<Command>,
|
requeue: &mpsc::Sender<Command>,
|
||||||
history: &mut Vec<brain::Message>,
|
conv: &mut Conversation,
|
||||||
heard: String,
|
heard: String,
|
||||||
) -> Result<()> {
|
) -> Result<()> {
|
||||||
|
let history = &mut conv.history;
|
||||||
eprintln!("luna-voice: heard {heard:?}");
|
eprintln!("luna-voice: heard {heard:?}");
|
||||||
// The caption shows what you SAID; the marker is only for her.
|
// The caption shows what you SAID; the marker is only for her.
|
||||||
ipc.emit(Event::Caption { speaker: "user", text: heard.clone() });
|
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::<String>(64);
|
let (delta_tx, mut delta_rx) = mpsc::channel::<String>(64);
|
||||||
let reply = tokio::spawn({
|
let reply = tokio::spawn({
|
||||||
let http = http.clone();
|
let http = http.clone();
|
||||||
let (url, key, model) = (cfg.hermes_url.clone(), cfg.hermes_key.clone(), cfg.model.clone());
|
let (url, key, model) = (cfg.hermes_url.clone(), cfg.hermes_key.clone(), cfg.model.clone());
|
||||||
let history = history.clone();
|
let messages = brain::outgoing(history, conv.session_id.is_some());
|
||||||
async move { brain::stream_reply(&http, &url, &key, &model, &history, delta_tx).await }
|
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);
|
let mut splitter = tts::SentenceSplitter::new(keep_tags);
|
||||||
|
|||||||
Reference in New Issue
Block a user