-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Implement sentence-based TTS streaming in interact mode
- Add sentence-utils.js with naive sentence completion algorithm - Modify readResponseStream to process sentences in real-time - Integrate TTS fetching for completed sentences - Utilize audio queue to prevent overlapping playback This commit introduces a significant improvement to the interact mode, enabling faster voice-based interactions by processing and sending completed sentences to the OpenAI TTS endpoint as they become available in the streamed response. Details: - Implement getCompleteSentences function in sentence-utils.js - Update readResponseStream to use sentence-based processing - Add logic to track and process only new sentences - Integrate with existing audio queue for smooth playback - Handle potential errors in TTS fetching Performance impact: - Reduces latency in voice responses during interact mode
- Loading branch information
Showing
3 changed files
with
85 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
export function getCompleteSentences(buffer) { | ||
const sentences = []; | ||
let currentSentence = ''; | ||
const sentenceEndings = ['.', '!', '?']; | ||
let inQuotes = false; | ||
let bracketDepth = 0; | ||
|
||
for (let i = 0; i < buffer.length; i++) { | ||
const char = buffer[i]; | ||
currentSentence += char; | ||
|
||
// Handle quotes | ||
if (char === '"') { | ||
inQuotes = !inQuotes; | ||
} | ||
|
||
// Handle brackets | ||
if (char === '(' || char === '[' || char === '{') { | ||
bracketDepth++; | ||
} else if (char === ')' || char === ']' || char === '}') { | ||
bracketDepth = Math.max(0, bracketDepth - 1); | ||
} | ||
|
||
// Check for sentence end | ||
if (sentenceEndings.includes(char) && | ||
!inQuotes && | ||
bracketDepth === 0 && | ||
(i === buffer.length - 1 || /\s/.test(buffer[i + 1]))) { | ||
sentences.push(currentSentence.trim()); | ||
currentSentence = ''; | ||
} | ||
} | ||
|
||
return sentences; | ||
} |