@indra.ai/deva 1.1.13 → 1.1.15
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/index.js +30 -24
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -29,8 +29,12 @@ class Deva {
|
|
|
29
29
|
data: '📀 DATA',
|
|
30
30
|
ask: '🙋♀️ ASK',
|
|
31
31
|
cmd: '📟 COMMAND',
|
|
32
|
-
question: '
|
|
33
|
-
|
|
32
|
+
question: '🤖 QUESTION',
|
|
33
|
+
question_ask: '🤖 SEND QUEATION TO ASK',
|
|
34
|
+
question_cmd: '🤖 SEND QUESTION TO CMD',
|
|
35
|
+
queation_answer: '🔮 ANSWER QUESTION',
|
|
36
|
+
ask_question: '🤖 ASK QUESTION',
|
|
37
|
+
ask_answer: '🔮 ASK ANSWER',
|
|
34
38
|
talk: '🎙️ TALK',
|
|
35
39
|
listen: '🎧 LISTEN',
|
|
36
40
|
error: '❌ ERROR',
|
|
@@ -84,10 +88,10 @@ class Deva {
|
|
|
84
88
|
- st: The state flag to set for the Deva that matches to this._states
|
|
85
89
|
describe
|
|
86
90
|
***************/
|
|
87
|
-
state(st) {
|
|
91
|
+
state(st, data=false) {
|
|
88
92
|
this._state = this._states[st];
|
|
89
93
|
this.prompt(this._state);
|
|
90
|
-
this.talk(`${this.agent.
|
|
94
|
+
this.talk(`${this.agent.key}:state`, data);
|
|
91
95
|
}
|
|
92
96
|
|
|
93
97
|
// Called from the init function to bind the elements defined in the this.bind variable.
|
|
@@ -336,6 +340,7 @@ class Deva {
|
|
|
336
340
|
***************/
|
|
337
341
|
ask(packet) {
|
|
338
342
|
if (!this._active) return Promise.resolve(this.messages.offline);
|
|
343
|
+
this.state('ask_question', packet);
|
|
339
344
|
|
|
340
345
|
packet.a = {
|
|
341
346
|
agent: this.agent || false,
|
|
@@ -370,8 +375,9 @@ class Deva {
|
|
|
370
375
|
else {
|
|
371
376
|
packet.a.text = result;
|
|
372
377
|
}
|
|
378
|
+
|
|
379
|
+
this.state('ask_answer', packet);
|
|
373
380
|
this.talk(`${this.agent.key}:ask:${packet.id}`, packet);
|
|
374
|
-
this.state('wait');
|
|
375
381
|
}).catch(err => {
|
|
376
382
|
this.talk(`${this.agent.key}:ask:${packet.id}`, {error:err.toString()});
|
|
377
383
|
return this.error(err, packet);
|
|
@@ -396,16 +402,14 @@ class Deva {
|
|
|
396
402
|
question(TEXT=false, DATA=false) {
|
|
397
403
|
if (!this._active) return Promise.resolve(this.messages.offline);
|
|
398
404
|
|
|
399
|
-
this.state('question'); // set the state to question.
|
|
400
|
-
|
|
401
405
|
const id = this.uid(); // generate a unique transport id for the question.
|
|
402
406
|
const t_split = TEXT.split(' ');
|
|
403
407
|
|
|
404
408
|
// check to see if the string is an #ask string to talk to the other Deva.
|
|
405
|
-
const isAsk = t_split[0].startsWith(this.askChr)
|
|
409
|
+
const isAsk = t_split[0].startsWith(this.askChr);
|
|
406
410
|
|
|
407
411
|
// check to see if the string is a command string to run a local method.
|
|
408
|
-
const isCmd = t_split[0].startsWith(this.cmdChr)
|
|
412
|
+
const isCmd = t_split[0].startsWith(this.cmdChr);
|
|
409
413
|
|
|
410
414
|
// Format the packet for return on the request.
|
|
411
415
|
const orig = TEXT;
|
|
@@ -433,15 +437,16 @@ class Deva {
|
|
|
433
437
|
// #agent method:param1:param2 with text strings for proccessing
|
|
434
438
|
// !method param:list:parse for the local agent
|
|
435
439
|
// if is an ask then we format one way
|
|
440
|
+
let _state = 'question';
|
|
436
441
|
if (isAsk) {
|
|
437
|
-
|
|
442
|
+
_state = 'question_ask'
|
|
443
|
+
key = t_split[0]substring(1);
|
|
438
444
|
params = t_split[1] ? t_split[1].split(':') : false;
|
|
439
445
|
method = params[0];
|
|
440
446
|
text = t_split.slice(2).join(' ').trim();
|
|
441
|
-
key = isAsk;
|
|
442
447
|
}
|
|
443
448
|
else if (isCmd) {
|
|
444
|
-
|
|
449
|
+
_state = 'question_cmd'
|
|
445
450
|
params = t_split[1] ? t_split[1].split(':') : false;
|
|
446
451
|
method = isCmd;
|
|
447
452
|
text = t_split.slice(1).join(' ').trim()
|
|
@@ -463,18 +468,20 @@ class Deva {
|
|
|
463
468
|
|
|
464
469
|
// hash the packet and insert the hash into the packet meta object.
|
|
465
470
|
packet.q.meta.hash = this.hash(JSON.stringify(packet.q));
|
|
471
|
+
this.state(_state, packet);
|
|
466
472
|
|
|
467
473
|
// If a question to another Deva with '#' then trigger events
|
|
468
474
|
if (isAsk) {
|
|
469
|
-
this.talk(`${
|
|
470
|
-
this.once(`${
|
|
475
|
+
this.talk(`${key}:ask`, packet);
|
|
476
|
+
this.once(`${key}:ask:${packet.id}`, answer => {
|
|
471
477
|
return resolve(answer);
|
|
472
478
|
});
|
|
473
479
|
}
|
|
474
480
|
// if the user sends a local command '$' then it will ask of the self.
|
|
475
481
|
else {
|
|
476
|
-
this.
|
|
477
|
-
|
|
482
|
+
if (typeof this.methods[method] !== 'function') {
|
|
483
|
+
return resolve(this._methodNotFound(packet));
|
|
484
|
+
}
|
|
478
485
|
this.methods[method](packet).then(result => {
|
|
479
486
|
const text = typeof result === 'object' ? result.text : result;
|
|
480
487
|
const html = typeof result === 'object' ? result.html : result;
|
|
@@ -497,7 +504,6 @@ class Deva {
|
|
|
497
504
|
// create a hash for entire packet and insert into packet
|
|
498
505
|
packet.hash = this.hash(JSON.stringify(packet));
|
|
499
506
|
|
|
500
|
-
this.talk(`log`, packet);
|
|
501
507
|
return resolve(packet);
|
|
502
508
|
}).catch(err => {
|
|
503
509
|
return this.error(err, packet);
|
|
@@ -535,7 +541,7 @@ class Deva {
|
|
|
535
541
|
return this._assignListeners();
|
|
536
542
|
}).then(() => {
|
|
537
543
|
this.state('init');
|
|
538
|
-
return this.onInit && typeof this.onInit === 'function' ? this.onInit() : this.start();
|
|
544
|
+
return this.onInit && typeof this.onInit === 'function' ? this.onInit() : this.start(this._state);
|
|
539
545
|
}).then(started => {
|
|
540
546
|
return resolve(started)
|
|
541
547
|
}).catch(err => {
|
|
@@ -549,10 +555,10 @@ class Deva {
|
|
|
549
555
|
// e: is the error to pass into the interface.
|
|
550
556
|
// packet: the packet that caused the error.
|
|
551
557
|
error(err,packet=false,reject=false) {
|
|
552
|
-
this.state('error');
|
|
558
|
+
this.state('error', err);
|
|
559
|
+
if (this.onError && typeof this.onError === 'function') ? return this.onError(err, packet, reject);
|
|
553
560
|
console.error(err)
|
|
554
|
-
|
|
555
|
-
return reject ? reject(err) : false;
|
|
561
|
+
return reject ? reject(err) : err;
|
|
556
562
|
}
|
|
557
563
|
|
|
558
564
|
/**************
|
|
@@ -566,7 +572,7 @@ class Deva {
|
|
|
566
572
|
start() {
|
|
567
573
|
if (!this._active) return;
|
|
568
574
|
this.state('start');
|
|
569
|
-
return this.onStart && typeof this.onStart === 'function' ? this.onStart() : this.enter();
|
|
575
|
+
return this.onStart && typeof this.onStart === 'function' ? this.onStart() : this.enter(this._state);
|
|
570
576
|
}
|
|
571
577
|
|
|
572
578
|
/**************
|
|
@@ -583,7 +589,7 @@ class Deva {
|
|
|
583
589
|
if (!this._active) return Promise.resolve(this.messages.offline);
|
|
584
590
|
this.state('stop');
|
|
585
591
|
this._active = false;
|
|
586
|
-
return this.onStop && typeof this.onStop === 'function' ? this.onStop() : this.exit();
|
|
592
|
+
return this.onStop && typeof this.onStop === 'function' ? this.onStop() : this.exit(this._state);
|
|
587
593
|
}
|
|
588
594
|
|
|
589
595
|
/**************
|
|
@@ -598,7 +604,7 @@ class Deva {
|
|
|
598
604
|
enter() {
|
|
599
605
|
if (!this._active) return Promise.resolve(this.messages.offline);
|
|
600
606
|
this.state('enter');
|
|
601
|
-
return this.onEnter && typeof this.onEnter === 'function' ? this.onEnter() : this.done(this.
|
|
607
|
+
return this.onEnter && typeof this.onEnter === 'function' ? this.onEnter() : this.done(this._state)
|
|
602
608
|
}
|
|
603
609
|
|
|
604
610
|
/**************
|