Skip to content

OpenRAG is a comprehensive, single package Retrieval-Augmented Generation platform built on Langflow, Docling, and Opensearch.

Python
4.7 KB
Raw Blame
"""Async CRUD over the ``conversations`` table — chat-history metadata."""

from datetime import UTC, datetime, timedelta
from typing import Any

from sqlalchemy import delete, select
from sqlalchemy.engine import CursorResult
from sqlalchemy.ext.asyncio import AsyncSession
from sqlmodel import col

from db.models import Conversation


class ConversationRepo:
    def __init__(self, session: AsyncSession):
        self.session = session

    async def get(self, response_id: str) -> Conversation | None:
        return await self.session.get(Conversation, response_id)

    async def list_for_user(self, user_id: str, limit: int = 200) -> list[Conversation]:
        result = await self.session.execute(
            select(Conversation)
            .where(col(Conversation.user_id) == user_id)
            .order_by(col(Conversation.last_activity).desc())
            .limit(limit)
        )
        return list(result.scalars().all())

    async def upsert(
        self,
        *,
        response_id: str,
        user_id: str,
        title: str | None = None,
        endpoint: str | None = None,
        previous_response_id: str | None = None,
        filter_id: str | None = None,
        total_messages: int = 0,
        created_at: datetime | None = None,
        last_activity: datetime | None = None,
    ) -> Conversation:
        now = datetime.now(UTC)
        existing = await self.get(response_id)
        if existing is None:
            row = Conversation(
                response_id=response_id,
                user_id=user_id,
                title=title,
                endpoint=endpoint,
                previous_response_id=previous_response_id,
                filter_id=filter_id,
                total_messages=total_messages,
                created_at=created_at or now,
                last_activity=last_activity or now,
            )
            self.session.add(row)
            await self.session.flush()
            return row
        # Update non-null fields
        if title is not None:
            existing.title = title
        if endpoint is not None:
            existing.endpoint = endpoint
        if previous_response_id is not None:
            existing.previous_response_id = previous_response_id
        if filter_id is not None:
            existing.filter_id = filter_id
        if total_messages:
            existing.total_messages = total_messages
        existing.last_activity = last_activity or now
        self.session.add(existing)
        await self.session.flush()
        return existing

    async def delete(self, response_id: str, user_id: str) -> bool:
        """Delete only if the row is owned by user_id."""
        row = await self.get(response_id)
        if row is None or row.user_id != user_id:
            return False
        await self.session.delete(row)
        await self.session.flush()
        return True

    async def delete_all_for_user(self, user_id: str) -> int:
        rows = await self.list_for_user(user_id, limit=10_000)
        for r in rows:
            await self.session.delete(r)
        await self.session.flush()
        return len(rows)

    async def delete_stale(self, ttl_days: int) -> int:
        """Delete conversations whose ``last_activity`` is older than *ttl_days*.

        Returns the number of rows deleted. Raises ``ValueError`` unless
        *ttl_days* >= 1: zero would delete nearly everything and a negative
        value would push the cutoff into the future, so neither is accepted.
        """
        if ttl_days < 1:
            raise ValueError(f"ttl_days must be at least 1, got {ttl_days}")

        # Must stay timezone-aware: SQLModel binds datetime fields through
        # UTCDateTime, which rejects naive values and normalises aware ones to UTC.
        cutoff = datetime.now(UTC) - timedelta(days=ttl_days)
        result: CursorResult = await self.session.execute(  # type: ignore[assignment]
            delete(Conversation).where(col(Conversation.last_activity) < cutoff)
        )
        await self.session.flush()
        return result.rowcount or 0

    async def to_metadata_dict(self, c: Conversation) -> dict[str, Any]:
        """JSON-shaped dict matching the legacy conversations.json
        per-conversation payload. Used by the service to keep API
        compatibility with existing call sites."""
        return {
            "response_id": c.response_id,
            "title": c.title,
            "endpoint": c.endpoint,
            "previous_response_id": c.previous_response_id,
            "filter_id": c.filter_id,
            "total_messages": c.total_messages or 0,
            "created_at": c.created_at.isoformat() if c.created_at else None,
            "last_activity": c.last_activity.isoformat() if c.last_activity else None,
        }