@athoscommerce/snap-store-mobx 1.2.3-beta.1 → 1.3.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.
Files changed (43) hide show
  1. package/dist/cjs/Autocomplete/AutocompleteStore.js +2 -2
  2. package/dist/cjs/Search/Stores/index.d.ts +1 -1
  3. package/dist/cjs/Search/Stores/index.d.ts.map +1 -1
  4. package/dist/cjs/Search/Stores/index.js +1 -2
  5. package/dist/cjs/index.d.ts +0 -4
  6. package/dist/cjs/index.d.ts.map +1 -1
  7. package/dist/cjs/index.js +1 -5
  8. package/dist/cjs/types.d.ts +2 -13
  9. package/dist/cjs/types.d.ts.map +1 -1
  10. package/dist/esm/Autocomplete/AutocompleteStore.js +1 -1
  11. package/dist/esm/Search/Stores/index.d.ts +1 -1
  12. package/dist/esm/Search/Stores/index.d.ts.map +1 -1
  13. package/dist/esm/Search/Stores/index.js +1 -1
  14. package/dist/esm/index.d.ts +0 -4
  15. package/dist/esm/index.d.ts.map +1 -1
  16. package/dist/esm/index.js +0 -2
  17. package/dist/esm/types.d.ts +2 -13
  18. package/dist/esm/types.d.ts.map +1 -1
  19. package/package.json +5 -5
  20. package/dist/cjs/Chat/ChatStore.d.ts +0 -55
  21. package/dist/cjs/Chat/ChatStore.d.ts.map +0 -1
  22. package/dist/cjs/Chat/ChatStore.js +0 -368
  23. package/dist/cjs/Chat/Stores/ChatAttachmentStore.d.ts +0 -98
  24. package/dist/cjs/Chat/Stores/ChatAttachmentStore.d.ts.map +0 -1
  25. package/dist/cjs/Chat/Stores/ChatAttachmentStore.js +0 -312
  26. package/dist/cjs/Chat/Stores/ChatCompareStore.d.ts +0 -14
  27. package/dist/cjs/Chat/Stores/ChatCompareStore.d.ts.map +0 -1
  28. package/dist/cjs/Chat/Stores/ChatCompareStore.js +0 -63
  29. package/dist/cjs/Chat/Stores/ChatSessionStore.d.ts +0 -103
  30. package/dist/cjs/Chat/Stores/ChatSessionStore.d.ts.map +0 -1
  31. package/dist/cjs/Chat/Stores/ChatSessionStore.js +0 -612
  32. package/dist/esm/Chat/ChatStore.d.ts +0 -55
  33. package/dist/esm/Chat/ChatStore.d.ts.map +0 -1
  34. package/dist/esm/Chat/ChatStore.js +0 -309
  35. package/dist/esm/Chat/Stores/ChatAttachmentStore.d.ts +0 -98
  36. package/dist/esm/Chat/Stores/ChatAttachmentStore.d.ts.map +0 -1
  37. package/dist/esm/Chat/Stores/ChatAttachmentStore.js +0 -220
  38. package/dist/esm/Chat/Stores/ChatCompareStore.d.ts +0 -14
  39. package/dist/esm/Chat/Stores/ChatCompareStore.d.ts.map +0 -1
  40. package/dist/esm/Chat/Stores/ChatCompareStore.js +0 -49
  41. package/dist/esm/Chat/Stores/ChatSessionStore.d.ts +0 -103
  42. package/dist/esm/Chat/Stores/ChatSessionStore.d.ts.map +0 -1
  43. package/dist/esm/Chat/Stores/ChatSessionStore.js +0 -581
@@ -1,612 +0,0 @@
1
- "use strict";
2
- var __assign = (this && this.__assign) || function () {
3
- __assign = Object.assign || function(t) {
4
- for (var s, i = 1, n = arguments.length; i < n; i++) {
5
- s = arguments[i];
6
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7
- t[p] = s[p];
8
- }
9
- return t;
10
- };
11
- return __assign.apply(this, arguments);
12
- };
13
- Object.defineProperty(exports, "__esModule", { value: true });
14
- exports.ChatSessionStore = void 0;
15
- var mobx_1 = require("mobx");
16
- var uuid_1 = require("uuid");
17
- var ChatAttachmentStore_1 = require("../Stores/ChatAttachmentStore");
18
- var ChatCompareStore_1 = require("./ChatCompareStore");
19
- var SearchResultStore_1 = require("../../Search/Stores/SearchResultStore");
20
- function createChatResultStore(results, meta) {
21
- return new SearchResultStore_1.SearchResultStore({
22
- config: {},
23
- state: { loaded: true },
24
- data: {
25
- search: { results: results },
26
- meta: meta,
27
- },
28
- });
29
- }
30
- function createChatProduct(result, meta) {
31
- return new SearchResultStore_1.Product({
32
- config: {},
33
- data: { result: result, meta: meta },
34
- position: 0,
35
- responseId: '',
36
- });
37
- }
38
- /** Extract raw serializable data from a Product instance for storage. */
39
- function serializeProduct(product) {
40
- if (!(product instanceof SearchResultStore_1.Product))
41
- return product;
42
- var raw = {
43
- id: product.id,
44
- mappings: product.mappings,
45
- attributes: product.attributes,
46
- };
47
- if (product.variants) {
48
- raw.variants = {
49
- data: product.variants.data.map(function (v) { return ({
50
- mappings: v.mappings,
51
- attributes: v.attributes,
52
- options: v.options,
53
- badges: v.badges,
54
- }); }),
55
- optionConfig: product.variants.optionConfig,
56
- };
57
- }
58
- return raw;
59
- }
60
- /** Serialize attachments for localStorage. Strips base64 from images (kept only at runtime). */
61
- function serializeAttachmentsForStorage(items) {
62
- return items.map(function (item) {
63
- if (item.type === 'image') {
64
- return {
65
- type: 'image',
66
- id: item.id,
67
- fileName: item.fileName,
68
- imageId: item.imageId,
69
- imageUrl: item.imageUrl,
70
- thumbnailUrl: item.thumbnailUrl,
71
- state: item.state,
72
- error: item.error,
73
- };
74
- }
75
- if (item.type === 'product') {
76
- return {
77
- type: 'product',
78
- id: item.id,
79
- productId: item.productId,
80
- thumbnailUrl: item.thumbnailUrl,
81
- name: item.name,
82
- requestType: item.requestType,
83
- state: item.state,
84
- error: item.error,
85
- };
86
- }
87
- return {
88
- type: 'facet',
89
- id: item.id,
90
- key: item.key,
91
- facetLabel: item.facetLabel,
92
- value: item.value,
93
- label: item.label,
94
- count: item.count,
95
- state: item.state,
96
- error: item.error,
97
- };
98
- });
99
- }
100
- /** Convert a chat message array to a plain serializable form for localStorage. */
101
- function serializeChatForStorage(chat) {
102
- return chat.map(function (message) {
103
- var _a, _b;
104
- switch (message.messageType) {
105
- case 'productSearchResult': {
106
- var msg = message;
107
- return __assign(__assign({}, msg), { results: Array.from(msg.results || []).map(serializeProduct) });
108
- }
109
- case 'inspirationResult': {
110
- var msg = message;
111
- return __assign(__assign({}, msg), { inspirationSections: (_a = msg.inspirationSections) === null || _a === void 0 ? void 0 : _a.map(function (section) { return (__assign(__assign({}, section), { products: Array.from(section.products || []).map(serializeProduct) })); }) });
112
- }
113
- case 'productAnswer': {
114
- var msg = message;
115
- return __assign(__assign({}, msg), { sourceProduct: serializeProduct(msg.sourceProduct) });
116
- }
117
- case 'productComparison': {
118
- var msg = message;
119
- return __assign(__assign({}, msg), { searchResults: Array.from(msg.searchResults || []).map(serializeProduct) });
120
- }
121
- case 'productRecommendation': {
122
- var msg = message;
123
- return __assign(__assign({}, msg), { recommendationResult: (_b = msg.recommendationResult) === null || _b === void 0 ? void 0 : _b.map(function (rec) { return (__assign(__assign({}, rec), { results: Array.from(rec.results || []).map(serializeProduct) })); }) });
124
- }
125
- case 'productQuery': {
126
- var msg = message;
127
- return __assign(__assign({}, msg), { sourceProduct: serializeProduct(msg.sourceProduct) });
128
- }
129
- default:
130
- return message;
131
- }
132
- });
133
- }
134
- var ChatSessionStore = /** @class */ (function () {
135
- function ChatSessionStore(params) {
136
- this.chat = [];
137
- this.actions = [];
138
- this.attachments = new ChatAttachmentStore_1.ChatAttachmentStore();
139
- this.comparisons = new ChatCompareStore_1.ChatCompareStore();
140
- this.feedbacks = [];
141
- this.sessionFeedback = null;
142
- this.feedbackDismissed = false;
143
- this.feedbackJustGiven = false;
144
- this.createdAt = new Date();
145
- this.requestType = '';
146
- this.dismissedSideChatMessageId = null;
147
- this.activeMessageId = null;
148
- this.sessionLimitReached = false;
149
- /** Whether raw stored results have been hydrated into Product/SearchResultStore instances. */
150
- this.hydrated = true;
151
- this.saveTimerId = null;
152
- var _a = params.data || {}, id = _a.id, sessionId = _a.sessionId, chat = _a.chat, attachments = _a.attachments, actions = _a.actions, feedbacks = _a.feedbacks, sessionFeedback = _a.sessionFeedback, feedbackDismissed = _a.feedbackDismissed, createdAt = _a.createdAt, committedComparisons = _a.committedComparisons;
153
- var stores = params.stores;
154
- this.id = id || (0, uuid_1.v4)();
155
- this.sessionId = sessionId;
156
- this.storage = stores.storage;
157
- this.actions = actions || [];
158
- this.createdAt = createdAt ? new Date(createdAt) : new Date();
159
- this.feedbacks = feedbacks || [];
160
- this.sessionFeedback = sessionFeedback || null;
161
- this.feedbackDismissed = feedbackDismissed || false;
162
- // if chat and attachments are passed, load them
163
- if (chat && chat.length > 0) {
164
- // productQuery messages only exist to drive the side-chat panel for an
165
- // in-flight discussProduct click; they must not be rehydrated on reload
166
- // or the side chat would re-open without the matching primary-chat state
167
- this.chat = chat.filter(function (message) { return message.messageType !== 'productQuery'; });
168
- }
169
- if (attachments && attachments.length > 0) {
170
- this.attachments.hydrate(attachments);
171
- // Any attachment already referenced by a sent user message is no longer
172
- // pending — transition it from 'active' to 'saved' so it stops appearing
173
- // in the attachment context bar while remaining available via get(id) for
174
- // rendering inside historical user messages.
175
- var usedAttachmentIds_1 = new Set();
176
- this.chat.forEach(function (msg) {
177
- if (msg.messageType === 'user' && msg.attachments) {
178
- msg.attachments.forEach(function (id) { return usedAttachmentIds_1.add(id); });
179
- }
180
- });
181
- this.attachments.items.forEach(function (item) {
182
- if ((item.state === 'active' || item.state === 'attached') && usedAttachmentIds_1.has(item.id)) {
183
- item.save();
184
- }
185
- });
186
- }
187
- // restore committed comparisons only if the thread is still anchored
188
- // to a product comparison — either the last response was a
189
- // productComparison or the user sent a follow-up before a response
190
- // arrived (pending state)
191
- if (committedComparisons && committedComparisons.length > 0) {
192
- var EXCLUDED_MESSAGE_TYPES_1 = ['topicDrift'];
193
- var visibleMessages = this.chat.filter(function (message) { return !EXCLUDED_MESSAGE_TYPES_1.includes(message.messageType); });
194
- var lastMessage = visibleMessages[visibleMessages.length - 1];
195
- if ((lastMessage === null || lastMessage === void 0 ? void 0 : lastMessage.messageType) === 'productComparison' || (lastMessage === null || lastMessage === void 0 ? void 0 : lastMessage.messageType) === 'user') {
196
- this.comparisons.committedItems = committedComparisons;
197
- }
198
- }
199
- (0, mobx_1.makeObservable)(this, {
200
- chat: mobx_1.observable,
201
- requestType: mobx_1.observable,
202
- actions: mobx_1.observable,
203
- attachments: mobx_1.observable,
204
- feedbacks: mobx_1.observable,
205
- sessionFeedback: mobx_1.observable,
206
- feedbackDismissed: mobx_1.observable,
207
- feedbackJustGiven: mobx_1.observable,
208
- dismissedSideChatMessageId: mobx_1.observable,
209
- activeMessageId: mobx_1.observable,
210
- sessionLimitReached: mobx_1.observable,
211
- activeMessage: mobx_1.computed,
212
- });
213
- }
214
- ChatSessionStore.prototype.dismissSideChat = function () {
215
- // clear the override first so the fallback (last eligible message) is what we
216
- // dismiss — otherwise closing while viewing an older message would leave the
217
- // last message undismissed and the side chat would auto-reopen on it
218
- this.activeMessageId = null;
219
- var fallback = this.activeMessage;
220
- if (fallback) {
221
- this.dismissedSideChatMessageId = fallback.id;
222
- }
223
- };
224
- ChatSessionStore.prototype.setActiveMessage = function (id) {
225
- this.activeMessageId = id;
226
- this.dismissedSideChatMessageId = null;
227
- };
228
- ChatSessionStore.prototype.pushProductQueryMessage = function (result) {
229
- var _a, _b;
230
- // capture the side-chat message that was active at click time so a back action
231
- // can restore it even when it's not the last message in the chat
232
- var sourceMessageId = (_a = this.activeMessage) === null || _a === void 0 ? void 0 : _a.id;
233
- // drop any trailing productQuery so a fresh discussProduct click replaces
234
- // the side-chat target rather than stacking up
235
- while (this.chat.length > 0 && ((_b = this.chat[this.chat.length - 1]) === null || _b === void 0 ? void 0 : _b.messageType) === 'productQuery') {
236
- this.chat.pop();
237
- }
238
- this.chat.push({
239
- id: (0, uuid_1.v4)(),
240
- messageType: 'productQuery',
241
- sourceProduct: result,
242
- sourceMessageId: sourceMessageId,
243
- });
244
- // re-show the side chat in case the user previously dismissed it
245
- this.dismissedSideChatMessageId = null;
246
- this.activeMessageId = null;
247
- this.save();
248
- };
249
- ChatSessionStore.prototype.popProductQueryMessage = function (restoreActiveMessageId) {
250
- var _a;
251
- while (this.chat.length > 0 && ((_a = this.chat[this.chat.length - 1]) === null || _a === void 0 ? void 0 : _a.messageType) === 'productQuery') {
252
- this.chat.pop();
253
- }
254
- this.activeMessageId = restoreActiveMessageId || null;
255
- this.save();
256
- };
257
- Object.defineProperty(ChatSessionStore.prototype, "isExpired", {
258
- get: function () {
259
- var ONE_DAY = 24 * 60 * 60 * 1000;
260
- var now = new Date();
261
- var diff = now.getTime() - this.createdAt.getTime();
262
- return diff > ONE_DAY;
263
- },
264
- enumerable: false,
265
- configurable: true
266
- });
267
- Object.defineProperty(ChatSessionStore.prototype, "topicDrift", {
268
- get: function () {
269
- var lastMessage = this.chat[this.chat.length - 1];
270
- return (lastMessage === null || lastMessage === void 0 ? void 0 : lastMessage.messageType) === 'topicDrift' ? lastMessage : null;
271
- },
272
- enumerable: false,
273
- configurable: true
274
- });
275
- Object.defineProperty(ChatSessionStore.prototype, "activeMessage", {
276
- get: function () {
277
- var EXCLUDED_MESSAGE_TYPES = ['topicDrift', 'productAnswer'];
278
- if (this.activeMessageId) {
279
- // Walk backward — the override is usually near the end
280
- for (var i = this.chat.length - 1; i >= 0; i--) {
281
- var m = this.chat[i];
282
- if (m.id === this.activeMessageId && !EXCLUDED_MESSAGE_TYPES.includes(m.messageType)) {
283
- return m;
284
- }
285
- }
286
- }
287
- // Find the last eligible message by iterating backwards
288
- var lastMessage = null;
289
- for (var i = this.chat.length - 1; i >= 0; i--) {
290
- if (!EXCLUDED_MESSAGE_TYPES.includes(this.chat[i].messageType)) {
291
- lastMessage = this.chat[i];
292
- break;
293
- }
294
- }
295
- // When the user sends a follow-up while in a productQuery flow (e.g. "discuss product"),
296
- // the last visible message becomes a 'user' message which would close the secondary panel.
297
- // Instead, keep the productQuery message as the active side-chat target so the product
298
- // information panel stays open during and after the request.
299
- if ((lastMessage === null || lastMessage === void 0 ? void 0 : lastMessage.messageType) === 'user' && this.requestType === 'productQuery') {
300
- for (var i = this.chat.length - 1; i >= 0; i--) {
301
- var m = this.chat[i];
302
- if (m.messageType === 'productQuery' && !EXCLUDED_MESSAGE_TYPES.includes(m.messageType)) {
303
- return m;
304
- }
305
- }
306
- }
307
- return lastMessage || null;
308
- },
309
- enumerable: false,
310
- configurable: true
311
- });
312
- ChatSessionStore.prototype.dismissTopicDrift = function () {
313
- this.chat = this.chat.filter(function (m) { return m.messageType !== 'topicDrift'; });
314
- this.save();
315
- };
316
- ChatSessionStore.prototype.handleTopicDrift = function () {
317
- var lastUserMessage;
318
- for (var i = this.chat.length - 1; i >= 0; i--) {
319
- if (this.chat[i].messageType === 'user') {
320
- lastUserMessage = this.chat[i];
321
- break;
322
- }
323
- }
324
- var messageText = lastUserMessage === null || lastUserMessage === void 0 ? void 0 : lastUserMessage.text;
325
- // remove all topicDrift messages and the last user message that triggered the drift
326
- if (lastUserMessage) {
327
- var lastUserIndex = this.chat.lastIndexOf(lastUserMessage);
328
- this.chat = this.chat.slice(0, lastUserIndex);
329
- }
330
- else {
331
- this.chat = this.chat.filter(function (m) { return m.messageType !== 'topicDrift'; });
332
- }
333
- this.save();
334
- return messageText;
335
- };
336
- ChatSessionStore.prototype.reset = function () {
337
- this.attachments.reset();
338
- this.chat = [];
339
- this.actions = [];
340
- this.feedbacks = [];
341
- this.sessionFeedback = null;
342
- };
343
- /** Persist the session to storage immediately (synchronous). */
344
- ChatSessionStore.prototype.saveImmediate = function () {
345
- if (this.saveTimerId !== null) {
346
- clearTimeout(this.saveTimerId);
347
- this.saveTimerId = null;
348
- }
349
- this.storage.set("chats.".concat(this.id), {
350
- sessionId: this.sessionId,
351
- chat: serializeChatForStorage(this.chat),
352
- attachments: serializeAttachmentsForStorage(this.attachments.items),
353
- actions: this.actions,
354
- feedbacks: this.feedbacks,
355
- sessionFeedback: this.sessionFeedback,
356
- feedbackDismissed: this.feedbackDismissed,
357
- createdAt: this.createdAt,
358
- committedComparisons: this.comparisons.committedItems,
359
- });
360
- };
361
- /**
362
- * Schedule a save — multiple calls within the debounce window are coalesced
363
- * into a single localStorage write.
364
- */
365
- ChatSessionStore.prototype.save = function () {
366
- var _this = this;
367
- if (this.saveTimerId !== null) {
368
- clearTimeout(this.saveTimerId);
369
- }
370
- this.saveTimerId = setTimeout(function () {
371
- _this.saveTimerId = null;
372
- _this.saveImmediate();
373
- }, 0);
374
- };
375
- /** Remove oldest stored sessions when exceeding the limit. */
376
- ChatSessionStore.pruneStoredSessions = function (storage, maxSessions) {
377
- if (maxSessions === void 0) { maxSessions = 10; }
378
- var storedChats = storage.get('chats');
379
- if (storedChats) {
380
- var chatIds = Object.keys(storedChats);
381
- if (chatIds.length > maxSessions) {
382
- chatIds
383
- .sort(function (a, b) {
384
- var _a, _b;
385
- var aTime = new Date(((_a = storedChats[a]) === null || _a === void 0 ? void 0 : _a.createdAt) || 0).getTime();
386
- var bTime = new Date(((_b = storedChats[b]) === null || _b === void 0 ? void 0 : _b.createdAt) || 0).getTime();
387
- return aTime - bTime;
388
- })
389
- .slice(0, chatIds.length - maxSessions)
390
- .forEach(function (id) {
391
- storage.set("chats.".concat(id), null);
392
- });
393
- }
394
- }
395
- };
396
- /** Re-wrap raw stored results as Product / SearchResultStore instances. */
397
- ChatSessionStore.prototype.hydrateResults = function (meta) {
398
- this.chat.forEach(function (message) {
399
- var _a, _b, _c, _d;
400
- if (message.messageType === 'productSearchResult') {
401
- var msg = message;
402
- if (((_a = msg.results) === null || _a === void 0 ? void 0 : _a.length) && !(msg.results[0] instanceof SearchResultStore_1.Product)) {
403
- msg.results = createChatResultStore(msg.results, meta);
404
- }
405
- }
406
- else if (message.messageType === 'inspirationResult') {
407
- var msg = message;
408
- (_b = msg.inspirationSections) === null || _b === void 0 ? void 0 : _b.forEach(function (section) {
409
- var _a;
410
- if (((_a = section.products) === null || _a === void 0 ? void 0 : _a.length) && !(section.products[0] instanceof SearchResultStore_1.Product)) {
411
- section.products = createChatResultStore(section.products, meta);
412
- }
413
- });
414
- }
415
- else if (message.messageType === 'productAnswer') {
416
- var msg = message;
417
- if (msg.sourceProduct && !(msg.sourceProduct instanceof SearchResultStore_1.Product)) {
418
- msg.sourceProduct = createChatProduct(msg.sourceProduct, meta);
419
- }
420
- }
421
- else if (message.messageType === 'productComparison') {
422
- var msg = message;
423
- if (((_c = msg.searchResults) === null || _c === void 0 ? void 0 : _c.length) && !(msg.searchResults[0] instanceof SearchResultStore_1.Product)) {
424
- msg.searchResults = createChatResultStore(msg.searchResults, meta);
425
- }
426
- }
427
- else if (message.messageType === 'productRecommendation') {
428
- var msg = message;
429
- (_d = msg.recommendationResult) === null || _d === void 0 ? void 0 : _d.forEach(function (rec) {
430
- var _a;
431
- if (((_a = rec.results) === null || _a === void 0 ? void 0 : _a.length) && !(rec.results[0] instanceof SearchResultStore_1.Product)) {
432
- rec.results = createChatResultStore(rec.results, meta);
433
- }
434
- });
435
- }
436
- });
437
- };
438
- ChatSessionStore.prototype.request = function (request) {
439
- var _this = this;
440
- var _a, _b;
441
- // clear the questions on new request
442
- this.actions = [];
443
- this.requestType = request.data.requestType;
444
- this.activeMessageId = null;
445
- // remove any attachments that failed to upload
446
- var errorAttachments = this.attachments.items.filter(function (item) { return item.state === 'error'; });
447
- errorAttachments.forEach(function (item) { return _this.attachments.items.splice(_this.attachments.items.indexOf(item), 1); });
448
- var attachments = [];
449
- if (request.data.requestType === 'productSearch') {
450
- var searchFilters = request.data.searchFilters;
451
- if (searchFilters && searchFilters.length > 0) {
452
- var filterTextArray_1 = [];
453
- searchFilters.forEach(function (filter) {
454
- var attachedFacets = _this.attachments.attached.filter(function (item) { return item.type == 'facet' && item.key == filter.key; });
455
- attachedFacets.forEach(function (attachedFacet) {
456
- attachments.push(attachedFacet.id);
457
- attachedFacet.activate();
458
- filterTextArray_1.push("".concat(attachedFacet.facetLabel, " ").concat(attachedFacet.label));
459
- });
460
- });
461
- this.chat.push({
462
- id: (0, uuid_1.v4)(),
463
- messageType: 'user',
464
- attachments: attachments.length > 0 ? attachments : undefined,
465
- text: "Filter by ".concat(filterTextArray_1.join(' and ')),
466
- requestType: request.data.requestType,
467
- });
468
- }
469
- }
470
- else if ('message' in request.data && request.data.message) {
471
- if (request.data.requestType === 'imageSearch') {
472
- var imageId_1 = request.data.attachedImageId;
473
- var attachedImage = this.attachments.attached.find(function (item) { return item.type == 'image' && item.imageId == imageId_1; });
474
- if (attachedImage) {
475
- attachments.push(attachedImage.id);
476
- attachedImage.activate();
477
- }
478
- }
479
- else if (request.data.requestType === 'productQuery') {
480
- var productId_1 = request.data.productId;
481
- var attachedProduct = this.attachments.attached.find(function (item) { return item.type == 'product' && item.productId == productId_1; });
482
- if (attachedProduct) {
483
- attachments.push(attachedProduct.id);
484
- attachedProduct.activate();
485
- }
486
- }
487
- else if (request.data.requestType === 'productComparison') {
488
- this.comparisons.compared.forEach(function (item) {
489
- var _a, _b, _c, _d, _e, _f, _g;
490
- var d = ((_a = item.result) === null || _a === void 0 ? void 0 : _a.display) || item.result;
491
- var attachment = _this.attachments.add({
492
- type: 'product',
493
- requestType: 'productComparison',
494
- productId: item.result.id,
495
- name: (_c = (_b = d.mappings) === null || _b === void 0 ? void 0 : _b.core) === null || _c === void 0 ? void 0 : _c.name,
496
- thumbnailUrl: ((_e = (_d = d.mappings) === null || _d === void 0 ? void 0 : _d.core) === null || _e === void 0 ? void 0 : _e.thumbnailImageUrl) || ((_g = (_f = d.mappings) === null || _f === void 0 ? void 0 : _f.core) === null || _g === void 0 ? void 0 : _g.imageUrl),
497
- });
498
- if (attachment) {
499
- attachments.push(attachment.id);
500
- attachment.activate();
501
- }
502
- });
503
- }
504
- this.chat.push({
505
- id: (0, uuid_1.v4)(),
506
- messageType: 'user',
507
- attachments: attachments.length > 0 ? attachments : undefined,
508
- text: request.data.message,
509
- requestType: request.data.requestType,
510
- });
511
- }
512
- else if (((_a = request.data) === null || _a === void 0 ? void 0 : _a.requestType) === 'productSimilar') {
513
- var attachedSimilarProduct = this.attachments.attached.find(function (item) { return item.type == 'product' && item.requestType == 'productSimilar'; });
514
- if (attachedSimilarProduct) {
515
- attachments.push(attachedSimilarProduct.id);
516
- attachedSimilarProduct.activate();
517
- this.chat.push({
518
- id: (0, uuid_1.v4)(),
519
- messageType: 'user',
520
- attachments: attachments.length > 0 ? attachments : undefined,
521
- text: "Show similar products to \"".concat(attachedSimilarProduct.name || attachedSimilarProduct.productId, "\""),
522
- requestType: request.data.requestType,
523
- });
524
- }
525
- }
526
- else if (((_b = request.data) === null || _b === void 0 ? void 0 : _b.requestType) === 'productComparison') {
527
- var productNames_1 = [];
528
- this.comparisons.compared.forEach(function (item) {
529
- var _a, _b, _c, _d, _e, _f, _g;
530
- var d = ((_a = item.result) === null || _a === void 0 ? void 0 : _a.display) || item.result;
531
- var attachment = _this.attachments.add({
532
- type: 'product',
533
- requestType: 'productComparison',
534
- productId: item.result.id,
535
- name: (_c = (_b = d.mappings) === null || _b === void 0 ? void 0 : _b.core) === null || _c === void 0 ? void 0 : _c.name,
536
- thumbnailUrl: ((_e = (_d = d.mappings) === null || _d === void 0 ? void 0 : _d.core) === null || _e === void 0 ? void 0 : _e.thumbnailImageUrl) || ((_g = (_f = d.mappings) === null || _f === void 0 ? void 0 : _f.core) === null || _g === void 0 ? void 0 : _g.imageUrl),
537
- });
538
- if (attachment) {
539
- attachments.push(attachment.id);
540
- attachment.activate();
541
- productNames_1.push(attachment.name || attachment.productId);
542
- }
543
- });
544
- if (attachments.length > 0) {
545
- this.chat.push({
546
- id: (0, uuid_1.v4)(),
547
- messageType: 'user',
548
- attachments: attachments,
549
- text: "Compare ".concat(productNames_1.map(function (name) { return "\"".concat(name, "\""); }).join(' and ')),
550
- requestType: request.data.requestType,
551
- });
552
- }
553
- }
554
- // snapshot the comparison list into the committed list so the
555
- // header section can clear and the footer can display them
556
- if (request.data.requestType === 'productComparison') {
557
- this.comparisons.commit();
558
- }
559
- this.save();
560
- };
561
- ChatSessionStore.prototype.update = function (data) {
562
- var _this = this;
563
- this.sessionId = data.chat.context.sessionId;
564
- var meta = data.meta;
565
- data.chat.data.forEach(function (messageData) {
566
- var _a, _b, _c, _d, _e;
567
- // check if the data has questions?
568
- if (messageData.messageType === 'actions') {
569
- _this.actions.push({
570
- type: 'actions',
571
- data: messageData.actions,
572
- });
573
- return;
574
- }
575
- if (messageData.messageType === 'productSearchResult' && ((_a = messageData.facets) === null || _a === void 0 ? void 0 : _a.length) > 0) {
576
- _this.actions.push({
577
- type: 'facets',
578
- data: messageData.facets,
579
- });
580
- }
581
- // convert raw results to Product instances (via SearchResultStore) so
582
- // display components can use result.display for mask-aware rendering
583
- if (messageData.messageType === 'productSearchResult' && ((_b = messageData.results) === null || _b === void 0 ? void 0 : _b.length)) {
584
- messageData.results = createChatResultStore(messageData.results, meta);
585
- }
586
- else if (messageData.messageType === 'inspirationResult' && ((_c = messageData.inspirationSections) === null || _c === void 0 ? void 0 : _c.length)) {
587
- messageData.inspirationSections = messageData.inspirationSections.map(function (section) {
588
- var _a;
589
- return (__assign(__assign({}, section), { products: ((_a = section.products) === null || _a === void 0 ? void 0 : _a.length)
590
- ? createChatResultStore(section.products, meta)
591
- : section.products }));
592
- });
593
- }
594
- else if (messageData.messageType === 'productAnswer' && messageData.sourceProduct) {
595
- messageData.sourceProduct = createChatProduct(messageData.sourceProduct, meta);
596
- }
597
- else if (messageData.messageType === 'productComparison' && ((_d = messageData.searchResults) === null || _d === void 0 ? void 0 : _d.length)) {
598
- messageData.searchResults = createChatResultStore(messageData.searchResults, meta);
599
- }
600
- else if (messageData.messageType === 'productRecommendation' && ((_e = messageData.recommendationResult) === null || _e === void 0 ? void 0 : _e.length)) {
601
- messageData.recommendationResult = messageData.recommendationResult.map(function (rec) {
602
- var _a;
603
- return (__assign(__assign({}, rec), { results: ((_a = rec.results) === null || _a === void 0 ? void 0 : _a.length) ? createChatResultStore(rec.results, meta) : rec.results }));
604
- });
605
- }
606
- _this.chat.push(messageData);
607
- });
608
- this.save();
609
- };
610
- return ChatSessionStore;
611
- }());
612
- exports.ChatSessionStore = ChatSessionStore;
@@ -1,55 +0,0 @@
1
- import { ChatStoreConfig } from '../types';
2
- import { MetaStore } from '../Meta/MetaStore';
3
- import type { MetaResponseModel } from '@athoscommerce/snapi-types';
4
- import { AbstractStore } from '../Abstract/AbstractStore';
5
- import type { ChatResponseModel, ChatRequestModel, ProductsResponseModel } from '@athoscommerce/snap-client';
6
- import { StorageStore } from '../Storage/StorageStore';
7
- import { ChatSessionStore } from './Stores/ChatSessionStore';
8
- import { ChatStatusResponse } from '@athoscommerce/snap-client';
9
- import { Product } from '../Search/Stores/SearchResultStore';
10
- export declare class ChatStore extends AbstractStore<ChatStoreConfig> {
11
- meta?: MetaStore;
12
- inputValue: string;
13
- open: boolean;
14
- storage: StorageStore;
15
- chats: ChatSessionStore[];
16
- currentChatId: string;
17
- chatEnabled: boolean | null;
18
- initChatLoading: boolean;
19
- suggestedQuestions: string[];
20
- welcomeMessage: string;
21
- features: ChatStatusResponse['features'];
22
- productQuickview: Product | null;
23
- productQuickviewError: string | null;
24
- /** Raw meta kept for lazy hydration of inactive chat sessions. */
25
- private storedMetaData;
26
- constructor(config: ChatStoreConfig);
27
- get currentChat(): ChatSessionStore | undefined;
28
- get chatsIds(): string[];
29
- get blocked(): boolean;
30
- handleChatStatusResponse(response: ChatStatusResponse): boolean;
31
- reset(): void;
32
- setProductQuickview(product: Product): void;
33
- updateProductQuickview(response: ProductsResponseModel): void;
34
- setProductQuickviewError(message: string): void;
35
- clearProductQuickview(): void;
36
- clearHistory(): void;
37
- createChat(data?: {
38
- sessionId: string;
39
- }): ChatSessionStore;
40
- switchChat(id: string): void;
41
- sendProductQuery(result: any, options: {
42
- requestType: 'productQuery' | 'productSimilar' | 'productComparison';
43
- }): void;
44
- compareProduct(result: any): void;
45
- addFacet(facet: any): void;
46
- removeFacet(key: string, value: string): void;
47
- clearPendingFacets(): void;
48
- isFacetSelected(key: string, value: string): boolean;
49
- request(request: ChatRequestModel): void;
50
- update(data: {
51
- chat: ChatResponseModel;
52
- meta: MetaResponseModel;
53
- }): void;
54
- }
55
- //# sourceMappingURL=ChatStore.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"ChatStore.d.ts","sourceRoot":"","sources":["../../../src/Chat/ChatStore.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,eAAe,EAAE,M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