Skip to content

Commit

Permalink
Update chat handling to clear chat and show help; small fix to `/expo…
Browse files Browse the repository at this point in the history
…rt` (jupyterlab#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 `<Enter>`

(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>
  • Loading branch information
2 people authored and Marchlak committed Oct 28, 2024
1 parent fea0aee commit 3bb388d
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
7 changes: 7 additions & 0 deletions packages/jupyter-ai/jupyter_ai/chat_handlers/clear.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
4 changes: 3 additions & 1 deletion packages/jupyter-ai/jupyter_ai/chat_handlers/export.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 <Enter> 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:
Expand Down

0 comments on commit 3bb388d

Please sign in to comment.