-
Notifications
You must be signed in to change notification settings - Fork 0
Lo/failsafe for utterance end #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -211,7 +211,9 @@ export default function Conversation(): JSX.Element { | |||||
| useEffect(() => { | ||||||
| const onTranscript = (data: LiveTranscriptionEvent) => { | ||||||
| let content = utteranceText(data); | ||||||
| if (content || data.speech_final) { | ||||||
|
|
||||||
| // i only want an empty transcript part if it is speech_final | ||||||
| if (content !== "" || data.speech_final) { | ||||||
| /** | ||||||
| * use an outbound message queue to build up the unsent utterance | ||||||
| */ | ||||||
|
|
@@ -240,14 +242,16 @@ export default function Conversation(): JSX.Element { | |||||
| }; | ||||||
| }, [addTranscriptPart, connection]); | ||||||
|
|
||||||
| const [currentUtterance, setCurrentUtterance] = useState(""); | ||||||
| const [currentUtterance, setCurrentUtterance] = useState<string>(); | ||||||
|
|
||||||
| const getCurrentUtterance = useCallback(() => { | ||||||
| return transcriptParts.filter(({ is_final, speech_final }, i, arr) => { | ||||||
| return is_final || speech_final || (!is_final && i === arr.length - 1); | ||||||
| }); | ||||||
| }, [transcriptParts]); | ||||||
|
|
||||||
| const [lastUtterance, setLastUtterance] = useState<number>(); | ||||||
|
|
||||||
| useEffect(() => { | ||||||
| const parts = getCurrentUtterance(); | ||||||
| const last = parts[parts.length - 1]; | ||||||
|
|
@@ -256,20 +260,66 @@ export default function Conversation(): JSX.Element { | |||||
| .join(" ") | ||||||
| .trim(); | ||||||
|
|
||||||
| if (content === "") return; | ||||||
| /** | ||||||
| * if the entire utterance is empty, don't go any further | ||||||
| * for example, many many many empty transcription responses | ||||||
| */ | ||||||
| if (!content) return; | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggestion (code-quality): Use block braces for ifs, whiles, etc. (
Suggested change
ExplanationIt is recommended to always use braces and create explicit statement blocks.Using the allowed syntax to just write a single statement can lead to very confusing |
||||||
|
|
||||||
| /** | ||||||
| * display the concatenated utterances | ||||||
| */ | ||||||
| setCurrentUtterance(content); | ||||||
|
|
||||||
| /** | ||||||
| * record the last time we recieved a word | ||||||
| */ | ||||||
| if (last.text !== "") { | ||||||
| setLastUtterance(Date.now()); | ||||||
| } | ||||||
|
|
||||||
| /** | ||||||
| * if the last part of the utterance, empty or not, is speech_final, send to the LLM. | ||||||
| */ | ||||||
| if (last && last.speech_final) { | ||||||
| append({ | ||||||
| role: "user", | ||||||
| content, | ||||||
| }); | ||||||
| clearTranscriptParts(); | ||||||
| setCurrentUtterance(""); | ||||||
| setCurrentUtterance(undefined); | ||||||
| } | ||||||
| }, [getCurrentUtterance, clearTranscriptParts, append]); | ||||||
|
|
||||||
| /** | ||||||
| * incomplete speech final failsafe | ||||||
| */ | ||||||
| useEffect(() => { | ||||||
| if (!lastUtterance || !currentUtterance) return; | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggestion (code-quality): Use block braces for ifs, whiles, etc. (
Suggested change
ExplanationIt is recommended to always use braces and create explicit statement blocks.Using the allowed syntax to just write a single statement can lead to very confusing |
||||||
|
|
||||||
| const interval = setInterval(() => { | ||||||
| const timeLived = Date.now() - lastUtterance; | ||||||
|
|
||||||
| console.log(timeLived, timeLived > 1500, currentUtterance); | ||||||
|
|
||||||
| if (currentUtterance !== "" && timeLived > 1500) { | ||||||
| console.log("failsafe fires! pew pew!!"); | ||||||
|
|
||||||
| append({ | ||||||
| role: "user", | ||||||
| content: currentUtterance, | ||||||
| }); | ||||||
| clearTranscriptParts(); | ||||||
| setCurrentUtterance(undefined); | ||||||
| } | ||||||
| }, 100); | ||||||
|
|
||||||
| return () => { | ||||||
| clearInterval(interval); | ||||||
| }; | ||||||
| // eslint-disable-next-line react-hooks/exhaustive-deps | ||||||
| }, [lastUtterance, currentUtterance]); | ||||||
|
|
||||||
| /** | ||||||
| * barge-in | ||||||
| */ | ||||||
|
|
||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
issue (complexity): Consider simplifying the incomplete speech handling logic to reduce complexity.
The current implementation adds unnecessary complexity to handle incomplete speech scenarios. Instead of introducing new state variables and effects, consider simplifying the approach:
lastUtterancestate and the separate effect for the incomplete speech failsafe.This approach simplifies the logic by:
This maintains the desired functionality of handling incomplete speech while reducing overall complexity and improving readability.