Spaces:
Running

WorkPilot-AI / core /memory /models.py
Aynkhn's picture
Deploy WorkPilot demo
fd5dd94 verified
Raw History Blame Contribute Delete
5.54 kB
"""Tables for knowledge, long-term memory and prompt versions. All tenant-scoped, RLS forced
(migration ``0009``).
Embeddings use an unsized ``vector`` column plus ``embed_model``: searches only compare rows
embedded by the same model, so a real embedder with another dimension can replace the
default hashing embedder without a schema change (re-ingest to switch).
"""
from __future__ import annotations
import uuid
from datetime import datetime
from pgvector.sqlalchemy import Vector
from sqlalchemy import (
DateTime,
ForeignKey,
Index,
Integer,
String,
Text,
UniqueConstraint,
func,
)
from sqlalchemy.dialects.postgresql import ARRAY, JSONB, TSVECTOR, UUID
from sqlalchemy.orm import Mapped, mapped_column
from core.db.base import Base, TenantMixin, TimestampMixin, new_id
class KnowledgeDoc(TenantMixin, TimestampMixin, Base):
__tablename__ = "knowledge_docs"
id: Mapped[uuid.UUID] = mapped_column(UUID(as_uuid=True), primary_key=True, default=new_id)
title: Mapped[str] = mapped_column(String(300), nullable=False)
source_type: Mapped[str] = mapped_column(String(20), nullable=False) # pdf|docx|markdown|...
source_uri: Mapped[str | None] = mapped_column(String(2000), nullable=True)
sha256: Mapped[str] = mapped_column(String(64), nullable=False)
size_bytes: Mapped[int] = mapped_column(Integer, nullable=False)
page_count: Mapped[int | None] = mapped_column(Integer, nullable=True)
chunk_count: Mapped[int] = mapped_column(Integer, nullable=False, default=0)
status: Mapped[str] = mapped_column(String(20), nullable=False, default="ready")
meta: Mapped[dict] = mapped_column(JSONB, nullable=False, default=dict)
created_by: Mapped[str] = mapped_column(String(120), nullable=False)
__table_args__ = (UniqueConstraint("tenant_id", "sha256", name="uq_knowledge_docs_sha256"),)
class KnowledgeChunk(TenantMixin, TimestampMixin, Base):
__tablename__ = "knowledge_chunks"
id: Mapped[uuid.UUID] = mapped_column(UUID(as_uuid=True), primary_key=True, default=new_id)
doc_id: Mapped[uuid.UUID] = mapped_column(
UUID(as_uuid=True),
ForeignKey("knowledge_docs.id", ondelete="CASCADE"),
nullable=False,
index=True,
)
ordinal: Mapped[int] = mapped_column(Integer, nullable=False)
page: Mapped[int | None] = mapped_column(Integer, nullable=True)
text: Mapped[str] = mapped_column(Text, nullable=False)
search_doc: Mapped[str] = mapped_column(TSVECTOR, nullable=False)
embedding: Mapped[list[float]] = mapped_column(Vector(), nullable=False)
embed_model: Mapped[str] = mapped_column(String(100), nullable=False)
flags: Mapped[list[str]] = mapped_column(ARRAY(String(40)), nullable=False, default=list)
__table_args__ = (
Index("ix_knowledge_chunks_search_doc", "search_doc", postgresql_using="gin"),
)
class MemoryItem(TenantMixin, Base):
"""One LangGraph-store item. ``namespace`` is the dot-joined tuple (labels cannot contain
dots), always starting with ``tenant.<tenant_id>``."""
__tablename__ = "memory_items"
id: Mapped[uuid.UUID] = mapped_column(UUID(as_uuid=True), primary_key=True, default=new_id)
namespace: Mapped[str] = mapped_column(String(500), nullable=False)
key: Mapped[str] = mapped_column(String(200), nullable=False)
kind: Mapped[str] = mapped_column(String(20), nullable=False) # prefs|customer|case|other
value: Mapped[dict] = mapped_column(JSONB, nullable=False)
checksum: Mapped[str] = mapped_column(String(64), nullable=False)
size_bytes: Mapped[int] = mapped_column(Integer, nullable=False)
embedding: Mapped[list[float] | None] = mapped_column(Vector(), nullable=True)
embed_model: Mapped[str | None] = mapped_column(String(100), nullable=True)
written_by: Mapped[str] = mapped_column(String(120), nullable=False)
created_at: Mapped[datetime] = mapped_column(
DateTime(timezone=True), server_default=func.now(), nullable=False
)
updated_at: Mapped[datetime] = mapped_column(
DateTime(timezone=True), server_default=func.now(), nullable=False
)
expires_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), nullable=False)
__table_args__ = (
UniqueConstraint("tenant_id", "namespace", "key", name="uq_memory_items_ns_key"),
Index("ix_memory_items_expires_at", "expires_at"),
)
class PromptVersion(TenantMixin, TimestampMixin, Base):
"""Procedural memory: a versioned prompt/SOP. ``labels`` (e.g. ``production``) are unique
per (tenant, name): setting a label moves it from the version that held it."""
__tablename__ = "prompt_versions"
id: Mapped[uuid.UUID] = mapped_column(UUID(as_uuid=True), primary_key=True, default=new_id)
name: Mapped[str] = mapped_column(String(200), nullable=False)
version: Mapped[int] = mapped_column(Integer, nullable=False)
body: Mapped[str] = mapped_column(Text, nullable=False)
labels: Mapped[list[str]] = mapped_column(ARRAY(String(40)), nullable=False, default=list)
meta: Mapped[dict] = mapped_column(JSONB, nullable=False, default=dict)
checksum: Mapped[str] = mapped_column(String(64), nullable=False)
created_by: Mapped[str] = mapped_column(String(120), nullable=False)
__table_args__ = (
UniqueConstraint("tenant_id", "name", "version", name="uq_prompt_versions_name_version"),
)