Merge remote-tracking branch 'origin/master' into ji

# Conflicts:
#	frontend-web/src/core/page-layout/sidebar-menu.ts
#	frontend-web/src/core/system-settings/api.ts
#	frontend-web/src/core/system-settings/hooks.ts
#	frontend-web/src/strategy-components/Header.tsx
#	offline-backend-20260512/backend/CLAUDE.md
#	offline-backend-20260512/backend/app/gateway/routers/system_settings.py
#	offline-backend-20260512/backend/packages/harness/deerflow/config/system_settings.py
#	offline-backend-20260512/backend/scripts/sql/column_additions_mysql.sql
This commit is contained in:
Administrator committed 2026-06-16 20:01:42 +08:00
commit b380575f16
103 files changed
+5915 -859

No files matched your search

+5 -1
View File
@@ -265,7 +265,7 @@ The AI-writing graph (`packages/harness/deerflow/agents/ai_writing/`) has four s
Seeding mirrors the roundtable pattern: verified `config.yaml` + `SOUL.md` ship in `app/gateway/routers/_ai_writing_seed_assets/<id>/`; `_ai_writing_seed.py::ensure_ai_writing_functional_agents()` copies them to `.deer-flow/agents/` when missing (never overwrites local edits), called from the startup lifespan (before `_sync_legacy_agents`, so the agents appear in `/api/agents` on first boot) and self-heals from `GET /api/ai-writing/sessions` + `GET /api/ai-writing/article-types`. Tests: `tests/test_ai_writing_seed.py`, `tests/test_ai_writing_agent_runner.py`.
**Skill-driven retrieval (检索类型).** `material_source` has exactly two values: `general` (通用检索, default) and `notebook` (我的空间); legacy `knowledge_base`/`skill` are normalized to `general` by the researcher node. General retrieval is orchestrated from the **skills configured on the `ai-writing-researcher` agent** (`config.yaml → skills`, editable from the agent management page): the built-in `knowledge-base-search` skill (知识库检索) runs the native keyword fan-out web/intranet search (the old default path), while any other configured skill searches with a `description`-targeted query; skills run in configured order with early-stop at `max_materials`, and an empty/unresolvable skills config falls back to the built-in knowledge search so retrieval always works. The skill itself is seeded from `_ai_writing_seed_assets/skills/knowledge-base-search/SKILL.md` into `skills/public/` (never overwrites), and a pre-existing researcher `config.yaml` lacking the `skills` key is patched in place with the default (explicit `skills: []` is respected). **PAUSE-1 input append flow**: when the user types natural language into the material-confirm input (`re_search`/legacy `enable_skill_search` + `user_query`), the node matches the most relevant configured skill via `SkillPicker(candidates=…)` (skipped when only one skill is configured), searches with the user's words, and **appends** the new materials to the existing package (URL-dedup, original order preserved, no LLM re-rank/truncation, 100-item hard cap). Tests: `tests/test_ai_writing_material_source.py`.
**Skill-driven retrieval (检索类型).** `material_source` has exactly two values: `general` (通用检索, default) and `notebook` (我的空间); legacy `knowledge_base`/`skill` are normalized to `general` by the researcher node. General retrieval is orchestrated from the **skills configured on the `ai-writing-researcher` agent** (`config.yaml → skills`, editable from the agent management page): the built-in `knowledge-base-search` skill (知识库检索) runs the native keyword fan-out web/intranet search (the old default path), while any other configured skill searches with a `description`-targeted query; skills run in configured order with early-stop at `max_materials`, and an empty/unresolvable skills config falls back to the built-in knowledge search so retrieval always works. The skill itself is seeded from `_ai_writing_seed_assets/skills/knowledge-base-search/SKILL.md` into `skills/public/` (never overwrites), and a pre-existing researcher `config.yaml` lacking the `skills` key is patched in place with the default (explicit `skills: []` is respected). **PAUSE-1 input append flow**: when the user types natural language into the material-confirm input (`re_search`/legacy `enable_skill_search` + `user_query`), the node matches the most relevant configured skill via `SkillPicker(candidates=…)` (skipped when only one skill is configured), searches with the user's words, and **appends** the new materials to the existing package (URL-dedup, original order preserved, no LLM re-rank/truncation, 100-item hard cap). **Skill-failure fault tolerance**: a configured skill that times out / raises / has all its keyword searches fail (vs. legitimately returning 0 results) no longer just silently moves on — the researcher node emits a `skill_search_step` of type `skill_error` (carrying the error `reason`), and if the configured skills collected **no** new materials at all it runs a `fallback` pass over other available skills (`_fallback_candidate_skills`: the built-in `knowledge-base-search` native path first, then other enabled catalog skills not yet tried) instead of erroring out. The frontend `SkillStepsCard` renders `skill_error` (rose) and `fallback` (amber) so the timeline shows "技能报错 → 改用其他技能搜集". Tests: `tests/test_ai_writing_material_source.py`.
### Admin Leaderboard Snapshots (`/api/admin/users/leaderboard*`)
@@ -338,6 +338,10 @@ Admin-configurable, **access-control-free** categories that published agents/ski
Each agent/skill carries a single `square_id` column (`AgentRow` / `SkillRow`, `String(64)` NOT NULL default `''`, migration `20260611_01`, idempotent SQL in `column_additions_mysql.sql`). One column = at most one square, so an item can never be "published twice". `''` = unassigned ? resolved to the default square at read/filter time. Writes normalize via `normalize_square_id`: agents on create/update (`square_id` field), skills on publish (`PUT /api/skills/custom/{name}/published` accepts optional `square_id`). Listing filters: `GET /api/agents?square=` and `GET /api/skills?square=` keep only that square's items; the **default** square additionally sweeps in legacy `''` rows (`square_is_default`). Tests: `tests/test_publish_squares.py`. Frontend: `core/system-settings` (`usePublishSquares`/`useUpdatePublishSquares`), admin `PublishSquaresSettingsPage` (embedded in the appearance/settings hub), square picker in the agent edit dialog + per-skill card, and a ??-tab square sub-filter in both galleries.
### Agent Module Tag Config (zzJC / zzXD 按类型聚合)
Backs the zzJC / zzXD agent-chat left sidebar (「按类型聚合智能体 + 聊天记录」). An admin picks which agent **tags** belong to a module; the module's left sidebar then shows only agents under those tags (empty/unconfigured = show all visible agents). Stored in `system_settings.json` → `agent_modules` (`AgentModuleSettings`: `modules: {module_key → [tag_id, ...]}`); pure helpers `get_module_tag_ids` / `set_module_tag_ids` (de-dupe + strip). Endpoints (in `app/gateway/routers/system_settings.py`): `GET /api/system-settings/agent-modules/{key}` (readable by any user — the sidebar needs it to filter), `PUT /api/system-settings/agent-modules/{key}` (admin only, full replacement of the tag-id list). Module keys are frontend-defined (`AGENT_MODULES` in `components/workspace/agents/agent-module-sidebar.tsx`, keyed by the module's landing agent id: `6bf`→`zzjc`, `10d69…`→`zzxd`). Tests: `tests/test_agent_modules.py`. Frontend: `core/system-settings` (`useAgentModuleConfig`/`useUpdateAgentModuleConfig`), admin `AgentModuleSettingsDialog` (gear button in the agent-chat header).
### 岗位 (Position) Subscription System
An admin-managed aggregation layer that decides, per user, which agents / skills
@@ -176,6 +176,10 @@ async def langgraph_runtime(app: FastAPI) -> AsyncGenerator[None, None]:
app.state.article_type_store = make_article_type_store(sf)
from deerflow.persistence.research_templates import make_research_template_store
app.state.research_template_store = make_research_template_store(sf)
from deerflow.persistence.memory_config import make_user_memory_config_store
app.state.memory_config_store = make_user_memory_config_store(sf)
@@ -275,7 +279,14 @@ async def langgraph_runtime(app: FastAPI) -> AsyncGenerator[None, None]:
_app = app # 闭包捕获 app,供网关访问 app.state(checkpointer/thread_store)
gateway_factory = lambda p: InProcessRoundtableGateway( # noqa: E731
_app, user_id=p.user_id, agents=p.agents, model=p.model
_app,
user_id=p.user_id,
agents=p.agents,
model=p.model,
# 席位执行模式沿用 Step2 选择,后台席位 run 据此覆盖 policy 的 thinking/reasoning + 开子代理。
seat_thinking_enabled=p.seat_thinking_enabled,
seat_reasoning_effort=p.seat_reasoning_effort,
seat_subagent_enabled=p.seat_subagent_enabled,
)
logger.info("[roundtable-jobs] gateway = inprocess (real models, default)")
@@ -448,6 +459,11 @@ def get_article_type_store(request: Request):
return getattr(request.app.state, "article_type_store", None)
def get_research_template_store(request: Request):
"""Return the research template store, or None when not configured."""
return getattr(request.app.state, "research_template_store", None)
def get_memory_config_store(request: Request):
"""Return the per-user memory config store."""
val = getattr(request.app.state, "memory_config_store", None)
@@ -14,8 +14,10 @@
from __future__ import annotations
import asyncio
import json
import logging
import os
import uuid
from datetime import UTC, datetime
from types import SimpleNamespace
@@ -51,6 +53,33 @@ _SUMMARY_FILE = "方案总结报告.md"
# 这里漏禁了 web_search,导致后台席位 / 报告能联网搜索而前端不能,现由 policy 统一。
# ── 后台作业「按用户限流」(2026-06-16)─────────────────────────────────────────
# 痛点:进 2+ 个后台挂起后,整个网关变慢——建立智能体连接、等待模型响应都慢。
# 根因:① checkpointer 用单连接 aiosqlite(AsyncSqliteSaver,内部一把 asyncio.Lock 串
# 行全进程的 checkpoint 读写);② 后台作业无并发上限——每个作业进程内驱动真实 leader/
# seat/report run,DAG/recommend 的 stage 还用 asyncio.gather 把席位真并行铺开。多个后台
# 作业同时把几十个 agent run 压到同一把 SQLite 锁 + 同一个离线模型端点上,前台实时会商
# 的 checkpoint 读写 / 模型调用只能排队 → 「干啥都慢」。
#
# 解法:给**每个用户**一个并发额度(默认 2),后台每个 agent 轮次(leader/seat/report/
# summary/dashboard)真正开跑前先取一个额度,跑完释放。同一用户的所有后台作业共享这 2 个
# 额度——随便挂多少个作业都能立刻建好,但同一瞬间真正在打模型/写 checkpoint 的后台轮次
# 至多 2 个,不会把共享资源打满。**前台实时路径(multi_agent._leader_run/_special_run、
# 普通聊天)不经过本文件的 `_run_agent_turn`,永不取这个额度,故前台不受限。**
_BACKGROUND_SLOTS_PER_USER = max(1, int(os.getenv("ROUNDTABLE_BACKGROUND_SLOTS_PER_USER", "2") or "2"))
# 按 user_id 缓存 Semaphore。网关全程跑在同一个事件循环上,普通 dict 取/建即可(无跨线程)。
_user_background_sems: dict[str, asyncio.Semaphore] = {}
def _user_background_semaphore(user_id: str) -> asyncio.Semaphore:
"""取该用户的后台并发额度信号量(懒建,每用户一把)。"""
sem = _user_background_sems.get(user_id)
if sem is None:
sem = asyncio.Semaphore(_BACKGROUND_SLOTS_PER_USER)
_user_background_sems[user_id] = sem
return sem
# ── 进程内底层操作 ───────────────────────────────────────────────────────────
@@ -156,6 +185,7 @@ def _tool_calls_since_last_human(messages: list[dict[str, Any]]) -> list[dict[st
async def _run_agent_turn(
app,
*,
user_id: str,
thread_id: str,
agent_name: str,
message: str,
@@ -164,11 +194,16 @@ async def _run_agent_turn(
skill_stop_names: list[str],
thinking_enabled: bool,
reasoning_effort: str,
subagent_enabled: bool = False,
) -> list[dict[str, Any]]:
"""进程内驱动 agent_name 在 thread_id 上跑一轮(发 message),阻塞到完成,返回最终 messages。
镜像 multi_agent ``_run_payload`` 的 body 形状:agent 通过 ``context.agent_name`` 选,
assistant_id 固定 lead_agent;关闭 memory recall / subagent / plan mode(临时研讨任务)。
并发受**该用户的后台额度**约束(``_user_background_semaphore``):真正开跑(start_run +
await run task)前先取额度,跑完释放。同一用户的所有后台作业共享这几个额度,避免 2+
后台挂起把单连接 SQLite checkpointer / 离线模型打满、拖垮前台实时会商。
"""
from app.gateway.routers.thread_runs import RunCreateRequest
from app.gateway.services import start_run
@@ -184,7 +219,8 @@ async def _run_agent_turn(
"reasoning_effort": reasoning_effort,
"thinking_enabled": thinking_enabled,
"is_plan_mode": False,
"subagent_enabled": False,
# ultra 席位才置真 → 解锁 task 子代理工具(席位 policy 未禁 task)。leader 恒 False。
"subagent_enabled": subagent_enabled,
"thread_id": thread_id,
"memory_recall_disabled": True,
# 与 multi_agent._run_payload 对齐:关 builtin 记忆注入,避免单例总控/席位
@@ -199,9 +235,12 @@ async def _run_agent_turn(
# 虚拟 request:带上空 ``state``(auth 中间件在真请求上会写 state.user / state.auth;
# 进程内没有这层,下游凡是读 request.state.X 的代码必须能拿到 None 而不是 AttributeError)。
request = SimpleNamespace(app=app, state=SimpleNamespace(user=None, auth=None))
record = await start_run(body, thread_id, request) # type: ignore[arg-type]
if getattr(record, "task", None) is not None:
await record.task # 阻塞到这轮 agent 跑完,checkpoint 已落盘
# 取该用户的后台额度后再开跑:start_run + await task 是最重的部分(建 agent、调模型、
# 落 checkpoint)。额度满时本轮在此 await 排队,等同用户某个在跑轮次释放额度后再开。
async with _user_background_semaphore(user_id):
record = await start_run(body, thread_id, request) # type: ignore[arg-type]
if getattr(record, "task", None) is not None:
await record.task # 阻塞到这轮 agent 跑完,checkpoint 已落盘
return await _read_thread_messages(app, thread_id)
@@ -262,7 +301,13 @@ def _read_summary_md(app, thread_id: str, user_id: str) -> str | None:
# it all at once」后发出超长 write_file,单次输出被 max_tokens 截断 → 工具调用从未执行、
# run 以 success 收尾但 outputs 下没有 .html → step3.html 回退成开场白文字。
# 自愈:检测产物缺失/截断后,同线程追加纠偏消息(要求 write_file 分段 + append 续写)重试。
_ARTIFACT_RETRY_LIMIT = 2
#
# 取值依据(2026-06-15 修复「后台 HTML 看板只生成一半、概率渲染失败」):模型单轮输出受
# ``config.yaml`` 的 ``max_tokens``(deepseek-chat = 4096)硬截。一份完整看板 HTML 几十 KB
# ≈ 上万 token,单轮写不完;旧值 2(最多 3 轮 ≈ 12k token)对长报告不够 → 落库半截 HTML →
# 渲染失败。调到 5(最多 6 轮 append ≈ 24k token,足够绝大多数看板)显著降低截断概率。
# 循环命中完整产物(``</html>`` / 非空 md / 合法 json)即 break,短报告不会白跑额外轮次。
_ARTIFACT_RETRY_LIMIT = 5
_REPORT_RETRY_MISSING = (
f"检测到 /mnt/user-data/outputs/{_REPORT_FILE} 还没有生成——你上一轮的 write_file 没有执行成功"
@@ -332,11 +377,26 @@ def _build_seat_intro(agents: list[SeatRef]) -> str:
class InProcessRoundtableGateway:
"""进程内真网关。executor 先调 ``init_threads`` 建好线程,再跑编排引擎。"""
def __init__(self, app, *, user_id: str, agents: list[SeatRef], model: str | None = None) -> None:
def __init__(
self,
app,
*,
user_id: str,
agents: list[SeatRef],
model: str | None = None,
seat_thinking_enabled: bool | None = None,
seat_reasoning_effort: str | None = None,
seat_subagent_enabled: bool = False,
) -> None:
self._app = app
self._user_id = user_id
self._agents = list(agents)
self._model = model
# 席位执行模式(沿用 Step2 选择):覆盖 seat policy 的 thinking/reasoning(None → policy 默认),
# subagent 仅 ultra 档为真。只作用 special 席位;leader 派活不受影响。
self._seat_thinking_enabled = seat_thinking_enabled
self._seat_reasoning_effort = seat_reasoning_effort
self._seat_subagent_enabled = seat_subagent_enabled
self._seat_intro = ""
self._first_leader = True
@@ -362,6 +422,7 @@ class InProcessRoundtableGateway:
policy = roundtable_run_policy("leader")
messages = await _run_agent_turn(
self._app,
user_id=self._user_id,
thread_id=thread_ids[coordinator_name],
agent_name=coordinator_name,
message=msg,
@@ -400,16 +461,27 @@ class InProcessRoundtableGateway:
async def run_seat(self, *, thread_ids, agent_id, task, model) -> SeatTurn:
policy = roundtable_run_policy("seat")
# 席位模式覆盖 policy 的 thinking/reasoning(None → policy 默认),与前端 _special_run 一致。
messages = await _run_agent_turn(
self._app,
user_id=self._user_id,
thread_id=thread_ids[agent_id],
agent_name=agent_id,
message=task,
model=model or self._model,
excluded_tools=policy.excluded_tools,
skill_stop_names=policy.skill_stop_names,
thinking_enabled=policy.thinking_enabled,
reasoning_effort=policy.reasoning_effort,
thinking_enabled=(
policy.thinking_enabled
if self._seat_thinking_enabled is None
else self._seat_thinking_enabled
),
reasoning_effort=(
policy.reasoning_effort
if self._seat_reasoning_effort is None
else self._seat_reasoning_effort
),
subagent_enabled=self._seat_subagent_enabled,
)
return SeatTurn(final_text=_latest_ai_text(messages), tool_calls=_tool_calls_since_last_human(messages))
@@ -427,6 +499,7 @@ class InProcessRoundtableGateway:
for attempt in range(1 + _ARTIFACT_RETRY_LIMIT):
messages = await _run_agent_turn(
self._app,
user_id=self._user_id,
thread_id=report_thread_id,
agent_name=REPORT_AGENT_ID,
message=message,
@@ -470,6 +543,7 @@ class InProcessRoundtableGateway:
for attempt in range(1 + _ARTIFACT_RETRY_LIMIT):
messages = await _run_agent_turn(
self._app,
user_id=self._user_id,
thread_id=summary_thread_id,
agent_name=SUMMARY_AGENT_ID,
message=message,
@@ -505,6 +579,7 @@ class InProcessRoundtableGateway:
for attempt in range(1 + _ARTIFACT_RETRY_LIMIT):
messages = await _run_agent_turn(
self._app,
user_id=self._user_id,
thread_id=thread_id,
agent_name=REPORT_AGENT_ID,
message=message,
@@ -179,6 +179,12 @@ class StartParams:
# 是否自动产出 Step3(方案总结报告 + 结果绘制)。默认沿用全局开关(现已恢复 True,
# 见 ROUNDTABLE_BACKGROUND_REPORT_ENABLED)。测试可显式覆盖。
enable_report: bool = ROUNDTABLE_BACKGROUND_REPORT_ENABLED
# 席位执行模式(快速/思考/专业问答/多智能体)派生参数,沿用 Step2 选择。仅 special 席位用:
# 覆盖 roundtable_run_policy("seat") 的 thinking/reasoning(None → policy 默认);
# subagent 仅 ultra 档为真。透传给进程内网关(InProcessRoundtableGateway)的席位 run。
seat_thinking_enabled: bool | None = None
seat_reasoning_effort: str | None = None
seat_subagent_enabled: bool = False
# 网关工厂:给定一次启动参数,返回一个 RoundtableGateway 实例。
@@ -90,6 +90,48 @@ class ArticleTypeResponse(BaseModel):
updated_at: str
class ResearchTemplateRequest(BaseModel):
name: str
description: Optional[str] = None
article_type_key: Optional[str] = None
outline: Optional[str] = None
material_source: str = "general"
word_count: int = 800
audience: Optional[str] = None
strict_mode: bool = False
topic: Optional[str] = None
sort_order: int = 0
class UpdateResearchTemplateRequest(BaseModel):
name: Optional[str] = None
description: Optional[str] = None
article_type_key: Optional[str] = None
outline: Optional[str] = None
material_source: Optional[str] = None
word_count: Optional[int] = None
audience: Optional[str] = None
strict_mode: Optional[bool] = None
topic: Optional[str] = None
sort_order: Optional[int] = None
class ResearchTemplateResponse(BaseModel):
id: str
name: str
description: Optional[str] = None
article_type_key: Optional[str] = None
outline: Optional[str] = None
material_source: str
word_count: int
audience: Optional[str] = None
strict_mode: bool
topic: Optional[str] = None
sort_order: int
created_at: str
updated_at: str
class SessionResponse(BaseModel):
id: str
user_id: Optional[str] = None
@@ -268,6 +310,94 @@ async def delete_article_type(article_type_id: str, request: Request):
return {"success": True}
# ── 研究模板(课题研究左栏一键预填表单,需求 0616 #5)────────────────────────────
@router.get("/research-templates", response_model=list[ResearchTemplateResponse])
async def list_research_templates(request: Request):
"""列出所有研究模板,按 sort_order 升序。
新机器/空表时自动写入内置范例模板(网摘/周报/要讯),幂等仅在表为空时执行。
"""
from app.gateway.deps import get_research_template_store
store = get_research_template_store(request)
if store is None:
return []
try:
await store.seed_defaults()
except Exception:
logger.exception("ai_writing: failed to seed default research templates")
return await store.list()
@router.post("/research-templates", response_model=ResearchTemplateResponse, status_code=201)
async def create_research_template(body: ResearchTemplateRequest, request: Request):
"""新增研究模板。"""
from app.gateway.deps import get_research_template_store
store = get_research_template_store(request)
if store is None:
raise HTTPException(status_code=503, detail="Research template store not available")
return await store.create(
name=body.name,
description=body.description,
article_type_key=body.article_type_key,
outline=body.outline,
material_source=body.material_source,
word_count=body.word_count,
audience=body.audience,
strict_mode=body.strict_mode,
topic=body.topic,
sort_order=body.sort_order,
)
@router.patch("/research-templates/{template_id}", response_model=ResearchTemplateResponse)
async def update_research_template(template_id: str, body: UpdateResearchTemplateRequest, request: Request):
"""更新研究模板字段(只传需要修改的字段)。"""
from app.gateway.deps import get_research_template_store
store = get_research_template_store(request)
if store is None:
raise HTTPException(status_code=503, detail="Research template store not available")
kwargs = {}
for field in (
"name",
"description",
"article_type_key",
"outline",
"material_source",
"word_count",
"audience",
"strict_mode",
"topic",
"sort_order",
):
val = getattr(body, field)
if val is not None:
kwargs[field] = val
updated = await store.update(template_id, **kwargs)
if updated is None:
raise HTTPException(status_code=404, detail="Research template not found")
return updated
@router.delete("/research-templates/{template_id}")
async def delete_research_template(template_id: str, request: Request):
"""删除研究模板。"""
from app.gateway.deps import get_research_template_store
store = get_research_template_store(request)
if store is None:
raise HTTPException(status_code=503, detail="Research template store not available")
deleted = await store.delete(template_id)
if not deleted:
raise HTTPException(status_code=404, detail="Research template not found")
return {"success": True}
# ── 管理接口 ──────────────────────────────────────────────────────────────────
@@ -637,6 +637,8 @@ def _run_payload(
skill_stop_names: list[str],
thinking_enabled: bool = True,
reasoning_effort: str = "medium",
subagent_enabled: bool = False,
stream_custom: bool = False,
multitask_strategy: str = "reject",
extra_context: dict[str, Any] | None = None,
files: list[dict[str, Any]] | None = None,
@@ -676,7 +678,9 @@ def _run_payload(
"reasoning_effort": reasoning_effort,
"thinking_enabled": thinking_enabled,
"is_plan_mode": False,
"subagent_enabled": False,
# 默认 False;圆桌「多智能体问答(ultra)」档会让席位 _special_run 传 True,
# 给席位解锁 task 子代理工具(席位 policy 未禁 task,故可用)。
"subagent_enabled": subagent_enabled,
"thread_id": thread_id,
# 圆桌四个 router(intent / recommend / leader / special)都是临时任务,
# 用户个人记忆与本次研讨无关。MemoryMiddleware 的 Hindsight recall 是
@@ -691,7 +695,12 @@ def _run_payload(
# 圆桌是一次性研讨,本就不该吃历史会话记忆,这里统一关注入根治该污染。
"memory_injection_enabled": False,
},
"stream_mode": ["messages-tuple", "values"],
# ultra 席位会派子智能体(task 工具),其 task_started/running/completed 进度走
# **custom** 流(StreamWriter)。仅这种场景加 "custom",让前端任务卡片能显示子智能体
# 实时进度;其余角色不加,避免无谓帧。
"stream_mode": (
["messages-tuple", "values", "custom"] if stream_custom else ["messages-tuple", "values"]
),
"stream_subgraphs": True,
"stream_resumable": True,
"assistant_id": "lead_agent",
@@ -1570,6 +1579,9 @@ async def _special_run(
no_file: bool = False,
peer_deliveries: list[dict[str, Any]] | None = None,
files: list[dict[str, Any]] | None = None,
thinking_enabled: bool | None = None,
reasoning_effort: str | None = None,
subagent_enabled: bool = False,
) -> AsyncIterator[str | dict[str, Any]]:
"""子智能体(special)单轮:执行总控下发的 task 文本,交付 content。
@@ -1584,6 +1596,12 @@ async def _special_run(
``peer_deliveries``:前序席位的**完整交付** ``[{name, content}, ...]``。注入到
run context 的 ``roundtable_peer_deliveries``,供席位用 ``read_peer_delivery`` 工具
**按需**取全文(平时只看广播摘要,§12.9 传摘要按需传全文)。不进 prompt,不耗 token。
``thinking_enabled`` / ``reasoning_effort`` / ``subagent_enabled``:圆桌第二步「席位
执行模式」(快速/思考/专业问答/多智能体问答)派生的覆盖值。**仅 special 席位**接收:
前两者为 None 时回退到 policy 默认(向后兼容,旧前端不传 → 维持 thinking + medium);
``subagent_enabled`` 仅 ultra 档为 True,给席位解锁 ``task`` 子代理工具。**总控
(leader)不受影响**,始终用其快速派活策略。
"""
t0 = time.perf_counter()
thread_id = agent_threads[agent_name]
@@ -1613,8 +1631,13 @@ async def _special_run(
new_message=new_message,
excluded_tools=seat_excluded,
skill_stop_names=merge_skill_stop_names(policy.skill_stop_names, skill_stop_names),
thinking_enabled=policy.thinking_enabled,
reasoning_effort=policy.reasoning_effort,
# 席位执行模式覆盖 policy 的 thinking/reasoning(None → 回退 policy 默认);
# subagent 仅 ultra 档置真。工具裁剪(excluded_tools)仍完全由 policy 决定。
thinking_enabled=(policy.thinking_enabled if thinking_enabled is None else thinking_enabled),
reasoning_effort=(policy.reasoning_effort if reasoning_effort is None else reasoning_effort),
subagent_enabled=subagent_enabled,
# ultra 席位才会派子智能体 → 才需要 custom 流回传子任务实时进度(task_running 等)。
stream_custom=subagent_enabled,
# 干预/重新派活会把同一席位 thread 再跑一次;抢占掉没结束的旧 run,避免 409。
multitask_strategy="rollback",
# 前序席位完整交付 → run context,供 read_peer_delivery 工具按需取全文(§12.9)。
@@ -1756,6 +1779,15 @@ async def run_multi_agent_stream(request: Request, payload: dict = Body(...)) ->
# 不写死任何默认模型;空串会让 lead_agent._resolve_model_name() 回退到
# config.yaml 的 models[0],与 /api/ai-writing 等其它路由一致。
model_name: str = str(payload.get("model") or "")
# 圆桌第二步「席位执行模式」(快速/思考/专业问答/多智能体问答)派生参数。**仅 special
# 席位消费**:覆盖 seat policy 的 thinking/reasoning。thinking_enabled/reasoning_effort
# 缺省(None/空)→ 后端按 policy 默认执行(向后兼容,旧前端不传维持 thinking + medium);
# subagent_enabled 仅 ultra 档为真,给席位解锁 task 子代理工具。leader 不消费这些字段。
_te_raw = payload.get("thinking_enabled")
seat_thinking_enabled: bool | None = bool(_te_raw) if _te_raw is not None else None
_re_raw = payload.get("reasoning_effort")
seat_reasoning_effort: str | None = _re_raw if isinstance(_re_raw, str) and _re_raw else None
seat_subagent_enabled: bool = bool(payload.get("subagent_enabled") or False)
if not agent_name or agent_name not in agent_threads:
raise HTTPException(
@@ -1803,6 +1835,9 @@ async def run_multi_agent_stream(request: Request, payload: dict = Body(...)) ->
no_file=no_file,
peer_deliveries=peer_deliveries,
files=files,
thinking_enabled=seat_thinking_enabled,
reasoning_effort=seat_reasoning_effort,
subagent_enabled=seat_subagent_enabled,
)
async for chunk in gen:
if isinstance(chunk, dict):
@@ -57,6 +57,8 @@ class ChainResponse(BaseModel):
stage_goals: list[str | None] | None = None
# 写给总控的「编排提示」(dag-only):告诉总控怎么派活、做什么。None/空 = 不注入。
coordinator_prompt: str | None = None
# 默认席位执行模式(flash/thinking/pro/ultra);None = 未设,选入研讨时回退默认(pro)。
seat_mode: str | None = None
# 发布状态:True = 公共(所有账号可在「公共」筛选查看 / 选用)。
is_public: bool = False
# 是否当前用户所有(仅本人可编辑 / 删除 / 发布;公共筛选里别人的链条 is_mine=False)。
@@ -83,6 +85,7 @@ class ChainCreateRequest(BaseModel):
stages: list[list[str]] | None = None
stage_goals: list[str | None] | None = None
coordinator_prompt: str | None = Field(default=None, max_length=4000)
seat_mode: str | None = Field(default=None, max_length=16)
class ChainUpdateRequest(BaseModel):
@@ -93,6 +96,7 @@ class ChainUpdateRequest(BaseModel):
stages: list[list[str]] | None = None
stage_goals: list[str | None] | None = None
coordinator_prompt: str | None = Field(default=None, max_length=4000)
seat_mode: str | None = Field(default=None, max_length=16)
# 发布开关(True 发布为公共 / False 取消发布);仅 owner 可改(update 本就 owner 限定)。
is_public: bool | None = None
@@ -224,6 +228,18 @@ def _validate_stage_goals(
)
_VALID_SEAT_MODES = {"flash", "thinking", "pro", "ultra"}
def _validate_seat_mode(seat_mode: str | None) -> None:
"""席位执行模式只能是 flash/thinking/pro/ultra 或 None(未设)。"""
if seat_mode is not None and seat_mode not in _VALID_SEAT_MODES:
raise HTTPException(
status_code=400,
detail=f"非法席位执行模式: {seat_mode}(应为 {'/'.join(sorted(_VALID_SEAT_MODES))} 之一)",
)
# ── routes ───────────────────────────────────────────────────────────────
@@ -249,6 +265,7 @@ async def create_chain(request: Request, body: ChainCreateRequest) -> ChainRespo
_validate_seats(body.seats)
_validate_stages(body.seats, body.stages)
_validate_stage_goals(body.stages, body.stage_goals)
_validate_seat_mode(body.seat_mode)
store = _get_store(request)
user_id = _current_user_id(request)
row = await store.create_chain(
@@ -261,6 +278,7 @@ async def create_chain(request: Request, body: ChainCreateRequest) -> ChainRespo
"stages": body.stages,
"stage_goals": body.stage_goals,
"coordinator_prompt": body.coordinator_prompt,
"seat_mode": body.seat_mode,
},
)
await _enrich_owner_names([row])
@@ -290,6 +308,7 @@ async def update_chain(request: Request, chain_id: str, body: ChainUpdateRequest
_validate_stages(body.seats, body.stages)
# Goals align to stages; reject goals sent without a layering, enforce length/size.
_validate_stage_goals(body.stages, body.stage_goals)
_validate_seat_mode(body.seat_mode)
store = _get_store(request)
user_id = _current_user_id(request)
# Distinguish "omitted" from "explicitly sent"; only forward provided fields.
@@ -68,6 +68,12 @@ class StartJobRequest(BaseModel):
# 预建好的 thread 映射(Phase 3b 真网关用;模拟网关忽略)。
threadIds: dict[str, str] | None = None
coordinatorName: str = "roundtable-coordinator"
# 席位执行模式(快速/思考/专业问答/多智能体)派生参数,由前端 toStartBody 下发。
# 仅 special 席位用:覆盖 roundtable_run_policy("seat") 的 thinking/reasoning;
# None/缺省 → 后端按 policy 默认(≈pro),向后兼容旧前端。subagent 仅 ultra 档为真。
thinking_enabled: bool | None = None
reasoning_effort: str | None = None
subagent_enabled: bool = False
class ResumeJobRequest(BaseModel):
@@ -219,6 +225,9 @@ async def start_job(request: Request, body: StartJobRequest) -> RoundtableJobRes
draft_id=body.draftId,
chain=body.chain,
orchestration_plan=body.orchestrationPlan,
seat_thinking_enabled=body.thinking_enabled,
seat_reasoning_effort=body.reasoning_effort,
seat_subagent_enabled=body.subagent_enabled,
)
)
return _to_response(row)
@@ -18,10 +18,12 @@ from deerflow.config.system_settings import (
PublishSquaresSettings,
SkillCompressionSettings,
SystemSettings,
get_module_tag_ids,
list_publish_squares,
load_system_settings,
resolve_default_square_id,
save_system_settings,
set_module_tag_ids,
)
logger = logging.getLogger(__name__)
@@ -436,3 +438,35 @@ async def get_log_system(request: Request) -> LogSystemResponse:
await _require_admin(request)
cfg = get_app_config().log_system
return LogSystemResponse(enabled=cfg.enabled, url=cfg.url, label=cfg.label)
class AgentModuleConfigResponse(BaseModel):
"""Selected agent-tag ids for a「按类型聚合」module (zzJC / zzXD)."""
key: str = Field(..., description="Module key, e.g. zzjc / zzxd")
tag_ids: list[str] = Field(default_factory=list, description="Selected agent tag ids")
class AgentModuleConfigUpdateRequest(BaseModel):
tag_ids: list[str] = Field(default_factory=list, description="Full replacement list of selected agent tag ids")
@router.get("/agent-modules/{key}", response_model=AgentModuleConfigResponse)
async def get_agent_module_config(key: str) -> AgentModuleConfigResponse:
"""Return the agent tag ids configured for ``key`` (readable by any user —
the module's left sidebar needs it to filter the agent list)."""
settings = load_system_settings().agent_modules
return AgentModuleConfigResponse(key=key, tag_ids=get_module_tag_ids(settings, key))
@router.put("/agent-modules/{key}", response_model=AgentModuleConfigResponse)
async def update_agent_module_config(
key: str, body: AgentModuleConfigUpdateRequest, request: Request
) -> AgentModuleConfigResponse:
"""Replace the agent tag ids configured for ``key``. Admin only."""
await _require_admin(request)
current = load_system_settings()
updated = set_module_tag_ids(current.agent_modules, key, body.tag_ids)
saved = save_system_settings(current.model_copy(update={"agent_modules": updated}))
return AgentModuleConfigResponse(key=key, tag_ids=get_module_tag_ids(saved.agent_modules, key))
+12 -12
View File
@@ -10,18 +10,18 @@ gateway:
port: 8001
models:
- name: deepseek-chat
display_name: DeepSeek Chat
use: langchain_openai:ChatOpenAI
model: deepseek-chat
api_key: sk-3fee562f834a4c08bb0d471ef44cb445
base_url: https://api.deepseek.com
request_timeout: 600.0
max_retries: 2
max_tokens: 4096
temperature: 1.0
supports_vision: false
supports_thinking: true
# - name: deepseek-chat
# display_name: DeepSeek Chat
# use: langchain_openai:ChatOpenAI
# model: deepseek-chat
# api_key: sk-e1c22b7931dc446f8b67e57878edd105
# base_url: https://api.deepseek.com
# request_timeout: 600.0
# max_retries: 2
# max_tokens: 4096
# temperature: 1.0
# supports_vision: false
# supports_thinking: true
# - name: zai-org/GLM-5-FP8
# display_name: zai-org/GLM-5-FP8
@@ -141,6 +141,30 @@ def _load_configured_skills() -> list:
return resolved or [_fallback_knowledge_skill()]
def _fallback_candidate_skills(tried: set[str]) -> list:
"""容错兜底:从技能目录里挑出**尚未尝试过**的可用技能,内置「知识库检索」优先。
当配置技能调用全部失败、且没收集到任何新素材时,researcher 用这些技能再试一轮,
而不是直接报错。内置知识库检索(原生多关键词并发,最稳)排在最前,其后是技能目录里
其它已启用的技能。技能目录加载失败时只回退内置知识库检索,保证兜底链永不断。
"""
candidates: list = []
seen: set[str] = set(tried)
if KNOWLEDGE_SEARCH_SKILL_NAME not in seen:
candidates.append(_fallback_knowledge_skill())
seen.add(KNOWLEDGE_SEARCH_SKILL_NAME)
try:
from deerflow.skills.storage import get_or_new_skill_storage
for s in get_or_new_skill_storage().load_skills(enabled_only=True):
if getattr(s, "name", None) and s.name not in seen:
candidates.append(s)
seen.add(s.name)
except Exception:
logger.warning("researcher: 兜底加载技能目录失败", exc_info=True)
return candidates
def _resolve_keyword_count(raw: object) -> int:
"""把用户传入的检索关键词数量规整到 [1, _MAX_KEYWORD_COUNT],非法值回退默认 6。"""
try:
@@ -511,24 +535,35 @@ async def researcher_node(state: AIWritingState, config: RunnableConfig) -> dict
added += 1
return added
async def _timed_search(skill_name: str, query: str, limit: int) -> list[SearchResult]:
async def _timed_search(skill_name: str, query: str, limit: int) -> tuple[list[SearchResult], str]:
# 加超时兜底:单个技能检索最多等 _SKILL_SEARCH_TIMEOUT_SECONDS 秒,
# 超时不再阻塞后续技能,避免整段过程看起来"冻结"。
# 返回 (结果, 错误详情):错误详情非空 = 该技能调用失败(触发兜底)。
try:
return await asyncio.wait_for(
results = await asyncio.wait_for(
provider.search(query, max_results=limit),
timeout=_SKILL_SEARCH_TIMEOUT_SECONDS,
) or []
)
return (results or []), ""
except asyncio.TimeoutError:
msg = f"检索超时(>{_SKILL_SEARCH_TIMEOUT_SECONDS}s)"
logger.warning("skill[%s] search timed out after %ss", skill_name, _SKILL_SEARCH_TIMEOUT_SECONDS)
search_errors.append(f"skill[{skill_name}]: 检索超时(>{_SKILL_SEARCH_TIMEOUT_SECONDS}s)")
return []
search_errors.append(f"skill[{skill_name}]: {msg}")
return [], msg
except Exception as e:
logger.warning("skill[%s] search failed: %s", skill_name, e)
search_errors.append(f"skill[{skill_name}]: {e}")
return []
return [], str(e)
for sk in ordered:
def _skill_display(sk) -> str:
return _KNOWLEDGE_SKILL_DISPLAY if sk.name == KNOWLEDGE_SEARCH_SKILL_NAME else sk.name
async def _execute_skill(sk) -> tuple[int, str]:
"""运行单个技能检索,返回 (新增素材数, 错误详情)。
错误详情非空 = 该技能整体调用失败(超时 / 抛异常 / 全部检索词失败),
调用方据此 emit ``skill_error`` 步骤并触发"改用其它技能"的兜底逻辑。
"""
is_knowledge = sk.name == KNOWLEDGE_SEARCH_SKILL_NAME
if is_knowledge and not is_user_query_search:
# 原生知识库检索:对全部检索词并发检索(旧版默认路径)。
@@ -557,13 +592,20 @@ async def researcher_node(state: AIWritingState, config: RunnableConfig) -> dict
gathered = await asyncio.gather(*[_search_one(kw) for kw in search_keywords], return_exceptions=True)
added = 0
errs: list[str] = []
for results in gathered:
if isinstance(results, Exception):
logger.warning("search failed: %s", results)
search_errors.append(f"{type(results).__name__}: {results}")
errs.append(f"{type(results).__name__}: {results}")
continue
added += _collect(results)
search_errors.extend(getattr(provider, "errors", []))
search_errors.extend(errs)
# 全部检索词都抛异常且 0 新增 → 视为该技能整体调用失败,触发兜底
error_detail = ""
if added == 0 and errs and len(errs) == total:
error_detail = ";".join(dict.fromkeys(errs))[:200]
return added, error_detail
else:
# 定向技能检索:user_query 模式优先用用户原话;非知识库技能再
# 附上 description 让 query 贴合该技能的领域。
@@ -576,14 +618,35 @@ async def researcher_node(state: AIWritingState, config: RunnableConfig) -> dict
"message": f"使用技能「{sk.name}」检索:{skill_query[:60]}",
"skill_name": sk.name,
})
added = _collect(await _timed_search(sk.name, skill_query, per_skill))
results, error_detail = await _timed_search(sk.name, skill_query, per_skill)
return _collect(results), error_detail
_emit_skill_step({
"type": "skill_result",
"message": f"技能「{_KNOWLEDGE_SKILL_DISPLAY if is_knowledge else sk.name}」检索到 {added} 条新素材",
"skill_name": sk.name,
"matched_count": added,
})
# 失败技能名(去重后用于兜底提示):记录配置技能里调用失败的那些
failed_skill_names: list[str] = []
tried_names: set[str] = set()
def _emit_skill_outcome(sk, added: int, error_detail: str) -> None:
"""单个技能跑完后,按成功 / 失败 emit 对应步骤事件。"""
if error_detail:
failed_skill_names.append(_skill_display(sk))
_emit_skill_step({
"type": "skill_error",
"message": f"技能「{_skill_display(sk)}」调用失败,尝试使用其他技能检索",
"skill_name": sk.name,
"reason": error_detail,
})
else:
_emit_skill_step({
"type": "skill_result",
"message": f"技能「{_skill_display(sk)}」检索到 {added} 条新素材",
"skill_name": sk.name,
"matched_count": added,
})
for sk in ordered:
added, error_detail = await _execute_skill(sk)
tried_names.add(sk.name)
_emit_skill_outcome(sk, added, error_detail)
# 早停:累积素材已达上限
if len(new_results) >= max_materials:
@@ -594,6 +657,27 @@ async def researcher_node(state: AIWritingState, config: RunnableConfig) -> dict
})
break
# ── 容错兜底:配置技能调用失败且没收集到任何新素材 → 改用其它可用技能继续检索 ──
# (不直接报错;前端时间线靠 skill_error / fallback 步骤展示"技能报错,改用其它技能搜集")
if not new_results and failed_skill_names:
fallback_skills = _fallback_candidate_skills(tried_names)
if fallback_skills:
_emit_skill_step({
"type": "fallback",
"message": (
f"已配置技能({'、'.join(dict.fromkeys(failed_skill_names))})检索失败,"
"改用其他技能继续搜集素材"
),
"skill_names": [_skill_display(s) for s in fallback_skills],
})
for sk in fallback_skills:
added, error_detail = await _execute_skill(sk)
tried_names.add(sk.name)
_emit_skill_outcome(sk, added, error_detail)
if new_results:
# 兜底已拿到素材即止,不再无谓尝试后续技能
break
# 合并去重
# - 首次检索 / 普通 re_search(无输入,用户只想重出):旧素材丢弃,覆盖式替换
# - 素材不足求助 supplement_search:保留旧素材 + 追加新检索结果
@@ -190,6 +190,37 @@ class SkillCompressionSettings(BaseModel):
model_config = ConfigDict(extra="ignore")
class AgentModuleSettings(BaseModel):
"""Admin-configured「按类型聚合」module → selected agent-tag ids.
Keyed by module key (e.g. ``"zzjc"`` / ``"zzxd"``). Each value is the list
of agent **tag ids** whose agents that module's left sidebar shows. A missing
key / empty list means "not configured" — the frontend falls back to showing
all visible agents grouped by tag.
"""
modules: dict[str, list[str]] = Field(
default_factory=dict,
description="module key → selected agent tag ids",
)
model_config = ConfigDict(extra="ignore")
def get_module_tag_ids(settings: AgentModuleSettings, key: str) -> list[str]:
"""Return the configured agent-tag ids for ``key`` (empty when unset)."""
return list(settings.modules.get(key, []))
def set_module_tag_ids(
settings: AgentModuleSettings, key: str, tag_ids: list[str]
) -> AgentModuleSettings:
"""Return a copy with ``key`` set to a de-duplicated, stripped tag-id list."""
cleaned = list(dict.fromkeys([str(t).strip() for t in tag_ids if str(t).strip()]))
modules = dict(settings.modules)
modules[key] = cleaned
return settings.model_copy(update={"modules": modules})
class SystemSettings(BaseModel):
"""Top-level system settings document."""
@@ -200,6 +231,7 @@ class SystemSettings(BaseModel):
citation_display: CitationDisplaySettings = Field(default_factory=CitationDisplaySettings)
leaderboard: LeaderboardSettings = Field(default_factory=LeaderboardSettings)
skill_compression: SkillCompressionSettings = Field(default_factory=SkillCompressionSettings)
agent_modules: AgentModuleSettings = Field(default_factory=AgentModuleSettings)
model_config = ConfigDict(extra="ignore")
@@ -0,0 +1,50 @@
"""Add seat_mode column to roundtable_chains.
Per-chain「默认席位执行模式」: an optional short string (flash/thinking/pro/ultra, see
frontend ``lib/seat-mode.ts``) the user picks in the chain editor. When the chain is
selected into a roundtable discussion the picker defaults to it (still overridable per
run). NULL = unset → callers fall back to the default (pro). VARCHAR(16) on MySQL, plain
String elsewhere — matching ``String(16)`` on ``RoundtableChainRow.seat_mode``.
``create_all`` adds the column for brand-new DBs; the startup ``_ensure_orm_columns_sync``
sweep adds it to already-existing tables (sqlite/mysql/postgres). This revision keeps the
alembic graph consistent with the rest of the chain columns and documents the change.
Revision ID: 20260615_01
Revises: 20260612_01
Create Date: 2026-06-15
"""
from __future__ import annotations
import sqlalchemy as sa
from alembic import op
from sqlalchemy import inspect
revision = "20260615_01"
down_revision = "20260612_01"
branch_labels = None
depends_on = None
def _has_table(table_name: str) -> bool:
inspector = inspect(op.get_bind())
return table_name in inspector.get_table_names()
def _has_column(table_name: str, column_name: str) -> bool:
if not _has_table(table_name):
return False
inspector = inspect(op.get_bind())
return any(col["name"] == column_name for col in inspector.get_columns(table_name))
def upgrade() -> None:
if _has_table("roundtable_chains") and not _has_column("roundtable_chains", "seat_mode"):
op.add_column("roundtable_chains", sa.Column("seat_mode", sa.String(length=16), nullable=True))
def downgrade() -> None:
if _has_column("roundtable_chains", "seat_mode"):
op.drop_column("roundtable_chains", "seat_mode")
@@ -45,6 +45,7 @@ from deerflow.persistence.notifications.model import NotificationRow
from deerflow.persistence.parallel_agent_tasks.model import ParallelAgentTaskRow
from deerflow.persistence.positions.model import PositionRow, PositionTagBindingRow
from deerflow.persistence.recommended_questions.model import RecommendedQuestionRow
from deerflow.persistence.research_templates.model import ResearchTemplateRow
from deerflow.persistence.roundtable_chains.model import RoundtableChainRow
from deerflow.persistence.roundtable_draft_shares.model import RoundtableDraftShareRow
from deerflow.persistence.roundtable_drafts.model import (
@@ -96,6 +97,7 @@ __all__ = [
"PositionRow",
"PositionTagBindingRow",
"RecommendedQuestionRow",
"ResearchTemplateRow",
"RoundtableChainRow",
"RoundtableDraftRow",
"RoundtableDraftShareRow",
@@ -0,0 +1,15 @@
from deerflow.persistence.research_templates.model import ResearchTemplateRow
from deerflow.persistence.research_templates.sql import ResearchTemplateRepository
def make_research_template_store(session_factory) -> ResearchTemplateRepository | None:
if session_factory is None:
return None
return ResearchTemplateRepository(session_factory)
__all__ = [
"ResearchTemplateRow",
"ResearchTemplateRepository",
"make_research_template_store",
]
@@ -0,0 +1,35 @@
"""ORM model for AI writing research templates (课题研究模板).
A research template bundles a set of writing-form presets — article type,
outline, material source, word count, audience, strict mode and a topic/intent —
so a user can one-click pre-fill the whole 课题研究 form. Maintained from the
research-template config page (需求 0616 #5).
"""
from __future__ import annotations
from datetime import UTC, datetime
from sqlalchemy import Boolean, DateTime, Integer, String, Text
from sqlalchemy.orm import Mapped, mapped_column
from deerflow.persistence.base import Base
class ResearchTemplateRow(Base):
__tablename__ = "ai_writing_research_templates"
id: Mapped[str] = mapped_column(String(64), primary_key=True)
name: Mapped[str] = mapped_column(String(128), nullable=False)
description: Mapped[str | None] = mapped_column(Text, nullable=True)
# 预设字段(点击模板时回填表单)。
article_type_key: Mapped[str | None] = mapped_column(String(64), nullable=True)
outline: Mapped[str | None] = mapped_column(Text, nullable=True)
material_source: Mapped[str] = mapped_column(String(32), nullable=False, default="general")
word_count: Mapped[int] = mapped_column(Integer, nullable=False, default=800)
audience: Mapped[str | None] = mapped_column(String(128), nullable=True)
strict_mode: Mapped[bool] = mapped_column(Boolean, nullable=False, default=False)
topic: Mapped[str | None] = mapped_column(Text, nullable=True)
sort_order: Mapped[int] = mapped_column(Integer, nullable=False, default=0)
created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), default=lambda: datetime.now(UTC))
updated_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), default=lambda: datetime.now(UTC))
@@ -0,0 +1,221 @@
"""SQLAlchemy-backed research template storage."""
from __future__ import annotations
import uuid
from datetime import UTC, datetime
from sqlalchemy import select
from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker
from deerflow.persistence.research_templates.model import ResearchTemplateRow
_UNSET = object()
# 新机器内置范例模板(表为空时由 seed_defaults() 写入;作为给用户的范例,内容务求规范好用)。
# article_type_key 取自默认文章类型(news/analysis/feature/report,见 20260517_02 迁移)。
DEFAULT_RESEARCH_TEMPLATES: list[dict] = [
{
"name": "网摘",
"description": "汇编近期网络热点信息,分条摘录核心要点并标注来源,快速掌握全貌",
"article_type_key": "news",
"material_source": "general",
"word_count": 1000,
"audience": "关注热点动态的一般读者",
"strict_mode": False,
"topic": (
"围绕【主题】检索并汇编近一周网络上的重要信息与热点动态,"
"逐条摘录核心事实、关键数据与原文出处,帮助读者快速掌握全貌。"
),
"outline": (
"一、热点速览(用一句话清单列出本期要点)\n"
"二、重点摘录\n"
" 1.【事件/话题标题】——核心事实、关键数据、信息来源\n"
" 2.【事件/话题标题】——……\n"
" 3.(按重要性依次罗列)\n"
"三、关联动态与背景补充(串联相关信息、补足来龙去脉)\n"
"四、小结与观察(提炼趋势或值得关注的看点)"
),
"sort_order": 0,
},
{
"name": "周报",
"description": "按周梳理工作进展、数据指标与问题风险,输出结构化周度汇报",
"article_type_key": "report",
"material_source": "general",
"word_count": 1200,
"audience": "团队成员与上级主管",
"strict_mode": True,
"topic": (
"汇总本周【项目/团队】的工作进展,撰写周报:"
"覆盖关键成果、数据指标、问题风险与下周计划,简明、客观、可量化。"
),
"outline": (
"一、本周概览(一句话总结 + 关键指标对比上周)\n"
"二、重点进展\n"
" 1. 已完成事项与成果(结论先行,附支撑数据)\n"
" 2. 关键数据 / 里程碑达成情况\n"
"三、问题与风险(逐条列出问题、影响面及应对措施)\n"
"四、下周计划与资源需求(目标、负责人、所需支持)"
),
"sort_order": 1,
},
{
"name": "要讯",
"description": "提炼最重要的资讯,精炼成简报式要点,突出核心事实与潜在影响",
"article_type_key": "analysis",
"material_source": "general",
"word_count": 600,
"audience": "需快速掌握要点的决策者",
"strict_mode": True,
"topic": (
"围绕【主题/领域】提炼近期最重要的资讯,编写要讯:"
"每条聚焦核心事实、关键背景与潜在影响,文字精炼、判断审慎。"
),
"outline": (
"一、头条要讯(1–3 条,每条用一句话点明核心)\n"
"二、要点详述(逐条展开)\n"
" · 事实:发生了什么、何时何地、涉及谁\n"
" · 背景:为何重要、与既有态势的关系\n"
" · 影响:可能带来的后果与连锁反应\n"
"三、研判与建议(给出倾向性判断与可行动建议)"
),
"sort_order": 2,
},
]
class ResearchTemplateRepository:
def __init__(self, session_factory: async_sessionmaker[AsyncSession]) -> None:
self._sf = session_factory
@staticmethod
def _row_to_dict(row: ResearchTemplateRow) -> dict:
d = row.to_dict()
for k in ("created_at", "updated_at"):
if isinstance(d.get(k), datetime):
d[k] = d[k].isoformat()
return d
async def create(
self,
*,
name: str,
description: str | None = None,
article_type_key: str | None = None,
outline: str | None = None,
material_source: str = "general",
word_count: int = 800,
audience: str | None = None,
strict_mode: bool = False,
topic: str | None = None,
sort_order: int = 0,
) -> dict:
now = datetime.now(UTC)
row = ResearchTemplateRow(
id=str(uuid.uuid4()),
name=name,
description=description,
article_type_key=article_type_key,
outline=outline,
material_source=material_source,
word_count=word_count,
audience=audience,
strict_mode=strict_mode,
topic=topic,
sort_order=sort_order,
created_at=now,
updated_at=now,
)
async with self._sf() as session:
session.add(row)
await session.commit()
await session.refresh(row)
return self._row_to_dict(row)
async def update(
self,
template_id: str,
*,
name=_UNSET,
description=_UNSET,
article_type_key=_UNSET,
outline=_UNSET,
material_source=_UNSET,
word_count=_UNSET,
audience=_UNSET,
strict_mode=_UNSET,
topic=_UNSET,
sort_order=_UNSET,
) -> dict | None:
async with self._sf() as session:
row = await session.get(ResearchTemplateRow, template_id)
if row is None:
return None
if name is not _UNSET:
row.name = name
if description is not _UNSET:
row.description = description
if article_type_key is not _UNSET:
row.article_type_key = article_type_key
if outline is not _UNSET:
row.outline = outline
if material_source is not _UNSET:
row.material_source = material_source
if word_count is not _UNSET:
row.word_count = word_count
if audience is not _UNSET:
row.audience = audience
if strict_mode is not _UNSET:
row.strict_mode = strict_mode
if topic is not _UNSET:
row.topic = topic
if sort_order is not _UNSET:
row.sort_order = sort_order
row.updated_at = datetime.now(UTC)
await session.commit()
await session.refresh(row)
return self._row_to_dict(row)
async def get(self, template_id: str) -> dict | None:
async with self._sf() as session:
row = await session.get(ResearchTemplateRow, template_id)
if row is None:
return None
return self._row_to_dict(row)
async def list(self) -> list[dict]:
stmt = select(ResearchTemplateRow).order_by(
ResearchTemplateRow.sort_order.asc(), ResearchTemplateRow.created_at.asc()
)
async with self._sf() as session:
result = await session.execute(stmt)
return [self._row_to_dict(r) for r in result.scalars()]
async def delete(self, template_id: str) -> bool:
async with self._sf() as session:
row = await session.get(ResearchTemplateRow, template_id)
if row is None:
return False
await session.delete(row)
await session.commit()
return True
async def seed_defaults(self) -> int:
"""新机器/空表时写入内置范例模板(网摘/周报/要讯)。
幂等:仅当表为空时插入;已有任何模板则跳过(避免覆盖用户配置或重复播种)。
返回插入条数。
"""
async with self._sf() as session:
existing = await session.execute(select(ResearchTemplateRow.id).limit(1))
if existing.first() is not None:
return 0
now = datetime.now(UTC)
for t in DEFAULT_RESEARCH_TEMPLATES:
session.add(
ResearchTemplateRow(id=str(uuid.uuid4()), created_at=now, updated_at=now, **t)
)
await session.commit()
return len(DEFAULT_RESEARCH_TEMPLATES)
@@ -49,6 +49,9 @@ class RoundtableChainRow(Base):
# 怎么派活、做什么(整体编排策略 / 风格 / 约束)。**仅分层 DAG 模式生效**:注入到总控
# 每阶段派活提示(``buildDagStageLeaderTask``)。NULL = 不注入。纯文本,存为 LONGTEXT。
coordinator_prompt: Mapped[str | None] = mapped_column(PortableLongText, nullable=True)
# 可选「默认席位执行模式」:flash/thinking/pro/ultra(见前端 lib/seat-mode.ts)。选入研讨时
# 弹窗默认用它(仍可临时改)。NULL = 未设,调用方回退默认(pro)。短串,VARCHAR(16)。
seat_mode: Mapped[str | None] = mapped_column(String(16), nullable=True)
# 发布开关:True = 公共(所有账号可在「公共」筛选里查看 / 选用);False = 仅本人可见。
# 编辑 / 删除始终只限 owner;发布只是放开「查看 + 选入研讨」。索引以支持「公共」列表筛选。
is_public: Mapped[bool] = mapped_column(
@@ -56,6 +56,8 @@ class RoundtableChainRepository:
"stage_goals": _loads(getattr(row, "stage_goals", None)),
# 总控编排提示(写给总控的派活指引,dag-only);None/空 = 不注入。纯文本,不 json。
"coordinator_prompt": getattr(row, "coordinator_prompt", None),
# 默认席位执行模式(flash/thinking/pro/ultra);None = 未设。
"seat_mode": getattr(row, "seat_mode", None),
# 发布状态 + 是否本人所有(前端据此显示「公共」徽标、决定能否编辑 / 发布)。
"is_public": bool(getattr(row, "is_public", False)),
"is_mine": (current_user_id is None) or (row.user_id == current_user_id),
@@ -110,6 +112,7 @@ class RoundtableChainRepository:
stages=_dumps(data.get("stages")),
stage_goals=_dumps(data.get("stage_goals")),
coordinator_prompt=(data.get("coordinator_prompt") or None),
seat_mode=(data.get("seat_mode") or None),
is_public=bool(data.get("is_public") or False),
created_at=now,
updated_at=now,
@@ -131,6 +134,7 @@ class RoundtableChainRepository:
stages=_UNSET,
stage_goals=_UNSET,
coordinator_prompt=_UNSET,
seat_mode=_UNSET,
is_public=_UNSET,
) -> dict[str, Any] | None:
async with self._sf() as session:
@@ -149,6 +153,8 @@ class RoundtableChainRepository:
row.stage_goals = _dumps(stage_goals)
if coordinator_prompt is not _UNSET:
row.coordinator_prompt = coordinator_prompt or None
if seat_mode is not _UNSET:
row.seat_mode = seat_mode or None
if is_public is not _UNSET and is_public is not None:
row.is_public = bool(is_public)
row.updated_at = datetime.now(UTC)
@@ -85,6 +85,10 @@ CALL _add_column_if_missing('roundtable_drafts', 'task_id', 'VARCHAR(128) NULL D
-- ORM: RoundtableJobRow.orchestration_plan = PortableLongText(LONGTEXT), nullable。
CALL _add_column_if_missing('roundtable_jobs', 'orchestration_plan', 'LONGTEXT NULL DEFAULT NULL');
-- seat_mode: 业务链条「默认席位执行模式」(flash/thinking/pro/ultra),对应 migration 20260615_01。
-- 选入研讨时弹窗默认用它(仍可临时改)。ORM: RoundtableChainRow.seat_mode = String(16), nullable。
CALL _add_column_if_missing('roundtable_chains', 'seat_mode', 'VARCHAR(16) NULL DEFAULT NULL');
-- ---- 岗位 (positions) 订阅系统,对应 migration 20260616_01 -------------------
-- positions / position_tag_bindings 是【新表】,由 create_all 自动建,无需补列。
-- 以下是给【已存在的表】补的列:
@@ -0,0 +1,60 @@
"""Tests for the admin-configured「按类型聚合」module → agent-tag config.
Backs the zzJC / zzXD module left sidebar (需求 0616 #3/#4): an admin picks
which agent tags belong to the module, persisted in ``system_settings.json``.
"""
from __future__ import annotations
import json
from pathlib import Path
import pytest
from deerflow.config import system_settings as ss
@pytest.fixture
def tmp_runtime_home(monkeypatch: pytest.MonkeyPatch, tmp_path: Path) -> Path:
"""Redirect ``runtime_home()`` to a per-test tmp dir."""
def fake_home() -> Path:
return tmp_path
monkeypatch.setattr(ss, "runtime_home", fake_home)
return tmp_path
def test_load_returns_empty_modules_when_missing(tmp_runtime_home: Path) -> None:
settings = ss.load_system_settings()
assert settings.agent_modules.modules == {}
assert ss.get_module_tag_ids(settings.agent_modules, "zzjc") == []
def test_set_module_tag_ids_dedupes_and_strips() -> None:
base = ss.AgentModuleSettings()
updated = ss.set_module_tag_ids(base, "zzjc", [" t1 ", "t2", "t1", "", " "])
assert updated.modules["zzjc"] == ["t1", "t2"]
# Original is untouched (returns a copy).
assert base.modules == {}
def test_set_module_tag_ids_keeps_other_modules() -> None:
base = ss.set_module_tag_ids(ss.AgentModuleSettings(), "zzjc", ["t1"])
both = ss.set_module_tag_ids(base, "zzxd", ["t2", "t3"])
assert ss.get_module_tag_ids(both, "zzjc") == ["t1"]
assert ss.get_module_tag_ids(both, "zzxd") == ["t2", "t3"]
def test_save_then_load_roundtrips(tmp_runtime_home: Path) -> None:
current = ss.load_system_settings()
updated = ss.set_module_tag_ids(current.agent_modules, "zzjc", ["tag-a", "tag-b"])
ss.save_system_settings(current.model_copy(update={"agent_modules": updated}))
file_path = tmp_runtime_home / "system_settings.json"
assert file_path.is_file()
saved = json.loads(file_path.read_text(encoding="utf-8"))
assert saved["agent_modules"]["modules"]["zzjc"] == ["tag-a", "tag-b"]
reloaded = ss.load_system_settings()
assert ss.get_module_tag_ids(reloaded.agent_modules, "zzjc") == ["tag-a", "tag-b"]
@@ -0,0 +1,124 @@
"""Tests for the research-template store (课题研究模板,需求 0616 #5).
A research template bundles writing-form presets (article type / outline /
material source / word count / audience / strict mode / topic) for one-click
pre-fill. Covers create (with defaults) / list ordering / get / partial update /
delete.
"""
from __future__ import annotations
import pytest
import pytest_asyncio
from sqlalchemy.ext.asyncio import async_sessionmaker, create_async_engine
from deerflow.persistence.base import Base
from deerflow.persistence.research_templates import ResearchTemplateRepository
@pytest_asyncio.fixture
async def store(tmp_path):
engine = create_async_engine(f"sqlite+aiosqlite:///{tmp_path / 'rt.db'}")
async with engine.begin() as conn:
import deerflow.persistence.research_templates.model # noqa: F401
await conn.run_sync(Base.metadata.create_all)
sf = async_sessionmaker(engine, expire_on_commit=False)
return ResearchTemplateRepository(sf)
@pytest.mark.asyncio
async def test_create_applies_defaults(store: ResearchTemplateRepository):
row = await store.create(name="深度分析模板")
assert row["name"] == "深度分析模板"
assert row["material_source"] == "general"
assert row["word_count"] == 800
assert row["strict_mode"] is False
assert row["article_type_key"] is None
assert row["id"]
@pytest.mark.asyncio
async def test_create_full_then_get_roundtrips(store: ResearchTemplateRepository):
created = await store.create(
name="舆情研究",
description="围绕舆情的研究模板",
article_type_key="analysis",
outline="一、背景\n二、分析\n三、结论",
material_source="notebook",
word_count=1500,
audience="决策者",
strict_mode=True,
topic="某品牌近期舆情",
sort_order=2,
)
fetched = await store.get(created["id"])
assert fetched is not None
assert fetched["article_type_key"] == "analysis"
assert fetched["outline"].startswith("一、背景")
assert fetched["material_source"] == "notebook"
assert fetched["word_count"] == 1500
assert fetched["audience"] == "决策者"
assert fetched["strict_mode"] is True
assert fetched["topic"] == "某品牌近期舆情"
@pytest.mark.asyncio
async def test_list_orders_by_sort_order(store: ResearchTemplateRepository):
await store.create(name="B", sort_order=2)
await store.create(name="A", sort_order=1)
rows = await store.list()
assert [r["name"] for r in rows] == ["A", "B"]
@pytest.mark.asyncio
async def test_update_partial_keeps_other_fields(store: ResearchTemplateRepository):
created = await store.create(name="模板", word_count=800, strict_mode=False)
updated = await store.update(created["id"], word_count=2000, strict_mode=True)
assert updated is not None
assert updated["word_count"] == 2000
assert updated["strict_mode"] is True
assert updated["name"] == "模板" # untouched
@pytest.mark.asyncio
async def test_update_missing_returns_none(store: ResearchTemplateRepository):
assert await store.update("nope", name="x") is None
@pytest.mark.asyncio
async def test_delete(store: ResearchTemplateRepository):
created = await store.create(name="待删")
assert await store.delete(created["id"]) is True
assert await store.get(created["id"]) is None
assert await store.delete(created["id"]) is False
@pytest.mark.asyncio
async def test_seed_defaults_inserts_builtins_when_empty(store: ResearchTemplateRepository):
inserted = await store.seed_defaults()
assert inserted == 3
rows = await store.list()
names = [r["name"] for r in rows]
assert names == ["网摘", "周报", "要讯"] # sort_order 0/1/2
# 内置模板内容完整(范例质量)。
by_name = {r["name"]: r for r in rows}
assert by_name["网摘"]["article_type_key"] == "news"
assert by_name["周报"]["strict_mode"] is True
assert by_name["要讯"]["outline"]
assert all(r["topic"] for r in rows)
@pytest.mark.asyncio
async def test_seed_defaults_is_idempotent(store: ResearchTemplateRepository):
assert await store.seed_defaults() == 3
# 已有模板 → 再次播种跳过,不重复、不覆盖。
assert await store.seed_defaults() == 0
assert len(await store.list()) == 3
@pytest.mark.asyncio
async def test_seed_defaults_skips_when_user_has_templates(store: ResearchTemplateRepository):
await store.create(name="用户自建")
assert await store.seed_defaults() == 0
assert [r["name"] for r in await store.list()] == ["用户自建"]