mirror of
https://github.com/danny-avila/LibreChat.git
synced 2025-12-18 09:20:15 +01:00
* fix: remove local state from Dropdown causing de-sync * refactor: cleanup STT code, avoid redundant states to prevent de-sync and side effects * fix: reset transcript after sending final text to prevent data loss * fix: clear timeout on component unmount to prevent memory leaks
49 lines
1.6 KiB
TypeScript
49 lines
1.6 KiB
TypeScript
import useSpeechToTextBrowser from './useSpeechToTextBrowser';
|
|
import useSpeechToTextExternal from './useSpeechToTextExternal';
|
|
import useGetAudioSettings from './useGetAudioSettings';
|
|
|
|
const useSpeechToText = (
|
|
setText: (text: string) => void,
|
|
onTranscriptionComplete: (text: string) => void,
|
|
): {
|
|
isLoading?: boolean;
|
|
isListening?: boolean;
|
|
stopRecording: () => void | (() => Promise<void>);
|
|
startRecording: () => void | (() => Promise<void>);
|
|
} => {
|
|
const { speechToTextEndpoint } = useGetAudioSettings();
|
|
const externalSpeechToText = speechToTextEndpoint === 'external';
|
|
|
|
const {
|
|
isListening: speechIsListeningBrowser,
|
|
isLoading: speechIsLoadingBrowser,
|
|
startRecording: startSpeechRecordingBrowser,
|
|
stopRecording: stopSpeechRecordingBrowser,
|
|
} = useSpeechToTextBrowser(setText, onTranscriptionComplete);
|
|
|
|
const {
|
|
isListening: speechIsListeningExternal,
|
|
isLoading: speechIsLoadingExternal,
|
|
externalStartRecording: startSpeechRecordingExternal,
|
|
externalStopRecording: stopSpeechRecordingExternal,
|
|
} = useSpeechToTextExternal(setText, onTranscriptionComplete);
|
|
|
|
const isListening = externalSpeechToText ? speechIsListeningExternal : speechIsListeningBrowser;
|
|
const isLoading = externalSpeechToText ? speechIsLoadingExternal : speechIsLoadingBrowser;
|
|
|
|
const startRecording = externalSpeechToText
|
|
? startSpeechRecordingExternal
|
|
: startSpeechRecordingBrowser;
|
|
const stopRecording = externalSpeechToText
|
|
? stopSpeechRecordingExternal
|
|
: stopSpeechRecordingBrowser;
|
|
|
|
return {
|
|
isLoading,
|
|
isListening,
|
|
stopRecording,
|
|
startRecording,
|
|
};
|
|
};
|
|
|
|
export default useSpeechToText;
|