feat: 完善策略优选项目工作流
This commit is contained in:
1 parent
a26fd32eba
commit
5d6627af50
45 files changed
+7235
-768
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
|
||||
@@ -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,
|
||||
|
||||
Reference in new issue
Block a user