@indra.ai/deva 1.54.17 → 1.56.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.
Files changed (2) hide show
  1. package/index.js +38 -3
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -859,15 +859,50 @@ class Deva {
859
859
  // if: isAsk wait for the once event which is key'd to the packet ID for specified responses
860
860
  this.action('ask', `${key}:${id.uid}`);
861
861
  this.intent('good', `ask:${key}:${id.uid}`);
862
- this.once(`${key}:ask:${packet.id.uid}`, answer => {
862
+ const evt = `${key}:ask:${packet.id.uid}`; // the once event keyed to this packet.
863
+ const pending = {evt, handler: false, timer: false}; // tracked so shutdown can clean up.
864
+
865
+ // single place to release the listener, the timer, and the pending record.
866
+ const release = () => {
867
+ clearTimeout(pending.timer);
868
+ this._pending.delete(pending);
869
+ if (this.events) this.ignore(evt, pending.handler);
870
+ };
871
+
872
+ pending.handler = answer => {
873
+ clearTimeout(pending.timer); // an answer arrived so cancel the timeout.
874
+ this._pending.delete(pending);
863
875
  this.action('talk', `${this._events.ask}:${id.uid}`);
864
876
  this.intent('good', `${this._events.ask}:${id.uid}`);
865
877
  this.talk(this._events.ask, this.lib.copy(answer));
866
878
  this.action('return', `finish:${key}:ask:${id.uid}`);
867
879
  this.intent('good', `finish:${method}:${id.uid}`);
868
880
  return this.finish(answer, resolve); // if:isAsk resolve the answer from the call
869
- });
870
- this.talk(`${key}:ask`, this.lib.copy(packet));
881
+ };
882
+
883
+ this._pending.add(pending);
884
+ this.once(evt, pending.handler);
885
+
886
+ // timeout so a silent Deva cannot hang the caller. 0 disables it.
887
+ const wait = this._config.timeout.ask;
888
+ if (wait > 0) {
889
+ pending.timer = setTimeout(() => {
890
+ if (!this._active) return; // shutdown already cleaned up.
891
+ release();
892
+ this.state('abort', `${key}:${method}:${id.uid}`);
893
+ this.intent('bad', `ask:${key}:${id.uid}`);
894
+ return resolve(this._messages.asktimeout);
895
+ }, wait);
896
+ }
897
+
898
+ // talk returns false when nobody is listening so fail fast instead of waiting.
899
+ const heard = this.talk(`${key}:ask`, this.lib.copy(packet));
900
+ if (!heard) {
901
+ release();
902
+ this.state('invalid', `${key}:${method}:${id.uid}`);
903
+ this.intent('bad', `ask:${key}:${id.uid}`);
904
+ return resolve(this._messages.noask);
905
+ }
871
906
  }
872
907
  else { // else: answer the question locally
873
908
  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.17",
4
+ "version": "1.56.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",