代码提交
This commit is contained in:
1 parent
f49def09ea
commit
45df500a9c
55 files changed
+3390
-461
No files matched your search
+2
-2
@@ -102,9 +102,9 @@ class AppearanceSettings(BaseModel):
|
||||
toggle, and any future first-time loads.
|
||||
"""
|
||||
|
||||
default_theme: Literal["light", "dark", "lightblue", "lightgreen"] = Field(
|
||||
default_theme: Literal["light", "dark", "lightblue", "lightgreen", "iceblue"] = Field(
|
||||
default="light",
|
||||
description="Theme applied for users who haven't picked their own (light / dark / lightblue / lightgreen)",
|
||||
description="Theme applied for users who haven't picked their own (light / dark / lightblue / lightgreen / iceblue)",
|
||||
)
|
||||
default_skill_view: Literal["card", "list"] = Field(
|
||||
default="list",
|
||||
|
||||
+69
@@ -0,0 +1,69 @@
|
||||
"""Persist complete position-roundtable conversation recovery snapshots.
|
||||
|
||||
Revision ID: 20260723_05
|
||||
Revises: 20260721_04
|
||||
Create Date: 2026-07-23
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import sqlalchemy as sa
|
||||
from alembic import op
|
||||
from sqlalchemy import inspect
|
||||
from sqlalchemy.dialects.mysql import LONGTEXT
|
||||
|
||||
revision = "20260723_05"
|
||||
down_revision = "20260721_04"
|
||||
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 _has_column(table_name: str, column_name: str) -> bool:
|
||||
if not _has_table(table_name):
|
||||
return False
|
||||
return column_name in {
|
||||
column["name"] for column in inspect(op.get_bind()).get_columns(table_name)
|
||||
}
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
long_text = LONGTEXT() if op.get_bind().dialect.name == "mysql" else sa.Text()
|
||||
session_table = "position_roundtable_sessions"
|
||||
if _has_table(session_table):
|
||||
for column in (
|
||||
"intent_conversation",
|
||||
"summary_conversation",
|
||||
"action_plan_conversation",
|
||||
):
|
||||
if not _has_column(session_table, column):
|
||||
op.add_column(
|
||||
session_table,
|
||||
sa.Column(column, long_text, nullable=True),
|
||||
)
|
||||
|
||||
node_table = "position_roundtable_nodes"
|
||||
if _has_table(node_table) and not _has_column(node_table, "conversation_snapshot"):
|
||||
op.add_column(
|
||||
node_table,
|
||||
sa.Column("conversation_snapshot", long_text, nullable=True),
|
||||
)
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
node_table = "position_roundtable_nodes"
|
||||
if _has_table(node_table) and _has_column(node_table, "conversation_snapshot"):
|
||||
op.drop_column(node_table, "conversation_snapshot")
|
||||
|
||||
session_table = "position_roundtable_sessions"
|
||||
if _has_table(session_table):
|
||||
for column in (
|
||||
"action_plan_conversation",
|
||||
"summary_conversation",
|
||||
"intent_conversation",
|
||||
):
|
||||
if _has_column(session_table, column):
|
||||
op.drop_column(session_table, column)
|
||||
+16
-4
@@ -2,9 +2,10 @@
|
||||
|
||||
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.
|
||||
confirmed task intent; every chain seat then has one durable node record.
|
||||
LangGraph threads remain the execution source, while these tables also retain
|
||||
database-owned recovery copies of conversations and text deliverables so the
|
||||
workspace survives replacement of container-local checkpoint/output volumes.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
@@ -31,13 +32,20 @@ class PositionRoundtableSessionRow(Base):
|
||||
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)
|
||||
# Database-owned recovery copy for the complete Step-1 conversation. The
|
||||
# normal LangGraph checkpoint remains the execution source, while this
|
||||
# snapshot lets the position workspace recover after a checkpoint volume
|
||||
# or container-local store has been replaced.
|
||||
intent_conversation: Mapped[str | None] = mapped_column(PortableLongText, nullable=True)
|
||||
# The two built-in closing agents own normal LangGraph threads too. Keep
|
||||
# their ids on the position session so a refresh can continue the same
|
||||
# report/planning dialogue instead of silently starting a fresh context.
|
||||
summary_thread_id: Mapped[str | None] = mapped_column(String(128), nullable=True)
|
||||
summary_snapshot: Mapped[str | None] = mapped_column(PortableLongText, nullable=True)
|
||||
summary_conversation: Mapped[str | None] = mapped_column(PortableLongText, nullable=True)
|
||||
action_plan_thread_id: Mapped[str | None] = mapped_column(String(128), nullable=True)
|
||||
action_plan_snapshot: Mapped[str | None] = mapped_column(PortableLongText, nullable=True)
|
||||
action_plan_conversation: 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)
|
||||
@@ -71,12 +79,16 @@ class PositionRoundtableNodeRow(Base):
|
||||
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.
|
||||
# The LangGraph/checkpoint store remains the execution source for messages.
|
||||
thread_id: Mapped[str | None] = mapped_column(String(128), nullable=True, unique=True)
|
||||
# locked / ready / running / done / stale / rejected / 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)
|
||||
# A complete, JSON-serialisable copy of the node chat plus its composer
|
||||
# draft. It is intentionally colocated with the node so deleting a
|
||||
# position Session removes every recovery snapshot through the FK cascade.
|
||||
conversation_snapshot: 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)
|
||||
rejection_count: Mapped[int] = mapped_column(Integer, nullable=False, default=0, server_default="0")
|
||||
|
||||
+180
-2
@@ -5,9 +5,10 @@ from __future__ import annotations
|
||||
import json
|
||||
from datetime import UTC, datetime
|
||||
from typing import Any
|
||||
from uuid import uuid4
|
||||
from uuid import NAMESPACE_URL, uuid4, uuid5
|
||||
|
||||
from sqlalchemy import select
|
||||
from sqlalchemy import delete, select
|
||||
from sqlalchemy.exc import IntegrityError
|
||||
from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker
|
||||
|
||||
from deerflow.persistence.position_roundtable.model import (
|
||||
@@ -55,10 +56,13 @@ class PositionRoundtableRepository:
|
||||
"chain_snapshot": _loads(row.chain_snapshot),
|
||||
"intent_thread_id": row.intent_thread_id,
|
||||
"intent_snapshot": _loads(row.intent_snapshot),
|
||||
"intent_conversation": _loads(row.intent_conversation),
|
||||
"summary_thread_id": row.summary_thread_id,
|
||||
"summary_snapshot": _loads(row.summary_snapshot),
|
||||
"summary_conversation": _loads(row.summary_conversation),
|
||||
"action_plan_thread_id": row.action_plan_thread_id,
|
||||
"action_plan_snapshot": _loads(row.action_plan_snapshot),
|
||||
"action_plan_conversation": _loads(row.action_plan_conversation),
|
||||
"status": row.status,
|
||||
"created_at": _time(row.created_at),
|
||||
"updated_at": _time(row.updated_at),
|
||||
@@ -78,6 +82,7 @@ class PositionRoundtableRepository:
|
||||
"status": row.status,
|
||||
"latest_answer": row.latest_answer,
|
||||
"artifact_manifest": _loads(row.artifact_manifest) or [],
|
||||
"conversation_snapshot": _loads(row.conversation_snapshot),
|
||||
"revision": row.revision,
|
||||
"upstream_revision": _loads(row.upstream_revision),
|
||||
"rejection_count": row.rejection_count or 0,
|
||||
@@ -110,6 +115,134 @@ class PositionRoundtableRepository:
|
||||
await session.refresh(row)
|
||||
return self._session_dict(row)
|
||||
|
||||
async def ensure_demo_session(
|
||||
self,
|
||||
user_id: str,
|
||||
*,
|
||||
demo_key: str,
|
||||
session_payload: dict[str, Any],
|
||||
nodes: list[dict[str, Any]],
|
||||
) -> tuple[dict[str, Any], list[dict[str, Any]]]:
|
||||
"""Create one deterministic, user-scoped demonstration session.
|
||||
|
||||
The browser owns the canonical JSON fixture so the same payload can be
|
||||
rendered locally when the gateway is unavailable. When the gateway is
|
||||
reachable this method imports that payload exactly once. Stable ids
|
||||
make repeated initialisation and React StrictMode double effects safe,
|
||||
while the user id in every UUID seed avoids cross-account collisions
|
||||
on the globally unique LangGraph thread columns.
|
||||
"""
|
||||
|
||||
def stable_id(label: str) -> str:
|
||||
return uuid5(
|
||||
NAMESPACE_URL,
|
||||
f"deerflow:position-roundtable-demo:{user_id}:{demo_key}:{label}",
|
||||
).hex
|
||||
|
||||
session_id = stable_id("session")
|
||||
existing = await self.get_session(session_id, user_id)
|
||||
if existing is not None:
|
||||
return existing, (await self.list_nodes(session_id, user_id)) or []
|
||||
|
||||
now = datetime.now(UTC)
|
||||
intent_thread_id = (
|
||||
stable_id("intent-thread") if session_payload.get("intent_thread_id") else None
|
||||
)
|
||||
summary_thread_id = (
|
||||
stable_id("summary-thread") if session_payload.get("summary_thread_id") else None
|
||||
)
|
||||
action_plan_thread_id = (
|
||||
stable_id("action-plan-thread")
|
||||
if session_payload.get("action_plan_thread_id")
|
||||
else None
|
||||
)
|
||||
|
||||
def with_thread_id(value: Any, thread_id: str | None) -> Any:
|
||||
if not isinstance(value, dict) or not thread_id:
|
||||
return value
|
||||
return {**value, "thread_id": thread_id}
|
||||
|
||||
row = PositionRoundtableSessionRow(
|
||||
id=session_id,
|
||||
user_id=user_id,
|
||||
external_task_id=session_payload.get("external_task_id") or None,
|
||||
task_snapshot=_dumps(session_payload.get("task_snapshot") or {}),
|
||||
chain_id=session_payload.get("chain_id") or None,
|
||||
chain_snapshot=_dumps(session_payload.get("chain_snapshot")),
|
||||
intent_thread_id=intent_thread_id,
|
||||
intent_snapshot=_dumps(session_payload.get("intent_snapshot")),
|
||||
intent_conversation=_dumps(session_payload.get("intent_conversation")),
|
||||
summary_thread_id=summary_thread_id,
|
||||
summary_snapshot=_dumps(
|
||||
with_thread_id(session_payload.get("summary_snapshot"), summary_thread_id)
|
||||
),
|
||||
summary_conversation=_dumps(session_payload.get("summary_conversation")),
|
||||
action_plan_thread_id=action_plan_thread_id,
|
||||
action_plan_snapshot=_dumps(
|
||||
with_thread_id(
|
||||
session_payload.get("action_plan_snapshot"), action_plan_thread_id
|
||||
)
|
||||
),
|
||||
action_plan_conversation=_dumps(
|
||||
session_payload.get("action_plan_conversation")
|
||||
),
|
||||
status=str(session_payload.get("status") or "completed"),
|
||||
created_at=now,
|
||||
updated_at=now,
|
||||
)
|
||||
|
||||
node_rows: list[PositionRoundtableNodeRow] = []
|
||||
for index, node in enumerate(nodes):
|
||||
node_key = str(node.get("node_key") or f"demo-node-{index + 1}")
|
||||
node_rows.append(
|
||||
PositionRoundtableNodeRow(
|
||||
id=stable_id(f"node:{node_key}"),
|
||||
session_id=session_id,
|
||||
node_key=node_key,
|
||||
stage_index=int(node.get("stage_index") or 0),
|
||||
seat_index=int(node.get("seat_index") or 0),
|
||||
agent_id=str(node.get("agent_id") or f"demo-agent-{index + 1}"),
|
||||
position_id=node.get("position_id") or None,
|
||||
thread_id=(
|
||||
stable_id(f"node-thread:{node_key}")
|
||||
if node.get("thread_id")
|
||||
else None
|
||||
),
|
||||
status=str(node.get("status") or "done"),
|
||||
latest_answer=node.get("latest_answer") or None,
|
||||
artifact_manifest=_dumps(node.get("artifact_manifest") or []),
|
||||
conversation_snapshot=_dumps(node.get("conversation_snapshot")),
|
||||
revision=int(node.get("revision") or 0),
|
||||
upstream_revision=_dumps(node.get("upstream_revision")),
|
||||
rejection_count=int(node.get("rejection_count") or 0),
|
||||
last_rejection=_dumps(node.get("last_rejection")),
|
||||
invalidated_by=node.get("invalidated_by") or None,
|
||||
created_at=now,
|
||||
updated_at=now,
|
||||
)
|
||||
)
|
||||
|
||||
try:
|
||||
async with self._sf() as session:
|
||||
session.add(row)
|
||||
session.add_all(node_rows)
|
||||
await session.commit()
|
||||
await session.refresh(row)
|
||||
for node_row in node_rows:
|
||||
await session.refresh(node_row)
|
||||
except IntegrityError:
|
||||
# Another tab may have seeded the same user/demo key between the
|
||||
# initial lookup and commit. The deterministic row is the desired
|
||||
# result, so recover it instead of returning a spurious 500.
|
||||
existing = await self.get_session(session_id, user_id)
|
||||
if existing is None:
|
||||
raise
|
||||
return existing, (await self.list_nodes(session_id, user_id)) or []
|
||||
|
||||
return self._session_dict(row), [
|
||||
self._node_dict(node_row) for node_row in node_rows
|
||||
]
|
||||
|
||||
async def list_sessions(
|
||||
self, user_id: str, *, external_task_id: str | None = None, limit: int = 50
|
||||
) -> list[dict[str, Any]]:
|
||||
@@ -136,10 +269,13 @@ class PositionRoundtableRepository:
|
||||
task_snapshot: dict[str, Any] | object = _UNSET,
|
||||
intent_thread_id: str | None | object = _UNSET,
|
||||
intent_snapshot: dict[str, Any] | None | object = _UNSET,
|
||||
intent_conversation: dict[str, Any] | None | object = _UNSET,
|
||||
summary_thread_id: str | None | object = _UNSET,
|
||||
summary_snapshot: dict[str, Any] | None | object = _UNSET,
|
||||
summary_conversation: dict[str, Any] | None | object = _UNSET,
|
||||
action_plan_thread_id: str | None | object = _UNSET,
|
||||
action_plan_snapshot: dict[str, Any] | None | object = _UNSET,
|
||||
action_plan_conversation: dict[str, Any] | None | object = _UNSET,
|
||||
status: str | object = _UNSET,
|
||||
) -> dict[str, Any] | None:
|
||||
async with self._sf() as session:
|
||||
@@ -152,14 +288,20 @@ class PositionRoundtableRepository:
|
||||
row.intent_thread_id = intent_thread_id or None
|
||||
if intent_snapshot is not _UNSET:
|
||||
row.intent_snapshot = _dumps(intent_snapshot)
|
||||
if intent_conversation is not _UNSET:
|
||||
row.intent_conversation = _dumps(intent_conversation)
|
||||
if summary_thread_id is not _UNSET:
|
||||
row.summary_thread_id = summary_thread_id or None
|
||||
if summary_snapshot is not _UNSET:
|
||||
row.summary_snapshot = _dumps(summary_snapshot)
|
||||
if summary_conversation is not _UNSET:
|
||||
row.summary_conversation = _dumps(summary_conversation)
|
||||
if action_plan_thread_id is not _UNSET:
|
||||
row.action_plan_thread_id = action_plan_thread_id or None
|
||||
if action_plan_snapshot is not _UNSET:
|
||||
row.action_plan_snapshot = _dumps(action_plan_snapshot)
|
||||
if action_plan_conversation is not _UNSET:
|
||||
row.action_plan_conversation = _dumps(action_plan_conversation)
|
||||
if status is not _UNSET:
|
||||
row.status = str(status)
|
||||
row.updated_at = datetime.now(UTC)
|
||||
@@ -265,6 +407,7 @@ class PositionRoundtableRepository:
|
||||
status: str | object = _UNSET,
|
||||
latest_answer: str | None | object = _UNSET,
|
||||
artifact_manifest: list[dict[str, Any]] | object = _UNSET,
|
||||
conversation_snapshot: dict[str, Any] | None | object = _UNSET,
|
||||
upstream_revision: dict[str, Any] | object = _UNSET,
|
||||
last_rejection: dict[str, Any] | None | object = _UNSET,
|
||||
invalidated_by: str | None | object = _UNSET,
|
||||
@@ -293,6 +436,8 @@ class PositionRoundtableRepository:
|
||||
row.latest_answer = latest_answer
|
||||
if artifact_manifest is not _UNSET:
|
||||
row.artifact_manifest = _dumps(artifact_manifest)
|
||||
if conversation_snapshot is not _UNSET:
|
||||
row.conversation_snapshot = _dumps(conversation_snapshot)
|
||||
if upstream_revision is not _UNSET:
|
||||
row.upstream_revision = _dumps(upstream_revision)
|
||||
if last_rejection is not _UNSET:
|
||||
@@ -392,5 +537,38 @@ class PositionRoundtableRepository:
|
||||
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 delete_session(self, session_id: str, user_id: str) -> bool:
|
||||
"""Delete one user-owned history record and all of its node snapshots."""
|
||||
async with self._sf() as session:
|
||||
parent = (
|
||||
await session.execute(
|
||||
select(PositionRoundtableSessionRow)
|
||||
.where(
|
||||
PositionRoundtableSessionRow.id == session_id,
|
||||
PositionRoundtableSessionRow.user_id == user_id,
|
||||
)
|
||||
.limit(1)
|
||||
)
|
||||
).scalar_one_or_none()
|
||||
if parent is None:
|
||||
return False
|
||||
|
||||
# Do not rely on database-specific FK cascade settings. SQLite
|
||||
# deployments may not have PRAGMA foreign_keys enabled, which
|
||||
# would otherwise leave orphaned conversation/artifact snapshots.
|
||||
await session.execute(
|
||||
delete(PositionRoundtableNodeRow).where(
|
||||
PositionRoundtableNodeRow.session_id == session_id
|
||||
)
|
||||
)
|
||||
await session.execute(
|
||||
delete(PositionRoundtableSessionRow).where(
|
||||
PositionRoundtableSessionRow.id == session_id,
|
||||
PositionRoundtableSessionRow.user_id == user_id,
|
||||
)
|
||||
)
|
||||
await session.commit()
|
||||
return True
|
||||
|
||||
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