From 9b8b5dc1ed16a9696e172f367aa6c3e341d4942d Mon Sep 17 00:00:00 2001 From: chughts Date: Fri, 7 Feb 2020 20:10:37 +0000 Subject: [PATCH] Allow any string as input identifier for node controlled session ids --- README.md | 4 ++++ package.json | 2 +- services/assistant/v2.html | 5 ++++- services/assistant/v2.js | 36 ++++++++++++++++++++++++++++-------- 4 files changed, 37 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 6b5accd8..417c85ff 100644 --- a/README.md +++ b/README.md @@ -7,6 +7,10 @@ Node-RED Watson Nodes for IBM Cloud CLA assistant + +### New in version 0.9.1 +- Assistant V2 - Allow flow to assign a string session id. The node maps this user specified session id to the real session id. Additional param option allow session id to be reset. + ### New in version 0.9.0 - Node-RED & IBM-Watson & Use of promises on API invocation & IAM URL construct migration & Removal of default endpoint of - Assistant V1 diff --git a/package.json b/package.json index dfc706c0..f906da51 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "node-red-node-watson", - "version": "0.9.0", + "version": "0.9.1", "description": "A collection of Node-RED nodes for IBM Watson services", "dependencies": { "async": "^1.5.2", diff --git a/services/assistant/v2.html b/services/assistant/v2.html index 3a8c603d..f4676453 100644 --- a/services/assistant/v2.html +++ b/services/assistant/v2.html @@ -112,7 +112,10 @@ If this field is not provided then the node will generate a new session_id and return it as part of the response. If the node is not used in multi-session mode, then a session_id need not be provided, to reset the session_id in single-session mode - send a null value as the session_id. Format: String + send a null value as the session_id. + Format: String +
  • msg.params.reset_session (optional): Will force a session reset + Format: Any
  • msg.params.assistant_id : unique identifier of the assistant to be used. Could be also configured in the node. Format: String
  • msg.params.timeout (optional): The timeout period (in millisecond) for Watson request. Leave empty or set to 0 to disable.
  • msg.params.alternate_intents (optional) : whether to return more than one intent. Default is false. Set to true to return all matching intents. For example, return all intents when the confidence is not high to allow users to choose their intent.
  • diff --git a/services/assistant/v2.js b/services/assistant/v2.js index 6790ee45..35c31bac 100644 --- a/services/assistant/v2.js +++ b/services/assistant/v2.js @@ -95,14 +95,27 @@ module.exports = function(RED) { function setSessionID(msg) { let session_id = null; + let id = null; - if (!config.multisession) { - let id = node.context().flow.get('session_id'); - if (id) { - session_id = id; + if (msg.params && 'reset_session' in msg.params) { + // There is a reset request, therefore no need + // to look it up + } else { + if (!config.multisession) { + if (msg.params && 'session_id' in msg.params && !msg.params.session_id) { + // Have a session id in single session mode + // but its a null string so force a reset of the session. + } else { + id = node.context().flow.get('session_id'); + } + } else if (msg.params && msg.params.session_id) { + let key = msg.params.session_id; + id = node.context().flow.get('session-' + key); } - } else if (msg.params && msg.params.session_id) { - session_id = msg.params.session_id; + } + + if (id) { + session_id = id; } return session_id; @@ -265,7 +278,8 @@ module.exports = function(RED) { return Promise.resolve(); } - function checkSession(params) { + + function checkSession(msg, params) { return new Promise(function resolver(resolve, reject){ if (params.sessionId) { resolve(); @@ -280,6 +294,12 @@ module.exports = function(RED) { if (params.sessionId) { if (!config.multisession) { node.context().flow.set('session_id', params.sessionId); + } else { + let key = params.sessionId; + if (msg.params && msg.params.session_id){ + key = msg.params.session_id; + } + node.context().flow.set('session-' + key, params.sessionId); } resolve(); } else { @@ -333,7 +353,7 @@ module.exports = function(RED) { return buildService(settings); }) .then(function(){ - return checkSession(params); + return checkSession(msg, params); }) .then(function(){ node.status({ fill: 'blue', shape: 'dot', text: 'Calling Assistant service ...'});