@opencx/widget 3.0.86 → 3.0.87

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 (35) hide show
  1. package/dist/designs.cjs +23 -23
  2. package/dist/designs.cjs.map +1 -1
  3. package/dist/designs.js +2817 -2760
  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 +12 -11
  10. package/dist/src/designs/react/hooks/useCanvas.d.ts +3 -0
  11. package/dist/src/designs/react/hooks/useTheme.d.ts +6 -0
  12. package/dist/src/designs/react/screens/chat/ChatCanvas.d.ts +2 -0
  13. package/dist/src/headless/core/__tests__/test-utils.d.ts +2 -2
  14. package/dist/src/headless/core/api/api-caller.d.ts +2 -2
  15. package/dist/src/headless/core/api/schema.d.ts +176 -75
  16. package/dist/src/headless/core/context/session.ctx.d.ts +8 -0
  17. package/dist/src/headless/core/context/widget.ctx.d.ts +2 -0
  18. package/dist/src/headless/core/types/dtos.d.ts +3 -2
  19. package/dist/src/headless/core/types/widget-config.d.ts +7 -0
  20. package/dist/src/headless/react/hooks/useModes.d.ts +12 -0
  21. package/dist/src/headless/react/hooks/useSessions.d.ts +12 -0
  22. package/dist/src/headless/react/index.d.ts +1 -0
  23. package/dist/{useWidgetTrigger-Bplu_1C-.cjs → useModes-C5Jd3NZU.cjs} +4 -4
  24. package/dist/useModes-C5Jd3NZU.cjs.map +1 -0
  25. package/dist/{useWidgetTrigger-Bi12WVrs.js → useModes-CCwRYrkY.js} +234 -225
  26. package/dist/useModes-CCwRYrkY.js.map +1 -0
  27. package/dist/{widget.ctx-oIT8sGvJ.cjs → widget.ctx-BIrCsbNY.cjs} +3 -3
  28. package/dist/{widget.ctx-oIT8sGvJ.cjs.map → widget.ctx-BIrCsbNY.cjs.map} +1 -1
  29. package/dist/{widget.ctx-DFFwNHvy.js → widget.ctx-D-BdBFz5.js} +54 -51
  30. package/dist/{widget.ctx-DFFwNHvy.js.map → widget.ctx-D-BdBFz5.js.map} +1 -1
  31. package/dist-embed/script.js +83 -83
  32. package/dist-embed/script.js.map +1 -1
  33. package/package.json +1 -1
  34. package/dist/useWidgetTrigger-Bi12WVrs.js.map +0 -1
  35. package/dist/useWidgetTrigger-Bplu_1C-.cjs.map +0 -1
@@ -76,8 +76,8 @@ class T {
76
76
  onProgress: i
77
77
  }) => new Promise((c, r) => {
78
78
  var v;
79
- const h = new FormData();
80
- h.append("file", t);
79
+ const g = new FormData();
80
+ g.append("file", t);
81
81
  const l = new XMLHttpRequest();
82
82
  if (s && (s.addEventListener("abort", () => {
83
83
  l.abort(), r(new DOMException("Aborted", "AbortError"));
@@ -105,16 +105,16 @@ class T {
105
105
  }), l.addEventListener("timeout", () => {
106
106
  r(new Error("Upload timed out"));
107
107
  });
108
- const { baseUrl: f } = this.constructClientOptions(this.userToken), p = `${f}/backend/widget/v2/upload`;
109
- l.open("POST", p), l.setRequestHeader("X-Bot-Token", this.config.token), this.userToken ?? ((v = this.config.user) == null ? void 0 : v.token) ? l.setRequestHeader("Authorization", `Bearer ${this.userToken}`) : console.error("User token not set"), l.send(h);
108
+ const { baseUrl: C } = this.constructClientOptions(this.userToken), p = `${C}/backend/widget/v2/upload`;
109
+ l.open("POST", p), l.setRequestHeader("X-Bot-Token", this.config.token), this.userToken ?? ((v = this.config.user) == null ? void 0 : v.token) ? l.setRequestHeader("Authorization", `Bearer ${this.userToken}`) : console.error("User token not set"), l.send(g);
110
110
  }), this.vote = async (t) => await this.client.POST("/backend/widget/v2/chat/vote", { body: t }), this.resolveSession = async (t, s) => await this.client.POST("/backend/widget/v2/session/resolve", {
111
111
  body: t,
112
112
  signal: s
113
113
  }), this.config = n, this.userToken = ((o = n.user) == null ? void 0 : o.token) || null;
114
- const { baseUrl: a, headers: g } = this.constructClientOptions(
114
+ const { baseUrl: a, headers: h } = this.constructClientOptions(
115
115
  (e = n.user) == null ? void 0 : e.token
116
116
  );
117
- this.client = this.createOpenAPIClient({ baseUrl: a, headers: g });
117
+ this.client = this.createOpenAPIClient({ baseUrl: a, headers: h });
118
118
  }
119
119
  }
120
120
  class P {
@@ -123,8 +123,8 @@ class P {
123
123
  O(this.state, a) || (this.state = a, this.notifySubscribers(a));
124
124
  }, this.setPartial = (a) => {
125
125
  if (a == null) return;
126
- const g = { ...this.state, ...a };
127
- this.set(g);
126
+ const h = { ...this.state, ...a };
127
+ this.set(h);
128
128
  }, this.reset = () => {
129
129
  this.set(this.initialState);
130
130
  }, this.notifySubscribers = (a) => {
@@ -150,7 +150,7 @@ class R {
150
150
  this.abortController.abort("Resetting poller"), (n = this.stopPolling) == null || n.call(this), this.stopPolling = null;
151
151
  }, this.stopPolling = null, this.startPolling = (n, a) => {
152
152
  if (this.stopPolling) return;
153
- const g = [], o = async () => {
153
+ const h = [], o = async () => {
154
154
  this.abortController = new AbortController(), this.state.setPartial({ isPolling: !0 });
155
155
  try {
156
156
  await n(this.abortController.signal);
@@ -161,10 +161,10 @@ class R {
161
161
  } finally {
162
162
  this.state.setPartial({ isPolling: !1 });
163
163
  }
164
- this.abortController.signal.aborted ? console.log("Poller aborted, not scheduling anymore") : g.push(setTimeout(o, a));
164
+ this.abortController.signal.aborted ? console.log("Poller aborted, not scheduling anymore") : h.push(setTimeout(o, a));
165
165
  };
166
166
  o(), this.stopPolling = () => {
167
- g.forEach(clearTimeout), this.state.reset();
167
+ h.forEach(clearTimeout), this.state.reset();
168
168
  };
169
169
  };
170
170
  }
@@ -181,7 +181,7 @@ class L {
181
181
  constructor({
182
182
  api: n,
183
183
  config: a,
184
- sessionCtx: g,
184
+ sessionCtx: h,
185
185
  messageCtx: o,
186
186
  sessionPollingIntervalSeconds: e
187
187
  }) {
@@ -192,19 +192,19 @@ class L {
192
192
  }, this.sessionPollingIntervalSeconds * 1e3) : this.poller.reset();
193
193
  });
194
194
  }, this.hackAndSlash = async (t, s) => {
195
- var h;
195
+ var g;
196
196
  this.messageCtx.state.get().messages.length === 0 && this.messageCtx.state.setPartial({ isInitialFetchLoading: !0 });
197
- const i = this.messageCtx.state.get().messages, c = i.length > 0 ? (h = i[i.length - 1]) == null ? void 0 : h.timestamp : void 0, { data: r } = await this.api.pollSessionAndHistory({
197
+ const i = this.messageCtx.state.get().messages, c = i.length > 0 ? (g = i[i.length - 1]) == null ? void 0 : g.timestamp : void 0, { data: r } = await this.api.pollSessionAndHistory({
198
198
  sessionId: t,
199
199
  abortSignal: s,
200
200
  lastMessageTimestamp: c
201
201
  });
202
202
  if (r != null && r.session && (this.sessionCtx.sessionState.setPartial({ session: r.session }), this.sessionCtx.setSessions([r.session])), r != null && r.history && r.history.length > 0) {
203
- const l = this.messageCtx.state.get().messages, f = r.history.map(this.mapHistoryToMessage).filter(
203
+ const l = this.messageCtx.state.get().messages, C = r.history.map(this.mapHistoryToMessage).filter(
204
204
  (x) => !l.some((p) => p.id === x.id)
205
205
  );
206
206
  this.messageCtx.state.setPartial({
207
- messages: [...l, ...f]
207
+ messages: [...l, ...C]
208
208
  });
209
209
  }
210
210
  this.messageCtx.state.get().isInitialFetchLoading && this.messageCtx.state.setPartial({ isInitialFetchLoading: !1 });
@@ -264,21 +264,21 @@ class L {
264
264
  if (c) return c;
265
265
  }
266
266
  return t.result;
267
- }, this.api = n, this.config = a, this.sessionCtx = g, this.messageCtx = o, this.sessionPollingIntervalSeconds = e, this.registerPolling();
267
+ }, this.api = n, this.config = a, this.sessionCtx = h, this.messageCtx = o, this.sessionPollingIntervalSeconds = e, this.registerPolling();
268
268
  }
269
269
  }
270
270
  class B {
271
271
  constructor({
272
272
  config: n,
273
273
  api: a,
274
- storageCtx: g
274
+ storageCtx: h
275
275
  }) {
276
276
  var o;
277
277
  this.shouldCollectData = () => {
278
278
  var e;
279
279
  return !!(!((e = this.state.get().contact) != null && e.token) && this.config.collectUserData);
280
280
  }, this.autoCreateUnverifiedUserIfNotExists = async () => {
281
- var e, t, s, i, c, r, h, l, f, x, p, b, v, u;
281
+ var e, t, s, i, c, r, g, l, C, x, p, b, v, u;
282
282
  if (!((e = this.config.user) != null && e.token)) {
283
283
  if (this.config.collectUserData && !((s = (t = this.config.user) == null ? void 0 : t.data) != null && s.email)) {
284
284
  if ((i = this.config.extraDataCollectionFields) != null && i.length)
@@ -287,7 +287,7 @@ class B {
287
287
  S && await this.setUnverifiedContact(S);
288
288
  return;
289
289
  }
290
- if (!((h = (r = this.config.user) == null ? void 0 : r.data) != null && h.email)) {
290
+ if (!((g = (r = this.config.user) == null ? void 0 : r.data) != null && g.email)) {
291
291
  const S = await ((l = this.storageCtx) == null ? void 0 : l.getContactToken());
292
292
  if (S) {
293
293
  await this.setUnverifiedContact(S);
@@ -295,7 +295,7 @@ class B {
295
295
  }
296
296
  }
297
297
  await this.createUnverifiedContact({
298
- email: (x = (f = this.config.user) == null ? void 0 : f.data) == null ? void 0 : x.email,
298
+ email: (x = (C = this.config.user) == null ? void 0 : C.data) == null ? void 0 : x.email,
299
299
  non_verified_name: ((b = (p = this.config.user) == null ? void 0 : p.data) == null ? void 0 : b.name) || "Anonymous",
300
300
  non_verified_custom_data: (u = (v = this.config.user) == null ? void 0 : v.data) == null ? void 0 : u.customData
301
301
  });
@@ -313,10 +313,10 @@ class B {
313
313
  this.state.setPartial({ isCreatingUnverifiedContact: !1 });
314
314
  }
315
315
  }, this.setUnverifiedContact = async (e) => {
316
- var i, c, r, h;
316
+ var i, c, r, g;
317
317
  const t = await ((i = this.storageCtx) == null ? void 0 : i.getExternalContactId()), s = ((c = this.config.user) == null ? void 0 : c.externalId) || t || E();
318
- this.api.setAuthToken(e), await ((r = this.storageCtx) == null ? void 0 : r.setContactToken(e)), await ((h = this.storageCtx) == null ? void 0 : h.setExternalContactId(s)), this.state.setPartial({ contact: { token: e, externalId: s } });
319
- }, this.config = n, this.storageCtx = g, this.api = a, this.state = new P({
318
+ this.api.setAuthToken(e), await ((r = this.storageCtx) == null ? void 0 : r.setContactToken(e)), await ((g = this.storageCtx) == null ? void 0 : g.setExternalContactId(s)), this.state.setPartial({ contact: { token: e, externalId: s } });
319
+ }, this.config = n, this.storageCtx = h, this.api = a, this.state = new P({
320
320
  contact: (o = n.user) != null && o.token ? {
321
321
  token: n.user.token,
322
322
  // Set optional externalId from config... not local storage
@@ -335,7 +335,7 @@ class q {
335
335
  constructor({
336
336
  api: n,
337
337
  contactCtx: a,
338
- sessionsPollingIntervalSeconds: g
338
+ sessionsPollingIntervalSeconds: h
339
339
  }) {
340
340
  this.sessionsRefresher = new R(), this.sessionState = new P({
341
341
  session: null,
@@ -413,14 +413,14 @@ class q {
413
413
  session_id: o.id
414
414
  });
415
415
  return e ? (this.sessionState.setPartial({ session: e, isResolvingSession: !1 }), { success: !0, data: e }) : (this.sessionState.setPartial({ isResolvingSession: !1 }), { success: !1, error: t });
416
- }, this.api = n, this.contactCtx = a, this.sessionsPollingIntervalSeconds = g, this.registerSessionsRefresherWrapper();
416
+ }, this.api = n, this.contactCtx = a, this.sessionsPollingIntervalSeconds = h, this.registerSessionsRefresherWrapper();
417
417
  }
418
418
  }
419
419
  class $ {
420
420
  constructor({
421
421
  config: n,
422
422
  api: a,
423
- sessionCtx: g,
423
+ sessionCtx: h,
424
424
  contactCtx: o
425
425
  }) {
426
426
  this.state = new P({
@@ -431,7 +431,7 @@ class $ {
431
431
  }), this.sendMessageAbortController = new AbortController(), this.reset = () => {
432
432
  this.sendMessageAbortController.abort("Resetting chat"), this.state.reset();
433
433
  }, this.sendMessage = async (e) => {
434
- var r, h, l, f, x, p, b, v;
434
+ var r, g, l, C, x, p, b, v;
435
435
  if (!e.content.trim() && (!e.attachments || e.attachments.length === 0)) {
436
436
  console.warn("Cannot send an empty message of no content or attachments");
437
437
  return;
@@ -451,7 +451,7 @@ class $ {
451
451
  ), S = this.state.get().messages;
452
452
  if (this.state.setPartial({
453
453
  messages: [...S, u]
454
- }), !((h = this.sessionCtx.sessionState.get().session) != null && h.id)) {
454
+ }), !((g = this.sessionCtx.sessionState.get().session) != null && g.id)) {
455
455
  if (!await this.sessionCtx.createSession()) {
456
456
  console.error("Failed to create session");
457
457
  return;
@@ -460,7 +460,7 @@ class $ {
460
460
  }
461
461
  const k = (l = this.sessionCtx.sessionState.get().session) == null ? void 0 : l.id;
462
462
  if (!k) return;
463
- const { data: C } = await this.api.sendMessage(
463
+ const { data: f } = await this.api.sendMessage(
464
464
  {
465
465
  uuid: u.id,
466
466
  bot_token: this.config.token,
@@ -479,26 +479,27 @@ class $ {
479
479
  },
480
480
  this.sendMessageAbortController.signal
481
481
  );
482
- if (C != null && C.success) {
483
- const w = this.toBotMessage(C);
482
+ if (f != null && f.success) {
483
+ const w = this.toBotMessage(f);
484
484
  if (w) {
485
485
  const I = this.state.get().messages;
486
486
  if (!!I.some(
487
487
  (A) => A.id === w.id
488
488
  )) {
489
489
  this.state.setPartial({
490
- lastAIResMightSolveUserIssue: ((f = C.autopilotResponse) == null ? void 0 : f.mightSolveUserIssue) || ((x = C.uiResponse) == null ? void 0 : x.mightSolveUserIssue)
490
+ lastAIResMightSolveUserIssue: ((C = f.autopilotResponse) == null ? void 0 : C.mightSolveUserIssue) || ((x = f.uiResponse) == null ? void 0 : x.mightSolveUserIssue)
491
491
  });
492
492
  return;
493
493
  }
494
494
  this.state.setPartial({
495
495
  messages: [...I, w],
496
- lastAIResMightSolveUserIssue: ((p = C.autopilotResponse) == null ? void 0 : p.mightSolveUserIssue) || ((b = C.uiResponse) == null ? void 0 : b.mightSolveUserIssue)
496
+ lastAIResMightSolveUserIssue: ((p = f.autopilotResponse) == null ? void 0 : p.mightSolveUserIssue) || ((b = f.uiResponse) == null ? void 0 : b.mightSolveUserIssue)
497
497
  });
498
498
  }
499
+ f.session && this.sessionCtx.sessionState.setPartial({ session: f.session });
499
500
  } else {
500
501
  const w = this.toBotErrorMessage(
501
- ((v = C == null ? void 0 : C.error) == null ? void 0 : v.message) || "Unknown error occurred"
502
+ ((v = f == null ? void 0 : f.error) == null ? void 0 : v.message) || "Unknown error occurred"
502
503
  ), I = this.state.get().messages;
503
504
  this.state.setPartial({
504
505
  messages: [...I, w]
@@ -512,7 +513,7 @@ class $ {
512
513
  }, this.toUserMessage = (e, t) => {
513
514
  const s = (() => {
514
515
  const i = this.contactCtx.state.get().extraCollectedData;
515
- return this.state.get().messages.length === 0 && i && Object.keys(i).length > 0 ? `${Object.entries(i).filter(([r, h]) => !!h).map(([r, h]) => `${r}: ${h}`).join(`
516
+ return this.state.get().messages.length === 0 && i && Object.keys(i).length > 0 ? `${Object.entries(i).filter(([r, g]) => !!g).map(([r, g]) => `${r}: ${g}`).join(`
516
517
  `)}
517
518
 
518
519
  ${e}` : e;
@@ -556,14 +557,14 @@ ${e}` : e;
556
557
  variant: "error",
557
558
  action: void 0
558
559
  }
559
- }), this.config = n, this.api = a, this.sessionCtx = g, this.contactCtx = o;
560
+ }), this.config = n, this.api = a, this.sessionCtx = h, this.contactCtx = o;
560
561
  }
561
562
  }
562
563
  class N {
563
564
  constructor({
564
565
  config: n,
565
566
  contactCtx: a,
566
- sessionCtx: g,
567
+ sessionCtx: h,
567
568
  resetChat: o
568
569
  }) {
569
570
  var e;
@@ -575,14 +576,14 @@ class N {
575
576
  });
576
577
  }), this.sessionCtx.sessionsState.subscribe(
577
578
  ({ isInitialFetchLoading: t, data: s }) => {
578
- var i, c, r, h;
579
+ var i, c, r, g;
579
580
  if ((i = this.config.router) != null && i.chatScreenOnly && // Do not route to a chat if we are currently inside one already
580
581
  // This also applies to newly created sessions; the new session will be in `sessionState` before it is refreshed and included in `sessionsState`
581
582
  !((c = this.sessionCtx.sessionState.get().session) != null && c.id)) {
582
- const l = (r = s.find((f) => f.isOpened)) == null ? void 0 : r.id;
583
+ const l = (r = s.find((C) => C.isOpened)) == null ? void 0 : r.id;
583
584
  return l ? this.toChatScreen(l) : void 0;
584
585
  }
585
- s.length || ((h = this.config.router) == null ? void 0 : h.goToChatIfNoSessions) !== !1 && !t && this.state.get().screen !== "chat" && this.toChatScreen();
586
+ s.length || ((g = this.config.router) == null ? void 0 : g.goToChatIfNoSessions) !== !1 && !t && this.state.get().screen !== "chat" && this.toChatScreen();
586
587
  }
587
588
  );
588
589
  }, this.toSessionsScreen = () => {
@@ -594,7 +595,7 @@ class N {
594
595
  this.sessionCtx.sessionState.setPartial({ session: s });
595
596
  }
596
597
  this.state.setPartial({ screen: "chat" });
597
- }, this.config = n, this.contactCtx = a, this.sessionCtx = g, this.resetChat = o, this.state = new P({
598
+ }, this.config = n, this.contactCtx = a, this.sessionCtx = h, this.resetChat = o, this.state = new P({
598
599
  screen: this.contactCtx.shouldCollectData() ? "welcome" : (e = this.config.router) != null && e.chatScreenOnly ? "chat" : "sessions"
599
600
  }), this.registerRoutingListener();
600
601
  }
@@ -614,15 +615,16 @@ class H {
614
615
  const m = class m {
615
616
  constructor({
616
617
  config: n,
617
- storage: a
618
+ storage: a,
619
+ modes: h
618
620
  }) {
619
- if (this.resetChat = () => {
621
+ if (this.modes = [], this.resetChat = () => {
620
622
  this.sessionCtx.reset(), this.messageCtx.reset();
621
623
  }, !m.pollingIntervalsSeconds)
622
624
  throw Error(
623
625
  "Widget polling values are not defined, did you call WidgetCtx.initialize()"
624
626
  );
625
- this.config = n, this.api = new T({ config: n }), this.storageCtx = a ? new H({ storage: a }) : void 0, this.contactCtx = new B({
627
+ this.config = n, this.api = new T({ config: n }), this.storageCtx = a ? new H({ storage: a }) : void 0, this.modes = h, this.contactCtx = new B({
626
628
  api: this.api,
627
629
  config: this.config,
628
630
  storageCtx: this.storageCtx
@@ -653,16 +655,17 @@ m.pollingIntervalsSeconds = null, m.initialize = async ({
653
655
  config: n,
654
656
  storage: a
655
657
  }) => {
656
- var o, e;
657
- const g = await new T({
658
+ var o, e, t;
659
+ const h = await new T({
658
660
  config: n
659
661
  }).getExternalWidgetConfig();
660
662
  return m.pollingIntervalsSeconds = {
661
- session: ((o = g.data) == null ? void 0 : o.sessionPollingIntervalSeconds) || 10,
662
- sessions: ((e = g.data) == null ? void 0 : e.sessionsPollingIntervalSeconds) || 60
663
+ session: ((o = h.data) == null ? void 0 : o.sessionPollingIntervalSeconds) || 10,
664
+ sessions: ((e = h.data) == null ? void 0 : e.sessionsPollingIntervalSeconds) || 60
663
665
  }, new m({
664
666
  config: n,
665
- storage: a
667
+ storage: a,
668
+ modes: ((t = h.data) == null ? void 0 : t.modes) || []
666
669
  });
667
670
  };
668
671
  let U = m;
@@ -670,4 +673,4 @@ export {
670
673
  P,
671
674
  U as W
672
675
  };
673
- //# sourceMappingURL=widget.ctx-DFFwNHvy.js.map
676
+ //# sourceMappingURL=widget.ctx-D-BdBFz5.js.map