Skip to content

Commit

Permalink
feat: Aggiungi gestione della memoria per Queen of Hearts e verifica …
Browse files Browse the repository at this point in the history
…JSON valido
  • Loading branch information
d-beezee committed Dec 24, 2024
1 parent 8e5c9ed commit 35136f5
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 10 deletions.
3 changes: 2 additions & 1 deletion lib/activation.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down Expand Up @@ -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)

Expand Down
1 change: 1 addition & 0 deletions lib/const.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
QUEEN_OF_HEARTS_INJESTION_SETTING = 'queen_of_hearts_injested'
21 changes: 13 additions & 8 deletions lib/save_memory.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
value = parsed_message['value']
log.info(parsed_message)
memory = Memory(cat, value, message.why.input, parsed_message["why"])
memory.save()
20 changes: 19 additions & 1 deletion queen_of_hearts.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -17,4 +21,18 @@ def activated(plugin):
@hook
def before_cat_sends_message(message, cat):
save_memory( message,cat)
return message
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."}

0 comments on commit 35136f5

Please sign in to comment.