From 988a265832086dd3690f15e78082d20af6daac80 Mon Sep 17 00:00:00 2001 From: Sanjiv Das Date: Sat, 16 Mar 2024 00:20:12 -0700 Subject: [PATCH] Update export.py - Added multiple chat history files - changed export to mention markdown files only - respond with filename in chat --- .../jupyter_ai/chat_handlers/export.py | 20 ++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/packages/jupyter-ai/jupyter_ai/chat_handlers/export.py b/packages/jupyter-ai/jupyter_ai/chat_handlers/export.py index a644ef423..7176ce63b 100644 --- a/packages/jupyter-ai/jupyter_ai/chat_handlers/export.py +++ b/packages/jupyter-ai/jupyter_ai/chat_handlers/export.py @@ -1,14 +1,12 @@ from typing import List - from jupyter_ai.models import AgentChatMessage, HumanChatMessage - from .base import BaseChatHandler, SlashCommandRoutingType - +import os class ExportChatHandler(BaseChatHandler): id = "export" name = "Export chat messages" - help = "Export the chat messages in various formats" + help = "Export the chat messages in markdown format" routing_type = SlashCommandRoutingType(slash_id="export") uses_llm = False @@ -24,10 +22,22 @@ def chat_message_to_markdown(self, message): else: return "" + # Multiple chat histories in separate files + def get_chat_filename(self, path='./chat_history.md'): + filename, extension = os.path.splitext(path) + counter = 1 + while os.path.exists(path): + path = filename + "_" + str(counter) + ".md" + counter += 1 + return path + async def process_message(self, _): markdown_content = "\n\n".join( self.chat_message_to_markdown(msg) for msg in self._chat_history ) # Write the markdown content to a file or do whatever you want with it - with open("./playground/chat_history.md", "w") as chat_history: + chat_filename = self.get_chat_filename() + with open(chat_filename, "w") as chat_history: chat_history.write(markdown_content) + + self.reply("File saved to " + chat_filename)