mirror of
https://github.com/jambonz/jambonz-feature-server.git
synced 2025-12-21 17:17:58 +00:00
only gather if we not already did
This commit is contained in:
@@ -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 = [];
|
||||
} else {
|
||||
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);
|
||||
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')) {
|
||||
this._enqueueTask(async() => {
|
||||
if (text && (evt?.data?.type !== 'gather')) {
|
||||
this.logger.info({text}, 'received text');
|
||||
this._enqueueTask(async() => {
|
||||
// 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) {
|
||||
switch (evt?.data?.type) {
|
||||
case 'hangup':
|
||||
this._enqueueTask(async() => {
|
||||
this.performAction({cognigyResult: 'hangup Succeeded'});
|
||||
@@ -305,19 +313,18 @@ class Cognigy extends Task {
|
||||
});
|
||||
return;
|
||||
case 'gather':
|
||||
this.gatherTaskExists = true;
|
||||
this.gatherTurn += 1;
|
||||
this._enqueueTask(async() => {
|
||||
this.gatherTask = this._makeGatherTask({
|
||||
textPrompt: evt.data.text,
|
||||
urlPrompt: evt.data.url,
|
||||
turnConfig: evt.data.config && evt.data.config.nextTurn
|
||||
turnConfig: 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:
|
||||
@@ -330,7 +337,6 @@ class Cognigy extends Task {
|
||||
this.notifyTaskDone();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async _onTranscription(cs, ep, evt) {
|
||||
this.logger.debug({evt}, `Cognigy: got transcription for callSid ${cs.callSid}`);
|
||||
|
||||
Reference in New Issue
Block a user