Files
esp32-server/app/xiaozhi/websocket.py
T
kunthawat a582c1ff36 feat: firmware-compatible handshake (header auth, transport=websocket) + /ota onboarding endpoint
- auth from Authorization/Device-Id headers (firmware v2.5.0), frame fallback kept
- server hello carries transport=websocket + session_id (firmware ParseServerHello)
- /ota GET+POST returns {websocket:{url,token,version}, server_time:{timestamp,timezone_offset}, firmware:{version}}
- config.yaml: device registered by MAC, ota block added
- 21 tests pass (added OTA + header-auth coverage)
2026-10-03 16:35:41 +07:00

114 lines
3.8 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_server_hello
from . import messages as msg
from .session import Session
def _bearer(value: str | None) -> str | None:
"""Strip an optional ``Bearer `` prefix from an Authorization header."""
if value is None:
return None
v = value.strip()
if not v:
return None
if v.lower().startswith("bearer "):
return v[7:].strip()
return v
class DeviceLoop:
def __init__(
self,
cfg: Config,
profile: VoiceProfile,
auth: DeviceAuth,
headers: dict[str, str] | None = None,
) -> None:
self.cfg = cfg
self.profile = profile
self.auth = auth
self.headers = headers or {}
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:
# FIRMWARE: auth from HTTP headers (Bearer token), not the frame.
# Fall back to frame fields for synthetic/test clients.
device_id = self.headers.get("device_id") or hello.device_id
token = _bearer(self.headers.get("authorization"))
if token is None:
token = hello.authorization or ""
self.auth.authenticate(device_id, token)
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_server_hello(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