Skip to content

Commit 529eae6

Browse files
fix: resets audio ui when permission is denied (Chainlit#2771)
Wraps connection to microphone in a try/catch block to automatically reset ui when permission is not granted. Without this fix, the UI remains in loading state until the window is refreshed. <!-- This is an auto-generated description by cubic. --> --- ## Summary by cubic Prevents the audio UI from freezing when microphone permission is denied by catching setup errors and resetting the session. If access is blocked, audio capture is cleaned up, the stream ends, and the UI returns to idle. - **Bug Fixes** - Wrapped microphone begin/connect/record in a try/catch to handle setup failures. - On permission denial, safely end the recorder (ignore errors), interrupt the player, emit audio_end, and set audioConnection to off. <sup>Written for commit 7dbd38d. Summary will update on new commits.</sup> <!-- End of auto-generated description by cubic. -->
1 parent f8dfa3a commit 529eae6

1 file changed

Lines changed: 26 additions & 15 deletions

File tree

libs/react-client/src/useChatSession.ts

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ import {
2323
firstUserInteraction,
2424
isAiSpeakingState,
2525
loadingState,
26-
modesState,
2726
mcpState,
2827
messagesState,
28+
modesState,
2929
resumeThreadErrorState,
3030
sessionIdState,
3131
sessionState,
@@ -40,8 +40,8 @@ import {
4040
IAction,
4141
ICommand,
4242
IElement,
43-
IMode,
4443
IMessageElement,
44+
IMode,
4545
IStep,
4646
ITasklistElement,
4747
IThread
@@ -217,20 +217,31 @@ const useChatSession = () => {
217217
let isFirstChunk = true;
218218
const startTime = Date.now();
219219
const mimeType = 'pcm16';
220-
// Connect to microphone
221-
await wavRecorder.begin();
222-
await wavStreamPlayer.connect();
223-
await wavRecorder.record(async (data) => {
224-
const elapsedTime = Date.now() - startTime;
225-
socket.emit('audio_chunk', {
226-
isStart: isFirstChunk,
227-
mimeType,
228-
elapsedTime,
229-
data: data.mono
220+
try {
221+
await wavRecorder.begin();
222+
await wavStreamPlayer.connect();
223+
await wavRecorder.record(async (data) => {
224+
const elapsedTime = Date.now() - startTime;
225+
socket.emit('audio_chunk', {
226+
isStart: isFirstChunk,
227+
mimeType,
228+
elapsedTime,
229+
data: data.mono
230+
});
231+
isFirstChunk = false;
230232
});
231-
isFirstChunk = false;
232-
});
233-
wavStreamPlayer.onStop = () => setIsAiSpeaking(false);
233+
wavStreamPlayer.onStop = () => setIsAiSpeaking(false);
234+
} catch {
235+
try {
236+
await wavRecorder.end();
237+
} catch {
238+
// ignored
239+
}
240+
await wavStreamPlayer.interrupt();
241+
socket.emit('audio_end');
242+
setAudioConnection('off');
243+
return;
244+
}
234245
} else {
235246
await wavRecorder.end();
236247
await wavStreamPlayer.interrupt();

0 commit comments

Comments
 (0)