@knowlearning/agents 0.5.5 → 0.5.7

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@knowlearning/agents",
3
- "version": "0.5.5",
3
+ "version": "0.5.7",
4
4
  "description": "API for embedding applications in KnowLearning systems.",
5
5
  "main": "node.js",
6
6
  "browser": "browser.js",
@@ -4,18 +4,23 @@ import MutableProxy from './json.js'
4
4
 
5
5
  const copy = x => JSON.parse(JSON.stringify(x))
6
6
 
7
+ async function scopeIsUninitialized(scope) {
8
+ const { ii } = await Agent.metadata(scope)
9
+ return ii === 0
10
+ }
11
+
7
12
  export default async function (storeDefinition) {
8
13
  // Set up persistance
9
14
  const { scope } = await Agent.environment()
10
15
  let state = copy(await Agent.state(scope))
11
16
 
12
17
  const scopedPaths = getScopedPaths(storeDefinition)
13
- const stateAttachedStore = await attachModuleState(savedState, storeDefinition, scopedPaths)
18
+ const stateAttachedStore = await attachModuleState(state, storeDefinition, scopedPaths)
14
19
  const s = stateAttachedStore.state
15
20
  const originalState = s instanceof Function ? s() : s
16
21
  const handlePatch = patch => Agent.interact(scope, patch)
17
22
 
18
- if (Object.entries(state).length === 0) {
23
+ if (await scopeIsUninitialized(scope)) {
19
24
  state = copy(originalState)
20
25
  handlePatch([{ op: 'add', path: [], value: state }])
21
26
  }
@@ -58,14 +63,20 @@ async function attachModuleState(state, module, scopedPaths, path='') {
58
63
  // if our path is in scoped paths, return new Mutable proxy attached to scope
59
64
  const scope = scopedPaths[path]
60
65
  if (scope) {
61
- const handlePatch = patch => Agent.interact(scope, patch)
66
+ const handlePatch = patch => {
67
+ patch.forEach(({ path }) => path.unshift('active'))
68
+ return Agent.interact(scope, patch)
69
+ }
62
70
  const initState = await Agent.state(scope)
63
71
  const ephemeralPaths = descendantPaths(path, scopedPaths)
64
72
  state = MutableProxy(copy(initState), handlePatch, ephemeralPaths)
65
- if (Object.keys(initState).length === 0) Object.assign(state, module.state())
73
+ if (await scopeIsUninitialized(scope)) {
74
+ Object.assign(state, module.state())
75
+ }
76
+ return { ...module, state: () => state }
66
77
  }
78
+ else return module
67
79
 
68
- return state ? { ...module, state: () => state } : module
69
80
  }
70
81
 
71
82
  function getScopedPaths(module, path="", paths={}) {