Merge branch 'main' into Eliott

This commit is contained in:
Lowei 2026-06-17 19:48:09 +02:00
commit 0c8b186453
3 changed files with 159 additions and 0 deletions

View file

@ -460,6 +460,41 @@ async def build_context_for_model(
return joined
async def get_structured_history(
user_id: int | str,
max_recent: int = 12,
guild_id: str | None = None,
) -> tuple[str, List[Dict[str, str]]]:
"""Retourne le résumé global et l'historique structuré (utile pour l'API Chat)."""
data = await load_user_memory_async(user_id, guild_id)
resume = data.get("resume_global", "")
recent = data.get("historique_recent", [])
recent_tail = recent[-max_recent:] if recent else []
structured = []
import datetime
for m in recent_tail:
um = m.get("user_message", "").strip()
bm = m.get("bot_message", "").strip()
ts = m.get("timestamp")
time_prefix = ""
if ts:
try:
time_str = datetime.datetime.fromtimestamp(float(ts)).strftime("%H:%M:%S")
time_prefix = f"[{time_str}] "
except (ValueError, TypeError):
pass
if um:
structured.append({"role": "user", "content": f"{time_prefix}{um}"})
if bm:
structured.append({"role": "assistant", "content": f"{time_prefix}{bm}"})
return resume, structured
async def purge_old_memories(days: int = 30) -> int:
"""
Supprime les fichiers de mémoire plus vieux que X jours pour conformité RGPD.