@@ -814,7 +814,7 @@ class ChatStore {
814814 ) ;
815815 }
816816 } ,
817- onError : ( error : Error ) => {
817+ onError : async ( error : Error ) => {
818818 this . setStreamingActive ( false ) ;
819819 if ( isAbortError ( error ) ) {
820820 cleanupStreamingState ( ) ;
@@ -826,13 +826,10 @@ class ChatStore {
826826 return ;
827827 }
828828 console . error ( 'Streaming error:' , error ) ;
829+ // keep whatever was streamed so far, the message stays in memory and in DB
830+ await this . savePartialResponseIfNeeded ( convId ) ;
829831 cleanupStreamingState ( ) ;
830832 this . clearPendingMessage ( convId ) ;
831- const idx = conversationsStore . findMessageIndex ( assistantMessage . id ) ;
832- if ( idx !== - 1 ) {
833- const failedMessage = conversationsStore . removeMessageAtIndex ( idx ) ;
834- if ( failedMessage ) DatabaseService . deleteMessage ( failedMessage . id ) . catch ( console . error ) ;
835- }
836833 const contextInfo = (
837834 error as Error & { contextInfo ?: { n_prompt_tokens : number ; n_ctx : number } }
838835 ) . contextInfo ;
@@ -1389,9 +1386,17 @@ class ChatStore {
13891386 }
13901387
13911388 console . error ( 'Continue generation error:' , error ) ;
1392- conversationsStore . updateMessageAtIndex ( idx , { content : originalContent } ) ;
1393-
1394- await DatabaseService . updateMessage ( msg . id , { content : originalContent } ) ;
1389+ // keep whatever was appended so far, the message stays in memory and in DB
1390+ await DatabaseService . updateMessage ( msg . id , {
1391+ content : originalContent + appendedContent ,
1392+ reasoningContent : originalReasoning + appendedReasoning || undefined ,
1393+ timestamp : Date . now ( )
1394+ } ) ;
1395+ conversationsStore . updateMessageAtIndex ( idx , {
1396+ content : originalContent + appendedContent ,
1397+ reasoningContent : originalReasoning + appendedReasoning || undefined ,
1398+ timestamp : Date . now ( )
1399+ } ) ;
13951400
13961401 this . setChatLoading ( msg . convId , false ) ;
13971402 this . clearChatStreaming ( msg . convId ) ;
0 commit comments