From 3bb388d21e09dc58fae27c2084d836a2aee1fb4a Mon Sep 17 00:00:00 2001 From: Sanjiv Das Date: Wed, 12 Jun 2024 11:09:05 -0700 Subject: [PATCH] Update chat handling to clear chat and show help; small fix to `/export` (#826) * Update chat handling (1) Make sure to use the `chat_history` prefix in the filename if none is supplied and not use a blank string on double `` (2) When using `/clear` clear the entire chat but show the help menu. * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> --- packages/jupyter-ai/jupyter_ai/chat_handlers/clear.py | 7 +++++++ packages/jupyter-ai/jupyter_ai/chat_handlers/export.py | 4 +++- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/packages/jupyter-ai/jupyter_ai/chat_handlers/clear.py b/packages/jupyter-ai/jupyter_ai/chat_handlers/clear.py index 0555abfca..51526801b 100644 --- a/packages/jupyter-ai/jupyter_ai/chat_handlers/clear.py +++ b/packages/jupyter-ai/jupyter_ai/chat_handlers/clear.py @@ -6,6 +6,8 @@ class ClearChatHandler(BaseChatHandler): + """Clear the chat panel and show the help menu""" + id = "clear" name = "Clear chat messages" help = "Clear the chat window" @@ -17,10 +19,15 @@ def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) async def process_message(self, _): + tmp_chat_history = self._chat_history[0] self._chat_history.clear() for handler in self._root_chat_handlers.values(): if not handler: continue handler.broadcast_message(ClearMessage()) + + self._chat_history = [tmp_chat_history] + self.reply(tmp_chat_history.body) + break diff --git a/packages/jupyter-ai/jupyter_ai/chat_handlers/export.py b/packages/jupyter-ai/jupyter_ai/chat_handlers/export.py index 3bde63018..ec3f1e53a 100644 --- a/packages/jupyter-ai/jupyter_ai/chat_handlers/export.py +++ b/packages/jupyter-ai/jupyter_ai/chat_handlers/export.py @@ -35,7 +35,9 @@ async def process_message(self, message: HumanChatMessage): self.chat_message_to_markdown(msg) for msg in self._chat_history ) args = self.parse_args(message) - chat_filename = args.path[0] if args.path else "chat_history" + chat_filename = ( + args.path[0] if (args.path and args.path[0] != "") else "chat_history" + ) # Handles both empty args and double tap key chat_filename = f"{chat_filename}-{datetime.now():%Y-%m-%d-%H-%M}.md" chat_file = os.path.join(self.root_dir, chat_filename) with open(chat_file, "w") as chat_history: