Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Delete chat message action to the Obsidian Plugin #1076

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

samhoooo
Copy link

@samhoooo samhoooo commented Jan 11, 2025

Closes #1020

Add Delete chat message action to the Obsidian Plugin

Description

The Khoj API supports deleting specific chat messages. The web app already uses that. But the Khoj Obsidian plugin doesn't use it yet.

This feature should

Add a Delete message button to each chat message footer in the Obsidian plugin's chat view
Clicking it should
Call the Khoj server API to delete the Khoj or user message
Refresh the Khoj Obsidian UI with the conversation turn (= Khoj + associated user message) deleted

Changes

  • Added ability to delete individual messages in chat conversations
  • Each message now has a delete button alongside copy and paste actions
  • Deleting a message removes both the user's message and Khoj's response (the full conversation turn)
  • Added confirmation dialog before deletion to prevent accidental deletions
  • Messages are deleted both from the UI and the Khoj server API
  • System messages cannot be deleted (e.g. setup hint)

Technical Details

  • Added new deleteMessage method to handle message deletion logic
  • Added DELETE API endpoint call to /api/chat/conversation/message
  • Delete button uses the same icon as in web version (trash-2)
  • Added new boolean parameter isSystemMessage to renderMessage method
  • Show delete button only if it is not a system message

Testing

  • Test deleting user messages
  • Test deleting Khoj responses
  • Verify both messages in a turn are removed
  • Verify deletion persists after page refresh
  • Verify no delete button on setup hint
  • Verify no delete button on error message
  • Verify no delete button on conversation starter
  • Test error cases (network failure, etc)

Screensort

Happy path:

Screen.Recording.2025-01-11.at.17.54.29.mov

Setup hint cannot be deleted:

image

const conversationId = chatBodyEl.dataset.conversationId;

// Get the turn_id from the message's data-meta attribute
const turnId = messageEl.getAttribute("data-meta");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @samhoooo ! This data field doesn't actually contain the turnId field that you need, so it just results in no change in the backend for the chat history. The turnId isn't currently being stored in the rendered message element. You would need to add some additional handling to store that, both in the streamed messages and in the chat history messages.

For streamed messages, you can update processMessageChunk and store the data in chatMessageState. The chunk.type in that case would be metadata. Then include it in the message element, similar to how data-meta is stored. I think finalizeChatBodyResponse would be a good place to add the data to the element.

You can give it the attribute name data-turnId and set the value to the incoming turnId.

For messages loaded from history, it would be similar, except you'd add the turnId field to renderMessageWithReferences. Just update the logic in the renderMessage function to include the turnId as well.

Then finally, in this deleteMesage function, you'll getAttribute("data-turnId"), instead of data-meta.

Does that make sense? Let me know if you need help with any of it. Thanks for opening the PR.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @sabaimran! Really appreciate your detailed explaination.

I have updated the PR to include the turnId handling as described, covered both streamed and messages loaded from history. I have also tested with my own account, delete some messages from Obsidian, and able to see chat history is updated on web.

Let me know if there is anything else I should watch out, thanks again for your guidance.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add Delete chat message action to the Obsidian Plugin
2 participants