@miso.ai/client-sdk-core 1.8.0-beta.17 → 1.8.0-beta.18
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.
- package/dist/cjs/api/api.js +25 -0
- package/dist/cjs/api/ask.js +1 -1
- package/dist/cjs/api/helpers.js +1 -1
- package/dist/cjs/api/index.js +49 -19
- package/dist/cjs/api/interactions.js +1 -1
- package/dist/cjs/api/recommendation.js +1 -1
- package/dist/cjs/api/search.js +1 -1
- package/dist/cjs/classes.js +5 -15
- package/dist/cjs/client.js +2 -2
- package/dist/cjs/index.js +31 -5
- package/dist/cjs/plugin/index.js +3 -3
- package/dist/cjs/plugin/plugin-root.js +3 -3
- package/dist/cjs/root.js +2 -2
- package/dist/cjs/version.js +1 -1
- package/package.json +9 -9
- package/{dist/es/api/index.js → src/api/api.js} +9 -6
- package/src/api/ask.js +75 -0
- package/src/api/base.js +47 -0
- package/{dist/es → src}/api/helpers.js +47 -91
- package/src/api/index.js +7 -0
- package/src/api/interactions.js +54 -0
- package/{dist/es → src}/api/recommendation.js +11 -2
- package/{dist/es → src}/api/search.js +8 -2
- package/src/classes.js +7 -0
- package/{dist/es → src}/client.js +25 -10
- package/{dist/es → src}/cmd.js +7 -8
- package/{dist/es → src}/constants.js +2 -2
- package/{dist/es → src}/context.js +4 -1
- package/src/index.js +3 -0
- package/{dist/es → src}/plugin/anonymous.js +5 -4
- package/{dist/es → src}/plugin/auto-events.js +24 -25
- package/src/plugin/index.js +3 -0
- package/{dist/es → src}/plugin/page-info.js +28 -24
- package/{dist/es → src}/plugin/plugin-root.js +37 -22
- package/{dist/es → src}/plugin/urls.js +2 -1
- package/{dist/es → src}/plugin/user.js +32 -37
- package/{dist/es → src}/root.js +10 -4
- package/src/utils.js +1 -0
- package/src/version.js +1 -0
- package/dist/es/api/ask.js +0 -85
- package/dist/es/api/base.js +0 -96
- package/dist/es/api/interactions.js +0 -82
- package/dist/es/classes.js +0 -20
- package/dist/es/index.js +0 -2
- package/dist/es/plugin/index.js +0 -3
- package/dist/es/utils.js +0 -1
- package/dist/es/version.js +0 -1
|
@@ -1,49 +1,53 @@
|
|
|
1
1
|
import { trimObj } from '@miso.ai/commons';
|
|
2
|
+
|
|
2
3
|
const ID = 'std:page-info';
|
|
4
|
+
|
|
3
5
|
export default class PageInfoPlugin {
|
|
6
|
+
|
|
4
7
|
static get id() {
|
|
5
8
|
return ID;
|
|
6
9
|
}
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
10
|
+
|
|
11
|
+
constructor() {
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
install(_, { addPayloadPass }) {
|
|
12
15
|
addPayloadPass(this._modifyPayload.bind(this));
|
|
13
16
|
}
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
payload
|
|
19
|
-
} = _ref2;
|
|
20
|
-
return apiGroup === 'interactions' && apiName === 'upload' ? this._modifyPayloadForInteractions(payload) : payload;
|
|
17
|
+
|
|
18
|
+
_modifyPayload({ apiGroup, apiName, payload }) {
|
|
19
|
+
return apiGroup === 'interactions' && apiName === 'upload' ?
|
|
20
|
+
this._modifyPayloadForInteractions(payload) : payload;
|
|
21
21
|
}
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
22
|
+
|
|
23
|
+
_modifyPayloadForInteractions({ data }) {
|
|
24
|
+
data = data.map(obj => ({
|
|
25
|
+
...obj,
|
|
26
|
+
context: {
|
|
27
|
+
...getPayloadContext(),
|
|
28
|
+
...obj.context,
|
|
29
|
+
},
|
|
28
30
|
}));
|
|
29
|
-
return {
|
|
30
|
-
data
|
|
31
|
-
};
|
|
31
|
+
return { data };
|
|
32
32
|
}
|
|
33
|
+
|
|
33
34
|
}
|
|
35
|
+
|
|
34
36
|
function getPayloadContext() {
|
|
35
37
|
return trimObj({
|
|
36
38
|
page: readPageInfo(),
|
|
37
39
|
campaign: readUtm()
|
|
38
40
|
});
|
|
39
41
|
}
|
|
42
|
+
|
|
40
43
|
function readPageInfo() {
|
|
41
44
|
return trimObj({
|
|
42
45
|
url: window.location.href,
|
|
43
46
|
referrer: document.referrer,
|
|
44
|
-
title: document.title
|
|
47
|
+
title: document.title,
|
|
45
48
|
});
|
|
46
49
|
}
|
|
50
|
+
|
|
47
51
|
function readUtm() {
|
|
48
52
|
const params = new URLSearchParams(window.location.search);
|
|
49
53
|
let utm = trimObj({
|
|
@@ -51,7 +55,7 @@ function readUtm() {
|
|
|
51
55
|
medium: params.get('utm_medium') || undefined,
|
|
52
56
|
name: params.get('utm_campaign') || undefined,
|
|
53
57
|
term: params.get('utm_term') || undefined,
|
|
54
|
-
content: params.get('utm_content') || undefined
|
|
58
|
+
content: params.get('utm_content') || undefined,
|
|
55
59
|
});
|
|
56
60
|
return Object.keys(utm).length === 0 ? undefined : utm;
|
|
57
|
-
}
|
|
61
|
+
}
|
|
@@ -1,12 +1,14 @@
|
|
|
1
|
-
import { delegateGetters, defineValues, Registry, Resolution, Resources
|
|
2
|
-
import classes from '../classes';
|
|
3
|
-
import AnonymousPlugin from './anonymous';
|
|
4
|
-
import { getPluginScriptUrl } from './urls';
|
|
1
|
+
import { delegateGetters, defineValues, Registry, Resolution, Resources } from '@miso.ai/commons';
|
|
2
|
+
import classes from '../classes.js';
|
|
3
|
+
import AnonymousPlugin from './anonymous.js';
|
|
4
|
+
import { getPluginScriptUrl } from './urls.js';
|
|
5
|
+
|
|
5
6
|
export default class PluginRoot extends Registry {
|
|
7
|
+
|
|
6
8
|
constructor(root) {
|
|
7
9
|
super('plugins', root, {
|
|
8
10
|
libName: 'plugin class',
|
|
9
|
-
keyName: 'id'
|
|
11
|
+
keyName: 'id',
|
|
10
12
|
});
|
|
11
13
|
this._root = root;
|
|
12
14
|
this._currentScript = document.currentScript;
|
|
@@ -18,16 +20,20 @@ export default class PluginRoot extends Registry {
|
|
|
18
20
|
this.context = new PluginContext(this, classes);
|
|
19
21
|
this.plugins = new Plugins(this);
|
|
20
22
|
}
|
|
23
|
+
|
|
21
24
|
isInstalled(id) {
|
|
22
25
|
this._checkKey(id);
|
|
23
26
|
return !!this._installed[id];
|
|
24
27
|
}
|
|
28
|
+
|
|
25
29
|
get installed() {
|
|
26
30
|
return Object.values(this._installed);
|
|
27
31
|
}
|
|
32
|
+
|
|
28
33
|
get(id) {
|
|
29
34
|
return this._installed[id];
|
|
30
35
|
}
|
|
36
|
+
|
|
31
37
|
getPluginClass(id) {
|
|
32
38
|
return this._libraries[id];
|
|
33
39
|
}
|
|
@@ -56,6 +62,7 @@ export default class PluginRoot extends Registry {
|
|
|
56
62
|
instance = this._instantiate(plugin, options);
|
|
57
63
|
this._install(instance);
|
|
58
64
|
}
|
|
65
|
+
|
|
59
66
|
async install(plugin, options) {
|
|
60
67
|
this.use(plugin, options);
|
|
61
68
|
if (typeof plugin === 'string' && !this.isRegistered(plugin)) {
|
|
@@ -63,31 +70,34 @@ export default class PluginRoot extends Registry {
|
|
|
63
70
|
}
|
|
64
71
|
return this.whenInstalled(plugin);
|
|
65
72
|
}
|
|
73
|
+
|
|
66
74
|
async whenInstalled(id) {
|
|
67
75
|
if (this.isInstalled(id)) {
|
|
68
76
|
return this.get(id);
|
|
69
77
|
}
|
|
70
|
-
const {
|
|
71
|
-
promise
|
|
72
|
-
} = this._installedResolutions[id] || (this._installedResolutions[id] = new Resolution());
|
|
78
|
+
const { promise } = this._installedResolutions[id] || (this._installedResolutions[id] = new Resolution());
|
|
73
79
|
return promise;
|
|
74
80
|
}
|
|
81
|
+
|
|
75
82
|
addSubtree(component) {
|
|
76
83
|
this._subtrees.push(component);
|
|
77
84
|
this._events.emit('subtree', component);
|
|
78
85
|
}
|
|
86
|
+
|
|
79
87
|
addPayloadPass(pass) {
|
|
80
88
|
if (typeof pass !== 'function') {
|
|
81
89
|
throw new Error(`Expect parameter to be a function: ${pass}`);
|
|
82
90
|
}
|
|
83
91
|
this._root._payloadPasses.push(pass);
|
|
84
92
|
}
|
|
93
|
+
|
|
85
94
|
addUrlPass(pass) {
|
|
86
95
|
if (typeof pass !== 'function') {
|
|
87
96
|
throw new Error(`Expect parameter to be a function: ${pass}`);
|
|
88
97
|
}
|
|
89
98
|
this._root._urlPasses.push(pass);
|
|
90
99
|
}
|
|
100
|
+
|
|
91
101
|
_tryConfig(instance, options) {
|
|
92
102
|
if (options === undefined) {
|
|
93
103
|
return;
|
|
@@ -99,6 +109,7 @@ export default class PluginRoot extends Registry {
|
|
|
99
109
|
this._warn(`Attempt to pass options to plugin "${instance.id}", which offers no config() method.`);
|
|
100
110
|
}
|
|
101
111
|
}
|
|
112
|
+
|
|
102
113
|
_instantiate(plugin, options) {
|
|
103
114
|
switch (typeof plugin) {
|
|
104
115
|
case 'string':
|
|
@@ -108,9 +119,7 @@ export default class PluginRoot extends Registry {
|
|
|
108
119
|
throw new Error(`Plugin class not found: ${plugin}`);
|
|
109
120
|
}
|
|
110
121
|
const instance = new pluginClass();
|
|
111
|
-
defineValues(instance, {
|
|
112
|
-
id: plugin
|
|
113
|
-
});
|
|
122
|
+
defineValues(instance, { id: plugin });
|
|
114
123
|
this._tryConfig(instance, options);
|
|
115
124
|
return instance;
|
|
116
125
|
case 'function':
|
|
@@ -118,6 +127,7 @@ export default class PluginRoot extends Registry {
|
|
|
118
127
|
}
|
|
119
128
|
throw new Error(`Unexpected parameters: ${plugin}`);
|
|
120
129
|
}
|
|
130
|
+
|
|
121
131
|
_install(instance) {
|
|
122
132
|
if (typeof instance.install !== 'function') {
|
|
123
133
|
// TODO: introduce PluginError
|
|
@@ -127,7 +137,7 @@ export default class PluginRoot extends Registry {
|
|
|
127
137
|
instance.installed = false;
|
|
128
138
|
instance.install(this.MisoClient, this.context);
|
|
129
139
|
instance.installed = true;
|
|
130
|
-
} catch
|
|
140
|
+
} catch(e) {
|
|
131
141
|
// TODO: introduce PluginError
|
|
132
142
|
throw e;
|
|
133
143
|
}
|
|
@@ -141,39 +151,41 @@ export default class PluginRoot extends Registry {
|
|
|
141
151
|
}
|
|
142
152
|
this._events.emit('install', instance);
|
|
143
153
|
}
|
|
154
|
+
|
|
144
155
|
_register(lib) {
|
|
145
156
|
super._register(lib);
|
|
146
157
|
this._fulfillUseRequests(lib.id);
|
|
147
158
|
}
|
|
159
|
+
|
|
148
160
|
_fulfillUseRequests(id) {
|
|
149
161
|
const requests = this._useRequests[id];
|
|
150
162
|
if (requests) {
|
|
151
163
|
for (const options of requests) {
|
|
152
164
|
try {
|
|
153
165
|
this.use(id, options);
|
|
154
|
-
} catch
|
|
166
|
+
} catch(e) {
|
|
155
167
|
this._error(e);
|
|
156
168
|
}
|
|
157
169
|
}
|
|
158
170
|
delete this._useRequests[id];
|
|
159
171
|
}
|
|
160
172
|
}
|
|
173
|
+
|
|
161
174
|
async _tryRegisterRemotely(id) {
|
|
162
175
|
if (id.startsWith('std:')) {
|
|
163
176
|
try {
|
|
164
177
|
await this._registerStdRemotely(id);
|
|
165
178
|
return;
|
|
166
|
-
} catch
|
|
179
|
+
} catch(e) {
|
|
167
180
|
console.error(e);
|
|
168
181
|
}
|
|
169
182
|
}
|
|
170
183
|
// TODO: try as URL
|
|
171
184
|
throw new Error(`Cannot register plugin remotely: ${id}`);
|
|
172
185
|
}
|
|
186
|
+
|
|
173
187
|
async _registerStdRemotely(id) {
|
|
174
|
-
const {
|
|
175
|
-
version
|
|
176
|
-
} = this._root;
|
|
188
|
+
const { version } = this._root;
|
|
177
189
|
const request = version === 'dev' ? `plugin:${id}@dev:${MisoClient.uuid}` : `plugin:${id}@${version}`;
|
|
178
190
|
|
|
179
191
|
// check if such script already exists
|
|
@@ -191,25 +203,28 @@ export default class PluginRoot extends Registry {
|
|
|
191
203
|
const Plugin = await new Resources().get(request);
|
|
192
204
|
!this.isRegistered(id) && this.register(Plugin);
|
|
193
205
|
}
|
|
206
|
+
|
|
194
207
|
}
|
|
195
208
|
|
|
196
209
|
/**
|
|
197
210
|
* The interface of extended features only for plugin developers.
|
|
198
211
|
*/
|
|
199
212
|
class PluginContext {
|
|
213
|
+
|
|
200
214
|
constructor(root, classes) {
|
|
201
215
|
delegateGetters(this, root, ['getPluginClass', 'addSubtree', 'addPayloadPass', 'addUrlPass', 'whenInstalled', 'whenRegistered']);
|
|
202
|
-
defineValues(this, {
|
|
203
|
-
classes
|
|
204
|
-
});
|
|
216
|
+
defineValues(this, { classes });
|
|
205
217
|
}
|
|
218
|
+
|
|
206
219
|
}
|
|
207
220
|
|
|
208
221
|
/**
|
|
209
222
|
* The interface of plugin features exposed to MisoClient.
|
|
210
223
|
*/
|
|
211
|
-
class Plugins {
|
|
224
|
+
class Plugins {
|
|
225
|
+
|
|
212
226
|
constructor(root) {
|
|
213
227
|
delegateGetters(this, root, ['installed', 'registered', 'isInstalled', 'isRegistered', 'whenInstalled', 'whenRegistered', 'get', 'register', 'use', 'install']);
|
|
214
228
|
}
|
|
215
|
-
|
|
229
|
+
|
|
230
|
+
}
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
export function getPluginScriptUrl(id, version, currentScript) {
|
|
2
2
|
// TODO: look up by manifest
|
|
3
3
|
const pkgName = id.slice(4);
|
|
4
|
+
|
|
4
5
|
if (version === 'dev') {
|
|
5
6
|
const src = currentScript.src;
|
|
6
7
|
const searchIndex = src.indexOf('?');
|
|
@@ -12,4 +13,4 @@ export function getPluginScriptUrl(id, version, currentScript) {
|
|
|
12
13
|
} else {
|
|
13
14
|
return `https://cdn.jsdelivr.net/npm/@miso.ai/client-sdk@${version}/dist/umd/plugins/${pkgName}.min.js`;
|
|
14
15
|
}
|
|
15
|
-
}
|
|
16
|
+
}
|
|
@@ -1,90 +1,84 @@
|
|
|
1
1
|
import { uuidv4, asString, trimObj, Component, delegateProperties, getOrComputeFromStorage } from '@miso.ai/commons';
|
|
2
|
+
|
|
2
3
|
const PLUGIN_ID = 'std:user';
|
|
4
|
+
|
|
3
5
|
export default class UserPlugin extends Component {
|
|
6
|
+
|
|
4
7
|
static get id() {
|
|
5
8
|
return PLUGIN_ID;
|
|
6
9
|
}
|
|
10
|
+
|
|
7
11
|
constructor() {
|
|
8
12
|
super('user');
|
|
9
13
|
this._contexts = new WeakMap();
|
|
10
14
|
}
|
|
15
|
+
|
|
11
16
|
install(MisoClient, context) {
|
|
12
17
|
context.addSubtree(this);
|
|
13
18
|
MisoClient.on('create', this._injectClient.bind(this));
|
|
14
19
|
context.addPayloadPass(this._modifyPayload.bind(this));
|
|
15
20
|
}
|
|
21
|
+
|
|
16
22
|
_injectClient(client) {
|
|
17
23
|
const context = new UserContext(this);
|
|
18
24
|
this._contexts.set(client, context);
|
|
19
25
|
delegateProperties(client.context, context, ['anonymous_id', 'user_id', 'user_hash']);
|
|
20
26
|
}
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
apiName,
|
|
26
|
-
payload
|
|
27
|
-
} = _ref;
|
|
28
|
-
return apiGroup === 'interactions' && apiName === 'upload' ? this._modifyPayloadForInteractions(client, payload) : this._modifyPayloadForOthers(client, payload);
|
|
27
|
+
|
|
28
|
+
_modifyPayload({ client, apiGroup, apiName, payload }) {
|
|
29
|
+
return apiGroup === 'interactions' && apiName === 'upload' ?
|
|
30
|
+
this._modifyPayloadForInteractions(client, payload) : this._modifyPayloadForOthers(client, payload);
|
|
29
31
|
}
|
|
32
|
+
|
|
30
33
|
_modifyPayloadForOthers(client, payload) {
|
|
31
|
-
const {
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
anonymous_id
|
|
35
|
-
} = client.context;
|
|
36
|
-
const userInfo = user_id ? {
|
|
37
|
-
user_id,
|
|
38
|
-
user_hash
|
|
39
|
-
} : {
|
|
40
|
-
anonymous_id
|
|
41
|
-
};
|
|
42
|
-
return Object.assign({}, userInfo, payload);
|
|
34
|
+
const { user_id, user_hash, anonymous_id } = client.context;
|
|
35
|
+
const userInfo = user_id ? { user_id, user_hash } : { anonymous_id };
|
|
36
|
+
return { ...userInfo, ...payload };
|
|
43
37
|
}
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
user_id
|
|
51
|
-
} = client.context;
|
|
52
|
-
const baseObj = trimObj({
|
|
53
|
-
anonymous_id,
|
|
54
|
-
user_id
|
|
55
|
-
});
|
|
56
|
-
data = data.map(obj => Object.assign({}, baseObj, obj));
|
|
57
|
-
return {
|
|
58
|
-
data
|
|
59
|
-
};
|
|
38
|
+
|
|
39
|
+
_modifyPayloadForInteractions(client, { data }) {
|
|
40
|
+
const { anonymous_id, user_id } = client.context;
|
|
41
|
+
const baseObj = trimObj({ anonymous_id, user_id });
|
|
42
|
+
data = data.map(obj => ({ ...baseObj, ...obj }));
|
|
43
|
+
return { data };
|
|
60
44
|
}
|
|
45
|
+
|
|
61
46
|
}
|
|
47
|
+
|
|
62
48
|
function getAutoAnonymousId() {
|
|
63
49
|
return getOrComputeFromStorage('miso_anonymous_id', uuidv4);
|
|
64
50
|
}
|
|
51
|
+
|
|
65
52
|
class UserContext {
|
|
53
|
+
|
|
66
54
|
constructor(plugin) {
|
|
67
55
|
this._plugin = plugin;
|
|
68
56
|
}
|
|
57
|
+
|
|
69
58
|
get anonymous_id() {
|
|
70
59
|
return this._anonymousId || this._autoAnonymousId || (this._autoAnonymousId = getAutoAnonymousId());
|
|
71
60
|
}
|
|
61
|
+
|
|
72
62
|
set anonymous_id(value) {
|
|
73
63
|
value = asString(value);
|
|
74
64
|
this._anonymousId = value;
|
|
75
65
|
this._plugin._events.emit('set', `anonymous_id = '${value}'`);
|
|
76
66
|
}
|
|
67
|
+
|
|
77
68
|
get user_id() {
|
|
78
69
|
return this._userId;
|
|
79
70
|
}
|
|
71
|
+
|
|
80
72
|
set user_id(value) {
|
|
81
73
|
value = asString(value);
|
|
82
74
|
this._userId = value;
|
|
83
75
|
this._plugin._events.emit('set', `user_id = '${value}'`);
|
|
84
76
|
}
|
|
77
|
+
|
|
85
78
|
get user_hash() {
|
|
86
79
|
return this._userHash;
|
|
87
80
|
}
|
|
81
|
+
|
|
88
82
|
set user_hash(value) {
|
|
89
83
|
if (typeof value !== 'string' && typeof value !== 'undefined') {
|
|
90
84
|
throw new Error(`User hash must be a string`);
|
|
@@ -92,4 +86,5 @@ class UserContext {
|
|
|
92
86
|
this._userHash = value;
|
|
93
87
|
this._plugin._events.emit('set', `user_hash = '${value}'`);
|
|
94
88
|
}
|
|
95
|
-
|
|
89
|
+
|
|
90
|
+
}
|
package/{dist/es → src}/root.js
RENAMED
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
import { delegateGetters, Component } from '@miso.ai/commons';
|
|
2
|
-
import version from './version';
|
|
3
|
-
import PluginRoot from './plugin/plugin-root';
|
|
2
|
+
import version from './version.js';
|
|
3
|
+
import PluginRoot from './plugin/plugin-root.js';
|
|
4
4
|
|
|
5
5
|
/**
|
|
6
6
|
* The hidden singleton root component that oversees all client components.
|
|
7
7
|
*/
|
|
8
8
|
class Root extends Component {
|
|
9
|
+
|
|
9
10
|
constructor() {
|
|
10
11
|
super();
|
|
11
12
|
this._events._replays.add('create');
|
|
@@ -15,15 +16,19 @@ class Root extends Component {
|
|
|
15
16
|
this._urlPasses = [];
|
|
16
17
|
this.version = version || 'dev';
|
|
17
18
|
}
|
|
19
|
+
|
|
18
20
|
get instances() {
|
|
19
|
-
return [...this._clients]; // write protection
|
|
21
|
+
return [ ...this._clients]; // write protection
|
|
20
22
|
}
|
|
21
23
|
|
|
22
24
|
async any() {
|
|
23
25
|
return this._clients[0] || this._any || (this._any = (await this._events.once('create')).data);
|
|
24
26
|
}
|
|
27
|
+
|
|
25
28
|
}
|
|
29
|
+
|
|
26
30
|
export const root = new Root();
|
|
31
|
+
|
|
27
32
|
export function init(MisoClient) {
|
|
28
33
|
const pluginRoot = root._pluginRoot;
|
|
29
34
|
root.MisoClient = pluginRoot.MisoClient = MisoClient;
|
|
@@ -31,7 +36,8 @@ export function init(MisoClient) {
|
|
|
31
36
|
delegateGetters(MisoClient, pluginRoot, ['plugins']);
|
|
32
37
|
return MisoClient;
|
|
33
38
|
}
|
|
39
|
+
|
|
34
40
|
export function register(client) {
|
|
35
41
|
root._clients.push(client);
|
|
36
42
|
root._events.emit('create', client);
|
|
37
|
-
}
|
|
43
|
+
}
|
package/src/utils.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { viewable } from '@miso.ai/commons';
|
package/src/version.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export default '1.8.0-beta.18';
|
package/dist/es/api/ask.js
DELETED
|
@@ -1,85 +0,0 @@
|
|
|
1
|
-
const _excluded = ["pollingInterval", "signal", "stallTimeout"];
|
|
2
|
-
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; }
|
|
3
|
-
import { polling, signals, StallTimeoutAbortController } from '@miso.ai/commons';
|
|
4
|
-
import ApiBase from './base';
|
|
5
|
-
export default class Ask extends ApiBase {
|
|
6
|
-
constructor(api) {
|
|
7
|
-
super(api, 'ask');
|
|
8
|
-
}
|
|
9
|
-
async questions(payload) {
|
|
10
|
-
let options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
|
11
|
-
if (typeof payload === 'string' && payload.charAt(0) !== '{') {
|
|
12
|
-
return new Answer(this, {
|
|
13
|
-
question_id: payload
|
|
14
|
-
}, options);
|
|
15
|
-
}
|
|
16
|
-
const response = await this._run('questions', payload, options);
|
|
17
|
-
return new Answer(this, response, options);
|
|
18
|
-
}
|
|
19
|
-
async _questions(payload, options) {
|
|
20
|
-
return this._run('questions', payload, options);
|
|
21
|
-
}
|
|
22
|
-
async _get(questionId, options) {
|
|
23
|
-
return this._run(`questions/${questionId}/answer`, undefined, Object.assign({}, options, {
|
|
24
|
-
method: 'GET'
|
|
25
|
-
}));
|
|
26
|
-
}
|
|
27
|
-
}
|
|
28
|
-
class Answer {
|
|
29
|
-
constructor(api, _ref) {
|
|
30
|
-
let {
|
|
31
|
-
question_id
|
|
32
|
-
} = _ref;
|
|
33
|
-
let options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
|
|
34
|
-
this._api = api;
|
|
35
|
-
this._questionId = question_id;
|
|
36
|
-
this._ac = new AbortController();
|
|
37
|
-
this._options = options;
|
|
38
|
-
}
|
|
39
|
-
get questionId() {
|
|
40
|
-
return this._questionId;
|
|
41
|
-
}
|
|
42
|
-
async get(options) {
|
|
43
|
-
return this._api._get(this._questionId, Object.assign({}, this._options, options));
|
|
44
|
-
}
|
|
45
|
-
abort(reason) {
|
|
46
|
-
this._ac.abort(reason);
|
|
47
|
-
}
|
|
48
|
-
[Symbol.asyncIterator]() {
|
|
49
|
-
var _this = this;
|
|
50
|
-
const fetch = async function () {
|
|
51
|
-
let {
|
|
52
|
-
signal
|
|
53
|
-
} = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
|
|
54
|
-
// TODO: pass signal
|
|
55
|
-
const response = await _this.get();
|
|
56
|
-
return [response, response.finished];
|
|
57
|
-
};
|
|
58
|
-
let _this$_options = this._options,
|
|
59
|
-
{
|
|
60
|
-
pollingInterval: interval,
|
|
61
|
-
signal,
|
|
62
|
-
stallTimeout = 120000
|
|
63
|
-
} = _this$_options,
|
|
64
|
-
options = _objectWithoutPropertiesLoose(_this$_options, _excluded);
|
|
65
|
-
const stac = new StallTimeoutAbortController(stallTimeout);
|
|
66
|
-
signal = signals.any(this._ac.signal, stac.signal, signal);
|
|
67
|
-
let prevResponse;
|
|
68
|
-
const onResponse = (response, finished) => {
|
|
69
|
-
if (finished) {
|
|
70
|
-
stac.clear();
|
|
71
|
-
} else if (isUpdated(prevResponse, response)) {
|
|
72
|
-
stac.touch();
|
|
73
|
-
}
|
|
74
|
-
prevResponse = response;
|
|
75
|
-
};
|
|
76
|
-
return polling(fetch, Object.assign({
|
|
77
|
-
interval,
|
|
78
|
-
signal,
|
|
79
|
-
onResponse
|
|
80
|
-
}, options))[Symbol.asyncIterator]();
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
function isUpdated(prev, curr) {
|
|
84
|
-
return !prev || prev.answer_stage !== curr.answer_stage || prev.answer.length !== curr.answer.length;
|
|
85
|
-
}
|
package/dist/es/api/base.js
DELETED
|
@@ -1,96 +0,0 @@
|
|
|
1
|
-
const _excluded = ["bulk"];
|
|
2
|
-
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; }
|
|
3
|
-
import { Component } from '@miso.ai/commons';
|
|
4
|
-
export default class ApiBase extends Component {
|
|
5
|
-
// TODO: use private fields (may encounter issues with rollup)
|
|
6
|
-
|
|
7
|
-
constructor(api, apiPath) {
|
|
8
|
-
super(apiPath, api);
|
|
9
|
-
this._apiPath = apiPath;
|
|
10
|
-
this.helpers = api.helpers;
|
|
11
|
-
this.clientOptions = api._client.options;
|
|
12
|
-
this.context = api._client.context;
|
|
13
|
-
}
|
|
14
|
-
async _run(apiName, payload) {
|
|
15
|
-
let options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
|
|
16
|
-
const {
|
|
17
|
-
bulk
|
|
18
|
-
} = options;
|
|
19
|
-
const bulkInfo = bulk ? {
|
|
20
|
-
bulk: this.helpers.bulkInfo
|
|
21
|
-
} : undefined;
|
|
22
|
-
const apiGroup = this._apiPath;
|
|
23
|
-
const url = this._url({
|
|
24
|
-
apiGroup,
|
|
25
|
-
apiName,
|
|
26
|
-
payload,
|
|
27
|
-
options
|
|
28
|
-
});
|
|
29
|
-
payload = this._preprocess({
|
|
30
|
-
apiGroup,
|
|
31
|
-
apiName,
|
|
32
|
-
payload,
|
|
33
|
-
options
|
|
34
|
-
});
|
|
35
|
-
const requestData = {
|
|
36
|
-
apiGroup,
|
|
37
|
-
apiName,
|
|
38
|
-
payload,
|
|
39
|
-
url,
|
|
40
|
-
options
|
|
41
|
-
};
|
|
42
|
-
this._events.emit('request', Object.assign({}, requestData, bulkInfo));
|
|
43
|
-
const response = await this._send(requestData);
|
|
44
|
-
const responseData = Object.assign({}, requestData, {
|
|
45
|
-
response
|
|
46
|
-
});
|
|
47
|
-
this._events.emit('response', Object.assign({}, responseData, bulkInfo));
|
|
48
|
-
return this._postprocess(responseData);
|
|
49
|
-
}
|
|
50
|
-
_preprocess(_ref) {
|
|
51
|
-
let {
|
|
52
|
-
apiGroup,
|
|
53
|
-
apiName,
|
|
54
|
-
payload
|
|
55
|
-
} = _ref;
|
|
56
|
-
return this.helpers.applyPayloadPasses(this, {
|
|
57
|
-
apiGroup,
|
|
58
|
-
apiName,
|
|
59
|
-
payload
|
|
60
|
-
});
|
|
61
|
-
}
|
|
62
|
-
_url(_ref2) {
|
|
63
|
-
let {
|
|
64
|
-
apiGroup,
|
|
65
|
-
apiName,
|
|
66
|
-
options
|
|
67
|
-
} = _ref2;
|
|
68
|
-
const url = this.helpers.url([apiGroup, apiName], options);
|
|
69
|
-
return this.helpers.applyUrlPasses(this, {
|
|
70
|
-
apiGroup,
|
|
71
|
-
apiName,
|
|
72
|
-
url,
|
|
73
|
-
options
|
|
74
|
-
});
|
|
75
|
-
}
|
|
76
|
-
async _send(_ref3) {
|
|
77
|
-
let {
|
|
78
|
-
apiGroup,
|
|
79
|
-
apiName,
|
|
80
|
-
url,
|
|
81
|
-
payload,
|
|
82
|
-
options: {
|
|
83
|
-
bulk
|
|
84
|
-
} = {}
|
|
85
|
-
} = _ref3,
|
|
86
|
-
options = _objectWithoutPropertiesLoose(_ref3.options, _excluded);
|
|
87
|
-
return bulk ? this.helpers.fetchForBulk(apiGroup, apiName, payload) : this.helpers.fetch(url, payload, options);
|
|
88
|
-
}
|
|
89
|
-
_postprocess(_ref4) {
|
|
90
|
-
let {
|
|
91
|
-
response
|
|
92
|
-
} = _ref4;
|
|
93
|
-
// TODO: we can introduce response passes
|
|
94
|
-
return response.data;
|
|
95
|
-
}
|
|
96
|
-
}
|