-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.js
264 lines (201 loc) · 8.2 KB
/
index.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
"use strict";
const EventEmitter = require("events");
const NodeSDK = require("./modules/sdk");
const Logger = require("./modules/logger");
const Queue = require("./modules/queue");
const Work = require("./modules/work");
const pkg = require("./package.json");
const Tags = require("./modules/tags");
const Works = require("./modules/works");
const Delayer = require("./modules/delayer");
const Factory = require("./modules/plugins/factory");
const LOG_ID = "CHATBOT - ";
class RainbowAgent {
constructor(nodeSDK, tags, options) {
process.on("uncaughtException", (err) => {
console.error(err);
});
process.on("warning", (err) => {
console.error(err);
});
process.on("unhandledRejection", (err) => {
console.error(err);
});
// Initialize component
this.options = options;
this.logger = Logger;
this.events = new EventEmitter();
this.queue = new Queue();
this.tags = new Tags(tags);
this.sdk = new NodeSDK(nodeSDK);
this.works = new Works(this.sdk);
this.delayer = new Delayer();
this.factory = new Factory();
// Callback for onMessage()
this._callbackMessage = null;
this._contextMessage = null;
// Callback for onTicket()
this._callbackTicket = null;
this._contextTicket = null;
this._isEnabled = true;
return this;
}
enable() {
this._isEnabled = true;
this.logger.log("warn", LOG_ID + "enable() - Mode is enabled");
}
disable() {
this._isEnabled = false;
this.logger.log("warn", LOG_ID + "disable() - Mode is disabled");
this.works.purge();
}
get state() {
return this._isEnabled;
}
start() {
let that = this;
this.logger.log("debug", LOG_ID + "------------------------------------------------");
this.logger.log("debug", LOG_ID + "welcome() - Welcome to Rainbow ChatBot");
this.logger.log("info", LOG_ID + "welcome() - v" + pkg.version);
this.logger.log("info", LOG_ID + "start() - Start services...");
this.sdk.start(this.events, this.logger).then(() => {
}).then(() => {
return that.delayer.start(that.events, that.logger);
}).then(() => {
return that.factory.start(that.events, that.logger);
}).then(() => {
return that.queue.start(that.events, that.logger, that.options);
}).then(() => {
return that.tags.start(that.events, that.logger);
}).then(() => {
return that.works.start(that.events, that.logger, that.factory);
}).then(() => {
that.logger.log("info", LOG_ID + "start() - All services started successfully!");
that.logger.log("info", LOG_ID + "start() - Ready to listen incoming requests...");
that.addPostListener();
}).catch((err) => {
that.logger.log("error", LOG_ID + "start() - Error starting " + err);
});
}
onMessage(func, context) {
this._callbackMessage = func;
this._contextMessage = context;
}
onTicket(func, context) {
this._callbackTicket = func;
this._contextTicket = context;
}
fireEvent(work, message) {
let that = this;
return new Promise((resolve) => {
if(!that._callbackMessage) {
resolve();
} else {
let content = message ? message.value : "";
return that._callbackMessage.call(that._contextMessage, work.tag, work.step, content, work.from, resolve);
}
});
}
fireTicketEvent(work) {
this._callbackTicket.call(this._contextTicket, work.tag, work.history, work.from, work.createdOn, work.endedOn, work.state, work.id);
}
addPostListener() {
let that = this;
// Listen message from users (new tasks or answers)
this.events.on("onmessagereceived", (msg) => {
let work = null;
if (this._isEnabled) {
// Qualify message (check tag)
let scenario = that.tags.qualify(msg);
// Get work if exists
work = that.works.getWork(msg, scenario);
// Add to queue if work
if(!work) {
that.logger.log("warn", LOG_ID + "onmessagereceived() - Incorrect message received");
return;
}
if(work.queued) {
that.logger.log("warn", LOG_ID + "onmessagereceived() - Existing work is running. No user input expected...");
return;
}
// Store message if scenario is inProgress
if(work.state === Work.STATE.INPROGRESS) {
if(!this.factory.isValid(work, work.scenario[work.step], msg.value)) {
return;
}
work.historize(msg);
}
that.fireEvent(work, msg).then((routedStep) => {
if(routedStep) {
// force next step
work.forcedNextStep = routedStep;
}
if(work.waiting) {
that.delayer.delay(work);
} else {
that.queue.addToQueue(work);
}
});
} else {
that.logger.log("warn", LOG_ID + "onmessagereceived() - Input not taken into account. Mode is disabled...");
}
});
// Listen when work has finished a task
this.events.on("ontaskfinished", (work) => {
if (work.state !== Work.STATE.CLOSED &&
work.state !== Work.STATE.BLOCKED &&
work.state !== Work.STATE.ABORTED &&
!work.pending) {
if(that._isEnabled) {
if(work.external) {
that.fireEvent(work, null).then((routedStep) => {
work.external = false;
if(routedStep) {
// force next step
work.forcedNextStep = routedStep;
}
if(work.waiting) {
that.delayer.delay(work);
} else {
that.queue.addToQueue(work);
}
});
} else {
if(work.waiting) {
that.delayer.delay(work);
} else {
that.queue.addToQueue(work);
}
}
}
else {
that.logger.log("warn", LOG_ID + "onmessagereceived() - Input not taken into account. Mode is disabled...");
}
}
else {
if(work.pending) {
that.logger.log("info", LOG_ID + "ontaskfinished() - Work[" + work.id + "] is waiting for incoming inputs...");
}
else {
that.logger.log("info", LOG_ID + "ontaskfinished() - Work [" + work.id + "] is closed, blocked or aborted");
work.endedOn = new Date();
that.fireTicketEvent(work);
}
}
});
this.events.on('ontaskexternal', (work) => {
that.fireEvent(work, null).then((routedStep) => {
if(routedStep) {
// force next step
work.forcedNextStep = routedStep;
}
if(work.waiting) {
that.delayer.delay(work);
} else {
that.queue.addToQueue(work);
}
});
});
}
}
module.exports = RainbowAgent;