@@ -550,7 +550,6 @@ class ChatStore {
550550 await this . updateConversationName ( this . activeConversation . id , title ) ;
551551 }
552552
553- const allMessages = await DatabaseStore . getConversationMessages ( this . activeConversation . id ) ;
554553 const assistantMessage = await this . createAssistantMessage ( userMessage . id ) ;
555554
556555 if ( ! assistantMessage ) {
@@ -560,15 +559,23 @@ class ChatStore {
560559 this . activeMessages . push ( assistantMessage ) ;
561560 // Don't update currNode until after streaming completes to maintain proper conversation path
562561
563- await this . streamChatCompletion ( allMessages , assistantMessage , undefined , ( error : Error ) => {
564- if ( error . name === 'ContextError' && userMessage ) {
565- const userMessageIndex = this . findMessageIndex ( userMessage . id ) ;
566- if ( userMessageIndex !== - 1 ) {
567- this . activeMessages . splice ( userMessageIndex , 1 ) ;
568- DatabaseStore . deleteMessage ( userMessage . id ) . catch ( console . error ) ;
562+ const conversationContext = this . activeMessages . slice ( 0 , - 1 ) ;
563+
564+ await this . streamChatCompletion (
565+ conversationContext ,
566+ assistantMessage ,
567+ undefined ,
568+ ( error : Error ) => {
569+ if ( error . name === 'ContextError' && userMessage ) {
570+ const userMessageIndex = this . findMessageIndex ( userMessage . id ) ;
571+
572+ if ( userMessageIndex !== - 1 ) {
573+ this . activeMessages . splice ( userMessageIndex , 1 ) ;
574+ DatabaseStore . deleteMessage ( userMessage . id ) . catch ( console . error ) ;
575+ }
569576 }
570577 }
571- } ) ;
578+ ) ;
572579 } catch ( error ) {
573580 if ( this . isAbortError ( error ) ) {
574581 this . isLoading = false ;
@@ -810,7 +817,6 @@ class ChatStore {
810817 this . currentResponse = '' ;
811818
812819 try {
813- const allMessages = await DatabaseStore . getConversationMessages ( this . activeConversation . id ) ;
814820 const assistantMessage = await this . createAssistantMessage ( ) ;
815821
816822 if ( ! assistantMessage ) {
@@ -821,7 +827,9 @@ class ChatStore {
821827 await DatabaseStore . updateCurrentNode ( this . activeConversation . id , assistantMessage . id ) ;
822828 this . activeConversation . currNode = assistantMessage . id ;
823829
824- await this . streamChatCompletion ( allMessages , assistantMessage ) ;
830+ const conversationContext = this . activeMessages . slice ( 0 , - 1 ) ;
831+
832+ await this . streamChatCompletion ( conversationContext , assistantMessage ) ;
825833 } catch ( regenerateError ) {
826834 console . error ( 'Failed to regenerate response:' , regenerateError ) ;
827835 this . isLoading = false ;
0 commit comments