@opencx/widget 3.0.32 → 3.0.34

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 (44) hide show
  1. package/dist/designs.cjs +54 -54
  2. package/dist/designs.cjs.map +1 -1
  3. package/dist/designs.js +1954 -1934
  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/headless/core/api.d.ts +1 -1
  14. package/dist/src/headless/core/context/contact.ctx.d.ts +8 -3
  15. package/dist/src/headless/core/context/message.ctx.d.ts +1 -1
  16. package/dist/src/headless/core/context/session.ctx.d.ts +2 -8
  17. package/dist/src/headless/core/context/storage.ctx.d.ts +12 -0
  18. package/dist/src/headless/core/context/widget.ctx.d.ts +6 -2
  19. package/dist/src/headless/core/index.d.ts +2 -1
  20. package/dist/src/headless/core/types/external-storage.d.ts +5 -0
  21. package/dist/src/headless/core/utils/Poller.d.ts +1 -1
  22. package/dist/src/headless/core/utils/PrimitiveState.d.ts +3 -35
  23. package/dist/src/headless/core/utils/PrimitiveState.spec.d.ts +1 -0
  24. package/dist/src/headless/react/ComponentRegistry.d.ts +1 -1
  25. package/dist/src/headless/react/WidgetProvider.d.ts +3 -2
  26. package/dist/src/headless/react/hooks/useContact.d.ts +1 -0
  27. package/dist/src/headless/react/hooks/useSessions.d.ts +1 -0
  28. package/dist/{useUploadFiles-Cr6lNjTS.cjs → useUploadFiles-C0y6HEB6.cjs} +2 -2
  29. package/dist/{useUploadFiles-CeI1AbIE.js.map → useUploadFiles-C0y6HEB6.cjs.map} +1 -1
  30. package/dist/{useUploadFiles-CeI1AbIE.js → useUploadFiles-DkWg1-Gg.js} +83 -83
  31. package/dist/useUploadFiles-DkWg1-Gg.js.map +1 -0
  32. package/dist/{widget.ctx-OHYgtVi5.js → widget.ctx-CplVbjtp.js} +171 -187
  33. package/dist/widget.ctx-CplVbjtp.js.map +1 -0
  34. package/dist/widget.ctx-D_AFA5p3.cjs +2 -0
  35. package/dist/widget.ctx-D_AFA5p3.cjs.map +1 -0
  36. package/dist-embed/script.js +105 -105
  37. package/dist-embed/script.js.map +1 -1
  38. package/package.json +5 -4
  39. package/dist/useUploadFiles-Cr6lNjTS.cjs.map +0 -1
  40. package/dist/widget.ctx-DmyynT8O.cjs +0 -2
  41. package/dist/widget.ctx-DmyynT8O.cjs.map +0 -1
  42. package/dist/widget.ctx-OHYgtVi5.js.map +0 -1
  43. /package/dist/src/headless/core/types/{WidgetConfig.d.ts → widget-config.d.ts} +0 -0
  44. /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 A = Object.defineProperty;
2
+ var U = (o, t, e) => t in o ? A(o, t, { enumerable: !0, configurable: !0, writable: !0, value: e }) : o[t] = e;
3
+ var s = (o, t, e) => U(o, typeof t != "symbol" ? t + "" : t, e);
4
+ import k from "axios";
5
+ import M from "openapi-fetch";
6
+ import R from "lodash.isequal";
7
+ import { v4 as P } from "uuid";
8
+ const T = (o) => {
9
+ console.log(o.error);
10
+ }, E = (o) => {
11
+ const t = M({
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 || T
21
17
  };
22
18
  return t.use(e), t;
23
19
  };
24
- class H {
20
+ class F {
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
+ }) => E({
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,108 +118,72 @@ 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
121
  class f {
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
+ R(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
- var e;
219
- return !!(!((e = this.state.get().contact) != null && e.token) && this.config.collectUserData);
220
- });
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
226
- });
164
+ var t;
165
+ return !!(!((t = this.state.get().contact) != null && t.token) && this.config.collectUserData);
166
+ });
167
+ s(this, "autoCreateUnverifiedUserIfNotExists", async () => {
168
+ var t, e, i, a, n, r, c, g, d, C, p;
169
+ if (!((t = this.config.user) != null && t.token)) {
170
+ if (this.config.collectUserData && !((i = (e = this.config.user) == null ? void 0 : e.data) != null && i.email)) {
171
+ const h = await ((a = this.storageCtx) == null ? void 0 : a.getContactToken());
172
+ h && await this.setUnverifiedContact(h);
173
+ return;
174
+ }
175
+ if (!((r = (n = this.config.user) == null ? void 0 : n.data) != null && r.email)) {
176
+ const h = await ((c = this.storageCtx) == null ? void 0 : c.getContactToken());
177
+ if (h) {
178
+ await this.setUnverifiedContact(h);
179
+ return;
180
+ }
181
+ }
182
+ await this.createUnverifiedContact({
183
+ name: ((d = (g = this.config.user) == null ? void 0 : g.data) == null ? void 0 : d.name) || "Anonymous",
184
+ email: (p = (C = this.config.user) == null ? void 0 : C.data) == null ? void 0 : p.email
185
+ });
186
+ }
227
187
  });
228
188
  s(this, "createUnverifiedContact", async (t) => {
229
189
  try {
@@ -232,20 +192,29 @@ class G {
232
192
  isErrorCreatingUnverifiedContact: !1
233
193
  });
234
194
  const { data: e } = await this.api.createUnverifiedContact(t);
235
- e != null && e.token ? (this.api.setAuthToken(e.token), this.state.setPartial({ contact: { token: e.token } })) : this.state.setPartial({ isErrorCreatingUnverifiedContact: !0 });
195
+ e != null && e.token ? await this.setUnverifiedContact(e.token) : this.state.setPartial({ isErrorCreatingUnverifiedContact: !0 });
236
196
  } finally {
237
197
  this.state.setPartial({ isCreatingUnverifiedContact: !1 });
238
198
  }
239
199
  });
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,
200
+ s(this, "setUnverifiedContact", async (t) => {
201
+ var a, n, r, c;
202
+ const e = await ((a = this.storageCtx) == null ? void 0 : a.getExternalContactId()), i = ((n = this.config.user) == null ? void 0 : n.externalId) || e || P();
203
+ this.api.setAuthToken(t), await ((r = this.storageCtx) == null ? void 0 : r.setContactToken(t)), await ((c = this.storageCtx) == null ? void 0 : c.setExternalContactId(i)), this.state.setPartial({ contact: { token: t, externalId: i } });
204
+ });
205
+ var a;
206
+ this.config = t, this.storageCtx = i, this.api = e, this.state = new f({
207
+ contact: (a = t.user) != null && a.token ? {
208
+ token: t.user.token,
209
+ // Set optional externalId from config... not local storage
210
+ externalId: t.user.externalId
211
+ } : null,
243
212
  isCreatingUnverifiedContact: !1,
244
213
  isErrorCreatingUnverifiedContact: !1
245
- }), !((r = t.user) != null && r.token) && !t.collectUserData && this.autoCreateUnverifiedUser();
214
+ }), this.autoCreateUnverifiedUserIfNotExists();
246
215
  }
247
216
  }
248
- class v {
217
+ class w {
249
218
  constructor() {
250
219
  s(this, "state", new f({
251
220
  isPolling: !1,
@@ -263,10 +232,10 @@ class v {
263
232
  this.abortController = new AbortController(), this.state.setPartial({ isPolling: !0 });
264
233
  try {
265
234
  await t(this.abortController.signal);
266
- } catch (r) {
235
+ } catch (n) {
267
236
  if (this.abortController.signal.aborted)
268
237
  return;
269
- console.error("Failed to poll:", r), this.state.setPartial({ isError: !0 });
238
+ console.error("Failed to poll:", n), this.state.setPartial({ isError: !0 });
270
239
  } finally {
271
240
  this.state.setPartial({ isPolling: !1 });
272
241
  }
@@ -278,20 +247,15 @@ class v {
278
247
  });
279
248
  }
280
249
  }
281
- function P() {
282
- return _();
250
+ function x() {
251
+ return P();
283
252
  }
284
- class j {
285
- constructor({
286
- config: t,
287
- api: e,
288
- contactCtx: i
289
- }) {
290
- s(this, "config");
253
+ class _ {
254
+ constructor({ api: t, contactCtx: e }) {
291
255
  s(this, "api");
292
256
  s(this, "contactCtx");
293
- s(this, "activeSessionPoller", new v());
294
- s(this, "sessionsRefresher", new v());
257
+ s(this, "activeSessionPoller", new w());
258
+ s(this, "sessionsRefresher", new w());
295
259
  s(this, "sessionState", new f({
296
260
  session: null,
297
261
  isCreatingSession: !1
@@ -322,37 +286,33 @@ class j {
322
286
  });
323
287
  });
324
288
  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());
289
+ var t;
290
+ // If the widget config was initially provided with a contact token, no state change would be triggered, so we just fetch
291
+ (t = this.contactCtx.state.get().contact) != null && t.token && !this.sessionsState.get().didStartInitialFetch ? this.registerSessionsRefresher() : this.contactCtx.state.subscribe(({ contact: e }) => {
292
+ e != null && e.token && !this.sessionsState.get().didStartInitialFetch && this.registerSessionsRefresher();
331
293
  });
332
294
  });
333
295
  s(this, "registerSessionsRefresher", () => {
334
296
  this.sessionsRefresher.startPolling(async () => {
297
+ this.sessionsState.get().didStartInitialFetch === !1 && this.sessionsState.setPartial({ didStartInitialFetch: !0 });
335
298
  const { data: t } = await this.getSessions({ cursor: void 0 });
336
299
  if (!t) return;
337
300
  const e = [...t.items, ...this.sessionsState.get().data].filter(
338
- (i, a, r) => a === r.findIndex((o) => i.id === o.id)
301
+ (i, a, n) => a === n.findIndex((r) => i.id === r.id)
339
302
  );
340
- this.sessionsState.setPartial({ data: e });
303
+ this.sessionsState.setPartial({ data: e }), this.sessionsState.get().isInitialFetchLoading === !0 && this.sessionsState.setPartial({ isInitialFetchLoading: !1 });
341
304
  }, 1e4);
342
305
  });
343
306
  s(this, "createSession", async () => {
344
- var i, a;
307
+ var a;
345
308
  this.sessionState.setPartial({ session: null, isCreatingSession: !0 });
346
- const { data: t, error: e } = await this.api.createSession({
347
- customData: (i = this.config.user) != null && i.externalId ? {
348
- external_id: (a = this.config.user) == null ? void 0 : a.externalId
309
+ const t = (a = this.contactCtx.state.get().contact) == null ? void 0 : a.externalId, { data: e, error: i } = await this.api.createSession({
310
+ customData: t ? {
311
+ external_id: t
349
312
  } : void 0
350
313
  });
351
- return t ? (this.sessionState.setPartial({ session: t, isCreatingSession: !1 }), t) : (console.error("Failed to create session:", e), null);
314
+ return e ? (this.sessionState.setPartial({ session: e, isCreatingSession: !1 }), e) : (console.error("Failed to create session:", i), null);
352
315
  });
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
316
  s(this, "loadMoreSessions", async () => {
357
317
  if (this.sessionsState.get().isLastPage) return;
358
318
  const { data: t } = await this.getSessions({
@@ -360,7 +320,7 @@ class j {
360
320
  });
361
321
  if (t) {
362
322
  const i = [...this.sessionsState.get().data, ...t.items].filter(
363
- (a, r, o) => r === o.findIndex((g) => a.id === g.id)
323
+ (a, n, r) => n === r.findIndex((c) => a.id === c.id)
364
324
  );
365
325
  this.sessionsState.setPartial({
366
326
  data: i,
@@ -370,18 +330,20 @@ class j {
370
330
  }
371
331
  });
372
332
  s(this, "getSessions", async ({ cursor: t }) => {
373
- var e, i;
374
- return (e = this.contactCtx.state.get().contact) != null && e.token ? await this.api.getSessions({
333
+ var i, a;
334
+ if (!((i = this.contactCtx.state.get().contact) != null && i.token)) return { data: null };
335
+ const e = (a = this.contactCtx.state.get().contact) == null ? void 0 : a.externalId;
336
+ return await this.api.getSessions({
375
337
  cursor: t,
376
- filters: (i = this.config.user) != null && i.externalId ? {
377
- external_id: this.config.user.externalId
338
+ filters: e ? {
339
+ external_id: e
378
340
  } : {}
379
- }) : { data: null };
341
+ });
380
342
  });
381
- this.config = t, this.api = e, this.contactCtx = i, this.registerActiveSessionPolling(), this.registerInitialSessionsFetch();
343
+ this.api = t, this.contactCtx = e, this.registerActiveSessionPolling(), this.registerInitialSessionsFetch();
382
344
  }
383
345
  }
384
- class N {
346
+ class L {
385
347
  constructor({
386
348
  config: t,
387
349
  api: e,
@@ -390,7 +352,7 @@ class N {
390
352
  s(this, "config");
391
353
  s(this, "api");
392
354
  s(this, "sessionCtx");
393
- s(this, "poller", new v());
355
+ s(this, "poller", new w());
394
356
  s(this, "state", new f({
395
357
  messages: [],
396
358
  isSendingMessage: !1,
@@ -409,12 +371,12 @@ class N {
409
371
  });
410
372
  });
411
373
  s(this, "sendMessage", async (t) => {
412
- var r, o, g, m, C, y, x, A, U;
374
+ var n, r, c, g, d, C, p, h, b;
413
375
  if (!t.content.trim() && (!t.attachments || t.attachments.length === 0)) {
414
376
  console.warn("Cannot send an empty message of no content or attachments");
415
377
  return;
416
378
  }
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);
379
+ 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
380
  if (i && e || // If last message is from user, then bot response did not arrive yet
419
381
  i && (a == null ? void 0 : a.type) === "FROM_USER") {
420
382
  console.warn("Cannot send messages while awaiting AI response");
@@ -426,50 +388,50 @@ class N {
426
388
  const S = this.toUserMessage(
427
389
  t.content.trim(),
428
390
  t.attachments || void 0
429
- ), k = this.state.get().messages;
391
+ ), I = this.state.get().messages;
430
392
  if (this.state.setPartial({
431
- messages: [...k, S]
432
- }), !((o = this.sessionCtx.sessionState.get().session) != null && o.id) && !await this.sessionCtx.createSession()) {
393
+ messages: [...I, S]
394
+ }), !((r = this.sessionCtx.sessionState.get().session) != null && r.id) && !await this.sessionCtx.createSession()) {
433
395
  console.error("Failed to create session");
434
396
  return;
435
397
  }
436
- const I = (g = this.sessionCtx.sessionState.get().session) == null ? void 0 : g.id;
437
- if (!I) return;
398
+ const v = (c = this.sessionCtx.sessionState.get().session) == null ? void 0 : c.id;
399
+ if (!v) return;
438
400
  const { data: l } = await this.api.sendMessage(
439
401
  {
440
402
  uuid: S.id,
441
403
  bot_token: this.config.token,
442
404
  headers: this.config.headers,
443
405
  query_params: this.config.queryParams,
444
- session_id: I,
445
- user: (m = this.config.user) == null ? void 0 : m.data,
406
+ session_id: v,
407
+ user: (g = this.config.user) == null ? void 0 : g.data,
446
408
  ...t
447
409
  },
448
410
  this.sendMessageAbortController.signal
449
411
  );
450
412
  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
413
+ const u = this.toBotMessage(l);
414
+ if (u) {
415
+ const m = this.state.get().messages;
416
+ if (!!m.some(
417
+ (y) => y.id === u.id
456
418
  )) {
457
419
  this.state.setPartial({
458
- lastAIResMightSolveUserIssue: ((C = l.autopilotResponse) == null ? void 0 : C.mightSolveUserIssue) || ((y = l.uiResponse) == null ? void 0 : y.mightSolveUserIssue)
420
+ lastAIResMightSolveUserIssue: ((d = l.autopilotResponse) == null ? void 0 : d.mightSolveUserIssue) || ((C = l.uiResponse) == null ? void 0 : C.mightSolveUserIssue)
459
421
  });
460
422
  return;
461
423
  }
462
424
  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)
425
+ messages: [...m, u],
426
+ lastAIResMightSolveUserIssue: ((p = l.autopilotResponse) == null ? void 0 : p.mightSolveUserIssue) || ((h = l.uiResponse) == null ? void 0 : h.mightSolveUserIssue)
465
427
  });
466
428
  }
467
429
  } 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;
430
+ const u = this.toBotErrorMessage(
431
+ ((b = l == null ? void 0 : l.error) == null ? void 0 : b.message) || "Unknown error occurred"
432
+ ), m = this.state.get().messages;
471
433
  this.state.setPartial({
472
- messages: [...b, d]
434
+ messages: [...m, u]
473
435
  });
474
436
  }
475
437
  } catch (S) {
@@ -479,26 +441,26 @@ class N {
479
441
  }
480
442
  });
481
443
  s(this, "fetchAndSetHistory", async (t, e) => {
482
- var r;
444
+ var n;
483
445
  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({
446
+ const i = (n = this.state.get().messages.at(-1)) == null ? void 0 : n.timestamp, { data: a } = await this.api.getSessionHistory({
485
447
  sessionId: t,
486
448
  lastMessageTimestamp: i,
487
449
  abortSignal: e
488
450
  });
489
451
  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)
452
+ const r = this.state.get().messages, c = a.map(this.mapHistoryToMessage).filter(
453
+ (g) => !r.some((d) => d.id === g.id)
492
454
  );
493
455
  this.state.setPartial({
494
- messages: [...o, ...g]
456
+ messages: [...r, ...c]
495
457
  });
496
458
  }
497
459
  this.state.get().isInitialFetchLoading && this.state.setPartial({ isInitialFetchLoading: !1 });
498
460
  });
499
461
  /** Not the best name but whatever */
500
462
  s(this, "mapHistoryToMessage", (t) => {
501
- var a, r, o;
463
+ var a, n, r;
502
464
  const e = {
503
465
  id: t.publicId,
504
466
  timestamp: t.sentAt || "",
@@ -533,9 +495,9 @@ class N {
533
495
  component: "bot_message",
534
496
  agent: {
535
497
  id: null,
536
- name: ((r = this.config.bot) == null ? void 0 : r.name) || "",
498
+ name: ((n = this.config.bot) == null ? void 0 : n.name) || "",
537
499
  isAi: !0,
538
- avatar: ((o = this.config.bot) == null ? void 0 : o.avatar) || ""
500
+ avatar: ((r = this.config.bot) == null ? void 0 : r.avatar) || ""
539
501
  },
540
502
  data: {
541
503
  message: t.content.text || "",
@@ -544,7 +506,7 @@ class N {
544
506
  };
545
507
  });
546
508
  s(this, "toUserMessage", (t, e) => ({
547
- id: P(),
509
+ id: x(),
548
510
  type: "FROM_USER",
549
511
  content: t,
550
512
  deliveredAt: (/* @__PURE__ */ new Date()).toISOString(),
@@ -555,7 +517,7 @@ class N {
555
517
  var e;
556
518
  return t.success && t.autopilotResponse ? {
557
519
  type: "FROM_BOT",
558
- id: t.autopilotResponse.id || P(),
520
+ id: t.autopilotResponse.id || x(),
559
521
  timestamp: (/* @__PURE__ */ new Date()).toISOString(),
560
522
  component: "bot_message",
561
523
  agent: this.config.bot ? {
@@ -575,7 +537,7 @@ class N {
575
537
  });
576
538
  s(this, "toBotErrorMessage", (t) => ({
577
539
  type: "FROM_BOT",
578
- id: P(),
540
+ id: x(),
579
541
  timestamp: (/* @__PURE__ */ new Date()).toISOString(),
580
542
  component: "TEXT",
581
543
  data: {
@@ -587,7 +549,7 @@ class N {
587
549
  this.config = t, this.api = e, this.sessionCtx = i, this.registerPolling();
588
550
  }
589
551
  }
590
- class X {
552
+ class D {
591
553
  constructor({
592
554
  contactCtx: t,
593
555
  sessionCtx: e,
@@ -621,29 +583,51 @@ class X {
621
583
  }), this.contactCtx = t, this.sessionCtx = e, this.resetChat = i, this.registerRoutingListener();
622
584
  }
623
585
  }
624
- class V {
625
- constructor({ config: t }) {
586
+ class B {
587
+ constructor({ storage: t }) {
588
+ s(this, "storage");
589
+ s(this, "KEYS", {
590
+ contactToken: "opencx__widget__contactToken",
591
+ externalContactId: "opencx__widget__externalContactId"
592
+ });
593
+ s(this, "setContactToken", async (t) => {
594
+ await this.storage.set(this.KEYS.contactToken, t);
595
+ });
596
+ s(this, "getContactToken", async () => this.storage.get(this.KEYS.contactToken));
597
+ s(this, "setExternalContactId", async (t) => {
598
+ await this.storage.set(this.KEYS.externalContactId, t);
599
+ });
600
+ s(this, "getExternalContactId", async () => this.storage.get(this.KEYS.externalContactId));
601
+ this.storage = t;
602
+ }
603
+ }
604
+ class j {
605
+ constructor({
606
+ config: t,
607
+ storage: e
608
+ }) {
626
609
  s(this, "config");
627
610
  s(this, "api");
628
611
  s(this, "contactCtx");
629
612
  s(this, "sessionCtx");
630
613
  s(this, "messageCtx");
631
614
  s(this, "routerCtx");
615
+ s(this, "storageCtx");
632
616
  s(this, "resetChat", () => {
633
617
  this.sessionCtx.reset(), this.messageCtx.reset();
634
618
  });
635
- this.config = t, this.api = new H({ config: t }), this.contactCtx = new G({
619
+ this.config = t, this.api = new F({ config: t }), this.storageCtx = e ? new B({ storage: e }) : void 0, this.contactCtx = new O({
636
620
  api: this.api,
637
- config: this.config
638
- }), this.sessionCtx = new j({
639
621
  config: this.config,
622
+ storageCtx: this.storageCtx
623
+ }), this.sessionCtx = new _({
640
624
  api: this.api,
641
625
  contactCtx: this.contactCtx
642
- }), this.messageCtx = new N({
626
+ }), this.messageCtx = new L({
643
627
  config: this.config,
644
628
  api: this.api,
645
629
  sessionCtx: this.sessionCtx
646
- }), this.routerCtx = new X({
630
+ }), this.routerCtx = new D({
647
631
  contactCtx: this.contactCtx,
648
632
  sessionCtx: this.sessionCtx,
649
633
  resetChat: this.resetChat
@@ -652,6 +636,6 @@ class V {
652
636
  }
653
637
  export {
654
638
  f as P,
655
- V as W
639
+ j as W
656
640
  };
657
- //# sourceMappingURL=widget.ctx-OHYgtVi5.js.map
641
+ //# sourceMappingURL=widget.ctx-CplVbjtp.js.map