mirror of
https://github.com/bytedance/deer-flow.git
synced 2026-08-13 16:28:38 +00:00
* feat(agents): database-backed storage for custom agent definitions Add an agent_storage.backend switch (default file, behaviour-unchanged) with a db backend that stores each custom agent as a row in the shared SQL persistence layer, so a multi-instance deployment sees the same agents on every node (#4331, #4357). Introduces an AgentStore interface routing all read/write surfaces, an agents table + migration 0006, startup validation, and a file->db importer. Follows the thread_meta store / run_events backend-switch / 0003_scheduled_tasks migration patterns; no new dependency. * fix(agents): make db storage path production-ready (review round 1) Addresses review feedback on the db/sync agent-storage path: - sql.py: mirror the async engine's per-connection SQLite PRAGMAs on the sync engine (busy_timeout=30000, synchronous=NORMAL, foreign_keys=ON, WAL) so both engines behave identically against the shared DB; guard the engine cache with a lock (double-checked) so concurrent first-touch cannot build duplicate engines or register the connect listener twice. - routers/agents.py + routers/assistants_compat.py: offload the sync-store reads that ran on the event loop (list/get/check, update's pre-read + legacy guard + refresh, and assistants_compat's four list routes) via asyncio.to_thread — on db+postgres each was a network round trip stalling the loop. Writes were already offloaded. - file.py: translate the create() mkdir(exist_ok=False) race FileExistsError into AgentExistsError (router 409, matching SqlAgentStore's IntegrityError path); correct the _write docstring — per-file atomic replace, two commits sequential not transactional. Tests: sync-engine PRAGMA + engine-cache reuse assertions; file create-race -> AgentExistsError; strict Blockbuster anchor over the read endpoints so a regression back onto the loop fails CI. * fix(agents): address round-2 review on the db store path - update_agent tool: align the docstring/inline comment with FileAgentStore._write. Cross-field write atomicity is db-only; the file backend commits config then soul via two sequential os.replace (a crash between them can leave a fresh config.yaml beside a stale SOUL.md). The dropped partial-write *reporting* is an intentional tradeoff — the stage-then-replace safety is preserved (test_update_agent_soul_failure_does_not_replace_config still holds). - SqlAgentStore.update(): true upsert. Catch IntegrityError on the insert-on-missing branch, re-fetch and apply, so two concurrent first-time writes (e.g. two setup_agent handshakes) converge instead of surfacing a raw UNIQUE(user_id, name) violation as a 500. Symmetric with create(). - get_agent_store(): document the graph-subprocess config-resolution invariant (the except->file fallback is a genuine no-config path, not a mask for a misconfigured graph process) and pin it with two tests driving the real get_app_config() file resolution: db resolves from an on-disk config.yaml, file fallback when config is unresolvable. * test(agents): cover SqlAgentStore.update() write-race upsert recovery Mandatory-TDD test for the round-2 fix in 0680340a: two concurrent first-time update()s where the loser's insert hits UNIQUE(user_id, name). Deterministically forces the IntegrityError recovery path by making the first _row probe miss the committed winner, and asserts last-writer-wins instead of a surfaced 500.
85 lines
3.5 KiB
Python
85 lines
3.5 KiB
Python
import logging
|
|
|
|
from langchain_core.messages import ToolMessage
|
|
from langchain_core.tools import tool
|
|
from langgraph.types import Command
|
|
|
|
from deerflow.config.agents_config import SOUL_FILENAME, validate_agent_name
|
|
from deerflow.config.paths import get_paths
|
|
from deerflow.persistence.agents import get_agent_store
|
|
from deerflow.runtime.user_context import resolve_runtime_user_id
|
|
from deerflow.tools.types import Runtime
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
@tool(parse_docstring=True)
|
|
def setup_agent(
|
|
soul: str,
|
|
description: str,
|
|
runtime: Runtime,
|
|
skills: list[str] | None = None,
|
|
) -> Command:
|
|
"""Setup the custom DeerFlow agent.
|
|
|
|
Args:
|
|
soul: Full SOUL.md content defining the agent's personality and behavior.
|
|
description: One-line description of what the agent does.
|
|
skills: Optional list of skill names this agent should use. None means use all enabled skills, empty list means no skills.
|
|
"""
|
|
|
|
# Reject empty / whitespace-only soul before touching the filesystem.
|
|
# Without this guard the tool would happily persist an empty SOUL.md and
|
|
# still report success, which caused the frontend to enter the "agent
|
|
# created" state for an unusable agent (issue #3549). Failing loud lets
|
|
# the model retry instead of silently producing a broken artifact and,
|
|
# together with the upstream agent_name fix, prevents the global default
|
|
# SOUL.md from being overwritten with empty content.
|
|
if not soul or not soul.strip():
|
|
return Command(
|
|
update={
|
|
"messages": [
|
|
ToolMessage(
|
|
content="Error: soul content is empty; refusing to create agent with an empty SOUL.md",
|
|
tool_call_id=runtime.tool_call_id,
|
|
)
|
|
]
|
|
}
|
|
)
|
|
|
|
agent_name: str | None = runtime.context.get("agent_name") if runtime.context else None
|
|
|
|
try:
|
|
agent_name = validate_agent_name(agent_name)
|
|
if agent_name:
|
|
# Custom agents are persisted under the current user's bucket (via
|
|
# the configured store — file or db) so different users, and
|
|
# different nodes, resolve the same agent. setup is idempotent, so
|
|
# this is an upsert.
|
|
user_id = resolve_runtime_user_id(runtime)
|
|
config_data: dict = {"name": agent_name}
|
|
if description:
|
|
config_data["description"] = description
|
|
if skills is not None:
|
|
config_data["skills"] = skills
|
|
get_agent_store().update(agent_name, config_data, soul, user_id=user_id)
|
|
else:
|
|
# Default agent (no agent_name): SOUL.md lives at the global base
|
|
# dir. It is not a custom-agent record, so it stays file-based
|
|
# regardless of the agent-storage backend.
|
|
paths = get_paths()
|
|
paths.base_dir.mkdir(parents=True, exist_ok=True)
|
|
(paths.base_dir / SOUL_FILENAME).write_text(soul, encoding="utf-8")
|
|
|
|
logger.info(f"[agent_creator] Created agent '{agent_name}'")
|
|
return Command(
|
|
update={
|
|
"created_agent_name": agent_name,
|
|
"messages": [ToolMessage(content=f"Agent '{agent_name}' created successfully!", tool_call_id=runtime.tool_call_id)],
|
|
}
|
|
)
|
|
|
|
except Exception as e:
|
|
logger.error(f"[agent_creator] Failed to create agent '{agent_name}': {e}", exc_info=True)
|
|
return Command(update={"messages": [ToolMessage(content=f"Error: {e}", tool_call_id=runtime.tool_call_id)]})
|