Skip to content

Commit

Permalink
[wit] support for different environments (wit-ai#71)
Browse files Browse the repository at this point in the history
  • Loading branch information
stopachka authored and blandinw committed Aug 9, 2016
1 parent db45e57 commit 1dc3f28
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 41 deletions.
5 changes: 3 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
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')
}
5 changes: 5 additions & 0 deletions lib/config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = {
DEFAULT_API_VERSION: '20160516',
DEFAULT_MAX_STEPS: 5,
DEFAULT_WIT_URL: 'https://api.wit.ai'
};
33 changes: 33 additions & 0 deletions lib/interactive.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
const {DEFAULT_MAX_STEPS} = require('./config');
const logger = require('./log.js');
const readline = require('readline');
const uuid = require('node-uuid');

module.exports = (wit, initContext, maxSteps) => {
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'});
};
prompt();
rl.on('line', (line) => {
line = line.trim();
if (!line) {
return prompt();
}
wit.runActions(sessionId, line, context, steps)
.then((ctx) => {
context = ctx;
prompt();
})
.catch(err => console.error(err))
});
};
46 changes: 8 additions & 38 deletions lib/wit.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
'use strict';

const fetch = require('node-fetch');
const readline = require('readline');
const uuid = require('node-uuid');
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 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';

function Wit(opts) {
Expand Down Expand Up @@ -127,35 +128,6 @@ function Wit(opts) {
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'});
};
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)
});
};
};

const makeWitResponseHandler = (logger, endpoint) => {
Expand Down Expand Up @@ -263,6 +235,4 @@ const clone = (obj) => {
}
};

module.exports = {
Wit,
};
module.exports = Wit;
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@
"repository": "https://github.com/wit-ai/node-wit",
"author": "The Wit Team <[email protected]>",
"dependencies": {
"isomorphic-fetch": "^2.2.1",
"node-fetch": "^1.5.1",
"node-uuid": "^1.4.7"
},
"engines": {
"node" : ">=4.0.0"
"node": ">=4.0.0"
}
}

0 comments on commit 1dc3f28

Please sign in to comment.