@miso.ai/client-sdk-ui 1.9.4 → 1.9.5-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.
@@ -50,7 +50,8 @@ class SessionMaker {
50
50
  return Object.freeze({
51
51
  active: false,
52
52
  uuid: (0, _commons.uuidv4)(),
53
- index: this._sessionIndex++
53
+ index: this._sessionIndex++,
54
+ meta: {}
54
55
  });
55
56
  }
56
57
  }
@@ -106,16 +106,24 @@ class Tracker {
106
106
  const state = this._getViewState();
107
107
  return state && state.meta && state.meta.miso_id || this._element && this._element.getAttribute('miso-id') || undefined;
108
108
  }
109
+ _getSessionMeta() {
110
+ const {
111
+ session
112
+ } = this._getViewState() || {};
113
+ return session && session.meta;
114
+ }
109
115
  getState(productId) {
110
116
  return this._tracker.getState(productId);
111
117
  }
112
118
  _handleEvent(data) {
113
119
  const property = this._role === 'results' ? 'products' : this._role; // TODO: ad-hoc, see #83
114
120
  const misoId = this._getMisoId();
121
+ const meta = this._getSessionMeta();
115
122
  // TODO: should trigger 'tracker' and let workflow translate to interactions
116
123
  this._hub.trigger(fields.interaction(), (0, _utils.toInteraction)({
117
124
  property,
118
- misoId
125
+ misoId,
126
+ meta
119
127
  }, data));
120
128
  }
121
129
  _destroy() {
@@ -16,7 +16,8 @@ function isProduct(property) {
16
16
  function toInteraction(_ref, _ref2) {
17
17
  let {
18
18
  property,
19
- misoId
19
+ misoId,
20
+ meta
20
21
  } = _ref;
21
22
  let {
22
23
  event,
@@ -37,12 +38,12 @@ function toInteraction(_ref, _ref2) {
37
38
  product_ids,
38
39
  miso_id: misoId,
39
40
  context: {
40
- custom_context: (0, _commons.trimObj)({
41
+ custom_context: (0, _commons.trimObj)(Object.assign({
41
42
  api_ts,
42
43
  property,
43
44
  items,
44
45
  trigger: manual ? 'manual' : 'auto'
45
- })
46
+ }, meta))
46
47
  }
47
48
  });
48
49
  }
@@ -4,4 +4,4 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.default = void 0;
7
- var _default = exports.default = '1.9.4';
7
+ var _default = exports.default = '1.9.5-beta.0';
@@ -10,7 +10,7 @@ var _options = require("./options.js");
10
10
  var _index = require("../actor/index.js");
11
11
  var _constants = require("../constants.js");
12
12
  var _index2 = require("../layout/index.js");
13
- const _excluded = ["q"];
13
+ const _excluded = ["q", "s"];
14
14
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
15
15
  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; }
16
16
  const DEFAULT_API_OPTIONS = Object.freeze({
@@ -116,7 +116,9 @@ class Ask extends _base.default {
116
116
  setValue = true,
117
117
  focus = true
118
118
  } = this._autoQuery = options;
119
- const q = new URLSearchParams(window.location.search).get('q');
119
+ const searchParams = new URLSearchParams(window.location.search);
120
+ const q = searchParams.get('q');
121
+ const s = searchParams.get('s');
120
122
  const {
121
123
  layout
122
124
  } = this._views.get(_constants.ROLE.QUERY);
@@ -130,14 +132,16 @@ class Ask extends _base.default {
130
132
  }
131
133
  if (q) {
132
134
  this.query({
133
- q
135
+ q,
136
+ s
134
137
  });
135
138
  }
136
139
  }
137
140
  query() {
138
141
  let _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
139
142
  let {
140
- q: question
143
+ q: question,
144
+ s: source
141
145
  } = _ref,
142
146
  payload = _objectWithoutPropertiesLoose(_ref, _excluded);
143
147
  payload = Object.assign({}, payload, {
@@ -146,6 +150,9 @@ class Ask extends _base.default {
146
150
  if (this.parentQuestionId) {
147
151
  payload.parent_question_id = this.parentQuestionId;
148
152
  }
153
+ if (source) {
154
+ (payload._meta || (payload._meta = {})).question_source = source;
155
+ }
149
156
 
150
157
  // clear question id from previous session
151
158
  if (this._questionId) {
@@ -156,6 +163,9 @@ class Ask extends _base.default {
156
163
  const {
157
164
  session
158
165
  } = this;
166
+ if (source) {
167
+ session.meta.question_source = source;
168
+ }
159
169
  this._hub.update(_index.fields.request(), (0, _options.mergeApiOptions)(this._options.resolved.api, {
160
170
  payload,
161
171
  session
@@ -204,6 +214,7 @@ class Ask extends _base.default {
204
214
  if (this._autoQuery && this._autoQuery.updateUrl !== false) {
205
215
  const url = new URL(window.location);
206
216
  const currentQuestion = url.searchParams.get('q');
217
+ // TODO: review this, handle &s=
207
218
  if (question !== currentQuestion && (currentQuestion || this._autoQuery.updateUrl === true)) {
208
219
  url.searchParams.set('q', question);
209
220
  window.history.replaceState({}, '', url);
@@ -306,6 +317,7 @@ class Ask extends _base.default {
306
317
  questionId = parentQuestionId;
307
318
  parentQuestionId = this.previous && this.previous.parentQuestionId;
308
319
  custom_context.property = 'suggested_followup_questions';
320
+ // TODO: question_source
309
321
  }
310
322
  payload.context = Object.assign({}, context, {
311
323
  custom_context: (0, _commons.trimObj)(Object.assign({
@@ -52,6 +52,15 @@ class Explore extends _base.default {
52
52
  product_id: value
53
53
  });
54
54
  }
55
+ useApi(options) {
56
+ const {
57
+ product_id
58
+ } = options;
59
+ if (product_id) {
60
+ this._productId = product_id;
61
+ }
62
+ return super.useApi(options);
63
+ }
55
64
  useLink(fn) {
56
65
  if (typeof fn !== 'function' && fn !== false) {
57
66
  throw new Error('useLink(fn) expects fn to be a function or false');
@@ -95,7 +104,7 @@ class Explore extends _base.default {
95
104
  // patch value with links
96
105
  const related_questions = value.related_questions.map(this._linkFn ? text => ({
97
106
  text,
98
- url: this._linkFn(text)
107
+ url: this._getAnswersUrl(text)
99
108
  }) : text => ({
100
109
  text
101
110
  }));
@@ -121,8 +130,17 @@ class Explore extends _base.default {
121
130
  if (!this._linkFn) {
122
131
  return;
123
132
  }
124
- const url = this._linkFn(q);
133
+ const url = this._getAnswersUrl(q);
125
134
  window.open(url, '_blank');
126
135
  }
136
+ _getAnswersUrl(text) {
137
+ if (!this._linkFn) {
138
+ return;
139
+ }
140
+ const url = this._linkFn(text);
141
+ if (this._productId) {
142
+ return `${url}&s=${encodeURIComponent(this._productId)}`;
143
+ }
144
+ }
127
145
  }
128
146
  exports.default = Explore;
@@ -135,9 +135,20 @@ function mergeApiOptions() {
135
135
  return false;
136
136
  }
137
137
  return mergeOptions(optionsList, (merged, options) => Object.assign(merged, Object.assign({}, options, {
138
- payload: Object.assign({}, merged.payload, options.payload)
138
+ payload: mergeApiPayloads(merged.payload, options.payload)
139
139
  })));
140
140
  }
141
+ function mergeApiPayloads(base, overrides) {
142
+ return Object.assign({}, base, overrides, mergeObjectValueIfPresent('_meta', base, overrides));
143
+ }
144
+ function mergeObjectValueIfPresent(key, base, overrides) {
145
+ const baseValue = base && base[key];
146
+ const overridesValue = overrides && overrides[key];
147
+ const value = baseValue ? overridesValue ? Object.assign({}, baseValue, overridesValue) : baseValue : overridesValue;
148
+ return value !== undefined ? {
149
+ [key]: value
150
+ } : {};
151
+ }
141
152
  function mergeLayoutsOptions() {
142
153
  for (var _len2 = arguments.length, optionsList = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
143
154
  optionsList[_key2] = arguments[_key2];
package/package.json CHANGED
@@ -26,7 +26,7 @@
26
26
  "url": "https://github.com/MisoAI/miso-client-js-sdk/issues"
27
27
  },
28
28
  "dependencies": {
29
- "@miso.ai/commons": "1.9.4"
29
+ "@miso.ai/commons": "1.9.5-beta.0"
30
30
  },
31
- "version": "1.9.4"
31
+ "version": "1.9.5-beta.0"
32
32
  }
@@ -41,6 +41,7 @@ export default class SessionMaker {
41
41
  active: false,
42
42
  uuid: uuidv4(),
43
43
  index: this._sessionIndex++,
44
+ meta: {},
44
45
  });
45
46
  }
46
47
 
@@ -101,6 +101,11 @@ export default class Tracker {
101
101
  return (state && state.meta && state.meta.miso_id) || (this._element && this._element.getAttribute('miso-id')) || undefined;
102
102
  }
103
103
 
104
+ _getSessionMeta() {
105
+ const { session } = this._getViewState() || {};
106
+ return session && session.meta;
107
+ }
108
+
104
109
  getState(productId) {
105
110
  return this._tracker.getState(productId);
106
111
  }
@@ -108,8 +113,9 @@ export default class Tracker {
108
113
  _handleEvent(data) {
109
114
  const property = this._role === 'results' ? 'products' : this._role; // TODO: ad-hoc, see #83
110
115
  const misoId = this._getMisoId();
116
+ const meta = this._getSessionMeta();
111
117
  // TODO: should trigger 'tracker' and let workflow translate to interactions
112
- this._hub.trigger(fields.interaction(), toInteraction({ property, misoId }, data));
118
+ this._hub.trigger(fields.interaction(), toInteraction({ property, misoId, meta }, data));
113
119
  }
114
120
 
115
121
  _destroy() {
@@ -12,7 +12,7 @@ export function isProduct(property) {
12
12
  return PRODUCT_TYPED_PROPERTIES.has(property);
13
13
  }
14
14
 
15
- export function toInteraction({ property, misoId }, { event, values, manual }) {
15
+ export function toInteraction({ property, misoId, meta }, { event, values, manual }) {
16
16
  let api_ts;
17
17
  if (misoId) {
18
18
  try {
@@ -32,6 +32,7 @@ export function toInteraction({ property, misoId }, { event, values, manual }) {
32
32
  property,
33
33
  items,
34
34
  trigger: manual ? 'manual' : 'auto',
35
+ ...meta,
35
36
  }),
36
37
  },
37
38
  });
package/src/version.js CHANGED
@@ -1 +1 @@
1
- export default '1.9.4';
1
+ export default '1.9.5-beta.0';
@@ -99,7 +99,9 @@ export default class Ask extends Workflow {
99
99
  // lifecycle //
100
100
  autoQuery(options = {}) {
101
101
  const { setValue = true, focus = true } = this._autoQuery = options;
102
- const q = new URLSearchParams(window.location.search).get('q');
102
+ const searchParams = new URLSearchParams(window.location.search);
103
+ const q = searchParams.get('q');
104
+ const s = searchParams.get('s');
103
105
  const { layout } = this._views.get(ROLE.QUERY);
104
106
  if (layout) {
105
107
  if (q && setValue) {
@@ -110,15 +112,19 @@ export default class Ask extends Workflow {
110
112
  }
111
113
  }
112
114
  if (q) {
113
- this.query({ q });
115
+ this.query({ q, s });
114
116
  }
115
117
  }
116
118
 
117
- query({ q: question, ...payload } = {}) {
119
+ query({ q: question, s: source, ...payload } = {}) {
118
120
  payload = { ...payload, question };
121
+
119
122
  if (this.parentQuestionId) {
120
123
  payload.parent_question_id = this.parentQuestionId;
121
124
  }
125
+ if (source) {
126
+ (payload._meta || (payload._meta = {})).question_source = source;
127
+ }
122
128
 
123
129
  // clear question id from previous session
124
130
  if (this._questionId) {
@@ -129,6 +135,9 @@ export default class Ask extends Workflow {
129
135
  this.restart();
130
136
 
131
137
  const { session } = this;
138
+ if (source) {
139
+ session.meta.question_source = source;
140
+ }
132
141
  this._hub.update(fields.request(), mergeApiOptions(this._options.resolved.api, { payload, session }));
133
142
 
134
143
  return this;
@@ -164,6 +173,7 @@ export default class Ask extends Workflow {
164
173
  if (this._autoQuery && this._autoQuery.updateUrl !== false) {
165
174
  const url = new URL(window.location);
166
175
  const currentQuestion = url.searchParams.get('q');
176
+ // TODO: review this, handle &s=
167
177
  if (question !== currentQuestion && (currentQuestion || this._autoQuery.updateUrl === true)) {
168
178
  url.searchParams.set('q', question);
169
179
  window.history.replaceState({}, '', url);
@@ -241,6 +251,7 @@ export default class Ask extends Workflow {
241
251
  questionId = parentQuestionId;
242
252
  parentQuestionId = this.previous && this.previous.parentQuestionId;
243
253
  custom_context.property = 'suggested_followup_questions';
254
+ // TODO: question_source
244
255
  }
245
256
  payload.context = {
246
257
  ...context,
@@ -52,6 +52,14 @@ export default class Explore extends Workflow {
52
52
  });
53
53
  }
54
54
 
55
+ useApi(options) {
56
+ const { product_id } = options;
57
+ if (product_id) {
58
+ this._productId = product_id;
59
+ }
60
+ return super.useApi(options);
61
+ }
62
+
55
63
  useLink(fn) {
56
64
  if (typeof fn !== 'function' && fn !== false) {
57
65
  throw new Error('useLink(fn) expects fn to be a function or false');
@@ -85,7 +93,7 @@ export default class Explore extends Workflow {
85
93
  return data;
86
94
  }
87
95
  // patch value with links
88
- const related_questions = value.related_questions.map(this._linkFn ? (text => ({ text, url: this._linkFn(text) })) : (text => ({ text })));
96
+ const related_questions = value.related_questions.map(this._linkFn ? (text => ({ text, url: this._getAnswersUrl(text) })) : (text => ({ text })));
89
97
  return {
90
98
  ...data,
91
99
  value: {
@@ -103,8 +111,18 @@ export default class Explore extends Workflow {
103
111
  if (!this._linkFn) {
104
112
  return;
105
113
  }
106
- const url = this._linkFn(q);
114
+ const url = this._getAnswersUrl(q);
107
115
  window.open(url, '_blank');
108
116
  }
109
117
 
118
+ _getAnswersUrl(text) {
119
+ if (!this._linkFn) {
120
+ return;
121
+ }
122
+ const url = this._linkFn(text);
123
+ if (this._productId) {
124
+ return `${url}&s=${encodeURIComponent(this._productId)}`;
125
+ }
126
+ }
127
+
110
128
  }
@@ -106,13 +106,25 @@ export function mergeApiOptions(...optionsList) {
106
106
  }
107
107
  return mergeOptions(optionsList, (merged, options) => Object.assign(merged, {
108
108
  ...options,
109
- payload: {
110
- ...merged.payload,
111
- ...options.payload,
112
- },
109
+ payload: mergeApiPayloads(merged.payload, options.payload),
113
110
  }));
114
111
  }
115
112
 
113
+ function mergeApiPayloads(base, overrides) {
114
+ return {
115
+ ...base,
116
+ ...overrides,
117
+ ...mergeObjectValueIfPresent('_meta', base, overrides),
118
+ };
119
+ }
120
+
121
+ function mergeObjectValueIfPresent(key, base, overrides) {
122
+ const baseValue = base && base[key];
123
+ const overridesValue = overrides && overrides[key];
124
+ const value = baseValue ? (overridesValue ? { ...baseValue, ...overridesValue } : baseValue) : overridesValue;
125
+ return value !== undefined ? { [key]: value } : {};
126
+ }
127
+
116
128
  export function mergeLayoutsOptions(...optionsList) {
117
129
  if (optionsList[optionsList.length - 1] === false) {
118
130
  return false;