mirror of
https://github.com/aljazceru/Auto-GPT.git
synced 2025-12-18 14:34:23 +01:00
Fix #840 - Add defensive coding for local memory to load the JSON file that was gitignored at 54101c7997
- Added unit test placholder for local cache test - Removed unused imports from local cache unit test placeholder
This commit is contained in:
@@ -28,10 +28,20 @@ class LocalCache(MemoryProviderSingleton):
|
||||
def __init__(self, cfg) -> None:
|
||||
self.filename = f"{cfg.memory_index}.json"
|
||||
if os.path.exists(self.filename):
|
||||
with open(self.filename, 'rb') as f:
|
||||
loaded = orjson.loads(f.read())
|
||||
self.data = CacheContent(**loaded)
|
||||
try:
|
||||
with open(self.filename, 'w+b') as f:
|
||||
file_content = f.read()
|
||||
if not file_content.strip():
|
||||
file_content = b'{}'
|
||||
f.write(file_content)
|
||||
|
||||
loaded = orjson.loads(file_content)
|
||||
self.data = CacheContent(**loaded)
|
||||
except orjson.JSONDecodeError:
|
||||
print(f"Error: The file '{self.filename}' is not in JSON format.")
|
||||
self.data = CacheContent()
|
||||
else:
|
||||
print(f"Warning: The file '{self.filename}' does not exist. Local memory would not be saved to a file.")
|
||||
self.data = CacheContent()
|
||||
|
||||
def add(self, text: str):
|
||||
|
||||
Reference in New Issue
Block a user