@knowlearning/agents 0.9.46 → 0.9.48

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.
@@ -31,6 +31,12 @@ export default function Agent({ host, token, WebSocket, protocol='ws', uuid, fet
31
31
  environment
32
32
  ] = messageQueue({ token, protocol, host, WebSocket, watchers, states, applyPatch, log, login, interact })
33
33
 
34
+ // initialize session
35
+ environment()
36
+ .then(({ session }) => {
37
+ interact('sessions', [{ op: 'add', path: [session], value: { queries: {}, subscriptions: {} } }])
38
+ })
39
+
34
40
  const internalReferences = {
35
41
  keyToSubscriptionId,
36
42
  watchers,
@@ -49,15 +55,6 @@ export default function Agent({ host, token, WebSocket, protocol='ws', uuid, fet
49
55
 
50
56
  const [ watch, removeWatcher ] = watchImplementation(internalReferences)
51
57
 
52
- const sessionPromise = new Promise(async resolve => {
53
- const { session } = await environment()
54
- const sessions = await state('sessions')
55
- sessions[session] = {
56
- queries: {}
57
- }
58
- resolve(sessions[session])
59
- })
60
-
61
58
  function state(scope, user, domain) { return stateImplementation(scope, user, domain, internalReferences) }
62
59
 
63
60
  function download(id) { return downloadImplementation(id, internalReferences) }
@@ -177,13 +174,29 @@ export default function Agent({ host, token, WebSocket, protocol='ws', uuid, fet
177
174
  }
178
175
 
179
176
  async function query(query, params, domain) {
180
- const session = await sessionPromise
181
177
  const id = uuid()
182
178
  const requested = Date.now()
183
- await new Promise(r => setTimeout(r)) // ensure next interaction gets sent on its own
184
- session.queries[id] = { query, params, domain }
179
+ const { session } = await environment()
180
+ await new Promise(r => setTimeout(r, 1)) // ensure next interaction gets sent on its own
181
+ interact('sessions', [
182
+ {
183
+ op: 'add',
184
+ path: ['active', session, 'queries', id],
185
+ value: { query, params, domain }
186
+ }
187
+ ])
185
188
  const { rows } = await lastMessageResponse()
186
- session.queries[id].agent_latency = Date.now() - requested
189
+ interact('sessions', [
190
+ {
191
+ op: 'add',
192
+ path: ['active', session, 'queries', id, 'agent_latency'],
193
+ value: Date.now() - requested
194
+ },
195
+ {
196
+ op: 'remove',
197
+ path: ['active', session, 'queries', id]
198
+ }
199
+ ])
187
200
  return rows
188
201
  }
189
202
 
@@ -1,8 +1,6 @@
1
1
  import { v4 as uuid, validate as isUUID } from 'uuid'
2
2
  import MutableProxy from '../../persistence/json.js'
3
3
 
4
- const SUBSCRIPTION_TYPE = 'application/json;type=subscription'
5
-
6
4
  export default function(scope='[]', user, domain, { keyToSubscriptionId, watchers, states, create, environment, lastMessageResponse, lastInteractionResponse, tagIfNotYetTaggedInSession, interact }) {
7
5
  let resolveMetadataPromise
8
6
  let metadataPromise = new Promise(resolve => resolveMetadataPromise = resolve)
@@ -16,20 +14,19 @@ export default function(scope='[]', user, domain, { keyToSubscriptionId, watcher
16
14
  watchers[qualifiedScope] = []
17
15
  states[qualifiedScope] = new Promise(async (resolve, reject) => {
18
16
  const { session } = await environment()
19
- create({
20
- id,
21
- active_type: SUBSCRIPTION_TYPE,
22
- active: { session, scope, user, domain, ii: null, initialized: Date.now() },
23
- })
24
-
17
+ await new Promise(r => setTimeout(r, 1))
18
+ interact('sessions', [{
19
+ op: 'add',
20
+ path: ['active', session, 'subscriptions', id],
21
+ value: { session, scope, user, domain, ii: null }
22
+ }])
25
23
  try {
26
24
  const state = await lastMessageResponse()
27
- tagIfNotYetTaggedInSession('subscribed', state.id)
28
- interact(id, [
29
- { op: 'add', path: ['active', 'ii'], value: state.ii }, // TODO: use state.ii when is coming down properly...
30
- { op: 'add', path: ['active', 'synced'], value: Date.now() }
31
- ])
32
-
25
+ interact('sessions', [{
26
+ op: 'add',
27
+ path: ['active', session, 'subscriptions', id, 'ii'],
28
+ value: state.ii
29
+ }])
33
30
  resolve(state)
34
31
  }
35
32
  catch (error) { reject(error) }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@knowlearning/agents",
3
- "version": "0.9.46",
3
+ "version": "0.9.48",
4
4
  "description": "API for embedding applications in KnowLearning systems.",
5
5
  "main": "node.js",
6
6
  "browser": "browser.js",