Files
voicebox/backend/routes/mcp_bindings.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

80 lines
2.2 KiB
Python

"""REST endpoints for per-MCP-client voice binding settings.
The Settings UI uses these to let users configure distinct voices per
agent (Claude Code in Morgan, Cursor in Scarlett, ...). The ``client_id``
column is the same value the MCP client sends in ``X-Voicebox-Client-Id``
(or the stdio shim pulls from ``VOICEBOX_CLIENT_ID``).
"""
from datetime import datetime, timezone
from fastapi import APIRouter, Depends, HTTPException
from sqlalchemy.orm import Session
from .. import models
from ..database import get_db
from ..database.models import MCPClientBinding
router = APIRouter()
@router.get(
"/mcp/bindings",
response_model=models.MCPClientBindingListResponse,
)
async def list_mcp_bindings(db: Session = Depends(get_db)):
rows = (
db.query(MCPClientBinding)
.order_by(MCPClientBinding.client_id)
.all()
)
return models.MCPClientBindingListResponse(
items=[models.MCPClientBindingResponse.model_validate(r) for r in rows]
)
@router.put(
"/mcp/bindings",
response_model=models.MCPClientBindingResponse,
)
async def upsert_mcp_binding(
data: models.MCPClientBindingUpsert,
db: Session = Depends(get_db),
):
"""Create-or-update a binding. Matches by client_id."""
row = (
db.query(MCPClientBinding)
.filter(MCPClientBinding.client_id == data.client_id)
.first()
)
if row is None:
row = MCPClientBinding(client_id=data.client_id)
db.add(row)
row.label = data.label
row.profile_id = data.profile_id
row.default_engine = data.default_engine
row.default_personality = data.default_personality
row.updated_at = datetime.now(timezone.utc)
db.commit()
db.refresh(row)
return models.MCPClientBindingResponse.model_validate(row)
@router.delete("/mcp/bindings/{client_id}")
async def delete_mcp_binding(
client_id: str,
db: Session = Depends(get_db),
):
row = (
db.query(MCPClientBinding)
.filter(MCPClientBinding.client_id == client_id)
.first()
)
if row is None:
raise HTTPException(status_code=404, detail="Binding not found")
db.delete(row)
db.commit()
return {"deleted": client_id}