# -*- coding: utf-8 -*-"""Base class for memoryTODO: a abstract class for a piece of memoryTODO: data structure to organize multiple memory pieces in memory class"""fromabcimportABC,abstractmethodfromtypingimportIterable,SequencefromtypingimportOptionalfromtypingimportUnionfromtypingimportCallablefrom..messageimportMsg
[docs]classMemoryBase(ABC):"""Base class for memory."""_version:int=1
[docs]@abstractmethoddefget_memory(self,recent_n:Optional[int]=None,filter_func:Optional[Callable[[int,dict],bool]]=None,)->list:""" Return a certain range (`recent_n` or all) of memory, filtered by `filter_func` Args: recent_n (int, optional): indicate the most recent N memory pieces to be returned. filter_func (Optional[Callable[[int, dict], bool]]): filter function to decide which pieces of memory should be returned, taking the index and a piece of memory as input and return True (return this memory) or False (does not return) """
[docs]@abstractmethoddefadd(self,memories:Union[Sequence[Msg],Msg,None],)->None:""" Adding new memory fragment, depending on how the memory are stored Args: memories (Union[Sequence[Msg], Msg, None]): Memories to be added. """
[docs]@abstractmethoddefdelete(self,index:Union[Iterable,int])->None:""" Delete memory fragment, depending on how the memory are stored and matched Args: index (Union[Iterable, int]): indices of the memory fragments to delete """
[docs]@abstractmethoddefload(self,memories:Union[str,list[Msg],Msg],overwrite:bool=False,)->None:""" Load memory, depending on how the memory are passed, design to load from both file or dict Args: memories (Union[str, list[Msg], Msg]): memories to be loaded. If it is in str type, it will be first checked if it is a file; otherwise it will be deserialized as messages. Otherwise, memories must be either in message type or list of messages. overwrite (bool): if True, clear the current memory before loading the new ones; if False, memories will be appended to the old one at the end. """
[docs]@abstractmethoddefexport(self,file_path:Optional[str]=None,to_mem:bool=False,)->Optional[list]:""" Export memory, depending on how the memory are stored Args: file_path (Optional[str]): file path to save the memory to. to_mem (Optional[str]): if True, just return the list of messages in memory Notice: this method prevents file_path is None when to_mem is False. """
[docs]@abstractmethoddefclear(self)->None:"""Clean memory, depending on how the memory are stored"""
[docs]@abstractmethoddefsize(self)->int:"""Returns the number of memory segments in memory."""raiseNotImplementedError