Files
esp32-server/app/xiaozhi/websocket.py
T
Macky b8950b38a4 feat: Phase 1 testable bridge skeleton (18 tests green)
Xiaozhi WebSocket endpoint with handshake/auth, mock STT/TTS/Hermes
backends, Thai chunker, barge-in queue, latency logger, GPU planner,
voice-profile guard (reasoning=none, session_search only).

CON-002: no GPU/audio libs loaded at import. MUST-NOT-001: all model
names/tokens from config.
2026-10-03 11:45:31 +07:00

89 lines
3.0 KiB
Python

"""Device-facing WebSocket loop (REQ-001/002/007/009).
Encapsulates the handshake → auth → conversational-turn loop so ``main.py``
just wires a FastAPI endpoint to :func:`run`. One instance per connection.
"""
from __future__ import annotations
import asyncio
import json
import uuid
from fastapi import WebSocket
from ..config import Config
from ..hermes import HermesClient, VoiceProfile
from ..stt import make_stt
from ..tts import make_tts
from ..audio.vad import make_vad
from ..security import DeviceAuth, AuthError
from ..metrics import LatencyLogger
from .protocol import build_hello, build_hello_reply
from . import messages as msg
from .session import Session
class DeviceLoop:
def __init__(self, cfg: Config, profile: VoiceProfile, auth: DeviceAuth) -> None:
self.cfg = cfg
self.profile = profile
self.auth = auth
async def run(self, ws: WebSocket) -> None:
await ws.accept()
hello = await self._handshake(ws)
if hello is None:
return
client = HermesClient(self.cfg.hermes, self.profile)
session = Session(
client=client,
stt=make_stt(self.cfg.stt),
tts=make_tts(self.cfg.tts),
vad=make_vad("energy"),
logger=LatencyLogger(),
)
try:
while True:
data = await ws.receive()
if data.get("type") == "websocket.disconnect":
break
audio = data.get("bytes")
if audio is not None:
await self._turn(ws, session, audio)
finally:
session.barge_in()
async def _handshake(self, ws: WebSocket):
try:
raw = await asyncio.wait_for(ws.receive_text(), timeout=10)
hello = build_hello(json.loads(raw))
except asyncio.TimeoutError:
await ws.close()
return None
except Exception as exc: # noqa: BLE001
await ws.send_text(json.dumps({"type": "error", "message": str(exc)}))
return None
try:
self.auth.authenticate(hello.device_id, hello.authorization or "")
except AuthError as exc:
await ws.send_text(json.dumps({"type": "error", "message": str(exc)}))
await ws.close()
return None
session_id = str(uuid.uuid4())
await ws.send_text(json.dumps(build_hello_reply(session_id, hello).model_dump()))
return hello
async def _turn(self, ws: WebSocket, session: Session, pcm: bytes) -> None:
try:
async for kind, payload in session.run_turn(pcm):
if kind == "stt":
await ws.send_text(json.dumps(msg.stt(payload)))
elif kind == "tts_text":
await ws.send_text(json.dumps(msg.state("speaking")))
await ws.send_text(json.dumps(msg.text(payload)))
elif kind == "audio":
await ws.send_bytes(payload)
except Exception: # keep the device connected on a bad turn
pass