- 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>
113 lines
3.7 KiB
QML
113 lines
3.7 KiB
QML
pragma ComponentBehavior: Bound
|
|
|
|
import QtQuick
|
|
import Quickshell
|
|
import Quickshell.Io
|
|
|
|
// Everything the Luna surfaces know, read off luna-voiced's unix socket
|
|
// (services/desktop/luna-voice.nix).
|
|
//
|
|
// Read-only on purpose: the key that toggles voice mode runs `luna-voice
|
|
// toggle` from Hyprland rather than writing here, so the toggle keeps working
|
|
// while the shell is reloading — and a QML error can never leave the mic on
|
|
// with nothing showing it.
|
|
//
|
|
// The daemon is a USER service that may start after the shell, restart under
|
|
// it, or not be installed at all, so a missing socket is a normal state and
|
|
// not an error: `reconnect` retries quietly and `connected` is what the
|
|
// widget shows.
|
|
QtObject {
|
|
id: root
|
|
|
|
// "idle" | "listening" | "thinking" | "speaking"
|
|
property string state: "idle"
|
|
// Voice mode itself — the widget is only on screen while this is true.
|
|
property bool active: false
|
|
// 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
|
|
property string userText: ""
|
|
property string lunaText: ""
|
|
property string error: ""
|
|
|
|
// `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
|
|
|
|
stdout: SplitParser {
|
|
splitMarker: "\n"
|
|
onRead: line => {
|
|
root.connected = true;
|
|
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; each attempt is a fresh connection.
|
|
readonly property var reconnect: Timer {
|
|
interval: 2000
|
|
running: !watch.running
|
|
repeat: true
|
|
onTriggered: watch.running = true
|
|
}
|
|
|
|
function ingest(line: string): void {
|
|
let msg;
|
|
try {
|
|
msg = JSON.parse(line);
|
|
} catch (e) {
|
|
return; // a half-written line is not worth a console error
|
|
}
|
|
|
|
switch (msg.type) {
|
|
case "mode":
|
|
root.active = msg.active;
|
|
if (!msg.active) {
|
|
root.state = "idle";
|
|
root.rms = 0;
|
|
}
|
|
break;
|
|
case "state":
|
|
root.state = msg.value;
|
|
// Level is only meaningful during the phase that produces it.
|
|
if (msg.value !== "listening" && msg.value !== "speaking")
|
|
root.rms = 0;
|
|
break;
|
|
case "amplitude":
|
|
root.rms = msg.rms;
|
|
break;
|
|
case "caption":
|
|
if (msg.speaker === "user") {
|
|
// A new user caption starts a turn, so it is what clears her
|
|
// last reply — NOT speech_start, which arrives after her
|
|
// first caption, not before it.
|
|
root.userText = msg.text;
|
|
root.lunaText = "";
|
|
} else {
|
|
root.lunaText = msg.text;
|
|
}
|
|
break;
|
|
case "error":
|
|
root.error = msg.message;
|
|
break;
|
|
}
|
|
}
|
|
}
|