Comparing version 4.0.0 to 4.1.0
@@ -0,1 +1,11 @@ | ||
## v4.1.0 | ||
- Support for different JS environments | ||
- `converse` now takes `reset` as an optional parameter | ||
### Breaking changes | ||
- `interactive` is no longer a function on the `Wit` client. Instead, you require it from the library: `require('node-wit').interactive` | ||
- `runActions` now resets the last turn on new messages and errors. | ||
## v4.0.0 | ||
@@ -2,0 +12,0 @@ |
'use strict'; | ||
let Wit = null; | ||
let interactive = null; | ||
try { | ||
// if running from repo | ||
Wit = require('../').Wit; | ||
interactive = require('../').interactive; | ||
} catch (e) { | ||
Wit = require('node-wit').Wit; | ||
interactive = require('node-wit').interactive; | ||
} | ||
@@ -32,2 +35,2 @@ | ||
const client = new Wit({accessToken, actions}); | ||
client.interactive(); | ||
interactive(client); |
'use strict'; | ||
let Wit = null; | ||
let interactive = null; | ||
try { | ||
// if running from repo | ||
Wit = require('../').Wit; | ||
interactive = require('../').interactive; | ||
} catch (e) { | ||
Wit = require('node-wit').Wit; | ||
interactive = require('node-wit').interactive; | ||
} | ||
const accessToken = (() => { | ||
@@ -87,2 +91,2 @@ if (process.argv.length !== 3) { | ||
const client = new Wit({accessToken, actions}); | ||
client.interactive(); | ||
interactive(client); |
@@ -40,4 +40,2 @@ 'use strict'; | ||
// Messenger API parameters | ||
const FB_PAGE_ID = process.env.FB_PAGE_ID; | ||
if (!FB_PAGE_ID) { throw new Error('missing FB_PAGE_ID') } | ||
const FB_PAGE_TOKEN = process.env.FB_PAGE_TOKEN; | ||
@@ -44,0 +42,0 @@ if (!FB_PAGE_TOKEN) { throw new Error('missing FB_PAGE_TOKEN') } |
'use strict'; | ||
let Wit = null; | ||
let interactive = null; | ||
try { | ||
// if running from repo | ||
Wit = require('../').Wit; | ||
interactive = require('../').interactive; | ||
} catch (e) { | ||
Wit = require('node-wit').Wit; | ||
interactive = require('node-wit').interactive; | ||
} | ||
@@ -48,2 +51,3 @@ | ||
context.forecast = 'sunny in ' + location; // we should call a weather API here | ||
delete context.missingLocation; | ||
} else { | ||
@@ -59,2 +63,2 @@ context.missingLocation = true; | ||
const client = new Wit({accessToken, actions}); | ||
client.interactive(); | ||
interactive(client); |
module.exports = { | ||
log: require('./lib/log.js'), | ||
Wit: require('./lib/wit.js').Wit, | ||
log: require('./lib/log'), | ||
Wit: require('./lib/wit'), | ||
interactive: require('./lib/interactive') | ||
} |
'use strict'; | ||
const fetch = require('node-fetch'); | ||
const readline = require('readline'); | ||
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'); | ||
const log = require('./log'); | ||
const DEFAULT_API_VERSION = '20160516'; | ||
const DEFAULT_MAX_STEPS = 5; | ||
const DEFAULT_WIT_URL = 'https://api.wit.ai'; | ||
const learnMore = 'Learn more at https://wit.ai/docs/quickstart'; | ||
@@ -22,2 +23,4 @@ | ||
this._sessions = {}; | ||
this.message = (message, context) => { | ||
@@ -41,3 +44,3 @@ let qs = 'q=' + encodeURIComponent(message); | ||
this.converse = (sessionId, message, context) => { | ||
this.converse = (sessionId, message, context, reset) => { | ||
let qs = 'session_id=' + encodeURIComponent(sessionId); | ||
@@ -47,2 +50,5 @@ if (message) { | ||
} | ||
if (reset) { | ||
qs += '&reset=true'; | ||
} | ||
const method = 'POST'; | ||
@@ -62,3 +68,3 @@ const fullURL = witURL + '/converse?' + qs; | ||
const continueRunActions = (sessionId, message, prevContext, i) => { | ||
const continueRunActions = (sessionId, currentRequest, message, prevContext, i) => { | ||
return (json) => { | ||
@@ -69,2 +75,5 @@ if (i < 0) { | ||
} | ||
if (currentRequest !== this._sessions[sessionId]) { | ||
return prevContext; | ||
} | ||
if (!json.type) { | ||
@@ -77,3 +86,3 @@ throw new Error('Couldn\'t find type in Wit response'); | ||
// backwards-cpmpatibility with API version 20160516 | ||
// backwards-compatibility with API version 20160516 | ||
if (json.type === 'merge') { | ||
@@ -105,61 +114,51 @@ json.type = 'action'; | ||
}; | ||
return actions.send(request, response).then((ctx) => { | ||
return actions.send(request, response).then(ctx => { | ||
if (ctx) { | ||
throw new Error('Cannot update context after \'send\' action'); | ||
} | ||
if (currentRequest !== this._sessions[sessionId]) { | ||
return ctx; | ||
} | ||
return this.converse(sessionId, null, prevContext).then( | ||
continueRunActions(sessionId, message, prevContext, i - 1) | ||
continueRunActions(sessionId, currentRequest, message, prevContext, i - 1) | ||
); | ||
}); | ||
} else if (json.type === 'action') { | ||
const action = json.action; | ||
throwIfActionMissing(actions, action); | ||
return actions[action](request).then((ctx) => { | ||
throwIfActionMissing(actions, json.action); | ||
return 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, message, nextContext, i - 1) | ||
continueRunActions(sessionId, currentRequest, message, nextContext, i - 1) | ||
); | ||
}); | ||
} else { | ||
logger.debug('unknown response type', json); | ||
logger.debug('unknown response type ' + json.type); | ||
throw new Error('unknown response type ' + json.type); | ||
} | ||
} | ||
}; | ||
}; | ||
this.runActions = (sessionId, message, context, maxSteps) => { | ||
this.runActions = function(sessionId, message, context, maxSteps) { | ||
if (!actions) throwMustHaveActions(); | ||
const steps = maxSteps ? maxSteps : DEFAULT_MAX_STEPS; | ||
return this.converse(sessionId, message, context).then( | ||
continueRunActions(sessionId, message, context, steps) | ||
); | ||
}; | ||
this.interactive = (initContext, maxSteps) => { | ||
if (!actions) throwMustHaveActions(); | ||
let context = typeof initContext === 'object' ? initContext : {}; | ||
const sessionId = uuid.v1(); | ||
const steps = maxSteps ? maxSteps : DEFAULT_MAX_STEPS; | ||
const rl = readline.createInterface({ | ||
input: process.stdin, | ||
output: process.stdout, | ||
}); | ||
rl.setPrompt('> '); | ||
const prompt = () => { | ||
rl.prompt(); | ||
rl.write(null, {ctrl: true, name: 'e'}); | ||
// Figuring out whether we need to reset the last turn. | ||
// Each new call increments an index for the session. | ||
// We only care about the last call to runActions. | ||
// All the previous ones are discarded (preemptive exit). | ||
const currentRequest = (this._sessions[sessionId] || 0) + 1; | ||
this._sessions[sessionId] = currentRequest; | ||
const cleanup = ctx => { | ||
if (currentRequest === this._sessions[sessionId]) { | ||
delete this._sessions[sessionId]; | ||
} | ||
return ctx; | ||
}; | ||
prompt(); | ||
rl.on('line', (line) => { | ||
line = line.trim(); | ||
if (!line) { | ||
return prompt(); | ||
} | ||
this.runActions(sessionId, line, context, steps) | ||
.then((ctx) => { | ||
context = ctx; | ||
prompt(); | ||
}) | ||
.catch(logger.error) | ||
}); | ||
return this.converse(sessionId, message, context, currentRequest > 1).then( | ||
continueRunActions(sessionId, currentRequest, message, context, steps) | ||
).then(cleanup); | ||
}; | ||
@@ -273,4 +272,2 @@ }; | ||
module.exports = { | ||
Wit, | ||
}; | ||
module.exports = Wit; |
{ | ||
"name": "node-wit", | ||
"version": "4.0.0", | ||
"version": "4.1.0", | ||
"description": "Wit.ai Node.js SDK", | ||
@@ -21,8 +21,8 @@ "keywords": [ | ||
"dependencies": { | ||
"node-fetch": "^1.5.1", | ||
"isomorphic-fetch": "^2.2.1", | ||
"node-uuid": "^1.4.7" | ||
}, | ||
"engines": { | ||
"node" : ">=4.0.0" | ||
"node": ">=4.0.0" | ||
} | ||
} |
@@ -36,4 +36,5 @@ # Wit Node.js SDK [![npm](https://img.shields.io/npm/v/node-wit.svg)](https://www.npmjs.com/package/node-wit) | ||
* `runActions` - a higher-level method to the Wit converse API | ||
* `interactive` - starts an interactive conversation with your bot | ||
You can also require a library function to test out your bot in the terminal. `require('node-wit').interactive` | ||
### Wit class | ||
@@ -113,2 +114,3 @@ | ||
A higher-level method to the Wit converse API. | ||
`runActions` resets the last turn on new messages and errors. | ||
@@ -149,2 +151,3 @@ Takes the following parameters: | ||
* `context` - the object representing the session state | ||
* `reset` - (optional) whether to reset the last turn | ||
@@ -166,3 +169,4 @@ Example: | ||
```js | ||
client.interactive(); | ||
const {interactive} = require('node-wit'); | ||
interactive(client); | ||
``` | ||
@@ -169,0 +173,0 @@ |
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
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
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 1 instance in 1 package
32968
14
686
212
5
3
+ Addedisomorphic-fetch@^2.2.1
+ Addedisomorphic-fetch@2.2.1(transitive)
+ Addedwhatwg-fetch@3.6.20(transitive)
- Removednode-fetch@^1.5.1