From ab6916609a8212dc236299e7fc284408de5fe6c7 Mon Sep 17 00:00:00 2001 From: phernandez Date: Thu, 30 Jan 2025 17:24:22 -0500 Subject: [PATCH] cleanup entity from markdown --- src/basic_memory/markdown/utils.py | 27 +++++++++++---------- src/basic_memory/services/entity_service.py | 15 ++++++------ 2 files changed, 22 insertions(+), 20 deletions(-) diff --git a/src/basic_memory/markdown/utils.py b/src/basic_memory/markdown/utils.py index 36b1d711..03fb5682 100644 --- a/src/basic_memory/markdown/utils.py +++ b/src/basic_memory/markdown/utils.py @@ -81,7 +81,7 @@ def entity_model_to_markdown(entity: Entity, content: Optional[str] = None) -> E ) -def entity_model_from_markdown(file_path: Path, markdown: EntityMarkdown) -> Entity: +def entity_model_from_markdown(file_path: Path, markdown: EntityMarkdown, entity: Optional[Entity] = None) -> Entity: """ Convert markdown entity to model. Does not include relations. @@ -98,16 +98,17 @@ def entity_model_from_markdown(file_path: Path, markdown: EntityMarkdown) -> Ent return obs.category permalink = markdown.frontmatter.permalink or generate_permalink(file_path) - model = Entity( - title=markdown.frontmatter.title or file_path.stem, - entity_type=markdown.frontmatter.type, - permalink=permalink, - file_path=str(file_path), - content_type="text/markdown", - created_at=markdown.frontmatter.created, - updated_at=markdown.frontmatter.modified, - entity_metadata={k:str(v) for k,v in markdown.frontmatter.metadata.items()}, - observations=[ + model = entity or Entity() + + model.title=markdown.frontmatter.title or file_path.stem + model.entity_type=markdown.frontmatter.type + model.permalink=permalink + model.file_path=str(file_path) + model.content_type="text/markdown" + model.created_at=markdown.frontmatter.created + model.updated_at=markdown.frontmatter.modified + model.entity_metadata={k:str(v) for k,v in markdown.frontmatter.metadata.items()} + model.observations=[ ObservationModel( content=obs.content, category=get_valid_category(obs), @@ -115,6 +116,6 @@ def entity_model_from_markdown(file_path: Path, markdown: EntityMarkdown) -> Ent tags=obs.tags, ) for obs in markdown.observations - ], - ) + ] + return model diff --git a/src/basic_memory/services/entity_service.py b/src/basic_memory/services/entity_service.py index 062146af..302d348a 100644 --- a/src/basic_memory/services/entity_service.py +++ b/src/basic_memory/services/entity_service.py @@ -190,7 +190,7 @@ class EntityService(BaseService[EntityModel]): async def create_entity_from_markdown( self, file_path: Path, markdown: EntityMarkdown ) -> EntityModel: - """First pass: Create entity and observations only. + """Create entity and observations only. Creates the entity with null checksum to indicate sync not complete. Relations will be added in second pass. @@ -205,7 +205,7 @@ class EntityService(BaseService[EntityModel]): async def update_entity_and_observations( self, file_path: Path | str, markdown: EntityMarkdown ) -> EntityModel: - """First pass: Update entity fields and observations. + """Update entity fields and observations. Updates everything except relations and sets null checksum to indicate sync not complete. @@ -217,14 +217,12 @@ class EntityService(BaseService[EntityModel]): if not db_entity: raise EntityNotFoundError(f"Entity not found: {file_path}") - # Update fields from markdown - db_entity.title = markdown.frontmatter.title - db_entity.entity_type = markdown.frontmatter.type - db_entity.entity_metadata = {k: str(v) for k, v in markdown.frontmatter.metadata.items()} - # Clear observations for entity await self.observation_repository.delete_by_fields(entity_id=db_entity.id) + # update values from markdown + db_entity = entity_model_from_markdown(file_path, markdown, db_entity) + # add new observations observations = [ Observation( @@ -238,6 +236,9 @@ class EntityService(BaseService[EntityModel]): ] await self.observation_repository.add_all(observations) + # checksum value is None == not finished with sync + db_entity.checksum = None + # update entity # checksum value is None == not finished with sync return await self.repository.update(