From 6149eff373a8b42498ae14349ac4f7946b49ab39 Mon Sep 17 00:00:00 2001 From: Dave Horton Date: Thu, 19 Jan 2023 09:29:17 -0500 Subject: [PATCH] =?UTF-8?q?when=20clearing=20stack=20on=20background=20gat?= =?UTF-8?q?her=20(bargein),=20search=20to=20see=20if=20=E2=80=A6=20(#221)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * when clearing stack on background gather (bargein), search to see if there is a config further down the stack that turns bargein off, and if so clear only down to there (#220) * avoid clearing the stack twice when a background gather bargein occurs * fix bug from prev commit --- .gitignore | 3 ++- lib/middleware.js | 4 +++- lib/session/call-session.js | 37 ++++++++++++++++++++++++++++--------- 3 files changed, 33 insertions(+), 11 deletions(-) diff --git a/.gitignore b/.gitignore index 7215369c..d26bda4e 100644 --- a/.gitignore +++ b/.gitignore @@ -40,4 +40,5 @@ examples/* ecosystem.config.js .vscode test/credentials/*.json -run-tests.sh \ No newline at end of file +run-tests.sh +run-coverage.sh \ No newline at end of file diff --git a/lib/middleware.js b/lib/middleware.js index 738e860a..fae43e09 100644 --- a/lib/middleware.js +++ b/lib/middleware.js @@ -245,7 +245,9 @@ module.exports = function(srf, logger) { // eslint-disable-next-line no-unused-vars const {call_hook, call_status_hook, ...appInfo} = app; // mask sensitive data like user/pass on webhook - logger.info({app: appInfo}, `retrieved application for incoming call to ${req.locals.calledNumber}`); + // eslint-disable-next-line no-unused-vars + const {requestor, notifier, ...loggable} = appInfo; + logger.info({app: loggable}, `retrieved application for incoming call to ${req.locals.calledNumber}`); req.locals.callInfo = new CallInfo({ req, app: app2, diff --git a/lib/session/call-session.js b/lib/session/call-session.js index 5c608709..d43f13da 100644 --- a/lib/session/call-session.js +++ b/lib/session/call-session.js @@ -457,10 +457,10 @@ class CallSession extends Emitter { this.backgroundGatherTask = makeTask(this.logger, t[0]); this._bargeInEnabled = true; this.backgroundGatherTask - .once('dtmf', this._clearTasks.bind(this)) - .once('vad', this._clearTasks.bind(this)) - .once('transcription', this._clearTasks.bind(this)) - .once('timeout', this._clearTasks.bind(this)); + .once('dtmf', this._clearTasks.bind(this, this.backgroundGatherTask)) + .once('vad', this._clearTasks.bind(this, this.backgroundGatherTask)) + .once('transcription', this._clearTasks.bind(this, this.backgroundGatherTask)) + .once('timeout', this._clearTasks.bind(this, this.backgroundGatherTask)); this.logger.info({gather}, 'CallSession:enableBotMode - starting background gather'); const resources = await this._evaluatePreconditions(this.backgroundGatherTask); const {span, ctx} = this.rootSpan.startChildSpan(`background-gather:${this.backgroundGatherTask.summary}`); @@ -999,14 +999,32 @@ class CallSession extends Emitter { } } - kill() { + kill(onBackgroundGatherBargein = false) { if (this.isConfirmCallSession) this.logger.debug('CallSession:kill (ConfirmSession)'); else this.logger.info('CallSession:kill'); if (this.currentTask) { this.currentTask.kill(this); this.currentTask = null; } - this.tasks = []; + if (onBackgroundGatherBargein) { + /* search for a config with bargein disabled */ + while (this.tasks.length) { + const t = this.tasks[0]; + if (t.name === TaskName.Config && t.bargeIn?.enable === false) { + /* found it, clear to that point and remove the disable + because we likely already received a partial transcription + and we don't want to kill the background gather before we + get the full transcription. + */ + delete t.bargeIn.enable; + this._bargeInEnabled = false; + this.logger.info('CallSession:kill - found bargein disabled in the stack, clearing to that point'); + break; + } + this.tasks.shift(); + } + } + else this.tasks = []; this.taskIdx = 0; } @@ -1614,11 +1632,12 @@ class CallSession extends Emitter { }); } - _clearTasks(evt) { - if (this.requestor instanceof WsRequestor) { + _clearTasks(backgroundGather, evt) { + if (this.requestor instanceof WsRequestor && !backgroundGather.cleared) { this.logger.info({evt}, 'CallSession:_clearTasks on event from background gather'); try { - this.kill(); + backgroundGather.cleared = true; + this.kill(true); } catch (err) {} } }