@knowlearning/agents 0.3.9 → 0.3.10

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.
Files changed (2) hide show
  1. package/agents/generic.js +20 -7
  2. 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
- console.log('INITIALIZING AGENT CONNECTION')
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
- console.log('AUTHORIZING NEWLY OPENED WS FOR SESSION:', session)
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
- //console.log('WS CONNECTION ERROR', error.message)
154
+ log('WS CONNECTION ERROR', error.message)
147
155
  }
148
156
 
149
157
  ws.onclose = async () => {
150
- if (authed) console.log(`CLOSED DOMAIN ${domain} USER ${user} SESSION ${session} CONNECTION TO SERVER ${server}`)
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,20 @@ export default function Agent({ host, token, WebSocket, protocol='ws', uuid, fet
311
319
  }
312
320
 
313
321
  function disconnect() {
314
- console.log('DISCONNECTED AGENT!!!!!!!!!!!!!!!')
322
+ log('DISCONNECTED AGENT!!!!!!!!!!!!!!!')
315
323
  disconnected = true
316
324
  ws.close()
317
325
  }
318
326
 
319
327
  function reconnect() {
320
- console.log('RECONNECTED AGENT!!!!!!!!!!!!!!!')
328
+ log('RECONNECTED AGENT!!!!!!!!!!!!!!!')
321
329
  initWS()
322
330
  }
323
331
 
332
+ function debug() {
333
+ mode = 'debug'
334
+ }
335
+
324
336
  return {
325
337
  environment,
326
338
  login,
@@ -335,6 +347,7 @@ export default function Agent({ host, token, WebSocket, protocol='ws', uuid, fet
335
347
  reset,
336
348
  metadata,
337
349
  disconnect,
338
- reconnect
350
+ reconnect,
351
+ debug
339
352
  }
340
353
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@knowlearning/agents",
3
- "version": "0.3.9",
3
+ "version": "0.3.10",
4
4
  "description": "API for embedding applications in KnowLearning systems.",
5
5
  "main": "node.js",
6
6
  "browser": "browser.js",