@@ -134,6 +134,9 @@ export type MessageType = {
134134 id ?: string ;
135135 followUpPrompts ?: string ;
136136 dateTime ?: string ;
137+ thinking ?: string ;
138+ thinkingDuration ?: number ;
139+ isThinking ?: boolean ;
137140} ;
138141
139142type IUploads = {
@@ -496,6 +499,7 @@ export const Bot = (botProps: BotProps & { class?: string }) => {
496499 const [ isLeadSaved , setIsLeadSaved ] = createSignal ( false ) ;
497500 const [ leadEmail , setLeadEmail ] = createSignal ( '' ) ;
498501 const [ disclaimerPopupOpen , setDisclaimerPopupOpen ] = createSignal ( false ) ;
502+ const [ isThinking , setIsThinking ] = createSignal ( false ) ;
499503
500504 const [ openFeedbackDialog , setOpenFeedbackDialog ] = createSignal ( false ) ;
501505 const [ feedback , setFeedback ] = createSignal ( '' ) ;
@@ -789,6 +793,41 @@ export const Bot = (botProps: BotProps & { class?: string }) => {
789793 } ) ;
790794 } ;
791795
796+ const handleThinkingEvent = ( data : string , duration ?: number ) => {
797+ if ( data && duration === undefined ) {
798+ setIsThinking ( true ) ;
799+ setMessages ( ( prevMessages ) => {
800+ const lastMsg = prevMessages [ prevMessages . length - 1 ] ;
801+ if ( lastMsg . type === 'userMessage' ) return prevMessages ;
802+ const allMessages = [ ...prevMessages . slice ( 0 , - 1 ) , { ...lastMsg , thinking : ( lastMsg . thinking || '' ) + data , isThinking : true } ] ;
803+ addChatMessage ( allMessages ) ;
804+ return allMessages ;
805+ } ) ;
806+ } else if ( data === '' && duration !== undefined ) {
807+ setIsThinking ( false ) ;
808+ setMessages ( ( prevMessages ) => {
809+ const lastMsg = prevMessages [ prevMessages . length - 1 ] ;
810+ if ( lastMsg . type === 'userMessage' ) return prevMessages ;
811+ const allMessages = [ ...prevMessages . slice ( 0 , - 1 ) , { ...lastMsg , thinkingDuration : duration , isThinking : false } ] ;
812+ addChatMessage ( allMessages ) ;
813+ return allMessages ;
814+ } ) ;
815+ }
816+ } ;
817+
818+ const finalizeThinking = ( ) => {
819+ if ( isThinking ( ) ) {
820+ setIsThinking ( false ) ;
821+ setMessages ( ( prevMessages ) => {
822+ const lastMsg = prevMessages [ prevMessages . length - 1 ] ;
823+ if ( lastMsg . type === 'userMessage' ) return prevMessages ;
824+ const allMessages = [ ...prevMessages . slice ( 0 , - 1 ) , { ...lastMsg , isThinking : false } ] ;
825+ addChatMessage ( allMessages ) ;
826+ return allMessages ;
827+ } ) ;
828+ }
829+ } ;
830+
792831 const clearPreviews = ( ) => {
793832 // Revoke the data uris to avoid memory leaks
794833 previews ( ) . forEach ( ( file ) => URL . revokeObjectURL ( file . preview ) ) ;
@@ -919,6 +958,9 @@ export const Bot = (botProps: BotProps & { class?: string }) => {
919958 case 'agentReasoning' :
920959 updateLastMessageAgentReasoning ( payload . data ) ;
921960 break ;
961+ case 'thinking' :
962+ handleThinkingEvent ( payload . data , payload . duration ) ;
963+ break ;
922964 case 'agentFlowEvent' :
923965 updateAgentFlowEvent ( payload . data ) ;
924966 break ;
@@ -942,6 +984,7 @@ export const Bot = (botProps: BotProps & { class?: string }) => {
942984 closeResponse ( ) ;
943985 break ;
944986 case 'end' :
987+ finalizeThinking ( ) ;
945988 setLocalStorageChatflow ( chatflowid , chatId ) ;
946989 closeResponse ( ) ;
947990 break ;
@@ -1194,6 +1237,8 @@ export const Bot = (botProps: BotProps & { class?: string }) => {
11941237 agentFlowExecutedData : data ?. agentFlowExecutedData ,
11951238 action : data ?. action ,
11961239 artifacts : data ?. artifacts ,
1240+ thinking : data ?. reasonContent ?. thinking ,
1241+ thinkingDuration : data ?. reasonContent ?. thinkingDuration ,
11971242 type : 'apiMessage' as messageType ,
11981243 feedback : null ,
11991244 dateTime : new Date ( ) . toISOString ( ) ,
@@ -1315,6 +1360,7 @@ export const Bot = (botProps: BotProps & { class?: string }) => {
13151360 messages . push ( { message : '' , type : 'leadCaptureMessage' } ) ;
13161361 }
13171362 setMessages ( messages ) ;
1363+ setShowScrollButton ( false ) ;
13181364 } catch ( error : any ) {
13191365 const errorData = error . response . data || `${ error . response . status } : ${ error . response . statusText } ` ;
13201366 console . error ( `error: ${ errorData } ` ) ;
@@ -1398,6 +1444,12 @@ export const Bot = (botProps: BotProps & { class?: string }) => {
13981444 if ( message . fileAnnotations ) chatHistory . fileAnnotations = message . fileAnnotations ;
13991445 if ( message . fileUploads ) chatHistory . fileUploads = message . fileUploads ;
14001446 if ( message . agentReasoning ) chatHistory . agentReasoning = message . agentReasoning ;
1447+ if ( ( message as any ) . reasonContent && typeof ( message as any ) . reasonContent === 'object' ) {
1448+ chatHistory . thinking = ( message as any ) . reasonContent . thinking ;
1449+ chatHistory . thinkingDuration = ( message as any ) . reasonContent . thinkingDuration ;
1450+ }
1451+ if ( message . thinking ) chatHistory . thinking = message . thinking ;
1452+ if ( message . thinkingDuration !== undefined ) chatHistory . thinkingDuration = message . thinkingDuration ;
14011453 if ( message . action ) chatHistory . action = message . action ;
14021454 if ( message . artifacts ) chatHistory . artifacts = message . artifacts ;
14031455 if ( message . followUpPrompts ) chatHistory . followUpPrompts = message . followUpPrompts ;
0 commit comments