Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 46 additions & 7 deletions lib/tasks/gather.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,10 @@ class TaskGather extends SttTask {
if (this.play) {
this.playTask = makeTask(this.logger, {play: this.play}, this);
}
if (!this.sayTask && !this.playTask) this.listenDuringPrompt = false;
if (!this.sayTask && !this.playTask) {
this.listenDuringPrompt = false;
this.playComplete = true; // No prompt, so consider it complete from the start
}

/* buffer speech for continuous asr */
this._bufferedTranscripts = [];
Expand Down Expand Up @@ -256,6 +259,16 @@ class TaskGather extends SttTask {
startDtmfListener();
}
this._stopVad();
// Check if we have buffered input ready (when bargein was disabled)
if (this._inputReady && !this.killed && !this.resolved) {
this.logger.debug(`Gather: prompt complete and input ready (${this._inputType}), resolving now`);
if (this._inputType === 'dtmf') {
this._resolve('dtmf-num-digits');
} else if (this._inputType === 'speech') {
this._resolve('speech', this._speechEvent);
}
return;
}
if (!this.killed) {
startListening(cs, ep);
if (this.input.includes('speech') && this.vendor === 'nuance' && this.listenDuringPrompt) {
Expand Down Expand Up @@ -292,6 +305,16 @@ class TaskGather extends SttTask {
startDtmfListener();
}
this._stopVad();
// Check if we have buffered input ready (when bargein was disabled)
if (this._inputReady && !this.killed && !this.resolved) {
this.logger.debug(`Gather: prompt complete and input ready (${this._inputType}), resolving now`);
if (this._inputType === 'dtmf') {
this._resolve('dtmf-num-digits');
} else if (this._inputType === 'speech') {
this._resolve('speech', this._speechEvent);
}
return;
}
if (!this.killed) {
startListening(cs, ep);
if (this.input.includes('speech') && this.vendor === 'nuance' && this.listenDuringPrompt) {
Expand Down Expand Up @@ -405,8 +428,15 @@ class TaskGather extends SttTask {
this.digitBuffer += evt.dtmf;
const len = this.digitBuffer.length;
if (len === this.numDigits || len === this.maxDigits) {
resolved = true;
this._resolve('dtmf-num-digits');
// Only resolve immediately if dtmfBargein is enabled, otherwise wait for prompt to complete or timeout
if (this.dtmfBargein) {
resolved = true;
this._resolve('dtmf-num-digits');
} else {
this.logger.debug('TaskGather:_onDtmf - sufficient digits collected but dtmfBargein=false, waiting for prompt completion');
this._inputReady = true;
this._inputType = 'dtmf';
}
}
}
if (!resolved && this.interDigitTimeout > 0 && this.digitBuffer.length >= this.minDigits) {
Expand Down Expand Up @@ -1035,7 +1065,15 @@ class TaskGather extends SttTask {
}

/* here is where we return a final transcript */
this._resolve('speech', evt);
// Only resolve immediately if bargein is enabled, otherwise wait for prompt to complete or timeout
if (this.bargein || this.playComplete) {
this._resolve('speech', evt);
} else {
this.logger.debug('TaskGather:_onTranscription - final transcript received but bargein=false, waiting for prompt completion');
this._inputReady = true;
this._inputType = 'speech';
this._speechEvent = evt;
}
/*}*/
}
}
Expand Down Expand Up @@ -1292,9 +1330,10 @@ class TaskGather extends SttTask {
}

this.resolved = true;
// If bargin is false and ws application return ack to verb:hook
// the gather should not play any audio
this._killAudio(this.cs);
// Kill audio only if prompt is still playing (bargein scenario or timeout during prompt)
if (!this.playComplete) {
this._killAudio(this.cs);
}
// Clear dtmf events, to avoid any case can leak the listener, just clean it
this.ep.removeAllListeners('dtmf');
clearTimeout(this.interDigitTimer);
Expand Down