from datetime import datetime from typing import Optional, List, Tuple from sqlalchemy import String, Text, TIMESTAMP, ForeignKey, Index, select, delete from sqlalchemy.orm import Mapped, mapped_column from sqlalchemy.ext.asyncio import AsyncSession from infra.database import Base, utc_now # ==================== ORM Model ==================== class AgentModel(Base): __tablename__ = "agents" # Primary key id: Mapped[int] = mapped_column(primary_key=True, autoincrement=True) # Agent unique identifier (external) agent_id: Mapped[str] = mapped_column(String(50), unique=True, nullable=False) # Foreign key to user table (user_id) owner_id: Mapped[str] = mapped_column( String(50), ForeignKey("user.user_id", ondelete="CASCADE"), nullable=False ) # Agent info name: Mapped[str] = mapped_column(String(100), nullable=False) avatar_url: Mapped[str | None] = mapped_column(Text, nullable=True) description: Mapped[str | None] = mapped_column(Text, nullable=True) # Agent configuration voice_id: Mapped[str | None] = mapped_column(String(50), nullable=True) instruction: Mapped[str] = mapped_column(Text, nullable=False) voice_opening: Mapped[str | None] = mapped_column(Text, nullable=True) voice_closing: Mapped[str | None] = mapped_column(Text, nullable=True) # Timestamps created_at: Mapped[datetime] = mapped_column( TIMESTAMP(timezone=True), default=utc_now, nullable=False ) updated_at: Mapped[datetime] = mapped_column( TIMESTAMP(timezone=True), default=utc_now, onupdate=utc_now, nullable=False ) __table_args__ = ( Index('idx_agents_agent_id', 'agent_id', unique=True), Index('idx_agents_owner_id', 'owner_id'), ) def __repr__(self): return f"" # ==================== Repository (CRUD Operations) ==================== class Agent: """ Agent Repository - Handles all database operations """ @staticmethod async def get_by_id(db: AsyncSession, agent_id: str) -> Optional[AgentModel]: """Get agent by agent_id""" result = await db.execute(select(AgentModel).where(AgentModel.agent_id == agent_id)) return result.scalar_one_or_none() @staticmethod async def get_agents_by_owner( db: AsyncSession, owner_id: str, cursor: Optional[str] = None, limit: int = 20 ) -> Tuple[List[AgentModel], Optional[str], bool]: """ Get agents owned by a user with cursor-based pagination Args: db: Database session owner_id: User ID cursor: ISO datetime string of last item's created_at limit: Number of items to return Returns: (agents, next_cursor, has_more) """ query = select(AgentModel).where(AgentModel.owner_id == owner_id) # Apply cursor filter if provided if cursor: try: cursor_time = datetime.fromisoformat(cursor) # Get records created before cursor time query = query.where(AgentModel.created_at < cursor_time) except (ValueError, TypeError): # Invalid cursor, ignore and start from beginning pass # Order by created_at DESC, agent_id DESC for stable sorting # Fetch limit + 1 to check if there are more records query = query.order_by( AgentModel.created_at.desc(), AgentModel.agent_id.desc() ).limit(limit + 1) result = await db.execute(query) agents: List[AgentModel] = result.scalars().all() # Check if there are more records has_more = len(agents) > limit if has_more: agents = agents[:limit] # Generate next cursor from last item next_cursor = None if has_more and agents: next_cursor = agents[-1].created_at.isoformat() return agents, next_cursor, has_more @staticmethod async def create( db: AsyncSession, agent_id: str, owner_id: str, name: str, instruction: str, avatar_url: Optional[str] = None, description: Optional[str] = None, voice_id: Optional[str] = None, voice_opening: Optional[str] = None, voice_closing: Optional[str] = None ) -> AgentModel: """Create a new agent""" agent = AgentModel( agent_id=agent_id, owner_id=owner_id, name=name, avatar_url=avatar_url, description=description, voice_id=voice_id, instruction=instruction, voice_opening=voice_opening, voice_closing=voice_closing ) db.add(agent) await db.commit() await db.refresh(agent) return agent @staticmethod async def update( db: AsyncSession, agent_id: str, **kwargs ) -> Optional[AgentModel]: """Update agent""" agent = await Agent.get_by_id(db, agent_id) if not agent: return None for key, value in kwargs.items(): if hasattr(agent, key): setattr(agent, key, value) await db.commit() await db.refresh(agent) return agent @staticmethod async def delete(db: AsyncSession, agent_id: str) -> bool: """Delete agent by agent_id""" result = await db.execute( delete(AgentModel).where(AgentModel.agent_id == agent_id) ) await db.commit() return result.rowcount > 0