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
// also executing tasks whenever they come in
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; }
@@ -177,7 +181,7 @@ class Cognigy extends Task {
}
_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 {retry, ...rest} = config;
this.retry = retry;
@@ -239,12 +243,16 @@ class Cognigy extends Task {
async _onBotFinalPing(cs, ep) {
this.logger.info({prompts: this.prompts}, 'Cognigy:_onBotFinalPing');
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;
this.gatherTask = this._makeGatherTask();
this.gatherTask.exec(cs, ep, this)
.catch((err) => this.logger.info({err}, 'Cognigy gather task returned error'));
this.prompts = [];
this.gatherTask.exec(cs, ep, this)
.catch((err) => this.logger.info({err}, 'Cognigy gather task returned error'));
} else {
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);
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"
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.logger.info({text}, 'received text');
// todo inject the session config into the say task
const sayTask = this._makeSayTask(text);
await sayTask.exec(cs, ep, this);
});
}
if (evt && evt.data && evt.data.type) {
try {
switch (evt.data.type) {
case 'hangup':
this._enqueueTask(async() => {
this.performAction({cognigyResult: 'hangup Succeeded'});
this.reportedFinalAction = true;
cs.replaceApplication([this._makeHangupTask(evt.data.reason)]);
this.taskQueue.end();
});
try {
switch (evt?.data?.type) {
case 'hangup':
this._enqueueTask(async() => {
this.performAction({cognigyResult: 'hangup Succeeded'});
this.reportedFinalAction = true;
cs.replaceApplication([this._makeHangupTask(evt.data.reason)]);
this.taskQueue.end();
});
return;
case 'refer':
this._enqueueTask(async() => {
this.performAction({cognigyResult: 'refer succeeded'});
this.reportedFinalAction = true;
cs.replaceApplication([this._makeReferTask(evt.data.referTo)]);
return;
case 'refer':
this._enqueueTask(async() => {
this.performAction({cognigyResult: 'refer succeeded'});
this.reportedFinalAction = true;
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;
case 'gather':
this.gatherTaskExists = true;
this._enqueueTask(async() => {
this.gatherTask = this._makeGatherTask({
textPrompt: evt.data.text,
urlPrompt: evt.data.url,
turnConfig: evt.data.config && evt.data.config.nextTurn
});
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();
try {
this.gatherTask.exec(cs, ep, this);
} catch (err) {
this.logger.info({err}, 'Cognigy gather task returned error');
}
});
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();
}
}