@opencx/widget-core 4.0.14 → 4.0.15

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -15,7 +15,7 @@ const H = (g) => {
15
15
  };
16
16
  class U {
17
17
  constructor({ config: n }) {
18
- var d, e;
18
+ var h, e;
19
19
  this.userToken = null, this.constructClientOptions = (t) => {
20
20
  const s = this.config.apiUrl || "https://api.open.cx", i = {
21
21
  "X-Bot-Token": this.config.token,
@@ -40,8 +40,6 @@ class U {
40
40
  this.client = this.createOpenAPIClient({ baseUrl: s, headers: i });
41
41
  }, this.getExternalWidgetConfig = async () => await this.client.GET("/backend/widget/v2/config", {
42
42
  params: { header: { "X-Bot-Token": this.config.token } }
43
- }), this.widgetPrelude = async () => await this.client.GET("/backend/widget/v2/prelude", {
44
- params: { header: { "X-Bot-Token": this.config.token } }
45
43
  }), this.sendMessage = async (t, s) => await this.client.POST("/backend/widget/v2/chat/send", {
46
44
  body: t,
47
45
  signal: s
@@ -78,48 +76,48 @@ class U {
78
76
  var w;
79
77
  const l = new FormData();
80
78
  l.append("file", t);
81
- const h = new XMLHttpRequest();
79
+ const d = new XMLHttpRequest();
82
80
  if (s && (s.addEventListener("abort", () => {
83
- h.abort(), r(new DOMException("Aborted", "AbortError"));
81
+ d.abort(), r(new DOMException("Aborted", "AbortError"));
84
82
  }), s.aborted)) {
85
83
  r(new DOMException("Aborted", "AbortError"));
86
84
  return;
87
85
  }
88
- h.upload.addEventListener("progress", (f) => {
86
+ d.upload.addEventListener("progress", (f) => {
89
87
  if (f.lengthComputable && i) {
90
88
  const S = Math.round(f.loaded / f.total * 100);
91
89
  i(S);
92
90
  }
93
- }), h.addEventListener("load", () => {
94
- if (h.status >= 200 && h.status < 300)
91
+ }), d.addEventListener("load", () => {
92
+ if (d.status >= 200 && d.status < 300)
95
93
  try {
96
- const f = JSON.parse(h.responseText);
94
+ const f = JSON.parse(d.responseText);
97
95
  o(f);
98
96
  } catch (f) {
99
97
  r(new Error(`Failed to parse response: ${f}`));
100
98
  }
101
99
  else
102
- r(new Error(`Upload failed with status: ${h.status}`));
103
- }), h.addEventListener("error", () => {
100
+ r(new Error(`Upload failed with status: ${d.status}`));
101
+ }), d.addEventListener("error", () => {
104
102
  r(new Error("Network error occurred"));
105
- }), h.addEventListener("timeout", () => {
103
+ }), d.addEventListener("timeout", () => {
106
104
  r(new Error("Upload timed out"));
107
105
  });
108
- const { baseUrl: m } = this.constructClientOptions(this.userToken), C = `${m}/backend/widget/v2/upload`;
109
- h.open("POST", C), h.setRequestHeader("X-Bot-Token", this.config.token), this.userToken ?? ((w = this.config.user) == null ? void 0 : w.token) ? h.setRequestHeader("Authorization", `Bearer ${this.userToken}`) : console.error("User token not set"), h.send(l);
106
+ const { baseUrl: p } = this.constructClientOptions(this.userToken), C = `${p}/backend/widget/v2/upload`;
107
+ d.open("POST", C), d.setRequestHeader("X-Bot-Token", this.config.token), this.userToken ?? ((w = this.config.user) == null ? void 0 : w.token) ? d.setRequestHeader("Authorization", `Bearer ${this.userToken}`) : console.error("User token not set"), d.send(l);
110
108
  }), 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
109
  body: t,
112
110
  signal: s
113
111
  }), this.createStateCheckpoint = async (t) => await this.client.POST("/backend/widget/v2/checkpoint", {
114
112
  body: t
115
- }), this.submitCsat = async (t) => await this.client.POST("/backend/widget/v2/submit-csat", { body: t }), this.config = n, this.userToken = ((d = n.user) == null ? void 0 : d.token) || null;
113
+ }), this.submitCsat = async (t) => await this.client.POST("/backend/widget/v2/submit-csat", { body: t }), this.config = n, this.userToken = ((h = n.user) == null ? void 0 : h.token) || null;
116
114
  const { baseUrl: a, headers: c } = this.constructClientOptions(
117
115
  (e = n.user) == null ? void 0 : e.token
118
116
  );
119
117
  this.client = this.createOpenAPIClient({ baseUrl: a, headers: c });
120
118
  }
121
119
  }
122
- function B(g, n) {
120
+ function $(g, n) {
123
121
  console.error(`Missing case for ${g} in ${n}`);
124
122
  }
125
123
  class y {
@@ -133,9 +131,9 @@ class y {
133
131
  }, this.reset = () => {
134
132
  this.set(this.initialState);
135
133
  }, this.notifySubscribers = (a) => {
136
- Array.from(this.subscribers).forEach((d) => {
134
+ Array.from(this.subscribers).forEach((h) => {
137
135
  try {
138
- d(a);
136
+ h(a);
139
137
  } catch (e) {
140
138
  console.error(e);
141
139
  }
@@ -155,7 +153,7 @@ class F {
155
153
  this.abortController.abort("Resetting poller"), (n = this.stopPolling) == null || n.call(this), this.stopPolling = null;
156
154
  }, this.stopPolling = null, this.startPolling = (n, a) => {
157
155
  if (this.stopPolling) return;
158
- const c = [], d = async () => {
156
+ const c = [], h = async () => {
159
157
  this.abortController = new AbortController(), this.state.setPartial({ isPolling: !0 });
160
158
  try {
161
159
  await n(this.abortController.signal);
@@ -166,15 +164,15 @@ class F {
166
164
  } finally {
167
165
  this.state.setPartial({ isPolling: !1 });
168
166
  }
169
- this.abortController.signal.aborted ? console.log("Poller aborted, not scheduling anymore") : c.push(setTimeout(d, a));
167
+ this.abortController.signal.aborted ? console.log("Poller aborted, not scheduling anymore") : c.push(setTimeout(h, a));
170
168
  };
171
- d(), this.stopPolling = () => {
169
+ h(), this.stopPolling = () => {
172
170
  c.forEach(clearTimeout), this.state.reset();
173
171
  };
174
172
  };
175
173
  }
176
174
  }
177
- function $(g) {
175
+ function B(g) {
178
176
  try {
179
177
  const n = g();
180
178
  return n instanceof Promise ? n.then((a) => ({ data: a })).catch((a) => ({ error: a })) : { data: n };
@@ -187,7 +185,7 @@ class N {
187
185
  api: n,
188
186
  config: a,
189
187
  sessionCtx: c,
190
- messageCtx: d,
188
+ messageCtx: h,
191
189
  sessionPollingIntervalSeconds: e
192
190
  }) {
193
191
  this.poller = new F(), this.fetchSessionAndFullHistoryAbortController = new AbortController(), this.registerPolling = () => {
@@ -214,19 +212,19 @@ class N {
214
212
  abortSignal: s,
215
213
  fetchFullHistory: i = !1
216
214
  }) => {
217
- var h;
215
+ var d;
218
216
  this.messageCtx.state.get().messages.length === 0 && this.messageCtx.state.setPartial({ isInitialFetchLoading: !0 });
219
- const o = this.messageCtx.state.get().messages, r = o.length > 0 ? ((h = o[o.length - 1]) == null ? void 0 : h.timestamp) ?? void 0 : void 0, { data: l } = await this.api.pollSessionAndHistory({
217
+ const o = this.messageCtx.state.get().messages, r = o.length > 0 ? ((d = o[o.length - 1]) == null ? void 0 : d.timestamp) ?? void 0 : void 0, { data: l } = await this.api.pollSessionAndHistory({
220
218
  sessionId: t,
221
219
  abortSignal: s,
222
220
  lastMessageTimestamp: i ? void 0 : r
223
221
  });
224
222
  if (l != null && l.session && (this.sessionCtx.sessionState.setPartial({ session: l.session }), this.sessionCtx.setSessions([l.session])), l != null && l.history && l.history.length > 0) {
225
- const m = this.messageCtx.state.get().messages, v = l.history.map(this.mapHistoryToMessage).filter((C) => C !== null).filter(
226
- (C) => !m.some((x) => x.id === C.id)
223
+ const p = this.messageCtx.state.get().messages, v = l.history.map(this.mapHistoryToMessage).filter((C) => C !== null).filter(
224
+ (C) => !p.some((x) => x.id === C.id)
227
225
  );
228
226
  this.messageCtx.state.setPartial({
229
- messages: [...m, ...v]
227
+ messages: [...p, ...v]
230
228
  });
231
229
  }
232
230
  this.messageCtx.state.get().isInitialFetchLoading && this.messageCtx.state.setPartial({ isInitialFetchLoading: !1 });
@@ -323,7 +321,7 @@ class N {
323
321
  case "none":
324
322
  return null;
325
323
  default:
326
- return B(
324
+ return $(
327
325
  t.systemMessagePayload,
328
326
  this.constructSystemMessage.name
329
327
  ), null;
@@ -332,11 +330,11 @@ class N {
332
330
  const s = t.result;
333
331
  if (s === null || typeof s != "object") return s;
334
332
  if ("responseBodyText" in s && typeof s.responseBodyText == "string") {
335
- const i = s.responseBodyText, o = $(() => JSON.parse(i)).data;
333
+ const i = s.responseBodyText, o = B(() => JSON.parse(i)).data;
336
334
  if (o) return o;
337
335
  }
338
336
  return t.result;
339
- }, this.api = n, this.config = a, this.sessionCtx = c, this.messageCtx = d, this.sessionPollingIntervalSeconds = e, this.registerPolling();
337
+ }, this.api = n, this.config = a, this.sessionCtx = c, this.messageCtx = h, this.sessionPollingIntervalSeconds = e, this.registerPolling();
340
338
  }
341
339
  }
342
340
  class Y {
@@ -345,12 +343,12 @@ class Y {
345
343
  api: a,
346
344
  storageCtx: c
347
345
  }) {
348
- var d;
346
+ var h;
349
347
  this.shouldCollectData = () => {
350
348
  var e;
351
349
  return !!(!((e = this.state.get().contact) != null && e.token) && this.config.collectUserData);
352
350
  }, this.autoCreateUnverifiedUserIfNotExists = async () => {
353
- var e, t, s, i, o, r, l, h, m, v, C, x, w, f;
351
+ var e, t, s, i, o, r, l, d, p, v, C, x, w, f;
354
352
  if (!((e = this.config.user) != null && e.token)) {
355
353
  if (this.config.collectUserData && !((s = (t = this.config.user) == null ? void 0 : t.data) != null && s.email)) {
356
354
  if ((i = this.config.extraDataCollectionFields) != null && i.length)
@@ -360,14 +358,14 @@ class Y {
360
358
  return;
361
359
  }
362
360
  if (!((l = (r = this.config.user) == null ? void 0 : r.data) != null && l.email)) {
363
- const S = await ((h = this.storageCtx) == null ? void 0 : h.getContactToken());
361
+ const S = await ((d = this.storageCtx) == null ? void 0 : d.getContactToken());
364
362
  if (S) {
365
363
  await this.setUnverifiedContact(S);
366
364
  return;
367
365
  }
368
366
  }
369
367
  await this.createUnverifiedContact({
370
- email: (v = (m = this.config.user) == null ? void 0 : m.data) == null ? void 0 : v.email,
368
+ email: (v = (p = this.config.user) == null ? void 0 : p.data) == null ? void 0 : v.email,
371
369
  non_verified_name: ((x = (C = this.config.user) == null ? void 0 : C.data) == null ? void 0 : x.name) || "Anonymous",
372
370
  non_verified_custom_data: (f = (w = this.config.user) == null ? void 0 : w.data) == null ? void 0 : f.customData
373
371
  });
@@ -389,7 +387,7 @@ class Y {
389
387
  const t = await ((i = this.storageCtx) == null ? void 0 : i.getExternalContactId()), s = ((o = this.config.user) == null ? void 0 : o.externalId) || t || O();
390
388
  this.api.setAuthToken(e), await ((r = this.storageCtx) == null ? void 0 : r.setContactToken(e)), await ((l = this.storageCtx) == null ? void 0 : l.setExternalContactId(s)), this.state.setPartial({ contact: { token: e, externalId: s } });
391
389
  }, this.config = n, this.storageCtx = c, this.api = a, this.state = new y({
392
- contact: (d = n.user) != null && d.token ? {
390
+ contact: (h = n.user) != null && h.token ? {
393
391
  token: n.user.token,
394
392
  // Set optional externalId from config... not local storage
395
393
  externalId: n.user.externalId
@@ -403,12 +401,12 @@ class Y {
403
401
  function I() {
404
402
  return O();
405
403
  }
406
- class X {
404
+ class K {
407
405
  constructor({
408
406
  config: n,
409
407
  api: a,
410
408
  sessionCtx: c,
411
- messageCtx: d
409
+ messageCtx: h
412
410
  }) {
413
411
  this.submitCsat = async (e) => {
414
412
  var r;
@@ -439,15 +437,15 @@ class X {
439
437
  session_id: t
440
438
  });
441
439
  return { data: i, error: o };
442
- }, this.config = n, this.api = a, this.sessionCtx = c, this.messageCtx = d;
440
+ }, this.config = n, this.api = a, this.sessionCtx = c, this.messageCtx = h;
443
441
  }
444
442
  }
445
- class G {
443
+ class X {
446
444
  constructor({
447
445
  config: n,
448
446
  api: a,
449
447
  contactCtx: c,
450
- sessionsPollingIntervalSeconds: d
448
+ sessionsPollingIntervalSeconds: h
451
449
  }) {
452
450
  this.sessionsRefresher = new F(), this.sessionState = new y({
453
451
  session: null,
@@ -535,15 +533,15 @@ class G {
535
533
  payload: e
536
534
  });
537
535
  return s ? { data: s } : { success: !1, error: i };
538
- }, this.config = n, this.api = a, this.contactCtx = c, this.sessionsPollingIntervalSeconds = d, this.registerSessionsRefresherWrapper();
536
+ }, this.config = n, this.api = a, this.contactCtx = c, this.sessionsPollingIntervalSeconds = h, this.registerSessionsRefresherWrapper();
539
537
  }
540
538
  }
541
- class K {
539
+ class z {
542
540
  constructor({
543
541
  config: n,
544
542
  api: a,
545
543
  sessionCtx: c,
546
- contactCtx: d
544
+ contactCtx: h
547
545
  }) {
548
546
  this.state = new y({
549
547
  messages: [],
@@ -553,7 +551,7 @@ class K {
553
551
  }), this.sendMessageAbortController = new AbortController(), this.reset = () => {
554
552
  this.sendMessageAbortController.abort("Resetting chat"), this.state.reset();
555
553
  }, this.sendMessage = async (e) => {
556
- var t, s, i, o, r, l, h, m, v, C;
554
+ var t, s, i, o, r, l, d, p, v, C;
557
555
  try {
558
556
  if (!e.content.trim() && (!e.attachments || e.attachments.length === 0)) {
559
557
  console.warn(
@@ -597,7 +595,7 @@ class K {
597
595
  }
598
596
  const T = (r = this.sessionCtx.sessionState.get().session) == null ? void 0 : r.id;
599
597
  if (!T) return;
600
- const { data: p } = await this.api.sendMessage(
598
+ const { data: m } = await this.api.sendMessage(
601
599
  {
602
600
  uuid: k.id,
603
601
  bot_token: this.config.token,
@@ -621,27 +619,27 @@ class K {
621
619
  },
622
620
  this.sendMessageAbortController.signal
623
621
  );
624
- if (p != null && p.success) {
625
- const u = this.toBotMessage(p);
622
+ if (m != null && m.success) {
623
+ const u = this.toBotMessage(m);
626
624
  if (u) {
627
625
  const P = this.state.get().messages;
628
626
  if (!!P.some(
629
627
  (_) => _.id === u.id
630
628
  )) {
631
629
  this.state.setPartial({
632
- lastAIResMightSolveUserIssue: ((l = p.autopilotResponse) == null ? void 0 : l.mightSolveUserIssue) || ((h = p.uiResponse) == null ? void 0 : h.mightSolveUserIssue)
630
+ lastAIResMightSolveUserIssue: ((l = m.autopilotResponse) == null ? void 0 : l.mightSolveUserIssue) || ((d = m.uiResponse) == null ? void 0 : d.mightSolveUserIssue)
633
631
  });
634
632
  return;
635
633
  }
636
634
  this.state.setPartial({
637
635
  messages: [...P, u],
638
- lastAIResMightSolveUserIssue: ((m = p.autopilotResponse) == null ? void 0 : m.mightSolveUserIssue) || ((v = p.uiResponse) == null ? void 0 : v.mightSolveUserIssue)
636
+ lastAIResMightSolveUserIssue: ((p = m.autopilotResponse) == null ? void 0 : p.mightSolveUserIssue) || ((v = m.uiResponse) == null ? void 0 : v.mightSolveUserIssue)
639
637
  });
640
638
  }
641
- p.session && this.sessionCtx.sessionState.setPartial({ session: p.session });
639
+ m.session && this.sessionCtx.sessionState.setPartial({ session: m.session });
642
640
  } else {
643
641
  const u = this.toBotErrorMessage(
644
- ((C = p == null ? void 0 : p.error) == null ? void 0 : C.message) || "Unknown error occurred"
642
+ ((C = m == null ? void 0 : m.error) == null ? void 0 : C.message) || "Unknown error occurred"
645
643
  ), P = this.state.get().messages;
646
644
  this.state.setPartial({
647
645
  messages: [...P, u]
@@ -699,15 +697,15 @@ ${e}` : e;
699
697
  variant: "error",
700
698
  action: void 0
701
699
  }
702
- }), this.config = n, this.api = a, this.sessionCtx = c, this.contactCtx = d;
700
+ }), this.config = n, this.api = a, this.sessionCtx = c, this.contactCtx = h;
703
701
  }
704
702
  }
705
- class z {
703
+ class G {
706
704
  constructor({
707
705
  config: n,
708
706
  contactCtx: a,
709
707
  sessionCtx: c,
710
- resetChat: d
708
+ resetChat: h
711
709
  }) {
712
710
  var e;
713
711
  this.registerRoutingListener = () => {
@@ -722,8 +720,8 @@ class z {
722
720
  if ((i = this.config.router) != null && i.chatScreenOnly && // Do not route to a chat if we are currently inside one already
723
721
  // This also applies to newly created sessions; the new session will be in `sessionState` before it is refreshed and included in `sessionsState`
724
722
  !((o = this.sessionCtx.sessionState.get().session) != null && o.id)) {
725
- const h = (r = s.find((m) => m.isOpened)) == null ? void 0 : r.id;
726
- return h ? this.toChatScreen(h) : void 0;
723
+ const d = (r = s.find((p) => p.isOpened)) == null ? void 0 : r.id;
724
+ return d ? this.toChatScreen(d) : void 0;
727
725
  }
728
726
  s.length || ((l = this.config.router) == null ? void 0 : l.goToChatIfNoSessions) !== !1 && !t && this.state.get().screen !== "chat" && this.toChatScreen();
729
727
  }
@@ -737,7 +735,7 @@ class z {
737
735
  this.sessionCtx.sessionState.setPartial({ session: s });
738
736
  }
739
737
  this.state.setPartial({ screen: "chat" });
740
- }, this.config = n, this.contactCtx = a, this.sessionCtx = c, this.resetChat = d, this.state = new y({
738
+ }, this.config = n, this.contactCtx = a, this.sessionCtx = c, this.resetChat = h, this.state = new y({
741
739
  screen: this.contactCtx.shouldCollectData() ? "welcome" : (e = this.config.router) != null && e.chatScreenOnly ? "chat" : "sessions"
742
740
  }), this.registerRoutingListener();
743
741
  }
@@ -761,7 +759,8 @@ const b = class b {
761
759
  constructor({
762
760
  config: n,
763
761
  storage: a,
764
- modes: c
762
+ modes: c,
763
+ org: h
765
764
  }) {
766
765
  if (this.modes = [], this.resetChat = () => {
767
766
  this.sessionCtx.reset(), this.messageCtx.reset();
@@ -769,21 +768,21 @@ const b = class b {
769
768
  throw Error(
770
769
  "Widget polling values are not defined, did you call WidgetCtx.initialize()"
771
770
  );
772
- this.config = n, this.api = new U({ config: n }), this.storageCtx = a ? new j({ storage: a, config: n }) : void 0, this.modes = c, this.contactCtx = new Y({
771
+ this.config = n, this.org = h, this.api = new U({ config: n }), this.storageCtx = a ? new j({ storage: a, config: n }) : void 0, this.modes = c, this.contactCtx = new Y({
773
772
  api: this.api,
774
773
  config: this.config,
775
774
  storageCtx: this.storageCtx
776
- }), this.sessionCtx = new G({
775
+ }), this.sessionCtx = new X({
777
776
  config: this.config,
778
777
  api: this.api,
779
778
  contactCtx: this.contactCtx,
780
779
  sessionsPollingIntervalSeconds: b.pollingIntervalsSeconds.sessions
781
- }), this.messageCtx = new K({
780
+ }), this.messageCtx = new z({
782
781
  config: this.config,
783
782
  api: this.api,
784
783
  sessionCtx: this.sessionCtx,
785
784
  contactCtx: this.contactCtx
786
- }), this.csatCtx = new X({
785
+ }), this.csatCtx = new K({
787
786
  config: this.config,
788
787
  api: this.api,
789
788
  sessionCtx: this.sessionCtx,
@@ -794,7 +793,7 @@ const b = class b {
794
793
  sessionCtx: this.sessionCtx,
795
794
  messageCtx: this.messageCtx,
796
795
  sessionPollingIntervalSeconds: b.pollingIntervalsSeconds.session
797
- }), this.routerCtx = new z({
796
+ }), this.routerCtx = new G({
798
797
  config: this.config,
799
798
  contactCtx: this.contactCtx,
800
799
  sessionCtx: this.sessionCtx,
@@ -806,23 +805,29 @@ b.pollingIntervalsSeconds = null, b.initialize = async ({
806
805
  config: n,
807
806
  storage: a
808
807
  }) => {
809
- var d, e, t;
808
+ var h, e, t;
810
809
  const c = await new U({
811
810
  config: n
812
811
  }).getExternalWidgetConfig();
812
+ if (!c.data)
813
+ throw new Error("Failed to fetch widget config");
813
814
  return b.pollingIntervalsSeconds = {
814
- session: ((d = c.data) == null ? void 0 : d.sessionPollingIntervalSeconds) || 10,
815
+ session: ((h = c.data) == null ? void 0 : h.sessionPollingIntervalSeconds) || 10,
815
816
  sessions: ((e = c.data) == null ? void 0 : e.sessionsPollingIntervalSeconds) || 60
816
817
  }, new b({
817
818
  config: n,
818
819
  storage: a,
819
- modes: ((t = c.data) == null ? void 0 : t.modes) || []
820
+ modes: ((t = c.data) == null ? void 0 : t.modes) || [],
821
+ org: {
822
+ id: c.data.org.id,
823
+ name: c.data.org.name
824
+ }
820
825
  });
821
826
  };
822
827
  let R = b;
823
828
  export {
824
829
  y as PrimitiveState,
825
830
  R as WidgetCtx,
826
- B as isExhaustive
831
+ $ as isExhaustive
827
832
  };
828
833
  //# sourceMappingURL=index.js.map