feat(square): 新增发布广场功能,智能体/技能按广场分类展示

- 后端新增 PublishSquare/PublishSquaresSettings 配置与读写接口
- agents/skills 增加 square_id 字段及对应迁移脚本
- 前端新增发布广场设置页、技能转移选择器,画廊按广场分 Tab 展示
- lead_agent prompt 及若干持久化层适配调整

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
吉鑫升andClaude Opus 4.8 committed 2026-06-11 11:37:42 +08:00
1 parent 3845d5090b
commit f0aec4cd7f
39 files changed
+1707 -226

No files matched your search

@@ -18,10 +18,10 @@ from app.gateway.auth.password import verify_password_async
from app.gateway.deps import get_agent_store, get_config, get_tag_store
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 list_custom_agents, load_agent_config, load_agent_soul, validate_agent_id
from deerflow.config.agents_config import CommonQuestion, list_custom_agents, load_agent_config, load_agent_soul, validate_agent_id
from deerflow.config.app_config import AppConfig
from deerflow.config.paths import get_paths
from deerflow.config.system_settings import load_system_settings
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.tags import TagStore
from deerflow.runtime.user_context import get_effective_user_id
@@ -42,6 +42,7 @@ class AgentResponse(BaseModel):
owner_username: str | None = Field(default=None, description="Publisher username (email local part). Null for built-in agents.")
published: bool = Field(default=False, description="Whether a user-created agent is public")
is_private: bool = Field(default=False, description="Whether the agent lives in the password-gated private square instead of the public one")
square_id: str = Field(default="", description="Publish square (发布广场) the agent is filed under. Empty string = the default square. Independent of is_private.")
builtin: bool = Field(default=False, description="True when user_id is null")
created_at: datetime | str | None = None
updated_at: datetime | str | None = None
@@ -53,9 +54,9 @@ class AgentResponse(BaseModel):
tool_groups: list[str] | None = None
skills: list[str] | None = None
common_questions: list[str] | None = Field(
common_questions: list[CommonQuestion] | None = Field(
default=None,
description="Frequently-asked questions shown in the agent chat sidebar (常用问题).",
description="Recommended questions shown on the agent chat home / sidebar (常用问题). Each item is {title, prompt}; legacy string lists are coerced.",
)
tags: list[dict] = Field(default_factory=list, description="Tags assigned to this agent")
@@ -103,10 +104,11 @@ class AgentCreateRequest(BaseModel):
soul: str = Field(default="", description="SOUL.md content - agent personality and behavioral guardrails")
published: bool = Field(default=False, description="Whether the new agent should be public")
is_private: bool = Field(default=False, description="When True, the new agent goes into the password-gated private square instead of the public one")
square_id: str | None = Field(default=None, description="Publish square (发布广场) to file the agent under. Omit / empty = the default square.")
skills: list[str] | None = Field(default=None, description="Skill whitelist persisted to the agent's config.yaml")
model: str | None = Field(default=None, description="Optional model override persisted to config.yaml")
tool_groups: list[str] | None = Field(default=None, description="Optional tool group whitelist persisted to config.yaml")
common_questions: list[str] | None = Field(default=None, description="Frequently-asked questions (常用问题) persisted to config.yaml")
common_questions: list[CommonQuestion] | None = Field(default=None, description="Recommended questions (常用问题) persisted to config.yaml. Each item is {title, prompt}; legacy strings are coerced.")
class AgentUpdateRequest(BaseModel):
@@ -117,10 +119,11 @@ class AgentUpdateRequest(BaseModel):
soul: str | None = Field(default=None, description="Updated SOUL.md content")
published: bool | None = Field(default=None, description="Updated publication state")
is_private: bool | None = Field(default=None, description="Move the agent into / out of the private square (true / false)")
square_id: str | None = Field(default=None, description="Publish square (发布广场) to file the agent under. Empty string = the default square.")
skills: list[str] | None = Field(default=None, description="Updated skill whitelist (config.yaml)")
model: str | None = Field(default=None, description="Updated model override (config.yaml)")
tool_groups: list[str] | None = Field(default=None, description="Updated tool group whitelist (config.yaml)")
common_questions: list[str] | None = Field(default=None, description="Updated frequently-asked questions (常用问题, config.yaml)")
common_questions: list[CommonQuestion] | None = Field(default=None, description="Updated recommended questions (常用问题, config.yaml). Each item is {title, prompt}; legacy strings are coerced.")
class AgentSelectorResponse(BaseModel):
@@ -224,7 +227,7 @@ def _write_agent_config(
skills: list[str] | None = None,
model: str | None = None,
tool_groups: list[str] | None = None,
common_questions: list[str] | None = None,
common_questions: list[CommonQuestion] | None = None,
) -> None:
agent_dir = get_paths().agent_dir(agent_id)
agent_dir.mkdir(parents=True, exist_ok=True)
@@ -241,7 +244,16 @@ def _write_agent_config(
if tool_groups is not None:
config_data["tool_groups"] = tool_groups
if common_questions is not None:
config_data["common_questions"] = common_questions
# 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()
]
with open(config_file, "w", encoding="utf-8") as f:
yaml.dump(config_data, f, default_flow_style=False, allow_unicode=True)
@@ -434,6 +446,7 @@ def _record_to_response(
user_id=record.get("user_id"),
published=bool(record.get("published", False)),
is_private=bool(record.get("is_private", False)),
square_id=str(record.get("square_id") or ""),
builtin=record.get("user_id") is None,
created_at=record.get("created_at"),
updated_at=record.get("updated_at"),
@@ -576,6 +589,10 @@ async def list_agents(
default=None,
description="Keep only agents published by this user id (the card's publisher). Built-in agents never match.",
),
square: str | None = Query(
default=None,
description="Keep only agents filed under this publish square (发布广场). Omit for all squares.",
),
admin: bool = Query(
default=False,
description=(
@@ -596,6 +613,11 @@ async def list_agents(
raise HTTPException(status_code=403, detail="Only system admins can list all agents")
paginate = scope is not None or page is not None
square_id = square.strip() if square and square.strip() else None
square_is_default = False
if square_id:
sq = load_system_settings().publish_squares
square_is_default = square_id == resolve_default_square_id(sq)
if paginate:
eff_page = page or 1
rows, total = await store.list_paginated(
@@ -608,6 +630,8 @@ async def list_agents(
admin=admin,
sort=sort,
owner=owner,
square_id=square_id,
square_is_default=square_is_default,
)
responses = await _build_list_responses(store, tag_store, user_id, rows)
return AgentsListResponse(agents=responses, total=total, page=eff_page, page_size=page_size)
@@ -625,6 +649,12 @@ async def list_agents(
responses = [r for r in responses if any(t.get("id") in wanted for t in r.tags)]
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 = _sort_agent_responses(responses, sort)
return AgentsListResponse(agents=responses, total=len(responses), page=1, page_size=len(responses))
except HTTPException:
@@ -754,6 +784,8 @@ async def create_agent_endpoint(request: AgentCreateRequest, http_request: Reque
"user_id": _current_user_id(http_request),
"published": request.published,
"is_private": request.is_private,
# Normalize to a valid configured square id (default when unset).
"square_id": normalize_square_id(request.square_id, load_system_settings().publish_squares),
}
)
# Mirror skills/tool_groups/model into the listing cache so the gallery
@@ -800,6 +832,10 @@ async def update_agent(agent_id: str, request: AgentUpdateRequest, http_request:
update_data["published"] = bool(request.published)
if "is_private" in fields_set:
update_data["is_private"] = bool(request.is_private)
if "square_id" in fields_set:
# Normalize to a valid configured square id (default when unset /
# unknown), so the row never holds a dangling square reference.
update_data["square_id"] = normalize_square_id(request.square_id, load_system_settings().publish_squares)
# Mutual exclusion: an agent moved into the private square cannot
# also be publicly published. Force ``published=false`` whenever the
# same request sets ``is_private=true``, even if the client forgot