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