-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathinteractive.js
More file actions
executable file
·35 lines (32 loc) · 865 Bytes
/
interactive.js
File metadata and controls
executable file
·35 lines (32 loc) · 865 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
'use strict';
const {DEFAULT_MAX_STEPS} = require('./config');
const logger = require('./log.js');
const readline = require('readline');
const uuid = require('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))
});
};