From e09c763d3a78bad869e4c4887044a28b12e22884 Mon Sep 17 00:00:00 2001 From: Dave Horton Date: Tue, 18 Feb 2025 16:30:59 -0500 Subject: [PATCH] #1088 ignore UtteranceEnd if we have unprocessed words (#1089) * #1088 ignore UtteranceEnd if we have unprocessed words * wip --- lib/tasks/gather.js | 29 ++++++++++++++++++++++++----- 1 file changed, 24 insertions(+), 5 deletions(-) diff --git a/lib/tasks/gather.js b/lib/tasks/gather.js index 3e4d6731..6a1bb41a 100644 --- a/lib/tasks/gather.js +++ b/lib/tasks/gather.js @@ -785,10 +785,16 @@ class TaskGather extends SttTask { this.logger.debug('Gather:_onTranscription - got UtteranceEnd event from deepgram but no buffered transcripts'); } else { - this.logger.debug('Gather:_onTranscription - got UtteranceEnd event from deepgram, return buffered transcript'); - evt = this.consolidateTranscripts(this._bufferedTranscripts, 1, this.language, this.vendor); - this._bufferedTranscripts = []; - this._resolve('speech', evt); + const utteranceTime = evt.last_word_end; + if (utteranceTime && this._dgTimeOfLastUnprocessedWord && utteranceTime < this._dgTimeOfLastUnprocessedWord) { + this.logger.debug('Gather:_onTranscription - got UtteranceEnd with unprocessed words, continue listening'); + } + else { + this.logger.debug('Gather:_onTranscription - got UtteranceEnd from deepgram, return buffered transcript'); + evt = this.consolidateTranscripts(this._bufferedTranscripts, 1, this.language, this.vendor); + this._bufferedTranscripts = []; + this._resolve('speech', evt); + } } return; } @@ -922,8 +928,21 @@ class TaskGather extends SttTask { if (originalEvent.is_final && evt.alternatives[0].transcript !== '') { this.logger.debug({evt}, 'Gather:_onTranscription - buffering a completed (partial) deepgram transcript'); this._bufferedTranscripts.push(evt); + this._dgTimeOfLastUnprocessedWord = null; + } + if (evt.alternatives[0].transcript === '') { + emptyTranscript = true; + } + else if (!originalEvent.is_final) { + /* Deepgram: we have unprocessed words-save last word end time so we can later compare to UtteranceEnd */ + const words = originalEvent.channel.alternatives[0].words; + if (words?.length > 0) { + this._dgTimeOfLastUnprocessedWord = words.slice(-1)[0].end; + this.logger.debug( + `TaskGather:_onTranscription - saving word end time: ${this._dgTimeOfLastUnprocessedWord}`); + } + } - if (evt.alternatives[0].transcript === '') emptyTranscript = true; } if (!emptyTranscript) {