From e48e325cfc3270c702ee44119fecad655cbbec04 Mon Sep 17 00:00:00 2001 From: Codex Date: Mon, 20 Jul 2026 22:31:36 +0800 Subject: [PATCH] feat: add MoviePilot LLM tools --- main.py | 141 ++++++++++++++++++++++++++++++++++++++++ tests/test_llm_tools.py | 25 +++++++ 2 files changed, 166 insertions(+) create mode 100644 tests/test_llm_tools.py diff --git a/main.py b/main.py index 3251be1..ec9b88b 100644 --- a/main.py +++ b/main.py @@ -245,6 +245,25 @@ class MoviePilotPlugin(star.Star): download_id = result.get("download_id") or result.get("id") or "未知" yield event.plain_result(f"下载任务已提交,任务 ID:{download_id}") return + if request.operation == "subscribe": + media = request.target["media"] + result = await self.subscription_service.create_subscription( + media, season=request.target.get("season") + ) + subscribe_id = result.get("id") + if not subscribe_id: + yield event.plain_result("订阅创建失败,MoviePilot 没有返回订阅编号。") + return + message = f"订阅已创建,ID:{subscribe_id}" + try: + await self.subscription_service.trigger_subscription_search( + int(subscribe_id) + ) + message += ";已自动触发一次资源搜索。" + except Exception as exc: + message += f";自动搜索失败:{exc}" + yield event.plain_result(message) + return if request.operation == "delete_download": task_id = str(request.target["task_id"]) await self.download_service.delete(task_id) @@ -282,6 +301,128 @@ class MoviePilotPlugin(star.Star): self.confirmations.cancel(item["code"]) yield event.plain_result("已取消待确认操作。" if pending else "当前没有待确认操作。") + @filter.llm_tool(name="moviepilot_search_media") + async def llm_search_media( + self, event: AstrMessageEvent, title: str, media_type: str = "" + ) -> None: + """搜索 MoviePilot 中的影视信息。 + + Args: + title(string): 要搜索的影视标题。 + media_type(string): 可选媒体类型,填写 movie 或 tv。 + """ + if not self.access_policy.is_allowed(event): + return + try: + results = await self.media_service.search_media( + event.unified_msg_origin, + title, + media_type=media_type or None, + ) + yield event.plain_result(format_media_results(results)) + except Exception as exc: + yield event.plain_result(f"媒体搜索失败:{exc}") + + @filter.llm_tool(name="moviepilot_search_resources") + async def llm_search_resources( + self, event: AstrMessageEvent, media_index: int + ) -> None: + """搜索指定影视的下载资源。 + + Args: + media_index(number): 当前会话最近一次媒体搜索结果的编号。 + """ + if not self.access_policy.is_allowed(event): + return + media = self.session_store.get_media_result(event.unified_msg_origin, media_index) + if media is None: + yield event.plain_result("媒体编号无效或已过期,请先重新搜索影视。") + return + try: + results = await self.media_service.search_resources( + event.unified_msg_origin, media + ) + yield event.plain_result(format_resource_results(results)) + except Exception as exc: + yield event.plain_result(f"资源搜索失败:{exc}") + + @filter.llm_tool(name="moviepilot_list_subscriptions") + async def llm_list_subscriptions(self, event: AstrMessageEvent) -> None: + """查询 MoviePilot 当前订阅列表。""" + if not self.access_policy.is_allowed(event): + return + try: + subscriptions = await self.subscription_service.list_subscriptions() + yield event.plain_result(format_subscriptions(subscriptions)) + except Exception as exc: + yield event.plain_result(f"查询订阅失败:{exc}") + + @filter.llm_tool(name="moviepilot_list_downloads") + async def llm_list_downloads(self, event: AstrMessageEvent) -> None: + """查询 MoviePilot 当前下载任务。""" + if not self.access_policy.is_allowed(event): + return + try: + tasks = await self.download_service.list_tasks() + yield event.plain_result(format_download_tasks(tasks)) + except Exception as exc: + yield event.plain_result(f"查询下载任务失败:{exc}") + + @filter.llm_tool(name="moviepilot_request_subscribe") + async def llm_request_subscribe( + self, event: AstrMessageEvent, media_index: int, season: int = 0 + ) -> None: + """请求订阅影视,并等待用户确认。 + + Args: + media_index(number): 当前会话最近一次媒体搜索结果的编号。 + season(number): 可选电视剧季号,电影填写 0。 + """ + if not self.access_policy.is_allowed(event): + return + media = self.session_store.get_media_result(event.unified_msg_origin, media_index) + if media is None: + yield event.plain_result("媒体编号无效或已过期,请先重新搜索影视。") + return + request = self.confirmations.create( + session_origin=event.unified_msg_origin, + user_id=event.get_sender_id(), + operation="subscribe", + target={"media": media, "season": season or None}, + ) + title = media.get("title") or media.get("name") or "未命名" + yield event.plain_result( + f"准备订阅《{title}》。请回复 /mp confirm {request.code} 确认。" + ) + + @filter.llm_tool(name="moviepilot_request_download") + async def llm_request_download( + self, event: AstrMessageEvent, resource_index: int + ) -> None: + """请求下载资源,并等待用户确认。 + + Args: + resource_index(number): 当前会话最近一次资源搜索结果的编号。 + """ + if not self.access_policy.is_allowed(event): + return + origin = event.unified_msg_origin + resource = self.session_store.get_resource_result(origin, resource_index) + media = self.session_store.get_resource_media(origin) + if resource is None or media is None: + yield event.plain_result("资源编号无效或已过期,请先重新搜索资源。") + return + request = self.confirmations.create( + session_origin=origin, + user_id=event.get_sender_id(), + operation="download", + target={"resource": resource, "media": media}, + ) + title = resource.get("title") or resource.get("name") or "未命名资源" + yield event.plain_result( + f"准备下载《{title}》。请回复 /mp confirm {request.code} 确认。" + ) + async def _send_notification(self, target: str, text: str) -> None: await self.context.send_message(target, MessageChain().message(text)) diff --git a/tests/test_llm_tools.py b/tests/test_llm_tools.py new file mode 100644 index 0000000..68c4cf0 --- /dev/null +++ b/tests/test_llm_tools.py @@ -0,0 +1,25 @@ +from pathlib import Path + + +MAIN_SOURCE = (Path(__file__).parents[1] / "main.py").read_text(encoding="utf-8") + + +def test_llm_tools_are_registered_with_argument_documentation() -> None: + expected_tools = ( + "moviepilot_search_media", + "moviepilot_search_resources", + "moviepilot_list_subscriptions", + "moviepilot_list_downloads", + "moviepilot_request_subscribe", + "moviepilot_request_download", + ) + + for tool_name in expected_tools: + assert f'name="{tool_name}"' in MAIN_SOURCE + assert MAIN_SOURCE.count("Args:") >= 4 + + +def test_llm_write_operations_use_confirmation_store() -> None: + assert 'operation="subscribe"' in MAIN_SOURCE + assert 'operation="download"' in MAIN_SOURCE + assert "/mp confirm" in MAIN_SOURCE