Skip to content

fix: ensure AI Assistant restart clears conversation history (#3261)#3317

Open
pierreeurope wants to merge 2 commits intokeplergl:masterfrom
pierreeurope:fix/issue-3261
Open

fix: ensure AI Assistant restart clears conversation history (#3261)#3317
pierreeurope wants to merge 2 commits intokeplergl:masterfrom
pierreeurope:fix/issue-3261

Conversation

@pierreeurope
Copy link
Copy Markdown
Contributor

Fixes #3261

This PR fixes a bug where clicking "Restart Chat" in the AI Assistant would clear the UI but continue sending the entire previous conversation history to LLM providers in subsequent API calls.

Problem:
The component was remounting (via the key prop) before the Redux state update had fully propagated, causing initialMessages to still contain the old conversation when the component re-initialized.

Solution:
Wrap the setRestartKey call in a setTimeout(0) to ensure it executes after the Redux state update has been applied. This guarantees that when the component remounts, aiAssistant?.messages is already empty.

Impact:

  • Eliminates wasted API tokens (potentially hundreds or thousands per conversation)
  • Reduces API costs
  • Improves response times
  • Ensures clean restart behavior

The fix is minimal and preserves all existing functionality while correctly resetting the conversation state.

@pierreeurope pierreeurope force-pushed the fix/issue-3261 branch 2 times, most recently from be17358 to a8ee7ef Compare February 10, 2026 19:17
@igorDykhta igorDykhta requested a review from lixun910 April 4, 2026 20:57
@lixun910 lixun910 requested a review from Copilot April 6, 2026 23:39
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Fixes an AI Assistant “Restart Chat” bug where the UI resets but subsequent LLM requests still include the prior conversation history, by ensuring both Redux and the assistant library state are cleared before forcing a remount.

Changes:

  • Clear Redux aiAssistant.messages when restarting chat.
  • Invoke the assistant library’s restartChat to reset internal state.
  • Defer the remount trigger (restartKey increment) to the next tick to avoid remounting with stale initialMessages.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +118 to +120

// Force remount after state has been cleared
// Use setTimeout to ensure Redux state update has propagated
Copy link

Copilot AI Apr 6, 2026

Choose a reason for hiding this comment

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

setTimeout schedules a state update that can still fire after AiAssistantComponent unmounts (e.g., when switching to config view or closing the panel), which can trigger React warnings and unnecessary work. Consider storing the timeout id in a ref and clearing it in a useEffect cleanup (or guarding the callback with an isMounted ref) before calling setRestartKey.

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

@copilot check if this comment makes sense and if yes, apply changes based on this feedback

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.

[Bug] AI Assistant restart chat still sends previous conversation history to LLM providers

3 participants