2026-06-17 04:51:49 +02:00
|
|
|
"""WebSocket-Echtzeit-Chat und -Sprache.
|
2026-06-17 04:26:55 +02:00
|
|
|
|
2026-06-17 04:51:49 +02:00
|
|
|
- /ws/chat : Text rein (JSON pro Turn), Antwort als Event-Folge zurueck.
|
|
|
|
|
- /ws/voice: Audio rein (binaere Chunks + Control), Transkription -> selbe Pipeline.
|
2026-06-17 04:26:55 +02:00
|
|
|
|
2026-06-17 05:06:58 +02:00
|
|
|
Antwort-Events: ack -> [token*] -> [audio*] -> semantic -> done.
|
2026-06-17 04:51:49 +02:00
|
|
|
Mit {"stream":true} kommen LLM-Token live, mit {"audio_stream":true} das Audio
|
|
|
|
|
satzweise (chunked TTS). /ws/voice sendet zuvor ein transcript-Event.
|
|
|
|
|
|
2026-06-17 05:06:58 +02:00
|
|
|
Barge-in: Ein {"type":"interrupt"}-Frame oder eine neue Eingabe bricht eine laufende
|
|
|
|
|
Antwort ab (-> interrupted-Event). Der Antwort-Turn laeuft als abbrechbarer Task.
|
|
|
|
|
|
|
|
|
|
Spaeter (eigene Increments): echte partielle Live-Transkripte (Streaming-STT-Dienst),
|
|
|
|
|
WebRTC.
|
2026-06-17 04:26:55 +02:00
|
|
|
"""
|
|
|
|
|
|
2026-06-17 05:06:58 +02:00
|
|
|
import asyncio
|
2026-06-17 04:51:49 +02:00
|
|
|
import json
|
|
|
|
|
|
2026-06-17 04:26:55 +02:00
|
|
|
from fastapi import APIRouter, WebSocket, WebSocketDisconnect
|
|
|
|
|
|
|
|
|
|
from app.config import settings
|
|
|
|
|
from app.errors import RoutingError
|
|
|
|
|
from app.dependencies import (
|
|
|
|
|
get_store,
|
|
|
|
|
resolve_route,
|
|
|
|
|
build_orchestrator,
|
|
|
|
|
resolve_output_endpoint,
|
|
|
|
|
)
|
|
|
|
|
from app.store import SessionOwnershipError
|
2026-06-17 05:06:58 +02:00
|
|
|
from app.audio.vad import EnergyVAD
|
2026-06-17 05:29:30 +02:00
|
|
|
from app.quota import enforce_quota, record_usage, QuotaExceededError
|
|
|
|
|
from app.safety.emergency import handle_emergency
|
2026-06-17 04:26:55 +02:00
|
|
|
|
|
|
|
|
router = APIRouter()
|
|
|
|
|
|
2026-06-17 04:51:49 +02:00
|
|
|
_OVERRIDE_KEYS = (
|
|
|
|
|
"input_endpoint",
|
|
|
|
|
"output_endpoint",
|
|
|
|
|
"language",
|
|
|
|
|
"stt_provider",
|
|
|
|
|
"llm_provider",
|
|
|
|
|
"tts_provider",
|
|
|
|
|
)
|
|
|
|
|
|
2026-06-17 04:26:55 +02:00
|
|
|
|
|
|
|
|
def _authenticate(token: str | None):
|
|
|
|
|
store = get_store()
|
|
|
|
|
if not settings.auth_enabled:
|
|
|
|
|
return store.ensure_anonymous_user()
|
|
|
|
|
if not token:
|
|
|
|
|
return None
|
|
|
|
|
return store.get_user_by_token(token)
|
|
|
|
|
|
|
|
|
|
|
2026-06-17 04:51:49 +02:00
|
|
|
async def _resolve(user, session_id, options):
|
|
|
|
|
"""Loest Route + Orchestrator + Output-Endpunkt auf (kann RoutingError/Ownership werfen)."""
|
|
|
|
|
overrides = {key: options.get(key) for key in _OVERRIDE_KEYS}
|
|
|
|
|
route = resolve_route(user, session_id, overrides)
|
|
|
|
|
orchestrator = build_orchestrator(route)
|
|
|
|
|
output = await resolve_output_endpoint(route)
|
|
|
|
|
return route, orchestrator, output
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async def _run_turn(websocket, store, user, session_id, route, orchestrator, output, text, options):
|
|
|
|
|
"""Faehrt einen Antwort-Turn und streamt die Events an den Client."""
|
|
|
|
|
conversation = (
|
|
|
|
|
store.get_recent_messages(session_id, settings.history_max_messages)
|
|
|
|
|
if session_id
|
|
|
|
|
else []
|
|
|
|
|
)
|
|
|
|
|
memories = store.get_memories(user.id)
|
|
|
|
|
llm_context = list(conversation)
|
|
|
|
|
if memories:
|
|
|
|
|
memory_text = "Was du ueber den Nutzer weisst:\n" + "\n".join(
|
|
|
|
|
f"- {m.content}" for m in memories
|
|
|
|
|
)
|
|
|
|
|
llm_context = [{"role": "system", "content": memory_text}] + llm_context
|
|
|
|
|
|
2026-06-17 05:29:30 +02:00
|
|
|
# Notfall-Erkennung zuerst (immer eskalieren, auch bei Quota-Limit).
|
|
|
|
|
emergency = handle_emergency(user, text, store)
|
|
|
|
|
if emergency:
|
|
|
|
|
await websocket.send_json({"type": "emergency", "category": emergency["category"]})
|
|
|
|
|
else:
|
|
|
|
|
try:
|
|
|
|
|
enforce_quota(user, store)
|
|
|
|
|
except QuotaExceededError as exc:
|
|
|
|
|
await websocket.send_json({"type": "error", "status": 429, "detail": str(exc)})
|
|
|
|
|
return
|
|
|
|
|
|
2026-06-17 04:51:49 +02:00
|
|
|
await websocket.send_json({"type": "ack", "route": route.as_dict()})
|
|
|
|
|
|
|
|
|
|
voice = options.get("voice") or settings.openrouter_tts_voice
|
|
|
|
|
stream = bool(options.get("stream"))
|
|
|
|
|
audio_stream = bool(options.get("audio_stream"))
|
|
|
|
|
|
|
|
|
|
on_token = None
|
|
|
|
|
if stream:
|
|
|
|
|
async def on_token(delta):
|
|
|
|
|
await websocket.send_json({"type": "token", "text": delta})
|
|
|
|
|
|
|
|
|
|
on_audio = None
|
|
|
|
|
if audio_stream:
|
|
|
|
|
audio_seq = 0
|
|
|
|
|
|
|
|
|
|
async def on_audio(chunk):
|
|
|
|
|
nonlocal audio_seq
|
|
|
|
|
await websocket.send_json({"type": "audio", "seq": audio_seq})
|
|
|
|
|
audio_seq += 1
|
|
|
|
|
await websocket.send_bytes(chunk)
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
if stream or audio_stream:
|
|
|
|
|
trace, audio = await orchestrator.chat_stream(
|
|
|
|
|
text,
|
|
|
|
|
language=route.language,
|
|
|
|
|
voice=voice,
|
|
|
|
|
output=output,
|
|
|
|
|
history=llm_context,
|
|
|
|
|
on_token=on_token,
|
|
|
|
|
on_audio=on_audio,
|
|
|
|
|
)
|
|
|
|
|
else:
|
|
|
|
|
trace, audio = await orchestrator.chat_text(
|
|
|
|
|
text,
|
|
|
|
|
language=route.language,
|
|
|
|
|
voice=voice,
|
|
|
|
|
output=output,
|
|
|
|
|
history=llm_context,
|
|
|
|
|
)
|
|
|
|
|
except Exception as exc:
|
|
|
|
|
await websocket.send_json({"type": "error", "status": 502, "detail": str(exc)})
|
|
|
|
|
return
|
|
|
|
|
|
2026-06-17 05:29:30 +02:00
|
|
|
record_usage(user, store, len(text) + len(trace.semantic_response or ""))
|
|
|
|
|
|
2026-06-17 04:51:49 +02:00
|
|
|
if session_id:
|
|
|
|
|
store.append_message(session_id, user.id, "user", text)
|
|
|
|
|
store.append_message(session_id, user.id, "assistant", trace.semantic_response)
|
|
|
|
|
|
|
|
|
|
await websocket.send_json(
|
|
|
|
|
{"type": "semantic", "text": trace.semantic_response, "spoken": trace.spoken_response}
|
|
|
|
|
)
|
|
|
|
|
if not audio_stream:
|
|
|
|
|
await websocket.send_bytes(audio)
|
|
|
|
|
await websocket.send_json({"type": "done", "audio_format": "pcm", "sample_rate": 24000})
|
|
|
|
|
|
|
|
|
|
|
2026-06-17 05:06:58 +02:00
|
|
|
async def _cancel_active(task, websocket) -> None:
|
|
|
|
|
"""Bricht einen laufenden Antwort-Turn ab (Barge-in) und meldet 'interrupted'."""
|
|
|
|
|
if task is None or task.done():
|
|
|
|
|
return
|
|
|
|
|
task.cancel()
|
|
|
|
|
try:
|
|
|
|
|
await task
|
|
|
|
|
except asyncio.CancelledError:
|
|
|
|
|
pass
|
|
|
|
|
await websocket.send_json({"type": "interrupted"})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async def _chat_turn(websocket, store, user, session_id, text, options):
|
|
|
|
|
try:
|
|
|
|
|
route, orchestrator, output = await _resolve(user, session_id, options)
|
|
|
|
|
except SessionOwnershipError as exc:
|
|
|
|
|
await websocket.send_json({"type": "error", "status": 403, "detail": str(exc)})
|
|
|
|
|
return
|
|
|
|
|
except RoutingError as exc:
|
|
|
|
|
await websocket.send_json({"type": "error", "status": 422, "detail": str(exc)})
|
|
|
|
|
return
|
|
|
|
|
await _run_turn(websocket, store, user, session_id, route, orchestrator, output, text, options)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async def _voice_turn(websocket, store, user, session_id, audio, fmt, options):
|
|
|
|
|
try:
|
|
|
|
|
route, orchestrator, output = await _resolve(user, session_id, options)
|
|
|
|
|
except SessionOwnershipError as exc:
|
|
|
|
|
await websocket.send_json({"type": "error", "status": 403, "detail": str(exc)})
|
|
|
|
|
return
|
|
|
|
|
except RoutingError as exc:
|
|
|
|
|
await websocket.send_json({"type": "error", "status": 422, "detail": str(exc)})
|
|
|
|
|
return
|
|
|
|
|
try:
|
|
|
|
|
transcript = await orchestrator.stt.transcribe(audio, fmt=fmt, language=route.language)
|
|
|
|
|
except Exception as exc:
|
|
|
|
|
await websocket.send_json({"type": "error", "status": 502, "detail": str(exc)})
|
|
|
|
|
return
|
|
|
|
|
await websocket.send_json({"type": "transcript", "text": transcript})
|
|
|
|
|
if not transcript or not transcript.strip():
|
|
|
|
|
await websocket.send_json({"type": "error", "detail": "empty transcript"})
|
|
|
|
|
return
|
|
|
|
|
await _run_turn(websocket, store, user, session_id, route, orchestrator, output, transcript, options)
|
|
|
|
|
|
|
|
|
|
|
2026-06-17 04:26:55 +02:00
|
|
|
@router.websocket("/ws/chat")
|
2026-06-17 04:51:49 +02:00
|
|
|
async def ws_chat(websocket: WebSocket, session_id: str | None = None, token: str | None = None):
|
2026-06-17 04:26:55 +02:00
|
|
|
user = _authenticate(token)
|
|
|
|
|
if user is None:
|
|
|
|
|
await websocket.close(code=1008)
|
|
|
|
|
return
|
|
|
|
|
await websocket.accept()
|
|
|
|
|
store = get_store()
|
2026-06-17 05:06:58 +02:00
|
|
|
active = None
|
2026-06-17 04:26:55 +02:00
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
while True:
|
|
|
|
|
msg = await websocket.receive_json()
|
2026-06-17 05:06:58 +02:00
|
|
|
if msg.get("type") == "interrupt":
|
|
|
|
|
await _cancel_active(active, websocket)
|
|
|
|
|
active = None
|
|
|
|
|
continue
|
2026-06-17 04:26:55 +02:00
|
|
|
text = (msg.get("text") or "").strip()
|
|
|
|
|
if not text:
|
|
|
|
|
await websocket.send_json({"type": "error", "detail": "empty text"})
|
|
|
|
|
continue
|
2026-06-17 05:06:58 +02:00
|
|
|
await _cancel_active(active, websocket) # Barge-in bei neuer Eingabe
|
|
|
|
|
active = asyncio.create_task(
|
|
|
|
|
_chat_turn(websocket, store, user, session_id, text, msg)
|
|
|
|
|
)
|
2026-06-17 04:51:49 +02:00
|
|
|
except WebSocketDisconnect:
|
2026-06-17 05:06:58 +02:00
|
|
|
if active and not active.done():
|
|
|
|
|
active.cancel()
|
2026-06-17 04:51:49 +02:00
|
|
|
return
|
2026-06-17 04:26:55 +02:00
|
|
|
|
|
|
|
|
|
2026-06-17 04:51:49 +02:00
|
|
|
@router.websocket("/ws/voice")
|
|
|
|
|
async def ws_voice(websocket: WebSocket, session_id: str | None = None, token: str | None = None):
|
|
|
|
|
user = _authenticate(token)
|
|
|
|
|
if user is None:
|
|
|
|
|
await websocket.close(code=1008)
|
|
|
|
|
return
|
|
|
|
|
await websocket.accept()
|
|
|
|
|
store = get_store()
|
|
|
|
|
|
|
|
|
|
audio_buffer = bytearray()
|
|
|
|
|
fmt = "wav"
|
2026-06-17 05:06:58 +02:00
|
|
|
active = None
|
|
|
|
|
vad = None
|
2026-06-17 17:32:12 +02:00
|
|
|
start_options: dict = {} # Konfig aus dem start-Frame (Provider, audio_stream, language)
|
2026-06-17 05:06:58 +02:00
|
|
|
|
|
|
|
|
async def _start_voice(audio: bytes, options: dict):
|
|
|
|
|
nonlocal active
|
|
|
|
|
await _cancel_active(active, websocket) # Barge-in bei neuer Aeusserung
|
|
|
|
|
active = asyncio.create_task(
|
|
|
|
|
_voice_turn(websocket, store, user, session_id, audio, fmt, options)
|
|
|
|
|
)
|
2026-06-17 04:51:49 +02:00
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
while True:
|
|
|
|
|
message = await websocket.receive()
|
|
|
|
|
if message["type"] == "websocket.disconnect":
|
2026-06-17 05:06:58 +02:00
|
|
|
if active and not active.done():
|
|
|
|
|
active.cancel()
|
2026-06-17 04:51:49 +02:00
|
|
|
return
|
2026-06-17 04:26:55 +02:00
|
|
|
|
2026-06-17 04:51:49 +02:00
|
|
|
if message.get("bytes") is not None:
|
|
|
|
|
audio_buffer.extend(message["bytes"])
|
2026-06-17 05:06:58 +02:00
|
|
|
# VAD: Aeusserungsende automatisch erkennen (opt-in via start-Frame).
|
|
|
|
|
if vad is not None and vad.feed(message["bytes"]):
|
|
|
|
|
audio = bytes(audio_buffer)
|
|
|
|
|
audio_buffer.clear()
|
|
|
|
|
vad.reset()
|
2026-06-17 17:32:12 +02:00
|
|
|
await _start_voice(audio, start_options)
|
2026-06-17 04:51:49 +02:00
|
|
|
continue
|
|
|
|
|
|
|
|
|
|
raw = message.get("text")
|
|
|
|
|
if raw is None:
|
|
|
|
|
continue
|
|
|
|
|
try:
|
|
|
|
|
control = json.loads(raw)
|
|
|
|
|
except ValueError:
|
|
|
|
|
await websocket.send_json({"type": "error", "detail": "invalid control frame"})
|
|
|
|
|
continue
|
2026-06-17 04:43:50 +02:00
|
|
|
|
2026-06-17 04:51:49 +02:00
|
|
|
ctype = control.get("type")
|
2026-06-17 05:06:58 +02:00
|
|
|
if ctype == "interrupt":
|
|
|
|
|
await _cancel_active(active, websocket)
|
|
|
|
|
active = None
|
|
|
|
|
continue
|
2026-06-17 04:51:49 +02:00
|
|
|
if ctype == "start":
|
|
|
|
|
audio_buffer.clear()
|
2026-06-17 17:32:12 +02:00
|
|
|
start_options = control # Provider/audio_stream/language fuer den Turn merken
|
2026-06-17 04:51:49 +02:00
|
|
|
fmt = control.get("format", "wav")
|
2026-06-17 05:06:58 +02:00
|
|
|
if control.get("vad"):
|
|
|
|
|
vad = EnergyVAD(
|
|
|
|
|
sample_rate=control.get("sample_rate", 16000),
|
|
|
|
|
threshold=control.get("vad_threshold", 500.0),
|
|
|
|
|
silence_ms=control.get("vad_silence_ms", 700.0),
|
|
|
|
|
)
|
|
|
|
|
else:
|
|
|
|
|
vad = None
|
2026-06-17 04:51:49 +02:00
|
|
|
continue
|
|
|
|
|
if ctype != "end":
|
|
|
|
|
continue
|
2026-06-17 04:43:50 +02:00
|
|
|
|
2026-06-17 04:51:49 +02:00
|
|
|
if not audio_buffer:
|
|
|
|
|
await websocket.send_json({"type": "error", "detail": "no audio received"})
|
|
|
|
|
continue
|
2026-06-17 04:37:37 +02:00
|
|
|
|
2026-06-17 05:06:58 +02:00
|
|
|
audio = bytes(audio_buffer)
|
|
|
|
|
audio_buffer.clear()
|
|
|
|
|
if vad is not None:
|
|
|
|
|
vad.reset()
|
2026-06-17 17:32:12 +02:00
|
|
|
# start-Frame-Konfig + end-Frame zusammenfuehren (end kann ueberschreiben)
|
|
|
|
|
await _start_voice(audio, {**start_options, **control})
|
2026-06-17 04:26:55 +02:00
|
|
|
except WebSocketDisconnect:
|
2026-06-17 05:06:58 +02:00
|
|
|
if active and not active.done():
|
|
|
|
|
active.cancel()
|
2026-06-17 04:26:55 +02:00
|
|
|
return
|