Skip to content

Commit

Permalink
refactor: The dialogWillComplete hook can no longer return null
Browse files Browse the repository at this point in the history
BREAKING CHANGE: The dialogWillComplete hook can no longer return null.
It should instead return a dialog action such as this.complete().
  • Loading branch information
yangeorget committed Jun 27, 2018
1 parent 34c3810 commit 7a5a8b8
Show file tree
Hide file tree
Showing 14 changed files with 97 additions and 346 deletions.
2 changes: 1 addition & 1 deletion packages/botfuel-dialog/src/dialogs/base-dialog.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class BaseDialog extends Dialog {
const extraData = await this.dialogWillDisplay(userMessage, data);
data = this.mergeData(extraData, data);
const botMessages = await this.display(userMessage, data);
const action = (await this.dialogWillComplete(userMessage, data)) || this.complete();
const action = await this.dialogWillComplete(userMessage, data);
return {
action,
botMessages,
Expand Down
18 changes: 11 additions & 7 deletions packages/botfuel-dialog/src/dialogs/confirmation-dialog.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,18 @@ const PromptDialog = require('./prompt-dialog');
*/
class ConfirmationDialog extends PromptDialog {
/** @inheritDoc */
async dialogWillComplete(userMessage, { matchedEntities }) {
logger.debug('dialogWillComplete', userMessage, { matchedEntities });
// Clean entities for this dialog so it can be reused later
await this.brain.conversationSet(userMessage.user, this.parameters.namespace, {});
if (matchedEntities.answer.values[0].value) {
return this.complete();
async dialogWillComplete(userMessage, data) {
logger.debug('dialogWillComplete', userMessage, data);
const answer = data.matchedEntities.answer;
if (answer) {
// Clean entities for this dialog so it can be reused later
await this.brain.conversationSet(userMessage.user, this.parameters.namespace, {});
if (answer.values[0].value === true) {
return this.complete();
}
return this.cancelPrevious();
}
return this.cancelPrevious();
return this.wait();
}
}

Expand Down
4 changes: 1 addition & 3 deletions packages/botfuel-dialog/src/dialogs/dialog.js
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,6 @@ class Dialog {
if (!extraData) {
return data;
}

// if extraData is an object
if (typeof extraData === 'object' && !Array.isArray(extraData)) {
if (data) {
Expand All @@ -270,10 +269,8 @@ class Dialog {
);
}
}

return { ...data, ...extraData };
}

// if extraData is a value
return { ...data, extraData };
}
Expand All @@ -286,6 +283,7 @@ class Dialog {
*/
async dialogWillComplete(userMessage: UserMessage, data: {}): Promise<any> {
logger.debug('dialogWillComplete', userMessage, data);
return this.complete();
}
}

Expand Down
26 changes: 11 additions & 15 deletions packages/botfuel-dialog/src/dialogs/prompt-dialog.js
Original file line number Diff line number Diff line change
Expand Up @@ -285,16 +285,13 @@ class PromptDialog extends Dialog {
*/
async execute(userMessage, data) {
logger.debug('execute', userMessage, data);

// get message entities extracted from the message
const { messageEntities } = data;
const userId = userMessage.user;

const dialogCache = await this.brain.conversationGet(userId, this.parameters.namespace);
const previouslyMatchedEntities = (dialogCache && dialogCache._entities) || {};
const previousQuestionEntity = (dialogCache && dialogCache._question) || undefined;
logger.debug('execute: previouslyMatchedEntities', previouslyMatchedEntities);

// Get missing entities and matched entities
const { missingEntities, matchedEntities } = await this.computeEntities(
messageEntities,
Expand All @@ -303,31 +300,30 @@ class PromptDialog extends Dialog {
previousQuestionEntity,
);
logger.debug('execute', { missingEntities, matchedEntities });

// save matched entities and next question in the brain
await this.brain.conversationSet(userId, this.parameters.namespace, {
_entities: matchedEntities,
_question: missingEntities.size > 0 ? missingEntities.keys().next().value : undefined,
});

data = { ...data, missingEntities, matchedEntities };
const extraData = await this.dialogWillDisplay(userMessage, data);
data = this.mergeData(extraData, data);

const botMessages = await this.display(userMessage, data);

if (missingEntities.size === 0) {
const action = (await this.dialogWillComplete(userMessage, data)) || this.complete();
return {
action,
botMessages,
};
}
const action = await this.dialogWillComplete(userMessage, data);
return {
action: this.wait(),
action,
botMessages,
};
}

/** @inheritDoc */
async dialogWillComplete(userMessage, data) {
logger.debug('dialogWillComplete', userMessage, data);
if (data.missingEntities.size === 0) {
return this.complete();
}
return this.wait();
}
}

module.exports = PromptDialog;
2 changes: 1 addition & 1 deletion packages/botfuel-dialog/src/dialogs/void-dialog.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class VoidDialog extends Dialog {
/** @inheritDoc */
async execute(userMessage, data) {
logger.debug('execute', userMessage, data);
const action = (await this.dialogWillComplete(userMessage, data)) || this.complete();
const action = await this.dialogWillComplete(userMessage, data);
return {
action,
botMessages: [],
Expand Down
1 change: 0 additions & 1 deletion packages/test-complexdialogs/shell-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,4 @@

module.exports = {
logger: 'info',
multiIntent: true,
};
8 changes: 6 additions & 2 deletions packages/test-complexdialogs/src/dialogs/alcohol-dialog.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,12 @@
const { PromptDialog } = require('botfuel-dialog');

class AlcoholDialog extends PromptDialog {
async dialogWillComplete(userMessage) {
await this.brain.userSet(userMessage.user, 'isAlcoholDialogCompleted', true);
async dialogWillComplete(userMessage, data) {
if (data.missingEntities.size === 0) {
await this.brain.userSet(userMessage.user, 'isAlcoholDialogCompleted', true);
return this.complete();
}
return this.wait();
}
}

Expand Down
27 changes: 13 additions & 14 deletions packages/test-complexdialogs/src/dialogs/cancel-dialog.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,30 +14,29 @@
* limitations under the License.
*/

const { ConfirmationDialog } = require('botfuel-dialog');
const { PromptDialog } = require('botfuel-dialog');

class Cancel extends ConfirmationDialog {
async dialogWillComplete(userMessage, { matchedEntities }) {
const answer = matchedEntities.boolean.values[0].value;

// Clean entities for this dialog so it can be reused later
await this.brain.conversationSet(userMessage.user, this.parameters.namespace, {});

if (answer) {
class CancelDialog extends PromptDialog {
async dialogWillComplete(userMessage, data) {
if (data.missingEntities.size === 0) {
// Clean entities for this dialog so it can be reused later
await this.brain.conversationSet(userMessage.user, this.parameters.namespace, {});
if (data.matchedEntities.answer.values[0].value === false) {
return this.complete();
}
return this.cancelPrevious('greetings');
}

return this.complete();
return this.wait();
}
}

Cancel.params = {
CancelDialog.params = {
namespace: 'cancel',
entities: {
boolean: {
answer: {
dim: 'system:boolean',
},
},
};

module.exports = Cancel;
module.exports = CancelDialog;
7 changes: 5 additions & 2 deletions packages/test-complexdialogs/src/dialogs/car-dialog.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,11 @@
const { PromptDialog } = require('botfuel-dialog');

class CarDialog extends PromptDialog {
async dialogWillComplete() {
return this.triggerNext('thanks');
async dialogWillComplete(userMessage, data) {
if (data.missingEntities.size === 0) {
return this.triggerNext('thanks');
}
return this.wait();
}
}

Expand Down
8 changes: 6 additions & 2 deletions packages/test-complexdialogs/src/dialogs/question-dialog.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,12 @@
const { PromptDialog } = require('botfuel-dialog');

class QuestionDialog extends PromptDialog {
async dialogWillComplete(userMessage) {
await this.brain.userSet(userMessage.user, 'isQuestionDialogCompleted', true);
async dialogWillComplete(userMessage, data) {
if (data.missingEntities.size === 0) {
await this.brain.userSet(userMessage.user, 'isQuestionDialogCompleted', true);
return this.complete();
}
return this.wait();
}
}

Expand Down
2 changes: 1 addition & 1 deletion packages/test-complexdialogs/src/views/cancel-view.en.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const { PromptView, BotTextMessage } = require('botfuel-dialog');

class CancelView extends PromptView {
render(userMessage, { matchedEntities }) {
const answer = matchedEntities.boolean && matchedEntities.boolean.values[0].value;
const answer = matchedEntities.answer && matchedEntities.answer.values[0].value;

if (answer === true) {
return [new BotTextMessage('Dialog canceled!')];
Expand Down
1 change: 0 additions & 1 deletion packages/test-complexdialogs/test-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,5 @@ module.exports = {
name: 'test',
},
logger: 'error',
multiIntent: true,
path: __dirname,
};
1 change: 1 addition & 0 deletions packages/test-middlewares/src/dialogs/mute-dialog.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const { BaseDialog } = require('botfuel-dialog');
class Mute extends BaseDialog {
dialogWillComplete(userMessage) {
this.brain.userSet(userMessage.user, '_isMuted', true);
return this.complete();
}
}

Expand Down
Loading

0 comments on commit 7a5a8b8

Please sign in to comment.