Files
homelab/pkgs/luna-voice/src/config.rs
T
darmanandClaude Opus 5.5 ed2ce8fafc 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>
2026-10-02 18:53:12 +02:00

113 lines
4.5 KiB
Rust

use anyhow::{Context, Result};
use std::path::PathBuf;
/// Everything the daemon needs, all from the environment so the NixOS module
/// is the only place that knows paths. Secrets arrive as *_FILE (sops paths),
/// never as values — an env var is readable from /proc for anything of the
/// user's, a 0400 file is not.
pub struct Config {
pub hermes_url: String,
pub hermes_key: String,
pub model: String,
pub system_prompt: Option<String>,
/// Prefixed to every spoken message so luna can tell this channel apart
/// from Telegram or the dashboard. Her own profile on mars keys off it —
/// which is why it is a per-message marker and not a system prompt: a
/// 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<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 voice_id: String,
pub tts_model: String,
pub whisper_bin: String,
pub whisper_model: PathBuf,
pub socket_path: PathBuf,
/// Silence that ends an utterance in hands-free mode. Hermes' own default.
pub silence_ms: u64,
/// Level above the running noise floor that starts one.
pub vad_mult: f32,
/// Speech during playback louder than noise_floor * this counts as barge-in.
pub barge_in_mult: f32,
pub barge_in_grace_ms: u64,
}
fn env(key: &str) -> Result<String> {
std::env::var(key).with_context(|| format!("{key} is not set"))
}
fn env_or(key: &str, default: &str) -> String {
std::env::var(key).unwrap_or_else(|_| default.to_string())
}
/// Reads a secret out of a file and trims it. A trailing newline in a bearer
/// token produces a 401 whose message says nothing about whitespace.
fn secret(key: &str) -> Result<String> {
let path = env(key)?;
let raw = std::fs::read_to_string(&path).with_context(|| format!("reading {path}"))?;
Ok(raw.trim().to_string())
}
impl Config {
pub fn from_env() -> Result<Self> {
let runtime = env_or(
"XDG_RUNTIME_DIR",
&format!("/run/user/{}", unsafe { libc_getuid() }),
);
Ok(Config {
hermes_url: env_or("LUNA_HERMES_URL", "http://mars.orbit.sol:8642"),
hermes_key: secret("LUNA_HERMES_KEY_FILE")?,
model: env_or("LUNA_MODEL", "hermes"),
system_prompt: std::env::var("LUNA_SYSTEM_PROMPT").ok(),
// Empty string disables it rather than sending a bare prefix.
voice_marker: match env_or("LUNA_VOICE_MARKER", "[voice]") {
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")?,
tts_model: env_or("LUNA_TTS_MODEL", "eleven_v3_conversational"),
whisper_bin: env_or("LUNA_WHISPER_BIN", "whisper-cli"),
whisper_model: PathBuf::from(env("LUNA_WHISPER_MODEL")?),
socket_path: PathBuf::from(env_or(
"LUNA_SOCKET",
&format!("{runtime}/luna-voiced.sock"),
)),
silence_ms: env_or("LUNA_SILENCE_MS", "2000").parse().unwrap_or(2000),
// 1.8, not Hermes' 3.0-ish: terra's interface has speech only
// ~6dB over its own ambient (0.0045 against 0.0022 measured), and
// no multiplier above ~2 can separate those. The real fix is gain
// on the interface; this makes it work at the gain it has.
vad_mult: env_or("LUNA_VAD_MULT", "1.8").parse().unwrap_or(1.8),
barge_in_mult: env_or("LUNA_BARGE_IN_MULT", "3.0").parse().unwrap_or(3.0),
barge_in_grace_ms: env_or("LUNA_BARGE_IN_GRACE_MS", "500")
.parse()
.unwrap_or(500),
})
}
}
// Avoids pulling in the libc crate for one call.
unsafe fn libc_getuid() -> u32 {
extern "C" {
fn getuid() -> u32;
}
getuid()
}