Files
Mofox-Core/src/plugin_system/apis/tool_api.py
minecraft1024a affd70b165 refactor(chat): 简化Action和PlusCommand的调用预处理
移除 `ChatBot` 和 `ActionModifier` 中用于过滤禁用组件的模板代码。

这两个模块现在直接从 `ComponentRegistry` 获取为当前聊天会话(`stream_id`)定制的可用组件列表。所有关于组件是否启用的判断逻辑都已下沉到 `plugin_system` 核心中,使得上层调用代码更清晰,且不再需要依赖 `global_announcement_manager` 来进行手动过滤。
2025-11-22 09:51:31 +08:00

67 lines
2.3 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

from typing import Any
from src.common.logger import get_logger
from src.plugin_system.base.base_tool import BaseTool
from src.plugin_system.base.component_types import ComponentType
logger = get_logger("tool_api")
def get_tool_instance(tool_name: str, chat_stream: Any = None) -> BaseTool | None:
"""获取公开工具实例
Args:
tool_name: 工具名称
chat_stream: 聊天流对象,用于提供上下文信息
Returns:
BaseTool: 工具实例如果工具不存在则返回None
"""
from src.plugin_system.core import component_registry
# 获取插件配置
tool_info = component_registry.get_component_info(tool_name, ComponentType.TOOL)
if tool_info:
plugin_config = component_registry.get_plugin_config(tool_info.plugin_name)
else:
plugin_config = None
tool_class: type[BaseTool] = component_registry.get_component_class(tool_name, ComponentType.TOOL) # type: ignore
return tool_class(plugin_config, chat_stream) if tool_class else None
def get_llm_available_tool_definitions(stream_id : str | None) -> list[dict[str, Any]]:
"""获取LLM可用的工具定义列表包括 MCP 工具)
Returns:
list[dict[str, Any]]: 工具定义列表
"""
from src.plugin_system.core import component_registry
llm_available_tools = component_registry.get_llm_available_tools(stream_id)
tool_definitions = []
# 获取常规工具定义
for tool_name, tool_class in llm_available_tools.items():
try:
# 调用类方法 get_tool_definition 获取定义
definition = tool_class.get_tool_definition()
tool_definitions.append(definition)
except Exception as e:
logger.error(f"获取工具 {tool_name} 的定义失败: {e}")
# 获取 MCP 工具定义
try:
mcp_tools = component_registry.get_mcp_tools()
for mcp_tool in mcp_tools:
try:
definition = mcp_tool.get_tool_definition()
tool_definitions.append(definition)
except Exception as e:
logger.error(f"获取 MCP 工具 {mcp_tool.name} 的定义失败: {e}")
except Exception as e:
logger.debug(f"获取 MCP 工具列表失败(可能未启用): {e}")
return tool_definitions