95 lines
6.3 KiB
Python
95 lines
6.3 KiB
Python
"""Portable ORM rows for the DeerFlow-local WeKnora Wiki mirror/index."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from datetime import UTC, datetime
|
|
|
|
from sqlalchemy import BigInteger, Boolean, ForeignKey, Index, Integer, LargeBinary, String, Text, UniqueConstraint, false
|
|
from sqlalchemy.orm import Mapped, mapped_column
|
|
|
|
from deerflow.persistence.base import Base
|
|
from deerflow.persistence.types import BeijingDateTime, PortableJSON, PortableLongText
|
|
|
|
|
|
def _now() -> datetime:
|
|
return datetime.now(UTC)
|
|
|
|
|
|
class LlmWikiWikiPageRow(Base):
|
|
__tablename__ = "llmwiki_wiki_pages"
|
|
__table_args__ = (
|
|
UniqueConstraint("knowledge_base_mapping_id", "slug_hash", name="uq_llmwiki_wiki_page_mapping_slug_hash"),
|
|
Index("ix_llmwiki_wiki_page_mapping_visibility", "knowledge_base_mapping_id", "status", "index_status", "is_remote_deleted"),
|
|
)
|
|
|
|
id: Mapped[str] = mapped_column(String(36), primary_key=True)
|
|
knowledge_base_mapping_id: Mapped[str] = mapped_column(String(36), ForeignKey("llmwiki_knowledge_bases.id", ondelete="CASCADE"), nullable=False, index=True)
|
|
remote_page_id: Mapped[str | None] = mapped_column(String(128), nullable=True, index=True)
|
|
slug: Mapped[str] = mapped_column(String(1024), nullable=False)
|
|
slug_hash: Mapped[str] = mapped_column(String(64), nullable=False)
|
|
title: Mapped[str] = mapped_column(String(512), nullable=False, default="", index=True)
|
|
summary: Mapped[str] = mapped_column(PortableLongText(), nullable=False, default="")
|
|
content_md: Mapped[str] = mapped_column(PortableLongText(), nullable=False, default="")
|
|
page_type: Mapped[str] = mapped_column(String(32), nullable=False, default="article", index=True)
|
|
status: Mapped[str] = mapped_column(String(32), nullable=False, default="published", index=True)
|
|
aliases: Mapped[list] = mapped_column(PortableJSON(), nullable=False, default=list)
|
|
category_path: Mapped[list] = mapped_column(PortableJSON(), nullable=False, default=list)
|
|
parent_slug: Mapped[str | None] = mapped_column(String(1024), nullable=True)
|
|
wiki_path: Mapped[str | None] = mapped_column(String(1024), nullable=True)
|
|
source_refs: Mapped[list] = mapped_column(PortableJSON(), nullable=False, default=list)
|
|
chunk_refs: Mapped[list] = mapped_column(PortableJSON(), nullable=False, default=list)
|
|
page_version: Mapped[int] = mapped_column(Integer, nullable=False, default=0, index=True)
|
|
content_hash: Mapped[str] = mapped_column(String(64), nullable=False, index=True)
|
|
remote_created_at: Mapped[datetime | None] = mapped_column(BeijingDateTime(), nullable=True)
|
|
remote_updated_at: Mapped[datetime | None] = mapped_column(BeijingDateTime(), nullable=True, index=True)
|
|
index_status: Mapped[str] = mapped_column(String(24), nullable=False, default="pending", index=True)
|
|
indexed_fingerprint: Mapped[str | None] = mapped_column(String(64), nullable=True)
|
|
indexed_at: Mapped[datetime | None] = mapped_column(BeijingDateTime(), nullable=True)
|
|
last_error: Mapped[str | None] = mapped_column(Text, nullable=True)
|
|
is_remote_deleted: Mapped[bool] = mapped_column(Boolean, nullable=False, default=False, server_default=false(), index=True)
|
|
last_seen_sync_id: Mapped[str | None] = mapped_column(String(36), nullable=True, index=True)
|
|
created_at: Mapped[datetime] = mapped_column(BeijingDateTime(), nullable=False, default=_now)
|
|
updated_at: Mapped[datetime] = mapped_column(BeijingDateTime(), nullable=False, default=_now, onupdate=_now, index=True)
|
|
|
|
|
|
class LlmWikiWikiVectorRow(Base):
|
|
__tablename__ = "llmwiki_wiki_vectors"
|
|
__table_args__ = (
|
|
UniqueConstraint("page_id", "section_index", "embedding_fingerprint", name="uq_llmwiki_wiki_vector_page_section_fingerprint"),
|
|
Index("ix_llmwiki_wiki_vector_snapshot", "knowledge_base_mapping_id", "embedding_fingerprint"),
|
|
)
|
|
|
|
id: Mapped[str] = mapped_column(String(36), primary_key=True)
|
|
page_id: Mapped[str] = mapped_column(String(36), ForeignKey("llmwiki_wiki_pages.id", ondelete="CASCADE"), nullable=False, index=True)
|
|
knowledge_base_mapping_id: Mapped[str] = mapped_column(String(36), nullable=False, index=True)
|
|
section_index: Mapped[int] = mapped_column(Integer, nullable=False)
|
|
heading: Mapped[str | None] = mapped_column(String(1024), nullable=True)
|
|
section_content: Mapped[str] = mapped_column(PortableLongText(), nullable=False)
|
|
content_hash: Mapped[str] = mapped_column(String(64), nullable=False, index=True)
|
|
embedding_fingerprint: Mapped[str] = mapped_column(String(64), nullable=False, index=True)
|
|
embedding_dimensions: Mapped[int] = mapped_column(Integer, nullable=False)
|
|
vector_blob: Mapped[bytes] = mapped_column(LargeBinary, nullable=False)
|
|
created_at: Mapped[datetime] = mapped_column(BeijingDateTime(), nullable=False, default=_now)
|
|
|
|
|
|
class LlmWikiWikiSyncStateRow(Base):
|
|
__tablename__ = "llmwiki_wiki_sync_states"
|
|
|
|
knowledge_base_mapping_id: Mapped[str] = mapped_column(String(36), ForeignKey("llmwiki_knowledge_bases.id", ondelete="CASCADE"), primary_key=True)
|
|
state: Mapped[str] = mapped_column(String(24), nullable=False, default="idle")
|
|
active_sync_id: Mapped[str | None] = mapped_column(String(36), nullable=True)
|
|
lease_owner: Mapped[str | None] = mapped_column(String(128), nullable=True)
|
|
lease_expires_at: Mapped[datetime | None] = mapped_column(BeijingDateTime(), nullable=True)
|
|
last_started_at: Mapped[datetime | None] = mapped_column(BeijingDateTime(), nullable=True)
|
|
last_completed_at: Mapped[datetime | None] = mapped_column(BeijingDateTime(), nullable=True)
|
|
last_success_at: Mapped[datetime | None] = mapped_column(BeijingDateTime(), nullable=True)
|
|
last_error: Mapped[str | None] = mapped_column(Text, nullable=True)
|
|
remote_page_count: Mapped[int] = mapped_column(Integer, nullable=False, default=0)
|
|
local_page_count: Mapped[int] = mapped_column(Integer, nullable=False, default=0)
|
|
ready_page_count: Mapped[int] = mapped_column(Integer, nullable=False, default=0)
|
|
vector_count: Mapped[int] = mapped_column(Integer, nullable=False, default=0)
|
|
failed_page_count: Mapped[int] = mapped_column(Integer, nullable=False, default=0)
|
|
index_revision: Mapped[int] = mapped_column(BigInteger, nullable=False, default=0)
|
|
embedding_fingerprint: Mapped[str | None] = mapped_column(String(64), nullable=True)
|
|
updated_at: Mapped[datetime] = mapped_column(BeijingDateTime(), nullable=False, default=_now, onupdate=_now)
|