@indra.ai/deva 1.64.5 → 1.64.8
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 +17 -5
- package/package.json +1 -1
- package/tests/suite/05-messaging.test.js +122 -0
- package/tests/suite/06-shutdown.test.js +102 -0
package/index.js
CHANGED
|
@@ -224,6 +224,7 @@ class Deva {
|
|
|
224
224
|
for (const pending of this._pending) {
|
|
225
225
|
clearTimeout(pending.timer);
|
|
226
226
|
if (this.events) this.ignore(pending.evt, pending.handler);
|
|
227
|
+
pending.resolve(this._messages.offline); // the Deva is going offline so answer the waiting caller.
|
|
227
228
|
}
|
|
228
229
|
this._pending.clear();
|
|
229
230
|
}
|
|
@@ -767,7 +768,7 @@ class Deva {
|
|
|
767
768
|
this.action('ask', `${key}:${id.uid}`);
|
|
768
769
|
this.intent('good', `ask:${key}:${id.uid}`);
|
|
769
770
|
const evt = `${key}:ask:${packet.id.uid}`; // the once event keyed to this packet.
|
|
770
|
-
const pending = {evt, handler: false, timer: false}; // tracked so shutdown can clean up.
|
|
771
|
+
const pending = {evt, handler: false, timer: false, resolve}; // tracked so shutdown can clean up.
|
|
771
772
|
|
|
772
773
|
// single place to release the listener, the timer, and the pending record.
|
|
773
774
|
const release = () => {
|
|
@@ -779,6 +780,14 @@ class Deva {
|
|
|
779
780
|
pending.handler = answer => {
|
|
780
781
|
clearTimeout(pending.timer); // an answer arrived so cancel the timeout.
|
|
781
782
|
this._pending.delete(pending);
|
|
783
|
+
|
|
784
|
+
// the asked Deva failed so the asker handles the error like a local method error.
|
|
785
|
+
if (answer.error) {
|
|
786
|
+
this.state('catch', `${key}:${method}:${id.uid}`);
|
|
787
|
+
this.intent('bad', `ask:${key}:${id.uid}`);
|
|
788
|
+
return this.err(answer.error, packet, reject);
|
|
789
|
+
}
|
|
790
|
+
|
|
782
791
|
this.action('talk', `${this._events.ask}:${id.uid}`);
|
|
783
792
|
this.intent('good', `${this._events.ask}:${id.uid}`);
|
|
784
793
|
this.talk(this._events.ask, this.lib.copy(answer));
|
|
@@ -999,20 +1008,23 @@ class Deva {
|
|
|
999
1008
|
this.state('valid', `${agent.key}:${key}:${method}:${packet.id.uid}`);
|
|
1000
1009
|
this.intent('good', `${agent.key}:${key}:${method}:${packet.id.uid}`);
|
|
1001
1010
|
this.talk(`${agent.key}:ask:${packet.id.uid}`, packet_stamped);
|
|
1011
|
+
|
|
1002
1012
|
}).catch(err => {
|
|
1003
1013
|
this.action('talk', `${agent.key}:${key}:${method}:err:${packet.id.uid}`);
|
|
1004
1014
|
this.state('catch', `${agent.key}:${key}:${method}:${packet.id.uid}`);
|
|
1005
1015
|
this.intent('bad', `${agent.key}:${key}:${method}:${packet.id.uid}`);
|
|
1006
|
-
|
|
1007
|
-
|
|
1016
|
+
// reply on the asker's event so the asker hears the error right away.
|
|
1017
|
+
this.talk(`${agent.key}:${key}:${packet.id.uid}`, {error:err});
|
|
1018
|
+
// log the error here; the asker owns the reject.
|
|
1019
|
+
return this.err(err, packet, () => {});
|
|
1008
1020
|
})
|
|
1009
1021
|
}
|
|
1010
1022
|
catch (e) {
|
|
1011
1023
|
this.action('talk', `${agent.key}:${key}:${method}:err:${packet.id.uid}`);
|
|
1012
1024
|
this.state('catch', `${agent.key}:${key}:${method}:${packet.id.uid}`);
|
|
1013
1025
|
this.intent('bad', `${agent.key}:${key}:${method}:${packet.id.uid}`);
|
|
1014
|
-
this.talk(`${agent.key}
|
|
1015
|
-
return this.err(e, packet)
|
|
1026
|
+
this.talk(`${agent.key}:${key}:${packet.id.uid}`, {error:e});
|
|
1027
|
+
return this.err(e, packet, () => {});
|
|
1016
1028
|
}
|
|
1017
1029
|
// now when we ask the meta params[0] should be the method
|
|
1018
1030
|
}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"id": "31711622713594414221",
|
|
3
3
|
"name": "@indra.ai/deva",
|
|
4
|
-
"version": "1.64.
|
|
4
|
+
"version": "1.64.8",
|
|
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:31711622713594414221 LICENSE.md",
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// Deva Core Test Suite: step 5, commands, ask and answers
|
|
3
|
+
// Copyright ©2000-2026 Quinn Michaels; All rights reserved.
|
|
4
|
+
// Owner Signature Required For Lawful Use.
|
|
5
|
+
// Distributed under VLA:31711622713594414221 LICENSE.md
|
|
6
|
+
|
|
7
|
+
import test from 'node:test';
|
|
8
|
+
import assert from 'node:assert/strict';
|
|
9
|
+
import {build, client, agent} from './_deva.js';
|
|
10
|
+
|
|
11
|
+
const TIERS = ['md5', 'sha256', 'sha512'];
|
|
12
|
+
const fresh = () => structuredClone(client);
|
|
13
|
+
const stamped = (obj, label) => { for (const t of TIERS) assert.equal(typeof obj[t], 'string', `${label} ${t}`); };
|
|
14
|
+
|
|
15
|
+
// a child Deva that answers #helper shout
|
|
16
|
+
const helper = () => build({
|
|
17
|
+
agent: {...agent, id: 'helper', key: 'helper', profile: {...agent.profile, name: 'Helper'}},
|
|
18
|
+
methods: {
|
|
19
|
+
shout: packet => Promise.resolve({text: packet.q.text.toUpperCase()}),
|
|
20
|
+
fail: () => Promise.reject(new Error('helper failed')),
|
|
21
|
+
throws: () => { throw new Error('helper threw'); },
|
|
22
|
+
},
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
// the parent Deva under test
|
|
27
|
+
const deva = build({
|
|
28
|
+
config: {hash: {}, timeout: {ask: 200}},
|
|
29
|
+
devas: {helper: helper()},
|
|
30
|
+
methods: {
|
|
31
|
+
greet: packet => Promise.resolve({text: `hello ${packet.q.text}`, data: {to: packet.q.text}}),
|
|
32
|
+
plain: () => Promise.resolve('just text'),
|
|
33
|
+
},
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
test('question() answers offline before init()', async () => {
|
|
37
|
+
assert.equal(await deva.question('/greet world'), deva._messages.offline);
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
test.describe('after init', () => {
|
|
41
|
+
test.before(async () => { await deva.init(fresh()); });
|
|
42
|
+
test.after(async () => { await deva.stop(); });
|
|
43
|
+
|
|
44
|
+
test('question() with no text answers the no text message', async () => {
|
|
45
|
+
assert.equal(await deva.question(), deva._messages.notext);
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
test('a /command runs the local method and returns a stamped q and a packet', async () => {
|
|
49
|
+
const packet = await deva.question('/greet world');
|
|
50
|
+
assert.equal(packet.q.meta.method, 'greet');
|
|
51
|
+
assert.equal(packet.q.text, 'world');
|
|
52
|
+
assert.equal(packet.a.text, 'hello world');
|
|
53
|
+
assert.deepEqual(packet.a.data, {to: 'world'});
|
|
54
|
+
assert.equal(packet.a.meta.key, deva.agent().key);
|
|
55
|
+
stamped(packet.q, 'q');
|
|
56
|
+
stamped(packet.a, 'a');
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
test('a method that returns plain text sets a.text and leaves data false', async () => {
|
|
60
|
+
const packet = await deva.question('/plain');
|
|
61
|
+
assert.equal(packet.a.text, 'just text');
|
|
62
|
+
assert.equal(packet.a.data, false);
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
test('an unknown /command answers method not found', async () => {
|
|
66
|
+
const result = await deva.question('/nope here');
|
|
67
|
+
assert.match(result.a.text, new RegExp(`^${deva._messages.method_not_found}`));
|
|
68
|
+
assert.equal(result.a.meta.method, 'nope');
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
test('the question event broadcasts the packet', async () => {
|
|
72
|
+
let heard = false;
|
|
73
|
+
const on = packet => { heard = packet; };
|
|
74
|
+
deva.events.once('devacore:question', on);
|
|
75
|
+
await deva.question('/greet event');
|
|
76
|
+
assert.equal(heard.q.text, 'event');
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
test('#helper shout asks the child Deva and returns its stamped answer', async () => {
|
|
80
|
+
const packet = await deva.question('#helper shout hi there');
|
|
81
|
+
assert.equal(packet.a.text, 'HI THERE');
|
|
82
|
+
assert.equal(packet.a.meta.key, 'helper');
|
|
83
|
+
assert.equal(packet.a.meta.method, 'shout');
|
|
84
|
+
stamped(packet.a, 'a');
|
|
85
|
+
stamped(packet, 'packet');
|
|
86
|
+
assert.equal(deva._pending.size, 0, 'no pending asks left');
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
test('#helper with an unknown method answers method not found from the child', async () => {
|
|
90
|
+
const result = await deva.question('#helper nope hi');
|
|
91
|
+
assert.match(result.a.text, new RegExp(`^${deva._messages.method_not_found} helper nope`));
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
test('#helper fail rejects the asker right away with the child error', async () => {
|
|
95
|
+
const started = Date.now();
|
|
96
|
+
await assert.rejects(deva.question('#helper fail hi'), {message: 'helper failed'});
|
|
97
|
+
assert.ok(Date.now() - started < 150, 'did not wait for the ask timeout');
|
|
98
|
+
assert.equal(deva._pending.size, 0, 'the error cleans up the pending ask');
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
test('#helper throws rejects the asker right away with the child error', async () => {
|
|
102
|
+
await assert.rejects(deva.question('#helper throws hi'), {message: 'helper threw'});
|
|
103
|
+
assert.equal(deva._pending.size, 0, 'the error cleans up the pending ask');
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
test('#nobody resolves right away with ask target not found', async () => {
|
|
107
|
+
const started = Date.now();
|
|
108
|
+
assert.equal(await deva.question('#nobody shout hi'), deva._messages.noask);
|
|
109
|
+
assert.ok(Date.now() - started < 150, 'fails fast, before the ask timeout');
|
|
110
|
+
assert.equal(deva._pending.size, 0);
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
test('a silent ask target resolves with ask timeout after config.timeout.ask', async () => {
|
|
114
|
+
const silent = () => {};
|
|
115
|
+
deva.events.on('silent:ask', silent);
|
|
116
|
+
const started = Date.now();
|
|
117
|
+
assert.equal(await deva.question('#silent shout hi'), deva._messages.asktimeout);
|
|
118
|
+
assert.ok(Date.now() - started >= 190, 'waited for the timeout');
|
|
119
|
+
assert.equal(deva._pending.size, 0, 'timeout cleans up the pending ask');
|
|
120
|
+
deva.events.removeListener('silent:ask', silent);
|
|
121
|
+
});
|
|
122
|
+
});
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// Deva Core Test Suite: step 6, stop and the shutdown chain
|
|
3
|
+
// Copyright ©2000-2026 Quinn Michaels; All rights reserved.
|
|
4
|
+
// Owner Signature Required For Lawful Use.
|
|
5
|
+
// Distributed under VLA:31711622713594414221 LICENSE.md
|
|
6
|
+
|
|
7
|
+
import test from 'node:test';
|
|
8
|
+
import assert from 'node:assert/strict';
|
|
9
|
+
import {build, client} from './_deva.js';
|
|
10
|
+
|
|
11
|
+
const TIERS = ['md5', 'sha256', 'sha512'];
|
|
12
|
+
const CHAIN = ['stop', 'close', 'leave', 'exit', 'shutdown'];
|
|
13
|
+
const fresh = () => structuredClone(client);
|
|
14
|
+
|
|
15
|
+
test('stop() answers offline before init()', async () => {
|
|
16
|
+
const deva = build();
|
|
17
|
+
assert.equal(await deva.stop(), deva._messages.offline);
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
test('stop() runs the full chain and resolves the stamped stop data', async () => {
|
|
21
|
+
const deva = build();
|
|
22
|
+
await deva.init(fresh());
|
|
23
|
+
const agent = deva.agent();
|
|
24
|
+
const clientHash = deva.client().sha256;
|
|
25
|
+
const data = await deva.stop();
|
|
26
|
+
|
|
27
|
+
assert.equal(data.key, 'shutdown', 'the last step sets the key');
|
|
28
|
+
assert.equal(data.value, agent.key);
|
|
29
|
+
assert.equal(data.agent, agent.sha256);
|
|
30
|
+
assert.equal(data.client, clientHash);
|
|
31
|
+
for (const t of TIERS) assert.equal(typeof data[t], 'string', `stop data ${t}`);
|
|
32
|
+
// every step stamps its time, in chain order.
|
|
33
|
+
for (let i = 0; i < CHAIN.length; i++) {
|
|
34
|
+
assert.equal(typeof data[CHAIN[i]], 'number', `${CHAIN[i]} time`);
|
|
35
|
+
if (i) assert.ok(data[CHAIN[i]] >= data[CHAIN[i - 1]], `${CHAIN[i]} after ${CHAIN[i - 1]}`);
|
|
36
|
+
}
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
test('each step talks its devacore event in chain order', async () => {
|
|
40
|
+
const deva = build();
|
|
41
|
+
await deva.init(fresh());
|
|
42
|
+
const heard = [];
|
|
43
|
+
for (const key of CHAIN) deva.events.on(deva._events[key], packet => heard.push(packet.key));
|
|
44
|
+
await deva.stop();
|
|
45
|
+
assert.deepEqual(heard, CHAIN);
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
test('the on hooks run in chain order and the Deva is cleared by onShutdown', async () => {
|
|
49
|
+
const order = [];
|
|
50
|
+
const next = {onStop: 'close', onClose: 'leave', onLeave: 'exit', onExit: 'shutdown'};
|
|
51
|
+
const hooks = {};
|
|
52
|
+
for (const name of [...Object.keys(next), 'onShutdown']) {
|
|
53
|
+
hooks[name] = function (data, resolve) {
|
|
54
|
+
order.push([name, Boolean(this._active)]);
|
|
55
|
+
return next[name] ? this[next[name]](data, resolve) : resolve(data);
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
const deva = build(hooks);
|
|
59
|
+
await deva.init(fresh());
|
|
60
|
+
await deva.stop();
|
|
61
|
+
assert.deepEqual(order, [
|
|
62
|
+
['onStop', true], ['onClose', true], ['onLeave', true], ['onExit', true],
|
|
63
|
+
['onShutdown', false], // shutdown clears _active before its hook runs.
|
|
64
|
+
]);
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
test('after stop the Deva is offline and its cleared features are false', async () => {
|
|
68
|
+
const deva = build();
|
|
69
|
+
await deva.init(fresh());
|
|
70
|
+
await deva.stop();
|
|
71
|
+
assert.equal(deva._active, false);
|
|
72
|
+
for (const item of ['_license', '_security', '_support', '_services', '_systems', '_config', 'events']) {
|
|
73
|
+
assert.equal(deva[item], false, `${item} cleared`);
|
|
74
|
+
}
|
|
75
|
+
assert.equal(await deva.question('/hello'), deva._messages.offline);
|
|
76
|
+
assert.equal(await deva.stop(), deva._messages.offline, 'a second stop is offline');
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
test('stop removes every listener this Deva put on the event bus', async () => {
|
|
80
|
+
const deva = build();
|
|
81
|
+
await deva.init(fresh());
|
|
82
|
+
const bus = deva.events;
|
|
83
|
+
assert.ok(bus.eventNames().length > 0, 'init added listeners');
|
|
84
|
+
await deva.stop();
|
|
85
|
+
assert.deepEqual(bus.eventNames(), [], 'nothing left on the bus');
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
test('a pending ask at shutdown answers offline and leaves nothing behind', async () => {
|
|
89
|
+
const deva = build({config: {hash: {}, timeout: {ask: 0}}}); // no timeout, so only shutdown can end it.
|
|
90
|
+
await deva.init(fresh());
|
|
91
|
+
const bus = deva.events;
|
|
92
|
+
const silent = () => {};
|
|
93
|
+
bus.on('silent:ask', silent);
|
|
94
|
+
const asked = deva.question('#silent shout hi');
|
|
95
|
+
await new Promise(r => setImmediate(r)); // let the ask get on the bus.
|
|
96
|
+
assert.equal(deva._pending.size, 1, 'the ask is waiting');
|
|
97
|
+
await deva.stop();
|
|
98
|
+
assert.equal(await asked, deva._messages.offline, 'the waiting caller is answered');
|
|
99
|
+
assert.equal(deva._pending.size, 0);
|
|
100
|
+
bus.removeListener('silent:ask', silent);
|
|
101
|
+
assert.deepEqual(bus.eventNames(), []);
|
|
102
|
+
});
|