"""ORM mapping between DeerFlow ownership and WeKnora knowledge bases.""" from __future__ import annotations from datetime import UTC, datetime from sqlalchemy import Boolean, String, UniqueConstraint, false, true from sqlalchemy.orm import Mapped, mapped_column from deerflow.persistence.base import Base from deerflow.persistence.types import BeijingDateTime class LlmWikiKnowledgeBaseRow(Base): __tablename__ = "llmwiki_knowledge_bases" id: Mapped[str] = mapped_column(String(36), primary_key=True) weknora_id: Mapped[str] = mapped_column(String(128), unique=True, index=True) owner_user_id: Mapped[str] = mapped_column(String(64), index=True) name: Mapped[str] = mapped_column(String(255), default="") description: Mapped[str] = mapped_column(String(2048), default="") kb_type: Mapped[str] = mapped_column(String(32), default="document") publication_status: Mapped[str] = mapped_column(String(16), default="private", index=True) published_by: Mapped[str | None] = mapped_column(String(64), nullable=True) reviewed_by: Mapped[str | None] = mapped_column(String(64), nullable=True) published_at: Mapped[datetime | None] = mapped_column(BeijingDateTime(), nullable=True) wiki_index_enabled: Mapped[bool] = mapped_column(Boolean, nullable=False, default=True, server_default=true(), index=True) external_search_enabled: Mapped[bool] = mapped_column(Boolean, nullable=False, default=False, server_default=false(), index=True) external_search_updated_at: Mapped[datetime | None] = mapped_column(BeijingDateTime(), nullable=True) 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)) def to_dict(self) -> dict: return {column.name: getattr(self, column.name) for column in self.__table__.columns} class LlmWikiConversationDepositRow(Base): """Mapping from one DeerFlow answer turn to its WeKnora manual document. The actual content lives in WeKnora. DeerFlow keeps only the stable ids it needs to rewrite/delete the document when the user regenerates or removes a conversation turn. """ __tablename__ = "llmwiki_conversation_deposits" __table_args__ = ( UniqueConstraint( "thread_id", "assistant_message_id", name="uq_llmwiki_conversation_deposit_answer", ), ) id: Mapped[str] = mapped_column(String(36), primary_key=True) thread_id: Mapped[str] = mapped_column(String(128), index=True) human_message_id: Mapped[str | None] = mapped_column(String(128), nullable=True, index=True) assistant_message_id: Mapped[str] = mapped_column(String(128), index=True) knowledge_base_mapping_id: Mapped[str] = mapped_column(String(36), index=True) weknora_knowledge_id: Mapped[str | None] = mapped_column(String(128), nullable=True) title: Mapped[str] = mapped_column(String(255), default="") created_by_user_id: Mapped[str | None] = mapped_column(String(64), nullable=True, index=True) 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)) def to_dict(self) -> dict: return {column.name: getattr(self, column.name) for column in self.__table__.columns}