Skip to content

Commit 9cd5414

Browse files
committed
fix: image stop issue, change all endVoiceConversation event by rn
1 parent c356793 commit 9cd5414

File tree

3 files changed

+22
-29
lines changed

3 files changed

+22
-29
lines changed

react-native/ios/Services/ConversationManager.swift

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,6 @@ class ConversationManager {
5757
}
5858

5959
novaSonicService?.onError = { [weak self] error in
60-
// end conversation
61-
Task{
62-
try await self?.endConversation()
63-
}
6460
self?.handleError(error)
6561
}
6662
}

react-native/src/chat/ChatScreen.tsx

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -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 (

react-native/src/chat/service/VoiceChatService.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,6 @@ export class VoiceChatService {
115115
private async getNewConfig(): Promise<object | null> {
116116
// Request new token
117117
const tokenResponse = await requestToken();
118-
console.log('new token:', tokenResponse);
119118
if (!tokenResponse) {
120119
if (this.onErrorCallback) {
121120
this.onErrorCallback('Failed to get credentials');

0 commit comments

Comments
 (0)