From 318686e622fc3633cc8714aa49804faa14f98ad7 Mon Sep 17 00:00:00 2001 From: Ilango Date: Tue, 23 Jan 2024 11:03:38 +0530 Subject: [PATCH] Fix issue where audio recording is not sent on stopping recording --- .../ui/src/views/chatmessage/ChatMessage.js | 359 +++++++++--------- 1 file changed, 183 insertions(+), 176 deletions(-) diff --git a/packages/ui/src/views/chatmessage/ChatMessage.js b/packages/ui/src/views/chatmessage/ChatMessage.js index e52bcfd5..006e2425 100644 --- a/packages/ui/src/views/chatmessage/ChatMessage.js +++ b/packages/ui/src/views/chatmessage/ChatMessage.js @@ -1,4 +1,4 @@ -import { useState, useRef, useEffect, useCallback } from 'react' +import { useState, useRef, useEffect, useCallback, Fragment } from 'react' import { useSelector } from 'react-redux' import PropTypes from 'prop-types' import socketIOClient from 'socket.io-client' @@ -96,6 +96,7 @@ export const ChatMessage = ({ open, chatflowid, isDialog }) => { // recording const [isRecording, setIsRecording] = useState(false) const [recordingNotSupported, setRecordingNotSupported] = useState(false) + const [isLoadingRecording, setIsLoadingRecording] = useState(false) const isFileAllowedForUpload = (file) => { const constraints = getAllowChatFlowUploads.data @@ -292,10 +293,8 @@ export const ChatMessage = ({ open, chatflowid, isDialog }) => { } const onRecordingStopped = async () => { + setIsLoadingRecording(true) stopAudioRecording(addRecordingToPreviews) - setIsRecording(false) - setRecordingNotSupported(false) - handlePromptClick('') } const onSourceDialogClick = (data, title) => { @@ -364,14 +363,13 @@ export const ChatMessage = ({ open, chatflowid, isDialog }) => { if (promptStarterInput !== undefined && promptStarterInput.trim() !== '') input = promptStarterInput setLoading(true) - const urls = [] - previews.map((item) => { - urls.push({ + const urls = previews.map((item) => { + return { data: item.data, type: item.type, name: item.name, mime: item.mime - }) + } }) clearPreviews() setMessages((prevMessages) => [...prevMessages, { message: input, type: 'userMessage', fileUploads: urls }]) @@ -383,7 +381,7 @@ export const ChatMessage = ({ open, chatflowid, isDialog }) => { history: messages.filter((msg) => msg.message !== 'Hi there! How can I help?'), chatId } - if (urls) params.uploads = urls + if (urls && urls.length > 0) params.uploads = urls if (isChatFlowAvailableToStream) params.socketIOClientId = socketIOClientId const response = await predictionApi.sendMessageAndGetPrediction(chatflowid, params) @@ -584,6 +582,16 @@ export const ChatMessage = ({ open, chatflowid, isDialog }) => { // eslint-disable-next-line react-hooks/exhaustive-deps }, [open, chatflowid]) + useEffect(() => { + // wait for audio recording to load and then send + if (previews.length === 1 && previews[0].type === 'audio') { + setIsRecording(false) + setRecordingNotSupported(false) + handlePromptClick('') + } + // eslint-disable-next-line + }, [previews]) + return (
{isDragActive && ( @@ -614,169 +622,167 @@ export const ChatMessage = ({ open, chatflowid, isDialog }) => { messages.map((message, index) => { return ( // The latest message sent by the user will be animated while waiting for a response - <> - - {/* Display the correct icon depending on the message type */} - {message.type === 'apiMessage' ? ( - AI - ) : ( - Me - )} -
- {message.usedTools && ( -
- {message.usedTools.map((tool, index) => { - return ( - onSourceDialogClick(tool, 'Used Tools')} - /> - ) - })} -
- )} - {message.fileUploads && message.fileUploads.length > 0 && ( -
- {message.fileUploads.map((item, index) => { - return ( - <> - {item.mime.startsWith('image/') ? ( - - - - ) : ( - // eslint-disable-next-line jsx-a11y/media-has-caption - - )} - - ) - })} -
- )} -
- {/* Messages are being rendered in Markdown format */} - - ) : ( - - {children} - - ) - } - }} - > - {message.message} - + + {/* Display the correct icon depending on the message type */} + {message.type === 'apiMessage' ? ( + AI + ) : ( + Me + )} +
+ {message.usedTools && ( +
+ {message.usedTools.map((tool, index) => { + return ( + onSourceDialogClick(tool, 'Used Tools')} + /> + ) + })}
- {message.fileAnnotations && ( -
- {message.fileAnnotations.map((fileAnnotation, index) => { - return ( - - ) - })} -
- )} - {message.sourceDocuments && ( -
- {removeDuplicateURL(message).map((source, index) => { - const URL = - source.metadata && source.metadata.source - ? isValidURL(source.metadata.source) - : undefined - return ( - - URL ? onURLClick(source.metadata.source) : onSourceDialogClick(source) - } + )} + {message.fileUploads && message.fileUploads.length > 0 && ( +
+ {message.fileUploads.map((item, index) => { + return ( + <> + {item.mime.startsWith('image/') ? ( + + + + ) : ( + // eslint-disable-next-line jsx-a11y/media-has-caption + + )} + + ) + })} +
+ )} +
+ {/* Messages are being rendered in Markdown format */} + + ) : ( + + {children} + ) - })} -
- )} + } + }} + > + {message.message} +
- - + {message.fileAnnotations && ( +
+ {message.fileAnnotations.map((fileAnnotation, index) => { + return ( + + ) + })} +
+ )} + {message.sourceDocuments && ( +
+ {removeDuplicateURL(message).map((source, index) => { + const URL = + source.metadata && source.metadata.source + ? isValidURL(source.metadata.source) + : undefined + return ( + + URL ? onURLClick(source.metadata.source) : onSourceDialogClick(source) + } + /> + ) + })} +
+ )} +
+
) })}
@@ -800,11 +806,10 @@ export const ChatMessage = ({ open, chatflowid, isDialog }) => { {previews && previews.length > 0 && ( {previews.map((item, index) => ( - <> + {item.mime.startsWith('image/') ? ( { backgroundColor: theme.palette.grey[500], flex: '0 0 auto' }} - key={index} variant='outlined' > { )} - + ))} )} @@ -854,7 +858,9 @@ export const ChatMessage = ({ open, chatflowid, isDialog }) => { {recordingNotSupported && (
-

To record audio, use modern browsers like Chrome or Firefox that support audio recording.

+ + To record audio, use modern browsers like Chrome or Firefox that support audio recording. +