mirror of
https://github.com/basicmachines-co/basic-memory
synced 2026-06-21 13:47:35 +00:00
MemoryService.create_relation implemented
This commit is contained in:
@@ -1,12 +1,11 @@
|
||||
"""Service for managing entities in the database."""
|
||||
from datetime import datetime, UTC
|
||||
from pathlib import Path
|
||||
from typing import Optional
|
||||
|
||||
from basic_memory.models import Entity as DbEntity
|
||||
from basic_memory.repository import EntityRepository
|
||||
from basic_memory.schemas import Entity
|
||||
from . import ServiceError, DatabaseSyncError
|
||||
from basic_memory.models import Entity as EntityModel
|
||||
from . import ServiceError
|
||||
|
||||
class EntityService:
|
||||
"""
|
||||
@@ -18,42 +17,32 @@ class EntityService:
|
||||
self.project_path = project_path
|
||||
self.entity_repo = entity_repo
|
||||
|
||||
async def create_entity(self, entity: Entity) -> Entity:
|
||||
async def create_entity(self, entity: Entity) -> EntityModel:
|
||||
"""Create a new entity in the database."""
|
||||
# Create DB record
|
||||
db_data = {
|
||||
**entity.model_dump(),
|
||||
"created_at": datetime.now(UTC),
|
||||
"updated_at": datetime.now(UTC)
|
||||
}
|
||||
await self.entity_repo.create(db_data)
|
||||
return entity
|
||||
return await self.entity_repo.create(db_data)
|
||||
|
||||
async def get_entity(self, entity_id: str) -> Entity:
|
||||
async def get_entity(self, entity_id: str) -> EntityModel:
|
||||
"""Get entity by ID."""
|
||||
db_entity = await self.entity_repo.find_by_id(entity_id)
|
||||
if not db_entity:
|
||||
raise ServiceError(f"Entity not found: {entity_id}")
|
||||
|
||||
return Entity(
|
||||
id=db_entity.id,
|
||||
name=db_entity.name,
|
||||
entity_type=db_entity.entity_type
|
||||
)
|
||||
return db_entity
|
||||
|
||||
async def get_by_name(self, name: str) -> Entity:
|
||||
# TODO name is not uniaue
|
||||
async def get_by_name(self, name: str) -> EntityModel:
|
||||
"""Get entity by name."""
|
||||
db_entity = await self.entity_repo.find_by_name(name)
|
||||
if not db_entity:
|
||||
raise ServiceError(f"Entity not found: {name}")
|
||||
|
||||
return Entity(
|
||||
id=db_entity.id,
|
||||
name=db_entity.name,
|
||||
entity_type=db_entity.entity_type
|
||||
)
|
||||
return db_entity
|
||||
|
||||
async def delete_entity(self, entity_id: str) -> bool:
|
||||
"""Delete entity from database."""
|
||||
await self.entity_repo.delete(entity_id)
|
||||
return True
|
||||
return await self.entity_repo.delete(entity_id)
|
||||
@@ -42,40 +42,28 @@ class MemoryService:
|
||||
|
||||
async def create_relations(self, relations_data: List[Dict[str, Any]]) -> List[Relation]:
|
||||
"""Create multiple relations between entities."""
|
||||
relations = []
|
||||
|
||||
for data in relations_data:
|
||||
# Resolve entities by ID
|
||||
from_entity, to_entity = await asyncio.gather(
|
||||
self.entity_service.get_entity(data["from_id"]),
|
||||
self.entity_service.get_entity(data["to_id"])
|
||||
)
|
||||
|
||||
# Create relation from schema data
|
||||
relation = Relation(
|
||||
from_id=from_entity.id,
|
||||
to_id=to_entity.id,
|
||||
relation_type=data["relation_type"],
|
||||
context=data.get("context")
|
||||
)
|
||||
|
||||
# Create in database
|
||||
stored_relation = await self.relation_service.create_relation(relation)
|
||||
|
||||
# Add to source entity's relations list
|
||||
relations = [Relation.model_validate(data) for data in relations_data]
|
||||
|
||||
for relation in relations:
|
||||
# First read complete entities from filesystem
|
||||
from_entity = await read_entity_file(self.entities_path, relation.from_id)
|
||||
to_entity = await read_entity_file(self.entities_path, relation.to_id)
|
||||
|
||||
# Add the new relation to the source entity
|
||||
if not hasattr(from_entity, 'relations'):
|
||||
from_entity.relations = []
|
||||
from_entity.relations.append(stored_relation)
|
||||
relations.append((stored_relation, from_entity))
|
||||
from_entity.relations.append(relation)
|
||||
|
||||
# Write updated entities in parallel
|
||||
async def write_file(entity: Entity):
|
||||
await write_entity_file(self.entities_path, entity)
|
||||
# Write updated entity files (filesystem is source of truth)
|
||||
await asyncio.gather(
|
||||
write_entity_file(self.entities_path, from_entity),
|
||||
write_entity_file(self.entities_path, to_entity)
|
||||
)
|
||||
|
||||
file_writes = [write_file(entity) for _, entity in relations]
|
||||
await asyncio.gather(*file_writes)
|
||||
# Now update the database index
|
||||
await self.relation_service.create_relation(relation)
|
||||
|
||||
return [relation for relation, _ in relations]
|
||||
return relations
|
||||
|
||||
async def add_observations(self, observations_data: List[Dict[str, Any]]) -> None:
|
||||
"""Add observations to existing entities."""
|
||||
@@ -129,7 +117,7 @@ class MemoryService:
|
||||
]
|
||||
entity_updates.append(entity)
|
||||
|
||||
# Write updated files in parallel
|
||||
# Write updated entities in parallel
|
||||
async def write_file(entity: Entity):
|
||||
await write_entity_file(self.entities_path, entity)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user