@@ -97,7 +97,9 @@ function ChatScreen(): React.JSX.Element {
9797 const tapIndex = route . params ?. tapIndex ;
9898 const mode = route . params ?. mode ?? currentMode ;
9999 const modeRef = useRef ( mode ) ;
100- const isNovaSonic = getTextModel ( ) . modelId . includes ( 'nova-sonic' ) ;
100+ const isNovaSonic =
101+ getTextModel ( ) . modelId . includes ( 'nova-sonic' ) &&
102+ modeRef . current === ChatMode . Text ;
101103
102104 const [ messages , setMessages ] = useState < SwiftChatMessage [ ] > ( [ ] ) ;
103105 const [ isLoadingMessages , setIsLoadingMessages ] = useState < boolean > ( false ) ;
@@ -133,6 +135,23 @@ function ChatScreen(): React.JSX.Element {
133135 const isVoiceLoading = useRef ( false ) ;
134136 const [ isShowVoiceLoading , setIsShowVoiceLoading ] = useState ( false ) ;
135137
138+ // End voice conversation and reset audio levels
139+ const endVoiceConversation = useCallback ( async ( ) => {
140+ if ( isVoiceLoading . current ) {
141+ return Promise . resolve ( false ) ;
142+ }
143+ isVoiceLoading . current = true ;
144+ setIsShowVoiceLoading ( true ) ;
145+ await voiceChatService . endConversation ( ) ;
146+ setAudioVolume ( 1 ) ;
147+ inputAudioLevelRef . current = 1 ;
148+ outputAudioLevelRef . current = 1 ;
149+ setChatStatus ( ChatStatus . Init ) ;
150+ isVoiceLoading . current = false ;
151+ setIsShowVoiceLoading ( false ) ;
152+ return true ;
153+ } , [ ] ) ;
154+
136155 // update refs value with state
137156 useEffect ( ( ) => {
138157 messagesRef . current = messages ;
@@ -163,10 +182,7 @@ function ChatScreen(): React.JSX.Element {
163182 message => {
164183 if ( getTextModel ( ) . modelId . includes ( 'nova-sonic' ) ) {
165184 handleVoiceChatTranscript ( 'ASSISTANT' , message ) ;
166- setAudioVolume ( 1 ) ;
167- inputAudioLevelRef . current = 1 ;
168- outputAudioLevelRef . current = 1 ;
169- setChatStatus ( ChatStatus . Init ) ;
185+ endVoiceConversation ( ) . then ( ) ;
170186 saveCurrentMessages ( ) ;
171187 console . log ( 'Voice chat error:' , message ) ;
172188 }
@@ -190,7 +206,7 @@ function ChatScreen(): React.JSX.Element {
190206 return ( ) => {
191207 voiceChatService . cleanup ( ) ;
192208 } ;
193- } , [ ] ) ;
209+ } , [ endVoiceConversation ] ) ;
194210
195211 // start new chat
196212 const startNewChat = useRef (
@@ -602,24 +618,6 @@ function ChatScreen(): React.JSX.Element {
602618 } ) ;
603619 } ;
604620
605- // Handle voice chat transcript
606- // End voice conversation and reset audio levels
607- const endVoiceConversation = useCallback ( async ( ) => {
608- if ( isVoiceLoading . current ) {
609- return Promise . resolve ( false ) ;
610- }
611- isVoiceLoading . current = true ;
612- setIsShowVoiceLoading ( true ) ;
613- await voiceChatService . endConversation ( ) ;
614- setAudioVolume ( 1 ) ;
615- inputAudioLevelRef . current = 1 ;
616- outputAudioLevelRef . current = 1 ;
617- setChatStatus ( ChatStatus . Init ) ;
618- isVoiceLoading . current = false ;
619- setIsShowVoiceLoading ( false ) ;
620- return true ;
621- } , [ ] ) ;
622-
623621 const handleVoiceChatTranscript = ( role : string , text : string ) => {
624622 const userId = role === 'USER' ? 1 : BOT_ID ;
625623 if (
0 commit comments