Files
voicebox/backend/mcp_server/events.py
T
James PineandClaude Opus 4.7 6b75e097e1 feat(mcp): Rust-owned speaking pill with self-contained audio playback
The pill window now surfaces for agent-initiated speech without main-window
involvement. Rust subscribes to /events/speak via a tokio task + reqwest
streaming body (speak_monitor.rs), shows the pill, and forwards events to
the dictate webview over Tauri's event bus. The pill plays audio via a
plain HTMLAudioElement and emits dictate:hide when playback ends. The
pill stays hidden through the ~1 s generation wait and only surfaces when
audio actually starts, with the counter armed at that moment.

Fixes a shared-dict mutation in mcp_server/events.publish() that caused
the second subscriber (Rust speak_monitor) to receive `event: message`
instead of named speak-start/speak-end frames. Also teaches the speak_monitor
parser to handle CRLF framing (sse-starlette default). Main-window
AudioPlayer now skips autoplay for source in {mcp, rest} to avoid
double-play when both windows are alive.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
2026-04-23 01:46:17 -07:00

42 lines
1.4 KiB
Python

"""In-memory pub/sub for speaking-pill SSE broadcasts.
MCP ``voicebox.speak`` calls and the REST ``POST /speak`` route publish
start/end events that DictateWindow subscribes to via /events/speak, so the
floating pill surfaces whenever an agent is speaking.
"""
import asyncio
from typing import Any
# Each subscriber gets its own queue. Bounded to drop oldest if a client lags.
_subscribers: set[asyncio.Queue[dict[str, Any]]] = set()
def subscribe() -> asyncio.Queue[dict[str, Any]]:
"""Register a new subscriber; caller must call unsubscribe() when done."""
queue: asyncio.Queue[dict[str, Any]] = asyncio.Queue(maxsize=64)
_subscribers.add(queue)
return queue
def unsubscribe(queue: asyncio.Queue[dict[str, Any]]) -> None:
_subscribers.discard(queue)
def publish(kind: str, payload: dict[str, Any]) -> None:
"""Fan out to all current subscribers. Non-blocking; drops on full queue.
Each subscriber gets its own dict copy — the SSE consumer calls
``event.pop("kind", ...)``, so sharing a single dict between queues
would mean the first consumer to drain its queue strips ``kind`` from
the object the next consumer later reads.
"""
for queue in list(_subscribers):
event = {"kind": kind, **payload}
try:
queue.put_nowait(event)
except asyncio.QueueFull:
# Slow subscriber — skip rather than block publishers.
pass