| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105 |
- # app/services/agent/tools/builtin/account_pool_tools.py
- from typing import Optional, Dict, Any, List
- from sqlalchemy.ext.asyncio import AsyncSession
- from sqlalchemy import select, desc
- from app.models.user import VasUser
- from app.models.account import Account
- from app.services.agent.tools.registry import agent_tool
- from app.core.logger import logger
- ADMIN_CHECK_MSG = "Permission denied: This operation is restricted to system administrators (admin)"
- @agent_tool(
- name="list_embassy_accounts",
- description="查询使领馆代预约账号池列表,支持按服务商/机构(如 VFS, TLS, TROOV)、国家、账号状态(0-未激活/无效, 1-正常可用, 2-风控锁定/冷却)及关键词筛选",
- )
- async def list_embassy_accounts(
- provider: Optional[str] = None,
- country: Optional[str] = None,
- status: Optional[int] = None,
- keyword: Optional[str] = None,
- limit: int = 15,
- offset: int = 0,
- *,
- db: AsyncSession,
- current_user: VasUser,
- ) -> Dict[str, Any]:
- """查询使馆预约账号池"""
- if current_user.role != "admin":
- return {"error": ADMIN_CHECK_MSG}
- limit = max(1, min(limit, 50))
- offset = max(0, offset)
- stmt = select(Account)
- if provider and provider.strip():
- stmt = stmt.where(Account.provider.ilike(f"%{provider.strip()}%"))
- if country and country.strip():
- stmt = stmt.where(Account.country.ilike(f"%{country.strip()}%"))
- if status is not None:
- stmt = stmt.where(Account.status == status)
- if keyword and keyword.strip():
- kw = f"%{keyword.strip()}%"
- stmt = stmt.where(Account.username.ilike(kw))
- stmt = stmt.order_by(desc(Account.id)).offset(offset).limit(limit)
- res = await db.execute(stmt)
- accounts = res.scalars().all()
- items = []
- for a in accounts:
- items.append({
- "account_id": a.id,
- "username": a.username,
- "provider": a.provider,
- "country": a.country,
- "status": a.status,
- "status_cn": "正常就绪" if a.status == 1 else ("锁定/冷却" if a.status == 2 else "已停用"),
- "used_count": getattr(a, "used_count", 0),
- "last_used_at": a.last_used_at.strftime("%Y-%m-%d %H:%M:%S") if getattr(a, "last_used_at", None) else None,
- })
- return {
- "count": len(items),
- "accounts": items,
- }
- @agent_tool(
- name="update_embassy_account_status",
- description="更新使馆账号池中指定账号的状态(1-设为正常, 2-设为冷却锁定, 0-设为停用)",
- is_dangerous=True,
- )
- async def update_embassy_account_status(
- account_id: int,
- status: int,
- *,
- db: AsyncSession,
- current_user: VasUser,
- ) -> Dict[str, Any]:
- """修改使馆账号状态"""
- if current_user.role != "admin":
- return {"error": ADMIN_CHECK_MSG}
- if status not in (0, 1, 2):
- return {"error": "Invalid status. Allowed values: 1 (active), 2 (locked), 0 (disabled)"}
- a = await db.get(Account, account_id)
- if not a:
- return {"error": f"Account #{account_id} not found"}
- old_status = a.status
- a.status = status
- await db.commit()
- await db.refresh(a)
- logger.info(f"👤 [AI_AGENT] Updated embassy account #{account_id}: status {old_status} -> {status} by admin={current_user.id}")
- return {
- "success": True,
- "account_id": a.id,
- "username": a.username,
- "previous_status": old_status,
- "new_status": a.status,
- }
|