@knowlearning/agents 0.9.57 → 0.9.59

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.
@@ -131,8 +131,10 @@ export default function EmbeddedAgent() {
131
131
  return send({ type: 'interact', scope, patch })
132
132
  }
133
133
 
134
- async function upload(name, type, data, id=uuid()) {
135
- const url = await send({ type: 'upload', name, contentType: type, id })
134
+ async function upload(info) {
135
+ let { name, type, data, id=uuid() } = info || {}
136
+
137
+ const url = await send({ type: 'upload', info: { name, type, id } })
136
138
 
137
139
  if (data === undefined) return url
138
140
  else {
@@ -1,6 +1,7 @@
1
1
  import RootAgent from './root.js'
2
2
  import EmbeddedAgent from './embedded.js'
3
3
  import { v1 as uuid, validate as validateUUID } from 'uuid'
4
+ import selectFile from './select-file.js'
4
5
 
5
6
  let Agent
6
7
 
@@ -15,6 +16,20 @@ export default function browserAgent(options={}) {
15
16
  const newAgent = embedded && !options.root ? EmbeddedAgent() : RootAgent(options)
16
17
  newAgent.embed = embed
17
18
 
19
+ const originalUpload = newAgent.upload
20
+ newAgent.upload = async info => {
21
+ if (info.browser) {
22
+ const file = await selectFile()
23
+ if (!file) return
24
+
25
+ info.data = await file.arrayBuffer()
26
+ if (!info.name) info.name = file.name
27
+ if (!info.type) info.type = file.type
28
+ }
29
+
30
+ return originalUpload(info)
31
+ }
32
+
18
33
  if (!Agent) Agent = newAgent
19
34
 
20
35
  return newAgent
@@ -108,8 +123,8 @@ function embed(environment, iframe) {
108
123
  .catch(error => sendDown(null, error.error))
109
124
  }
110
125
  else if (type === 'upload') {
111
- const { name, contentType, id } = message
112
- sendDown(await Agent.upload(name, contentType, undefined, id))
126
+ const { info } = message
127
+ sendDown(await Agent.upload(info))
113
128
  }
114
129
  else if (type === 'download') {
115
130
  sendDown(await Agent.download(message.id).url())
@@ -0,0 +1,15 @@
1
+ export default async function selectFile() {
2
+ return new Promise((resolve, reject) => {
3
+ const input = document.createElement('input')
4
+ input.type = 'file'
5
+
6
+ input.addEventListener('change', async (event) => {
7
+ const file = event.target.files[0]
8
+
9
+ if (file) resolve(file)
10
+ else resolve(null)
11
+ })
12
+
13
+ input.click()
14
+ })
15
+ }
@@ -69,6 +69,8 @@ export default function Agent({ host, token, WebSocket, protocol='ws', uuid, fet
69
69
  { op: 'add', path: ['active_type'], value: active_type },
70
70
  { op: 'add', path: ['active'], value: active }
71
71
  ]
72
+ if (name) patch.push({ op: 'add', path: ['name'], value: name })
73
+
72
74
  interact(id, patch, false)
73
75
  return id
74
76
  }
@@ -88,12 +90,11 @@ export default function Agent({ host, token, WebSocket, protocol='ws', uuid, fet
88
90
  await tag(tag_type, target)
89
91
  }
90
92
 
91
- // TODO: if no data, set up streaming upload
92
- async function upload(name, type, data, id=uuid()) {
93
- // TODO: include data size info...
93
+ async function upload({ name, type, data, id=uuid() }) {
94
94
  create({
95
95
  active_type: UPLOAD_TYPE,
96
- active: { id, type }
96
+ active: { id, type, name },
97
+ name
97
98
  })
98
99
  const { url } = await lastMessageResponse()
99
100
 
@@ -122,7 +123,6 @@ export default function Agent({ host, token, WebSocket, protocol='ws', uuid, fet
122
123
 
123
124
  const resolveAndUnwatch = async (update) => {
124
125
  const { ii } = await response
125
- console.log('RESOLVE LAST INTERACTION....', ii, update, scope)
126
126
  if (update.ii === ii) {
127
127
  resolve(ii)
128
128
  removeWatcher(qualifiedScope, resolveAndUnwatch)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@knowlearning/agents",
3
- "version": "0.9.57",
3
+ "version": "0.9.59",
4
4
  "description": "API for embedding applications in KnowLearning systems.",
5
5
  "main": "node.js",
6
6
  "browser": "browser.js",