fix: valida mensagens e sequência de eventos WebSocket

This commit is contained in:
alex alves
2026-11-03 10:49:53 -03:00
parent 82553d86c7
commit 153b2986b7
2 changed files with 156 additions and 6 deletions

View File

@@ -554,14 +554,24 @@ async def audio_socket(websocket: WebSocket) -> None:
)
continue
event_type = payload.get("type")
if not isinstance(event_type, str):
if not isinstance(payload, dict):
await send_json(
websocket,
error_event(
"INVALID_MESSAGE",
"Field 'type' is required.",
"Message must be a JSON object.",
),
)
continue
event_type = payload.get("type")
if not isinstance(event_type, str) or not event_type.strip():
await send_json(
websocket,
error_event(
"INVALID_MESSAGE",
"Field 'type' must be a non-empty string.",
),
)
continue
@@ -654,6 +664,16 @@ async def audio_socket(websocket: WebSocket) -> None:
# ---------------------------------------------------------
# Todos os eventos abaixo exigem sessão ativa
# ---------------------------------------------------------
if session is None:
await send_json(
websocket,
error_event(
"SESSION_NOT_FOUND",
"session.start is required before this event.",
),
)
continue
if session.closed:
await send_json(
websocket,
@@ -669,6 +689,18 @@ async def audio_socket(websocket: WebSocket) -> None:
# audio.start
# ---------------------------------------------------------
if event_type == "audio.start":
if session.active_turn_id is not None:
await send_json(
websocket,
error_event(
"INVALID_TURN",
"A turn is already active.",
session_id=session.session_id,
turn_id=session.active_turn_id,
),
)
continue
turn_id: int | None = None
try: