- db/database.py: conversation_messages 테이블 + save/load/cleanup 헬퍼 - conversation.py: write-through (memory + DB) + lazy load on first access - 메모리 캐시 1시간 TTL, DB 7일 보관 - add/get/format_for_prompt가 async로 변경 - worker.py: 모든 conversation_store 호출에 await 추가 - main.py lifespan에 startup cleanup 호출 (7일 이상 정리) 서버 재시작 후 "방금 그거 더 자세히" 같은 후속 질문이 컨텍스트 유지 Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
108 lines
4.0 KiB
Python
108 lines
4.0 KiB
Python
"""Conversation — user별 최근 대화 기억 (메모리 캐시 + sqlite write-through)."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import logging
|
|
from collections import defaultdict
|
|
from dataclasses import dataclass, field
|
|
from time import time
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
MAX_HISTORY = 10 # 메모리 캐시 user당 최근 대화 수
|
|
HISTORY_TTL = 3600.0 # 1시간 이후 메모리 만료 (DB는 7일 보관)
|
|
DRAFT_TTL = 300.0 # pending_draft 5분 만료
|
|
DB_LOAD_LIMIT = 40 # DB lazy load 시 최대 메시지 수
|
|
|
|
|
|
@dataclass
|
|
class Message:
|
|
role: str # "user" | "assistant"
|
|
content: str
|
|
timestamp: float = field(default_factory=time)
|
|
|
|
|
|
class ConversationStore:
|
|
def __init__(self) -> None:
|
|
self._history: dict[str, list[Message]] = defaultdict(list)
|
|
self._loaded_users: set[str] = set() # DB lazy load 완료한 user_id
|
|
self._pending_drafts: dict[str, tuple[dict, float]] = {} # user_id → (draft_data, created_at)
|
|
|
|
async def add(self, user_id: str, role: str, content: str) -> None:
|
|
"""메모리 + DB write-through."""
|
|
from db.database import save_conversation_message
|
|
msg = Message(role=role, content=content)
|
|
|
|
# 메모리 캐시
|
|
msgs = self._history[user_id]
|
|
msgs.append(msg)
|
|
if len(msgs) > MAX_HISTORY:
|
|
self._history[user_id] = msgs[-MAX_HISTORY:]
|
|
|
|
# DB write-through (실패해도 메모리에는 남음)
|
|
try:
|
|
await save_conversation_message(user_id, role, content, msg.timestamp)
|
|
except Exception:
|
|
logger.warning("Failed to persist conversation for %s", user_id, exc_info=True)
|
|
|
|
async def get(self, user_id: str) -> list[Message]:
|
|
"""만료되지 않은 최근 대화. 메모리 비어있으면 DB lazy load."""
|
|
from db.database import load_conversation_messages
|
|
now = time()
|
|
|
|
# 메모리 캐시에서 fresh 한 것만
|
|
msgs = self._history.get(user_id, [])
|
|
fresh = [m for m in msgs if now - m.timestamp < HISTORY_TTL]
|
|
|
|
# 메모리 비어있고 DB lazy load 안 했으면 DB에서 가져옴
|
|
if not fresh and user_id not in self._loaded_users:
|
|
self._loaded_users.add(user_id)
|
|
since = now - HISTORY_TTL
|
|
try:
|
|
db_msgs = await load_conversation_messages(user_id, since, limit=DB_LOAD_LIMIT)
|
|
fresh = [
|
|
Message(role=m["role"], content=m["content"], timestamp=m["created_at"])
|
|
for m in db_msgs
|
|
]
|
|
if fresh:
|
|
logger.info("Loaded %d messages from DB for %s", len(fresh), user_id)
|
|
except Exception:
|
|
logger.warning("Failed to load conversation from DB for %s", user_id, exc_info=True)
|
|
|
|
self._history[user_id] = fresh
|
|
return fresh
|
|
|
|
async def format_for_prompt(self, user_id: str) -> str:
|
|
"""EXAONE에 전달할 대화 이력 포맷."""
|
|
msgs = await self.get(user_id)
|
|
lines = []
|
|
for m in msgs[-6:]: # 최근 6개만
|
|
prefix = "사용자" if m.role == "user" else "이드"
|
|
lines.append(f"{prefix}: {m.content}")
|
|
# pending_draft 표시
|
|
draft = self.get_pending_draft(user_id)
|
|
if draft:
|
|
lines.append(f"[시스템: pending_draft 있음 — {draft.get('title', '일정')} {draft.get('date', '')} {draft.get('time', '')}]")
|
|
if not lines:
|
|
return ""
|
|
return "\n".join(lines)
|
|
|
|
def set_pending_draft(self, user_id: str, draft_data: dict) -> None:
|
|
self._pending_drafts[user_id] = (draft_data, time())
|
|
|
|
def get_pending_draft(self, user_id: str) -> dict | None:
|
|
entry = self._pending_drafts.get(user_id)
|
|
if not entry:
|
|
return None
|
|
data, created_at = entry
|
|
if time() - created_at > DRAFT_TTL:
|
|
del self._pending_drafts[user_id]
|
|
return None
|
|
return data
|
|
|
|
def clear_pending_draft(self, user_id: str) -> None:
|
|
self._pending_drafts.pop(user_id, None)
|
|
|
|
|
|
conversation_store = ConversationStore()
|