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:
@@ -5,8 +5,10 @@ independent from storage/persistence concerns.
|
||||
"""
|
||||
|
||||
from datetime import datetime, UTC
|
||||
from typing import List, Optional
|
||||
from pydantic import BaseModel
|
||||
from typing import List, Optional, Dict, Any
|
||||
from uuid import uuid4
|
||||
|
||||
from pydantic import BaseModel, model_validator
|
||||
|
||||
|
||||
class Observation(BaseModel):
|
||||
@@ -47,6 +49,32 @@ class Entity(BaseModel):
|
||||
observations: List[Observation] = []
|
||||
relations: List[Relation] = []
|
||||
|
||||
@model_validator(mode='before')
|
||||
@classmethod
|
||||
def generate_id(cls, data: dict) -> dict:
|
||||
"""Generate an ID if one wasn't provided during instantiation"""
|
||||
if not data.get('id') and data.get('name'):
|
||||
timestamp = datetime.now(UTC).strftime("%Y%m%d")
|
||||
normalized_name = data['name'].lower().replace(" ", "-")
|
||||
data['id'] = f"{timestamp}-{normalized_name}-{uuid4().hex[:8]}"
|
||||
return data
|
||||
|
||||
def model_dump(self, **kwargs) -> Dict[str, Any]:
|
||||
"""Serialize entity, handling relations to prevent circular references"""
|
||||
# Get basic data without relations
|
||||
exclude = kwargs.pop('exclude', set())
|
||||
exclude.add('relations')
|
||||
basic_data = super().model_dump(exclude=exclude, **kwargs)
|
||||
|
||||
# Add serialized relations if we have any
|
||||
if 'relations' not in exclude and self.relations:
|
||||
basic_data['relations'] = [
|
||||
relation.model_dump(**kwargs)
|
||||
for relation in self.relations
|
||||
]
|
||||
|
||||
return basic_data
|
||||
|
||||
def file_name(self) -> str:
|
||||
"""Get the markdown file name for this entity."""
|
||||
return f"{self.id}.md"
|
||||
Reference in New Issue
Block a user