This repository has been archived by the owner on Feb 5, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 101
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add audio interface support (COR-000) (#178)
- Loading branch information
Showing
32 changed files
with
540 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,11 @@ | ||
export { default as close } from './close.svg?react'; | ||
export { default as closeV2 } from './closeV2.svg?react'; | ||
export { default as largeArrowLeft } from './large-arrow-left.svg?react'; | ||
export { default as microphone } from './microphone.svg?react'; | ||
export { default as minus } from './minus.svg?react'; | ||
export { default as smallArrowUp } from './small-arrow-up.svg?react'; | ||
export { default as sound } from './sound.svg?react'; | ||
export { default as soundOff } from './sound-off.svg?react'; | ||
export { default as stop } from './stop.svg?react'; | ||
export { default as thumbsUp } from './thumbs-up.svg?react'; | ||
export { default as topCaret } from './top-caret.svg?react'; |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
packages/react-chat/src/components/ChatInput/AudioInputButton.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import Icon from '../Icon'; | ||
import { AutoInputButtonContainer } from './styled'; | ||
|
||
interface AudioInputButtonProps { | ||
onStop: () => void; | ||
onStart: () => void; | ||
listening: boolean; | ||
} | ||
|
||
export const AudioInputButton: React.FC<AudioInputButtonProps> = ({ onStop, onStart, listening }) => { | ||
return ( | ||
<AutoInputButtonContainer onClick={listening ? onStop : onStart} listening={listening}> | ||
<Icon svg={listening ? 'stop' : 'microphone'} /> | ||
</AutoInputButtonContainer> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
import { useEffect, useLayoutEffect, useRef, useState } from 'react'; | ||
import ReactSpeechRecognition, { useSpeechRecognition as useReactSpeechRecognition } from 'react-speech-recognition'; | ||
|
||
import type { ChatSpeechRecognitionConfig, ChatSpeechRecognitionState } from '@/dtos/ChatConfig.dto'; | ||
|
||
export const useSpeechRecognition = ({ | ||
onSend, | ||
onValueChange, | ||
customSpeechRecognition, | ||
}: { | ||
onSend?: () => void; | ||
onValueChange: (value: string) => void; | ||
customSpeechRecognition?: ChatSpeechRecognitionConfig; | ||
}) => { | ||
const textareaRef = useRef<HTMLTextAreaElement>(null); | ||
const reactSpeechRecognition = useReactSpeechRecognition({ clearTranscriptOnListen: true }); | ||
|
||
const customSpeechRecognitionEnabled = | ||
!!customSpeechRecognition && | ||
(customSpeechRecognition.overrideNative || !reactSpeechRecognition.browserSupportsSpeechRecognition); | ||
|
||
const prevListening = useRef( | ||
customSpeechRecognitionEnabled ? customSpeechRecognition.initialState.listening : reactSpeechRecognition.listening | ||
); | ||
const prevProcessing = useRef( | ||
customSpeechRecognitionEnabled ? customSpeechRecognition.initialState.processing : false | ||
); | ||
const onSendPersisted = useRef(onSend); | ||
onSendPersisted.current = onSend; | ||
|
||
const [customSpeechRecognitionState, setCustomSpeechRecognitionState] = useState<ChatSpeechRecognitionState>( | ||
customSpeechRecognitionEnabled | ||
? customSpeechRecognition.initialState | ||
: { | ||
listening: reactSpeechRecognition.listening, | ||
transcript: reactSpeechRecognition.transcript, | ||
processing: false, | ||
microphoneAvailable: reactSpeechRecognition.isMicrophoneAvailable, | ||
} | ||
); | ||
|
||
const onStartListening = (): void => { | ||
if (customSpeechRecognitionEnabled) { | ||
customSpeechRecognition.resetTranscript(); | ||
customSpeechRecognition.startListening(); | ||
} else { | ||
reactSpeechRecognition.resetTranscript(); | ||
ReactSpeechRecognition.startListening({ continuous: true }); | ||
} | ||
}; | ||
|
||
const onStopListening = (): void => { | ||
if (customSpeechRecognitionEnabled) { | ||
customSpeechRecognition.stopListening(); | ||
} else { | ||
ReactSpeechRecognition.stopListening(); | ||
} | ||
}; | ||
|
||
useLayoutEffect(() => { | ||
if (customSpeechRecognitionEnabled || !reactSpeechRecognition.listening) return; | ||
|
||
onValueChange(reactSpeechRecognition.transcript); | ||
}, [customSpeechRecognitionEnabled, reactSpeechRecognition.transcript]); | ||
|
||
useEffect(() => { | ||
if (customSpeechRecognitionEnabled) { | ||
if (prevProcessing.current && !customSpeechRecognitionState.processing) { | ||
onSendPersisted.current?.(); | ||
customSpeechRecognition.resetTranscript(); | ||
textareaRef.current?.focus(); | ||
} | ||
|
||
prevProcessing.current = customSpeechRecognitionState.processing; | ||
} else { | ||
if (prevListening.current && !reactSpeechRecognition.listening) { | ||
onSendPersisted.current?.(); | ||
reactSpeechRecognition.resetTranscript(); | ||
textareaRef.current?.focus(); | ||
} | ||
|
||
prevListening.current = reactSpeechRecognition.listening; | ||
} | ||
}, [customSpeechRecognitionEnabled, reactSpeechRecognition.listening, customSpeechRecognitionState.processing]); | ||
|
||
useEffect(() => { | ||
if (!customSpeechRecognitionEnabled) return undefined; | ||
|
||
return customSpeechRecognition.onStateChange(setCustomSpeechRecognitionState); | ||
}, [customSpeechRecognitionEnabled]); | ||
|
||
return { | ||
available: customSpeechRecognitionEnabled || reactSpeechRecognition.browserSupportsSpeechRecognition, | ||
listening: customSpeechRecognitionEnabled | ||
? customSpeechRecognitionState.listening | ||
: reactSpeechRecognition.listening, | ||
processing: customSpeechRecognitionEnabled ? customSpeechRecognitionState.processing : false, | ||
textareaRef, | ||
stopListening: onStopListening, | ||
startListening: onStartListening, | ||
microphoneAvailable: customSpeechRecognitionEnabled | ||
? customSpeechRecognitionState.microphoneAvailable | ||
: reactSpeechRecognition.isMicrophoneAvailable, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.