@indra.ai/deva 1.54.17 → 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.
Files changed (2) hide show
  1. package/index.js +44 -7
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -860,14 +860,51 @@ class Deva {
860
860
  this.action('ask', `${key}:${id.uid}`);
861
861
  this.intent('good', `ask:${key}:${id.uid}`);
862
862
  this.once(`${key}:ask:${packet.id.uid}`, answer => {
863
- this.action('talk', `${this._events.ask}:${id.uid}`);
864
- this.intent('good', `${this._events.ask}:${id.uid}`);
865
- this.talk(this._events.ask, this.lib.copy(answer));
866
- this.action('return', `finish:${key}:ask:${id.uid}`);
867
- this.intent('good', `finish:${method}:${id.uid}`);
868
- 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
+ }
869
907
  });
870
- this.talk(`${key}:ask`, this.lib.copy(packet));
871
908
  }
872
909
  else { // else: answer the question locally
873
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.17",
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",