@knowlearning/agents 0.4.2 → 0.4.4
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/agents/generic.js +22 -9
- package/package.json +1 -1
package/agents/generic.js
CHANGED
|
@@ -29,6 +29,7 @@ export default function Agent({ host, token, WebSocket, protocol='ws', uuid, fet
|
|
|
29
29
|
const states = {}
|
|
30
30
|
const responses = {}
|
|
31
31
|
const watchers = {}
|
|
32
|
+
const keyToSubscriptionId = {}
|
|
32
33
|
const lastInteractionUpdateForWatchedScope = {}
|
|
33
34
|
const messageQueue = []
|
|
34
35
|
let resolveEnvironment
|
|
@@ -48,6 +49,10 @@ export default function Agent({ host, token, WebSocket, protocol='ws', uuid, fet
|
|
|
48
49
|
log('INITIALIZING AGENT CONNECTION')
|
|
49
50
|
initWS()
|
|
50
51
|
|
|
52
|
+
function subscription(key) {
|
|
53
|
+
return subscriptions[keyToSubscriptionId[key]]
|
|
54
|
+
}
|
|
55
|
+
|
|
51
56
|
function log() {
|
|
52
57
|
if (mode === 'debug') console.log(...arguments)
|
|
53
58
|
}
|
|
@@ -155,11 +160,12 @@ export default function Agent({ host, token, WebSocket, protocol='ws', uuid, fet
|
|
|
155
160
|
}
|
|
156
161
|
else {
|
|
157
162
|
const key = `${message.domain}/${message.user}/${message.scope}`
|
|
163
|
+
const sub = subscription(key)
|
|
158
164
|
if (watchers[key]) {
|
|
159
|
-
if (
|
|
165
|
+
if (sub.ii + 1 !== message.ii) {
|
|
160
166
|
// TODO: this seems to be an error that happens with decent regularity (an answer with a given si was skipped/failed)
|
|
161
167
|
// we should be wary of out-of-order ii being passed down (maybe need to wait for older ones???)
|
|
162
|
-
console.warn('UNEXPECTED UPDATE INTERACTION INDEX!!!!!!!!!!! last index in session',
|
|
168
|
+
console.warn('UNEXPECTED UPDATE INTERACTION INDEX!!!!!!!!!!! last index in session', sub, ' passed index ', message.ii)
|
|
163
169
|
}
|
|
164
170
|
states[key] = await states[key] || {}
|
|
165
171
|
|
|
@@ -168,7 +174,7 @@ export default function Agent({ host, token, WebSocket, protocol='ws', uuid, fet
|
|
|
168
174
|
|
|
169
175
|
applyPatch(states[key], standardJSONPatch(message.patch.slice(lastResetPatchIndex + 1)))
|
|
170
176
|
watchers[key].forEach(fn => fn({ ...message, state: states[key] }))
|
|
171
|
-
|
|
177
|
+
sub.ii = message.ii
|
|
172
178
|
}
|
|
173
179
|
}
|
|
174
180
|
}
|
|
@@ -206,18 +212,25 @@ export default function Agent({ host, token, WebSocket, protocol='ws', uuid, fet
|
|
|
206
212
|
const k = `${d || domain}/${u || user}/${scope}`
|
|
207
213
|
resolveKey(k)
|
|
208
214
|
|
|
209
|
-
if (
|
|
215
|
+
if (!keyToSubscriptionId[k]) {
|
|
216
|
+
const id = uuid()
|
|
217
|
+
|
|
218
|
+
keyToSubscriptionId[k] = id
|
|
210
219
|
watchers[k] = []
|
|
211
220
|
states[k] = new Promise(async resolve => {
|
|
212
|
-
subscriptions[
|
|
221
|
+
subscriptions[id] = { scope, ii: null, session }
|
|
222
|
+
|
|
223
|
+
if (d !== domain) subscriptions[id].domain = d
|
|
224
|
+
if (u !== user) subscriptions[id].user = u
|
|
225
|
+
|
|
213
226
|
const { ii, state } = await lastMessageResponse()
|
|
214
|
-
subscriptions[
|
|
227
|
+
subscriptions[id].ii = ii
|
|
215
228
|
resolve(state)
|
|
216
229
|
if (ii === -1) {
|
|
217
230
|
// -1 indicates the result is a computed scope, so
|
|
218
231
|
// ii does not apply (we clear out the subscription to not cache value)
|
|
219
232
|
delete states[k]
|
|
220
|
-
delete subscriptions[
|
|
233
|
+
delete subscriptions[id]
|
|
221
234
|
}
|
|
222
235
|
})
|
|
223
236
|
}
|
|
@@ -247,7 +260,7 @@ export default function Agent({ host, token, WebSocket, protocol='ws', uuid, fet
|
|
|
247
260
|
const k = await key
|
|
248
261
|
removeWatcher(k, watchFn)
|
|
249
262
|
if (watchers[k].length === 0) {
|
|
250
|
-
delete subscriptions[k]
|
|
263
|
+
delete subscriptions[keyToSubscriptionId[k]]
|
|
251
264
|
delete watchers[k]
|
|
252
265
|
}
|
|
253
266
|
}
|
|
@@ -353,7 +366,7 @@ export default function Agent({ host, token, WebSocket, protocol='ws', uuid, fet
|
|
|
353
366
|
function reconnect() {
|
|
354
367
|
log('RECONNECTED AGENT!!!!!!!!!!!!!!!')
|
|
355
368
|
disconnected = false
|
|
356
|
-
|
|
369
|
+
restartConnection()
|
|
357
370
|
}
|
|
358
371
|
|
|
359
372
|
function debug() {
|