@indra.ai/deva 1.1.93 → 1.1.95
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 +161 -132
- package/package.json +3 -3
package/index.js
CHANGED
|
@@ -75,6 +75,132 @@ class Deva {
|
|
|
75
75
|
}
|
|
76
76
|
}
|
|
77
77
|
|
|
78
|
+
/**************
|
|
79
|
+
func: _assignBind
|
|
80
|
+
params: none
|
|
81
|
+
describe:
|
|
82
|
+
The assign bind function will bind the translate functions and parse functions
|
|
83
|
+
of the agent and bind their functionality to the state machine.
|
|
84
|
+
***************/
|
|
85
|
+
_assignBind() {
|
|
86
|
+
return new Promise((resolve, reject) => {
|
|
87
|
+
try {
|
|
88
|
+
this.bind.forEach(bind => { // loop over the bind items func, method, listener...
|
|
89
|
+
if (this[bind]) for (let x in this[bind]) { // if the root has a bind func, method, listener
|
|
90
|
+
if (typeof this[bind][x] === 'function') { // check to make sure object is a fucntion
|
|
91
|
+
this[bind][x] = this[bind][x].bind(this); // bind the item from the bind object
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
});
|
|
95
|
+
// bind translate
|
|
96
|
+
const translate = this._agent && this._agent.translate && typeof this._agent.translate === 'function';
|
|
97
|
+
if (translate) this._agent.translate = this._agent.translate.bind(this);
|
|
98
|
+
// bind parser
|
|
99
|
+
const parse = this._agent && this._agent.parse && typeof this._agent.parse === 'function';
|
|
100
|
+
if (parse) this._agent.parse = this._agent.parse.bind(this);
|
|
101
|
+
// bind process
|
|
102
|
+
const process = this._agent && this._agent.process && typeof this._agent.process === 'function';
|
|
103
|
+
if (process) this._agent.process = this._agent.process.bind(this);
|
|
104
|
+
}
|
|
105
|
+
catch (e) {
|
|
106
|
+
return this.error(e, false, reject); // trigger the this.error for errors
|
|
107
|
+
}
|
|
108
|
+
finally {
|
|
109
|
+
return resolve(); // when the configuration is complete then return an empty resolve.
|
|
110
|
+
}
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/**************
|
|
115
|
+
func: _assignListeners
|
|
116
|
+
params: none
|
|
117
|
+
describe:
|
|
118
|
+
Assign listeners will take the this.lisners objects and assign the appropriate
|
|
119
|
+
lisnter values for the event bus.
|
|
120
|
+
***************/
|
|
121
|
+
_assignListeners() {
|
|
122
|
+
return new Promise((resolve, reject) => {
|
|
123
|
+
try {
|
|
124
|
+
// set the default listeners for the states of the agent.
|
|
125
|
+
|
|
126
|
+
for (let state in this._states) {
|
|
127
|
+
if (typeof this[state] === 'function') {
|
|
128
|
+
this.events.on(`${this._agent.key}:${state}`, packet => {
|
|
129
|
+
return this[state](packet);
|
|
130
|
+
});
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
// set the assigned listeners for the agent.
|
|
135
|
+
for (let listener in this.listeners) {
|
|
136
|
+
this.events.on(listener, packet => {
|
|
137
|
+
return this.listeners[listener](packet);
|
|
138
|
+
})
|
|
139
|
+
}
|
|
140
|
+
return resolve();
|
|
141
|
+
}
|
|
142
|
+
catch (e) {
|
|
143
|
+
return this.error(e, false, reject);
|
|
144
|
+
}
|
|
145
|
+
});
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
// Some elements will inherit the data of the parent. this object will loop over
|
|
149
|
+
// any children data that theis deva has and assign the inherited information.
|
|
150
|
+
/**************
|
|
151
|
+
func: _assignInherit
|
|
152
|
+
params: none
|
|
153
|
+
describe:
|
|
154
|
+
The assign inherit will make sure the Devas in the current Deva have all the
|
|
155
|
+
inherited properties all setup to collaborate efficiently.
|
|
156
|
+
***************/
|
|
157
|
+
_assignInherit() {
|
|
158
|
+
return new Promise((resolve, reject) => {
|
|
159
|
+
try {
|
|
160
|
+
for (let d in this.devas) {
|
|
161
|
+
this.inherit.forEach(inherit => {
|
|
162
|
+
this.devas[d][inherit] = this[inherit];
|
|
163
|
+
});
|
|
164
|
+
}
|
|
165
|
+
return resolve();
|
|
166
|
+
}
|
|
167
|
+
catch (e) {
|
|
168
|
+
return this.error(e, false, reject);
|
|
169
|
+
}
|
|
170
|
+
});
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
// General handler for when a method is NOT found from a user command.
|
|
174
|
+
/**************
|
|
175
|
+
func: _methodNotFound
|
|
176
|
+
params:
|
|
177
|
+
- packet: The packet to relay when a method is not found.
|
|
178
|
+
describe:
|
|
179
|
+
The _methodNotFound function allows for additional security by firing
|
|
180
|
+
a specfici program functon every single time a interaction happens wehre a
|
|
181
|
+
method is not located. This assits in security and support by identifying
|
|
182
|
+
troubls or users who may be attemptng to explit features.
|
|
183
|
+
|
|
184
|
+
Then we talk a security event that watches all methods and return the packet.
|
|
185
|
+
|
|
186
|
+
This will return a not found text string preventing any furhter processing.
|
|
187
|
+
***************/
|
|
188
|
+
_methodNotFound(packet) {
|
|
189
|
+
packet.a = {
|
|
190
|
+
id: this.uid(),
|
|
191
|
+
agent: this.agent() || false,
|
|
192
|
+
client: this.client() || false,
|
|
193
|
+
text: `${this._messages.method_not_found}`,
|
|
194
|
+
meta: {
|
|
195
|
+
key: this._agent.key,
|
|
196
|
+
method: packet.q.meta.method,
|
|
197
|
+
},
|
|
198
|
+
created: Date.now(),
|
|
199
|
+
};
|
|
200
|
+
this.state('method_not_found');
|
|
201
|
+
return packet;
|
|
202
|
+
}
|
|
203
|
+
|
|
78
204
|
/**************
|
|
79
205
|
func: Client
|
|
80
206
|
params: client - client provided data.
|
|
@@ -520,131 +646,6 @@ class Deva {
|
|
|
520
646
|
}
|
|
521
647
|
});
|
|
522
648
|
}
|
|
523
|
-
/**************
|
|
524
|
-
func: _assignBind
|
|
525
|
-
params: none
|
|
526
|
-
describe:
|
|
527
|
-
The assign bind function will bind the translate functions and parse functions
|
|
528
|
-
of the agent and bind their functionality to the state machine.
|
|
529
|
-
***************/
|
|
530
|
-
_assignBind() {
|
|
531
|
-
return new Promise((resolve, reject) => {
|
|
532
|
-
try {
|
|
533
|
-
this.bind.forEach(bind => { // loop over the bind items func, method, listener...
|
|
534
|
-
if (this[bind]) for (let x in this[bind]) { // if the root has a bind func, method, listener
|
|
535
|
-
if (typeof this[bind][x] === 'function') { // check to make sure object is a fucntion
|
|
536
|
-
this[bind][x] = this[bind][x].bind(this); // bind the item from the bind object
|
|
537
|
-
}
|
|
538
|
-
}
|
|
539
|
-
});
|
|
540
|
-
// bind translate
|
|
541
|
-
const translate = this._agent && this._agent.translate && typeof this._agent.translate === 'function';
|
|
542
|
-
if (translate) this._agent.translate = this._agent.translate.bind(this);
|
|
543
|
-
// bind parser
|
|
544
|
-
const parse = this._agent && this._agent.parse && typeof this._agent.parse === 'function';
|
|
545
|
-
if (parse) this._agent.parse = this._agent.parse.bind(this);
|
|
546
|
-
// bind process
|
|
547
|
-
const process = this._agent && this._agent.process && typeof this._agent.process === 'function';
|
|
548
|
-
if (process) this._agent.process = this._agent.process.bind(this);
|
|
549
|
-
}
|
|
550
|
-
catch (e) {
|
|
551
|
-
return this.error(e, false, reject); // trigger the this.error for errors
|
|
552
|
-
}
|
|
553
|
-
finally {
|
|
554
|
-
return resolve(); // when the configuration is complete then return an empty resolve.
|
|
555
|
-
}
|
|
556
|
-
});
|
|
557
|
-
}
|
|
558
|
-
|
|
559
|
-
/**************
|
|
560
|
-
func: _assignListeners
|
|
561
|
-
params: none
|
|
562
|
-
describe:
|
|
563
|
-
Assign listeners will take the this.lisners objects and assign the appropriate
|
|
564
|
-
lisnter values for the event bus.
|
|
565
|
-
***************/
|
|
566
|
-
_assignListeners() {
|
|
567
|
-
return new Promise((resolve, reject) => {
|
|
568
|
-
try {
|
|
569
|
-
// set the default listeners for the states of the agent.
|
|
570
|
-
|
|
571
|
-
for (let state in this._states) {
|
|
572
|
-
if (typeof this[state] === 'function') {
|
|
573
|
-
this.events.on(`${this._agent.key}:${state}`, packet => {
|
|
574
|
-
return this[state](packet);
|
|
575
|
-
});
|
|
576
|
-
}
|
|
577
|
-
}
|
|
578
|
-
|
|
579
|
-
// set the assigned listeners for the agent.
|
|
580
|
-
for (let listener in this.listeners) {
|
|
581
|
-
this.events.on(listener, packet => {
|
|
582
|
-
return this.listeners[listener](packet);
|
|
583
|
-
})
|
|
584
|
-
}
|
|
585
|
-
return resolve();
|
|
586
|
-
}
|
|
587
|
-
catch (e) {
|
|
588
|
-
return this.error(e, false, reject);
|
|
589
|
-
}
|
|
590
|
-
});
|
|
591
|
-
}
|
|
592
|
-
|
|
593
|
-
// Some elements will inherit the data of the parent. this object will loop over
|
|
594
|
-
// any children data that theis deva has and assign the inherited information.
|
|
595
|
-
/**************
|
|
596
|
-
func: _assignInherit
|
|
597
|
-
params: none
|
|
598
|
-
describe:
|
|
599
|
-
The assign inherit will make sure the Devas in the current Deva have all the
|
|
600
|
-
inherited properties all setup to collaborate efficiently.
|
|
601
|
-
***************/
|
|
602
|
-
_assignInherit() {
|
|
603
|
-
return new Promise((resolve, reject) => {
|
|
604
|
-
try {
|
|
605
|
-
for (let d in this.devas) {
|
|
606
|
-
this.inherit.forEach(inherit => {
|
|
607
|
-
this.devas[d][inherit] = this[inherit];
|
|
608
|
-
});
|
|
609
|
-
}
|
|
610
|
-
return resolve();
|
|
611
|
-
}
|
|
612
|
-
catch (e) {
|
|
613
|
-
return this.error(e, false, reject);
|
|
614
|
-
}
|
|
615
|
-
});
|
|
616
|
-
}
|
|
617
|
-
|
|
618
|
-
// General handler for when a method is NOT found from a user command.
|
|
619
|
-
/**************
|
|
620
|
-
func: _methodNotFound
|
|
621
|
-
params:
|
|
622
|
-
- packet: The packet to relay when a method is not found.
|
|
623
|
-
describe:
|
|
624
|
-
The _methodNotFound function allows for additional security by firing
|
|
625
|
-
a specfici program functon every single time a interaction happens wehre a
|
|
626
|
-
method is not located. This assits in security and support by identifying
|
|
627
|
-
troubls or users who may be attemptng to explit features.
|
|
628
|
-
|
|
629
|
-
Then we talk a security event that watches all methods and return the packet.
|
|
630
|
-
|
|
631
|
-
This will return a not found text string preventing any furhter processing.
|
|
632
|
-
***************/
|
|
633
|
-
_methodNotFound(packet) {
|
|
634
|
-
packet.a = {
|
|
635
|
-
id: this.uid(),
|
|
636
|
-
agent: this.agent() || false,
|
|
637
|
-
client: this.client() || false,
|
|
638
|
-
text: `${this._messages.method_not_found}`,
|
|
639
|
-
meta: {
|
|
640
|
-
key: this._agent.key,
|
|
641
|
-
method: packet.q.meta.method,
|
|
642
|
-
},
|
|
643
|
-
created: Date.now(),
|
|
644
|
-
};
|
|
645
|
-
this.state('method_not_found');
|
|
646
|
-
return packet;
|
|
647
|
-
}
|
|
648
649
|
|
|
649
650
|
/**************
|
|
650
651
|
func: talk
|
|
@@ -779,7 +780,6 @@ class Deva {
|
|
|
779
780
|
this.talk(config.events.question, this.copy(packet)); // global question event make sure to copy data.
|
|
780
781
|
|
|
781
782
|
if (isAsk) { // isAsk check if the question isAsk and talk
|
|
782
|
-
this.state('ask');
|
|
783
783
|
// if: isAsk wait for the once event which is key'd to the packet ID for specified responses
|
|
784
784
|
this.talk(`${key}:ask`, packet);
|
|
785
785
|
this.once(`${key}:ask:${packet.id}`, answer => {
|
|
@@ -788,7 +788,7 @@ class Deva {
|
|
|
788
788
|
});
|
|
789
789
|
}
|
|
790
790
|
else { // else: answer tue question locally
|
|
791
|
-
this.action('
|
|
791
|
+
this.action('question_answer');
|
|
792
792
|
return this.answer(packet, resolve, reject);
|
|
793
793
|
}
|
|
794
794
|
}
|
|
@@ -1162,7 +1162,6 @@ class Deva {
|
|
|
1162
1162
|
describe
|
|
1163
1163
|
***************/
|
|
1164
1164
|
state(state) {
|
|
1165
|
-
console.log('STATE', state);
|
|
1166
1165
|
try {
|
|
1167
1166
|
if (!this._states[state]) return;
|
|
1168
1167
|
this._state = state;
|
|
@@ -1279,16 +1278,16 @@ class Deva {
|
|
|
1279
1278
|
- st: The context flag to set for the Deva that matches to this._contexts
|
|
1280
1279
|
describe
|
|
1281
1280
|
***************/
|
|
1282
|
-
context(
|
|
1281
|
+
context(value) {
|
|
1283
1282
|
try {
|
|
1284
|
-
this._context =
|
|
1283
|
+
this._context = value;
|
|
1285
1284
|
const _data = {
|
|
1286
1285
|
id: this.uid(true),
|
|
1287
1286
|
key: 'context',
|
|
1288
|
-
value
|
|
1287
|
+
value,
|
|
1289
1288
|
agent: this.agent(),
|
|
1290
1289
|
client: this.client(),
|
|
1291
|
-
text,
|
|
1290
|
+
text: this.vars.context[value] || value,
|
|
1292
1291
|
created: Date.now(),
|
|
1293
1292
|
};
|
|
1294
1293
|
_data.hash = this.hash(_data);
|
|
@@ -1829,6 +1828,11 @@ class Deva {
|
|
|
1829
1828
|
return splitter.slice(0, maxwords).join(' ');
|
|
1830
1829
|
}
|
|
1831
1830
|
|
|
1831
|
+
/**************
|
|
1832
|
+
func: dupes
|
|
1833
|
+
params: dupers
|
|
1834
|
+
describe: remove duplicees from an array.
|
|
1835
|
+
***************/
|
|
1832
1836
|
dupes(dupers) {
|
|
1833
1837
|
if (!Array.isArray(dupers)) return dupers;
|
|
1834
1838
|
const check = [];
|
|
@@ -1840,6 +1844,31 @@ class Deva {
|
|
|
1840
1844
|
});
|
|
1841
1845
|
}
|
|
1842
1846
|
|
|
1847
|
+
/**************
|
|
1848
|
+
func: help
|
|
1849
|
+
params:
|
|
1850
|
+
- msg: the help msg to search against
|
|
1851
|
+
- help_dir: base directory of the deva help files.
|
|
1852
|
+
describe:
|
|
1853
|
+
the help utility makes it easy to create help files for your deva. the utility
|
|
1854
|
+
checks the existence of a help file in the passed in directory then if
|
|
1855
|
+
one exists it will then present it based on the users request text input.
|
|
1856
|
+
***************/
|
|
1857
|
+
help(msg, help_dir) {
|
|
1858
|
+
return new Promise((resolve, reject) => {
|
|
1859
|
+
const params = msg.split(' ');
|
|
1860
|
+
let helpFile = 'main';
|
|
1861
|
+
if (params[0]) helpFile = params[0];
|
|
1862
|
+
if (params[1]) helpFile = `${params[0]}_${params[1]}`;
|
|
1863
|
+
helpFile = path.join(help_dir, 'help', `${helpFile}.feecting`);
|
|
1864
|
+
try {
|
|
1865
|
+
return resolve(fs.readFileSync(helpFile, 'utf8'))
|
|
1866
|
+
} catch (e) {
|
|
1867
|
+
return reject(e)
|
|
1868
|
+
}
|
|
1869
|
+
});
|
|
1870
|
+
}
|
|
1871
|
+
|
|
1843
1872
|
/**************
|
|
1844
1873
|
func: error
|
|
1845
1874
|
params:
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@indra.ai/deva",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.95",
|
|
4
4
|
"description": "The Deva Core",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -27,8 +27,8 @@
|
|
|
27
27
|
}
|
|
28
28
|
},
|
|
29
29
|
"engines": {
|
|
30
|
-
"npm": ">=
|
|
31
|
-
"node": ">=
|
|
30
|
+
"npm": ">=9.5.1",
|
|
31
|
+
"node": ">=18.16.0"
|
|
32
32
|
},
|
|
33
33
|
"dependencies": {}
|
|
34
34
|
}
|