Files
voicebox/backend/routes/events.py
T
Jamie Pine fe14df5fda chore(backend): Ruff lint pass — deprecated APIs, exception leaks, dead patterns
Mechanical sweep of items called out in the PR review:

- qwen_llm_backend: AutoModelForCausalLM.from_pretrained(torch_dtype=…) is deprecated in transformers ≥4.41 in favor of dtype=. Renamed.
- routes/llm: try/except around backend.generate() raised HTTPException(500, detail=str(e)) which leaks stack traces / paths to clients and trips Ruff B904. Now logs the original exception server-side and hands the client a generic message; chained via `from e` to preserve traceback context.
- mcp_bindings + mcp_server/context: datetime.utcnow() is deprecated since 3.12. Switched the two assignment sites to datetime.now(timezone.utc). The schema-level `default=datetime.utcnow` defaults in database/models.py are left for a later schema-aware pass.
- routes/generations: `logger = …` sat between two import blocks (Ruff E402). Moved below imports.
- mcp_server/server + tests/test_refinement_samples: typing.Callable / typing.Iterable have been preferred-via collections.abc since 3.9 (Ruff UP035).
- routes/events: `except asyncio.TimeoutError` aliases plain `TimeoutError` since 3.11 (UP041).
- services/captures: hoisted WHISPER_NATIVE_FORMATS to module scope (was a function-local UPPER_SNAKE that tripped N806) and replaced the raw_path.unlink try/except OSError-pass with contextlib.suppress (SIM105). Semantic equivalence preserved — written_files.remove(raw_path) still only runs when unlink succeeds because it sits inside the suppressed block after the unlink call.
- database/migrations: hoisted the duplicate `import sqlite3` from inside two helper bodies to a single module-level import.
2026-04-25 01:40:34 -07:00

47 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())