@knowlearning/agents 0.3.9 → 0.3.11
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 -7
- package/package.json +1 -1
package/agents/generic.js
CHANGED
|
@@ -32,6 +32,7 @@ export default function Agent({ host, token, WebSocket, protocol='ws', uuid, fet
|
|
|
32
32
|
let resolveEnvironment
|
|
33
33
|
let disconnected = false
|
|
34
34
|
let failedConnections = 0
|
|
35
|
+
let mode = 'normal'
|
|
35
36
|
const environmentPromise = new Promise(r => resolveEnvironment = r)
|
|
36
37
|
|
|
37
38
|
const sessionData = new MutableProxy({}, patch => queueMessage({scope: 'sessions', patch}))
|
|
@@ -43,13 +44,17 @@ export default function Agent({ host, token, WebSocket, protocol='ws', uuid, fet
|
|
|
43
44
|
patches: {}
|
|
44
45
|
}
|
|
45
46
|
|
|
47
|
+
function log() {
|
|
48
|
+
if (mode === 'debug') console.log(...arguments)
|
|
49
|
+
}
|
|
50
|
+
|
|
46
51
|
function removeWatcher(key, fn) {
|
|
47
52
|
const watcherIndex = watchers[key].findIndex(x => x === fn)
|
|
48
53
|
if (watcherIndex > -1) watchers[key].splice(watcherIndex, 1)
|
|
49
54
|
else console.warn('TRIED TO REMOVE WATCHER THAT DOES NOT EXIST')
|
|
50
55
|
}
|
|
51
56
|
|
|
52
|
-
|
|
57
|
+
log('INITIALIZING AGENT CONNECTION')
|
|
53
58
|
initWS()
|
|
54
59
|
|
|
55
60
|
let lastSentSI = -1
|
|
@@ -77,14 +82,17 @@ export default function Agent({ host, token, WebSocket, protocol='ws', uuid, fet
|
|
|
77
82
|
ws = new WebSocket(`${protocol}://${host}`)
|
|
78
83
|
|
|
79
84
|
ws.onopen = async () => {
|
|
80
|
-
|
|
85
|
+
log('AUTHORIZING NEWLY OPENED WS FOR SESSION:', session)
|
|
81
86
|
failedConnections = 0
|
|
82
87
|
ws.send(JSON.stringify({ token: await token, session }))
|
|
83
88
|
}
|
|
84
89
|
|
|
85
90
|
ws.onmessage = async ({ data }) => {
|
|
86
91
|
try {
|
|
92
|
+
log('handling message')
|
|
87
93
|
const message = JSON.parse(data)
|
|
94
|
+
log('message', message)
|
|
95
|
+
|
|
88
96
|
if (message.error) console.warn('ERROR RESPONSE', message)
|
|
89
97
|
|
|
90
98
|
if (!authed) {
|
|
@@ -143,11 +151,11 @@ export default function Agent({ host, token, WebSocket, protocol='ws', uuid, fet
|
|
|
143
151
|
}
|
|
144
152
|
|
|
145
153
|
ws.onerror = async error => {
|
|
146
|
-
|
|
154
|
+
log('WS CONNECTION ERROR', error.message)
|
|
147
155
|
}
|
|
148
156
|
|
|
149
157
|
ws.onclose = async () => {
|
|
150
|
-
if (authed)
|
|
158
|
+
if (authed) log(`CLOSED DOMAIN ${domain} USER ${user} SESSION ${session} CONNECTION TO SERVER ${server}`)
|
|
151
159
|
authed = false
|
|
152
160
|
if (!disconnected) {
|
|
153
161
|
await new Promise(r => setTimeout(r, Math.min(1000, failedConnections * 100)))
|
|
@@ -311,16 +319,22 @@ export default function Agent({ host, token, WebSocket, protocol='ws', uuid, fet
|
|
|
311
319
|
}
|
|
312
320
|
|
|
313
321
|
function disconnect() {
|
|
314
|
-
|
|
322
|
+
log('DISCONNECTED AGENT!!!!!!!!!!!!!!!')
|
|
323
|
+
authed = false
|
|
315
324
|
disconnected = true
|
|
316
325
|
ws.close()
|
|
317
326
|
}
|
|
318
327
|
|
|
319
328
|
function reconnect() {
|
|
320
|
-
|
|
329
|
+
log('RECONNECTED AGENT!!!!!!!!!!!!!!!')
|
|
330
|
+
disconnected = false
|
|
321
331
|
initWS()
|
|
322
332
|
}
|
|
323
333
|
|
|
334
|
+
function debug() {
|
|
335
|
+
mode = 'debug'
|
|
336
|
+
}
|
|
337
|
+
|
|
324
338
|
return {
|
|
325
339
|
environment,
|
|
326
340
|
login,
|
|
@@ -335,6 +349,7 @@ export default function Agent({ host, token, WebSocket, protocol='ws', uuid, fet
|
|
|
335
349
|
reset,
|
|
336
350
|
metadata,
|
|
337
351
|
disconnect,
|
|
338
|
-
reconnect
|
|
352
|
+
reconnect,
|
|
353
|
+
debug
|
|
339
354
|
}
|
|
340
355
|
}
|