Socket
Socket
Sign inDemoInstall

node-wit

Package Overview
Dependencies
8
Maintainers
6
Versions
30
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 4.1.0 to 4.2.0

4

index.js

@@ -0,1 +1,3 @@

'use strict';
module.exports = {

@@ -5,2 +7,2 @@ log: require('./lib/log'),

interactive: require('./lib/interactive')
}
};

@@ -0,1 +1,3 @@

'use strict';
module.exports = {

@@ -5,2 +7,2 @@ DEFAULT_API_VERSION: '20160516',

DEFAULT_WIT_URL: 'https://api.wit.ai'
};
};

@@ -1,22 +0,28 @@

const {DEFAULT_MAX_STEPS} = require('./config');
const logger = require('./log.js');
const readline = require('readline');
const uuid = require('node-uuid');
'use strict';
module.exports = (wit, initContext, maxSteps) => {
let context = typeof initContext === 'object' ? initContext : {};
const sessionId = uuid.v1();
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
const steps = maxSteps ? maxSteps : DEFAULT_MAX_STEPS;
const rl = readline.createInterface({
var _require = require('./config'),
DEFAULT_MAX_STEPS = _require.DEFAULT_MAX_STEPS;
var logger = require('./log.js');
var readline = require('readline');
var uuid = require('uuid');
module.exports = function (wit, initContext, maxSteps) {
var context = (typeof initContext === 'undefined' ? 'undefined' : _typeof(initContext)) === 'object' ? initContext : {};
var sessionId = uuid.v1();
var steps = maxSteps ? maxSteps : DEFAULT_MAX_STEPS;
var rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
output: process.stdout
});
rl.setPrompt('> ');
const prompt = () => {
var prompt = function prompt() {
rl.prompt();
rl.write(null, {ctrl: true, name: 'e'});
rl.write(null, { ctrl: true, name: 'e' });
};
prompt();
rl.on('line', (line) => {
rl.on('line', function (line) {
line = line.trim();

@@ -26,9 +32,9 @@ if (!line) {

}
wit.runActions(sessionId, line, context, steps)
.then((ctx) => {
wit.runActions(sessionId, line, context, steps).then(function (ctx) {
context = ctx;
prompt();
})
.catch(err => console.error(err))
}).catch(function (err) {
return console.error(err);
});
});
};
};
'use strict';
const DEBUG = 'debug';
const INFO = 'info';
const WARN = 'warn';
const ERROR = 'error';
var _funcs;
const levels = [DEBUG, INFO, WARN, ERROR];
const funcs = {
[DEBUG]: console.error.bind(console, '[wit][debug]'),
[INFO]: console.log.bind(console, '[wit]'),
[WARN]: console.warn.bind(console, '[wit]'),
[ERROR]: console.error.bind(console, '[wit]'),
};
const noop = () => {}
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
const Logger = function(lvl) {
var DEBUG = 'debug';
var INFO = 'info';
var WARN = 'warn';
var ERROR = 'error';
var levels = [DEBUG, INFO, WARN, ERROR];
var funcs = (_funcs = {}, _defineProperty(_funcs, DEBUG, console.error.bind(console, '[wit][debug]')), _defineProperty(_funcs, INFO, console.log.bind(console, '[wit]')), _defineProperty(_funcs, WARN, console.warn.bind(console, '[wit]')), _defineProperty(_funcs, ERROR, console.error.bind(console, '[wit]')), _funcs);
var noop = function noop() {};
var Logger = function Logger(lvl) {
var _this = this;
this.level = lvl || INFO;
levels.forEach((x) => {
const should = levels.indexOf(x) >= levels.indexOf(lvl);
this[x] = should ? funcs[x] : noop;
levels.forEach(function (x) {
var should = levels.indexOf(x) >= levels.indexOf(lvl);
_this[x] = should ? funcs[x] : noop;
});
};
module.exports = { Logger, DEBUG, INFO, WARN, ERROR };
module.exports = { Logger: Logger, DEBUG: DEBUG, INFO: INFO, WARN: WARN, ERROR: ERROR };
'use strict';
const {
DEFAULT_API_VERSION,
DEFAULT_MAX_STEPS,
DEFAULT_WIT_URL
} = require('./config');
const fetch = require('isomorphic-fetch');
const log = require('./log');
const uuid = require('node-uuid');
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
const learnMore = 'Learn more at https://wit.ai/docs/quickstart';
var _slicedToArray = function () { function sliceIterator(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"]) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } return function (arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { return sliceIterator(arr, i); } else { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } }; }();
var _require = require('./config'),
DEFAULT_API_VERSION = _require.DEFAULT_API_VERSION,
DEFAULT_MAX_STEPS = _require.DEFAULT_MAX_STEPS,
DEFAULT_WIT_URL = _require.DEFAULT_WIT_URL;
var fetch = require('isomorphic-fetch');
var log = require('./log');
var learnMore = 'Learn more at https://wit.ai/docs/quickstart';
function Wit(opts) {
var _this = this;
if (!(this instanceof Wit)) {

@@ -19,28 +24,31 @@ return new Wit(opts);

const {
accessToken, apiVersion, actions, headers, logger, witURL
} = this.config = Object.freeze(validate(opts));
var _config = this.config = Object.freeze(validate(opts)),
accessToken = _config.accessToken,
apiVersion = _config.apiVersion,
actions = _config.actions,
headers = _config.headers,
logger = _config.logger,
witURL = _config.witURL;
this._sessions = {};
this.message = (message, context) => {
let qs = 'q=' + encodeURIComponent(message);
this.message = function (message, context) {
var qs = 'q=' + encodeURIComponent(message);
if (context) {
qs += '&context=' + encodeURIComponent(JSON.stringify(context));
}
const method = 'GET';
const fullURL = witURL + '/message?' + qs
const handler = makeWitResponseHandler(logger, 'message');
var method = 'GET';
var fullURL = witURL + '/message?' + qs;
var handler = makeWitResponseHandler(logger, 'message');
logger.debug(method, fullURL);
return fetch(fullURL, {
method,
headers,
})
.then(response => Promise.all([response.json(), response.status]))
.then(handler)
;
method: method,
headers: headers
}).then(function (response) {
return Promise.all([response.json(), response.status]);
}).then(handler);
};
this.converse = (sessionId, message, context, reset) => {
let qs = 'session_id=' + encodeURIComponent(sessionId);
this.converse = function (sessionId, message, context, reset) {
var qs = 'session_id=' + encodeURIComponent(sessionId);
if (message) {

@@ -52,18 +60,17 @@ qs += '&q=' + encodeURIComponent(message);

}
const method = 'POST';
const fullURL = witURL + '/converse?' + qs;
const handler = makeWitResponseHandler(logger, 'converse');
var method = 'POST';
var fullURL = witURL + '/converse?' + qs;
var handler = makeWitResponseHandler(logger, 'converse');
logger.debug(method, fullURL);
return fetch(fullURL, {
method,
headers,
body: JSON.stringify(context),
})
.then(response => Promise.all([response.json(), response.status]))
.then(handler)
;
method: method,
headers: headers,
body: JSON.stringify(context)
}).then(function (response) {
return Promise.all([response.json(), response.status]);
}).then(handler);
};
const continueRunActions = (sessionId, currentRequest, message, prevContext, i) => {
return (json) => {
var continueRunActions = function continueRunActions(sessionId, currentRequest, message, prevContext, i) {
return function (json) {
if (i < 0) {

@@ -73,3 +80,3 @@ logger.warn('Max steps reached, stopping.');

}
if (currentRequest !== this._sessions[sessionId]) {
if (currentRequest !== _this._sessions[sessionId]) {
return prevContext;

@@ -98,36 +105,29 @@ }

const request = {
sessionId,
var request = {
sessionId: sessionId,
context: clone(prevContext),
text: message,
entities: json.entities,
entities: json.entities
};
if (json.type === 'msg') {
throwIfActionMissing(actions, 'send');
const response = {
var response = {
text: json.msg,
quickreplies: json.quickreplies,
quickreplies: json.quickreplies
};
return actions.send(request, response).then(ctx => {
return runAction(actions, 'send', request, response).then(function (ctx) {
if (ctx) {
throw new Error('Cannot update context after \'send\' action');
}
if (currentRequest !== this._sessions[sessionId]) {
if (currentRequest !== _this._sessions[sessionId]) {
return ctx;
}
return this.converse(sessionId, null, prevContext).then(
continueRunActions(sessionId, currentRequest, message, prevContext, i - 1)
);
return _this.converse(sessionId, null, prevContext).then(continueRunActions(sessionId, currentRequest, message, prevContext, i - 1));
});
} else if (json.type === 'action') {
throwIfActionMissing(actions, json.action);
return actions[json.action](request).then(ctx => {
const nextContext = ctx || {};
if (currentRequest !== this._sessions[sessionId]) {
return runAction(actions, json.action, request).then(function (ctx) {
var nextContext = ctx || {};
if (currentRequest !== _this._sessions[sessionId]) {
return nextContext;
}
return this.converse(sessionId, null, nextContext).then(
continueRunActions(sessionId, currentRequest, message, nextContext, i - 1)
);
return _this.converse(sessionId, null, nextContext).then(continueRunActions(sessionId, currentRequest, message, nextContext, i - 1));
});

@@ -141,6 +141,7 @@ } else {

this.runActions = function(sessionId, message, context, maxSteps) {
this.runActions = function (sessionId, message, context, maxSteps) {
var _this2 = this;
if (!actions) throwMustHaveActions();
const steps = maxSteps ? maxSteps : DEFAULT_MAX_STEPS;
var steps = maxSteps ? maxSteps : DEFAULT_MAX_STEPS;
// Figuring out whether we need to reset the last turn.

@@ -150,7 +151,7 @@ // Each new call increments an index for the session.

// All the previous ones are discarded (preemptive exit).
const currentRequest = (this._sessions[sessionId] || 0) + 1;
var currentRequest = (this._sessions[sessionId] || 0) + 1;
this._sessions[sessionId] = currentRequest;
const cleanup = ctx => {
if (currentRequest === this._sessions[sessionId]) {
delete this._sessions[sessionId];
var cleanup = function cleanup(ctx) {
if (currentRequest === _this2._sessions[sessionId]) {
delete _this2._sessions[sessionId];
}

@@ -160,11 +161,9 @@ return ctx;

return this.converse(sessionId, message, context, currentRequest > 1).then(
continueRunActions(sessionId, currentRequest, message, context, steps)
).then(cleanup);
return this.converse(sessionId, message, context, currentRequest > 1).then(continueRunActions(sessionId, currentRequest, message, context, steps)).then(cleanup);
};
};
const makeWitResponseHandler = (logger, endpoint) => {
return rsp => {
const error = err => {
var makeWitResponseHandler = function makeWitResponseHandler(logger, endpoint) {
return function (rsp) {
var error = function error(err) {
logger.error('[' + endpoint + '] Error: ' + err);

@@ -178,3 +177,5 @@ throw err;

const [json, status] = rsp;
var _rsp = _slicedToArray(rsp, 2),
json = _rsp[0],
status = _rsp[1];

@@ -185,3 +186,3 @@ if (json instanceof Error) {

const err = json.error || status !== 200 && json.body + ' (' + status + ')';
var err = json.error || status !== 200 && json.body + ' (' + status + ')';

@@ -194,10 +195,10 @@ if (err) {

return json;
}
};
};
const throwMustHaveActions = () => {
throw new Error('You must provide the `actions` parameter to be able to use runActions. ' + learnMore)
var throwMustHaveActions = function throwMustHaveActions() {
throw new Error('You must provide the `actions` parameter to be able to use runActions. ' + learnMore);
};
const throwIfActionMissing = (actions, action) => {
var throwIfActionMissing = function throwIfActionMissing(actions, action) {
if (!actions[action]) {

@@ -208,3 +209,12 @@ throw new Error('No \'' + action + '\' action found.');

const validate = (opts) => {
var runAction = function runAction(actions, name) {
for (var _len = arguments.length, rest = Array(_len > 2 ? _len - 2 : 0), _key = 2; _key < _len; _key++) {
rest[_key - 2] = arguments[_key];
}
throwIfActionMissing(actions, name);
return Promise.resolve(actions[name].apply(actions, rest));
};
var validate = function validate(opts) {
if (!opts.accessToken) {

@@ -218,3 +228,3 @@ throw new Error('Could not find access token, learn more at https://wit.ai/docs');

'Accept': 'application/vnd.wit.' + opts.apiVersion + '+json',
'Content-Type': 'application/json',
'Content-Type': 'application/json'
};

@@ -229,4 +239,4 @@ opts.logger = opts.logger || new log.Logger(log.INFO);

const validateActions = (logger, actions) => {
if (typeof actions !== 'object') {
var validateActions = function validateActions(logger, actions) {
if ((typeof actions === 'undefined' ? 'undefined' : _typeof(actions)) !== 'object') {
throw new Error('Actions should be an object. ' + learnMore);

@@ -238,3 +248,3 @@ }

Object.keys(actions).forEach(key => {
Object.keys(actions).forEach(function (key) {
if (typeof actions[key] !== 'function') {

@@ -244,6 +254,3 @@ logger.warn('The \'' + key + '\' action should be a function.');

if (key === 'say' && actions[key].length > 2 ||
key === 'merge' && actions[key].length > 2 ||
key === 'error' && actions[key].length > 2
) {
if (key === 'say' && actions[key].length > 2 || key === 'merge' && actions[key].length > 2 || key === 'error' && actions[key].length > 2) {
logger.warn('The \'' + key + '\' action has been deprecated. ' + learnMore);

@@ -264,12 +271,18 @@ }

const clone = (obj) => {
if (obj !== null && typeof obj === 'object') {
var clone = function clone(obj) {
if (obj !== null && (typeof obj === 'undefined' ? 'undefined' : _typeof(obj)) === 'object') {
if (Array.isArray(obj)) {
return obj.map(clone);
} else {
const newObj = {};
Object.keys(obj).forEach(k => {
newObj[k] = clone(obj[k]);
});
return newObj;
var _ret = function () {
var newObj = {};
Object.keys(obj).forEach(function (k) {
newObj[k] = clone(obj[k]);
});
return {
v: newObj
};
}();
if ((typeof _ret === 'undefined' ? 'undefined' : _typeof(_ret)) === "object") return _ret.v;
}

@@ -281,2 +294,2 @@ } else {

module.exports = Wit;
module.exports = Wit;
{
"name": "node-wit",
"version": "4.1.0",
"version": "4.2.0",
"description": "Wit.ai Node.js SDK",

@@ -16,3 +16,3 @@ "keywords": [

"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
"test": "mocha ./tests/lib.js"
},

@@ -23,7 +23,15 @@ "repository": "https://github.com/wit-ai/node-wit",

"isomorphic-fetch": "^2.2.1",
"node-uuid": "^1.4.7"
"uuid": "^3.0.0"
},
"engines": {
"node": ">=4.0.0"
},
"devDependencies": {
"babel-cli": "^6.16.0",
"babel-preset-es2015": "^6.16.0",
"babel-preset-stage-0": "^6.16.0",
"chai": "^3.5.0",
"mocha": "^3.1.2",
"sinon": "^1.17.6"
}
}
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc