From 60ee229efacedbd22f3b2ba4a125613550d51353 Mon Sep 17 00:00:00 2001 From: WilliamMRS Date: Thu, 17 Oct 2024 19:59:46 +0200 Subject: [PATCH] feat: append message to chat file --- core/main.py | 4 ++-- core/modules/chat.py | 45 ++++++++++++++++++++++++++++++++------------ 2 files changed, 35 insertions(+), 14 deletions(-) diff --git a/core/main.py b/core/main.py index 7dbcd70..7e3d62a 100644 --- a/core/main.py +++ b/core/main.py @@ -63,12 +63,12 @@ def summarize_store(): @socketio.on('connect') def connect(data): emit("You're connected to Jarvis streaming server...") - print('Client connected') + print('UI connected to backend') # Base event that's fired when user gracefully disconnects @socketio.on('disconnect') def disconnect(): - print('Client disconnected') + print('UI disconnected') # Custom event. Fired when the user sends a prompt. @socketio.on('user_prompt') diff --git a/core/modules/chat.py b/core/modules/chat.py index c191301..5292ec3 100644 --- a/core/modules/chat.py +++ b/core/modules/chat.py @@ -5,23 +5,44 @@ def read_chat(id: str) -> dict: ''' Uses chat_id to get the chat JSON file and returns a python dict object. ''' - dirname = os.path.dirname(os.path.dirname(__file__)) # Creates folder in core named user_data - filepath = os.path.join(dirname, f'user_data/chats/{id}.json') - # Open and read the JSON file - with open(filepath, 'r') as file: - data = json.load(file) - return data + try: + dirname = os.path.dirname(os.path.dirname(__file__)) # Creates folder in core named user_data + filepath = os.path.join(dirname, f'user_data/chats/{id}.txt') + # Open and read the chat txt file + with open(filepath, 'r') as file: + raw_text = file.read() + chat = json.loads("[" + raw_text + "]") # creates a dict by wrapping all messages as an array, making it valid json. Then loading it using json.load. + return chat + except Exception as e: + return e -def upsert_chat(chat_object: dict): - ''' - Upserts a chat dictionary object, saving it as json file in the user_data folder. - Upserting means to update or create if the file doesn't exist yet. Overwriting previous data. - ''' +def append_message_to_chat(message: dict, id: str): try: - print("hey") + dirname = os.path.dirname(os.path.dirname(__file__)) # Creates folder in core named user_data + filepath = os.path.join(dirname, f'user_data/chats/{id}.txt') + # Convert json to text and prepare for appending + message_txt = json.dump(message) + message_txt = f"\n,{message_txt}" + # Open the chat file + with open(filepath, 'a') as file: + file.write(message_txt) except Exception as e: return e +# def upsert_chat(chat_object: dict, id: str): +# ''' +# Upserts a chat dictionary object, saving it as json file in the user_data folder. +# Upserting means to update or create if the file doesn't exist yet. Overwriting previous data. +# ''' +# try: +# dirname = os.path.dirname(os.path.dirname(__file__)) # Creates folder in core named user_data +# filepath = os.path.join(dirname, f'user_data/chats/{id}.txt') +# # Open and write the JSON file +# with open(filepath, 'w') as file: +# file.write(json.dump(chat_object)) +# return True # Returns true if successfull +# except Exception as e: +# return e # json.dumps() - From python to json # json.load() - From json to python \ No newline at end of file