From a896f3f312bcecf9b9df588b63a4a3b78efbe06d Mon Sep 17 00:00:00 2001 From: navyseal4000 Date: Thu, 21 Nov 2024 07:55:53 -0600 Subject: [PATCH] Clear speech to text, listening upon submission --- app/components/chat/BaseChat.tsx | 38 ++++++++++++++++++++++++++++---- app/utils/constants.ts | 2 +- 2 files changed, 35 insertions(+), 5 deletions(-) diff --git a/app/components/chat/BaseChat.tsx b/app/components/chat/BaseChat.tsx index fee28c0..dde0cca 100644 --- a/app/components/chat/BaseChat.tsx +++ b/app/components/chat/BaseChat.tsx @@ -145,6 +145,7 @@ export const BaseChat = React.forwardRef( const [modelList, setModelList] = useState(MODEL_LIST); const [isListening, setIsListening] = useState(false); const [recognition, setRecognition] = useState(null); + const [transcript, setTranscript] = useState(''); useEffect(() => { // Load API keys from cookies on component mount @@ -177,6 +178,9 @@ export const BaseChat = React.forwardRef( .map(result => result.transcript) .join(''); + setTranscript(transcript); + + if (handleInputChange) { const syntheticEvent = { target: { value: transcript }, @@ -208,6 +212,25 @@ export const BaseChat = React.forwardRef( } }; + const handleSendMessage = (event: React.UIEvent, messageInput?: string) => { + if (sendMessage) { + sendMessage(event, messageInput); + if (recognition) { + recognition.abort(); // Stop current recognition + setTranscript(''); // Clear transcript + setIsListening(false); + + // Clear the input by triggering handleInputChange with empty value + if (handleInputChange) { + const syntheticEvent = { + target: { value: '' }, + } as React.ChangeEvent; + handleInputChange(syntheticEvent); + } + } + } + }; + const updateApiKey = (provider: string, key: string) => { try { const updatedApiKeys = { ...apiKeys, [provider]: key }; @@ -301,8 +324,11 @@ export const BaseChat = React.forwardRef( } event.preventDefault(); - - sendMessage?.(event); + if (isStreaming) { + handleStop?.(); + return; + } + handleSendMessage?.(event); } }} value={input} @@ -327,7 +353,7 @@ export const BaseChat = React.forwardRef( return; } - sendMessage?.(event); + handleSendMessage?.(event); }} /> )} @@ -384,7 +410,11 @@ export const BaseChat = React.forwardRef(