使Tool支持读取配置文件,优化了开始执行工具调用时的日志输出

This commit is contained in:
Windpicker-owo
2025-08-06 12:01:31 +08:00
parent c7ac95b9f8
commit 94a66bd235
3 changed files with 38 additions and 4 deletions

View File

@@ -10,9 +10,12 @@ logger = get_logger("tool_api")
def get_tool_instance(tool_name: str) -> Optional[BaseTool]: def get_tool_instance(tool_name: str) -> Optional[BaseTool]:
"""获取公开工具实例""" """获取公开工具实例"""
from src.plugin_system.core import component_registry from src.plugin_system.core import component_registry
# 获取插件配置
plugin_name =component_registry.get_component_info(tool_name, ComponentType.TOOL).plugin_name
plugin_config = component_registry.get_plugin_config(plugin_name)
tool_class: Type[BaseTool] = component_registry.get_component_class(tool_name, ComponentType.TOOL) # type: ignore tool_class: Type[BaseTool] = component_registry.get_component_class(tool_name, ComponentType.TOOL) # type: ignore
return tool_class() if tool_class else None return tool_class(plugin_config) if tool_class else None
def get_llm_available_tool_definitions(): def get_llm_available_tool_definitions():

View File

@@ -1,5 +1,5 @@
from abc import ABC, abstractmethod from abc import ABC, abstractmethod
from typing import Any, List, Tuple from typing import Any, List, Optional, Tuple
from rich.traceback import install from rich.traceback import install
from src.common.logger import get_logger from src.common.logger import get_logger
@@ -29,6 +29,9 @@ class BaseTool(ABC):
available_for_llm: bool = False available_for_llm: bool = False
"""是否可供LLM使用""" """是否可供LLM使用"""
def __init__(self, plugin_config: Optional[dict] = None):
self.plugin_config = plugin_config or {} # 直接存储插件配置字典
@classmethod @classmethod
def get_tool_definition(cls) -> dict[str, Any]: def get_tool_definition(cls) -> dict[str, Any]:
"""获取工具定义用于LLM工具调用 """获取工具定义用于LLM工具调用
@@ -89,3 +92,28 @@ class BaseTool(ABC):
raise ValueError(f"工具类 {self.__class__.__name__} 缺少必要参数: {param_name}") raise ValueError(f"工具类 {self.__class__.__name__} 缺少必要参数: {param_name}")
return await self.execute(function_args) return await self.execute(function_args)
def get_config(self, key: str, default=None):
"""获取插件配置值,使用嵌套键访问
Args:
key: 配置键名,使用嵌套访问如 "section.subsection.key"
default: 默认值
Returns:
Any: 配置值或默认值
"""
if not self.plugin_config:
return default
# 支持嵌套键访问
keys = key.split(".")
current = self.plugin_config
for k in keys:
if isinstance(current, dict) and k in current:
current = current[k]
else:
return default
return current

View File

@@ -148,8 +148,11 @@ class ToolExecutor:
if not tool_calls: if not tool_calls:
logger.debug(f"{self.log_prefix}无需执行工具") logger.debug(f"{self.log_prefix}无需执行工具")
return [], [] return [], []
logger.info(f"{self.log_prefix}开始执行工具调用: {tool_calls}") # 提取tool_calls中的函数名称
func_names = [call.func_name for call in tool_calls if call.func_name]
logger.info(f"{self.log_prefix}开始执行工具调用: {func_names}")
# 执行每个工具调用 # 执行每个工具调用
for tool_call in tool_calls: for tool_call in tool_calls: