mirror of
https://github.com/basicmachines-co/basic-memory
synced 2026-06-21 13:47:35 +00:00
add knowledge_writer
This commit is contained in:
@@ -0,0 +1,73 @@
|
||||
"""Writer for knowledge entity markdown files."""
|
||||
|
||||
from datetime import datetime, UTC
|
||||
from typing import Optional, Dict, Any
|
||||
|
||||
import yaml
|
||||
from loguru import logger
|
||||
|
||||
from basic_memory.models import Entity as EntityModel
|
||||
|
||||
|
||||
class KnowledgeWriter:
|
||||
"""Formats entities into markdown files."""
|
||||
|
||||
async def format_frontmatter(self, entity: EntityModel) -> dict:
|
||||
"""Generate frontmatter metadata for entity."""
|
||||
now = datetime.now(UTC).isoformat()
|
||||
return {
|
||||
"type": entity.entity_type,
|
||||
"id": entity.id,
|
||||
"created": now,
|
||||
"modified": now
|
||||
}
|
||||
|
||||
async def format_metadata(self, metadata: Optional[Dict[str, Any]] = None) -> str:
|
||||
"""Format metadata section as YAML block."""
|
||||
if not metadata:
|
||||
return ""
|
||||
|
||||
try:
|
||||
yaml_block = yaml.dump(metadata, sort_keys=False)
|
||||
return (
|
||||
"# Metadata\n"
|
||||
"<!-- anything below this line is for AI -->\n\n"
|
||||
"```yml\n"
|
||||
f"{yaml_block}"
|
||||
"```\n"
|
||||
)
|
||||
except Exception as e:
|
||||
logger.warning(f"Failed to format metadata YAML: {e}")
|
||||
return "" # Skip metadata on error
|
||||
|
||||
async def format_content(self, entity: EntityModel, metadata: Optional[Dict[str, Any]] = None) -> str:
|
||||
"""Format entity content as markdown."""
|
||||
sections = [
|
||||
f"# {entity.name}\n"
|
||||
]
|
||||
|
||||
if entity.description:
|
||||
sections.extend([
|
||||
entity.description,
|
||||
""
|
||||
])
|
||||
|
||||
if entity.observations:
|
||||
sections.extend([
|
||||
"## Observations",
|
||||
*[f"- {obs.content}" for obs in entity.observations],
|
||||
""
|
||||
])
|
||||
|
||||
if entity.relations:
|
||||
sections.extend([
|
||||
"## Relations",
|
||||
*[f"- [[{rel.to_entity.name}]] {rel.relation_type}" for rel in entity.relations],
|
||||
""
|
||||
])
|
||||
|
||||
if metadata:
|
||||
sections.append("\n")
|
||||
sections.append(await self.format_metadata(metadata))
|
||||
|
||||
return "\n".join(sections)
|
||||
@@ -10,6 +10,7 @@ from basic_memory.models import Entity as EntityModel
|
||||
from basic_memory.schemas import Entity as EntitySchema, Relation as RelationSchema
|
||||
from basic_memory.services.entity_service import EntityService
|
||||
from basic_memory.services.exceptions import EntityNotFoundError, FileOperationError
|
||||
from basic_memory.services.file_service import FileService
|
||||
from basic_memory.services.observation_service import ObservationService
|
||||
from basic_memory.services.relation_service import RelationService
|
||||
|
||||
@@ -31,7 +32,7 @@ class KnowledgeService:
|
||||
entity_service: EntityService,
|
||||
observation_service: ObservationService,
|
||||
relation_service: RelationService,
|
||||
file_service, # FileService
|
||||
file_service: FileService, # FileService
|
||||
knowledge_parser: KnowledgeParser,
|
||||
):
|
||||
self.entity_service = entity_service
|
||||
|
||||
Reference in New Issue
Block a user