from __future__ import annotations from sqlalchemy import Boolean, Float, ForeignKey, Integer, JSON, String, Text from sqlalchemy.orm import Mapped, mapped_column from app.db.base import Base, TimestampMixin, UUIDPrimaryKeyMixin class KnowledgeItem(UUIDPrimaryKeyMixin, TimestampMixin, Base): __tablename__ = "knowledge_items" mine_id: Mapped[str | None] = mapped_column( String(32), ForeignKey("mines.id", ondelete="SET NULL"), nullable=True, index=True ) document_id: Mapped[str | None] = mapped_column( String(32), ForeignKey("business_documents.id", ondelete="SET NULL"), nullable=True ) code: Mapped[str | None] = mapped_column(String(64), nullable=True, index=True) title: Mapped[str] = mapped_column(String(255), index=True) knowledge_type: Mapped[str] = mapped_column(String(32), index=True) category: Mapped[str] = mapped_column(String(64), default="", index=True) source_type: Mapped[str] = mapped_column(String(32), default="manual") source_ref: Mapped[str] = mapped_column(String(512), default="") content: Mapped[str] = mapped_column(Text) summary: Mapped[str] = mapped_column(Text, default="") tags: Mapped[list] = mapped_column(JSON, default=list) status: Mapped[str] = mapped_column(String(32), default="published", index=True) version: Mapped[str] = mapped_column(String(32), default="V1.0") confidence: Mapped[float | None] = mapped_column(Float, nullable=True) is_verified: Mapped[bool] = mapped_column(Boolean, default=False, index=True) verified_by: Mapped[str] = mapped_column(String(128), default="") metadata_json: Mapped[dict] = mapped_column("metadata", JSON, default=dict) class KnowledgeChunk(UUIDPrimaryKeyMixin, TimestampMixin, Base): __tablename__ = "knowledge_chunks" knowledge_id: Mapped[str] = mapped_column( String(32), ForeignKey("knowledge_items.id", ondelete="CASCADE"), index=True ) chunk_index: Mapped[int] = mapped_column(Integer) title: Mapped[str] = mapped_column(String(255), default="") content: Mapped[str] = mapped_column(Text) token_count: Mapped[int] = mapped_column(Integer, default=0) vector_id: Mapped[str] = mapped_column(String(128), default="", index=True) embedding_model: Mapped[str] = mapped_column(String(128), default="") metadata_json: Mapped[dict] = mapped_column("metadata", JSON, default=dict)