xiaozhi-esp32-server/main/live-agent-api/orm/agent/repository.py

123 lines
3.2 KiB
Python

"""
Agent Repository
Data access layer for Agent entity.
Encapsulates all database operations for Agent.
"""
from typing import Optional
from sqlalchemy import select, func
from sqlalchemy.ext.asyncio import AsyncSession
from orm.agent.model import Agent
class AgentRepository:
"""Repository for Agent data access operations"""
def __init__(self, db: AsyncSession):
"""
Initialize repository with database session
Args:
db: SQLAlchemy async session
"""
self.db = db
async def find_by_id(self, agent_id: str) -> Optional[Agent]:
"""
Find agent by ID
Args:
agent_id: Agent unique ID
Returns:
Agent instance or None if not found
"""
stmt = select(Agent).where(Agent.id == agent_id)
result = await self.db.execute(stmt)
return result.scalar_one_or_none()
async def find_all(
self,
search: Optional[str] = None,
page: int = 1,
page_size: int = 20,
) -> tuple[list[Agent], int]:
"""
Find all agents with filtering and pagination (sorted by creation time desc)
Args:
search: Search keyword (searches in name and system_prompt)
page: Page number (starting from 1)
page_size: Number of items per page
Returns:
Tuple of (agents list, total count)
"""
# Build base query
stmt = select(Agent)
# Apply search filter
if search:
search_pattern = f"%{search}%"
stmt = stmt.where(Agent.name.ilike(search_pattern))
# Get total count
count_stmt = select(func.count()).select_from(stmt.subquery())
total_result = await self.db.execute(count_stmt)
total = total_result.scalar_one()
# Apply sorting
stmt = stmt.order_by(Agent.created_at.desc())
# Apply pagination
offset = (page - 1) * page_size
stmt = stmt.offset(offset).limit(page_size)
# Execute query
result = await self.db.execute(stmt)
agents = list(result.scalars().all())
return agents, total
async def save(self, agent: Agent) -> Agent:
"""
Save or update agent
Args:
agent: Agent instance to save
Returns:
Saved agent instance
"""
self.db.add(agent)
await self.db.flush()
await self.db.refresh(agent)
return agent
async def delete(self, agent: Agent) -> None:
"""
Delete agent
Args:
agent: Agent instance to delete
"""
await self.db.delete(agent)
await self.db.flush()
async def exists(self, agent_id: str) -> bool:
"""
Check if agent exists
Args:
agent_id: Agent unique ID
Returns:
True if agent exists, False otherwise
"""
stmt = select(func.count()).where(Agent.id == agent_id)
result = await self.db.execute(stmt)
count = result.scalar_one()
return count > 0