Skip to content

Commit

Permalink
fix: Fix an issue where voice may be enabled while bot is still strea…
Browse files Browse the repository at this point in the history
…ming messages
  • Loading branch information
tjtanjin committed May 20, 2024
1 parent 66c7575 commit 6cbaf8d
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 6 deletions.
13 changes: 8 additions & 5 deletions src/components/ChatBotContainer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { isChatBotVisible, isDesktop, parseMarkupMessage } from "../services/Uti
import { useBotOptions } from "../context/BotOptionsContext";
import { useMessages } from "../context/MessagesContext";
import { usePaths } from "../context/PathsContext";
import { Block } from "../types/Block";
import { Flow } from "../types/Flow";
import { Message } from "../types/Message";
import { Params } from "../types/Params";
Expand Down Expand Up @@ -238,7 +239,7 @@ const ChatBotContainer = ({ flow }: { flow: Flow }) => {
// performs pre-processing when paths change
useEffect(() => {
const currPath = getCurrPath();
if (!currPath ) {
if (!currPath) {
return;
}
const block = flow[currPath];
Expand All @@ -249,19 +250,20 @@ const ChatBotContainer = ({ flow }: { flow: Flow }) => {
return;
}

syncVoiceWithChatInput(keepVoiceOnRef.current && !block.chatDisabled, botOptions);
const params = {prevPath: getPrevPath(), userInput: paramsInputRef.current,
injectMessage, streamMessage, openChat};
callNewBlock(currPath, params);
callNewBlock(currPath, block, params);
}, [paths]);

/**
* Calls the new block for preprocessing upon change to path.
*
* @param currPath the current path
* @param block the current block
* @param params parameters that may be used in the block
*/
const callNewBlock = async (currPath: keyof Flow, params: Params) => {
const callNewBlock = async (currPath: keyof Flow, block: Block, params: Params) => {
// when bot first loads, disable textarea first to allow uninterrupted sending of initial messages
if (currPath === "start") {
setTextAreaDisabled(true);
}
Expand All @@ -272,6 +274,7 @@ const ChatBotContainer = ({ flow }: { flow: Flow }) => {
// cleanup logic after preprocessing of a block
setIsBotTyping(false);
updateTextArea();
syncVoiceWithChatInput(keepVoiceOnRef.current && !block.chatDisabled, botOptions);

// cleanup logic after preprocessing of a block (affects only streaming messages)
isBotStreamingRef.current = false
Expand Down Expand Up @@ -411,7 +414,7 @@ const ChatBotContainer = ({ flow }: { flow: Flow }) => {
* @param useMarkup boolean indicating whether markup is used
*/
const simulateStream = async (message: Message, streamSpeed: number, useMarkup: boolean) => {
// when simulating stream, disable text area and stop bot typing
// stop bot typing when simulating stream
setIsBotTyping(false);

// set an initial empty message to be used for streaming
Expand Down
2 changes: 1 addition & 1 deletion src/services/VoiceService.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ export const stopVoiceRecording = () => {
/**
* Syncs voice toggle to textarea state (voice should not be enabled if textarea is disabled).
*
* @param keepVoiceOn boolean indicating if voice was and thus is to be kept toggled on
* @param keepVoiceOn boolean indicating if voice was on and thus is to be kept toggled on
* @param botOptions options provided to the bot
*/
export const syncVoiceWithChatInput = (keepVoiceOn: boolean, botOptions: Options) => {
Expand Down

0 comments on commit 6cbaf8d

Please sign in to comment.