mirror of
https://github.com/basicmachines-co/basic-memory
synced 2026-06-21 13:47:35 +00:00
knowledge_service tests passing
This commit is contained in:
@@ -1,30 +1,34 @@
|
||||
"""File operations for knowledge service."""
|
||||
|
||||
from pathlib import Path
|
||||
from typing import Tuple
|
||||
from typing import Tuple, Optional
|
||||
|
||||
from loguru import logger
|
||||
|
||||
from basic_memory.markdown.knowledge_writer import KnowledgeWriter
|
||||
from basic_memory.markdown.note_writer import NoteWriter
|
||||
from basic_memory.models import Entity as EntityModel
|
||||
from basic_memory.models.knowledge import EntityType
|
||||
from basic_memory.services.entity_service import EntityService
|
||||
from basic_memory.services.exceptions import FileOperationError
|
||||
from basic_memory.services.file_service import FileService
|
||||
|
||||
|
||||
class FileOperations:
|
||||
"""File operations for knowledge entities."""
|
||||
"""File operations for both knowledge and note entities."""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
entity_service: EntityService,
|
||||
file_service: FileService,
|
||||
knowledge_writer: KnowledgeWriter,
|
||||
note_writer: NoteWriter,
|
||||
base_path: Path,
|
||||
):
|
||||
self.entity_service = entity_service
|
||||
self.file_service = file_service
|
||||
self.knowledge_writer = knowledge_writer
|
||||
self.note_writer = note_writer
|
||||
self.base_path = base_path
|
||||
|
||||
async def file_exists(self, path: Path) -> bool:
|
||||
@@ -35,25 +39,37 @@ class FileOperations:
|
||||
|
||||
def get_entity_path(self, entity: EntityModel) -> Path:
|
||||
"""Generate filesystem path for entity."""
|
||||
return self.base_path / entity.entity_type / f"{entity.name}.md"
|
||||
if entity.file_path:
|
||||
return self.base_path / entity.file_path
|
||||
return self.base_path / f"{entity.path_id}.md"
|
||||
|
||||
async def write_entity_file(self, entity: EntityModel) -> Tuple[Path, str]:
|
||||
async def write_entity_file(
|
||||
self,
|
||||
entity: EntityModel,
|
||||
content: Optional[str] = None,
|
||||
) -> Tuple[Path, str]:
|
||||
"""Write entity to filesystem and return path and checksum."""
|
||||
try:
|
||||
# Ensure we have a fresh entity with all relations loaded
|
||||
# Ensure we have a fresh entity with all relations
|
||||
entity = await self.entity_service.get_by_path_id(entity.path_id)
|
||||
|
||||
frontmatter = await self.knowledge_writer.format_frontmatter(entity)
|
||||
# Format content
|
||||
path = self.get_entity_path(entity)
|
||||
entity_content = await self.knowledge_writer.format_content(entity)
|
||||
file_content = await self.file_service.add_frontmatter(
|
||||
frontmatter=frontmatter,
|
||||
content=entity_content,
|
||||
|
||||
# Select writer based on entity type
|
||||
writer = self.note_writer if entity.entity_type == EntityType.NOTE else self.knowledge_writer
|
||||
|
||||
# Get frontmatter and content
|
||||
frontmatter = await writer.format_frontmatter(entity)
|
||||
file_content = await writer.format_content(
|
||||
entity=entity,
|
||||
content=content or entity.description or "",
|
||||
)
|
||||
|
||||
# Write and get checksum
|
||||
return path, await self.file_service.write_file(path, file_content)
|
||||
|
||||
# Add frontmatter and write
|
||||
content_with_frontmatter = await self.file_service.add_frontmatter(
|
||||
frontmatter=frontmatter,
|
||||
content=file_content
|
||||
)
|
||||
path = self.get_entity_path(entity)
|
||||
return path, await self.file_service.write_file(path, content_with_frontmatter)
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to write entity file: {e}")
|
||||
|
||||
@@ -19,6 +19,7 @@ from .file_operations import FileOperations
|
||||
from .entity_operations import EntityOperations
|
||||
from .relation_operations import RelationOperations
|
||||
from .observation_operations import ObservationOperations
|
||||
from ...markdown.note_writer import NoteWriter
|
||||
|
||||
|
||||
class KnowledgeService:
|
||||
@@ -42,6 +43,7 @@ class KnowledgeService:
|
||||
relation_service: RelationService,
|
||||
file_service: FileService,
|
||||
knowledge_writer: KnowledgeWriter,
|
||||
note_writer: NoteWriter,
|
||||
base_path: Path,
|
||||
):
|
||||
|
||||
@@ -52,6 +54,7 @@ class KnowledgeService:
|
||||
entity_service=entity_service,
|
||||
file_service=file_service,
|
||||
knowledge_writer=knowledge_writer,
|
||||
note_writer=note_writer,
|
||||
base_path=base_path
|
||||
)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user