diff --git a/app.py b/app.py
index b630984..cbb1885 100644
--- a/app.py
+++ b/app.py
@@ -138,6 +138,521 @@ def on_join(data):
)
+@socketio.on("message")
+def handle_message(data):
+ room_name = data["room_name"]
+ room = get_room(room_name)
+
+ # Save the message to the database
+ new_message = Message(
+ username=data["username"],
+ content=data["message"],
+ room_id=room.id,
+ )
+ db.session.add(new_message)
+ db.session.commit()
+
+ emit(
+ "message",
+ {
+ "id": new_message.id,
+ "username": data["username"],
+ "content": data["message"],
+ },
+ room=room.name,
+ )
+
+ # detect and process special commands.
+ commands = data["message"].splitlines()
+
+ for command in commands:
+ if command.startswith("/s3 ls"):
+ # Extract the S3 file path pattern
+ s3_file_path_pattern = command.split(" ", 2)[2]
+ # List files from S3 and emit their names
+ eventlet.spawn(
+ list_s3_files, room.name, s3_file_path_pattern, data["username"]
+ )
+ if command.startswith("/s3 load"):
+ # Extract the S3 file path
+ s3_file_path = command.split(" ", 2)[2]
+ # Load the file from S3 and emit its content
+ eventlet.spawn(load_s3_file, room_name, s3_file_path, data["username"])
+ if command.startswith("/s3 save"):
+ # Extract the S3 key path
+ s3_key_path = command.split(" ", 2)[2]
+ # Save the most recent code block to S3
+ eventlet.spawn(
+ save_code_block_to_s3, room_name, s3_key_path, data["username"]
+ )
+ if command.startswith("/title new"):
+ eventlet.spawn(generate_new_title, room_name, data["username"])
+ if command.startswith("/cancel"):
+ # Cancel the most recent generation request
+ eventlet.spawn(cancel_generation, room_name, data["username"])
+
+ if "dall-e-3" in data["message"]:
+ # Use the entire message as the prompt for DALL-E 3
+ # Generate the image and emit its URL
+ eventlet.spawn(
+ generate_dalle_image, data["room_name"], data["message"], data["username"]
+ )
+
+ if (
+ "claude-v1" in data["message"]
+ or "claude-v2" in data["message"]
+ or "gpt-3" in data["message"]
+ or "gpt-4" in data["message"]
+ ):
+ # Emit a temporary message indicating that llm is processing
+ emit(
+ "message",
+ {"id": None, "content": f"Processing..."},
+ room=room.name,
+ )
+
+ if "claude-v1" in data["message"]:
+ eventlet.spawn(chat_claude, data["username"], room.name, data["message"])
+ if "claude-v2" in data["message"]:
+ eventlet.spawn(
+ chat_claude,
+ data["username"],
+ room.name,
+ data["message"],
+ model_name="anthropic.claude-v2",
+ )
+ if "gpt-3" in data["message"]:
+ eventlet.spawn(chat_gpt, data["username"], room.name, data["message"])
+ if "gpt-4" in data["message"]:
+ eventlet.spawn(
+ chat_gpt,
+ data["username"],
+ room.name,
+ data["message"],
+ model_name="gpt-4-1106-preview",
+ )
+
+
+@socketio.on("delete_message")
+def handle_delete_message(data):
+ msg_id = data["message_id"]
+ # Delete the message from the database
+ message = db.session.query(Message).filter(Message.id == msg_id).one_or_none()
+ if message:
+ db.session.delete(message)
+ db.session.commit()
+
+ # Notify all clients in the room to remove the message from their DOM
+ emit("message_deleted", {"message_id": msg_id}, room=data["room_name"])
+
+
+def chat_claude(username, room_name, message, model_name="anthropic.claude-v1"):
+ with app.app_context():
+ room = get_room(room_name)
+ # claude has a 100,000 token context window for prompts.
+ all_messages = (
+ Message.query.filter_by(room_id=room.id).order_by(Message.id.desc()).all()
+ )
+
+ chat_history = ""
+
+ def is_base64_image(content):
+ return '= 5:
+ room.title = gpt_generate_room_title(last_messages, model_name)
+ db.session.add(room)
+ db.session.commit()
+ socketio.emit("update_room_title", {"title": room.title}, room=room.name)
+ # Emit an event to update this rooms title in the sidebar for all users.
+ updated_room_data = {"id": room.id, "name": room.name, "title": room.title}
+ socketio.emit("update_room_list", updated_room_data, room=None)
+
+ def is_base64_image(content):
+ return '
{revised_prompt}
' + + except Exception as e: + # Set the content to an error message + content = f"Error generating image: {e}" + + # Store the content in the database and emit to the frontend + with app.app_context(): + room = get_room(room_name) + new_message = Message( + username=username, + content=content, # Store the img tag or error message as the content + room_id=room.id, # Make sure you have the room ID available + ) + db.session.add(new_message) + db.session.commit() + + # Emit the message with the content to the frontend + socketio.emit( + "message", + {"id": new_message.id, "username": username, "content": content}, + room=room_name, + ) + + def find_most_recent_code_block(room_name): with app.app_context(): # Get the room object from the database @@ -385,522 +900,6 @@ def cancel_generation(room_name, username): ) -@socketio.on("message") -def handle_message(data): - room_name = data["room_name"] - room = get_room(room_name) - - # Save the message to the database - new_message = Message( - username=data["username"], - content=data["message"], - room_id=room.id, - ) - db.session.add(new_message) - db.session.commit() - - emit( - "message", - { - "id": new_message.id, - "username": data["username"], - "content": data["message"], - }, - room=room.name, - ) - - # detect and process special commands. - commands = data["message"].splitlines() - - for command in commands: - if command.startswith("/s3 ls"): - # Extract the S3 file path pattern - s3_file_path_pattern = command.split(" ", 2)[2] - # List files from S3 and emit their names - eventlet.spawn( - list_s3_files, room.name, s3_file_path_pattern, data["username"] - ) - if command.startswith("/s3 load"): - # Extract the S3 file path - s3_file_path = command.split(" ", 2)[2] - # Load the file from S3 and emit its content - eventlet.spawn(load_s3_file, room_name, s3_file_path, data["username"]) - if command.startswith("/s3 save"): - # Extract the S3 key path - s3_key_path = command.split(" ", 2)[2] - # Save the most recent code block to S3 - eventlet.spawn( - save_code_block_to_s3, room_name, s3_key_path, data["username"] - ) - if command.startswith("/title new"): - eventlet.spawn(generate_new_title, room_name, data["username"]) - if command.startswith("/cancel"): - # Cancel the most recent generation request - eventlet.spawn(cancel_generation, room_name, data["username"]) - - if "dall-e-3" in data["message"]: - # Use the entire message as the prompt for DALL-E 3 - # Generate the image and emit its URL - eventlet.spawn( - generate_dalle_image, data["room_name"], data["message"], data["username"] - ) - - if ( - "claude-v1" in data["message"] - or "claude-v2" in data["message"] - or "gpt-3" in data["message"] - or "gpt-4" in data["message"] - ): - # Emit a temporary message indicating that llm is processing - emit( - "message", - {"id": None, "content": f"Processing..."}, - room=room.name, - ) - - if "claude-v1" in data["message"]: - eventlet.spawn(chat_claude, data["username"], room.name, data["message"]) - if "claude-v2" in data["message"]: - eventlet.spawn( - chat_claude, - data["username"], - room.name, - data["message"], - model_name="anthropic.claude-v2", - ) - if "gpt-3" in data["message"]: - eventlet.spawn(chat_gpt, data["username"], room.name, data["message"]) - if "gpt-4" in data["message"]: - eventlet.spawn( - chat_gpt, - data["username"], - room.name, - data["message"], - model_name="gpt-4-1106-preview", - ) - - -@socketio.on("delete_message") -def handle_delete_message(data): - msg_id = data["message_id"] - # Delete the message from the database - message = db.session.query(Message).filter(Message.id == msg_id).one_or_none() - if message: - db.session.delete(message) - db.session.commit() - - # Notify all clients in the room to remove the message from their DOM - emit("message_deleted", {"message_id": msg_id}, room=data["room_name"]) - - -def chat_claude(username, room_name, message, model_name="anthropic.claude-v1"): - with app.app_context(): - room = get_room(room_name) - # claude has a 100,000 token context window for prompts. - all_messages = ( - Message.query.filter_by(room_id=room.id).order_by(Message.id.desc()).all() - ) - - chat_history = "" - - def is_base64_image(content): - return "{revised_prompt}
' - - except Exception as e: - # Set the content to an error message - content = f"Error generating image: {e}" - - # Store the content in the database and emit to the frontend - with app.app_context(): - room = get_room(room_name) - new_message = Message( - username=username, - content=content, # Store the img tag or error message as the content - room_id=room.id, # Make sure you have the room ID available - ) - db.session.add(new_message) - db.session.commit() - - # Emit the message with the content to the frontend - socketio.emit( - "message", - {"id": new_message.id, "username": username, "content": content}, - room=room_name, - ) - - if __name__ == "__main__": import argparse