Skip to content

Commit

Permalink
Merge pull request #299 from chughts/discrud
Browse files Browse the repository at this point in the history
Passages for Discovery
  • Loading branch information
chughts committed Jun 4, 2017
2 parents ea499a8 + 8180160 commit 2377e9a
Show file tree
Hide file tree
Showing 9 changed files with 224 additions and 128 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ Node-RED Watson Nodes for IBM Bluemix

<a href="https://cla-assistant.io/watson-developer-cloud/node-red-node-watson"><img src="https://cla-assistant.io/readme/badge/watson-developer-cloud/node-red-node-watson" alt="CLA assistant" /></a>

### New in version 0.5.8
- Visual Reconition Node, now accepts readstream on msg.payload
- Add passages parameter to Discovery Node

### New in version 0.5.7
- Fix to Tone Analyzer to allow JSON as input
- Enabled Conversation Tone method to Tone Analyzer Node
Expand Down
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "node-red-node-watson",
"version": "0.5.7",
"version": "0.5.8",
"description": "A collection of Node-RED nodes for IBM Watson services",
"dependencies": {
"alchemy-api": "^1.3.0",
Expand All @@ -11,9 +11,10 @@
"temp": "^0.8.3",
"qs": "6.x",
"image-type": "^2.0.2",
"watson-developer-cloud": "^2.31.2",
"watson-developer-cloud": "^2.32.1",
"kuromoji": "^0.0.5",
"is-docx": "^0.0.3"
"is-docx": "^0.0.3",
"stream-to-array" : "^2.3.0"
},
"repository": {
"type": "git",
Expand Down
62 changes: 44 additions & 18 deletions services/conversation/v1.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@
* limitations under the License.
**/

module.exports = function (RED) {
module.exports = function(RED) {
const SERVICE_IDENTIFIER = 'conversation';
var pkg = require('../../package.json'),
ConversationV1 = require('watson-developer-cloud/conversation/v1'),
serviceutils = require('../../utilities/service-utils'),
service = null, sUsername = null, sPassword = null;
service = null,
sUsername = null,
sPassword = null;

service = serviceutils.getServiceCreds(SERVICE_IDENTIFIER);

Expand All @@ -28,13 +30,19 @@ module.exports = function (RED) {
sPassword = service.password;
}

RED.httpAdmin.get('/watson-conversation/vcap', function (req, res) {
res.json(service ? {bound_service: true} : null);
RED.httpAdmin.get('/watson-conversation/vcap', function(req, res) {
res.json(service ? {
bound_service: true
} : null);
});

function verifyPayload(node, msg) {
if (!msg.payload) {
node.status({fill:'red', shape:'ring', text:'missing payload'});
node.status({
fill: 'red',
shape: 'ring',
text: 'missing payload'
});
node.error('Missing property: msg.payload', msg);
return false;
}
Expand All @@ -57,11 +65,13 @@ module.exports = function (RED) {

function verifyInputs(node, msg, config, params) {
if (!config.workspaceid && !msg.params.workspace_id) {
node.error('Missing workspace_id. check node documentation.',msg);
node.error('Missing workspace_id. check node documentation.', msg);
return false;
}
// mandatory message
params.input = {text:msg.payload};
params.input = {
text: msg.payload
};
var prop = null;

if (config.context) {
Expand Down Expand Up @@ -127,9 +137,13 @@ module.exports = function (RED) {
var userName = sUsername || node.credentials.username,
passWord = sPassword || node.credentials.password;

if ( !(userName || msg.params.username) ||
!(passWord || msg.params.password) ) {
node.status({fill:'red', shape:'ring', text:'missing credentials'});
if (!(userName || msg.params.username) ||
!(passWord || msg.params.password)) {
node.status({
fill: 'red',
shape: 'ring',
text: 'missing credentials'
});
node.error('Missing Watson Conversation API service credentials', msg);
return false;
}
Expand Down Expand Up @@ -157,8 +171,11 @@ module.exports = function (RED) {
function processResponse(err, body, node, msg, config) {
if (err !== null && body === null) {
node.error(err);
node.status({fill:'red', shape:'ring',
text:'call to watson conversation service failed'});
node.status({
fill: 'red',
shape: 'ring',
text: 'call to watson conversation service failed'
});
return;
}
msg.payload = body;
Expand All @@ -179,20 +196,25 @@ module.exports = function (RED) {
}

function execute(params, node, msg, config) {
node.status({fill:'blue', shape:'dot' , text:'Calling Conversation service ...'});
node.status({
fill: 'blue',
shape: 'dot',
text: 'Calling Conversation service ...'
});
// call POST /message through SDK
node.service.message(params, function(err, body) {
processResponse(err, body, node, msg, config);
});
}

// This is the Watson Conversation V1 (GA) Node
function WatsonConversationV1Node (config) {
var node = this, b = false;
function WatsonConversationV1Node(config) {
var node = this,
b = false;

RED.nodes.createNode(this, config);

node.on('input', function (msg) {
node.on('input', function(msg) {
var params = {};

node.status({});
Expand All @@ -215,8 +237,12 @@ module.exports = function (RED) {

RED.nodes.registerType('watson-conversation-v1', WatsonConversationV1Node, {
credentials: {
username: {type:'text'},
password: {type:'password'}
username: {
type: 'text'
},
password: {
type: 'password'
}
}
});
};
44 changes: 24 additions & 20 deletions services/discovery/discovery-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,13 @@ DiscoveryUtils.prototype = {

buildParamsForPayload: function(msg, config, params) {
var isJSON = this.isJsonString(msg.payload) ||
this.isJsonObject(msg.payload);
this.isJsonObject(msg.payload);

// Payload (text to be analysed) must be a string (content is either raw string or Buffer)
if (typeof msg.payload === 'string' || isJSON) {
params.file = this.isJsonObject(msg.payload) ?
JSON.stringify(msg.payload) :
msg.payload;
JSON.stringify(msg.payload) :
msg.payload;
}
return params;
},
Expand All @@ -69,13 +69,14 @@ DiscoveryUtils.prototype = {

params = this.buildParamsForName(msg, config, params);

['environment_id','collection_id','configuration_id',
['environment_id', 'collection_id', 'configuration_id',
'collection_name',
'query','description','size'].forEach(function(f) {
'query', 'passages', 'description', 'size'
].forEach(function(f) {
params = me.buildParamsFor(msg, config, params, f);
});

['count','filter','aggregation','return'].forEach(function(f) {
['count', 'filter', 'aggregation', 'return'].forEach(function(f) {
params = me.buildParamsFromConfig(config, params, f);
});

Expand Down Expand Up @@ -107,51 +108,54 @@ DiscoveryUtils.prototype = {
}
params.query += config.query3 + ':"' + config.queryvalue3 + '"';
}
if (config.passages) {
params.passages = config.passages;
}

return params;
},

paramEnvCheck: function (params) {
paramEnvCheck: function(params) {
var response = '';
if (!params.environment_id) {
response = 'Missing Environment ID ';
}
return response;
},

paramJSONCheck: function (params) {
paramJSONCheck: function(params) {
var response = '';
if (!params.file) {
response = 'Missing JSON file on payload';
}
return response;
},

paramNameCheck: function (params) {
paramNameCheck: function(params) {
var response = '';
if (!params.name) {
response = 'Missing Name ';
}
return response;
},

paramDescriptionCheck: function (params) {
paramDescriptionCheck: function(params) {
var response = '';
if (!params.description) {
response = 'Missing Description ';
}
return response;
},

paramCollectionCheck: function (params) {
paramCollectionCheck: function(params) {
var response = '';
if (!params.collection_id) {
response = 'Missing Collection ID ';
}
return response;
},

paramConfigurationCheck: function (params) {
paramConfigurationCheck: function(params) {
var response = '';
if (!params.configuration_id) {
response = 'Missing Configuration ID ';
Expand Down Expand Up @@ -184,7 +188,7 @@ DiscoveryUtils.prototype = {
},

// sorting functions
uniqueFilter: function (value, index, self) {
uniqueFilter: function(value, index, self) {
return self.indexOf(value) === index;
},

Expand All @@ -194,8 +198,8 @@ DiscoveryUtils.prototype = {
if ('object' === typeof schemaData) {
for (var k in schemaData) {
if ('results' === k &&
'object' === typeof schemaData[k] &&
'object' === typeof schemaData[k][0]) {
'object' === typeof schemaData[k] &&
'object' === typeof schemaData[k][0]) {
fields = this.buildFieldByStep(schemaData[k][0], fields, '');
}
}
Expand All @@ -206,11 +210,11 @@ DiscoveryUtils.prototype = {
return fields;
},

// reportError: function (node, msg, message) {
// var messageTxt = message.error ? message.error : message;
// node.status({fill:'red', shape:'dot', text: messageTxt});
// node.error(message, msg);
// } ,
// reportError: function (node, msg, message) {
// var messageTxt = message.error ? message.error : message;
// node.status({fill:'red', shape:'dot', text: messageTxt});
// node.error(message, msg);
// } ,

isJsonString: function(str) {
try {
Expand Down
22 changes: 20 additions & 2 deletions services/discovery/v1-query-builder.html
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,15 @@
<input type="hidden" id="node-input-queryvalue3hidden"/>
</div>

<div class="form-row">
<label>&nbsp;</label>
<input type="checkbox" id="node-input-passages" style="display: inline-block; width: auto; vertical-align: top;">
<label for="node-input-passages" style="width: 70%;"> Passages</label>
</div>
<div>
<input type="hidden" id="node-input-passageshidden"/>
</div>

</script>

<script type="text/x-red" data-help-name="watson-discovery-v1-query-builder">
Expand Down Expand Up @@ -140,7 +149,7 @@
disQB.query1_selected = $('#node-input-query1hidden').val();
disQB.query2_selected = $('#node-input-query2hidden').val();
disQB.query3_selected = $('#node-input-query3hidden').val();

disQB.passages_selected = $('#node-input-passageshidden').val();

// Nothing is to be shown unless the values to show in the listCollections
// can be obtained from the service.
Expand Down Expand Up @@ -510,6 +519,10 @@

disQB.queryvalue3_selected = $('#node-input-queryvalue3hidden').val();
$('select#node-input-queryvalue3').val(disQB.queryvalue3_selected);

disQB.passages_selected = $('#node-input-passageshidden').val();
$('inpu#node-input-passages').val(disQB.passages_selected);

}


Expand Down Expand Up @@ -583,6 +596,9 @@

disQB.queryvalue3_selected = $('#node-input-queryvalue3').val();
$('#node-input-queryvalue3hidden').val(disQB.queryvalue3_selected);

disQB.passages_selected = $('#node-input-passages').val();
$('#node-input-passageshidden').val(disQB.passages_selected);
}

(function() {
Expand All @@ -606,7 +622,9 @@
query3: {value: ''},
query3hidden: {value: ''},
queryvalue3: {value: ''},
queryvalue3hidden: {value: ''}
queryvalue3hidden: {value: ''},
passages: {value: false},
passageshidden: {value: false}
},
credentials: {
username: {type:"text"} //,
Expand Down
Loading

0 comments on commit 2377e9a

Please sign in to comment.