From 1f930e81079c691aeaddbf0e3e93a8b742da8ae7 Mon Sep 17 00:00:00 2001 From: Alfredo Date: Tue, 17 Sep 2024 19:05:04 +0200 Subject: [PATCH 1/4] refactor: reusable delete_collections --- core/cat/routes/memory.py | 10 +++------- core/cat/utils.py | 14 ++++++++++++++ 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/core/cat/routes/memory.py b/core/cat/routes/memory.py index f6d1c3b0..32f004a9 100644 --- a/core/cat/routes/memory.py +++ b/core/cat/routes/memory.py @@ -4,6 +4,7 @@ from cat.auth.connection import HTTPAuth from cat.auth.permissions import AuthPermission, AuthResource +from cat import utils import time @@ -103,19 +104,14 @@ async def wipe_collections( """Delete and create all collections""" ccat = request.app.state.ccat - collections = list(ccat.memory.vectors.collections.keys()) - vector_memory = ccat.memory.vectors - to_return = {} - for c in collections: - ret = vector_memory.vector_db.delete_collection(collection_name=c) - to_return[c] = ret + deleted_memories = utils.delete_collections(ccat) ccat.load_memory() # recreate the long term memories ccat.mad_hatter.find_plugins() return { - "deleted": to_return, + "deleted": deleted_memories, } diff --git a/core/cat/utils.py b/core/cat/utils.py index ba46745a..5a3db1e9 100644 --- a/core/cat/utils.py +++ b/core/cat/utils.py @@ -310,3 +310,17 @@ def items(self): def __contains__(self, key): return key in self.keys() + + +def delete_collections(ccat: CheshireCat) -> Dict[str, bool]: + """Delete all collections""" + + collections = list(ccat.memory.vectors.collections.keys()) + vector_memory = ccat.memory.vectors + + deleted = {} + for c in collections: + ret = vector_memory.vector_db.delete_collection(collection_name=c) + deleted[c] = ret + + return deleted From ade411bccbb1bd6fd78c76239821eeef88d12473 Mon Sep 17 00:00:00 2001 From: Alfredo Date: Tue, 17 Sep 2024 19:06:05 +0200 Subject: [PATCH 2/4] chore: avoid circular import issues --- core/cat/utils.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/core/cat/utils.py b/core/cat/utils.py index 5a3db1e9..a204a5d5 100644 --- a/core/cat/utils.py +++ b/core/cat/utils.py @@ -1,5 +1,11 @@ """Various utiles used from the projects.""" +# Avoids circular import issues for type hints +from __future__ import annotations +from typing import TYPE_CHECKING +if TYPE_CHECKING: + from cat.looking_glass.cheshire_cat import CheshireCat + import os import traceback import inspect From 24518ad34333fbd23bc164cff3e9a3ead8d3e967 Mon Sep 17 00:00:00 2001 From: Alfredo Date: Tue, 17 Sep 2024 19:07:26 +0200 Subject: [PATCH 3/4] feat: factory reset --- core/cat/routes/settings.py | 46 +++++++++++++++++++++++++++++++++++-- 1 file changed, 44 insertions(+), 2 deletions(-) diff --git a/core/cat/routes/settings.py b/core/cat/routes/settings.py index 3bf8e08b..a5ca0f8f 100644 --- a/core/cat/routes/settings.py +++ b/core/cat/routes/settings.py @@ -1,9 +1,14 @@ +import os +import json + +from cat import utils from cat.auth.permissions import AuthPermission, AuthResource from cat.auth.connection import HTTPAuth -from fastapi import Depends, APIRouter, HTTPException +from fastapi import Depends, APIRouter, HTTPException, Request from cat.db import models from cat.db import crud - +from cat.env import get_env +from cat.looking_glass.cheshire_cat import CheshireCat router = APIRouter() @@ -102,3 +107,40 @@ def delete_setting( crud.delete_setting_by_id(settingId) return {"deleted": settingId} + + +@router.post("/factory-reset") +def factory_reset( + request: Request, + stray=Depends(HTTPAuth(AuthResource.SETTINGS, AuthPermission.WRITE)), +): + """Delete Cat's memory and metadata.json file (settings db). + A 'soft restart' is performed recreating the Cat instance and the metadata.json file. + Plugins are not affected but need to be switched on. + """ + + metadata_file = get_env("CCAT_METADATA_FILE") + metadata_content = {} + + try: + if os.path.exists(metadata_file): + with open(metadata_file, 'r') as file: + metadata_content = json.load(file) + + os.remove(metadata_file) + + deleted_memories = utils.delete_collections(request.app.state.ccat) + + utils.singleton.instances.clear() + request.app.state.strays.clear() + + request.app.state.ccat = CheshireCat() + except Exception as e: + raise HTTPException( + status_code=500, + detail={ + "error": f"An unexpected error occurred during factory reset: {str(e)}", + }, + ) + + return {"deleted_metadata": metadata_content, "deleted_memories": deleted_memories} From 9fe72b8799b190fca66c60c467344e97c284249a Mon Sep 17 00:00:00 2001 From: Alfredo Date: Sat, 19 Oct 2024 19:30:45 +0200 Subject: [PATCH 4/4] refactor: moved delete_collections to VectorMemory --- core/cat/memory/vector_memory.py | 12 ++++++++++++ core/cat/routes/memory/collections.py | 4 +--- core/cat/routes/settings.py | 4 +--- core/cat/utils.py | 19 ------------------- 4 files changed, 14 insertions(+), 25 deletions(-) diff --git a/core/cat/memory/vector_memory.py b/core/cat/memory/vector_memory.py index ea422f4f..27506692 100644 --- a/core/cat/memory/vector_memory.py +++ b/core/cat/memory/vector_memory.py @@ -87,6 +87,18 @@ def delete_collection(self, collection_name: str): """Delete specific vector collection""" return self.vector_db.delete_collection(collection_name) + + def delete_collections(self) -> dict[str, bool]: + """Delete all collections""" + + collections = list(self.collections.keys()) + + deleted = {} + for c in collections: + ret = self.delete_collection(c) + deleted[c] = ret + + return deleted def get_collection(self, collection_name: str): """Get collection info""" diff --git a/core/cat/routes/memory/collections.py b/core/cat/routes/memory/collections.py index 4ef72b26..83ed13ca 100644 --- a/core/cat/routes/memory/collections.py +++ b/core/cat/routes/memory/collections.py @@ -6,7 +6,6 @@ from cat.auth.permissions import AuthPermission, AuthResource from cat.memory.vector_memory import VectorMemory from cat.looking_glass.stray_cat import StrayCat -from cat import utils router = APIRouter() @@ -40,8 +39,7 @@ async def wipe_collections( ) -> Dict: """Delete and create all collections""" - vector_memory: VectorMemory = stray.memory.vectors - deleted_memories = utils.delete_collections(vector_memory) + deleted_memories = stray.memory.vectors.delete_collections() ccat: CheshireCat = request.app.state.ccat ccat.load_memory() # recreate the long term memories diff --git a/core/cat/routes/settings.py b/core/cat/routes/settings.py index 25ddd258..8fa20196 100644 --- a/core/cat/routes/settings.py +++ b/core/cat/routes/settings.py @@ -9,7 +9,6 @@ from cat.db import crud from cat.env import get_env from cat.looking_glass.cheshire_cat import CheshireCat -from cat.memory.vector_memory import VectorMemory router = APIRouter() @@ -130,8 +129,7 @@ def factory_reset( os.remove(metadata_file) - vector_memory: VectorMemory = stray.memory.vectors - deleted_memories = utils.delete_collections(vector_memory) + deleted_memories = stray.memory.vectors.delete_collections() utils.singleton.instances.clear() request.app.state.strays.clear() diff --git a/core/cat/utils.py b/core/cat/utils.py index f4b2bd43..ba46745a 100644 --- a/core/cat/utils.py +++ b/core/cat/utils.py @@ -1,11 +1,5 @@ """Various utiles used from the projects.""" -# Avoids circular import issues for type hints -from __future__ import annotations -from typing import TYPE_CHECKING -if TYPE_CHECKING: - from cat.memory.vector_memory import VectorMemory - import os import traceback import inspect @@ -316,16 +310,3 @@ def items(self): def __contains__(self, key): return key in self.keys() - - -def delete_collections(vector_memory: VectorMemory) -> Dict[str, bool]: - """Delete all collections""" - - collections = list(vector_memory.collections.keys()) - - deleted = {} - for c in collections: - ret = vector_memory.delete_collection(c) - deleted[c] = ret - - return deleted