fix(terra): surface empty Hermes replies, reconnect the voice widget

- luna-voice treats an empty reply or a stream error frame as a failure,
  so a dead provider key notifies instead of speaking "".
- The Luna widget reads `luna-voice watch` instead of Quickshell's
  Socket, which never retries after one failed connect and left the
  widget OFFLINE after a daemon restart.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-10-02 02:23:07 +02:00
co-authored by Claude Opus 5.5
parent a9022fa8fc
commit 48aa11eab5
2 changed files with 65 additions and 22 deletions
@@ -23,7 +23,9 @@ QtObject {
property string state: "idle"
// Voice mode itself — the widget is only on screen while this is true.
property bool active: false
property bool connected: socket.connected
// Set by the first line, not by the process starting: the daemon replays
// its state on connect, so only a live connection produces one.
property bool connected: false
// Mic level while listening, her output level while speaking. Drives the
// only genuinely live part of the visuals.
property real rms: 0
@@ -31,35 +33,39 @@ QtObject {
property string lunaText: ""
property string error: ""
readonly property var socket: Socket {
id: socket
path: `${Quickshell.env("XDG_RUNTIME_DIR") ?? "/run/user/1000"}/luna-voiced.sock`
connected: true
// `luna-voice watch`, not Quickshell's Socket: a Socket whose connect
// fails once (daemon mid-restart) keeps the dead QLocalSocket and ignores
// every later `connected = true`, so the widget stuck on OFFLINE.
readonly property var watch: Process {
id: watch
command: ["luna-voice", "watch"]
running: true
onConnectionStateChanged: {
if (!socket.connected) {
// A daemon that went away is not a daemon that is idle: reset
// rather than leave the last frame of a conversation frozen
// on screen.
root.active = false;
root.state = "idle";
root.rms = 0;
stdout: SplitParser {
splitMarker: "\n"
onRead: line => {
root.connected = true;
root.ingest(line);
}
}
parser: SplitParser {
splitMarker: "\n"
onRead: line => root.ingest(line)
onExited: {
// A daemon that went away is not a daemon that is idle: reset
// rather than leave the last frame of a conversation frozen
// on screen.
root.connected = false;
root.active = false;
root.state = "idle";
root.rms = 0;
}
}
// Retry forever, cheaply. Setting `connected` true on an already-open
// socket is a no-op, so this doubles as the initial connect.
// Retry forever, cheaply; each attempt is a fresh connection.
readonly property var reconnect: Timer {
interval: 2000
running: !socket.connected
running: !watch.running
repeat: true
onTriggered: socket.connected = true
onTriggered: watch.running = true
}
function ingest(line: string): void {
+39 -2
View File
@@ -70,7 +70,10 @@ pub async fn stream_reply(
let Some(data) = line.strip_prefix("data:") else { continue };
let data = data.trim();
if data == "[DONE]" {
return Ok(full);
return nonempty(full);
}
if let Some(msg) = parse_error(data) {
bail!("hermes: {msg}");
}
if let Some(delta) = parse_delta(data) {
full.push_str(&delta);
@@ -82,9 +85,30 @@ pub async fn stream_reply(
}
}
}
nonempty(full)
}
/// Hermes answers a failed upstream call with 200 and an empty stream, so an
/// empty reply is the only sign of e.g. a dead provider key.
fn nonempty(full: String) -> Result<String> {
if full.trim().is_empty() {
bail!("hermes returned an empty reply (check its log on mars)");
}
Ok(full)
}
/// An `{"error": ...}` frame, as OpenAI-compatible servers send mid-stream.
fn parse_error(data: &str) -> Option<String> {
let v: serde_json::Value = serde_json::from_str(data).ok()?;
let err = v.get("error")?;
Some(
err.get("message")
.and_then(|m| m.as_str())
.map(str::to_string)
.unwrap_or_else(|| err.to_string()),
)
}
/// Pulls choices[0].delta.content out of one SSE data payload. Tool-call
/// frames and role-only frames carry no content and are skipped.
fn parse_delta(data: &str) -> Option<String> {
@@ -100,7 +124,20 @@ fn parse_delta(data: &str) -> Option<String> {
#[cfg(test)]
mod tests {
use super::parse_delta;
use super::{nonempty, parse_delta, parse_error};
#[test]
fn error_frame_carries_its_message() {
let d = r#"{"error":{"type":"api_error","message":"Invalid credential"}}"#;
assert_eq!(parse_error(d).as_deref(), Some("Invalid credential"));
assert!(parse_error(r#"{"choices":[{"delta":{"content":"hi"}}]}"#).is_none());
}
#[test]
fn empty_reply_is_an_error() {
assert!(nonempty(" \n".into()).is_err());
assert_eq!(nonempty("hi".into()).unwrap(), "hi");
}
#[test]
fn extracts_content_delta() {