@opencx/widget 3.0.93 → 3.0.95

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.
@@ -1,21 +1,21 @@
1
1
  import M from "openapi-fetch";
2
2
  import O from "lodash.isequal";
3
3
  import { v4 as E } from "uuid";
4
- const F = (g) => {
5
- console.log(g.error);
6
- }, _ = (g) => {
4
+ const F = (d) => {
5
+ console.log(d.error);
6
+ }, _ = (d) => {
7
7
  const a = M({
8
- baseUrl: g.baseUrl
8
+ baseUrl: d.baseUrl
9
9
  }), o = {
10
- onRequest: g.onRequest,
11
- onResponse: g.onResponse,
12
- onError: g.onError || F
10
+ onRequest: d.onRequest,
11
+ onResponse: d.onResponse,
12
+ onError: d.onError || F
13
13
  };
14
14
  return a.use(o), a;
15
15
  };
16
16
  class T {
17
17
  constructor({ config: a }) {
18
- var n, e;
18
+ var g, 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,
@@ -30,8 +30,8 @@ class T {
30
30
  }) => _({
31
31
  baseUrl: t,
32
32
  onRequest: ({ request: i }) => {
33
- Object.entries(s).forEach(([c, r]) => {
34
- r && i.headers.set(c, r);
33
+ Object.entries(s).forEach(([r, n]) => {
34
+ n && i.headers.set(r, n);
35
35
  });
36
36
  }
37
37
  }), this.setAuthToken = (t) => {
@@ -58,9 +58,9 @@ class T {
58
58
  lastMessageTimestamp: s,
59
59
  abortSignal: i
60
60
  }) => {
61
- const c = s ? { lastMessageTimestamp: s } : void 0;
61
+ const r = s ? { lastMessageTimestamp: s } : void 0;
62
62
  return await this.client.GET("/backend/widget/v2/poll/{sessionId}", {
63
- params: { path: { sessionId: t }, query: c },
63
+ params: { path: { sessionId: t }, query: r },
64
64
  signal: i
65
65
  });
66
66
  }, this.getSessions = async ({
@@ -74,49 +74,49 @@ class T {
74
74
  file: t,
75
75
  abortSignal: s,
76
76
  onProgress: i
77
- }) => new Promise((c, r) => {
77
+ }) => new Promise((r, n) => {
78
78
  var v;
79
- const d = new FormData();
80
- d.append("file", t);
81
- const l = new XMLHttpRequest();
79
+ const h = new FormData();
80
+ h.append("file", t);
81
+ const c = new XMLHttpRequest();
82
82
  if (s && (s.addEventListener("abort", () => {
83
- l.abort(), r(new DOMException("Aborted", "AbortError"));
83
+ c.abort(), n(new DOMException("Aborted", "AbortError"));
84
84
  }), s.aborted)) {
85
- r(new DOMException("Aborted", "AbortError"));
85
+ n(new DOMException("Aborted", "AbortError"));
86
86
  return;
87
87
  }
88
- l.upload.addEventListener("progress", (u) => {
88
+ c.upload.addEventListener("progress", (u) => {
89
89
  if (u.lengthComputable && i) {
90
90
  const S = Math.round(u.loaded / u.total * 100);
91
91
  i(S);
92
92
  }
93
- }), l.addEventListener("load", () => {
94
- if (l.status >= 200 && l.status < 300)
93
+ }), c.addEventListener("load", () => {
94
+ if (c.status >= 200 && c.status < 300)
95
95
  try {
96
- const u = JSON.parse(l.responseText);
97
- c(u);
96
+ const u = JSON.parse(c.responseText);
97
+ r(u);
98
98
  } catch (u) {
99
- r(new Error(`Failed to parse response: ${u}`));
99
+ n(new Error(`Failed to parse response: ${u}`));
100
100
  }
101
101
  else
102
- r(new Error(`Upload failed with status: ${l.status}`));
103
- }), l.addEventListener("error", () => {
104
- r(new Error("Network error occurred"));
105
- }), l.addEventListener("timeout", () => {
106
- r(new Error("Upload timed out"));
102
+ n(new Error(`Upload failed with status: ${c.status}`));
103
+ }), c.addEventListener("error", () => {
104
+ n(new Error("Network error occurred"));
105
+ }), c.addEventListener("timeout", () => {
106
+ n(new Error("Upload timed out"));
107
107
  });
108
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(d);
109
+ c.open("POST", p), c.setRequestHeader("X-Bot-Token", this.config.token), this.userToken ?? ((v = this.config.user) == null ? void 0 : v.token) ? c.setRequestHeader("Authorization", `Bearer ${this.userToken}`) : console.error("User token not set"), c.send(h);
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.createStateCheckpoint = async (t) => await this.client.POST("/backend/widget/v2/checkpoint", {
114
114
  body: t
115
- }), this.config = a, this.userToken = ((n = a.user) == null ? void 0 : n.token) || null;
116
- const { baseUrl: o, headers: h } = this.constructClientOptions(
115
+ }), this.config = a, this.userToken = ((g = a.user) == null ? void 0 : g.token) || null;
116
+ const { baseUrl: o, headers: l } = this.constructClientOptions(
117
117
  (e = a.user) == null ? void 0 : e.token
118
118
  );
119
- this.client = this.createOpenAPIClient({ baseUrl: o, headers: h });
119
+ this.client = this.createOpenAPIClient({ baseUrl: o, headers: l });
120
120
  }
121
121
  }
122
122
  class P {
@@ -125,14 +125,14 @@ class P {
125
125
  O(this.state, o) || (this.state = o, this.notifySubscribers(o));
126
126
  }, this.setPartial = (o) => {
127
127
  if (o == null) return;
128
- const h = { ...this.state, ...o };
129
- this.set(h);
128
+ const l = { ...this.state, ...o };
129
+ this.set(l);
130
130
  }, this.reset = () => {
131
131
  this.set(this.initialState);
132
132
  }, this.notifySubscribers = (o) => {
133
- Array.from(this.subscribers).forEach((n) => {
133
+ Array.from(this.subscribers).forEach((g) => {
134
134
  try {
135
- n(o);
135
+ g(o);
136
136
  } catch (e) {
137
137
  console.error(e);
138
138
  }
@@ -152,7 +152,7 @@ class R {
152
152
  this.abortController.abort("Resetting poller"), (a = this.stopPolling) == null || a.call(this), this.stopPolling = null;
153
153
  }, this.stopPolling = null, this.startPolling = (a, o) => {
154
154
  if (this.stopPolling) return;
155
- const h = [], n = async () => {
155
+ const l = [], g = async () => {
156
156
  this.abortController = new AbortController(), this.state.setPartial({ isPolling: !0 });
157
157
  try {
158
158
  await a(this.abortController.signal);
@@ -163,20 +163,20 @@ class R {
163
163
  } finally {
164
164
  this.state.setPartial({ isPolling: !1 });
165
165
  }
166
- this.abortController.signal.aborted ? console.log("Poller aborted, not scheduling anymore") : h.push(setTimeout(n, o));
166
+ this.abortController.signal.aborted ? console.log("Poller aborted, not scheduling anymore") : l.push(setTimeout(g, o));
167
167
  };
168
- n(), this.stopPolling = () => {
169
- h.forEach(clearTimeout), this.state.reset();
168
+ g(), this.stopPolling = () => {
169
+ l.forEach(clearTimeout), this.state.reset();
170
170
  };
171
171
  };
172
172
  }
173
173
  }
174
- function z(g) {
175
- return g();
174
+ function z(d) {
175
+ return d();
176
176
  }
177
- function D(g) {
177
+ function D(d) {
178
178
  try {
179
- const a = g();
179
+ const a = d();
180
180
  return a instanceof Promise ? a.then((o) => ({ data: o })).catch((o) => ({ error: o })) : { data: a };
181
181
  } catch (a) {
182
182
  return { error: a };
@@ -186,8 +186,8 @@ class L {
186
186
  constructor({
187
187
  api: a,
188
188
  config: o,
189
- sessionCtx: h,
190
- messageCtx: n,
189
+ sessionCtx: l,
190
+ messageCtx: g,
191
191
  sessionPollingIntervalSeconds: e
192
192
  }) {
193
193
  this.poller = new R(), this.registerPolling = () => {
@@ -197,24 +197,24 @@ class L {
197
197
  }, this.sessionPollingIntervalSeconds * 1e3) : this.poller.reset();
198
198
  });
199
199
  }, this.hackAndSlash = async (t, s) => {
200
- var d;
200
+ var h;
201
201
  this.messageCtx.state.get().messages.length === 0 && this.messageCtx.state.setPartial({ isInitialFetchLoading: !0 });
202
- const i = this.messageCtx.state.get().messages, c = i.length > 0 ? (d = i[i.length - 1]) == null ? void 0 : d.timestamp : void 0, { data: r } = await this.api.pollSessionAndHistory({
202
+ const i = this.messageCtx.state.get().messages, r = i.length > 0 ? (h = i[i.length - 1]) == null ? void 0 : h.timestamp : void 0, { data: n } = await this.api.pollSessionAndHistory({
203
203
  sessionId: t,
204
204
  abortSignal: s,
205
- lastMessageTimestamp: c
205
+ lastMessageTimestamp: r
206
206
  });
207
- 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) {
208
- const l = this.messageCtx.state.get().messages, C = r.history.map(this.mapHistoryToMessage).filter(
209
- (x) => !l.some((p) => p.id === x.id)
207
+ if (n != null && n.session && (this.sessionCtx.sessionState.setPartial({ session: n.session }), this.sessionCtx.setSessions([n.session])), n != null && n.history && n.history.length > 0) {
208
+ const c = this.messageCtx.state.get().messages, C = n.history.map(this.mapHistoryToMessage).filter(
209
+ (x) => !c.some((p) => p.id === x.id)
210
210
  );
211
211
  this.messageCtx.state.setPartial({
212
- messages: [...l, ...C]
212
+ messages: [...c, ...C]
213
213
  });
214
214
  }
215
215
  this.messageCtx.state.get().isInitialFetchLoading && this.messageCtx.state.setPartial({ isInitialFetchLoading: !1 });
216
216
  }, this.mapHistoryToMessage = (t) => {
217
- var c, r;
217
+ var r, n;
218
218
  const s = {
219
219
  id: t.publicId,
220
220
  timestamp: t.sentAt || "",
@@ -249,9 +249,9 @@ class L {
249
249
  component: "bot_message",
250
250
  agent: {
251
251
  id: null,
252
- name: ((c = this.config.bot) == null ? void 0 : c.name) || "",
252
+ name: ((r = this.config.bot) == null ? void 0 : r.name) || "",
253
253
  isAi: !0,
254
- avatar: ((r = this.config.bot) == null ? void 0 : r.avatar) || ""
254
+ avatar: ((n = this.config.bot) == null ? void 0 : n.avatar) || ""
255
255
  },
256
256
  data: {
257
257
  message: t.content.text || "",
@@ -265,35 +265,35 @@ class L {
265
265
  const s = t.result;
266
266
  if (s === null || typeof s != "object") return s;
267
267
  if ("responseBodyText" in s && typeof s.responseBodyText == "string") {
268
- const i = s.responseBodyText, c = D(() => JSON.parse(i)).data;
269
- if (c) return c;
268
+ const i = s.responseBodyText, r = D(() => JSON.parse(i)).data;
269
+ if (r) return r;
270
270
  }
271
271
  return t.result;
272
- }, this.api = a, this.config = o, this.sessionCtx = h, this.messageCtx = n, this.sessionPollingIntervalSeconds = e, this.registerPolling();
272
+ }, this.api = a, this.config = o, this.sessionCtx = l, this.messageCtx = g, this.sessionPollingIntervalSeconds = e, this.registerPolling();
273
273
  }
274
274
  }
275
275
  class B {
276
276
  constructor({
277
277
  config: a,
278
278
  api: o,
279
- storageCtx: h
279
+ storageCtx: l
280
280
  }) {
281
- var n;
281
+ var g;
282
282
  this.shouldCollectData = () => {
283
283
  var e;
284
284
  return !!(!((e = this.state.get().contact) != null && e.token) && this.config.collectUserData);
285
285
  }, this.autoCreateUnverifiedUserIfNotExists = async () => {
286
- var e, t, s, i, c, r, d, l, C, x, p, w, v, u;
286
+ var e, t, s, i, r, n, h, c, C, x, p, b, v, u;
287
287
  if (!((e = this.config.user) != null && e.token)) {
288
288
  if (this.config.collectUserData && !((s = (t = this.config.user) == null ? void 0 : t.data) != null && s.email)) {
289
289
  if ((i = this.config.extraDataCollectionFields) != null && i.length)
290
290
  return;
291
- const S = await ((c = this.storageCtx) == null ? void 0 : c.getContactToken());
291
+ const S = await ((r = this.storageCtx) == null ? void 0 : r.getContactToken());
292
292
  S && await this.setUnverifiedContact(S);
293
293
  return;
294
294
  }
295
- if (!((d = (r = this.config.user) == null ? void 0 : r.data) != null && d.email)) {
296
- const S = await ((l = this.storageCtx) == null ? void 0 : l.getContactToken());
295
+ if (!((h = (n = this.config.user) == null ? void 0 : n.data) != null && h.email)) {
296
+ const S = await ((c = this.storageCtx) == null ? void 0 : c.getContactToken());
297
297
  if (S) {
298
298
  await this.setUnverifiedContact(S);
299
299
  return;
@@ -301,7 +301,7 @@ class B {
301
301
  }
302
302
  await this.createUnverifiedContact({
303
303
  email: (x = (C = this.config.user) == null ? void 0 : C.data) == null ? void 0 : x.email,
304
- non_verified_name: ((w = (p = this.config.user) == null ? void 0 : p.data) == null ? void 0 : w.name) || "Anonymous",
304
+ non_verified_name: ((b = (p = this.config.user) == null ? void 0 : p.data) == null ? void 0 : b.name) || "Anonymous",
305
305
  non_verified_custom_data: (u = (v = this.config.user) == null ? void 0 : v.data) == null ? void 0 : u.customData
306
306
  });
307
307
  }
@@ -318,11 +318,11 @@ class B {
318
318
  this.state.setPartial({ isCreatingUnverifiedContact: !1 });
319
319
  }
320
320
  }, this.setUnverifiedContact = async (e) => {
321
- var i, c, r, d;
322
- const t = await ((i = this.storageCtx) == null ? void 0 : i.getExternalContactId()), s = ((c = this.config.user) == null ? void 0 : c.externalId) || t || E();
323
- this.api.setAuthToken(e), await ((r = this.storageCtx) == null ? void 0 : r.setContactToken(e)), await ((d = this.storageCtx) == null ? void 0 : d.setExternalContactId(s)), this.state.setPartial({ contact: { token: e, externalId: s } });
324
- }, this.config = a, this.storageCtx = h, this.api = o, this.state = new P({
325
- contact: (n = a.user) != null && n.token ? {
321
+ var i, r, n, h;
322
+ const t = await ((i = this.storageCtx) == null ? void 0 : i.getExternalContactId()), s = ((r = this.config.user) == null ? void 0 : r.externalId) || t || E();
323
+ this.api.setAuthToken(e), await ((n = this.storageCtx) == null ? void 0 : n.setContactToken(e)), await ((h = this.storageCtx) == null ? void 0 : h.setExternalContactId(s)), this.state.setPartial({ contact: { token: e, externalId: s } });
324
+ }, this.config = a, this.storageCtx = l, this.api = o, this.state = new P({
325
+ contact: (g = a.user) != null && g.token ? {
326
326
  token: a.user.token,
327
327
  // Set optional externalId from config... not local storage
328
328
  externalId: a.user.externalId
@@ -338,9 +338,10 @@ function y() {
338
338
  }
339
339
  class q {
340
340
  constructor({
341
- api: a,
342
- contactCtx: o,
343
- sessionsPollingIntervalSeconds: h
341
+ config: a,
342
+ api: o,
343
+ contactCtx: l,
344
+ sessionsPollingIntervalSeconds: g
344
345
  }) {
345
346
  this.sessionsRefresher = new R(), this.sessionState = new P({
346
347
  session: null,
@@ -358,84 +359,85 @@ class q {
358
359
  }), this.reset = async () => {
359
360
  this.sessionState.reset();
360
361
  }, this.registerSessionsRefresherWrapper = () => {
361
- var n;
362
+ var e;
362
363
  // If the widget config was initially provided with a contact token, no state change would be triggered, so we just fetch
363
- (n = this.contactCtx.state.get().contact) != null && n.token && !this.sessionsState.get().didStartInitialFetch ? this.registerSessionsRefresher() : this.contactCtx.state.subscribe(({ contact: e }) => {
364
- e != null && e.token && !this.sessionsState.get().didStartInitialFetch && this.registerSessionsRefresher();
364
+ (e = this.contactCtx.state.get().contact) != null && e.token && !this.sessionsState.get().didStartInitialFetch ? this.registerSessionsRefresher() : this.contactCtx.state.subscribe(({ contact: t }) => {
365
+ t != null && t.token && !this.sessionsState.get().didStartInitialFetch && this.registerSessionsRefresher();
365
366
  });
366
367
  }, this.registerSessionsRefresher = () => {
367
368
  this.sessionsRefresher.startPolling(async () => {
368
369
  this.sessionsState.get().didStartInitialFetch === !1 && this.sessionsState.setPartial({ didStartInitialFetch: !0 }), await this.refreshSessions(), this.sessionsState.get().isInitialFetchLoading === !0 && this.sessionsState.setPartial({ isInitialFetchLoading: !1 });
369
370
  }, this.sessionsPollingIntervalSeconds * 1e3);
370
371
  }, this.createSession = async () => {
371
- var s;
372
+ var r;
372
373
  this.sessionState.setPartial({ session: null, isCreatingSession: !0 });
373
- const n = (s = this.contactCtx.state.get().contact) == null ? void 0 : s.externalId, { data: e, error: t } = await this.api.createSession({
374
- customData: n ? {
375
- external_id: n
376
- } : void 0
374
+ const e = (r = this.contactCtx.state.get().contact) == null ? void 0 : r.externalId, t = {
375
+ ...this.config.sessionCustomData,
376
+ ...e ? { external_id: e } : {}
377
+ }, { data: s, error: i } = await this.api.createSession({
378
+ customData: Object.keys(t).length > 0 ? t : void 0
377
379
  });
378
- return e ? (this.sessionState.setPartial({ session: e, isCreatingSession: !1 }), e) : (this.sessionState.setPartial({ isCreatingSession: !1 }), console.error("Failed to create session:", t), null);
380
+ return s ? (this.sessionState.setPartial({ session: s, isCreatingSession: !1 }), s) : (this.sessionState.setPartial({ isCreatingSession: !1 }), console.error("Failed to create session:", i), null);
379
381
  }, this.loadMoreSessions = async () => {
380
382
  if (this.sessionsState.get().isLastPage) return;
381
- const { data: n } = await this.getSessions({
383
+ const { data: e } = await this.getSessions({
382
384
  cursor: this.sessionsState.get().cursor
383
385
  });
384
- if (n) {
385
- const t = [...this.sessionsState.get().data, ...n.items].filter(
386
- (s, i, c) => i === c.findIndex((r) => s.id === r.id)
386
+ if (e) {
387
+ const s = [...this.sessionsState.get().data, ...e.items].filter(
388
+ (i, r, n) => r === n.findIndex((h) => i.id === h.id)
387
389
  );
388
390
  this.sessionsState.setPartial({
389
- data: t,
390
- cursor: n.next || void 0,
391
- isLastPage: n.next === null
391
+ data: s,
392
+ cursor: e.next || void 0,
393
+ isLastPage: e.next === null
392
394
  });
393
395
  }
394
- }, this.getSessions = async ({ cursor: n }) => {
395
- var t, s;
396
- if (!((t = this.contactCtx.state.get().contact) != null && t.token)) return { data: null };
397
- const e = (s = this.contactCtx.state.get().contact) == null ? void 0 : s.externalId;
396
+ }, this.getSessions = async ({ cursor: e }) => {
397
+ var s, i;
398
+ if (!((s = this.contactCtx.state.get().contact) != null && s.token)) return { data: null };
399
+ const t = (i = this.contactCtx.state.get().contact) == null ? void 0 : i.externalId;
398
400
  return await this.api.getSessions({
399
- cursor: n,
400
- filters: e ? {
401
- external_id: e
401
+ cursor: e,
402
+ filters: t ? {
403
+ external_id: t
402
404
  } : {}
403
405
  });
404
- }, this.setSessions = (n) => {
405
- const e = [...n, ...this.sessionsState.get().data].filter(
406
- (t, s, i) => s === i.findIndex((c) => t.id === c.id)
406
+ }, this.setSessions = (e) => {
407
+ const t = [...e, ...this.sessionsState.get().data].filter(
408
+ (s, i, r) => i === r.findIndex((n) => s.id === n.id)
407
409
  );
408
- this.sessionsState.setPartial({ data: e });
410
+ this.sessionsState.setPartial({ data: t });
409
411
  }, this.refreshSessions = async () => {
410
- const { data: n } = await this.getSessions({ cursor: void 0 });
411
- n && this.setSessions(n.items);
412
+ const { data: e } = await this.getSessions({ cursor: void 0 });
413
+ e && this.setSessions(e.items);
412
414
  }, this.resolveSession = async () => {
413
- const n = this.sessionState.get().session;
414
- if (!n || !n.isOpened)
415
+ const e = this.sessionState.get().session;
416
+ if (!e || !e.isOpened)
415
417
  return { success: !1, error: "Session is not opened" };
416
418
  this.sessionState.setPartial({ isResolvingSession: !0 });
417
- const { data: e, error: t } = await this.api.resolveSession({
418
- session_id: n.id
419
+ const { data: t, error: s } = await this.api.resolveSession({
420
+ session_id: e.id
419
421
  });
420
- return e ? (this.sessionState.setPartial({ session: e, isResolvingSession: !1 }), { success: !0, data: e }) : (this.sessionState.setPartial({ isResolvingSession: !1 }), { success: !1, error: t });
421
- }, this.createStateCheckpoint = async (n) => {
422
- var i;
423
- const e = (i = this.sessionState.get().session) == null ? void 0 : i.id;
424
- if (!e) return;
425
- const { data: t, error: s } = await this.api.createStateCheckpoint({
426
- session_id: e,
427
- payload: n
422
+ return t ? (this.sessionState.setPartial({ session: t, isResolvingSession: !1 }), { success: !0, data: t }) : (this.sessionState.setPartial({ isResolvingSession: !1 }), { success: !1, error: s });
423
+ }, this.createStateCheckpoint = async (e) => {
424
+ var r;
425
+ const t = (r = this.sessionState.get().session) == null ? void 0 : r.id;
426
+ if (!t) return;
427
+ const { data: s, error: i } = await this.api.createStateCheckpoint({
428
+ session_id: t,
429
+ payload: e
428
430
  });
429
- return t ? { data: t } : { success: !1, error: s };
430
- }, this.api = a, this.contactCtx = o, this.sessionsPollingIntervalSeconds = h, this.registerSessionsRefresherWrapper();
431
+ return s ? { data: s } : { success: !1, error: i };
432
+ }, this.config = a, this.api = o, this.contactCtx = l, this.sessionsPollingIntervalSeconds = g, this.registerSessionsRefresherWrapper();
431
433
  }
432
434
  }
433
435
  class $ {
434
436
  constructor({
435
437
  config: a,
436
438
  api: o,
437
- sessionCtx: h,
438
- contactCtx: n
439
+ sessionCtx: l,
440
+ contactCtx: g
439
441
  }) {
440
442
  this.state = new P({
441
443
  messages: [],
@@ -445,14 +447,14 @@ class $ {
445
447
  }), this.sendMessageAbortController = new AbortController(), this.reset = () => {
446
448
  this.sendMessageAbortController.abort("Resetting chat"), this.state.reset();
447
449
  }, this.sendMessage = async (e) => {
448
- var r, d, l, C, x, p, w, v;
450
+ var n, h, c, C, x, p, b, v;
449
451
  if (!e.content.trim() && (!e.attachments || e.attachments.length === 0)) {
450
452
  console.warn("Cannot send an empty message of no content or attachments");
451
453
  return;
452
454
  }
453
- const t = this.state.get().isSendingMessage, s = ((r = this.sessionCtx.sessionState.get().session) == null ? void 0 : r.assignee.kind) === "ai", i = this.state.get().messages, c = i.length > 0 ? i[i.length - 1] : void 0;
455
+ const t = this.state.get().isSendingMessage, s = ((n = this.sessionCtx.sessionState.get().session) == null ? void 0 : n.assignee.kind) === "ai", i = this.state.get().messages, r = i.length > 0 ? i[i.length - 1] : void 0;
454
456
  if (s && t || // If last message is from user, then bot response did not arrive yet
455
- s && (c == null ? void 0 : c.type) === "FROM_USER") {
457
+ s && (r == null ? void 0 : r.type) === "FROM_USER") {
456
458
  console.warn("Cannot send messages while awaiting AI response");
457
459
  return;
458
460
  }
@@ -465,14 +467,14 @@ class $ {
465
467
  ), S = this.state.get().messages;
466
468
  if (this.state.setPartial({
467
469
  messages: [...S, u]
468
- }), !((d = this.sessionCtx.sessionState.get().session) != null && d.id)) {
470
+ }), !((h = this.sessionCtx.sessionState.get().session) != null && h.id)) {
469
471
  if (!await this.sessionCtx.createSession()) {
470
472
  console.error("Failed to create session");
471
473
  return;
472
474
  }
473
475
  this.sessionCtx.refreshSessions();
474
476
  }
475
- const k = (l = this.sessionCtx.sessionState.get().session) == null ? void 0 : l.id;
477
+ const k = (c = this.sessionCtx.sessionState.get().session) == null ? void 0 : c.id;
476
478
  if (!k) return;
477
479
  const { data: f } = await this.api.sendMessage(
478
480
  {
@@ -495,11 +497,11 @@ class $ {
495
497
  this.sendMessageAbortController.signal
496
498
  );
497
499
  if (f != null && f.success) {
498
- const b = this.toBotMessage(f);
499
- if (b) {
500
+ const w = this.toBotMessage(f);
501
+ if (w) {
500
502
  const I = this.state.get().messages;
501
503
  if (!!I.some(
502
- (A) => A.id === b.id
504
+ (A) => A.id === w.id
503
505
  )) {
504
506
  this.state.setPartial({
505
507
  lastAIResMightSolveUserIssue: ((C = f.autopilotResponse) == null ? void 0 : C.mightSolveUserIssue) || ((x = f.uiResponse) == null ? void 0 : x.mightSolveUserIssue)
@@ -507,17 +509,17 @@ class $ {
507
509
  return;
508
510
  }
509
511
  this.state.setPartial({
510
- messages: [...I, b],
511
- lastAIResMightSolveUserIssue: ((p = f.autopilotResponse) == null ? void 0 : p.mightSolveUserIssue) || ((w = f.uiResponse) == null ? void 0 : w.mightSolveUserIssue)
512
+ messages: [...I, w],
513
+ lastAIResMightSolveUserIssue: ((p = f.autopilotResponse) == null ? void 0 : p.mightSolveUserIssue) || ((b = f.uiResponse) == null ? void 0 : b.mightSolveUserIssue)
512
514
  });
513
515
  }
514
516
  f.session && this.sessionCtx.sessionState.setPartial({ session: f.session });
515
517
  } else {
516
- const b = this.toBotErrorMessage(
518
+ const w = this.toBotErrorMessage(
517
519
  ((v = f == null ? void 0 : f.error) == null ? void 0 : v.message) || "Unknown error occurred"
518
520
  ), I = this.state.get().messages;
519
521
  this.state.setPartial({
520
- messages: [...I, b]
522
+ messages: [...I, w]
521
523
  });
522
524
  }
523
525
  } catch (u) {
@@ -528,7 +530,7 @@ class $ {
528
530
  }, this.toUserMessage = (e, t) => {
529
531
  const s = (() => {
530
532
  const i = this.contactCtx.state.get().extraCollectedData;
531
- return this.state.get().messages.length === 0 && i && Object.keys(i).length > 0 ? `${Object.entries(i).filter(([r, d]) => !!d).map(([r, d]) => `${r}: ${d}`).join(`
533
+ return this.state.get().messages.length === 0 && i && Object.keys(i).length > 0 ? `${Object.entries(i).filter(([n, h]) => !!h).map(([n, h]) => `${n}: ${h}`).join(`
532
534
  `)}
533
535
 
534
536
  ${e}` : e;
@@ -572,15 +574,15 @@ ${e}` : e;
572
574
  variant: "error",
573
575
  action: void 0
574
576
  }
575
- }), this.config = a, this.api = o, this.sessionCtx = h, this.contactCtx = n;
577
+ }), this.config = a, this.api = o, this.sessionCtx = l, this.contactCtx = g;
576
578
  }
577
579
  }
578
580
  class N {
579
581
  constructor({
580
582
  config: a,
581
583
  contactCtx: o,
582
- sessionCtx: h,
583
- resetChat: n
584
+ sessionCtx: l,
585
+ resetChat: g
584
586
  }) {
585
587
  var e;
586
588
  this.registerRoutingListener = () => {
@@ -591,14 +593,14 @@ class N {
591
593
  });
592
594
  }), this.sessionCtx.sessionsState.subscribe(
593
595
  ({ isInitialFetchLoading: t, data: s }) => {
594
- var i, c, r, d;
596
+ var i, r, n, h;
595
597
  if ((i = this.config.router) != null && i.chatScreenOnly && // Do not route to a chat if we are currently inside one already
596
598
  // This also applies to newly created sessions; the new session will be in `sessionState` before it is refreshed and included in `sessionsState`
597
- !((c = this.sessionCtx.sessionState.get().session) != null && c.id)) {
598
- const l = (r = s.find((C) => C.isOpened)) == null ? void 0 : r.id;
599
- return l ? this.toChatScreen(l) : void 0;
599
+ !((r = this.sessionCtx.sessionState.get().session) != null && r.id)) {
600
+ const c = (n = s.find((C) => C.isOpened)) == null ? void 0 : n.id;
601
+ return c ? this.toChatScreen(c) : void 0;
600
602
  }
601
- s.length || ((d = this.config.router) == null ? void 0 : d.goToChatIfNoSessions) !== !1 && !t && this.state.get().screen !== "chat" && this.toChatScreen();
603
+ s.length || ((h = this.config.router) == null ? void 0 : h.goToChatIfNoSessions) !== !1 && !t && this.state.get().screen !== "chat" && this.toChatScreen();
602
604
  }
603
605
  );
604
606
  }, this.toSessionsScreen = () => {
@@ -610,7 +612,7 @@ class N {
610
612
  this.sessionCtx.sessionState.setPartial({ session: s });
611
613
  }
612
614
  this.state.setPartial({ screen: "chat" });
613
- }, this.config = a, this.contactCtx = o, this.sessionCtx = h, this.resetChat = n, this.state = new P({
615
+ }, this.config = a, this.contactCtx = o, this.sessionCtx = l, this.resetChat = g, this.state = new P({
614
616
  screen: this.contactCtx.shouldCollectData() ? "welcome" : (e = this.config.router) != null && e.chatScreenOnly ? "chat" : "sessions"
615
617
  }), this.registerRoutingListener();
616
618
  }
@@ -631,7 +633,7 @@ const m = class m {
631
633
  constructor({
632
634
  config: a,
633
635
  storage: o,
634
- modes: h
636
+ modes: l
635
637
  }) {
636
638
  if (this.modes = [], this.resetChat = () => {
637
639
  this.sessionCtx.reset(), this.messageCtx.reset();
@@ -639,11 +641,12 @@ const m = class m {
639
641
  throw Error(
640
642
  "Widget polling values are not defined, did you call WidgetCtx.initialize()"
641
643
  );
642
- this.config = a, this.api = new T({ config: a }), this.storageCtx = o ? new H({ storage: o }) : void 0, this.modes = h, this.contactCtx = new B({
644
+ this.config = a, this.api = new T({ config: a }), this.storageCtx = o ? new H({ storage: o }) : void 0, this.modes = l, this.contactCtx = new B({
643
645
  api: this.api,
644
646
  config: this.config,
645
647
  storageCtx: this.storageCtx
646
648
  }), this.sessionCtx = new q({
649
+ config: this.config,
647
650
  api: this.api,
648
651
  contactCtx: this.contactCtx,
649
652
  sessionsPollingIntervalSeconds: m.pollingIntervalsSeconds.sessions
@@ -670,17 +673,17 @@ m.pollingIntervalsSeconds = null, m.initialize = async ({
670
673
  config: a,
671
674
  storage: o
672
675
  }) => {
673
- var n, e, t;
674
- const h = await new T({
676
+ var g, e, t;
677
+ const l = await new T({
675
678
  config: a
676
679
  }).getExternalWidgetConfig();
677
680
  return m.pollingIntervalsSeconds = {
678
- session: ((n = h.data) == null ? void 0 : n.sessionPollingIntervalSeconds) || 10,
679
- sessions: ((e = h.data) == null ? void 0 : e.sessionsPollingIntervalSeconds) || 60
681
+ session: ((g = l.data) == null ? void 0 : g.sessionPollingIntervalSeconds) || 10,
682
+ sessions: ((e = l.data) == null ? void 0 : e.sessionsPollingIntervalSeconds) || 60
680
683
  }, new m({
681
684
  config: a,
682
685
  storage: o,
683
- modes: ((t = h.data) == null ? void 0 : t.modes) || []
686
+ modes: ((t = l.data) == null ? void 0 : t.modes) || []
684
687
  });
685
688
  };
686
689
  let U = m;
@@ -689,4 +692,4 @@ export {
689
692
  U as W,
690
693
  z as r
691
694
  };
692
- //# sourceMappingURL=widget.ctx-Djrb64eX.js.map
695
+ //# sourceMappingURL=widget.ctx-CfUvw50X.js.map