add support for session config & cleanup

This commit is contained in:
akirilyuk
2022-02-02 20:19:38 +01:00
parent b9dfecceff
commit baed1b0eac
2 changed files with 48 additions and 48 deletions

View File

@@ -189,17 +189,10 @@ class Cognigy extends Task {
return gather;
}
_makeSayTask(text) {
const opts = {
text,
synthesizer: this.data.synthesizer ||
{
vendor: 'default',
language: 'default',
voice: 'default'
}
};
const say = makeTask(this.logger, {say: opts}, this);
_makeSayTask({ text, turnConfig } = {}) {
this.logger.debug({text, turnConfig}, '_makeSayTask');
const config = this.config.makeSayTaskConfig({text, turnConfig });
const say = makeTask(this.logger, { say: config }, this);
return say;
}
@@ -211,7 +204,8 @@ class Cognigy extends Task {
}
_makeHangupTask(reason) {
return makeTask(this.logger, {hangup: {
return makeTask(this.logger, {
hangup: {
headers: {
'X-Reason': reason
}
@@ -219,7 +213,12 @@ class Cognigy extends Task {
}
_makePlayTask(url, loop) {
return makeTask(this.logger, {
play: {
url,
loop
}
});
}
/* if we need to interrupt the currently-running say task(s), call this */
@@ -242,24 +241,15 @@ class Cognigy extends Task {
async _onBotFinalPing(cs, ep) {
this.logger.info({prompts: this.prompts}, 'Cognigy:_onBotFinalPing');
this._enqueueTask(async() => {
const gatherTask = this._makeGatherTask();
await gatherTask.exec(cs, ep, this);
try {
// 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'));
} else {
this.logger.info({}, 'no need to create a gather task, it already exists!');
}
await sayTask.exec(cs, ep, this);
} catch (err) {
this.logger.error({err}, 'Could not execute bot final ping!');
this.logger.info({err}, 'Cognigy gather task returned error');
}
});
}
async _onBotUtterance(cs, ep, evt) {
@@ -281,7 +271,6 @@ class Cognigy extends Task {
}
const text = parseBotText(evt);
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?.type !== 'gather')) {
@@ -303,7 +292,6 @@ class Cognigy extends Task {
cs.replaceApplication([this._makeHangupTask(evt.data.reason)]);
this.taskQueue.end();
});
return;
case 'refer':
this._enqueueTask(async() => {
@@ -312,21 +300,26 @@ class Cognigy extends Task {
cs.replaceApplication([this._makeReferTask(evt.data.referTo)]);
});
return;
case 'gather':
this.gatherTurn += 1;
case 'promt':
this._enqueueTask(async() => {
this.gatherTask = this._makeGatherTask({
textPrompt: evt.data.text,
urlPrompt: evt.data.url,
turnConfig: evt?.data?.config?.nextTurn
const sayTask = this._makeSayTask({
text: evt.data.text,
nextTurn: evt?.data?.config?.nextTurn
});
try {
await this.gatherTask.exec(cs, ep, this);
await sayTask.exec(cs, ep, this);
} catch (err) {
this.logger.info({err}, 'Cognigy gather task returned error');
this.logger.info({err}, 'Cognigy sayTask task returned error');
}
});
return;
case 'setSessionConfig':
// change session params in the order they come in with the say tasks
// so we are consistent with the flow logic executed within cognigy
this._enqueueTask(async() => {
if (evt?.data?.config?.session) this.config.update(evt.data.config.session);
});
return;
default:
break;
}
@@ -405,8 +398,8 @@ class Cognigy extends Task {
const {noInputRetries, noInputSpeech, noInputUrl} = this.retry;
this.logger.debug({evt, retry: this.retry}, 'Cognigy: got timeout');
if (noInputRetries && this.timeoutCount++ < noInputRetries) {
this.gatherTask = this._makeGatherTask({textPrompt: noInputSpeech, urlPrompt: noInputUrl});
this.gatherTask.exec(cs, ep, this)
const gatherTask = this._makeGatherTask({textPrompt: noInputSpeech, urlPrompt: noInputUrl});
gatherTask.exec(cs, ep, this)
.catch((err) => this.logger.info({err}, 'Cognigy gather task returned error'));
}
else {

View File

@@ -26,11 +26,18 @@ class SpeechConfig extends Emitter {
session
);
}
//this.turnConfig = nextTurn;
this.logger.debug({opts, sessionLevel: this.sessionConfig}, 'SpeechConfig updated');
}
makeGatherTaskConfig({textPrompt, urlPrompt, turnConfig} = {}) {
makeSayTaskConfig({text, turnConfig = {}} = {}) {
const synthesizer = lodash.merge({}, this.sessionConfig.synthesizer, turnConfig);
return {
text,
synthesizer
};
}
makeGatherTaskConfig({textPrompt, urlPrompt, turnConfig = {}} = {}) {
// we merge from top to bottom deeply so we wil have
// defaults from session config and then will override them via turn config
const opts = lodash.merge(