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.
64 lines
1.6 KiB
Python
64 lines
1.6 KiB
Python
"""Event/message frames the bridge sends to the device (text, not audio).
|
|
|
|
These drive the device UI: "listening" / "thinking" / "speaking" states and
|
|
transcript display. FIRMWARE: exact field names may vary by build.
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
from typing import Optional
|
|
|
|
|
|
class MsgType:
|
|
LISTENING = "listening"
|
|
THINKING = "thinking"
|
|
SPEAKING = "speaking"
|
|
TEXT = "text" # assistant transcript to display
|
|
STT = "stt" # user transcript to display
|
|
TTS = "tts" # assistant text + optional audio flag
|
|
STATE = "state" # listening / speaking state transitions
|
|
|
|
|
|
def state(msg: str) -> dict:
|
|
return {"type": MsgType.STATE, "state": msg}
|
|
|
|
|
|
def text(msg: str, role: str = "assistant") -> dict:
|
|
return {"type": MsgType.TEXT, "role": role, "text": msg}
|
|
|
|
|
|
def stt(text: str) -> dict:
|
|
return {"type": MsgType.STT, "text": text}
|
|
|
|
|
|
def tts(text: str, audio: bool = True) -> dict:
|
|
return {"type": MsgType.TTS, "text": text, "audio": audio}
|
|
|
|
|
|
def listen_start() -> dict:
|
|
return state("listening")
|
|
|
|
|
|
def speak_start() -> dict:
|
|
return state("speaking")
|
|
|
|
|
|
def thinking() -> dict:
|
|
return state("thinking")
|
|
|
|
|
|
def build_message(kind: str, text: str = "", **extra) -> dict:
|
|
"""Small dispatcher used by the session to emit device-facing messages."""
|
|
if kind == "text":
|
|
return text(text)
|
|
if kind == "stt":
|
|
return stt(text)
|
|
if kind == "tts":
|
|
return tts(text)
|
|
if kind == "listening":
|
|
return listen_start()
|
|
if kind == "speaking":
|
|
return speak_start()
|
|
if kind == "thinking":
|
|
return thinking()
|
|
raise ValueError(f"unknown message kind: {kind}")
|