ref:将make_question从Plugin改为回复前工作;为tool添加chat_id字段
This commit is contained in:
@@ -1,14 +1,25 @@
|
||||
from typing import Optional, Type
|
||||
from typing import Optional, Type, TYPE_CHECKING
|
||||
from src.plugin_system.base.base_tool import BaseTool
|
||||
from src.plugin_system.base.component_types import ComponentType
|
||||
|
||||
from src.common.logger import get_logger
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from src.chat.message_receive.chat_stream import ChatStream
|
||||
|
||||
logger = get_logger("tool_api")
|
||||
|
||||
|
||||
def get_tool_instance(tool_name: str) -> Optional[BaseTool]:
|
||||
"""获取公开工具实例"""
|
||||
def get_tool_instance(tool_name: str, chat_stream: Optional["ChatStream"] = None) -> Optional[BaseTool]:
|
||||
"""获取公开工具实例
|
||||
|
||||
Args:
|
||||
tool_name: 工具名称
|
||||
chat_stream: 聊天流对象,用于传递聊天上下文信息
|
||||
|
||||
Returns:
|
||||
Optional[BaseTool]: 工具实例,如果未找到则返回None
|
||||
"""
|
||||
from src.plugin_system.core import component_registry
|
||||
|
||||
# 获取插件配置
|
||||
@@ -19,7 +30,7 @@ def get_tool_instance(tool_name: str) -> Optional[BaseTool]:
|
||||
plugin_config = None
|
||||
|
||||
tool_class: Type[BaseTool] = component_registry.get_component_class(tool_name, ComponentType.TOOL) # type: ignore
|
||||
return tool_class(plugin_config) if tool_class else None
|
||||
return tool_class(plugin_config, chat_stream) if tool_class else None
|
||||
|
||||
|
||||
def get_llm_available_tool_definitions():
|
||||
|
||||
@@ -1,10 +1,13 @@
|
||||
from abc import ABC, abstractmethod
|
||||
from typing import Any, List, Optional, Tuple
|
||||
from typing import Any, List, Optional, Tuple, TYPE_CHECKING
|
||||
from rich.traceback import install
|
||||
|
||||
from src.common.logger import get_logger
|
||||
from src.plugin_system.base.component_types import ComponentType, ToolInfo, ToolParamType
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from src.chat.message_receive.chat_stream import ChatStream
|
||||
|
||||
install(extra_lines=3)
|
||||
|
||||
logger = get_logger("base_tool")
|
||||
@@ -29,8 +32,23 @@ class BaseTool(ABC):
|
||||
available_for_llm: bool = False
|
||||
"""是否可供LLM使用"""
|
||||
|
||||
def __init__(self, plugin_config: Optional[dict] = None):
|
||||
def __init__(self, plugin_config: Optional[dict] = None, chat_stream: Optional["ChatStream"] = None):
|
||||
"""初始化工具基类
|
||||
|
||||
Args:
|
||||
plugin_config: 插件配置字典
|
||||
chat_stream: 聊天流对象,用于获取聊天上下文信息
|
||||
"""
|
||||
self.plugin_config = plugin_config or {} # 直接存储插件配置字典
|
||||
|
||||
# =============================================================================
|
||||
# 便捷属性 - 直接在初始化时获取常用聊天信息(与BaseAction保持一致)
|
||||
# =============================================================================
|
||||
|
||||
# 获取聊天流对象
|
||||
self.chat_stream = chat_stream
|
||||
self.chat_id = self.chat_stream.stream_id if self.chat_stream else None
|
||||
self.platform = getattr(self.chat_stream, "platform", None) if self.chat_stream else None
|
||||
|
||||
@classmethod
|
||||
def get_tool_definition(cls) -> dict[str, Any]:
|
||||
|
||||
@@ -223,7 +223,7 @@ class ToolExecutor:
|
||||
function_args["llm_called"] = True # 标记为LLM调用
|
||||
|
||||
# 获取对应工具实例
|
||||
tool_instance = tool_instance or get_tool_instance(function_name)
|
||||
tool_instance = tool_instance or get_tool_instance(function_name, self.chat_stream)
|
||||
if not tool_instance:
|
||||
logger.warning(f"未知工具名称: {function_name}")
|
||||
return None
|
||||
|
||||
Reference in New Issue
Block a user