以 Hermes 的可插拔记忆架构为骨架重写 deerflow 记忆模块,兼容原有 结构化数据,并新增 Hindsight 作为外部记忆后端。 P1 - 抽象层 + builtin 本地记忆: - MemoryProvider 抽象基类 + MemoryManager 编排层(builtin + ≤1 外部) - BuiltinFileProvider:USER.md/MEMORY.md 二元存储、字符配额、文件锁、 原子写、frozen snapshot - memory 工具(LangChain @tool):LLM 主动写入用户画像/工作笔记 - 记忆内容安全扫描(注入/外渗模式 + 不可见 unicode) - memory_config 改为嵌套结构 + 旧扁平字段兼容 + per-user 三层覆盖 - 迁移脚本 migrate_memory_to_v2.py:旧 memory.json -> USER.md/MEMORY.md - Gateway 新增 /api/memory/v2 与 v2/config 端点(用户可配置白名单字段) P2 - Hindsight 外部 provider: - HindsightProvider:双 bank 隔离(user bank 跨 agent 共享 + work bank agent 私有)、并发召回合并、auto-retain、on_memory_write 镜像 - hindsight_recall/reflect/retain 三个 LangChain 工具 - MemoryMiddleware.before_model 注入 Hindsight 动态召回 - context/tools/hybrid 三种 memory_mode - hindsight-client 作为可选依赖,未装时自动退回纯 builtin 注入走系统提示词(builtin frozen snapshot)+ before_model(Hindsight 动态召回)。后台抽取保留但默认关闭,P3 期重接入。 Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
198 lines
8.9 KiB
Python
198 lines
8.9 KiB
Python
import logging
|
|
|
|
from langchain.tools import BaseTool
|
|
|
|
from deerflow.config import get_app_config
|
|
from deerflow.config.app_config import AppConfig
|
|
from deerflow.reflection import resolve_variable
|
|
from deerflow.sandbox.security import is_host_bash_allowed
|
|
from deerflow.tools.builtins import ask_clarification_tool, memory_tool, present_file_tool, scheduled_task_tool, task_tool, view_image_tool
|
|
from deerflow.tools.builtins.tool_search import reset_deferred_registry
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
BUILTIN_TOOLS = [
|
|
present_file_tool,
|
|
ask_clarification_tool,
|
|
]
|
|
|
|
SUBAGENT_TOOLS = [
|
|
task_tool,
|
|
# task_status_tool is no longer exposed to LLM (backend handles polling internally)
|
|
]
|
|
|
|
|
|
def _is_host_bash_tool(tool: object) -> bool:
|
|
"""Return True if the tool config represents a host-bash execution surface."""
|
|
group = getattr(tool, "group", None)
|
|
use = getattr(tool, "use", None)
|
|
if group == "bash":
|
|
return True
|
|
if use == "deerflow.sandbox.tools:bash_tool":
|
|
return True
|
|
return False
|
|
|
|
|
|
def get_available_tools(
|
|
groups: list[str] | None = None,
|
|
include_mcp: bool = True,
|
|
model_name: str | None = None,
|
|
subagent_enabled: bool = False,
|
|
is_scheduled_run: bool = False,
|
|
*,
|
|
app_config: AppConfig | None = None,
|
|
) -> list[BaseTool]:
|
|
"""Get all available tools from config.
|
|
|
|
Note: MCP tools should be initialized at application startup using
|
|
`initialize_mcp_tools()` from deerflow.mcp module.
|
|
|
|
Args:
|
|
groups: Optional list of tool groups to filter by.
|
|
include_mcp: Whether to include tools from MCP servers (default: True).
|
|
model_name: Optional model name to determine if vision tools should be included.
|
|
subagent_enabled: Whether to include subagent tools (task, task_status).
|
|
|
|
Returns:
|
|
List of available tools.
|
|
"""
|
|
config = app_config or get_app_config()
|
|
tool_configs = [tool for tool in config.tools if groups is None or tool.group in groups]
|
|
|
|
# Do not expose host bash by default when LocalSandboxProvider is active.
|
|
if not is_host_bash_allowed(config):
|
|
tool_configs = [tool for tool in tool_configs if not _is_host_bash_tool(tool)]
|
|
|
|
loaded_tools_raw = [(cfg, resolve_variable(cfg.use, BaseTool)) for cfg in tool_configs]
|
|
|
|
# Warn when the config ``name`` field and the tool object's ``.name``
|
|
# attribute diverge — this mismatch is the root cause of issue #1803 where
|
|
# the LLM receives one name in its tool schema but the runtime router
|
|
# recognises a different name, producing "not a valid tool" errors.
|
|
for cfg, loaded in loaded_tools_raw:
|
|
if cfg.name != loaded.name:
|
|
logger.warning(
|
|
"Tool name mismatch: config name %r does not match tool .name %r (use: %s). The tool's own .name will be used for binding.",
|
|
cfg.name,
|
|
loaded.name,
|
|
cfg.use,
|
|
)
|
|
|
|
loaded_tools = [t for _, t in loaded_tools_raw]
|
|
|
|
# Conditionally add tools based on config
|
|
builtin_tools = BUILTIN_TOOLS.copy()
|
|
if not is_scheduled_run:
|
|
builtin_tools.append(scheduled_task_tool)
|
|
|
|
# Memory V2:记忆与 builtin provider 都启用时,暴露 memory 工具给 LLM 主动写入
|
|
memory_config = getattr(config, "memory", None)
|
|
if memory_config is not None and getattr(memory_config, "enabled", False):
|
|
builtin_config = getattr(memory_config, "builtin", None)
|
|
if builtin_config is not None and getattr(builtin_config, "enabled", False):
|
|
builtin_tools.append(memory_tool)
|
|
logger.info("Including memory tool (Memory V2 builtin provider enabled)")
|
|
|
|
# Hindsight 在 tools / hybrid 模式下,额外暴露 3 个 Hindsight 工具
|
|
if getattr(memory_config, "provider", "builtin") == "hindsight":
|
|
hindsight_config = getattr(memory_config, "hindsight", None)
|
|
memory_mode = getattr(hindsight_config, "memory_mode", "context") if hindsight_config else "context"
|
|
if memory_mode in ("tools", "hybrid"):
|
|
from deerflow.tools.builtins.hindsight_tools import HINDSIGHT_TOOLS
|
|
|
|
builtin_tools.extend(HINDSIGHT_TOOLS)
|
|
logger.info("Including Hindsight tools (memory_mode=%s)", memory_mode)
|
|
skill_evolution_config = getattr(config, "skill_evolution", None)
|
|
if getattr(skill_evolution_config, "enabled", False):
|
|
from deerflow.tools.builtins.skill_tools import skill_list_tool, skill_view_tool
|
|
from deerflow.tools.skill_manage_tool import skill_manage_tool
|
|
|
|
builtin_tools.extend([skill_list_tool, skill_view_tool, skill_manage_tool])
|
|
|
|
# Add subagent tools only if enabled via runtime parameter
|
|
if subagent_enabled:
|
|
builtin_tools.extend(SUBAGENT_TOOLS)
|
|
logger.info("Including subagent tools (task)")
|
|
|
|
# If no model_name specified, use the first model (default)
|
|
if model_name is None and config.models:
|
|
model_name = config.models[0].name
|
|
|
|
# Add view_image_tool only if the model supports vision
|
|
model_config = config.get_model_config(model_name) if model_name else None
|
|
if model_config is not None and model_config.supports_vision:
|
|
builtin_tools.append(view_image_tool)
|
|
logger.info(f"Including view_image_tool for model '{model_name}' (supports_vision=True)")
|
|
|
|
# Get cached MCP tools if enabled
|
|
# NOTE: We use ExtensionsConfig.from_file() instead of config.extensions
|
|
# to always read the latest configuration from disk. This ensures that changes
|
|
# made through the Gateway API (which runs in a separate process) are immediately
|
|
# reflected when loading MCP tools.
|
|
mcp_tools = []
|
|
# Reset deferred registry upfront to prevent stale state from previous calls
|
|
reset_deferred_registry()
|
|
if include_mcp:
|
|
try:
|
|
from deerflow.config.extensions_config import ExtensionsConfig
|
|
from deerflow.mcp.cache import get_cached_mcp_tools
|
|
|
|
extensions_config = ExtensionsConfig.from_file()
|
|
if extensions_config.get_enabled_mcp_servers():
|
|
mcp_tools = get_cached_mcp_tools()
|
|
if mcp_tools:
|
|
logger.info(f"Using {len(mcp_tools)} cached MCP tool(s)")
|
|
|
|
# When tool_search is enabled, register MCP tools in the
|
|
# deferred registry and add tool_search to builtin tools.
|
|
if config.tool_search.enabled:
|
|
from deerflow.tools.builtins.tool_search import DeferredToolRegistry, set_deferred_registry
|
|
from deerflow.tools.builtins.tool_search import tool_search as tool_search_tool
|
|
|
|
registry = DeferredToolRegistry()
|
|
for t in mcp_tools:
|
|
registry.register(t)
|
|
set_deferred_registry(registry)
|
|
builtin_tools.append(tool_search_tool)
|
|
logger.info(f"Tool search active: {len(mcp_tools)} tools deferred")
|
|
except ImportError:
|
|
logger.warning("MCP module not available. Install 'langchain-mcp-adapters' package to enable MCP tools.")
|
|
except Exception as e:
|
|
logger.error(f"Failed to get cached MCP tools: {e}")
|
|
|
|
# Add invoke_acp_agent tool if any ACP agents are configured
|
|
acp_tools: list[BaseTool] = []
|
|
try:
|
|
from deerflow.tools.builtins.invoke_acp_agent_tool import build_invoke_acp_agent_tool
|
|
|
|
if app_config is None:
|
|
from deerflow.config.acp_config import get_acp_agents
|
|
|
|
acp_agents = get_acp_agents()
|
|
else:
|
|
acp_agents = getattr(config, "acp_agents", {}) or {}
|
|
if acp_agents:
|
|
acp_tools.append(build_invoke_acp_agent_tool(acp_agents))
|
|
logger.info(f"Including invoke_acp_agent tool ({len(acp_agents)} agent(s): {list(acp_agents.keys())})")
|
|
except Exception as e:
|
|
logger.warning(f"Failed to load ACP tool: {e}")
|
|
|
|
logger.info(f"Total tools loaded: {len(loaded_tools)}, built-in tools: {len(builtin_tools)}, MCP tools: {len(mcp_tools)}, ACP tools: {len(acp_tools)}")
|
|
|
|
# Deduplicate by tool name — config-loaded tools take priority, followed by
|
|
# built-ins, MCP tools, and ACP tools. Duplicate names cause the LLM to
|
|
# receive ambiguous or concatenated function schemas (issue #1803).
|
|
all_tools = loaded_tools + builtin_tools + mcp_tools + acp_tools
|
|
seen_names: set[str] = set()
|
|
unique_tools: list[BaseTool] = []
|
|
for t in all_tools:
|
|
if t.name not in seen_names:
|
|
unique_tools.append(t)
|
|
seen_names.add(t.name)
|
|
else:
|
|
logger.warning(
|
|
"Duplicate tool name %r detected and skipped — check your config.yaml and MCP server registrations (issue #1803).",
|
|
t.name,
|
|
)
|
|
return unique_tools
|