mirror of
https://github.com/bytedance/deer-flow.git
synced 2026-05-19 15:13:40 +00:00
* feat(agent): add update_agent tool for in-chat custom-agent self-updates (#2616) Custom agents had no built-in way to persist updates to their own SOUL.md / config.yaml from a normal chat — `setup_agent` was only bound during the bootstrap flow, so when the user asked the agent to refine its description or personality, the agent would shell out via bash/write_file and the edits landed in a temporary sandbox/tool workspace instead of `{base_dir}/agents/{agent_name}/`. Changes: - New `update_agent` builtin tool with partial-update semantics (only the fields you pass are written) and atomic temp-file + os.replace writes so a failed update never corrupts existing SOUL.md / config.yaml. - Lead agent now binds `update_agent` in the non-bootstrap path whenever `agent_name` is set in the runtime context. Default agent (no agent_name) and bootstrap flow are unchanged. - New `<self_update>` system-prompt section is injected for custom agents, instructing them to use `update_agent` — and explicitly NOT bash / write_file — to persist self-updates. - Tests: 11 new cases in `tests/test_update_agent_tool.py` covering validation (missing/invalid agent_name, unknown agent, no fields), partial updates (soul-only, description-only, skills=[] vs omitted), no-op detection, atomic-write safety, and AgentConfig round-tripping; plus 2 new cases in `tests/test_lead_agent_prompt.py` covering the self-update prompt section. - Docs: updated backend/CLAUDE.md builtin tools list and tools.mdx (en/zh) with the new tool description. * feat(agent): isolate custom agents per user Store custom agent definitions under the effective user, keep legacy agents readable until migration, and cover API/tool/migration behavior with tests. Co-authored-by: Cursor <cursoragent@cursor.com> * feat: consistent write/delete targets & add --user-id to migration --------- Co-authored-by: Cursor <cursoragent@cursor.com>
201 lines
6.8 KiB
Python
201 lines
6.8 KiB
Python
"""Configuration and loaders for custom agents.
|
|
|
|
Custom agents are stored per-user under ``{base_dir}/users/{user_id}/agents/{name}/``.
|
|
A legacy shared layout at ``{base_dir}/agents/{name}/`` is still readable so that
|
|
installations that pre-date user isolation continue to work until they run the
|
|
``scripts/migrate_user_isolation.py`` migration. New writes always target the
|
|
per-user layout.
|
|
"""
|
|
|
|
import logging
|
|
import re
|
|
from pathlib import Path
|
|
from typing import Any
|
|
|
|
import yaml
|
|
from pydantic import BaseModel
|
|
|
|
from deerflow.config.paths import get_paths
|
|
from deerflow.runtime.user_context import get_effective_user_id
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
SOUL_FILENAME = "SOUL.md"
|
|
AGENT_NAME_PATTERN = re.compile(r"^[A-Za-z0-9-]+$")
|
|
|
|
|
|
def validate_agent_name(name: str | None) -> str | None:
|
|
"""Validate a custom agent name before using it in filesystem paths."""
|
|
if name is None:
|
|
return None
|
|
if not isinstance(name, str):
|
|
raise ValueError("Invalid agent name. Expected a string or None.")
|
|
if not AGENT_NAME_PATTERN.fullmatch(name):
|
|
raise ValueError(f"Invalid agent name '{name}'. Must match pattern: {AGENT_NAME_PATTERN.pattern}")
|
|
return name
|
|
|
|
|
|
class AgentConfig(BaseModel):
|
|
"""Configuration for a custom agent."""
|
|
|
|
name: str
|
|
description: str = ""
|
|
model: str | None = None
|
|
tool_groups: list[str] | None = None
|
|
# skills controls which skills are loaded into the agent's prompt:
|
|
# - None (or omitted): load all enabled skills (default fallback behavior)
|
|
# - [] (explicit empty list): disable all skills
|
|
# - ["skill1", "skill2"]: load only the specified skills
|
|
skills: list[str] | None = None
|
|
|
|
|
|
def resolve_agent_dir(name: str, *, user_id: str | None = None) -> Path:
|
|
"""Return the on-disk directory for an agent, preferring the per-user layout.
|
|
|
|
Resolution order:
|
|
1. ``{base_dir}/users/{user_id}/agents/{name}/`` (per-user, current layout).
|
|
2. ``{base_dir}/agents/{name}/`` (legacy shared layout — read-only fallback).
|
|
|
|
If neither exists, the per-user path is returned so callers that intend to
|
|
create the agent write into the new layout.
|
|
|
|
Args:
|
|
name: Validated agent name.
|
|
user_id: Owner of the agent. Defaults to the effective user from the
|
|
request context (or ``"default"`` in no-auth mode).
|
|
"""
|
|
paths = get_paths()
|
|
effective_user = user_id or get_effective_user_id()
|
|
user_path = paths.user_agent_dir(effective_user, name)
|
|
if user_path.exists():
|
|
return user_path
|
|
|
|
legacy_path = paths.agent_dir(name)
|
|
if legacy_path.exists():
|
|
return legacy_path
|
|
|
|
return user_path
|
|
|
|
|
|
def load_agent_config(name: str | None, *, user_id: str | None = None) -> AgentConfig | None:
|
|
"""Load the custom or default agent's config from its directory.
|
|
|
|
Reads from the per-user layout first; falls back to the legacy shared layout
|
|
for installations that have not yet been migrated.
|
|
|
|
Args:
|
|
name: The agent name.
|
|
user_id: Owner of the agent. Defaults to the effective user from the
|
|
current request context.
|
|
|
|
Returns:
|
|
AgentConfig instance, or ``None`` if ``name`` is ``None``.
|
|
|
|
Raises:
|
|
FileNotFoundError: If the agent directory or config.yaml does not exist.
|
|
ValueError: If config.yaml cannot be parsed.
|
|
"""
|
|
|
|
if name is None:
|
|
return None
|
|
|
|
name = validate_agent_name(name)
|
|
agent_dir = resolve_agent_dir(name, user_id=user_id)
|
|
config_file = agent_dir / "config.yaml"
|
|
|
|
if not agent_dir.exists():
|
|
raise FileNotFoundError(f"Agent directory not found: {agent_dir}")
|
|
|
|
if not config_file.exists():
|
|
raise FileNotFoundError(f"Agent config not found: {config_file}")
|
|
|
|
try:
|
|
with open(config_file, encoding="utf-8") as f:
|
|
data: dict[str, Any] = yaml.safe_load(f) or {}
|
|
except yaml.YAMLError as e:
|
|
raise ValueError(f"Failed to parse agent config {config_file}: {e}") from e
|
|
|
|
# Ensure name is set from directory name if not in file
|
|
if "name" not in data:
|
|
data["name"] = name
|
|
|
|
# Strip unknown fields before passing to Pydantic (e.g. legacy prompt_file)
|
|
known_fields = set(AgentConfig.model_fields.keys())
|
|
data = {k: v for k, v in data.items() if k in known_fields}
|
|
|
|
return AgentConfig(**data)
|
|
|
|
|
|
def load_agent_soul(agent_name: str | None, *, user_id: str | None = None) -> str | None:
|
|
"""Read the SOUL.md file for a custom agent, if it exists.
|
|
|
|
SOUL.md defines the agent's personality, values, and behavioral guardrails.
|
|
It is injected into the lead agent's system prompt as additional context.
|
|
|
|
Args:
|
|
agent_name: The name of the agent or None for the default agent.
|
|
user_id: Owner of the agent. Defaults to the effective user from the
|
|
current request context.
|
|
|
|
Returns:
|
|
The SOUL.md content as a string, or None if the file does not exist.
|
|
"""
|
|
if agent_name:
|
|
agent_dir = resolve_agent_dir(agent_name, user_id=user_id)
|
|
else:
|
|
agent_dir = get_paths().base_dir
|
|
soul_path = agent_dir / SOUL_FILENAME
|
|
if not soul_path.exists():
|
|
return None
|
|
content = soul_path.read_text(encoding="utf-8").strip()
|
|
return content or None
|
|
|
|
|
|
def list_custom_agents(*, user_id: str | None = None) -> list[AgentConfig]:
|
|
"""Scan the agents directory and return all valid custom agents.
|
|
|
|
Returns the union of agents in the per-user layout and the legacy shared
|
|
layout, so that pre-migration installations remain visible until they are
|
|
migrated. Per-user entries shadow legacy entries with the same name.
|
|
|
|
Args:
|
|
user_id: Owner whose agents to list. Defaults to the effective user
|
|
from the current request context.
|
|
|
|
Returns:
|
|
List of AgentConfig for each valid agent directory found.
|
|
"""
|
|
paths = get_paths()
|
|
effective_user = user_id or get_effective_user_id()
|
|
|
|
seen: set[str] = set()
|
|
agents: list[AgentConfig] = []
|
|
|
|
user_root = paths.user_agents_dir(effective_user)
|
|
legacy_root = paths.agents_dir
|
|
|
|
for root in (user_root, legacy_root):
|
|
if not root.exists():
|
|
continue
|
|
for entry in sorted(root.iterdir()):
|
|
if not entry.is_dir():
|
|
continue
|
|
if entry.name in seen:
|
|
continue
|
|
config_file = entry / "config.yaml"
|
|
if not config_file.exists():
|
|
logger.debug(f"Skipping {entry.name}: no config.yaml")
|
|
continue
|
|
|
|
try:
|
|
agent_cfg = load_agent_config(entry.name, user_id=effective_user)
|
|
if agent_cfg is None:
|
|
continue
|
|
agents.append(agent_cfg)
|
|
seen.add(entry.name)
|
|
except Exception as e:
|
|
logger.warning(f"Skipping agent '{entry.name}': {e}")
|
|
|
|
agents.sort(key=lambda a: a.name)
|
|
return agents
|