@@ -420,7 +420,6 @@ export function useChat(
420420 chatIdRef . current = undefined
421421 setResolvedChatId ( undefined )
422422 appliedChatIdRef . current = undefined
423- abortControllerRef . current ?. abort ( )
424423 abortControllerRef . current = null
425424 sendingRef . current = false
426425 setMessages ( [ ] )
@@ -445,7 +444,26 @@ export function useChat(
445444
446445 appliedChatIdRef . current = chatHistory . id
447446 const mappedMessages = chatHistory . messages . map ( mapStoredMessage )
448- setMessages ( mappedMessages )
447+ const shouldPreserveActiveStreamingMessage =
448+ sendingRef . current && Boolean ( activeStreamId ) && activeStreamId === streamIdRef . current
449+
450+ if ( shouldPreserveActiveStreamingMessage ) {
451+ setMessages ( ( prev ) => {
452+ const localStreamingAssistant = prev [ prev . length - 1 ]
453+ if ( localStreamingAssistant ?. role !== 'assistant' ) {
454+ return mappedMessages
455+ }
456+
457+ const nextMessages =
458+ mappedMessages [ mappedMessages . length - 1 ] ?. role === 'assistant'
459+ ? mappedMessages . slice ( 0 , - 1 )
460+ : mappedMessages
461+
462+ return [ ...nextMessages , localStreamingAssistant ]
463+ } )
464+ } else {
465+ setMessages ( mappedMessages )
466+ }
449467
450468 if ( chatHistory . resources . some ( ( r ) => r . id === 'streaming-file' ) ) {
451469 fetch ( '/api/copilot/chat/resources' , {
@@ -1467,7 +1485,6 @@ export function useChat(
14671485 return ( ) => {
14681486 streamReaderRef . current ?. cancel ( ) . catch ( ( ) => { } )
14691487 streamReaderRef . current = null
1470- abortControllerRef . current ?. abort ( )
14711488 abortControllerRef . current = null
14721489 streamGenRef . current ++
14731490 sendingRef . current = false
0 commit comments