From 35136f58236fec5ecac907accc316f4c6595f281 Mon Sep 17 00:00:00 2001 From: Davide Bizzi Date: Tue, 24 Dec 2024 16:35:35 +0100 Subject: [PATCH] feat: Aggiungi gestione della memoria per Queen of Hearts e verifica JSON valido --- lib/activation.py | 3 ++- lib/const.py | 1 + lib/save_memory.py | 21 +++++++++++++-------- queen_of_hearts.py | 20 +++++++++++++++++++- 4 files changed, 35 insertions(+), 10 deletions(-) create mode 100644 lib/const.py diff --git a/lib/activation.py b/lib/activation.py index 9e0e738..fec1ef5 100644 --- a/lib/activation.py +++ b/lib/activation.py @@ -2,6 +2,7 @@ from cat.looking_glass.cheshire_cat import CheshireCat from cat.db.crud import get_setting_by_name,upsert_setting_by_name from cat.db.models import Setting +from .const import QUEEN_OF_HEARTS_INJESTION_SETTING ccat = CheshireCat() @@ -91,7 +92,7 @@ def update_injested_ids(key, value): def activation(plugin): - setting_key=plugin.id + "_injested" + setting_key=QUEEN_OF_HEARTS_INJESTION_SETTING injested_ids = get_injested_ids(setting_key) diff --git a/lib/const.py b/lib/const.py new file mode 100644 index 0000000..f2eca7b --- /dev/null +++ b/lib/const.py @@ -0,0 +1 @@ +QUEEN_OF_HEARTS_INJESTION_SETTING = 'queen_of_hearts_injested' \ No newline at end of file diff --git a/lib/save_memory.py b/lib/save_memory.py index b626944..aa0920c 100644 --- a/lib/save_memory.py +++ b/lib/save_memory.py @@ -2,17 +2,22 @@ from .memory import Memory import json +def is_valid_json(json_string): + try: + json.loads(json_string) + return True + except (ValueError, TypeError): + return False def save_memory( message, cat ): - log.info(message.content) - log.info(message.why.input) - parsed_message = json.loads(message.content) + if (is_valid_json(message.content)): + parsed_message = json.loads(message.content) - value = parsed_message['value'] - log.info(parsed_message) - memory = Memory(cat, value, message.why.input, parsed_message["why"]) - - memory.save() \ No newline at end of file + value = parsed_message['value'] + log.info(parsed_message) + memory = Memory(cat, value, message.why.input, parsed_message["why"]) + + memory.save() \ No newline at end of file diff --git a/queen_of_hearts.py b/queen_of_hearts.py index 0ad38ef..02d4dbd 100644 --- a/queen_of_hearts.py +++ b/queen_of_hearts.py @@ -1,5 +1,9 @@ from cat.mad_hatter.decorators import hook from cat.mad_hatter.decorators import plugin +from cat.mad_hatter.decorators import tool +from cat.db.crud import get_setting_by_name,delete_setting_by_id + +from .lib.const import QUEEN_OF_HEARTS_INJESTION_SETTING from .lib.activation import activation from .lib.prefix import prompt_prefix from .lib.save_memory import save_memory @@ -17,4 +21,18 @@ def activated(plugin): @hook def before_cat_sends_message(message, cat): save_memory( message,cat) - return message \ No newline at end of file + return message + + +@hook(priority=0) +def agent_fast_reply(fast_reply, cat): + if (cat.working_memory.user_message_json.text == "/empty_qoh"): + + setting = get_setting_by_name(QUEEN_OF_HEARTS_INJESTION_SETTING) + if setting is not None: + delete_setting_by_id(setting["setting_id"]) + return { + "output": "Ok, la memoria di Queen of Hearts è stata svuotata." + } + + return {"output":"Non c'è nulla da svuotare."}