fix(ai-assistant): clear LLM history on restart chat - #3262
Conversation
akashrai2003
commented
Dec 17, 2025
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>
There was a problem hiding this comment.
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/corelibrary 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.
| useEffect(() => { | ||
| restartChatRef.current = libraryRestartChat; | ||
| }, [libraryRestartChat]); |
There was a problem hiding this comment.
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.
| useEffect(() => { | |
| restartChatRef.current = libraryRestartChat; | |
| }, [libraryRestartChat]); | |
| restartChatRef.current = libraryRestartChat; |
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
left a comment
There was a problem hiding this comment.
Thank you @akashrai2003 !