mirror of
https://github.com/jxxghp/MoviePilot.git
synced 2026-03-20 03:57:30 +08:00
优化Agent上下文大小
This commit is contained in:
@@ -60,7 +60,7 @@ class MoviePilotTool(BaseTool, metaclass=ABCMeta):
|
|||||||
"""设置回调处理器"""
|
"""设置回调处理器"""
|
||||||
self._callback_handler = callback_handler
|
self._callback_handler = callback_handler
|
||||||
|
|
||||||
async def send_tool_message(self, message: str, title: str = ""):
|
async def send_tool_message(self, message: str, title: str = "MoviePilot助手"):
|
||||||
"""发送工具消息"""
|
"""发送工具消息"""
|
||||||
await ToolChain().async_post_message(
|
await ToolChain().async_post_message(
|
||||||
Notification(
|
Notification(
|
||||||
|
|||||||
@@ -51,8 +51,33 @@ class GetRecommendationsTool(MoviePilotTool):
|
|||||||
results = recommend_chain.bangumi_calendar(limit=limit)
|
results = recommend_chain.bangumi_calendar(limit=limit)
|
||||||
|
|
||||||
if results:
|
if results:
|
||||||
# 使用 to_dict() 方法
|
# 限制最多20条结果
|
||||||
return json.dumps(results)
|
total_count = len(results)
|
||||||
|
limited_results = results[:20]
|
||||||
|
# 精简字段,只保留关键信息
|
||||||
|
simplified_results = []
|
||||||
|
for r in limited_results:
|
||||||
|
# r 已经是字典格式(to_dict的结果)
|
||||||
|
simplified = {
|
||||||
|
"title": r.get("title"),
|
||||||
|
"en_title": r.get("en_title"),
|
||||||
|
"year": r.get("year"),
|
||||||
|
"type": r.get("type"),
|
||||||
|
"season": r.get("season"),
|
||||||
|
"tmdb_id": r.get("tmdb_id"),
|
||||||
|
"imdb_id": r.get("imdb_id"),
|
||||||
|
"douban_id": r.get("douban_id"),
|
||||||
|
"overview": r.get("overview", "")[:200] + "..." if r.get("overview") and len(r.get("overview", "")) > 200 else r.get("overview"),
|
||||||
|
"vote_average": r.get("vote_average"),
|
||||||
|
"poster_path": r.get("poster_path"),
|
||||||
|
"detail_link": r.get("detail_link")
|
||||||
|
}
|
||||||
|
simplified_results.append(simplified)
|
||||||
|
result_json = json.dumps(simplified_results, ensure_ascii=False, indent=2)
|
||||||
|
# 如果结果被裁剪,添加提示信息
|
||||||
|
if total_count > 20:
|
||||||
|
return f"注意:推荐结果共找到 {total_count} 条,为节省上下文空间,仅显示前 20 条结果。\n\n{result_json}"
|
||||||
|
return result_json
|
||||||
return "未找到推荐内容。"
|
return "未找到推荐内容。"
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f"获取推荐失败: {e}", exc_info=True)
|
logger.error(f"获取推荐失败: {e}", exc_info=True)
|
||||||
|
|||||||
@@ -39,7 +39,41 @@ class QueryDownloadsTool(MoviePilotTool):
|
|||||||
continue
|
continue
|
||||||
filtered_downloads.append(dl)
|
filtered_downloads.append(dl)
|
||||||
if filtered_downloads:
|
if filtered_downloads:
|
||||||
return json.dumps([d.model_dump() for d in filtered_downloads])
|
# 限制最多20条结果
|
||||||
|
total_count = len(filtered_downloads)
|
||||||
|
limited_downloads = filtered_downloads[:20]
|
||||||
|
# 精简字段,只保留关键信息
|
||||||
|
simplified_downloads = []
|
||||||
|
for d in limited_downloads:
|
||||||
|
simplified = {
|
||||||
|
"downloader": d.downloader,
|
||||||
|
"hash": d.hash,
|
||||||
|
"title": d.title,
|
||||||
|
"name": d.name,
|
||||||
|
"year": d.year,
|
||||||
|
"season_episode": d.season_episode,
|
||||||
|
"size": d.size,
|
||||||
|
"progress": d.progress,
|
||||||
|
"state": d.state,
|
||||||
|
"upspeed": d.upspeed,
|
||||||
|
"dlspeed": d.dlspeed,
|
||||||
|
"left_time": d.left_time
|
||||||
|
}
|
||||||
|
# 精简 media 字段
|
||||||
|
if d.media:
|
||||||
|
simplified["media"] = {
|
||||||
|
"tmdbid": d.media.get("tmdbid"),
|
||||||
|
"type": d.media.get("type"),
|
||||||
|
"title": d.media.get("title"),
|
||||||
|
"season": d.media.get("season"),
|
||||||
|
"episode": d.media.get("episode")
|
||||||
|
}
|
||||||
|
simplified_downloads.append(simplified)
|
||||||
|
result_json = json.dumps(simplified_downloads, ensure_ascii=False, indent=2)
|
||||||
|
# 如果结果被裁剪,添加提示信息
|
||||||
|
if total_count > 20:
|
||||||
|
return f"注意:查询结果共找到 {total_count} 条,为节省上下文空间,仅显示前 20 条结果。\n\n{result_json}"
|
||||||
|
return result_json
|
||||||
return "未找到相关下载任务。"
|
return "未找到相关下载任务。"
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f"查询下载失败: {e}", exc_info=True)
|
logger.error(f"查询下载失败: {e}", exc_info=True)
|
||||||
|
|||||||
@@ -37,8 +37,37 @@ class QuerySubscribesTool(MoviePilotTool):
|
|||||||
continue
|
continue
|
||||||
filtered_subscribes.append(sub)
|
filtered_subscribes.append(sub)
|
||||||
if filtered_subscribes:
|
if filtered_subscribes:
|
||||||
return json.dumps([s.to_dict() for s in filtered_subscribes], ensure_ascii=False, indent=2)
|
# 限制最多20条结果
|
||||||
return "未找到相关订阅。"
|
total_count = len(filtered_subscribes)
|
||||||
|
limited_subscribes = filtered_subscribes[:20]
|
||||||
|
# 精简字段,只保留关键信息
|
||||||
|
simplified_subscribes = []
|
||||||
|
for s in limited_subscribes:
|
||||||
|
simplified = {
|
||||||
|
"id": s.id,
|
||||||
|
"name": s.name,
|
||||||
|
"year": s.year,
|
||||||
|
"type": s.type,
|
||||||
|
"season": s.season,
|
||||||
|
"tmdbid": s.tmdbid,
|
||||||
|
"doubanid": s.doubanid,
|
||||||
|
"bangumiid": s.bangumiid,
|
||||||
|
"poster": s.poster,
|
||||||
|
"vote": s.vote,
|
||||||
|
"description": s.description[:200] + "..." if s.description and len(s.description) > 200 else s.description,
|
||||||
|
"state": s.state,
|
||||||
|
"total_episode": s.total_episode,
|
||||||
|
"lack_episode": s.lack_episode,
|
||||||
|
"last_update": s.last_update,
|
||||||
|
"username": s.username
|
||||||
|
}
|
||||||
|
simplified_subscribes.append(simplified)
|
||||||
|
result_json = json.dumps(simplified_subscribes, ensure_ascii=False, indent=2)
|
||||||
|
# 如果结果被裁剪,添加提示信息
|
||||||
|
if total_count > 20:
|
||||||
|
return f"注意:查询结果共找到 {total_count} 条,为节省上下文空间,仅显示前 20 条结果。\n\n{result_json}"
|
||||||
|
return result_json
|
||||||
|
return "未找到相关订阅"
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f"查询订阅失败: {e}", exc_info=True)
|
logger.error(f"查询订阅失败: {e}", exc_info=True)
|
||||||
return f"查询订阅时发生错误: {str(e)}"
|
return f"查询订阅时发生错误: {str(e)}"
|
||||||
|
|||||||
@@ -60,7 +60,32 @@ class SearchMediaTool(MoviePilotTool):
|
|||||||
filtered_results.append(result)
|
filtered_results.append(result)
|
||||||
|
|
||||||
if filtered_results:
|
if filtered_results:
|
||||||
return json.dumps([r.to_dict() for r in filtered_results], ensure_ascii=False, indent=2)
|
# 限制最多20条结果
|
||||||
|
total_count = len(filtered_results)
|
||||||
|
limited_results = filtered_results[:20]
|
||||||
|
# 精简字段,只保留关键信息
|
||||||
|
simplified_results = []
|
||||||
|
for r in limited_results:
|
||||||
|
simplified = {
|
||||||
|
"title": r.title,
|
||||||
|
"en_title": r.en_title,
|
||||||
|
"year": r.year,
|
||||||
|
"type": r.type.value if r.type else None,
|
||||||
|
"season": r.season,
|
||||||
|
"tmdb_id": r.tmdb_id,
|
||||||
|
"imdb_id": r.imdb_id,
|
||||||
|
"douban_id": r.douban_id,
|
||||||
|
"overview": r.overview[:200] + "..." if r.overview and len(r.overview) > 200 else r.overview,
|
||||||
|
"vote_average": r.vote_average,
|
||||||
|
"poster_path": r.poster_path,
|
||||||
|
"detail_link": r.detail_link
|
||||||
|
}
|
||||||
|
simplified_results.append(simplified)
|
||||||
|
result_json = json.dumps(simplified_results, ensure_ascii=False, indent=2)
|
||||||
|
# 如果结果被裁剪,添加提示信息
|
||||||
|
if total_count > 20:
|
||||||
|
return f"注意:搜索结果共找到 {total_count} 条,为节省上下文空间,仅显示前 20 条结果。\n\n{result_json}"
|
||||||
|
return result_json
|
||||||
else:
|
else:
|
||||||
return f"未找到符合条件的媒体资源: {title}"
|
return f"未找到符合条件的媒体资源: {title}"
|
||||||
else:
|
else:
|
||||||
|
|||||||
@@ -52,7 +52,52 @@ class SearchTorrentsTool(MoviePilotTool):
|
|||||||
filtered_torrents.append(torrent)
|
filtered_torrents.append(torrent)
|
||||||
|
|
||||||
if filtered_torrents:
|
if filtered_torrents:
|
||||||
return json.dumps([t.to_dict() for t in filtered_torrents], ensure_ascii=False, indent=2)
|
# 限制最多50条结果
|
||||||
|
total_count = len(filtered_torrents)
|
||||||
|
limited_torrents = filtered_torrents[:50]
|
||||||
|
# 精简字段,只保留关键信息
|
||||||
|
simplified_torrents = []
|
||||||
|
for t in limited_torrents:
|
||||||
|
simplified = {}
|
||||||
|
# 精简 torrent_info
|
||||||
|
if t.torrent_info:
|
||||||
|
simplified["torrent_info"] = {
|
||||||
|
"title": t.torrent_info.title,
|
||||||
|
"size": t.torrent_info.size,
|
||||||
|
"seeders": t.torrent_info.seeders,
|
||||||
|
"peers": t.torrent_info.peers,
|
||||||
|
"site_name": t.torrent_info.site_name,
|
||||||
|
"enclosure": t.torrent_info.enclosure,
|
||||||
|
"page_url": t.torrent_info.page_url,
|
||||||
|
"volume_factor": t.torrent_info.volume_factor,
|
||||||
|
"pubdate": t.torrent_info.pubdate
|
||||||
|
}
|
||||||
|
# 精简 media_info
|
||||||
|
if t.media_info:
|
||||||
|
simplified["media_info"] = {
|
||||||
|
"title": t.media_info.title,
|
||||||
|
"en_title": t.media_info.en_title,
|
||||||
|
"year": t.media_info.year,
|
||||||
|
"type": t.media_info.type.value if t.media_info.type else None,
|
||||||
|
"season": t.media_info.season,
|
||||||
|
"tmdb_id": t.media_info.tmdb_id
|
||||||
|
}
|
||||||
|
# 精简 meta_info
|
||||||
|
if t.meta_info:
|
||||||
|
simplified["meta_info"] = {
|
||||||
|
"name": t.meta_info.name,
|
||||||
|
"cn_name": t.meta_info.cn_name,
|
||||||
|
"en_name": t.meta_info.en_name,
|
||||||
|
"year": t.meta_info.year,
|
||||||
|
"type": t.meta_info.type.value if t.meta_info.type else None,
|
||||||
|
"begin_season": t.meta_info.begin_season
|
||||||
|
}
|
||||||
|
simplified_torrents.append(simplified)
|
||||||
|
result_json = json.dumps(simplified_torrents, ensure_ascii=False, indent=2)
|
||||||
|
# 如果结果被裁剪,添加提示信息
|
||||||
|
if total_count > 50:
|
||||||
|
return f"注意:搜索结果共找到 {total_count} 条,为节省上下文空间,仅显示前 50 条结果。\n\n{result_json}"
|
||||||
|
return result_json
|
||||||
else:
|
else:
|
||||||
return f"未找到相关种子资源: {title}"
|
return f"未找到相关种子资源: {title}"
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
|
|||||||
@@ -868,7 +868,7 @@ class MessageChain(ChainBase):
|
|||||||
source=source,
|
source=source,
|
||||||
userid=userid,
|
userid=userid,
|
||||||
username=username,
|
username=username,
|
||||||
title="正在处理您的请求,请稍候..."
|
title="MoviePilot助手已收到您的请求,请稍候..."
|
||||||
))
|
))
|
||||||
|
|
||||||
# 生成会话ID
|
# 生成会话ID
|
||||||
|
|||||||
Reference in New Issue
Block a user