@indra.ai/deva 1.54.10 → 1.55.0

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/config/index.js CHANGED
@@ -17,6 +17,9 @@ import msgs from './msgs.json' with {type:'json'};
17
17
  export default {
18
18
  cmdChr: '/',
19
19
  askChr: '#',
20
+ timeout: {
21
+ ask: 30000
22
+ },
20
23
  container: {
21
24
  begin: '::BEGIN',
22
25
  end: '::END',
package/config/msgs.json CHANGED
@@ -18,6 +18,8 @@
18
18
  "nodata": "⛔️ NO DATA PROVIDED",
19
19
  "nokey": "⛔️ NO KEY PROVIDED",
20
20
  "invalidkey": "⛔️ INVALID KEY PROVIDED",
21
+ "asktimeout": "⛔️ ASK TIMEOUT",
22
+ "noask": "⛔️ NO ASK PROVIDED",
21
23
  "method_not_found": "⛔️ METHOD NOT FOUND",
22
24
  "help_not_found": "⛔️ HELP NOT FOUND",
23
25
  "invalid_agent": "⛔️ Agent Hash Invalid",
package/index.js CHANGED
@@ -19,6 +19,8 @@ class Deva {
19
19
  this._info = opts.info || false; // the deva information from the package file.
20
20
  this._config = opts.config || {}; // local Config Object
21
21
  this._config.ready_hash = config.ready_hash; // items to hash at ready
22
+ if (!this._config.timeout) this._config.timeout = config.timeout; // add timeout values to _config.
23
+
22
24
  this._agent = opts.agent || false; // Agent profile object
23
25
  this._client = {}; // this will be set on init.
24
26
  this._active = false; // the active/birth date.
@@ -67,6 +69,9 @@ class Deva {
67
69
  this._uid = config.uid; // set the uid options
68
70
 
69
71
  this._events = config.events; // set the core system events
72
+ this._bound = new Map(); // evt -> wrapper registered on the shared bus
73
+ this._pending = new Set(); // pending asks: {evt, handler, timer}
74
+
70
75
  this._feature = config.feature; // set the feature from config data.
71
76
  this._features = config.features; // set the features from config data.
72
77
 
@@ -179,10 +184,10 @@ class Deva {
179
184
  }
180
185
 
181
186
  // set the assigned listeners for the agent.
182
- for (let listener in this.listeners) { // loop over the liteners
183
- this.events.on(listener, packet => { // set the event listener
184
- return this.listeners[listener](packet); // return the listener function
185
- })
187
+ for (let listener in this.listeners) { // loop over the listeners
188
+ const handler = packet => this.listeners[listener](packet);
189
+ this._bound.set(listener, handler);
190
+ this.events.on(listener, handler);
186
191
  }
187
192
  return resolve(); // resolve the function after everything is done.
188
193
  }
@@ -192,8 +197,41 @@ class Deva {
192
197
  });
193
198
  }
194
199
 
200
+ /**************
201
+ func: _removeListeners
202
+ params: none
203
+ describe:
204
+ Remove listeners will take the this.lisners objects and remove the appropriate
205
+ listener values for the event bus.
206
+ ***************/
207
+ _removeListeners() {
208
+ if (!this.events || !this.listeners) return;
209
+ for (const evt in this.listeners) {
210
+ const handler = this._bound.get(evt);
211
+ if (handler) this.ignore(evt, handler); // ignore() already calls events.removeListener
212
+ }
213
+ this._bound.clear();
214
+ return this._clearPending(); // pending asks go in the same pass
215
+ }
216
+
217
+ /**************
218
+ func: _clearPending
219
+ params: none
220
+ describe:
221
+ Clears the timer and removes the listener for every pending ask so a
222
+ shutdown leaves nothing behind on the shared event bus.
223
+ ***************/
224
+ _clearPending() {
225
+ if (!this._pending) return;
226
+ for (const pending of this._pending) {
227
+ clearTimeout(pending.timer);
228
+ if (this.events) this.ignore(pending.evt, pending.handler);
229
+ }
230
+ this._pending.clear();
231
+ }
232
+
195
233
  // Some elements will inherit the data of the parent. this object will loop over
196
- // any children data that theis deva has and assign the inherited information.
234
+ // any children data that this deva has and assign the inherited information.
197
235
  /**************
198
236
  func: _assignInherit
199
237
  params: none
@@ -339,7 +377,8 @@ class Deva {
339
377
 
340
378
  if (clear) {
341
379
  this.state('loop', `${key}:clear:${data.id.uid}`);
342
- for (let item of config.invoke.exit.clear) {
380
+ for (let item of clear) {
381
+ if (item === 'listeners') this._removeListeners();
343
382
  this[item] = false;
344
383
  }
345
384
  }
@@ -693,9 +732,9 @@ class Deva {
693
732
  this.action('listen', evt);
694
733
  this.intent('good', `listen:${evt}`);
695
734
  this.listeners[evt] = callback;
696
- return this.events.on(evt, packet => {
697
- return this.listeners[evt](packet);
698
- });
735
+ const handler = packet => this.listeners[evt](packet);
736
+ this._bound.set(evt, handler);
737
+ return this.events.on(evt, handler);
699
738
  }
700
739
 
701
740
  /**************
@@ -821,14 +860,51 @@ class Deva {
821
860
  this.action('ask', `${key}:${id.uid}`);
822
861
  this.intent('good', `ask:${key}:${id.uid}`);
823
862
  this.once(`${key}:ask:${packet.id.uid}`, answer => {
824
- this.action('talk', `${this._events.ask}:${id.uid}`);
825
- this.intent('good', `${this._events.ask}:${id.uid}`);
826
- this.talk(this._events.ask, this.lib.copy(answer));
827
- this.action('return', `finish:${key}:ask:${id.uid}`);
828
- this.intent('good', `finish:${method}:${id.uid}`);
829
- return this.finish(answer, resolve); // if:isAsk resolve the answer from the call
863
+ const evt = `${key}:ask:${packet.id.uid}`; // the once event keyed to this packet.
864
+ const pending = {evt, handler: false, timer: false}; // tracked so shutdown can clean up.
865
+
866
+ // single place to release the listener, the timer, and the pending record.
867
+ const release = () => {
868
+ clearTimeout(pending.timer);
869
+ this._pending.delete(pending);
870
+ if (this.events) this.ignore(evt, pending.handler);
871
+ };
872
+
873
+ pending.handler = answer => {
874
+ clearTimeout(pending.timer); // an answer arrived so cancel the timeout.
875
+ this._pending.delete(pending);
876
+ this.action('talk', `${this._events.ask}:${id.uid}`);
877
+ this.intent('good', `${this._events.ask}:${id.uid}`);
878
+ this.talk(this._events.ask, this.lib.copy(answer));
879
+ this.action('return', `finish:${key}:ask:${id.uid}`);
880
+ this.intent('good', `finish:${method}:${id.uid}`);
881
+ return this.finish(answer, resolve); // if:isAsk resolve the answer from the call
882
+ };
883
+
884
+ this._pending.add(pending);
885
+ this.once(evt, pending.handler);
886
+
887
+ // timeout so a silent Deva cannot hang the caller. 0 disables it.
888
+ const wait = this._config.timeout.ask;
889
+ if (wait > 0) {
890
+ pending.timer = setTimeout(() => {
891
+ if (!this._active) return; // shutdown already cleaned up.
892
+ release();
893
+ this.state('abort', `${key}:${method}:${id.uid}`);
894
+ this.intent('bad', `ask:${key}:${id.uid}`);
895
+ return resolve(this._messages.asktimeout);
896
+ }, wait);
897
+ }
898
+
899
+ // talk returns false when nobody is listening so fail fast instead of waiting.
900
+ const heard = this.talk(`${key}:ask`, this.lib.copy(packet));
901
+ if (!heard) {
902
+ release();
903
+ this.state('invalid', `${key}:${method}:${id.uid}`);
904
+ this.intent('bad', `ask:${key}:${id.uid}`);
905
+ return resolve(this._messages.noask);
906
+ }
830
907
  });
831
- this.talk(`${key}:ask`, this.lib.copy(packet));
832
908
  }
833
909
  else { // else: answer the question locally
834
910
  this.state('answer', `${method}:${id.uid}`); //set the answer state to the method
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "id": "23221940810208468106",
3
3
  "name": "@indra.ai/deva",
4
- "version": "1.54.10",
4
+ "version": "1.55.0",
5
5
  "description": "Deva Core a Vedic-inspired Event Based Context Aware Feature, Zone, Action, and State Machine integrated Artificial Intelligence Framework",
6
6
  "main": "index.js",
7
7
  "license": "VLA:23221940810208468106 LICENSE.md",