feat: 新增独立企业深度研究工作台,并接入工作流报告结构与 8BF 深色锁定
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
e881c242fe
commit
11f928bc95
24 files changed
+3275
-40
No files matched your search
+31
@@ -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)
|
||||
+75
@@ -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
|
||||
+172
@@ -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]
|
||||
+77
@@ -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)
|
||||
)
|
||||
+320
@@ -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]
|
||||
+3
@@ -46,6 +46,7 @@ from deerflow.persistence.deep_research_sessions.model import DeepResearchSessio
|
||||
from deerflow.persistence.deep_research_sources.model import DeepResearchSourceRow
|
||||
from deerflow.persistence.document_rewrite_versions.model import DocumentRewriteVersionRow
|
||||
from deerflow.persistence.embed_sessions.model import EmbedSessionRow
|
||||
from deerflow.persistence.enterprise_research.model import EnterpriseResearchReportJobRow, EnterpriseResearchTaskRow
|
||||
from deerflow.persistence.feedback.model import FeedbackRow
|
||||
from deerflow.persistence.intent_turns.model import IntentTurnRow
|
||||
from deerflow.persistence.light_apps.model import LightAppRow
|
||||
@@ -162,6 +163,8 @@ __all__ = [
|
||||
"TaskCopTaskRow",
|
||||
"TaskReportRow",
|
||||
"EmbedSessionRow",
|
||||
"EnterpriseResearchTaskRow",
|
||||
"EnterpriseResearchReportJobRow",
|
||||
"IntentTurnRow",
|
||||
"ThreadMetaRow",
|
||||
"ThreadShareRow",
|
||||
|
||||
Reference in new issue
Block a user