mirror of
https://github.com/basicmachines-co/basic-memory
synced 2026-06-21 13:47:35 +00:00
cleanup entity from markdown
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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(
|
||||
|
||||
Reference in New Issue
Block a user