@knowlearning/agents 0.9.81 → 0.9.84

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.
@@ -14,11 +14,11 @@ export default options => {
14
14
  const Connection = function () {
15
15
  const ws = new WebSocket(`${protocol === 'https:' ? 'wss' : 'ws'}://${isLocal() ? DEVELOPMENT_HOST : REMOTE_HOST}`)
16
16
 
17
- this.send = message => ws.send(message)
17
+ this.send = message => ws.send(JSON.stringify(message))
18
18
  this.close = () => ws.close()
19
19
 
20
- ws.onopen = () => this.onopen && this.onopen()
21
- ws.onmessage = ({ data }) => this.onmessage && this.onmessage(data)
20
+ ws.onopen = () => this.onopen()
21
+ ws.onmessage = ({ data }) => this.onmessage(data.length === 0 ? null : JSON.parse(data))
22
22
  ws.onerror = error => this.onerror && this.onerror(error)
23
23
  ws.onclose = error => this.onclose && this.onclose(error)
24
24
 
@@ -69,7 +69,7 @@ export default function messageQueue({ token, Connection, watchers, states, appl
69
69
  while (authed && lastSentSI+1 < messageQueue.length) {
70
70
  lastSynchronousScopePatched = null
71
71
  try {
72
- connection.send(JSON.stringify(messageQueue[lastSentSI + 1]))
72
+ connection.send(messageQueue[lastSentSI + 1])
73
73
  lastSentSI += 1
74
74
  // async so we don't try and push more to a closed connection
75
75
  await new Promise(r=>r())
@@ -121,16 +121,15 @@ export default function messageQueue({ token, Connection, watchers, states, appl
121
121
  if (!sessionMetrics.connected) sessionMetrics.connected = Date.now()
122
122
  log('AUTHORIZING NEWLY OPENED CONNECTION FOR SESSION:', session)
123
123
  failedConnections = 0
124
- connection.send(JSON.stringify({ token: await token(), session }))
124
+ connection.send({ token: await token(), session })
125
125
  }
126
126
 
127
- connection.onmessage = async data => {
127
+ connection.onmessage = async message => {
128
128
  checkHeartbeat()
129
- if (data.length === 0) return // heartbeat
129
+ if (!message) return // heartbeat
130
130
 
131
131
  try {
132
132
  log('handling message', disconnected, authed)
133
- const message = JSON.parse(data)
134
133
  log('message', JSON.stringify(message))
135
134
 
136
135
  if (message.error) console.warn('ERROR RESPONSE', message)
@@ -167,7 +166,7 @@ export default function messageQueue({ token, Connection, watchers, states, appl
167
166
  .forEach(([res, rej]) => message.error ? rej(message) : res(message))
168
167
 
169
168
  delete responses[message.si]
170
- connection.send(JSON.stringify({ack: message.si})) // acknowledgement that we have received the response for this message
169
+ connection.send({ack: message.si}) // acknowledgement that we have received the response for this message
171
170
  resolveSyncPromises()
172
171
  }
173
172
  else {
package/deno.js CHANGED
@@ -12,6 +12,7 @@ function Connection() {
12
12
 
13
13
  const delay = new Promise(r => setTimeout(r))
14
14
  delay.then(() => thisConnection.onopen())
15
+ denoProcess.onmessage = ({ data }) => thisConnection.onmessage(data)
15
16
 
16
17
  // TODO: consider what onclose and onerror mean in this case
17
18
  return thisConnection
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@knowlearning/agents",
3
- "version": "0.9.81",
3
+ "version": "0.9.84",
4
4
  "description": "API for embedding applications in KnowLearning systems.",
5
5
  "main": "node.js",
6
6
  "browser": "browser.js",