代码提交
This commit is contained in:
1 parent
f49def09ea
commit
45df500a9c
55 files changed
+3390
-461
No files matched your search
+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