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:
@@ -23,7 +23,9 @@ QtObject {
|
|||||||
property string state: "idle"
|
property string state: "idle"
|
||||||
// Voice mode itself — the widget is only on screen while this is true.
|
// Voice mode itself — the widget is only on screen while this is true.
|
||||||
property bool active: false
|
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
|
// Mic level while listening, her output level while speaking. Drives the
|
||||||
// only genuinely live part of the visuals.
|
// only genuinely live part of the visuals.
|
||||||
property real rms: 0
|
property real rms: 0
|
||||||
@@ -31,35 +33,39 @@ QtObject {
|
|||||||
property string lunaText: ""
|
property string lunaText: ""
|
||||||
property string error: ""
|
property string error: ""
|
||||||
|
|
||||||
readonly property var socket: Socket {
|
// `luna-voice watch`, not Quickshell's Socket: a Socket whose connect
|
||||||
id: socket
|
// fails once (daemon mid-restart) keeps the dead QLocalSocket and ignores
|
||||||
path: `${Quickshell.env("XDG_RUNTIME_DIR") ?? "/run/user/1000"}/luna-voiced.sock`
|
// every later `connected = true`, so the widget stuck on OFFLINE.
|
||||||
connected: true
|
readonly property var watch: Process {
|
||||||
|
id: watch
|
||||||
|
command: ["luna-voice", "watch"]
|
||||||
|
running: true
|
||||||
|
|
||||||
onConnectionStateChanged: {
|
stdout: SplitParser {
|
||||||
if (!socket.connected) {
|
splitMarker: "\n"
|
||||||
// A daemon that went away is not a daemon that is idle: reset
|
onRead: line => {
|
||||||
// rather than leave the last frame of a conversation frozen
|
root.connected = true;
|
||||||
// on screen.
|
root.ingest(line);
|
||||||
root.active = false;
|
|
||||||
root.state = "idle";
|
|
||||||
root.rms = 0;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
parser: SplitParser {
|
onExited: {
|
||||||
splitMarker: "\n"
|
// A daemon that went away is not a daemon that is idle: reset
|
||||||
onRead: line => root.ingest(line)
|
// 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
|
// Retry forever, cheaply; each attempt is a fresh connection.
|
||||||
// socket is a no-op, so this doubles as the initial connect.
|
|
||||||
readonly property var reconnect: Timer {
|
readonly property var reconnect: Timer {
|
||||||
interval: 2000
|
interval: 2000
|
||||||
running: !socket.connected
|
running: !watch.running
|
||||||
repeat: true
|
repeat: true
|
||||||
onTriggered: socket.connected = true
|
onTriggered: watch.running = true
|
||||||
}
|
}
|
||||||
|
|
||||||
function ingest(line: string): void {
|
function ingest(line: string): void {
|
||||||
|
|||||||
@@ -70,7 +70,10 @@ pub async fn stream_reply(
|
|||||||
let Some(data) = line.strip_prefix("data:") else { continue };
|
let Some(data) = line.strip_prefix("data:") else { continue };
|
||||||
let data = data.trim();
|
let data = data.trim();
|
||||||
if data == "[DONE]" {
|
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) {
|
if let Some(delta) = parse_delta(data) {
|
||||||
full.push_str(&delta);
|
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)
|
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
|
/// Pulls choices[0].delta.content out of one SSE data payload. Tool-call
|
||||||
/// frames and role-only frames carry no content and are skipped.
|
/// frames and role-only frames carry no content and are skipped.
|
||||||
fn parse_delta(data: &str) -> Option<String> {
|
fn parse_delta(data: &str) -> Option<String> {
|
||||||
@@ -100,7 +124,20 @@ fn parse_delta(data: &str) -> Option<String> {
|
|||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
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]
|
#[test]
|
||||||
fn extracts_content_delta() {
|
fn extracts_content_delta() {
|
||||||
|
|||||||
Reference in New Issue
Block a user