41 lines
1.1 KiB
Python
41 lines
1.1 KiB
Python
|
|
from __future__ import annotations
|
||
|
|
|
||
|
|
from dataclasses import dataclass, field
|
||
|
|
from time import time
|
||
|
|
|
||
|
|
|
||
|
|
@dataclass
|
||
|
|
class ChatTurn:
|
||
|
|
role: str
|
||
|
|
content: str
|
||
|
|
ts: float = field(default_factory=time)
|
||
|
|
|
||
|
|
|
||
|
|
class ChatStore:
|
||
|
|
"""In-memory chat history for the DJ panel."""
|
||
|
|
|
||
|
|
def __init__(self, max_turns: int = 40) -> None:
|
||
|
|
self._turns: list[ChatTurn] = []
|
||
|
|
self._max = max_turns
|
||
|
|
self.pending_vibe: str = ""
|
||
|
|
|
||
|
|
def add(self, role: str, content: str) -> None:
|
||
|
|
self._turns.append(ChatTurn(role=role, content=content))
|
||
|
|
if len(self._turns) > self._max:
|
||
|
|
self._turns = self._turns[-self._max :]
|
||
|
|
|
||
|
|
def history(self) -> list[dict]:
|
||
|
|
return [{"role": t.role, "content": t.content} for t in self._turns]
|
||
|
|
|
||
|
|
def public_log(self) -> list[dict]:
|
||
|
|
return [
|
||
|
|
{"role": t.role, "content": t.content, "ts": t.ts}
|
||
|
|
for t in self._turns
|
||
|
|
if t.role in ("user", "dj")
|
||
|
|
]
|
||
|
|
|
||
|
|
def take_vibe_hint(self) -> str:
|
||
|
|
hint = self.pending_vibe.strip()
|
||
|
|
self.pending_vibe = ""
|
||
|
|
return hint
|