@@ -937,7 +937,7 @@ export const Bot = (botProps: BotProps & { class?: string }) => {
937937 setIsMessageStopping ( false ) ;
938938
939939 // Stop all TTS when aborting message
940- stopAllTTSLocally ( ) ;
940+ stopAllTTS ( ) ;
941941
942942 setMessages ( ( prevMessages ) => {
943943 const allMessages = [ ...cloneDeep ( prevMessages ) ] ;
@@ -1801,7 +1801,7 @@ export const Bot = (botProps: BotProps & { class?: string }) => {
18011801 setTTSAction ( true ) ;
18021802
18031803 // Ensure complete cleanup before starting new TTS
1804- stopAllTTSLocally ( ) ;
1804+ stopAllTTS ( ) ;
18051805
18061806 setIsTTSLoading ( ( prevState ) => ( {
18071807 ...prevState ,
@@ -1812,8 +1812,12 @@ export const Bot = (botProps: BotProps & { class?: string }) => {
18121812 const allMessages = [ ...cloneDeep ( prevMessages ) ] ;
18131813 const lastMessage = allMessages [ allMessages . length - 1 ] ;
18141814 if ( lastMessage . type === 'userMessage' ) return allMessages ;
1815- if ( lastMessage . id ) return allMessages ;
1816- allMessages [ allMessages . length - 1 ] . id = data . chatMessageId ;
1815+ const existingId = lastMessage . id || lastMessage . messageId ;
1816+ if ( ! existingId ) {
1817+ allMessages [ allMessages . length - 1 ] . id = data . chatMessageId ;
1818+ } else if ( ! lastMessage . id ) {
1819+ allMessages [ allMessages . length - 1 ] . id = existingId ;
1820+ }
18171821 return allMessages ;
18181822 } ) ;
18191823
@@ -1948,7 +1952,7 @@ export const Bot = (botProps: BotProps & { class?: string }) => {
19481952 const playingHandler = ( ) => {
19491953 setIsTTSLoading ( ( prevState ) => {
19501954 const newState = { ...prevState } ;
1951- newState [ data . chatMessageId ] = false ;
1955+ delete newState [ data . chatMessageId ] ;
19521956 return newState ;
19531957 } ) ;
19541958 setIsTTSPlaying ( ( prevState ) => ( {
@@ -2126,7 +2130,7 @@ export const Bot = (botProps: BotProps & { class?: string }) => {
21262130 cleanupTTSForMessage ( messageId ) ;
21272131 } ;
21282132
2129- const stopAllTTSLocally = ( ) => {
2133+ const stopAllTTS = ( ) => {
21302134 const audioElements = ttsAudio ( ) ;
21312135 Object . keys ( audioElements ) . forEach ( ( messageId ) => {
21322136 if ( audioElements [ messageId ] ) {
@@ -2151,11 +2155,7 @@ export const Bot = (botProps: BotProps & { class?: string }) => {
21512155 setIsTTSLoading ( { } ) ;
21522156 } ;
21532157
2154- const stopAllTTS = async ( ) => {
2155- // First do local cleanup
2156- stopAllTTSLocally ( ) ;
2157-
2158- // Then abort any active TTS requests on the server
2158+ const handleTTSAbortAll = async ( ) => {
21592159 const activeTTSMessages = Object . keys ( isTTSLoading ( ) ) . concat ( Object . keys ( isTTSPlaying ( ) ) ) ;
21602160 for ( const messageId of activeTTSMessages ) {
21612161 try {
@@ -2188,10 +2188,8 @@ export const Bot = (botProps: BotProps & { class?: string }) => {
21882188 setTTSAction ( true ) ;
21892189
21902190 // Ensure complete cleanup before starting new TTS
2191- await stopAllTTS ( ) ;
2192-
2193- // Wait for cleanup to complete
2194- await new Promise ( ( resolve ) => setTimeout ( resolve , 150 ) ) ;
2191+ await handleTTSAbortAll ( ) ;
2192+ stopAllTTS ( ) ;
21952193
21962194 handleTTSStart ( { chatMessageId : messageId , format : 'mp3' } ) ;
21972195
@@ -2265,7 +2263,6 @@ export const Bot = (botProps: BotProps & { class?: string }) => {
22652263 }
22662264 } catch ( error : any ) {
22672265 if ( error . name === 'AbortError' ) {
2268- console . log ( 'TTS request was aborted' ) ;
22692266 cleanupTTSForMessage ( messageId ) ;
22702267 } else {
22712268 console . error ( 'Error with TTS:' , error ) ;
0 commit comments