@opencx/widget-core 4.0.32 → 4.0.35

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
@@ -30,8 +30,8 @@ class M {
30
30
  }) => B({
31
31
  baseUrl: e,
32
32
  onRequest: ({ request: n }) => {
33
- Object.entries(t).forEach(([o, r]) => {
34
- r && n.headers.set(o, r);
33
+ Object.entries(t).forEach(([o, l]) => {
34
+ l && n.headers.set(o, l);
35
35
  });
36
36
  }
37
37
  }), this.setAuthToken = (e) => {
@@ -72,15 +72,15 @@ class M {
72
72
  file: e,
73
73
  abortSignal: t,
74
74
  onProgress: n
75
- }) => new Promise((o, r) => {
75
+ }) => new Promise((o, l) => {
76
76
  var S;
77
77
  const c = new FormData();
78
78
  c.append("file", e);
79
79
  const u = new XMLHttpRequest();
80
80
  if (t && (t.addEventListener("abort", () => {
81
- u.abort(), r(new DOMException("Aborted", "AbortError"));
81
+ u.abort(), l(new DOMException("Aborted", "AbortError"));
82
82
  }), t.aborted)) {
83
- r(new DOMException("Aborted", "AbortError"));
83
+ l(new DOMException("Aborted", "AbortError"));
84
84
  return;
85
85
  }
86
86
  u.upload.addEventListener("progress", (f) => {
@@ -94,14 +94,14 @@ class M {
94
94
  const f = JSON.parse(u.responseText);
95
95
  o(f);
96
96
  } catch (f) {
97
- r(new Error(`Failed to parse response: ${f}`));
97
+ l(new Error(`Failed to parse response: ${f}`));
98
98
  }
99
99
  else
100
- r(new Error(`Upload failed with status: ${u.status}`));
100
+ l(new Error(`Upload failed with status: ${u.status}`));
101
101
  }), u.addEventListener("error", () => {
102
- r(new Error("Network error occurred"));
102
+ l(new Error("Network error occurred"));
103
103
  }), u.addEventListener("timeout", () => {
104
- r(new Error("Upload timed out"));
104
+ l(new Error("Upload timed out"));
105
105
  });
106
106
  const { baseUrl: p } = this.constructClientOptions(this.userToken), g = `${p}/backend/widget/v2/upload`;
107
107
  u.open("POST", g), u.setRequestHeader("X-Bot-Token", this.config.token), this.userToken ?? ((S = this.config.user) == null ? void 0 : S.token) ? u.setRequestHeader("Authorization", `Bearer ${this.userToken}`) : console.error("User token not set"), u.send(c);
@@ -111,10 +111,10 @@ class M {
111
111
  }), this.createStateCheckpoint = async (e) => await this.client.POST("/backend/widget/v2/checkpoint", {
112
112
  body: e
113
113
  }), this.submitCsat = async (e) => await this.client.POST("/backend/widget/v2/submit-csat", { body: e }), this.config = i, this.userToken = ((d = i.user) == null ? void 0 : d.token) || null;
114
- const { baseUrl: a, headers: l } = this.constructClientOptions(
114
+ const { baseUrl: a, headers: r } = this.constructClientOptions(
115
115
  (s = i.user) == null ? void 0 : s.token
116
116
  );
117
- this.client = this.createOpenAPIClient({ baseUrl: a, headers: l });
117
+ this.client = this.createOpenAPIClient({ baseUrl: a, headers: r });
118
118
  }
119
119
  }
120
120
  function H(h, i) {
@@ -126,8 +126,8 @@ class y {
126
126
  N(this.state, a) || (this.state = a, this.notifySubscribers(a));
127
127
  }, this.setPartial = (a) => {
128
128
  if (a == null) return;
129
- const l = { ...this.state, ...a };
130
- this.set(l);
129
+ const r = { ...this.state, ...a };
130
+ this.set(r);
131
131
  }, this.reset = () => {
132
132
  this.set(this.initialState);
133
133
  }, this.notifySubscribers = (a) => {
@@ -153,7 +153,7 @@ class O {
153
153
  this.abortController.abort("Resetting poller"), (i = this.stopPolling) == null || i.call(this), this.stopPolling = null;
154
154
  }, this.stopPolling = null, this.startPolling = (i, a) => {
155
155
  if (this.stopPolling) return;
156
- const l = [], d = async () => {
156
+ const r = [], d = async () => {
157
157
  this.abortController = new AbortController(), this.state.setPartial({ isPolling: !0 });
158
158
  try {
159
159
  await i(this.abortController.signal);
@@ -164,10 +164,10 @@ class O {
164
164
  } finally {
165
165
  this.state.setPartial({ isPolling: !1 });
166
166
  }
167
- this.abortController.signal.aborted ? console.log("Poller aborted, not scheduling anymore") : l.push(setTimeout(d, a));
167
+ this.abortController.signal.aborted ? console.log("Poller aborted, not scheduling anymore") : r.push(setTimeout(d, a));
168
168
  };
169
169
  d(), this.stopPolling = () => {
170
- l.forEach(clearTimeout), this.state.reset();
170
+ r.forEach(clearTimeout), this.state.reset();
171
171
  };
172
172
  };
173
173
  }
@@ -184,7 +184,7 @@ class q {
184
184
  constructor({
185
185
  api: i,
186
186
  config: a,
187
- sessionCtx: l,
187
+ sessionCtx: r,
188
188
  messageCtx: d,
189
189
  sessionPollingIntervalSeconds: s
190
190
  }) {
@@ -214,10 +214,10 @@ class q {
214
214
  }) => {
215
215
  var u;
216
216
  this.messageCtx.state.get().messages.length === 0 && this.messageCtx.state.setPartial({ isInitialFetchLoading: !0 });
217
- const o = this.messageCtx.state.get().messages, r = o.length > 0 ? ((u = o[o.length - 1]) == null ? void 0 : u.timestamp) ?? void 0 : void 0, { data: c } = await this.api.pollSessionAndHistory({
217
+ const o = this.messageCtx.state.get().messages, l = o.length > 0 ? ((u = o[o.length - 1]) == null ? void 0 : u.timestamp) ?? void 0 : void 0, { data: c } = await this.api.pollSessionAndHistory({
218
218
  sessionId: e,
219
219
  abortSignal: t,
220
- lastMessageTimestamp: n ? void 0 : r
220
+ lastMessageTimestamp: n ? void 0 : l
221
221
  });
222
222
  if (c != null && c.session && (this.sessionCtx.sessionState.setPartial({ session: c.session }), this.sessionCtx.setSessions([c.session])), c != null && c.history && c.history.length > 0) {
223
223
  const p = this.messageCtx.state.get().messages, w = c.history.map(this.mapHistoryToMessage).filter((g) => g !== null).filter(
@@ -229,7 +229,7 @@ class q {
229
229
  }
230
230
  this.messageCtx.state.get().isInitialFetchLoading && this.messageCtx.state.setPartial({ isInitialFetchLoading: !1 });
231
231
  }, this.mapHistoryToMessage = (e) => {
232
- var n, o;
232
+ var n;
233
233
  const t = {
234
234
  id: e.publicId,
235
235
  timestamp: e.sentAt || "",
@@ -252,13 +252,16 @@ class q {
252
252
  },
253
253
  agent: {
254
254
  name: e.sender.name || "",
255
- avatar: e.sender.avatar || "",
255
+ // Do not set avatarUrl from config here... let it be taken from the config at render time
256
+ // Only set avatar url from the backend's response
257
+ avatarUrl: e.sender.avatar || null,
258
+ avatar: e.sender.avatar || null,
256
259
  id: null,
257
260
  isAi: !1
258
261
  }
259
262
  };
260
263
  if (e.sender.kind === "ai") {
261
- const r = e.actionCalls && e.actionCalls.length > 0 ? e.actionCalls[e.actionCalls.length - 1] : void 0;
264
+ const o = e.actionCalls && e.actionCalls.length > 0 ? e.actionCalls[e.actionCalls.length - 1] : void 0;
262
265
  return {
263
266
  ...t,
264
267
  type: "AI",
@@ -267,20 +270,22 @@ class q {
267
270
  id: null,
268
271
  name: ((n = this.config.bot) == null ? void 0 : n.name) || "",
269
272
  isAi: !0,
270
- avatar: ((o = this.config.bot) == null ? void 0 : o.avatar) || ""
273
+ // Do not set avatarUrl from config here... let it be taken from the config at render time
274
+ avatarUrl: null,
275
+ avatar: null
271
276
  },
272
277
  data: {
273
278
  message: e.content.text || "",
274
- action: r ? {
275
- name: r.actionName,
276
- data: this.extractActionResult(r)
279
+ action: o ? {
280
+ name: o.actionName,
281
+ data: this.extractActionResult(o)
277
282
  } : void 0
278
283
  }
279
284
  };
280
285
  }
281
286
  if (e.sender.kind === "system") {
282
- const r = this.constructSystemMessage(e);
283
- return r === null ? null : { ...r };
287
+ const o = this.constructSystemMessage(e);
288
+ return o === null ? null : { ...o };
284
289
  }
285
290
  return null;
286
291
  }, this.constructSystemMessage = (e) => {
@@ -334,21 +339,21 @@ class q {
334
339
  if (o) return o;
335
340
  }
336
341
  return e.result;
337
- }, this.api = i, this.config = a, this.sessionCtx = l, this.messageCtx = d, this.sessionPollingIntervalSeconds = s, this.registerPolling();
342
+ }, this.api = i, this.config = a, this.sessionCtx = r, this.messageCtx = d, this.sessionPollingIntervalSeconds = s, this.registerPolling();
338
343
  }
339
344
  }
340
345
  class V {
341
346
  constructor({
342
347
  config: i,
343
348
  api: a,
344
- storageCtx: l
349
+ storageCtx: r
345
350
  }) {
346
351
  var d;
347
352
  this.shouldCollectData = () => {
348
353
  var s;
349
354
  return !!(!((s = this.state.get().contact) != null && s.token) && this.config.collectUserData);
350
355
  }, this.autoCreateUnverifiedUserIfNotExists = async () => {
351
- var s, e, t, n, o, r, c, u, p, w, g, C, S, f;
356
+ var s, e, t, n, o, l, c, u, p, w, g, C, S, f;
352
357
  if (!((s = this.config.user) != null && s.token)) {
353
358
  if (this.config.collectUserData && !((t = (e = this.config.user) == null ? void 0 : e.data) != null && t.email)) {
354
359
  if ((n = this.config.extraDataCollectionFields) != null && n.length)
@@ -357,7 +362,7 @@ class V {
357
362
  v && await this.setUnverifiedContact(v);
358
363
  return;
359
364
  }
360
- if (!((c = (r = this.config.user) == null ? void 0 : r.data) != null && c.email)) {
365
+ if (!((c = (l = this.config.user) == null ? void 0 : l.data) != null && c.email)) {
361
366
  const v = await ((u = this.storageCtx) == null ? void 0 : u.getContactToken());
362
367
  if (v) {
363
368
  await this.setUnverifiedContact(v);
@@ -383,10 +388,10 @@ class V {
383
388
  this.state.setPartial({ isCreatingUnverifiedContact: !1 });
384
389
  }
385
390
  }, this.setUnverifiedContact = async (s) => {
386
- var n, o, r, c;
391
+ var n, o, l, c;
387
392
  const e = await ((n = this.storageCtx) == null ? void 0 : n.getExternalContactId()), t = ((o = this.config.user) == null ? void 0 : o.externalId) || e || z();
388
- this.api.setAuthToken(s), await ((r = this.storageCtx) == null ? void 0 : r.setContactToken(s)), await ((c = this.storageCtx) == null ? void 0 : c.setExternalContactId(t)), this.state.setPartial({ contact: { token: s, externalId: t } });
389
- }, this.config = i, this.storageCtx = l, this.api = a, this.state = new y({
393
+ this.api.setAuthToken(s), await ((l = this.storageCtx) == null ? void 0 : l.setContactToken(s)), await ((c = this.storageCtx) == null ? void 0 : c.setExternalContactId(t)), this.state.setPartial({ contact: { token: s, externalId: t } });
394
+ }, this.config = i, this.storageCtx = r, this.api = a, this.state = new y({
390
395
  contact: (d = i.user) != null && d.token ? {
391
396
  token: i.user.token,
392
397
  // Set optional externalId from config... not local storage
@@ -405,12 +410,12 @@ class Y {
405
410
  constructor({
406
411
  config: i,
407
412
  api: a,
408
- sessionCtx: l,
413
+ sessionCtx: r,
409
414
  messageCtx: d
410
415
  }) {
411
416
  this.submitCsat = async (s) => {
412
- var r;
413
- const e = (r = this.sessionCtx.sessionState.get().session) == null ? void 0 : r.id;
417
+ var l;
418
+ const e = (l = this.sessionCtx.sessionState.get().session) == null ? void 0 : l.id;
414
419
  if (!e)
415
420
  return { data: null, error: "No session id found" };
416
421
  const t = k();
@@ -437,14 +442,14 @@ class Y {
437
442
  session_id: e
438
443
  });
439
444
  return { data: n, error: o };
440
- }, this.config = i, this.api = a, this.sessionCtx = l, this.messageCtx = d;
445
+ }, this.config = i, this.api = a, this.sessionCtx = r, this.messageCtx = d;
441
446
  }
442
447
  }
443
448
  class $ {
444
449
  constructor({
445
450
  config: i,
446
451
  api: a,
447
- contactCtx: l,
452
+ contactCtx: r,
448
453
  sessionsPollingIntervalSeconds: d
449
454
  }) {
450
455
  this.sessionsRefresher = new O(), this.sessionState = new y({
@@ -496,7 +501,7 @@ class $ {
496
501
  });
497
502
  if (s) {
498
503
  const t = [...this.sessionsState.get().data, ...s.items].filter(
499
- (n, o, r) => o === r.findIndex((c) => n.id === c.id)
504
+ (n, o, l) => o === l.findIndex((c) => n.id === c.id)
500
505
  );
501
506
  this.sessionsState.setPartial({
502
507
  data: t,
@@ -516,7 +521,7 @@ class $ {
516
521
  });
517
522
  }, this.setSessions = (s) => {
518
523
  const e = [...s, ...this.sessionsState.get().data].filter(
519
- (t, n, o) => n === o.findIndex((r) => t.id === r.id)
524
+ (t, n, o) => n === o.findIndex((l) => t.id === l.id)
520
525
  );
521
526
  this.sessionsState.setPartial({ data: e });
522
527
  }, this.refreshSessions = async () => {
@@ -540,14 +545,14 @@ class $ {
540
545
  payload: s
541
546
  });
542
547
  return t != null && t.success ? { success: !0 } : { success: !1 };
543
- }, this.config = i, this.api = a, this.contactCtx = l, this.sessionsPollingIntervalSeconds = d, this.registerSessionsRefresherWrapper();
548
+ }, this.config = i, this.api = a, this.contactCtx = r, this.sessionsPollingIntervalSeconds = d, this.registerSessionsRefresherWrapper();
544
549
  }
545
550
  }
546
551
  class K {
547
552
  constructor({
548
553
  config: i,
549
554
  api: a,
550
- sessionCtx: l,
555
+ sessionCtx: r,
551
556
  contactCtx: d
552
557
  }) {
553
558
  this.state = new y({
@@ -559,7 +564,7 @@ class K {
559
564
  }), this.sendMessageAbortController = new AbortController(), this.reset = () => {
560
565
  this.sendMessageAbortController.abort("Resetting chat"), this.state.reset();
561
566
  }, this.sendMessage = async (s) => {
562
- var e, t, n, o, r, c, u, p, w;
567
+ var e, t, n, o, l, c, u, p, w;
563
568
  try {
564
569
  if (!s.content.trim() && (!s.attachments || s.attachments.length === 0)) {
565
570
  console.warn(
@@ -640,7 +645,7 @@ class K {
640
645
  (j) => j.id === _.id
641
646
  )) {
642
647
  this.state.setPartial({
643
- lastAIResMightSolveUserIssue: ((r = m.autopilotResponse) == null ? void 0 : r.mightSolveUserIssue) || ((c = m.uiResponse) == null ? void 0 : c.mightSolveUserIssue)
648
+ lastAIResMightSolveUserIssue: ((l = m.autopilotResponse) == null ? void 0 : l.mightSolveUserIssue) || ((c = m.uiResponse) == null ? void 0 : c.mightSolveUserIssue)
644
649
  });
645
650
  return;
646
651
  }
@@ -669,7 +674,7 @@ class K {
669
674
  }, this.toUserMessage = (s, e) => {
670
675
  const t = (() => {
671
676
  const n = this.contactCtx.state.get().extraCollectedData;
672
- return this.state.get().messages.length === 0 && n && Object.keys(n).length > 0 ? `${Object.entries(n).filter(([r, c]) => !!c).map(([r, c]) => `${r}: ${c}`).join(`
677
+ return this.state.get().messages.length === 0 && n && Object.keys(n).length > 0 ? `${Object.entries(n).filter(([l, c]) => !!c).map(([l, c]) => `${l}: ${c}`).join(`
673
678
  `)}
674
679
 
675
680
  ${s}` : s;
@@ -692,7 +697,9 @@ ${s}` : s;
692
697
  agent: this.config.bot ? {
693
698
  name: this.config.bot.name || "",
694
699
  isAi: !0,
695
- avatar: this.config.bot.avatar || "",
700
+ // Do not set avatarUrl here... let it be taken from the config at render time
701
+ avatarUrl: null,
702
+ avatar: null,
696
703
  id: null
697
704
  } : void 0,
698
705
  data: {
@@ -713,14 +720,14 @@ ${s}` : s;
713
720
  variant: "error",
714
721
  action: void 0
715
722
  }
716
- }), this.config = i, this.api = a, this.sessionCtx = l, this.contactCtx = d;
723
+ }), this.config = i, this.api = a, this.sessionCtx = r, this.contactCtx = d;
717
724
  }
718
725
  }
719
726
  class G {
720
727
  constructor({
721
728
  config: i,
722
729
  contactCtx: a,
723
- sessionCtx: l,
730
+ sessionCtx: r,
724
731
  resetChat: d
725
732
  }) {
726
733
  var s;
@@ -732,11 +739,11 @@ class G {
732
739
  });
733
740
  }), this.sessionCtx.sessionsState.subscribe(
734
741
  ({ isInitialFetchLoading: e, data: t }) => {
735
- var n, o, r, c;
742
+ var n, o, l, c;
736
743
  if ((n = this.config.router) != null && n.chatScreenOnly && // Do not route to a chat if we are currently inside one already
737
744
  // This also applies to newly created sessions; the new session will be in `sessionState` before it is refreshed and included in `sessionsState`
738
745
  !((o = this.sessionCtx.sessionState.get().session) != null && o.id)) {
739
- const u = (r = t.find((p) => p.isOpened)) == null ? void 0 : r.id;
746
+ const u = (l = t.find((p) => p.isOpened)) == null ? void 0 : l.id;
740
747
  return u ? this.toChatScreen(u) : void 0;
741
748
  }
742
749
  t.length || ((c = this.config.router) == null ? void 0 : c.goToChatIfNoSessions) !== !1 && !e && this.state.get().screen !== "chat" && this.toChatScreen();
@@ -751,7 +758,7 @@ class G {
751
758
  this.sessionCtx.sessionState.setPartial({ session: t });
752
759
  }
753
760
  this.state.setPartial({ screen: "chat" });
754
- }, this.config = i, this.contactCtx = a, this.sessionCtx = l, this.resetChat = d, this.state = new y({
761
+ }, this.config = i, this.contactCtx = a, this.sessionCtx = r, this.resetChat = d, this.state = new y({
755
762
  screen: this.contactCtx.shouldCollectData() ? "welcome" : (s = this.config.router) != null && s.chatScreenOnly ? "chat" : "sessions"
756
763
  }), this.registerRoutingListener();
757
764
  }
@@ -762,12 +769,12 @@ class J {
762
769
  config: a
763
770
  }) {
764
771
  this.KEYS = {
765
- contactToken: (l) => `opencx-widget:org-token-${l}:contact-token`,
766
- externalContactId: (l) => `opencx-widget:org-token-${l}:external-contact-id`
767
- }, this.setContactToken = async (l) => {
768
- await this.storage.set(this.KEYS.contactToken(this.config.token), l);
769
- }, this.getContactToken = async () => this.storage.get(this.KEYS.contactToken(this.config.token)), this.setExternalContactId = async (l) => {
770
- await this.storage.set(this.KEYS.externalContactId(this.config.token), l);
772
+ contactToken: (r) => `opencx-widget:org-token-${r}:contact-token`,
773
+ externalContactId: (r) => `opencx-widget:org-token-${r}:external-contact-id`
774
+ }, this.setContactToken = async (r) => {
775
+ await this.storage.set(this.KEYS.contactToken(this.config.token), r);
776
+ }, this.getContactToken = async () => this.storage.get(this.KEYS.contactToken(this.config.token)), this.setExternalContactId = async (r) => {
777
+ await this.storage.set(this.KEYS.externalContactId(this.config.token), r);
771
778
  }, this.getExternalContactId = async () => this.storage.get(this.KEYS.externalContactId(this.config.token)), this.storage = i, this.config = a;
772
779
  }
773
780
  }
@@ -775,7 +782,7 @@ const b = class b {
775
782
  constructor({
776
783
  config: i,
777
784
  storage: a,
778
- modes: l,
785
+ modes: r,
779
786
  org: d
780
787
  }) {
781
788
  if (this.modes = [], this.resetChat = () => {
@@ -784,7 +791,7 @@ const b = class b {
784
791
  throw Error(
785
792
  "Widget polling values are not defined, did you call WidgetCtx.initialize()"
786
793
  );
787
- this.config = i, this.org = d, this.api = new M({ config: i }), this.storageCtx = a ? new J({ storage: a, config: i }) : void 0, this.modes = l, this.contactCtx = new V({
794
+ this.config = i, this.org = d, this.api = new M({ config: i }), this.storageCtx = a ? new J({ storage: a, config: i }) : void 0, this.modes = r, this.contactCtx = new V({
788
795
  api: this.api,
789
796
  config: this.config,
790
797
  storageCtx: this.storageCtx
@@ -822,21 +829,21 @@ b.pollingIntervalsSeconds = null, b.initialize = async ({
822
829
  storage: a
823
830
  }) => {
824
831
  var d, s, e;
825
- const l = await new M({
832
+ const r = await new M({
826
833
  config: i
827
834
  }).getExternalWidgetConfig();
828
- if (!l.data)
835
+ if (!r.data)
829
836
  throw new Error("Failed to fetch widget config");
830
837
  return b.pollingIntervalsSeconds = {
831
- session: ((d = l.data) == null ? void 0 : d.sessionPollingIntervalSeconds) || 10,
832
- sessions: ((s = l.data) == null ? void 0 : s.sessionsPollingIntervalSeconds) || 60
838
+ session: ((d = r.data) == null ? void 0 : d.sessionPollingIntervalSeconds) || 10,
839
+ sessions: ((s = r.data) == null ? void 0 : s.sessionsPollingIntervalSeconds) || 60
833
840
  }, new b({
834
841
  config: i,
835
842
  storage: a,
836
- modes: ((e = l.data) == null ? void 0 : e.modes) || [],
843
+ modes: ((e = r.data) == null ? void 0 : e.modes) || [],
837
844
  org: {
838
- id: l.data.org.id,
839
- name: l.data.org.name
845
+ id: r.data.org.id,
846
+ name: r.data.org.name
840
847
  }
841
848
  });
842
849
  };
@@ -1102,8 +1109,8 @@ function pe(h) {
1102
1109
  return de.includes(h);
1103
1110
  }
1104
1111
  function fe(h, i, a) {
1105
- var l;
1106
- return ((l = a == null ? void 0 : a[i]) == null ? void 0 : l[h]) || D[i][h] || "";
1112
+ var r;
1113
+ return ((r = a == null ? void 0 : a[i]) == null ? void 0 : r[h]) || D[i][h] || "";
1107
1114
  }
1108
1115
  export {
1109
1116
  y as PrimitiveState,