Skip to content

Commit

Permalink
feat: append message to chat file
Browse files Browse the repository at this point in the history
  • Loading branch information
WilliamMRS committed Oct 17, 2024
1 parent 3e01f4a commit 60ee229
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 14 deletions.
4 changes: 2 additions & 2 deletions core/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
45 changes: 33 additions & 12 deletions core/modules/chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 60ee229

Please sign in to comment.