80bef6f524
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
349 lines
14 KiB
Python
349 lines
14 KiB
Python
"""Orchestrates the pet's mic -> wake word -> STT -> server -> TTS pipeline.
|
|
|
|
Runs on a background QThread (see ui/app.py) so the Qt event loop / window
|
|
painting is never blocked by audio I/O or network calls. Talks to the UI
|
|
only through Qt signals (state_changed / said / log / action / napping),
|
|
which Qt marshals safely across threads — this class never touches a QWidget
|
|
directly.
|
|
|
|
Beyond the core loop it owns the side channels that let the pet act on its
|
|
own: the heartbeat (proactive announcements), the desktop notification
|
|
bridge, quiet hours, barge-in, and the live wake-word threshold.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import threading
|
|
import time
|
|
from typing import Optional
|
|
|
|
from PySide6.QtCore import QObject, Signal
|
|
|
|
from . import config, history as history_mod, notifications, pet_actions, quiet, screen_context, server_client, speech_text
|
|
from .audio import barge_in, mic, stt, tts, wake_word
|
|
from .state import PetState, PetStateMachine
|
|
|
|
# How often to re-check whether the pet should be napping. The fullscreen
|
|
# probe shells out to xprop, so this deliberately isn't every heartbeat tick.
|
|
_NAP_CHECK_INTERVAL_SECONDS = 10.0
|
|
|
|
|
|
class PetController(QObject):
|
|
state_changed = Signal(str) # PetState.value
|
|
said = Signal(str) # text now showing in the speech bubble
|
|
log = Signal(str)
|
|
action = Signal(dict) # parsed petctl action for the UI to perform
|
|
napping = Signal(bool) # quiet hours / fullscreen do-not-disturb
|
|
finished = Signal()
|
|
|
|
def __init__(self):
|
|
super().__init__()
|
|
self._running = True
|
|
self._muted = False
|
|
self._talk_now = threading.Event()
|
|
self._stream = None
|
|
self._last_heartbeat = 0.0
|
|
self._state = PetStateMachine(on_change=self._handle_state_change)
|
|
|
|
# Conversation scrollback, shared read-only with the UI's History
|
|
# window. Append-only from this thread; the UI only ever snapshots it.
|
|
self.history = history_mod.ConversationHistory(limit=config.HISTORY_LIMIT)
|
|
|
|
# Wake-word sensitivity is live-tunable (tray tuner), so it's read
|
|
# through a callable on every frame rather than captured per listen.
|
|
self._wake_threshold = config.WAKE_WORD_THRESHOLD
|
|
self._near_misses = wake_word.NearMissLog()
|
|
|
|
self._barge_in: Optional[barge_in.BargeInDetector] = None
|
|
self._napping = False
|
|
self._nap_forced: Optional[bool] = None # petctl nap on/off overrides the schedule
|
|
self._last_nap_check = 0.0
|
|
|
|
self._notification_watcher: Optional[notifications.NotificationWatcher] = None
|
|
self._notification_gate = notifications.NotificationGate(
|
|
config.NOTIFICATION_FILTER, config.NOTIFICATION_MIN_INTERVAL_SECONDS
|
|
)
|
|
self._pending_notifications: list[notifications.Notification] = []
|
|
self._notification_lock = threading.Lock()
|
|
|
|
# ── external controls (safe to call from the Qt/UI thread) ─────────
|
|
|
|
def request_talk_now(self) -> None:
|
|
self._talk_now.set()
|
|
|
|
def toggle_mute(self) -> bool:
|
|
self._muted = not self._muted
|
|
self.log.emit("Muted." if self._muted else "Unmuted.")
|
|
return self._muted
|
|
|
|
def set_napping(self, napping: Optional[bool]) -> None:
|
|
"""Force the nap state on/off, or pass None to hand control back to
|
|
the quiet-hours schedule."""
|
|
self._nap_forced = napping
|
|
if napping is not None:
|
|
self._apply_nap_state(napping)
|
|
|
|
def wake_threshold(self) -> float:
|
|
return self._wake_threshold
|
|
|
|
def set_wake_threshold(self, value: float) -> None:
|
|
self._wake_threshold = min(max(float(value), 0.01), 0.99)
|
|
|
|
def wake_stats(self) -> dict:
|
|
return {"peak": self._near_misses.peak, "near_misses": self._near_misses.entries()}
|
|
|
|
def reset_wake_stats(self) -> None:
|
|
self._near_misses.clear()
|
|
|
|
def stop(self) -> None:
|
|
self._running = False
|
|
self._talk_now.set() # wake up anything blocked waiting on it
|
|
if self._notification_watcher is not None:
|
|
self._notification_watcher.stop()
|
|
|
|
# ── internal ─────────────────────────────────────────────────────────
|
|
|
|
def _handle_state_change(self, _old: PetState, new: PetState) -> None:
|
|
self.state_changed.emit(new.value)
|
|
|
|
def _should_continue(self) -> bool:
|
|
return self._running
|
|
|
|
def run(self) -> None:
|
|
"""Thread entry point (connected to QThread.started)."""
|
|
missing = config.missing_config()
|
|
if missing:
|
|
self.log.emit(f"Missing config: {', '.join(missing)} — set them in .env and restart.")
|
|
self.finished.emit()
|
|
return
|
|
try:
|
|
self._stream = mic.open_input_stream()
|
|
except Exception as exc:
|
|
self.log.emit(f"Could not open microphone: {exc}")
|
|
self.finished.emit()
|
|
return
|
|
|
|
if config.BARGE_IN:
|
|
self._barge_in = barge_in.BargeInDetector(self._stream)
|
|
|
|
with self._stream:
|
|
try:
|
|
health = server_client.check_health()
|
|
self.log.emit(f"Connected to server: {health}")
|
|
except Exception as exc:
|
|
self.log.emit(f"Server not reachable yet ({exc}) — will keep trying per-request.")
|
|
self._start_notification_bridge()
|
|
self._loop()
|
|
if self._notification_watcher is not None:
|
|
self._notification_watcher.stop()
|
|
self.finished.emit()
|
|
|
|
def _loop(self) -> None:
|
|
while self._running:
|
|
if self._muted:
|
|
triggered = self._talk_now.wait(timeout=0.5)
|
|
if not self._running:
|
|
return
|
|
if not triggered:
|
|
continue
|
|
self._talk_now.clear()
|
|
else:
|
|
if not self._wait_for_wake_or_click():
|
|
if not self._running:
|
|
return
|
|
continue
|
|
self._handle_conversation_turn()
|
|
|
|
def _wait_for_wake_or_click(self) -> bool:
|
|
"""True once either the wake phrase was heard or a click-to-talk
|
|
request came in; False on a spurious wakeup (loop again)."""
|
|
def should_continue() -> bool:
|
|
return self._running and not self._talk_now.is_set()
|
|
|
|
detected = wake_word.listen_for_wake_word(
|
|
self._stream,
|
|
should_continue=should_continue,
|
|
threshold=self.wake_threshold, # callable: the tuner slider is live
|
|
on_tick=self._maybe_heartbeat,
|
|
on_score=self._observe_wake_score,
|
|
)
|
|
if not self._running:
|
|
return False
|
|
if detected:
|
|
self._talk_now.clear() # in case both fired around the same time
|
|
return True
|
|
if self._talk_now.is_set():
|
|
self._talk_now.clear()
|
|
return True
|
|
return False
|
|
|
|
def _observe_wake_score(self, score: float, threshold: float) -> None:
|
|
self._near_misses.observe(score, threshold, time.time())
|
|
|
|
def _handle_conversation_turn(self) -> None:
|
|
self._state.transition(PetState.LISTENING)
|
|
pcm = mic.record_utterance(self._stream, should_continue=self._should_continue)
|
|
if pcm is None:
|
|
self._state.transition(PetState.IDLE)
|
|
return
|
|
|
|
self._state.transition(PetState.THINKING)
|
|
try:
|
|
text = stt.transcribe(pcm)
|
|
except stt.SttError as exc:
|
|
self.log.emit(f"STT failed: {exc}")
|
|
self._state.transition(PetState.ERROR)
|
|
self._state.transition(PetState.IDLE)
|
|
return
|
|
if not text:
|
|
self._state.transition(PetState.IDLE)
|
|
return
|
|
self.log.emit(f"You: {text}")
|
|
self.history.add(history_mod.USER, text, time.time())
|
|
|
|
try:
|
|
# What's focused right now rides along, so "what's this error?"
|
|
# has a referent without you having to describe the window.
|
|
reply = server_client.converse(
|
|
screen_context.context_for(text), on_command=self._handle_command
|
|
)
|
|
except server_client.ServerError as exc:
|
|
self.log.emit(f"Server error: {exc}")
|
|
self._state.transition(PetState.ERROR)
|
|
self._state.transition(PetState.IDLE)
|
|
return
|
|
|
|
self._speak(reply)
|
|
self._state.transition(PetState.IDLE)
|
|
|
|
def _handle_command(self, command: str) -> str:
|
|
"""Server-relayed command. `petctl ...` drives the pet's body and
|
|
never reaches a shell; everything else is a real command, exactly as
|
|
before (see the security notes in the README)."""
|
|
try:
|
|
action = pet_actions.parse(command)
|
|
except pet_actions.ActionError as exc:
|
|
self.log.emit(f"petctl: {exc}")
|
|
return f"[pet] {exc}"
|
|
if action is None:
|
|
return server_client.run_local_command(command)
|
|
self.log.emit(f"Pet action: {action}")
|
|
if action["action"] == "nap":
|
|
self.set_napping(bool(action["enabled"]))
|
|
self.action.emit(action)
|
|
return pet_actions.describe(action)
|
|
|
|
def _speak(self, text: str) -> None:
|
|
self._state.transition(PetState.TALKING)
|
|
# Bubble gets the markdown stripped but emoji kept (it can't render
|
|
# **bold** but draws emoji fine); tts.speak() does its own, stricter
|
|
# sanitizing for the voice.
|
|
self.said.emit(speech_text.for_display(text))
|
|
self.log.emit(f"Bolt: {text}")
|
|
self.history.add(history_mod.PET, text, time.time())
|
|
|
|
should_stop = None
|
|
if self._barge_in is not None:
|
|
self._barge_in.reset()
|
|
should_stop = self._barge_in.check
|
|
completed = tts.speak(
|
|
text,
|
|
on_error=lambda exc: self.log.emit(f"TTS failed: {exc}"),
|
|
should_stop=should_stop,
|
|
)
|
|
if not completed:
|
|
# You talked over it — take that as the start of the next turn
|
|
# rather than making you say the wake word again.
|
|
self.log.emit("Interrupted — listening.")
|
|
self._talk_now.set()
|
|
|
|
# ── quiet hours / do-not-disturb ─────────────────────────────────────
|
|
|
|
def _apply_nap_state(self, napping: bool) -> None:
|
|
if napping == self._napping:
|
|
return
|
|
self._napping = napping
|
|
self.log.emit("Napping — no proactive noise." if napping else "Awake.")
|
|
self.napping.emit(napping)
|
|
|
|
def _refresh_nap_state(self) -> None:
|
|
if self._nap_forced is not None:
|
|
self._apply_nap_state(self._nap_forced)
|
|
return
|
|
now = time.monotonic()
|
|
if now - self._last_nap_check < _NAP_CHECK_INTERVAL_SECONDS:
|
|
return
|
|
self._last_nap_check = now
|
|
napping = quiet.is_quiet(
|
|
config.QUIET_HOURS,
|
|
on_error=lambda exc: self.log.emit(f"QUIET_HOURS is malformed ({exc}) — ignoring it."),
|
|
)
|
|
if not napping and config.DND_ON_FULLSCREEN:
|
|
napping = screen_context.is_fullscreen_active()
|
|
self._apply_nap_state(napping)
|
|
|
|
# ── desktop notification bridge ──────────────────────────────────────
|
|
|
|
def _start_notification_bridge(self) -> None:
|
|
if not config.NOTIFICATION_BRIDGE:
|
|
return
|
|
watcher = notifications.NotificationWatcher(self._queue_notification)
|
|
problem = watcher.start()
|
|
if problem:
|
|
self.log.emit(problem)
|
|
return
|
|
self._notification_watcher = watcher
|
|
self.log.emit("Notification bridge on.")
|
|
|
|
def _queue_notification(self, notification: notifications.Notification) -> None:
|
|
"""Called on the watcher thread — just queue it; forwarding happens on
|
|
the pipeline thread where it can't collide with a live conversation."""
|
|
if not self._notification_gate.should_forward(notification, time.monotonic()):
|
|
return
|
|
with self._notification_lock:
|
|
self._pending_notifications.append(notification)
|
|
|
|
def _drain_notifications(self) -> None:
|
|
with self._notification_lock:
|
|
pending, self._pending_notifications = self._pending_notifications, []
|
|
for notification in pending:
|
|
if not self._running or self._napping:
|
|
return
|
|
self.log.emit(f"Notification: {notification.as_text()}")
|
|
self.history.add(history_mod.SYSTEM, notification.as_text(), time.time())
|
|
try:
|
|
reply = server_client.converse(
|
|
f"[desktop notification] {notification.as_text()}",
|
|
on_command=self._handle_command,
|
|
)
|
|
except server_client.ServerError as exc:
|
|
self.log.emit(f"Couldn't forward notification: {exc}")
|
|
return
|
|
if reply.strip():
|
|
self._speak(reply)
|
|
self._state.transition(PetState.IDLE)
|
|
|
|
# ── heartbeat ────────────────────────────────────────────────────────
|
|
|
|
def _maybe_heartbeat(self) -> None:
|
|
self._refresh_nap_state()
|
|
now = time.monotonic()
|
|
if now - self._last_heartbeat < config.HEARTBEAT_INTERVAL_SECONDS:
|
|
return
|
|
self._last_heartbeat = now
|
|
if self._state.state != PetState.IDLE:
|
|
return
|
|
if self._napping:
|
|
return # quiet hours: still answers when spoken to, just doesn't start
|
|
self._drain_notifications()
|
|
if self._state.state != PetState.IDLE:
|
|
return
|
|
try:
|
|
announcement = server_client.report_status()
|
|
except server_client.ServerError as exc:
|
|
self.log.emit(f"Heartbeat failed: {exc}")
|
|
return
|
|
if announcement:
|
|
self._speak(announcement)
|
|
self._state.transition(PetState.IDLE)
|