"""Conversation summary persistence — single-row table."""
from datetime import datetime, timezone
class SummaryMixin:
"""Summary storage operations.
Expected on the composite class:
_get_pool() -> asyncpg.Pool
"""
async def get_summary(self, user_id: str | None = None) -> str | None:
pool = await self._get_pool()
async with pool.acquire() as conn:
if user_id is None:
return await conn.fetchval("SELECT content FROM memory_summary WHERE id=1 AND user_id IS NULL")
return await conn.fetchval("SELECT content FROM memory_summary WHERE id=1 AND user_id = $1", user_id)
async def set_summary(self, content: str, user_id: str | None = None) -> None:
now = datetime.now(timezone.utc)
pool = await self._get_pool()
async with pool.acquire() as conn:
await conn.execute(
"""INSERT INTO memory_summary (id, user_id, content, generated_at) VALUES (1, $1, $2, $3)
ON CONFLICT(id, user_id) DO UPDATE SET
content = EXCLUDED.content,
generated_at = EXCLUDED.generated_at""",
user_id, content, now,
)