Spaces:
Sleeping
Sleeping
File size: 1,523 Bytes
89b6a21 127bfda | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 | import threading
import time
from collections import deque
from typing import Any
MAX_HUD_MESSAGES = 20
HUD_MESSAGE_TTL_SECONDS = 600.0 # 10 minutos
class HudRuntime:
"""Buffer FIFO de mensagens privadas para o streamer (HUD overlay)."""
def __init__(self) -> None:
self._lock = threading.Lock()
self._messages: deque[dict[str, Any]] = deque(maxlen=MAX_HUD_MESSAGES)
def push_message(self, text: str, source: str = "autonomy") -> dict[str, Any]:
if not text or not text.strip():
return {"ok": False, "reason": "empty_message"}
now = time.time()
entry: dict[str, Any] = {
"ts": now,
"text": text.strip()[:300],
"source": (source or "autonomy").strip().lower(),
}
with self._lock:
self._messages.append(entry)
return {"ok": True, "entry": entry}
def get_messages(self, since: float = 0.0) -> list[dict[str, Any]]:
now = time.time()
cutoff = max(since, now - HUD_MESSAGE_TTL_SECONDS)
with self._lock:
return [m for m in self._messages if float(m.get("ts", 0)) > cutoff]
def clear(self) -> None:
with self._lock:
self._messages.clear()
def get_status(self) -> dict[str, Any]:
with self._lock:
return {
"count": len(self._messages),
"max": MAX_HUD_MESSAGES,
}
hud_runtime = HudRuntime()
__all__ = ["MAX_HUD_MESSAGES", "HudRuntime", "hud_runtime"]
|