@opencx/widget 3.0.31 → 3.0.33

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 (45) hide show
  1. package/dist/designs.cjs +65 -65
  2. package/dist/designs.cjs.map +1 -1
  3. package/dist/designs.js +5800 -5772
  4. package/dist/designs.js.map +1 -1
  5. package/dist/index.cjs +1 -1
  6. package/dist/index.js +1 -1
  7. package/dist/react.cjs +1 -1
  8. package/dist/react.cjs.map +1 -1
  9. package/dist/react.js +22 -16
  10. package/dist/react.js.map +1 -1
  11. package/dist/src/designs/react/components/custom-components/Handoff.component.d.ts +3 -0
  12. package/dist/src/designs/react/hooks/useWidgetContentHeight.d.ts +1 -1
  13. package/dist/src/designs/react/index.d.ts +3 -1
  14. package/dist/src/headless/core/api.d.ts +1 -1
  15. package/dist/src/headless/core/context/contact.ctx.d.ts +6 -3
  16. package/dist/src/headless/core/context/message.ctx.d.ts +1 -1
  17. package/dist/src/headless/core/context/session.ctx.d.ts +2 -5
  18. package/dist/src/headless/core/context/storage.ctx.d.ts +12 -0
  19. package/dist/src/headless/core/context/widget.ctx.d.ts +6 -2
  20. package/dist/src/headless/core/index.d.ts +2 -1
  21. package/dist/src/headless/core/types/external-storage.d.ts +5 -0
  22. package/dist/src/headless/core/types/messages.d.ts +1 -1
  23. package/dist/src/headless/core/utils/Poller.d.ts +1 -1
  24. package/dist/src/headless/core/utils/PrimitiveState.d.ts +3 -35
  25. package/dist/src/headless/core/utils/PrimitiveState.spec.d.ts +1 -0
  26. package/dist/src/headless/react/ComponentRegistry.d.ts +1 -1
  27. package/dist/src/headless/react/WidgetProvider.d.ts +3 -2
  28. package/dist/src/headless/react/hooks/useSessions.d.ts +1 -0
  29. package/dist/{useUploadFiles-BzTmt4K3.js → useUploadFiles-BQllMoIx.js} +83 -83
  30. package/dist/useUploadFiles-BQllMoIx.js.map +1 -0
  31. package/dist/{useUploadFiles-D5HbucUw.cjs → useUploadFiles-Ckj8FegM.cjs} +2 -2
  32. package/dist/{useUploadFiles-BzTmt4K3.js.map → useUploadFiles-Ckj8FegM.cjs.map} +1 -1
  33. package/dist/{widget.ctx-OHYgtVi5.js → widget.ctx-D0ExFLNH.js} +141 -176
  34. package/dist/widget.ctx-D0ExFLNH.js.map +1 -0
  35. package/dist/widget.ctx-DnH2-LG7.cjs +2 -0
  36. package/dist/widget.ctx-DnH2-LG7.cjs.map +1 -0
  37. package/dist-embed/script.js +118 -118
  38. package/dist-embed/script.js.map +1 -1
  39. package/package.json +5 -4
  40. package/dist/useUploadFiles-D5HbucUw.cjs.map +0 -1
  41. package/dist/widget.ctx-DmyynT8O.cjs +0 -2
  42. package/dist/widget.ctx-DmyynT8O.cjs.map +0 -1
  43. package/dist/widget.ctx-OHYgtVi5.js.map +0 -1
  44. /package/dist/src/headless/core/types/{WidgetConfig.d.ts → widget-config.d.ts} +0 -0
  45. /package/dist/src/headless/core/utils/{PrimitiveState.test.d.ts → Poller.spec.d.ts} +0 -0
@@ -1,27 +1,23 @@
1
- var L = Object.defineProperty;
2
- var M = (n) => {
3
- throw TypeError(n);
4
- };
5
- var O = (n, t, e) => t in n ? L(n, t, { enumerable: !0, configurable: !0, writable: !0, value: e }) : n[t] = e;
6
- var s = (n, t, e) => O(n, typeof t != "symbol" ? t + "" : t, e), R = (n, t, e) => t.has(n) || M("Cannot " + e);
7
- var h = (n, t, e) => (R(n, t, "read from private field"), e ? e.call(n) : t.get(n)), w = (n, t, e) => t.has(n) ? M("Cannot add the same private member more than once") : t instanceof WeakSet ? t.add(n) : t.set(n, e), p = (n, t, e, i) => (R(n, t, "write to private field"), i ? i.call(n, e) : t.set(n, e), e);
8
- import T from "axios";
9
- import E from "openapi-fetch";
10
- import D from "lodash.isequal";
11
- import { v4 as _ } from "uuid";
12
- const B = (n) => {
13
- console.log(n.error);
14
- }, q = (n) => {
15
- const t = E({
16
- baseUrl: n.baseUrl
1
+ var y = Object.defineProperty;
2
+ var A = (o, t, e) => t in o ? y(o, t, { enumerable: !0, configurable: !0, writable: !0, value: e }) : o[t] = e;
3
+ var s = (o, t, e) => A(o, typeof t != "symbol" ? t + "" : t, e);
4
+ import k from "axios";
5
+ import U from "openapi-fetch";
6
+ import M from "lodash.isequal";
7
+ import { v4 as R } from "uuid";
8
+ const E = (o) => {
9
+ console.log(o.error);
10
+ }, F = (o) => {
11
+ const t = U({
12
+ baseUrl: o.baseUrl
17
13
  }), e = {
18
- onRequest: n.onRequest,
19
- onResponse: n.onResponse,
20
- onError: n.onError || B
14
+ onRequest: o.onRequest,
15
+ onResponse: o.onResponse,
16
+ onError: o.onError || E
21
17
  };
22
18
  return t.use(e), t;
23
19
  };
24
- class H {
20
+ class T {
25
21
  constructor({
26
22
  config: t
27
23
  }) {
@@ -40,18 +36,18 @@ class H {
40
36
  s(this, "createOpenAPIClient", ({
41
37
  baseUrl: t,
42
38
  headers: e
43
- }) => q({
39
+ }) => F({
44
40
  baseUrl: t,
45
41
  onRequest: ({ request: i }) => {
46
- Object.entries(e).forEach(([a, r]) => {
47
- r && i.headers.set(a, r);
42
+ Object.entries(e).forEach(([a, n]) => {
43
+ n && i.headers.set(a, n);
48
44
  });
49
45
  }
50
46
  }));
51
47
  s(this, "createAxiosUploadClient", ({
52
48
  baseUrl: t,
53
49
  headers: e
54
- }) => T.create({
50
+ }) => k.create({
55
51
  baseURL: `${t}/backend/widget/v2/upload`,
56
52
  headers: e
57
53
  }));
@@ -122,107 +118,57 @@ class H {
122
118
  this.client = this.createOpenAPIClient({ baseUrl: e, headers: i }), this.uploadFileClient = this.createAxiosUploadClient({ baseUrl: e, headers: i });
123
119
  }
124
120
  }
125
- var c, u;
126
- class f {
121
+ class g {
127
122
  constructor(t) {
128
123
  s(this, "subscribers", /* @__PURE__ */ new Set());
129
- w(this, c);
124
+ s(this, "state");
130
125
  s(this, "initialState");
131
- w(this, u);
132
- s(this, "lifecycleListeners", /* @__PURE__ */ new Map());
133
- s(this, "emitLifecycle", (t, e) => {
134
- const i = this.lifecycleListeners.get(t);
135
- if (i) {
136
- const a = {
137
- type: t,
138
- timestamp: Date.now(),
139
- data: e
140
- };
141
- i.forEach((r) => {
142
- try {
143
- r(a);
144
- } catch {
145
- }
146
- });
147
- }
126
+ s(this, "get", () => this.state);
127
+ s(this, "set", (t) => {
128
+ M(this.state, t) || (this.state = t, this.notifySubscribers(t));
129
+ });
130
+ s(this, "setPartial", (t) => {
131
+ if (t == null) return;
132
+ const e = { ...this.state, ...t };
133
+ this.set(e);
134
+ });
135
+ s(this, "reset", () => {
136
+ this.set(this.initialState);
148
137
  });
149
138
  s(this, "notifySubscribers", (t) => {
150
139
  Array.from(this.subscribers).forEach((i) => {
151
140
  try {
152
141
  i(t);
153
142
  } catch (a) {
154
- this.emitLifecycle("error", { error: a });
143
+ console.error(a);
155
144
  }
156
145
  });
157
146
  });
158
- /**
159
- * Subscribe to state changes
160
- * @param callback Function to call when state changes
161
- * @returns Unsubscribe function
162
- */
163
147
  s(this, "subscribe", (t) => (this.subscribers.add(t), () => {
164
148
  this.subscribers.delete(t);
165
149
  }));
166
- s(this, "onLifecycle", (t, e) => {
167
- this.lifecycleListeners.has(t) || this.lifecycleListeners.set(t, /* @__PURE__ */ new Set());
168
- const i = this.lifecycleListeners.get(t);
169
- return i.add(e), () => {
170
- i.delete(e), i.size === 0 && this.lifecycleListeners.delete(t);
171
- };
172
- });
173
- /** Get the current state */
174
- s(this, "get", () => h(this, c));
175
- // TODO make this provide prev state
176
- /**
177
- * Set the state and notify subscribers if the state changes
178
- * @param newState The new state to set
179
- */
180
- s(this, "set", (t) => {
181
- this.emitLifecycle("beforeUpdate", {
182
- previousState: h(this, c),
183
- nextState: t
184
- }), D(h(this, c), t) || (p(this, c, t), p(this, u, Date.now()), this.emitLifecycle("stateChange", { state: t }), this.notifySubscribers(t)), this.emitLifecycle("afterUpdate", { state: t });
185
- });
186
- // TODO make this provide prev state
187
- s(this, "setPartial", (t) => {
188
- if (t == null) return;
189
- const e = { ...h(this, c), ...t };
190
- this.set(e);
191
- });
192
- /**
193
- * Clear all subscriptions
194
- */
195
- s(this, "clear", () => {
196
- this.emitLifecycle(
197
- "destroy"
198
- /* DESTROY */
199
- ), this.subscribers = /* @__PURE__ */ new Set(), this.lifecycleListeners = /* @__PURE__ */ new Map();
200
- });
201
- s(this, "reset", () => {
202
- this.set(this.initialState);
203
- });
204
- s(this, "lastUpdated", () => h(this, u));
205
- p(this, c, t), this.initialState = t, p(this, u, Date.now()), this.emitLifecycle("init", { initialState: h(this, c) });
150
+ this.state = t, this.initialState = t;
206
151
  }
207
152
  }
208
- c = new WeakMap(), u = new WeakMap();
209
- class G {
153
+ class O {
210
154
  constructor({
211
155
  config: t,
212
- api: e
156
+ api: e,
157
+ storageCtx: i
213
158
  }) {
214
159
  s(this, "config");
160
+ s(this, "storageCtx");
215
161
  s(this, "api");
216
162
  s(this, "state");
217
163
  s(this, "shouldCollectData", () => {
218
164
  var e;
219
165
  return !!(!((e = this.state.get().contact) != null && e.token) && this.config.collectUserData);
220
166
  });
221
- s(this, "autoCreateUnverifiedUser", async () => {
222
- var t, e, i, a;
223
- await this.createUnverifiedContact({
224
- name: ((e = (t = this.config.user) == null ? void 0 : t.data) == null ? void 0 : e.name) || "Anonymous",
225
- email: (a = (i = this.config.user) == null ? void 0 : i.data) == null ? void 0 : a.email
167
+ s(this, "autoCreateUnverifiedUserIfNotExists", async () => {
168
+ var t, e, i, a, n;
169
+ (t = this.config.user) != null && t.token || this.config.collectUserData || await this.createUnverifiedContact({
170
+ name: ((i = (e = this.config.user) == null ? void 0 : e.data) == null ? void 0 : i.name) || "Anonymous",
171
+ email: (n = (a = this.config.user) == null ? void 0 : a.data) == null ? void 0 : n.email
226
172
  });
227
173
  });
228
174
  s(this, "createUnverifiedContact", async (t) => {
@@ -237,17 +183,17 @@ class G {
237
183
  this.state.setPartial({ isCreatingUnverifiedContact: !1 });
238
184
  }
239
185
  });
240
- var i, a, r;
241
- this.config = t, this.api = e, this.state = new f({
242
- contact: (i = t.user) != null && i.token ? { token: (a = t.user) == null ? void 0 : a.token } : null,
186
+ var a, n;
187
+ this.config = t, this.storageCtx = i, this.api = e, this.state = new g({
188
+ contact: (a = t.user) != null && a.token ? { token: (n = t.user) == null ? void 0 : n.token } : null,
243
189
  isCreatingUnverifiedContact: !1,
244
190
  isErrorCreatingUnverifiedContact: !1
245
- }), !((r = t.user) != null && r.token) && !t.collectUserData && this.autoCreateUnverifiedUser();
191
+ }), this.autoCreateUnverifiedUserIfNotExists();
246
192
  }
247
193
  }
248
- class v {
194
+ class S {
249
195
  constructor() {
250
- s(this, "state", new f({
196
+ s(this, "state", new g({
251
197
  isPolling: !1,
252
198
  isError: !1
253
199
  }));
@@ -263,10 +209,10 @@ class v {
263
209
  this.abortController = new AbortController(), this.state.setPartial({ isPolling: !0 });
264
210
  try {
265
211
  await t(this.abortController.signal);
266
- } catch (r) {
212
+ } catch (n) {
267
213
  if (this.abortController.signal.aborted)
268
214
  return;
269
- console.error("Failed to poll:", r), this.state.setPartial({ isError: !0 });
215
+ console.error("Failed to poll:", n), this.state.setPartial({ isError: !0 });
270
216
  } finally {
271
217
  this.state.setPartial({ isPolling: !1 });
272
218
  }
@@ -278,10 +224,10 @@ class v {
278
224
  });
279
225
  }
280
226
  }
281
- function P() {
282
- return _();
227
+ function p() {
228
+ return R();
283
229
  }
284
- class j {
230
+ class _ {
285
231
  constructor({
286
232
  config: t,
287
233
  api: e,
@@ -290,13 +236,13 @@ class j {
290
236
  s(this, "config");
291
237
  s(this, "api");
292
238
  s(this, "contactCtx");
293
- s(this, "activeSessionPoller", new v());
294
- s(this, "sessionsRefresher", new v());
295
- s(this, "sessionState", new f({
239
+ s(this, "activeSessionPoller", new S());
240
+ s(this, "sessionsRefresher", new S());
241
+ s(this, "sessionState", new g({
296
242
  session: null,
297
243
  isCreatingSession: !1
298
244
  }));
299
- s(this, "sessionsState", new f({
245
+ s(this, "sessionsState", new g({
300
246
  data: [],
301
247
  cursor: void 0,
302
248
  isLastPage: !1,
@@ -322,22 +268,21 @@ class j {
322
268
  });
323
269
  });
324
270
  s(this, "registerInitialSessionsFetch", () => {
325
- var e;
326
- const t = async () => {
327
- this.sessionsState.setPartial({ didStartInitialFetch: !0 }), await this.loadMoreSessions(), this.sessionsState.setPartial({ isInitialFetchLoading: !1 });
328
- };
329
- (e = this.contactCtx.state.get().contact) != null && e.token && !this.sessionsState.get().didStartInitialFetch && (t(), this.registerSessionsRefresher()), this.contactCtx.state.subscribe(({ contact: i }) => {
330
- i != null && i.token && !this.sessionsState.get().didStartInitialFetch && (t(), this.registerSessionsRefresher());
271
+ var t;
272
+ // If the widget config was initially provided with a contact token, no state change would be triggered, so we just fetch
273
+ (t = this.contactCtx.state.get().contact) != null && t.token && !this.sessionsState.get().didStartInitialFetch ? this.registerSessionsRefresher() : this.contactCtx.state.subscribe(({ contact: e }) => {
274
+ e != null && e.token && !this.sessionsState.get().didStartInitialFetch && this.registerSessionsRefresher();
331
275
  });
332
276
  });
333
277
  s(this, "registerSessionsRefresher", () => {
334
278
  this.sessionsRefresher.startPolling(async () => {
279
+ this.sessionsState.get().didStartInitialFetch === !1 && this.sessionsState.setPartial({ didStartInitialFetch: !0 });
335
280
  const { data: t } = await this.getSessions({ cursor: void 0 });
336
281
  if (!t) return;
337
282
  const e = [...t.items, ...this.sessionsState.get().data].filter(
338
- (i, a, r) => a === r.findIndex((o) => i.id === o.id)
283
+ (i, a, n) => a === n.findIndex((r) => i.id === r.id)
339
284
  );
340
- this.sessionsState.setPartial({ data: e });
285
+ this.sessionsState.setPartial({ data: e }), this.sessionsState.get().isInitialFetchLoading === !0 && this.sessionsState.setPartial({ isInitialFetchLoading: !1 });
341
286
  }, 1e4);
342
287
  });
343
288
  s(this, "createSession", async () => {
@@ -350,9 +295,6 @@ class j {
350
295
  });
351
296
  return t ? (this.sessionState.setPartial({ session: t, isCreatingSession: !1 }), t) : (console.error("Failed to create session:", e), null);
352
297
  });
353
- /**
354
- * Let's keep this private for now until we figure out how to do paginated fetching in tandem with the interval refreshing
355
- */
356
298
  s(this, "loadMoreSessions", async () => {
357
299
  if (this.sessionsState.get().isLastPage) return;
358
300
  const { data: t } = await this.getSessions({
@@ -360,7 +302,7 @@ class j {
360
302
  });
361
303
  if (t) {
362
304
  const i = [...this.sessionsState.get().data, ...t.items].filter(
363
- (a, r, o) => r === o.findIndex((g) => a.id === g.id)
305
+ (a, n, r) => n === r.findIndex((c) => a.id === c.id)
364
306
  );
365
307
  this.sessionsState.setPartial({
366
308
  data: i,
@@ -381,7 +323,7 @@ class j {
381
323
  this.config = t, this.api = e, this.contactCtx = i, this.registerActiveSessionPolling(), this.registerInitialSessionsFetch();
382
324
  }
383
325
  }
384
- class N {
326
+ class L {
385
327
  constructor({
386
328
  config: t,
387
329
  api: e,
@@ -390,8 +332,8 @@ class N {
390
332
  s(this, "config");
391
333
  s(this, "api");
392
334
  s(this, "sessionCtx");
393
- s(this, "poller", new v());
394
- s(this, "state", new f({
335
+ s(this, "poller", new S());
336
+ s(this, "state", new g({
395
337
  messages: [],
396
338
  isSendingMessage: !1,
397
339
  lastAIResMightSolveUserIssue: !1,
@@ -409,12 +351,12 @@ class N {
409
351
  });
410
352
  });
411
353
  s(this, "sendMessage", async (t) => {
412
- var r, o, g, m, C, y, x, A, U;
354
+ var n, r, c, d, u, m, x, b, v;
413
355
  if (!t.content.trim() && (!t.attachments || t.attachments.length === 0)) {
414
356
  console.warn("Cannot send an empty message of no content or attachments");
415
357
  return;
416
358
  }
417
- const e = this.state.get().isSendingMessage, i = ((r = this.sessionCtx.sessionState.get().session) == null ? void 0 : r.assignee.kind) === "ai", a = this.state.get().messages.at(-1);
359
+ const e = this.state.get().isSendingMessage, i = ((n = this.sessionCtx.sessionState.get().session) == null ? void 0 : n.assignee.kind) === "ai", a = this.state.get().messages.at(-1);
418
360
  if (i && e || // If last message is from user, then bot response did not arrive yet
419
361
  i && (a == null ? void 0 : a.type) === "FROM_USER") {
420
362
  console.warn("Cannot send messages while awaiting AI response");
@@ -423,82 +365,82 @@ class N {
423
365
  this.sendMessageAbortController = new AbortController(), this.state.setPartial({ lastAIResMightSolveUserIssue: !1 });
424
366
  try {
425
367
  this.state.setPartial({ isSendingMessage: !0 });
426
- const S = this.toUserMessage(
368
+ const f = this.toUserMessage(
427
369
  t.content.trim(),
428
370
  t.attachments || void 0
429
- ), k = this.state.get().messages;
371
+ ), P = this.state.get().messages;
430
372
  if (this.state.setPartial({
431
- messages: [...k, S]
432
- }), !((o = this.sessionCtx.sessionState.get().session) != null && o.id) && !await this.sessionCtx.createSession()) {
373
+ messages: [...P, f]
374
+ }), !((r = this.sessionCtx.sessionState.get().session) != null && r.id) && !await this.sessionCtx.createSession()) {
433
375
  console.error("Failed to create session");
434
376
  return;
435
377
  }
436
- const I = (g = this.sessionCtx.sessionState.get().session) == null ? void 0 : g.id;
437
- if (!I) return;
378
+ const w = (c = this.sessionCtx.sessionState.get().session) == null ? void 0 : c.id;
379
+ if (!w) return;
438
380
  const { data: l } = await this.api.sendMessage(
439
381
  {
440
- uuid: S.id,
382
+ uuid: f.id,
441
383
  bot_token: this.config.token,
442
384
  headers: this.config.headers,
443
385
  query_params: this.config.queryParams,
444
- session_id: I,
445
- user: (m = this.config.user) == null ? void 0 : m.data,
386
+ session_id: w,
387
+ user: (d = this.config.user) == null ? void 0 : d.data,
446
388
  ...t
447
389
  },
448
390
  this.sendMessageAbortController.signal
449
391
  );
450
392
  if (l != null && l.success) {
451
- const d = this.toBotMessage(l);
452
- if (d) {
453
- const b = this.state.get().messages;
454
- if (!!b.some(
455
- (F) => F.id === d.id
393
+ const h = this.toBotMessage(l);
394
+ if (h) {
395
+ const C = this.state.get().messages;
396
+ if (!!C.some(
397
+ (I) => I.id === h.id
456
398
  )) {
457
399
  this.state.setPartial({
458
- lastAIResMightSolveUserIssue: ((C = l.autopilotResponse) == null ? void 0 : C.mightSolveUserIssue) || ((y = l.uiResponse) == null ? void 0 : y.mightSolveUserIssue)
400
+ lastAIResMightSolveUserIssue: ((u = l.autopilotResponse) == null ? void 0 : u.mightSolveUserIssue) || ((m = l.uiResponse) == null ? void 0 : m.mightSolveUserIssue)
459
401
  });
460
402
  return;
461
403
  }
462
404
  this.state.setPartial({
463
- messages: [...b, d],
464
- lastAIResMightSolveUserIssue: ((x = l.autopilotResponse) == null ? void 0 : x.mightSolveUserIssue) || ((A = l.uiResponse) == null ? void 0 : A.mightSolveUserIssue)
405
+ messages: [...C, h],
406
+ lastAIResMightSolveUserIssue: ((x = l.autopilotResponse) == null ? void 0 : x.mightSolveUserIssue) || ((b = l.uiResponse) == null ? void 0 : b.mightSolveUserIssue)
465
407
  });
466
408
  }
467
409
  } else {
468
- const d = this.toBotErrorMessage(
469
- ((U = l == null ? void 0 : l.error) == null ? void 0 : U.message) || "Unknown error occurred"
470
- ), b = this.state.get().messages;
410
+ const h = this.toBotErrorMessage(
411
+ ((v = l == null ? void 0 : l.error) == null ? void 0 : v.message) || "Unknown error occurred"
412
+ ), C = this.state.get().messages;
471
413
  this.state.setPartial({
472
- messages: [...b, d]
414
+ messages: [...C, h]
473
415
  });
474
416
  }
475
- } catch (S) {
476
- this.sendMessageAbortController.signal.aborted || console.error("Failed to send message:", S);
417
+ } catch (f) {
418
+ this.sendMessageAbortController.signal.aborted || console.error("Failed to send message:", f);
477
419
  } finally {
478
420
  this.state.setPartial({ isSendingMessage: !1 });
479
421
  }
480
422
  });
481
423
  s(this, "fetchAndSetHistory", async (t, e) => {
482
- var r;
424
+ var n;
483
425
  this.state.get().messages.length === 0 && this.state.setPartial({ isInitialFetchLoading: !0 });
484
- const i = (r = this.state.get().messages.at(-1)) == null ? void 0 : r.timestamp, { data: a } = await this.api.getSessionHistory({
426
+ const i = (n = this.state.get().messages.at(-1)) == null ? void 0 : n.timestamp, { data: a } = await this.api.getSessionHistory({
485
427
  sessionId: t,
486
428
  lastMessageTimestamp: i,
487
429
  abortSignal: e
488
430
  });
489
431
  if (a && a.length > 0) {
490
- const o = this.state.get().messages, g = a.map(this.mapHistoryToMessage).filter(
491
- (m) => !o.some((C) => C.id === m.id)
432
+ const r = this.state.get().messages, c = a.map(this.mapHistoryToMessage).filter(
433
+ (d) => !r.some((u) => u.id === d.id)
492
434
  );
493
435
  this.state.setPartial({
494
- messages: [...o, ...g]
436
+ messages: [...r, ...c]
495
437
  });
496
438
  }
497
439
  this.state.get().isInitialFetchLoading && this.state.setPartial({ isInitialFetchLoading: !1 });
498
440
  });
499
441
  /** Not the best name but whatever */
500
442
  s(this, "mapHistoryToMessage", (t) => {
501
- var a, r, o;
443
+ var a, n, r;
502
444
  const e = {
503
445
  id: t.publicId,
504
446
  timestamp: t.sentAt || "",
@@ -533,9 +475,9 @@ class N {
533
475
  component: "bot_message",
534
476
  agent: {
535
477
  id: null,
536
- name: ((r = this.config.bot) == null ? void 0 : r.name) || "",
478
+ name: ((n = this.config.bot) == null ? void 0 : n.name) || "",
537
479
  isAi: !0,
538
- avatar: ((o = this.config.bot) == null ? void 0 : o.avatar) || ""
480
+ avatar: ((r = this.config.bot) == null ? void 0 : r.avatar) || ""
539
481
  },
540
482
  data: {
541
483
  message: t.content.text || "",
@@ -544,7 +486,7 @@ class N {
544
486
  };
545
487
  });
546
488
  s(this, "toUserMessage", (t, e) => ({
547
- id: P(),
489
+ id: p(),
548
490
  type: "FROM_USER",
549
491
  content: t,
550
492
  deliveredAt: (/* @__PURE__ */ new Date()).toISOString(),
@@ -555,7 +497,7 @@ class N {
555
497
  var e;
556
498
  return t.success && t.autopilotResponse ? {
557
499
  type: "FROM_BOT",
558
- id: t.autopilotResponse.id || P(),
500
+ id: t.autopilotResponse.id || p(),
559
501
  timestamp: (/* @__PURE__ */ new Date()).toISOString(),
560
502
  component: "bot_message",
561
503
  agent: this.config.bot ? {
@@ -575,7 +517,7 @@ class N {
575
517
  });
576
518
  s(this, "toBotErrorMessage", (t) => ({
577
519
  type: "FROM_BOT",
578
- id: P(),
520
+ id: p(),
579
521
  timestamp: (/* @__PURE__ */ new Date()).toISOString(),
580
522
  component: "TEXT",
581
523
  data: {
@@ -587,7 +529,7 @@ class N {
587
529
  this.config = t, this.api = e, this.sessionCtx = i, this.registerPolling();
588
530
  }
589
531
  }
590
- class X {
532
+ class D {
591
533
  constructor({
592
534
  contactCtx: t,
593
535
  sessionCtx: e,
@@ -616,34 +558,57 @@ class X {
616
558
  }
617
559
  this.state.setPartial({ screen: "chat" });
618
560
  });
619
- this.state = new f({
561
+ this.state = new g({
620
562
  screen: t.shouldCollectData() ? "welcome" : "sessions"
621
563
  }), this.contactCtx = t, this.sessionCtx = e, this.resetChat = i, this.registerRoutingListener();
622
564
  }
623
565
  }
624
- class V {
625
- constructor({ config: t }) {
566
+ class B {
567
+ constructor({ storage: t }) {
568
+ s(this, "storage");
569
+ s(this, "KEYS", {
570
+ contactToken: "opencx__widget__contactToken",
571
+ externalContactId: "opencx__widget__externalContactId"
572
+ });
573
+ s(this, "setContactToken", async (t) => {
574
+ await this.storage.set(this.KEYS.contactToken, t);
575
+ });
576
+ s(this, "getContactToken", async () => this.storage.get(this.KEYS.contactToken));
577
+ s(this, "setExternalContactId", async (t) => {
578
+ await this.storage.set(this.KEYS.externalContactId, t);
579
+ });
580
+ s(this, "getExternalContactId", async () => this.storage.get(this.KEYS.externalContactId));
581
+ this.storage = t;
582
+ }
583
+ }
584
+ class j {
585
+ constructor({
586
+ config: t,
587
+ storage: e
588
+ }) {
626
589
  s(this, "config");
627
590
  s(this, "api");
628
591
  s(this, "contactCtx");
629
592
  s(this, "sessionCtx");
630
593
  s(this, "messageCtx");
631
594
  s(this, "routerCtx");
595
+ s(this, "storageCtx");
632
596
  s(this, "resetChat", () => {
633
597
  this.sessionCtx.reset(), this.messageCtx.reset();
634
598
  });
635
- this.config = t, this.api = new H({ config: t }), this.contactCtx = new G({
599
+ this.config = t, this.api = new T({ config: t }), this.storageCtx = e ? new B({ storage: e }) : void 0, this.contactCtx = new O({
636
600
  api: this.api,
637
- config: this.config
638
- }), this.sessionCtx = new j({
601
+ config: this.config,
602
+ storageCtx: this.storageCtx
603
+ }), this.sessionCtx = new _({
639
604
  config: this.config,
640
605
  api: this.api,
641
606
  contactCtx: this.contactCtx
642
- }), this.messageCtx = new N({
607
+ }), this.messageCtx = new L({
643
608
  config: this.config,
644
609
  api: this.api,
645
610
  sessionCtx: this.sessionCtx
646
- }), this.routerCtx = new X({
611
+ }), this.routerCtx = new D({
647
612
  contactCtx: this.contactCtx,
648
613
  sessionCtx: this.sessionCtx,
649
614
  resetChat: this.resetChat
@@ -651,7 +616,7 @@ class V {
651
616
  }
652
617
  }
653
618
  export {
654
- f as P,
655
- V as W
619
+ g as P,
620
+ j as W
656
621
  };
657
- //# sourceMappingURL=widget.ctx-OHYgtVi5.js.map
622
+ //# sourceMappingURL=widget.ctx-D0ExFLNH.js.map