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,21 +281,21 @@ 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._enqueueTask(async() => {
this.logger.info({text}, 'received text'); this.logger.info({text}, 'received text');
this._enqueueTask(async() => {
// 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'});
@@ -305,19 +313,18 @@ class Cognigy extends Task {
}); });
return; return;
case 'gather': case 'gather':
this.gatherTaskExists = true; this.gatherTurn += 1;
this._enqueueTask(async() => { this._enqueueTask(async() => {
this.gatherTask = this._makeGatherTask({ this.gatherTask = this._makeGatherTask({
textPrompt: evt.data.text, textPrompt: evt.data.text,
urlPrompt: evt.data.url, urlPrompt: evt.data.url,
turnConfig: evt.data.config && evt.data.config.nextTurn turnConfig: evt?.data?.config?.nextTurn
}); });
try { try {
this.gatherTask.exec(cs, ep, this); this.gatherTask.exec(cs, ep, this);
} catch (err) { } catch (err) {
this.logger.info({err}, 'Cognigy gather task returned error'); this.logger.info({err}, 'Cognigy gather task returned error');
} }
this.gatherTaskExists = false;
}); });
return; return;
default: default:
@@ -330,7 +337,6 @@ class Cognigy extends Task {
this.notifyTaskDone(); this.notifyTaskDone();
} }
} }
}
async _onTranscription(cs, ep, evt) { async _onTranscription(cs, ep, evt) {
this.logger.debug({evt}, `Cognigy: got transcription for callSid ${cs.callSid}`); this.logger.debug({evt}, `Cognigy: got transcription for callSid ${cs.callSid}`);