@indra.ai/deva 1.1.15 → 1.1.17
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/examples/hello-world.js +7 -5
- package/index.js +82 -64
- package/package.json +1 -1
package/examples/hello-world.js
CHANGED
|
@@ -5,6 +5,7 @@ const Deva = require('../index');
|
|
|
5
5
|
const HelloWorld = new Deva({
|
|
6
6
|
client: {
|
|
7
7
|
id: 100,
|
|
8
|
+
key: 'hello',
|
|
8
9
|
},
|
|
9
10
|
agent: {
|
|
10
11
|
id: 101,
|
|
@@ -37,11 +38,11 @@ const HelloWorld = new Deva({
|
|
|
37
38
|
hello: 'Hello World'
|
|
38
39
|
},
|
|
39
40
|
listeners: {
|
|
40
|
-
'
|
|
41
|
-
console.log(
|
|
41
|
+
'hello:state'(packet) {
|
|
42
|
+
console.log(packet.state);
|
|
42
43
|
},
|
|
43
|
-
'
|
|
44
|
-
console.log(
|
|
44
|
+
'prompt'(text) {
|
|
45
|
+
console.log(text);
|
|
45
46
|
}
|
|
46
47
|
},
|
|
47
48
|
deva: {},
|
|
@@ -65,7 +66,8 @@ const HelloWorld = new Deva({
|
|
|
65
66
|
HelloWorld.init().then(done => {
|
|
66
67
|
return HelloWorld.question('/state how are you')
|
|
67
68
|
}).then(answer => {
|
|
68
|
-
console.log(
|
|
69
|
+
// console.log(answer);
|
|
70
|
+
console.log(answer.a.text);
|
|
69
71
|
});
|
|
70
72
|
|
|
71
73
|
|
package/index.js
CHANGED
|
@@ -9,6 +9,30 @@ class Deva {
|
|
|
9
9
|
opts = opts || {};
|
|
10
10
|
this._id = randomUUID(); // the unique id assigned to the agent at load
|
|
11
11
|
this._state = 'OFFLINE'; // current state of agent.
|
|
12
|
+
this._active = false; // the active/birth date.
|
|
13
|
+
this.security = false; // inherited Security features.
|
|
14
|
+
this.support = false; // inherited Support features.
|
|
15
|
+
this.config = opts.config || {}; // local Config Object
|
|
16
|
+
this.events = opts.events || new EventEmitter({}); // Event Bus
|
|
17
|
+
this.lib = opts.lib || {}; // used for loading library functions
|
|
18
|
+
this.agent = opts.agent || false; // Agent profile object
|
|
19
|
+
this.client = opts.client || false; // Client profile object
|
|
20
|
+
this.devas = opts.devas || {}; // Devas which are loaded
|
|
21
|
+
this.vars = opts.vars || {}; // Variables object
|
|
22
|
+
this.listeners = opts.listeners || {}; // local Listeners
|
|
23
|
+
this.modules = opts.modules || {}; // 3rd Party Modules
|
|
24
|
+
this.func = opts.func || {}; // local Functions
|
|
25
|
+
this.methods = opts.methods || {}; // local Methods
|
|
26
|
+
this.maxListeners = opts.maxListenners || 0; // set the local maxListeners
|
|
27
|
+
|
|
28
|
+
for (var opt in opts) {
|
|
29
|
+
if (!this[opt]) this[opt] = opts[opt]; // set any remaining opts to this.
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
this.cmdChr = '/';
|
|
33
|
+
this.askChr = '#';
|
|
34
|
+
this.inherit = ["events", "config", "lib", "security", "client"];
|
|
35
|
+
this.bind = ["listeners", "methods", "func", "lib", "security", "agent", "client"];
|
|
12
36
|
this._states = { // The available states to work with.
|
|
13
37
|
offline: '👻 OFFLINE',
|
|
14
38
|
online: '📡 ONLINE',
|
|
@@ -29,12 +53,13 @@ class Deva {
|
|
|
29
53
|
data: '📀 DATA',
|
|
30
54
|
ask: '🙋♀️ ASK',
|
|
31
55
|
cmd: '📟 COMMAND',
|
|
32
|
-
question: '
|
|
33
|
-
question_ask: '
|
|
34
|
-
question_cmd: '
|
|
35
|
-
|
|
36
|
-
ask_question: '
|
|
37
|
-
ask_answer: '
|
|
56
|
+
question: '🐵 QUESTION',
|
|
57
|
+
question_ask: '🧙♂️ QUESTION ASK',
|
|
58
|
+
question_cmd: '🪄 QUESTION CMD',
|
|
59
|
+
question_answer: '🔮 QUESTION ANSWER',
|
|
60
|
+
ask_question: '👽 ASK QUESTION',
|
|
61
|
+
ask_answer: '🛸 ASK ANSWER',
|
|
62
|
+
method_not_found: '😩 METHOD NOT FOUND',
|
|
38
63
|
talk: '🎙️ TALK',
|
|
39
64
|
listen: '🎧 LISTEN',
|
|
40
65
|
error: '❌ ERROR',
|
|
@@ -46,35 +71,18 @@ class Deva {
|
|
|
46
71
|
systems: '👽 SYSTEMS',
|
|
47
72
|
solutions: '🔬 SOLUTIONS',
|
|
48
73
|
};
|
|
49
|
-
this.
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
this.func = opts.func || {}; // local Functions
|
|
62
|
-
this.methods = opts.methods || {}; // local Methods
|
|
63
|
-
this.maxListeners = opts.maxListenners || 0; // set the local maxListeners
|
|
64
|
-
|
|
65
|
-
for (var opt in opts) {
|
|
66
|
-
if (!this[opt]) this[opt] = opts[opt]; // set any remaining opts to this.
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
this.cmdChr = '/';
|
|
70
|
-
this.askChr = '#';
|
|
71
|
-
this.inherit = ["events", "config", "lib", "security", "client"];
|
|
72
|
-
this.bind = ["listeners", "methods", "func", "lib", "security", "agent", "client"];
|
|
73
|
-
this.messages = {
|
|
74
|
-
offline: 'AGENT OFFLINE',
|
|
75
|
-
stopped: 'DEVAS STOPPED',
|
|
76
|
-
notext: 'NO TEXT',
|
|
77
|
-
notfound: 'NOT FOUND',
|
|
74
|
+
this._messages = {
|
|
75
|
+
offline: '🙅♂️ DEVA OFFLINE',
|
|
76
|
+
init: '✅ DEVA INITIALIZED',
|
|
77
|
+
start: '✅ DEVA STARTED',
|
|
78
|
+
stop: '💥 DEVA STOPPED',
|
|
79
|
+
enter: '🖖 DEVA ENTERED',
|
|
80
|
+
exit: '🚪 DEVA EXITED',
|
|
81
|
+
done: '👍 DEVA DONE',
|
|
82
|
+
devas_started: '🤝 DEVAS STARTED',
|
|
83
|
+
devas_stopped: '🛑 DEVAS STOPPED',
|
|
84
|
+
notext: '❌ NO TEXT',
|
|
85
|
+
method_not_found: '❌ THAT IS NOT GONNA WORK!',
|
|
78
86
|
}
|
|
79
87
|
}
|
|
80
88
|
|
|
@@ -89,9 +97,24 @@ class Deva {
|
|
|
89
97
|
describe
|
|
90
98
|
***************/
|
|
91
99
|
state(st, data=false) {
|
|
100
|
+
if (!Object.keys(this._states).includes(st)) return;
|
|
92
101
|
this._state = this._states[st];
|
|
102
|
+
const _data = {
|
|
103
|
+
id: this.uid(true),
|
|
104
|
+
client: this.client.id,
|
|
105
|
+
agent: this.agent.id,
|
|
106
|
+
st: st,
|
|
107
|
+
state: this._state,
|
|
108
|
+
data,
|
|
109
|
+
created: Date.now(),
|
|
110
|
+
};
|
|
93
111
|
this.prompt(this._state);
|
|
94
|
-
this.talk(`${this.agent.key}:state`,
|
|
112
|
+
this.talk(`${this.agent.key}:state`, _data);
|
|
113
|
+
return this._state;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
states() {
|
|
117
|
+
return this._states;
|
|
95
118
|
}
|
|
96
119
|
|
|
97
120
|
// Called from the init function to bind the elements defined in the this.bind variable.
|
|
@@ -140,12 +163,6 @@ class Deva {
|
|
|
140
163
|
_assignListeners() {
|
|
141
164
|
return new Promise((resolve, reject) => {
|
|
142
165
|
try {
|
|
143
|
-
// set the default listeners for the states of the agent.
|
|
144
|
-
for (let state in this._states) {
|
|
145
|
-
this.events.on(`${this.agent.key}:${state}`, packet => {
|
|
146
|
-
return this[state](packet);
|
|
147
|
-
})
|
|
148
|
-
}
|
|
149
166
|
// set the assigned listeners for the agent.
|
|
150
167
|
for (let listener in this.listeners) {
|
|
151
168
|
this.events.on(listener, packet => {
|
|
@@ -208,13 +225,14 @@ class Deva {
|
|
|
208
225
|
packet.a = {
|
|
209
226
|
agent: this.agent || false,
|
|
210
227
|
client: this.client || false,
|
|
211
|
-
text: `${packet.q.meta.method}
|
|
228
|
+
text: `${this._messages.method_not_found} - ${packet.q.meta.method} `,
|
|
212
229
|
meta: {
|
|
213
230
|
key: this.agent.key,
|
|
214
231
|
method: packet.q.meta.method,
|
|
215
232
|
},
|
|
216
233
|
created: Date.now(),
|
|
217
234
|
};
|
|
235
|
+
this.state('method_not_found', packet);
|
|
218
236
|
return packet;
|
|
219
237
|
}
|
|
220
238
|
|
|
@@ -339,7 +357,7 @@ class Deva {
|
|
|
339
357
|
so the event is specific to the talk.
|
|
340
358
|
***************/
|
|
341
359
|
ask(packet) {
|
|
342
|
-
if (!this._active) return Promise.resolve(this.
|
|
360
|
+
if (!this._active) return Promise.resolve(this._messages.offline);
|
|
343
361
|
this.state('ask_question', packet);
|
|
344
362
|
|
|
345
363
|
packet.a = {
|
|
@@ -400,7 +418,7 @@ class Deva {
|
|
|
400
418
|
describe:
|
|
401
419
|
***************/
|
|
402
420
|
question(TEXT=false, DATA=false) {
|
|
403
|
-
if (!this._active) return Promise.resolve(this.
|
|
421
|
+
if (!this._active) return Promise.resolve(this._messages.offline);
|
|
404
422
|
|
|
405
423
|
const id = this.uid(); // generate a unique transport id for the question.
|
|
406
424
|
const t_split = TEXT.split(' ');
|
|
@@ -427,9 +445,9 @@ class Deva {
|
|
|
427
445
|
key = this.agent.key;
|
|
428
446
|
|
|
429
447
|
return new Promise((resolve, reject) => {
|
|
430
|
-
if (!TEXT) return reject(this.
|
|
448
|
+
if (!TEXT) return reject(this._messages.notext);
|
|
431
449
|
try {
|
|
432
|
-
if (!this._active) return reject(this.
|
|
450
|
+
if (!this._active) return reject(this._messages.offline);
|
|
433
451
|
|
|
434
452
|
// *: send just a string of text
|
|
435
453
|
// !: send a command to the local agent
|
|
@@ -440,7 +458,7 @@ class Deva {
|
|
|
440
458
|
let _state = 'question';
|
|
441
459
|
if (isAsk) {
|
|
442
460
|
_state = 'question_ask'
|
|
443
|
-
key = t_split[0]substring(1);
|
|
461
|
+
key = t_split[0].substring(1);
|
|
444
462
|
params = t_split[1] ? t_split[1].split(':') : false;
|
|
445
463
|
method = params[0];
|
|
446
464
|
text = t_split.slice(2).join(' ').trim();
|
|
@@ -448,7 +466,7 @@ class Deva {
|
|
|
448
466
|
else if (isCmd) {
|
|
449
467
|
_state = 'question_cmd'
|
|
450
468
|
params = t_split[1] ? t_split[1].split(':') : false;
|
|
451
|
-
method =
|
|
469
|
+
method = t_split[0].substring(1);
|
|
452
470
|
text = t_split.slice(1).join(' ').trim()
|
|
453
471
|
}
|
|
454
472
|
|
|
@@ -503,6 +521,7 @@ class Deva {
|
|
|
503
521
|
|
|
504
522
|
// create a hash for entire packet and insert into packet
|
|
505
523
|
packet.hash = this.hash(JSON.stringify(packet));
|
|
524
|
+
this.state('question_answer', packet);
|
|
506
525
|
|
|
507
526
|
return resolve(packet);
|
|
508
527
|
}).catch(err => {
|
|
@@ -541,7 +560,7 @@ class Deva {
|
|
|
541
560
|
return this._assignListeners();
|
|
542
561
|
}).then(() => {
|
|
543
562
|
this.state('init');
|
|
544
|
-
return this.onInit && typeof this.onInit === 'function' ? this.onInit() : this.start(this.
|
|
563
|
+
return this.onInit && typeof this.onInit === 'function' ? this.onInit() : this.start(this._messages.init);
|
|
545
564
|
}).then(started => {
|
|
546
565
|
return resolve(started)
|
|
547
566
|
}).catch(err => {
|
|
@@ -556,7 +575,7 @@ class Deva {
|
|
|
556
575
|
// packet: the packet that caused the error.
|
|
557
576
|
error(err,packet=false,reject=false) {
|
|
558
577
|
this.state('error', err);
|
|
559
|
-
if (this.onError && typeof this.onError === 'function')
|
|
578
|
+
if (this.onError && typeof this.onError === 'function') return this.onError(err, packet, reject);
|
|
560
579
|
console.error(err)
|
|
561
580
|
return reject ? reject(err) : err;
|
|
562
581
|
}
|
|
@@ -572,7 +591,7 @@ class Deva {
|
|
|
572
591
|
start() {
|
|
573
592
|
if (!this._active) return;
|
|
574
593
|
this.state('start');
|
|
575
|
-
return this.onStart && typeof this.onStart === 'function' ? this.onStart() : this.enter(this.
|
|
594
|
+
return this.onStart && typeof this.onStart === 'function' ? this.onStart() : this.enter(this._messages.start);
|
|
576
595
|
}
|
|
577
596
|
|
|
578
597
|
/**************
|
|
@@ -586,10 +605,10 @@ class Deva {
|
|
|
586
605
|
If the deva is offline it will return the offline message.
|
|
587
606
|
***************/
|
|
588
607
|
stop() {
|
|
589
|
-
if (!this._active) return Promise.resolve(this.
|
|
608
|
+
if (!this._active) return Promise.resolve(this._messages.offline);
|
|
590
609
|
this.state('stop');
|
|
591
610
|
this._active = false;
|
|
592
|
-
return this.onStop && typeof this.onStop === 'function' ? this.onStop() : this.exit(this.
|
|
611
|
+
return this.onStop && typeof this.onStop === 'function' ? this.onStop() : this.exit(this._messages.stop);
|
|
593
612
|
}
|
|
594
613
|
|
|
595
614
|
/**************
|
|
@@ -602,9 +621,9 @@ class Deva {
|
|
|
602
621
|
If the Deva is offline it will return the offline message.
|
|
603
622
|
***************/
|
|
604
623
|
enter() {
|
|
605
|
-
if (!this._active) return Promise.resolve(this.
|
|
624
|
+
if (!this._active) return Promise.resolve(this._messages.offline);
|
|
606
625
|
this.state('enter');
|
|
607
|
-
return this.onEnter && typeof this.onEnter === 'function' ? this.onEnter() : this.done(this.
|
|
626
|
+
return this.onEnter && typeof this.onEnter === 'function' ? this.onEnter() : this.done(this._messages.enter)
|
|
608
627
|
}
|
|
609
628
|
|
|
610
629
|
/**************
|
|
@@ -620,10 +639,10 @@ class Deva {
|
|
|
620
639
|
If the deva is offline it will return the offline message.
|
|
621
640
|
***************/
|
|
622
641
|
exit() {
|
|
623
|
-
if (!this._active) return Promise.resolve(this.
|
|
642
|
+
if (!this._active) return Promise.resolve(this._messages.offline);
|
|
624
643
|
this.state('exit');
|
|
625
644
|
this._active = false;
|
|
626
|
-
return this.onExit && typeof this.onExit === 'function' ? this.onExit() : Promise.resolve(this.
|
|
645
|
+
return this.onExit && typeof this.onExit === 'function' ? this.onExit() : Promise.resolve(this._messages.exit)
|
|
627
646
|
}
|
|
628
647
|
|
|
629
648
|
/**************
|
|
@@ -637,10 +656,10 @@ class Deva {
|
|
|
637
656
|
If the deva is offline it will return the offline message.
|
|
638
657
|
***************/
|
|
639
658
|
done(msg=false) {
|
|
640
|
-
if (!this._active) return Promise.resolve(this.
|
|
659
|
+
if (!this._active) return Promise.resolve(this._messages.offline);
|
|
641
660
|
this.state('done');
|
|
642
661
|
msg = msg ? msg : this._state;
|
|
643
|
-
return this.onDone && typeof this.onDone === 'function' ? this.onDone() : Promise.resolve(this.
|
|
662
|
+
return this.onDone && typeof this.onDone === 'function' ? this.onDone() : Promise.resolve(this._messages.done)
|
|
644
663
|
}
|
|
645
664
|
|
|
646
665
|
/**************
|
|
@@ -654,7 +673,7 @@ class Deva {
|
|
|
654
673
|
If the deva is offline it will return the offline message.
|
|
655
674
|
***************/
|
|
656
675
|
status(addto=false) {
|
|
657
|
-
if (!this._active) return Promise.resolve(this.
|
|
676
|
+
if (!this._active) return Promise.resolve(this._messages.offline);
|
|
658
677
|
const id = this.uid();
|
|
659
678
|
const dateFormat = new Intl.DateTimeFormat('en-US', { dateStyle: 'medium', timeStyle: 'medium' }).format(this._active);
|
|
660
679
|
let text = `${this.agent.name} is ONLINE since ${dateFormat}`;
|
|
@@ -697,14 +716,13 @@ class Deva {
|
|
|
697
716
|
startDevas() {
|
|
698
717
|
this.state('devas_start');
|
|
699
718
|
return new Promise((resolve, reject) => {
|
|
700
|
-
this.prompt
|
|
701
719
|
const devas = [];
|
|
702
720
|
for (let x in this.devas) {
|
|
703
721
|
devas.push(this.devas[x].init());
|
|
704
722
|
}
|
|
705
723
|
Promise.all(devas).then(() => {
|
|
706
724
|
this.state('devas_ready');
|
|
707
|
-
return resolve(this.
|
|
725
|
+
return resolve(this._messages.devas_started);
|
|
708
726
|
}).catch(reject);
|
|
709
727
|
});
|
|
710
728
|
}
|
|
@@ -723,7 +741,7 @@ class Deva {
|
|
|
723
741
|
}
|
|
724
742
|
Promise.all(devas).then(() => {
|
|
725
743
|
this.state('devas_stoped');
|
|
726
|
-
return resolve(this.
|
|
744
|
+
return resolve(this._messages.devas_stopped);
|
|
727
745
|
}).catch(reject);
|
|
728
746
|
});
|
|
729
747
|
}
|