| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788 |
- # app/services/agent/tools/builtin/product_tools.py
- from typing import Optional, Dict, Any, List
- from sqlalchemy.ext.asyncio import AsyncSession
- from sqlalchemy import select, or_, desc
- from app.models.product import VasProduct
- from app.services.agent.tools.registry import agent_tool
- @agent_tool(
- name="search_products",
- description="检索可用的增值服务/签证/产品列表,支持按国家、签证类型或关键词搜索",
- )
- async def search_products(
- keyword: Optional[str] = None,
- country: Optional[str] = None,
- visa_type: Optional[str] = None,
- limit: int = 10,
- *,
- db: AsyncSession,
- ) -> Dict[str, Any]:
- """搜索并列出可用的产品"""
- limit = max(1, min(limit, 30))
- stmt = select(VasProduct).where(VasProduct.enabled == 1)
- if country and country.strip():
- stmt = stmt.where(VasProduct.country.ilike(f"%{country.strip()}%"))
- if visa_type and visa_type.strip():
- stmt = stmt.where(VasProduct.visa_type.ilike(f"%{visa_type.strip()}%"))
- if keyword and keyword.strip():
- kw = f"%{keyword.strip()}%"
- stmt = stmt.where(or_(VasProduct.title.ilike(kw), VasProduct.description.ilike(kw)))
- stmt = stmt.order_by(desc(VasProduct.recommend_score), desc(VasProduct.id)).limit(limit)
- res = await db.execute(stmt)
- products = res.scalars().all()
- items = []
- for p in products:
- items.append({
- "product_id": p.id,
- "title": p.title,
- "country": p.country,
- "city": p.city,
- "visa_type": p.visa_type,
- "price_amount": p.price_amount,
- "currency": p.price_currency,
- "description": p.description[:200] if p.description else "",
- })
- return {
- "count": len(items),
- "products": items,
- }
- @agent_tool(
- name="get_product_detail",
- description="获取指定商品ID的详细信息与配置说明",
- )
- async def get_product_detail(
- product_id: int,
- *,
- db: AsyncSession,
- ) -> Dict[str, Any]:
- """获取单个产品的详细信息"""
- stmt = select(VasProduct).where(VasProduct.id == product_id)
- res = await db.execute(stmt)
- p = res.scalar_one_or_none()
- if not p:
- return {"error": f"商品 ID {product_id} 不存在"}
- return {
- "product_id": p.id,
- "title": p.title,
- "country": p.country,
- "city": p.city,
- "visa_type": p.visa_type,
- "provider": p.provider,
- "price_amount": p.price_amount,
- "currency": p.price_currency,
- "enabled": bool(p.enabled),
- "description": p.description,
- "extra_fields": p.extra_fields,
- "schema_id": p.schema_id,
- }
|