From 75e637002b1e52109d30e148bb6b74fb2cf79844 Mon Sep 17 00:00:00 2001 From: Russell Ballestrini Date: Wed, 18 Sep 2024 16:27:40 -0400 Subject: [PATCH] download markdown of conversation useful for github or jira modified: app.py modified: templates/base.html modified: templates/chat.html --- app.py | 40 ++++++++++++++++++++++++++++++++++++++++ templates/base.html | 12 +++++++++--- templates/chat.html | 6 +++++- 3 files changed, 54 insertions(+), 4 deletions(-) diff --git a/app.py b/app.py index 986430c..e86f5d6 100644 --- a/app.py +++ b/app.py @@ -314,6 +314,46 @@ def download_chat_history(): return response +@app.route("/download_chat_history_md", methods=["GET"]) +def download_chat_history_md(): + room_name = request.args.get("room_name") + room = get_room(room_name) + + if not room: + return jsonify({"error": "Room not found"}), 404 + + messages = Message.query.filter_by(room_id=room.id).all() + + if not messages: + return jsonify({"error": "No messages found"}), 404 + + # Access system users from the existing context + chat_history_md = [] + toc = [] + for index, message in enumerate(messages): + if not message.is_base64_image(): # Correctly call the method + role = "System" if message.username in system_users else "User" + header = f"### {role}: {message.username} (Turn {index + 1})" + toc.append( + f"- [{role}: {message.username} (Turn {index + 1})](#{role.lower()}-{message.username.lower().replace(' ', '-')}-turn-{index + 1})" + ) + chat_history_md.append(f"{header}\n\n{message.content}\n\n---\n") + + if not chat_history_md: + return jsonify({"error": "No valid messages found"}), 404 + + markdown_content = ( + f"# Chat History for {room.name}\n\n## Table of Contents\n" + + "\n".join(toc) + + "\n\n" + + "\n".join(chat_history_md) + ) + + response = Response(response=markdown_content, status=200, mimetype="text/markdown") + response.headers["Content-Disposition"] = f'attachment; filename="{room.name}.md"' + return response + + @app.route("/search") def search_page(): # Query all rooms so that newest is first. diff --git a/templates/base.html b/templates/base.html index 408f45b..aff35d7 100644 --- a/templates/base.html +++ b/templates/base.html @@ -163,11 +163,17 @@ display: block; padding-right: 0.8em; /* Adjust the padding as needed */ } - #downloadButton { - position: fixed; - top: 10px; + .download-links { + position: absolute; right: 10px; + top: 10px; z-index: 1000; + display: flex; + flex-direction: column; + } + + .download-links a { + margin-bottom: 5px; } diff --git a/templates/chat.html b/templates/chat.html index 6909ffb..7054313 100644 --- a/templates/chat.html +++ b/templates/chat.html @@ -3,7 +3,11 @@ {% block title %}Chatroom{% endblock %} {% block content %} - Download Chat History +