@knowlearning/agents 0.9.171 → 0.9.172

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.
@@ -91,21 +91,6 @@ export default function EmbeddedAgent() {
91
91
  return id
92
92
  }
93
93
 
94
- const tagTypeToTargetCache = {}
95
- async function tagIfNotYetTaggedInSession(tag_type, target) {
96
- const targetCache = tagTypeToTargetCache[tag_type]
97
- if (targetCache && targetCache[target]) return
98
-
99
- // always use absolute referene when tagging
100
- if (!isUUID(target)) target = (await metadata(target)).id
101
-
102
- if (!tagTypeToTargetCache[tag_type]) tagTypeToTargetCache[tag_type] = {}
103
- if (tagTypeToTargetCache[tag_type][target]) return
104
-
105
- tagTypeToTargetCache[tag_type][target] = true
106
- await tag(tag_type, target)
107
- }
108
-
109
94
  async function patch(root, scopes) {
110
95
  // TODO: consider watch function added to return to receive progress
111
96
  return send({ type: 'patch', root, scopes })
@@ -116,7 +101,6 @@ export default function EmbeddedAgent() {
116
101
  const { context } = await environment()
117
102
  scope = JSON.stringify(context)
118
103
  }
119
- tagIfNotYetTaggedInSession('subscribed', scope)
120
104
  const startState = await send({ type: 'state', scope, user, domain })
121
105
  return new PatchProxy(startState, patch => {
122
106
  // TODO: reject updates if user is not owner
@@ -130,10 +114,8 @@ export default function EmbeddedAgent() {
130
114
  return interact(scope, [{ op: 'add', path:['active'], value: null }])
131
115
  }
132
116
 
133
- // TODO: better approach than exposing addTag
134
- function interact(scope, patch, addTag=true) {
135
- if (addTag) tagIfNotYetTaggedInSession('mutated', scope)
136
- return send({ type: 'interact', scope, patch })
117
+ function interact(scope, patch, _, context) {
118
+ return send({ type: 'interact', scope, patch, context })
137
119
  }
138
120
 
139
121
  async function upload(info) {
@@ -213,10 +195,6 @@ export default function EmbeddedAgent() {
213
195
  })
214
196
  }
215
197
 
216
- function tag(tag_type, target, context=[]) {
217
- return send({ type: 'tag', tag_type, target, context })
218
- }
219
-
220
198
  function login(provider, username, password) {
221
199
  return send({ type: 'login', provider, username, password })
222
200
  }
@@ -247,7 +225,6 @@ export default function EmbeddedAgent() {
247
225
  reconnect,
248
226
  synced,
249
227
  close,
250
- query,
251
- tag
228
+ query
252
229
  }
253
230
  }
@@ -99,12 +99,11 @@ function embed(environment, iframe) {
99
99
  })
100
100
  }
101
101
  else if (type === 'interact') {
102
- let { scope, patch } = message
102
+ let { scope, patch, context=[] } = message
103
103
  const namespacedScope = getNamespacedScope(environment.namespace, scope)
104
104
  let before, after
105
105
  if (listeners.mutate) before = copy(await Agent.state(namespacedScope))
106
- await Agent.interact(namespacedScope, patch, false)
107
- if (listeners.mutate) after = copy(await Agent.state(namespacedScope))
106
+ await Agent.interact(namespacedScope, patch, true, [environment.id, ...context])
108
107
  if (listeners.mutate) {
109
108
  const patchCopy = copy(patch)
110
109
  patchCopy.forEach(op => op.path.shift()) // remove "active" path prefix
@@ -112,7 +111,7 @@ function embed(environment, iframe) {
112
111
  .mutate({
113
112
  scope: namespacedScope,
114
113
  before,
115
- after,
114
+ after: copy(await Agent.state(namespacedScope)),
116
115
  patch: patchCopy
117
116
  })
118
117
  }
@@ -124,11 +123,6 @@ function embed(environment, iframe) {
124
123
 
125
124
  sendDown(await Agent.metadata(namespacedScope, user, domain))
126
125
  }
127
- else if (type === 'tag') {
128
- const { tag_type, target, context } = message
129
- const prependedContext = [environment.id, ...context]
130
- sendDown(await Agent.tag(tag_type, target, prependedContext))
131
- }
132
126
  else if (type === 'state') {
133
127
  const { scope, user, domain } = message
134
128
  const namespacedScope = getNamespacedScope(environment.namespace, scope)
@@ -9,7 +9,6 @@ import downloadImplementation from '../download.js'
9
9
  // for resoling default scope in context
10
10
  const DEFAULT_SCOPE_NAME = '[]'
11
11
  const UPLOAD_TYPE = 'application/json;type=upload'
12
- const TAG_TYPE = 'application/json;type=tag'
13
12
  const DOMAIN_CLAIM_TYPE = 'application/json;type=domain-claim'
14
13
 
15
14
  export default function Agent({ Connection, domain, token, sid, uuid, fetch, applyPatch, login, logout, reboot, handleDomainMessage, log:passedLog=console.log, variables={} }) {
@@ -17,7 +16,6 @@ export default function Agent({ Connection, domain, token, sid, uuid, fetch, app
17
16
  const watchers = {}
18
17
  const keyToSubscriptionId = {}
19
18
  const lastInteractionResponse = {}
20
- const tagTypeToTargetCache = {}
21
19
 
22
20
  log('INITIALIZING AGENT CONNECTION')
23
21
  const [
@@ -32,7 +30,7 @@ export default function Agent({ Connection, domain, token, sid, uuid, fetch, app
32
30
  // initialize session
33
31
  environment()
34
32
  .then(({ session }) => {
35
- interact('sessions', [{ op: 'add', path: ['active', session], value: { queries: {}, subscriptions: {} } }], false, false)
33
+ interact('sessions', [{ op: 'add', path: ['active', session], value: { queries: {}, subscriptions: {} } }], false)
36
34
  })
37
35
 
38
36
  const internalReferences = {
@@ -44,7 +42,6 @@ export default function Agent({ Connection, domain, token, sid, uuid, fetch, app
44
42
  environment,
45
43
  lastInteractionResponse,
46
44
  lastMessageResponse,
47
- tagIfNotYetTaggedInSession,
48
45
  interact,
49
46
  fetch,
50
47
  synced,
@@ -71,25 +68,10 @@ export default function Agent({ Connection, domain, token, sid, uuid, fetch, app
71
68
  ]
72
69
  if (name) patch.push({ op: 'add', path: ['name'], value: name })
73
70
 
74
- interact(id, patch, false)
71
+ interact(id, patch)
75
72
  return id
76
73
  }
77
74
 
78
- async function tagIfNotYetTaggedInSession(tag_type, target) {
79
- const targetCache = tagTypeToTargetCache[tag_type]
80
- if (targetCache && targetCache[target]) return
81
-
82
- if (!targetCache) tagTypeToTargetCache[tag_type] = {}
83
- if (tagTypeToTargetCache[tag_type][target]) return
84
-
85
- tagTypeToTargetCache[tag_type][target] = true
86
-
87
- // always use absolute referene when tagging
88
- if (!isUUID(target)) target = (await metadata(target)).id
89
-
90
- await tag(tag_type, target)
91
- }
92
-
93
75
  async function upload(info) {
94
76
  const { name, type, data, id=uuid() } = info || {}
95
77
  create({
@@ -110,11 +92,9 @@ export default function Agent({ Connection, domain, token, sid, uuid, fetch, app
110
92
  }
111
93
  }
112
94
 
113
- // TODO: addTag option should probably not be exposed
114
- async function interact(scope=DEFAULT_SCOPE_NAME, patch, addTag=true, manageLocalState=true) {
115
- if (addTag) tagIfNotYetTaggedInSession('mutated', scope)
95
+ async function interact(scope=DEFAULT_SCOPE_NAME, patch, manageLocalState=true, context=[]) {
116
96
  // TODO: ensure user is owner of scope
117
- const response = queueMessage({scope, patch})
97
+ const response = queueMessage({scope, patch, context})
118
98
 
119
99
  // if we are watching this scope, we want to keep track of last interaction we fired
120
100
  const qualifiedScope = isUUID(scope) ? scope : `//${scope}`
@@ -174,7 +154,7 @@ export default function Agent({ Connection, domain, token, sid, uuid, fetch, app
174
154
  path: ['active', session, 'queries', id],
175
155
  value: { query, params, domain, context }
176
156
  }
177
- ], false, false)
157
+ ], false)
178
158
  try {
179
159
  const response = await lastMessageResponse()
180
160
  const { rows } = response
@@ -189,7 +169,7 @@ export default function Agent({ Connection, domain, token, sid, uuid, fetch, app
189
169
  op: 'remove',
190
170
  path: ['active', session, 'queries', id]
191
171
  }
192
- ], false, false)
172
+ ], false)
193
173
  return rows
194
174
  }
195
175
  catch (error) {
@@ -197,13 +177,6 @@ export default function Agent({ Connection, domain, token, sid, uuid, fetch, app
197
177
  }
198
178
  }
199
179
 
200
- function tag(tag_type, target, context=[]) {
201
- return create({
202
- active_type: TAG_TYPE,
203
- active: { tag_type, target, context }
204
- })
205
- }
206
-
207
180
  const reactions = { child: [] }
208
181
 
209
182
  function on(event, reaction) {
@@ -234,7 +207,6 @@ export default function Agent({ Connection, domain, token, sid, uuid, fetch, app
234
207
  synced,
235
208
  disconnect,
236
209
  reconnect,
237
- tag,
238
210
  debug,
239
211
  on
240
212
  }
@@ -38,7 +38,7 @@ export default function messageQueue({ token, sid, domain, Connection, watchers,
38
38
 
39
39
  async function environment() { return { variables, ...(await environmentPromise), context: [] } }
40
40
 
41
- function queueMessage({ scope, patch }) {
41
+ function queueMessage({ scope, patch, context }) {
42
42
  if (lastSynchronousScopePatched === scope) {
43
43
  const i = messageQueue.length - 1
44
44
  messageQueue[i].patch = [...messageQueue[i].patch, ...patch]
@@ -46,7 +46,7 @@ export default function messageQueue({ token, sid, domain, Connection, watchers,
46
46
  else {
47
47
  si += 1
48
48
  lastSynchronousScopePatchPromise = new Promise((resolve, reject) => responses[si] = [[resolve, reject]])
49
- messageQueue.push({ scope, patch, si, ts: Date.now()})
49
+ messageQueue.push({ scope, patch, context, si, ts: Date.now()})
50
50
  lastSynchronousScopePatched = scope
51
51
  flushMessageQueue()
52
52
  }
@@ -1,7 +1,7 @@
1
1
  import { v4 as uuid, validate as isUUID } from 'uuid'
2
2
  import PatchProxy from '@knowlearning/patch-proxy'
3
3
 
4
- export default function(scope='[]', user, domain, { keyToSubscriptionId, watchers, states, create, environment, lastMessageResponse, lastInteractionResponse, tagIfNotYetTaggedInSession, interact, log }) {
4
+ export default function(scope='[]', user, domain, { keyToSubscriptionId, watchers, states, create, environment, lastMessageResponse, lastInteractionResponse, interact, log }) {
5
5
  let resolveMetadataPromise
6
6
  let metadataPromise = new Promise(resolve => resolveMetadataPromise = resolve)
7
7
 
@@ -20,7 +20,7 @@ export default function(scope='[]', user, domain, { keyToSubscriptionId, watcher
20
20
  op: 'add',
21
21
  path: ['active', session, 'subscriptions', id],
22
22
  value: { scope, user, domain, ii: null }
23
- }], false, false)
23
+ }], false)
24
24
  try {
25
25
  resolve(await lastMessageResponse())
26
26
  }
package/deno.js CHANGED
@@ -51,7 +51,7 @@ function getAgent(domain, forceNew) {
51
51
  value = `ERROR: error occurred logging arguments ${error} ${arguments}`
52
52
  }
53
53
 
54
- agent.interact('sessions', [{ op: 'add', path: ['active', session, 'log'], value }], false, false)
54
+ agent.interact('sessions', [{ op: 'add', path: ['active', session, 'log'], value }], false)
55
55
  },
56
56
  fetch,
57
57
  applyPatch: fastJSONPatch.applyPatch,