product_tools.py 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. # app/services/agent/tools/builtin/product_tools.py
  2. from typing import Optional, Dict, Any, List
  3. from sqlalchemy.ext.asyncio import AsyncSession
  4. from sqlalchemy import select, or_, desc
  5. from app.models.product import VasProduct
  6. from app.services.agent.tools.registry import agent_tool
  7. @agent_tool(
  8. name="search_products",
  9. description="检索可用的增值服务/签证/产品列表,支持按国家、签证类型或关键词搜索",
  10. )
  11. async def search_products(
  12. keyword: Optional[str] = None,
  13. country: Optional[str] = None,
  14. visa_type: Optional[str] = None,
  15. limit: int = 10,
  16. *,
  17. db: AsyncSession,
  18. ) -> Dict[str, Any]:
  19. """搜索并列出可用的产品"""
  20. limit = max(1, min(limit, 30))
  21. stmt = select(VasProduct).where(VasProduct.enabled == 1)
  22. if country and country.strip():
  23. stmt = stmt.where(VasProduct.country.ilike(f"%{country.strip()}%"))
  24. if visa_type and visa_type.strip():
  25. stmt = stmt.where(VasProduct.visa_type.ilike(f"%{visa_type.strip()}%"))
  26. if keyword and keyword.strip():
  27. kw = f"%{keyword.strip()}%"
  28. stmt = stmt.where(or_(VasProduct.title.ilike(kw), VasProduct.description.ilike(kw)))
  29. stmt = stmt.order_by(desc(VasProduct.recommend_score), desc(VasProduct.id)).limit(limit)
  30. res = await db.execute(stmt)
  31. products = res.scalars().all()
  32. items = []
  33. for p in products:
  34. items.append({
  35. "product_id": p.id,
  36. "title": p.title,
  37. "country": p.country,
  38. "city": p.city,
  39. "visa_type": p.visa_type,
  40. "price_amount": p.price_amount,
  41. "currency": p.price_currency,
  42. "description": p.description[:200] if p.description else "",
  43. })
  44. return {
  45. "count": len(items),
  46. "products": items,
  47. }
  48. @agent_tool(
  49. name="get_product_detail",
  50. description="获取指定商品ID的详细信息与配置说明",
  51. )
  52. async def get_product_detail(
  53. product_id: int,
  54. *,
  55. db: AsyncSession,
  56. ) -> Dict[str, Any]:
  57. """获取单个产品的详细信息"""
  58. stmt = select(VasProduct).where(VasProduct.id == product_id)
  59. res = await db.execute(stmt)
  60. p = res.scalar_one_or_none()
  61. if not p:
  62. return {"error": f"Product ID {product_id} does not exist"}
  63. return {
  64. "product_id": p.id,
  65. "title": p.title,
  66. "country": p.country,
  67. "city": p.city,
  68. "visa_type": p.visa_type,
  69. "provider": p.provider,
  70. "price_amount": p.price_amount,
  71. "currency": p.price_currency,
  72. "enabled": bool(p.enabled),
  73. "description": p.description,
  74. "extra_fields": p.extra_fields,
  75. "schema_id": p.schema_id,
  76. }
  77. @agent_tool(
  78. name="admin_list_all_products",
  79. description="管理员查看系统所有签证商品(包含已启用与已下架的商品),支持按国家、服务商筛选",
  80. )
  81. async def admin_list_all_products(
  82. country: Optional[str] = None,
  83. provider: Optional[str] = None,
  84. enabled: Optional[bool] = None,
  85. limit: int = 20,
  86. *,
  87. db: AsyncSession,
  88. ) -> Dict[str, Any]:
  89. """管理员全景查询商品列表"""
  90. stmt = select(VasProduct)
  91. if enabled is not None:
  92. stmt = stmt.where(VasProduct.enabled == (1 if enabled else 0))
  93. if country and country.strip():
  94. stmt = stmt.where(VasProduct.country.ilike(f"%{country.strip()}%"))
  95. if provider and provider.strip():
  96. stmt = stmt.where(VasProduct.provider.ilike(f"%{provider.strip()}%"))
  97. stmt = stmt.order_by(desc(VasProduct.id)).limit(max(1, min(limit, 50)))
  98. res = await db.execute(stmt)
  99. products = res.scalars().all()
  100. items = []
  101. for p in products:
  102. items.append({
  103. "product_id": p.id,
  104. "title": p.title,
  105. "country": p.country,
  106. "city": p.city,
  107. "visa_type": p.visa_type,
  108. "provider": p.provider,
  109. "price_amount": p.price_amount,
  110. "currency": p.price_currency,
  111. "enabled": bool(p.enabled),
  112. "recommend_score": p.recommend_score,
  113. })
  114. return {
  115. "count": len(items),
  116. "products": items,
  117. }
  118. @agent_tool(
  119. name="admin_toggle_product_status",
  120. description="上架或下架指定商品(enabled: true-上架启用, false-下架停用)",
  121. is_dangerous=True,
  122. )
  123. async def admin_toggle_product_status(
  124. product_id: int,
  125. enabled: bool,
  126. *,
  127. db: AsyncSession,
  128. ) -> Dict[str, Any]:
  129. """更新商品上下架状态"""
  130. stmt = select(VasProduct).where(VasProduct.id == product_id)
  131. res = await db.execute(stmt)
  132. p = res.scalar_one_or_none()
  133. if not p:
  134. return {"error": f"Product ID {product_id} does not exist"}
  135. old_status = bool(p.enabled)
  136. p.enabled = 1 if enabled else 0
  137. await db.commit()
  138. await db.refresh(p)
  139. return {
  140. "success": True,
  141. "product_id": p.id,
  142. "title": p.title,
  143. "previous_status": "enabled" if old_status else "disabled",
  144. "new_status": "enabled" if enabled else "disabled",
  145. }
  146. @agent_tool(
  147. name="admin_update_product_price",
  148. description="修改指定商品的服务费用单价(price_amount 单位为分,如 35000 代表 350 元)",
  149. is_dangerous=True,
  150. )
  151. async def admin_update_product_price(
  152. product_id: int,
  153. price_amount: int,
  154. *,
  155. db: AsyncSession,
  156. ) -> Dict[str, Any]:
  157. """修改商品单价"""
  158. if price_amount <= 0:
  159. return {"error": "Price amount must be greater than 0"}
  160. stmt = select(VasProduct).where(VasProduct.id == product_id)
  161. res = await db.execute(stmt)
  162. p = res.scalar_one_or_none()
  163. if not p:
  164. return {"error": f"Product ID {product_id} does not exist"}
  165. old_price = p.price_amount
  166. p.price_amount = price_amount
  167. await db.commit()
  168. await db.refresh(p)
  169. return {
  170. "success": True,
  171. "product_id": p.id,
  172. "title": p.title,
  173. "previous_price": old_price,
  174. "new_price": p.price_amount,
  175. "currency": p.price_currency,
  176. }