Restore active sessions on startup

This commit is contained in:
2026-05-12 08:04:59 -05:00
parent a09197e85a
commit bce93b39e0
3 changed files with 64 additions and 1 deletions

View File

@@ -3,7 +3,7 @@
import logging
import uuid
from datetime import datetime
from sqlalchemy import select, update
from sqlalchemy import func, select, update
from sqlalchemy.ext.asyncio import AsyncSession
from app.memory.models import (
@@ -56,6 +56,16 @@ class Repository:
result = await self.session.execute(stmt)
return result.scalars().first()
async def get_active_sessions(self) -> list[StreamSession]:
"""Retrieve sessions that are still marked active."""
stmt = (
select(StreamSession)
.where(StreamSession.is_active.is_(True))
.order_by(StreamSession.started_at.asc())
)
result = await self.session.execute(stmt)
return list(result.scalars().all())
# Chat Message operations
async def add_chat_message(
@@ -95,6 +105,14 @@ class Repository:
result = await self.session.execute(stmt)
return list(result.scalars().all())
async def count_messages(self, session_id: str) -> int:
"""Count chat messages stored for a session."""
stmt = select(func.count()).select_from(ChatMessage).where(
ChatMessage.session_id == session_id
)
result = await self.session.execute(stmt)
return result.scalar_one()
async def get_messages_since(
self, session_id: str, since: datetime
) -> list[ChatMessage]: