Files
voicebox/backend/routes/events.py
T
Jamie Pine b434db22f6 chore(backend): repair test suite and bring ruff to green
The suite hadn't run green since the routes refactor:
- test_profile_duplicate_names.py imported the pre-refactor module
  layout and broke collection; now imports backend.services.profiles
- tests/conftest.py puts the repo root and backend dir on sys.path so
  files collect standalone instead of depending on run order
- test_cors.py tested a hand-copied mirror of the origin list that had
  drifted from app.py (missing http://tauri.localhost); it now builds
  the app via the real create_app() factory
- test_progress.py simulated a 1KB download, below the tracker's 1MB
  reporting threshold; simulation raised to 5MB
- slow/timeout markers registered in pyproject

Ruff: ~900 violations auto-fixed (typing modernization, import
sorting, unused imports, whitespace). The remaining rules are baselined
in pyproject.toml with per-rule counts to burn down, plus per-file
carve-outs for deliberate env-before-import ordering. ruff check is
now clean; suite is 134 passed, 2 skipped.
2026-07-26 23:16:09 -07:00

46 lines
1.4 KiB
Python

"""Server-Sent-Event streams the frontend subscribes to.
``GET /events/speak`` — broadcasts ``speak-start`` / ``speak-end`` events
whenever an agent-initiated speak (MCP tool or POST /speak) runs. The
DictateWindow uses them to show the floating pill in a `speaking` state.
"""
import asyncio
import json
import logging
from fastapi import APIRouter, Request
from sse_starlette.sse import EventSourceResponse
from ..mcp_server import events as mcp_events
logger = logging.getLogger(__name__)
router = APIRouter()
@router.get("/events/speak")
async def speak_events(request: Request):
"""SSE stream of speak-start / speak-end events."""
async def event_stream():
queue = mcp_events.subscribe()
try:
# Immediate hello so EventSource knows the connection is live.
yield {"event": "ready", "data": "{}"}
while True:
if await request.is_disconnected():
return
try:
event = await asyncio.wait_for(queue.get(), timeout=15.0)
except TimeoutError:
# Heartbeat so proxies don't reap idle streams.
yield {"event": "ping", "data": "{}"}
continue
kind = event.pop("kind", "message")
yield {"event": kind, "data": json.dumps(event)}
finally:
mcp_events.unsubscribe(queue)
return EventSourceResponse(event_stream())