only gather if we not already did

This commit is contained in:
akirilyuk
2022-02-02 15:37:21 +01:00
parent 53cfbc1f56
commit 67d18b26ff

View File

@@ -52,6 +52,10 @@ class Cognigy extends Task {
// create a task queue so we can execute our taskss subsequently // create a task queue so we can execute our taskss subsequently
// also executing tasks whenever they come in // also executing tasks whenever they come in
this.taskQueue = queue({concurrency: 1, autostart: 1}); this.taskQueue = queue({concurrency: 1, autostart: 1});
// keep track of turns so we only do gather once per turn
this.turn = 0;
this.gatherTurn = 0;
} }
get name() { return TaskName.Cognigy; } get name() { return TaskName.Cognigy; }
@@ -177,7 +181,7 @@ class Cognigy extends Task {
} }
_makeGatherTask({textPrompt, urlPrompt, turnConfig} = {}) { _makeGatherTask({textPrompt, urlPrompt, turnConfig} = {}) {
this.logger.debug({textPrompt, urlPrompt, turnConfig}, "_makeGatherTask"); this.logger.debug({textPrompt, urlPrompt, turnConfig}, '_makeGatherTask');
const config = this.config.makeGatherTaskConfig({textPrompt, urlPrompt, turnConfig}); const config = this.config.makeGatherTaskConfig({textPrompt, urlPrompt, turnConfig});
const {retry, ...rest} = config; const {retry, ...rest} = config;
this.retry = retry; this.retry = retry;
@@ -239,12 +243,16 @@ class Cognigy extends Task {
async _onBotFinalPing(cs, ep) { async _onBotFinalPing(cs, ep) {
this.logger.info({prompts: this.prompts}, 'Cognigy:_onBotFinalPing'); this.logger.info({prompts: this.prompts}, 'Cognigy:_onBotFinalPing');
try { try {
if (!this.gatherTaskExists) { // count up the turn counter
this.turn += 1;
if (this.turn > this.gatherTurn) {
// we never have create a gather in the current turn and only played so lets listen to customer input
// after we have played everything
this.gatherTurn = this.turn;
await this.taskQueue.lastPromise; await this.taskQueue.lastPromise;
this.gatherTask = this._makeGatherTask(); this.gatherTask = this._makeGatherTask();
this.gatherTask.exec(cs, ep, this) this.gatherTask.exec(cs, ep, this)
.catch((err) => this.logger.info({err}, 'Cognigy gather task returned error')); .catch((err) => this.logger.info({err}, 'Cognigy gather task returned error'));
this.prompts = [];
} else { } else {
this.logger.info({}, 'no need to create a gather task, it already exists!'); this.logger.info({}, 'no need to create a gather task, it already exists!');
} }
@@ -273,62 +281,60 @@ class Cognigy extends Task {
} }
const text = parseBotText(evt); const text = parseBotText(evt);
if (evt.data && evt.data.config && evt.data.config.session) this.config.update(evt.data.config.session); if (evt?.data?.config?.session) this.config.update(evt.data.config.session);
// only add say task if its a normal cognigy node and not a "gather task" // only add say task if its a normal cognigy node and not a "gather task"
if (text && (!evt.data || !evt.data.type || !evt.data.type !== 'gather')) { if (text && (evt?.data?.type !== 'gather')) {
this.logger.info({text}, 'received text');
this._enqueueTask(async() => { this._enqueueTask(async() => {
this.logger.info({text}, 'received text'); // todo inject the session config into the say task
const sayTask = this._makeSayTask(text); const sayTask = this._makeSayTask(text);
await sayTask.exec(cs, ep, this); await sayTask.exec(cs, ep, this);
}); });
} }
if (evt && evt.data && evt.data.type) { try {
try { switch (evt?.data?.type) {
switch (evt.data.type) { case 'hangup':
case 'hangup': this._enqueueTask(async() => {
this._enqueueTask(async() => { this.performAction({cognigyResult: 'hangup Succeeded'});
this.performAction({cognigyResult: 'hangup Succeeded'}); this.reportedFinalAction = true;
this.reportedFinalAction = true; cs.replaceApplication([this._makeHangupTask(evt.data.reason)]);
cs.replaceApplication([this._makeHangupTask(evt.data.reason)]); this.taskQueue.end();
this.taskQueue.end(); });
});
return; return;
case 'refer': case 'refer':
this._enqueueTask(async() => { this._enqueueTask(async() => {
this.performAction({cognigyResult: 'refer succeeded'}); this.performAction({cognigyResult: 'refer succeeded'});
this.reportedFinalAction = true; this.reportedFinalAction = true;
cs.replaceApplication([this._makeReferTask(evt.data.referTo)]); cs.replaceApplication([this._makeReferTask(evt.data.referTo)]);
});
return;
case 'gather':
this.gatherTurn += 1;
this._enqueueTask(async() => {
this.gatherTask = this._makeGatherTask({
textPrompt: evt.data.text,
urlPrompt: evt.data.url,
turnConfig: evt?.data?.config?.nextTurn
}); });
return; try {
case 'gather': this.gatherTask.exec(cs, ep, this);
this.gatherTaskExists = true; } catch (err) {
this._enqueueTask(async() => { this.logger.info({err}, 'Cognigy gather task returned error');
this.gatherTask = this._makeGatherTask({ }
textPrompt: evt.data.text, });
urlPrompt: evt.data.url, return;
turnConfig: evt.data.config && evt.data.config.nextTurn default:
}); break;
try {
this.gatherTask.exec(cs, ep, this);
} catch (err) {
this.logger.info({err}, 'Cognigy gather task returned error');
}
this.gatherTaskExists = false;
});
return;
default:
break;
}
} catch (err) {
this.logger.info({err, evtData: evt.data}, 'encountered error parsing cognigy response data');
if (!this.hasReportedFinalAction) this.performAction({cognigyResult: 'error', err});
this.reportedFinalAction = true;
this.notifyTaskDone();
} }
} catch (err) {
this.logger.info({err, evtData: evt.data}, 'encountered error parsing cognigy response data');
if (!this.hasReportedFinalAction) this.performAction({cognigyResult: 'error', err});
this.reportedFinalAction = true;
this.notifyTaskDone();
} }
} }