@@ -38,6 +38,7 @@ const [ASSISTANT, TOOL, ERROR] = ["assistant", "tool", "error"];
3838const Chat = ( ) => {
3939 const lastQuestionRef = useRef < string > ( "" ) ;
4040 const chatMessageStreamEnd = useRef < HTMLDivElement | null > ( null ) ;
41+ const audioStopRef = useRef < ( ( ) => void ) | null > ( null ) ;
4142 const [ isLoading , setIsLoading ] = useState < boolean > ( false ) ;
4243 const [ isGenerating , setIsGenerating ] = useState < boolean > ( false ) ; // Add this state
4344 const [ showLoadingMessage , setShowLoadingMessage ] = useState < boolean > ( false ) ;
@@ -303,11 +304,13 @@ const Chat = () => {
303304 } ;
304305
305306 const clearChat = ( ) => {
307+ audioStopRef . current ?.( ) ;
306308 lastQuestionRef . current = "" ;
307309 setActiveCitation ( undefined ) ;
308310 setAnswers ( [ ] ) ;
309311 setConversationId ( uuidv4 ( ) ) ;
310312 setSelectedConvId ( "" ) ;
313+ setActiveCardIndex ( null ) ;
311314 } ;
312315
313316 const stopGenerating = ( ) => {
@@ -385,9 +388,12 @@ const Chat = () => {
385388 [ ]
386389 ) ;
387390
388- const handleSpeech = ( index : number , status : string ) => {
391+ const handleSpeech = ( index : number , status : string , stopAudioFn ?: ( ) => void ) => {
389392 if ( status != "pause" ) setActiveCardIndex ( index ) ;
390393 setIsTextToSpeachActive ( status == "speak" ? true : false ) ;
394+ if ( status == "speak" && stopAudioFn ) {
395+ audioStopRef . current = stopAudioFn ;
396+ }
391397 } ;
392398 const onSetShowHistoryPanel = ( ) => {
393399 if ( ! showHistoryPanel ) {
@@ -423,8 +429,15 @@ const Chat = () => {
423429 console . error ( "No conversation Id found" ) ;
424430 return ;
425431 }
432+
433+ if ( id !== selectedConvId ) {
434+ audioStopRef . current ?.( ) ;
435+ setActiveCardIndex ( null ) ;
436+ }
437+
426438 const messages = getMessagesByConvId ( id ) ;
427439 if ( messages . length === 0 ) {
440+ setAnswers ( [ ] ) ;
428441 setFetchingConvMessages ( true ) ;
429442 const responseMessages = await historyRead ( id ) ;
430443 setAnswers ( responseMessages ) ;
@@ -433,7 +446,10 @@ const Chat = () => {
433446 } else {
434447 setAnswers ( messages ) ;
435448 }
436- setSelectedConvId ( id ) ;
449+
450+ if ( id !== selectedConvId ) {
451+ setSelectedConvId ( id ) ;
452+ }
437453 } ;
438454
439455 useEffect ( ( ) => {
@@ -477,6 +493,7 @@ const Chat = () => {
477493 ) ;
478494 setChatHistory ( tempChatHistory ) ;
479495 if ( id === selectedConvId ) {
496+ audioStopRef . current ?.( ) ;
480497 lastQuestionRef . current = "" ;
481498 setActiveCitation ( undefined ) ;
482499 setAnswers ( [ ] ) ;
0 commit comments