feat: 报告结构支持检索方向/来源与智能新增,并修复协助卡停轮与宿主跳转
为深度研究报告结构增加检索方向与技能来源、对话式智能新增页与详情排版;澄清中间件在 after_model 可靠结束本轮,避免弱模型出卡后继续生成;任务按钮补充 host 跳转与相关深链能力。 Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
a5a9c952e8
commit
51afebb394
67 files changed
+4511
-699
No files matched your search
+30
@@ -5,14 +5,44 @@ from __future__ import annotations
|
||||
from deerflow.persistence.report_structures.base import ReportStructureStore
|
||||
from deerflow.persistence.report_structures.memory import MemoryReportStructureStore
|
||||
from deerflow.persistence.report_structures.model import ReportStructureRow
|
||||
from deerflow.persistence.report_structures.directions import (
|
||||
MAX_DIRECTION_CHARS,
|
||||
MAX_RETRIEVAL_DIRECTIONS,
|
||||
normalize_retrieval_directions,
|
||||
)
|
||||
from deerflow.persistence.report_structures.skills import (
|
||||
MAX_RETRIEVAL_SKILLS,
|
||||
MAX_SKILL_NAME_CHARS,
|
||||
normalize_retrieval_skills,
|
||||
)
|
||||
from deerflow.persistence.report_structures.modes import (
|
||||
DEFAULT_STRUCTURE_MODE,
|
||||
STRUCTURE_MODE_ADAPTIVE,
|
||||
STRUCTURE_MODE_FIXED,
|
||||
STRUCTURE_MODES,
|
||||
StructureMode,
|
||||
normalize_structure_mode,
|
||||
)
|
||||
from deerflow.persistence.report_structures.sql import ReportStructureRepository
|
||||
|
||||
__all__ = [
|
||||
"DEFAULT_STRUCTURE_MODE",
|
||||
"MemoryReportStructureStore",
|
||||
"ReportStructureRepository",
|
||||
"ReportStructureRow",
|
||||
"ReportStructureStore",
|
||||
"STRUCTURE_MODE_ADAPTIVE",
|
||||
"STRUCTURE_MODE_FIXED",
|
||||
"STRUCTURE_MODES",
|
||||
"StructureMode",
|
||||
"MAX_DIRECTION_CHARS",
|
||||
"MAX_RETRIEVAL_DIRECTIONS",
|
||||
"MAX_RETRIEVAL_SKILLS",
|
||||
"MAX_SKILL_NAME_CHARS",
|
||||
"make_report_structure_store",
|
||||
"normalize_retrieval_directions",
|
||||
"normalize_retrieval_skills",
|
||||
"normalize_structure_mode",
|
||||
]
|
||||
|
||||
|
||||
|
||||
+2
-2
@@ -31,12 +31,12 @@ class ReportStructureStore(ABC):
|
||||
|
||||
@abstractmethod
|
||||
async def create_structure(self, data: dict[str, Any]) -> dict[str, Any]:
|
||||
"""Insert ``{id, title, name, content, created_by}`` (enabled forced True)."""
|
||||
"""Insert ``{id, title, name, content, structure_mode, retrieval_directions, retrieval_skills, created_by}`` (enabled forced True)."""
|
||||
raise NotImplementedError
|
||||
|
||||
@abstractmethod
|
||||
async def update_structure(self, structure_id: str, data: dict[str, Any]) -> dict[str, Any] | None:
|
||||
"""Update any of ``title`` / ``name`` / ``content``. None when missing."""
|
||||
"""Update any of ``title`` / ``name`` / ``content`` / ``structure_mode`` / ``retrieval_directions`` / ``retrieval_skills``. None when missing."""
|
||||
raise NotImplementedError
|
||||
|
||||
@abstractmethod
|
||||
|
||||
+65
@@ -0,0 +1,65 @@
|
||||
"""Normalize the optional 检索方向 list stored on a report structure.
|
||||
|
||||
Kept next to the ORM column so the store / API can round-trip a JSON list
|
||||
without importing the Deep Research runtime. Empty → original collection path.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
from typing import Any
|
||||
|
||||
MAX_RETRIEVAL_DIRECTIONS = 12
|
||||
MAX_DIRECTION_CHARS = 200
|
||||
|
||||
_EMPTY_TOKENS = frozenset({"", "[]", "{}", "null", "none", "undefined"})
|
||||
|
||||
|
||||
def _coerce_listish(value: Any) -> list[Any]:
|
||||
"""Flatten model-produced JSON strings like ``"[]"`` / ``'["a","b"]'`` into a list."""
|
||||
if value is None or isinstance(value, bool):
|
||||
return []
|
||||
if isinstance(value, (list, tuple)):
|
||||
return list(value)
|
||||
if isinstance(value, str):
|
||||
text = value.strip()
|
||||
if text.casefold() in _EMPTY_TOKENS:
|
||||
return []
|
||||
if text[:1] in "[{":
|
||||
try:
|
||||
parsed = json.loads(text)
|
||||
except (json.JSONDecodeError, TypeError, ValueError):
|
||||
return [value]
|
||||
if parsed is None:
|
||||
return []
|
||||
if isinstance(parsed, list):
|
||||
return parsed
|
||||
return []
|
||||
return [value]
|
||||
return []
|
||||
|
||||
|
||||
def normalize_retrieval_directions(value: Any) -> list[str]:
|
||||
"""Return a de-duplicated, bounded list of retrieval-direction strings.
|
||||
|
||||
``None`` / empty / unusable input → ``[]`` (callers keep the original
|
||||
query-planning path). Newlines inside a single item are flattened so the
|
||||
collector prompt and search queries stay one-line.
|
||||
"""
|
||||
parts = _coerce_listish(value)
|
||||
|
||||
seen: set[str] = set()
|
||||
out: list[str] = []
|
||||
for item in parts:
|
||||
text = " ".join(str(item).replace("\x00", "").replace("\r", "").split()).strip()
|
||||
if not text or text.casefold() in _EMPTY_TOKENS:
|
||||
continue
|
||||
text = text[:MAX_DIRECTION_CHARS]
|
||||
key = text.casefold()
|
||||
if key in seen:
|
||||
continue
|
||||
seen.add(key)
|
||||
out.append(text)
|
||||
if len(out) >= MAX_RETRIEVAL_DIRECTIONS:
|
||||
break
|
||||
return out
|
||||
+12
@@ -6,6 +6,9 @@ from datetime import UTC, datetime
|
||||
from typing import Any
|
||||
|
||||
from deerflow.persistence.report_structures.base import ReportStructureStore
|
||||
from deerflow.persistence.report_structures.directions import normalize_retrieval_directions
|
||||
from deerflow.persistence.report_structures.skills import normalize_retrieval_skills
|
||||
from deerflow.persistence.report_structures.modes import normalize_structure_mode
|
||||
|
||||
|
||||
def _now_iso() -> str:
|
||||
@@ -33,6 +36,9 @@ class MemoryReportStructureStore(ReportStructureStore):
|
||||
"title": str(data.get("title") or ""),
|
||||
"name": str(data.get("name") or ""),
|
||||
"content": str(data.get("content") or ""),
|
||||
"structure_mode": normalize_structure_mode(data.get("structure_mode")),
|
||||
"retrieval_directions": normalize_retrieval_directions(data.get("retrieval_directions")),
|
||||
"retrieval_skills": normalize_retrieval_skills(data.get("retrieval_skills")),
|
||||
"enabled": True,
|
||||
"created_by": data.get("created_by"),
|
||||
"created_at": _now_iso(),
|
||||
@@ -48,6 +54,12 @@ class MemoryReportStructureStore(ReportStructureStore):
|
||||
for key in ("title", "name", "content"):
|
||||
if key in data:
|
||||
row[key] = str(data[key])
|
||||
if "structure_mode" in data:
|
||||
row["structure_mode"] = normalize_structure_mode(data["structure_mode"])
|
||||
if "retrieval_directions" in data:
|
||||
row["retrieval_directions"] = normalize_retrieval_directions(data["retrieval_directions"])
|
||||
if "retrieval_skills" in data:
|
||||
row["retrieval_skills"] = normalize_retrieval_skills(data["retrieval_skills"])
|
||||
row["updated_at"] = _now_iso()
|
||||
return dict(row)
|
||||
|
||||
|
||||
+18
-1
@@ -8,6 +8,16 @@ structure the user can pick to fill ``custom_outline``:
|
||||
- ``name`` — 名称, a secondary display label.
|
||||
- ``content`` — 接口内容, the Markdown outline text applied to the research
|
||||
config's ``custom_outline`` when the card is selected.
|
||||
- ``structure_mode`` — 结构约束: ``fixed`` (固定大纲) reproduces the stored
|
||||
headings and hierarchy; ``adaptive`` (弹性大纲) keeps the current
|
||||
material-driven layout. Existing rows default to ``adaptive``.
|
||||
- ``retrieval_directions`` — 检索方向, an optional list of search facets.
|
||||
Empty / unset keeps today's collector query-planning; a non-empty list
|
||||
tells material collection to **split retrievable terms along those
|
||||
directions** (not to search the labels themselves).
|
||||
- ``retrieval_skills`` — 检索来源, an optional list of skill names. Empty /
|
||||
unset keeps today's collector tools; a non-empty list tells material
|
||||
collection to call those skills.
|
||||
- ``enabled`` — 启用/停用; creation always defaults to True (the caller cannot
|
||||
choose it) and a dedicated toggle flips it later.
|
||||
- ``created_by`` — 创建用户名 (audit-only display field).
|
||||
@@ -24,7 +34,7 @@ from sqlalchemy import Boolean, String, Text
|
||||
from sqlalchemy.orm import Mapped, mapped_column
|
||||
|
||||
from deerflow.persistence.base import Base
|
||||
from deerflow.persistence.types import BeijingDateTime
|
||||
from deerflow.persistence.types import BeijingDateTime, PortableJSON
|
||||
|
||||
|
||||
class ReportStructureRow(Base):
|
||||
@@ -37,6 +47,13 @@ class ReportStructureRow(Base):
|
||||
# ``server_default`` 必须显式声明:否则在已存在该表但缺列的旧库上,启动期
|
||||
# ``_ensure_orm_columns_sync`` 会跳过「NOT NULL 且无 server_default」的列(见
|
||||
# engine.py),该列永远补不上 → 查询报 Unknown column → 500。
|
||||
structure_mode: Mapped[str] = mapped_column(
|
||||
String(16), nullable=False, default="adaptive", server_default="adaptive"
|
||||
)
|
||||
# Nullable JSON so ``_ensure_orm_columns_sync`` can ADD COLUMN on existing
|
||||
# tables. ``None`` / missing reads as an empty list at the store boundary.
|
||||
retrieval_directions: Mapped[list | None] = mapped_column(PortableJSON(), default=list, nullable=True)
|
||||
retrieval_skills: Mapped[list | None] = mapped_column(PortableJSON(), default=list, nullable=True)
|
||||
enabled: Mapped[bool] = mapped_column(Boolean, nullable=False, default=True, server_default="1")
|
||||
created_by: Mapped[str | None] = mapped_column(String(128), nullable=True)
|
||||
created_at: Mapped[datetime] = mapped_column(BeijingDateTime(), nullable=False, default=lambda: datetime.now(UTC))
|
||||
|
||||
+25
@@ -0,0 +1,25 @@
|
||||
"""Report-structure writing constraint (结构约束).
|
||||
|
||||
``fixed`` — 固定大纲: the writer must reproduce the stored outline's headings
|
||||
and hierarchy. ``adaptive`` — 弹性大纲: keep the current, material-driven
|
||||
layout (sections without evidence may be merged or omitted).
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Any, Literal
|
||||
|
||||
StructureMode = Literal["fixed", "adaptive"]
|
||||
|
||||
STRUCTURE_MODE_FIXED: StructureMode = "fixed"
|
||||
STRUCTURE_MODE_ADAPTIVE: StructureMode = "adaptive"
|
||||
STRUCTURE_MODES: tuple[StructureMode, ...] = (STRUCTURE_MODE_FIXED, STRUCTURE_MODE_ADAPTIVE)
|
||||
DEFAULT_STRUCTURE_MODE: StructureMode = STRUCTURE_MODE_ADAPTIVE
|
||||
|
||||
|
||||
def normalize_structure_mode(value: Any) -> StructureMode:
|
||||
"""Coerce unknown / legacy values to a supported mode. Default is adaptive."""
|
||||
raw = str(value or "").strip().lower()
|
||||
if raw in {"fixed", "canonical", "prescriptive", "rigid"}:
|
||||
return STRUCTURE_MODE_FIXED
|
||||
return STRUCTURE_MODE_ADAPTIVE
|
||||
+40
@@ -0,0 +1,40 @@
|
||||
"""Normalize the optional 检索技能 list stored on a report structure.
|
||||
|
||||
Skill names (hyphen-case ids) are the retrieval *source*. Empty → original
|
||||
collection path (collector web_search / default Q&A research skills).
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Any
|
||||
|
||||
from deerflow.persistence.report_structures.directions import _EMPTY_TOKENS, _coerce_listish
|
||||
|
||||
MAX_RETRIEVAL_SKILLS = 8
|
||||
MAX_SKILL_NAME_CHARS = 64
|
||||
|
||||
|
||||
def normalize_retrieval_skills(value: Any) -> list[str]:
|
||||
"""Return a de-duplicated, bounded list of skill names.
|
||||
|
||||
``None`` / empty / unusable input → ``[]`` (callers keep the original
|
||||
collection path). Whitespace inside a name is rejected so the collector
|
||||
allowlist and ``skill_view`` lookups stay exact folder ids.
|
||||
"""
|
||||
parts = _coerce_listish(value)
|
||||
|
||||
seen: set[str] = set()
|
||||
out: list[str] = []
|
||||
for item in parts:
|
||||
text = str(item).replace("\x00", "").replace("\r", "").replace("\n", "").strip()
|
||||
if not text or text.casefold() in _EMPTY_TOKENS or any(ch.isspace() for ch in text):
|
||||
continue
|
||||
text = text[:MAX_SKILL_NAME_CHARS]
|
||||
key = text.casefold()
|
||||
if key in seen:
|
||||
continue
|
||||
seen.add(key)
|
||||
out.append(text)
|
||||
if len(out) >= MAX_RETRIEVAL_SKILLS:
|
||||
break
|
||||
return out
|
||||
+14
@@ -11,7 +11,10 @@ from sqlalchemy.exc import IntegrityError
|
||||
from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker
|
||||
|
||||
from deerflow.persistence.report_structures.base import ReportStructureStore
|
||||
from deerflow.persistence.report_structures.directions import normalize_retrieval_directions
|
||||
from deerflow.persistence.report_structures.skills import normalize_retrieval_skills
|
||||
from deerflow.persistence.report_structures.model import ReportStructureRow
|
||||
from deerflow.persistence.report_structures.modes import normalize_structure_mode
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
@@ -22,6 +25,8 @@ def _to_dict(row: ReportStructureRow) -> dict[str, Any]:
|
||||
value = data.get(key)
|
||||
if isinstance(value, datetime):
|
||||
data[key] = value.isoformat()
|
||||
data["retrieval_directions"] = normalize_retrieval_directions(data.get("retrieval_directions"))
|
||||
data["retrieval_skills"] = normalize_retrieval_skills(data.get("retrieval_skills"))
|
||||
return data
|
||||
|
||||
|
||||
@@ -49,6 +54,9 @@ class ReportStructureRepository(ReportStructureStore):
|
||||
title=str(data.get("title") or ""),
|
||||
name=str(data.get("name") or ""),
|
||||
content=str(data.get("content") or ""),
|
||||
structure_mode=normalize_structure_mode(data.get("structure_mode")),
|
||||
retrieval_directions=normalize_retrieval_directions(data.get("retrieval_directions")),
|
||||
retrieval_skills=normalize_retrieval_skills(data.get("retrieval_skills")),
|
||||
# 创建永远默认启用(前端不提供该项填写)。
|
||||
enabled=True,
|
||||
created_by=data.get("created_by"),
|
||||
@@ -69,6 +77,12 @@ class ReportStructureRepository(ReportStructureStore):
|
||||
for key in ("title", "name", "content"):
|
||||
if key in data:
|
||||
setattr(row, key, str(data[key]))
|
||||
if "structure_mode" in data:
|
||||
row.structure_mode = normalize_structure_mode(data["structure_mode"])
|
||||
if "retrieval_directions" in data:
|
||||
row.retrieval_directions = normalize_retrieval_directions(data["retrieval_directions"])
|
||||
if "retrieval_skills" in data:
|
||||
row.retrieval_skills = normalize_retrieval_skills(data["retrieval_skills"])
|
||||
row.updated_at = datetime.now(UTC)
|
||||
await session.commit()
|
||||
await session.refresh(row)
|
||||
|
||||
Reference in new issue
Block a user