theprogrammablemind 9.5.1-beta.10 → 9.5.1-beta.13

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/client.js CHANGED
@@ -916,7 +916,7 @@ const defaultInnerProcess = (config, errorHandler, responses) => {
916
916
  console.log('')
917
917
  const screen_width = process.stdout.columns
918
918
  // || 0 for when running without a console
919
- const widths = [70, 10, Math.max(80, screen_width - 71 || 0)]
919
+ const widths = Lines.addRemainder([70, 10])
920
920
  const lines = new Lines(widths)
921
921
  lines.setElement(0, 0, '--- The paraphrases are ----------')
922
922
  lines.setElement(0, 2, '--- The response strings are ----------')
@@ -2011,9 +2011,15 @@ const ensureTestFile = (module, name, type) => {
2011
2011
  }
2012
2012
 
2013
2013
  const knowledgeModule = async (...args) => {
2014
- await knowledgeModuleImpl(...args).catch((e) => {
2014
+ await knowledgeModuleImpl(...args).catch(async (e) => {
2015
2015
  console.error(e)
2016
- process.exit(-1)
2016
+ function sleep(ms) {
2017
+ return new Promise((resolve) => {
2018
+ setTimeout(resolve, ms);
2019
+ });
2020
+ }
2021
+ await sleep(1) // get the stderr to flush
2022
+ await process.exit(-1); // tiny trick: empty write forces flush of console.error buffer
2017
2023
  })
2018
2024
  }
2019
2025
 
package/lines.js CHANGED
@@ -5,7 +5,7 @@ class Lines {
5
5
  this.rows = []
6
6
  }
7
7
 
8
- static SCREEN_WIDTH = 132
8
+ static SCREEN_WIDTH = 164
9
9
 
10
10
  static addRemainder(widths) {
11
11
  const sum = widths.reduce((a, b) => a + b)
package/package.json CHANGED
@@ -73,6 +73,6 @@
73
73
  "sort-json": "^2.0.0",
74
74
  "uuid": "^8.3.2"
75
75
  },
76
- "version": "9.5.1-beta.10",
76
+ "version": "9.5.1-beta.13",
77
77
  "license": "UNLICENSED"
78
78
  }
package/src/config.js CHANGED
@@ -10,7 +10,7 @@ const { ecatch } = require('./helpers')
10
10
  const runtime = require('../runtime')
11
11
  const _ = require('lodash')
12
12
  const db = require('./debug')
13
- const { fragmentInstantiator } = require('./fragments')
13
+ const { fragmentInstantiator, fragmentMapperInstantiator } = require('./fragments')
14
14
 
15
15
  const debugBreak = () => {
16
16
  // debugger
@@ -402,11 +402,15 @@ const handleBridgeProps = (config, bridge, { addFirst, uuid } = {}) => {
402
402
  if (bridge.generatorp) {
403
403
  const match = bridge.generatorp.match || (() => true)
404
404
  const apply = typeof bridge.generatorp === 'function' ? bridge.generatorp : bridge.generatorp.apply || bridge.generatorp
405
- const level = bridge.generatorp.level >= 0 ? bridge.generatorp.level : bridge.level + 1
405
+ let level = bridge.generatorp.level >= 0 ? bridge.generatorp.level : bridge.level + 1
406
+ if (!bridge.bridge) {
407
+ level = 0
408
+ }
406
409
 
407
410
  const generator = {
408
411
  where: bridge.generatorp.where || bridge.where || helpers.where(4),
409
- match: async (args) => bridge.id === args.context.marker && args.context.level === level && args.context.paraphrase && await match(args),
412
+ // match: async (args) => bridge.id === args.context.marker && args.context.level === level && args.context.paraphrase && await match(args),
413
+ match: async (args) => args.isA(args.context.marker, bridge.id) && args.context.level === level && args.context.paraphrase && await match(args),
410
414
  apply: (args) => apply(args),
411
415
  applyWrapped: apply,
412
416
  property: 'generatorp'
@@ -423,7 +427,8 @@ const handleBridgeProps = (config, bridge, { addFirst, uuid } = {}) => {
423
427
  const level = bridge.generatorr.level >= 0 ? bridge.generatorr.level : bridge.level + 1
424
428
  const generator = {
425
429
  where: bridge.generatorr.where || bridge.where || helpers.where(4),
426
- match: async (args) => bridge.id === args.context.marker && args.context.level === level && !args.context.paraphrase && (args.context.response || args.context.isResponse) && await match(args),
430
+ // match: async (args) => bridge.id === args.context.marker && args.context.level === level && !args.context.paraphrase && (args.context.response || args.context.isResponse) && await match(args),
431
+ match: async (args) => args.isA(args.context.marker, bridge.id) && args.context.level === level && !args.context.paraphrase && (args.context.response || args.context.isResponse) && await match(args),
427
432
  apply: (args) => apply(args),
428
433
  applyWrapped: apply,
429
434
  property: 'generatorr'
@@ -903,6 +908,10 @@ class Config {
903
908
  return config_toServer(config)
904
909
  }
905
910
 
911
+ async run(handler) {
912
+ return configHelpers.run(this, handler)
913
+ }
914
+
906
915
  async fixtures () {
907
916
  if (this.testConfig?.fixtures) {
908
917
  const args = {}
@@ -1108,26 +1117,42 @@ class Config {
1108
1117
  return instance
1109
1118
  }
1110
1119
 
1111
- fragment (args, query) {
1120
+ getFragment(query) {
1112
1121
  for (const instance of (this.instances || [])) {
1113
1122
  for (const fragment of (instance.fragments || [])) {
1114
1123
  if (fragment.query === query) {
1115
- return fragmentInstantiator(args, fragment.contexts)
1124
+ return fragment
1116
1125
  }
1117
1126
  }
1118
1127
  for (const fragment of (instance.resultss || [])) {
1119
1128
  if (fragment.isFragment && fragment.query === query) {
1120
- return fragmentInstantiator(args, fragment.contexts)
1129
+ return fragment
1121
1130
  }
1122
1131
  }
1123
1132
  for (const fragment of (this.fragmentsBeingBuilt || [])) {
1124
1133
  if (fragment.query === query) {
1125
- return fragmentInstantiator(args, fragment.contexts)
1134
+ return fragment
1126
1135
  }
1127
1136
  }
1128
1137
  }
1129
1138
  }
1130
1139
 
1140
+ fragment (args, query) {
1141
+ const fragment = this.getFragment(query)
1142
+ if (fragment) {
1143
+ return fragmentInstantiator(args, fragment.contexts)
1144
+ }
1145
+ }
1146
+
1147
+ fragmentMapper (args, values, fromModelQuery, toModelQuery) {
1148
+ const fromModelFragment = this.getFragment(fromModelQuery)
1149
+ console.dir(fromModelFragment)
1150
+ const toModelFragment = this.getFragment(toModelQuery)
1151
+ console.dir(toModelFragment)
1152
+ const mapper = fragmentMapperInstantiator(values, fromModelFragment.contexts, toModelFragment.contexts)
1153
+ return mapper
1154
+ }
1155
+
1131
1156
  // { rebuild: false, isModule: false }
1132
1157
  needsRebuild (template, instance, options) {
1133
1158
  if (options.rebuild) {
@@ -1486,6 +1511,11 @@ class Config {
1486
1511
  }
1487
1512
  }
1488
1513
 
1514
+ updateBridge(id, updater) {
1515
+ const bridge = this.config.bridges.find((b) => b.id === id)
1516
+ updater({ config: this, bridge })
1517
+ }
1518
+
1489
1519
  addBridge (bridge, uuid) {
1490
1520
  if (!this.config.bridges) {
1491
1521
  this.config.bridges = []
@@ -1787,7 +1817,7 @@ class Config {
1787
1817
 
1788
1818
  // set the args in the api's
1789
1819
  setArgs (args) {
1790
- const setArgs = (config) => {
1820
+ const setConfigArgs = (config) => {
1791
1821
  if (!config._api) {
1792
1822
  return
1793
1823
  }
@@ -1800,10 +1830,10 @@ class Config {
1800
1830
  }
1801
1831
  }
1802
1832
 
1803
- setArgs(this)
1833
+ setConfigArgs(this)
1804
1834
  for (const config of this.configs) {
1805
1835
  if (config.config instanceof Config) {
1806
- setArgs(config.config)
1836
+ setConfigArgs(config.config)
1807
1837
  }
1808
1838
  }
1809
1839
  }
@@ -1832,6 +1862,16 @@ class Config {
1832
1862
  }
1833
1863
  }
1834
1864
 
1865
+ getObjects () {
1866
+ const configs = {}
1867
+ const ns = this.config.objects.namespaced
1868
+ configs[this.name] = this
1869
+ for (const config of this.configs) {
1870
+ configs[config._name] = ns[config._uuid]
1871
+ }
1872
+ return configs
1873
+ }
1874
+
1835
1875
  getConfig (name) {
1836
1876
  if (this.name === name) {
1837
1877
  return this
@@ -121,6 +121,9 @@ const setupArgs = (args, config, logs, hierarchy, uuidForScoping) => {
121
121
  args.fragments = (query) => {
122
122
  return config.fragment(args, query)
123
123
  }
124
+ args.fragmentMapper = (values, fromModelQuery, toModelQuery) => {
125
+ return config.fragmentMapper(args, values, fromModelQuery, toModelQuery)
126
+ }
124
127
  args.breakOnSemantics = false
125
128
  args.theDebugger = {
126
129
  breakOnSemantics: (value) => args.breakOnSemantics = value
@@ -141,15 +144,27 @@ const setupArgs = (args, config, logs, hierarchy, uuidForScoping) => {
141
144
  return { ...args, assumed: Object.assign({}, assumed, (args.assumed || {}), ...moreAssumed) }
142
145
  }
143
146
 
144
- args.s = (c) => config.getSemantics(logs).apply(args, c)
145
- args.g = (c, a = {}) => {
146
- return config.getGenerators(logs).apply(addAssumed(args, a), c, a)
147
+ args.s = (c, options = {}) => config.getSemantics(logs).apply(args, c, options)
148
+ args.g = (c, rest = {}) => {
149
+ // if (JSON.stringify(rest) !== '{}' && !(rest.assumed || rest.options)) {
150
+ // debugger
151
+ // }
152
+ const { assumed = {}, options = {} } = rest
153
+ return config.getGenerators(logs).apply(addAssumed(args, assumed), c, assumed, options)
147
154
  }
148
- args.gp = (c, a = {}) => {
149
- return config.getGenerators(logs).apply(addAssumed(args, a, { paraphrase: true, isResponse: false, response: false }), c, { paraphrase: true, isResponse: false, response: false })
155
+ args.gp = (c, rest = {}) => {
156
+ // if (JSON.stringify(rest) !== '{}' && !(rest.assumed || rest.options)) {
157
+ // debugger
158
+ // }
159
+ const { assumed = {}, options = {} } = rest
160
+ return config.getGenerators(logs).apply(addAssumed(args, assumed, { paraphrase: true, isResponse: false, response: false }), c, { paraphrase: true, isResponse: false, response: false }, options)
150
161
  }
151
- args.gr = (c, a = {}) => {
152
- return config.getGenerators(logs).apply(addAssumed(args, a, { paraphrase: false, isResponse: true }), { ...c, paraphrase: false, isResponse: true })
162
+ args.gr = (c, rest = {}) => {
163
+ // if (JSON.stringify(rest) !== '{}' && !(rest.assumed || rest.options)) {
164
+ // debugger
165
+ // }
166
+ const { assumed = {}, options = {} } = rest
167
+ return config.getGenerators(logs).apply(addAssumed(args, assumed, { paraphrase: false, isResponse: true }), { ...c, paraphrase: false, isResponse: true }, options)
153
168
  }
154
169
  args.e = (c) => {
155
170
  if (!c) {
@@ -177,13 +192,14 @@ const setupArgs = (args, config, logs, hierarchy, uuidForScoping) => {
177
192
  args.getUUIDScoped = (uuid) => {
178
193
  return {
179
194
  api: getAPI(uuid),
180
- apis: getAPIs(uuid)
195
+ // apis: getAPIs(uuid)
181
196
  }
182
197
  }
183
198
  config.getAddedArgs(args)
184
199
 
185
200
  Object.assign(args, args.getUUIDScoped(uuidForScoping || config.uuid))
186
201
  args.apis = args.apis || ((name) => config.getConfig(name).api)
202
+ // args.apis = (name) => config.getAPIs(name)
187
203
  /*
188
204
  if (uuidForScoping) {
189
205
  Object.assign(args, args.getUUIDScoped(uuidForScoping))
@@ -202,6 +218,22 @@ const getObjects = (objects) => {
202
218
  }
203
219
  }
204
220
 
221
+ const run = async (config, handler) => {
222
+ // map to hash
223
+ config = config || {}
224
+ if (config.config) {
225
+ config = config
226
+ }
227
+
228
+ const hierarchy = new DigraphInternal((config.config || {}).hierarchy || [])
229
+
230
+ const objects = config.config.objects.namespaced[config.uuid]
231
+ const logs = []
232
+ const args = {}
233
+ setupArgs(args, config, logs, hierarchy)
234
+ return handler(args)
235
+ }
236
+
205
237
  const processContext = async (context, { objects = {}, config, logs = [] }) => {
206
238
  const generators = config.getGenerators(logs)
207
239
  const semantics = config.getSemantics(logs)
@@ -516,6 +548,7 @@ module.exports = {
516
548
  // listable,
517
549
  setupArgs,
518
550
  processContext,
551
+ run,
519
552
  getObjects,
520
553
  gs,
521
554
  processContextsB,
package/src/fragments.js CHANGED
@@ -3,7 +3,9 @@ const helpers = require('./helpers')
3
3
 
4
4
  function fragmentInstantiator (args, contexts) {
5
5
  return new Object({
6
- contexts: () => contexts,
6
+ contexts: () => {
7
+ return _.cloneDeep(contexts)
8
+ },
7
9
  instantiate: async (mappings) => {
8
10
  const instantiated = _.cloneDeep(contexts)
9
11
  const todo = [{ context: instantiated, path: [] }]
package/src/generators.js CHANGED
@@ -1,4 +1,3 @@
1
- const { stringify } = require('flatted');
2
1
  const { args: contextArgs, normalizeGenerator } = require('./helpers')
3
2
  const Lines = require('../lines')
4
3
  const helpers = require('./helpers')
@@ -186,7 +185,7 @@ class Generators {
186
185
  // this.logs.push(`Generators: applied ${generator.toString()}\n to\n ${stringify(context)}`)
187
186
  let errorMessage = 'The apply function did not return a value'
188
187
  try {
189
- generated = await generator.apply(args, objects, context, hierarchy, config, response, log)
188
+ generated = await generator.apply(args, objects, context, hierarchy, config, response, log, options)
190
189
  } catch (e) {
191
190
  // the next if handle this
192
191
  generated = null
@@ -213,7 +212,7 @@ class Generators {
213
212
  lines.newRow()
214
213
  lines.setElement(0, 1, 'TO')
215
214
  lines.setElement(0, 2, `context_id: ${context.context_id}`)
216
- lines.setElement(1, 2, stringify(helpers.sortJson(context, { depth: 10 }), null, 2))
215
+ lines.setElement(1, 2, JSON.stringify(helpers.sortJson(context, { depth: 10 }), null, 2))
217
216
  lines.newRow()
218
217
  lines.setElement(0, 1, 'STACK')
219
218
  lines.setElement(0, 2, stack)
@@ -224,7 +223,7 @@ class Generators {
224
223
  lines.setElement(0, 1, 'ERROR')
225
224
  lines.setElement(0, 2, errorMessage)
226
225
  this.logs.push(lines.toString())
227
- const message = `ERROR while applying (${source}) ${generator.toLabel()}\n to\n ${stringify(context, null, 2)}.\n${errorMessage}'`
226
+ const message = `ERROR while applying (${source}) ${generator.toLabel()}\n to\n ${JSON.stringify(context, null, 2)}.\n${errorMessage}'`
228
227
  // this.logs.push(message)
229
228
  // return [message]
230
229
  args.calls.pop()
@@ -255,7 +254,7 @@ class Generators {
255
254
  lines.newRow()
256
255
  lines.setElement(0, 1, 'TO')
257
256
  lines.setElement(0, 2, `context_id: ${context.context_id}`)
258
- lines.setElement(1, 2, stringify(helpers.sortJson(context, { depth: 25 }), null, 2))
257
+ lines.setElement(1, 2, JSON.stringify(helpers.sortJson(context, { depth: 25 }), null, 2))
259
258
  this.logs.push(lines.toString())
260
259
  }
261
260
  applied = true
@@ -273,7 +272,7 @@ class Generators {
273
272
  lines.setElement(0, 2, stack)
274
273
  lines.newRow()
275
274
  lines.setElement(0, 1, 'TO')
276
- lines.setElement(0, 2, stringify(context, null, 2))
275
+ lines.setElement(0, 2, JSON.stringify(context, null, 2))
277
276
  this.logs.push(lines.toString())
278
277
  }
279
278
  return ((config || {}).parenthesized ? '(' + generated + ')' : generated)
package/src/semantics.js CHANGED
@@ -70,6 +70,7 @@ class Semantic {
70
70
  }
71
71
 
72
72
  async matches (args, context, options = {}) {
73
+ args = {...args}
73
74
  this.fixUpArgs(args, context)
74
75
  const matches = await this.matcher(args)
75
76
  if (matches && (options.debug || {}).match || args.callId === this.callId) {
@@ -81,6 +82,7 @@ class Semantic {
81
82
  }
82
83
 
83
84
  async apply (args, context, s, options = {}) {
85
+ args = {...args}
84
86
  const { config } = args
85
87
  if (config && config.debugLoops) {
86
88
  console.log('apply', this.toLabel())