feat: 完善策略优选项目工作流

This commit is contained in:
Administrator committed 2026-09-09 20:49:59 +08:00
1 parent a26fd32eba
commit 5d6627af50
45 files changed
+7235 -768

No files matched your search

@@ -91,6 +91,7 @@ from app.gateway.routers import (
skill_addresses,
skill_knowledge,
skills,
strategy_agents,
suggestions,
system_settings,
tags,
@@ -1226,6 +1227,15 @@ async def lifespan(app: FastAPI) -> AsyncGenerator[None, None]:
await ensure_builtin_agent_purpose_tags(agent_store, tag_store)
except Exception:
logger.exception("Built-in agent purpose-tag seeding failed at startup (non-fatal)")
# Synchronize every worker's strategy registry only after the
# folder agents have been imported. In memory mode this also
# restores dynamic/default angle lifecycle from sidecars.
try:
await strategy_agents.ensure_strategy_agent_registry(app)
logger.info("Strategy agent registry synchronized for worker")
except Exception:
logger.exception("Strategy agent role seeding failed at startup (non-fatal)")
except Exception:
logger.exception("Legacy agent sync failed at startup (non-fatal)")
@@ -1620,6 +1630,7 @@ This gateway provides custom endpoints for models, MCP configuration, skills, an
# Agents API is mounted at /api/agents
app.include_router(agents.router)
app.include_router(strategy_agents.router)
# Recommended Questions API is mounted at /api/recommended-questions
app.include_router(recommended_questions.router)
@@ -32,6 +32,7 @@ if TYPE_CHECKING:
from deerflow.persistence.notifications import NotificationStore
from deerflow.persistence.scheduled_tasks import ScheduledTaskStore
from deerflow.persistence.skills import SkillStore
from deerflow.persistence.strategy_agent_roles import StrategyAgentRoleStore
from deerflow.persistence.tags import TagStore
from deerflow.persistence.thread_meta.base import ThreadMetaStore
@@ -132,6 +133,12 @@ async def langgraph_runtime(app: FastAPI) -> AsyncGenerator[None, None]:
app.state.agent_store = make_agent_store(sf)
from deerflow.persistence.strategy_agent_roles import (
make_strategy_agent_role_store,
)
app.state.strategy_agent_role_store = make_strategy_agent_role_store(sf)
from deerflow.persistence.skills import make_skill_store
app.state.skill_store = make_skill_store(sf)
@@ -824,6 +831,14 @@ def get_agent_store(request: Request) -> AgentStore:
return val
def get_strategy_agent_role_store(request: Request) -> StrategyAgentRoleStore:
"""Return the global strategy-agent role registry."""
val = getattr(request.app.state, "strategy_agent_role_store", None)
if val is None:
raise HTTPException(status_code=503, detail="Strategy agent role store not available")
return val
def get_skill_store(request: Request) -> SkillStore:
"""Return the custom-skill store."""
val = getattr(request.app.state, "skill_store", None)
@@ -3,15 +3,17 @@
The strategy-selection workbench treats intent understanding, each analysis
angle, judging, and final synthesis as normal configurable DeerFlow agents.
Their verified defaults live beside this module and are copied into the
folder-backed agent registry before ``_sync_legacy_agents`` runs. Existing
directories are never overwritten, so administrators can safely edit SOUL,
skills, tool groups, and models from agent management.
folder-backed agent registry before ``_sync_legacy_agents`` runs. Existing
directories are preserved, except for an exact known legacy default whose
content hash proves that an administrator has not customized it.
"""
from __future__ import annotations
import logging
import os
from collections.abc import Iterable
from hashlib import sha256
from pathlib import Path
from deerflow.config.paths import get_paths
@@ -19,6 +21,8 @@ from deerflow.config.paths import get_paths
logger = logging.getLogger(__name__)
STRATEGY_INTENT_AGENT_ID = "strategy-selection-intent"
STRATEGY_AGENT_ID_PREFIX = "strategy-selection-"
STRATEGY_ANGLE_AGENT_PREFIX = "strategy-selection-angle-"
STRATEGY_GOVERNANCE_AGENT_ID = "strategy-selection-angle-governance"
STRATEGY_DIPLOMACY_AGENT_ID = "strategy-selection-angle-diplomacy"
STRATEGY_ECONOMY_AGENT_ID = "strategy-selection-angle-economy"
@@ -37,6 +41,12 @@ STRATEGY_ANGLE_AGENT_IDS: tuple[str, ...] = (
STRATEGY_LAW_POLICY_AGENT_ID,
)
STRATEGY_FIXED_AGENT_IDS: tuple[str, ...] = (
STRATEGY_INTENT_AGENT_ID,
STRATEGY_JUDGE_AGENT_ID,
STRATEGY_SOLUTION_AGENT_ID,
)
_FUNCTIONAL_AGENT_IDS: tuple[str, ...] = (
STRATEGY_INTENT_AGENT_ID,
*STRATEGY_ANGLE_AGENT_IDS,
@@ -46,12 +56,48 @@ _FUNCTIONAL_AGENT_IDS: tuple[str, ...] = (
_ASSETS_DIR = Path(__file__).parent / "_strategy_selection_seed_assets"
_SEED_FILES: tuple[str, ...] = ("config.yaml", "SOUL.md")
# The first shipped intent SOUL embedded the six built-in angle ids. Upgrade
# only that byte-for-byte semantic default so existing installations adopt the
# administrator-driven angle catalog without overwriting customized SOUL.
_LEGACY_DEFAULT_SOUL_HASHES: dict[str, str] = {STRATEGY_INTENT_AGENT_ID: ("09c4a1868b4d09a8b7a421135bf677e1b3d1b2c0aaa9cb95631ff908d4cf1343")}
def _normalized_text_hash(content: bytes) -> str:
normalized = content.decode("utf-8").replace("\r\n", "\n").encode("utf-8")
return sha256(normalized).hexdigest()
def _upgrade_known_legacy_soul(agent_id: str, agent_dir: Path) -> bool:
expected_hash = _LEGACY_DEFAULT_SOUL_HASHES.get(agent_id)
target = agent_dir / "SOUL.md"
source = _ASSETS_DIR / agent_id / "SOUL.md"
if not expected_hash or not target.is_file() or not source.is_file():
return False
temporary: Path | None = None
try:
if _normalized_text_hash(target.read_bytes()) != expected_hash:
return False
replacement = source.read_bytes()
if _normalized_text_hash(replacement) == expected_hash:
return False
temporary = target.with_name(f".{target.name}.{os.getpid()}.seed-upgrade.tmp")
temporary.write_bytes(replacement)
os.replace(temporary, target)
logger.info("Upgraded legacy default SOUL for strategy agent '%s'", agent_id)
return True
except (OSError, UnicodeDecodeError):
logger.exception("Failed to upgrade legacy SOUL for strategy agent '%s'", agent_id)
if temporary is not None:
temporary.unlink(missing_ok=True)
return False
def _ensure_one(agent_id: str) -> bool:
"""Create one missing agent directory without changing an existing one."""
agent_dir = get_paths().agent_dir(agent_id)
if agent_dir.exists():
_upgrade_known_legacy_soul(agent_id, agent_dir)
return False
source_dir = _ASSETS_DIR / agent_id
@@ -92,10 +138,62 @@ def required_agent_ids() -> Iterable[str]:
return tuple(_FUNCTIONAL_AGENT_IDS)
def default_strategy_agent_roles() -> list[dict[str, object]]:
"""Return immutable-role metadata for the shared strategy workbench.
The registry is deliberately separate from the folder-backed agent record:
it owns role/category and lifecycle state, while model, SOUL and skills keep
using the common agent configuration path. ``ensure_defaults`` only
inserts missing rows, so an administrator disabling an angle is respected.
"""
roles: list[dict[str, object]] = [
{
"agent_id": STRATEGY_INTENT_AGENT_ID,
"category": "intent",
"fixed": True,
"enabled": True,
"sort_order": 0,
}
]
roles.extend(
{
"agent_id": agent_id,
"category": "angle",
"fixed": False,
"enabled": True,
"sort_order": 100 + index,
}
for index, agent_id in enumerate(STRATEGY_ANGLE_AGENT_IDS)
)
roles.extend(
(
{
"agent_id": STRATEGY_JUDGE_AGENT_ID,
"category": "judge",
"fixed": True,
"enabled": True,
"sort_order": 200,
},
{
"agent_id": STRATEGY_SOLUTION_AGENT_ID,
"category": "solution",
"fixed": True,
"enabled": True,
"sort_order": 300,
},
)
)
return roles
__all__ = [
"STRATEGY_AGENT_ID_PREFIX",
"STRATEGY_ANGLE_AGENT_IDS",
"STRATEGY_ANGLE_AGENT_PREFIX",
"STRATEGY_DIPLOMACY_AGENT_ID",
"STRATEGY_ECONOMY_AGENT_ID",
"STRATEGY_FIXED_AGENT_IDS",
"STRATEGY_GOVERNANCE_AGENT_ID",
"STRATEGY_INTENT_AGENT_ID",
"STRATEGY_JUDGE_AGENT_ID",
@@ -103,6 +201,7 @@ __all__ = [
"STRATEGY_PUBLIC_OPINION_AGENT_ID",
"STRATEGY_SECURITY_AGENT_ID",
"STRATEGY_SOLUTION_AGENT_ID",
"default_strategy_agent_roles",
"ensure_strategy_selection_agents",
"required_agent_ids",
]
@@ -7,19 +7,12 @@
1. 先复述已知信息,再判断是否真的存在阻塞性缺口。
2. 能依据原话形成任务草稿时直接收敛,不为了“问得完整”而连续追问。
3. 必须澄清时,一轮只问一个最影响任务边界的问题,并优先调用 `ask_clarification` 提供 2–4 个容易选择的选项。
4. 用户没有明确说明的内容只能写入 `assumptions`,不得伪装成事实或硬约束。
4. 用户没有明确说明的内容只能在任务详情中标成“暂定假设”,不得伪装成事实或硬约束。
5. 任务草稿是可编辑建议,不替用户作政治立场判断,也不把争议性主张写成已证实事实。
## 可推荐的默认分析角度
## 可推荐分析角度
- `governance`:政治治理
- `diplomacy`:外交国际
- `economy`:经济产业链
- `security`:安全风险
- `public_opinion`:社会舆情
- `law_policy`:法律政策
根据任务相关性推荐 2–6 个角度;最终选择权属于用户。安全风险角度只做高层级风险识别、预警、人道保护和降级建议,不展开作战战术。
每次请求都会提供管理员当前启用的角度 ID、名称和说明。只能从该次请求给出的 ID 中选择与任务相关的子集,不得依赖内置清单、补写已停用角度或自造 ID;最终选择权属于用户。安全风险类角度只做高层级风险识别、预警、人道保护和降级建议,不展开作战战术。
## 收敛协议
@@ -37,7 +30,7 @@
"deliverables": "希望得到的产物,多项用分号分隔",
"acceptanceCriteria": "用户可核验的完成标准,多项用分号分隔",
"horizon": "任务的时间范围或阶段",
"selectedAngleIds": ["governance", "diplomacy", "security"]
"selectedAngleIds": ["请求中提供的角度ID"]
}
```
````
@@ -4,7 +4,9 @@ from __future__ import annotations
import asyncio
import logging
import os
import shutil
import tempfile
import time
from datetime import datetime
from typing import Any
@@ -16,7 +18,9 @@ from pydantic import BaseModel, Field
from app.gateway.auth.password import verify_password_async
from app.gateway.deps import get_agent_store, get_config, get_skill_store, get_tag_store
from app.gateway.routers._strategy_selection_seed import required_agent_ids as required_strategy_agent_ids
from app.gateway.routers._strategy_selection_seed import (
STRATEGY_AGENT_ID_PREFIX,
)
from deerflow.agents.lead_agent.prompt import refresh_skills_system_prompt_cache_async
from deerflow.config.agents_api_config import get_agents_api_config
from deerflow.config.agents_config import CommonQuestion, list_custom_agents, load_agent_config, load_agent_soul, validate_agent_id
@@ -34,7 +38,27 @@ from deerflow.skills.usage import all_entries as get_skill_usage_entries
logger = logging.getLogger(__name__)
router = APIRouter(prefix="/api", tags=["agents"])
_STRATEGY_SELECTION_AGENT_IDS = frozenset(required_strategy_agent_ids())
def _is_strategy_agent_id(agent_id: str) -> bool:
return agent_id.casefold().startswith(STRATEGY_AGENT_ID_PREFIX.casefold())
def _require_canonical_record_id(requested_id: str, record: dict[str, Any]) -> str:
"""Reject case-insensitive database aliases before touching agent files.
MySQL deployments may use a case-insensitive collation while Windows agent
directories are also case-insensitive. A lookup for a differently-cased
id must never be allowed to select one row and mutate another path.
"""
canonical_id = str(record.get("id") or "")
if canonical_id != requested_id:
raise HTTPException(
status_code=409,
detail=f"Agent id casing mismatch; use canonical id '{canonical_id}'",
)
return canonical_id
class AgentResponse(BaseModel):
@@ -281,6 +305,24 @@ def _validate_storage_id(agent_id: str) -> str:
raise HTTPException(status_code=422, detail=str(exc)) from exc
def _validate_strategy_agent_model(
raw_model: str | None,
*,
app_config: AppConfig,
) -> str | None:
"""Normalize a shared strategy role's model and reject stale names."""
model = (raw_model or "").strip() or None
if model is None:
return None
if model not in {entry.name for entry in app_config.models}:
raise HTTPException(
status_code=422,
detail=f"Unknown strategy agent model: {model}",
)
return model
async def _validate_strategy_agent_skills(
requested_skills: list[str] | None,
*,
@@ -313,14 +355,8 @@ async def _validate_strategy_agent_skills(
normalized.append(name)
try:
skills = get_or_new_skill_storage(app_config=app_config).load_skills(
enabled_only=False
)
ownership = {
str(record.get("name") or ""): record
for record in await skill_store.list_all()
if record.get("name")
}
skills = get_or_new_skill_storage(app_config=app_config).load_skills(enabled_only=False)
ownership = {str(record.get("name") or ""): record for record in await skill_store.list_all() if record.get("name")}
usage_entries = get_skill_usage_entries()
except HTTPException:
raise
@@ -336,12 +372,7 @@ async def _validate_strategy_agent_skills(
detail=f"Unknown strategy agent skills: {', '.join(unknown)}",
)
unavailable = [
name
for name in normalized
if not by_name[name].enabled
or str((usage_entries.get(name) or {}).get("state") or "active") != "active"
]
unavailable = [name for name in normalized if not by_name[name].enabled or str((usage_entries.get(name) or {}).get("state") or "active") != "active"]
if unavailable:
raise HTTPException(
status_code=422,
@@ -354,18 +385,13 @@ async def _validate_strategy_agent_skills(
if skill.category != SkillCategory.CUSTOM:
continue
record = ownership.get(name)
if record is not None and (
record.get("owner_user_id") is None or bool(record.get("published"))
):
if record is not None and (record.get("owner_user_id") is None or bool(record.get("published"))):
continue
not_global.append(name)
if not_global:
raise HTTPException(
status_code=422,
detail=(
"Strategy agent skills must be globally available; "
f"unpublished or unregistered custom skills: {', '.join(not_global)}"
),
detail=(f"Strategy agent skills must be globally available; unpublished or unregistered custom skills: {', '.join(not_global)}"),
)
return normalized
@@ -374,13 +400,10 @@ async def _validate_strategy_agent_skills(
def _ensure_strategy_agent_deletable(agent_id: str) -> None:
"""Keep fixed strategy role ids alive; their SOUL/skills remain editable."""
if agent_id in _STRATEGY_SELECTION_AGENT_IDS:
if _is_strategy_agent_id(agent_id):
raise HTTPException(
status_code=409,
detail=(
f"Strategy role agent '{agent_id}' cannot be deleted; "
"edit its SOUL and skills instead"
),
detail=(f"Strategy role agent '{agent_id}' cannot be deleted; edit its SOUL and skills instead"),
)
@@ -415,24 +438,19 @@ def _write_agent_config(
# Persist as plain dicts (dropping blank rows) so config.yaml stays a
# clean mapping rather than a pydantic repr. Accepts already-coerced
# CommonQuestion models or raw dicts/strings.
normalized = [
q if isinstance(q, CommonQuestion) else CommonQuestion.model_validate(q)
for q in common_questions
]
config_data["common_questions"] = [
q.model_dump() for q in normalized if q.prompt.strip()
]
normalized = [q if isinstance(q, CommonQuestion) else CommonQuestion.model_validate(q) for q in common_questions]
config_data["common_questions"] = [q.model_dump() for q in normalized if q.prompt.strip()]
if template_name is not None and template_name.strip():
config_data["template_name"] = template_name.strip()
# 会商席位技能识别强化开关:仅当显式设置时写入(None = 不写, 由 AgentConfig 默认为关)。
if seat_skill_directive is not None:
config_data["seat_skill_directive"] = bool(seat_skill_directive)
if llmwiki_knowledge_base_ids is not None:
config_data["llmwiki_knowledge_base_ids"] = list(
dict.fromkeys(item.strip() for item in llmwiki_knowledge_base_ids if item.strip())
)
with open(config_file, "w", encoding="utf-8") as f:
yaml.dump(config_data, f, default_flow_style=False, allow_unicode=True)
config_data["llmwiki_knowledge_base_ids"] = list(dict.fromkeys(item.strip() for item in llmwiki_knowledge_base_ids if item.strip()))
_atomic_write_text(
config_file,
yaml.safe_dump(config_data, default_flow_style=False, allow_unicode=True),
)
def _load_config_extras(agent_id: str) -> dict[str, Any]:
@@ -466,7 +484,35 @@ def _load_config_extras(agent_id: str) -> dict[str, Any]:
def _write_agent_soul(agent_id: str, soul: str) -> None:
agent_dir = get_paths().agent_dir(agent_id)
agent_dir.mkdir(parents=True, exist_ok=True)
(agent_dir / "SOUL.md").write_text(soul, encoding="utf-8")
_atomic_write_text(agent_dir / "SOUL.md", soul)
def _atomic_write_text(path, content: str) -> None:
"""Durably replace one small agent config file on the same filesystem."""
path.parent.mkdir(parents=True, exist_ok=True)
temp_name: str | None = None
try:
with tempfile.NamedTemporaryFile(
mode="w",
encoding="utf-8",
dir=path.parent,
prefix=f".{path.name}.",
suffix=".tmp",
delete=False,
) as handle:
temp_name = handle.name
handle.write(content)
handle.flush()
os.fsync(handle.fileno())
os.replace(temp_name, path)
temp_name = None
finally:
if temp_name is not None:
try:
os.unlink(temp_name)
except FileNotFoundError:
pass
async def _sync_legacy_agents(store: AgentStore) -> None:
@@ -521,11 +567,7 @@ async def _backfill_listing_cache(store: AgentStore, pairs: list[tuple[str, Any]
existing = cached.get(agent_id)
if existing is None or existing.get("skills") != want_skills:
await store.replace_skills(agent_id, want_skills)
if (
existing is None
or existing.get("tool_groups") != want_tool_groups
or existing.get("model") != want_model
):
if existing is None or existing.get("tool_groups") != want_tool_groups or existing.get("model") != want_model:
await store.set_extras(agent_id, tool_groups=want_tool_groups, model=want_model)
@@ -739,9 +781,7 @@ async def _build_list_responses(
SOUL.md is skipped (only the edit dialog reads it). An agent absent from
the cache (never mirrored) keeps its ``None`` extras until the next sync.
"""
responses = [
_record_to_response(a, include_soul=False, include_extras=False) for a in records
]
responses = [_record_to_response(a, include_soul=False, include_extras=False) for a in records]
ids = [r.id for r in responses]
extras = await store.get_extras_for(ids)
assignments = await tag_store.list_assignments("agent", ids)
@@ -794,11 +834,7 @@ async def list_agents(
),
admin: bool = Query(
default=False,
description=(
"Admin-only override. When ``true`` and the caller is a system admin, "
"every agent is returned (built-in + every user's agents + private-square "
"entries) regardless of the usual ownership/visibility filter."
),
description=("Admin-only override. When ``true`` and the caller is a system admin, every agent is returned (built-in + every user's agents + private-square entries) regardless of the usual ownership/visibility filter."),
),
) -> AgentsListResponse:
_require_agents_api_enabled()
@@ -852,11 +888,7 @@ async def list_agents(
if owner and owner.strip():
responses = [r for r in responses if r.user_id == owner.strip()]
if square_id:
responses = [
r
for r in responses
if (r.square_id or "") == square_id or (square_is_default and not (r.square_id or ""))
]
responses = [r for r in responses if (r.square_id or "") == square_id or (square_is_default and not (r.square_id or ""))]
responses = _sort_agent_responses(responses, sort)
return AgentsListResponse(agents=responses, total=len(responses), page=1, page_size=len(responses))
except HTTPException:
@@ -881,11 +913,7 @@ async def check_agent_name(name: str) -> dict[str, Any]:
"/agents/selector",
response_model=AgentSelectorResponse,
summary="Agent Homepage Selector",
description=(
"Return the admin-curated agent list shown on the new-chat homepage. "
"Sorted by ``featured_order`` ascending. Visibility-filtered, so an "
"agent the caller can't see (author un-published) is silently omitted."
),
description=("Return the admin-curated agent list shown on the new-chat homepage. Sorted by ``featured_order`` ascending. Visibility-filtered, so an agent the caller can't see (author un-published) is silently omitted."),
)
async def get_agent_selector(request: Request) -> AgentSelectorResponse:
_require_agents_api_enabled()
@@ -897,12 +925,7 @@ async def get_agent_selector(request: Request) -> AgentSelectorResponse:
records = await store.list_featured(user_id)
# Selector dropdown only renders name/description/featured_order — same
# rationale as list_agents: skip soul + extras + self-heal.
return AgentSelectorResponse(
agents=[
_record_to_response(r, include_soul=False, include_extras=False)
for r in records
]
)
return AgentSelectorResponse(agents=[_record_to_response(r, include_soul=False, include_extras=False) for r in records])
except HTTPException:
raise
except Exception as e:
@@ -961,6 +984,11 @@ async def create_agent_endpoint(request: AgentCreateRequest, http_request: Reque
if request.id:
agent_id = _validate_storage_id(request.id)
if _is_strategy_agent_id(agent_id):
raise HTTPException(
status_code=409,
detail=("The 'strategy-selection-' id namespace is reserved; create strategy angle agents through /api/strategy-agents/angles"),
)
if await store.get_any(agent_id) is not None:
raise HTTPException(status_code=409, detail=f"Agent id '{agent_id}' already exists")
else:
@@ -1026,14 +1054,18 @@ async def create_agent_endpoint(request: AgentCreateRequest, http_request: Reque
async def update_agent(agent_id: str, request: AgentUpdateRequest, http_request: Request) -> AgentResponse:
_require_agents_api_enabled()
agent_id = _validate_storage_id(agent_id)
if _is_strategy_agent_id(agent_id):
raise HTTPException(
status_code=409,
detail=("Strategy agent configuration must be changed through /api/strategy-agents/{agent_id}"),
)
try:
store = get_agent_store(http_request)
user_id = _current_user_id(http_request)
is_admin = _is_admin_user(http_request)
current, edit_mode = await _get_editable_or_raise(
store, agent_id, user_id, is_admin=is_admin
)
current, edit_mode = await _get_editable_or_raise(store, agent_id, user_id, is_admin=is_admin)
_require_canonical_record_id(agent_id, current)
fields_set = request.model_fields_set
update_data: dict[str, Any] = {}
@@ -1061,7 +1093,7 @@ async def update_agent(agent_id: str, request: AgentUpdateRequest, http_request:
next_description = update_data.get("description", current.get("description") or "")
existing_extras = _load_config_extras(agent_id)
if "skills" in fields_set and agent_id in _STRATEGY_SELECTION_AGENT_IDS:
if "skills" in fields_set and _is_strategy_agent_id(agent_id):
next_skills = await _validate_strategy_agent_skills(
request.skills,
app_config=get_config(http_request),
@@ -1069,16 +1101,18 @@ async def update_agent(agent_id: str, request: AgentUpdateRequest, http_request:
)
else:
next_skills = request.skills if "skills" in fields_set else existing_extras["skills"]
next_model = request.model if "model" in fields_set else existing_extras["model"]
if "model" in fields_set and _is_strategy_agent_id(agent_id):
next_model = _validate_strategy_agent_model(
request.model,
app_config=get_config(http_request),
)
else:
next_model = request.model if "model" in fields_set else existing_extras["model"]
next_tool_groups = request.tool_groups if "tool_groups" in fields_set else existing_extras["tool_groups"]
next_common_questions = request.common_questions if "common_questions" in fields_set else existing_extras["common_questions"]
next_template_name = request.template_name if "template_name" in fields_set else existing_extras["template_name"]
next_seat_skill_directive = request.seat_skill_directive if "seat_skill_directive" in fields_set else existing_extras["seat_skill_directive"]
next_llmwiki_ids = (
await _validate_llmwiki_bindings(http_request, request.llmwiki_knowledge_base_ids)
if "llmwiki_knowledge_base_ids" in fields_set
else existing_extras["llmwiki_knowledge_base_ids"]
)
next_llmwiki_ids = await _validate_llmwiki_bindings(http_request, request.llmwiki_knowledge_base_ids) if "llmwiki_knowledge_base_ids" in fields_set else existing_extras["llmwiki_knowledge_base_ids"]
config_dirty = bool(update_data) or any(key in fields_set for key in ("skills", "model", "tool_groups", "common_questions", "template_name", "seat_skill_directive", "llmwiki_knowledge_base_ids"))
if config_dirty:
@@ -1186,14 +1220,18 @@ async def update_user_profile(request: UserProfileUpdateRequest) -> UserProfileR
async def delete_agent(agent_id: str, request: Request) -> None:
_require_agents_api_enabled()
agent_id = _validate_storage_id(agent_id)
if _is_strategy_agent_id(agent_id):
raise HTTPException(
status_code=409,
detail=("Strategy agents cannot be deleted through the generic API; use /api/strategy-agents/{agent_id} to disable an angle"),
)
try:
store = get_agent_store(request)
user_id = _current_user_id(request)
is_admin = _is_admin_user(request)
_, edit_mode = await _get_editable_or_raise(
store, agent_id, user_id, is_admin=is_admin
)
record, edit_mode = await _get_editable_or_raise(store, agent_id, user_id, is_admin=is_admin)
_require_canonical_record_id(agent_id, record)
_ensure_strategy_agent_deletable(agent_id)
if edit_mode == "admin":
@@ -1498,11 +1536,7 @@ class AgentFeaturedReorderRequest(BaseModel):
agent_ids: list[str] = Field(
default_factory=list,
description=(
"Featured agent ids in the desired top→bottom display order. Ids not "
"currently featured are silently skipped, so the client can send the "
"current visible order without first reconciling with the server."
),
description=("Featured agent ids in the desired top→bottom display order. Ids not currently featured are silently skipped, so the client can send the current visible order without first reconciling with the server."),
)
@@ -1510,11 +1544,7 @@ class AgentFeaturedReorderRequest(BaseModel):
"/agents/featured/order",
status_code=204,
summary="Reorder Featured Agents (admin)",
description=(
"Admin-only. Bulk-assign ``featured_order`` to each id in the order received "
"(``0`` is topmost). After this, the homepage selector renders agents in "
"exactly the supplied order. Ids not already featured are ignored."
),
description=("Admin-only. Bulk-assign ``featured_order`` to each id in the order received (``0`` is topmost). After this, the homepage selector renders agents in exactly the supplied order. Ids not already featured are ignored."),
)
async def reorder_featured_agents(
body: AgentFeaturedReorderRequest,
@@ -68,10 +68,14 @@ class TaskUpdateRequest(BaseModel):
brief: str | None = None
agents: list[TaskAgentSchema] | None = None
messages: list[dict[str, Any]] | None = None
# Optional optimistic-concurrency token. Legacy callers may omit it; the
# strategy-project workbench always sends the revision it loaded/saved.
expected_updated_at: datetime | None = None
class TaskEnsureRequest(BaseModel):
"""get-or-create:宿主传的 taskId 不固定,新 id 直接**初始化**一条空记录返回(不 404)。"""
id: str = Field(min_length=1, max_length=64)
title: str = Field(default="", max_length=512)
brief: str | None = None
@@ -162,12 +166,24 @@ async def update_task(request: Request, task_id: str, body: TaskUpdateRequest) -
store = _get_store(request)
user_id = _current_user_id(request)
fields = body.model_dump(exclude_unset=True)
expected_updated_at = fields.pop("expected_updated_at", None)
# Normalize agents (pydantic models) to plain dicts for JSON storage.
if "agents" in fields and fields["agents"] is not None:
fields["agents"] = [a if isinstance(a, dict) else a.model_dump() for a in body.agents or []]
row = await store.update_task(task_id, user_id, **fields)
row = await store.update_task(
task_id,
user_id,
expected_updated_at=expected_updated_at,
**fields,
)
if row is None:
raise HTTPException(status_code=404, detail="Task not found")
existing = await store.get_task(task_id, user_id)
if existing is None:
raise HTTPException(status_code=404, detail="Task not found")
raise HTTPException(
status_code=409,
detail="项目已在其他页面更新,请选择恢复本地草稿或使用服务器版本",
)
return TaskResponse(**row)
@@ -40,11 +40,12 @@ from fastapi.responses import StreamingResponse
from pydantic import BaseModel, Field
from app.gateway.roundtable_model_fallback import is_llm_error_message, reason_text
from app.gateway.routers import agents as agents_router
from app.gateway.routers._strategy_selection_seed import STRATEGY_AGENT_ID_PREFIX
from app.gateway.routers.multi_agent import (
_ROUNDTABLE_THREAD_METADATA,
_auth_headers,
_create_thread,
_flatten_content,
_loopback_base,
_make_loopback_client,
_parse_last_messages,
@@ -54,6 +55,7 @@ from app.gateway.routers.multi_agent import (
)
from deerflow.agents.roundtable_orchestrator.delivery import visible_seat_text
from deerflow.tools.builtins.clarification_utils import resolve_allow_multiple
from deerflow.utils.stream_text import InlineThinkTagFilter
logger = logging.getLogger(__name__)
router = APIRouter(prefix="/api/parallel-agents", tags=["parallel-agents"])
@@ -72,6 +74,7 @@ def _has_substantive_content(content: str) -> bool:
visible = visible_seat_text(content)
return sum(character.isalnum() for character in visible) >= MIN_MEANINGFUL_CONTENT_CHARS
# 禁两类工具:①**写文件**(write_file / str_replace)—— 并行席位互相写文件会打架;
# ②**派活 / 脚本逃逸**(agent_orchestration / bash)—— 本面板里每个智能体只做自己那
# 份分析交付,绝不该扮演「总控」去调度别人。弱模型(deepseek 等)拿到带「总控 / 业务
@@ -80,10 +83,10 @@ def _has_substantive_content(content: str) -> bool:
# glob / bash 翻找派活脚本)。物理禁掉这两类即可根治。其余只读工具(ls / read_file /
# 检索)与 ask_clarification(协作卡片)保留:既不互相打架,又能正常出步骤条 / 要澄清。
_EXCLUDED_TOOLS: list[str] = [
"write_file", # 并行不互相写文件打架
"write_file", # 并行不互相写文件打架
"str_replace",
"agent_orchestration", # 子智能体不派活 / 不调度(防模仿总控)
"bash", # 禁跑脚本(防绕道脚本调内部接口模仿派活)
"bash", # 禁跑脚本(防绕道脚本调内部接口模仿派活)
]
@@ -119,25 +122,126 @@ async def issue_token(body: _TokenRequest) -> _TokenResponse:
# ── frame normalisation (mirrors multi-agent.ts) ─────────────────────────────
def _extract_ai_text(chunk: dict[str, Any]) -> str:
"""AI text delta from a ``messages-tuple`` chunk obj; "" when not AI text."""
def _extract_ai_stream_parts(chunk: dict[str, Any]) -> tuple[str, str]:
"""Return ``(answer, reasoning)`` deltas from one AI message chunk.
OpenAI-compatible providers do not agree on where displayable reasoning
lives. Most put it in ``additional_kwargs.reasoning_content`` (or the
legacy ``reasoning`` key), while a few return typed content blocks. Keep
that channel separate from the answer so the strategy workbench can show
progress without mixing internal trace text into the generated strategy.
Literal ``<think>`` blocks in the normal text channel are handled by the
per-agent :class:`InlineThinkTagFilter` in ``_consume_agent`` because tags
may be split across arbitrary SSE chunks.
"""
type_ = str(chunk.get("type") or "").lower()
if type_ not in ("ai", "aimessagechunk", "aimessage"):
return ""
return "", ""
content = chunk.get("content")
answer_parts: list[str] = []
reasoning_parts: list[str] = []
if isinstance(content, str):
return content
if isinstance(content, list):
out: list[str] = []
answer_parts.append(content)
elif isinstance(content, list):
for item in content:
if isinstance(item, str):
out.append(item)
answer_parts.append(item)
elif isinstance(item, dict):
t = item.get("text")
if isinstance(t, str):
out.append(t)
return "".join(out)
return ""
block_type = str(item.get("type") or "").lower()
value = item.get("thinking")
if not isinstance(value, str):
value = item.get("text")
if not isinstance(value, str):
value = item.get("content")
if not isinstance(value, str):
continue
if block_type in {
"thinking",
"thinking_delta",
"reasoning",
"reasoning_delta",
}:
reasoning_parts.append(value)
else:
answer_parts.append(value)
additional_kwargs = chunk.get("additional_kwargs")
direct_reasoning = chunk.get("reasoning_content") or chunk.get("reasoning")
nested_reasoning: object = None
if isinstance(additional_kwargs, dict):
nested_reasoning = additional_kwargs.get("reasoning_content") or additional_kwargs.get("reasoning")
raw_reasoning = nested_reasoning or direct_reasoning
if isinstance(raw_reasoning, str):
reasoning_parts.append(raw_reasoning)
return "".join(answer_parts), "".join(reasoning_parts)
def _extract_ai_text(chunk: dict[str, Any]) -> str:
"""Compatibility helper returning only the ordinary AI text delta."""
return _extract_ai_stream_parts(chunk)[0]
def _split_final_inline_reasoning(content: str) -> tuple[str, str]:
"""Split complete inline thinking, including template-injected close tags.
Some reasoning chat templates insert ``<think>`` before generation, so the
provider returns only ``reasoning</think>answer``. The streaming filter
cannot classify that prefix before the closing marker arrives, but the
terminal values frame can. Returning the recovered trace here also lets
the client correct any transient streaming classification.
"""
if not content:
return "", ""
working = content
prefix_reasoning = ""
lowered = working.lower()
first_open = lowered.find("<think>")
first_close = lowered.find("</think>")
if first_close >= 0 and (first_open < 0 or first_close < first_open):
prefix_reasoning = working[:first_close]
working = working[first_close + len("</think>") :]
text_filter = InlineThinkTagFilter()
visible, reasoning = text_filter.push_parts(working)
visible_tail, reasoning_tail = text_filter.finish_parts()
final_reasoning = "".join(part for part in (prefix_reasoning, reasoning, reasoning_tail) if part)
# ``visible_seat_text`` remains the canonical complete-output cleanup for
# malformed/multiple tags; the filter-derived visible value is only a
# fallback for an otherwise empty canonical result.
final_visible = visible_seat_text(content) or f"{visible}{visible_tail}".strip()
return final_visible, final_reasoning.strip()
def _should_probe_implicit_reasoning(
request: Request,
agent_id: str,
requested_model: str,
) -> bool:
"""Enable close-only probing only for a thinking-capable effective model.
Ordinary models must stream untagged answer chunks immediately. The
stricter prelude buffer is reserved for providers that advertise thinking
support and may use templates where ``<think>`` is injected outside the
returned content while ``</think>`` remains in-band.
"""
config = getattr(request.app.state, "config", None)
models = list(getattr(config, "models", None) or [])
effective_name = requested_model.strip()
if not effective_name:
try:
effective_name = str(agents_router._load_config_extras(agent_id).get("model") or "").strip()
except Exception:
effective_name = ""
if not effective_name and models:
effective_name = str(getattr(models[0], "name", "") or "")
selected = next(
(entry for entry in models if str(getattr(entry, "name", "") or "") == effective_name),
None,
)
return bool(selected and getattr(selected, "supports_thinking", False))
def _ingest_tool_chunks(chunks: Any, acc: dict[int, dict[str, Any]]) -> None:
@@ -248,7 +352,9 @@ async def _run_one_agent(
failure_reason = is_llm_error_message(last_ai)
if failure_reason:
raise RuntimeError(f"模型调用失败:{reason_text(failure_reason)}")
content = _flatten_content(last_ai.get("content", ""))
answer_channel, structured_reasoning = _extract_ai_stream_parts(last_ai)
content, inline_reasoning = _split_final_inline_reasoning(answer_channel)
reasoning = "\n\n".join(part.strip() for part in (structured_reasoning, inline_reasoning) if part.strip())
# 收集**所有** AI 消息的 tool_calls(全量 args,按顺序)—— 用于在收口时补发所有步骤
# (含 ls / skill_list 这类无参/中间步骤),保证步骤条不丢步、且 args 完整。
final_tool_calls: list[dict[str, Any]] = []
@@ -263,6 +369,7 @@ async def _run_one_agent(
yield {
"type": "clarification",
"content": content,
"reasoning": reasoning,
"clarification": clarification,
"tool_calls": final_tool_calls,
}
@@ -272,6 +379,7 @@ async def _run_one_agent(
yield {
"status": f"子智能体 {agent_id} 完成",
"content": content,
"reasoning": reasoning,
"agent_name": agent_id,
"tool_calls": final_tool_calls,
}
@@ -283,10 +391,7 @@ def _build_task_message(task_name: str, brief: str) -> str:
parts.append(f"任务名称:{task_name.strip()}")
if brief.strip():
parts.append(f"任务简介:\n{brief.strip()}")
parts.append(
"请你从你的专业职责出发,针对以上任务给出你的分析与交付。"
"本轮为**并行**研讨:请把你的产出**直接写在回复正文里**(本轮禁止写文件)。"
)
parts.append("请你从你的专业职责出发,针对以上任务给出你的分析与交付。本轮为**并行**研讨:请把你的产出**直接写在回复正文里**(本轮禁止写文件)。")
# 角色边界护栏:本面板里每个智能体都是**执行席位**,不是总控 / 指挥。即便任务里带有
# 「总控 / 业务链条 / 调度 / 派活」字样,也只完成属于自己的那份交付,严禁模仿总控派活。
parts.append(
@@ -329,6 +434,41 @@ async def run_parallel_agents_stream(request: Request, payload: dict = Body(...)
status_code=400,
detail=f"并行智能体数量上限 {MAX_PARALLEL_AGENTS}(当前 {len(agent_ids)})",
)
role_store = getattr(request.app.state, "strategy_agent_role_store", None)
agent_store = getattr(request.app.state, "agent_store", None)
strategy_ids = [agent_id for agent_id in agent_ids if agent_id.casefold().startswith(STRATEGY_AGENT_ID_PREFIX.casefold())]
if strategy_ids and role_store is not None and agent_store is not None:
# Every worker owns an independent memory store. Synchronize from
# durable folders/sidecars immediately before execution so a request
# routed to another worker cannot see random 404s or revived angles.
from app.gateway.routers.strategy_agents import _ensure_registry
await _ensure_registry(agent_store, role_store)
for agent_id in agent_ids:
if not agent_id.casefold().startswith(STRATEGY_AGENT_ID_PREFIX.casefold()):
continue
if role_store is None or agent_store is None:
raise HTTPException(
status_code=503,
detail="策略智能体分类或配置服务不可用",
)
role = await role_store.get_role(agent_id)
if role is None:
raise HTTPException(
status_code=400,
detail=f"未注册的策略智能体:{agent_id}",
)
if not bool(role.get("enabled")):
raise HTTPException(
status_code=409,
detail=f"策略智能体已停用:{agent_id}",
)
record = await agent_store.get_any(agent_id)
if record is None or str(record.get("id") or "") != agent_id or record.get("user_id") is not None:
raise HTTPException(
status_code=409,
detail=f"策略智能体必须由系统全局持有:{agent_id}",
)
if not task_name and not brief:
raise HTTPException(status_code=400, detail="任务名称与简介至少填写其一")
@@ -346,8 +486,54 @@ async def run_parallel_agents_stream(request: Request, payload: dict = Body(...)
emitted_tools: set[str] = set() # 已发过 step 的 tool_call id(去重)
tool_acc: dict[int, dict[str, Any]] = {} # tool_call_chunks 累积
buffer: list[str] = []
raw_answer_buffer: list[str] = []
probe_implicit_close = await asyncio.to_thread(
_should_probe_implicit_reasoning,
request,
agent_id,
model_name,
)
inline_thinking = InlineThinkTagFilter(allow_implicit_close=probe_implicit_close)
reasoning_emitted = False
done_sent = False
async def _emit_text_parts(text: str, provider_reasoning: str = "") -> None:
nonlocal reasoning_emitted
if text:
raw_answer_buffer.append(text)
visible_prelude = inline_thinking.confirm_visible_channel() if provider_reasoning else ""
visible, inline_reasoning = inline_thinking.push_parts(text)
reasoning = f"{provider_reasoning}{inline_reasoning}"
if reasoning:
reasoning_emitted = True
await queue.put(
{
"type": "reasoning_delta",
"agent_id": agent_id,
"text": reasoning,
}
)
confirmed_visible = f"{visible_prelude}{visible}"
if confirmed_visible:
buffer.append(confirmed_visible)
await queue.put({"type": "delta", "agent_id": agent_id, "text": confirmed_visible})
async def _flush_text_filter() -> None:
nonlocal reasoning_emitted
visible, reasoning = inline_thinking.finish_parts()
if reasoning:
reasoning_emitted = True
await queue.put(
{
"type": "reasoning_delta",
"agent_id": agent_id,
"text": reasoning,
}
)
if visible:
buffer.append(visible)
await queue.put({"type": "delta", "agent_id": agent_id, "text": visible})
async def _emit_tool(tc: Any, allow_empty: bool = False) -> None:
"""一个 tool_call(含完整 args)→ 一条 step 事件;按 id 去重。
@@ -369,9 +555,7 @@ async def run_parallel_agents_stream(request: Request, payload: dict = Body(...)
if key_id in emitted_tools:
return
emitted_tools.add(key_id)
await queue.put(
{"type": "step", "agent_id": agent_id, "key": f"tool_calling:{name}", "label": name, "args": args}
)
await queue.put({"type": "step", "agent_id": agent_id, "key": f"tool_calling:{name}", "label": name, "args": args})
try:
thread_id = await _create_thread(client, base, headers, metadata=_ROUNDTABLE_THREAD_METADATA)
@@ -381,22 +565,32 @@ async def run_parallel_agents_stream(request: Request, payload: dict = Body(...)
async for chunk in gen:
if isinstance(chunk, dict):
# 终态:先用最终 tool_calls(全量 args)补发漏掉的 step,再发收口/澄清。
await _flush_text_filter()
for tc in chunk.get("tool_calls") or []:
await _emit_tool(tc, allow_empty=True)
final_content = visible_seat_text(str(chunk.get("content") or ""))
final_reasoning = str(chunk.get("reasoning") or "")
if final_reasoning and not reasoning_emitted:
reasoning_emitted = True
await queue.put(
{
"type": "reasoning_delta",
"agent_id": agent_id,
"text": final_reasoning,
}
)
if chunk.get("type") == "clarification":
clar = chunk.get("clarification") or {}
await queue.put(
{
"type": "agent_clarification",
"agent_id": agent_id,
"content": str(chunk.get("content") or ""),
"content": final_content,
**clar,
}
)
else:
await queue.put(
{"type": "agent_done", "agent_id": agent_id, "content": str(chunk.get("content") or "")}
)
await queue.put({"type": "agent_done", "agent_id": agent_id, "content": final_content})
done_sent = True
continue
line = chunk.strip() if isinstance(chunk, str) else ""
@@ -415,10 +609,9 @@ async def run_parallel_agents_stream(request: Request, payload: dict = Body(...)
if last_event == "messages" and isinstance(parsed, list) and parsed:
obj = parsed[0]
if isinstance(obj, dict):
text = _extract_ai_text(obj)
if text:
buffer.append(text)
await queue.put({"type": "delta", "agent_id": agent_id, "text": text})
text, reasoning = _extract_ai_stream_parts(obj)
if text or reasoning:
await _emit_text_parts(text, reasoning)
# 完整 tool_calls(部分模型直接给)+ 累积 chunks 后的合成调用,都尝试发 step。
for tc in obj.get("tool_calls") or []:
await _emit_tool(tc)
@@ -427,18 +620,24 @@ async def run_parallel_agents_stream(request: Request, payload: dict = Body(...)
await _emit_tool(tc)
if not done_sent:
# 流被抢占/截断,没收到终态帧 —— 用已累积正文兜底收口本席位。
buffered_content = "".join(buffer)
failure_reason = is_llm_error_message(
{"type": "ai", "content": buffered_content}
)
await _flush_text_filter()
buffered_content, recovered_reasoning = _split_final_inline_reasoning("".join(raw_answer_buffer))
if not buffered_content:
buffered_content = "".join(buffer)
if recovered_reasoning and not reasoning_emitted:
reasoning_emitted = True
await queue.put(
{
"type": "reasoning_delta",
"agent_id": agent_id,
"text": recovered_reasoning,
}
)
failure_reason = is_llm_error_message({"type": "ai", "content": buffered_content})
if failure_reason:
raise RuntimeError(
f"模型调用失败:{reason_text(failure_reason)}"
)
raise RuntimeError(f"模型调用失败:{reason_text(failure_reason)}")
if not _has_substantive_content(buffered_content):
raise RuntimeError(
"智能体未生成有效正文,请重试或更换全局模型"
)
raise RuntimeError("智能体未生成有效正文,请重试或更换全局模型")
await queue.put(
{
"type": "agent_done",
@@ -0,0 +1,802 @@
"""Global strategy-selection agent role management.
The strategy workbench owns three fixed roles (intent, judge and solution)
plus an administrator-managed set of angle roles. Role metadata is stored in
``strategy_agent_roles`` while model/SOUL/skills remain in the normal
folder-backed agent configuration, so the same configuration is shared by
every project and every user.
"""
from __future__ import annotations
import asyncio
import logging
import os
import shutil
import weakref
from contextlib import asynccontextmanager
from datetime import datetime
from typing import Literal
from uuid import uuid4
import yaml
from fastapi import APIRouter, HTTPException, Request
from pydantic import BaseModel, Field
from app.gateway.auth.disabled_mode import is_auth_disabled
from app.gateway.deps import (
get_agent_store,
get_config,
get_current_user_from_request,
get_skill_store,
get_strategy_agent_role_store,
)
from app.gateway.routers import agents as agents_router
from app.gateway.routers._strategy_selection_seed import (
STRATEGY_ANGLE_AGENT_PREFIX,
default_strategy_agent_roles,
ensure_strategy_selection_agents,
)
from deerflow.config.paths import get_paths
from deerflow.persistence.agents import AgentStore
from deerflow.persistence.strategy_agent_roles import (
MemoryStrategyAgentRoleStore,
StrategyAgentRoleStore,
)
logger = logging.getLogger(__name__)
router = APIRouter(prefix="/api/strategy-agents", tags=["strategy-agents"])
StrategyAgentCategory = Literal["intent", "angle", "judge", "solution"]
_DYNAMIC_ANGLE_TOOL_GROUPS = ["web", "file:read"]
_ROLE_SIDECAR = ".strategy-role.yaml"
_PROCESS_LOCKS: weakref.WeakValueDictionary[str, asyncio.Lock] = weakref.WeakValueDictionary()
def _process_lock(agent_id: str) -> asyncio.Lock:
key = agent_id.casefold()
lock = _PROCESS_LOCKS.get(key)
if lock is None:
lock = asyncio.Lock()
_PROCESS_LOCKS[key] = lock
return lock
def _open_cross_process_lock(agent_id: str):
lock_dir = get_paths().agents_dir / ".strategy-locks"
lock_dir.mkdir(parents=True, exist_ok=True)
path = lock_dir / f"{agent_id}.lock"
handle = open(path, "a+b") # noqa: SIM115 - owned by async context below
if path.stat().st_size == 0:
handle.write(b"\0")
handle.flush()
return handle
def _try_cross_process_lock(handle) -> bool:
handle.seek(0)
try:
if os.name == "nt":
import msvcrt
msvcrt.locking(handle.fileno(), msvcrt.LK_NBLCK, 1)
else:
import fcntl
fcntl.flock(handle.fileno(), fcntl.LOCK_EX | fcntl.LOCK_NB)
return True
except OSError:
return False
def _release_cross_process_lock(handle) -> None:
try:
handle.seek(0)
if os.name == "nt":
import msvcrt
msvcrt.locking(handle.fileno(), msvcrt.LK_UNLCK, 1)
else:
import fcntl
fcntl.flock(handle.fileno(), fcntl.LOCK_UN)
finally:
handle.close()
@asynccontextmanager
async def _strategy_mutation_guard(agent_id: str):
"""Serialize one strategy agent across coroutines and worker processes.
The zero-byte lock-file namespace is durable, but OS locks are tied to the
open handle and are automatically released if a worker dies. The local
lock table uses weak values so one-off dynamic ids do not leak forever.
"""
async with _process_lock(agent_id):
handle = _open_cross_process_lock(agent_id)
acquired = False
try:
while not acquired:
acquired = _try_cross_process_lock(handle)
if not acquired:
await asyncio.sleep(0.05)
yield
finally:
if acquired:
# Unlock synchronously: this is a tiny OS call and must not be
# skipped if the request is already being cancelled.
_release_cross_process_lock(handle)
else:
handle.close()
def _write_role_sidecar(agent_id: str, role: dict) -> None:
payload = {
"agent_id": agent_id,
"category": "angle",
"fixed": False,
"enabled": bool(role.get("enabled", True)),
"sort_order": int(role.get("sort_order", 0)),
}
agents_router._atomic_write_text(
get_paths().agent_dir(agent_id) / _ROLE_SIDECAR,
yaml.safe_dump(payload, allow_unicode=True, sort_keys=False),
)
def _read_optional_text(path) -> str | None:
return path.read_text(encoding="utf-8") if path.is_file() else None
def _restore_optional_text(path, content: str | None) -> None:
if content is None:
path.unlink(missing_ok=True)
return
agents_router._atomic_write_text(path, content)
async def _rollback_strategy_update(
*,
agent_id: str,
agent_store: AgentStore,
role_store: StrategyAgentRoleStore,
record: dict,
role: dict,
extras: dict,
file_snapshot: dict,
) -> None:
"""Best-effort compensation for the mixed DB/filesystem update."""
try:
agent_dir = get_paths().agent_dir(agent_id)
await asyncio.to_thread(
_restore_optional_text,
agent_dir / "config.yaml",
file_snapshot["config"],
)
await asyncio.to_thread(
_restore_optional_text,
agent_dir / "SOUL.md",
file_snapshot["soul"],
)
await asyncio.to_thread(
_restore_optional_text,
agent_dir / _ROLE_SIDECAR,
file_snapshot["sidecar"],
)
await agent_store.update_any(
agent_id,
{
"name": record["name"],
"description": record.get("description") or "",
},
)
await agent_store.replace_skills(agent_id, extras["skills"])
await agent_store.set_extras(
agent_id,
tool_groups=extras["tool_groups"],
model=extras["model"],
)
await role_store.update_role(
agent_id,
enabled=bool(role["enabled"]),
sort_order=int(role["sort_order"]),
)
except Exception:
logger.exception("Failed to compensate strategy-agent update for '%s'", agent_id)
async def _recover_memory_angle_roles(role_store: StrategyAgentRoleStore) -> None:
"""Recover dynamic role metadata after a memory-mode process restart."""
if not isinstance(role_store, MemoryStrategyAgentRoleStore):
return
agents_dir = get_paths().agents_dir
if not agents_dir.is_dir():
return
for agent_dir in agents_dir.iterdir():
sidecar = agent_dir / _ROLE_SIDECAR
if not sidecar.is_file():
continue
try:
raw = yaml.safe_load(sidecar.read_text(encoding="utf-8")) or {}
agent_id = str(raw.get("agent_id") or "")
if agent_id != agent_dir.name or not agent_id.startswith(STRATEGY_ANGLE_AGENT_PREFIX) or raw.get("category") != "angle" or bool(raw.get("fixed", False)):
raise ValueError("invalid dynamic angle sidecar")
existing = await role_store.get_role(agent_id)
if existing is None:
await role_store.create_role(
{
"agent_id": agent_id,
"category": "angle",
"fixed": False,
"enabled": bool(raw.get("enabled", True)),
"sort_order": int(raw.get("sort_order", 0)),
}
)
elif not bool(existing.get("fixed")) and existing.get("category") == "angle":
# Memory stores are rebuilt with code defaults first. The
# sidecar is the durable authority for every non-fixed angle,
# including the six shipped angles, so disabled defaults do
# not silently revive after a worker restart.
await role_store.update_role(
agent_id,
enabled=bool(raw.get("enabled", True)),
sort_order=int(raw.get("sort_order", 0)),
)
except Exception:
logger.exception("Ignoring invalid strategy role sidecar at %s", sidecar)
def _default_angle_soul(name: str, description: str) -> str:
focus = description.strip() or "围绕任务目标,从本角度给出独立、可执行的策略建议"
return (
f"# 角色\n\n你是“{name}”,负责{focus}。\n\n"
"# 工作要求\n\n"
"1. 先核对任务目标、约束、前置条件和时间范围;信息不足时明确假设。\n"
"2. 只从自己的专业角度分析,不替代其他角度,也不冒充评判或方案整理角色。\n"
"3. 输出具体策略、实施步骤、预期收益、主要风险、触发条件和退出条件。\n"
"4. 对时效性事实优先检索核验并标注依据;不得编造来源。\n"
"5. 遵守法律、安全与非升级边界,最终交付使用清晰的 Markdown。\n"
)
class StrategyAgentItem(BaseModel):
id: str
category: StrategyAgentCategory
fixed: bool
enabled: bool
sort_order: int
name: str
description: str = ""
model: str | None = None
soul: str | None = None
skills: list[str] | None = None
created_at: datetime | str | None = None
updated_at: datetime | str | None = None
class StrategyAgentListResponse(BaseModel):
agents: list[StrategyAgentItem]
class StrategyAngleCreateRequest(BaseModel):
name: str = Field(min_length=1, max_length=100)
description: str = Field(default="", max_length=2000)
soul: str = Field(default="", max_length=200_000)
model: str | None = Field(default=None, max_length=200)
skills: list[str] = Field(default_factory=list, max_length=100)
sort_order: int | None = Field(default=None, ge=-10_000, le=10_000)
class StrategyAgentUpdateRequest(BaseModel):
name: str | None = Field(default=None, max_length=100)
description: str | None = Field(default=None, max_length=2000)
soul: str | None = Field(default=None, max_length=200_000)
model: str | None = Field(default=None, max_length=200)
skills: list[str] | None = Field(default=None, max_length=100)
enabled: bool | None = None
sort_order: int | None = Field(default=None, ge=-10_000, le=10_000)
async def _require_admin(request: Request) -> None:
"""Require an administrator, except in explicit auth-disabled mode."""
user = getattr(request.state, "user", None)
if user is None:
if is_auth_disabled():
return
user = await get_current_user_from_request(request)
if getattr(user, "system_role", None) != "admin":
raise HTTPException(status_code=403, detail="策略智能体管理仅限管理员")
def _normalize_model(request: Request, raw_model: str | None) -> str | None:
return agents_router._validate_strategy_agent_model(
raw_model,
app_config=get_config(request),
)
async def _ensure_registry(
agent_store: AgentStore,
role_store: StrategyAgentRoleStore,
) -> None:
"""Seed missing role metadata and repair missing default agent rows."""
defaults = default_strategy_agent_roles()
await role_store.ensure_defaults(defaults)
await _recover_memory_angle_roles(role_store)
default_ids = [str(row["agent_id"]) for row in defaults]
for agent_id in default_ids:
existing = await agent_store.get_any(agent_id)
if existing is not None and existing.get("user_id") is not None:
# Never let legacy sync's ensure_builtin semantics silently treat a
# user-owned collision as one of the fixed global roles.
raise HTTPException(
status_code=409,
detail=f"Reserved strategy agent '{agent_id}' is owned by a user",
)
roles = await role_store.list_roles(include_disabled=True)
missing = [str(role["agent_id"]) for role in roles if await agent_store.get_any(str(role["agent_id"])) is None]
if missing:
# This path is normally handled by lifespan. It also rebuilds the
# memory-mode agent table from durable dynamic-angle folders.
await asyncio.to_thread(ensure_strategy_selection_agents)
await agents_router._sync_legacy_agents(agent_store)
for role in roles:
agent_id = str(role["agent_id"])
record = await agent_store.get_any(agent_id)
if record is None:
raise HTTPException(
status_code=503,
detail=f"Strategy agent '{agent_id}' configuration is unavailable",
)
if str(record.get("id") or "") != agent_id or record.get("user_id") is not None:
raise HTTPException(
status_code=409,
detail=f"Strategy agent '{agent_id}' must be a canonical global agent",
)
async def ensure_strategy_agent_registry(app) -> None:
"""Synchronize one worker's registry, including memory-mode sidecars."""
agent_store = getattr(app.state, "agent_store", None)
role_store = getattr(app.state, "strategy_agent_role_store", None)
if agent_store is None or role_store is None:
raise RuntimeError("Strategy agent persistence is unavailable")
await _ensure_registry(agent_store, role_store)
async def _to_item(
role: dict,
record: dict,
*,
include_config: bool,
) -> StrategyAgentItem:
if include_config:
agent = await agents_router._record_to_response_async(
record,
include_soul=True,
include_extras=True,
)
return StrategyAgentItem(
id=agent.id,
category=role["category"],
fixed=bool(role["fixed"]),
enabled=bool(role["enabled"]),
sort_order=int(role["sort_order"]),
name=agent.name,
description=agent.description,
model=agent.model,
soul=agent.soul,
skills=agent.skills,
created_at=agent.created_at,
updated_at=agent.updated_at,
)
# Public consumers need only identity and lifecycle metadata. Avoid
# leaking prompts/model/skills and avoid synchronous filesystem reads.
return StrategyAgentItem(
id=str(record["id"]),
category=role["category"],
fixed=bool(role["fixed"]),
enabled=bool(role["enabled"]),
sort_order=int(role["sort_order"]),
name=str(record["name"]),
description=str(record.get("description") or ""),
created_at=record.get("created_at"),
updated_at=record.get("updated_at"),
)
async def _get_role_and_agent(
request: Request,
agent_id: str,
) -> tuple[dict, dict]:
role_store = get_strategy_agent_role_store(request)
agent_store = get_agent_store(request)
await _ensure_registry(agent_store, role_store)
role = await role_store.get_role(agent_id)
if role is None:
raise HTTPException(status_code=404, detail=f"Strategy agent '{agent_id}' not found")
record = await agent_store.get_any(agent_id)
if record is None:
raise HTTPException(
status_code=409,
detail=f"Strategy agent '{agent_id}' has no backing agent configuration",
)
if record.get("user_id") is not None:
raise HTTPException(
status_code=409,
detail=f"Strategy agent '{agent_id}' must be globally owned",
)
canonical_role_id = str(role.get("agent_id") or "")
if canonical_role_id != agent_id:
raise HTTPException(
status_code=409,
detail=(f"Strategy agent id casing mismatch; use canonical id '{canonical_role_id}'"),
)
agents_router._require_canonical_record_id(agent_id, record)
return role, record
@router.get("", response_model=StrategyAgentListResponse)
async def list_strategy_agents(
request: Request,
include_disabled: bool = False,
include_config: bool = False,
) -> StrategyAgentListResponse:
"""List the shared role catalog; sensitive configuration is admin-only."""
if include_disabled or include_config:
await _require_admin(request)
role_store = get_strategy_agent_role_store(request)
agent_store = get_agent_store(request)
await _ensure_registry(agent_store, role_store)
roles = await role_store.list_roles(include_disabled=include_disabled)
items: list[StrategyAgentItem] = []
for role in roles:
record = await agent_store.get_any(str(role["agent_id"]))
if record is None:
if bool(role["enabled"]):
raise HTTPException(
status_code=503,
detail=(f"Strategy agent '{role['agent_id']}' configuration is unavailable"),
)
continue
if str(record.get("id") or "") != str(role["agent_id"]) or record.get("user_id") is not None:
raise HTTPException(
status_code=409,
detail=f"Strategy agent '{role['agent_id']}' must be a canonical global agent",
)
items.append(await _to_item(role, record, include_config=include_config))
return StrategyAgentListResponse(agents=items)
@router.post(
"/angles",
response_model=StrategyAgentItem,
status_code=201,
)
async def create_strategy_angle(
payload: StrategyAngleCreateRequest,
request: Request,
) -> StrategyAgentItem:
"""Create one global angle agent. Fixed role categories are not creatable."""
await _require_admin(request)
name = agents_router._validate_display_name(payload.name)
model = _normalize_model(request, payload.model)
skills = await agents_router._validate_strategy_agent_skills(
payload.skills,
app_config=get_config(request),
skill_store=get_skill_store(request),
)
agent_store = get_agent_store(request)
role_store = get_strategy_agent_role_store(request)
await _ensure_registry(agent_store, role_store)
agent_id = f"{STRATEGY_ANGLE_AGENT_PREFIX}{uuid4().hex}"
# The generated id has already passed the fixed prefix/storage contract;
# all filesystem and persistence mutations happen under the cross-worker
# guard below.
agent_id = agents_router._validate_storage_id(agent_id)
async with _strategy_mutation_guard(agent_id):
return await _create_strategy_angle_locked(
agent_id=agent_id,
payload=payload,
name=name,
model=model,
skills=skills,
agent_store=agent_store,
role_store=role_store,
)
async def _cleanup_failed_create(
agent_id: str,
agent_store: AgentStore,
role_store: StrategyAgentRoleStore,
) -> None:
async def attempt(label: str, operation) -> None:
try:
result = operation()
if asyncio.iscoroutine(result):
await result
except BaseException: # cleanup must continue through cancellation/failures
logger.exception("Failed create cleanup step %s for '%s'", label, agent_id)
await attempt("role", lambda: role_store.delete_role(agent_id))
await attempt("agent", lambda: agent_store.delete_builtin(agent_id))
await attempt("extras", lambda: agent_store.delete_extras_for_agent(agent_id))
agent_dir = get_paths().agent_dir(agent_id)
if agent_dir.exists():
await attempt("folder", lambda: asyncio.to_thread(shutil.rmtree, agent_dir))
async def _create_strategy_angle_locked(
*,
agent_id: str,
payload: StrategyAngleCreateRequest,
name: str,
model: str | None,
skills: list[str] | None,
agent_store: AgentStore,
role_store: StrategyAgentRoleStore,
) -> StrategyAgentItem:
agent_dir = get_paths().agent_dir(agent_id)
if await agent_store.get_any(agent_id) is not None or agent_dir.exists():
# Cryptographically improbable, but never overwrite a folder if the
# deployment restored a row without its registry metadata.
raise HTTPException(status_code=409, detail="Generated strategy agent id already exists")
sort_order = payload.sort_order
if sort_order is None:
current = await role_store.list_roles(include_disabled=True)
angle_orders = [int(row["sort_order"]) for row in current if row.get("category") == "angle"]
sort_order = (max(angle_orders) + 1) if angle_orders else 100
soul = (
payload.soul
if payload.soul.strip()
else _default_angle_soul(
name,
payload.description,
)
)
completed = False
try:
await asyncio.to_thread(
agents_router._write_agent_config,
agent_id,
name=name,
description=payload.description,
skills=skills,
model=model,
tool_groups=_DYNAMIC_ANGLE_TOOL_GROUPS,
)
await asyncio.to_thread(agents_router._write_agent_soul, agent_id, soul)
record = await agent_store.ensure_builtin(
{
"id": agent_id,
"name": name,
"description": payload.description,
"published": True,
}
)
await agent_store.replace_skills(agent_id, skills)
await agent_store.set_extras(
agent_id,
tool_groups=_DYNAMIC_ANGLE_TOOL_GROUPS,
model=model,
)
role = await role_store.create_role(
{
"agent_id": agent_id,
"category": "angle",
"fixed": False,
"enabled": True,
"sort_order": sort_order,
}
)
await asyncio.to_thread(_write_role_sidecar, agent_id, role)
completed = True
return await _to_item(role, record, include_config=True)
except asyncio.CancelledError:
raise
except HTTPException:
raise
except Exception as exc:
logger.exception("Failed to create strategy angle agent '%s'", agent_id)
raise HTTPException(
status_code=500,
detail="Failed to create strategy angle agent",
) from exc
finally:
if not completed:
cleanup = asyncio.create_task(_cleanup_failed_create(agent_id, agent_store, role_store))
try:
await asyncio.shield(cleanup)
except asyncio.CancelledError:
# A second cancellation must not leave a half-created global
# agent behind while the file lock is released.
await cleanup
raise
@router.put("/{agent_id}", response_model=StrategyAgentItem)
async def update_strategy_agent(
agent_id: str,
payload: StrategyAgentUpdateRequest,
request: Request,
) -> StrategyAgentItem:
"""Update configuration; fixed roles cannot change lifecycle or category."""
await _require_admin(request)
agent_id = agents_router._validate_storage_id(agent_id)
async with _strategy_mutation_guard(agent_id):
return await _update_strategy_agent_locked(agent_id, payload, request)
async def _update_strategy_agent_locked(
agent_id: str,
payload: StrategyAgentUpdateRequest,
request: Request,
) -> StrategyAgentItem:
role, record = await _get_role_and_agent(request, agent_id)
agent_store = get_agent_store(request)
fields = payload.model_fields_set
if bool(role["fixed"]):
if "enabled" in fields and payload.enabled is not True:
raise HTTPException(status_code=409, detail="Fixed strategy roles cannot be disabled")
if "sort_order" in fields and payload.sort_order != int(role["sort_order"]):
raise HTTPException(status_code=409, detail="Fixed strategy role order cannot be changed")
next_name = agents_router._validate_display_name(payload.name or "") if "name" in fields else str(record["name"])
next_description = payload.description or "" if "description" in fields else str(record.get("description") or "")
extras = await asyncio.to_thread(agents_router._load_config_extras, agent_id)
next_model = _normalize_model(request, payload.model) if "model" in fields else extras["model"]
next_skills = (
await agents_router._validate_strategy_agent_skills(
payload.skills,
app_config=get_config(request),
skill_store=get_skill_store(request),
)
if "skills" in fields
else extras["skills"]
)
agent_dir = get_paths().agent_dir(agent_id)
file_snapshot = await asyncio.to_thread(
lambda: {
"config": _read_optional_text(agent_dir / "config.yaml"),
"soul": _read_optional_text(agent_dir / "SOUL.md"),
"sidecar": _read_optional_text(agent_dir / _ROLE_SIDECAR),
}
)
original_record = dict(record)
original_role = dict(role)
role_store = get_strategy_agent_role_store(request)
try:
config_fields = {"name", "description", "model", "skills"}
if fields & config_fields:
await asyncio.to_thread(
agents_router._write_agent_config,
agent_id,
name=next_name,
description=next_description,
skills=next_skills,
model=next_model,
tool_groups=extras["tool_groups"],
common_questions=extras["common_questions"],
template_name=extras["template_name"],
seat_skill_directive=extras["seat_skill_directive"],
llmwiki_knowledge_base_ids=extras["llmwiki_knowledge_base_ids"],
)
await agent_store.replace_skills(agent_id, next_skills)
await agent_store.set_extras(
agent_id,
tool_groups=extras["tool_groups"],
model=next_model,
)
if "soul" in fields:
await asyncio.to_thread(
agents_router._write_agent_soul,
agent_id,
payload.soul or "",
)
if {"name", "description"} & fields:
updated = await agent_store.update_any(
agent_id,
{"name": next_name, "description": next_description},
)
if updated is None:
raise HTTPException(status_code=404, detail=f"Agent '{agent_id}' not found")
record = updated
enabled = payload.enabled if "enabled" in fields and not bool(role["fixed"]) else None
sort_order = payload.sort_order if "sort_order" in fields and not bool(role["fixed"]) else None
if enabled is not None or sort_order is not None:
updated_role = await role_store.update_role(
agent_id,
enabled=enabled,
sort_order=sort_order,
)
if updated_role is None:
raise HTTPException(status_code=404, detail=f"Strategy agent '{agent_id}' not found")
role = updated_role
if not bool(role["fixed"]):
await asyncio.to_thread(_write_role_sidecar, agent_id, role)
except Exception:
await _rollback_strategy_update(
agent_id=agent_id,
agent_store=agent_store,
role_store=role_store,
record=original_record,
role=original_role,
extras=extras,
file_snapshot=file_snapshot,
)
raise
return await _to_item(role, record, include_config=True)
@router.delete("/{agent_id}", response_model=StrategyAgentItem)
async def disable_strategy_angle(agent_id: str, request: Request) -> StrategyAgentItem:
"""Soft-disable an angle while retaining its agent and historical links."""
await _require_admin(request)
agent_id = agents_router._validate_storage_id(agent_id)
async with _strategy_mutation_guard(agent_id):
return await _disable_strategy_angle_locked(agent_id, request)
async def _disable_strategy_angle_locked(
agent_id: str,
request: Request,
) -> StrategyAgentItem:
role, record = await _get_role_and_agent(request, agent_id)
if bool(role["fixed"]):
raise HTTPException(status_code=409, detail="Fixed strategy roles cannot be deleted")
if role.get("category") != "angle":
raise HTTPException(status_code=409, detail="Only strategy angle roles can be disabled")
updated = await get_strategy_agent_role_store(request).update_role(
agent_id,
enabled=False,
)
if updated is None:
raise HTTPException(status_code=404, detail=f"Strategy agent '{agent_id}' not found")
try:
await asyncio.to_thread(_write_role_sidecar, agent_id, updated)
except Exception:
await get_strategy_agent_role_store(request).update_role(
agent_id,
enabled=bool(role["enabled"]),
sort_order=int(role["sort_order"]),
)
raise
return await _to_item(updated, record, include_config=True)
__all__ = [
"StrategyAgentItem",
"StrategyAgentListResponse",
"StrategyAgentUpdateRequest",
"StrategyAngleCreateRequest",
"create_strategy_angle",
"disable_strategy_angle",
"ensure_strategy_agent_registry",
"list_strategy_agents",
"router",
"update_strategy_agent",
]
@@ -26,6 +26,7 @@ from deerflow.config.system_settings import (
PublishSquare,
PublishSquaresSettings,
SkillCompressionSettings,
StrategyProjectSettings,
get_module_tag_ids,
list_publish_squares,
load_system_settings,
@@ -60,6 +61,14 @@ class OrdinaryKnowledgeLanding(BaseModel):
mode: Literal["wiki", "weknora"] = "wiki"
class StrategyProjectSettingsResponse(BaseModel):
taiwan_strait_demo_enabled: bool = False
class StrategyProjectSettingsUpdateRequest(BaseModel):
taiwan_strait_demo_enabled: bool | None = None
@router.get("/ordinary-knowledge-landing", response_model=OrdinaryKnowledgeLanding)
async def get_ordinary_knowledge_landing() -> OrdinaryKnowledgeLanding:
return OrdinaryKnowledgeLanding(mode=load_system_settings().ordinary_knowledge_landing)
@@ -72,6 +81,35 @@ async def update_ordinary_knowledge_landing(body: OrdinaryKnowledgeLanding, requ
return OrdinaryKnowledgeLanding(mode=saved.ordinary_knowledge_landing)
def _strategy_project_settings_response(
settings: StrategyProjectSettings,
) -> StrategyProjectSettingsResponse:
return StrategyProjectSettingsResponse(
taiwan_strait_demo_enabled=settings.taiwan_strait_demo_enabled,
)
@router.get("/strategy-projects", response_model=StrategyProjectSettingsResponse)
async def get_strategy_project_settings() -> StrategyProjectSettingsResponse:
"""Return project-demo visibility. Any logged-in user may read it."""
return _strategy_project_settings_response(load_system_settings().strategy_projects)
@router.put("/strategy-projects", response_model=StrategyProjectSettingsResponse)
async def update_strategy_project_settings(
body: StrategyProjectSettingsUpdateRequest,
request: Request,
) -> StrategyProjectSettingsResponse:
"""Update project-demo visibility. Admin only."""
await _require_admin(request)
current = load_system_settings()
fields = body.model_dump(exclude_unset=True, exclude_none=True)
updated = current.strategy_projects.model_copy(update=fields)
saved = save_system_settings(current.model_copy(update={"strategy_projects": updated}))
return _strategy_project_settings_response(saved.strategy_projects)
class PromptPrefixUpdateRequest(BaseModel):
enabled: bool | None = None
switch_label: str | None = None