Skip to content

Commit

Permalink
fix: fix failsafe repeats
Browse files Browse the repository at this point in the history
  • Loading branch information
lukeocodes committed Apr 12, 2024
1 parent 51754cd commit 5f9b25b
Show file tree
Hide file tree
Showing 6 changed files with 2,941 additions and 2,304 deletions.
9 changes: 0 additions & 9 deletions .devcontainer/devcontainer.json

This file was deleted.

27 changes: 25 additions & 2 deletions app/components/Conversation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
LiveTranscriptionEvents,
} from "@deepgram/sdk";
import { Message, useChat } from "ai/react";
import { NextUIProvider, Spinner } from "@nextui-org/react";
import { NextUIProvider } from "@nextui-org/react";
import { useMicVAD } from "@ricky0123/vad-react";
import { useNowPlaying } from "react-nowplaying";
import { useQueue } from "@uidotdev/usehooks";
Expand Down Expand Up @@ -161,6 +161,7 @@ export default function Conversation(): JSX.Element {

const [currentUtterance, setCurrentUtterance] = useState<string>();
const [failsafeTimeout, setFailsafeTimeout] = useState<NodeJS.Timeout>();
const [failsafeTriggered, setFailsafeTriggered] = useState<boolean>(false);

const onSpeechEnd = useCallback(() => {
/**
Expand All @@ -174,6 +175,7 @@ export default function Conversation(): JSX.Element {
setTimeout(() => {
if (currentUtterance) {
console.log("failsafe fires! pew pew!!");
setFailsafeTriggered(true);
append({
role: "user",
content: currentUtterance,
Expand Down Expand Up @@ -203,6 +205,12 @@ export default function Conversation(): JSX.Element {
*/
if (!microphoneOpen) return;


/**
* We we're talking again, we want to wait for a transcript.
*/
setFailsafeTriggered(false);

if (!player?.ended) {
stopAudio();
console.log("barging in! SHH!");
Expand Down Expand Up @@ -325,6 +333,15 @@ export default function Conversation(): JSX.Element {
*/
if (!content) return;

/**
* failsafe was triggered since we last sent a message to TTS
*/
if (failsafeTriggered) {
clearTranscriptParts();
setCurrentUtterance(undefined);
return;
}

/**
* display the concatenated utterances
*/
Expand All @@ -349,7 +366,13 @@ export default function Conversation(): JSX.Element {
clearTranscriptParts();
setCurrentUtterance(undefined);
}
}, [getCurrentUtterance, clearTranscriptParts, append, failsafeTimeout]);
}, [
getCurrentUtterance,
clearTranscriptParts,
append,
failsafeTimeout,
failsafeTriggered,
]);

/**
* magic microphone audio queue processing
Expand Down
16 changes: 5 additions & 11 deletions app/context/Toast.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,13 @@ const ToastContextProvider = ({ children }: ToastContextInterface) => {
<>
{children}
<ToastContainer
position="top-center"
autoClose={5000}
hideProgressBar={false}
newestOnTop={false}
closeOnClick
rtl={false}
pauseOnFocusLoss
draggable
pauseOnHover
theme="dark"
position={"bottom-center"}
autoClose={8000}
theme={"dark"}
limit={1}
transition={Bounce}
progressClassName="bg-gradient-to-r bg-gradient to-[#13EF93]/50 from-[#149AFB]/80"
className={"md:w-96 mb-26"}
progressClassName={"bg-gradient-to-r bg-gradient to-[#13EF93]/50 from-[#149AFB]/80"}
/>
</>
</ToastContext.Provider>
Expand Down
2 changes: 1 addition & 1 deletion app/globals.css
Original file line number Diff line number Diff line change
Expand Up @@ -147,4 +147,4 @@ body {
/* Override pygments style background color. */
.markdown .highlight pre {
@apply bg-gray-100 !important;
}
}
1 change: 1 addition & 0 deletions app/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { MicrophoneContextProvider } from "./context/Microphone";
import { AudioStoreContextProvider } from "./context/AudioStore";
import { ToastContextProvider } from "./context/Toast";

import 'react-toastify/dist/ReactToastify.css';
import "./globals.css";

import type { Metadata, Viewport } from "next";
Expand Down
Loading

0 comments on commit 5f9b25b

Please sign in to comment.