theprogrammablemind_4wp 7.4.0-beta.1 → 7.4.0-beta.2

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 +69 -42
  2. package/package.json +1 -1
  3. package/src/config.js +47 -2
package/client.js CHANGED
@@ -103,8 +103,23 @@ const setupArgs = (args, config, logs, hierarchy) => {
103
103
  args.asList = asList
104
104
  args.retry = () => { throw new RetryError() }
105
105
  const scopedAsk = getAsk(config)
106
+
107
+ const getAPI = (uuid) => {
108
+ if (config && config.getAPI) {
109
+ return config.getAPI(uuid)
110
+ }
111
+ }
112
+ const getAPIs = (uuid) => {
113
+ if (config && config.getAPIs) {
114
+ return config.getAPIs(uuid)
115
+ }
116
+ }
106
117
  args.getUUIDScoped = (uuid) => {
107
- return { ask: scopedAsk(uuid) }
118
+ return {
119
+ ask: scopedAsk(uuid),
120
+ api: getAPI(uuid),
121
+ apis: getAPIs(uuid)
122
+ }
108
123
  }
109
124
  args.motivation = (m) => config.addMotivation(m)
110
125
  args.s = (c) => config.getSemantics(logs).apply(args, c)
@@ -491,7 +506,7 @@ const processInstance = (config, instance) => {
491
506
  global.transitoryMode = transitoryMode
492
507
  }
493
508
 
494
- const _process = async (config, query, { credentials, writeTests, isTest, saveDeveloper, testConfig, testsFN, errorHandler = defaultErrorHandler, beforeQuery } = {}) => {
509
+ const _process = async (config, query, { credentials, writeTests, isTest, saveDeveloper, testConfig, testsFN, errorHandler = defaultErrorHandler } = {}) => {
495
510
  if (credentials) {
496
511
  config.server(credentials.server, credentials.key)
497
512
  }
@@ -504,7 +519,7 @@ const _process = async (config, query, { credentials, writeTests, isTest, saveDe
504
519
  if (writeTests) {
505
520
  config.rebuild()
506
521
  const objects = getObjects(config.config.objects)(config.uuid)
507
- beforeQuery({ query, isModule: false, objects })
522
+ config.beforeQuery({ query, isModule: false, objects })
508
523
  }
509
524
  } catch(error) {
510
525
  throw error
@@ -605,7 +620,7 @@ const getConfigForTest = (config, testConfig) => {
605
620
  return configForTest
606
621
  }
607
622
 
608
- const runTest = async (config, expected, { verbose, beforeQuery, afterTest, testConfig, debug }) => {
623
+ const runTest = async (config, expected, { verbose, afterTest, testConfig, debug }) => {
609
624
  const test = expected.query
610
625
  // initialize in between test so state is not preserved since the test was adding without state
611
626
  config.rebuild()
@@ -623,7 +638,7 @@ const runTest = async (config, expected, { verbose, beforeQuery, afterTest, test
623
638
  }
624
639
 
625
640
  const objects = getObjects(config.config.objects)(config.uuid)
626
- beforeQuery({ query: test, isModule: false, objects })
641
+ config.beforeQuery({ query: test, isModule: false, objects })
627
642
  config.resetMotivations()
628
643
  try {
629
644
  const result = await _process(config, test, { errorHandler, isTest: true })
@@ -649,8 +664,8 @@ const runTest = async (config, expected, { verbose, beforeQuery, afterTest, test
649
664
  const failed_config = !matching(actual_config, expected_config)
650
665
  let failed = failed_paraphrases || failed_responses || failed_contexts || failed_objects || failed_config
651
666
  if (!failed) {
652
- if (afterTest) {
653
- failed = afterTest({ query: test, expected, actual: result, config })
667
+ if (config.afterTest) {
668
+ failed = config.afterTest({ query: test, expected, actual: result, config })
654
669
  if (failed) {
655
670
  return {
656
671
  utterance: test,
@@ -710,21 +725,20 @@ const runTestsHelper = async (config, tests, failed, juicyBits) => {
710
725
 
711
726
  const runTests = async (config, testFile, juicyBits) => {
712
727
  const tests = JSON.parse(runtime.fs.readFileSync(testFile))
713
- const { beforeTests, afterTests } = juicyBits
714
- beforeTests()
728
+ config.beforeTests()
715
729
  if (juicyBits.verbose) {
716
730
  console.log('\n', testFile, '-----------------------------------------------', '\n')
717
731
  }
718
732
  const v = await runTestsHelper(config, [...tests], [], juicyBits)
719
- afterTests()
733
+ config.afterTests()
720
734
  return v
721
735
  }
722
736
 
723
- const saveTest = async (testFile, config, test, expected, beforeQuery, testConfig, saveDeveloper) => {
737
+ const saveTest = async (testFile, config, test, expected, testConfig, saveDeveloper) => {
724
738
  config.rebuild()
725
739
  const objects = getObjects(config.config.objects)(config.uuid)
726
740
  config.resetMotivations()
727
- beforeQuery({ query: test, isModule: false, objects })
741
+ config.beforeQuery({ query: test, isModule: false, objects })
728
742
  console.log(test)
729
743
  const result = await _process(config, test, { isTest: true })
730
744
  // const actualObjects = config.config.objects
@@ -732,23 +746,23 @@ const saveTest = async (testFile, config, test, expected, beforeQuery, testConfi
732
746
  writeTest(testFile, test, config.config.objects, result.generated, result.paraphrases, result.responses, result.contexts, result.associations, result.metadata, actualConfig, saveDeveloper)
733
747
  }
734
748
 
735
- const saveTestsHelper = async (testFile, config, tests, todo, beforeQuery, testConfig, saveDeveloper) => {
749
+ const saveTestsHelper = async (testFile, config, tests, todo, testConfig, saveDeveloper) => {
736
750
  if (todo.length === 0) {
737
751
  return
738
752
  }
739
753
  const test = todo.pop()
740
754
  config.rebuild()
741
- const result = await saveTest(testFile, config, test, tests[test], beforeQuery, testConfig, saveDeveloper)
755
+ const result = await saveTest(testFile, config, test, tests[test], testConfig, saveDeveloper)
742
756
  // initialize in between test so state is not preserved since the test was adding without state
743
757
  // config.initialize({force: true})
744
758
  config.rebuild()
745
- return saveTestsHelper(testFile, config, tests, todo, beforeQuery, testConfig, saveDeveloper)
759
+ return saveTestsHelper(testFile, config, tests, todo, testConfig, saveDeveloper)
746
760
  }
747
761
 
748
- const saveTests = (config, testFile, beforeQuery, testConfig) => {
762
+ const saveTests = (config, testFile, testConfig) => {
749
763
  const tests = JSON.parse(runtime.fs.readFileSync(testFile))
750
764
  console.log(testFile)
751
- return saveTestsHelper(testFile, config, tests, tests.map( (test) => test.query ), beforeQuery, testConfig)
765
+ return saveTestsHelper(testFile, config, tests, tests.map( (test) => test.query ), testConfig)
752
766
  }
753
767
 
754
768
  /*
@@ -969,7 +983,7 @@ entodicton.knowledgeModule( {
969
983
  `
970
984
  */
971
985
 
972
- const build = async ({ config, target, beforeQuery, template, errorHandler = defaultErrorHandler }) => {
986
+ const build = async ({ config, target, template, errorHandler = defaultErrorHandler }) => {
973
987
  const accumulators = {
974
988
  resultss: [],
975
989
  fragments: [],
@@ -1007,7 +1021,7 @@ const build = async ({ config, target, beforeQuery, template, errorHandler = def
1007
1021
  }
1008
1022
 
1009
1023
  try {
1010
- const results = await _process(config, query.query, { beforeQuery })
1024
+ const results = await _process(config, query.query, {})
1011
1025
  if (config.config.debug) {
1012
1026
  // TODO pass in the error handler like the other ones
1013
1027
  defaultInnerProcess(config, defaultErrorHandler, results)
@@ -1104,11 +1118,40 @@ const knowledgeModule = async ({
1104
1118
  afterTest = () => {}
1105
1119
  } = {}) => {
1106
1120
 
1107
- const isProcess = require.main === moduleFromJSFile
1121
+ config.beforeQuery = beforeQuery
1122
+ config.beforeTests = beforeTests
1123
+ config.afterTests = afterTests
1124
+ config.beforeTest = beforeTest
1125
+ config.afterTest = afterTest
1126
+
1108
1127
  const testConfig = test
1109
1128
 
1129
+ if (!moduleFromJSFile) {
1130
+ throw "'module' is a required parameter. The value should be either 'module' or a lambda that will be called when the file is acting as a module."
1131
+ }
1132
+ if (!config) {
1133
+ throw "'config' is a required parameter. The value should the config that defines the knowledge module."
1134
+ }
1135
+ if (!config.name) {
1136
+ throw "config must have 'name' set to the knowledge module name."
1137
+ }
1138
+ if (!description) {
1139
+ throw "'description' is a required parameter. The value should the description of the knowledge module."
1140
+ }
1141
+ if (!test) {
1142
+ throw "'test' is a required parameter. The value should the path to the file used to store the tests of the knowledge module and the contents of the file in the form { name: <filePath>, contexts: <json> }."
1143
+ }
1144
+
1145
+ const isProcess = require.main === moduleFromJSFile
1146
+ let loadForTesting = false
1147
+ if (global.theprogrammablemind) {
1148
+ if (global.theprogrammablemind.loadForTesting[config.name]) {
1149
+ loadForTesting = true
1150
+ }
1151
+ }
1152
+
1110
1153
  // remove test only stuff
1111
- if (!isProcess) {
1154
+ if (!isProcess && !loadForTesting) {
1112
1155
  config.config.operators = config.config.operators.filter( (operator) => {
1113
1156
  if (operator.development) {
1114
1157
  return false
@@ -1125,22 +1168,6 @@ const knowledgeModule = async ({
1125
1168
  })
1126
1169
  }
1127
1170
 
1128
- if (!moduleFromJSFile) {
1129
- throw "'module' is a required parameter. The value should be either 'module' or a lambda that will be called when the file is acting as a module."
1130
- }
1131
- if (!config) {
1132
- throw "'config' is a required parameter. The value should the config that defines the knowledge module."
1133
- }
1134
- if (!config.name) {
1135
- throw "config must have 'name' set to the knowledge module name."
1136
- }
1137
- if (!description) {
1138
- throw "'description' is a required parameter. The value should the description of the knowledge module."
1139
- }
1140
- if (!test) {
1141
- throw "'test' is a required parameter. The value should the path to the file used to store the tests of the knowledge module and the contents of the file in the form { name: <filePath>, contexts: <json> }."
1142
- }
1143
-
1144
1171
  let module
1145
1172
  if (_.isFunction(moduleFromJSFile)) {
1146
1173
  module = moduleFromJSFile
@@ -1179,6 +1206,7 @@ const knowledgeModule = async ({
1179
1206
  description: 'Entodicton knowledge module'
1180
1207
  })
1181
1208
 
1209
+ parser.add_argument('-tfn', '--testFileName', { help: 'Override the test file for the module when the tests are being run' })
1182
1210
  parser.add_argument('-t', '--test', { action: 'store_true', help: 'Run the tests. Create tests by running with the --query + --save flag' })
1183
1211
  parser.add_argument('-tv', '--testVerbose', { action: 'store_true', help: 'Run the tests in verbose mode. Create tests by running with the --query or --loop with the --save flag' })
1184
1212
  parser.add_argument('-tva', '--testAllVerbose', { action: 'store_true', help: 'Run the tests in verbose mode. All the tests will be run instead of stopping at first failure. Create tests by running with the --query or --loop with the --save flag' })
@@ -1373,9 +1401,8 @@ const knowledgeModule = async ({
1373
1401
 
1374
1402
  if (!args.query && !args.test && !args.info && (args.save || args.saveDeveloper)) {
1375
1403
  global.transitoryMode = true
1376
- saveTests(config, test, beforeQuery, testConfig, args.saveDeveloper)
1404
+ saveTests(config, test, testConfig, args.saveDeveloper)
1377
1405
  // } else if (args.build) {
1378
- // build({ config, target: args.build, beforeQuery, errorHandler })
1379
1406
  } else if (args.info) {
1380
1407
  showInfo(description, section, config)
1381
1408
  } else if (args.test || args.testVerbose || args.testAllVerbose) {
@@ -1388,7 +1415,7 @@ const knowledgeModule = async ({
1388
1415
  }
1389
1416
  return
1390
1417
  }
1391
- runTests(config, test, { debug: args.debug, testConfig: testConfig, verbose: args.testVerbose || args.testAllVerbose, stopAtFirstError: !args.testAllVerbose, beforeQuery, beforeTests, afterTests, beforeTest, afterTest }).then((results) => {
1418
+ runTests(config, args.testFileName ? `${args.testFileName}.test.json` : test, { debug: args.debug, testConfig: testConfig, verbose: args.testVerbose || args.testAllVerbose, stopAtFirstError: !args.testAllVerbose }).then((results) => {
1392
1419
  if (results.length > 0 && args.vimdiff) {
1393
1420
  for (const result of results) {
1394
1421
  vimdiff(result.expected, result.actual)
@@ -1461,9 +1488,9 @@ const knowledgeModule = async ({
1461
1488
  if (args.objectDiff) {
1462
1489
  global.beforeObjects = _.cloneDeep(objects)
1463
1490
  }
1464
- beforeQuery({ query: args.query, isModule: false, objects })
1491
+ config.beforeQuery({ query: args.query, isModule: false, objects })
1465
1492
  try {
1466
- processResults(_process(config, args.query, { dontAddAssociations: args.dontAddAssociations, writeTests: args.save || args.saveDeveloper, saveDeveloper: args.saveDeveloper, testConfig, testsFN: test, beforeQuery }))
1493
+ processResults(_process(config, args.query, { dontAddAssociations: args.dontAddAssociations, writeTests: args.save || args.saveDeveloper, saveDeveloper: args.saveDeveloper, testConfig, testsFN: test }))
1467
1494
  } catch( error ) {
1468
1495
  console.log('Error', error);
1469
1496
  }
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.0-beta.1",
64
+ "version": "7.4.0-beta.2",
65
65
  "license": "ISC"
66
66
  }
package/src/config.js CHANGED
@@ -767,6 +767,23 @@ class Config {
767
767
  }
768
768
  }
769
769
 
770
+ getAPIs (uuid) {
771
+ let config
772
+ if (this._uuid === uuid) {
773
+ config = this
774
+ } else {
775
+ for (const km of this.configs) {
776
+ if (km._uuid === uuid) {
777
+ config = km.config
778
+ break
779
+ }
780
+ }
781
+ }
782
+ if (config && config._api && config._api.multiApi) {
783
+ return config._api.apis
784
+ }
785
+ }
786
+
770
787
  getServer() {
771
788
  return this._server
772
789
  }
@@ -888,6 +905,19 @@ class Config {
888
905
  return configs;
889
906
  }
890
907
 
908
+ getConfigByUUID (uuid) {
909
+ if (this.uuid === uuid) {
910
+ return this
911
+ }
912
+ for (const config of this.configs) {
913
+ if (config.config instanceof Config) {
914
+ if (config.uuid === uuid) {
915
+ return config.config
916
+ }
917
+ }
918
+ }
919
+ }
920
+
891
921
  getConfig (name) {
892
922
  if (this.name === name) {
893
923
  return this
@@ -932,7 +962,13 @@ class Config {
932
962
  }
933
963
 
934
964
  this.addedArgss = []
935
- const isProcess = require.main === module
965
+ let isProcess = require.main === module
966
+ if (global.theprogrammablemind && config) {
967
+ if (global.theprogrammablemind.loadForTesting[config.name]) {
968
+ isProcess = true
969
+ this.loadedForTesting = true
970
+ }
971
+ }
936
972
  this.isModule = !isProcess
937
973
  if (this.isModule) {
938
974
  this.removeDevelopmentElements(config)
@@ -1260,6 +1296,7 @@ class Config {
1260
1296
  cp.tests = this.tests
1261
1297
  cp.motivations = this.motivations
1262
1298
  cp.isModule = this.isModule
1299
+ cp.loadedForTesting = this.loadedForTesting
1263
1300
  cp.initInstances = this.initInstances.slice()
1264
1301
  cp.instances = this.instances.slice()
1265
1302
  cp.configCounter = this.configCounter
@@ -1608,6 +1645,12 @@ class Config {
1608
1645
  // already set
1609
1646
  // this.isModule = this.isModule || mainIsModule
1610
1647
  mainIsModule = (mainIsModule === undefined) ? this.isModule : mainIsModule
1648
+ if (mainIsModule !== undefined) {
1649
+ this.isModule = mainIsModule
1650
+ }
1651
+ if (this.loadedForTesting) {
1652
+ this.isModule = false
1653
+ }
1611
1654
  this.config.objects.namespaced = {}
1612
1655
  this.resetWasInitialized()
1613
1656
 
@@ -1645,7 +1688,9 @@ class Config {
1645
1688
  if (!(config instanceof Config)) {
1646
1689
  config = this
1647
1690
  isSelf = true
1648
- isModule = mainIsModule
1691
+ isModule = config.isModule || mainIsModule
1692
+ } else {
1693
+ isModule = config.isModule
1649
1694
  }
1650
1695
  if (!isSelf) {
1651
1696
  config.config = _.cloneDeep(config.initConfig)