@@ -538,6 +538,8 @@ export class ChatWidgetCore {
538538 if ( messageIndex !== - 1 ) {
539539 chat . messages [ messageIndex ] = assistantMessage ;
540540 this . storage . updateChat ( chat ) ;
541+ // Update the history panel's local state to keep UI in sync
542+ this . historyPanel . updateChatInLocalState ( chat ) ;
541543 }
542544 }
543545
@@ -744,6 +746,9 @@ export class ChatWidgetCore {
744746 chatContainer . style . display = 'flex' ;
745747 this . isOpen = true ;
746748
749+ // Refresh history panel from storage to ensure it's up to date
750+ this . historyPanel . refreshFromStorage ( ) ;
751+
747752 // Change the toggle button icon to a cross
748753 if ( toggleButton ) {
749754 toggleButton . innerHTML =
@@ -792,6 +797,7 @@ export class ChatWidgetCore {
792797 sender : 'user' ,
793798 timestamp : Date . now ( ) ,
794799 files : [ ...this . selectedFiles ] , // Include any selected files
800+ isComplete : true , // User messages are always complete when created
795801 } ;
796802
797803 this . hideWelcomeScreen ( ) ;
@@ -1081,6 +1087,9 @@ export class ChatWidgetCore {
10811087 return ;
10821088 }
10831089
1090+ // Mark the message as complete
1091+ lastMessage . isComplete = true ;
1092+
10841093 // Process pending contracts and update images (now async)
10851094 await this . processContractImages ( lastMessage . id ) ;
10861095
@@ -1097,11 +1106,22 @@ export class ChatWidgetCore {
10971106 text : lastMessage . text ,
10981107 intermediateSteps : lastMessage . intermediateSteps ,
10991108 processedImages : lastMessage . processedImages ,
1109+ isComplete : true , // Ensure it's marked as complete in storage too
11001110 } ;
11011111 }
11021112 return m ;
11031113 } ) ;
11041114 this . storage . updateChat ( chat ) ;
1115+ // Update the history panel's local state to keep UI in sync
1116+ this . historyPanel . updateChatInLocalState ( chat ) ;
1117+ }
1118+
1119+ // Add feedback buttons now that the message is complete
1120+ if (
1121+ typeof this . options . onFeedback === 'function' &&
1122+ lastMessage . sender === 'bot'
1123+ ) {
1124+ this . addFeedbackButtonsToMessage ( lastMessage . id , lastMessage . feedback ) ;
11051125 }
11061126
11071127 // hide the intermediate steps container
@@ -1208,6 +1228,7 @@ export class ChatWidgetCore {
12081228 text,
12091229 sender : 'bot' ,
12101230 timestamp : Date . now ( ) ,
1231+ isComplete : ! streaming , // Complete if not streaming, incomplete if streaming
12111232 } ;
12121233
12131234 this . addMessage ( botMessage ) ;
@@ -1277,6 +1298,8 @@ export class ChatWidgetCore {
12771298 chat . messages . push ( message ) ;
12781299 chat . updatedAt = Date . now ( ) ;
12791300 this . storage . updateChat ( chat ) ;
1301+ // Update the history panel's local state to keep UI in sync
1302+ this . historyPanel . updateChatInLocalState ( chat ) ;
12801303 }
12811304 }
12821305
@@ -1380,10 +1403,11 @@ export class ChatWidgetCore {
13801403 contentContainer . appendChild ( mainMessageContentWrapper ) ;
13811404 contentContainer . appendChild ( timestamp ) ;
13821405
1383- // Add feedback buttons for bot messages
1406+ // Add feedback buttons for bot messages only after they are complete
13841407 if (
13851408 typeof this . options . onFeedback === 'function' &&
1386- message . sender === 'bot'
1409+ message . sender === 'bot' &&
1410+ message . isComplete
13871411 ) {
13881412 const feedbackContainer = UIComponents . createFeedbackButtons (
13891413 message . id ,
@@ -1647,11 +1671,9 @@ export class ChatWidgetCore {
16471671 }
16481672 }
16491673
1650- private async loadChatHistory ( sessionId : string , updatedAt : number ) {
1674+ private async loadChatHistory ( sessionId : string ) {
16511675 const chats = this . storage . getChats ( ) ;
1652- const chat = chats . find (
1653- ( h : HistoryChat ) => h . sessionId === sessionId && h . updatedAt === updatedAt
1654- ) ;
1676+ const chat = chats . find ( ( h : HistoryChat ) => h . sessionId === sessionId ) ;
16551677 if ( chat ) {
16561678 this . historyPanel . setActiveChat ( chat ) ;
16571679 this . hideWelcomeScreen ( ) ;
@@ -1668,6 +1690,10 @@ export class ChatWidgetCore {
16681690 for ( const message of this . messages ) {
16691691 // skip empty messages since they are meaningless
16701692 if ( message . text ) {
1693+ // Ensure historical messages are marked as complete
1694+ if ( message . isComplete === undefined ) {
1695+ message . isComplete = true ;
1696+ }
16711697 await this . renderMessage ( message ) ;
16721698 }
16731699 }
@@ -1719,4 +1745,38 @@ export class ChatWidgetCore {
17191745 newChatButton . disabled = ! ! this . abortController ;
17201746 }
17211747 }
1748+
1749+ private addFeedbackButtonsToMessage (
1750+ messageId : string ,
1751+ selectedFeedback ?: 'positive' | 'negative'
1752+ ) : void {
1753+ const messageElement = this . widgetElement ?. querySelector (
1754+ `#chat-message-${ messageId } `
1755+ ) ;
1756+ if ( ! messageElement ) {
1757+ return ;
1758+ }
1759+
1760+ const contentContainer = messageElement . querySelector (
1761+ '.chat-message-content'
1762+ ) ;
1763+ if ( ! contentContainer ) {
1764+ return ;
1765+ }
1766+
1767+ // Check if feedback buttons already exist
1768+ const existingFeedback = contentContainer . querySelector (
1769+ '.chat-message-feedback'
1770+ ) ;
1771+ if ( existingFeedback ) {
1772+ return ;
1773+ }
1774+
1775+ // Create and add feedback buttons
1776+ const feedbackContainer = UIComponents . createFeedbackButtons (
1777+ messageId ,
1778+ selectedFeedback
1779+ ) ;
1780+ contentContainer . appendChild ( feedbackContainer ) ;
1781+ }
17221782}
0 commit comments