From a596abb16431b52f67bcbfcd6b9c41db32fe7475 Mon Sep 17 00:00:00 2001 From: amitu Date: Wed, 19 Oct 2016 15:48:34 -0700 Subject: [PATCH] solution for one instance --- lib/GraphDialog.js | 32 ++++++++++++++++++++++++++------ src/GraphDialog.ts | 37 ++++++++++++++++++++++++++++++------- 2 files changed, 56 insertions(+), 13 deletions(-) diff --git a/lib/GraphDialog.js b/lib/GraphDialog.js index 93c7a04..45f9a71 100644 --- a/lib/GraphDialog.js +++ b/lib/GraphDialog.js @@ -13,6 +13,7 @@ var GraphDialog = (function () { function GraphDialog(options) { if (options === void 0) { options = {}; } this.options = options; + this.navMap = new Common_1.Map(); this.loopDialogName = '__internalLoop'; if (!options.bot) throw new Error('please provide the bot object'); @@ -33,10 +34,20 @@ var GraphDialog = (function () { parser.init().then(function () { console.log('parser is ready'); _this.nav = new nav.Navigator(parser); + _this.navKey = uuid.v4(); + _this.navMap.add(_this.navKey, _this.nav); return resolve(_this); }).catch(function (e) { return reject(e); }); }); }; + GraphDialog.prototype.reload = function () { + var _this = this; + return new Promise(function (resolve, reject) { + return _this.init() + .then(function () { return resolve(); }) + .catch(function (err) { return reject(err); }); + }); + }; GraphDialog.fromScenario = function (options) { if (options === void 0) { options = {}; } var gd = new GraphDialog(options); @@ -47,9 +58,17 @@ var GraphDialog = (function () { console.log('get dialog'); return function (session, results, next) { console.log('calling loop function for the first time'); - session.replaceDialog('/' + _this.loopDialogName); + session.dialogData._navId = _this.navKey; + session.dialogData._dialogDataFlag = true; + session.replaceDialog('/' + _this.loopDialogName, session.dialogData); }; }; + GraphDialog.prototype.getNavigator = function (session) { + var navId = session.dialogData._navId; + if (navId && this.navMap.has(navId)) + return this.navMap.get(navId); + return this.nav; + }; GraphDialog.prototype.setBotDialog = function () { var _this = this; this.options.bot.dialog('/' + this.loopDialogName, [ @@ -61,6 +80,7 @@ var GraphDialog = (function () { delete obj['BotBuilder.Data.WaterfallStep']; extend(true, session.dialogData, obj); } + return _this.stepInteractionHandler(session, results, next); }, function (session, results, next) { @@ -79,7 +99,7 @@ var GraphDialog = (function () { GraphDialog.prototype.stepInteractionHandler = function (session, results, next) { var _this = this; session.dialogData._lastMessage = session.message && session.message.text; - var currentNode = this.nav.getCurrentNode(session); + var currentNode = this.getNavigator(session).getCurrentNode(session); console.log("perform action: " + currentNode.id + ", " + currentNode.type); switch (currentNode.type) { case NodeType.text: @@ -95,7 +115,7 @@ var GraphDialog = (function () { }); break; case NodeType.score: - var botModels = currentNode.data.models.map(function (model) { return _this.nav.models.get(model); }); + var botModels = currentNode.data.models.map(function (model) { return _this.getNavigator(session).models.get(model); }); var text = session.dialogData[currentNode.data.source] || session.dialogData._lastMessage; console.log("LUIS scoring for node: " + currentNode.id + ", text: '" + text + "' LUIS models: " + botModels); this.intentScorer.collectIntents(botModels, text, currentNode.data.threashold) @@ -109,7 +129,7 @@ var GraphDialog = (function () { break; case NodeType.handler: var handlerName = currentNode.data.name; - var handler = this.nav.handlers.get(handlerName); + var handler = this.getNavigator(session).handlers.get(handlerName); console.log('calling handler: ', currentNode.id, handlerName); handler(session, next, currentNode.data); break; @@ -134,7 +154,7 @@ var GraphDialog = (function () { } }; GraphDialog.prototype.stepResultCollectionHandler = function (session, results, next) { - var currentNode = this.nav.getCurrentNode(session); + var currentNode = this.getNavigator(session).getCurrentNode(session); var varname = currentNode.varname; if (!(results.response && varname)) return next(); @@ -158,7 +178,7 @@ var GraphDialog = (function () { return next(); }; GraphDialog.prototype.setNextStepHandler = function (session, args, next) { - var nextNode = this.nav.getNextNode(session); + var nextNode = this.getNavigator(session).getNextNode(session); if (nextNode) { console.log("step handler node: " + nextNode.id); } diff --git a/src/GraphDialog.ts b/src/GraphDialog.ts index ff72970..2d4357d 100644 --- a/src/GraphDialog.ts +++ b/src/GraphDialog.ts @@ -37,6 +37,9 @@ export interface IGraphDialog { export class GraphDialog implements IGraphDialog { private nav: nav.Navigator; + private navKey: string; + private navMap: Map = new Map(); + private intentScorer: i.IIntentScorer; private done: () => any; private customTypeHandlers: Map; @@ -45,7 +48,7 @@ export class GraphDialog implements IGraphDialog { constructor(private options: IGraphDialogOptions = {}) { if (!options.bot) throw new Error('please provide the bot object'); - // TODO add GUID + this.loopDialogName += options.scenario + uuid.v4(); this.setBotDialog(); @@ -60,16 +63,26 @@ export class GraphDialog implements IGraphDialog { } public init(): Promise { - return new Promise((resolve, reject) => { + return new Promise((resolve, reject) => { let parser = new Parser(this.options); parser.init().then(() => { console.log('parser is ready'); this.nav = new nav.Navigator(parser); + this.navKey = uuid.v4(); + this.navMap.add(this.navKey, this.nav); return resolve(this); }).catch(e => reject(e)); }); } + public reload(): Promise { + return new Promise((resolve, reject) => { + return this.init() + .then(() => resolve()) + .catch(err => reject(err)); + }); + } + public static fromScenario(options: IGraphDialogOptions = {}): Promise { let gd = new GraphDialog(options); return gd.init(); @@ -79,10 +92,20 @@ export class GraphDialog implements IGraphDialog { console.log('get dialog'); return (session: builder.Session, results, next) => { console.log('calling loop function for the first time'); + + // assign the navigator Id for the user + session.dialogData._navId = this.navKey; session.replaceDialog('/' + this.loopDialogName); }; } + private getNavigator(session: builder.Session): nav.Navigator { + var navId = session.dialogData._navId; + if (navId && this.navMap.has(navId)) + return this.navMap.get(navId); + return this.nav; + } + private setBotDialog(): void { this.options.bot.dialog('/' + this.loopDialogName, [ @@ -114,7 +137,7 @@ export class GraphDialog implements IGraphDialog { // TODO: add option for 'bot is typeing' message before sending the answer private stepInteractionHandler(session: builder.Session, results, next): void { session.dialogData._lastMessage = session.message && session.message.text; - let currentNode = this.nav.getCurrentNode(session); + let currentNode = this.getNavigator(session).getCurrentNode(session); console.log(`perform action: ${currentNode.id}, ${currentNode.type}`); switch (currentNode.type) { @@ -138,7 +161,7 @@ export class GraphDialog implements IGraphDialog { break; case NodeType.score: - var botModels = currentNode.data.models.map(model => this.nav.models.get(model)); + var botModels = currentNode.data.models.map(model => this.getNavigator(session).models.get(model)); var text = session.dialogData[currentNode.data.source] || session.dialogData._lastMessage; console.log(`LUIS scoring for node: ${currentNode.id}, text: \'${text}\' LUIS models: ${botModels}`); @@ -158,7 +181,7 @@ export class GraphDialog implements IGraphDialog { case NodeType.handler: var handlerName = currentNode.data.name; - let handler: IHandler = this.nav.handlers.get(handlerName); + let handler: IHandler = this.getNavigator(session).handlers.get(handlerName); console.log('calling handler: ', currentNode.id, handlerName); handler(session, next, currentNode.data); break; @@ -189,7 +212,7 @@ export class GraphDialog implements IGraphDialog { } private stepResultCollectionHandler(session: builder.Session, results, next) { - let currentNode = this.nav.getCurrentNode(session); + let currentNode = this.getNavigator(session).getCurrentNode(session); let varname = currentNode.varname; if (!(results.response && varname)) @@ -219,7 +242,7 @@ export class GraphDialog implements IGraphDialog { } private setNextStepHandler(session: builder.Session, args, next): any { - let nextNode = this.nav.getNextNode(session); + let nextNode = this.getNavigator(session).getNextNode(session); if (nextNode) { console.log(`step handler node: ${nextNode.id}`);