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.
This commit is contained in:
jxxghp
2025-10-31 09:16:53 +08:00
parent c6baf43986
commit 055117d83d
9 changed files with 115 additions and 50 deletions

View File

@@ -17,21 +17,36 @@ class AddSubscribeTool(MoviePilotTool):
logger.info(f"执行工具: {self.name}, 参数: title={title}, year={year}, media_type={media_type}, season={season}, tmdb_id={tmdb_id}")
# 发送工具执行说明
self._send_tool_message(f"正在添加订阅: {title} ({year}) - {media_type}", "info")
self._send_tool_message(f"正在添加订阅: {title} ({year}) - {media_type}", title="添加订阅")
try:
subscribe_chain = SubscribeChain()
sid, message = subscribe_chain.add(mtype=MediaType(media_type), title=title, year=year,
tmdbid=tmdb_id, season=season, username=self._user_id)
# 转换 tmdb_id 为整数
tmdbid_int = None
if tmdb_id:
try:
tmdbid_int = int(tmdb_id)
except (ValueError, TypeError):
logger.warning(f"无效的 tmdb_id: {tmdb_id},将忽略")
sid, message = subscribe_chain.add(
mtype=MediaType(media_type),
title=title,
year=year,
tmdbid=tmdbid_int,
season=season,
username=self._user_id
)
if sid:
success_message = f"成功添加订阅:{title} ({year})"
self._send_tool_message(success_message, "success")
self._send_tool_message(success_message, title="订阅成功")
return success_message
else:
error_message = f"添加订阅失败:{message}"
self._send_tool_message(error_message, "error")
self._send_tool_message(error_message, title="订阅失败")
return error_message
except Exception as e:
error_message = f"添加订阅时发生错误: {str(e)}"
self._send_tool_message(error_message, "error")
logger.error(f"添加订阅失败: {e}", exc_info=True)
self._send_tool_message(error_message, title="订阅失败")
return error_message