@@ -5,6 +5,7 @@ import useChatHistory from 'hooks/useChatHistory';
5
5
import useStorageService from 'hooks/useStorageService' ;
6
6
import { FunctionComponent } from 'react' ;
7
7
import * as React from 'react' ;
8
+ import { unstable_batchedUpdates } from 'react-dom' ;
8
9
import VoiceOutput from 'shared/components/Speech/VoiceOutput' ;
9
10
import Application , { GptModels } from 'shared/constants/Application' ;
10
11
import HtmlHelper from 'shared/helpers/HtmlHelper' ;
@@ -164,6 +165,7 @@ const ContentPanel: FunctionComponent<IContentPanelProps> = ({ props }) => {
164
165
} , [ model ] ) ;
165
166
166
167
const elements : ContentPanelElements = React . useMemo ( ( ) => new ContentPanelElements ( props ) , [ ] ) ;
168
+ const refLatestResponseFromAi = React . useRef < HTMLDivElement > ( null ) ;
167
169
168
170
return (
169
171
< >
@@ -534,12 +536,18 @@ const ContentPanel: FunctionComponent<IContentPanelProps> = ({ props }) => {
534
536
} else {
535
537
const assistantResponses = newChatHistory . filter ( ( r ) => r . role === 'assistant' ) ;
536
538
assistantResponses [ assistantResponses . length - 1 ] . content += ChatHelper . cleanupResponseContent ( response ) ;
537
- const messageSelector = isCustomPanelOpen
538
- ? `.${ styles . customPanel } .${ styles . message } `
539
- : `div[id="${ wpId } "] .${ styles . message } ` ;
540
- const allMessages = document . querySelectorAll ( messageSelector ) ;
541
- const lastMessage = allMessages [ allMessages . length - 1 ] ;
542
- lastMessage . innerHTML += response ;
539
+ const queryParameters = new URLSearchParams ( window . location . search ) ;
540
+ const prev = queryParameters . get ( 'prev' ) ;
541
+ if ( ! prev && refLatestResponseFromAi . current ) {
542
+ refLatestResponseFromAi . current . innerHTML += response ;
543
+ } else {
544
+ const messageSelector = isCustomPanelOpen
545
+ ? `.${ styles . customPanel } .${ styles . message } `
546
+ : `div[id="${ wpId } "] .${ styles . message } ` ;
547
+ const allMessages = document . querySelectorAll ( messageSelector ) ;
548
+ const lastMessage = allMessages [ allMessages . length - 1 ] ;
549
+ lastMessage . innerHTML += response ;
550
+ }
543
551
}
544
552
// Bug fix: the following line caused to heavy rerendering. Replaced with a lighter code above.
545
553
//setChatHistory(newChatHistory); // Sets the updated array with new memory address.
@@ -549,36 +557,42 @@ const ContentPanel: FunctionComponent<IContentPanelProps> = ({ props }) => {
549
557
550
558
if ( props . apiService . isConfigured ( ) ) {
551
559
if ( ! props . streaming ) {
552
- props . apiService . callQueryText ( payload ) . then ( ( response ) => handleResponse ( response ) ) ;
560
+ props . apiService . callQueryText ( payload ) . then ( ( response ) => {
561
+ unstable_batchedUpdates ( ( ) => {
562
+ handleResponse ( response ) ;
563
+ } ) ;
564
+ } ) ;
553
565
} else {
554
566
let firstResponse = true ;
555
567
props . apiService . callQueryText ( payload , true , stopSignal , ( message : string , done ?: boolean , isError ?: boolean ) => {
556
- setIsProgress ( false ) ;
557
- setIsStreamProgress ( true ) ;
558
- if ( isError ) {
559
- setResponseContentError ( strings . TextUndeterminedError ) ;
560
- setIsStreamProgress ( false ) ;
561
- setFormattedContent ( [ ] ) ;
562
- } else if ( ! done ) {
563
- if ( message ) {
564
- handleResponseStream ( message , firstResponse ) ;
565
- firstResponse = false ;
566
- setResponseContentError ( undefined ) ;
567
- }
568
- } else {
569
- setIsStreamProgress ( false ) ;
570
- setFormattedContent ( [ ] ) ;
571
- if ( ! firstResponse ) {
572
- setResponseContentError ( undefined ) ;
573
- //const chatMessageId = `${styles.message}_${newChatHistory.length - 1}`;
574
- const chatMessageId = `${ styles . message } _${ wpId } _${ newChatHistory . length - 1 } ` ;
575
- setDisabledHighlights ( [ ...disabledHighlights . filter ( ( id ) => id !== chatMessageId ) ] ) ;
576
- saveChatHistory ( newChatHistory ) ;
577
- } else {
578
- // Authentication error?
568
+ unstable_batchedUpdates ( ( ) => {
569
+ setIsProgress ( false ) ;
570
+ setIsStreamProgress ( true ) ;
571
+ if ( isError ) {
579
572
setResponseContentError ( strings . TextUndeterminedError ) ;
573
+ setIsStreamProgress ( false ) ;
574
+ setFormattedContent ( [ ] ) ;
575
+ } else if ( ! done ) {
576
+ if ( message ) {
577
+ handleResponseStream ( message , firstResponse ) ;
578
+ firstResponse = false ;
579
+ setResponseContentError ( undefined ) ;
580
+ }
581
+ } else {
582
+ setIsStreamProgress ( false ) ;
583
+ setFormattedContent ( [ ] ) ;
584
+ if ( ! firstResponse ) {
585
+ setResponseContentError ( undefined ) ;
586
+ //const chatMessageId = `${styles.message}_${newChatHistory.length - 1}`;
587
+ const chatMessageId = `${ styles . message } _${ wpId } _${ newChatHistory . length - 1 } ` ;
588
+ setDisabledHighlights ( [ ...disabledHighlights . filter ( ( id ) => id !== chatMessageId ) ] ) ;
589
+ saveChatHistory ( newChatHistory ) ;
590
+ } else {
591
+ // Authentication error?
592
+ setResponseContentError ( strings . TextUndeterminedError ) ;
593
+ }
580
594
}
581
- }
595
+ } ) ;
582
596
} ) ;
583
597
}
584
598
} else {
@@ -703,6 +717,7 @@ const ContentPanel: FunctionComponent<IContentPanelProps> = ({ props }) => {
703
717
< div
704
718
id = { chatMessageId }
705
719
className = { [ 'ai' , styles . message , isCustomPanelOpen ? styles . insidePanel : undefined ] . join ( ' ' ) . trim ( ) }
720
+ ref = { refLatestResponseFromAi }
706
721
>
707
722
{ ! disabledHighlights ?. find ( ( id ) => id === chatMessageId ) ? formattedRows [ index ] : content }
708
723
</ div >
@@ -711,6 +726,7 @@ const ContentPanel: FunctionComponent<IContentPanelProps> = ({ props }) => {
711
726
id = { chatMessageId }
712
727
className = { [ 'ai' , styles . message ] . join ( ' ' ) }
713
728
dangerouslySetInnerHTML = { { __html : r . content } }
729
+ ref = { refLatestResponseFromAi }
714
730
/>
715
731
)
716
732
) : (
0 commit comments