deerflow-code/offline-backend-20260512/backend/packages/harness/deerflow/persistence/dashboard_sessions/model.py
2026-09-07 18:24:55 +08:00

50 lines
2.4 KiB
Python

"""ORM model for the standalone 大屏绘制智能体 session (按 taskId 存取).
One row per external ``task_id`` — the standalone dashboard page persists its
chat transcript + the latest structured ``report_json`` so that returning to the
same task either shows the finished dashboard directly (valid report_json) or
restores the generation process.
**Global / shared by task** — like ``roundtable_drafts`` task-bound drafts, the
store keys on ``task_id`` and does **not** scope by ``user_id`` (open read +
shared writes). ``user_id`` is recorded for audit only.
``transcript`` / ``report_json`` are large JSON blobs (chat history + report
data) stored as serialized strings in ``PortableLongText`` (MySQL ``LONGTEXT``)
to dodge MySQL's 64 KB ``TEXT`` cap — same approach as ``roundtable_drafts``.
"""
from __future__ import annotations
from datetime import UTC, datetime
from sqlalchemy import String, UniqueConstraint
from sqlalchemy.orm import Mapped, mapped_column
from deerflow.persistence.base import Base
from deerflow.persistence.types import BeijingDateTime, PortableLongText
class DashboardSessionRow(Base):
__tablename__ = "dashboard_sessions"
# One session per task — keep it unique so upsert-by-task is unambiguous.
__table_args__ = (UniqueConstraint("task_id", name="uq_dashboard_sessions_task_id"),)
id: Mapped[str] = mapped_column(String(64), primary_key=True)
# External task id (deep-link). The session is shared by task, not by user.
task_id: Mapped[str] = mapped_column(String(128), nullable=False, index=True)
# Audit only — who last wrote. Never used for access control.
user_id: Mapped[str | None] = mapped_column(String(64), nullable=True)
# Chat transcript JSON (the dashboard agent's dialogue bubbles).
transcript: Mapped[str | None] = mapped_column(PortableLongText, nullable=True)
# Latest structured dashboard data (report-json string). Presence of a
# parseable value = "有效结果" → the page shows the dashboard directly.
report_json: 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),
)