44 lines
2.4 KiB
Python
44 lines
2.4 KiB
Python
"""ORM model for thread metadata."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from datetime import UTC, datetime
|
|
|
|
from sqlalchemy import Index, String
|
|
from sqlalchemy.orm import Mapped, mapped_column
|
|
|
|
from deerflow.persistence.base import Base
|
|
from deerflow.persistence.types import BeijingDateTime, PortableJSON
|
|
|
|
|
|
class ThreadMetaRow(Base):
|
|
__tablename__ = "threads_meta"
|
|
__table_args__ = (
|
|
Index("ix_threads_meta_created_user", "created_at", "user_id"),
|
|
Index("ix_threads_meta_user_created", "user_id", "created_at"),
|
|
# Task deep-link history is globally shared and therefore cannot use
|
|
# the normal ``user_id`` scope. Keep its high-cardinality lookup
|
|
# fields out of ``metadata_json`` so task entry never has to scan and
|
|
# Python-filter every thread in the installation.
|
|
Index("ix_threads_meta_task_agent_updated", "task_id", "agent_id", "updated_at", "thread_id"),
|
|
Index("ix_threads_meta_task_action_agent_updated", "task_id", "action_id", "agent_id", "updated_at", "thread_id"),
|
|
# The ordinary recent-chat path has an owner filter and the same
|
|
# updated-at ordering. This prevents a full table sort there too.
|
|
Index("ix_threads_meta_user_updated", "user_id", "updated_at", "thread_id"),
|
|
)
|
|
|
|
thread_id: Mapped[str] = mapped_column(String(64), primary_key=True)
|
|
assistant_id: Mapped[str | None] = mapped_column(String(128), index=True)
|
|
user_id: Mapped[str | None] = mapped_column(String(64), index=True)
|
|
# Indexed projections of the corresponding metadata values. The JSON
|
|
# blob remains authoritative and supports arbitrary metadata filters;
|
|
# these columns exist solely for the task-workspace hot path.
|
|
task_id: Mapped[str | None] = mapped_column(String(128), nullable=True)
|
|
agent_id: Mapped[str | None] = mapped_column(String(128), nullable=True)
|
|
action_id: Mapped[str | None] = mapped_column(String(128), nullable=True)
|
|
display_name: Mapped[str | None] = mapped_column(String(256))
|
|
status: Mapped[str] = mapped_column(String(20), default="idle")
|
|
metadata_json: Mapped[dict] = mapped_column(PortableJSON(), default=dict)
|
|
created_at: Mapped[datetime] = mapped_column(BeijingDateTime(), default=lambda: datetime.now(UTC))
|
|
updated_at: Mapped[datetime] = mapped_column(BeijingDateTime(), default=lambda: datetime.now(UTC), onupdate=lambda: datetime.now(UTC))
|