forked from kengz/aiva
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.js
84 lines (74 loc) · 2.09 KB
/
client.js
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
// The client for js; imports all js modules49
const _ = require('lomath')
const path = require('path')
const requireDir = require('require-dir')
const socketIOClient = require('socket.io-client')
const log = require('../src/log')
let libJs
let client
const ioid = 'js' // the id of this script for io client registration
// log.info(libJs.ai.nlp.POS.getPOS({input: "remind me to do laundry"}))
// correct the reply JSON
/* istanbul ignore next */
function correctReply(reply, msg) {
let cReply = reply
if (!_.isPlainObject(cReply)) {
cReply = { output: cReply }
}
// autofill if not already exist
cReply.to = cReply.to || msg.from
cReply.from = cReply.from || ioid
cReply.hash = cReply.hash || msg.hash
return cReply
}
// 1. Register the socket.io client
// 2. Write module methods and register as handlers
// done in your module scripts
// 3. listener to handle incoming payload.
/**
* The handle for client.on('take') on getting replies
* @param {JSON} msg Replied msg
*/
/* istanbul ignore next */
function handle(msg) {
const to = msg.to
const intent = msg.intent
if (to && intent) {
let reply
// try JSON or JSON.input as input
try {
reply = _.get(libJs[to], intent)(msg)
} catch (err) {
try {
reply = _.get(libJs[to], intent)(msg.input)
} catch (deepErr) {
log.debug('js handle fails.', deepErr)
}
} finally {
// try JSON or made-JSON output
reply = correctReply(reply, msg)
if (reply.to) {
client.emit('pass', reply)
}
}
}
}
/* istanbul ignore next */
function join() {
// import all in lib/js/, called only when the modules are needed
libJs = requireDir(path.join(__dirname, 'js'))
log.debug('import js lib from client.js')
const IOPORT = process.env.IOPORT || 6466
log.info(`Starting socketIO client for js at ${IOPORT}`)
client = socketIOClient(`http://localhost:${IOPORT}`)
client.emit('join', ioid)
client.on('disconnect', client.disconnect)
client.on('take', handle)
}
module.exports = {
join,
}
/* istanbul ignore next */
if (require.main === module) {
join()
}