diff --git a/lib/tasks/cognigy/index.js b/lib/tasks/cognigy/index.js index ca88d443..14a4fbbb 100644 --- a/lib/tasks/cognigy/index.js +++ b/lib/tasks/cognigy/index.js @@ -51,7 +51,9 @@ class Cognigy extends Task { this.timeoutCount = 0; // 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}); + this.changeConfigTasks = []; // keep track of turns so we only do gather once per turn this.turn = 0; @@ -77,7 +79,13 @@ class Cognigy extends Task { this.taskQueue.push(async(cb) => { this.logger.debug('executing task from queue'); try { + const result = await boundTask(); + // if this is a config task, remove it from the config task storage, + // as we have now executed it + if(task.isConfigTask){ + this.changeConfigTasks.shift(); + } resolver(result); cb(result); } catch (err) { @@ -87,6 +95,11 @@ class Cognigy extends Task { } this.logger.debug('say task executed from queue'); }); + + // if this is a config task, lets also push the config + if(task.isConfigTask){ + this.changeConfigTasks.push(task); + } if (this.taskQueue.lastPromise) { // resolve the previous promise for cleanup this.taskQueue.lastPromise.resolve({}); @@ -344,9 +357,11 @@ class Cognigy extends Task { 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() => { + const updateConfigTask = () => { if (evt?.data?.config?.session) this.config.update(evt.data.config.session); - }); + }; + updateConfigTask.isConfigTask = true; + this._enqueueTask(updateConfigTask); return; default: break; @@ -371,6 +386,11 @@ class Cognigy extends Task { // clear task queue, resolve the last promise and cleanup; this.taskQueue.lastPromise.resolve(); this.taskQueue.end(); + while(this.changeConfigTasks.length > 0){ + // apply all the config tasks FIFO + const changeConfigTask = this.changeConfigTasks.shift(); + changeConfigTask(); + } this.taskQueue.autostart = true; }