33"""
44
55from abc import ABC , abstractmethod
6- import inspect
7- from typing import List , Dict , Any , Optional , Union
6+ from typing import List , Dict , Any
87from datetime import datetime
98
109class BaseMemory (ABC ):
@@ -14,17 +13,6 @@ def __init__(self, memory_key: str = "chat_history"):
1413 self .memory_key = memory_key
1514 self .created_at = datetime .now ()
1615
17- def __init_subclass__ (cls , ** kwargs ):
18- """Auto-wrap sync overrides so BaseMemory API stays async."""
19- super ().__init_subclass__ (** kwargs )
20- for method_name in ("add_message" , "get_messages" , "clear" , "save" , "load" ):
21- method = cls .__dict__ .get (method_name )
22- if method is None :
23- continue
24- if inspect .iscoroutinefunction (method ):
25- continue
26- setattr (cls , method_name , BaseMemory ._make_async_wrapper (method ))
27-
2816 @abstractmethod
2917 async def add_message (self , message : Dict [str , str ]) -> None :
3018 """Add a message to memory."""
@@ -48,40 +36,4 @@ async def save(self) -> None:
4836 @abstractmethod
4937 async def load (self ) -> None :
5038 """Load memory from persistent storage."""
51- pass
52-
53- @staticmethod
54- async def _maybe_await (value : Any ) -> Any :
55- """Await value when needed; otherwise return directly."""
56- if inspect .isawaitable (value ):
57- return await value
58- return value
59-
60- @staticmethod
61- def _make_async_wrapper (sync_method ):
62- """Wrap a synchronous method with an async callable."""
63- async def _wrapped (self , * args , ** kwargs ):
64- return sync_method (self , * args , ** kwargs )
65- _wrapped .__name__ = sync_method .__name__
66- _wrapped .__doc__ = sync_method .__doc__
67- return _wrapped
68-
69- async def add_message_async (self , message : Dict [str , str ]) -> None :
70- """Backward-compatible alias for async add_message()."""
71- await self .add_message (message )
72-
73- async def get_messages_async (self ) -> List [Dict [str , str ]]:
74- """Backward-compatible alias for async get_messages()."""
75- return await self .get_messages ()
76-
77- async def clear_async (self ) -> None :
78- """Backward-compatible alias for async clear()."""
79- await self .clear ()
80-
81- async def save_async (self ) -> None :
82- """Backward-compatible alias for async save()."""
83- await self .save ()
84-
85- async def load_async (self ) -> None :
86- """Backward-compatible alias for async load()."""
87- await self .load ()
39+ pass
0 commit comments