Files
MoviePilot/app/agent/tools/impl/get_recommendations.py
jxxghp 055117d83d refactor: enhance tool message handling and improve error logging
- Updated _send_tool_message to accept a title parameter for better message context.
- Modified various tool implementations to utilize the new title parameter for clearer messaging.
- Improved error logging across multiple tools to include exception details for better debugging.
2025-10-31 09:16:53 +08:00

41 lines
1.9 KiB
Python

"""获取推荐工具"""
import json
from typing import Optional
from app.chain.recommend import RecommendChain
from app.log import logger
from app.agent.tools.base import MoviePilotTool
class GetRecommendationsTool(MoviePilotTool):
name: str = "get_recommendations"
description: str = "获取热门媒体推荐,包括电影、电视剧等热门内容。"
async def _arun(self, explanation: str, source: Optional[str] = "tmdb_trending",
media_type: Optional[str] = "all", limit: Optional[int] = 20) -> str:
logger.info(f"执行工具: {self.name}, 参数: source={source}, media_type={media_type}, limit={limit}")
try:
recommend_chain = RecommendChain()
results = []
if source == "tmdb_trending":
results = recommend_chain.tmdb_trending(limit=limit)
elif source == "douban_hot":
if media_type == "movie":
results = recommend_chain.douban_movie_hot(limit=limit)
elif media_type == "tv":
results = recommend_chain.douban_tv_hot(limit=limit)
else: # all
results.extend(recommend_chain.douban_movie_hot(limit=limit))
results.extend(recommend_chain.douban_tv_hot(limit=limit))
elif source == "bangumi_calendar":
results = recommend_chain.bangumi_calendar(limit=limit)
if results:
# 使用 to_dict() 方法
return json.dumps([r.to_dict() if hasattr(r, 'to_dict') else r.dict() if hasattr(r, 'dict') else r.model_dump() if hasattr(r, 'model_dump') else r for r in results], ensure_ascii=False, indent=2)
return "未找到推荐内容。"
except Exception as e:
logger.error(f"获取推荐失败: {e}", exc_info=True)
return f"获取推荐时发生错误: {str(e)}"