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
+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() {