Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

solution for one instance #27

Open
wants to merge 1 commit into
base: dev-amitu
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 26 additions & 6 deletions lib/GraphDialog.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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);
Expand All @@ -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, [
Expand All @@ -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) {
Expand All @@ -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:
Expand All @@ -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)
Expand All @@ -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;
Expand All @@ -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();
Expand All @@ -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);
}
Expand Down
37 changes: 30 additions & 7 deletions src/GraphDialog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ export interface IGraphDialog {
export class GraphDialog implements IGraphDialog {

private nav: nav.Navigator;
private navKey: string;
private navMap: Map<nav.Navigator> = new Map<nav.Navigator>();

private intentScorer: i.IIntentScorer;
private done: () => any;
private customTypeHandlers: Map<a.ICustomNodeTypeHandler>;
Expand All @@ -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();

Expand All @@ -60,16 +63,26 @@ export class GraphDialog implements IGraphDialog {
}

public init(): Promise<IGraphDialog> {
return new Promise((resolve, reject) => {
return new Promise<IGraphDialog>((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<void> {
return new Promise<void>((resolve, reject) => {
return this.init()
.then(() => resolve())
.catch(err => reject(err));
});
}

public static fromScenario(options: IGraphDialogOptions = {}): Promise<IGraphDialog> {
let gd = new GraphDialog(options);
return gd.init();
Expand All @@ -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, [
Expand Down Expand Up @@ -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) {
Expand All @@ -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}`);
Expand All @@ -158,7 +181,7 @@ export class GraphDialog implements IGraphDialog {

case NodeType.handler:
var handlerName = currentNode.data.name;
let handler: IHandler = <IHandler>this.nav.handlers.get(handlerName);
let handler: IHandler = <IHandler>this.getNavigator(session).handlers.get(handlerName);
console.log('calling handler: ', currentNode.id, handlerName);
handler(session, next, currentNode.data);
break;
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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}`);
Expand Down