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

# Conflicts:
#	.gitignore
#	frontend-web/src/components/workspace/artifacts/context.tsx
#	frontend-web/src/components/workspace/messages/message-group.tsx
#	offline-backend-20260512/backend/CLAUDE.md
#	offline-backend-20260512/backend/README.md
#	offline-backend-20260512/backend/app/gateway/app.py
#	offline-backend-20260512/backend/app/gateway/deps.py
#	offline-backend-20260512/backend/app/gateway/routers/llmwiki.py
This commit is contained in:
吉鑫升 committed 2026-08-24 14:21:19 +08:00
commit e818c24769
262 files changed
+29527 -5236

No files matched your search

@@ -3,16 +3,24 @@
from __future__ import annotations
from deerflow.persistence.compact_menu_overrides.base import CompactMenuOverrideStore
from deerflow.persistence.compact_menu_overrides.keys import (
ALLOWED_COMPACT_MENU_TYPES,
DEFAULT_COMPACT_MENU_TYPE,
normalize_compact_menu_type,
)
from deerflow.persistence.compact_menu_overrides.memory import MemoryCompactMenuOverrideStore
from deerflow.persistence.compact_menu_overrides.model import CompactMenuOverrideRow
from deerflow.persistence.compact_menu_overrides.sql import CompactMenuOverrideRepository
__all__ = [
"ALLOWED_COMPACT_MENU_TYPES",
"CompactMenuOverrideRepository",
"CompactMenuOverrideRow",
"CompactMenuOverrideStore",
"DEFAULT_COMPACT_MENU_TYPE",
"MemoryCompactMenuOverrideStore",
"make_compact_menu_override_store",
"normalize_compact_menu_type",
]
@@ -8,11 +8,17 @@ from typing import Any
class CompactMenuOverrideStore(ABC):
@abstractmethod
async def list_overrides(self) -> list[dict[str, Any]]:
"""Return every stored compact-menu override row."""
async def list_overrides(self, menu_type: str | None = None) -> list[dict[str, Any]]:
"""Return compact-menu override rows for one type (default A)."""
raise NotImplementedError
@abstractmethod
async def replace_all(self, rows: list[dict[str, Any]], *, updated_by: str | None = None) -> list[dict[str, Any]]:
"""Atomically replace the entire compact-menu override set."""
async def replace_all(
self,
rows: list[dict[str, Any]],
*,
menu_type: str | None = None,
updated_by: str | None = None,
) -> list[dict[str, Any]]:
"""Atomically replace the compact-menu override set for one type."""
raise NotImplementedError
@@ -0,0 +1,47 @@
"""Compact-menu type helpers.
Two website deployments can keep independent compact-sidebar layouts.
Type ``A`` is the historical default (existing rows stay as-is). Type ``B``
is stored under a prefixed primary key so it never collides with type A on
databases that still use ``node_id`` as the sole PK.
"""
from __future__ import annotations
DEFAULT_COMPACT_MENU_TYPE = "A"
ALLOWED_COMPACT_MENU_TYPES = ("A", "B")
_STORAGE_SEP = "::"
_TYPE_ALIASES = {
"A": "A",
"1": "A",
"DEFAULT": "A",
"B": "B",
"2": "B",
"ALT": "B",
}
def normalize_compact_menu_type(value: str | None) -> str:
raw = (value or "").strip().upper()
return _TYPE_ALIASES.get(raw, DEFAULT_COMPACT_MENU_TYPE)
def storage_node_id(menu_type: str, node_id: str) -> str:
"""PK value that stays unique across types on a node_id-only primary key."""
t = normalize_compact_menu_type(menu_type)
nid = str(node_id)
if t == DEFAULT_COMPACT_MENU_TYPE:
prefix = f"{t}{_STORAGE_SEP}"
return nid[len(prefix) :] if nid.startswith(prefix) else nid
prefix = f"{t}{_STORAGE_SEP}"
return nid if nid.startswith(prefix) else f"{prefix}{nid}"
def public_node_id(stored: str, menu_type: str | None = None) -> str:
"""Strip the type prefix so API callers always see the real menu node id."""
t = normalize_compact_menu_type(menu_type)
nid = str(stored)
prefix = f"{t}{_STORAGE_SEP}"
if nid.startswith(prefix):
return nid[len(prefix) :]
return nid
@@ -6,12 +6,19 @@ from datetime import UTC, datetime
from typing import Any
from deerflow.persistence.compact_menu_overrides.base import CompactMenuOverrideStore
from deerflow.persistence.compact_menu_overrides.keys import (
normalize_compact_menu_type,
public_node_id,
storage_node_id,
)
def _normalize(data: dict[str, Any], updated_by: str | None) -> dict[str, Any]:
def _normalize(data: dict[str, Any], menu_type: str, updated_by: str | None) -> dict[str, Any]:
placement = str(data.get("placement") or "sidebar")
node_id = public_node_id(str(data["node_id"]), menu_type)
return {
"node_id": str(data["node_id"]),
"node_id": storage_node_id(menu_type, node_id),
"menu_type": menu_type,
"placement": placement if placement in {"sidebar", "settings"} else "sidebar",
"sort_order": int(data.get("sort_order", 0)),
"label": (data.get("label") or None),
@@ -20,18 +27,38 @@ def _normalize(data: dict[str, Any], updated_by: str | None) -> dict[str, Any]:
}
def _public(row: dict[str, Any]) -> dict[str, Any]:
menu_type = normalize_compact_menu_type(row.get("menu_type"))
out = dict(row)
out["menu_type"] = menu_type
out["node_id"] = public_node_id(str(row.get("node_id") or ""), menu_type)
return out
class MemoryCompactMenuOverrideStore(CompactMenuOverrideStore):
def __init__(self) -> None:
self._rows: dict[str, dict[str, Any]] = {}
async def list_overrides(self) -> list[dict[str, Any]]:
return [dict(r) for r in sorted(self._rows.values(), key=lambda r: r.get("sort_order", 0))]
async def list_overrides(self, menu_type: str | None = None) -> list[dict[str, Any]]:
t = normalize_compact_menu_type(menu_type)
rows = [r for r in self._rows.values() if normalize_compact_menu_type(r.get("menu_type")) == t]
return [_public(r) for r in sorted(rows, key=lambda r: r.get("sort_order", 0))]
async def replace_all(self, rows: list[dict[str, Any]], *, updated_by: str | None = None) -> list[dict[str, Any]]:
self._rows = {}
async def replace_all(
self,
rows: list[dict[str, Any]],
*,
menu_type: str | None = None,
updated_by: str | None = None,
) -> list[dict[str, Any]]:
t = normalize_compact_menu_type(menu_type)
self._rows = {
k: v for k, v in self._rows.items() if normalize_compact_menu_type(v.get("menu_type")) != t
}
for r in rows:
node_id = r.get("node_id")
if not node_id:
continue
self._rows[str(node_id)] = _normalize(r, updated_by)
return [dict(r) for r in self._rows.values()]
stored = _normalize(r, t, updated_by)
self._rows[str(stored["node_id"])] = stored
return await self.list_overrides(t)
@@ -8,7 +8,7 @@ from __future__ import annotations
from datetime import UTC, datetime
from sqlalchemy import Integer, String
from sqlalchemy import Index, Integer, String
from sqlalchemy.orm import Mapped, mapped_column
from deerflow.persistence.base import Base
@@ -17,8 +17,10 @@ from deerflow.persistence.types import BeijingDateTime
class CompactMenuOverrideRow(Base):
__tablename__ = "compact_menu_overrides"
__table_args__ = (Index("ix_compact_menu_overrides_menu_type", "menu_type"),)
node_id: Mapped[str] = mapped_column(String(128), primary_key=True)
menu_type: Mapped[str] = mapped_column(String(32), default="A", server_default="A")
placement: Mapped[str] = mapped_column(String(32), default="sidebar")
sort_order: Mapped[int] = mapped_column(Integer, default=0)
label: Mapped[str | None] = mapped_column(String(255))
@@ -5,25 +5,36 @@ from __future__ import annotations
from datetime import datetime
from typing import Any
from sqlalchemy import delete, select
from sqlalchemy import delete, or_, select
from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker
from deerflow.persistence.compact_menu_overrides.base import CompactMenuOverrideStore
from deerflow.persistence.compact_menu_overrides.keys import (
DEFAULT_COMPACT_MENU_TYPE,
normalize_compact_menu_type,
public_node_id,
storage_node_id,
)
from deerflow.persistence.compact_menu_overrides.model import CompactMenuOverrideRow
def _to_dict(row: CompactMenuOverrideRow) -> dict[str, Any]:
data = row.to_dict()
menu_type = normalize_compact_menu_type(data.get("menu_type"))
data["menu_type"] = menu_type
data["node_id"] = public_node_id(str(data.get("node_id") or ""), menu_type)
value = data.get("updated_at")
if isinstance(value, datetime):
data["updated_at"] = value.isoformat()
return data
def _row_from(data: dict[str, Any], updated_by: str | None) -> CompactMenuOverrideRow:
def _row_from(data: dict[str, Any], menu_type: str, updated_by: str | None) -> CompactMenuOverrideRow:
placement = str(data.get("placement") or "sidebar")
node_id = str(data["node_id"])
return CompactMenuOverrideRow(
node_id=str(data["node_id"]),
node_id=storage_node_id(menu_type, node_id),
menu_type=menu_type,
placement=placement if placement in {"sidebar", "settings"} else "sidebar",
sort_order=int(data.get("sort_order", 0)),
label=(data.get("label") or None),
@@ -35,23 +46,42 @@ class CompactMenuOverrideRepository(CompactMenuOverrideStore):
def __init__(self, session_factory: async_sessionmaker[AsyncSession]) -> None:
self._sf = session_factory
async def list_overrides(self) -> list[dict[str, Any]]:
async def list_overrides(self, menu_type: str | None = None) -> list[dict[str, Any]]:
t = normalize_compact_menu_type(menu_type)
stmt = select(CompactMenuOverrideRow).order_by(CompactMenuOverrideRow.sort_order.asc())
stmt = stmt.where(_menu_type_filter(t))
async with self._sf() as session:
result = await session.execute(stmt)
return [_to_dict(row) for row in result.scalars()]
async def replace_all(self, rows: list[dict[str, Any]], *, updated_by: str | None = None) -> list[dict[str, Any]]:
async def replace_all(
self,
rows: list[dict[str, Any]],
*,
menu_type: str | None = None,
updated_by: str | None = None,
) -> list[dict[str, Any]]:
t = normalize_compact_menu_type(menu_type)
by_id: dict[str, dict[str, Any]] = {}
for r in rows:
node_id = r.get("node_id")
if node_id:
by_id[str(node_id)] = r
by_id[public_node_id(str(node_id), t)] = r
async with self._sf() as session:
await session.execute(delete(CompactMenuOverrideRow))
new_rows = [_row_from(r, updated_by) for r in by_id.values()]
await session.execute(delete(CompactMenuOverrideRow).where(_menu_type_filter(t)))
new_rows = [_row_from(r, t, updated_by) for r in by_id.values()]
session.add_all(new_rows)
await session.commit()
for row in new_rows:
await session.refresh(row)
return [_to_dict(row) for row in new_rows]
def _menu_type_filter(menu_type: str):
if menu_type == DEFAULT_COMPACT_MENU_TYPE:
return or_(
CompactMenuOverrideRow.menu_type == menu_type,
CompactMenuOverrideRow.menu_type.is_(None),
CompactMenuOverrideRow.menu_type == "",
)
return CompactMenuOverrideRow.menu_type == menu_type
@@ -68,13 +68,7 @@ class DeepResearchEventRepository:
"""
for attempt in range(max_retries):
async with self._sf() as session:
current_max = (
await session.execute(
select(func.max(DeepResearchEventRow.seq)).where(
DeepResearchEventRow.job_id == job_id
)
)
).scalar() or 0
current_max = (await session.execute(select(func.max(DeepResearchEventRow.seq)).where(DeepResearchEventRow.job_id == job_id))).scalar() or 0
next_seq = int(current_max) + 1
row = DeepResearchEventRow(
job_id=job_id,
@@ -86,49 +80,38 @@ class DeepResearchEventRepository:
)
session.add(row)
try:
# Do not issue ``session.refresh(row)`` after commit. On a
# MySQL read/write-splitting deployment that read can land
# on a lagging replica, which cannot see the just-committed
# event and raises ``Could not refresh instance``. Flush
# assigns generated primary-key/default values on the writer
# connection, so the plain dict is complete before commit.
await session.flush()
result = self._row_to_dict(row)
await session.commit()
await session.refresh(row)
return self._row_to_dict(row)
return result
except IntegrityError:
await session.rollback()
continue # retry with recomputed max
raise RuntimeError(
f"deep_research_events.append: seq collision persisted after {max_retries} retries (job={job_id})"
)
raise RuntimeError(f"deep_research_events.append: seq collision persisted after {max_retries} retries (job={job_id})")
async def list_after(
self, job_id: str, *, after: int = 0, limit: int = 200
) -> list[dict[str, Any]]:
async def list_after(self, job_id: str, *, after: int = 0, limit: int = 200) -> list[dict[str, Any]]:
"""Events with ``seq > after`` in ascending order (SSE replay, §15.3)."""
async with self._sf() as session:
stmt = (
select(DeepResearchEventRow)
.where(DeepResearchEventRow.job_id == job_id)
.where(DeepResearchEventRow.seq > after)
.order_by(DeepResearchEventRow.seq.asc())
.limit(limit)
)
stmt = select(DeepResearchEventRow).where(DeepResearchEventRow.job_id == job_id).where(DeepResearchEventRow.seq > after).order_by(DeepResearchEventRow.seq.asc()).limit(limit)
rows = (await session.execute(stmt)).scalars().all()
return [self._row_to_dict(r) for r in rows]
async def last_seq(self, job_id: str) -> int:
async with self._sf() as session:
mx = (
await session.execute(
select(func.max(DeepResearchEventRow.seq)).where(
DeepResearchEventRow.job_id == job_id
)
)
).scalar()
mx = (await session.execute(select(func.max(DeepResearchEventRow.seq)).where(DeepResearchEventRow.job_id == job_id))).scalar()
return int(mx or 0)
async def delete_by_job(self, job_id: str) -> int:
from sqlalchemy import delete
async with self._sf() as session:
result = await session.execute(
delete(DeepResearchEventRow).where(DeepResearchEventRow.job_id == job_id)
)
result = await session.execute(delete(DeepResearchEventRow).where(DeepResearchEventRow.job_id == job_id))
await session.commit()
return result.rowcount
@@ -136,8 +119,6 @@ class DeepResearchEventRepository:
from sqlalchemy import delete
async with self._sf() as session:
result = await session.execute(
delete(DeepResearchEventRow).where(DeepResearchEventRow.session_id == session_id)
)
result = await session.execute(delete(DeepResearchEventRow).where(DeepResearchEventRow.session_id == session_id))
await session.commit()
return result.rowcount
@@ -113,9 +113,18 @@ class DeepResearchJobRepository:
phase=phase,
)
session.add(row)
# Do not issue ``session.refresh(row)`` after commit. A
# MySQL read/write-splitting proxy can route that read to
# a lagging replica, even though the INSERT has already
# committed on the writer. The caller would receive 500
# while an invisible queued job remains in the database.
# ``flush`` materializes ORM defaults on the writer, so we
# can return a complete, durable snapshot without a
# replica-dependent readback.
await session.flush()
result = self._row_to_dict(row)
await session.commit()
await session.refresh(row)
return self._row_to_dict(row), True
return result, True
except IntegrityError:
# Unique-index conflict → an active job already exists.
pass
@@ -230,13 +239,14 @@ class DeepResearchJobRepository:
)
if result.rowcount == 0:
return None
await session.commit()
row = (
await session.execute(
select(DeepResearchJobRow).where(DeepResearchJobRow.id == job_id)
)
).scalar_one_or_none()
return self._row_to_dict(row) if row else None
snapshot = self._row_to_dict(row) if row else None
await session.commit()
return snapshot
# ── heartbeat ───────────────────────────────────────────────────────────
@@ -351,13 +361,14 @@ class DeepResearchJobRepository:
)
if result.rowcount == 0:
return None
await session.commit()
row = (
await session.execute(
select(DeepResearchJobRow).where(DeepResearchJobRow.id == job_id)
)
).scalar_one_or_none()
return self._row_to_dict(row) if row else None
snapshot = self._row_to_dict(row) if row else None
await session.commit()
return snapshot
# ── two-phase cancel ────────────────────────────────────────────────────
@@ -385,13 +396,14 @@ class DeepResearchJobRepository:
)
if result.rowcount == 0:
return None
await session.commit()
row = (
await session.execute(
select(DeepResearchJobRow).where(DeepResearchJobRow.id == job_id)
)
).scalar_one_or_none()
return self._row_to_dict(row) if row else None
snapshot = self._row_to_dict(row) if row else None
await session.commit()
return snapshot
async def finalize_cancel(
self, job_id: str, *, lease_owner: str | None = None
@@ -419,13 +431,14 @@ class DeepResearchJobRepository:
)
if result.rowcount == 0:
return None
await session.commit()
row = (
await session.execute(
select(DeepResearchJobRow).where(DeepResearchJobRow.id == job_id)
)
).scalar_one_or_none()
return self._row_to_dict(row) if row else None
snapshot = self._row_to_dict(row) if row else None
await session.commit()
return snapshot
# ── resume after human-review interrupt ─────────────────────────────────
@@ -460,13 +473,14 @@ class DeepResearchJobRepository:
)
if result.rowcount == 0:
return None
await session.commit()
row = (
await session.execute(
select(DeepResearchJobRow).where(DeepResearchJobRow.id == job_id)
)
).scalar_one_or_none()
return self._row_to_dict(row) if row else None
snapshot = self._row_to_dict(row) if row else None
await session.commit()
return snapshot
# ── cleanup ─────────────────────────────────────────────────────────────
@@ -74,9 +74,13 @@ class DeepResearchMessageRepository:
seq=int(current_max) + 1,
)
session.add(row)
# Keep the successful append independent of a later replica read.
# The explicit id/seq and Python-side defaults are available after
# flush, before the transaction releases its writer connection.
await session.flush()
snapshot = self._row_to_dict(row)
await session.commit()
await session.refresh(row)
return self._row_to_dict(row)
return snapshot
async def list_by_session(
self, session_id: str, *, user_id: str | _AutoSentinel = AUTO, limit: int = 100
@@ -139,10 +143,11 @@ class DeepResearchMessageRepository:
if not result.rowcount:
await session.rollback()
return None
await session.commit()
stmt = select(DeepResearchMessageRow).where(*where)
row = (await session.execute(stmt)).scalar_one_or_none()
return self._row_to_dict(row) if row is not None else None
snapshot = self._row_to_dict(row) if row is not None else None
await session.commit()
return snapshot
async def delete_by_session(self, session_id: str) -> int:
from sqlalchemy import delete
@@ -85,9 +85,13 @@ class DeepResearchSessionRepository:
runtime_thread_id=runtime_thread_id,
)
session.add(row)
# New sessions are a control-plane write. Avoid a post-commit
# refresh because MySQL read/write-splitting can make a just
# created session look absent on a lagging replica.
await session.flush()
snapshot = self._row_to_dict(row)
await session.commit()
await session.refresh(row)
return self._row_to_dict(row)
return snapshot
# ── read ────────────────────────────────────────────────────────────────
@@ -107,6 +111,7 @@ class DeepResearchSessionRepository:
status: str | None = None,
limit: int = 20,
offset: int = 0,
include_config: bool = False,
) -> list[dict[str, Any]]:
"""Paginated session list for the history panel (excludes heavy report text)."""
resolved = resolve_user_id(user_id, method_name="DeepResearchSessionRepository.list_by_user")
@@ -124,7 +129,8 @@ class DeepResearchSessionRepository:
d = self._row_to_dict(r)
d.pop("report_markdown", None)
d.pop("report_html", None)
d.pop("config_snapshot", None)
if not include_config:
d.pop("config_snapshot", None)
d.pop("plan_snapshot", None)
d.pop("usage_snapshot", None)
d.pop("error", None)
@@ -187,13 +193,14 @@ class DeepResearchSessionRepository:
)
if result.rowcount == 0:
return None
await session.commit()
row = (
await session.execute(
select(DeepResearchSessionRow).where(DeepResearchSessionRow.id == session_id)
)
).scalar_one_or_none()
return self._row_to_dict(row) if row else None
snapshot = self._row_to_dict(row) if row else None
await session.commit()
return snapshot
async def replace_report_if_unchanged(
self,
@@ -231,13 +238,14 @@ class DeepResearchSessionRepository:
)
if result.rowcount == 0:
return None
await session.commit()
row = (
await session.execute(
select(DeepResearchSessionRow).where(DeepResearchSessionRow.id == session_id)
)
).scalar_one_or_none()
return self._row_to_dict(row) if row else None
snapshot = self._row_to_dict(row) if row else None
await session.commit()
return snapshot
# ── delete ──────────────────────────────────────────────────────────────
@@ -23,6 +23,13 @@ from deerflow.persistence.types import BeijingDateTime, PortableLongText
class DeepResearchSourceRow(Base):
__tablename__ = "deep_research_sources"
__table_args__ = (
# MySQL requires a prefix length when indexing TEXT columns. The
# column remains unbounded so long URLs are preserved; the prefix is
# only an index implementation detail and equality filters still
# compare the complete URL value.
sa.Index("ix_deep_research_sources_url", "url", mysql_length=512),
)
id: Mapped[str] = mapped_column(String(64), primary_key=True)
session_id: Mapped[str] = mapped_column(String(64), nullable=False, index=True)
@@ -32,7 +39,7 @@ class DeepResearchSourceRow(Base):
# The query that first surfaced this material.
first_seen_query: Mapped[str | None] = mapped_column(Text, nullable=True)
title: Mapped[str] = mapped_column(Text, nullable=False, default="")
url: Mapped[str | None] = mapped_column(Text, nullable=True, index=True)
url: Mapped[str | None] = mapped_column(Text, nullable=True)
raw_content: Mapped[str] = mapped_column(PortableLongText, nullable=False, default="")
snippet: Mapped[str | None] = mapped_column(Text, nullable=True)
source: Mapped[str] = mapped_column(String(256), nullable=False, default="")
@@ -3,6 +3,7 @@
from __future__ import annotations
import logging
import uuid
from typing import Any
from sqlalchemy import func, select, update
@@ -37,9 +38,14 @@ class DeepResearchSourceRepository:
row = DeepResearchSourceRow(session_id=session_id, job_id=job_id, **fields)
session.add(row)
try:
# The source was produced on the worker's write path. Build
# its response on that same connection so a replica-lag read
# cannot turn a successfully collected source into a failed
# research run.
await session.flush()
snapshot = self._row_to_dict(row)
await session.commit()
await session.refresh(row)
return self._row_to_dict(row)
return snapshot
except Exception:
await session.rollback()
# Fallback: find existing by rec_uuid or url.
@@ -121,6 +127,66 @@ class DeepResearchSourceRepository:
stmt = stmt.where(DeepResearchSourceRow.selected == selected)
return int((await session.execute(stmt)).scalar() or 0)
async def clone_selected_to_session(
self,
source_session_id: str,
target_session_id: str,
*,
user_id: str | _AutoSentinel = AUTO,
) -> dict[str, str]:
"""Copy the selected evidence into a new report session.
Source ids are globally unique primary keys, so a report variant cannot
reuse the parent rows. Return the ``old_id -> new_id`` mapping to let
the caller repair the copied Markdown citation markers before the
variant becomes visible.
"""
resolved = resolve_user_id(
user_id,
method_name="DeepResearchSourceRepository.clone_selected_to_session",
)
async with self._sf() as session:
stmt = select(DeepResearchSourceRow).where(
DeepResearchSourceRow.session_id == source_session_id,
DeepResearchSourceRow.selected.is_(True),
)
if resolved is not None:
stmt = stmt.where(DeepResearchSourceRow.user_id == resolved)
source_rows = (await session.execute(stmt)).scalars().all()
id_map: dict[str, str] = {}
cloned_rows: list[DeepResearchSourceRow] = []
for source in source_rows:
cloned_id = f"drs_src_{uuid.uuid4().hex}"
id_map[source.id] = cloned_id
cloned_rows.append(
DeepResearchSourceRow(
id=cloned_id,
session_id=target_session_id,
job_id=None,
user_id=resolved if resolved is not None else source.user_id,
first_seen_query=source.first_seen_query,
title=source.title,
url=source.url,
raw_content=source.raw_content,
snippet=source.snippet,
source=source.source,
source_type=source.source_type,
published_at=source.published_at,
relevance_score=source.relevance_score,
rec_uuid=source.rec_uuid,
content_hash=source.content_hash,
selected=True,
selection_reason=source.selection_reason,
citation_key=source.citation_key,
)
)
if cloned_rows:
session.add_all(cloned_rows)
await session.flush()
await session.commit()
return id_map
# ── update selection ────────────────────────────────────────────────────
async def set_selected(
@@ -147,13 +213,14 @@ class DeepResearchSourceRepository:
)
if result.rowcount == 0:
return None
await session.commit()
row = (
await session.execute(
select(DeepResearchSourceRow).where(DeepResearchSourceRow.id == source_id)
)
).scalar_one_or_none()
return self._row_to_dict(row) if row else None
snapshot = self._row_to_dict(row) if row else None
await session.commit()
return snapshot
async def delete_by_session(self, session_id: str) -> int:
from sqlalchemy import delete
@@ -54,9 +54,17 @@ class DocumentRewriteVersionRepository:
model_name=model_name,
)
session.add(row)
# Do not refresh after commit. In a read/write-splitting MySQL
# deployment that read can be routed to a lagging replica even
# though the INSERT succeeded on the writer. The caller treats a
# version-record failure as a reason to restore the document, so
# that replica-only error would otherwise undo a valid rewrite.
# Flush materializes the Python/SQLAlchemy defaults on the writer
# and lets us return a complete snapshot without a second query.
await session.flush()
snapshot = self._row_to_dict(row)
await session.commit()
await session.refresh(row)
return self._row_to_dict(row)
return snapshot
async def get(
self,
@@ -0,0 +1,31 @@
"""Enterprise-research task persistence."""
from deerflow.persistence.enterprise_research.base import EnterpriseResearchReportJobStore, EnterpriseResearchTaskStore
from deerflow.persistence.enterprise_research.memory import MemoryEnterpriseResearchReportJobStore, MemoryEnterpriseResearchTaskStore
from deerflow.persistence.enterprise_research.model import EnterpriseResearchReportJobRow, EnterpriseResearchTaskRow
from deerflow.persistence.enterprise_research.sql import EnterpriseResearchReportJobRepository, EnterpriseResearchTaskRepository
__all__ = [
"EnterpriseResearchTaskRow",
"EnterpriseResearchReportJobRow",
"EnterpriseResearchTaskRepository",
"EnterpriseResearchReportJobRepository",
"EnterpriseResearchTaskStore",
"EnterpriseResearchReportJobStore",
"MemoryEnterpriseResearchTaskStore",
"MemoryEnterpriseResearchReportJobStore",
"make_enterprise_research_task_store",
"make_enterprise_research_report_job_store",
]
def make_enterprise_research_task_store(session_factory):
if session_factory is None:
return MemoryEnterpriseResearchTaskStore()
return EnterpriseResearchTaskRepository(session_factory)
def make_enterprise_research_report_job_store(session_factory):
if session_factory is None:
return MemoryEnterpriseResearchReportJobStore()
return EnterpriseResearchReportJobRepository(session_factory)
@@ -0,0 +1,75 @@
"""Persistence contract for the independent enterprise-research workbench."""
from __future__ import annotations
from abc import ABC, abstractmethod
from typing import Any
class EnterpriseResearchTaskStore(ABC):
"""Owner-scoped task storage; evidence snapshots are DeerFlow-owned."""
@abstractmethod
async def list_tasks(self, user_id: str, *, limit: int = 20) -> list[dict[str, Any]]:
raise NotImplementedError
@abstractmethod
async def get_task(self, task_id: str, user_id: str) -> dict[str, Any] | None:
raise NotImplementedError
@abstractmethod
async def create_task(self, data: dict[str, Any]) -> dict[str, Any]:
raise NotImplementedError
@abstractmethod
async def update_task(self, task_id: str, user_id: str, data: dict[str, Any]) -> dict[str, Any] | None:
raise NotImplementedError
@abstractmethod
async def delete_task(self, task_id: str, user_id: str) -> bool:
raise NotImplementedError
class EnterpriseResearchReportJobStore(ABC):
"""Owner-scoped, frozen-input jobs for enterprise report writing."""
@abstractmethod
async def create_job(self, data: dict[str, Any]) -> dict[str, Any]:
raise NotImplementedError
@abstractmethod
async def get_job(self, job_id: str, user_id: str) -> dict[str, Any] | None:
raise NotImplementedError
@abstractmethod
async def get_active_for_task(self, task_id: str, user_id: str) -> dict[str, Any] | None:
raise NotImplementedError
@abstractmethod
async def claim_job(self, job_id: str, user_id: str) -> bool:
"""Atomically move one queued job to running."""
raise NotImplementedError
@abstractmethod
async def requeue_job(self, job_id: str, user_id: str, *, expected_updated_at: str | None) -> bool:
"""Atomically requeue the exact running snapshot seen during recovery."""
raise NotImplementedError
@abstractmethod
async def transition_job(
self, job_id: str, user_id: str, *, expected_statuses: set[str], data: dict[str, Any]
) -> dict[str, Any] | None:
"""Apply a terminal/state transition only from one of the expected states."""
raise NotImplementedError
@abstractmethod
async def update_job(self, job_id: str, user_id: str, data: dict[str, Any]) -> dict[str, Any] | None:
raise NotImplementedError
@abstractmethod
async def list_jobs(self, task_id: str, user_id: str, *, limit: int = 10) -> list[dict[str, Any]]:
raise NotImplementedError
@abstractmethod
async def list_recoverable(self, *, limit: int = 100) -> list[dict[str, Any]]:
raise NotImplementedError
@@ -0,0 +1,172 @@
"""In-memory fallback for enterprise-research tasks."""
from __future__ import annotations
from datetime import UTC, datetime
from typing import Any
from deerflow.persistence.enterprise_research.base import EnterpriseResearchReportJobStore, EnterpriseResearchTaskStore
def _now() -> str:
return datetime.now(UTC).isoformat()
class MemoryEnterpriseResearchTaskStore(EnterpriseResearchTaskStore):
def __init__(self) -> None:
self._rows: dict[str, dict[str, Any]] = {}
async def list_tasks(self, user_id: str, *, limit: int = 20) -> list[dict[str, Any]]:
rows = [dict(row) for row in self._rows.values() if row["user_id"] == user_id]
return sorted(rows, key=lambda row: row["updated_at"], reverse=True)[:limit]
async def get_task(self, task_id: str, user_id: str) -> dict[str, Any] | None:
row = self._rows.get(task_id)
return dict(row) if row is not None and row["user_id"] == user_id else None
async def create_task(self, data: dict[str, Any]) -> dict[str, Any]:
now = _now()
row = {
"id": str(data["id"]),
"user_id": str(data["user_id"]),
"subject": str(data["subject"]),
"focus": str(data.get("focus") or ""),
"template_id": str(data["template_id"]),
"knowledge_base_ids": list(data.get("knowledge_base_ids") or []),
"queries": list(data.get("queries") or []),
"selected_source_ids": list(data.get("selected_source_ids") or []),
"sources": list(data.get("sources") or []),
"plan_markdown": data.get("plan_markdown"),
"report_markdown": data.get("report_markdown"),
"report_summary": data.get("report_summary"),
"report_model_name": data.get("report_model_name"),
"active_report_job_id": data.get("active_report_job_id"),
"status": str(data.get("status") or "draft"),
"warning": data.get("warning"),
"created_at": now,
"updated_at": now,
}
self._rows[row["id"]] = row
return dict(row)
async def update_task(self, task_id: str, user_id: str, data: dict[str, Any]) -> dict[str, Any] | None:
row = self._rows.get(task_id)
if row is None or row["user_id"] != user_id:
return None
for key in (
"status", "warning", "sources", "selected_source_ids", "plan_markdown",
"report_markdown", "report_summary", "report_model_name", "active_report_job_id",
):
if key in data:
row[key] = data[key]
row["updated_at"] = _now()
return dict(row)
async def delete_task(self, task_id: str, user_id: str) -> bool:
row = self._rows.get(task_id)
if row is None or row["user_id"] != user_id:
return False
del self._rows[task_id]
return True
class MemoryEnterpriseResearchReportJobStore(EnterpriseResearchReportJobStore):
def __init__(self) -> None:
self._rows: dict[str, dict[str, Any]] = {}
async def create_job(self, data: dict[str, Any]) -> dict[str, Any]:
now = _now()
status = str(data.get("status") or "queued")
active_dedupe_key = (
f"{data['user_id']}:{data['task_id']}" if status in {"queued", "running"} else None
)
if active_dedupe_key:
existing = next(
(row for row in self._rows.values() if row.get("active_dedupe_key") == active_dedupe_key),
None,
)
if existing is not None:
return dict(existing)
row = {
"id": str(data["id"]), "task_id": str(data["task_id"]), "user_id": str(data["user_id"]),
"status": status, "phase": str(data.get("phase") or "queued"),
"model_name": data.get("model_name"), "action": str(data.get("action") or "initial"),
"active_dedupe_key": active_dedupe_key,
"instruction": data.get("instruction"), "style": data.get("style"),
"target_length": data.get("target_length"), "plan_snapshot": str(data["plan_snapshot"]),
"sources": list(data.get("sources") or []), "report_markdown": str(data.get("report_markdown") or ""),
"original_report": data.get("original_report"),
"report_summary": data.get("report_summary"), "error_message": data.get("error_message"),
"created_at": now, "updated_at": now,
}
self._rows[row["id"]] = row
return dict(row)
async def get_job(self, job_id: str, user_id: str) -> dict[str, Any] | None:
row = self._rows.get(job_id)
return dict(row) if row is not None and row["user_id"] == user_id else None
async def get_active_for_task(self, task_id: str, user_id: str) -> dict[str, Any] | None:
rows = [row for row in self._rows.values() if row["task_id"] == task_id and row["user_id"] == user_id and row["status"] in {"queued", "running"}]
return dict(max(rows, key=lambda row: row["updated_at"])) if rows else None
async def claim_job(self, job_id: str, user_id: str) -> bool:
row = self._rows.get(job_id)
if row is None or row["user_id"] != user_id or row["status"] != "queued":
return False
row["status"] = "running"
row["updated_at"] = _now()
return True
async def requeue_job(self, job_id: str, user_id: str, *, expected_updated_at: str | None) -> bool:
row = self._rows.get(job_id)
if (
row is None
or row["user_id"] != user_id
or row["status"] != "running"
or row.get("updated_at") != expected_updated_at
):
return False
row.update({"status": "queued", "phase": "queued", "report_markdown": "", "error_message": None})
row["updated_at"] = _now()
return True
async def transition_job(
self, job_id: str, user_id: str, *, expected_statuses: set[str], data: dict[str, Any]
) -> dict[str, Any] | None:
row = self._rows.get(job_id)
if row is None or row["user_id"] != user_id or row["status"] not in expected_statuses:
return None
for key in (
"status", "phase", "report_markdown", "report_summary", "error_message", "model_name",
"action", "instruction", "style", "target_length", "original_report",
):
if key in data:
row[key] = data[key]
if data.get("status") in {"completed", "failed", "cancelled"}:
row["active_dedupe_key"] = None
row["updated_at"] = _now()
return dict(row)
async def update_job(self, job_id: str, user_id: str, data: dict[str, Any]) -> dict[str, Any] | None:
row = self._rows.get(job_id)
if row is None or row["user_id"] != user_id:
return None
for key in (
"status", "phase", "report_markdown", "report_summary", "error_message", "model_name",
"action", "instruction", "style", "target_length", "original_report",
):
if key in data:
row[key] = data[key]
if data.get("status") in {"completed", "failed", "cancelled"}:
row["active_dedupe_key"] = None
row["updated_at"] = _now()
return dict(row)
async def list_jobs(self, task_id: str, user_id: str, *, limit: int = 10) -> list[dict[str, Any]]:
rows = [dict(row) for row in self._rows.values() if row["task_id"] == task_id and row["user_id"] == user_id]
return sorted(rows, key=lambda row: row["updated_at"], reverse=True)[:limit]
async def list_recoverable(self, *, limit: int = 100) -> list[dict[str, Any]]:
rows = [dict(row) for row in self._rows.values() if row["status"] in {"queued", "running"}]
return sorted(rows, key=lambda row: row["updated_at"])[:limit]
@@ -0,0 +1,77 @@
"""ORM rows for the independent enterprise-research workbench."""
from __future__ import annotations
from datetime import UTC, datetime
from sqlalchemy import Index, String, Text, UniqueConstraint
from sqlalchemy.orm import Mapped, mapped_column
from deerflow.persistence.base import Base
from deerflow.persistence.types import BeijingDateTime, PortableJSON, PortableLongText
class EnterpriseResearchTaskRow(Base):
"""A user-owned research task plus its bounded WeKnora evidence snapshot."""
__tablename__ = "enterprise_research_tasks"
__table_args__ = (Index("ix_enterprise_research_tasks_owner_updated", "user_id", "updated_at"),)
id: Mapped[str] = mapped_column(String(64), primary_key=True)
user_id: Mapped[str] = mapped_column(String(128), nullable=False, index=True)
subject: Mapped[str] = mapped_column(String(256), nullable=False)
focus: Mapped[str] = mapped_column(Text, nullable=False, default="", server_default="")
template_id: Mapped[str] = mapped_column(String(64), nullable=False)
knowledge_base_ids: Mapped[list[str]] = mapped_column(PortableJSON(), nullable=False, default=list)
queries: Mapped[list[str]] = mapped_column(PortableJSON(), nullable=False, default=list)
# Nullable keeps upgrades safe: the generic schema reconciler can add this
# field to a phase-one table without an unsafe MySQL TEXT default.
selected_source_ids: Mapped[list[str] | None] = mapped_column(PortableJSON(), nullable=True, default=list)
# A WeKnora result can contain a long document chunk. MySQL TEXT is only
# 64 KiB, so evidence uses LONGTEXT through PortableLongText.
sources_json: Mapped[str] = mapped_column(PortableLongText(), nullable=False, default="[]", server_default="[]")
plan_markdown: Mapped[str | None] = mapped_column(PortableLongText(), nullable=True)
report_markdown: Mapped[str | None] = mapped_column(PortableLongText(), nullable=True)
report_summary: Mapped[str | None] = mapped_column(PortableLongText(), nullable=True)
report_model_name: Mapped[str | None] = mapped_column(String(256), nullable=True)
active_report_job_id: Mapped[str | None] = mapped_column(String(64), nullable=True)
status: Mapped[str] = mapped_column(String(32), nullable=False, default="draft", server_default="draft")
warning: Mapped[str | None] = mapped_column(PortableLongText(), nullable=True)
created_at: Mapped[datetime] = mapped_column(BeijingDateTime(), nullable=False, default=lambda: datetime.now(UTC))
updated_at: Mapped[datetime] = mapped_column(
BeijingDateTime(), nullable=False, default=lambda: datetime.now(UTC), onupdate=lambda: datetime.now(UTC)
)
class EnterpriseResearchReportJobRow(Base):
"""A report job whose plan/evidence are frozen when the user starts it."""
__tablename__ = "enterprise_research_report_jobs"
__table_args__ = (
Index("ix_enterprise_research_report_jobs_task_owner_updated", "task_id", "user_id", "updated_at"),
UniqueConstraint("active_dedupe_key", name="uq_enterprise_research_report_jobs_active"),
)
id: Mapped[str] = mapped_column(String(64), primary_key=True)
task_id: Mapped[str] = mapped_column(String(64), nullable=False, index=True)
user_id: Mapped[str] = mapped_column(String(128), nullable=False, index=True)
status: Mapped[str] = mapped_column(String(32), nullable=False, default="queued", server_default="queued")
phase: Mapped[str] = mapped_column(String(32), nullable=False, default="queued", server_default="queued")
model_name: Mapped[str | None] = mapped_column(String(256), nullable=True)
# Present only for queued/running rows. NULL terminal rows keep unlimited
# history while this key rejects cross-process duplicate starts.
active_dedupe_key: Mapped[str | None] = mapped_column(String(256), nullable=True)
action: Mapped[str | None] = mapped_column(String(32), nullable=True, default="initial")
instruction: Mapped[str | None] = mapped_column(PortableLongText(), nullable=True)
style: Mapped[str | None] = mapped_column(String(32), nullable=True)
target_length: Mapped[int | None] = mapped_column(nullable=True)
plan_snapshot: Mapped[str] = mapped_column(PortableLongText(), nullable=False)
sources_json: Mapped[str] = mapped_column(PortableLongText(), nullable=False)
original_report: Mapped[str | None] = mapped_column(PortableLongText(), nullable=True)
report_markdown: Mapped[str] = mapped_column(PortableLongText(), nullable=False, default="", server_default="")
report_summary: Mapped[str | None] = mapped_column(PortableLongText(), nullable=True)
error_message: Mapped[str | None] = mapped_column(PortableLongText(), nullable=True)
created_at: Mapped[datetime] = mapped_column(BeijingDateTime(), nullable=False, default=lambda: datetime.now(UTC))
updated_at: Mapped[datetime] = mapped_column(
BeijingDateTime(), nullable=False, default=lambda: datetime.now(UTC), onupdate=lambda: datetime.now(UTC)
)
@@ -0,0 +1,320 @@
"""SQLAlchemy store for enterprise-research tasks."""
from __future__ import annotations
import json
from datetime import UTC, datetime
from typing import Any
from sqlalchemy import delete, select, update
from sqlalchemy.exc import IntegrityError
from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker
from deerflow.persistence.enterprise_research.base import EnterpriseResearchReportJobStore, EnterpriseResearchTaskStore
from deerflow.persistence.enterprise_research.model import EnterpriseResearchReportJobRow, EnterpriseResearchTaskRow
def _to_dict(row: EnterpriseResearchTaskRow) -> dict[str, Any]:
try:
sources = json.loads(row.sources_json or "[]")
except (TypeError, ValueError):
sources = []
data: dict[str, Any] = {
"id": row.id,
"user_id": row.user_id,
"subject": row.subject,
"focus": row.focus,
"template_id": row.template_id,
"knowledge_base_ids": list(row.knowledge_base_ids or []),
"queries": list(row.queries or []),
"selected_source_ids": list(row.selected_source_ids or []),
"sources": sources if isinstance(sources, list) else [],
"plan_markdown": row.plan_markdown,
"report_markdown": row.report_markdown,
"report_summary": row.report_summary,
"report_model_name": row.report_model_name,
"active_report_job_id": row.active_report_job_id,
"status": row.status,
"warning": row.warning,
"created_at": row.created_at.isoformat() if row.created_at else None,
"updated_at": row.updated_at.isoformat() if row.updated_at else None,
}
return data
class EnterpriseResearchTaskRepository(EnterpriseResearchTaskStore):
def __init__(self, session_factory: async_sessionmaker[AsyncSession]) -> None:
self._sf = session_factory
async def list_tasks(self, user_id: str, *, limit: int = 20) -> list[dict[str, Any]]:
stmt = (
select(EnterpriseResearchTaskRow)
.where(EnterpriseResearchTaskRow.user_id == user_id)
.order_by(EnterpriseResearchTaskRow.updated_at.desc())
.limit(limit)
)
async with self._sf() as session:
result = await session.execute(stmt)
return [_to_dict(row) for row in result.scalars()]
async def get_task(self, task_id: str, user_id: str) -> dict[str, Any] | None:
stmt = select(EnterpriseResearchTaskRow).where(
EnterpriseResearchTaskRow.id == task_id,
EnterpriseResearchTaskRow.user_id == user_id,
)
async with self._sf() as session:
row = (await session.execute(stmt)).scalar_one_or_none()
return _to_dict(row) if row is not None else None
async def create_task(self, data: dict[str, Any]) -> dict[str, Any]:
now = datetime.now(UTC)
row = EnterpriseResearchTaskRow(
id=str(data["id"]),
user_id=str(data["user_id"]),
subject=str(data["subject"]),
focus=str(data.get("focus") or ""),
template_id=str(data["template_id"]),
knowledge_base_ids=list(data.get("knowledge_base_ids") or []),
queries=list(data.get("queries") or []),
selected_source_ids=list(data.get("selected_source_ids") or []),
sources_json=json.dumps(list(data.get("sources") or []), ensure_ascii=False),
plan_markdown=data.get("plan_markdown"),
report_markdown=data.get("report_markdown"),
report_summary=data.get("report_summary"),
report_model_name=data.get("report_model_name"),
active_report_job_id=data.get("active_report_job_id"),
status=str(data.get("status") or "draft"),
warning=data.get("warning"),
created_at=now,
updated_at=now,
)
async with self._sf() as session:
session.add(row)
# Do not refresh after commit: a MySQL read/write split can route it
# to a lagging replica. The flushed in-memory row is authoritative.
await session.flush()
payload = _to_dict(row)
await session.commit()
return payload
async def update_task(self, task_id: str, user_id: str, data: dict[str, Any]) -> dict[str, Any] | None:
stmt = select(EnterpriseResearchTaskRow).where(
EnterpriseResearchTaskRow.id == task_id,
EnterpriseResearchTaskRow.user_id == user_id,
)
async with self._sf() as session:
row = (await session.execute(stmt)).scalar_one_or_none()
if row is None:
return None
if "status" in data:
row.status = str(data["status"])
if "warning" in data:
row.warning = data["warning"]
if "sources" in data:
row.sources_json = json.dumps(list(data["sources"] or []), ensure_ascii=False)
if "selected_source_ids" in data:
row.selected_source_ids = list(data["selected_source_ids"] or [])
if "plan_markdown" in data:
row.plan_markdown = data["plan_markdown"]
for key in ("report_markdown", "report_summary", "report_model_name", "active_report_job_id"):
if key in data:
setattr(row, key, data[key])
row.updated_at = datetime.now(UTC)
await session.flush()
payload = _to_dict(row)
await session.commit()
return payload
async def delete_task(self, task_id: str, user_id: str) -> bool:
async with self._sf() as session:
result = await session.execute(
delete(EnterpriseResearchTaskRow).where(
EnterpriseResearchTaskRow.id == task_id,
EnterpriseResearchTaskRow.user_id == user_id,
)
)
await session.commit()
return bool(result.rowcount)
def _job_to_dict(row: EnterpriseResearchReportJobRow) -> dict[str, Any]:
try:
sources = json.loads(row.sources_json or "[]")
except (TypeError, ValueError):
sources = []
return {
"id": row.id, "task_id": row.task_id, "user_id": row.user_id, "status": row.status,
"phase": row.phase, "model_name": row.model_name, "action": row.action or "initial",
"instruction": row.instruction, "style": row.style, "target_length": row.target_length,
"plan_snapshot": row.plan_snapshot,
"sources": sources if isinstance(sources, list) else [], "report_markdown": row.report_markdown,
"original_report": row.original_report,
"report_summary": row.report_summary, "error_message": row.error_message,
"created_at": row.created_at.isoformat() if row.created_at else None,
"updated_at": row.updated_at.isoformat() if row.updated_at else None,
}
class EnterpriseResearchReportJobRepository(EnterpriseResearchReportJobStore):
def __init__(self, session_factory: async_sessionmaker[AsyncSession]) -> None:
self._sf = session_factory
async def create_job(self, data: dict[str, Any]) -> dict[str, Any]:
now = datetime.now(UTC)
status = str(data.get("status") or "queued")
active_dedupe_key = (
f"{data['user_id']}:{data['task_id']}" if status in {"queued", "running"} else None
)
row = EnterpriseResearchReportJobRow(
id=str(data["id"]), task_id=str(data["task_id"]), user_id=str(data["user_id"]),
status=status, phase=str(data.get("phase") or "queued"),
model_name=data.get("model_name"), action=str(data.get("action") or "initial"),
active_dedupe_key=active_dedupe_key,
instruction=data.get("instruction"), style=data.get("style"), target_length=data.get("target_length"),
plan_snapshot=str(data["plan_snapshot"]),
sources_json=json.dumps(list(data.get("sources") or []), ensure_ascii=False),
original_report=data.get("original_report"),
report_markdown=str(data.get("report_markdown") or ""), report_summary=data.get("report_summary"),
error_message=data.get("error_message"), created_at=now, updated_at=now,
)
async with self._sf() as session:
session.add(row)
try:
await session.flush()
payload = _job_to_dict(row)
await session.commit()
return payload
except IntegrityError:
await session.rollback()
if not active_dedupe_key:
raise
existing = (await session.execute(
select(EnterpriseResearchReportJobRow)
.where(EnterpriseResearchReportJobRow.active_dedupe_key == active_dedupe_key)
.limit(1)
)).scalar_one_or_none()
if existing is None:
raise
return _job_to_dict(existing)
async def get_job(self, job_id: str, user_id: str) -> dict[str, Any] | None:
async with self._sf() as session:
row = (await session.execute(select(EnterpriseResearchReportJobRow).where(
EnterpriseResearchReportJobRow.id == job_id, EnterpriseResearchReportJobRow.user_id == user_id,
))).scalar_one_or_none()
return _job_to_dict(row) if row is not None else None
async def get_active_for_task(self, task_id: str, user_id: str) -> dict[str, Any] | None:
async with self._sf() as session:
row = (await session.execute(select(EnterpriseResearchReportJobRow).where(
EnterpriseResearchReportJobRow.task_id == task_id,
EnterpriseResearchReportJobRow.user_id == user_id,
EnterpriseResearchReportJobRow.status.in_(["queued", "running"]),
).order_by(EnterpriseResearchReportJobRow.updated_at.desc()).limit(1))).scalar_one_or_none()
return _job_to_dict(row) if row is not None else None
async def claim_job(self, job_id: str, user_id: str) -> bool:
async with self._sf() as session:
result = await session.execute(
update(EnterpriseResearchReportJobRow)
.where(
EnterpriseResearchReportJobRow.id == job_id,
EnterpriseResearchReportJobRow.user_id == user_id,
EnterpriseResearchReportJobRow.status == "queued",
)
.values(status="running", updated_at=datetime.now(UTC))
)
await session.commit()
return bool(result.rowcount)
async def requeue_job(self, job_id: str, user_id: str, *, expected_updated_at: str | None) -> bool:
if not expected_updated_at:
return False
try:
expected = datetime.fromisoformat(expected_updated_at)
except ValueError:
return False
async with self._sf() as session:
result = await session.execute(
update(EnterpriseResearchReportJobRow)
.where(
EnterpriseResearchReportJobRow.id == job_id,
EnterpriseResearchReportJobRow.user_id == user_id,
EnterpriseResearchReportJobRow.status == "running",
EnterpriseResearchReportJobRow.updated_at == expected,
)
.values(
status="queued",
phase="queued",
report_markdown="",
error_message=None,
updated_at=datetime.now(UTC),
)
)
await session.commit()
return bool(result.rowcount)
async def transition_job(
self, job_id: str, user_id: str, *, expected_statuses: set[str], data: dict[str, Any]
) -> dict[str, Any] | None:
allowed = {
"status", "phase", "report_markdown", "report_summary", "error_message", "model_name",
"action", "instruction", "style", "target_length", "original_report",
}
values = {key: value for key, value in data.items() if key in allowed}
if data.get("status") in {"completed", "failed", "cancelled"}:
values["active_dedupe_key"] = None
values["updated_at"] = datetime.now(UTC)
async with self._sf() as session:
result = await session.execute(
update(EnterpriseResearchReportJobRow)
.where(
EnterpriseResearchReportJobRow.id == job_id,
EnterpriseResearchReportJobRow.user_id == user_id,
EnterpriseResearchReportJobRow.status.in_(expected_statuses),
)
.values(**values)
)
if not result.rowcount:
await session.rollback()
return None
await session.commit()
return await self.get_job(job_id, user_id)
async def update_job(self, job_id: str, user_id: str, data: dict[str, Any]) -> dict[str, Any] | None:
async with self._sf() as session:
row = (await session.execute(select(EnterpriseResearchReportJobRow).where(
EnterpriseResearchReportJobRow.id == job_id, EnterpriseResearchReportJobRow.user_id == user_id,
))).scalar_one_or_none()
if row is None:
return None
for key in (
"status", "phase", "report_markdown", "report_summary", "error_message", "model_name",
"action", "instruction", "style", "target_length", "original_report",
):
if key in data:
setattr(row, key, data[key])
if data.get("status") in {"completed", "failed", "cancelled"}:
row.active_dedupe_key = None
row.updated_at = datetime.now(UTC)
await session.flush()
payload = _job_to_dict(row)
await session.commit()
return payload
async def list_jobs(self, task_id: str, user_id: str, *, limit: int = 10) -> list[dict[str, Any]]:
async with self._sf() as session:
rows = (await session.execute(select(EnterpriseResearchReportJobRow).where(
EnterpriseResearchReportJobRow.task_id == task_id, EnterpriseResearchReportJobRow.user_id == user_id,
).order_by(EnterpriseResearchReportJobRow.updated_at.desc()).limit(limit))).scalars()
return [_job_to_dict(row) for row in rows]
async def list_recoverable(self, *, limit: int = 100) -> list[dict[str, Any]]:
async with self._sf() as session:
rows = (await session.execute(
select(EnterpriseResearchReportJobRow)
.where(EnterpriseResearchReportJobRow.status.in_(["queued", "running"]))
.order_by(EnterpriseResearchReportJobRow.updated_at.asc())
.limit(limit)
)).scalars()
return [_job_to_dict(row) for row in rows]
@@ -0,0 +1,52 @@
"""Add compact_menu_overrides.menu_type (简介模式菜单分套).
Two website deployments keep independent compact/sidebar-summary left menus.
Type ``A`` is the historical default; type ``B`` is stored under a prefixed
``node_id`` so it never collides on databases that still use ``node_id`` as
the sole primary key.
ORM: ``CompactMenuOverrideRow.menu_type = String(32), NOT NULL, default A``.
``create_all`` gives new DBs the column; existing DBs are patched by this
revision and by startup ``_ensure_orm_columns_sync``.
Revision ID: 20260819_01
Revises: 20260816_01
Create Date: 2026-08-19
"""
from __future__ import annotations
import sqlalchemy as sa
from alembic import op
from sqlalchemy import inspect
revision = "20260819_01"
down_revision = "20260816_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("compact_menu_overrides") and not _has_column("compact_menu_overrides", "menu_type"):
op.add_column(
"compact_menu_overrides",
sa.Column("menu_type", sa.String(length=32), nullable=False, server_default="A"),
)
def downgrade() -> None:
if _has_column("compact_menu_overrides", "menu_type"):
op.drop_column("compact_menu_overrides", "menu_type")
@@ -0,0 +1,51 @@
"""Add enabled column to roundtable_chains.
Per-chain 启停 switch: True = 可在选链弹窗 / 公共入口里选用;False = 停用(管理列表
仍可见,但不可选入研讨)。Default True → existing chains stay enabled. BOOLEAN NOT
NULL DEFAULT 1, matching ``RoundtableChainRow.enabled``.
``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: 20260819_02
Revises: 20260819_01
Create Date: 2026-08-19
"""
from __future__ import annotations
import sqlalchemy as sa
from alembic import op
from sqlalchemy import inspect
revision = "20260819_02"
down_revision = "20260819_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", "enabled"):
op.add_column(
"roundtable_chains",
sa.Column("enabled", sa.Boolean(), nullable=False, server_default="1"),
)
def downgrade() -> None:
if _has_column("roundtable_chains", "enabled"):
op.drop_column("roundtable_chains", "enabled")
@@ -0,0 +1,49 @@
"""Add structure_mode to report_structures.
Per-structure 结构约束: ``fixed`` (固定大纲) vs ``adaptive`` (弹性大纲).
Existing rows stay ``adaptive`` so current writing behaviour is unchanged.
``create_all`` adds the column for brand-new DBs; the startup
``_ensure_orm_columns_sync`` sweep adds it to already-existing tables.
Revision ID: 20260821_01
Revises: 20260819_02
Create Date: 2026-08-21
"""
from __future__ import annotations
import sqlalchemy as sa
from alembic import op
from sqlalchemy import inspect
revision = "20260821_01"
down_revision = "20260819_02"
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("report_structures") and not _has_column("report_structures", "structure_mode"):
op.add_column(
"report_structures",
sa.Column("structure_mode", sa.String(length=16), nullable=False, server_default="adaptive"),
)
def downgrade() -> None:
if _has_column("report_structures", "structure_mode"):
op.drop_column("report_structures", "structure_mode")
@@ -0,0 +1,52 @@
"""Add retrieval_directions to report_structures.
Optional 检索方向 list on each 报告结构. Empty / NULL keeps today's
collector query-planning; a non-empty list tells material collection to
search along those facets.
ORM: ``ReportStructureRow.retrieval_directions = PortableJSON()``
(SQLite/PG native JSON, MySQL TEXT). ``create_all`` adds the column on
brand-new DBs; existing tables are patched by startup
``_ensure_orm_columns_sync`` (nullable, no server_default required).
Revision ID: 20260822_01
Revises: 20260821_01
Create Date: 2026-08-22
"""
from __future__ import annotations
import sqlalchemy as sa
from alembic import op
from sqlalchemy import inspect
revision = "20260822_01"
down_revision = "20260821_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("report_structures") and not _has_column("report_structures", "retrieval_directions"):
op.add_column(
"report_structures",
sa.Column("retrieval_directions", sa.Text(), nullable=True),
)
def downgrade() -> None:
if _has_column("report_structures", "retrieval_directions"):
op.drop_column("report_structures", "retrieval_directions")
@@ -0,0 +1,52 @@
"""Add retrieval_skills to report_structures.
Optional 检索来源 list of skill names on each 报告结构. Empty / NULL keeps
today's collector tools; a non-empty list tells material collection to
call those skills.
ORM: ``ReportStructureRow.retrieval_skills = PortableJSON()``
(SQLite/PG native JSON, MySQL TEXT). ``create_all`` adds the column on
brand-new DBs; existing tables are patched by startup
``_ensure_orm_columns_sync`` (nullable, no server_default required).
Revision ID: 20260822_02
Revises: 20260822_01
Create Date: 2026-08-22
"""
from __future__ import annotations
import sqlalchemy as sa
from alembic import op
from sqlalchemy import inspect
revision = "20260822_02"
down_revision = "20260822_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("report_structures") and not _has_column("report_structures", "retrieval_skills"):
op.add_column(
"report_structures",
sa.Column("retrieval_skills", sa.Text(), nullable=True),
)
def downgrade() -> None:
if _has_column("report_structures", "retrieval_skills"):
op.drop_column("report_structures", "retrieval_skills")
@@ -65,6 +65,7 @@ from deerflow.persistence.position_roundtable.model import (
)
from deerflow.persistence.positions.model import PositionRow, PositionTagBindingRow
from deerflow.persistence.recommended_questions.model import RecommendedQuestionRow
from deerflow.persistence.report_structures.model import ReportStructureRow
from deerflow.persistence.research_templates.model import ResearchTemplateRow
from deerflow.persistence.roundtable_artifact_submissions.model import RoundtableArtifactSubmissionRow
from deerflow.persistence.roundtable_chains.model import RoundtableChainRow
@@ -141,6 +142,7 @@ __all__ = [
"PositionRow",
"PositionTagBindingRow",
"RecommendedQuestionRow",
"ReportStructureRow",
"ResearchTemplateRow",
"RoundtableChainRow",
"RoundtableArtifactSubmissionRow",
@@ -0,0 +1,52 @@
"""Deep-research report-structure persistence (报告结构 / 工作流配置)."""
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",
]
def make_report_structure_store(session_factory):
if session_factory is None:
return MemoryReportStructureStore()
return ReportStructureRepository(session_factory)
@@ -0,0 +1,64 @@
"""Abstract report-structure persistence API (报告结构 / 工作流配置).
Global / shared store: no ``user_id`` scoping — the deep-research 工作流配置
dialog shows the same cards to every account. ``created_by`` is a display-only
audit field (创建用户名). Rows are managed per-item (create / update /
toggle-enabled / delete); ``ensure_defaults`` lazily seeds the two built-in
outlines the first time the table is empty so a fresh deployment renders the
same 通用分析结构 / 递进式论证结构 cards the frontend used to hardcode.
"""
from __future__ import annotations
import logging
from abc import ABC, abstractmethod
from typing import Any
from deerflow.persistence.report_structures.defaults import DEFAULT_REPORT_STRUCTURE_ROWS
logger = logging.getLogger(__name__)
class ReportStructureStore(ABC):
@abstractmethod
async def list_structures(self) -> list[dict[str, Any]]:
"""Return every stored structure, most recently modified first."""
raise NotImplementedError
@abstractmethod
async def get_structure(self, structure_id: str) -> dict[str, Any] | None:
raise NotImplementedError
@abstractmethod
async def create_structure(self, data: dict[str, Any]) -> dict[str, Any]:
"""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`` / ``structure_mode`` / ``retrieval_directions`` / ``retrieval_skills``. None when missing."""
raise NotImplementedError
@abstractmethod
async def set_enabled(self, structure_id: str, enabled: bool) -> dict[str, Any] | None:
"""Flip 启用/停用 (also bumps updated_at). None when missing."""
raise NotImplementedError
@abstractmethod
async def delete_structure(self, structure_id: str) -> bool:
raise NotImplementedError
@abstractmethod
async def count(self) -> int:
raise NotImplementedError
async def ensure_defaults(self) -> None:
"""Seed the built-in outlines when the table has never held a row.
Concurrent first reads can race; seed ids are fixed so a losing insert
fails on the primary key and is simply skipped.
"""
if await self.count() > 0:
return
for row in DEFAULT_REPORT_STRUCTURE_ROWS:
await self.create_structure(dict(row))
@@ -0,0 +1,269 @@
"""深度研究「报告结构」默认种子(新机器部署开箱即有的内置结构库)。
前两套(通用分析结构 / 递进式论证结构)逐字提取自前端
``frontend-web/src/pages/deep-research/ReportConfigCard.tsx`` 的
``DEFAULT_REPORT_OUTLINE`` / ``PROGRESSIVE_REPORT_OUTLINE``;其余三套为本仓库
补充的常用研究结构(快速简报 / 事件分析 / 对象调查)。
表为空(全新部署、从未写过数据)时 ``ensure_defaults`` 会把全部内置结构插入
``report_structures``;已有数据的库不会追加。若调优了某套结构,请同步更新
本文件,否则新机器补建出来的还是旧版。
"""
from __future__ import annotations
GENERAL_OUTLINE = """# 报告标题
# 引言
用 1–2 段说明研究对象、提出本议题的背景、研究范围及资料边界;简要交代全文的分析路径,不单独拆分小节。
# 一、研究对象与事实基础
## 1.1 概念、范围与背景
界定研究对象、关键概念、时间和空间范围;说明读者理解问题所需的背景。
## 1.2 已有事实与证据
按时间或逻辑顺序梳理已知事实、数据、案例或材料发现;优先使用可核验的来源。
## 1.3 当前状态与主要特征
归纳研究对象目前呈现的状态、关键变化、共性与差异;没有数据支撑时明确说明。
# 二、问题形成与作用机制
## 2.1 直接原因与触发条件
分析近期事件、条件变化或关键节点如何推动问题出现或演变。
## 2.2 深层影响因素
从制度、技术、资源、行为、环境或历史条件中选择适用维度,分析长期影响。
## 2.3 因果关系与相互作用
说明不同因素如何共同作用;区分来源已证实的事实、分析判断和仍待验证的推断。
# 三、影响、问题与不同观点
## 3.1 对研究对象的直接影响
从结果、效率、质量、风险、成本、体验或其他适用维度,分析当前影响。
## 3.2 对相关主体与外部环境的影响
说明影响如何传导到相关群体、组织、地区、系统或后续事项;不适用的维度可省略。
## 3.3 主要争议与解释差异
并列材料支持的不同观点、解释或结论,并说明各自的证据基础与局限。
# 四、比较、趋势与不确定性
## 4.1 可比案例或横向比较
在材料允许时选择历史案例、不同地区、不同方案或不同阶段进行比较,说明异同与启示。
## 4.2 发展趋势与关键变量
识别后续可能变化、决定性变量、观察指标和潜在转折点;不把预测写成既成事实。
## 4.3 风险情景与不确定性
说明不同情景的触发条件、可能影响和证据不足之处;对没有依据的推测明确标注。
# 五、结论、建议与后续关注
## 5.1 核心结论
用编号或分点方式收束全文;每条结论都应能回溯到正文中的事实和分析。
## 5.2 可行建议或应对方向
仅在研究材料支持时提出建议;说明适用对象、前提条件、优先级和预期作用。
## 5.3 资料局限与后续关注
列出证据缺口、数据口径差异、尚不能下结论的问题,以及值得持续跟踪的事项。
# 参考资料
仅列出本报告实际使用的来源,并与正文引用保持一致。
> 写作要求:引言保持简洁;每个二级小节按材料充分程度写 2–5 段完整分析,每段围绕一个明确观点展开,采用“事实/证据 → 分析 → 影响或小结”的顺序。章节可根据研究主题合并、增删或调整,但不要为了套模板而编造不适用的主体、数据、案例或引用。"""
PROGRESSIVE_OUTLINE = """# 报告标题
# 引言
用 1–2 段说明研究对象、核心问题、研究范围和资料边界;简要说明本文将按“事实基线 → 成因机制 → 影响判断 → 应对方向 → 结论”的递进逻辑展开。
# 一、事实基线:问题是什么
## 1.1 概念界定与研究范围
明确关键概念、时间/对象边界和判断口径,避免后续讨论混用概念。
## 1.2 关键事实、数据与证据
按时间或逻辑顺序列出已经发生的事实、可核验数据、案例和来源。
## 1.3 基线判断与待解问题
基于前两节归纳当前状态、主要矛盾和需要解释的核心问题。
> 本章产出:后续分析必须以本章已经确认的事实和问题为基础,不得在后文补写没有来源支撑的新事实。
# 二、形成机制:为什么会这样
## 2.1 直接原因与触发条件
从第一章识别的问题出发,分析近期事件、条件变化或关键节点如何导致当前结果。
## 2.2 深层条件与关键约束
结合第一章的证据,说明制度、技术、资源、行为、环境或历史条件等长期影响。
## 2.3 因果链条与关键变量
把前两节串成清晰的“条件 → 机制 → 结果”链条,标出决定性变量及仍待验证的环节。
> 本章产出:第三章只能沿用本章已说明的机制评估影响,不能把相关性直接写成因果关系。
# 三、结果与影响:会带来什么
## 3.1 直接结果与短期影响
依据第二章的因果链条,分析对研究对象最直接、最先出现的影响。
## 3.2 传导效应与中长期影响
继续说明影响如何传导至相关主体、外部环境或后续事项;不适用的维度可省略。
## 3.3 权衡、分歧与边界
比较不同影响之间的取舍、材料支持的不同观点,以及结论适用的边界。
> 本章产出:第四章的趋势判断、情景和建议应以本章已评估的影响、风险和权衡为依据。
# 四、趋势与应对:接下来怎么办
## 4.1 未来走势与观察指标
从第三章的影响出发,识别可能变化、关键观察指标和潜在转折点;不把预测写成既成事实。
## 4.2 可选方案与适用条件
针对第三章发现的问题和风险,提出可行的应对方向、前提条件与优先级。
## 4.3 风险情景与验证重点
说明不同情景的触发条件、可能后果,以及下一步需要补充验证的数据或材料。
> 本章产出:第五章只综合前四章已经建立的事实、机制、影响和建议,不新增独立论点。
# 五、综合结论与后续关注
## 5.1 核心结论
按“事实基线 → 原因机制 → 主要影响 → 应对方向”逐项收束,每条结论都可回溯到前文证据。
## 5.2 局限与后续研究
列出资料缺口、数据口径差异、尚不能下结论的问题和后续跟踪清单。
# 参考资料
仅列出本报告实际使用的来源,并与正文引用保持一致。
> 写作要求:除引言外,每个二级小节写 2–5 段完整分析。后章必须明确承接前章的结论;若材料无法支持递进关系,应说明资料限制而非编造衔接。"""
BRIEF_OUTLINE = """# 报告标题
# 核心结论
用 3–5 条要点直接给出最重要的结论与判断,每条一两句话并可回溯到后文依据;先说结论,再列支撑。
# 关键事实
## 事实清单
按重要性列出已核验的关键事实、数据与出处;不确定的信息单独注明置信度。
## 时间与背景
用最短的篇幅交代理解结论所需的事件顺序与背景,不展开细节。
# 风险与不确定性
列出可能推翻或削弱上述结论的因素、证据缺口和正在变化中的变量。
# 建议行动
按优先级给出可执行的建议或下一步关注点,说明适用前提与预期作用;材料不足时明确说明证据尚不支持。
# 参考资料
仅列出本简报实际使用的来源,并与正文引用保持一致。
> 写作要求:全文控制在较短篇幅(建议 800–1500 字),信息密度优先;不写套话与背景铺陈,所有论断要么有来源支撑,要么明确标注为分析判断。"""
EVENT_OUTLINE = """# 报告标题
# 引言
用 1–2 段说明事件的性质、发生时间地点、当前状态与本报告要回答的核心问题。
# 一、事件概述
## 1.1 事件是什么
客观描述发生了什么:主体、行为、规模、直接结果;只写已核验事实。
## 1.2 各方说法
并列当事方、官方与媒体的不同表述,注明各自的信息来源与立场。
# 二、时间线还原
## 2.1 起因与前情
梳理事件发生的背景、前置条件与诱发节点。
## 2.2 关键节点
按时间顺序还原事件演进的关键节点,每个节点注明时间与来源;存在争议的时间点单独标注。
## 2.3 当前状态
说明事件目前的进展、处置情况与未决事项。
# 三、原因与责任分析
## 3.1 直接原因
分析触发事件的直接因素与机制。
## 3.2 深层原因
从制度、管理、技术、环境等维度分析更深层的促成条件。
## 3.3 责任与归因
基于事实分析各方责任,区分已认定的结论与仍在调查的部分。
# 四、影响与反应
## 4.1 直接影响
评估事件对当事方与相关群体已造成的影响。
## 4.2 各方反应与后续动作
梳理官方处置、当事方回应、舆论反应与衍生事件。
## 4.3 潜在延伸影响
分析可能的中长期影响与传导路径,注明判断依据。
# 五、结论与后续关注
## 5.1 核心结论
收束事件性质、原因与影响的核心判断。
## 5.2 待验证问题与观察点
列出尚未证实的关键问题与值得持续跟踪的信号。
# 参考资料
仅列出本报告实际使用的来源,并与正文引用保持一致。
> 写作要求:严格区分“已证实事实—各方声称—分析推断”三个层次并分别标注;时间线每个节点须有来源;对争议点呈现多方说法,不单方采信。"""
PROFILE_OUTLINE = """# 报告标题
# 引言
说明调查对象的界定范围(个人、组织或产品)、研究目的与资料边界(公开信息的时间范围与覆盖面)。
# 一、基本情况
## 1.1 基本信息与背景
梳理对象的公认基本信息与来历,注明信息时效。
## 1.2 发展沿革
按时间梳理关键成长节点、转折与现状。
# 二、能力与表现
## 2.1 核心能力或竞争力
分析对象的核心能力、资源禀赋或产品力,给出可观察的证据。
## 2.2 代表性成果与表现
列举代表性成果、业绩或事件表现,并评估其含金量与可持续性。
## 2.3 短板与争议
并列材料中的短板、批评与争议点,说明各方依据。
# 三、关系网络与生态
## 3.1 关联方与合作关系
梳理重要的合作关系、关联方与利益结构。
## 3.2 立场与倾向
基于言行记录分析对象的立场、风格与决策倾向。
# 四、风险评估
## 4.1 已知风险
汇总已暴露的风险点、负面记录与可靠性问题。
## 4.2 潜在风险与信息缺口
指出资料不足导致的盲区与需要进一步核实的问题。
# 五、综合评估
## 5.1 画像结论
用几句话给出对象的综合画像与可信度评估。
## 5.2 交往或合作建议
给出与对象打交道的注意事项与建议,说明前提。
# 参考资料
仅列出本报告实际使用的来源,并与正文引用保持一致。
> 写作要求:只依据公开可核验信息,猜测与推断必须明确标注;负面信息须注明来源与时间,区分“事实—评价—传闻”;信息不足时如实说明,不为了完整而编造。"""
# 固定 id:并发首查时重复插入会撞主键而被吞掉,不会产生重复行。
DEFAULT_REPORT_STRUCTURE_ROWS: list[dict] = [
{
"id": "seed-report-outline-general",
"title": "通用分析结构",
"name": "通用分析结构",
"content": GENERAL_OUTLINE,
"created_by": "系统内置",
},
{
"id": "seed-report-outline-progressive",
"title": "递进式论证结构",
"name": "递进式论证结构",
"content": PROGRESSIVE_OUTLINE,
"created_by": "系统内置",
},
{
"id": "seed-report-outline-brief",
"title": "快速简报结构",
"name": "快速简报结构",
"content": BRIEF_OUTLINE,
"created_by": "系统内置",
},
{
"id": "seed-report-outline-event",
"title": "事件分析结构",
"name": "事件分析结构",
"content": EVENT_OUTLINE,
"created_by": "系统内置",
},
{
"id": "seed-report-outline-profile",
"title": "对象调查结构",
"name": "对象调查结构",
"content": PROFILE_OUTLINE,
"created_by": "系统内置",
},
]
@@ -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
@@ -0,0 +1,78 @@
"""In-memory report-structure store (auth-less tests / no-DB fallback)."""
from __future__ import annotations
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:
return datetime.now(UTC).isoformat()
def _sort_rows(rows: list[dict[str, Any]]) -> list[dict[str, Any]]:
return sorted(rows, key=lambda r: (r.get("updated_at") or "", r.get("created_at") or ""), reverse=True)
class MemoryReportStructureStore(ReportStructureStore):
def __init__(self) -> None:
self._rows: dict[str, dict[str, Any]] = {}
async def list_structures(self) -> list[dict[str, Any]]:
return _sort_rows(list(self._rows.values()))
async def get_structure(self, structure_id: str) -> dict[str, Any] | None:
row = self._rows.get(structure_id)
return dict(row) if row else None
async def create_structure(self, data: dict[str, Any]) -> dict[str, Any]:
row = {
"id": str(data["id"]),
"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(),
"updated_at": _now_iso(),
}
self._rows[row["id"]] = row
return dict(row)
async def update_structure(self, structure_id: str, data: dict[str, Any]) -> dict[str, Any] | None:
row = self._rows.get(structure_id)
if row is None:
return None
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)
async def set_enabled(self, structure_id: str, enabled: bool) -> dict[str, Any] | None:
row = self._rows.get(structure_id)
if row is None:
return None
row["enabled"] = bool(enabled)
row["updated_at"] = _now_iso()
return dict(row)
async def delete_structure(self, structure_id: str) -> bool:
return self._rows.pop(structure_id, None) is not None
async def count(self) -> int:
return len(self._rows)
@@ -0,0 +1,62 @@
"""ORM model for deep-research report structures (报告结构 / 工作流配置).
Global / shared — every account sees the same structure cards in the deep
research workbench's 工作流配置 dialog. Each row is one reusable report
structure the user can pick to fill ``custom_outline``:
- ``title`` — 标题, the card's primary label and the list's 标题筛选 target.
- ``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).
Fresh deployments get the two built-in outlines (通用分析结构 / 递进式论证结构)
seeded by ``ensure_defaults`` — see ``defaults.py``.
"""
from __future__ import annotations
from datetime import UTC, datetime
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, PortableJSON
class ReportStructureRow(Base):
__tablename__ = "report_structures"
id: Mapped[str] = mapped_column(String(64), primary_key=True)
title: Mapped[str] = mapped_column(String(256), nullable=False)
name: Mapped[str] = mapped_column(String(256), default="", nullable=False)
content: Mapped[str] = mapped_column(Text, nullable=False)
# ``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))
updated_at: Mapped[datetime] = mapped_column(
BeijingDateTime(), nullable=False, default=lambda: datetime.now(UTC), onupdate=lambda: datetime.now(UTC)
)
@@ -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
@@ -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
@@ -0,0 +1,122 @@
"""SQLAlchemy-backed report-structure store (global / shared)."""
from __future__ import annotations
import logging
from datetime import UTC, datetime
from typing import Any
from sqlalchemy import delete, func, select
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__)
def _to_dict(row: ReportStructureRow) -> dict[str, Any]:
data = row.to_dict()
for key in ("created_at", "updated_at"):
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
class ReportStructureRepository(ReportStructureStore):
def __init__(self, session_factory: async_sessionmaker[AsyncSession]) -> None:
self._sf = session_factory
async def list_structures(self) -> list[dict[str, Any]]:
stmt = select(ReportStructureRow).order_by(
ReportStructureRow.updated_at.desc(), ReportStructureRow.created_at.asc()
)
async with self._sf() as session:
result = await session.execute(stmt)
return [_to_dict(row) for row in result.scalars()]
async def get_structure(self, structure_id: str) -> dict[str, Any] | None:
async with self._sf() as session:
row = await session.get(ReportStructureRow, structure_id)
return _to_dict(row) if row is not None else None
async def create_structure(self, data: dict[str, Any]) -> dict[str, Any]:
now = datetime.now(UTC)
row = ReportStructureRow(
id=str(data["id"]),
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,
updated_at=now,
)
async with self._sf() as session:
session.add(row)
await session.commit()
await session.refresh(row)
return _to_dict(row)
async def update_structure(self, structure_id: str, data: dict[str, Any]) -> dict[str, Any] | None:
async with self._sf() as session:
row = await session.get(ReportStructureRow, structure_id)
if row is None:
return None
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)
return _to_dict(row)
async def set_enabled(self, structure_id: str, enabled: bool) -> dict[str, Any] | None:
async with self._sf() as session:
row = await session.get(ReportStructureRow, structure_id)
if row is None:
return None
row.enabled = bool(enabled)
row.updated_at = datetime.now(UTC)
await session.commit()
await session.refresh(row)
return _to_dict(row)
async def delete_structure(self, structure_id: str) -> bool:
async with self._sf() as session:
result = await session.execute(delete(ReportStructureRow).where(ReportStructureRow.id == structure_id))
await session.commit()
return bool(result.rowcount)
async def count(self) -> int:
async with self._sf() as session:
return int(await session.scalar(select(func.count()).select_from(ReportStructureRow)) or 0)
async def ensure_defaults(self) -> None:
"""Seed when empty; a concurrent first-read loses on the fixed PK silently."""
if await self.count() > 0:
return
from deerflow.persistence.report_structures.defaults import DEFAULT_REPORT_STRUCTURE_ROWS
for row in DEFAULT_REPORT_STRUCTURE_ROWS:
try:
await self.create_structure(dict(row))
except IntegrityError:
logger.debug("report-structure default '%s' already inserted by a concurrent request", row["id"])
@@ -37,6 +37,23 @@ class RoundtableArtifactSubmissionStore(ABC):
"""
raise NotImplementedError
@abstractmethod
async def register(
self,
task_id: str,
artifact: dict[str, Any],
*,
task_delivery_id: str | None = None,
actor_id: str | None = None,
expected_version: int | None = None,
) -> dict[str, Any]:
"""Persist a generated-but-not-yet-submitted artifact receipt.
This lets the UI retain the external task-process-report id across a
refresh before the user explicitly submits the Markdown artifact.
"""
raise NotImplementedError
@abstractmethod
async def cancel(
self,
@@ -109,6 +109,63 @@ class MemoryRoundtableArtifactSubmissionStore(RoundtableArtifactSubmissionStore)
)
return deepcopy(row)
async def register(
self,
task_id: str,
artifact: dict[str, Any],
*,
task_delivery_id: str | None = None,
actor_id: str | None = None,
expected_version: int | None = None,
) -> dict[str, Any]:
"""Save an external receipt while keeping the artifact unsubmitted."""
from deerflow.persistence.roundtable_artifact_submissions.sql import SubmissionVersionConflict
key = submission_key(task_id, artifact)
row = self._rows.get((task_id, key))
values = _artifact_values(artifact)
if row is None:
now = _now()
row = {
"id": uuid4().hex,
"task_id": task_id,
"idempotency_key": key,
**values,
"task_delivery_id": task_delivery_id,
"submitted": False,
"version": 0,
"submitted_at": None,
"cancelled_at": None,
"created_by": actor_id,
"updated_by": actor_id,
"submitted_by": None,
"cancelled_by": None,
"created_at": now,
"updated_at": now,
}
self._rows[(task_id, key)] = row
return deepcopy(row)
if expected_version is not None and row.get("version", 0) != expected_version:
raise SubmissionVersionConflict(deepcopy(row))
changed = (
row.get("task_delivery_id") != task_delivery_id
or any(row.get(name) != value for name, value in values.items())
)
if changed:
now = _now()
row.update(values)
row.update(
{
"task_delivery_id": task_delivery_id,
"updated_by": actor_id,
"updated_at": now,
"version": row.get("version", 0) + 1,
}
)
return deepcopy(row)
async def cancel(
self,
task_id: str,
@@ -88,6 +88,22 @@ def _requires_submission_update(
)
def _requires_registration_update(
row: RoundtableArtifactSubmissionRow,
artifact: dict[str, Any],
task_delivery_id: str | None,
) -> bool:
"""Compare receipt metadata without treating ``submitted=False`` as a change."""
return (
row.task_delivery_id != task_delivery_id
or row.session_id != artifact.get("session_id")
or row.thread_id != artifact.get("thread_id")
or row.node_key != artifact.get("node_key")
or row.artifact_name != artifact.get("name")
or row.mime_type != artifact.get("mime_type")
)
class RoundtableArtifactSubmissionRepository(RoundtableArtifactSubmissionStore):
def __init__(self, session_factory: async_sessionmaker[AsyncSession]) -> None:
self._sf = session_factory
@@ -181,6 +197,70 @@ class RoundtableArtifactSubmissionRepository(RoundtableArtifactSubmissionStore):
raise RuntimeError("submit: retry loop exhausted (unexpected)")
async def register(
self,
task_id: str,
artifact: dict[str, Any],
*,
task_delivery_id: str | None = None,
actor_id: str | None = None,
expected_version: int | None = None,
) -> dict[str, Any]:
"""Persist an external process-report receipt before user submission."""
key = submission_key(task_id, artifact)
for _attempt in range(2):
async with self._sf() as session:
result = await session.execute(
select(RoundtableArtifactSubmissionRow)
.where(
RoundtableArtifactSubmissionRow.task_id == task_id,
RoundtableArtifactSubmissionRow.idempotency_key == key,
)
.with_for_update()
)
row = result.scalars().first()
now = datetime.now(UTC)
if row is None:
row = RoundtableArtifactSubmissionRow(
id=uuid4().hex,
task_id=task_id,
idempotency_key=key,
task_delivery_id=task_delivery_id,
submitted=False,
created_by=actor_id,
updated_by=actor_id,
created_at=now,
updated_at=now,
version=0,
)
_apply_artifact(row, artifact)
session.add(row)
try:
await session.commit()
except IntegrityError:
await session.rollback()
continue
await session.refresh(row)
return _to_dict(row)
if expected_version is not None and row.version != expected_version:
raise SubmissionVersionConflict(_to_dict(row))
if not _requires_registration_update(row, artifact, task_delivery_id):
return _to_dict(row)
_apply_artifact(row, artifact)
row.task_delivery_id = task_delivery_id
row.updated_by = actor_id
row.updated_at = now
row.version = row.version + 1
await session.commit()
await session.refresh(row)
return _to_dict(row)
raise RuntimeError("register: retry loop exhausted (unexpected)")
async def cancel(
self,
task_id: str,
@@ -63,6 +63,12 @@ class RoundtableChainRow(Base):
is_public: Mapped[bool] = mapped_column(
Boolean, nullable=False, default=False, server_default="0", index=True
)
# 启停开关:True = 启用(可在选链弹窗 / 公共入口里选用);False = 停用(管理列表仍可见,
# 但不可选入研讨)。默认启用,存量链条补列后也为 True。``server_default`` 必须显式声明,
# 否则既有表上启动期 ``_ensure_orm_columns_sync`` 会跳过「NOT NULL 且无 server_default」的列。
enabled: Mapped[bool] = mapped_column(
Boolean, nullable=False, default=True, server_default="1"
)
created_at: Mapped[datetime] = mapped_column(
BeijingDateTime(), nullable=False, default=lambda: datetime.now(UTC)
)
@@ -62,6 +62,8 @@ class RoundtableChainRepository:
"gather_first": bool(getattr(row, "gather_first", False)),
# 发布状态 + 是否本人所有(前端据此显示「公共」徽标、决定能否编辑 / 发布)。
"is_public": bool(getattr(row, "is_public", False)),
# 启停状态:缺省 True(存量行 / 补列前视为启用)。
"enabled": bool(getattr(row, "enabled", True)),
"is_mine": (current_user_id is None) or (row.user_id == current_user_id),
# 配置者(owner)的 user_id;路由层据此解析成可读用户名(owner_name)。
"owner_id": row.user_id,
@@ -122,6 +124,7 @@ class RoundtableChainRepository:
seat_mode=(data.get("seat_mode") or None),
gather_first=bool(data.get("gather_first") or False),
is_public=bool(data.get("is_public") or False),
enabled=True if data.get("enabled") is None else bool(data.get("enabled")),
created_at=now,
updated_at=now,
)
@@ -145,6 +148,7 @@ class RoundtableChainRepository:
seat_mode=_UNSET,
gather_first=_UNSET,
is_public=_UNSET,
enabled=_UNSET,
is_admin: bool = False,
) -> dict[str, Any] | None:
async with self._sf() as session:
@@ -170,6 +174,8 @@ class RoundtableChainRepository:
row.gather_first = bool(gather_first)
if is_public is not _UNSET and is_public is not None:
row.is_public = bool(is_public)
if enabled is not _UNSET and enabled is not None:
row.enabled = bool(enabled)
row.updated_at = datetime.now(UTC)
await session.commit()
await session.refresh(row)
@@ -8,8 +8,10 @@ left task panel. Each row is one button:
- ``business`` — which workspace the button belongs to (``rwfx`` / ``3qfx`` /
``xdfx``).
- ``label`` — display name.
- ``link_type`` — ``url`` (external, opened in a new tab) or ``business``
(internal switch to another deep-link business).
- ``link_type`` — ``url`` (external / in-app route), ``business``
(internal switch to another deep-link business), ``purpose`` (task-purpose
dialog), or ``host`` (iframe ``postMessage`` ``HOST_NAVIGATE`` to the
embedding host).
- ``target`` — the full URL (``url``) or the target business code (``business``).
- ``append_task_id`` — for ``url`` buttons, whether to append ``?id={taskId}``.
- ``enabled`` — 停用 parks a button without deleting it.
@@ -15,9 +15,33 @@ three-state semantics (see :mod:`deerflow.runtime.user_context`):
from __future__ import annotations
import abc
from typing import Any
from deerflow.runtime.user_context import AUTO, _AutoSentinel
# Configuration wizards / machine workers that must not appear in the default
# conversation list. Prefer ``metadata.system=true`` on create; this set also
# covers older rows that only set ``thread_type``.
EXCLUDED_SYSTEM_THREAD_TYPES = frozenset(
{
"scheduler",
"roundtable",
"notebook",
"deep_research",
"writing_setup",
"report_structure_setup",
}
)
def is_excluded_system_thread(metadata: dict[str, Any] | None) -> bool:
"""Return True when a thread should be hidden from user conversation lists."""
md = metadata or {}
if md.get("system"):
return True
thread_type = md.get("thread_type")
return isinstance(thread_type, str) and thread_type in EXCLUDED_SYSTEM_THREAD_TYPES
class ThreadMetaStore(abc.ABC):
@abc.abstractmethod
@@ -56,9 +80,9 @@ class ThreadMetaStore(abc.ABC):
) -> list[dict]:
"""Search threads ordered by ``updated_at`` desc.
``exclude_system=True`` drops threads whose ``metadata.system`` is
truthy (scheduler runs, roundtable workers, …) so user-facing
conversation lists are not crowded out by machine-created threads.
``exclude_system=True`` drops machine / wizard threads (``metadata.system``
or known ``thread_type`` values such as scheduler / report_structure_setup)
so user-facing conversation lists are not crowded out.
``query`` is a case-insensitive substring match on the thread title
(``display_name``). ``limit``/``offset`` apply to the *filtered*
result set.
@@ -11,7 +11,7 @@ from typing import Any
from langgraph.store.base import BaseStore
from deerflow.persistence.thread_meta.base import ThreadMetaStore
from deerflow.persistence.thread_meta.base import ThreadMetaStore, is_excluded_system_thread
from deerflow.runtime.user_context import AUTO, _AutoSentinel, resolve_user_id
from deerflow.utils.time import coerce_iso, now_iso
@@ -107,7 +107,7 @@ class MemoryThreadMetaStore(ThreadMetaStore):
# until the requested window is full so ``limit``/``offset`` refer to
# filtered rows.
def _matches(record: dict) -> bool:
if exclude_system and (record.get("metadata") or {}).get("system"):
if exclude_system and is_excluded_system_thread(record.get("metadata")):
return False
if normalized_query and normalized_query not in (record.get("display_name") or "").lower():
return False
@@ -169,7 +169,7 @@ class MemoryThreadMetaStore(ThreadMetaStore):
break
for item in items:
record = self._item_to_dict(item)
if exclude_system and (record.get("metadata") or {}).get("system"):
if exclude_system and is_excluded_system_thread(record.get("metadata")):
continue
if normalized_query and normalized_query not in (record.get("display_name") or "").lower():
continue
@@ -8,7 +8,7 @@ from typing import Any
from sqlalchemy import func, select, update
from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker
from deerflow.persistence.thread_meta.base import ThreadMetaStore
from deerflow.persistence.thread_meta.base import ThreadMetaStore, is_excluded_system_thread
from deerflow.persistence.thread_meta.model import ThreadMetaRow
from deerflow.runtime.user_context import AUTO, _AutoSentinel, resolve_user_id
@@ -166,7 +166,7 @@ class ThreadMetaRepository(ThreadMetaStore):
md = row.get("metadata") or {}
if metadata and not all(md.get(k) == v for k, v in metadata.items()):
return False
if exclude_system and md.get("system"):
if exclude_system and is_excluded_system_thread(md):
return False
return True
@@ -233,7 +233,7 @@ class ThreadMetaRepository(ThreadMetaStore):
md = metadata_json or {}
if metadata and not all(md.get(k) == v for k, v in metadata.items()):
continue
if exclude_system and md.get("system"):
if exclude_system and is_excluded_system_thread(md):
continue
total += 1
if len(rows) < batch_size: