@indra.ai/deva 1.56.4 → 1.59.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 (3) hide show
  1. package/config/index.js +1 -0
  2. package/index.js +80 -164
  3. package/package.json +1 -1
package/config/index.js CHANGED
@@ -20,6 +20,7 @@ export default {
20
20
  timeout: {
21
21
  ask: 30000
22
22
  },
23
+ hashes: ['md5', 'sha256', 'sha512'],
23
24
  container: {
24
25
  begin: '::BEGIN',
25
26
  end: '::END',
package/index.js CHANGED
@@ -18,6 +18,7 @@ class Deva {
18
18
  this._id = opts.id || randomUUID(); // the unique id assigned to the agent at load
19
19
  this._info = opts.info || false; // the deva information from the package file.
20
20
  this._config = opts.config || {}; // local Config Object
21
+ this._config.hashes = config.hashes; // set hashes array from config for stamping.
21
22
  this._config.ready_hash = config.ready_hash; // items to hash at ready
22
23
  this._config.timeout = {...config.timeout, ...this._config.timeout}; // add timeout values to _config.
23
24
 
@@ -282,7 +283,7 @@ class Deva {
282
283
  const client = this.client() || false;
283
284
  const {meta, params} = packet.q;
284
285
  const text = `${this._messages.method_not_found} ${agent.key} ${meta.method}`
285
- packet.a = {
286
+ const packet_answer = {
286
287
  id,
287
288
  agent,
288
289
  client,
@@ -293,22 +294,11 @@ class Deva {
293
294
  },
294
295
  created: Date.now(),
295
296
  };
297
+ // set packet into packet.a after stamping it.
298
+ packet.a = this._stamp(packet_answer);
296
299
 
297
- this.action('hash', `md5:${packet.a.id.uid}`);
298
- packet.a.md5 = this.hash(packet.a, 'md5');
299
- this.action('hash', `sha256:${packet.a.id.uid}`);
300
- packet.a.sha256 = this.hash(packet.a, 'sha256');
301
- this.action('hash', `sha512:${packet.a.id.uid}`);
302
- packet.a.sha512 = this.hash(packet.a, 'sha512');
303
-
304
- delete packet.md5;
305
- delete packet.sha256;
306
- delete packet.sha512;
307
-
308
- packet.md5 = this.hash(packet, 'md5');
309
- packet.sha256 = this.hash(packet, 'sha256');
310
- packet.sha512 = this.hash(packet, 'sha512');
311
-
300
+ // stamp the packet before return
301
+ const packet_stamped = this._stamp(this._unstamp(packet), `methodNotFound:${meta.method}`, packet.id.uid);
312
302
  this.state('invalid', `${meta.method}:${packet.id.uid}`);
313
303
  return packet;
314
304
  }
@@ -354,28 +344,12 @@ class Deva {
354
344
  this.zone(key, data.id.uid);
355
345
  this.action(key, data.id.uid);
356
346
  this.state(key, data.id.uid);
357
-
358
- this.action('delete', `${key}:md5:${data.id.uid}`);
359
- delete data.md5;
360
-
361
- this.action('delete', `${key}:sha256:${data.id.uid}`);
362
- delete data.sha256;
363
-
364
- this.action('delete', `${key}:sha512:${data.id.uid}`);
365
- delete data.sha512;
366
-
347
+
367
348
  this.state('data', `${key}:date:${data.id.uid}`);
368
349
  data[key] = Date.now();// set the complete date on the whole data.
369
350
 
370
- this.action('hash', `${key}:md5:${data.id.uid}`);
371
- data.md5 = this.hash(data, 'md5');
372
-
373
- this.action('hash', `${key}:sha256:${data.id.uid}`)
374
- data.sha256 = this.hash(data, 'sha256');
351
+ const data_stamped = this._stamp(this._unstamp(data), key, data.id.uid);
375
352
 
376
- this.action('hash', `${key}:sha512:${data.id.uid}`)
377
- data.sha512 = this.hash(data, 'sha512');
378
-
379
353
  // setup the complete talk event
380
354
  this.action('talk', `${this._events[key]}:${data.id.uid}`); // action talk for the event.
381
355
  this.talk(this._events[key], this.lib.copy(data)); // talk the complete event
@@ -405,6 +379,37 @@ class Deva {
405
379
  return next_key ? this[next_key](data, resolve) : resolve(data);
406
380
  }
407
381
 
382
+ /**************
383
+ func: _stamp
384
+ params:
385
+ - data: the data to stamp
386
+ - label: the label for the hash action.
387
+ describe: the stamp function will take a data object and apply the hashes from the this._config.hashes array.
388
+ usage: this._stamp(data, 'value')
389
+ ***************/
390
+ _stamp(data, label='', uid='') {
391
+ for (const algo of this._config.hashes) {
392
+ if (label) this.action('hash', `${label}:${algo}:${uid}`);
393
+ data[algo] = this.hash(data, algo);
394
+ }
395
+ return data;
396
+ }
397
+
398
+ /**************
399
+ func: _unstamp
400
+ params:
401
+ - data: the data to unstamp
402
+ describe: the unstamp function will take a data object and remove the hashes over the this._hashes array.
403
+ usage: this._unstamp(data)
404
+ ***************/
405
+ _unstamp(data) {
406
+ for (const algo of this._config.hashes) {
407
+ this.action('delete', `${algo}:${data.id.uid}`);
408
+ delete data[algo]
409
+ };
410
+ return data;
411
+ }
412
+
408
413
  /**************
409
414
  func: Client
410
415
  params: client - client provided data.
@@ -845,16 +850,8 @@ class Deva {
845
850
  created: Date.now(), // timestamp the question
846
851
  }
847
852
 
848
- // hash the question
849
-
850
- this.action('hash', `${method}:md5:${id.uid}`);
851
- packet.q.md5 = this.hash(packet.q, 'md5');
852
-
853
- this.action('hash', `${method}:sha256:${id.uid}`)
854
- packet.q.sha256 = this.hash(packet.q, 'sha256');
855
-
856
- this.action('hash', `${method}:sha512:${id.uid}`)
857
- packet.q.sha512 = this.hash(packet.q, 'sha512');
853
+ // stamp the question
854
+ packet.q = this._stamp(packet.q, method, id.uid);
858
855
 
859
856
  this.action('talk', `${this._events.question}:${id.uid}`);
860
857
  this.intent('good', `${this._events.question}:${id.uid}`);
@@ -988,18 +985,8 @@ class Deva {
988
985
  data, // set the answer data
989
986
  created: Date.now(), // set the created date for the answer
990
987
  };
991
- // create a hash for the answer and insert into answer meta.
992
- this.action('hash', `${key}:${method}:${subkey}:md5:${id.uid}`);
993
- packet_answer.md5 = this.hash(packet_answer, 'md5');
994
-
995
- this.action('hash', `${key}:${method}:${subkey}:sha256:${id.uid}`);
996
- packet_answer.sha256 = this.hash(packet_answer, 'sha256');
997
-
998
- this.action('hash', `${key}:${method}:${subkey}:sha512:${id.uid}`);
999
- packet_answer.sha512 = this.hash(packet_answer, 'sha512');
1000
-
1001
- this.state('set', `${key}:${method}:${subkey}:${id.uid}`);
1002
- packet.a = packet_answer; // set the packet.a to the packet_answer
988
+ // stamp the packet_answer into packet.a
989
+ packet.a = this._stamp(packet_answer, `${key}:${method}:${subkey}`, id.uid); // set the packet.a to the packet_answer
1003
990
 
1004
991
  this.action('talk', `${this._events.answer}:${id.uid}`);
1005
992
  this.talk(this._events.answer, this.lib.copy(packet)); // global talk event
@@ -1087,46 +1074,26 @@ class Deva {
1087
1074
  this.state('set', `${key}:${method}:${sub_key}:result:${packet.id.uid}`);
1088
1075
  packet_answer.text = result;
1089
1076
  }
1090
-
1091
- // delete previous hashes before creating new ones.
1092
- this.action('delete', `${key}:${method}:packet:md5:${packet.id.uid}`);
1093
- delete packet.md5;
1094
- this.action('delete', `${key}:${method}:packet:sha256:${packet.id.uid}`);
1095
- delete packet.sha256;
1096
- this.action('delete', `${key}:${method}:packet:sha512:${packet.id.uid}`);
1097
- delete packet.sha512;
1098
1077
 
1099
- this.action('hash', `${key}:${method}:${sub_key}:md5:${packet.id.uid}`);
1100
- packet_answer.md5 = this.hash(packet_answer, 'md5'); // md5 the answer.
1101
- this.action('hash', `${key}:${method}:${sub_key}:sha256:${packet.id.uid}`);
1102
- packet_answer.sha256 = this.hash(packet_answer, 'sha256'); // sha256 the answer.
1103
- this.action('hash', `${key}:${method}:${sub_key}:sha512:${packet.id.uid}`);
1104
- packet_answer.sha512 = this.hash(packet_answer, 'sha512'); // sha512 the answer
1078
+ // stamp the packet_answer and set it into packet.a.
1079
+ this.state('set', `${key}:${method}:packet:a:${packet.id.uid}`);
1080
+ packet.a = this._stamp(packet_answer, `${key}:${method}:packet:a`, packet.id.uid); // set the packet answer.
1105
1081
 
1106
1082
  this.state('set', `${key}:${method}:packet:q:agent:${packet.id.uid}`);
1107
1083
  packet.q.agent = agent; // set the question agent as the ask agent.
1108
-
1109
- this.state('set', `${key}:${method}:packet:a:${packet.id.uid}`);
1110
- packet.a = packet_answer; // set the packet answer.
1111
1084
 
1112
1085
  // create new hashes for the packet before return.
1113
- // delete previous hashes before creating new ones.
1114
- this.action('hash', `${key}:${method}:packet:md5:${packet.id.uid}`);
1115
- packet.md5 = this.hash(packet, 'md5');
1116
- this.action('hash', `${key}:${method}:packet:sha256:${packet.id.uid}`);
1117
- packet.sha256 = this.hash(packet, 'sha256');
1118
- this.action('hash', `${key}:${method}:packet:sha512:${packet.id.uid}`);
1119
- packet.sha512 = this.hash(packet, 'sha512');
1120
-
1086
+ const packet_stamped = this._stamp(this._unstamp(packet), `${key}:${method}:packet`, packet.id.uid);
1087
+
1121
1088
  this.action('talk', `${this._events.answer}:${key}:${method}:${packet.id.uid}`);
1122
1089
  this.state('valid', `${this._events.answer}:${key}:${method}:${packet.id.uid}`);
1123
1090
  this.intent('good', `${this._events.answer}:${key}:${method}:${packet.id.uid}`);
1124
- this.talk(this._events.answer, this.lib.copy(packet)); // global talk event
1091
+ this.talk(this._events.answer, this.lib.copy(packet_stamped)); // global talk event
1125
1092
 
1126
1093
  this.action('talk', `${agent.key}:${key}:${method}:${packet.id.uid}`);
1127
1094
  this.state('valid', `${agent.key}:${key}:${method}:${packet.id.uid}`);
1128
1095
  this.intent('good', `${agent.key}:${key}:${method}:${packet.id.uid}`);
1129
- this.talk(`${agent.key}:ask:${packet.id.uid}`, packet);
1096
+ this.talk(`${agent.key}:ask:${packet.id.uid}`, packet_stamped);
1130
1097
  }).catch(err => {
1131
1098
  this.action('talk', `${agent.key}:${key}:${method}:err:${packet.id.uid}`);
1132
1099
  this.state('catch', `${agent.key}:${key}:${method}:${packet.id.uid}`);
@@ -1550,12 +1517,10 @@ class Deva {
1550
1517
  created: Date.now(),
1551
1518
  };
1552
1519
 
1553
- data.md5 = this.hash(data, 'md5');
1554
- data.sha256 = this.hash(data, 'sha256');
1555
- data.sha512 = this.hash(data, 'sha512');
1556
-
1557
- this.talk(this._events.context, this.lib.copy(data));
1558
- return data;
1520
+ const data_stamped = this._stamp(data, `context`, id.uid);
1521
+
1522
+ this.talk(this._events.context, this.lib.copy(data_stamped));
1523
+ return data_stamped;
1559
1524
  } catch (e) {
1560
1525
  this.state('catch', `context:${value}:${id.uid}`);
1561
1526
  return this.err(e, value);
@@ -1578,15 +1543,8 @@ class Deva {
1578
1543
  created: Date.now(),
1579
1544
  };
1580
1545
 
1581
- this.action('hash', `${data.key}:md5:${data.id.uid}`);
1582
- data.md5 = this.hash(data, 'md5');
1583
- this.action('hash', `${data.key}:sha256:${data.id.uid}`);
1584
- data.sha256 = this.hash(data, 'sha256');
1585
- this.action('hash', `${data.key}:sha512:${data.id.uid}`);
1586
- data.sha512 = this.hash(data, 'sha512');
1587
-
1588
- this.state('return', `${data.key}:${id.uid}`);
1589
- return data;
1546
+ this.state('return', `${key}:${id.uid}`);
1547
+ return this._stamp(data, key, id.uid);
1590
1548
  }
1591
1549
 
1592
1550
  /**************
@@ -1645,7 +1603,7 @@ class Deva {
1645
1603
  describe: Returns a list of available actions in the system.
1646
1604
  ***************/
1647
1605
  actions() {
1648
- this._workflowItems('actions');
1606
+ return this._workflowItems('actions');
1649
1607
  }
1650
1608
 
1651
1609
  /**************
@@ -1702,7 +1660,7 @@ class Deva {
1702
1660
  describe: returns the available beliefs values.
1703
1661
  ***************/
1704
1662
  beliefs() {
1705
- return this._workflowItems('intents');
1663
+ return this._workflowItems('beliefs');
1706
1664
  }
1707
1665
 
1708
1666
  /**************
@@ -1731,11 +1689,12 @@ class Deva {
1731
1689
  client: this.client(), // set the client
1732
1690
  created: Date.now(), // set the data created date.
1733
1691
  };
1734
-
1692
+
1693
+ // manual hashing to avoid _stamp conflict at load.
1735
1694
  data.md5 = this.hash(data, 'md5');
1736
1695
  data.sha256 = this.hash(data, 'sha256');
1737
1696
  data.sha512 = this.hash(data, 'sha512');
1738
-
1697
+
1739
1698
  this.talk(this._events[key], this.lib.copy(data)); // broadcast the state event
1740
1699
  return data;
1741
1700
  } catch (e) { // catch any errors
@@ -1752,7 +1711,7 @@ class Deva {
1752
1711
  if (!this._active) return this._messages.offline;
1753
1712
  const id = this.uid(); // set the current uid
1754
1713
  this.action(key, id.uid); // set the action to the passed key
1755
- const value = this[_key]; // get the _keys value
1714
+ const value = this[`_${key}`]; // get the _keys value
1756
1715
  // set the data packet for the states
1757
1716
  this.state('data', `${key}:${id.uid}`);
1758
1717
  const data = {
@@ -1764,15 +1723,8 @@ class Deva {
1764
1723
  created: Date.now(),
1765
1724
  }
1766
1725
 
1767
- this.action('hash', `${data.key}:md5:${data.id.uid}`);
1768
- data.md5 = this.hash(data, 'md5');
1769
- this.action('hash', `${data.key}:sha256:${data.id.uid}`);
1770
- data.sha256 = this.hash(data, 'sha256');
1771
- this.action('hash', `${data.key}:sha512:${data.id.uid}`);
1772
- data.sha512 = this.hash(data, 'sha512');
1773
-
1774
1726
  this.action('return', `${data.key}:${id.uid}`);
1775
- return data;
1727
+ return this._stamp(data, data.key, id.uid);
1776
1728
  }
1777
1729
 
1778
1730
  /**************
@@ -1784,6 +1736,7 @@ class Deva {
1784
1736
  client() {
1785
1737
  if (!this._active) return this._messages.offline; // check the active status
1786
1738
  const data = this.lib.copy(this._client); // create a copy of the client data
1739
+ // manual hashing here to prevent stack overflow with this._stamp.
1787
1740
  data.md5 = this.hash(data, 'md5');
1788
1741
  data.sha256 = this.hash(data, 'sha256');
1789
1742
  data.sha512 = this.hash(data, 'sha512');
@@ -1799,6 +1752,7 @@ class Deva {
1799
1752
  agent() {
1800
1753
  if (!this._active) return this._messages.offline; // check the active status
1801
1754
  const data = this.lib.copy(this._agent); // create a copy of the agent data.
1755
+ // manual hashing here to prevent stack overflow with this._stamp.
1802
1756
  data.md5 = this.hash(data, 'md5');
1803
1757
  data.sha256 = this.hash(data, 'sha256');
1804
1758
  data.sha512 = this.hash(data, 'sha512');
@@ -2064,20 +2018,15 @@ class Deva {
2064
2018
  created: Date.now(),
2065
2019
  }
2066
2020
 
2067
- this.action('hash', `${key}:${value}:md5:${data.id.uid}`);
2068
- data.md5 = this.hash(data, 'md5'); // md5 the data packet
2069
-
2070
- this.action('hash', `${key}:${value}:sha256:${data.id.uid}`);
2071
- data.sha256 = this.hash(data, 'sha256'); // sha256 the data packet
2072
-
2073
- this.action('hash', `${key}:${value}:sha512:${data.id.uid}`);
2074
- data.sha512 = this.hash(data, 'sha512'); // sha512 the data packet
2021
+ // stamp the data
2022
+ const data_stamped = this._stamp(data, `${key}:${value}`, data.id.uid);
2075
2023
 
2024
+ // talk the prompt event to anyone listening
2076
2025
  this.action('talk', `${key}:${value}:${id.uid}`);
2077
- this.talk(this._events.prompt, data);
2026
+ this.talk(this._events.prompt, data_stamped);
2078
2027
 
2079
2028
  this.action('return', `${key}:${value}:${id.uid}`);
2080
- return data;
2029
+ return data_stamped;
2081
2030
  }
2082
2031
 
2083
2032
  /**************
@@ -2094,18 +2043,10 @@ class Deva {
2094
2043
  const data = this.lib.copy(this._core);
2095
2044
  data.id = id;
2096
2045
  data.created = Date.now();
2097
-
2098
- delete data.md5; // delete old md5 hash
2099
- delete data.sha256; // delete old sha256 hash
2100
- delete data.sha512; // delete old sha512 hash
2101
-
2102
- data.md5 = this.hash(data, 'md5');
2103
- data.sha256 = this.hash(data, 'sha256');
2104
- data.sha512 = this.hash(data, 'sha512');
2105
2046
 
2106
2047
  this.action('return', `core:${id.uid}`);
2107
2048
  this.intent('good', `core:${id.uid}`);
2108
- return data;
2049
+ return this._stamp(this._unstamp(data), 'core', data.id.uid);
2109
2050
  }
2110
2051
 
2111
2052
  // machine returns the sha256 machine fingerprint
@@ -2124,6 +2065,7 @@ class Deva {
2124
2065
  cpus: os.cpus(),
2125
2066
  created: Date.now(),
2126
2067
  };
2068
+ // machine has to do manual hashing to prevent stack overflow.
2127
2069
  data.md5 = this.hash(data, 'md5');
2128
2070
  data.sha256 = this.hash(data, 'sha256');
2129
2071
  data.sha512 = this.hash(data, 'sha512');
@@ -2140,16 +2082,9 @@ class Deva {
2140
2082
  const data = this.lib.copy(this._info);
2141
2083
  data.created = Date.now();
2142
2084
 
2143
- this.action('hash', `info:md5:${data.id}`);
2144
- data.md5 = this.hash(data, 'md5');
2145
- this.action('hash', `info:sha256:${data.id}`);
2146
- data.sha256 = this.hash(data, 'sha256');
2147
- this.action('hash', `info:sha512:${data.id}`);
2148
- data.sha512 = this.hash(data, 'sha512');
2149
-
2150
2085
  this.action('return', `info:${data.id}`);
2151
2086
  this.intent('good', `info:${data.id}`);
2152
- return data;
2087
+ return this._stamp(data, 'info', data.id);
2153
2088
  }
2154
2089
 
2155
2090
  /**************
@@ -2288,21 +2223,13 @@ class Deva {
2288
2223
  },
2289
2224
  packet,
2290
2225
  created: Date.now(),
2291
- }
2292
-
2293
- this.action('hash', `${data.key}:${data.value}:md5:${data.id.uid}`);
2294
- data.md5 = this.hash(data, 'md5'); // md5 the data packet
2295
-
2296
- this.action('hash', `${data.key}:${data.value}:sha256:${data.id.uid}`);
2297
- data.sha256 = this.hash(data, 'sha256'); // sha256 the data packet
2298
-
2299
-
2300
- this.action('hash', `${data.key}:${data.value}:sha512:${data.id.uid}`);
2301
- data.sha512 = this.hash(data, 'sha512'); // sha512 the data packet
2226
+ };
2227
+ // stamp the data before return
2228
+ const data_stamped = this._stamp(data, `${data.key}:${data.value}`, data.id.uid);
2302
2229
 
2303
2230
  // set the action to talk and emit the talk error.
2304
2231
  this.action('talk', `${this._events.error}:${data.id.uid}`);
2305
- this.talk(this._events.error, this.lib.copy(data));
2232
+ this.talk(this._events.error, this.lib.copy(data_stamped));
2306
2233
 
2307
2234
  const hasOnError = this.onError && typeof this.onError === 'function' ? true : false;
2308
2235
  // if block handles returning the onError and setting the state to hasOnError
@@ -2401,6 +2328,7 @@ class Deva {
2401
2328
  const uid = `${begin_random}${end_random}`; // set uid to local constant
2402
2329
  data.uid = uid; // set base data object.
2403
2330
  }
2331
+ // manual hashing to prevent overflow on stack calls.
2404
2332
  data.md5 = this.hash(data, 'md5'); // md5 the uid and created.
2405
2333
  data.sha256 = this.hash(data, 'sha256'); // sha256 the uid, created, md5
2406
2334
  data.sha512 = this.hash(data, 'sha512'); // sha512 the uid, created, md5, sha256.
@@ -2478,18 +2406,9 @@ class Deva {
2478
2406
  copyright,
2479
2407
  };
2480
2408
 
2481
- this.action('hash', `${data.key}:sign:md5:${data.id.uid}`);
2482
- data.md5 = this.hash(data, 'md5'); // hash data packet into md5 and inert into data.
2483
-
2484
- this.action('hash', `${data.key}:sign:sha256:${data.id.uid}`);
2485
- data.sha256 = this.hash(data, 'sha256'); // hash data into sha 256 then set in data.
2486
-
2487
- this.action('hash', `${data.key}:sign:sha512:${data.id.uid}`);
2488
- data.sha512 = this.hash(data, 'sha512'); // hash data into sha 512 then set in data.
2489
-
2490
2409
  this.action('return', `${data.key}:sign:${data.id.uid}`);
2491
2410
  this.intent('good', `${data.key}:sign:${data.id.uid}`);
2492
- return data;
2411
+ return this._stamp(data, 'sign', data.id.uid);
2493
2412
  }
2494
2413
 
2495
2414
  /**************
@@ -2529,13 +2448,10 @@ class Deva {
2529
2448
  personal: personalVLA_hash,
2530
2449
  package: packageVLA_hash,
2531
2450
  };
2532
- approved.md5 = this.hash(approved, 'md5');
2533
- approved.sha256 = this.hash(approved, 'sha256');
2534
- approved.sha512 = this.hash(approved, 'sha512');
2535
2451
 
2536
2452
  this.state('return', `license:${packageVLA.uid}`);
2537
2453
  this.intent('good', `license:${packageVLA.uid}`);
2538
- return approved;
2454
+ return this._stamp(approved, 'license_check', packageVLA.uid);
2539
2455
  }
2540
2456
 
2541
2457
  /**************
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "id": "23221940810208468106",
3
3
  "name": "@indra.ai/deva",
4
- "version": "1.56.4",
4
+ "version": "1.59.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",