feat(position-roundtable): add standalone position collaboration
This commit is contained in:
1 parent
b93b82fcaf
commit
85e119db08
22 files changed
+3494
-201
No files matched your search
+87
@@ -0,0 +1,87 @@
|
||||
"""Create standalone position-collaboration sessions and nodes.
|
||||
|
||||
The tables only index the task / intent / frozen business-chain workflow.
|
||||
Conversation messages remain in LangGraph checkpoints and artifacts stay in the
|
||||
existing thread artifact store, so this migration intentionally has no message
|
||||
or file columns beyond lightweight snapshots and manifests.
|
||||
|
||||
Revision ID: 20260721_01
|
||||
Revises: 20260701_01
|
||||
Create Date: 2026-07-21
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import sqlalchemy as sa
|
||||
from alembic import op
|
||||
from sqlalchemy import inspect
|
||||
from sqlalchemy.dialects.mysql import LONGTEXT
|
||||
|
||||
|
||||
revision = "20260721_01"
|
||||
down_revision = "20260701_01"
|
||||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
|
||||
def _has_table(table_name: str) -> bool:
|
||||
return table_name in inspect(op.get_bind()).get_table_names()
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
long_text = LONGTEXT() if op.get_bind().dialect.name == "mysql" else sa.Text()
|
||||
if not _has_table("position_roundtable_sessions"):
|
||||
op.create_table(
|
||||
"position_roundtable_sessions",
|
||||
sa.Column("id", sa.String(64), primary_key=True),
|
||||
sa.Column("user_id", sa.String(64), nullable=False),
|
||||
sa.Column("external_task_id", sa.String(128), nullable=True),
|
||||
sa.Column("task_snapshot", long_text, nullable=True),
|
||||
sa.Column("chain_id", sa.String(64), nullable=True),
|
||||
sa.Column("chain_snapshot", long_text, nullable=True),
|
||||
sa.Column("intent_thread_id", sa.String(128), nullable=True),
|
||||
sa.Column("intent_snapshot", long_text, nullable=True),
|
||||
sa.Column("status", sa.String(32), nullable=False, server_default="intent_pending"),
|
||||
sa.Column("created_at", sa.DateTime(), nullable=False),
|
||||
sa.Column("updated_at", sa.DateTime(), nullable=False),
|
||||
)
|
||||
op.create_index("ix_position_roundtable_sessions_user_id", "position_roundtable_sessions", ["user_id"])
|
||||
op.create_index(
|
||||
"ix_position_roundtable_sessions_external_task_id",
|
||||
"position_roundtable_sessions",
|
||||
["external_task_id"],
|
||||
)
|
||||
op.create_index("ix_position_roundtable_sessions_chain_id", "position_roundtable_sessions", ["chain_id"])
|
||||
op.create_index("ix_position_roundtable_sessions_status", "position_roundtable_sessions", ["status"])
|
||||
|
||||
if not _has_table("position_roundtable_nodes"):
|
||||
op.create_table(
|
||||
"position_roundtable_nodes",
|
||||
sa.Column("id", sa.String(64), primary_key=True),
|
||||
sa.Column("session_id", sa.String(64), sa.ForeignKey("position_roundtable_sessions.id", ondelete="CASCADE"), nullable=False),
|
||||
sa.Column("node_key", sa.String(256), nullable=False),
|
||||
sa.Column("stage_index", sa.Integer(), nullable=False),
|
||||
sa.Column("seat_index", sa.Integer(), nullable=False),
|
||||
sa.Column("agent_id", sa.String(128), nullable=False),
|
||||
sa.Column("position_id", sa.String(64), nullable=True),
|
||||
sa.Column("thread_id", sa.String(128), nullable=True, unique=True),
|
||||
sa.Column("status", sa.String(32), nullable=False, server_default="locked"),
|
||||
sa.Column("latest_answer", long_text, nullable=True),
|
||||
sa.Column("artifact_manifest", long_text, nullable=True),
|
||||
sa.Column("revision", sa.Integer(), nullable=False, server_default="0"),
|
||||
sa.Column("upstream_revision", long_text, nullable=True),
|
||||
sa.Column("created_at", sa.DateTime(), nullable=False),
|
||||
sa.Column("updated_at", sa.DateTime(), nullable=False),
|
||||
sa.UniqueConstraint("session_id", "node_key", name="uq_position_roundtable_nodes_session_key"),
|
||||
)
|
||||
op.create_index("ix_position_roundtable_nodes_session_id", "position_roundtable_nodes", ["session_id"])
|
||||
op.create_index("ix_position_roundtable_nodes_agent_id", "position_roundtable_nodes", ["agent_id"])
|
||||
op.create_index("ix_position_roundtable_nodes_position_id", "position_roundtable_nodes", ["position_id"])
|
||||
op.create_index("ix_position_roundtable_nodes_status", "position_roundtable_nodes", ["status"])
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
if _has_table("position_roundtable_nodes"):
|
||||
op.drop_table("position_roundtable_nodes")
|
||||
if _has_table("position_roundtable_sessions"):
|
||||
op.drop_table("position_roundtable_sessions")
|
||||
+6
@@ -46,6 +46,10 @@ from deerflow.persistence.menu_overrides.model import MenuOverrideRow
|
||||
from deerflow.persistence.models.run_event import RunEventRow
|
||||
from deerflow.persistence.notifications.model import NotificationRow
|
||||
from deerflow.persistence.parallel_agent_tasks.model import ParallelAgentTaskRow
|
||||
from deerflow.persistence.position_roundtable.model import (
|
||||
PositionRoundtableNodeRow,
|
||||
PositionRoundtableSessionRow,
|
||||
)
|
||||
from deerflow.persistence.positions.model import PositionRow, PositionTagBindingRow
|
||||
from deerflow.persistence.recommended_questions.model import RecommendedQuestionRow
|
||||
from deerflow.persistence.research_templates.model import ResearchTemplateRow
|
||||
@@ -104,6 +108,8 @@ __all__ = [
|
||||
"MenuOverrideRow",
|
||||
"NotificationRow",
|
||||
"ParallelAgentTaskRow",
|
||||
"PositionRoundtableNodeRow",
|
||||
"PositionRoundtableSessionRow",
|
||||
"PositionRow",
|
||||
"PositionTagBindingRow",
|
||||
"RecommendedQuestionRow",
|
||||
|
||||
+22
@@ -0,0 +1,22 @@
|
||||
"""Position-collaboration session and node persistence."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from deerflow.persistence.position_roundtable.model import (
|
||||
PositionRoundtableNodeRow,
|
||||
PositionRoundtableSessionRow,
|
||||
)
|
||||
from deerflow.persistence.position_roundtable.sql import PositionRoundtableRepository
|
||||
|
||||
__all__ = [
|
||||
"PositionRoundtableNodeRow",
|
||||
"PositionRoundtableRepository",
|
||||
"PositionRoundtableSessionRow",
|
||||
"make_position_roundtable_store",
|
||||
]
|
||||
|
||||
|
||||
def make_position_roundtable_store(session_factory):
|
||||
if session_factory is None:
|
||||
return None
|
||||
return PositionRoundtableRepository(session_factory)
|
||||
+83
@@ -0,0 +1,83 @@
|
||||
"""Persistence models for the standalone position-collaboration workspace.
|
||||
|
||||
This deliberately does **not** reuse ``roundtable_drafts`` or
|
||||
``roundtable_jobs``. A session captures the selected business chain and the
|
||||
confirmed task intent; every chain seat then has one durable node record. The
|
||||
node points at an existing LangGraph thread, which continues to own the actual
|
||||
message history and generated artifacts.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from datetime import UTC, datetime
|
||||
|
||||
from sqlalchemy import ForeignKey, Integer, String, UniqueConstraint
|
||||
from sqlalchemy.orm import Mapped, mapped_column
|
||||
|
||||
from deerflow.persistence.base import Base
|
||||
from deerflow.persistence.types import BeijingDateTime, PortableLongText
|
||||
|
||||
|
||||
class PositionRoundtableSessionRow(Base):
|
||||
__tablename__ = "position_roundtable_sessions"
|
||||
|
||||
id: Mapped[str] = mapped_column(String(64), primary_key=True)
|
||||
# Phase one uses normal account isolation. A future membership / role ACL
|
||||
# can be added without changing the session or thread identifiers.
|
||||
user_id: Mapped[str] = mapped_column(String(64), nullable=False, index=True)
|
||||
external_task_id: Mapped[str | None] = mapped_column(String(128), nullable=True, index=True)
|
||||
task_snapshot: Mapped[str | None] = mapped_column(PortableLongText, nullable=True)
|
||||
chain_id: Mapped[str | None] = mapped_column(String(64), nullable=True, index=True)
|
||||
chain_snapshot: Mapped[str | None] = mapped_column(PortableLongText, nullable=True)
|
||||
intent_thread_id: Mapped[str | None] = mapped_column(String(128), nullable=True)
|
||||
intent_snapshot: Mapped[str | None] = mapped_column(PortableLongText, nullable=True)
|
||||
# intent_pending -> active -> completed / archived. The router owns legal
|
||||
# transitions; the DB keeps it as a concise, portable string.
|
||||
status: Mapped[str] = mapped_column(String(32), nullable=False, default="intent_pending", index=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 PositionRoundtableNodeRow(Base):
|
||||
__tablename__ = "position_roundtable_nodes"
|
||||
__table_args__ = (
|
||||
UniqueConstraint("session_id", "node_key", name="uq_position_roundtable_nodes_session_key"),
|
||||
)
|
||||
|
||||
id: Mapped[str] = mapped_column(String(64), primary_key=True)
|
||||
session_id: Mapped[str] = mapped_column(
|
||||
String(64),
|
||||
ForeignKey("position_roundtable_sessions.id", ondelete="CASCADE"),
|
||||
nullable=False,
|
||||
index=True,
|
||||
)
|
||||
# Stable inside one frozen chain snapshot; e.g. ``stage-1-seat-2-agent``.
|
||||
node_key: Mapped[str] = mapped_column(String(256), nullable=False)
|
||||
stage_index: Mapped[int] = mapped_column(Integer, nullable=False)
|
||||
seat_index: Mapped[int] = mapped_column(Integer, nullable=False)
|
||||
agent_id: Mapped[str] = mapped_column(String(128), nullable=False, index=True)
|
||||
position_id: Mapped[str | None] = mapped_column(String(64), nullable=True, index=True)
|
||||
# The real messages remain in the standard LangGraph/checkpoint store.
|
||||
thread_id: Mapped[str | None] = mapped_column(String(128), nullable=True, unique=True)
|
||||
# locked / ready / running / done / stale / error
|
||||
status: Mapped[str] = mapped_column(String(32), nullable=False, default="locked", index=True)
|
||||
latest_answer: Mapped[str | None] = mapped_column(PortableLongText, nullable=True)
|
||||
artifact_manifest: Mapped[str | None] = mapped_column(PortableLongText, nullable=True)
|
||||
revision: Mapped[int] = mapped_column(Integer, nullable=False, default=0)
|
||||
upstream_revision: 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),
|
||||
)
|
||||
+279
@@ -0,0 +1,279 @@
|
||||
"""SQL repository for position-collaboration sessions and chain nodes."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
from datetime import UTC, datetime
|
||||
from typing import Any
|
||||
from uuid import uuid4
|
||||
|
||||
from sqlalchemy import select
|
||||
from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker
|
||||
|
||||
from deerflow.persistence.position_roundtable.model import (
|
||||
PositionRoundtableNodeRow,
|
||||
PositionRoundtableSessionRow,
|
||||
)
|
||||
|
||||
_UNSET = object()
|
||||
|
||||
|
||||
def _dumps(value: Any) -> str | None:
|
||||
if value is None:
|
||||
return None
|
||||
return json.dumps(value, ensure_ascii=False)
|
||||
|
||||
|
||||
def _loads(value: Any) -> Any:
|
||||
if not isinstance(value, str) or not value:
|
||||
return None
|
||||
try:
|
||||
return json.loads(value)
|
||||
except Exception:
|
||||
return None
|
||||
|
||||
|
||||
def _time(value: Any) -> str | Any:
|
||||
return value.isoformat() if isinstance(value, datetime) else value
|
||||
|
||||
|
||||
class PositionRoundtableRepository:
|
||||
def __init__(self, session_factory: async_sessionmaker[AsyncSession]) -> None:
|
||||
self._sf = session_factory
|
||||
|
||||
@staticmethod
|
||||
def _session_dict(row: PositionRoundtableSessionRow) -> dict[str, Any]:
|
||||
return {
|
||||
"id": row.id,
|
||||
"external_task_id": row.external_task_id,
|
||||
"task_snapshot": _loads(row.task_snapshot) or {},
|
||||
"chain_id": row.chain_id,
|
||||
"chain_snapshot": _loads(row.chain_snapshot),
|
||||
"intent_thread_id": row.intent_thread_id,
|
||||
"intent_snapshot": _loads(row.intent_snapshot),
|
||||
"status": row.status,
|
||||
"created_at": _time(row.created_at),
|
||||
"updated_at": _time(row.updated_at),
|
||||
}
|
||||
|
||||
@staticmethod
|
||||
def _node_dict(row: PositionRoundtableNodeRow) -> dict[str, Any]:
|
||||
return {
|
||||
"id": row.id,
|
||||
"session_id": row.session_id,
|
||||
"node_key": row.node_key,
|
||||
"stage_index": row.stage_index,
|
||||
"seat_index": row.seat_index,
|
||||
"agent_id": row.agent_id,
|
||||
"position_id": row.position_id,
|
||||
"thread_id": row.thread_id,
|
||||
"status": row.status,
|
||||
"latest_answer": row.latest_answer,
|
||||
"artifact_manifest": _loads(row.artifact_manifest) or [],
|
||||
"revision": row.revision,
|
||||
"upstream_revision": _loads(row.upstream_revision),
|
||||
"created_at": _time(row.created_at),
|
||||
"updated_at": _time(row.updated_at),
|
||||
}
|
||||
|
||||
async def create_session(
|
||||
self,
|
||||
user_id: str,
|
||||
*,
|
||||
external_task_id: str | None,
|
||||
task_snapshot: dict[str, Any] | None,
|
||||
) -> dict[str, Any]:
|
||||
now = datetime.now(UTC)
|
||||
row = PositionRoundtableSessionRow(
|
||||
id=uuid4().hex,
|
||||
user_id=user_id,
|
||||
external_task_id=external_task_id or None,
|
||||
task_snapshot=_dumps(task_snapshot or {}),
|
||||
status="intent_pending",
|
||||
created_at=now,
|
||||
updated_at=now,
|
||||
)
|
||||
async with self._sf() as session:
|
||||
session.add(row)
|
||||
await session.commit()
|
||||
await session.refresh(row)
|
||||
return self._session_dict(row)
|
||||
|
||||
async def list_sessions(
|
||||
self, user_id: str, *, external_task_id: str | None = None, limit: int = 50
|
||||
) -> list[dict[str, Any]]:
|
||||
stmt = select(PositionRoundtableSessionRow).where(PositionRoundtableSessionRow.user_id == user_id)
|
||||
if external_task_id:
|
||||
stmt = stmt.where(PositionRoundtableSessionRow.external_task_id == external_task_id)
|
||||
stmt = stmt.order_by(PositionRoundtableSessionRow.updated_at.desc()).limit(limit)
|
||||
async with self._sf() as session:
|
||||
rows = (await session.execute(stmt)).scalars().all()
|
||||
return [self._session_dict(row) for row in rows]
|
||||
|
||||
async def get_session(self, session_id: str, user_id: str) -> dict[str, Any] | None:
|
||||
async with self._sf() as session:
|
||||
row = await session.get(PositionRoundtableSessionRow, session_id)
|
||||
if row is None or row.user_id != user_id:
|
||||
return None
|
||||
return self._session_dict(row)
|
||||
|
||||
async def update_session(
|
||||
self,
|
||||
session_id: str,
|
||||
user_id: str,
|
||||
*,
|
||||
task_snapshot: dict[str, Any] | object = _UNSET,
|
||||
intent_thread_id: str | None | object = _UNSET,
|
||||
intent_snapshot: dict[str, Any] | None | object = _UNSET,
|
||||
status: str | object = _UNSET,
|
||||
) -> dict[str, Any] | None:
|
||||
async with self._sf() as session:
|
||||
row = await session.get(PositionRoundtableSessionRow, session_id)
|
||||
if row is None or row.user_id != user_id:
|
||||
return None
|
||||
if task_snapshot is not _UNSET:
|
||||
row.task_snapshot = _dumps(task_snapshot)
|
||||
if intent_thread_id is not _UNSET:
|
||||
row.intent_thread_id = intent_thread_id or None
|
||||
if intent_snapshot is not _UNSET:
|
||||
row.intent_snapshot = _dumps(intent_snapshot)
|
||||
if status is not _UNSET:
|
||||
row.status = str(status)
|
||||
row.updated_at = datetime.now(UTC)
|
||||
await session.commit()
|
||||
await session.refresh(row)
|
||||
return self._session_dict(row)
|
||||
|
||||
async def activate_session(
|
||||
self,
|
||||
session_id: str,
|
||||
user_id: str,
|
||||
*,
|
||||
chain_id: str,
|
||||
chain_snapshot: dict[str, Any],
|
||||
nodes: list[dict[str, Any]],
|
||||
) -> tuple[dict[str, Any], list[dict[str, Any]]] | None:
|
||||
"""Freeze a chain snapshot and create its nodes once.
|
||||
|
||||
Repeated requests are intentionally idempotent: once nodes exist the
|
||||
existing snapshot and nodes are returned unchanged.
|
||||
"""
|
||||
now = datetime.now(UTC)
|
||||
async with self._sf() as session:
|
||||
row = await session.get(PositionRoundtableSessionRow, session_id)
|
||||
if row is None or row.user_id != user_id:
|
||||
return None
|
||||
node_rows = (
|
||||
await session.execute(
|
||||
select(PositionRoundtableNodeRow)
|
||||
.where(PositionRoundtableNodeRow.session_id == session_id)
|
||||
.order_by(PositionRoundtableNodeRow.stage_index, PositionRoundtableNodeRow.seat_index)
|
||||
)
|
||||
).scalars().all()
|
||||
if node_rows:
|
||||
return self._session_dict(row), [self._node_dict(node) for node in node_rows]
|
||||
|
||||
row.chain_id = chain_id
|
||||
row.chain_snapshot = _dumps(chain_snapshot)
|
||||
row.status = "active"
|
||||
row.updated_at = now
|
||||
node_rows = [
|
||||
PositionRoundtableNodeRow(
|
||||
id=uuid4().hex,
|
||||
session_id=session_id,
|
||||
node_key=str(node["node_key"]),
|
||||
stage_index=int(node["stage_index"]),
|
||||
seat_index=int(node["seat_index"]),
|
||||
agent_id=str(node["agent_id"]),
|
||||
position_id=node.get("position_id") or None,
|
||||
status=str(node.get("status") or "locked"),
|
||||
created_at=now,
|
||||
updated_at=now,
|
||||
)
|
||||
for node in nodes
|
||||
]
|
||||
session.add_all(node_rows)
|
||||
await session.commit()
|
||||
await session.refresh(row)
|
||||
for node in node_rows:
|
||||
await session.refresh(node)
|
||||
return self._session_dict(row), [self._node_dict(node) for node in node_rows]
|
||||
|
||||
async def list_nodes(self, session_id: str, user_id: str) -> list[dict[str, Any]] | None:
|
||||
async with self._sf() as session:
|
||||
parent = await session.get(PositionRoundtableSessionRow, session_id)
|
||||
if parent is None or parent.user_id != user_id:
|
||||
return None
|
||||
rows = (
|
||||
await session.execute(
|
||||
select(PositionRoundtableNodeRow)
|
||||
.where(PositionRoundtableNodeRow.session_id == session_id)
|
||||
.order_by(PositionRoundtableNodeRow.stage_index, PositionRoundtableNodeRow.seat_index)
|
||||
)
|
||||
).scalars().all()
|
||||
return [self._node_dict(row) for row in rows]
|
||||
|
||||
async def get_node(self, session_id: str, node_key: str, user_id: str) -> dict[str, Any] | None:
|
||||
async with self._sf() as session:
|
||||
stmt = (
|
||||
select(PositionRoundtableNodeRow)
|
||||
.join(PositionRoundtableSessionRow, PositionRoundtableSessionRow.id == PositionRoundtableNodeRow.session_id)
|
||||
.where(
|
||||
PositionRoundtableNodeRow.session_id == session_id,
|
||||
PositionRoundtableNodeRow.node_key == node_key,
|
||||
PositionRoundtableSessionRow.user_id == user_id,
|
||||
)
|
||||
.limit(1)
|
||||
)
|
||||
row = (await session.execute(stmt)).scalars().first()
|
||||
return self._node_dict(row) if row is not None else None
|
||||
|
||||
async def update_node(
|
||||
self,
|
||||
session_id: str,
|
||||
node_key: str,
|
||||
user_id: str,
|
||||
*,
|
||||
thread_id: str | None | object = _UNSET,
|
||||
status: str | object = _UNSET,
|
||||
latest_answer: str | None | object = _UNSET,
|
||||
artifact_manifest: list[dict[str, Any]] | object = _UNSET,
|
||||
upstream_revision: dict[str, Any] | object = _UNSET,
|
||||
increment_revision: bool = False,
|
||||
) -> dict[str, Any] | None:
|
||||
async with self._sf() as session:
|
||||
stmt = (
|
||||
select(PositionRoundtableNodeRow)
|
||||
.join(PositionRoundtableSessionRow, PositionRoundtableSessionRow.id == PositionRoundtableNodeRow.session_id)
|
||||
.where(
|
||||
PositionRoundtableNodeRow.session_id == session_id,
|
||||
PositionRoundtableNodeRow.node_key == node_key,
|
||||
PositionRoundtableSessionRow.user_id == user_id,
|
||||
)
|
||||
.limit(1)
|
||||
)
|
||||
row = (await session.execute(stmt)).scalars().first()
|
||||
if row is None:
|
||||
return None
|
||||
if thread_id is not _UNSET:
|
||||
row.thread_id = thread_id or None
|
||||
if status is not _UNSET:
|
||||
row.status = str(status)
|
||||
if latest_answer is not _UNSET:
|
||||
row.latest_answer = latest_answer
|
||||
if artifact_manifest is not _UNSET:
|
||||
row.artifact_manifest = _dumps(artifact_manifest)
|
||||
if upstream_revision is not _UNSET:
|
||||
row.upstream_revision = _dumps(upstream_revision)
|
||||
if increment_revision:
|
||||
row.revision += 1
|
||||
row.updated_at = datetime.now(UTC)
|
||||
await session.commit()
|
||||
await session.refresh(row)
|
||||
return self._node_dict(row)
|
||||
|
||||
async def archive_session(self, session_id: str, user_id: str) -> dict[str, Any] | None:
|
||||
return await self.update_session(session_id, user_id, status="archived")
|
||||
|
||||
async def list_results(self, session_id: str, user_id: str) -> list[dict[str, Any]] | None:
|
||||
return await self.list_nodes(session_id, user_id)
|
||||
Reference in new issue
Block a user