update entity/repostitory/service

This commit is contained in:
phernandez
2025-01-06 18:57:04 -06:00
parent fb330fe745
commit ea359d2484
10 changed files with 115 additions and 253 deletions
-5
View File
@@ -40,10 +40,5 @@ class Document(Base):
DateTime, server_default=text("CURRENT_TIMESTAMP"), onupdate=text("CURRENT_TIMESTAMP")
)
# Relationships
entities: Mapped[List["Entity"]] = relationship( # pyright: ignore [reportUndefinedVariable] # noqa: F821
"Entity", back_populates="document", cascade="all, delete-orphan"
)
def __repr__(self) -> str:
return f"Document(id={self.id}, path_id='{self.path_id}', checksum='{self.checksum}', created_at='{self.created_at}', updated_at='{self.updated_at}')"
+41 -23
View File
@@ -3,47 +3,69 @@
from datetime import datetime
from typing import Optional
from sqlalchemy import Integer, String, Text, ForeignKey, UniqueConstraint, text, DateTime, Index
from sqlalchemy import (
Integer,
String,
Text,
ForeignKey,
UniqueConstraint,
text,
DateTime,
Index,
JSON,
CheckConstraint,
)
from sqlalchemy.orm import Mapped, mapped_column, relationship
from basic_memory.models.base import Base
from basic_memory.models.documents import Document
from enum import Enum
class EntityType(str, Enum):
"""Types of knowledge nodes."""
KNOWLEDGE = "knowledge"
NOTE = "note"
class Entity(Base):
"""
Core entity in the knowledge graph.
Entities represent semantic nodes maintained by the AI layer. Each entity:
- Has a unique numeric ID (database-generated)
- Maps to a document file on disk (optional)
- Maps to a file on disk
- Maintains a checksum for change detection
- Tracks both source document and semantic properties
"""
__tablename__ = "entity"
__table_args__ = (
UniqueConstraint("entity_type", "name", name="uix_entity_type_name"),
UniqueConstraint("path_id", name="uix_entity_path_id"), # Make path_id unique
Index("ix_entity_type", "entity_type"),
Index("ix_entity_doc_id", "doc_id"),
Index("ix_entity_created_at", "created_at"), # For timeline queries
Index("ix_entity_updated_at", "updated_at") # For timeline queries
Index("ix_entity_updated_at", "updated_at"), # For timeline queries
CheckConstraint(
f"entity_type IN {tuple(t.value for t in EntityType)}", name="check_entity_type"
),
)
# Core identity
id: Mapped[int] = mapped_column(Integer, primary_key=True)
name: Mapped[str] = mapped_column(String)
entity_type: Mapped[str] = mapped_column(String)
# Normalized path for URIs - must be unique
path_id: Mapped[str] = mapped_column(String, index=True)
entity_type: Mapped[EntityType] = mapped_column(String, default=EntityType.KNOWLEDGE)
entity_metadata: Mapped[Optional[dict]] = mapped_column(JSON, nullable=True)
# Normalized path for URIs
path_id: Mapped[str] = mapped_column(String, unique=True, index=True)
# Actual filesystem relative path
file_path: Mapped[str] = mapped_column(String, unique=True, index=True)
# Content and validation
description: Mapped[Optional[str]] = mapped_column(Text, nullable=True)
# checksum of file
checksum: Mapped[Optional[str]] = mapped_column(String, nullable=True)
# Content for knowledge entity_type
description: Mapped[Optional[str]] = mapped_column(Text, nullable=True)
# Metadata and tracking
created_at: Mapped[datetime] = mapped_column(DateTime, server_default=text("CURRENT_TIMESTAMP"))
@@ -51,11 +73,6 @@ class Entity(Base):
DateTime, server_default=text("CURRENT_TIMESTAMP"), onupdate=text("CURRENT_TIMESTAMP")
)
# Relations
doc_id: Mapped[Optional[int]] = mapped_column(
Integer, ForeignKey("document.id", ondelete="SET NULL"), nullable=True
)
# Relationships
observations = relationship(
"Observation", back_populates="entity", cascade="all, delete-orphan"
@@ -72,7 +89,6 @@ class Entity(Base):
foreign_keys="[Relation.to_id]",
cascade="all, delete-orphan",
)
document: Mapped[Optional[Document]] = relationship(Document, back_populates="entities")
@property
def relations(self):
@@ -99,8 +115,8 @@ class Observation(Base):
__tablename__ = "observation"
__table_args__ = (
Index("ix_observation_entity_id", "entity_id"), # Add FK index
Index("ix_observation_category", "category"), # Add category index
Index("ix_observation_entity_id", "entity_id"), # Add FK index
Index("ix_observation_category", "category"), # Add category index
Index("ix_observation_created_at", "created_at"), # For timeline queries
Index("ix_observation_updated_at", "updated_at"), # For timeline queries
)
@@ -112,7 +128,7 @@ class Observation(Base):
String,
nullable=False,
default=ObservationCategory.NOTE.value,
server_default=ObservationCategory.NOTE.value
server_default=ObservationCategory.NOTE.value,
)
context: Mapped[str] = mapped_column(Text, nullable=True)
created_at: Mapped[datetime] = mapped_column(DateTime, server_default=text("CURRENT_TIMESTAMP"))
@@ -153,8 +169,10 @@ class Relation(Base):
)
# Relationships
from_entity = relationship("Entity", foreign_keys=[from_id], back_populates="outgoing_relations")
from_entity = relationship(
"Entity", foreign_keys=[from_id], back_populates="outgoing_relations"
)
to_entity = relationship("Entity", foreign_keys=[to_id], back_populates="incoming_relations")
def __repr__(self) -> str:
return f"Relation(id={self.id}, from_id={self.from_id}, to_id={self.to_id}, type='{self.relation_type}')"
return f"Relation(id={self.id}, from_id={self.from_id}, to_id={self.to_id}, type='{self.relation_type}')"