@indra.ai/deva 1.1.15 → 1.1.16
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 -65
- 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,23 @@ 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];
|
|
93
|
-
|
|
94
|
-
|
|
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
|
+
};
|
|
111
|
+
this.talk(`${this.agent.key}:state`, _data);
|
|
112
|
+
return this._state;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
states() {
|
|
116
|
+
return this._states;
|
|
95
117
|
}
|
|
96
118
|
|
|
97
119
|
// Called from the init function to bind the elements defined in the this.bind variable.
|
|
@@ -140,12 +162,6 @@ class Deva {
|
|
|
140
162
|
_assignListeners() {
|
|
141
163
|
return new Promise((resolve, reject) => {
|
|
142
164
|
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
165
|
// set the assigned listeners for the agent.
|
|
150
166
|
for (let listener in this.listeners) {
|
|
151
167
|
this.events.on(listener, packet => {
|
|
@@ -208,13 +224,14 @@ class Deva {
|
|
|
208
224
|
packet.a = {
|
|
209
225
|
agent: this.agent || false,
|
|
210
226
|
client: this.client || false,
|
|
211
|
-
text: `${packet.q.meta.method}
|
|
227
|
+
text: `${this._messages.method_not_found} - ${packet.q.meta.method} `,
|
|
212
228
|
meta: {
|
|
213
229
|
key: this.agent.key,
|
|
214
230
|
method: packet.q.meta.method,
|
|
215
231
|
},
|
|
216
232
|
created: Date.now(),
|
|
217
233
|
};
|
|
234
|
+
this.state('method_not_found', packet);
|
|
218
235
|
return packet;
|
|
219
236
|
}
|
|
220
237
|
|
|
@@ -339,7 +356,7 @@ class Deva {
|
|
|
339
356
|
so the event is specific to the talk.
|
|
340
357
|
***************/
|
|
341
358
|
ask(packet) {
|
|
342
|
-
if (!this._active) return Promise.resolve(this.
|
|
359
|
+
if (!this._active) return Promise.resolve(this._messages.offline);
|
|
343
360
|
this.state('ask_question', packet);
|
|
344
361
|
|
|
345
362
|
packet.a = {
|
|
@@ -400,7 +417,7 @@ class Deva {
|
|
|
400
417
|
describe:
|
|
401
418
|
***************/
|
|
402
419
|
question(TEXT=false, DATA=false) {
|
|
403
|
-
if (!this._active) return Promise.resolve(this.
|
|
420
|
+
if (!this._active) return Promise.resolve(this._messages.offline);
|
|
404
421
|
|
|
405
422
|
const id = this.uid(); // generate a unique transport id for the question.
|
|
406
423
|
const t_split = TEXT.split(' ');
|
|
@@ -427,9 +444,9 @@ class Deva {
|
|
|
427
444
|
key = this.agent.key;
|
|
428
445
|
|
|
429
446
|
return new Promise((resolve, reject) => {
|
|
430
|
-
if (!TEXT) return reject(this.
|
|
447
|
+
if (!TEXT) return reject(this._messages.notext);
|
|
431
448
|
try {
|
|
432
|
-
if (!this._active) return reject(this.
|
|
449
|
+
if (!this._active) return reject(this._messages.offline);
|
|
433
450
|
|
|
434
451
|
// *: send just a string of text
|
|
435
452
|
// !: send a command to the local agent
|
|
@@ -440,7 +457,7 @@ class Deva {
|
|
|
440
457
|
let _state = 'question';
|
|
441
458
|
if (isAsk) {
|
|
442
459
|
_state = 'question_ask'
|
|
443
|
-
key = t_split[0]substring(1);
|
|
460
|
+
key = t_split[0].substring(1);
|
|
444
461
|
params = t_split[1] ? t_split[1].split(':') : false;
|
|
445
462
|
method = params[0];
|
|
446
463
|
text = t_split.slice(2).join(' ').trim();
|
|
@@ -448,7 +465,7 @@ class Deva {
|
|
|
448
465
|
else if (isCmd) {
|
|
449
466
|
_state = 'question_cmd'
|
|
450
467
|
params = t_split[1] ? t_split[1].split(':') : false;
|
|
451
|
-
method =
|
|
468
|
+
method = t_split[0].substring(1);
|
|
452
469
|
text = t_split.slice(1).join(' ').trim()
|
|
453
470
|
}
|
|
454
471
|
|
|
@@ -503,6 +520,7 @@ class Deva {
|
|
|
503
520
|
|
|
504
521
|
// create a hash for entire packet and insert into packet
|
|
505
522
|
packet.hash = this.hash(JSON.stringify(packet));
|
|
523
|
+
this.state('question_answer', packet);
|
|
506
524
|
|
|
507
525
|
return resolve(packet);
|
|
508
526
|
}).catch(err => {
|
|
@@ -541,7 +559,7 @@ class Deva {
|
|
|
541
559
|
return this._assignListeners();
|
|
542
560
|
}).then(() => {
|
|
543
561
|
this.state('init');
|
|
544
|
-
return this.onInit && typeof this.onInit === 'function' ? this.onInit() : this.start(this.
|
|
562
|
+
return this.onInit && typeof this.onInit === 'function' ? this.onInit() : this.start(this._messages.init);
|
|
545
563
|
}).then(started => {
|
|
546
564
|
return resolve(started)
|
|
547
565
|
}).catch(err => {
|
|
@@ -556,7 +574,7 @@ class Deva {
|
|
|
556
574
|
// packet: the packet that caused the error.
|
|
557
575
|
error(err,packet=false,reject=false) {
|
|
558
576
|
this.state('error', err);
|
|
559
|
-
if (this.onError && typeof this.onError === 'function')
|
|
577
|
+
if (this.onError && typeof this.onError === 'function') return this.onError(err, packet, reject);
|
|
560
578
|
console.error(err)
|
|
561
579
|
return reject ? reject(err) : err;
|
|
562
580
|
}
|
|
@@ -572,7 +590,7 @@ class Deva {
|
|
|
572
590
|
start() {
|
|
573
591
|
if (!this._active) return;
|
|
574
592
|
this.state('start');
|
|
575
|
-
return this.onStart && typeof this.onStart === 'function' ? this.onStart() : this.enter(this.
|
|
593
|
+
return this.onStart && typeof this.onStart === 'function' ? this.onStart() : this.enter(this._messages.start);
|
|
576
594
|
}
|
|
577
595
|
|
|
578
596
|
/**************
|
|
@@ -586,10 +604,10 @@ class Deva {
|
|
|
586
604
|
If the deva is offline it will return the offline message.
|
|
587
605
|
***************/
|
|
588
606
|
stop() {
|
|
589
|
-
if (!this._active) return Promise.resolve(this.
|
|
607
|
+
if (!this._active) return Promise.resolve(this._messages.offline);
|
|
590
608
|
this.state('stop');
|
|
591
609
|
this._active = false;
|
|
592
|
-
return this.onStop && typeof this.onStop === 'function' ? this.onStop() : this.exit(this.
|
|
610
|
+
return this.onStop && typeof this.onStop === 'function' ? this.onStop() : this.exit(this._messages.stop);
|
|
593
611
|
}
|
|
594
612
|
|
|
595
613
|
/**************
|
|
@@ -602,9 +620,9 @@ class Deva {
|
|
|
602
620
|
If the Deva is offline it will return the offline message.
|
|
603
621
|
***************/
|
|
604
622
|
enter() {
|
|
605
|
-
if (!this._active) return Promise.resolve(this.
|
|
623
|
+
if (!this._active) return Promise.resolve(this._messages.offline);
|
|
606
624
|
this.state('enter');
|
|
607
|
-
return this.onEnter && typeof this.onEnter === 'function' ? this.onEnter() : this.done(this.
|
|
625
|
+
return this.onEnter && typeof this.onEnter === 'function' ? this.onEnter() : this.done(this._messages.enter)
|
|
608
626
|
}
|
|
609
627
|
|
|
610
628
|
/**************
|
|
@@ -620,10 +638,10 @@ class Deva {
|
|
|
620
638
|
If the deva is offline it will return the offline message.
|
|
621
639
|
***************/
|
|
622
640
|
exit() {
|
|
623
|
-
if (!this._active) return Promise.resolve(this.
|
|
641
|
+
if (!this._active) return Promise.resolve(this._messages.offline);
|
|
624
642
|
this.state('exit');
|
|
625
643
|
this._active = false;
|
|
626
|
-
return this.onExit && typeof this.onExit === 'function' ? this.onExit() : Promise.resolve(this.
|
|
644
|
+
return this.onExit && typeof this.onExit === 'function' ? this.onExit() : Promise.resolve(this._messages.exit)
|
|
627
645
|
}
|
|
628
646
|
|
|
629
647
|
/**************
|
|
@@ -637,10 +655,10 @@ class Deva {
|
|
|
637
655
|
If the deva is offline it will return the offline message.
|
|
638
656
|
***************/
|
|
639
657
|
done(msg=false) {
|
|
640
|
-
if (!this._active) return Promise.resolve(this.
|
|
658
|
+
if (!this._active) return Promise.resolve(this._messages.offline);
|
|
641
659
|
this.state('done');
|
|
642
660
|
msg = msg ? msg : this._state;
|
|
643
|
-
return this.onDone && typeof this.onDone === 'function' ? this.onDone() : Promise.resolve(this.
|
|
661
|
+
return this.onDone && typeof this.onDone === 'function' ? this.onDone() : Promise.resolve(this._messages.done)
|
|
644
662
|
}
|
|
645
663
|
|
|
646
664
|
/**************
|
|
@@ -654,7 +672,7 @@ class Deva {
|
|
|
654
672
|
If the deva is offline it will return the offline message.
|
|
655
673
|
***************/
|
|
656
674
|
status(addto=false) {
|
|
657
|
-
if (!this._active) return Promise.resolve(this.
|
|
675
|
+
if (!this._active) return Promise.resolve(this._messages.offline);
|
|
658
676
|
const id = this.uid();
|
|
659
677
|
const dateFormat = new Intl.DateTimeFormat('en-US', { dateStyle: 'medium', timeStyle: 'medium' }).format(this._active);
|
|
660
678
|
let text = `${this.agent.name} is ONLINE since ${dateFormat}`;
|
|
@@ -697,14 +715,13 @@ class Deva {
|
|
|
697
715
|
startDevas() {
|
|
698
716
|
this.state('devas_start');
|
|
699
717
|
return new Promise((resolve, reject) => {
|
|
700
|
-
this.prompt
|
|
701
718
|
const devas = [];
|
|
702
719
|
for (let x in this.devas) {
|
|
703
720
|
devas.push(this.devas[x].init());
|
|
704
721
|
}
|
|
705
722
|
Promise.all(devas).then(() => {
|
|
706
723
|
this.state('devas_ready');
|
|
707
|
-
return resolve(this.
|
|
724
|
+
return resolve(this._messages.devas_started);
|
|
708
725
|
}).catch(reject);
|
|
709
726
|
});
|
|
710
727
|
}
|
|
@@ -723,7 +740,7 @@ class Deva {
|
|
|
723
740
|
}
|
|
724
741
|
Promise.all(devas).then(() => {
|
|
725
742
|
this.state('devas_stoped');
|
|
726
|
-
return resolve(this.
|
|
743
|
+
return resolve(this._messages.devas_stopped);
|
|
727
744
|
}).catch(reject);
|
|
728
745
|
});
|
|
729
746
|
}
|