初始化

This commit is contained in:
gozeros committed 2026-09-07 18:24:55 +08:00
1 parent 34474bd5fc
commit b638b541d0
3008 files changed
+837699

No files matched your search

@@ -0,0 +1,18 @@
"""Task-scoped roundtable-draft persistence (separate, unpartitioned store)."""
from __future__ import annotations
from deerflow.persistence.roundtable_task_drafts.model import RoundtableTaskDraftRow
from deerflow.persistence.roundtable_task_drafts.sql import RoundtableTaskDraftRepository
__all__ = [
"RoundtableTaskDraftRepository",
"RoundtableTaskDraftRow",
"make_roundtable_task_draft_store",
]
def make_roundtable_task_draft_store(session_factory):
if session_factory is None:
return None
return RoundtableTaskDraftRepository(session_factory)
@@ -0,0 +1,55 @@
"""ORM model for **task-scoped** roundtable-planning drafts.
This is the dedicated, separate store for roundtable 聊天记录 opened through a
taskId deep-link (无界嵌入抽屉). Unlike :mod:`roundtable_drafts` (per-login-user
isolation), task drafts are keyed **purely by ``task_id``** and are **never
partitioned by user** — one ``task_id`` may have many records, all shared across
whoever opens that task. ``created_by`` is audit-only (never used for filtering).
Kept physically separate from ``roundtable_drafts`` so the two never mix: a
user's personal history (``GET /api/roundtable-drafts``) holds only their own
drafts, and task records live entirely here.
``step1`` / ``step2`` / ``step3`` are JSON serialized into ``PortableLongText``
(MySQL ``LONGTEXT``) — the Step 2 transcript / Step 3 report HTML easily exceed
MySQL's 64 KB ``TEXT`` cap — mirroring ``roundtable_drafts``.
"""
from __future__ import annotations
from datetime import UTC, datetime
from sqlalchemy import Integer, String
from sqlalchemy.orm import Mapped, mapped_column
from deerflow.persistence.base import Base
from deerflow.persistence.types import BeijingDateTime, PortableLongText
class RoundtableTaskDraftRow(Base):
__tablename__ = "roundtable_task_drafts"
id: Mapped[str] = mapped_column(String(64), primary_key=True)
# 外部任务 id(无界嵌入抽屉传入)。这是唯一的归属维度:所有读/写/删只按它过滤,
# 一个 task_id 可有多条记录、跨用户共享。必填、建索引。
task_id: Mapped[str] = mapped_column(String(128), nullable=False, index=True)
# 仅审计:建档时的登录用户。绝不参与任何过滤/鉴权(task 记录不分权)。
created_by: Mapped[str | None] = mapped_column(String(64), nullable=True)
# Auto-derived task summary (objective → first user message → timestamp).
title: Mapped[str] = mapped_column(String(512), nullable=False, default="")
# Furthest step reached when this snapshot was taken (1 | 2 | 3).
furthest_step: Mapped[int] = mapped_column(Integer, nullable=False, default=1)
# JSON blobs matching the frontend Step1Snapshot / Step2Snapshot / Step3 shapes.
step1: Mapped[str | None] = mapped_column(PortableLongText, nullable=True) # JSON object
step2: Mapped[str | None] = mapped_column(PortableLongText, nullable=True) # JSON object
step3: Mapped[str | None] = mapped_column(PortableLongText, nullable=True) # JSON object
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),
)
# 乐观并发控制版本号:每次 update_draft 自增;传入 expected_version 时校验,命中
# 0 行即并发冲突(防 step2.runs 读-改-写丢失更新——多用户/多 worker 并发)。
version: Mapped[int] = mapped_column(Integer, nullable=False, default=0, server_default="0")
@@ -0,0 +1,221 @@
"""SQLAlchemy-backed **task-scoped** roundtable-draft store.
Mirrors :mod:`roundtable_drafts` but every read/write/delete is keyed **only by
``task_id`` / row id — never by ``user_id``** (task records are shared, not
partitioned by user). ``created_by`` is stored for audit but never filtered on.
``get_draft`` / ``update_draft`` keep a ``user_id`` positional purely for
signature-compatibility with :class:`RoundtableDraftRepository` (so the job
executor can call either store uniformly); the value is ignored here.
JSON columns (``step1`` / ``step2`` / ``step3``) are stored as serialized strings
in ``PortableLongText`` and round-tripped with ``json.dumps`` / ``json.loads``.
"""
from __future__ import annotations
import json
import re
from datetime import UTC, datetime
from typing import Any
from sqlalchemy import case, select, update
from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker
from deerflow.persistence.roundtable_drafts.sql import DraftConcurrentWriteError
from deerflow.persistence.roundtable_task_drafts.model import RoundtableTaskDraftRow
_UNSET = object()
# 业务链名抽取:在已加载进内存的 step2 JSON 串里直接找 `"chain":{...,"title":"X"}`,
# 不整体 json.loads(step2 是 LONGTEXT 大块)。`"chain":null`(自由模式)不匹配 → None。
# 与 roundtable_drafts/sql.py 保持一致。
_CHAIN_TITLE_RE = re.compile(r'"chain"\s*:\s*\{[^{}]*?"title"\s*:\s*"((?:\\.|[^"\\])*)"')
def _extract_chain_title(step2_raw: Any) -> str | None:
"""从 step2 原始 JSON 串里抽取所用业务链名(无链条 → None,前端显示「自由模式」)。"""
if not isinstance(step2_raw, str) or not step2_raw:
return None
m = _CHAIN_TITLE_RE.search(step2_raw)
if not m:
return None
title = m.group(1)
try:
title = json.loads(f'"{title}"')
except Exception:
pass
title = title.strip()
return title or None
def _loads(value: Any) -> Any:
if not isinstance(value, str):
return value
if not value:
return None
try:
return json.loads(value)
except Exception:
return None
def _dumps(value: Any) -> str | None:
if value is None:
return None
return json.dumps(value, ensure_ascii=False)
class RoundtableTaskDraftRepository:
def __init__(self, session_factory: async_sessionmaker[AsyncSession]) -> None:
self._sf = session_factory
@staticmethod
def _to_meta(row: RoundtableTaskDraftRow) -> dict[str, Any]:
"""Lightweight projection for the history dropdown (no big blobs)."""
return {
"id": row.id,
"task_id": row.task_id,
"title": row.title,
"furthest_step": row.furthest_step,
# 乐观锁版本号:列表也携带,供前端本地副本对账(冲突时重拉合并)。
"version": row.version,
# 所用业务链名(历史卡片展示用);无链条 → None → 前端显示「自由模式」。
"chain_title": _extract_chain_title(row.step2),
"created_at": row.created_at.isoformat() if isinstance(row.created_at, datetime) else row.created_at,
"updated_at": row.updated_at.isoformat() if isinstance(row.updated_at, datetime) else row.updated_at,
}
@classmethod
def _to_dict(cls, row: RoundtableTaskDraftRow) -> dict[str, Any]:
d = cls._to_meta(row)
d["step1"] = _loads(row.step1)
d["step2"] = _loads(row.step2)
d["step3"] = _loads(row.step3)
return d
# ── reads (all keyed by task_id / id only) ───────────────────────────────
async def list_drafts_by_task(self, task_id: str, *, limit: int = 100) -> list[dict[str, Any]]:
"""All drafts for a task (lightweight meta, newest first). 不按 user 分权。"""
stmt = (
select(RoundtableTaskDraftRow)
.where(RoundtableTaskDraftRow.task_id == task_id)
.order_by(RoundtableTaskDraftRow.updated_at.desc())
.limit(limit)
)
async with self._sf() as session:
result = await session.execute(stmt)
return [self._to_meta(row) for row in result.scalars()]
async def get_latest_draft_by_task(self, task_id: str) -> dict[str, Any] | None:
"""Newest draft for a task (full), or None."""
stmt = (
select(RoundtableTaskDraftRow)
.where(RoundtableTaskDraftRow.task_id == task_id)
.order_by(RoundtableTaskDraftRow.updated_at.desc())
.limit(1)
)
async with self._sf() as session:
result = await session.execute(stmt)
row = result.scalars().first()
return self._to_dict(row) if row is not None else None
async def get_draft(self, draft_id: str, user_id: str | None = None) -> dict[str, Any] | None:
"""Full draft by id. ``user_id`` is ignored (signature-compat only)."""
async with self._sf() as session:
row = await session.get(RoundtableTaskDraftRow, draft_id)
return self._to_dict(row) if row is not None else None
# ── writes ───────────────────────────────────────────────────────────────
async def create_draft(self, data: dict[str, Any], *, created_by: str | None = None) -> dict[str, Any]:
task_id = (data.get("task_id") or "").strip()
if not task_id:
raise ValueError("task_id is required for a task draft")
now = datetime.now(UTC)
row = RoundtableTaskDraftRow(
id=data["id"],
task_id=task_id,
created_by=(created_by or None),
title=(data.get("title") or "").strip(),
furthest_step=int(data.get("furthest_step") or 1),
step1=_dumps(data.get("step1")),
step2=_dumps(data.get("step2")),
step3=_dumps(data.get("step3")),
created_at=now,
updated_at=now,
)
async with self._sf() as session:
session.add(row)
await session.commit()
await session.refresh(row)
return self._to_dict(row)
async def update_draft(
self,
draft_id: str,
user_id: str | None = None,
*,
title=_UNSET,
furthest_step=_UNSET,
step1=_UNSET,
step2=_UNSET,
step3=_UNSET,
expected_version: int | None = None,
) -> dict[str, Any] | None:
"""Partial update by id (``user_id`` ignored — signature-compat only).
与个人草稿一致:原子条件 UPDATE,版本判定与写入在同一条 SQL 里,
多 worker 并发下无检查后竞争窗口。``rowcount == 0`` 时回读区分
「不存在」与「版本冲突」。
"""
values: dict[str, Any] = {
"version": RoundtableTaskDraftRow.version + 1,
"updated_at": datetime.now(UTC),
}
if title is not _UNSET and title is not None:
values["title"] = str(title).strip()
if furthest_step is not _UNSET and furthest_step is not None:
# furthest_step 单调不回退:迟到的自动保存不得把已推进的步骤拉回去。
step_value = int(furthest_step)
values["furthest_step"] = case(
(RoundtableTaskDraftRow.furthest_step >= step_value, RoundtableTaskDraftRow.furthest_step),
else_=step_value,
)
if step1 is not _UNSET:
values["step1"] = _dumps(step1)
if step2 is not _UNSET:
values["step2"] = _dumps(step2)
if step3 is not _UNSET:
values["step3"] = _dumps(step3)
where = [RoundtableTaskDraftRow.id == draft_id]
if expected_version is not None:
where.append(RoundtableTaskDraftRow.version == expected_version)
async with self._sf() as session:
result = await session.execute(
update(RoundtableTaskDraftRow).where(*where).values(**values)
)
if result.rowcount == 0:
if expected_version is not None:
existing = await session.get(RoundtableTaskDraftRow, draft_id)
if existing is not None:
raise DraftConcurrentWriteError(
draft_id, expected=expected_version, actual=existing.version
)
return None
await session.commit()
row = await session.get(RoundtableTaskDraftRow, draft_id)
await session.refresh(row)
return self._to_dict(row)
async def delete_draft(self, draft_id: str) -> bool:
async with self._sf() as session:
row = await session.get(RoundtableTaskDraftRow, draft_id)
if row is None:
return False
await session.delete(row)
await session.commit()
return True