From 0d86bfeb7a747206844e64498c1b35856960ec65 Mon Sep 17 00:00:00 2001 From: phernandez Date: Sat, 28 Dec 2024 14:31:28 -0600 Subject: [PATCH] sync wip --- src/basic_memory/markdown/__init__.py | 4 +-- src/basic_memory/markdown/knowledge_parser.py | 8 +++--- src/basic_memory/markdown/schemas.py | 2 +- .../services/sync/knowledge_sync_service.py | 25 +++++++++++++++---- tests/markdown/test_entity_parsing.py | 6 ++--- 5 files changed, 30 insertions(+), 15 deletions(-) diff --git a/src/basic_memory/markdown/__init__.py b/src/basic_memory/markdown/__init__.py index 909a3276..72eb0627 100644 --- a/src/basic_memory/markdown/__init__.py +++ b/src/basic_memory/markdown/__init__.py @@ -2,7 +2,7 @@ from basic_memory.markdown.knowledge_parser import KnowledgeParser from basic_memory.markdown.schemas import ( - Entity, + EntityMarkdown, EntityContent, EntityFrontmatter, EntityMetadata, @@ -12,7 +12,7 @@ from basic_memory.markdown.schemas import ( from basic_memory.utils.file_utils import ParseError __all__ = [ - "Entity", + "EntityMarkdown", "EntityContent", "EntityFrontmatter", "EntityMetadata", diff --git a/src/basic_memory/markdown/knowledge_parser.py b/src/basic_memory/markdown/knowledge_parser.py index a8ee0bbd..078bb0b3 100644 --- a/src/basic_memory/markdown/knowledge_parser.py +++ b/src/basic_memory/markdown/knowledge_parser.py @@ -8,7 +8,7 @@ from loguru import logger from basic_memory.markdown.base_parser import MarkdownParser, ParseError from basic_memory.markdown.schemas import ( - Entity, + EntityMarkdown, EntityFrontmatter, EntityContent, EntityMetadata, @@ -17,7 +17,7 @@ from basic_memory.markdown.schemas import ( ) -class KnowledgeParser(MarkdownParser[Entity]): +class KnowledgeParser(MarkdownParser[EntityMarkdown]): """Parser for entity markdown files. Entity files must have: @@ -277,6 +277,6 @@ class KnowledgeParser(MarkdownParser[Entity]): async def create_document( self, frontmatter: EntityFrontmatter, content: EntityContent, metadata: EntityMetadata - ) -> Entity: + ) -> EntityMarkdown: """Create entity from parsed sections.""" - return Entity(frontmatter=frontmatter, content=content, entity_metadata=metadata) + return EntityMarkdown(frontmatter=frontmatter, content=content, entity_metadata=metadata) diff --git a/src/basic_memory/markdown/schemas.py b/src/basic_memory/markdown/schemas.py index 74f4186a..4427abd4 100644 --- a/src/basic_memory/markdown/schemas.py +++ b/src/basic_memory/markdown/schemas.py @@ -50,7 +50,7 @@ class EntityMetadata(BaseModel): data: Dict[str, Any] = {} -class Entity(BaseModel): +class EntityMarkdown(BaseModel): """Complete entity combining frontmatter, content, and metadata.""" frontmatter: EntityFrontmatter diff --git a/src/basic_memory/services/sync/knowledge_sync_service.py b/src/basic_memory/services/sync/knowledge_sync_service.py index 21df534f..3c3a6d7b 100644 --- a/src/basic_memory/services/sync/knowledge_sync_service.py +++ b/src/basic_memory/services/sync/knowledge_sync_service.py @@ -1,7 +1,8 @@ from pathlib import Path -from basic_memory.markdown import KnowledgeParser -from basic_memory.services import FileChangeScanner, KnowledgeService +from basic_memory.markdown import KnowledgeParser, EntityMarkdown +from basic_memory.models.knowledge import Entity as EntityModel +from basic_memory.services import FileChangeScanner, KnowledgeService, EntityService from basic_memory.services.sync.utils import FileState, SyncReport @@ -9,17 +10,31 @@ class KnowledgeSyncService: def __init__( self, scanner: FileChangeScanner, - knowledge_service: KnowledgeService, + entity_service: EntityService, knowledge_parser: KnowledgeParser, ): self.scanner = scanner - self.knowledge_service = knowledge_service + self.entity_service = entity_service self.knowledge_parser = knowledge_parser + + # async def markdown_to_entity_model(self, markdown_entity: EntityMarkdown) -> EntityModel: + # return EntityModel( + # name=markdown_entity.frontmatter.id, + # entity_type=markdown_entity.frontmatter.entity_type, + # path_id=markdown_entity.frontmatter.id, + # file_path= + # description=markdown_entity.content.description, + # checksum= + # created_at= + # updated_at= + # observations= + # + # ) async def sync_new_entity(self, directory: Path, path: str) -> None: """Handle syncing a new entity file.""" entity = await self.knowledge_parser.parse_file(directory / path) - await self.knowledge_service.create_entity(entity) + await self.entity_service.add(entity) async def sync_modified_entity(self, directory: Path, path: str) -> None: """Handle syncing a modified entity file.""" diff --git a/tests/markdown/test_entity_parsing.py b/tests/markdown/test_entity_parsing.py index 147e8dd2..b9b451a8 100644 --- a/tests/markdown/test_entity_parsing.py +++ b/tests/markdown/test_entity_parsing.py @@ -6,7 +6,7 @@ from textwrap import dedent import pytest from basic_memory.markdown.knowledge_parser import KnowledgeParser -from basic_memory.markdown.schemas import Entity, EntityFrontmatter, EntityContent +from basic_memory.markdown.schemas import EntityMarkdown, EntityFrontmatter, EntityContent from basic_memory.utils.file_utils import ParseError, FileError @@ -56,7 +56,7 @@ async def test_parse_complete_file(tmp_path, valid_entity_content): entity = await parser.parse_file(test_file) # Verify entity structure - assert isinstance(entity, Entity) + assert isinstance(entity, EntityMarkdown) assert isinstance(entity.frontmatter, EntityFrontmatter) assert isinstance(entity.content, EntityContent) @@ -131,7 +131,7 @@ async def test_parse_content_str(valid_entity_content): parser = KnowledgeParser() entity = await parser.parse_content_str(valid_entity_content) - assert isinstance(entity, Entity) + assert isinstance(entity, EntityMarkdown) assert entity.frontmatter.type == "component" assert entity.content.title == "Auth Service" assert len(entity.content.observations) == 3