theprogrammablemind_4wp 7.4.1-beta.2 → 7.4.1-beta.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. package/client.js +50 -9
  2. package/package.json +1 -1
  3. package/src/config.js +46 -7
package/client.js CHANGED
@@ -470,13 +470,22 @@ const doWithRetries = async (n, url, queryParams, data) => {
470
470
  }
471
471
  }
472
472
 
473
- const setupProcessB = ({ config }) => {
473
+ const setupProcessB = ({ config, allowDelta=false } = {}) => {
474
474
  const key = config._key
475
475
 
476
- const data = Object.assign({ key, version: '3' }, config.config, { uuid: config._uuid })
477
- for (const uuid of Object.keys(data.namespaces)) {
478
- const km = config.configs.find((km) => km.uuid === uuid)
479
- data.namespaces[uuid].name = km.name
476
+ const data = Object.assign({ key, version: '3' }, { uuid: config._uuid })
477
+ if (allowDelta && config.allowDelta && config.hasDelta()) {
478
+ // console.log('config', config)
479
+ data.delta = config.delta()
480
+ } else {
481
+ Object.assign(data, config.config)
482
+ }
483
+
484
+ if (data.namespaces) {
485
+ for (const uuid of Object.keys(data.namespaces)) {
486
+ const km = config.configs.find((km) => km.uuid === uuid)
487
+ data.namespaces[uuid].name = km.name
488
+ }
480
489
  }
481
490
 
482
491
  // const generators = new Generators((data.generators || []).map((g) => new Generator(normalizeGenerator(g))))
@@ -529,7 +538,7 @@ const _process = async (config, query, { credentials, writeTests, isTest, saveDe
529
538
  throw error
530
539
  }
531
540
 
532
- const { data, /* generators, semantics, */ hierarchy } = setupProcessB({ config })
541
+ let { data, /* generators, semantics, */ hierarchy } = setupProcessB({ config, allowDelta: true })
533
542
  let queries = query.split('\\n')
534
543
 
535
544
  try {
@@ -553,8 +562,28 @@ const _process = async (config, query, { credentials, writeTests, isTest, saveDe
553
562
  if (queries.length === 0) {
554
563
  break;
555
564
  }
565
+
556
566
  data.utterance = queries[0]
557
- const json = await doWithRetries(retries, url, queryParams, data)
567
+ let json = await doWithRetries(retries, url, queryParams, data)
568
+ let resetData = false
569
+ if (json.code == 'NOT_IN_CACHE') {
570
+ resetData = true
571
+ const setupB = setupProcessB({ config, allowDelta: false })
572
+ data = setupB.data
573
+ hierarchy = setupB.hierarchy
574
+ data.utterance = queries[0]
575
+ json = await doWithRetries(retries, url, queryParams, data)
576
+ }
577
+ if (json.cacheKey) {
578
+ config.cacheKey = json.cacheKey
579
+ if (resetData) {
580
+ if (queries.length > 1) {
581
+ const setupB = setupProcessB({ config, allowDelta: true })
582
+ data = setupB.data
583
+ hierarchy = setupB.hierarchy
584
+ }
585
+ }
586
+ }
558
587
  json.contexts = json.results
559
588
  delete json.results
560
589
  if (json.status !== 200) {
@@ -1329,7 +1358,7 @@ const knowledgeModule = async ({
1329
1358
  }
1330
1359
  }
1331
1360
  if (args.print.includes('c')) {
1332
- const { data } = setupProcessB({ config })
1361
+ const { data } = setupProcessB({ config })
1333
1362
  console.log("Config as sent to server")
1334
1363
  console.log(JSON.stringify(data, null, 2));
1335
1364
  }
@@ -1488,7 +1517,19 @@ const knowledgeModule = async ({
1488
1517
  if (!('then' in promise)) {
1489
1518
  throw 'Return a promise from process in the definition of knowledgeModule'
1490
1519
  }
1491
- promise.then(() => f()).catch( (e) => f() )
1520
+ promise
1521
+ .then(() => {
1522
+ f()
1523
+ })
1524
+ .catch( (e) => {
1525
+ if (e.errno == 'ECONNREFUSED') {
1526
+ console.log(e)
1527
+ readline.close()
1528
+ } else {
1529
+ console.log(e)
1530
+ f()
1531
+ }
1532
+ })
1492
1533
  })
1493
1534
  f()
1494
1535
  } else if (args.query) {
package/package.json CHANGED
@@ -61,6 +61,6 @@
61
61
  "json-stable-stringify": "^1.0.1",
62
62
  "node-fetch": "^2.6.1"
63
63
  },
64
- "version": "7.4.1-beta.2",
64
+ "version": "7.4.1-beta.4",
65
65
  "license": "ISC"
66
66
  }
package/src/config.js CHANGED
@@ -572,6 +572,7 @@ class Config {
572
572
  }
573
573
  }
574
574
  this.config.associations.positive.push(association)
575
+ this._delta.json.associations.push({ action: 'add', association })
575
576
  }
576
577
 
577
578
  // TODO add more error checking to these like addHierarchy has
@@ -580,6 +581,7 @@ class Config {
580
581
  this.config.priorities = []
581
582
  }
582
583
  this.config.priorities.push(priorities)
584
+ this._delta.json.priorities.push({ action: 'add', priorities })
583
585
  }
584
586
 
585
587
  addHierarchy (child, parent) {
@@ -603,6 +605,7 @@ class Config {
603
605
  }
604
606
  // this.config.hierarchy.push([properties.child, properties.parent])
605
607
  this.config.hierarchy.push(properties)
608
+ this._delta.json.hierarchy.push([child, parent])
606
609
  }
607
610
 
608
611
  addHierarchyChildParent (child, parent) {
@@ -630,6 +633,7 @@ class Config {
630
633
  }
631
634
 
632
635
  this.config.hierarchy.push([child, parent])
636
+ this._delta.json.hierarchy.push([child, parent])
633
637
  }
634
638
 
635
639
  getBridge (id, level) {
@@ -659,6 +663,7 @@ class Config {
659
663
  }
660
664
  bridges.push(def)
661
665
  this.checkBridges();
666
+ this._delta.json.bridges.push({ action: 'add', bridge: def })
662
667
  }
663
668
 
664
669
  addGenerator (match, apply) {
@@ -735,17 +740,19 @@ class Config {
735
740
 
736
741
  operators.unshift(operator)
737
742
  this.checkOperators()
743
+
744
+ this._delta.json.operators.push({ action: 'add', operator })
738
745
  }
739
746
 
740
747
  addWord (word, def) {
741
- this.addWordInternal(this.config, word, def)
748
+ this.addWordInternal(word, def)
742
749
  }
743
750
 
744
- addWordInternal (config, word, def) {
745
- if (!config.words) {
746
- config.words = {}
751
+ addWordInternal (word, def) {
752
+ if (!this.config.words) {
753
+ this.config.words = {}
747
754
  }
748
- const words = config.words
755
+ const words = this.config.words
749
756
  def = Object.assign({}, def, { uuid: this._uuid })
750
757
  if (words[word]) {
751
758
  if (!words[word].some((e) => helpers.safeEquals(e, def))) {
@@ -754,6 +761,8 @@ class Config {
754
761
  } else {
755
762
  words[word] = [def]
756
763
  }
764
+
765
+ this._delta.json.words.push({ action: 'add', word, def })
757
766
  }
758
767
 
759
768
  getAPI (uuid) {
@@ -970,6 +979,9 @@ class Config {
970
979
  throw 'Excepted the config argument to be a hash not a Config object'
971
980
  }
972
981
 
982
+ this.allowDelta = false
983
+ this.resetDelta()
984
+
973
985
  this.addedArgss = []
974
986
  let isProcess = require.main === module
975
987
  if (global.theprogrammablemind && config) {
@@ -1072,11 +1084,11 @@ class Config {
1072
1084
  if (bridge.words) {
1073
1085
  for (let def of bridge.words) {
1074
1086
  if (typeof def == 'string') {
1075
- this.addWordInternal(this.config, def, {"id": bridge.id, "initial": "{}" })
1087
+ this.addWordInternal(def, {"id": bridge.id, "initial": "{}" })
1076
1088
  } else {
1077
1089
  const word = def.word
1078
1090
  def = { initial: JSON.stringify(def), id: bridge.id, word: undefined }
1079
- this.addWordInternal(this.config, word, def)
1091
+ this.addWordInternal(word, def)
1080
1092
  }
1081
1093
  }
1082
1094
  }
@@ -1174,6 +1186,32 @@ class Config {
1174
1186
  }
1175
1187
  }
1176
1188
 
1189
+ hasDelta () {
1190
+ return this._delta.json
1191
+ }
1192
+
1193
+ delta () {
1194
+ return { cacheKey: this._delta.cacheKey, json: this._delta.json }
1195
+ }
1196
+
1197
+ resetDelta (cacheKey) {
1198
+ this._delta = {
1199
+ cacheKey,
1200
+ json: {
1201
+ words: [],
1202
+ operators: [],
1203
+ bridges: [],
1204
+ associations: [],
1205
+ priorities: [],
1206
+ hierarchy: [],
1207
+ }
1208
+ }
1209
+ }
1210
+
1211
+ set cacheKey (cacheKey) {
1212
+ this.resetDelta(cacheKey)
1213
+ }
1214
+
1177
1215
  get api () {
1178
1216
  if (this._api && this._api.multiApi) {
1179
1217
  return this._api.api(this._api)
@@ -1646,6 +1684,7 @@ class Config {
1646
1684
 
1647
1685
  // rebuild ({ isModule: mainIsModule = false } = {}) {
1648
1686
  rebuild ({ isModule: mainIsModule } = {}) {
1687
+ this.resetDelta()
1649
1688
  const debug = this.config.debug;
1650
1689
  this.config = _.cloneDeep(this.initConfig)
1651
1690
  if (debug) {