diff --git a/src/basic_memory/fileio.py b/src/basic_memory/fileio.py index b6c749a0..acc7c39a 100644 --- a/src/basic_memory/fileio.py +++ b/src/basic_memory/fileio.py @@ -19,7 +19,7 @@ class EntityNotFoundError(Exception): pass -async def write_entity_file(entities_path: Path, entity: EntityIn) -> bool: +async def write_entity_file(entities_path: Path, entity_id: str, entity: EntityIn) -> bool: """ Write entity to filesystem in markdown format. @@ -33,9 +33,9 @@ async def write_entity_file(entities_path: Path, entity: EntityIn) -> bool: Raises: FileOperationError: If file operations fail """ - logger.debug(f"Writing entity file for {entity.file_path}") + logger.debug(f"Writing entity file for {entity_id}") - entity_path = entities_path / entity.file_path + entity_path = entities_path / entity_id # Handle directory creation separately try: @@ -84,7 +84,7 @@ async def write_entity_file(entities_path: Path, entity: EntityIn) -> bool: except Exception as e: raise FileOperationError(f"Failed to finalize entity file: {str(e)}") from e - logger.debug(f"Wrote entity file: {entity.file_path}") + logger.debug(f"Wrote entity file: {entity_id}") return True diff --git a/src/basic_memory/services/memory_service.py b/src/basic_memory/services/memory_service.py index 67ee25bd..b897a779 100644 --- a/src/basic_memory/services/memory_service.py +++ b/src/basic_memory/services/memory_service.py @@ -33,28 +33,25 @@ class MemoryService: """Create multiple entities with their observations.""" logger.debug(f"Creating {len(entities_in)} entities") - # First check if any entities already exist and generate IDs - for entity in entities_in: - # Generate ID upfront - entity.id = Entity.generate_id(entity.entity_type, entity.name) - logger.debug(f"Generated ID for entity: {entity.id}") - - # Check if entity exists + # Write files in parallel (filesystem is source of truth) + async def write_file(entity: EntityIn): try: existing = await self.entity_service.get_by_type_and_name( - entity.entity_type, + entity.entity_type, entity.name ) if existing: - raise ValueError( + logger.error( f"Entity already exists: {entity.entity_type}/{entity.name}" ) - except EntityNotFoundError: - # This is good - means entity doesn't exist - pass - # Write files in parallel (filesystem is source of truth) - async def write_file(entity: EntityIn): + except EntityNotFoundError: + # TODO replace with self.entity_service.exists(entity.entity_type, entity.name) + return + + # Generate ID and data dict for this entity + entity_id = Entity.generate_id(entity.entity_type, entity.name) + await write_entity_file(self.entities_path, entity) file_writes = [write_file(entity) for entity in entities_in]