From e952f425818db8e22ebe2fbc642fa49516f70fd7 Mon Sep 17 00:00:00 2001 From: Offer Akrabi Date: Tue, 23 Oct 2018 10:44:17 +0300 Subject: [PATCH 01/31] added routeByEntities flag --- lib/evaluation-response.js | 1 + lib/handler.js | 40 ++++++++++++++++++++++++++++---------- 2 files changed, 31 insertions(+), 10 deletions(-) diff --git a/lib/evaluation-response.js b/lib/evaluation-response.js index 6edbb14..f0e3e79 100644 --- a/lib/evaluation-response.js +++ b/lib/evaluation-response.js @@ -7,6 +7,7 @@ function EvaluationResponse(callback) { requestResult: {}, intentities: [], handleUtterance: true, + routeByEntities: true, context: { }, internal:{ diff --git a/lib/handler.js b/lib/handler.js index 66d4dc0..47d1b29 100644 --- a/lib/handler.js +++ b/lib/handler.js @@ -59,6 +59,10 @@ Handler.prototype.handleEvaluationRequest = function (request, callback) { saveContext(this, request, context, evaluationResponse); callback(err, evaluationResponse.response); }); + // set the routingByEntities flag to the value defined in the manifest + if(this.manifest && this.manifest.routeByEntities) { + evaluationResponse.response.routeByEntities = this.manifest.routeByEntities; + } let context = createContext(request); let state = this.state || 'DEFAULT'; do { @@ -101,22 +105,15 @@ Handler.prototype.evaluateRequest = function (request, evaluationResponse, conte evaluationResponse.response.intentities.push(intentity); } }); - let textResponse; - if(output) { - context.skill = output.context ? output.context : {}; - if(output.actions) { - for(let action of output.actions) { - evaluationResponse.response.actions.push(action) - } - } - textResponse = output.text ? output.text : undefined; - } + let textResponse = output.text; + copyFromNluResponse(context, evaluationResponse, output); callback(textResponse, evaluationResponse, context, undefined); } }); }; + Handler.prototype.handleRequest = function (request, callback) { logger.info('Request', JSON.stringify(request)); // State and session context for short access @@ -391,4 +388,27 @@ let setupWCSCredentialsFromEnv = function (self) { "username": process.env.WCS_USERNAME } } +}; + +/** + * copies data from the Nlu evaluation response to the evaluation response and context + * @param context + * @param evaluationResponse + * @param NluResponse + */ +let copyFromNluResponse = function(context, evaluationResponse, NluResponse) { + if(NluResponse) { + if(output.context) { + context.skill = NluResponse.context || {}; + //set handleUtterance flag from NLU context + evaluationResponse.handleUtterance = NluResponse.context.handleUtterance || evaluationResponse.response.handleUtterance; + //set routeByEntities flag from NLU context + evaluationResponse.routeByEntities = NluResponse.context.routeByEntities || evaluationResponse.response.routeByEntities; + } + if(NluResponse.actions) { + for(let action of NluResponse.actions) { + evaluationResponse.response.actions.push(action) + } + } + } }; \ No newline at end of file From cd1210045206d416e424ecaa6b9a98c3e8aee334 Mon Sep 17 00:00:00 2001 From: Offer Akrabi Date: Tue, 23 Oct 2018 12:27:26 +0300 Subject: [PATCH 02/31] added routeByEntities flag --- lib/evaluation-response.js | 4 ++++ lib/handler.js | 6 +++--- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/lib/evaluation-response.js b/lib/evaluation-response.js index f0e3e79..f7a425b 100644 --- a/lib/evaluation-response.js +++ b/lib/evaluation-response.js @@ -27,4 +27,8 @@ EvaluationResponse.prototype.rejectUtterance = function() { return this; }; +EvaluationResponse.prototype.setRoutingByEntities = function(value) { + this.response.routeByEntities = value; + return this; +}; module.exports = EvaluationResponse; diff --git a/lib/handler.js b/lib/handler.js index 47d1b29..2534f16 100644 --- a/lib/handler.js +++ b/lib/handler.js @@ -60,8 +60,8 @@ Handler.prototype.handleEvaluationRequest = function (request, callback) { callback(err, evaluationResponse.response); }); // set the routingByEntities flag to the value defined in the manifest - if(this.manifest && this.manifest.routeByEntities) { - evaluationResponse.response.routeByEntities = this.manifest.routeByEntities; + if(this.manifest && this.manifest.routeByEntities !== undefined) { + evaluationResponse.setRoutingByEntities(this.manifest.routeByEntities); } let context = createContext(request); let state = this.state || 'DEFAULT'; @@ -105,7 +105,7 @@ Handler.prototype.evaluateRequest = function (request, evaluationResponse, conte evaluationResponse.response.intentities.push(intentity); } }); - let textResponse = output.text; + let textResponse = output && output.text ? output.text : undefined; copyFromNluResponse(context, evaluationResponse, output); callback(textResponse, evaluationResponse, context, undefined); From c631c4c5c2960e4891878e6d28fca89a3efcf835 Mon Sep 17 00:00:00 2001 From: Offer Akrabi Date: Wed, 24 Oct 2018 10:14:45 +0300 Subject: [PATCH 03/31] added validiy check for setRoutingByEntities --- lib/evaluation-response.js | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/lib/evaluation-response.js b/lib/evaluation-response.js index f7a425b..918df8f 100644 --- a/lib/evaluation-response.js +++ b/lib/evaluation-response.js @@ -28,7 +28,13 @@ EvaluationResponse.prototype.rejectUtterance = function() { }; EvaluationResponse.prototype.setRoutingByEntities = function(value) { - this.response.routeByEntities = value; + if(typeof(value) !== 'boolean') { + console.warn('Could not set RouteByEntities flag, given value was not a boolean'); + throw('Could not set RouteByEntities flag, given value was not a boolean'); + } else { + this.response.routeByEntities = value; + } return this; }; + module.exports = EvaluationResponse; From 026496465b2dae3a17a12ba4547911bdbe02d5ca Mon Sep 17 00:00:00 2001 From: Offer Akrabi Date: Sun, 28 Oct 2018 18:49:23 +0200 Subject: [PATCH 04/31] removed exception --- lib/evaluation-response.js | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/evaluation-response.js b/lib/evaluation-response.js index 918df8f..42f9911 100644 --- a/lib/evaluation-response.js +++ b/lib/evaluation-response.js @@ -30,7 +30,6 @@ EvaluationResponse.prototype.rejectUtterance = function() { EvaluationResponse.prototype.setRoutingByEntities = function(value) { if(typeof(value) !== 'boolean') { console.warn('Could not set RouteByEntities flag, given value was not a boolean'); - throw('Could not set RouteByEntities flag, given value was not a boolean'); } else { this.response.routeByEntities = value; } From a1ac9d7beab8f4372a54fc119d279bf3f9e167d9 Mon Sep 17 00:00:00 2001 From: Offer Akrabi Date: Wed, 31 Oct 2018 14:36:06 +0200 Subject: [PATCH 05/31] updated to work with apikey --- lib/handler.js | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/lib/handler.js b/lib/handler.js index 2534f16..ef3f981 100644 --- a/lib/handler.js +++ b/lib/handler.js @@ -39,7 +39,7 @@ Handler.prototype.initialize = function () { } if (this.wcsCredentials) { let credentials = this.wcsCredentials.credentials; - setupWcs(this, credentials.url, credentials.username, credentials.password, credentials.version_date) + setupWcs(this, credentials.url, credentials.username, credentials.password, credentials.version_date, credentials.api_key) } }; @@ -257,17 +257,20 @@ let getIntentity = function (self, request, cb) { * sets up your WCS credentials, these will be used to access your * WCS service * + * @param self * @param wcsUrl - your wcs url, for US: https://gateway.watsonplatform.net/conversation/api * for Germany: https://gateway-fra.watsonplatform.net/conversation/api * @param wcsUsername - your wcs username * @param wcsPassword - your wcs password * @param versionDate - your wcs version date + * @param apiKey */ -let setupWcs = function (self, wcsUrl, wcsUsername, wcsPassword, versionDate) { +let setupWcs = function (self, wcsUrl, wcsUsername, wcsPassword, versionDate, apiKey) { try { self.conversation = Promise.promisifyAll( new Conversation({ url: wcsUrl, + iam_apikey: apiKey, username: wcsUsername, password: wcsPassword, version_date: versionDate From a1524ca9b0e6f05a76b49f4dc461f56276b8ad63 Mon Sep 17 00:00:00 2001 From: Offer Akrabi Date: Wed, 31 Oct 2018 15:06:58 +0200 Subject: [PATCH 06/31] changed conversation to assistant --- lib/handler.js | 2 +- package.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/handler.js b/lib/handler.js index ef3f981..d4875bc 100644 --- a/lib/handler.js +++ b/lib/handler.js @@ -10,7 +10,7 @@ const sprintf = require('i18next-sprintf-postprocessor'); const Response = require('./response'); const EvaluationResponse = require('./evaluation-response'); const logger = require('./logger'); -const Conversation = require('watson-developer-cloud/conversation/v1'); +const Conversation = require('watson-developer-cloud/assistant/v1'); const Promise = require('bluebird'); const fs = require('fs'); const path = require('path'); diff --git a/package.json b/package.json index b39d774..0051ea3 100644 --- a/package.json +++ b/package.json @@ -32,7 +32,7 @@ "promise-reflect": "^1.1.0", "swagger-express-mw": "^0.7.0", "swagger-tools": "^0.10.1", - "watson-developer-cloud": "^2.4.3", + "watson-developer-cloud": "^3.13.0", "winston": "^2.3.1", "xregexp": "^3.1.1" }, From 6ab6fc362726f4ae8ec6a301cebdcb39a67668a5 Mon Sep 17 00:00:00 2001 From: Offer Akrabi Date: Wed, 31 Oct 2018 15:30:05 +0200 Subject: [PATCH 07/31] fix instancination function --- lib/nlu/bundles/engines/wcs/workspace.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/nlu/bundles/engines/wcs/workspace.js b/lib/nlu/bundles/engines/wcs/workspace.js index 178858e..4c43b2a 100644 --- a/lib/nlu/bundles/engines/wcs/workspace.js +++ b/lib/nlu/bundles/engines/wcs/workspace.js @@ -4,7 +4,7 @@ 'use strict'; -const watson = require('watson-developer-cloud'); +const AssistantV1 = require('watson-developer-cloud/assistant/v1'); const hash = require('object-hash'); const logger = require('../../../../logger'); @@ -14,7 +14,7 @@ var Workspace = function (credentials) { // options.url = "https://gateway-fra.watsonplatform.net/conversation/api/"; try { - this.wcs = watson.conversation(options); + this.wcs = new AssistantV1(options); } catch (error) { logger.error("Error setting up your wcs workspace, please check your credentials" + " (either in env vars or wcs.json). \n" + error); From 23800f17dd8ad23de985c653ce4172ba22822111 Mon Sep 17 00:00:00 2001 From: Offer Akrabi Date: Wed, 31 Oct 2018 15:36:37 +0200 Subject: [PATCH 08/31] small bug fix --- lib/handler.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/handler.js b/lib/handler.js index 2534f16..b7dcf49 100644 --- a/lib/handler.js +++ b/lib/handler.js @@ -398,7 +398,7 @@ let setupWCSCredentialsFromEnv = function (self) { */ let copyFromNluResponse = function(context, evaluationResponse, NluResponse) { if(NluResponse) { - if(output.context) { + if(NluResponse.context) { context.skill = NluResponse.context || {}; //set handleUtterance flag from NLU context evaluationResponse.handleUtterance = NluResponse.context.handleUtterance || evaluationResponse.response.handleUtterance; From 79c2f2742e1c8f6cad74faced9d67959b92caec7 Mon Sep 17 00:00:00 2001 From: Offer Akrabi Date: Wed, 31 Oct 2018 17:13:42 +0200 Subject: [PATCH 09/31] fixes --- lib/handler.js | 60 ++++++++++++++++++------ lib/nlu/bundles/engines/wcs/workspace.js | 6 ++- lib/nlu/factory.js | 58 +++++++++++++++-------- 3 files changed, 88 insertions(+), 36 deletions(-) diff --git a/lib/handler.js b/lib/handler.js index d4875bc..1d99d1f 100644 --- a/lib/handler.js +++ b/lib/handler.js @@ -29,9 +29,7 @@ let Handler = function () { * Initializes the handler, sets up the connection to wcs */ Handler.prototype.initialize = function () { - if(process.env.WCS_USERNAME && process.env.WCS_URL && process.env.WCS_PASSWORD && process.env.WCS_VERSION_DATE - && process.env.WCS_VERSION && process.env.WCS_WORKSPACE_ID && process.env.WCS_WORKSPACE_NAME && - process.env.WCS_WORKSPACE_LANGUAGE) { + if(isValidWaEnvVars()) { setupWCSCredentialsFromEnv(this); } else { let rawJson = fs.readFileSync(path.join(process.env.skillSDKResDir, nluFolder, wcsFileName)); @@ -39,7 +37,7 @@ Handler.prototype.initialize = function () { } if (this.wcsCredentials) { let credentials = this.wcsCredentials.credentials; - setupWcs(this, credentials.url, credentials.username, credentials.password, credentials.version_date, credentials.api_key) + setupWcs(this, credentials.url, credentials.username, credentials.password, credentials.version_date, credentials.version, credentials.iam_apikey) } }; @@ -265,16 +263,26 @@ let getIntentity = function (self, request, cb) { * @param versionDate - your wcs version date * @param apiKey */ -let setupWcs = function (self, wcsUrl, wcsUsername, wcsPassword, versionDate, apiKey) { +let setupWcs = function (self, wcsUrl, wcsUsername, wcsPassword, versionDate, version, apiKey) { try { - self.conversation = Promise.promisifyAll( - new Conversation({ - url: wcsUrl, + let options; + if(apiKey) { + options = { + version: version, iam_apikey: apiKey, + url: wcsUrl + } + + } else { + options = { + url: wcsUrl, username: wcsUsername, password: wcsPassword, - version_date: versionDate - }) + version: versionDate + } + } + self.conversation = Promise.promisifyAll( + new Conversation(options) ); } catch (err) { console.error('Conversation service failure or missing credentials.'); @@ -376,14 +384,22 @@ let saveContext = function (self, request, context, response) { */ let setupWCSCredentialsFromEnv = function (self) { - self.wcsCredentials = { + let options = { "workspace": { [process.env.WCS_WORKSPACE_LANGUAGE]: { "name": process.env.WCS_WORKSPACE_NAME, "workspace_id": process.env.WCS_WORKSPACE_ID } - }, - "credentials": { + } + }; + if(process.env.WA_API_KEY) { + options.credentials = { + "url": process.env.WCS_URL, + "version": process.env.WCS_VERSION_DATE, + "iam_apikey": process.env.WA_API_KEY + } + } else if(process.env.WCS_PASSWORD && process.env.WCS_USERNAME) { + options.credentials = { "url": process.env.WCS_URL, "version": process.env.WCS_VERSION, "version_date": process.env.WCS_VERSION_DATE, @@ -391,6 +407,7 @@ let setupWCSCredentialsFromEnv = function (self) { "username": process.env.WCS_USERNAME } } + self.wcsCredentials = options; }; /** @@ -401,7 +418,7 @@ let setupWCSCredentialsFromEnv = function (self) { */ let copyFromNluResponse = function(context, evaluationResponse, NluResponse) { if(NluResponse) { - if(output.context) { + if(NluResponse.context) { context.skill = NluResponse.context || {}; //set handleUtterance flag from NLU context evaluationResponse.handleUtterance = NluResponse.context.handleUtterance || evaluationResponse.response.handleUtterance; @@ -414,4 +431,17 @@ let copyFromNluResponse = function(context, evaluationResponse, NluResponse) { } } } -}; \ No newline at end of file +}; + +/** + * check that all the WA env var credentials exist + * @returns {string} + */ +let isValidWaEnvVars = function() { + return (((process.env.WCS_USERNAME && process.env.WCS_URL && process.env.WCS_PASSWORD && process.env.WCS_VERSION_DATE + && process.env.WCS_VERSION) || + (process.env.WA_API_KEY && process.env.WCS_VERSION_DATE)) && + (process.env.WCS_WORKSPACE_ID && process.env.WCS_WORKSPACE_NAME && + process.env.WCS_WORKSPACE_LANGUAGE)); +}; + diff --git a/lib/nlu/bundles/engines/wcs/workspace.js b/lib/nlu/bundles/engines/wcs/workspace.js index 4c43b2a..1dace14 100644 --- a/lib/nlu/bundles/engines/wcs/workspace.js +++ b/lib/nlu/bundles/engines/wcs/workspace.js @@ -11,8 +11,10 @@ const logger = require('../../../../logger'); var Workspace = function (credentials) { let options = JSON.parse(JSON.stringify(credentials)); - // options.url = "https://gateway-fra.watsonplatform.net/conversation/api/"; - + // hack for WA changing version to version_date + if(options.version_date) { + options.version = options.version_date; + } try { this.wcs = new AssistantV1(options); } catch (error) { diff --git a/lib/nlu/factory.js b/lib/nlu/factory.js index 4a8c007..91eacdd 100644 --- a/lib/nlu/factory.js +++ b/lib/nlu/factory.js @@ -126,20 +126,18 @@ module.exports = Factory; function getNLU(type, name) { return new Promise(function (resolve, reject) { - let nlu; - if(process.env.WCS_USERNAME && process.env.WCS_URL && process.env.WCS_PASSWORD && process.env.WCS_VERSION_DATE - && process.env.WCS_VERSION && process.env.WCS_WORKSPACE_ID && process.env.WCS_WORKSPACE_NAME && - process.env.WCS_WORKSPACE_LANGUAGE) { - nlu = createNLUFromEnv(); - } else { - nlu = require(`${resPath}/nlu/${type}`); - } - if (!nlu) { - reject(errors.couldNotReadNluType); - } - nlu.name = name; - nlu.type = type; - resolve(nlu) + let nlu; + if(isValidWaEnvVars()) { + nlu = createNLUFromEnv(); + } else { + nlu = require(`${resPath}/nlu/${type}`); + } + if (!nlu) { + reject(errors.couldNotReadNluType); + } + nlu.name = name; + nlu.type = type; + resolve(nlu) }).catch(function(err) { logger.error(errors.couldNotReadNluType(type)); next(); @@ -147,19 +145,41 @@ function getNLU(type, name) { } function createNLUFromEnv() { - return { + let options = { "workspace": { [process.env.WCS_WORKSPACE_LANGUAGE]: { "name": process.env.WCS_WORKSPACE_NAME, "workspace_id": process.env.WCS_WORKSPACE_ID } - }, - "credentials": { + } + }; + if(process.env.WA_API_KEY) { + options.credentials = { + "url": process.env.WCS_URL, + "version": process.env.WCS_VERSION_DATE, + "iam_apikey": process.env.WA_API_KEY + } + } else if(process.env.WCS_PASSWORD && process.env.WCS_USERNAME) { + options.credentials = { "url": process.env.WCS_URL, "version": process.env.WCS_VERSION, "version_date": process.env.WCS_VERSION_DATE, "password": process.env.WCS_PASSWORD, "username": process.env.WCS_USERNAME } - }; -} + } + return options; +}; + +/** + * check that all the WA env var credentials exist + * @returns {string} + */ +let isValidWaEnvVars = function() { + return (((process.env.WCS_USERNAME && process.env.WCS_URL && process.env.WCS_PASSWORD && process.env.WCS_VERSION_DATE + && process.env.WCS_VERSION) || + (process.env.WA_API_KEY && process.env.WCS_VERSION_DATE)) && + (process.env.WCS_WORKSPACE_ID && process.env.WCS_WORKSPACE_NAME && + process.env.WCS_WORKSPACE_LANGUAGE)); +}; + From 2e0f15e37a6a57522e8bb5f1658f2777d69f1ab7 Mon Sep 17 00:00:00 2001 From: Offer Akrabi Date: Thu, 1 Nov 2018 11:28:49 +0200 Subject: [PATCH 10/31] small bug fix --- lib/handler.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/handler.js b/lib/handler.js index 1d99d1f..3715fac 100644 --- a/lib/handler.js +++ b/lib/handler.js @@ -421,9 +421,9 @@ let copyFromNluResponse = function(context, evaluationResponse, NluResponse) { if(NluResponse.context) { context.skill = NluResponse.context || {}; //set handleUtterance flag from NLU context - evaluationResponse.handleUtterance = NluResponse.context.handleUtterance || evaluationResponse.response.handleUtterance; + evaluationResponse.response.handleUtterance = NluResponse.context.handleUtterance || evaluationResponse.response.handleUtterance; //set routeByEntities flag from NLU context - evaluationResponse.routeByEntities = NluResponse.context.routeByEntities || evaluationResponse.response.routeByEntities; + evaluationResponse.response.routeByEntities = NluResponse.context.routeByEntities || evaluationResponse.response.routeByEntities; } if(NluResponse.actions) { for(let action of NluResponse.actions) { From bba0b17864fd6d1d60783608259c6e09d9ca2cac Mon Sep 17 00:00:00 2001 From: Offer Akrabi Date: Thu, 1 Nov 2018 11:35:51 +0200 Subject: [PATCH 11/31] small bug fix --- lib/handler.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/handler.js b/lib/handler.js index 3715fac..89d000e 100644 --- a/lib/handler.js +++ b/lib/handler.js @@ -421,9 +421,9 @@ let copyFromNluResponse = function(context, evaluationResponse, NluResponse) { if(NluResponse.context) { context.skill = NluResponse.context || {}; //set handleUtterance flag from NLU context - evaluationResponse.response.handleUtterance = NluResponse.context.handleUtterance || evaluationResponse.response.handleUtterance; + evaluationResponse.response.handleUtterance = NluResponse.context.handleUtterance !== undefined ? NluResponse.context.handleUtterance : evaluationResponse.response.handleUtterance; //set routeByEntities flag from NLU context - evaluationResponse.response.routeByEntities = NluResponse.context.routeByEntities || evaluationResponse.response.routeByEntities; + evaluationResponse.response.routeByEntities = NluResponse.context.routeByEntities !== undefined ? NluResponse.context.routeByEntities : evaluationResponse.response.routeByEntities; } if(NluResponse.actions) { for(let action of NluResponse.actions) { From b04494e10dcd87c697f42acf9750766888c8e651 Mon Sep 17 00:00:00 2001 From: Offer Akrabi Date: Thu, 1 Nov 2018 13:24:25 +0200 Subject: [PATCH 12/31] changes from review --- lib/handler.js | 52 +++----------------- lib/nlu/bundles/engines/wcs/workspace.js | 4 +- lib/nlu/factory.js | 45 ++--------------- lib/nlu/utils.js | 62 ++++++++++++++++++++++++ 4 files changed, 73 insertions(+), 90 deletions(-) create mode 100644 lib/nlu/utils.js diff --git a/lib/handler.js b/lib/handler.js index 89d000e..3985300 100644 --- a/lib/handler.js +++ b/lib/handler.js @@ -10,13 +10,14 @@ const sprintf = require('i18next-sprintf-postprocessor'); const Response = require('./response'); const EvaluationResponse = require('./evaluation-response'); const logger = require('./logger'); -const Conversation = require('watson-developer-cloud/assistant/v1'); +const Assistant = require('watson-developer-cloud/assistant/v1'); const Promise = require('bluebird'); const fs = require('fs'); const path = require('path'); const async = require('async'); const errors = require('./error-responses'); const instrument = require('./instrument.js'); +const WaEnvUtils = require('./nlu/utils'); const nluFolder = '/nlu'; const wcsFileName = '/wcs.json'; @@ -29,8 +30,8 @@ let Handler = function () { * Initializes the handler, sets up the connection to wcs */ Handler.prototype.initialize = function () { - if(isValidWaEnvVars()) { - setupWCSCredentialsFromEnv(this); + if(WaEnvUtils.isValidWaEnvVars()) { + this.wcsCredentials = WaEnvUtils.setupWCSCredentialsFromEnv(); } else { let rawJson = fs.readFileSync(path.join(process.env.skillSDKResDir, nluFolder, wcsFileName)); this.wcsCredentials = JSON.parse(rawJson); @@ -282,7 +283,7 @@ let setupWcs = function (self, wcsUrl, wcsUsername, wcsPassword, versionDate, ve } } self.conversation = Promise.promisifyAll( - new Conversation(options) + new Assistant(options) ); } catch (err) { console.error('Conversation service failure or missing credentials.'); @@ -378,37 +379,6 @@ let saveContext = function (self, request, context, response) { }); }; -/** - * Sets up the handler's wcs credentials from environment variables - * @param self - */ - -let setupWCSCredentialsFromEnv = function (self) { - let options = { - "workspace": { - [process.env.WCS_WORKSPACE_LANGUAGE]: { - "name": process.env.WCS_WORKSPACE_NAME, - "workspace_id": process.env.WCS_WORKSPACE_ID - } - } - }; - if(process.env.WA_API_KEY) { - options.credentials = { - "url": process.env.WCS_URL, - "version": process.env.WCS_VERSION_DATE, - "iam_apikey": process.env.WA_API_KEY - } - } else if(process.env.WCS_PASSWORD && process.env.WCS_USERNAME) { - options.credentials = { - "url": process.env.WCS_URL, - "version": process.env.WCS_VERSION, - "version_date": process.env.WCS_VERSION_DATE, - "password": process.env.WCS_PASSWORD, - "username": process.env.WCS_USERNAME - } - } - self.wcsCredentials = options; -}; /** * copies data from the Nlu evaluation response to the evaluation response and context @@ -433,15 +403,5 @@ let copyFromNluResponse = function(context, evaluationResponse, NluResponse) { } }; -/** - * check that all the WA env var credentials exist - * @returns {string} - */ -let isValidWaEnvVars = function() { - return (((process.env.WCS_USERNAME && process.env.WCS_URL && process.env.WCS_PASSWORD && process.env.WCS_VERSION_DATE - && process.env.WCS_VERSION) || - (process.env.WA_API_KEY && process.env.WCS_VERSION_DATE)) && - (process.env.WCS_WORKSPACE_ID && process.env.WCS_WORKSPACE_NAME && - process.env.WCS_WORKSPACE_LANGUAGE)); -}; + diff --git a/lib/nlu/bundles/engines/wcs/workspace.js b/lib/nlu/bundles/engines/wcs/workspace.js index 1dace14..eb78459 100644 --- a/lib/nlu/bundles/engines/wcs/workspace.js +++ b/lib/nlu/bundles/engines/wcs/workspace.js @@ -4,7 +4,7 @@ 'use strict'; -const AssistantV1 = require('watson-developer-cloud/assistant/v1'); +const Assistant = require('watson-developer-cloud/assistant/v1'); const hash = require('object-hash'); const logger = require('../../../../logger'); @@ -16,7 +16,7 @@ var Workspace = function (credentials) { options.version = options.version_date; } try { - this.wcs = new AssistantV1(options); + this.wcs = new Assistant(options); } catch (error) { logger.error("Error setting up your wcs workspace, please check your credentials" + " (either in env vars or wcs.json). \n" + error); diff --git a/lib/nlu/factory.js b/lib/nlu/factory.js index 91eacdd..6dee221 100644 --- a/lib/nlu/factory.js +++ b/lib/nlu/factory.js @@ -14,6 +14,7 @@ const {getSupportedUser} = require('./supported'); const errors = require('../error-responses'); const fs = require('fs'); const resPath = process.env.skillSDKResDir; +const WaEnvUtils = require('./utils'); /** @@ -127,8 +128,8 @@ module.exports = Factory; function getNLU(type, name) { return new Promise(function (resolve, reject) { let nlu; - if(isValidWaEnvVars()) { - nlu = createNLUFromEnv(); + if(WaEnvUtils.isValidWaEnvVars()) { + nlu = WaEnvUtils.setupWCSCredentialsFromEnv(); } else { nlu = require(`${resPath}/nlu/${type}`); } @@ -143,43 +144,3 @@ function getNLU(type, name) { next(); }) } - -function createNLUFromEnv() { - let options = { - "workspace": { - [process.env.WCS_WORKSPACE_LANGUAGE]: { - "name": process.env.WCS_WORKSPACE_NAME, - "workspace_id": process.env.WCS_WORKSPACE_ID - } - } - }; - if(process.env.WA_API_KEY) { - options.credentials = { - "url": process.env.WCS_URL, - "version": process.env.WCS_VERSION_DATE, - "iam_apikey": process.env.WA_API_KEY - } - } else if(process.env.WCS_PASSWORD && process.env.WCS_USERNAME) { - options.credentials = { - "url": process.env.WCS_URL, - "version": process.env.WCS_VERSION, - "version_date": process.env.WCS_VERSION_DATE, - "password": process.env.WCS_PASSWORD, - "username": process.env.WCS_USERNAME - } - } - return options; -}; - -/** - * check that all the WA env var credentials exist - * @returns {string} - */ -let isValidWaEnvVars = function() { - return (((process.env.WCS_USERNAME && process.env.WCS_URL && process.env.WCS_PASSWORD && process.env.WCS_VERSION_DATE - && process.env.WCS_VERSION) || - (process.env.WA_API_KEY && process.env.WCS_VERSION_DATE)) && - (process.env.WCS_WORKSPACE_ID && process.env.WCS_WORKSPACE_NAME && - process.env.WCS_WORKSPACE_LANGUAGE)); -}; - diff --git a/lib/nlu/utils.js b/lib/nlu/utils.js new file mode 100644 index 0000000..10ef8d4 --- /dev/null +++ b/lib/nlu/utils.js @@ -0,0 +1,62 @@ +/* +© Copyright IBM Corp. 2017 +*/ + + + +'use strict'; + + +/** + * check that all the WA env var credentials exist + * @returns {string} + */ +let isValidWaEnvVars = function() { + return (((process.env.WCS_USERNAME && process.env.WCS_URL && process.env.WCS_PASSWORD && process.env.WCS_VERSION_DATE + && process.env.WCS_VERSION) || + (process.env.WA_API_KEY && process.env.WCS_VERSION_DATE)) && + (process.env.WCS_WORKSPACE_ID && process.env.WCS_WORKSPACE_NAME && + process.env.WCS_WORKSPACE_LANGUAGE)); +}; + + +/** + * Sets up the handler's wcs credentials from environment variables + * @param handler + */ + +let setupWCSCredentialsFromEnv = function () { + let options = { + "workspace": { + [process.env.WCS_WORKSPACE_LANGUAGE]: { + "name": process.env.WCS_WORKSPACE_NAME, + "workspace_id": process.env.WCS_WORKSPACE_ID + } + } + }; + // new way of wa authentication - with iam api-key + if(process.env.WA_API_KEY) { + options.credentials = { + "url": process.env.WCS_URL, + "version": process.env.WCS_VERSION_DATE, + "iam_apikey": process.env.WA_API_KEY + } + } + // deprecated way of wa authentication + else if(process.env.WCS_PASSWORD && process.env.WCS_USERNAME) { + options.credentials = { + "url": process.env.WCS_URL, + "version": process.env.WCS_VERSION, + "version_date": process.env.WCS_VERSION_DATE, + "password": process.env.WCS_PASSWORD, + "username": process.env.WCS_USERNAME + } + } + return options; +}; + + +module.exports = { + isValidWaEnvVars: isValidWaEnvVars, + setupWCSCredentialsFromEnv: setupWCSCredentialsFromEnv +}; \ No newline at end of file From 533cdf80c50b372b95ee30cf994b721ed49d7b97 Mon Sep 17 00:00:00 2001 From: Offer Akrabi Date: Thu, 1 Nov 2018 13:31:58 +0200 Subject: [PATCH 13/31] added small fix --- lib/handler.js | 4 ++-- lib/nlu/factory.js | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/handler.js b/lib/handler.js index 3985300..4fd8ae0 100644 --- a/lib/handler.js +++ b/lib/handler.js @@ -264,7 +264,7 @@ let getIntentity = function (self, request, cb) { * @param versionDate - your wcs version date * @param apiKey */ -let setupWcs = function (self, wcsUrl, wcsUsername, wcsPassword, versionDate, version, apiKey) { +let setupWcs = function (handler, wcsUrl, wcsUsername, wcsPassword, versionDate, version, apiKey) { try { let options; if(apiKey) { @@ -282,7 +282,7 @@ let setupWcs = function (self, wcsUrl, wcsUsername, wcsPassword, versionDate, ve version: versionDate } } - self.conversation = Promise.promisifyAll( + handler.conversation = Promise.promisifyAll( new Assistant(options) ); } catch (err) { diff --git a/lib/nlu/factory.js b/lib/nlu/factory.js index 6dee221..6f7aba7 100644 --- a/lib/nlu/factory.js +++ b/lib/nlu/factory.js @@ -128,7 +128,7 @@ module.exports = Factory; function getNLU(type, name) { return new Promise(function (resolve, reject) { let nlu; - if(WaEnvUtils.isValidWaEnvVars()) { + if(WaEnvUtils.isValidWaEnvVars() && type === 'wcs') { nlu = WaEnvUtils.setupWCSCredentialsFromEnv(); } else { nlu = require(`${resPath}/nlu/${type}`); From 220a3441a6e759f21a7d25a3b0bbac5da066ed35 Mon Sep 17 00:00:00 2001 From: gaynell-gonsalves <35956168+gaynell-gonsalves@users.noreply.github.com> Date: Tue, 6 Nov 2018 14:32:23 +0530 Subject: [PATCH 14/31] Updated version to 0.0.17 (WAS Nov Release) --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 0051ea3..7bfc6d2 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "skill-sdk-nodejs", - "version": "0.0.16", + "version": "0.0.17", "description": "This is the SDK to be used when developing a skill for Watson Assistant Solutions using Node.js", "main": "index.js", "scripts": { From 24196c13a5293e5c0b90d33cdd02165f1cc86237 Mon Sep 17 00:00:00 2001 From: Offer Akrabi Date: Tue, 6 Nov 2018 14:13:22 +0200 Subject: [PATCH 15/31] saved built in context --- lib/evaluation-response.js | 1 + lib/handler.js | 6 ++++-- lib/response.js | 1 + 3 files changed, 6 insertions(+), 2 deletions(-) diff --git a/lib/evaluation-response.js b/lib/evaluation-response.js index 42f9911..16f579e 100644 --- a/lib/evaluation-response.js +++ b/lib/evaluation-response.js @@ -8,6 +8,7 @@ function EvaluationResponse(callback) { intentities: [], handleUtterance: true, routeByEntities: true, + builtInContext: {}, context: { }, internal:{ diff --git a/lib/handler.js b/lib/handler.js index 4fd8ae0..d20cb4f 100644 --- a/lib/handler.js +++ b/lib/handler.js @@ -348,7 +348,8 @@ let createContext = function (request) { return { utterance: request.context.application.attributes, session: request.context.session.attributes, - skill: skillContext + skill: skillContext, + builtIn: request.context.builtIn }; }; @@ -374,7 +375,8 @@ let saveContext = function (self, request, context, response) { Object.assign(response.response, { context: { application: request.context.application, - session: sessionContext + session: sessionContext, + builtIn: context.builtIn } }); }; diff --git a/lib/response.js b/lib/response.js index 5a88743..b634f7b 100644 --- a/lib/response.js +++ b/lib/response.js @@ -15,6 +15,7 @@ function Response(callback) { deleteSkillSession: true, captureInput: false, speech: {}, + builtInContext: {}, context: { inConversation: false }, From dfee6f6ea61471982fcbffc18a05f3c52ee622b5 Mon Sep 17 00:00:00 2001 From: Offer Akrabi Date: Wed, 7 Nov 2018 10:23:07 +0200 Subject: [PATCH 16/31] updated swagger --- lib/server/api/swagger/swagger.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/server/api/swagger/swagger.yaml b/lib/server/api/swagger/swagger.yaml index 2d796ce..e7cdecc 100644 --- a/lib/server/api/swagger/swagger.yaml +++ b/lib/server/api/swagger/swagger.yaml @@ -283,7 +283,7 @@ definitions: error: type: integer example: 200 - shouldEndSession: + deleteSkillSession: type: boolean example: true captureInput: From 0141d9009b04276b4480f6e0997f32f9542be841 Mon Sep 17 00:00:00 2001 From: Offer Akrabi Date: Wed, 7 Nov 2018 10:29:30 +0200 Subject: [PATCH 17/31] updated swagger --- lib/server/api/swagger/swagger.yaml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lib/server/api/swagger/swagger.yaml b/lib/server/api/swagger/swagger.yaml index 2d796ce..270b7db 100644 --- a/lib/server/api/swagger/swagger.yaml +++ b/lib/server/api/swagger/swagger.yaml @@ -348,6 +348,10 @@ definitions: type: string example: 'Hello' description: utterance + retext: + type: string + example: 'hello' + description: utterance context: type: object description: context of conversation From f08a9f31cb1ed9a9d6015d96ea276d8d86101442 Mon Sep 17 00:00:00 2001 From: Offer Akrabi Date: Mon, 12 Nov 2018 14:38:32 +0200 Subject: [PATCH 18/31] updated response and evaluateResponse objects --- lib/evaluation-response.js | 1 - lib/response.js | 1 - 2 files changed, 2 deletions(-) diff --git a/lib/evaluation-response.js b/lib/evaluation-response.js index 16f579e..42f9911 100644 --- a/lib/evaluation-response.js +++ b/lib/evaluation-response.js @@ -8,7 +8,6 @@ function EvaluationResponse(callback) { intentities: [], handleUtterance: true, routeByEntities: true, - builtInContext: {}, context: { }, internal:{ diff --git a/lib/response.js b/lib/response.js index b634f7b..5a88743 100644 --- a/lib/response.js +++ b/lib/response.js @@ -15,7 +15,6 @@ function Response(callback) { deleteSkillSession: true, captureInput: false, speech: {}, - builtInContext: {}, context: { inConversation: false }, From d3ffa3086d844d0ab27cacfc2629a7a0657caa60 Mon Sep 17 00:00:00 2001 From: Offer Akrabi Date: Mon, 12 Nov 2018 14:40:07 +0200 Subject: [PATCH 19/31] updated response and evaluateResponse objects --- lib/handler.js | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/handler.js b/lib/handler.js index d20cb4f..adf5118 100644 --- a/lib/handler.js +++ b/lib/handler.js @@ -2,7 +2,6 @@ © Copyright IBM Corp. 2017 */ - 'use strict'; const i18 = require('i18next'); From 0f25420dd3bbe72a2c8fcb61b5b47087ffd0cb31 Mon Sep 17 00:00:00 2001 From: Offer Akrabi Date: Tue, 13 Nov 2018 11:45:37 +0200 Subject: [PATCH 20/31] added placeholder for context validation functions in handler --- lib/handler.js | 36 ++++++++++++++++++++++++++++++++++++ package.json | 3 ++- 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/lib/handler.js b/lib/handler.js index adf5118..173c933 100644 --- a/lib/handler.js +++ b/lib/handler.js @@ -17,6 +17,7 @@ const async = require('async'); const errors = require('./error-responses'); const instrument = require('./instrument.js'); const WaEnvUtils = require('./nlu/utils'); +const builtInContextValidation = require('wa-built-in-context'); const nluFolder = '/nlu'; const wcsFileName = '/wcs.json'; @@ -222,6 +223,41 @@ Handler.prototype.saveEvaluationContext = function (context, evaluationContext) context.session = evaluationContext.session.attributes; }; +/** + * sset built in context + * @param context - context object created by the sdk + * @param builtInContext - user altered built in context + */ +Handler.prototype.setBuiltInContext = function(context, builtInContext) { + const validationResult = builtInContextValidation.validateBuiltinContext(builtInContext); + if(validationResult.valid) { + context.builtIn = builtInContext; + console.info('Successfully set built-in context'); + } else { + console.error('Error setting built-in context: ' + validationResult.errors); + } +}; + +/** + * get built in context + * @param context + * @returns {*} + */ +Handler.prototype.getBuiltInContext = function(context) { + return context.builtIn; +}; + +/** + * validate built in context + * @param builtInContext - user altered built-in context + * @returns {boolean} + */ +Handler.prototype.validateBuiltInContext = function(builtInContext) { + const validationResult = builtInContextValidation.validateBuiltinContext(builtInContext); + return validationResult.valid; +}; + + module.exports = Handler; diff --git a/package.json b/package.json index 7bfc6d2..8ccf62a 100644 --- a/package.json +++ b/package.json @@ -34,7 +34,8 @@ "swagger-tools": "^0.10.1", "watson-developer-cloud": "^3.13.0", "winston": "^2.3.1", - "xregexp": "^3.1.1" + "xregexp": "^3.1.1", + "wa-built-in-context": "git+https://d6744fcda2390d6f40e524aa1fc94ce6bb543864:x-oauth-basic@github.ibm.com/ConsumerIoT/ContextValidation.git" }, "devDependencies": { "mocha": "^3.2.0", From fda5d8a99ea0ef7d16be01b1b095aa20f69997bf Mon Sep 17 00:00:00 2001 From: Offer Akrabi Date: Tue, 13 Nov 2018 14:57:37 +0200 Subject: [PATCH 21/31] added get/set/validate functions for built-in context --- lib/handler.js | 34 ++++++++++++++++++++++++---------- 1 file changed, 24 insertions(+), 10 deletions(-) diff --git a/lib/handler.js b/lib/handler.js index 173c933..6052dac 100644 --- a/lib/handler.js +++ b/lib/handler.js @@ -226,25 +226,40 @@ Handler.prototype.saveEvaluationContext = function (context, evaluationContext) /** * sset built in context * @param context - context object created by the sdk - * @param builtInContext - user altered built in context + * @param path - path to property + * @param value - desired value to be set */ -Handler.prototype.setBuiltInContext = function(context, builtInContext) { - const validationResult = builtInContextValidation.validateBuiltinContext(builtInContext); +Handler.prototype.setBuiltInContextProperty = function(context, path, value) { + const validationResult = builtInContextValidation.validateBuiltinContext(object); if(validationResult.valid) { - context.builtIn = builtInContext; - console.info('Successfully set built-in context'); + const result = builtInContextValidation.setProperty(context.builtIn, path, value); + if(result.valid) { + console.info('Successfully set built-in context property'); + return result + } else { + console.error('Error setting built-in context property'); + return result; + } } else { - console.error('Error setting built-in context: ' + validationResult.errors); + console.error('Error setting built-in context property'); + return validationResult; } }; /** * get built in context * @param context + * @param path * @returns {*} */ -Handler.prototype.getBuiltInContext = function(context) { - return context.builtIn; +Handler.prototype.getBuiltInContextProperty = function(context, path) { + const result = builtInContextValidation.getProperty(context.builtIn, path); + if(result.valid) { + return result.value; + } else { + console.error('Error getting built-in context: ' + result.msg); + return undefined; + } }; /** @@ -253,8 +268,7 @@ Handler.prototype.getBuiltInContext = function(context) { * @returns {boolean} */ Handler.prototype.validateBuiltInContext = function(builtInContext) { - const validationResult = builtInContextValidation.validateBuiltinContext(builtInContext); - return validationResult.valid; + return builtInContextValidation.validateBuiltinContext(builtInContext); }; From 3de4fd803f4e1f9f7a82d59d2662aef6bb3da8bd Mon Sep 17 00:00:00 2001 From: Offer Akrabi Date: Wed, 14 Nov 2018 10:12:52 +0200 Subject: [PATCH 22/31] added delete functino --- lib/handler.js | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/lib/handler.js b/lib/handler.js index 6052dac..c917dfa 100644 --- a/lib/handler.js +++ b/lib/handler.js @@ -17,7 +17,7 @@ const async = require('async'); const errors = require('./error-responses'); const instrument = require('./instrument.js'); const WaEnvUtils = require('./nlu/utils'); -const builtInContextValidation = require('wa-built-in-context'); +const builtInContextUtils = require('wa-built-in-context'); const nluFolder = '/nlu'; const wcsFileName = '/wcs.json'; @@ -230,9 +230,9 @@ Handler.prototype.saveEvaluationContext = function (context, evaluationContext) * @param value - desired value to be set */ Handler.prototype.setBuiltInContextProperty = function(context, path, value) { - const validationResult = builtInContextValidation.validateBuiltinContext(object); + const validationResult = builtInContextUtils.validateBuiltinContext(object); if(validationResult.valid) { - const result = builtInContextValidation.setProperty(context.builtIn, path, value); + const result = builtInContextUtils.setProperty(context.builtIn, path, value); if(result.valid) { console.info('Successfully set built-in context property'); return result @@ -246,6 +246,17 @@ Handler.prototype.setBuiltInContextProperty = function(context, path, value) { } }; +/** + * delete a built in context property + * @param context - sdk context object + * @param path - path to property + * @returns {{}} + */ +Handler.prototype.deleteBuiltInContextProperty = function(context, path) { + return builtInContextUtils.deleteProperty(context.builtIn, path); +}; + + /** * get built in context * @param context @@ -253,7 +264,7 @@ Handler.prototype.setBuiltInContextProperty = function(context, path, value) { * @returns {*} */ Handler.prototype.getBuiltInContextProperty = function(context, path) { - const result = builtInContextValidation.getProperty(context.builtIn, path); + const result = builtInContextUtils.getProperty(context.builtIn, path); if(result.valid) { return result.value; } else { @@ -268,7 +279,7 @@ Handler.prototype.getBuiltInContextProperty = function(context, path) { * @returns {boolean} */ Handler.prototype.validateBuiltInContext = function(builtInContext) { - return builtInContextValidation.validateBuiltinContext(builtInContext); + return builtInContextUtils.validateBuiltinContext(builtInContext); }; From 404d7d5ae1fbbf97411bdfd6fa359fd237dde1f7 Mon Sep 17 00:00:00 2001 From: Offer Akrabi Date: Wed, 14 Nov 2018 10:31:16 +0200 Subject: [PATCH 23/31] updated error messages --- lib/handler.js | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/lib/handler.js b/lib/handler.js index c917dfa..ca90708 100644 --- a/lib/handler.js +++ b/lib/handler.js @@ -234,14 +234,15 @@ Handler.prototype.setBuiltInContextProperty = function(context, path, value) { if(validationResult.valid) { const result = builtInContextUtils.setProperty(context.builtIn, path, value); if(result.valid) { - console.info('Successfully set built-in context property'); + context.builtIn = validationResult.obj; + console.info('Successfully set built-in context property ' + path); return result } else { - console.error('Error setting built-in context property'); + console.error('Error setting built-in context property ' + path); return result; } } else { - console.error('Error setting built-in context property'); + console.error('Error setting built-in context property ' + path); return validationResult; } }; @@ -253,7 +254,15 @@ Handler.prototype.setBuiltInContextProperty = function(context, path, value) { * @returns {{}} */ Handler.prototype.deleteBuiltInContextProperty = function(context, path) { - return builtInContextUtils.deleteProperty(context.builtIn, path); + const result = builtInContextUtils.deleteProperty(context.builtIn, path); + if(result.valid) { + context.builtIn = result.obj; + console.log('Successfully deleted built-in context property ' + path); + return result; + } else { + console.error('Error deleting built-in context property ' + path); + return result; + } }; @@ -266,10 +275,10 @@ Handler.prototype.deleteBuiltInContextProperty = function(context, path) { Handler.prototype.getBuiltInContextProperty = function(context, path) { const result = builtInContextUtils.getProperty(context.builtIn, path); if(result.valid) { - return result.value; + return result; } else { - console.error('Error getting built-in context: ' + result.msg); - return undefined; + console.error('Error getting built-in context property ' + path); + return result; } }; @@ -278,8 +287,8 @@ Handler.prototype.getBuiltInContextProperty = function(context, path) { * @param builtInContext - user altered built-in context * @returns {boolean} */ -Handler.prototype.validateBuiltInContext = function(builtInContext) { - return builtInContextUtils.validateBuiltinContext(builtInContext); +Handler.prototype.validateBuiltInContext = function(context) { + return builtInContextUtils.validateBuiltinContext(context.builtIn); }; From bd0f231b1e258dc9436472968d05d2c2635f1fab Mon Sep 17 00:00:00 2001 From: Offer Akrabi Date: Wed, 14 Nov 2018 16:23:05 +0200 Subject: [PATCH 24/31] small fixes --- lib/handler.js | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/lib/handler.js b/lib/handler.js index ca90708..202e243 100644 --- a/lib/handler.js +++ b/lib/handler.js @@ -19,6 +19,7 @@ const instrument = require('./instrument.js'); const WaEnvUtils = require('./nlu/utils'); const builtInContextUtils = require('wa-built-in-context'); + const nluFolder = '/nlu'; const wcsFileName = '/wcs.json'; @@ -230,20 +231,23 @@ Handler.prototype.saveEvaluationContext = function (context, evaluationContext) * @param value - desired value to be set */ Handler.prototype.setBuiltInContextProperty = function(context, path, value) { - const validationResult = builtInContextUtils.validateBuiltinContext(object); - if(validationResult.valid) { - const result = builtInContextUtils.setProperty(context.builtIn, path, value); - if(result.valid) { + const result = builtInContextUtils.setProperty(context.builtIn, path, value); + if(result.valid) { + const validationResult = builtInContextUtils.validateBuiltinContext(result.obj); + if(validationResult.valid) { context.builtIn = validationResult.obj; - console.info('Successfully set built-in context property ' + path); + logger.info('Successfully set built-in context property ' + path); return result } else { - console.error('Error setting built-in context property ' + path); + result.valid = false; + result.msg = validationResult; + result.obj = context.builtIn; + logger.error('Error setting built-in context property ' + path); return result; } } else { - console.error('Error setting built-in context property ' + path); - return validationResult; + logger.error('Error setting built-in context property ' + path); + return result; } }; @@ -257,10 +261,10 @@ Handler.prototype.deleteBuiltInContextProperty = function(context, path) { const result = builtInContextUtils.deleteProperty(context.builtIn, path); if(result.valid) { context.builtIn = result.obj; - console.log('Successfully deleted built-in context property ' + path); + logger.log('Successfully deleted built-in context property ' + path); return result; } else { - console.error('Error deleting built-in context property ' + path); + logger.error('Error deleting built-in context property ' + path); return result; } }; @@ -277,14 +281,14 @@ Handler.prototype.getBuiltInContextProperty = function(context, path) { if(result.valid) { return result; } else { - console.error('Error getting built-in context property ' + path); + logger.error('Error getting built-in context property ' + path); return result; } }; /** * validate built in context - * @param builtInContext - user altered built-in context + * @param context - user altered built-in context * @returns {boolean} */ Handler.prototype.validateBuiltInContext = function(context) { From a1a917b1ef1b69282ff769848526d2c5f8326620 Mon Sep 17 00:00:00 2001 From: Offer Akrabi Date: Mon, 19 Nov 2018 15:26:45 +0200 Subject: [PATCH 25/31] updates from code review --- lib/error-responses.js | 18 +++++++++++++++++- lib/handler.js | 28 +++++++++++++++++++--------- package.json | 2 +- 3 files changed, 37 insertions(+), 11 deletions(-) diff --git a/lib/error-responses.js b/lib/error-responses.js index dd1860b..84b3702 100644 --- a/lib/error-responses.js +++ b/lib/error-responses.js @@ -25,6 +25,19 @@ function noNluDeclared() { return `Could not evaluate request, no NLU engines declared in the manifest`; } +function errorSettingBuiltInContext(path) { + return 'Error setting built-in context property ' + path; +} + +function errorDeletingBuiltInContext(path) { + return 'Error deleting built-in context property ' + path; +} + +function builtInContextValidationError() { + return 'Error validating your built-in context object, it will not be saved to your session context'; +} + + // Return response errors module.exports = { @@ -32,5 +45,8 @@ module.exports = { couldNotReadNluType: couldNotReadNluType, invalidAction: invalidAction, noEvaluationAction: noEvaluationAction, - noNluDeclared: noNluDeclared + noNluDeclared: noNluDeclared, + errorSettingBuiltInContext: errorSettingBuiltInContext, + errorDeletingBuiltInContext: errorDeletingBuiltInContext, + builtInContextValidationError: builtInContextValidationError }; diff --git a/lib/handler.js b/lib/handler.js index 202e243..6590831 100644 --- a/lib/handler.js +++ b/lib/handler.js @@ -17,7 +17,7 @@ const async = require('async'); const errors = require('./error-responses'); const instrument = require('./instrument.js'); const WaEnvUtils = require('./nlu/utils'); -const builtInContextUtils = require('wa-built-in-context'); +const builtInContextUtils = require('wa-context-utils'); const nluFolder = '/nlu'; @@ -231,7 +231,7 @@ Handler.prototype.saveEvaluationContext = function (context, evaluationContext) * @param value - desired value to be set */ Handler.prototype.setBuiltInContextProperty = function(context, path, value) { - const result = builtInContextUtils.setProperty(context.builtIn, path, value); + const result = builtInContextUtils.setProperty(JSON.parse(JSON.stringify(context.builtIn)), path, value); if(result.valid) { const validationResult = builtInContextUtils.validateBuiltinContext(result.obj); if(validationResult.valid) { @@ -242,11 +242,11 @@ Handler.prototype.setBuiltInContextProperty = function(context, path, value) { result.valid = false; result.msg = validationResult; result.obj = context.builtIn; - logger.error('Error setting built-in context property ' + path); + logger.error(errors.errorSettingBuiltInContext(path)); return result; } } else { - logger.error('Error setting built-in context property ' + path); + logger.error(errors.errorSettingBuiltInContext(path)); return result; } }; @@ -258,13 +258,19 @@ Handler.prototype.setBuiltInContextProperty = function(context, path, value) { * @returns {{}} */ Handler.prototype.deleteBuiltInContextProperty = function(context, path) { - const result = builtInContextUtils.deleteProperty(context.builtIn, path); + const result = builtInContextUtils.deleteProperty(JSON.parse(JSON.stringify(context.builtIn)), path); if(result.valid) { - context.builtIn = result.obj; - logger.log('Successfully deleted built-in context property ' + path); - return result; + const validationResult = builtInContextUtils.validateBuiltinContext(result.obj); + if(validationResult.valid) { + context.builtIn = result.obj; + logger.log('Successfully deleted built-in context property ' + path); + return result; + } else { + logger.error(errors.errorDeletingBuiltInContext(path)); + return result; + } } else { - logger.error('Error deleting built-in context property ' + path); + logger.error(errors.errorDeletingBuiltInContext(path)); return result; } }; @@ -434,6 +440,10 @@ let createContext = function (request) { * @param response */ let saveContext = function (self, request, context, response) { + const validationResult = builtInContextUtils.validateBuiltinContext(context.builtIn); + if(!validationResult.valid) { + logger.error(errors.builtInContextValidationError()); + } request.context.session.attributes.state = self.state; if(response.response.context && response.response.context.inConversation !== undefined) { context.skill.inConversation = response.response.context.inConversation; diff --git a/package.json b/package.json index 8ccf62a..90c90c3 100644 --- a/package.json +++ b/package.json @@ -35,7 +35,7 @@ "watson-developer-cloud": "^3.13.0", "winston": "^2.3.1", "xregexp": "^3.1.1", - "wa-built-in-context": "git+https://d6744fcda2390d6f40e524aa1fc94ce6bb543864:x-oauth-basic@github.ibm.com/ConsumerIoT/ContextValidation.git" + "wa-context-utils": "^1.0.0" }, "devDependencies": { "mocha": "^3.2.0", From 3c35b4190482edf9efbe1d71fa9ddc8d122af408 Mon Sep 17 00:00:00 2001 From: Offer Akrabi Date: Tue, 20 Nov 2018 10:47:04 +0200 Subject: [PATCH 26/31] changed builtIn to lowercase --- lib/handler.js | 20 ++++++++++---------- package.json | 2 +- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/lib/handler.js b/lib/handler.js index 6590831..20ea362 100644 --- a/lib/handler.js +++ b/lib/handler.js @@ -231,17 +231,17 @@ Handler.prototype.saveEvaluationContext = function (context, evaluationContext) * @param value - desired value to be set */ Handler.prototype.setBuiltInContextProperty = function(context, path, value) { - const result = builtInContextUtils.setProperty(JSON.parse(JSON.stringify(context.builtIn)), path, value); + const result = builtInContextUtils.setProperty(JSON.parse(JSON.stringify(context.builtin)), path, value); if(result.valid) { const validationResult = builtInContextUtils.validateBuiltinContext(result.obj); if(validationResult.valid) { - context.builtIn = validationResult.obj; + context.builtin = validationResult.obj; logger.info('Successfully set built-in context property ' + path); return result } else { result.valid = false; result.msg = validationResult; - result.obj = context.builtIn; + result.obj = context.builtin; logger.error(errors.errorSettingBuiltInContext(path)); return result; } @@ -258,11 +258,11 @@ Handler.prototype.setBuiltInContextProperty = function(context, path, value) { * @returns {{}} */ Handler.prototype.deleteBuiltInContextProperty = function(context, path) { - const result = builtInContextUtils.deleteProperty(JSON.parse(JSON.stringify(context.builtIn)), path); + const result = builtInContextUtils.deleteProperty(JSON.parse(JSON.stringify(context.builtin)), path); if(result.valid) { const validationResult = builtInContextUtils.validateBuiltinContext(result.obj); if(validationResult.valid) { - context.builtIn = result.obj; + context.builtin = result.obj; logger.log('Successfully deleted built-in context property ' + path); return result; } else { @@ -283,7 +283,7 @@ Handler.prototype.deleteBuiltInContextProperty = function(context, path) { * @returns {*} */ Handler.prototype.getBuiltInContextProperty = function(context, path) { - const result = builtInContextUtils.getProperty(context.builtIn, path); + const result = builtInContextUtils.getProperty(context.builtin, path); if(result.valid) { return result; } else { @@ -298,7 +298,7 @@ Handler.prototype.getBuiltInContextProperty = function(context, path) { * @returns {boolean} */ Handler.prototype.validateBuiltInContext = function(context) { - return builtInContextUtils.validateBuiltinContext(context.builtIn); + return builtInContextUtils.validateBuiltinContext(context.builtin); }; @@ -428,7 +428,7 @@ let createContext = function (request) { utterance: request.context.application.attributes, session: request.context.session.attributes, skill: skillContext, - builtIn: request.context.builtIn + builtIn: request.context.builtin }; }; @@ -440,7 +440,7 @@ let createContext = function (request) { * @param response */ let saveContext = function (self, request, context, response) { - const validationResult = builtInContextUtils.validateBuiltinContext(context.builtIn); + const validationResult = builtInContextUtils.validateBuiltinContext(context.builtin); if(!validationResult.valid) { logger.error(errors.builtInContextValidationError()); } @@ -459,7 +459,7 @@ let saveContext = function (self, request, context, response) { context: { application: request.context.application, session: sessionContext, - builtIn: context.builtIn + builtIn: context.builtin } }); }; diff --git a/package.json b/package.json index 90c90c3..2890ff7 100644 --- a/package.json +++ b/package.json @@ -35,7 +35,7 @@ "watson-developer-cloud": "^3.13.0", "winston": "^2.3.1", "xregexp": "^3.1.1", - "wa-context-utils": "^1.0.0" + "wa-context-utils":"^0.0.2" }, "devDependencies": { "mocha": "^3.2.0", From 774d25c976cfc8feb1d602a05474aeaf54407df6 Mon Sep 17 00:00:00 2001 From: Offer Akrabi Date: Tue, 20 Nov 2018 11:38:59 +0200 Subject: [PATCH 27/31] added threshold to evaluation response --- lib/evaluation-response.js | 1 + lib/handler.js | 4 ++++ 2 files changed, 5 insertions(+) diff --git a/lib/evaluation-response.js b/lib/evaluation-response.js index 42f9911..904b3de 100644 --- a/lib/evaluation-response.js +++ b/lib/evaluation-response.js @@ -6,6 +6,7 @@ function EvaluationResponse(callback) { responseCode: ErrorCodes.ok, requestResult: {}, intentities: [], + threshold: 0.85, handleUtterance: true, routeByEntities: true, context: { diff --git a/lib/handler.js b/lib/handler.js index 4fd8ae0..9137f6d 100644 --- a/lib/handler.js +++ b/lib/handler.js @@ -48,6 +48,7 @@ Handler.prototype.initialize = function () { * @param callback - a callback function that gets the evaluation response */ Handler.prototype.handleEvaluationRequest = function (request, callback) { + let self = this; if(!this.engines || this.engines.length < 1) { callback({responseCode: 500, requestResult: errors.noNluDeclared()}); return; @@ -55,6 +56,9 @@ Handler.prototype.handleEvaluationRequest = function (request, callback) { let evaluationResponse = new EvaluationResponse((err, result) => { instrument.exitTime(evaluationResponse.response, "evaluate"); + if(self.manifest && self.manifest.threshold) { + evaluationResponse.response.threshold = self.manifest.threshold; + } saveContext(this, request, context, evaluationResponse); callback(err, evaluationResponse.response); }); From cb86a740c9b569d58d0cea0a2098ff74bf1e9456 Mon Sep 17 00:00:00 2001 From: Offer Akrabi Date: Wed, 21 Nov 2018 10:14:12 +0200 Subject: [PATCH 28/31] changes requested in code review --- lib/handler.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/handler.js b/lib/handler.js index 20ea362..f1f0c37 100644 --- a/lib/handler.js +++ b/lib/handler.js @@ -428,7 +428,7 @@ let createContext = function (request) { utterance: request.context.application.attributes, session: request.context.session.attributes, skill: skillContext, - builtIn: request.context.builtin + builtin: request.context.builtin }; }; @@ -459,7 +459,7 @@ let saveContext = function (self, request, context, response) { context: { application: request.context.application, session: sessionContext, - builtIn: context.builtin + builtin: context.builtin } }); }; From b87a1a9b1b809460cc884dfc5d032cc79260d5ab Mon Sep 17 00:00:00 2001 From: Offer Akrabi Date: Thu, 22 Nov 2018 13:45:46 +0200 Subject: [PATCH 29/31] removed validation checks --- lib/error-responses.js | 2 +- lib/handler.js | 35 ++++++++++------------------------- 2 files changed, 11 insertions(+), 26 deletions(-) diff --git a/lib/error-responses.js b/lib/error-responses.js index 84b3702..6b325a3 100644 --- a/lib/error-responses.js +++ b/lib/error-responses.js @@ -34,7 +34,7 @@ function errorDeletingBuiltInContext(path) { } function builtInContextValidationError() { - return 'Error validating your built-in context object, it will not be saved to your session context'; + return 'Error validating your built-in context object, it will not be saved'; } diff --git a/lib/handler.js b/lib/handler.js index f1f0c37..3f35e1c 100644 --- a/lib/handler.js +++ b/lib/handler.js @@ -230,21 +230,12 @@ Handler.prototype.saveEvaluationContext = function (context, evaluationContext) * @param path - path to property * @param value - desired value to be set */ -Handler.prototype.setBuiltInContextProperty = function(context, path, value) { +Handler.prototype.setBuiltinContextProperty = function(context, path, value) { const result = builtInContextUtils.setProperty(JSON.parse(JSON.stringify(context.builtin)), path, value); if(result.valid) { - const validationResult = builtInContextUtils.validateBuiltinContext(result.obj); - if(validationResult.valid) { - context.builtin = validationResult.obj; - logger.info('Successfully set built-in context property ' + path); - return result - } else { - result.valid = false; - result.msg = validationResult; - result.obj = context.builtin; - logger.error(errors.errorSettingBuiltInContext(path)); - return result; - } + context.builtin = result.obj; + logger.info('Successfully set built-in context property ' + path); + return result } else { logger.error(errors.errorSettingBuiltInContext(path)); return result; @@ -257,18 +248,12 @@ Handler.prototype.setBuiltInContextProperty = function(context, path, value) { * @param path - path to property * @returns {{}} */ -Handler.prototype.deleteBuiltInContextProperty = function(context, path) { +Handler.prototype.deleteBuiltinContextProperty = function(context, path) { const result = builtInContextUtils.deleteProperty(JSON.parse(JSON.stringify(context.builtin)), path); if(result.valid) { - const validationResult = builtInContextUtils.validateBuiltinContext(result.obj); - if(validationResult.valid) { - context.builtin = result.obj; - logger.log('Successfully deleted built-in context property ' + path); - return result; - } else { - logger.error(errors.errorDeletingBuiltInContext(path)); - return result; - } + context.builtin = result.obj; + logger.log('Successfully deleted built-in context property ' + path); + return result; } else { logger.error(errors.errorDeletingBuiltInContext(path)); return result; @@ -282,7 +267,7 @@ Handler.prototype.deleteBuiltInContextProperty = function(context, path) { * @param path * @returns {*} */ -Handler.prototype.getBuiltInContextProperty = function(context, path) { +Handler.prototype.getBuiltinContextProperty = function(context, path) { const result = builtInContextUtils.getProperty(context.builtin, path); if(result.valid) { return result; @@ -297,7 +282,7 @@ Handler.prototype.getBuiltInContextProperty = function(context, path) { * @param context - user altered built-in context * @returns {boolean} */ -Handler.prototype.validateBuiltInContext = function(context) { +Handler.prototype.validateBuiltinContext = function(context) { return builtInContextUtils.validateBuiltinContext(context.builtin); }; From e358a73cc3606ef52d5cc11772eb68a152733355 Mon Sep 17 00:00:00 2001 From: Offer Akrabi Date: Thu, 22 Nov 2018 13:51:49 +0200 Subject: [PATCH 30/31] update pakcage bersion --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 2890ff7..7afaafb 100644 --- a/package.json +++ b/package.json @@ -35,7 +35,7 @@ "watson-developer-cloud": "^3.13.0", "winston": "^2.3.1", "xregexp": "^3.1.1", - "wa-context-utils":"^0.0.2" + "wa-context-utils": "^0.0.3" }, "devDependencies": { "mocha": "^3.2.0", From 8403bffdeb6a58005e1ad1a57584ee8bbd2cd249 Mon Sep 17 00:00:00 2001 From: gaynell-gonsalves <35956168+gaynell-gonsalves@users.noreply.github.com> Date: Mon, 3 Dec 2018 12:14:59 +0530 Subject: [PATCH 31/31] Updated to v0.0.18 for WASo 29-Nov release --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 7afaafb..2d2a576 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "skill-sdk-nodejs", - "version": "0.0.17", + "version": "0.0.18", "description": "This is the SDK to be used when developing a skill for Watson Assistant Solutions using Node.js", "main": "index.js", "scripts": {