mirror of
https://github.com/jxxghp/MoviePilot.git
synced 2026-09-04 23:17:20 +08:00
Merge remote-tracking branch 'origin/v3' into v3
# Conflicts: # app/api/endpoints/agent.py # app/api/endpoints/anthropic.py # app/api/endpoints/openai.py # app/chain/__init__.py # app/chain/message.py # app/chain/site.py # app/chain/subscribe.py # app/chain/transfer.py # app/modules/discord/__init__.py # app/modules/qqbot/__init__.py # app/modules/slack/__init__.py # app/modules/telegram/__init__.py # app/modules/wechat/__init__.py # app/runtime/extensions/module_manager.py # app/runtime/extensions/service_registry.py # tests/test_agent_interaction.py # tests/test_slash_command_interactions.py # tests/test_web_agent_stream.py
This commit is contained in:
+60
-4
@@ -5,12 +5,11 @@ from abc import ABCMeta, abstractmethod
|
||||
from concurrent.futures import ThreadPoolExecutor
|
||||
from functools import partial
|
||||
from pathlib import Path
|
||||
from typing import Any, Callable, ClassVar, Optional
|
||||
from typing import TYPE_CHECKING, Any, Callable, ClassVar, Optional, Protocol
|
||||
|
||||
from langchain_core.tools import BaseTool
|
||||
from pydantic import PrivateAttr
|
||||
|
||||
from app.agent.callback import StreamingHandler
|
||||
from app.agent.policy.sanitizer import (
|
||||
summarize_error,
|
||||
summarize_input,
|
||||
@@ -25,6 +24,54 @@ from app.runtime.log import logger
|
||||
from app.schemas import Message
|
||||
from app.schemas.types import NotificationChannel, MessageType
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from app.agent.callback import StreamingHandler as _StreamingHandlerProtocol
|
||||
else:
|
||||
class _StreamingHandlerProtocol(Protocol):
|
||||
"""工具执行仅依赖的流式缓冲合同。"""
|
||||
|
||||
@property
|
||||
def is_streaming(self) -> bool:
|
||||
"""是否正在收集流式输出。"""
|
||||
...
|
||||
|
||||
@property
|
||||
def is_auto_flushing(self) -> bool:
|
||||
"""是否由渠道编辑能力自动刷新缓冲。"""
|
||||
...
|
||||
|
||||
@property
|
||||
def last_buffer_char(self) -> str:
|
||||
"""返回缓冲区最后一个字符。"""
|
||||
...
|
||||
|
||||
def emit(self, token: str) -> str:
|
||||
"""追加流式文本并返回实际追加内容。"""
|
||||
...
|
||||
|
||||
async def take(self) -> str:
|
||||
"""取出并清空当前缓冲内容。"""
|
||||
...
|
||||
|
||||
def record_tool_call(
|
||||
self,
|
||||
tool_name: str,
|
||||
tool_message: Optional[str] = None,
|
||||
tool_kwargs: Optional[dict[str, Any]] = None,
|
||||
) -> None:
|
||||
"""记录一次待汇总的工具调用。"""
|
||||
...
|
||||
|
||||
|
||||
|
||||
def __getattr__(name: str) -> Any:
|
||||
"""显式访问历史 StreamingHandler 符号时返回 canonical 实现。"""
|
||||
if name == "StreamingHandler":
|
||||
from app.agent.callback import StreamingHandler
|
||||
|
||||
return StreamingHandler
|
||||
raise AttributeError(f"module 'app.agent.tools.base' has no attribute {name!r}")
|
||||
|
||||
|
||||
class ToolChain(ChainBase):
|
||||
pass
|
||||
@@ -206,7 +253,7 @@ class MoviePilotTool(BaseTool, metaclass=ABCMeta):
|
||||
_channel: Optional[str] = PrivateAttr(default=None)
|
||||
_source: Optional[str] = PrivateAttr(default=None)
|
||||
_username: Optional[str] = PrivateAttr(default=None)
|
||||
_stream_handler: Optional[StreamingHandler] = PrivateAttr(default=None)
|
||||
_stream_handler: Optional[_StreamingHandlerProtocol] = PrivateAttr(default=None)
|
||||
_require_admin: bool = PrivateAttr(default=False)
|
||||
_agent_context: dict = PrivateAttr(default_factory=dict)
|
||||
|
||||
@@ -387,7 +434,9 @@ class MoviePilotTool(BaseTool, metaclass=ABCMeta):
|
||||
self._source = source
|
||||
self._username = username
|
||||
|
||||
def set_stream_handler(self, stream_handler: StreamingHandler):
|
||||
def set_stream_handler(
|
||||
self, stream_handler: Optional[_StreamingHandlerProtocol]
|
||||
) -> None:
|
||||
"""
|
||||
设置回调处理器
|
||||
"""
|
||||
@@ -642,3 +691,10 @@ class MoviePilotTool(BaseTool, metaclass=ABCMeta):
|
||||
save_history=False,
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
# 普通导入保持 callback 冷态;显式导入或历史星号导入仍解析真实类。
|
||||
__all__ = sorted(
|
||||
{name for name in globals() if not name.startswith("_")}
|
||||
| {"StreamingHandler"}
|
||||
)
|
||||
|
||||
@@ -7,9 +7,9 @@ from typing import Any, Dict, Iterable, Optional
|
||||
from app.runtime.events import eventmanager
|
||||
from app.db.oper.subscribe import SubscribeOper
|
||||
from app.db.oper.systemconfig import SystemConfigOper
|
||||
from app.application.filter import RuleHelper
|
||||
from app.application.filter_rules import RuleParser
|
||||
from app.application.filter_rules import BUILTIN_RULE_SET
|
||||
from app.application.rules import RuleHelper
|
||||
from app.application.rules import RuleParser
|
||||
from app.application.rules import BUILTIN_RULE_SET
|
||||
from app.schemas import CustomRule, FilterRuleGroup
|
||||
from app.schemas.event import ConfigChangeEventData
|
||||
from app.schemas.types import EventType, SystemConfigKey
|
||||
|
||||
@@ -70,14 +70,14 @@ def reload_plugin_runtime(plugin_id: str) -> None:
|
||||
重载插件并重新注册其命令、定时任务和 API。
|
||||
"""
|
||||
# 这些依赖只在真正执行重载时才导入,避免普通查询工具引入不必要的初始化开销。
|
||||
from app.api.endpoints.plugin import register_plugin_api
|
||||
from app.command import Command
|
||||
from app.scheduler import Scheduler
|
||||
from app.application.plugins import register_plugin_api
|
||||
from app.application.commands import init_commands
|
||||
from app.application.scheduling import update_plugin_job
|
||||
|
||||
plugin_manager = PluginManager()
|
||||
plugin_manager.reload_plugin(plugin_id)
|
||||
Scheduler().update_plugin_job(plugin_id)
|
||||
Command().init_commands(plugin_id)
|
||||
update_plugin_job(plugin_id)
|
||||
init_commands(plugin_id)
|
||||
register_plugin_api(plugin_id)
|
||||
|
||||
|
||||
@@ -333,8 +333,11 @@ async def uninstall_plugin_runtime(plugin_id: str) -> dict[str, Any]:
|
||||
"""
|
||||
按现有卸载逻辑移除插件,并清理运行态注册与分组信息。
|
||||
"""
|
||||
from app.api.endpoints.plugin import _remove_plugin_from_folders, remove_plugin_api
|
||||
from app.scheduler import Scheduler
|
||||
from app.application.plugins import (
|
||||
remove_plugin_api,
|
||||
remove_plugin_from_folders,
|
||||
)
|
||||
from app.application.scheduling import remove_plugin_job
|
||||
|
||||
config_oper = SystemConfigOper()
|
||||
install_plugins = config_oper.get(SystemConfigKey.UserInstalledPlugins) or []
|
||||
@@ -343,7 +346,7 @@ async def uninstall_plugin_runtime(plugin_id: str) -> dict[str, Any]:
|
||||
await config_oper.async_set(SystemConfigKey.UserInstalledPlugins, install_plugins)
|
||||
|
||||
remove_plugin_api(plugin_id)
|
||||
Scheduler().remove_plugin_job(plugin_id)
|
||||
remove_plugin_job(plugin_id)
|
||||
|
||||
plugin_manager = PluginManager()
|
||||
plugin_class = plugin_manager.plugins.get(plugin_id)
|
||||
@@ -362,7 +365,7 @@ async def uninstall_plugin_runtime(plugin_id: str) -> dict[str, Any]:
|
||||
except Exception:
|
||||
clone_files_removed = False
|
||||
|
||||
_remove_plugin_from_folders(plugin_id)
|
||||
remove_plugin_from_folders(plugin_id)
|
||||
plugin_manager.remove_plugin(plugin_id)
|
||||
|
||||
return {
|
||||
|
||||
@@ -133,6 +133,7 @@ def simplify_search_result(
|
||||
context: Context,
|
||||
index: int,
|
||||
include_description: bool = False,
|
||||
include_labels: bool = False,
|
||||
) -> dict:
|
||||
"""
|
||||
精简单条搜索结果
|
||||
@@ -140,6 +141,7 @@ def simplify_search_result(
|
||||
:param context: 搜索结果上下文
|
||||
:param index: 搜索结果在原始缓存中的序号
|
||||
:param include_description: 是否返回种子简介
|
||||
:param include_labels: 是否返回种子标签
|
||||
:return: 精简后的搜索结果
|
||||
"""
|
||||
simplified = {}
|
||||
@@ -162,6 +164,8 @@ def simplify_search_result(
|
||||
}
|
||||
if include_description:
|
||||
simplified["torrent_info"]["description"] = torrent_info.description
|
||||
if include_labels:
|
||||
simplified["torrent_info"]["labels"] = torrent_info.labels or []
|
||||
|
||||
if media_info:
|
||||
if getattr(media_info, "type", None) == MediaType.MUSIC:
|
||||
|
||||
@@ -99,7 +99,7 @@ class CreateAgentTaskTool(MoviePilotTool):
|
||||
|
||||
def _create_task(self, payload: CreateAgentTaskInput) -> dict:
|
||||
"""持久化任务并立即注册到运行时调度器。"""
|
||||
from app.scheduler import Scheduler
|
||||
from app.application.scheduling import update_agent_task_job
|
||||
|
||||
trigger_value = payload.trigger
|
||||
if payload.trigger_type == "date" and payload.delay_minutes is not None:
|
||||
@@ -130,8 +130,7 @@ class CreateAgentTaskTool(MoviePilotTool):
|
||||
source=self._source or (chat.source if chat else None),
|
||||
original_chat_id=chat.original_chat_id if chat else None,
|
||||
)
|
||||
scheduler = Scheduler()
|
||||
next_run_at = scheduler.update_agent_task_job(task.id)
|
||||
next_run_at = update_agent_task_job(task.id)
|
||||
return AgentTaskOper.to_dict(
|
||||
task,
|
||||
next_run_at=next_run_at,
|
||||
|
||||
@@ -31,14 +31,14 @@ class DeleteAgentTaskTool(MoviePilotTool):
|
||||
|
||||
def _delete_task(self, task_id: int) -> bool:
|
||||
"""删除当前用户的任务并移除运行时调度。"""
|
||||
from app.scheduler import Scheduler
|
||||
from app.application.scheduling import remove_agent_task_job
|
||||
|
||||
deleted = AgentTaskOper().delete(
|
||||
task_id=task_id,
|
||||
user_id=str(self._user_id),
|
||||
)
|
||||
if deleted:
|
||||
Scheduler().remove_agent_task_job(task_id)
|
||||
remove_agent_task_job(task_id)
|
||||
return deleted
|
||||
|
||||
async def run(self, task_id: int, **kwargs: object) -> str:
|
||||
|
||||
@@ -42,6 +42,10 @@ class GetSearchResultsInput(BaseModel):
|
||||
False,
|
||||
description="Whether to include torrent descriptions in returned results",
|
||||
)
|
||||
include_labels: Optional[bool] = Field(
|
||||
False,
|
||||
description="Whether to include torrent labels in returned results",
|
||||
)
|
||||
show_filter_options: Optional[bool] = Field(
|
||||
False,
|
||||
description="Whether to return only optional filter options for re-checking available conditions",
|
||||
@@ -79,6 +83,7 @@ class GetSearchResultsTool(MoviePilotTool):
|
||||
title_pattern: Optional[str] = None,
|
||||
content_pattern: Optional[str] = None,
|
||||
include_description: bool = False,
|
||||
include_labels: bool = False,
|
||||
show_filter_options: bool = False,
|
||||
page: Optional[int] = 1,
|
||||
**kwargs,
|
||||
@@ -96,6 +101,7 @@ class GetSearchResultsTool(MoviePilotTool):
|
||||
:param title_pattern: 仅匹配种子标题的正则表达式
|
||||
:param content_pattern: 匹配种子标题、简介和标签的正则表达式
|
||||
:param include_description: 是否在结果中返回种子简介
|
||||
:param include_labels: 是否在结果中返回种子标签
|
||||
:param show_filter_options: 是否只返回可用筛选项
|
||||
:param page: 分页页码
|
||||
:param kwargs: 工具框架附加参数
|
||||
@@ -103,7 +109,7 @@ class GetSearchResultsTool(MoviePilotTool):
|
||||
"""
|
||||
page = max(1, page or 1)
|
||||
logger.info(
|
||||
f"执行工具: {self.name}, 参数: site={site}, season={season}, free_state={free_state}, video_code={video_code}, edition={edition}, resolution={resolution}, release_group={release_group}, title_pattern={title_pattern}, content_pattern={content_pattern}, include_description={include_description}, show_filter_options={show_filter_options}, page={page}"
|
||||
f"执行工具: {self.name}, 参数: site={site}, season={season}, free_state={free_state}, video_code={video_code}, edition={edition}, resolution={resolution}, release_group={release_group}, title_pattern={title_pattern}, content_pattern={content_pattern}, include_description={include_description}, include_labels={include_labels}, show_filter_options={show_filter_options}, page={page}"
|
||||
)
|
||||
|
||||
try:
|
||||
@@ -193,6 +199,7 @@ class GetSearchResultsTool(MoviePilotTool):
|
||||
item,
|
||||
index,
|
||||
include_description=include_description,
|
||||
include_labels=include_labels,
|
||||
)
|
||||
for item, index in zip(page_items, page_indices)
|
||||
]
|
||||
|
||||
@@ -14,7 +14,6 @@ class ListSlashCommandsInput(BaseModel):
|
||||
"""查询所有可用斜杠命令工具的输入参数模型"""
|
||||
|
||||
|
||||
|
||||
class ListSlashCommandsTool(MoviePilotTool):
|
||||
name: str = "list_slash_commands"
|
||||
tags: list[str] = [
|
||||
@@ -41,10 +40,9 @@ class ListSlashCommandsTool(MoviePilotTool):
|
||||
logger.info(f"执行工具: {self.name}")
|
||||
|
||||
try:
|
||||
from app.command import Command
|
||||
from app.application.commands import get_commands
|
||||
|
||||
command_obj = Command()
|
||||
all_commands = command_obj.get_commands()
|
||||
all_commands = get_commands()
|
||||
|
||||
if not all_commands:
|
||||
return "当前没有可用的命令"
|
||||
|
||||
@@ -48,7 +48,7 @@ class QueryAgentTasksTool(MoviePilotTool):
|
||||
enabled: Optional[bool],
|
||||
) -> list[dict]:
|
||||
"""读取当前用户的任务及运行时下一次触发时间。"""
|
||||
from app.scheduler import Scheduler
|
||||
from app.application.scheduling import get_agent_task_next_run
|
||||
|
||||
oper = AgentTaskOper()
|
||||
if task_id:
|
||||
@@ -56,12 +56,11 @@ class QueryAgentTasksTool(MoviePilotTool):
|
||||
tasks = [task] if task else []
|
||||
else:
|
||||
tasks = oper.list(user_id=str(self._user_id), enabled=enabled)
|
||||
scheduler = Scheduler()
|
||||
result = []
|
||||
for task in tasks:
|
||||
data = oper.to_dict(
|
||||
task,
|
||||
next_run_at=scheduler.get_agent_task_next_run(task.id),
|
||||
next_run_at=get_agent_task_next_run(task.id),
|
||||
timezone=settings.TZ,
|
||||
)
|
||||
if task_id:
|
||||
|
||||
@@ -39,13 +39,15 @@ class QuerySchedulersTool(MoviePilotTool):
|
||||
"""查询非 Agent 自主任务的运行时定时服务。"""
|
||||
logger.info(f"执行工具: {self.name}")
|
||||
try:
|
||||
from app.scheduler import AGENT_TASK_JOB_PREFIX, Scheduler
|
||||
from app.application.scheduling import (
|
||||
AGENT_TASK_JOB_PREFIX,
|
||||
list_scheduler_jobs,
|
||||
)
|
||||
|
||||
scheduler = Scheduler()
|
||||
agent_task_prefix = f"{AGENT_TASK_JOB_PREFIX}-"
|
||||
schedulers = [
|
||||
scheduler_item
|
||||
for scheduler_item in scheduler.list()
|
||||
for scheduler_item in list_scheduler_jobs()
|
||||
if not str(scheduler_item.id or "").startswith(agent_task_prefix)
|
||||
]
|
||||
if schedulers:
|
||||
|
||||
@@ -56,7 +56,7 @@ class RunAgentTaskTool(MoviePilotTool):
|
||||
|
||||
async def run(self, task_id: int, **kwargs: object) -> str:
|
||||
"""立即执行当前用户拥有且已启用的 Agent 自主定时任务。"""
|
||||
from app.scheduler import Scheduler
|
||||
from app.application.scheduling import start_agent_task
|
||||
|
||||
payload = RunAgentTaskInput(task_id=task_id)
|
||||
status, task_name = await self.run_blocking(
|
||||
@@ -70,7 +70,7 @@ class RunAgentTaskTool(MoviePilotTool):
|
||||
return f"Agent 定时任务 {task_id} 已暂停,请先恢复后再执行"
|
||||
if status == "running":
|
||||
return f"Agent 定时任务 {task_id} 正在执行,请勿重复触发"
|
||||
if not Scheduler().start_agent_task(payload.task_id):
|
||||
if not start_agent_task(payload.task_id):
|
||||
return f"Agent 定时任务 {task_id} 尚未注册到运行时调度器,无法立即执行"
|
||||
return (
|
||||
f"Agent 定时任务 {task_id} 已提交立即执行:{task_name}。"
|
||||
|
||||
@@ -46,12 +46,14 @@ class RunSchedulerTool(MoviePilotTool):
|
||||
@staticmethod
|
||||
def _run_scheduler_sync(job_id: str) -> tuple[bool, str]:
|
||||
"""同步触发定时服务,避免调度器扫描阻塞事件循环。"""
|
||||
from app.scheduler import Scheduler
|
||||
from app.application.scheduling import (
|
||||
list_scheduler_jobs,
|
||||
start_scheduler_job,
|
||||
)
|
||||
|
||||
scheduler = Scheduler()
|
||||
for scheduler_item in scheduler.list():
|
||||
for scheduler_item in list_scheduler_jobs():
|
||||
if scheduler_item.id == job_id:
|
||||
scheduler.start(job_id)
|
||||
start_scheduler_job(job_id)
|
||||
return True, scheduler_item.name
|
||||
return False, ""
|
||||
|
||||
@@ -60,7 +62,7 @@ class RunSchedulerTool(MoviePilotTool):
|
||||
logger.info(f"执行工具: {self.name}, 参数: job_id={job_id}")
|
||||
|
||||
try:
|
||||
from app.scheduler import AGENT_TASK_JOB_PREFIX
|
||||
from app.application.scheduling import AGENT_TASK_JOB_PREFIX
|
||||
|
||||
if job_id.startswith(f"{AGENT_TASK_JOB_PREFIX}-"):
|
||||
return (
|
||||
|
||||
@@ -57,16 +57,15 @@ class RunSlashCommandTool(MoviePilotTool):
|
||||
if not command.startswith("/"):
|
||||
command = f"/{command}"
|
||||
|
||||
# 从全局 Command 单例中验证命令是否存在(包含系统预设命令 + 插件命令 + 其他命令)
|
||||
from app.command import Command
|
||||
# 从命令注册表中验证命令是否存在(包含系统预设命令 + 插件命令 + 其他命令)
|
||||
from app.application.commands import get_command, get_commands
|
||||
|
||||
cmd_name = command.split()[0]
|
||||
command_obj = Command()
|
||||
matched_command = command_obj.get(cmd_name)
|
||||
matched_command = get_command(cmd_name)
|
||||
|
||||
if not matched_command:
|
||||
# 列出所有可用命令帮助用户
|
||||
all_commands = command_obj.get_commands()
|
||||
all_commands = get_commands()
|
||||
available_cmds = [
|
||||
f"{cmd} - {info.get('description', '无描述')}"
|
||||
for cmd, info in all_commands.items()
|
||||
|
||||
@@ -100,7 +100,7 @@ class UpdateAgentTaskTool(MoviePilotTool):
|
||||
|
||||
def _update_task(self, payload: UpdateAgentTaskInput) -> Optional[dict]:
|
||||
"""更新当前用户的任务并刷新运行时调度。"""
|
||||
from app.scheduler import Scheduler
|
||||
from app.application.scheduling import update_agent_task_job
|
||||
|
||||
oper = AgentTaskOper()
|
||||
task = oper.get(task_id=payload.task_id, user_id=str(self._user_id))
|
||||
@@ -174,8 +174,7 @@ class UpdateAgentTaskTool(MoviePilotTool):
|
||||
if current and current.last_status == "running":
|
||||
return {"error": f"Agent 定时任务 {payload.task_id} 正在执行,请稍后再修改"}
|
||||
return None
|
||||
scheduler = Scheduler()
|
||||
next_run_at = scheduler.update_agent_task_job(payload.task_id)
|
||||
next_run_at = update_agent_task_job(payload.task_id)
|
||||
updated_task = oper.get(task_id=payload.task_id, user_id=str(self._user_id))
|
||||
return oper.to_dict(
|
||||
updated_task,
|
||||
|
||||
+135
-65
@@ -1,24 +1,16 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
import threading
|
||||
import uuid
|
||||
from typing import Any, Dict, List, Optional
|
||||
from typing import TYPE_CHECKING, Any, Dict, List, Optional
|
||||
|
||||
from app.agent.policy import (
|
||||
DEFAULT_TOOL_POLICY_ORCHESTRATOR,
|
||||
AgentToolPolicyOrchestrator,
|
||||
AuthSource,
|
||||
PrincipalType,
|
||||
ToolOrigin,
|
||||
ToolPolicyContext,
|
||||
call_policy_hook,
|
||||
summarize_error,
|
||||
)
|
||||
from app.agent.tools.base import ToolExecutionTimeoutError, format_tool_result_for_agent
|
||||
from app.agent.tools.factory import MoviePilotToolFactory
|
||||
from app.agent.tools.catalog import ToolCatalogSnapshot
|
||||
from app.runtime.extensions.plugin_manager import PluginManager
|
||||
from app.runtime.log import logger
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from app.agent.policy import AgentToolPolicyOrchestrator, ToolPolicyContext
|
||||
from app.agent.tools.catalog import ToolCatalogSnapshot
|
||||
|
||||
|
||||
class ToolDefinition:
|
||||
"""
|
||||
@@ -53,31 +45,33 @@ class MoviePilotToolsManager:
|
||||
self.user_id = user_id
|
||||
self.session_id = session_id
|
||||
self.is_admin = is_admin
|
||||
self.policy_orchestrator = (
|
||||
policy_orchestrator or DEFAULT_TOOL_POLICY_ORCHESTRATOR
|
||||
)
|
||||
self._policy_context = ToolPolicyContext(
|
||||
session_id=session_id,
|
||||
user_id=user_id,
|
||||
origin=ToolOrigin.OPERATOR_DIRECT,
|
||||
principal_type=PrincipalType.SYSTEM_ADMIN_INTEGRATION,
|
||||
auth_source=AuthSource.API_TOKEN,
|
||||
channel=None,
|
||||
source="api",
|
||||
agent_context={"is_admin": is_admin},
|
||||
)
|
||||
self.policy_orchestrator = policy_orchestrator
|
||||
self._policy_context: Optional[ToolPolicyContext] = None
|
||||
self.tools: List[Any] = []
|
||||
self.catalog: Optional[ToolCatalogSnapshot] = None
|
||||
self._tools_lock = threading.Lock()
|
||||
self._plugin_agent_tools_revision = -1
|
||||
self._load_tools()
|
||||
self._catalog_materialized = False
|
||||
self._catalog_managed_by_factory = False
|
||||
|
||||
def _load_tools(self) -> None:
|
||||
@staticmethod
|
||||
def _summarize_error(error: Exception) -> str:
|
||||
"""仅在错误路径加载策略脱敏器,保持默认导入轻量。"""
|
||||
from app.agent.policy import summarize_error
|
||||
|
||||
return summarize_error(error)
|
||||
|
||||
def _load_tools_locked(self) -> None:
|
||||
"""
|
||||
加载所有MoviePilot工具
|
||||
在 manager 锁内加载所有 MoviePilot 工具。
|
||||
|
||||
工厂负责插件 revision 前后稳定窗口;manager 只发布完整快照,避免
|
||||
并发调用观察到一半刷新后的工具列表。
|
||||
"""
|
||||
from app.agent.runtime_loader import get_tool_factory
|
||||
|
||||
try:
|
||||
catalog = MoviePilotToolFactory.create_catalog(
|
||||
catalog = get_tool_factory().create_catalog(
|
||||
session_id=self.session_id,
|
||||
user_id=self.user_id,
|
||||
channel=None,
|
||||
@@ -89,17 +83,43 @@ class MoviePilotToolsManager:
|
||||
self.catalog = catalog
|
||||
self.tools = catalog.tools
|
||||
self._plugin_agent_tools_revision = catalog.plugin_revision
|
||||
self._catalog_materialized = True
|
||||
self._catalog_managed_by_factory = True
|
||||
logger.info(f"成功加载 {len(self.tools)} 个工具")
|
||||
except Exception as e:
|
||||
logger.error(f"加载工具失败: {summarize_error(e)}")
|
||||
logger.error(f"加载工具失败: {self._summarize_error(e)}")
|
||||
self.tools = []
|
||||
self.catalog = None
|
||||
self._plugin_agent_tools_revision = -1
|
||||
self._catalog_materialized = False
|
||||
self._catalog_managed_by_factory = False
|
||||
|
||||
def _load_tools(self) -> None:
|
||||
"""兼容显式刷新入口,并保证外部调用仍原子发布完整目录。"""
|
||||
with self._tools_lock:
|
||||
self._load_tools_locked()
|
||||
|
||||
def _ensure_tools_current(self) -> None:
|
||||
"""
|
||||
在插件工具注册表变化后惰性刷新工具实例。
|
||||
首次使用时加载目录,并在插件注册表变化后惰性刷新工具实例。
|
||||
"""
|
||||
# 调用方可能显式注入工具实例;这些实例仍由调用方拥有,manager 不应
|
||||
# 在第一次查询时用全量目录覆盖它们。
|
||||
if not self._catalog_materialized and self.tools:
|
||||
self._catalog_materialized = True
|
||||
return
|
||||
|
||||
if self._catalog_materialized and not self._catalog_managed_by_factory:
|
||||
return
|
||||
|
||||
if not self._catalog_materialized:
|
||||
with self._tools_lock:
|
||||
if not self._catalog_materialized:
|
||||
self._load_tools_locked()
|
||||
return
|
||||
|
||||
from app.runtime.extensions.plugin_manager import PluginManager
|
||||
|
||||
plugin_manager = PluginManager()
|
||||
if (
|
||||
self._plugin_agent_tools_revision
|
||||
@@ -112,7 +132,41 @@ class MoviePilotToolsManager:
|
||||
== plugin_manager.get_plugin_agent_tools_revision()
|
||||
):
|
||||
return
|
||||
self._load_tools()
|
||||
self._load_tools_locked()
|
||||
|
||||
def _ensure_policy_runtime(
|
||||
self,
|
||||
) -> tuple[AgentToolPolicyOrchestrator, ToolPolicyContext]:
|
||||
"""返回 direct 入口的策略对象,仅在真实工具调用前完成构造。"""
|
||||
policy_orchestrator = self.policy_orchestrator
|
||||
policy_context = self._policy_context
|
||||
if policy_orchestrator is not None and policy_context is not None:
|
||||
return policy_orchestrator, policy_context
|
||||
|
||||
from app.agent.policy import (
|
||||
DEFAULT_TOOL_POLICY_ORCHESTRATOR,
|
||||
AuthSource,
|
||||
PrincipalType,
|
||||
ToolOrigin,
|
||||
ToolPolicyContext,
|
||||
)
|
||||
|
||||
if policy_orchestrator is None:
|
||||
policy_orchestrator = DEFAULT_TOOL_POLICY_ORCHESTRATOR
|
||||
if policy_context is None:
|
||||
policy_context = ToolPolicyContext(
|
||||
session_id=self.session_id,
|
||||
user_id=self.user_id,
|
||||
origin=ToolOrigin.OPERATOR_DIRECT,
|
||||
principal_type=PrincipalType.SYSTEM_ADMIN_INTEGRATION,
|
||||
auth_source=AuthSource.API_TOKEN,
|
||||
channel=None,
|
||||
source="api",
|
||||
agent_context={"is_admin": self.is_admin},
|
||||
)
|
||||
self.policy_orchestrator = policy_orchestrator
|
||||
self._policy_context = policy_context
|
||||
return policy_orchestrator, policy_context
|
||||
|
||||
def list_tools(self) -> List[ToolDefinition]:
|
||||
"""
|
||||
@@ -122,8 +176,10 @@ class MoviePilotToolsManager:
|
||||
工具定义列表
|
||||
"""
|
||||
self._ensure_tools_current()
|
||||
with self._tools_lock:
|
||||
tools = list(self.tools)
|
||||
tools_list = []
|
||||
for tool in self.tools:
|
||||
for tool in tools:
|
||||
if getattr(tool, "_require_admin", False) and not self.is_admin:
|
||||
continue
|
||||
# 获取工具的输入参数模型
|
||||
@@ -156,26 +212,31 @@ class MoviePilotToolsManager:
|
||||
工具实例,如果未找到返回None
|
||||
"""
|
||||
self._ensure_tools_current()
|
||||
return next(
|
||||
(tool for tool in self.tools if tool.name == tool_name),
|
||||
None,
|
||||
)
|
||||
with self._tools_lock:
|
||||
return next(
|
||||
(tool for tool in self.tools if tool.name == tool_name),
|
||||
None,
|
||||
)
|
||||
|
||||
def get_strict_tool(self, tool_name: str) -> Optional[Any]:
|
||||
"""按当前目录唯一身份解析严格调用,重名时稳定失败。"""
|
||||
self._ensure_tools_current()
|
||||
if self.catalog is None or [
|
||||
id(tool) for tool in self.catalog.tools
|
||||
] != [id(tool) for tool in self.tools]:
|
||||
self.catalog = ToolCatalogSnapshot.from_tools(
|
||||
self.tools,
|
||||
plugin_revision=self._plugin_agent_tools_revision,
|
||||
factory_revision=MoviePilotToolFactory.catalog_factory_revision(),
|
||||
)
|
||||
if self.catalog is None:
|
||||
return None
|
||||
entry = self.catalog.resolve_unique(tool_name)
|
||||
return entry.tool if entry else None
|
||||
with self._tools_lock:
|
||||
if self.catalog is None or [
|
||||
id(tool) for tool in self.catalog.tools
|
||||
] != [id(tool) for tool in self.tools]:
|
||||
from app.agent.runtime_loader import get_tool_factory
|
||||
from app.agent.tools.catalog import ToolCatalogSnapshot
|
||||
|
||||
self.catalog = ToolCatalogSnapshot.from_tools(
|
||||
self.tools,
|
||||
plugin_revision=self._plugin_agent_tools_revision,
|
||||
factory_revision=get_tool_factory().catalog_factory_revision(),
|
||||
)
|
||||
if self.catalog is None:
|
||||
return None
|
||||
entry = self.catalog.resolve_unique(tool_name)
|
||||
return entry.tool if entry else None
|
||||
|
||||
@staticmethod
|
||||
def _resolve_field_schema(field_info: Dict[str, Any]) -> Dict[str, Any]:
|
||||
@@ -265,7 +326,7 @@ class MoviePilotToolsManager:
|
||||
schema = args_schema.model_json_schema()
|
||||
properties = schema.get("properties", {})
|
||||
except Exception as e:
|
||||
logger.warning(f"获取工具schema失败: {summarize_error(e)}")
|
||||
logger.warning(f"获取工具schema失败: {MoviePilotToolsManager._summarize_error(e)}")
|
||||
return arguments
|
||||
|
||||
# 规范化参数
|
||||
@@ -320,7 +381,14 @@ class MoviePilotToolsManager:
|
||||
)
|
||||
return error_msg
|
||||
|
||||
from app.agent.policy import call_policy_hook
|
||||
from app.agent.tools.base import (
|
||||
ToolExecutionTimeoutError,
|
||||
format_tool_result_for_agent,
|
||||
)
|
||||
|
||||
observation = None
|
||||
policy_orchestrator = None
|
||||
try:
|
||||
permission_error = self._check_tool_permission(tool_instance)
|
||||
if permission_error:
|
||||
@@ -328,11 +396,12 @@ class MoviePilotToolsManager:
|
||||
|
||||
# 规范化参数类型
|
||||
normalized_arguments = self._normalize_arguments(tool_instance, arguments)
|
||||
self._policy_context.agent_context["is_admin"] = self.is_admin
|
||||
policy_orchestrator, policy_context = self._ensure_policy_runtime()
|
||||
policy_context.agent_context["is_admin"] = self.is_admin
|
||||
observation = call_policy_hook(
|
||||
"start",
|
||||
self.policy_orchestrator.start,
|
||||
context=self._policy_context,
|
||||
policy_orchestrator.start,
|
||||
context=policy_context,
|
||||
tool=tool_instance,
|
||||
arguments=normalized_arguments,
|
||||
)
|
||||
@@ -346,28 +415,29 @@ class MoviePilotToolsManager:
|
||||
max_chars=getattr(tool_instance, "result_max_chars", None),
|
||||
)
|
||||
except ToolExecutionTimeoutError as e:
|
||||
if observation:
|
||||
call_policy_hook("fail", self.policy_orchestrator.fail, observation, e)
|
||||
logger.warning(summarize_error(e))
|
||||
if observation is not None and policy_orchestrator is not None:
|
||||
call_policy_hook("fail", policy_orchestrator.fail, observation, e)
|
||||
error_summary = self._summarize_error(e)
|
||||
logger.warning(error_summary)
|
||||
return format_tool_result_for_agent(
|
||||
summarize_error(e),
|
||||
error_summary,
|
||||
tool_name=tool_name,
|
||||
max_chars=getattr(tool_instance, "result_max_chars", None),
|
||||
)
|
||||
except Exception as e:
|
||||
if observation:
|
||||
call_policy_hook("fail", self.policy_orchestrator.fail, observation, e)
|
||||
error_summary = summarize_error(e)
|
||||
if observation is not None and policy_orchestrator is not None:
|
||||
call_policy_hook("fail", policy_orchestrator.fail, observation, e)
|
||||
error_summary = self._summarize_error(e)
|
||||
logger.error(f"调用工具 {tool_name} 时发生错误: {error_summary}")
|
||||
error_msg = json.dumps(
|
||||
{"error": f"调用工具 '{tool_name}' 时发生错误: {error_summary}"},
|
||||
ensure_ascii=False,
|
||||
)
|
||||
return error_msg
|
||||
if observation:
|
||||
if observation is not None and policy_orchestrator is not None:
|
||||
call_policy_hook(
|
||||
"finish",
|
||||
self.policy_orchestrator.finish,
|
||||
policy_orchestrator.finish,
|
||||
observation,
|
||||
str_result,
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user