@knowlearning/agents 0.1.6 → 0.2.0

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.
@@ -0,0 +1,13 @@
1
+ import RootAgent from './root.js'
2
+ import EmbeddedAgent from './embedded.js'
3
+ import embed from './embed.js'
4
+
5
+ export default function() {
6
+ let embedded
7
+
8
+ try { embedded = window.self !== window.top }
9
+ catch (e) { embedded = true }
10
+
11
+ window.Agent = embedded ? EmbeddedAgent() : RootAgent()
12
+ Agent.embed = embed
13
+ }
@@ -4,7 +4,7 @@ const copy = x => JSON.parse(JSON.stringify(x))
4
4
 
5
5
  const watchers = {}
6
6
 
7
- export default function Experience(environment, iframe) {
7
+ export default function embed(environment, iframe) {
8
8
  const postMessageQueue = []
9
9
  let frameLoaded = false
10
10
  let embeddedAgentInitialized = false
@@ -24,10 +24,6 @@ export default function Experience(environment, iframe) {
24
24
  }
25
25
  }
26
26
 
27
- this.remove = () => {
28
- if (iframe.parentNode) iframe.parentNode.removeChild(iframe)
29
- }
30
-
31
27
  const handleMessage = async message => {
32
28
  const { requestId, type } = message
33
29
 
@@ -122,5 +118,11 @@ export default function Experience(environment, iframe) {
122
118
  }
123
119
  })
124
120
 
125
- return this
121
+ function remove () {
122
+ if (iframe.parentNode) iframe.parentNode.removeChild(iframe)
123
+ }
124
+
125
+ return {
126
+ remove
127
+ }
126
128
  }
@@ -1,6 +1,5 @@
1
1
  import { v1 as uuid } from 'uuid'
2
2
  import MutableProxy from '../../persistence/json.js'
3
- import Experience from './experience.js'
4
3
 
5
4
  export default function EmbeddedAgent() {
6
5
  let messageIndex = 0
@@ -145,18 +144,17 @@ export default function EmbeddedAgent() {
145
144
  function reconnect() { return send({ type: 'reconnect' }) }
146
145
 
147
146
  return {
147
+ environment,
148
148
  login,
149
149
  logout,
150
- environment,
151
150
  state,
152
- mutate,
153
- patch,
154
- interact,
155
- metadata,
156
151
  upload,
157
152
  download,
153
+ interact,
154
+ patch,
155
+ mutate,
156
+ metadata,
158
157
  disconnect,
159
- reconnect,
160
- Experience
158
+ reconnect
161
159
  }
162
160
  }
@@ -1,4 +1,4 @@
1
- import { initializeApp } from 'npm/unscoped/firebase/9.12.1/app'
1
+ import { initializeApp } from 'firebase/app'
2
2
  import {
3
3
  getAuth,
4
4
  signOut as firebaseSignOut,
@@ -8,7 +8,7 @@ import {
8
8
  signInWithRedirect,
9
9
  signInWithEmailAndPassword,
10
10
  signInAnonymously
11
- } from 'npm/unscoped/firebase/9.12.1/auth'
11
+ } from 'firebase/auth'
12
12
 
13
13
  initializeApp({
14
14
  "apiKey": "AIzaSyAxjYuF-2JmXxlXnGgNu2CO4Q41EAtUgrY",
@@ -1,22 +1,17 @@
1
- import { v1 as uuid } from 'npm/unscoped/uuid/9.0.0'
2
- import { applyPatch } from 'npm/unscoped/fast-json-patch/3.1.1'
1
+ import { v1 as uuid } from 'uuid'
2
+ import { applyPatch } from 'fast-json-patch'
3
3
  import { onAuth, login, logout } from './firebase-auth.js'
4
4
  import GenericAgent from '../generic.js'
5
- import Experience from './experience.js'
6
5
 
7
- const Agent = new GenericAgent({
8
- host: window.location.host,
9
- protocol: window.location.protocol === 'https:' ? 'wss' : 'ws',
10
- token: new Promise(r => onAuth(({ getIdToken: g }) => r(g()))),
11
- WebSocket,
12
- uuid,
13
- fetch,
14
- applyPatch,
15
- login,
16
- logout,
17
- reboot: () => window.location.reload()
6
+ export default () => GenericAgent({
7
+ host: window.location.host,
8
+ protocol: window.location.protocol === 'https:' ? 'wss' : 'ws',
9
+ token: new Promise(r => onAuth(({ getIdToken: g }) => r(g()))),
10
+ WebSocket,
11
+ uuid,
12
+ fetch,
13
+ applyPatch,
14
+ login,
15
+ logout,
16
+ reboot: () => window.location.reload()
18
17
  })
19
-
20
- Agent.Experience = Experience
21
-
22
- export default Agent
package/agents/generic.js CHANGED
@@ -28,7 +28,7 @@ export default function Agent({ host, token, WebSocket, protocol='ws', uuid, fet
28
28
  let resolveEnvironment
29
29
  let disconnected = false
30
30
  let failedConnections = 0
31
- const environment = new Promise(r => resolveEnvironment = r)
31
+ const environmentPromise = new Promise(r => resolveEnvironment = r)
32
32
 
33
33
  const sessionData = new MutableProxy({}, patch => queueMessage({scope: 'sessions', patch}))
34
34
 
@@ -45,21 +45,6 @@ export default function Agent({ host, token, WebSocket, protocol='ws', uuid, fet
45
45
  else console.warn('TRIED TO REMOVE WATCHER THAT DOES NOT EXIST')
46
46
  }
47
47
 
48
- this.environment = function () { return environment }
49
-
50
- this.state = state
51
- this.upload = upload
52
- this.download = download
53
- this.interact = interact
54
- this.patch = patch
55
- this.mutate = mutate
56
- this.metadata = metadata
57
-
58
- this.login = login
59
- this.logout = logout
60
- this.disconnect = disconnect
61
- this.reconnect = reconnect
62
-
63
48
  console.log('INITIALIZING AGENT CONNECTION')
64
49
  initWS()
65
50
 
@@ -168,13 +153,15 @@ export default function Agent({ host, token, WebSocket, protocol='ws', uuid, fet
168
153
  }
169
154
  }
170
155
 
156
+ function environment() { return environmentPromise }
157
+
171
158
  function state(scope, u, d) {
172
159
  let watchFn
173
160
  let resolveKey
174
161
  const key = new Promise(r => resolveKey = r)
175
162
 
176
163
  const promise = new Promise(async resolveState => {
177
- await environment // environment not set until first connection
164
+ await environmentPromise // environment not set until first connection
178
165
 
179
166
  if (!u) u = user
180
167
  if (!d) d = domain
@@ -267,7 +254,7 @@ export default function Agent({ host, token, WebSocket, protocol='ws', uuid, fet
267
254
  queueMessage({scope, patch})
268
255
  const response = lastMessageResponse()
269
256
 
270
- await environment
257
+ await environmentPromise
271
258
 
272
259
  const key = `${domain}/${user}/${scope}`
273
260
 
@@ -315,5 +302,18 @@ export default function Agent({ host, token, WebSocket, protocol='ws', uuid, fet
315
302
  initWS()
316
303
  }
317
304
 
318
- return this
305
+ return {
306
+ environment,
307
+ login,
308
+ logout,
309
+ state,
310
+ upload,
311
+ download,
312
+ interact,
313
+ patch,
314
+ mutate,
315
+ metadata,
316
+ disconnect,
317
+ reconnect
318
+ }
319
319
  }
@@ -1,5 +1,5 @@
1
1
  // TODO: export browser, node, deno, and generic agents
2
- export { default as BrowserAgent } from '../agents/browser/embedded.js'
2
+ export { default as BrowserAgent } from '../agents/browser/root.js'
3
3
  export { default as GenericAgent } from '../agents/generic.js'
4
4
  export { default as vuePersistentStore } from '../persistence/vuex.js'
5
5
  export { default as vueContentComponent } from '../vue/3/content.vue'
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@knowlearning/agents",
3
- "version": "0.1.6",
3
+ "version": "0.2.0",
4
4
  "description": "API for embedding applications in KnowLearning systems.",
5
5
  "main": "package/node.js",
6
6
  "browser": "package/browser.js",
@@ -25,7 +25,8 @@
25
25
  "dependencies": {
26
26
  "fast-json-patch": "^3.1.1",
27
27
  "node-fetch": "^3.3.1",
28
- "uuid": "^8.3.2"
28
+ "uuid": "^8.3.2",
29
+ "firebase": "^9.12.1"
29
30
  }
30
31
  }
31
32
 
@@ -32,14 +32,6 @@ export default function MutableProxy(state, interact, ephemeralPaths={}, parentP
32
32
 
33
33
  const traps = {
34
34
  set(target, prop, value) {
35
- const path = [...parentPath, prop]
36
- const serializedPath = serializePath(path)
37
-
38
- if (ephemeralPaths[serializedPath]) {
39
- target[prop] = value
40
- return true
41
- }
42
-
43
35
  if (isArray) {
44
36
  if (!/^\d+$/.test(prop)) {
45
37
  target[prop] = value
@@ -48,6 +40,14 @@ export default function MutableProxy(state, interact, ephemeralPaths={}, parentP
48
40
  else prop = parseInt(prop)
49
41
  }
50
42
 
43
+ const path = [...parentPath, prop]
44
+ const serializedPath = serializePath(path)
45
+
46
+ if (ephemeralPaths[serializedPath]) {
47
+ target[prop] = value
48
+ return true
49
+ }
50
+
51
51
  if (value === undefined) {
52
52
  console.log('EXTRA ERROR INFO. target, prop, value', target, prop, value)
53
53
  throw new Error(`Setting properties to undefined is not supported. Please use a delete statement. Attempted to set ${serializedPath}`)
package/vue/3/content.vue CHANGED
@@ -19,7 +19,7 @@ export default {
19
19
  }
20
20
  },
21
21
  unmounted() {
22
- if (this.experience) this.experience.remove()
22
+ if (this.embedding) this.embedding.remove()
23
23
  },
24
24
  methods: {
25
25
  setup(iframe) {
@@ -27,10 +27,10 @@ export default {
27
27
 
28
28
  const { id, scope } = this
29
29
  this.iframe = iframe
30
- this.experience = new Agent.Experience({ content: id, scope }, iframe)
30
+ this.embedding = new Agent.embed({ content: id, scope }, iframe)
31
31
 
32
32
  /*
33
- const { handle } = this.experience
33
+ const { handle } = this.embedding
34
34
  // if save or edit are listened to, attach handler
35
35
  if (this.$attrs.onSave) {
36
36
  handle('save', () => new Promise((resolve, reject) => this.$emit('save', { resolve, reject })))