sync update from LAN repo (source dhk-1 @ 3e87be55)

This commit is contained in:
gozeros committed 2026-10-21 15:06:08 +08:00
1 parent ab6068a7c1
commit 42f2a26fa8
1482 files changed
+227759 -9584

No files matched your search

@@ -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
@@ -15,7 +17,10 @@ from fastapi import APIRouter, Depends, HTTPException, Query, Request
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_tag_store
from app.gateway.deps import get_agent_store, get_config, get_skill_store, get_tag_store
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
@@ -23,15 +28,39 @@ from deerflow.config.app_config import AppConfig
from deerflow.config.paths import get_paths
from deerflow.config.system_settings import load_system_settings, normalize_square_id, resolve_default_square_id
from deerflow.persistence.agents import AgentStore
from deerflow.persistence.skills import SkillStore
from deerflow.persistence.tags import TagStore
from deerflow.runtime.user_context import get_effective_user_id
from deerflow.skills.cascade import sync_agent_skills_to_available
from deerflow.skills.storage import get_or_new_skill_storage
from deerflow.skills.types import SkillCategory
from deerflow.skills.usage import all_entries as get_skill_usage_entries
logger = logging.getLogger(__name__)
router = APIRouter(prefix="/api", tags=["agents"])
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):
"""Response model for an agent."""
@@ -276,6 +305,121 @@ 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,
*,
app_config: AppConfig,
skill_store: SkillStore,
existing_skills: list[str] | None = None,
) -> list[str] | None:
"""Validate a global strategy role's skill allowlist.
Strategy-selection agents are built-in roles shared by every project and
every user. Their allowlists may therefore reference only canonical skill
names that exist on disk, are enabled/active, and are globally visible:
deployment-owned public skills, explicit built-in rows, or custom skills
currently published by their owner. An update may retain a previously
configured skill that was since disabled/archived, so administrators can
remove it without the rest of the edit being rejected; newly attaching an
unavailable skill remains forbidden. In particular, an administrator
must not be able to attach a different user's unpublished skill merely
because ``?admin=true`` exposes it in the management catalog.
"""
if requested_skills is None:
return None
normalized: list[str] = []
for raw_name in requested_skills:
name = raw_name.strip()
if not name:
raise HTTPException(
status_code=422,
detail="Strategy agent skill names cannot be blank",
)
if name not in normalized:
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")}
usage_entries = get_skill_usage_entries()
except HTTPException:
raise
except Exception as exc: # noqa: BLE001 - stable API response
logger.exception("Failed to validate strategy-agent skill allowlist")
raise HTTPException(status_code=503, detail="Skill catalog is unavailable") from exc
by_name = {skill.name: skill for skill in skills}
unknown = [name for name in normalized if name not in by_name]
if unknown:
raise HTTPException(
status_code=422,
detail=f"Unknown strategy agent skills: {', '.join(unknown)}",
)
retained_names = {str(name).strip() for name in (existing_skills or []) if str(name).strip()}
unavailable = [
name
for name in normalized
if (
not by_name[name].enabled
or str((usage_entries.get(name) or {}).get("state") or "active") != "active"
)
and name not in retained_names
]
if unavailable:
raise HTTPException(
status_code=422,
detail=f"Disabled or archived strategy agent skills: {', '.join(unavailable)}",
)
not_global: list[str] = []
for name in normalized:
skill = by_name[name]
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"))):
continue
not_global.append(name)
if not_global:
raise HTTPException(
status_code=422,
detail=(f"Strategy agent skills must be globally available; unpublished or unregistered custom skills: {', '.join(not_global)}"),
)
return normalized
def _ensure_strategy_agent_deletable(agent_id: str) -> None:
"""Keep fixed strategy role ids alive; their SOUL/skills remain editable."""
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"),
)
def _write_agent_config(
agent_id: str,
*,
@@ -307,24 +451,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]:
@@ -358,7 +497,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:
@@ -413,11 +580,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)
@@ -631,9 +794,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)
@@ -686,11 +847,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()
@@ -744,11 +901,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:
@@ -773,11 +926,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()
@@ -789,12 +938,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:
@@ -853,6 +997,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:
@@ -918,14 +1067,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] = {}
@@ -953,17 +1106,26 @@ 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)
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 "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),
skill_store=get_skill_store(http_request),
)
else:
next_skills = request.skills if "skills" in fields_set else existing_extras["skills"]
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:
@@ -1071,14 +1233,19 @@ 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":
deleted = await store.delete_any(agent_id)
@@ -1382,11 +1549,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."),
)
@@ -1394,11 +1557,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,