- 在 RelationshipFetcher 中添加 build_chat_stream_impression 方法,支持聊天流印象信息构建 - 扩展数据库模型,为 ChatStreams 表添加聊天流印象相关字段(stream_impression_text、stream_chat_style、stream_topic_keywords、stream_interest_score) - 为 UserRelationships 表添加用户别名和偏好关键词字段(user_aliases、preference_keywords) - 在 DefaultReplyer、Prompt 和 S4U PromptBuilder 中集成用户关系信息和聊天流印象的组合输出 - 重构工具系统,为 BaseTool 添加 chat_stream 参数支持上下文感知 - 移除旧的 ChatterRelationshipTracker 及相关关系追踪逻辑,统一使用评分API - 在 AffinityChatterPlugin 中添加 UserProfileTool 和 ChatStreamImpressionTool 支持 - 优化计划执行器,移除关系追踪相关代码并改进错误处理 BREAKING CHANGE: 移除了 ChatterRelationshipTracker 类及相关的关系追踪功能,现在统一使用 scoring_api 进行关系管理。BaseTool 构造函数现在需要 chat_stream 参数。
67 lines
2.3 KiB
Python
67 lines
2.3 KiB
Python
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() -> 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()
|
||
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
|
||
|