account_pool_tools.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. # app/services/agent/tools/builtin/account_pool_tools.py
  2. from typing import Optional, Dict, Any, List
  3. from sqlalchemy.ext.asyncio import AsyncSession
  4. from sqlalchemy import select, desc
  5. from app.models.user import VasUser
  6. from app.models.account import Account
  7. from app.services.agent.tools.registry import agent_tool
  8. from app.core.logger import logger
  9. ADMIN_CHECK_MSG = "Permission denied: This operation is restricted to system administrators (admin)"
  10. @agent_tool(
  11. name="list_embassy_accounts",
  12. description="查询使领馆代预约账号池列表,支持按服务商/机构(如 VFS, TLS, TROOV)、国家、账号状态(0-未激活/无效, 1-正常可用, 2-风控锁定/冷却)及关键词筛选",
  13. )
  14. async def list_embassy_accounts(
  15. provider: Optional[str] = None,
  16. country: Optional[str] = None,
  17. status: Optional[int] = None,
  18. keyword: Optional[str] = None,
  19. limit: int = 15,
  20. offset: int = 0,
  21. *,
  22. db: AsyncSession,
  23. current_user: VasUser,
  24. ) -> Dict[str, Any]:
  25. """查询使馆预约账号池"""
  26. if current_user.role != "admin":
  27. return {"error": ADMIN_CHECK_MSG}
  28. limit = max(1, min(limit, 50))
  29. offset = max(0, offset)
  30. stmt = select(Account)
  31. if provider and provider.strip():
  32. stmt = stmt.where(Account.provider.ilike(f"%{provider.strip()}%"))
  33. if country and country.strip():
  34. stmt = stmt.where(Account.country.ilike(f"%{country.strip()}%"))
  35. if status is not None:
  36. stmt = stmt.where(Account.status == status)
  37. if keyword and keyword.strip():
  38. kw = f"%{keyword.strip()}%"
  39. stmt = stmt.where(Account.username.ilike(kw))
  40. stmt = stmt.order_by(desc(Account.id)).offset(offset).limit(limit)
  41. res = await db.execute(stmt)
  42. accounts = res.scalars().all()
  43. items = []
  44. for a in accounts:
  45. items.append({
  46. "account_id": a.id,
  47. "username": a.username,
  48. "provider": a.provider,
  49. "country": a.country,
  50. "status": a.status,
  51. "status_cn": "正常就绪" if a.status == 1 else ("锁定/冷却" if a.status == 2 else "已停用"),
  52. "used_count": getattr(a, "used_count", 0),
  53. "last_used_at": a.last_used_at.strftime("%Y-%m-%d %H:%M:%S") if getattr(a, "last_used_at", None) else None,
  54. })
  55. return {
  56. "count": len(items),
  57. "accounts": items,
  58. }
  59. @agent_tool(
  60. name="update_embassy_account_status",
  61. description="更新使馆账号池中指定账号的状态(1-设为正常, 2-设为冷却锁定, 0-设为停用)",
  62. is_dangerous=True,
  63. )
  64. async def update_embassy_account_status(
  65. account_id: int,
  66. status: int,
  67. *,
  68. db: AsyncSession,
  69. current_user: VasUser,
  70. ) -> Dict[str, Any]:
  71. """修改使馆账号状态"""
  72. if current_user.role != "admin":
  73. return {"error": ADMIN_CHECK_MSG}
  74. if status not in (0, 1, 2):
  75. return {"error": "Invalid status. Allowed values: 1 (active), 2 (locked), 0 (disabled)"}
  76. a = await db.get(Account, account_id)
  77. if not a:
  78. return {"error": f"Account #{account_id} not found"}
  79. old_status = a.status
  80. a.status = status
  81. await db.commit()
  82. await db.refresh(a)
  83. logger.info(f"👤 [AI_AGENT] Updated embassy account #{account_id}: status {old_status} -> {status} by admin={current_user.id}")
  84. return {
  85. "success": True,
  86. "account_id": a.id,
  87. "username": a.username,
  88. "previous_status": old_status,
  89. "new_status": a.status,
  90. }