"""SQLAlchemy-backed custom agent repository.""" from __future__ import annotations from datetime import UTC, datetime from typing import Any from sqlalchemy import func, or_, select from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker from deerflow.persistence.agents.base import AgentStore from deerflow.persistence.agents.model import AgentRow def _row_to_dict(row: AgentRow) -> dict[str, Any]: data = row.to_dict() data["builtin"] = data.get("user_id") is None for key in ("created_at", "updated_at"): value = data.get(key) if isinstance(value, datetime): data[key] = value.isoformat() return data class AgentRepository(AgentStore): def __init__(self, session_factory: async_sessionmaker[AsyncSession]) -> None: self._sf = session_factory @staticmethod def _visible_filter(user_id: str): return or_(AgentRow.user_id.is_(None), AgentRow.user_id == user_id, AgentRow.published.is_(True)) async def list_visible(self, user_id: str) -> list[dict[str, Any]]: stmt = select(AgentRow).where(self._visible_filter(user_id)).order_by(AgentRow.created_at.asc()) async with self._sf() as session: result = await session.execute(stmt) return [_row_to_dict(row) for row in result.scalars()] async def get_visible(self, agent_id: str, user_id: str) -> dict[str, Any] | None: stmt = select(AgentRow).where(AgentRow.id == agent_id, self._visible_filter(user_id)) async with self._sf() as session: row = (await session.execute(stmt)).scalar_one_or_none() return _row_to_dict(row) if row is not None else None async def get_any(self, agent_id: str) -> dict[str, Any] | None: async with self._sf() as session: row = await session.get(AgentRow, agent_id) return _row_to_dict(row) if row is not None else None async def get_owned(self, agent_id: str, user_id: str) -> dict[str, Any] | None: stmt = select(AgentRow).where(AgentRow.id == agent_id, AgentRow.user_id == user_id) async with self._sf() as session: row = (await session.execute(stmt)).scalar_one_or_none() return _row_to_dict(row) if row is not None else None async def create(self, data: dict[str, Any]) -> dict[str, Any]: now = datetime.now(UTC) row = AgentRow( id=data["id"], name=data["name"], description=data.get("description") or "", user_id=data.get("user_id"), published=bool(data.get("published", False)), created_at=now, updated_at=now, ) async with self._sf() as session: session.add(row) await session.commit() await session.refresh(row) return _row_to_dict(row) async def ensure_builtin(self, data: dict[str, Any]) -> dict[str, Any]: async with self._sf() as session: row = await session.get(AgentRow, data["id"]) if row is None: now = datetime.now(UTC) row = AgentRow( id=data["id"], name=data["name"], description=data.get("description") or "", user_id=None, published=bool(data.get("published", True)), created_at=now, updated_at=now, ) session.add(row) elif row.user_id is None: row.name = data["name"] row.description = data.get("description") or "" row.published = bool(data.get("published", True)) row.updated_at = datetime.now(UTC) await session.commit() await session.refresh(row) return _row_to_dict(row) async def update(self, agent_id: str, user_id: str, data: dict[str, Any]) -> dict[str, Any] | None: async with self._sf() as session: row = await session.get(AgentRow, agent_id) if row is None or row.user_id != user_id: return None for key in ("name", "description", "published"): if key in data: setattr(row, key, data[key]) row.updated_at = datetime.now(UTC) await session.commit() await session.refresh(row) return _row_to_dict(row) async def update_builtin(self, agent_id: str, data: dict[str, Any]) -> dict[str, Any] | None: async with self._sf() as session: row = await session.get(AgentRow, agent_id) if row is None or row.user_id is not None: return None for key in ("name", "description", "published"): if key in data: setattr(row, key, data[key]) row.updated_at = datetime.now(UTC) await session.commit() await session.refresh(row) return _row_to_dict(row) async def delete(self, agent_id: str, user_id: str) -> bool: async with self._sf() as session: row = await session.get(AgentRow, agent_id) if row is None or row.user_id != user_id: return False await session.delete(row) await session.commit() return True async def delete_builtin(self, agent_id: str) -> bool: async with self._sf() as session: row = await session.get(AgentRow, agent_id) if row is None or row.user_id is not None: return False await session.delete(row) await session.commit() return True async def count_owned(self, user_id: str) -> int: stmt = select(func.count()).select_from(AgentRow).where(AgentRow.user_id == user_id) async with self._sf() as session: return await session.scalar(stmt) or 0