@miso.ai/client-sdk-core 1.7.0 → 1.7.1-beta.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.
@@ -10,20 +10,20 @@ const _excluded = ["iterable"],
10
10
  _excluded2 = ["pollingInternal"];
11
11
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
12
12
  function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
13
- class Answers extends _base.default {
13
+ class Ask extends _base.default {
14
14
  constructor(api) {
15
- super(api, 'answers');
15
+ super(api, 'ask');
16
16
  }
17
- async answer(payload) {
17
+ questions(payload) {
18
18
  let _ref = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
19
19
  let {
20
20
  iterable = false
21
21
  } = _ref,
22
22
  options = _objectWithoutPropertiesLoose(_ref, _excluded);
23
- // TODO: it's possible to merge _iterable and _answer
24
- return typeof payload === 'string' ? this._get(payload, options) : iterable ? this._iterable(payload, options) : this._answer(payload, options);
23
+ // TODO: it's possible to merge _iterator and _answer
24
+ return typeof payload === 'string' ? this._get(payload, options) : iterable ? this._iterator(payload, options) : this._questions(payload, options);
25
25
  }
26
- async _answer(payload, options) {
26
+ async _questions(payload, options) {
27
27
  return this._run('questions', payload, options);
28
28
  }
29
29
  async _get(answerId, options) {
@@ -31,7 +31,7 @@ class Answers extends _base.default {
31
31
  method: 'GET'
32
32
  }));
33
33
  }
34
- _iterable(payload) {
34
+ _iterator(payload) {
35
35
  try {
36
36
  var _this = this;
37
37
  let _ref2 = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
@@ -40,7 +40,7 @@ class Answers extends _base.default {
40
40
  pollingInternal = 500
41
41
  } = _ref2,
42
42
  options = _objectWithoutPropertiesLoose(_ref2, _excluded2);
43
- const response = await _this._answer(payload, options);
43
+ const response = await _this._questions(payload, options);
44
44
  yield response;
45
45
  const {
46
46
  question_id,
@@ -49,9 +49,22 @@ class Answers extends _base.default {
49
49
  if (finished) {
50
50
  return;
51
51
  }
52
+ let apiErrorCount = 0;
52
53
  const buffer = new _commons.ValueBuffer();
53
54
  const intervalId = setInterval(async () => {
54
- const response = await _this._get(question_id, options);
55
+ let response;
56
+ try {
57
+ response = await _this._get(question_id, options);
58
+ } catch (error) {
59
+ apiErrorCount++;
60
+ if (apiErrorCount > 10) {
61
+ clearInterval(intervalId);
62
+ // TODO: cascade to buffer and user
63
+ }
64
+
65
+ console.error(error); // TODO
66
+ return;
67
+ }
55
68
  const {
56
69
  finished
57
70
  } = response;
@@ -67,4 +80,4 @@ class Answers extends _base.default {
67
80
  }
68
81
  }
69
82
  }
70
- exports.default = Answers;
83
+ exports.default = Ask;
@@ -7,7 +7,7 @@ exports.default = void 0;
7
7
  var _commons = require("@miso.ai/commons");
8
8
  var _helpers = _interopRequireDefault(require("./helpers"));
9
9
  var _interactions = _interopRequireDefault(require("./interactions"));
10
- var _answers = _interopRequireDefault(require("./answers"));
10
+ var _ask = _interopRequireDefault(require("./ask"));
11
11
  var _search = _interopRequireDefault(require("./search"));
12
12
  var _recommendation = _interopRequireDefault(require("./recommendation"));
13
13
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
@@ -17,7 +17,7 @@ class Api extends _commons.Component {
17
17
  this._client = client;
18
18
  this.helpers = new _helpers.default(client, root);
19
19
  this.interactions = new _interactions.default(this);
20
- this.answers = new _answers.default(this);
20
+ this.ask = new _ask.default(this);
21
21
  this.search = new _search.default(this);
22
22
  this.recommendation = new _recommendation.default(this);
23
23
  }
@@ -15,8 +15,10 @@ class PluginRoot extends _commons.Registry {
15
15
  keyName: 'id'
16
16
  });
17
17
  this._root = root;
18
+ this._currentScript = document.currentScript;
18
19
  this._events._replays.add('install').add('subtree');
19
20
  this._installed = {};
21
+ this._installedResolutions = {};
20
22
  this._useRequests = {};
21
23
  this._subtrees = [];
22
24
  this.context = new PluginContext(this, _classes.default);
@@ -60,9 +62,22 @@ class PluginRoot extends _commons.Registry {
60
62
  instance = this._instantiate(plugin, options);
61
63
  this._install(instance);
62
64
  }
63
-
64
- // TODO: unuse()
65
-
65
+ async install(plugin, options) {
66
+ this.use(plugin, options);
67
+ if (typeof plugin === 'string' && !this.isRegistered(plugin)) {
68
+ await this._tryRegisterRemotely(plugin);
69
+ }
70
+ return this.whenInstalled(plugin);
71
+ }
72
+ async whenInstalled(id) {
73
+ if (this.isInstalled(id)) {
74
+ return this.get(id);
75
+ }
76
+ const {
77
+ promise
78
+ } = this._installedResolutions[id] || (this._installedResolutions[id] = new _commons.Resolution());
79
+ return promise;
80
+ }
66
81
  addSubtree(component) {
67
82
  this._subtrees.push(component);
68
83
  this._events.emit('subtree', component);
@@ -124,6 +139,10 @@ class PluginRoot extends _commons.Registry {
124
139
  if (instance.id) {
125
140
  // we don't track anonymous plugins
126
141
  this._installed[instance.id] = instance;
142
+ if (this._installedResolutions[instance.id]) {
143
+ this._installedResolutions[instance.id].resolve(instance);
144
+ delete this._installedResolutions[instance.id];
145
+ }
127
146
  }
128
147
  this._events.emit('install', instance);
129
148
  }
@@ -144,6 +163,33 @@ class PluginRoot extends _commons.Registry {
144
163
  delete this._useRequests[id];
145
164
  }
146
165
  }
166
+ async _tryRegisterRemotely(id) {
167
+ if (id.startsWith('std:')) {
168
+ try {
169
+ await this._registerStdRemotely(id);
170
+ return;
171
+ } catch (e) {
172
+ console.error(e);
173
+ }
174
+ }
175
+ // TODO: try as URL
176
+ throw new Error(`Cannot register plugin remotely: ${id}`);
177
+ }
178
+ async _registerStdRemotely(id) {
179
+ // TODO: look up by manifest
180
+ const pkgName = id.slice(4);
181
+ const src = this._currentScript.src;
182
+ const searchIndex = src.indexOf('?');
183
+ const k = searchIndex < 0 ? src.length : searchIndex;
184
+ const i = src.lastIndexOf('/', k) + 1;
185
+ const j = src.indexOf('.', i);
186
+ const ext = j < 0 ? '' : src.slice(j, k);
187
+ const script = document.createElement('script');
188
+ script.async = true;
189
+ script.src = `${src.slice(0, i)}plugins/${pkgName}${ext}`;
190
+ document.head.appendChild(script);
191
+ await this.whenRegistered(id);
192
+ }
147
193
  }
148
194
 
149
195
  /**
@@ -152,7 +198,7 @@ class PluginRoot extends _commons.Registry {
152
198
  exports.default = PluginRoot;
153
199
  class PluginContext {
154
200
  constructor(root, classes) {
155
- (0, _commons.delegateGetters)(this, root, ['getPluginClass', 'addSubtree', 'addPayloadPass', 'addUrlPass']);
201
+ (0, _commons.delegateGetters)(this, root, ['getPluginClass', 'addSubtree', 'addPayloadPass', 'addUrlPass', 'whenInstalled', 'whenRegistered']);
156
202
  (0, _commons.defineValues)(this, {
157
203
  classes
158
204
  });
@@ -164,6 +210,6 @@ class PluginContext {
164
210
  */
165
211
  class Plugins {
166
212
  constructor(root) {
167
- (0, _commons.delegateGetters)(this, root, ['installed', 'registered', 'isInstalled', 'isRegistered', 'get', 'register', 'use']);
213
+ (0, _commons.delegateGetters)(this, root, ['installed', 'registered', 'isInstalled', 'isRegistered', 'whenInstalled', 'whenRegistered', 'get', 'register', 'use', 'install']);
168
214
  }
169
215
  }
@@ -4,5 +4,5 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.default = void 0;
7
- var _default = '1.7.0';
7
+ var _default = '1.7.1-beta.0';
8
8
  exports.default = _default;
@@ -3,20 +3,20 @@ const _excluded = ["iterable"],
3
3
  function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
4
4
  import { ValueBuffer } from '@miso.ai/commons';
5
5
  import ApiBase from './base';
6
- export default class Answers extends ApiBase {
6
+ export default class Ask extends ApiBase {
7
7
  constructor(api) {
8
- super(api, 'answers');
8
+ super(api, 'ask');
9
9
  }
10
- async answer(payload) {
10
+ questions(payload) {
11
11
  let _ref = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
12
12
  let {
13
13
  iterable = false
14
14
  } = _ref,
15
15
  options = _objectWithoutPropertiesLoose(_ref, _excluded);
16
- // TODO: it's possible to merge _iterable and _answer
17
- return typeof payload === 'string' ? this._get(payload, options) : iterable ? this._iterable(payload, options) : this._answer(payload, options);
16
+ // TODO: it's possible to merge _iterator and _answer
17
+ return typeof payload === 'string' ? this._get(payload, options) : iterable ? this._iterator(payload, options) : this._questions(payload, options);
18
18
  }
19
- async _answer(payload, options) {
19
+ async _questions(payload, options) {
20
20
  return this._run('questions', payload, options);
21
21
  }
22
22
  async _get(answerId, options) {
@@ -24,7 +24,7 @@ export default class Answers extends ApiBase {
24
24
  method: 'GET'
25
25
  }));
26
26
  }
27
- _iterable(payload) {
27
+ _iterator(payload) {
28
28
  try {
29
29
  var _this = this;
30
30
  let _ref2 = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
@@ -33,7 +33,7 @@ export default class Answers extends ApiBase {
33
33
  pollingInternal = 500
34
34
  } = _ref2,
35
35
  options = _objectWithoutPropertiesLoose(_ref2, _excluded2);
36
- const response = await _this._answer(payload, options);
36
+ const response = await _this._questions(payload, options);
37
37
  yield response;
38
38
  const {
39
39
  question_id,
@@ -42,9 +42,22 @@ export default class Answers extends ApiBase {
42
42
  if (finished) {
43
43
  return;
44
44
  }
45
+ let apiErrorCount = 0;
45
46
  const buffer = new ValueBuffer();
46
47
  const intervalId = setInterval(async () => {
47
- const response = await _this._get(question_id, options);
48
+ let response;
49
+ try {
50
+ response = await _this._get(question_id, options);
51
+ } catch (error) {
52
+ apiErrorCount++;
53
+ if (apiErrorCount > 10) {
54
+ clearInterval(intervalId);
55
+ // TODO: cascade to buffer and user
56
+ }
57
+
58
+ console.error(error); // TODO
59
+ return;
60
+ }
48
61
  const {
49
62
  finished
50
63
  } = response;
@@ -1,7 +1,7 @@
1
1
  import { Component } from '@miso.ai/commons';
2
2
  import ApiHelpers from './helpers';
3
3
  import Interactions from './interactions';
4
- import Answers from './answers';
4
+ import Ask from './ask';
5
5
  import Search from './search';
6
6
  import Recommendation from './recommendation';
7
7
  export default class Api extends Component {
@@ -10,7 +10,7 @@ export default class Api extends Component {
10
10
  this._client = client;
11
11
  this.helpers = new ApiHelpers(client, root);
12
12
  this.interactions = new Interactions(this);
13
- this.answers = new Answers(this);
13
+ this.ask = new Ask(this);
14
14
  this.search = new Search(this);
15
15
  this.recommendation = new Recommendation(this);
16
16
  }
@@ -1,4 +1,4 @@
1
- import { delegateGetters, defineValues, Registry } from '@miso.ai/commons';
1
+ import { delegateGetters, defineValues, Registry, Resolution } from '@miso.ai/commons';
2
2
  import classes from '../classes';
3
3
  import AnonymousPlugin from './anonymous';
4
4
  export default class PluginRoot extends Registry {
@@ -8,8 +8,10 @@ export default class PluginRoot extends Registry {
8
8
  keyName: 'id'
9
9
  });
10
10
  this._root = root;
11
+ this._currentScript = document.currentScript;
11
12
  this._events._replays.add('install').add('subtree');
12
13
  this._installed = {};
14
+ this._installedResolutions = {};
13
15
  this._useRequests = {};
14
16
  this._subtrees = [];
15
17
  this.context = new PluginContext(this, classes);
@@ -53,9 +55,22 @@ export default class PluginRoot extends Registry {
53
55
  instance = this._instantiate(plugin, options);
54
56
  this._install(instance);
55
57
  }
56
-
57
- // TODO: unuse()
58
-
58
+ async install(plugin, options) {
59
+ this.use(plugin, options);
60
+ if (typeof plugin === 'string' && !this.isRegistered(plugin)) {
61
+ await this._tryRegisterRemotely(plugin);
62
+ }
63
+ return this.whenInstalled(plugin);
64
+ }
65
+ async whenInstalled(id) {
66
+ if (this.isInstalled(id)) {
67
+ return this.get(id);
68
+ }
69
+ const {
70
+ promise
71
+ } = this._installedResolutions[id] || (this._installedResolutions[id] = new Resolution());
72
+ return promise;
73
+ }
59
74
  addSubtree(component) {
60
75
  this._subtrees.push(component);
61
76
  this._events.emit('subtree', component);
@@ -117,6 +132,10 @@ export default class PluginRoot extends Registry {
117
132
  if (instance.id) {
118
133
  // we don't track anonymous plugins
119
134
  this._installed[instance.id] = instance;
135
+ if (this._installedResolutions[instance.id]) {
136
+ this._installedResolutions[instance.id].resolve(instance);
137
+ delete this._installedResolutions[instance.id];
138
+ }
120
139
  }
121
140
  this._events.emit('install', instance);
122
141
  }
@@ -137,6 +156,33 @@ export default class PluginRoot extends Registry {
137
156
  delete this._useRequests[id];
138
157
  }
139
158
  }
159
+ async _tryRegisterRemotely(id) {
160
+ if (id.startsWith('std:')) {
161
+ try {
162
+ await this._registerStdRemotely(id);
163
+ return;
164
+ } catch (e) {
165
+ console.error(e);
166
+ }
167
+ }
168
+ // TODO: try as URL
169
+ throw new Error(`Cannot register plugin remotely: ${id}`);
170
+ }
171
+ async _registerStdRemotely(id) {
172
+ // TODO: look up by manifest
173
+ const pkgName = id.slice(4);
174
+ const src = this._currentScript.src;
175
+ const searchIndex = src.indexOf('?');
176
+ const k = searchIndex < 0 ? src.length : searchIndex;
177
+ const i = src.lastIndexOf('/', k) + 1;
178
+ const j = src.indexOf('.', i);
179
+ const ext = j < 0 ? '' : src.slice(j, k);
180
+ const script = document.createElement('script');
181
+ script.async = true;
182
+ script.src = `${src.slice(0, i)}plugins/${pkgName}${ext}`;
183
+ document.head.appendChild(script);
184
+ await this.whenRegistered(id);
185
+ }
140
186
  }
141
187
 
142
188
  /**
@@ -144,7 +190,7 @@ export default class PluginRoot extends Registry {
144
190
  */
145
191
  class PluginContext {
146
192
  constructor(root, classes) {
147
- delegateGetters(this, root, ['getPluginClass', 'addSubtree', 'addPayloadPass', 'addUrlPass']);
193
+ delegateGetters(this, root, ['getPluginClass', 'addSubtree', 'addPayloadPass', 'addUrlPass', 'whenInstalled', 'whenRegistered']);
148
194
  defineValues(this, {
149
195
  classes
150
196
  });
@@ -156,6 +202,6 @@ class PluginContext {
156
202
  */
157
203
  class Plugins {
158
204
  constructor(root) {
159
- delegateGetters(this, root, ['installed', 'registered', 'isInstalled', 'isRegistered', 'get', 'register', 'use']);
205
+ delegateGetters(this, root, ['installed', 'registered', 'isInstalled', 'isRegistered', 'whenInstalled', 'whenRegistered', 'get', 'register', 'use', 'install']);
160
206
  }
161
207
  }
@@ -1 +1 @@
1
- export default '1.7.0';
1
+ export default '1.7.1-beta.0';
package/package.json CHANGED
@@ -29,7 +29,7 @@
29
29
  "url": "https://github.com/MisoAI/miso-client-js-sdk/issues"
30
30
  },
31
31
  "dependencies": {
32
- "@miso.ai/commons": "1.7.0"
32
+ "@miso.ai/commons": "1.7.1-beta.0"
33
33
  },
34
- "version": "1.7.0"
34
+ "version": "1.7.1-beta.0"
35
35
  }