@athoscommerce/snap-store-mobx 1.2.2 → 1.2.3-beta.1

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 (41) hide show
  1. package/dist/cjs/Chat/ChatStore.d.ts +55 -0
  2. package/dist/cjs/Chat/ChatStore.d.ts.map +1 -0
  3. package/dist/cjs/Chat/ChatStore.js +368 -0
  4. package/dist/cjs/Chat/Stores/ChatAttachmentStore.d.ts +98 -0
  5. package/dist/cjs/Chat/Stores/ChatAttachmentStore.d.ts.map +1 -0
  6. package/dist/cjs/Chat/Stores/ChatAttachmentStore.js +312 -0
  7. package/dist/cjs/Chat/Stores/ChatCompareStore.d.ts +14 -0
  8. package/dist/cjs/Chat/Stores/ChatCompareStore.d.ts.map +1 -0
  9. package/dist/cjs/Chat/Stores/ChatCompareStore.js +63 -0
  10. package/dist/cjs/Chat/Stores/ChatSessionStore.d.ts +103 -0
  11. package/dist/cjs/Chat/Stores/ChatSessionStore.d.ts.map +1 -0
  12. package/dist/cjs/Chat/Stores/ChatSessionStore.js +612 -0
  13. package/dist/cjs/Search/Stores/index.d.ts +1 -1
  14. package/dist/cjs/Search/Stores/index.d.ts.map +1 -1
  15. package/dist/cjs/Search/Stores/index.js +2 -1
  16. package/dist/cjs/index.d.ts +4 -0
  17. package/dist/cjs/index.d.ts.map +1 -1
  18. package/dist/cjs/index.js +5 -1
  19. package/dist/cjs/types.d.ts +13 -2
  20. package/dist/cjs/types.d.ts.map +1 -1
  21. package/dist/esm/Chat/ChatStore.d.ts +55 -0
  22. package/dist/esm/Chat/ChatStore.d.ts.map +1 -0
  23. package/dist/esm/Chat/ChatStore.js +309 -0
  24. package/dist/esm/Chat/Stores/ChatAttachmentStore.d.ts +98 -0
  25. package/dist/esm/Chat/Stores/ChatAttachmentStore.d.ts.map +1 -0
  26. package/dist/esm/Chat/Stores/ChatAttachmentStore.js +220 -0
  27. package/dist/esm/Chat/Stores/ChatCompareStore.d.ts +14 -0
  28. package/dist/esm/Chat/Stores/ChatCompareStore.d.ts.map +1 -0
  29. package/dist/esm/Chat/Stores/ChatCompareStore.js +49 -0
  30. package/dist/esm/Chat/Stores/ChatSessionStore.d.ts +103 -0
  31. package/dist/esm/Chat/Stores/ChatSessionStore.d.ts.map +1 -0
  32. package/dist/esm/Chat/Stores/ChatSessionStore.js +581 -0
  33. package/dist/esm/Search/Stores/index.d.ts +1 -1
  34. package/dist/esm/Search/Stores/index.d.ts.map +1 -1
  35. package/dist/esm/Search/Stores/index.js +1 -1
  36. package/dist/esm/index.d.ts +4 -0
  37. package/dist/esm/index.d.ts.map +1 -1
  38. package/dist/esm/index.js +2 -0
  39. package/dist/esm/types.d.ts +13 -2
  40. package/dist/esm/types.d.ts.map +1 -1
  41. package/package.json +5 -5
@@ -0,0 +1,55 @@
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
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ChatStore.d.ts","sourceRoot":"","sources":["../../../src/Chat/ChatStore.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,eAAe,EAAE,MAAM,UAAU,CAAC;AAC3C,OAAO,EAAE,SAAS,EAAE,MAAM,mBAAmB,CAAC;AAC9C,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,4BAA4B,CAAC;AACpE,OAAO,EAAE,aAAa,EAAE,MAAM,2BAA2B,CAAC;AAC1D,OAAO,KAAK,EAAE,iBAAiB,EAAE,gBAAgB,EAAE,qBAAqB,EAAE,MAAM,4BAA4B,CAAC;AAC7G,OAAO,EAAE,YAAY,EAAE,MAAM,yBAAyB,CAAC;AACvD,OAAO,EAAE,gBAAgB,EAAE,MAAM,2BAA2B,CAAC;AAE7D,OAAO,EAAE,kBAAkB,EAAE,MAAM,4BAA4B,CAAC;AAChE,OAAO,EAAE,OAAO,EAAY,MAAM,oCAAoC,CAAC;AAIvE,qBAAa,SAAU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Ac,GAAG,gBAAgB,GAAG,mBAAmB,CAAA;KAAE,GAAG,IAAI;IAkBtH,cAAc,CAAC,MAAM,EAAE,GAAG,GAAG,IAAI;IAMjC,QAAQ,CAAC,KAAK,EAAE,GAAG,GAAG,IAAI;IAW1B,WAAW,CAAC,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,IAAI;IAgB7C,kBAAkB,IAAI,IAAI;IAW1B,eAAe,CAAC,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,OAAO;IAYpD,OAAO,CAAC,OAAO,EAAE,gBAAgB,GAAG,IAAI;IAmBxC,MAAM,CAAC,IAAI,EAAE;QAAE,IAAI,EAAE,iBAAiB,CAAC;QAAC,IAAI,EAAE,iBAAiB,CAAA;KAAE,GAAG,IAAI;CAkB/E"}
@@ -0,0 +1,368 @@
1
+ "use strict";
2
+ var __extends = (this && this.__extends) || (function () {
3
+ var extendStatics = function (d, b) {
4
+ extendStatics = Object.setPrototypeOf ||
5
+ ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
6
+ function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
7
+ return extendStatics(d, b);
8
+ };
9
+ return function (d, b) {
10
+ if (typeof b !== "function" && b !== null)
11
+ throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
12
+ extendStatics(d, b);
13
+ function __() { this.constructor = d; }
14
+ d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
15
+ };
16
+ })();
17
+ var __assign = (this && this.__assign) || function () {
18
+ __assign = Object.assign || function(t) {
19
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
20
+ s = arguments[i];
21
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
22
+ t[p] = s[p];
23
+ }
24
+ return t;
25
+ };
26
+ return __assign.apply(this, arguments);
27
+ };
28
+ Object.defineProperty(exports, "__esModule", { value: true });
29
+ exports.ChatStore = void 0;
30
+ var mobx_1 = require("mobx");
31
+ var MetaStore_1 = require("../Meta/MetaStore");
32
+ var AbstractStore_1 = require("../Abstract/AbstractStore");
33
+ var StorageStore_1 = require("../Storage/StorageStore");
34
+ var ChatSessionStore_1 = require("./Stores/ChatSessionStore");
35
+ var SearchResultStore_1 = require("../Search/Stores/SearchResultStore");
36
+ var CHAT_STATUS_EXPIRATION_TIME = 1000 * 60 * 60 * 12; // 12 hours
37
+ var ChatStore = /** @class */ (function (_super) {
38
+ __extends(ChatStore, _super);
39
+ function ChatStore(config) {
40
+ var _this = _super.call(this, config) || this;
41
+ _this.meta = undefined;
42
+ _this.inputValue = '';
43
+ _this.open = false;
44
+ _this.chats = [];
45
+ _this.chatEnabled = null;
46
+ _this.initChatLoading = false;
47
+ _this.suggestedQuestions = [];
48
+ _this.welcomeMessage = '';
49
+ _this.features = { imageSearch: { enabled: false }, similarProducts: { enabled: false } };
50
+ _this.productQuickview = null;
51
+ _this.productQuickviewError = null;
52
+ /** Raw meta kept for lazy hydration of inactive chat sessions. */
53
+ _this.storedMetaData = null;
54
+ var legacyKey = "ss-".concat(_this.config.id);
55
+ var storageKey = _this.config.siteId ? "ss-".concat(_this.config.siteId, "-").concat(_this.config.id) : legacyKey;
56
+ _this.storage = new StorageStore_1.StorageStore({
57
+ type: 'local',
58
+ key: storageKey,
59
+ });
60
+ var storedChatStatusResponse = _this.storage.get('chatStatusResponse');
61
+ if (storedChatStatusResponse) {
62
+ try {
63
+ var storedChatStatus = JSON.parse(storedChatStatusResponse);
64
+ if (storedChatStatus.checkTime && Date.now() - storedChatStatus.checkTime > CHAT_STATUS_EXPIRATION_TIME) {
65
+ // chat status is expired, remove from storage to trigger a new check
66
+ _this.storage.set('chatStatusResponse', null);
67
+ }
68
+ else {
69
+ _this.handleChatStatusResponse(storedChatStatus.response);
70
+ }
71
+ }
72
+ catch (_a) {
73
+ _this.storage.set('chatStatusResponse', null);
74
+ }
75
+ }
76
+ // check for entries in storage
77
+ var latestChatId = '';
78
+ var storedChats = _this.storage.get('chats');
79
+ storedChats &&
80
+ Object.keys(storedChats || {}).forEach(function (chatId) {
81
+ var chatData = storedChats[chatId];
82
+ if (chatData) {
83
+ var restoredChat = new ChatSessionStore_1.ChatSessionStore({
84
+ data: __assign(__assign({}, chatData), { id: chatId }),
85
+ stores: {
86
+ storage: _this.storage,
87
+ },
88
+ });
89
+ // Mark as unhydrated — results are still raw JSON from storage.
90
+ // Only the active session will be hydrated below.
91
+ restoredChat.hydrated = false;
92
+ _this.chats.push(restoredChat);
93
+ latestChatId = chatId;
94
+ }
95
+ });
96
+ var storedMeta = _this.storage.get('meta');
97
+ if (storedMeta) {
98
+ try {
99
+ var metaData = JSON.parse(storedMeta);
100
+ _this.meta = new MetaStore_1.MetaStore({
101
+ data: {
102
+ meta: metaData,
103
+ },
104
+ });
105
+ // Keep raw meta for lazy hydration of inactive sessions
106
+ _this.storedMetaData = metaData;
107
+ // Only hydrate the active chat session — inactive sessions will be
108
+ // hydrated lazily when switched to via switchChat()
109
+ var activeChat = _this.chats.find(function (chat) { return chat.id === latestChatId; });
110
+ if (activeChat) {
111
+ activeChat.hydrateResults(metaData);
112
+ activeChat.hydrated = true;
113
+ }
114
+ }
115
+ catch (_b) {
116
+ _this.storage.set('meta', null);
117
+ }
118
+ }
119
+ _this.currentChatId = latestChatId;
120
+ (0, mobx_1.makeObservable)(_this, {
121
+ meta: mobx_1.observable,
122
+ inputValue: mobx_1.observable,
123
+ open: mobx_1.observable,
124
+ chats: mobx_1.observable,
125
+ currentChatId: mobx_1.observable,
126
+ chatEnabled: mobx_1.observable,
127
+ initChatLoading: mobx_1.observable,
128
+ blocked: mobx_1.computed,
129
+ currentChat: mobx_1.computed,
130
+ chatsIds: mobx_1.computed,
131
+ features: mobx_1.observable,
132
+ suggestedQuestions: mobx_1.observable,
133
+ welcomeMessage: mobx_1.observable,
134
+ productQuickview: mobx_1.observable,
135
+ productQuickviewError: mobx_1.observable,
136
+ });
137
+ return _this;
138
+ }
139
+ Object.defineProperty(ChatStore.prototype, "currentChat", {
140
+ get: function () {
141
+ var _this = this;
142
+ return this.chats.find(function (chat) { return chat.id === _this.currentChatId; });
143
+ },
144
+ enumerable: false,
145
+ configurable: true
146
+ });
147
+ Object.defineProperty(ChatStore.prototype, "chatsIds", {
148
+ get: function () {
149
+ return this.chats.map(function (chat) { return chat.id; });
150
+ },
151
+ enumerable: false,
152
+ configurable: true
153
+ });
154
+ Object.defineProperty(ChatStore.prototype, "blocked", {
155
+ get: function () {
156
+ var _a;
157
+ var isBlocked = false;
158
+ var blockedAttachments = (_a = this.currentChat) === null || _a === void 0 ? void 0 : _a.attachments.items.some(function (item) { return item.type === 'image' && item.state === 'loading'; });
159
+ if (this.loading || blockedAttachments) {
160
+ isBlocked = true;
161
+ }
162
+ return isBlocked;
163
+ },
164
+ enumerable: false,
165
+ configurable: true
166
+ });
167
+ ChatStore.prototype.handleChatStatusResponse = function (response) {
168
+ var chatbot = response.chatbot, features = response.features;
169
+ var status = chatbot.status, suggestedQuestions = chatbot.suggestedQuestions, welcomeMessage = chatbot.welcomeMessage;
170
+ this.chatEnabled = status.enabled === true;
171
+ this.features = features;
172
+ this.suggestedQuestions = suggestedQuestions || [];
173
+ this.welcomeMessage = welcomeMessage || '';
174
+ this.storage.set('chatStatusResponse', JSON.stringify({ response: response, checkTime: Date.now() }));
175
+ return this.chatEnabled;
176
+ };
177
+ ChatStore.prototype.reset = function () {
178
+ this.chats = [];
179
+ this.clearProductQuickview();
180
+ this.storage.clear();
181
+ this.createChat();
182
+ };
183
+ ChatStore.prototype.setProductQuickview = function (product) {
184
+ this.productQuickview = product;
185
+ this.productQuickviewError = null;
186
+ };
187
+ ChatStore.prototype.updateProductQuickview = function (response) {
188
+ var _a;
189
+ if (!this.productQuickview) {
190
+ return;
191
+ }
192
+ // merge parent-level mappings from the API response into the Product
193
+ this.productQuickview.mappings = __assign(__assign({}, this.productQuickview.mappings), { core: __assign(__assign({}, this.productQuickview.mappings.core), response.mappings.core) });
194
+ var meta = ((_a = this.meta) === null || _a === void 0 ? void 0 : _a.data) || {};
195
+ if (this.productQuickview.variants) {
196
+ // update existing Variants with new data from the Products API
197
+ this.productQuickview.variants.optionConfig = response.variants.optionConfig;
198
+ this.productQuickview.variants.update(response.variants.data);
199
+ }
200
+ else {
201
+ // create Variants for the first time using the Products API data
202
+ this.productQuickview.variants = new SearchResultStore_1.Variants({
203
+ data: {
204
+ mask: this.productQuickview.mask,
205
+ variants: response.variants.data,
206
+ optionConfig: response.variants.optionConfig,
207
+ meta: meta,
208
+ },
209
+ });
210
+ }
211
+ };
212
+ ChatStore.prototype.setProductQuickviewError = function (message) {
213
+ this.productQuickviewError = message;
214
+ };
215
+ ChatStore.prototype.clearProductQuickview = function () {
216
+ this.productQuickview = null;
217
+ this.productQuickviewError = null;
218
+ };
219
+ ChatStore.prototype.clearHistory = function () {
220
+ // drop previous chats while keeping the conversation the user is currently in;
221
+ // if there is no current chat, fall back to a full reset
222
+ var currentChat = this.currentChat;
223
+ if (!currentChat) {
224
+ this.reset();
225
+ return;
226
+ }
227
+ var storedChats = this.storage.get('chats') || {};
228
+ var filtered = {};
229
+ if (storedChats[currentChat.id]) {
230
+ filtered[currentChat.id] = storedChats[currentChat.id];
231
+ }
232
+ this.storage.set('chats', filtered);
233
+ this.chats = [currentChat];
234
+ };
235
+ ChatStore.prototype.createChat = function (data) {
236
+ // Prune old sessions before creating a new one to keep storage bounded
237
+ ChatSessionStore_1.ChatSessionStore.pruneStoredSessions(this.storage);
238
+ var newChat = new ChatSessionStore_1.ChatSessionStore({
239
+ data: {
240
+ sessionId: data === null || data === void 0 ? void 0 : data.sessionId,
241
+ },
242
+ stores: {
243
+ storage: this.storage,
244
+ },
245
+ });
246
+ this.currentChatId = newChat.id;
247
+ this.chats.push(newChat);
248
+ return newChat;
249
+ };
250
+ ChatStore.prototype.switchChat = function (id) {
251
+ var chatExists = this.chats.find(function (chat) { return chat.id === id; });
252
+ if (chatExists) {
253
+ // Lazily hydrate results when switching to a session for the first time
254
+ if (!chatExists.hydrated && this.storedMetaData) {
255
+ chatExists.hydrateResults(this.storedMetaData);
256
+ chatExists.hydrated = true;
257
+ }
258
+ this.currentChatId = id;
259
+ }
260
+ };
261
+ ChatStore.prototype.sendProductQuery = function (result, options) {
262
+ var _a, _b, _c, _d, _e, _f, _g;
263
+ var display = (result === null || result === void 0 ? void 0 : result.display) || result;
264
+ (_a = this.currentChat) === null || _a === void 0 ? void 0 : _a.attachments.add({
265
+ type: 'product',
266
+ requestType: options.requestType,
267
+ productId: result.id,
268
+ name: (_c = (_b = display.mappings) === null || _b === void 0 ? void 0 : _b.core) === null || _c === void 0 ? void 0 : _c.name,
269
+ thumbnailUrl: ((_e = (_d = display.mappings) === null || _d === void 0 ? void 0 : _d.core) === null || _e === void 0 ? void 0 : _e.thumbnailImageUrl) || ((_g = (_f = display.mappings) === null || _f === void 0 ? void 0 : _f.core) === null || _g === void 0 ? void 0 : _g.imageUrl),
270
+ });
271
+ // for the productQuery flow we want the secondary chat to immediately
272
+ // show the product details panel — push a productQuery message so it
273
+ // becomes the active side-chat message until a productAnswer arrives
274
+ if (options.requestType === 'productQuery' && this.currentChat) {
275
+ this.currentChat.pushProductQueryMessage(result);
276
+ }
277
+ };
278
+ ChatStore.prototype.compareProduct = function (result) {
279
+ var _a;
280
+ (_a = this.currentChat) === null || _a === void 0 ? void 0 : _a.comparisons.add({
281
+ result: result,
282
+ });
283
+ };
284
+ ChatStore.prototype.addFacet = function (facet) {
285
+ var _a;
286
+ (_a = this.currentChat) === null || _a === void 0 ? void 0 : _a.attachments.add({
287
+ type: 'facet',
288
+ key: facet.key,
289
+ facetLabel: facet.facetLabel,
290
+ value: facet.value,
291
+ label: facet.label,
292
+ count: facet.count,
293
+ });
294
+ };
295
+ ChatStore.prototype.removeFacet = function (key, value) {
296
+ var _a, _b, _c;
297
+ var facetAttachment = (_a = this.currentChat) === null || _a === void 0 ? void 0 : _a.attachments.items.find(function (item) {
298
+ return item.type === 'facet' &&
299
+ item.key === key &&
300
+ item.value === value &&
301
+ (item.state === 'active' || item.state === 'attached');
302
+ });
303
+ if (facetAttachment) {
304
+ var index = (_b = this.currentChat) === null || _b === void 0 ? void 0 : _b.attachments.items.indexOf(facetAttachment);
305
+ if (index !== undefined && index !== -1) {
306
+ (_c = this.currentChat) === null || _c === void 0 ? void 0 : _c.attachments.items.splice(index, 1);
307
+ }
308
+ }
309
+ };
310
+ ChatStore.prototype.clearPendingFacets = function () {
311
+ var _this = this;
312
+ var _a;
313
+ var facetAttachments = ((_a = this.currentChat) === null || _a === void 0 ? void 0 : _a.attachments.items.filter(function (item) { return item.type === 'facet' && (item.state === 'active' || item.state === 'attached'); })) || [];
314
+ facetAttachments.forEach(function (item) {
315
+ var _a, _b;
316
+ var index = (_a = _this.currentChat) === null || _a === void 0 ? void 0 : _a.attachments.items.indexOf(item);
317
+ if (index !== undefined && index !== -1) {
318
+ (_b = _this.currentChat) === null || _b === void 0 ? void 0 : _b.attachments.items.splice(index, 1);
319
+ }
320
+ });
321
+ };
322
+ ChatStore.prototype.isFacetSelected = function (key, value) {
323
+ var _a;
324
+ return (((_a = this.currentChat) === null || _a === void 0 ? void 0 : _a.attachments.items.some(function (item) {
325
+ return item.type === 'facet' &&
326
+ item.key === key &&
327
+ item.value === value &&
328
+ (item.state === 'active' || item.state === 'attached');
329
+ })) || false);
330
+ };
331
+ ChatStore.prototype.request = function (request) {
332
+ var _this = this;
333
+ var _a, _b, _c;
334
+ (_a = this.currentChat) === null || _a === void 0 ? void 0 : _a.request(request);
335
+ // remove any facet attachments after request
336
+ var facetAttachments = ((_b = this.currentChat) === null || _b === void 0 ? void 0 : _b.attachments.attached.filter(function (item) { return item.type === 'facet'; })) || [];
337
+ facetAttachments.forEach(function (item) {
338
+ var _a;
339
+ (_a = _this.currentChat) === null || _a === void 0 ? void 0 : _a.attachments.remove(item.id);
340
+ });
341
+ // remove any productSimilar attachments after request
342
+ var productSimilarAttachments = ((_c = this.currentChat) === null || _c === void 0 ? void 0 : _c.attachments.attached.filter(function (item) { return item.type === 'product' && item.requestType === 'productSimilar'; })) || [];
343
+ productSimilarAttachments.forEach(function (item) {
344
+ var _a;
345
+ (_a = _this.currentChat) === null || _a === void 0 ? void 0 : _a.attachments.remove(item.id);
346
+ });
347
+ };
348
+ ChatStore.prototype.update = function (data) {
349
+ // TODO: handle error
350
+ // if(err?.responseBody?.errorMessage || err?.responseBody?.errorCode) {
351
+ // const errorMessage = err?.responseBody?.errorMessage || 'An unknown error has occurred.';
352
+ // }
353
+ var _a;
354
+ (_a = this.currentChat) === null || _a === void 0 ? void 0 : _a.update(data);
355
+ if (!this.meta) {
356
+ this.storage.set('meta', JSON.stringify(data.meta));
357
+ }
358
+ this.meta = new MetaStore_1.MetaStore({
359
+ data: {
360
+ meta: data.meta,
361
+ },
362
+ });
363
+ // Keep raw meta in sync for lazy hydration of inactive sessions
364
+ this.storedMetaData = data.meta;
365
+ };
366
+ return ChatStore;
367
+ }(AbstractStore_1.AbstractStore));
368
+ exports.ChatStore = ChatStore;
@@ -0,0 +1,98 @@
1
+ type AttachmentState = 'loading' | 'error' | 'attached' | 'active' | 'saved';
2
+ type AttachmentError = {
3
+ message: string;
4
+ };
5
+ export type ChatAttachmentAddAttachment = ChatAttachmentImageConfig | ChatAttachmentProductConfig | ChatAttachmentFacetConfig;
6
+ export declare class ChatAttachmentStore {
7
+ items: ChatAttachments[];
8
+ constructor();
9
+ get attached(): ChatAttachments[];
10
+ get compared(): ChatAttachmentProduct[];
11
+ add<T extends ChatAttachments>(attachment: ChatAttachmentAddAttachment): T;
12
+ remove(id: string): void;
13
+ get(id: string): ChatAttachments | undefined;
14
+ reset(): void;
15
+ hydrate(attachments: ChatAttachmentAddAttachment[]): void;
16
+ }
17
+ type ChatAttachments = ChatAttachmentImage | ChatAttachmentProduct | ChatAttachmentFacet;
18
+ declare abstract class ChatAttachment {
19
+ abstract type: string;
20
+ id: string;
21
+ state: AttachmentState;
22
+ error?: AttachmentError;
23
+ constructor(params?: {
24
+ data?: {
25
+ id?: string;
26
+ state?: AttachmentState;
27
+ error?: AttachmentError;
28
+ };
29
+ });
30
+ save(): void;
31
+ activate(): void;
32
+ }
33
+ type ChatAttachmentImageConfig = {
34
+ type: 'image';
35
+ id?: string;
36
+ base64?: string;
37
+ fileName?: string;
38
+ imageId?: string;
39
+ imageUrl?: string;
40
+ thumbnailUrl?: string;
41
+ state?: AttachmentState;
42
+ error?: AttachmentError;
43
+ };
44
+ type ChatAttachmentProductConfig = {
45
+ type: 'product';
46
+ id?: string;
47
+ productId: string;
48
+ thumbnailUrl: string;
49
+ name: string;
50
+ requestType: 'productQuery' | 'productSimilar' | 'productComparison';
51
+ state?: AttachmentState;
52
+ error?: AttachmentError;
53
+ };
54
+ type ChatAttachmentFacetConfig = {
55
+ type: 'facet';
56
+ id?: string;
57
+ key: string;
58
+ facetLabel: string;
59
+ value: string;
60
+ label: string;
61
+ count: number;
62
+ state?: AttachmentState;
63
+ error?: AttachmentError;
64
+ };
65
+ export declare class ChatAttachmentProduct extends ChatAttachment {
66
+ type: 'product' | never;
67
+ productId: string;
68
+ thumbnailUrl: string;
69
+ name: string;
70
+ requestType: 'productQuery' | 'productSimilar' | 'productComparison';
71
+ constructor({ id, productId, thumbnailUrl, name, requestType, state, error }: ChatAttachmentProductConfig);
72
+ }
73
+ export declare class ChatAttachmentFacet extends ChatAttachment {
74
+ type: 'facet' | never;
75
+ key: string;
76
+ facetLabel: string;
77
+ value: string;
78
+ label: string;
79
+ count: number;
80
+ constructor({ id, key, facetLabel, value, label, count, state, error }: ChatAttachmentFacetConfig);
81
+ }
82
+ export declare class ChatAttachmentImage extends ChatAttachment {
83
+ type: 'image' | never;
84
+ fileName?: string;
85
+ imageId?: string;
86
+ imageUrl?: string;
87
+ thumbnailUrl?: string;
88
+ base64?: string;
89
+ constructor({ id, base64, fileName, imageId, imageUrl, thumbnailUrl, state, error }: ChatAttachmentImageConfig);
90
+ update: ({ imageId, imageUrl, thumbnailUrl, error, }: {
91
+ imageId?: string;
92
+ imageUrl?: string;
93
+ thumbnailUrl?: string;
94
+ error?: AttachmentError;
95
+ }) => Promise<void>;
96
+ }
97
+ export {};
98
+ //# sourceMappingURL=ChatAttachmentStore.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ChatAttachmentStore.d.ts","sourceRoot":"","sources":["../../../../src/Chat/Stores/ChatAttachmentStore.ts"],"names":[],"mappings":"AAKA,KAAK,eAAe,GAAG,SAAS,GAAG,OAAO,GAAG,UAAU,GAAG,QAAQ,GAAG,OAAO,CAAC;AAC7E,KAAK,eAAe,GAAG;IACtB,OAAO,EAAE,MAAM,CAAC;CAChB,CAAC;AAEF,MAAM,MAAM,2BAA2B,GAAG,yBAAyB,GAAG,2BAA2B,GAAG,yBAAyB,CAAC;AAE9H,qBAAa,mBAAmB;IACxB,KAAK,EAAE,eAAe,EAAE,CAAM;;IAUrC,IAAI,QAAQ,sBAEX;IAED,IAAI,QAAQ,4BAKX;IAED,GAAG,CAAC,CAAC,SAAS,eAAe,EAAE,UAAU,EAAE,2BAA2B,GAAG,CAAC;IA+F1E,MAAM,CAAC,EAAE,EAAE,MAAM;IAajB,GAAG,CAAC,EAAE,EAAE,MAAM,GAAG,eAAe,GAAG,SAAS;IAI5C,KAAK,IAAI,IAAI;IAMb,OAAO,CAAC,WAAW,EAAE,2BAA2B,EAAE,GAAG,IAAI;CAezD;AAID,KAAK,eAAe,GAAG,mBAAmB,GAAG,qBAAqB,GAAG,mBAAmB,CAAC;AAEzF,uBAAe,cAAc;IAC5B,SAAgB,IAAI,EAAE,MAAM,CAAC;IAEtB,EAAE,EAAE,MAAM,CAAC;IACX,KAAK,EAAE,eAAe,CAAa;IACnC,KAAK,CAAC,EAAE,eAAe,CAAa;gBAE/B,MAAM,GAAE;QAAE,IAAI,CAAC,EAAE;YAAE,EAAE,CAAC,EAAE,MAAM,CAAC;YAAC,KAAK,CAAC,EAAE,eAAe,CAAC;YAAC,KAAK,CAAC,EAAE,e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