Skip to content

feat(chat): Implement autosave for chat after messages and responses #1582

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

Merged
merged 4 commits into from
Jul 1, 2025

Conversation

Mathieu2301
Copy link
Contributor

This PR introduces a more robust autosave feature for the chat.

Previously, autosave was only triggered when creating a new chat or reloading the plugin. This could lead to data loss if the user closed Obsidian or if the plugin crashed.

The new implementation saves the chat automatically at several key moments, ensuring the conversation is always up-to-date:

  • After a user sends a message.
  • After the AI finishes generating a response.
  • After a user edits a message.
  • After an AI response is regenerated.

This is controlled by the "Autosave Chat" setting, which has been updated to reflect the new behavior. The dependencies of the useCallback hooks for handleEdit and handleRegenerate have also been updated to include the new logic.

I was inspired to make this change after losing several very important conversations multiple times in the same week. I was getting quite frustrated that the "Autosave Chat" setting didn't work in a way that felt logical, only saving on a new chat. I decided to fix it to create the behavior I, and likely many others, would expect, ensuring no more conversations are lost unexpectedly.

Adds autosave functionality to the chat component, saving the chat after every user message and AI response if the autosave setting is enabled. Updates the autosave chat setting description for clarity.
@logancyang
Copy link
Owner

bugbot run

cursor[bot]

This comment was marked as outdated.

Copy link
Owner

@logancyang logancyang left a comment

Choose a reason for hiding this comment

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

Thanks for the PR. Initially I wanted to have a separate cache for the current chat to prevent the data loss in case of a crash. This is fine too, I guess, but it's a bit too frequent, e.g. it saves both before and after an AI response, and the saving is a blocking op so it could impact performance.

cc @zeroliu wdyt?

@Mathieu2301
Copy link
Contributor Author

Ah I see, that makes sense! We should remove the await to make it non-blocking. That gives us a quick solution that keeps the crash protection without the performance impact.

Updated the Chat component to filter visible messages using sharedState.getMessages() instead of chatHistory, ensuring the latest messages are used. Also removed a redundant notice when updating an existing note.
The handleSaveAsNote function is now called without await in autosaveChat blocks.
@Mathieu2301
Copy link
Contributor Author

During testing of the autosave feature, I discovered that the saved chat files were missing the last 2 messages (user message and AI response).
This was caused by the handleSaveAsNote function using the React state chatHistory instead of the up-to-date sharedState.

Fixed by:

  • Modified handleSaveAsNote to use sharedState.getMessages() directly instead of chatHistory
  • Updated useCallback dependencies to include sharedState instead of chatHistory

Also removed the "Chat updated..." toast notifications that appeared on every autosave, as they were becoming intrusive during regular usage. The "Chat saved..." notification for new files is preserved.

@logancyang
Copy link
Owner

bugbot run

Copy link

@cursor cursor bot left a comment

Choose a reason for hiding this comment

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

✅ BugBot reviewed your changes and found no bugs!


Was this report helpful? Give feedback by reacting with 👍 or 👎

@logancyang
Copy link
Owner

This is functionally fine for now but there's a significant UX issue that is - with autosave on, the user will see a notice "chat saved" on every message. We have to make it much more subtle before we introduce this change.

@Mathieu2301
Copy link
Contributor Author

with autosave on, the user will see a notice "chat saved" on every message

I removed the "Chat updated..." toast notifications that appeared on every autosave

@logancyang
Copy link
Owner

with autosave on, the user will see a notice "chat saved" on every message

I removed the "Chat updated..." toast notifications that appeared on every autosave

Oh I see, and the "chat saved as note ..." notice only pops up at the first message and not subsequent ones. That's fine I think. Thanks for the PR, merging now and let's see what the users say 🙏

@logancyang logancyang merged commit ecf8eef into logancyang:master Jul 1, 2025
3 checks passed
wenzhengjiang pushed a commit that referenced this pull request Jul 9, 2025
…1582)

* feat(chat): Implement autosave for chat after messages and responses

Adds autosave functionality to the chat component, saving the chat after every user message and AI response if the autosave setting is enabled. Updates the autosave chat setting description for clarity.

* fix(chat): Use sharedState for visible messages in Chat

Updated the Chat component to filter visible messages using sharedState.getMessages() instead of chatHistory, ensuring the latest messages are used. Also removed a redundant notice when updating an existing note.

* fix(chat): Make autosave non-blocking

The handleSaveAsNote function is now called without await in autosaveChat blocks.

---------

Co-authored-by: Logan Yang <[email protected]>
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.

2 participants