@indra.ai/deva 1.1.14 → 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 +92 -75
- 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,11 +53,13 @@ class Deva {
|
|
|
29
53
|
data: '📀 DATA',
|
|
30
54
|
ask: '🙋♀️ ASK',
|
|
31
55
|
cmd: '📟 COMMAND',
|
|
32
|
-
question: '
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
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',
|
|
37
63
|
talk: '🎙️ TALK',
|
|
38
64
|
listen: '🎧 LISTEN',
|
|
39
65
|
error: '❌ ERROR',
|
|
@@ -45,35 +71,18 @@ class Deva {
|
|
|
45
71
|
systems: '👽 SYSTEMS',
|
|
46
72
|
solutions: '🔬 SOLUTIONS',
|
|
47
73
|
};
|
|
48
|
-
this.
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
this.func = opts.func || {}; // local Functions
|
|
61
|
-
this.methods = opts.methods || {}; // local Methods
|
|
62
|
-
this.maxListeners = opts.maxListenners || 0; // set the local maxListeners
|
|
63
|
-
|
|
64
|
-
for (var opt in opts) {
|
|
65
|
-
if (!this[opt]) this[opt] = opts[opt]; // set any remaining opts to this.
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
this.cmdChr = '/';
|
|
69
|
-
this.askChr = '#';
|
|
70
|
-
this.inherit = ["events", "config", "lib", "security", "client"];
|
|
71
|
-
this.bind = ["listeners", "methods", "func", "lib", "security", "agent", "client"];
|
|
72
|
-
this.messages = {
|
|
73
|
-
offline: 'AGENT OFFLINE',
|
|
74
|
-
stopped: 'DEVAS STOPPED',
|
|
75
|
-
notext: 'NO TEXT',
|
|
76
|
-
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!',
|
|
77
86
|
}
|
|
78
87
|
}
|
|
79
88
|
|
|
@@ -87,10 +96,24 @@ class Deva {
|
|
|
87
96
|
- st: The state flag to set for the Deva that matches to this._states
|
|
88
97
|
describe
|
|
89
98
|
***************/
|
|
90
|
-
state(st) {
|
|
99
|
+
state(st, data=false) {
|
|
100
|
+
if (!Object.keys(this._states).includes(st)) return;
|
|
91
101
|
this._state = this._states[st];
|
|
92
|
-
|
|
93
|
-
|
|
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;
|
|
94
117
|
}
|
|
95
118
|
|
|
96
119
|
// Called from the init function to bind the elements defined in the this.bind variable.
|
|
@@ -139,12 +162,6 @@ class Deva {
|
|
|
139
162
|
_assignListeners() {
|
|
140
163
|
return new Promise((resolve, reject) => {
|
|
141
164
|
try {
|
|
142
|
-
// set the default listeners for the states of the agent.
|
|
143
|
-
for (let state in this._states) {
|
|
144
|
-
this.events.on(`${this.agent.key}:${state}`, packet => {
|
|
145
|
-
return this[state](packet);
|
|
146
|
-
})
|
|
147
|
-
}
|
|
148
165
|
// set the assigned listeners for the agent.
|
|
149
166
|
for (let listener in this.listeners) {
|
|
150
167
|
this.events.on(listener, packet => {
|
|
@@ -207,13 +224,14 @@ class Deva {
|
|
|
207
224
|
packet.a = {
|
|
208
225
|
agent: this.agent || false,
|
|
209
226
|
client: this.client || false,
|
|
210
|
-
text: `${packet.q.meta.method}
|
|
227
|
+
text: `${this._messages.method_not_found} - ${packet.q.meta.method} `,
|
|
211
228
|
meta: {
|
|
212
229
|
key: this.agent.key,
|
|
213
230
|
method: packet.q.meta.method,
|
|
214
231
|
},
|
|
215
232
|
created: Date.now(),
|
|
216
233
|
};
|
|
234
|
+
this.state('method_not_found', packet);
|
|
217
235
|
return packet;
|
|
218
236
|
}
|
|
219
237
|
|
|
@@ -338,8 +356,8 @@ class Deva {
|
|
|
338
356
|
so the event is specific to the talk.
|
|
339
357
|
***************/
|
|
340
358
|
ask(packet) {
|
|
341
|
-
if (!this._active) return Promise.resolve(this.
|
|
342
|
-
this.state('
|
|
359
|
+
if (!this._active) return Promise.resolve(this._messages.offline);
|
|
360
|
+
this.state('ask_question', packet);
|
|
343
361
|
|
|
344
362
|
packet.a = {
|
|
345
363
|
agent: this.agent || false,
|
|
@@ -374,6 +392,8 @@ class Deva {
|
|
|
374
392
|
else {
|
|
375
393
|
packet.a.text = result;
|
|
376
394
|
}
|
|
395
|
+
|
|
396
|
+
this.state('ask_answer', packet);
|
|
377
397
|
this.talk(`${this.agent.key}:ask:${packet.id}`, packet);
|
|
378
398
|
}).catch(err => {
|
|
379
399
|
this.talk(`${this.agent.key}:ask:${packet.id}`, {error:err.toString()});
|
|
@@ -397,9 +417,7 @@ class Deva {
|
|
|
397
417
|
describe:
|
|
398
418
|
***************/
|
|
399
419
|
question(TEXT=false, DATA=false) {
|
|
400
|
-
if (!this._active) return Promise.resolve(this.
|
|
401
|
-
|
|
402
|
-
this.state('question'); // set the state to question.
|
|
420
|
+
if (!this._active) return Promise.resolve(this._messages.offline);
|
|
403
421
|
|
|
404
422
|
const id = this.uid(); // generate a unique transport id for the question.
|
|
405
423
|
const t_split = TEXT.split(' ');
|
|
@@ -426,9 +444,9 @@ class Deva {
|
|
|
426
444
|
key = this.agent.key;
|
|
427
445
|
|
|
428
446
|
return new Promise((resolve, reject) => {
|
|
429
|
-
if (!TEXT) return reject(this.
|
|
447
|
+
if (!TEXT) return reject(this._messages.notext);
|
|
430
448
|
try {
|
|
431
|
-
if (!this._active) return reject(this.
|
|
449
|
+
if (!this._active) return reject(this._messages.offline);
|
|
432
450
|
|
|
433
451
|
// *: send just a string of text
|
|
434
452
|
// !: send a command to the local agent
|
|
@@ -436,21 +454,20 @@ class Deva {
|
|
|
436
454
|
// #agent method:param1:param2 with text strings for proccessing
|
|
437
455
|
// !method param:list:parse for the local agent
|
|
438
456
|
// if is an ask then we format one way
|
|
439
|
-
let _state = '
|
|
457
|
+
let _state = 'question';
|
|
440
458
|
if (isAsk) {
|
|
441
|
-
_state = '
|
|
442
|
-
key = t_split[0]substring(1);
|
|
459
|
+
_state = 'question_ask'
|
|
460
|
+
key = t_split[0].substring(1);
|
|
443
461
|
params = t_split[1] ? t_split[1].split(':') : false;
|
|
444
462
|
method = params[0];
|
|
445
463
|
text = t_split.slice(2).join(' ').trim();
|
|
446
464
|
}
|
|
447
465
|
else if (isCmd) {
|
|
448
|
-
_state = '
|
|
466
|
+
_state = 'question_cmd'
|
|
449
467
|
params = t_split[1] ? t_split[1].split(':') : false;
|
|
450
|
-
method =
|
|
468
|
+
method = t_split[0].substring(1);
|
|
451
469
|
text = t_split.slice(1).join(' ').trim()
|
|
452
470
|
}
|
|
453
|
-
this.state(_state);
|
|
454
471
|
|
|
455
472
|
packet.q = {
|
|
456
473
|
agent: this.agent || false,
|
|
@@ -468,6 +485,7 @@ class Deva {
|
|
|
468
485
|
|
|
469
486
|
// hash the packet and insert the hash into the packet meta object.
|
|
470
487
|
packet.q.meta.hash = this.hash(JSON.stringify(packet.q));
|
|
488
|
+
this.state(_state, packet);
|
|
471
489
|
|
|
472
490
|
// If a question to another Deva with '#' then trigger events
|
|
473
491
|
if (isAsk) {
|
|
@@ -479,7 +497,6 @@ class Deva {
|
|
|
479
497
|
// if the user sends a local command '$' then it will ask of the self.
|
|
480
498
|
else {
|
|
481
499
|
if (typeof this.methods[method] !== 'function') {
|
|
482
|
-
this.state('answer_cmd');
|
|
483
500
|
return resolve(this._methodNotFound(packet));
|
|
484
501
|
}
|
|
485
502
|
this.methods[method](packet).then(result => {
|
|
@@ -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();
|
|
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 => {
|
|
@@ -555,10 +573,10 @@ class Deva {
|
|
|
555
573
|
// e: is the error to pass into the interface.
|
|
556
574
|
// packet: the packet that caused the error.
|
|
557
575
|
error(err,packet=false,reject=false) {
|
|
558
|
-
this.state('error');
|
|
576
|
+
this.state('error', err);
|
|
577
|
+
if (this.onError && typeof this.onError === 'function') return this.onError(err, packet, reject);
|
|
559
578
|
console.error(err)
|
|
560
|
-
|
|
561
|
-
return reject ? reject(err) : false;
|
|
579
|
+
return reject ? reject(err) : err;
|
|
562
580
|
}
|
|
563
581
|
|
|
564
582
|
/**************
|
|
@@ -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();
|
|
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();
|
|
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
|
}
|