@opencx/widget 3.0.92 → 3.0.94

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,17 +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 D(g) {
174
+ function z(d) {
175
+ return d();
176
+ }
177
+ function D(d) {
175
178
  try {
176
- const a = g();
179
+ const a = d();
177
180
  return a instanceof Promise ? a.then((o) => ({ data: o })).catch((o) => ({ error: o })) : { data: a };
178
181
  } catch (a) {
179
182
  return { error: a };
@@ -183,8 +186,8 @@ class L {
183
186
  constructor({
184
187
  api: a,
185
188
  config: o,
186
- sessionCtx: h,
187
- messageCtx: n,
189
+ sessionCtx: l,
190
+ messageCtx: g,
188
191
  sessionPollingIntervalSeconds: e
189
192
  }) {
190
193
  this.poller = new R(), this.registerPolling = () => {
@@ -194,24 +197,24 @@ class L {
194
197
  }, this.sessionPollingIntervalSeconds * 1e3) : this.poller.reset();
195
198
  });
196
199
  }, this.hackAndSlash = async (t, s) => {
197
- var d;
200
+ var h;
198
201
  this.messageCtx.state.get().messages.length === 0 && this.messageCtx.state.setPartial({ isInitialFetchLoading: !0 });
199
- 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({
200
203
  sessionId: t,
201
204
  abortSignal: s,
202
- lastMessageTimestamp: c
205
+ lastMessageTimestamp: r
203
206
  });
204
- 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) {
205
- const l = this.messageCtx.state.get().messages, C = r.history.map(this.mapHistoryToMessage).filter(
206
- (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)
207
210
  );
208
211
  this.messageCtx.state.setPartial({
209
- messages: [...l, ...C]
212
+ messages: [...c, ...C]
210
213
  });
211
214
  }
212
215
  this.messageCtx.state.get().isInitialFetchLoading && this.messageCtx.state.setPartial({ isInitialFetchLoading: !1 });
213
216
  }, this.mapHistoryToMessage = (t) => {
214
- var c, r;
217
+ var r, n;
215
218
  const s = {
216
219
  id: t.publicId,
217
220
  timestamp: t.sentAt || "",
@@ -246,9 +249,9 @@ class L {
246
249
  component: "bot_message",
247
250
  agent: {
248
251
  id: null,
249
- name: ((c = this.config.bot) == null ? void 0 : c.name) || "",
252
+ name: ((r = this.config.bot) == null ? void 0 : r.name) || "",
250
253
  isAi: !0,
251
- avatar: ((r = this.config.bot) == null ? void 0 : r.avatar) || ""
254
+ avatar: ((n = this.config.bot) == null ? void 0 : n.avatar) || ""
252
255
  },
253
256
  data: {
254
257
  message: t.content.text || "",
@@ -262,35 +265,35 @@ class L {
262
265
  const s = t.result;
263
266
  if (s === null || typeof s != "object") return s;
264
267
  if ("responseBodyText" in s && typeof s.responseBodyText == "string") {
265
- const i = s.responseBodyText, c = D(() => JSON.parse(i)).data;
266
- if (c) return c;
268
+ const i = s.responseBodyText, r = D(() => JSON.parse(i)).data;
269
+ if (r) return r;
267
270
  }
268
271
  return t.result;
269
- }, 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();
270
273
  }
271
274
  }
272
275
  class B {
273
276
  constructor({
274
277
  config: a,
275
278
  api: o,
276
- storageCtx: h
279
+ storageCtx: l
277
280
  }) {
278
- var n;
281
+ var g;
279
282
  this.shouldCollectData = () => {
280
283
  var e;
281
284
  return !!(!((e = this.state.get().contact) != null && e.token) && this.config.collectUserData);
282
285
  }, this.autoCreateUnverifiedUserIfNotExists = async () => {
283
- 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;
284
287
  if (!((e = this.config.user) != null && e.token)) {
285
288
  if (this.config.collectUserData && !((s = (t = this.config.user) == null ? void 0 : t.data) != null && s.email)) {
286
289
  if ((i = this.config.extraDataCollectionFields) != null && i.length)
287
290
  return;
288
- const S = await ((c = this.storageCtx) == null ? void 0 : c.getContactToken());
291
+ const S = await ((r = this.storageCtx) == null ? void 0 : r.getContactToken());
289
292
  S && await this.setUnverifiedContact(S);
290
293
  return;
291
294
  }
292
- if (!((d = (r = this.config.user) == null ? void 0 : r.data) != null && d.email)) {
293
- 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());
294
297
  if (S) {
295
298
  await this.setUnverifiedContact(S);
296
299
  return;
@@ -298,7 +301,7 @@ class B {
298
301
  }
299
302
  await this.createUnverifiedContact({
300
303
  email: (x = (C = this.config.user) == null ? void 0 : C.data) == null ? void 0 : x.email,
301
- 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",
302
305
  non_verified_custom_data: (u = (v = this.config.user) == null ? void 0 : v.data) == null ? void 0 : u.customData
303
306
  });
304
307
  }
@@ -315,11 +318,11 @@ class B {
315
318
  this.state.setPartial({ isCreatingUnverifiedContact: !1 });
316
319
  }
317
320
  }, this.setUnverifiedContact = async (e) => {
318
- var i, c, r, d;
319
- const t = await ((i = this.storageCtx) == null ? void 0 : i.getExternalContactId()), s = ((c = this.config.user) == null ? void 0 : c.externalId) || t || E();
320
- 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 } });
321
- }, this.config = a, this.storageCtx = h, this.api = o, this.state = new P({
322
- 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 ? {
323
326
  token: a.user.token,
324
327
  // Set optional externalId from config... not local storage
325
328
  externalId: a.user.externalId
@@ -335,9 +338,10 @@ function y() {
335
338
  }
336
339
  class q {
337
340
  constructor({
338
- api: a,
339
- contactCtx: o,
340
- sessionsPollingIntervalSeconds: h
341
+ config: a,
342
+ api: o,
343
+ contactCtx: l,
344
+ sessionsPollingIntervalSeconds: g
341
345
  }) {
342
346
  this.sessionsRefresher = new R(), this.sessionState = new P({
343
347
  session: null,
@@ -355,84 +359,85 @@ class q {
355
359
  }), this.reset = async () => {
356
360
  this.sessionState.reset();
357
361
  }, this.registerSessionsRefresherWrapper = () => {
358
- var n;
362
+ var e;
359
363
  // If the widget config was initially provided with a contact token, no state change would be triggered, so we just fetch
360
- (n = this.contactCtx.state.get().contact) != null && n.token && !this.sessionsState.get().didStartInitialFetch ? this.registerSessionsRefresher() : this.contactCtx.state.subscribe(({ contact: e }) => {
361
- 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();
362
366
  });
363
367
  }, this.registerSessionsRefresher = () => {
364
368
  this.sessionsRefresher.startPolling(async () => {
365
369
  this.sessionsState.get().didStartInitialFetch === !1 && this.sessionsState.setPartial({ didStartInitialFetch: !0 }), await this.refreshSessions(), this.sessionsState.get().isInitialFetchLoading === !0 && this.sessionsState.setPartial({ isInitialFetchLoading: !1 });
366
370
  }, this.sessionsPollingIntervalSeconds * 1e3);
367
371
  }, this.createSession = async () => {
368
- var s;
372
+ var r;
369
373
  this.sessionState.setPartial({ session: null, isCreatingSession: !0 });
370
- const n = (s = this.contactCtx.state.get().contact) == null ? void 0 : s.externalId, { data: e, error: t } = await this.api.createSession({
371
- customData: n ? {
372
- external_id: n
373
- } : 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
374
379
  });
375
- 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);
376
381
  }, this.loadMoreSessions = async () => {
377
382
  if (this.sessionsState.get().isLastPage) return;
378
- const { data: n } = await this.getSessions({
383
+ const { data: e } = await this.getSessions({
379
384
  cursor: this.sessionsState.get().cursor
380
385
  });
381
- if (n) {
382
- const t = [...this.sessionsState.get().data, ...n.items].filter(
383
- (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)
384
389
  );
385
390
  this.sessionsState.setPartial({
386
- data: t,
387
- cursor: n.next || void 0,
388
- isLastPage: n.next === null
391
+ data: s,
392
+ cursor: e.next || void 0,
393
+ isLastPage: e.next === null
389
394
  });
390
395
  }
391
- }, this.getSessions = async ({ cursor: n }) => {
392
- var t, s;
393
- if (!((t = this.contactCtx.state.get().contact) != null && t.token)) return { data: null };
394
- 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;
395
400
  return await this.api.getSessions({
396
- cursor: n,
397
- filters: e ? {
398
- external_id: e
401
+ cursor: e,
402
+ filters: t ? {
403
+ external_id: t
399
404
  } : {}
400
405
  });
401
- }, this.setSessions = (n) => {
402
- const e = [...n, ...this.sessionsState.get().data].filter(
403
- (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)
404
409
  );
405
- this.sessionsState.setPartial({ data: e });
410
+ this.sessionsState.setPartial({ data: t });
406
411
  }, this.refreshSessions = async () => {
407
- const { data: n } = await this.getSessions({ cursor: void 0 });
408
- n && this.setSessions(n.items);
412
+ const { data: e } = await this.getSessions({ cursor: void 0 });
413
+ e && this.setSessions(e.items);
409
414
  }, this.resolveSession = async () => {
410
- const n = this.sessionState.get().session;
411
- if (!n || !n.isOpened)
415
+ const e = this.sessionState.get().session;
416
+ if (!e || !e.isOpened)
412
417
  return { success: !1, error: "Session is not opened" };
413
418
  this.sessionState.setPartial({ isResolvingSession: !0 });
414
- const { data: e, error: t } = await this.api.resolveSession({
415
- session_id: n.id
419
+ const { data: t, error: s } = await this.api.resolveSession({
420
+ session_id: e.id
416
421
  });
417
- return e ? (this.sessionState.setPartial({ session: e, isResolvingSession: !1 }), { success: !0, data: e }) : (this.sessionState.setPartial({ isResolvingSession: !1 }), { success: !1, error: t });
418
- }, this.createStateCheckpoint = async (n) => {
419
- var i;
420
- const e = (i = this.sessionState.get().session) == null ? void 0 : i.id;
421
- if (!e) return;
422
- const { data: t, error: s } = await this.api.createStateCheckpoint({
423
- session_id: e,
424
- 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
425
430
  });
426
- return t ? { data: t } : { success: !1, error: s };
427
- }, 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();
428
433
  }
429
434
  }
430
435
  class $ {
431
436
  constructor({
432
437
  config: a,
433
438
  api: o,
434
- sessionCtx: h,
435
- contactCtx: n
439
+ sessionCtx: l,
440
+ contactCtx: g
436
441
  }) {
437
442
  this.state = new P({
438
443
  messages: [],
@@ -442,14 +447,14 @@ class $ {
442
447
  }), this.sendMessageAbortController = new AbortController(), this.reset = () => {
443
448
  this.sendMessageAbortController.abort("Resetting chat"), this.state.reset();
444
449
  }, this.sendMessage = async (e) => {
445
- var r, d, l, C, x, p, w, v;
450
+ var n, h, c, C, x, p, b, v;
446
451
  if (!e.content.trim() && (!e.attachments || e.attachments.length === 0)) {
447
452
  console.warn("Cannot send an empty message of no content or attachments");
448
453
  return;
449
454
  }
450
- 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;
451
456
  if (s && t || // If last message is from user, then bot response did not arrive yet
452
- s && (c == null ? void 0 : c.type) === "FROM_USER") {
457
+ s && (r == null ? void 0 : r.type) === "FROM_USER") {
453
458
  console.warn("Cannot send messages while awaiting AI response");
454
459
  return;
455
460
  }
@@ -462,14 +467,14 @@ class $ {
462
467
  ), S = this.state.get().messages;
463
468
  if (this.state.setPartial({
464
469
  messages: [...S, u]
465
- }), !((d = this.sessionCtx.sessionState.get().session) != null && d.id)) {
470
+ }), !((h = this.sessionCtx.sessionState.get().session) != null && h.id)) {
466
471
  if (!await this.sessionCtx.createSession()) {
467
472
  console.error("Failed to create session");
468
473
  return;
469
474
  }
470
475
  this.sessionCtx.refreshSessions();
471
476
  }
472
- 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;
473
478
  if (!k) return;
474
479
  const { data: f } = await this.api.sendMessage(
475
480
  {
@@ -492,11 +497,11 @@ class $ {
492
497
  this.sendMessageAbortController.signal
493
498
  );
494
499
  if (f != null && f.success) {
495
- const b = this.toBotMessage(f);
496
- if (b) {
500
+ const w = this.toBotMessage(f);
501
+ if (w) {
497
502
  const I = this.state.get().messages;
498
503
  if (!!I.some(
499
- (A) => A.id === b.id
504
+ (A) => A.id === w.id
500
505
  )) {
501
506
  this.state.setPartial({
502
507
  lastAIResMightSolveUserIssue: ((C = f.autopilotResponse) == null ? void 0 : C.mightSolveUserIssue) || ((x = f.uiResponse) == null ? void 0 : x.mightSolveUserIssue)
@@ -504,17 +509,17 @@ class $ {
504
509
  return;
505
510
  }
506
511
  this.state.setPartial({
507
- messages: [...I, b],
508
- 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)
509
514
  });
510
515
  }
511
516
  f.session && this.sessionCtx.sessionState.setPartial({ session: f.session });
512
517
  } else {
513
- const b = this.toBotErrorMessage(
518
+ const w = this.toBotErrorMessage(
514
519
  ((v = f == null ? void 0 : f.error) == null ? void 0 : v.message) || "Unknown error occurred"
515
520
  ), I = this.state.get().messages;
516
521
  this.state.setPartial({
517
- messages: [...I, b]
522
+ messages: [...I, w]
518
523
  });
519
524
  }
520
525
  } catch (u) {
@@ -525,7 +530,7 @@ class $ {
525
530
  }, this.toUserMessage = (e, t) => {
526
531
  const s = (() => {
527
532
  const i = this.contactCtx.state.get().extraCollectedData;
528
- 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(`
529
534
  `)}
530
535
 
531
536
  ${e}` : e;
@@ -569,15 +574,15 @@ ${e}` : e;
569
574
  variant: "error",
570
575
  action: void 0
571
576
  }
572
- }), 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;
573
578
  }
574
579
  }
575
580
  class N {
576
581
  constructor({
577
582
  config: a,
578
583
  contactCtx: o,
579
- sessionCtx: h,
580
- resetChat: n
584
+ sessionCtx: l,
585
+ resetChat: g
581
586
  }) {
582
587
  var e;
583
588
  this.registerRoutingListener = () => {
@@ -588,14 +593,14 @@ class N {
588
593
  });
589
594
  }), this.sessionCtx.sessionsState.subscribe(
590
595
  ({ isInitialFetchLoading: t, data: s }) => {
591
- var i, c, r, d;
596
+ var i, r, n, h;
592
597
  if ((i = this.config.router) != null && i.chatScreenOnly && // Do not route to a chat if we are currently inside one already
593
598
  // This also applies to newly created sessions; the new session will be in `sessionState` before it is refreshed and included in `sessionsState`
594
- !((c = this.sessionCtx.sessionState.get().session) != null && c.id)) {
595
- const l = (r = s.find((C) => C.isOpened)) == null ? void 0 : r.id;
596
- 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;
597
602
  }
598
- 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();
599
604
  }
600
605
  );
601
606
  }, this.toSessionsScreen = () => {
@@ -607,7 +612,7 @@ class N {
607
612
  this.sessionCtx.sessionState.setPartial({ session: s });
608
613
  }
609
614
  this.state.setPartial({ screen: "chat" });
610
- }, 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({
611
616
  screen: this.contactCtx.shouldCollectData() ? "welcome" : (e = this.config.router) != null && e.chatScreenOnly ? "chat" : "sessions"
612
617
  }), this.registerRoutingListener();
613
618
  }
@@ -628,7 +633,7 @@ const m = class m {
628
633
  constructor({
629
634
  config: a,
630
635
  storage: o,
631
- modes: h
636
+ modes: l
632
637
  }) {
633
638
  if (this.modes = [], this.resetChat = () => {
634
639
  this.sessionCtx.reset(), this.messageCtx.reset();
@@ -636,11 +641,12 @@ const m = class m {
636
641
  throw Error(
637
642
  "Widget polling values are not defined, did you call WidgetCtx.initialize()"
638
643
  );
639
- 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({
640
645
  api: this.api,
641
646
  config: this.config,
642
647
  storageCtx: this.storageCtx
643
648
  }), this.sessionCtx = new q({
649
+ config: this.config,
644
650
  api: this.api,
645
651
  contactCtx: this.contactCtx,
646
652
  sessionsPollingIntervalSeconds: m.pollingIntervalsSeconds.sessions
@@ -667,22 +673,23 @@ m.pollingIntervalsSeconds = null, m.initialize = async ({
667
673
  config: a,
668
674
  storage: o
669
675
  }) => {
670
- var n, e, t;
671
- const h = await new T({
676
+ var g, e, t;
677
+ const l = await new T({
672
678
  config: a
673
679
  }).getExternalWidgetConfig();
674
680
  return m.pollingIntervalsSeconds = {
675
- session: ((n = h.data) == null ? void 0 : n.sessionPollingIntervalSeconds) || 10,
676
- 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
677
683
  }, new m({
678
684
  config: a,
679
685
  storage: o,
680
- modes: ((t = h.data) == null ? void 0 : t.modes) || []
686
+ modes: ((t = l.data) == null ? void 0 : t.modes) || []
681
687
  });
682
688
  };
683
689
  let U = m;
684
690
  export {
685
691
  P,
686
- U as W
692
+ U as W,
693
+ z as r
687
694
  };
688
- //# sourceMappingURL=widget.ctx-uAWzif3-.js.map
695
+ //# sourceMappingURL=widget.ctx-CfUvw50X.js.map