mirror of
https://github.com/bytedance/deer-flow.git
synced 2026-09-22 04:26:18 +00:00
* feat(channels): add GitHub event-driven agents (#3754) Add a webhook-driven GitHub channel with fail-closed webhook routing, deterministic per-agent PR/issue threads, mention-gated trigger fan-out, GitHub App token injection for sandboxed gh/git commands, and backend/AGENTS.md documentation. * fix(llm-middleware): classify bare IndexError as transient Upstream chat providers occasionally return 200 OK with an empty generations list (observed against Volces "coding" on ark.cn-beijing.volces.com). When that happens, langchain_core.language_models.chat_models.ainvoke raises ``IndexError: list index out of range`` at ``llm_result.generations[0][0].message`` and kills the run. Treat a bare IndexError reaching the middleware as a transient upstream-payload glitch and route it through the existing retry/backoff path instead of failing the whole agent run. The retry budget and backoff schedule are unchanged. Adds three regression tests covering the classifier and both the recover-on-retry and exhausted-retries paths. * fix(runtime): ignore stale LLM fallback markers from prior runs When a run on a thread ends with the LLM-error-handling middleware emitting a `deerflow_error_fallback`-marked AIMessage (e.g. after the IndexError empty-generations classification fix lands), that message is persisted to the thread's checkpoint as part of the messages channel. LangGraph replays the full message history in `stream_mode="values"` chunks, so every subsequent run on the same thread re-streams the stale fallback marker — and the worker's chunk scanner faithfully picks it up, flipping `RunStatus.success` to `RunStatus.error` for runs that themselves had no LLM failure at all. Snapshot the set of pre-existing message ids from the pre-run checkpoint and thread it through `_extract_llm_error_fallback_message` / `_try_extract_from_message` as a filter. Markers on history messages are ignored; markers on fresh messages produced during this run still trip the error path. Falls back to an empty set when the checkpointer is absent or the snapshot can't be captured, preserving the prior behavior on first-run / no-state paths. Adds unit tests for the new filter (helper-level and `_collect_pre_existing_message_ids`) plus an integration test exercising the full `run_agent` path with a stale history checkpointer. * fix(channels): make github channel fire-and-forget to avoid httpx.ReadTimeout on long runs GitHub agent runs (clone -> edit -> test -> push -> PR) routinely exceed the langgraph_sdk default 300s read deadline. The manager's runs.wait call kept an HTTP stream open for the entire run lifetime, so the long run blew up with httpx.ReadTimeout and the outer except branch then released the dedupe key and emitted a false 'internal error' outbound. The GitHub channel's outbound send is log-only by design: agents post to the issue/PR via the gh CLI in the sandbox when they choose to comment or create a PR. There is nothing for the manager to ferry back, so the long-poll was pure overhead. This change adds ChannelRunPolicy.fire_and_forget (default False) and sets it True for the github channel. When fire_and_forget is True, _handle_chat dispatches via client.runs.create (short POST, returns once the run is pending) instead of client.runs.wait, and skips the response-extraction + outbound-publish block. ConflictError on a busy thread still trips the standard THREAD_BUSY_MESSAGE path so behavior on the busy case is preserved for any future non-github fire-and-forget channel. Other (non-github) channels are unchanged: their policy defaults fire_and_forget=False and they continue to dispatch via runs.wait. Adds 6 regression tests in tests/test_channels.py::TestGithubFireAndForget: - Default ChannelRunPolicy.fire_and_forget is False. - The github policy registers fire_and_forget=True. - github inbound calls runs.create, not runs.wait, with the right kwargs. - github inbound publishes no outbound on success. - ConflictError from runs.create still emits THREAD_BUSY_MESSAGE. - Non-github channels (slack) still dispatch via runs.wait. * test(lead-agent): accept user_id kwarg in skill-policy test stubs The two GitHub-channel tests added in #3754 stubbed _load_enabled_skills_for_tool_policy with a lambda that only accepted `available_skills` and `app_config`, but the real function (and its call site in agent.py) also passes `user_id`. This raised TypeError on every run, failing backend-unit-tests. Add `user_id=None` to match the three sibling stubs in the same file. * refactor(gateway): disambiguate context-key set names The two frozensets _INTERNAL_ONLY_CONTEXT_KEYS and _CONTEXT_ONLY_KEYS shared a confusable "CONTEXT_ONLY" token in different orders, and the first broke the _CONTEXT_<X>_KEYS pattern of its sibling _CONTEXT_CONFIGURABLE_KEYS. Rename to make the distinct axes explicit: _CONTEXT_INTERNAL_CALLER_KEYS - WHO: internal callers (scheduler) only _CONTEXT_RUNTIME_ONLY_KEYS - WHERE: runtime context only, never configurable Pure rename, no behavior change.
601 lines
29 KiB
Python
601 lines
29 KiB
Python
"""Lead agent factory.
|
|
|
|
INVARIANT — tracing callback placement
|
|
======================================
|
|
|
|
Tracing callbacks (Langfuse, LangSmith) are attached at the **graph
|
|
invocation root** in :func:`_make_lead_agent` (see the
|
|
``build_tracing_callbacks()`` block that appends to ``config["callbacks"]``).
|
|
Every ``create_chat_model(...)`` call inside this module — and inside any
|
|
middleware reachable from this graph (e.g. ``TitleMiddleware``) — MUST pass
|
|
``attach_tracing=False``.
|
|
|
|
Forgetting that flag emits duplicate spans (one rooted at the graph, one at
|
|
the model) AND prevents the Langfuse handler's ``propagate_attributes``
|
|
path from firing, so ``session_id`` / ``user_id`` never reach the trace.
|
|
The four current sites are: bootstrap agent, default agent, summarization
|
|
middleware, and the async path inside ``TitleMiddleware``. Any new in-graph
|
|
``create_chat_model`` call must add to this list and pass the flag.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import logging
|
|
|
|
from langchain.agents import create_agent
|
|
from langchain.agents.middleware import AgentMiddleware
|
|
from langchain_core.runnables import RunnableConfig
|
|
|
|
from deerflow.agents.lead_agent.prompt import apply_prompt_template
|
|
from deerflow.agents.memory.summarization_hook import memory_flush_hook
|
|
from deerflow.agents.middlewares.clarification_middleware import ClarificationMiddleware
|
|
from deerflow.agents.middlewares.loop_detection_middleware import LoopDetectionMiddleware
|
|
from deerflow.agents.middlewares.memory_middleware import MemoryMiddleware
|
|
from deerflow.agents.middlewares.safety_finish_reason_middleware import SafetyFinishReasonMiddleware
|
|
from deerflow.agents.middlewares.subagent_limit_middleware import SubagentLimitMiddleware
|
|
from deerflow.agents.middlewares.summarization_middleware import BeforeSummarizationHook, DeerFlowSummarizationMiddleware
|
|
from deerflow.agents.middlewares.title_middleware import TitleMiddleware
|
|
from deerflow.agents.middlewares.todo_middleware import TodoMiddleware
|
|
from deerflow.agents.middlewares.token_usage_middleware import TokenUsageMiddleware
|
|
from deerflow.agents.middlewares.tool_error_handling_middleware import build_lead_runtime_middlewares
|
|
from deerflow.agents.middlewares.view_image_middleware import ViewImageMiddleware
|
|
from deerflow.agents.thread_state import ThreadState
|
|
from deerflow.config.agents_config import load_agent_config, validate_agent_name
|
|
from deerflow.config.app_config import AppConfig, get_app_config
|
|
from deerflow.models import create_chat_model
|
|
from deerflow.skills.tool_policy import SKILL_LOADING_TOOL_NAMES, filter_tools_by_skill_allowed_tools
|
|
from deerflow.skills.types import Skill
|
|
from deerflow.tracing import build_tracing_callbacks
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
_BOOTSTRAP_SKILL_NAMES = {"bootstrap"}
|
|
_NON_INTERACTIVE_DISABLED_TOOL_NAMES = frozenset({"ask_clarification"})
|
|
|
|
# Channels whose inbound messages originate from untrusted external
|
|
# commenters (anyone on a GitHub repo, etc.) and whose run context is
|
|
# therefore unsafe for admin-shaped tools like ``update_agent``. The
|
|
# corresponding gate lives in :func:`_make_lead_agent`; the channel name
|
|
# itself is plumbed into ``run_context`` by
|
|
# ``ChannelManager._resolve_run_params``.
|
|
_WEBHOOK_CHANNELS: frozenset[str] = frozenset({"github"})
|
|
|
|
|
|
def _get_runtime_config(config: RunnableConfig) -> dict:
|
|
"""Merge legacy configurable options with LangGraph runtime context."""
|
|
cfg = dict(config.get("configurable", {}) or {})
|
|
context = config.get("context", {}) or {}
|
|
if isinstance(context, dict):
|
|
cfg.update(context)
|
|
return cfg
|
|
|
|
|
|
def _resolve_model_name(requested_model_name: str | None = None, *, app_config: AppConfig | None = None) -> str:
|
|
"""Resolve a runtime model name safely, falling back to default if invalid. Returns None if no models are configured."""
|
|
app_config = app_config or get_app_config()
|
|
default_model_name = app_config.models[0].name if app_config.models else None
|
|
if default_model_name is None:
|
|
raise ValueError("No chat models are configured. Please configure at least one model in config.yaml.")
|
|
|
|
if requested_model_name and app_config.get_model_config(requested_model_name):
|
|
return requested_model_name
|
|
|
|
if requested_model_name and requested_model_name != default_model_name:
|
|
logger.warning(f"Model '{requested_model_name}' not found in config; fallback to default model '{default_model_name}'.")
|
|
return default_model_name
|
|
|
|
|
|
def _create_summarization_middleware(*, app_config: AppConfig | None = None) -> DeerFlowSummarizationMiddleware | None:
|
|
"""Create and configure the summarization middleware from config."""
|
|
resolved_app_config = app_config or get_app_config()
|
|
config = resolved_app_config.summarization
|
|
|
|
if not config.enabled:
|
|
return None
|
|
|
|
# Prepare trigger parameter
|
|
trigger = None
|
|
if config.trigger is not None:
|
|
if isinstance(config.trigger, list):
|
|
trigger = [t.to_tuple() for t in config.trigger]
|
|
else:
|
|
trigger = config.trigger.to_tuple()
|
|
|
|
# Prepare keep parameter
|
|
keep = config.keep.to_tuple()
|
|
|
|
# Prepare model parameter.
|
|
# Bind "middleware:summarize" tag so RunJournal identifies these LLM calls
|
|
# as middleware rather than lead_agent (SummarizationMiddleware is a
|
|
# LangChain built-in, so we tag the model at creation time).
|
|
# attach_tracing=False because the graph-level RunnableConfig (set in
|
|
# ``_make_lead_agent``) already carries tracing callbacks; binding them
|
|
# again at the model level would emit duplicate spans and break
|
|
# ``session_id`` / ``user_id`` propagation.
|
|
if config.model_name:
|
|
model = create_chat_model(name=config.model_name, thinking_enabled=False, app_config=resolved_app_config, attach_tracing=False)
|
|
else:
|
|
model = create_chat_model(thinking_enabled=False, app_config=resolved_app_config, attach_tracing=False)
|
|
model = model.with_config(tags=["middleware:summarize"])
|
|
|
|
# Prepare kwargs
|
|
kwargs = {
|
|
"model": model,
|
|
"trigger": trigger,
|
|
"keep": keep,
|
|
}
|
|
|
|
if config.trim_tokens_to_summarize is not None:
|
|
kwargs["trim_tokens_to_summarize"] = config.trim_tokens_to_summarize
|
|
|
|
if config.summary_prompt is not None:
|
|
kwargs["summary_prompt"] = config.summary_prompt
|
|
|
|
hooks: list[BeforeSummarizationHook] = []
|
|
if resolved_app_config.memory.enabled:
|
|
hooks.append(memory_flush_hook)
|
|
|
|
return DeerFlowSummarizationMiddleware(
|
|
**kwargs,
|
|
before_summarization=hooks,
|
|
)
|
|
|
|
|
|
def _create_todo_list_middleware(is_plan_mode: bool) -> TodoMiddleware | None:
|
|
"""Create and configure the TodoList middleware.
|
|
|
|
Args:
|
|
is_plan_mode: Whether to enable plan mode with TodoList middleware.
|
|
|
|
Returns:
|
|
TodoMiddleware instance if plan mode is enabled, None otherwise.
|
|
"""
|
|
if not is_plan_mode:
|
|
return None
|
|
|
|
# Custom prompts matching DeerFlow's style
|
|
system_prompt = """
|
|
<todo_list_system>
|
|
You have access to the `write_todos` tool to help you manage and track complex multi-step objectives.
|
|
|
|
**CRITICAL RULES:**
|
|
- Mark todos as completed IMMEDIATELY after finishing each step - do NOT batch completions
|
|
- Keep EXACTLY ONE task as `in_progress` at any time (unless tasks can run in parallel)
|
|
- Update the todo list in REAL-TIME as you work - this gives users visibility into your progress
|
|
- DO NOT use this tool for simple tasks (< 3 steps) - just complete them directly
|
|
|
|
**When to Use:**
|
|
This tool is designed for complex objectives that require systematic tracking:
|
|
- Complex multi-step tasks requiring 3+ distinct steps
|
|
- Non-trivial tasks needing careful planning and execution
|
|
- User explicitly requests a todo list
|
|
- User provides multiple tasks (numbered or comma-separated list)
|
|
- The plan may need revisions based on intermediate results
|
|
|
|
**When NOT to Use:**
|
|
- Single, straightforward tasks
|
|
- Trivial tasks (< 3 steps)
|
|
- Purely conversational or informational requests
|
|
- Simple tool calls where the approach is obvious
|
|
|
|
**Best Practices:**
|
|
- Break down complex tasks into smaller, actionable steps
|
|
- Use clear, descriptive task names
|
|
- Remove tasks that become irrelevant
|
|
- Add new tasks discovered during implementation
|
|
- Don't be afraid to revise the todo list as you learn more
|
|
|
|
**Task Management:**
|
|
Writing todos takes time and tokens - use it when helpful for managing complex problems, not for simple requests.
|
|
</todo_list_system>
|
|
"""
|
|
|
|
tool_description = """Use this tool to create and manage a structured task list for complex work sessions.
|
|
|
|
**IMPORTANT: Only use this tool for complex tasks (3+ steps). For simple requests, just do the work directly.**
|
|
|
|
## When to Use
|
|
|
|
Use this tool in these scenarios:
|
|
1. **Complex multi-step tasks**: When a task requires 3 or more distinct steps or actions
|
|
2. **Non-trivial tasks**: Tasks requiring careful planning or multiple operations
|
|
3. **User explicitly requests todo list**: When the user directly asks you to track tasks
|
|
4. **Multiple tasks**: When users provide a list of things to be done
|
|
5. **Dynamic planning**: When the plan may need updates based on intermediate results
|
|
|
|
## When NOT to Use
|
|
|
|
Skip this tool when:
|
|
1. The task is straightforward and takes less than 3 steps
|
|
2. The task is trivial and tracking provides no benefit
|
|
3. The task is purely conversational or informational
|
|
4. It's clear what needs to be done and you can just do it
|
|
|
|
## How to Use
|
|
|
|
1. **Starting a task**: Mark it as `in_progress` BEFORE beginning work
|
|
2. **Completing a task**: Mark it as `completed` IMMEDIATELY after finishing
|
|
3. **Updating the list**: Add new tasks, remove irrelevant ones, or update descriptions as needed
|
|
4. **Multiple updates**: You can make several updates at once (e.g., complete one task and start the next)
|
|
|
|
## Task States
|
|
|
|
- `pending`: Task not yet started
|
|
- `in_progress`: Currently working on (can have multiple if tasks run in parallel)
|
|
- `completed`: Task finished successfully
|
|
|
|
## Task Completion Requirements
|
|
|
|
**CRITICAL: Only mark a task as completed when you have FULLY accomplished it.**
|
|
|
|
Never mark a task as completed if:
|
|
- There are unresolved issues or errors
|
|
- Work is partial or incomplete
|
|
- You encountered blockers preventing completion
|
|
- You couldn't find necessary resources or dependencies
|
|
- Quality standards haven't been met
|
|
|
|
If blocked, keep the task as `in_progress` and create a new task describing what needs to be resolved.
|
|
|
|
## Best Practices
|
|
|
|
- Create specific, actionable items
|
|
- Break complex tasks into smaller, manageable steps
|
|
- Use clear, descriptive task names
|
|
- Update task status in real-time as you work
|
|
- Mark tasks complete IMMEDIATELY after finishing (don't batch completions)
|
|
- Remove tasks that are no longer relevant
|
|
- **IMPORTANT**: When you write the todo list, mark your first task(s) as `in_progress` immediately
|
|
- **IMPORTANT**: Unless all tasks are completed, always have at least one task `in_progress` to show progress
|
|
|
|
Being proactive with task management demonstrates thoroughness and ensures all requirements are completed successfully.
|
|
|
|
**Remember**: If you only need a few tool calls to complete a task and it's clear what to do, it's better to just do the task directly and NOT use this tool at all.
|
|
"""
|
|
|
|
return TodoMiddleware(system_prompt=system_prompt, tool_description=tool_description)
|
|
|
|
|
|
# ThreadDataMiddleware must be before SandboxMiddleware to ensure thread_id is available
|
|
# UploadsMiddleware should be after ThreadDataMiddleware to access thread_id
|
|
# DanglingToolCallMiddleware patches missing ToolMessages before model sees the history
|
|
# SummarizationMiddleware should be early to reduce context before other processing
|
|
# TodoListMiddleware should be before ClarificationMiddleware to allow todo management
|
|
# TitleMiddleware generates title after first exchange
|
|
# MemoryMiddleware queues conversation for memory update (after TitleMiddleware)
|
|
# ViewImageMiddleware should be before ClarificationMiddleware to inject image details before LLM
|
|
# ToolErrorHandlingMiddleware should be before ClarificationMiddleware to convert tool exceptions to ToolMessages
|
|
# ClarificationMiddleware should be last to intercept clarification requests after model calls
|
|
def build_middlewares(
|
|
config: RunnableConfig,
|
|
model_name: str | None,
|
|
agent_name: str | None = None,
|
|
custom_middlewares: list[AgentMiddleware] | None = None,
|
|
*,
|
|
available_skills: set[str] | None = None,
|
|
app_config: AppConfig | None = None,
|
|
deferred_setup=None,
|
|
user_id: str | None = None,
|
|
):
|
|
"""Build the lead-agent middleware chain based on runtime configuration.
|
|
|
|
Public entry point for the lead agent's full middleware composition. Used by
|
|
``make_lead_agent`` and by the embedded ``DeerFlowClient`` (a lead-agent variant
|
|
that needs the identical chain). Keep this name stable: it is imported across a
|
|
module boundary, so renames/signature changes ripple into ``client.py``.
|
|
|
|
Args:
|
|
config: Runtime configuration containing configurable options like is_plan_mode.
|
|
model_name: Resolved runtime model name; gates vision-only middleware.
|
|
agent_name: If provided, MemoryMiddleware will use per-agent memory storage.
|
|
custom_middlewares: Optional list of custom middlewares to inject into the chain.
|
|
app_config: Explicit AppConfig; falls back to ``get_app_config()`` when omitted.
|
|
deferred_setup: Optional deferred-MCP-tool setup that attaches
|
|
``DeferredToolFilterMiddleware`` when ``tool_search`` is enabled.
|
|
user_id: Effective user ID for user-scoped skill loading. Passed through
|
|
to ``SkillActivationMiddleware`` so it can resolve per-user custom skills.
|
|
|
|
Returns:
|
|
List of middleware instances.
|
|
"""
|
|
resolved_app_config = app_config or get_app_config()
|
|
middlewares = build_lead_runtime_middlewares(app_config=resolved_app_config, lazy_init=True)
|
|
|
|
# Always inject current date (and optionally memory) as <system-reminder> into the
|
|
# first HumanMessage to keep the system prompt fully static for prefix-cache reuse.
|
|
from deerflow.agents.middlewares.dynamic_context_middleware import DynamicContextMiddleware
|
|
|
|
middlewares.append(DynamicContextMiddleware(agent_name=agent_name, app_config=resolved_app_config))
|
|
|
|
# Deterministically load a full SKILL.md when the user starts the turn with
|
|
# /skill-name. This keeps the base system prompt metadata-only while giving
|
|
# explicit user activation priority over model-side relevance guessing.
|
|
from deerflow.agents.middlewares.skill_activation_middleware import SkillActivationMiddleware
|
|
|
|
middlewares.append(SkillActivationMiddleware(available_skills=available_skills, app_config=resolved_app_config, user_id=user_id))
|
|
|
|
# Capture completed task delegations and loaded skill files before
|
|
# summarization can compact them, then inject durable context channels
|
|
# (summary + ledger + skills) into model calls.
|
|
from deerflow.agents.middlewares.durable_context_middleware import DurableContextMiddleware
|
|
|
|
middlewares.append(
|
|
DurableContextMiddleware(
|
|
skills_container_path=resolved_app_config.skills.container_path,
|
|
skill_file_read_tool_names=resolved_app_config.summarization.skill_file_read_tool_names,
|
|
)
|
|
)
|
|
|
|
# Add summarization middleware if enabled
|
|
summarization_middleware = _create_summarization_middleware(app_config=resolved_app_config)
|
|
if summarization_middleware is not None:
|
|
middlewares.append(summarization_middleware)
|
|
|
|
# Add TodoList middleware if plan mode is enabled
|
|
cfg = _get_runtime_config(config)
|
|
is_plan_mode = cfg.get("is_plan_mode", False)
|
|
todo_list_middleware = _create_todo_list_middleware(is_plan_mode)
|
|
if todo_list_middleware is not None:
|
|
middlewares.append(todo_list_middleware)
|
|
|
|
# Add TokenUsageMiddleware when token_usage tracking is enabled
|
|
if resolved_app_config.token_usage.enabled:
|
|
middlewares.append(TokenUsageMiddleware())
|
|
|
|
# Add TitleMiddleware
|
|
middlewares.append(TitleMiddleware(app_config=resolved_app_config))
|
|
|
|
# Add MemoryMiddleware (after TitleMiddleware)
|
|
middlewares.append(MemoryMiddleware(agent_name=agent_name, memory_config=resolved_app_config.memory))
|
|
|
|
# Add ViewImageMiddleware only if the current model supports vision.
|
|
# Use the resolved runtime model_name from make_lead_agent to avoid stale config values.
|
|
model_config = resolved_app_config.get_model_config(model_name) if model_name else None
|
|
if model_config is not None and model_config.supports_vision:
|
|
middlewares.append(ViewImageMiddleware())
|
|
|
|
# Hide deferred tool schemas from model binding until tool_search promotes them.
|
|
# The deferred set + catalog hash come from the build-time setup (assembled
|
|
# after tool-policy filtering); promotion is read from graph state.
|
|
if deferred_setup is not None and deferred_setup.deferred_names:
|
|
from deerflow.agents.middlewares.deferred_tool_filter_middleware import DeferredToolFilterMiddleware
|
|
|
|
middlewares.append(DeferredToolFilterMiddleware(deferred_setup.deferred_names, deferred_setup.catalog_hash))
|
|
|
|
# Coalesce every SystemMessage into a single leading one before the request
|
|
# reaches the provider. Strict backends (vLLM, SGLang, Qwen, Anthropic)
|
|
# reject non-leading SystemMessages. See system_message_coalescing_middleware.py.
|
|
from deerflow.agents.middlewares.system_message_coalescing_middleware import SystemMessageCoalescingMiddleware
|
|
|
|
middlewares.append(SystemMessageCoalescingMiddleware())
|
|
|
|
# Add SubagentLimitMiddleware to truncate excess parallel task calls
|
|
subagent_enabled = cfg.get("subagent_enabled", False)
|
|
if subagent_enabled:
|
|
max_concurrent_subagents = cfg.get("max_concurrent_subagents", 3)
|
|
middlewares.append(SubagentLimitMiddleware(max_concurrent=max_concurrent_subagents))
|
|
|
|
# LoopDetectionMiddleware — detect and break repetitive tool call loops
|
|
loop_detection_config = resolved_app_config.loop_detection
|
|
if loop_detection_config.enabled:
|
|
middlewares.append(LoopDetectionMiddleware.from_config(loop_detection_config))
|
|
|
|
# TokenBudgetMiddleware - enforce per-run token limits
|
|
token_budget_config = resolved_app_config.token_budget
|
|
if token_budget_config.enabled:
|
|
from deerflow.agents.middlewares.token_budget_middleware import TokenBudgetMiddleware
|
|
|
|
middlewares.append(TokenBudgetMiddleware.from_config(token_budget_config))
|
|
|
|
# Inject custom middlewares before ClarificationMiddleware
|
|
if custom_middlewares:
|
|
middlewares.extend(custom_middlewares)
|
|
|
|
# SafetyFinishReasonMiddleware — suppress tool execution when the provider
|
|
# safety-terminated the response. Registered after custom middlewares so
|
|
# that LangChain's reverse-order after_model dispatch runs Safety first;
|
|
# cleared tool_calls then flow through Loop/Subagent accounting without
|
|
# firing extra alarms. See safety_finish_reason_middleware.py docstring.
|
|
safety_config = resolved_app_config.safety_finish_reason
|
|
if safety_config.enabled:
|
|
middlewares.append(SafetyFinishReasonMiddleware.from_config(safety_config))
|
|
|
|
# ClarificationMiddleware should always be last
|
|
middlewares.append(ClarificationMiddleware())
|
|
return middlewares
|
|
|
|
|
|
def _available_skill_names(agent_config, is_bootstrap: bool) -> set[str] | None:
|
|
if is_bootstrap:
|
|
return set(_BOOTSTRAP_SKILL_NAMES)
|
|
if agent_config and agent_config.skills is not None:
|
|
return set(agent_config.skills)
|
|
return None
|
|
|
|
|
|
def _load_enabled_skills_for_tool_policy(available_skills: set[str] | None, *, app_config: AppConfig, user_id: str | None = None) -> list[Skill]:
|
|
try:
|
|
from deerflow.agents.lead_agent.prompt import get_enabled_skills_for_config
|
|
|
|
skills = get_enabled_skills_for_config(app_config, user_id=user_id)
|
|
except Exception:
|
|
logger.exception("Failed to load skills for allowed-tools policy")
|
|
raise
|
|
|
|
if available_skills is None:
|
|
return skills
|
|
return [skill for skill in skills if skill.name in available_skills]
|
|
|
|
|
|
def make_lead_agent(config: RunnableConfig):
|
|
"""LangGraph graph factory; keep the signature compatible with LangGraph Server."""
|
|
runtime_config = _get_runtime_config(config)
|
|
runtime_app_config = runtime_config.get("app_config")
|
|
return _make_lead_agent(config, app_config=runtime_app_config or get_app_config())
|
|
|
|
|
|
def _make_lead_agent(config: RunnableConfig, *, app_config: AppConfig):
|
|
# Lazy import to avoid circular dependency
|
|
from deerflow.tools import get_available_tools
|
|
from deerflow.tools.builtins import setup_agent, update_agent
|
|
from deerflow.tools.builtins.tool_search import assemble_deferred_tools
|
|
|
|
cfg = _get_runtime_config(config)
|
|
resolved_app_config = app_config
|
|
|
|
# Extract user_id for user-scoped skill loading.
|
|
# LangGraph gateway injects user_id into config["configurable"];
|
|
# fall back to the runtime contextvar when not present.
|
|
from deerflow.runtime.user_context import get_effective_user_id
|
|
|
|
runtime_user_id = cfg.get("user_id")
|
|
resolved_user_id = str(runtime_user_id) if runtime_user_id else get_effective_user_id()
|
|
|
|
thinking_enabled = cfg.get("thinking_enabled", True)
|
|
reasoning_effort = cfg.get("reasoning_effort", None)
|
|
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)
|
|
non_interactive = bool(cfg.get("non_interactive", False))
|
|
agent_name = validate_agent_name(cfg.get("agent_name"))
|
|
|
|
agent_config = load_agent_config(agent_name) if not is_bootstrap else None
|
|
available_skills = _available_skill_names(agent_config, is_bootstrap)
|
|
# Custom agent model from agent config (if any), or None to let _resolve_model_name pick the default
|
|
agent_model_name = agent_config.model if agent_config and agent_config.model else None
|
|
|
|
# Final model name resolution: request → agent config → global default, with fallback for unknown names
|
|
model_name = _resolve_model_name(requested_model_name or agent_model_name, app_config=resolved_app_config)
|
|
|
|
model_config = resolved_app_config.get_model_config(model_name)
|
|
|
|
if model_config is None:
|
|
raise ValueError("No chat model could be resolved. Please configure at least one model in config.yaml or provide a valid 'model_name'/'model' in the request.")
|
|
if thinking_enabled and not model_config.supports_thinking:
|
|
logger.warning(f"Thinking mode is enabled but model '{model_name}' does not support it; fallback to non-thinking mode.")
|
|
thinking_enabled = False
|
|
|
|
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",
|
|
agent_name or "default",
|
|
thinking_enabled,
|
|
reasoning_effort,
|
|
model_name,
|
|
is_plan_mode,
|
|
subagent_enabled,
|
|
max_concurrent_subagents,
|
|
)
|
|
|
|
# Inject run metadata for LangSmith trace tagging
|
|
if "metadata" not in config:
|
|
config["metadata"] = {}
|
|
|
|
config["metadata"].update(
|
|
{
|
|
"agent_name": agent_name or "default",
|
|
"model_name": model_name or "default",
|
|
"thinking_enabled": thinking_enabled,
|
|
"reasoning_effort": reasoning_effort,
|
|
"is_plan_mode": is_plan_mode,
|
|
"subagent_enabled": subagent_enabled,
|
|
"tool_groups": agent_config.tool_groups if agent_config else None,
|
|
"available_skills": sorted(available_skills) if available_skills is not None else None,
|
|
}
|
|
)
|
|
|
|
# Inject tracing callbacks at the graph invocation root so a single LangGraph
|
|
# run produces one trace with all node / LLM / tool calls as child spans,
|
|
# AND so the Langfuse handler sees ``on_chain_start(parent_run_id=None)`` and
|
|
# actually propagates ``langfuse_session_id`` / ``langfuse_user_id`` from
|
|
# ``config["metadata"]`` onto the trace. Without root-level attachment the
|
|
# model is a nested observation and the handler strips ``langfuse_*`` keys.
|
|
tracing_callbacks = build_tracing_callbacks()
|
|
if tracing_callbacks:
|
|
existing = config.get("callbacks") or []
|
|
if not isinstance(existing, list):
|
|
existing = list(existing)
|
|
config["callbacks"] = [*existing, *tracing_callbacks]
|
|
|
|
skills_for_tool_policy = _load_enabled_skills_for_tool_policy(available_skills, app_config=resolved_app_config, user_id=resolved_user_id)
|
|
|
|
if is_bootstrap:
|
|
# Special bootstrap agent with minimal prompt for initial custom agent creation flow
|
|
# Keep the bootstrap skill set intentionally narrow so agent creation
|
|
# remains deterministic before the custom agent's own config exists.
|
|
raw_tools = get_available_tools(model_name=model_name, subagent_enabled=subagent_enabled, app_config=resolved_app_config) + [setup_agent]
|
|
filtered = filter_tools_by_skill_allowed_tools(raw_tools, skills_for_tool_policy, always_allowed_tool_names=SKILL_LOADING_TOOL_NAMES)
|
|
if non_interactive:
|
|
filtered = [tool for tool in filtered if tool.name not in _NON_INTERACTIVE_DISABLED_TOOL_NAMES]
|
|
final_tools, setup = assemble_deferred_tools(filtered, enabled=resolved_app_config.tool_search.enabled)
|
|
return create_agent(
|
|
model=create_chat_model(name=model_name, thinking_enabled=thinking_enabled, app_config=resolved_app_config, attach_tracing=False),
|
|
tools=final_tools,
|
|
middleware=build_middlewares(
|
|
config,
|
|
model_name=model_name,
|
|
available_skills=set(_BOOTSTRAP_SKILL_NAMES),
|
|
app_config=resolved_app_config,
|
|
deferred_setup=setup,
|
|
user_id=resolved_user_id,
|
|
),
|
|
system_prompt=apply_prompt_template(
|
|
subagent_enabled=subagent_enabled,
|
|
max_concurrent_subagents=max_concurrent_subagents,
|
|
available_skills=set(_BOOTSTRAP_SKILL_NAMES),
|
|
app_config=resolved_app_config,
|
|
deferred_names=setup.deferred_names,
|
|
user_id=resolved_user_id,
|
|
),
|
|
state_schema=ThreadState,
|
|
)
|
|
|
|
# Custom agents can update their own SOUL.md / config via update_agent.
|
|
# The default agent (no agent_name) does not see this tool.
|
|
#
|
|
# Withhold ``update_agent`` from runs triggered by webhook channels
|
|
# (currently only ``github``). Webhook prompts come from arbitrary
|
|
# external commenters — anyone who can post on a configured repo and
|
|
# types ``@<bot>`` clears the trigger gate. Exposing the tool there
|
|
# gives that commenter a path to mutate the agent's ``tool_groups``
|
|
# / ``SOUL.md`` / ``model``, and the change persists for every
|
|
# subsequent run. Self-mutation belongs in operator-trusted surfaces
|
|
# (the chat UI, the HTTP API), not in webhook fan-out.
|
|
#
|
|
# The channel name is plumbed into ``run_context`` by
|
|
# ``ChannelManager._resolve_run_params``; bootstrap and direct invocations
|
|
# leave it unset, so ``update_agent`` remains available there.
|
|
channel_name = cfg.get("channel_name")
|
|
is_webhook_channel = channel_name in _WEBHOOK_CHANNELS
|
|
extra_tools = [update_agent] if agent_name and not is_webhook_channel else []
|
|
# Default lead agent (unchanged behavior)
|
|
raw_tools = get_available_tools(model_name=model_name, groups=agent_config.tool_groups if agent_config else None, subagent_enabled=subagent_enabled, app_config=resolved_app_config)
|
|
filtered = filter_tools_by_skill_allowed_tools(raw_tools + extra_tools, skills_for_tool_policy, always_allowed_tool_names=SKILL_LOADING_TOOL_NAMES)
|
|
if non_interactive:
|
|
filtered = [tool for tool in filtered if tool.name not in _NON_INTERACTIVE_DISABLED_TOOL_NAMES]
|
|
final_tools, setup = assemble_deferred_tools(filtered, enabled=resolved_app_config.tool_search.enabled)
|
|
return create_agent(
|
|
model=create_chat_model(name=model_name, thinking_enabled=thinking_enabled, reasoning_effort=reasoning_effort, app_config=resolved_app_config, attach_tracing=False),
|
|
tools=final_tools,
|
|
middleware=build_middlewares(
|
|
config,
|
|
model_name=model_name,
|
|
agent_name=agent_name,
|
|
available_skills=available_skills,
|
|
app_config=resolved_app_config,
|
|
deferred_setup=setup,
|
|
user_id=resolved_user_id,
|
|
),
|
|
system_prompt=apply_prompt_template(
|
|
subagent_enabled=subagent_enabled,
|
|
max_concurrent_subagents=max_concurrent_subagents,
|
|
agent_name=agent_name,
|
|
available_skills=available_skills,
|
|
app_config=resolved_app_config,
|
|
deferred_names=setup.deferred_names,
|
|
user_id=resolved_user_id,
|
|
),
|
|
state_schema=ThreadState,
|
|
)
|