Skip to content

fix(ai-assistant): clear LLM history on restart chat - #3262

Merged
igorDykhta merged 2 commits into
keplergl:masterfrom
akashrai2003:fix/ai-assistant-chat-history
Dec 24, 2025
Merged

fix(ai-assistant): clear LLM history on restart chat#3262
igorDykhta merged 2 commits into
keplergl:masterfrom
akashrai2003:fix/ai-assistant-chat-history

Conversation

@akashrai2003

Copy link
Copy Markdown
Contributor
## Description
Fixes the restart chat functionality to properly clear LLM message history by calling the library's built-in `restartChat` function and forcing component remount.

## Problem
When "Restart Chat" is clicked, the UI resets but LLM providers still receive old messages, wasting tokens and increasing costs.

## Solution
- Call `restartChat()` from `@openassistant/core` to clear library state
- Force `AiAssistant` component remount using React key prop
- Ensures both library and component state are reset

## Changes
- Added `restartChat` from useAssistant hook
- Store in ref to access latest version
- Call `restartChat()` in onRestartAssistant handler
- Added `restartKey` state to force component remount
- Applied `key={restartKey}` to AiAssistant component

## Testing
- [x] Tested locally
- [x] Verified network logs show no old messages after restart
- [x] Works with all LLM providers (OpenAI, Google Gemini)
- [x] No breaking changes

Fixes #3261

## Checklist
- [x] Code follows project style guidelines
- [x] Comments explain the fix
- [x] Commit includes DCO sign-off
- [x] No breaking changes

When clicking 'Restart Chat', the LLM provider was still receiving the
entire conversation history in API calls, wasting tokens and increasing
costs.

Root cause: @openassistant library uses singleton instances that persist
across React component remounts, maintaining internal messageHistory.

Solution:
- Extract restartChat function from useAssistant hook
- Call it to clear library's internal singleton message cache
- Force React component remount using key prop
- Clear Redux message state

This eliminates token waste and reduces API costs for all users.

Signed-off-by: Akash Rai <akashtooop@gmail.com>
Copilot AI review requested due to automatic review settings December 17, 2025 16:09

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

This PR fixes the restart chat functionality in the AI Assistant to properly clear LLM message history. Previously, when users clicked "Restart Chat", the UI would reset but LLM providers still received old messages in subsequent requests, wasting tokens and increasing costs.

Key Changes:

  • Integrated the restartChat() function from the @openassistant/core library to clear internal library state
  • Implemented component remounting using React's key prop to ensure complete state reset
  • Added error handling for the restart operation with proper async/await pattern

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

Comment on lines +78 to +80
useEffect(() => {
restartChatRef.current = libraryRestartChat;
}, [libraryRestartChat]);

Copilot AI Dec 17, 2025

Copy link

Choose a reason for hiding this comment

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

The useEffect hook is unnecessary for updating the ref. Following React best practices and the pattern used elsewhere in the codebase (e.g., monaco-editor.tsx), refs can be updated via direct assignment in the component body. Replace the useEffect with direct assignment.

Suggested change
useEffect(() => {
restartChatRef.current = libraryRestartChat;
}, [libraryRestartChat]);
restartChatRef.current = libraryRestartChat;

Copilot uses AI. Check for mistakes.
Apply code review feedback - direct ref assignment is preferred
over useEffect for updating refs in component body.

Signed-off-by: Akash Rai <akashtooop@gmail.com>
@lixun910
lixun910 self-requested a review December 23, 2025 17:55

@lixun910 lixun910 left a comment

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.

Thank you @akashrai2003 !

@igorDykhta
igorDykhta merged commit 26e4a17 into keplergl:master Dec 24, 2025
7 checks passed
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.

4 participants