增加智能体

This commit is contained in:
许海洋 committed 2026-05-20 17:38:04 +08:00
1 parent 39def1c20b
commit 37bd8c6591
10 files changed
+600 -90

No files matched your search

@@ -336,13 +336,13 @@ def _make_lead_agent(config: RunnableConfig, *, app_config: AppConfig):
requested_model_name: str | None = cfg.get("model_name") or cfg.get("model")
is_plan_mode = cfg.get("is_plan_mode", False)
subagent_enabled = cfg.get("subagent_enabled", False)
max_concurrent_subagents = cfg.get("max_concurrent_subagents", 3)
is_bootstrap = cfg.get("is_bootstrap", False)
writing_mode = bool(cfg.get("writing_mode", False))
writing_artifact_path = cfg.get("writing_artifact_path") if writing_mode else None
if not isinstance(writing_artifact_path, str) or not writing_artifact_path.strip():
writing_artifact_path = None
agent_id = validate_agent_id(cfg.get("agent_id") or cfg.get("agent_name"))
max_concurrent_subagents = cfg.get("max_concurrent_subagents", 3)
is_bootstrap = cfg.get("is_bootstrap", False)
writing_mode = bool(cfg.get("writing_mode", False))
writing_artifact_path = cfg.get("writing_artifact_path") if writing_mode else None
if not isinstance(writing_artifact_path, str) or not writing_artifact_path.strip():
writing_artifact_path = None
agent_id = validate_agent_id(cfg.get("agent_id") or cfg.get("agent_name"))
agent_config = load_agent_config(agent_id) if not is_bootstrap else None
agent_display_name = agent_config.name if agent_config else agent_id
@@ -360,16 +360,52 @@ def _make_lead_agent(config: RunnableConfig, *, app_config: AppConfig):
logger.warning(f"Thinking mode is enabled but model '{model_name}' does not support it; fallback to non-thinking mode.")
thinking_enabled = False
# Check if we're in subagent mode (router agent delegating to a subagent)
subagent_mode = cfg.get("subagent_mode")
if subagent_mode and agent_id:
# Load subagent config and SOUL.md from subagents/{subagent_mode}/
from deerflow.config.paths import get_paths
paths = get_paths()
subagent_dir = paths.agent_dir(agent_id) / "subagents" / subagent_mode
subagent_soul_path = subagent_dir / "SOUL.md"
subagent_config_path = subagent_dir / "config.yaml"
if subagent_soul_path.exists():
subagent_soul = subagent_soul_path.read_text(encoding="utf-8").strip()
else:
subagent_soul = None
subagent_tool_groups = None
if subagent_config_path.exists():
try:
import yaml
with open(subagent_config_path, encoding="utf-8") as f:
subagent_data = yaml.safe_load(f) or {}
subagent_tool_groups = subagent_data.get("tool_groups")
except yaml.YAMLError:
logger.warning(f"Failed to parse subagent config: {subagent_config_path}")
logger.info(
"Subagent mode(%s) for router(%s) -> subagent_soul: %s, tool_groups: %s",
subagent_mode,
agent_id,
"found" if subagent_soul else "not found",
subagent_tool_groups,
)
logger.info(
"Create Agent(%s) -> thinking_enabled: %s, reasoning_effort: %s, model_name: %s, is_plan_mode: %s, subagent_enabled: %s, max_concurrent_subagents: %s, writing_mode: %s",
agent_id or "default",
thinking_enabled,
reasoning_effort,
model_name,
is_plan_mode,
subagent_enabled,
max_concurrent_subagents,
writing_mode,
"Create Agent(%s) -> thinking_enabled: %s, reasoning_effort: %s, model_name: %s, is_plan_mode: %s, subagent_enabled: %s, max_concurrent_subagents: %s, writing_mode: %s, subagent_mode: %s",
agent_id or "default",
thinking_enabled,
reasoning_effort,
model_name,
is_plan_mode,
subagent_enabled,
max_concurrent_subagents,
writing_mode,
subagent_mode,
)
# Inject run metadata for LangSmith trace tagging
@@ -393,10 +429,10 @@ def _make_lead_agent(config: RunnableConfig, *, app_config: AppConfig):
"thinking_enabled": thinking_enabled,
"reasoning_effort": reasoning_effort,
"is_plan_mode": is_plan_mode,
"subagent_enabled": subagent_enabled,
"writing_mode": writing_mode,
"writing_artifact_path": writing_artifact_path,
"tool_groups": agent_config.tool_groups if agent_config else None,
"subagent_enabled": subagent_enabled,
"writing_mode": writing_mode,
"writing_artifact_path": writing_artifact_path,
"tool_groups": subagent_tool_groups if subagent_mode else (agent_config.tool_groups if agent_config else None),
"available_skills": runtime_available_skills,
}
)
@@ -415,21 +451,24 @@ def _make_lead_agent(config: RunnableConfig, *, app_config: AppConfig):
middleware=_build_middlewares(config, model_name=model_name, app_config=resolved_app_config),
system_prompt=apply_prompt_template(
subagent_enabled=subagent_enabled,
max_concurrent_subagents=max_concurrent_subagents,
writing_mode=writing_mode,
writing_artifact_path=writing_artifact_path,
available_skills=set(["bootstrap"]),
app_config=resolved_app_config,
max_concurrent_subagents=max_concurrent_subagents,
writing_mode=writing_mode,
writing_artifact_path=writing_artifact_path,
available_skills=set(["bootstrap"]),
app_config=resolved_app_config,
),
state_schema=ThreadState,
)
# Default lead agent (unchanged behavior)
# Use subagent tool groups if in subagent mode, otherwise use parent agent config
effective_tool_groups = subagent_tool_groups if subagent_mode else (agent_config.tool_groups if agent_config else None)
return create_agent(
model=create_chat_model(name=model_name, thinking_enabled=thinking_enabled, reasoning_effort=reasoning_effort, app_config=resolved_app_config),
tools=get_available_tools(
model_name=model_name,
groups=agent_config.tool_groups if agent_config else None,
groups=effective_tool_groups,
subagent_enabled=subagent_enabled,
is_scheduled_run=cfg.get("is_scheduled_run", False),
app_config=resolved_app_config,
@@ -438,12 +477,13 @@ def _make_lead_agent(config: RunnableConfig, *, app_config: AppConfig):
system_prompt=apply_prompt_template(
subagent_enabled=subagent_enabled,
max_concurrent_subagents=max_concurrent_subagents,
agent_id=agent_id,
agent_name=agent_display_name,
writing_mode=writing_mode,
writing_artifact_path=writing_artifact_path,
available_skills=(set(agent_config.skills or []) if agent_config else None),
app_config=resolved_app_config,
agent_id=agent_id,
agent_name=agent_display_name,
writing_mode=writing_mode,
writing_artifact_path=writing_artifact_path,
available_skills=(set(agent_config.skills or []) if agent_config else None),
app_config=resolved_app_config,
soul_override=subagent_soul if subagent_mode else None,
),
state_schema=ThreadState,
)