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; return gather;
} }
_makeSayTask(text) { _makeSayTask({ text, turnConfig } = {}) {
const opts = { this.logger.debug({text, turnConfig}, '_makeSayTask');
text, const config = this.config.makeSayTaskConfig({text, turnConfig });
synthesizer: this.data.synthesizer || const say = makeTask(this.logger, { say: config }, this);
{
vendor: 'default',
language: 'default',
voice: 'default'
}
};
const say = makeTask(this.logger, {say: opts}, this);
return say; return say;
} }
@@ -211,7 +204,8 @@ class Cognigy extends Task {
} }
_makeHangupTask(reason) { _makeHangupTask(reason) {
return makeTask(this.logger, {hangup: { return makeTask(this.logger, {
hangup: {
headers: { headers: {
'X-Reason': reason 'X-Reason': reason
} }
@@ -219,7 +213,12 @@ class Cognigy extends Task {
} }
_makePlayTask(url, loop) { _makePlayTask(url, loop) {
return makeTask(this.logger, {
play: {
url,
loop
}
});
} }
/* if we need to interrupt the currently-running say task(s), call this */ /* 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) { async _onBotFinalPing(cs, ep) {
this.logger.info({prompts: this.prompts}, 'Cognigy:_onBotFinalPing'); this.logger.info({prompts: this.prompts}, 'Cognigy:_onBotFinalPing');
this._enqueueTask(async() => {
const gatherTask = this._makeGatherTask();
await gatherTask.exec(cs, ep, this);
try { try {
// count up the turn counter await sayTask.exec(cs, ep, this);
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!');
}
} catch (err) { } 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) { async _onBotUtterance(cs, ep, evt) {
@@ -281,7 +271,6 @@ class Cognigy extends Task {
} }
const text = parseBotText(evt); 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" // only add say task if its a normal cognigy node and not a "gather task"
if (text && (evt?.data?.type !== 'gather')) { if (text && (evt?.data?.type !== 'gather')) {
@@ -303,7 +292,6 @@ class Cognigy extends Task {
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() => {
@@ -312,21 +300,26 @@ class Cognigy extends Task {
cs.replaceApplication([this._makeReferTask(evt.data.referTo)]); cs.replaceApplication([this._makeReferTask(evt.data.referTo)]);
}); });
return; return;
case 'gather': case 'promt':
this.gatherTurn += 1;
this._enqueueTask(async() => { this._enqueueTask(async() => {
this.gatherTask = this._makeGatherTask({ const sayTask = this._makeSayTask({
textPrompt: evt.data.text, text: evt.data.text,
urlPrompt: evt.data.url, nextTurn: evt?.data?.config?.nextTurn
turnConfig: evt?.data?.config?.nextTurn
}); });
try { try {
await this.gatherTask.exec(cs, ep, this); await sayTask.exec(cs, ep, this);
} catch (err) { } catch (err) {
this.logger.info({err}, 'Cognigy gather task returned error'); this.logger.info({err}, 'Cognigy sayTask task returned error');
} }
}); });
return; 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: default:
break; break;
} }
@@ -405,8 +398,8 @@ class Cognigy extends Task {
const {noInputRetries, noInputSpeech, noInputUrl} = this.retry; const {noInputRetries, noInputSpeech, noInputUrl} = this.retry;
this.logger.debug({evt, retry: this.retry}, 'Cognigy: got timeout'); this.logger.debug({evt, retry: this.retry}, 'Cognigy: got timeout');
if (noInputRetries && this.timeoutCount++ < noInputRetries) { if (noInputRetries && this.timeoutCount++ < noInputRetries) {
this.gatherTask = this._makeGatherTask({textPrompt: noInputSpeech, urlPrompt: noInputUrl}); const gatherTask = this._makeGatherTask({textPrompt: noInputSpeech, urlPrompt: noInputUrl});
this.gatherTask.exec(cs, ep, 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'));
} }
else { else {

View File

@@ -26,11 +26,18 @@ class SpeechConfig extends Emitter {
session session
); );
} }
//this.turnConfig = nextTurn;
this.logger.debug({opts, sessionLevel: this.sessionConfig}, 'SpeechConfig updated'); 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 // we merge from top to bottom deeply so we wil have
// defaults from session config and then will override them via turn config // defaults from session config and then will override them via turn config
const opts = lodash.merge( const opts = lodash.merge(