mirror of
https://github.com/jxxghp/MoviePilot.git
synced 2026-03-20 03:57:30 +08:00
add ai agent
This commit is contained in:
293
app/agent/tools/Agent Tools v1.0.json
Normal file
293
app/agent/tools/Agent Tools v1.0.json
Normal file
@@ -0,0 +1,293 @@
|
||||
[
|
||||
{
|
||||
"description": "搜索媒体资源,包括电影、电视剧、动漫等。可以根据标题、年份、类型等条件进行搜索。",
|
||||
"name": "search_media",
|
||||
"parameters": {
|
||||
"properties": {
|
||||
"explanation": {
|
||||
"description": "使用此工具的原因说明",
|
||||
"type": "string"
|
||||
},
|
||||
"title": {
|
||||
"description": "媒体标题",
|
||||
"type": "string"
|
||||
},
|
||||
"year": {
|
||||
"description": "年份(可选)",
|
||||
"type": "string"
|
||||
},
|
||||
"media_type": {
|
||||
"description": "媒体类型:movie(电影)、tv(电视剧)、anime(动漫)",
|
||||
"type": "string"
|
||||
},
|
||||
"season": {
|
||||
"description": "季数(仅电视剧和动漫)",
|
||||
"type": "integer"
|
||||
}
|
||||
},
|
||||
"required": [
|
||||
"title",
|
||||
"explanation"
|
||||
],
|
||||
"type": "object"
|
||||
}
|
||||
},
|
||||
{
|
||||
"description": "添加媒体订阅,为用户感兴趣的媒体内容创建订阅规则。",
|
||||
"name": "add_subscribe",
|
||||
"parameters": {
|
||||
"properties": {
|
||||
"explanation": {
|
||||
"description": "使用此工具的原因说明",
|
||||
"type": "string"
|
||||
},
|
||||
"title": {
|
||||
"description": "媒体标题",
|
||||
"type": "string"
|
||||
},
|
||||
"year": {
|
||||
"description": "年份",
|
||||
"type": "string"
|
||||
},
|
||||
"media_type": {
|
||||
"description": "媒体类型:movie(电影)、tv(电视剧)",
|
||||
"type": "string"
|
||||
},
|
||||
"season": {
|
||||
"description": "季数(仅电视剧)",
|
||||
"type": "integer"
|
||||
},
|
||||
"tmdb_id": {
|
||||
"description": "TMDB ID(可选)",
|
||||
"type": "string"
|
||||
}
|
||||
},
|
||||
"required": [
|
||||
"title",
|
||||
"year",
|
||||
"media_type",
|
||||
"explanation"
|
||||
],
|
||||
"type": "object"
|
||||
}
|
||||
},
|
||||
{
|
||||
"description": "搜索站点种子资源,根据媒体信息搜索可下载的种子文件。",
|
||||
"name": "search_torrents",
|
||||
"parameters": {
|
||||
"properties": {
|
||||
"explanation": {
|
||||
"description": "使用此工具的原因说明",
|
||||
"type": "string"
|
||||
},
|
||||
"title": {
|
||||
"description": "资源标题",
|
||||
"type": "string"
|
||||
},
|
||||
"year": {
|
||||
"description": "年份(可选)",
|
||||
"type": "string"
|
||||
},
|
||||
"media_type": {
|
||||
"description": "媒体类型:movie(电影)、tv(电视剧)",
|
||||
"type": "string"
|
||||
},
|
||||
"season": {
|
||||
"description": "季数(仅电视剧)",
|
||||
"type": "integer"
|
||||
},
|
||||
"sites": {
|
||||
"description": "搜索的站点ID列表(可选)",
|
||||
"items": {
|
||||
"type": "integer"
|
||||
},
|
||||
"type": "array"
|
||||
}
|
||||
},
|
||||
"required": [
|
||||
"title",
|
||||
"explanation"
|
||||
],
|
||||
"type": "object"
|
||||
}
|
||||
},
|
||||
{
|
||||
"description": "添加下载任务,将搜索到的种子资源添加到下载器。",
|
||||
"name": "add_download",
|
||||
"parameters": {
|
||||
"properties": {
|
||||
"explanation": {
|
||||
"description": "使用此工具的原因说明",
|
||||
"type": "string"
|
||||
},
|
||||
"torrent_title": {
|
||||
"description": "种子标题",
|
||||
"type": "string"
|
||||
},
|
||||
"torrent_url": {
|
||||
"description": "种子下载链接",
|
||||
"type": "string"
|
||||
},
|
||||
"downloader": {
|
||||
"description": "下载器名称(可选)",
|
||||
"type": "string"
|
||||
},
|
||||
"save_path": {
|
||||
"description": "保存路径(可选)",
|
||||
"type": "string"
|
||||
},
|
||||
"labels": {
|
||||
"description": "标签(可选,多个用逗号分隔)",
|
||||
"type": "string"
|
||||
}
|
||||
},
|
||||
"required": [
|
||||
"torrent_title",
|
||||
"torrent_url",
|
||||
"explanation"
|
||||
],
|
||||
"type": "object"
|
||||
}
|
||||
},
|
||||
{
|
||||
"description": "查询订阅状态,查看用户的订阅列表和状态。",
|
||||
"name": "query_subscribes",
|
||||
"parameters": {
|
||||
"properties": {
|
||||
"explanation": {
|
||||
"description": "使用此工具的原因说明",
|
||||
"type": "string"
|
||||
},
|
||||
"status": {
|
||||
"description": "订阅状态过滤:active(活跃)、inactive(非活跃)、all(全部)",
|
||||
"type": "string"
|
||||
},
|
||||
"media_type": {
|
||||
"description": "媒体类型过滤:movie(电影)、tv(电视剧)、all(全部)",
|
||||
"type": "string"
|
||||
}
|
||||
},
|
||||
"required": [
|
||||
"explanation"
|
||||
],
|
||||
"type": "object"
|
||||
}
|
||||
},
|
||||
{
|
||||
"description": "查询下载状态,查看下载器的任务列表和进度。",
|
||||
"name": "query_downloads",
|
||||
"parameters": {
|
||||
"properties": {
|
||||
"explanation": {
|
||||
"description": "使用此工具的原因说明",
|
||||
"type": "string"
|
||||
},
|
||||
"downloader": {
|
||||
"description": "下载器名称(可选)",
|
||||
"type": "string"
|
||||
},
|
||||
"status": {
|
||||
"description": "下载状态过滤:downloading(下载中)、completed(已完成)、paused(暂停)、all(全部)",
|
||||
"type": "string"
|
||||
}
|
||||
},
|
||||
"required": [
|
||||
"explanation"
|
||||
],
|
||||
"type": "object"
|
||||
}
|
||||
},
|
||||
{
|
||||
"description": "查询下载器配置,查看可用的下载器列表和配置信息。",
|
||||
"name": "query_downloaders",
|
||||
"parameters": {
|
||||
"properties": {
|
||||
"explanation": {
|
||||
"description": "使用此工具的原因说明",
|
||||
"type": "string"
|
||||
}
|
||||
},
|
||||
"required": [
|
||||
"explanation"
|
||||
],
|
||||
"type": "object"
|
||||
}
|
||||
},
|
||||
{
|
||||
"description": "获取热门媒体推荐,包括电影、电视剧等热门内容。",
|
||||
"name": "get_recommendations",
|
||||
"parameters": {
|
||||
"properties": {
|
||||
"explanation": {
|
||||
"description": "使用此工具的原因说明",
|
||||
"type": "string"
|
||||
},
|
||||
"source": {
|
||||
"description": "推荐来源:tmdb_trending(TMDB热门)、douban_hot(豆瓣热门)、bangumi_calendar(Bangumi日历)",
|
||||
"type": "string"
|
||||
},
|
||||
"media_type": {
|
||||
"description": "媒体类型:movie(电影)、tv(电视剧)、all(全部)",
|
||||
"type": "string"
|
||||
},
|
||||
"limit": {
|
||||
"description": "返回数量限制(默认20)",
|
||||
"type": "integer"
|
||||
}
|
||||
},
|
||||
"required": [
|
||||
"explanation"
|
||||
],
|
||||
"type": "object"
|
||||
}
|
||||
},
|
||||
{
|
||||
"description": "查询媒体库状态,查看已入库的媒体文件情况。",
|
||||
"name": "query_media_library",
|
||||
"parameters": {
|
||||
"properties": {
|
||||
"explanation": {
|
||||
"description": "使用此工具的原因说明",
|
||||
"type": "string"
|
||||
},
|
||||
"media_type": {
|
||||
"description": "媒体类型:movie(电影)、tv(电视剧)、all(全部)",
|
||||
"type": "string"
|
||||
},
|
||||
"title": {
|
||||
"description": "媒体标题(可选,用于精确查询)",
|
||||
"type": "string"
|
||||
}
|
||||
},
|
||||
"required": [
|
||||
"explanation"
|
||||
],
|
||||
"type": "object"
|
||||
}
|
||||
},
|
||||
{
|
||||
"description": "发送消息通知,向用户发送操作结果或重要信息。",
|
||||
"name": "send_message",
|
||||
"parameters": {
|
||||
"properties": {
|
||||
"explanation": {
|
||||
"description": "使用此工具的原因说明",
|
||||
"type": "string"
|
||||
},
|
||||
"message": {
|
||||
"description": "要发送的消息内容",
|
||||
"type": "string"
|
||||
},
|
||||
"message_type": {
|
||||
"description": "消息类型:info(信息)、success(成功)、warning(警告)、error(错误)",
|
||||
"type": "string"
|
||||
}
|
||||
},
|
||||
"required": [
|
||||
"message",
|
||||
"explanation"
|
||||
],
|
||||
"type": "object"
|
||||
}
|
||||
}
|
||||
]
|
||||
29
app/agent/tools/__init__.py
Normal file
29
app/agent/tools/__init__.py
Normal file
@@ -0,0 +1,29 @@
|
||||
"""MoviePilot工具模块"""
|
||||
|
||||
from .base import MoviePilotTool
|
||||
from app.agent.tools.impl.search_media import SearchMediaTool
|
||||
from app.agent.tools.impl.add_subscribe import AddSubscribeTool
|
||||
from app.agent.tools.impl.search_torrents import SearchTorrentsTool
|
||||
from app.agent.tools.impl.add_download import AddDownloadTool
|
||||
from app.agent.tools.impl.query_subscribes import QuerySubscribesTool
|
||||
from app.agent.tools.impl.query_downloads import QueryDownloadsTool
|
||||
from app.agent.tools.impl.query_downloaders import QueryDownloadersTool
|
||||
from app.agent.tools.impl.get_recommendations import GetRecommendationsTool
|
||||
from app.agent.tools.impl.query_media_library import QueryMediaLibraryTool
|
||||
from app.agent.tools.impl.send_message import SendMessageTool
|
||||
from .factory import MoviePilotToolFactory
|
||||
|
||||
__all__ = [
|
||||
"MoviePilotTool",
|
||||
"SearchMediaTool",
|
||||
"AddSubscribeTool",
|
||||
"SearchTorrentsTool",
|
||||
"AddDownloadTool",
|
||||
"QuerySubscribesTool",
|
||||
"QueryDownloadsTool",
|
||||
"QueryDownloadersTool",
|
||||
"GetRecommendationsTool",
|
||||
"QueryMediaLibraryTool",
|
||||
"SendMessageTool",
|
||||
"MoviePilotToolFactory"
|
||||
]
|
||||
38
app/agent/tools/base.py
Normal file
38
app/agent/tools/base.py
Normal file
@@ -0,0 +1,38 @@
|
||||
"""MoviePilot工具基类"""
|
||||
|
||||
from langchain.tools import BaseTool
|
||||
from pydantic import PrivateAttr
|
||||
|
||||
from app.helper.message import MessageHelper
|
||||
from app.log import logger
|
||||
|
||||
|
||||
class MoviePilotTool(BaseTool):
|
||||
"""MoviePilot专用工具基类"""
|
||||
|
||||
_session_id: str = PrivateAttr()
|
||||
_user_id: str = PrivateAttr()
|
||||
_message_helper: MessageHelper = PrivateAttr()
|
||||
|
||||
def __init__(self, session_id: str, user_id: str, message_helper: MessageHelper = None, **kwargs):
|
||||
super().__init__(**kwargs)
|
||||
self._session_id = session_id
|
||||
self._user_id = user_id
|
||||
self._message_helper = message_helper or MessageHelper()
|
||||
|
||||
def _run(self, **kwargs) -> str:
|
||||
raise NotImplementedError
|
||||
|
||||
async def _arun(self, **kwargs) -> str:
|
||||
raise NotImplementedError
|
||||
|
||||
def _send_tool_message(self, message: str, message_type: str = "info"):
|
||||
"""发送工具执行消息"""
|
||||
try:
|
||||
self._message_helper.put(
|
||||
message=message,
|
||||
role="system",
|
||||
title=f"AI工具执行 ({message_type})"
|
||||
)
|
||||
except Exception as e:
|
||||
logger.error(f"发送工具消息失败: {e}")
|
||||
46
app/agent/tools/factory.py
Normal file
46
app/agent/tools/factory.py
Normal file
@@ -0,0 +1,46 @@
|
||||
"""MoviePilot工具工厂"""
|
||||
|
||||
from typing import List
|
||||
|
||||
from app.helper.message import MessageHelper
|
||||
from app.log import logger
|
||||
from .base import MoviePilotTool
|
||||
from app.agent.tools.impl.search_media import SearchMediaTool
|
||||
from app.agent.tools.impl.add_subscribe import AddSubscribeTool
|
||||
from app.agent.tools.impl.search_torrents import SearchTorrentsTool
|
||||
from app.agent.tools.impl.add_download import AddDownloadTool
|
||||
from app.agent.tools.impl.query_subscribes import QuerySubscribesTool
|
||||
from app.agent.tools.impl.query_downloads import QueryDownloadsTool
|
||||
from app.agent.tools.impl.query_downloaders import QueryDownloadersTool
|
||||
from app.agent.tools.impl.get_recommendations import GetRecommendationsTool
|
||||
from app.agent.tools.impl.query_media_library import QueryMediaLibraryTool
|
||||
from app.agent.tools.impl.send_message import SendMessageTool
|
||||
|
||||
|
||||
class MoviePilotToolFactory:
|
||||
"""MoviePilot工具工厂"""
|
||||
|
||||
@staticmethod
|
||||
def create_tools(session_id: str, user_id: str, message_helper: MessageHelper = None) -> List[MoviePilotTool]:
|
||||
"""创建MoviePilot工具列表"""
|
||||
tools = []
|
||||
tool_definitions = [
|
||||
SearchMediaTool,
|
||||
AddSubscribeTool,
|
||||
SearchTorrentsTool,
|
||||
AddDownloadTool,
|
||||
QuerySubscribesTool,
|
||||
QueryDownloadsTool,
|
||||
QueryDownloadersTool,
|
||||
GetRecommendationsTool,
|
||||
QueryMediaLibraryTool,
|
||||
SendMessageTool
|
||||
]
|
||||
for ToolClass in tool_definitions:
|
||||
tools.append(ToolClass(
|
||||
session_id=session_id,
|
||||
user_id=user_id,
|
||||
message_helper=message_helper
|
||||
))
|
||||
logger.info(f"成功创建 {len(tools)} 个MoviePilot工具")
|
||||
return tools
|
||||
0
app/agent/tools/impl/__init__.py
Normal file
0
app/agent/tools/impl/__init__.py
Normal file
58
app/agent/tools/impl/add_download.py
Normal file
58
app/agent/tools/impl/add_download.py
Normal file
@@ -0,0 +1,58 @@
|
||||
"""添加下载工具"""
|
||||
|
||||
from typing import Optional
|
||||
|
||||
from app.chain.download import DownloadChain
|
||||
from app.core.context import Context
|
||||
from app.core.metainfo import MetaInfo
|
||||
from app.log import logger
|
||||
from app.schemas import TorrentInfo
|
||||
from app.agent.tools.base import MoviePilotTool
|
||||
|
||||
|
||||
class AddDownloadTool(MoviePilotTool):
|
||||
name: str = "add_download"
|
||||
description: str = "添加下载任务,将搜索到的种子资源添加到下载器。"
|
||||
|
||||
async def _arun(self, torrent_title: str, torrent_url: str, explanation: str,
|
||||
downloader: Optional[str] = None, save_path: Optional[str] = None,
|
||||
labels: Optional[str] = None) -> str:
|
||||
logger.info(f"执行工具: {self.name}, 参数: torrent_title={torrent_title}, torrent_url={torrent_url}, downloader={downloader}, save_path={save_path}, labels={labels}")
|
||||
|
||||
# 发送工具执行说明
|
||||
self._send_tool_message(f"正在添加下载任务: {torrent_title}", "info")
|
||||
|
||||
try:
|
||||
if not torrent_title or not torrent_url:
|
||||
error_message = "错误:必须提供种子标题和下载链接"
|
||||
self._send_tool_message(error_message, "error")
|
||||
return error_message
|
||||
|
||||
# 使用DownloadChain添加下载
|
||||
download_chain = DownloadChain()
|
||||
|
||||
# 创建下载上下文
|
||||
torrent_info = TorrentInfo(
|
||||
title=torrent_title,
|
||||
download_url=torrent_url
|
||||
)
|
||||
meta_info = MetaInfo(title=torrent_title)
|
||||
context = Context(
|
||||
torrent_info=torrent_info,
|
||||
meta_info=meta_info
|
||||
)
|
||||
|
||||
did = download_chain.download_single(context=context, downloader=downloader,
|
||||
save_path=save_path, label=labels)
|
||||
if did:
|
||||
success_message = f"成功添加下载任务:{torrent_title}"
|
||||
self._send_tool_message(success_message, "success")
|
||||
return success_message
|
||||
else:
|
||||
error_message = "添加下载任务失败"
|
||||
self._send_tool_message(error_message, "error")
|
||||
return error_message
|
||||
except Exception as e:
|
||||
error_message = f"添加下载任务时发生错误: {str(e)}"
|
||||
self._send_tool_message(error_message, "error")
|
||||
return error_message
|
||||
37
app/agent/tools/impl/add_subscribe.py
Normal file
37
app/agent/tools/impl/add_subscribe.py
Normal file
@@ -0,0 +1,37 @@
|
||||
"""添加订阅工具"""
|
||||
|
||||
from typing import Optional
|
||||
|
||||
from app.chain.subscribe import SubscribeChain
|
||||
from app.log import logger
|
||||
from app.schemas.types import MediaType
|
||||
from app.agent.tools.base import MoviePilotTool
|
||||
|
||||
|
||||
class AddSubscribeTool(MoviePilotTool):
|
||||
name: str = "add_subscribe"
|
||||
description: str = "添加媒体订阅,为用户感兴趣的媒体内容创建订阅规则。"
|
||||
|
||||
async def _arun(self, title: str, year: str, media_type: str, explanation: str,
|
||||
season: Optional[int] = None, tmdb_id: Optional[str] = None) -> str:
|
||||
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")
|
||||
|
||||
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)
|
||||
if sid:
|
||||
success_message = f"成功添加订阅:{title} ({year})"
|
||||
self._send_tool_message(success_message, "success")
|
||||
return success_message
|
||||
else:
|
||||
error_message = f"添加订阅失败:{message}"
|
||||
self._send_tool_message(error_message, "error")
|
||||
return error_message
|
||||
except Exception as e:
|
||||
error_message = f"添加订阅时发生错误: {str(e)}"
|
||||
self._send_tool_message(error_message, "error")
|
||||
return error_message
|
||||
39
app/agent/tools/impl/get_recommendations.py
Normal file
39
app/agent/tools/impl/get_recommendations.py
Normal file
@@ -0,0 +1,39 @@
|
||||
"""获取推荐工具"""
|
||||
|
||||
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:
|
||||
return json.dumps([r.dict() for r in results], ensure_ascii=False, indent=2)
|
||||
return "未找到推荐内容。"
|
||||
except Exception as e:
|
||||
logger.error(f"获取推荐失败: {e}")
|
||||
return f"获取推荐时发生错误: {str(e)}"
|
||||
25
app/agent/tools/impl/query_downloaders.py
Normal file
25
app/agent/tools/impl/query_downloaders.py
Normal file
@@ -0,0 +1,25 @@
|
||||
"""查询下载器工具"""
|
||||
|
||||
import json
|
||||
|
||||
from app.db.systemconfig_oper import SystemConfigOper
|
||||
from app.log import logger
|
||||
from app.schemas.types import SystemConfigKey
|
||||
from app.agent.tools.base import MoviePilotTool
|
||||
|
||||
|
||||
class QueryDownloadersTool(MoviePilotTool):
|
||||
name: str = "query_downloaders"
|
||||
description: str = "查询下载器配置,查看可用的下载器列表和配置信息。"
|
||||
|
||||
async def _arun(self, explanation: str) -> str:
|
||||
logger.info(f"执行工具: {self.name}")
|
||||
try:
|
||||
system_config_oper = SystemConfigOper()
|
||||
downloaders_config = system_config_oper.get(SystemConfigKey.Downloaders)
|
||||
if downloaders_config:
|
||||
return json.dumps(downloaders_config, ensure_ascii=False, indent=2)
|
||||
return "未配置下载器。"
|
||||
except Exception as e:
|
||||
logger.error(f"查询下载器失败: {e}")
|
||||
return f"查询下载器时发生错误: {str(e)}"
|
||||
33
app/agent/tools/impl/query_downloads.py
Normal file
33
app/agent/tools/impl/query_downloads.py
Normal file
@@ -0,0 +1,33 @@
|
||||
"""查询下载工具"""
|
||||
|
||||
import json
|
||||
from typing import Optional
|
||||
|
||||
from app.db.download_oper import DownloadOper
|
||||
from app.log import logger
|
||||
from app.agent.tools.base import MoviePilotTool
|
||||
|
||||
|
||||
class QueryDownloadsTool(MoviePilotTool):
|
||||
name: str = "query_downloads"
|
||||
description: str = "查询下载状态,查看下载器的任务列表和进度。"
|
||||
|
||||
async def _arun(self, explanation: str, downloader: Optional[str] = None,
|
||||
status: Optional[str] = "all") -> str:
|
||||
logger.info(f"执行工具: {self.name}, 参数: downloader={downloader}, status={status}")
|
||||
try:
|
||||
download_oper = DownloadOper()
|
||||
downloads = download_oper.list()
|
||||
filtered_downloads = []
|
||||
for dl in downloads:
|
||||
if downloader and dl.downloader != downloader:
|
||||
continue
|
||||
if status != "all" and dl.status != status:
|
||||
continue
|
||||
filtered_downloads.append(dl)
|
||||
if filtered_downloads:
|
||||
return json.dumps([d.dict() for d in filtered_downloads], ensure_ascii=False, indent=2)
|
||||
return "未找到相关下载任务。"
|
||||
except Exception as e:
|
||||
logger.error(f"查询下载失败: {e}")
|
||||
return f"查询下载时发生错误: {str(e)}"
|
||||
33
app/agent/tools/impl/query_media_library.py
Normal file
33
app/agent/tools/impl/query_media_library.py
Normal file
@@ -0,0 +1,33 @@
|
||||
"""查询媒体库工具"""
|
||||
|
||||
import json
|
||||
from typing import Optional
|
||||
|
||||
from app.db.media_oper import MediaOper
|
||||
from app.log import logger
|
||||
from app.agent.tools.base import MoviePilotTool
|
||||
|
||||
|
||||
class QueryMediaLibraryTool(MoviePilotTool):
|
||||
name: str = "query_media_library"
|
||||
description: str = "查询媒体库状态,查看已入库的媒体文件情况。"
|
||||
|
||||
async def _arun(self, explanation: str, media_type: Optional[str] = "all",
|
||||
title: Optional[str] = None) -> str:
|
||||
logger.info(f"执行工具: {self.name}, 参数: media_type={media_type}, title={title}")
|
||||
try:
|
||||
media_oper = MediaOper()
|
||||
medias = media_oper.list()
|
||||
filtered_medias = []
|
||||
for media in medias:
|
||||
if media_type != "all" and media.type != media_type:
|
||||
continue
|
||||
if title and title.lower() not in media.title.lower():
|
||||
continue
|
||||
filtered_medias.append(media)
|
||||
if filtered_medias:
|
||||
return json.dumps([m.dict() for m in filtered_medias], ensure_ascii=False, indent=2)
|
||||
return "媒体库中未找到相关媒体。"
|
||||
except Exception as e:
|
||||
logger.error(f"查询媒体库失败: {e}")
|
||||
return f"查询媒体库时发生错误: {str(e)}"
|
||||
33
app/agent/tools/impl/query_subscribes.py
Normal file
33
app/agent/tools/impl/query_subscribes.py
Normal file
@@ -0,0 +1,33 @@
|
||||
"""查询订阅工具"""
|
||||
|
||||
import json
|
||||
from typing import Optional
|
||||
|
||||
from app.db.subscribe_oper import SubscribeOper
|
||||
from app.log import logger
|
||||
from app.agent.tools.base import MoviePilotTool
|
||||
|
||||
|
||||
class QuerySubscribesTool(MoviePilotTool):
|
||||
name: str = "query_subscribes"
|
||||
description: str = "查询订阅状态,查看用户的订阅列表和状态。"
|
||||
|
||||
async def _arun(self, explanation: str, status: Optional[str] = "all",
|
||||
media_type: Optional[str] = "all") -> str:
|
||||
logger.info(f"执行工具: {self.name}, 参数: status={status}, media_type={media_type}")
|
||||
try:
|
||||
subscribe_oper = SubscribeOper()
|
||||
subscribes = subscribe_oper.list()
|
||||
filtered_subscribes = []
|
||||
for sub in subscribes:
|
||||
if status != "all" and sub.status != status:
|
||||
continue
|
||||
if media_type != "all" and sub.type != media_type:
|
||||
continue
|
||||
filtered_subscribes.append(sub)
|
||||
if filtered_subscribes:
|
||||
return json.dumps([s.dict() for s in filtered_subscribes], ensure_ascii=False, indent=2)
|
||||
return "未找到相关订阅。"
|
||||
except Exception as e:
|
||||
logger.error(f"查询订阅失败: {e}")
|
||||
return f"查询订阅时发生错误: {str(e)}"
|
||||
42
app/agent/tools/impl/search_media.py
Normal file
42
app/agent/tools/impl/search_media.py
Normal file
@@ -0,0 +1,42 @@
|
||||
"""搜索媒体工具"""
|
||||
|
||||
import json
|
||||
from typing import Optional
|
||||
|
||||
from app.chain.media import MediaChain
|
||||
from app.log import logger
|
||||
from app.agent.tools.base import MoviePilotTool
|
||||
|
||||
|
||||
class SearchMediaTool(MoviePilotTool):
|
||||
name: str = "search_media"
|
||||
description: str = "搜索媒体资源,包括电影、电视剧、动漫等。可以根据标题、年份、类型等条件进行搜索。"
|
||||
|
||||
async def _arun(self, title: str, explanation: str, year: Optional[str] = None,
|
||||
media_type: Optional[str] = None, season: Optional[int] = None) -> str:
|
||||
logger.info(f"执行工具: {self.name}, 参数: title={title}, year={year}, media_type={media_type}, season={season}")
|
||||
|
||||
# 发送工具执行说明
|
||||
self._send_tool_message(f"正在搜索媒体资源: {title}" + (f" ({year})" if year else ""), "info")
|
||||
|
||||
try:
|
||||
media_chain = MediaChain()
|
||||
results = media_chain.search_media(title=title, year=year, mtype=media_type, season=season)
|
||||
if results:
|
||||
result_message = f"找到 {len(results)} 个相关媒体资源"
|
||||
self._send_tool_message(result_message, "success")
|
||||
|
||||
# 发送详细结果
|
||||
for i, result in enumerate(results[:5]): # 只显示前5个结果
|
||||
media_info = f"{i+1}. {result.title} ({result.year}) - {result.type}"
|
||||
self._send_tool_message(media_info, "info")
|
||||
|
||||
return json.dumps([r.dict() for r in results], ensure_ascii=False, indent=2)
|
||||
else:
|
||||
error_message = f"未找到相关媒体资源: {title}"
|
||||
self._send_tool_message(error_message, "warning")
|
||||
return error_message
|
||||
except Exception as e:
|
||||
error_message = f"搜索媒体失败: {str(e)}"
|
||||
self._send_tool_message(error_message, "error")
|
||||
return error_message
|
||||
54
app/agent/tools/impl/search_torrents.py
Normal file
54
app/agent/tools/impl/search_torrents.py
Normal file
@@ -0,0 +1,54 @@
|
||||
"""搜索种子工具"""
|
||||
|
||||
import json
|
||||
from typing import List, Optional
|
||||
|
||||
from app.chain.search import SearchChain
|
||||
from app.log import logger
|
||||
from app.schemas.types import MediaType
|
||||
from app.agent.tools.base import MoviePilotTool
|
||||
|
||||
|
||||
class SearchTorrentsTool(MoviePilotTool):
|
||||
name: str = "search_torrents"
|
||||
description: str = "搜索站点种子资源,根据媒体信息搜索可下载的种子文件。"
|
||||
|
||||
async def _arun(self, title: str, explanation: str, year: Optional[str] = None,
|
||||
media_type: Optional[str] = None, season: Optional[int] = None,
|
||||
sites: Optional[List[int]] = None) -> str:
|
||||
logger.info(f"执行工具: {self.name}, 参数: title={title}, year={year}, media_type={media_type}, season={season}, sites={sites}")
|
||||
|
||||
# 发送工具执行说明
|
||||
self._send_tool_message(f"正在搜索种子资源: {title}" + (f" ({year})" if year else ""), "info")
|
||||
|
||||
try:
|
||||
search_chain = SearchChain()
|
||||
torrents = search_chain.search_by_title(title=title, sites=sites)
|
||||
filtered_torrents = []
|
||||
for torrent in torrents:
|
||||
if year and torrent.meta_info.year != year:
|
||||
continue
|
||||
if media_type and torrent.media_info and torrent.media_info.type != MediaType(media_type):
|
||||
continue
|
||||
if season and torrent.meta_info.begin_season != season:
|
||||
continue
|
||||
filtered_torrents.append(torrent)
|
||||
|
||||
if filtered_torrents:
|
||||
result_message = f"找到 {len(filtered_torrents)} 个相关种子资源"
|
||||
self._send_tool_message(result_message, "success")
|
||||
|
||||
# 发送详细结果
|
||||
for i, torrent in enumerate(filtered_torrents[:5]): # 只显示前5个结果
|
||||
torrent_info = f"{i+1}. {torrent.title} - {torrent.site_name}"
|
||||
self._send_tool_message(torrent_info, "info")
|
||||
|
||||
return json.dumps([t.dict() for t in filtered_torrents], ensure_ascii=False, indent=2)
|
||||
else:
|
||||
error_message = f"未找到相关种子资源: {title}"
|
||||
self._send_tool_message(error_message, "warning")
|
||||
return error_message
|
||||
except Exception as e:
|
||||
error_message = f"搜索种子时发生错误: {str(e)}"
|
||||
self._send_tool_message(error_message, "error")
|
||||
return error_message
|
||||
22
app/agent/tools/impl/send_message.py
Normal file
22
app/agent/tools/impl/send_message.py
Normal file
@@ -0,0 +1,22 @@
|
||||
"""发送消息工具"""
|
||||
|
||||
from typing import Optional
|
||||
|
||||
from app.helper.message import MessageHelper
|
||||
from app.log import logger
|
||||
from app.agent.tools.base import MoviePilotTool
|
||||
|
||||
|
||||
class SendMessageTool(MoviePilotTool):
|
||||
name: str = "send_message"
|
||||
description: str = "发送消息通知,向用户发送操作结果或重要信息。"
|
||||
|
||||
async def _arun(self, message: str, explanation: str, message_type: Optional[str] = "info") -> str:
|
||||
logger.info(f"执行工具: {self.name}, 参数: message={message}, message_type={message_type}")
|
||||
try:
|
||||
message_helper = MessageHelper()
|
||||
message_helper.put(message=message, role="system", title=f"AI助手通知 ({message_type})")
|
||||
return "消息已发送。"
|
||||
except Exception as e:
|
||||
logger.error(f"发送消息失败: {e}")
|
||||
return f"发送消息时发生错误: {str(e)}"
|
||||
Reference in New Issue
Block a user