Comparing version 4.2.0 to 4.3.0
@@ -1,3 +0,1 @@ | ||
'use strict'; | ||
module.exports = { | ||
@@ -4,0 +2,0 @@ log: require('./lib/log'), |
@@ -1,3 +0,1 @@ | ||
'use strict'; | ||
module.exports = { | ||
@@ -7,2 +5,2 @@ DEFAULT_API_VERSION: '20160516', | ||
DEFAULT_WIT_URL: 'https://api.wit.ai' | ||
}; | ||
}; |
'use strict'; | ||
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 {DEFAULT_MAX_STEPS} = require('./config'); | ||
const logger = require('./log.js'); | ||
const readline = require('readline'); | ||
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({ | ||
module.exports = (wit, context) => { | ||
const rl = readline.createInterface({ | ||
input: process.stdin, | ||
output: process.stdout | ||
output: process.stdout, | ||
}); | ||
rl.setPrompt('> '); | ||
var prompt = function prompt() { | ||
const prompt = () => { | ||
rl.prompt(); | ||
rl.write(null, { ctrl: true, name: 'e' }); | ||
rl.write(null, {ctrl: true, name: 'e'}); | ||
}; | ||
prompt(); | ||
rl.on('line', function (line) { | ||
rl.on('line', (line) => { | ||
line = line.trim(); | ||
@@ -32,9 +23,9 @@ if (!line) { | ||
} | ||
wit.runActions(sessionId, line, context, steps).then(function (ctx) { | ||
context = ctx; | ||
wit.message(line, context) | ||
.then((rsp) => { | ||
console.log(JSON.stringify(rsp)); | ||
prompt(); | ||
}).catch(function (err) { | ||
return console.error(err); | ||
}); | ||
}) | ||
.catch(err => console.error(err)) | ||
}); | ||
}; | ||
}; |
'use strict'; | ||
var _funcs; | ||
const DEBUG = 'debug'; | ||
const INFO = 'info'; | ||
const WARN = 'warn'; | ||
const ERROR = 'error'; | ||
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 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 = () => {} | ||
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; | ||
const Logger = function(lvl) { | ||
this.level = lvl || INFO; | ||
levels.forEach(function (x) { | ||
var should = levels.indexOf(x) >= levels.indexOf(lvl); | ||
_this[x] = should ? funcs[x] : noop; | ||
levels.forEach((x) => { | ||
const should = levels.indexOf(x) >= levels.indexOf(lvl); | ||
this[x] = should ? funcs[x] : noop; | ||
}); | ||
}; | ||
module.exports = { Logger: Logger, DEBUG: DEBUG, INFO: INFO, WARN: WARN, ERROR: ERROR }; | ||
module.exports = { Logger, DEBUG, INFO, WARN, ERROR }; |
194
lib/wit.js
'use strict'; | ||
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 { | ||
DEFAULT_API_VERSION, | ||
DEFAULT_MAX_STEPS, | ||
DEFAULT_WIT_URL | ||
} = require('./config'); | ||
const fetch = require('isomorphic-fetch'); | ||
const log = require('./log'); | ||
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"); } }; }(); | ||
const learnMore = 'Learn more at https://wit.ai/docs/quickstart'; | ||
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)) { | ||
@@ -24,31 +18,28 @@ return new Wit(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; | ||
const { | ||
accessToken, apiVersion, actions, headers, logger, witURL | ||
} = this.config = Object.freeze(validate(opts)); | ||
this._sessions = {}; | ||
this.message = function (message, context) { | ||
var qs = 'q=' + encodeURIComponent(message); | ||
this.message = (message, context) => { | ||
let qs = 'q=' + encodeURIComponent(message); | ||
if (context) { | ||
qs += '&context=' + encodeURIComponent(JSON.stringify(context)); | ||
} | ||
var method = 'GET'; | ||
var fullURL = witURL + '/message?' + qs; | ||
var handler = makeWitResponseHandler(logger, 'message'); | ||
const method = 'GET'; | ||
const fullURL = witURL + '/message?' + qs | ||
const handler = makeWitResponseHandler(logger, 'message'); | ||
logger.debug(method, fullURL); | ||
return fetch(fullURL, { | ||
method: method, | ||
headers: headers | ||
}).then(function (response) { | ||
return Promise.all([response.json(), response.status]); | ||
}).then(handler); | ||
method, | ||
headers, | ||
}) | ||
.then(response => Promise.all([response.json(), response.status])) | ||
.then(handler) | ||
; | ||
}; | ||
this.converse = function (sessionId, message, context, reset) { | ||
var qs = 'session_id=' + encodeURIComponent(sessionId); | ||
this.converse = (sessionId, message, context, reset) => { | ||
let qs = 'session_id=' + encodeURIComponent(sessionId); | ||
if (message) { | ||
@@ -60,17 +51,18 @@ qs += '&q=' + encodeURIComponent(message); | ||
} | ||
var method = 'POST'; | ||
var fullURL = witURL + '/converse?' + qs; | ||
var handler = makeWitResponseHandler(logger, 'converse'); | ||
const method = 'POST'; | ||
const fullURL = witURL + '/converse?' + qs; | ||
const handler = makeWitResponseHandler(logger, 'converse'); | ||
logger.debug(method, fullURL); | ||
return fetch(fullURL, { | ||
method: method, | ||
headers: headers, | ||
body: JSON.stringify(context) | ||
}).then(function (response) { | ||
return Promise.all([response.json(), response.status]); | ||
}).then(handler); | ||
method, | ||
headers, | ||
body: JSON.stringify(context), | ||
}) | ||
.then(response => Promise.all([response.json(), response.status])) | ||
.then(handler) | ||
; | ||
}; | ||
var continueRunActions = function continueRunActions(sessionId, currentRequest, message, prevContext, i) { | ||
return function (json) { | ||
const continueRunActions = (sessionId, currentRequest, message, prevContext, i) => { | ||
return (json) => { | ||
if (i < 0) { | ||
@@ -80,3 +72,3 @@ logger.warn('Max steps reached, stopping.'); | ||
} | ||
if (currentRequest !== _this._sessions[sessionId]) { | ||
if (currentRequest !== this._sessions[sessionId]) { | ||
return prevContext; | ||
@@ -105,29 +97,33 @@ } | ||
var request = { | ||
sessionId: sessionId, | ||
const request = { | ||
sessionId, | ||
context: clone(prevContext), | ||
text: message, | ||
entities: json.entities | ||
entities: json.entities, | ||
}; | ||
if (json.type === 'msg') { | ||
var response = { | ||
const response = { | ||
text: json.msg, | ||
quickreplies: json.quickreplies | ||
quickreplies: json.quickreplies, | ||
}; | ||
return runAction(actions, 'send', request, response).then(function (ctx) { | ||
return runAction(actions, 'send', request, response).then(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') { | ||
return runAction(actions, json.action, request).then(function (ctx) { | ||
var nextContext = ctx || {}; | ||
if (currentRequest !== _this._sessions[sessionId]) { | ||
return runAction(actions, json.action, request).then(ctx => { | ||
const 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,7 +137,5 @@ } else { | ||
this.runActions = function (sessionId, message, context, maxSteps) { | ||
var _this2 = this; | ||
this.runActions = function(sessionId, message, context, maxSteps) { | ||
if (!actions) throwMustHaveActions(); | ||
var steps = maxSteps ? maxSteps : DEFAULT_MAX_STEPS; | ||
const steps = maxSteps ? maxSteps : DEFAULT_MAX_STEPS; | ||
// Figuring out whether we need to reset the last turn. | ||
@@ -151,7 +145,7 @@ // Each new call increments an index for the session. | ||
// All the previous ones are discarded (preemptive exit). | ||
var currentRequest = (this._sessions[sessionId] || 0) + 1; | ||
const currentRequest = (this._sessions[sessionId] || 0) + 1; | ||
this._sessions[sessionId] = currentRequest; | ||
var cleanup = function cleanup(ctx) { | ||
if (currentRequest === _this2._sessions[sessionId]) { | ||
delete _this2._sessions[sessionId]; | ||
const cleanup = ctx => { | ||
if (currentRequest === this._sessions[sessionId]) { | ||
delete this._sessions[sessionId]; | ||
} | ||
@@ -161,9 +155,11 @@ 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); | ||
}; | ||
}; | ||
var makeWitResponseHandler = function makeWitResponseHandler(logger, endpoint) { | ||
return function (rsp) { | ||
var error = function error(err) { | ||
const makeWitResponseHandler = (logger, endpoint) => { | ||
return rsp => { | ||
const error = err => { | ||
logger.error('[' + endpoint + '] Error: ' + err); | ||
@@ -177,5 +173,3 @@ throw err; | ||
var _rsp = _slicedToArray(rsp, 2), | ||
json = _rsp[0], | ||
status = _rsp[1]; | ||
const [json, status] = rsp; | ||
@@ -186,3 +180,3 @@ if (json instanceof Error) { | ||
var err = json.error || status !== 200 && json.body + ' (' + status + ')'; | ||
const err = json.error || status !== 200 && json.body + ' (' + status + ')'; | ||
@@ -195,10 +189,10 @@ if (err) { | ||
return json; | ||
}; | ||
} | ||
}; | ||
var throwMustHaveActions = function throwMustHaveActions() { | ||
throw new Error('You must provide the `actions` parameter to be able to use runActions. ' + learnMore); | ||
const throwMustHaveActions = () => { | ||
throw new Error('You must provide the `actions` parameter to be able to use runActions. ' + learnMore) | ||
}; | ||
var throwIfActionMissing = function throwIfActionMissing(actions, action) { | ||
const throwIfActionMissing = (actions, action) => { | ||
if (!actions[action]) { | ||
@@ -209,12 +203,8 @@ throw new Error('No \'' + action + '\' action found.'); | ||
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]; | ||
} | ||
const runAction = (actions, name, ...rest) => { | ||
throwIfActionMissing(actions, name); | ||
return Promise.resolve(actions[name].apply(actions, rest)); | ||
return Promise.resolve(actions[name](...rest)); | ||
}; | ||
var validate = function validate(opts) { | ||
const validate = (opts) => { | ||
if (!opts.accessToken) { | ||
@@ -228,6 +218,7 @@ 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', | ||
}; | ||
opts.logger = opts.logger || new log.Logger(log.INFO); | ||
if (opts.actions) { | ||
opts.logger.warn('Stories and POST /converse have been deprecated. This will break in February 2018!'); | ||
opts.actions = validateActions(opts.logger, opts.actions); | ||
@@ -239,4 +230,4 @@ } | ||
var validateActions = function validateActions(logger, actions) { | ||
if ((typeof actions === 'undefined' ? 'undefined' : _typeof(actions)) !== 'object') { | ||
const validateActions = (logger, actions) => { | ||
if (typeof actions !== 'object') { | ||
throw new Error('Actions should be an object. ' + learnMore); | ||
@@ -248,3 +239,3 @@ } | ||
Object.keys(actions).forEach(function (key) { | ||
Object.keys(actions).forEach(key => { | ||
if (typeof actions[key] !== 'function') { | ||
@@ -254,3 +245,6 @@ 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); | ||
@@ -271,18 +265,12 @@ } | ||
var clone = function clone(obj) { | ||
if (obj !== null && (typeof obj === 'undefined' ? 'undefined' : _typeof(obj)) === 'object') { | ||
const clone = (obj) => { | ||
if (obj !== null && typeof obj === 'object') { | ||
if (Array.isArray(obj)) { | ||
return obj.map(clone); | ||
} else { | ||
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; | ||
const newObj = {}; | ||
Object.keys(obj).forEach(k => { | ||
newObj[k] = clone(obj[k]); | ||
}); | ||
return newObj; | ||
} | ||
@@ -294,2 +282,2 @@ } else { | ||
module.exports = Wit; | ||
module.exports = Wit; |
{ | ||
"name": "node-wit", | ||
"version": "4.2.0", | ||
"version": "4.3.0", | ||
"description": "Wit.ai Node.js SDK", | ||
@@ -16,3 +16,3 @@ "keywords": [ | ||
"scripts": { | ||
"test": "mocha ./tests/lib.js" | ||
"test": "mocha --timeout 10000 ./tests/lib.js" | ||
}, | ||
@@ -19,0 +19,0 @@ "repository": "https://github.com/wit-ai/node-wit", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 4 instances in 1 package
Non-permissive License
License(Experimental) A license not known to be considered permissive was found.
Found 1 instance in 1 package
Unidentified License
License(Experimental) Something that seems like a license was found, but its contents could not be matched with a known license.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
No README
QualityPackage does not have a README. This may indicate a failed publish or a low quality package.
Found 1 instance in 1 package
No License Found
License(Experimental) License information could not be found.
Found 1 instance in 1 package
109739
22
777
1
189
2
80
7