@opencx/widget 3.0.49 → 3.0.50

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,25 +1,24 @@
1
- import F from "axios";
2
1
  import O from "openapi-fetch";
3
- import _ from "lodash.isequal";
4
- import { v4 as R } from "uuid";
5
- const D = (h) => {
6
- console.log(h.error);
7
- }, L = (h) => {
8
- const n = O({
9
- baseUrl: h.baseUrl
2
+ import F from "lodash.isequal";
3
+ import { v4 as A } from "uuid";
4
+ const L = (g) => {
5
+ console.log(g.error);
6
+ }, D = (g) => {
7
+ const a = O({
8
+ baseUrl: g.baseUrl
10
9
  }), i = {
11
- onRequest: h.onRequest,
12
- onResponse: h.onResponse,
13
- onError: h.onError || D
10
+ onRequest: g.onRequest,
11
+ onResponse: g.onResponse,
12
+ onError: g.onError || L
14
13
  };
15
- return n.use(i), n;
14
+ return a.use(i), a;
16
15
  };
17
- class k {
16
+ class E {
18
17
  constructor({
19
- config: n
18
+ config: a
20
19
  }) {
21
- var a;
22
- this.constructClientOptions = (t) => {
20
+ var n;
21
+ this.userToken = null, this.constructClientOptions = (t) => {
23
22
  const s = this.config.apiUrl || "https://api.open.cx", e = {
24
23
  "X-Bot-Token": this.config.token,
25
24
  "Content-Type": "application/json",
@@ -30,22 +29,17 @@ class k {
30
29
  }, this.createOpenAPIClient = ({
31
30
  baseUrl: t,
32
31
  headers: s
33
- }) => L({
32
+ }) => D({
34
33
  baseUrl: t,
35
34
  onRequest: ({ request: e }) => {
36
- Object.entries(s).forEach(([o, l]) => {
37
- l && e.headers.set(o, l);
35
+ Object.entries(s).forEach(([o, r]) => {
36
+ r && e.headers.set(o, r);
38
37
  });
39
38
  }
40
- }), this.createAxiosUploadClient = ({
41
- baseUrl: t,
42
- headers: s
43
- }) => F.create({
44
- baseURL: `${t}/backend/widget/v2/upload`,
45
- headers: s
46
39
  }), this.setAuthToken = (t) => {
40
+ this.userToken = t;
47
41
  const { baseUrl: s, headers: e } = this.constructClientOptions(t);
48
- this.client = this.createOpenAPIClient({ baseUrl: s, headers: e }), this.uploadFileClient = this.createAxiosUploadClient({ baseUrl: s, headers: e });
42
+ this.client = this.createOpenAPIClient({ baseUrl: s, headers: e });
49
43
  }, this.getExternalWidgetConfig = async () => await this.client.GET("/backend/widget/v2/config", {
50
44
  params: { header: { "X-Bot-Token": this.config.token } }
51
45
  }), this.widgetPrelude = async () => await this.client.GET("/backend/widget/v2/prelude", {
@@ -78,60 +72,86 @@ class k {
78
72
  }) => await this.client.GET("/backend/widget/v2/sessions", {
79
73
  params: { query: { cursor: t, filters: JSON.stringify(s) } },
80
74
  signal: e
81
- }), this.uploadFile = async (t, s = {}) => {
82
- const e = new FormData();
83
- e.append("file", t.file);
84
- const { data: o } = await this.uploadFileClient.post("", e, {
85
- headers: {
86
- "Content-Type": "multipart/form-data"
87
- },
88
- ...s
75
+ }), this.uploadFile = async ({
76
+ file: t,
77
+ abortSignal: s,
78
+ onProgress: e
79
+ }) => new Promise((o, r) => {
80
+ const l = new FormData();
81
+ l.append("file", t);
82
+ const c = new XMLHttpRequest();
83
+ if (s && (s.addEventListener("abort", () => {
84
+ c.abort(), r(new DOMException("Aborted", "AbortError"));
85
+ }), s.aborted)) {
86
+ r(new DOMException("Aborted", "AbortError"));
87
+ return;
88
+ }
89
+ c.upload.addEventListener("progress", (d) => {
90
+ if (d.lengthComputable && e) {
91
+ const x = Math.round(d.loaded / d.total * 100);
92
+ e(x);
93
+ }
94
+ }), c.addEventListener("load", () => {
95
+ if (c.status >= 200 && c.status < 300)
96
+ try {
97
+ const d = JSON.parse(c.responseText);
98
+ o(d);
99
+ } catch (d) {
100
+ r(new Error(`Failed to parse response: ${d}`));
101
+ }
102
+ else
103
+ r(new Error(`Upload failed with status: ${c.status}`));
104
+ }), c.addEventListener("error", () => {
105
+ r(new Error("Network error occurred"));
106
+ }), c.addEventListener("timeout", () => {
107
+ r(new Error("Upload timed out"));
89
108
  });
90
- return o;
91
- }, this.vote = async (t) => await this.client.POST("/backend/widget/v2/chat/vote", { body: t }), this.config = n;
92
- const { baseUrl: i, headers: c } = this.constructClientOptions(
93
- (a = n.user) == null ? void 0 : a.token
109
+ const { baseUrl: f } = this.constructClientOptions(this.userToken), C = `${f}/backend/widget/v2/upload`;
110
+ c.open("POST", C), c.setRequestHeader("X-Bot-Token", this.config.token), this.userToken && c.setRequestHeader("Authorization", `Bearer ${this.userToken}`), c.send(l);
111
+ }), this.vote = async (t) => await this.client.POST("/backend/widget/v2/chat/vote", { body: t }), this.config = a;
112
+ const { baseUrl: i, headers: h } = this.constructClientOptions(
113
+ (n = a.user) == null ? void 0 : n.token
94
114
  );
95
- this.client = this.createOpenAPIClient({ baseUrl: i, headers: c }), this.uploadFileClient = this.createAxiosUploadClient({ baseUrl: i, headers: c });
115
+ this.client = this.createOpenAPIClient({ baseUrl: i, headers: h });
96
116
  }
97
117
  }
98
- class v {
99
- constructor(n) {
118
+ class b {
119
+ constructor(a) {
100
120
  this.subscribers = /* @__PURE__ */ new Set(), this.get = () => this.state, this.set = (i) => {
101
- _(this.state, i) || (this.state = i, this.notifySubscribers(i));
121
+ F(this.state, i) || (this.state = i, this.notifySubscribers(i));
102
122
  }, this.setPartial = (i) => {
103
123
  if (i == null) return;
104
- const c = { ...this.state, ...i };
105
- this.set(c);
124
+ const h = { ...this.state, ...i };
125
+ this.set(h);
106
126
  }, this.reset = () => {
107
127
  this.set(this.initialState);
108
128
  }, this.notifySubscribers = (i) => {
109
- Array.from(this.subscribers).forEach((a) => {
129
+ Array.from(this.subscribers).forEach((n) => {
110
130
  try {
111
- a(i);
131
+ n(i);
112
132
  } catch (t) {
113
133
  console.error(t);
114
134
  }
115
135
  });
116
136
  }, this.subscribe = (i) => (this.subscribers.add(i), () => {
117
137
  this.subscribers.delete(i);
118
- }), this.state = n, this.initialState = n;
138
+ }), this.state = a, this.initialState = a;
119
139
  }
120
140
  }
121
- class M {
141
+ class T {
122
142
  constructor() {
123
- this.state = new v({
143
+ this.state = new b({
124
144
  isPolling: !1,
125
145
  isError: !1
126
146
  }), this.abortController = new AbortController(), this.reset = () => {
127
- var n;
128
- this.abortController.abort("Resetting poller"), (n = this.stopPolling) == null || n.call(this), this.stopPolling = null;
129
- }, this.stopPolling = null, this.startPolling = (n, i) => {
147
+ var a;
148
+ this.abortController.abort("Resetting poller"), (a = this.stopPolling) == null || a.call(this), this.stopPolling = null;
149
+ }, this.stopPolling = null, this.startPolling = (a, i) => {
130
150
  if (this.stopPolling) return;
131
- const c = [], a = async () => {
151
+ const h = [], n = async () => {
132
152
  this.abortController = new AbortController(), this.state.setPartial({ isPolling: !0 });
133
153
  try {
134
- await n(this.abortController.signal);
154
+ await a(this.abortController.signal);
135
155
  } catch (t) {
136
156
  if (this.abortController.signal.aborted)
137
157
  return;
@@ -139,47 +159,47 @@ class M {
139
159
  } finally {
140
160
  this.state.setPartial({ isPolling: !1 });
141
161
  }
142
- this.abortController.signal.aborted ? console.log("Poller aborted, not scheduling anymore") : c.push(setTimeout(a, i));
162
+ this.abortController.signal.aborted ? console.log("Poller aborted, not scheduling anymore") : h.push(setTimeout(n, i));
143
163
  };
144
- a(), this.stopPolling = () => {
145
- c.forEach(clearTimeout), this.state.reset();
164
+ n(), this.stopPolling = () => {
165
+ h.forEach(clearTimeout), this.state.reset();
146
166
  };
147
167
  };
148
168
  }
149
169
  }
150
- class B {
170
+ class _ {
151
171
  constructor({
152
- api: n,
172
+ api: a,
153
173
  config: i,
154
- sessionCtx: c,
155
- messageCtx: a,
174
+ sessionCtx: h,
175
+ messageCtx: n,
156
176
  sessionPollingIntervalSeconds: t
157
177
  }) {
158
- this.poller = new M(), this.registerPolling = () => {
178
+ this.poller = new T(), this.registerPolling = () => {
159
179
  this.sessionCtx.sessionState.subscribe(({ session: s }) => {
160
180
  s != null && s.id ? this.poller.startPolling(async (e) => {
161
181
  this.hackAndSlash(s.id, e);
162
182
  }, this.sessionPollingIntervalSeconds * 1e3) : this.poller.reset();
163
183
  });
164
184
  }, this.hackAndSlash = async (s, e) => {
165
- var g;
185
+ var c;
166
186
  this.messageCtx.state.get().messages.length === 0 && this.messageCtx.state.setPartial({ isInitialFetchLoading: !0 });
167
- const o = this.messageCtx.state.get().messages, l = o.length > 0 ? (g = o[o.length - 1]) == null ? void 0 : g.timestamp : void 0, { data: r } = await this.api.pollSessionAndHistory({
187
+ const o = this.messageCtx.state.get().messages, r = o.length > 0 ? (c = o[o.length - 1]) == null ? void 0 : c.timestamp : void 0, { data: l } = await this.api.pollSessionAndHistory({
168
188
  sessionId: s,
169
189
  abortSignal: e,
170
- lastMessageTimestamp: l
190
+ lastMessageTimestamp: r
171
191
  });
172
- if (r != null && r.session && this.sessionCtx.sessionState.setPartial({ session: r.session }), r != null && r.history && r.history.length > 0) {
173
- const f = this.messageCtx.state.get().messages, p = r.history.map(this.mapHistoryToMessage).filter(
174
- (m) => !f.some((S) => S.id === m.id)
192
+ if (l != null && l.session && this.sessionCtx.sessionState.setPartial({ session: l.session }), l != null && l.history && l.history.length > 0) {
193
+ const f = this.messageCtx.state.get().messages, m = l.history.map(this.mapHistoryToMessage).filter(
194
+ (C) => !f.some((d) => d.id === C.id)
175
195
  );
176
196
  this.messageCtx.state.setPartial({
177
- messages: [...f, ...p]
197
+ messages: [...f, ...m]
178
198
  });
179
199
  }
180
200
  this.messageCtx.state.get().isInitialFetchLoading && this.messageCtx.state.setPartial({ isInitialFetchLoading: !1 });
181
201
  }, this.mapHistoryToMessage = (s) => {
182
- var l, r;
202
+ var r, l;
183
203
  const e = {
184
204
  id: s.publicId,
185
205
  timestamp: s.sentAt || "",
@@ -214,48 +234,48 @@ class B {
214
234
  component: "bot_message",
215
235
  agent: {
216
236
  id: null,
217
- name: ((l = this.config.bot) == null ? void 0 : l.name) || "",
237
+ name: ((r = this.config.bot) == null ? void 0 : r.name) || "",
218
238
  isAi: !0,
219
- avatar: ((r = this.config.bot) == null ? void 0 : r.avatar) || ""
239
+ avatar: ((l = this.config.bot) == null ? void 0 : l.avatar) || ""
220
240
  },
221
241
  data: {
222
242
  message: s.content.text || "",
223
243
  action: o ? { name: o.actionName, data: o.result } : void 0
224
244
  }
225
245
  };
226
- }, this.api = n, this.config = i, this.sessionCtx = c, this.messageCtx = a, this.sessionPollingIntervalSeconds = t, this.registerPolling();
246
+ }, this.api = a, this.config = i, this.sessionCtx = h, this.messageCtx = n, this.sessionPollingIntervalSeconds = t, this.registerPolling();
227
247
  }
228
248
  }
229
- class q {
249
+ class B {
230
250
  constructor({
231
- config: n,
251
+ config: a,
232
252
  api: i,
233
- storageCtx: c
253
+ storageCtx: h
234
254
  }) {
235
- var a;
255
+ var n;
236
256
  this.shouldCollectData = () => {
237
257
  var t;
238
258
  return !!(!((t = this.state.get().contact) != null && t.token) && this.config.collectUserData);
239
259
  }, this.autoCreateUnverifiedUserIfNotExists = async () => {
240
- var t, s, e, o, l, r, g, f, p, m, S, b;
260
+ var t, s, e, o, r, l, c, f, m, C, d, x;
241
261
  if (!((t = this.config.user) != null && t.token)) {
242
262
  if (this.config.collectUserData && !((e = (s = this.config.user) == null ? void 0 : s.data) != null && e.email)) {
243
263
  if ((o = this.config.extraDataCollectionFields) != null && o.length)
244
264
  return;
245
- const C = await ((l = this.storageCtx) == null ? void 0 : l.getContactToken());
246
- C && await this.setUnverifiedContact(C);
265
+ const S = await ((r = this.storageCtx) == null ? void 0 : r.getContactToken());
266
+ S && await this.setUnverifiedContact(S);
247
267
  return;
248
268
  }
249
- if (!((g = (r = this.config.user) == null ? void 0 : r.data) != null && g.email)) {
250
- const C = await ((f = this.storageCtx) == null ? void 0 : f.getContactToken());
251
- if (C) {
252
- await this.setUnverifiedContact(C);
269
+ if (!((c = (l = this.config.user) == null ? void 0 : l.data) != null && c.email)) {
270
+ const S = await ((f = this.storageCtx) == null ? void 0 : f.getContactToken());
271
+ if (S) {
272
+ await this.setUnverifiedContact(S);
253
273
  return;
254
274
  }
255
275
  }
256
276
  await this.createUnverifiedContact({
257
- name: ((m = (p = this.config.user) == null ? void 0 : p.data) == null ? void 0 : m.name) || "Anonymous",
258
- email: (b = (S = this.config.user) == null ? void 0 : S.data) == null ? void 0 : b.email
277
+ name: ((C = (m = this.config.user) == null ? void 0 : m.data) == null ? void 0 : C.name) || "Anonymous",
278
+ email: (x = (d = this.config.user) == null ? void 0 : d.data) == null ? void 0 : x.email
259
279
  });
260
280
  }
261
281
  }, this.createUnverifiedContact = async (t, s) => {
@@ -271,14 +291,14 @@ class q {
271
291
  this.state.setPartial({ isCreatingUnverifiedContact: !1 });
272
292
  }
273
293
  }, this.setUnverifiedContact = async (t) => {
274
- var o, l, r, g;
275
- const s = await ((o = this.storageCtx) == null ? void 0 : o.getExternalContactId()), e = ((l = this.config.user) == null ? void 0 : l.externalId) || s || R();
276
- this.api.setAuthToken(t), await ((r = this.storageCtx) == null ? void 0 : r.setContactToken(t)), await ((g = this.storageCtx) == null ? void 0 : g.setExternalContactId(e)), this.state.setPartial({ contact: { token: t, externalId: e } });
277
- }, this.config = n, this.storageCtx = c, this.api = i, this.state = new v({
278
- contact: (a = n.user) != null && a.token ? {
279
- token: n.user.token,
294
+ var o, r, l, c;
295
+ const s = await ((o = this.storageCtx) == null ? void 0 : o.getExternalContactId()), e = ((r = this.config.user) == null ? void 0 : r.externalId) || s || A();
296
+ this.api.setAuthToken(t), await ((l = this.storageCtx) == null ? void 0 : l.setContactToken(t)), await ((c = this.storageCtx) == null ? void 0 : c.setExternalContactId(e)), this.state.setPartial({ contact: { token: t, externalId: e } });
297
+ }, this.config = a, this.storageCtx = h, this.api = i, this.state = new b({
298
+ contact: (n = a.user) != null && n.token ? {
299
+ token: a.user.token,
280
300
  // Set optional externalId from config... not local storage
281
- externalId: n.user.externalId
301
+ externalId: a.user.externalId
282
302
  } : null,
283
303
  extraCollectedData: void 0,
284
304
  isCreatingUnverifiedContact: !1,
@@ -286,19 +306,19 @@ class q {
286
306
  }), this.autoCreateUnverifiedUserIfNotExists();
287
307
  }
288
308
  }
289
- function I() {
290
- return R();
309
+ function P() {
310
+ return A();
291
311
  }
292
- class $ {
312
+ class q {
293
313
  constructor({
294
- api: n,
314
+ api: a,
295
315
  contactCtx: i,
296
- sessionsPollingIntervalSeconds: c
316
+ sessionsPollingIntervalSeconds: h
297
317
  }) {
298
- this.sessionsRefresher = new M(), this.sessionState = new v({
318
+ this.sessionsRefresher = new T(), this.sessionState = new b({
299
319
  session: null,
300
320
  isCreatingSession: !1
301
- }), this.sessionsState = new v({
321
+ }), this.sessionsState = new b({
302
322
  data: [],
303
323
  cursor: void 0,
304
324
  isLastPage: !1,
@@ -310,9 +330,9 @@ class $ {
310
330
  }), this.reset = async () => {
311
331
  this.sessionState.reset();
312
332
  }, this.registerSessionsRefresherWrapper = () => {
313
- var a;
333
+ var n;
314
334
  // If the widget config was initially provided with a contact token, no state change would be triggered, so we just fetch
315
- (a = this.contactCtx.state.get().contact) != null && a.token && !this.sessionsState.get().didStartInitialFetch ? this.registerSessionsRefresher() : this.contactCtx.state.subscribe(({ contact: t }) => {
335
+ (n = this.contactCtx.state.get().contact) != null && n.token && !this.sessionsState.get().didStartInitialFetch ? this.registerSessionsRefresher() : this.contactCtx.state.subscribe(({ contact: t }) => {
316
336
  t != null && t.token && !this.sessionsState.get().didStartInitialFetch && this.registerSessionsRefresher();
317
337
  });
318
338
  }, this.registerSessionsRefresher = () => {
@@ -322,55 +342,55 @@ class $ {
322
342
  }, this.createSession = async () => {
323
343
  var e;
324
344
  this.sessionState.setPartial({ session: null, isCreatingSession: !0 });
325
- const a = (e = this.contactCtx.state.get().contact) == null ? void 0 : e.externalId, { data: t, error: s } = await this.api.createSession({
326
- customData: a ? {
327
- external_id: a
345
+ const n = (e = this.contactCtx.state.get().contact) == null ? void 0 : e.externalId, { data: t, error: s } = await this.api.createSession({
346
+ customData: n ? {
347
+ external_id: n
328
348
  } : void 0
329
349
  });
330
350
  return t ? (this.sessionState.setPartial({ session: t, isCreatingSession: !1 }), t) : (console.error("Failed to create session:", s), null);
331
351
  }, this.loadMoreSessions = async () => {
332
352
  if (this.sessionsState.get().isLastPage) return;
333
- const { data: a } = await this.getSessions({
353
+ const { data: n } = await this.getSessions({
334
354
  cursor: this.sessionsState.get().cursor
335
355
  });
336
- if (a) {
337
- const s = [...this.sessionsState.get().data, ...a.items].filter(
338
- (e, o, l) => o === l.findIndex((r) => e.id === r.id)
356
+ if (n) {
357
+ const s = [...this.sessionsState.get().data, ...n.items].filter(
358
+ (e, o, r) => o === r.findIndex((l) => e.id === l.id)
339
359
  );
340
360
  this.sessionsState.setPartial({
341
361
  data: s,
342
- cursor: a.next || void 0,
343
- isLastPage: a.next === null
362
+ cursor: n.next || void 0,
363
+ isLastPage: n.next === null
344
364
  });
345
365
  }
346
- }, this.getSessions = async ({ cursor: a }) => {
366
+ }, this.getSessions = async ({ cursor: n }) => {
347
367
  var s, e;
348
368
  if (!((s = this.contactCtx.state.get().contact) != null && s.token)) return { data: null };
349
369
  const t = (e = this.contactCtx.state.get().contact) == null ? void 0 : e.externalId;
350
370
  return await this.api.getSessions({
351
- cursor: a,
371
+ cursor: n,
352
372
  filters: t ? {
353
373
  external_id: t
354
374
  } : {}
355
375
  });
356
376
  }, this.refreshSessions = async () => {
357
- const { data: a } = await this.getSessions({ cursor: void 0 });
358
- if (!a) return;
359
- const t = [...a.items, ...this.sessionsState.get().data].filter(
360
- (s, e, o) => e === o.findIndex((l) => s.id === l.id)
377
+ const { data: n } = await this.getSessions({ cursor: void 0 });
378
+ if (!n) return;
379
+ const t = [...n.items, ...this.sessionsState.get().data].filter(
380
+ (s, e, o) => e === o.findIndex((r) => s.id === r.id)
361
381
  );
362
382
  this.sessionsState.setPartial({ data: t });
363
- }, this.api = n, this.contactCtx = i, this.sessionsPollingIntervalSeconds = c, this.registerSessionsRefresherWrapper();
383
+ }, this.api = a, this.contactCtx = i, this.sessionsPollingIntervalSeconds = h, this.registerSessionsRefresherWrapper();
364
384
  }
365
385
  }
366
- class j {
386
+ class $ {
367
387
  constructor({
368
- config: n,
388
+ config: a,
369
389
  api: i,
370
- sessionCtx: c,
371
- contactCtx: a
390
+ sessionCtx: h,
391
+ contactCtx: n
372
392
  }) {
373
- this.state = new v({
393
+ this.state = new b({
374
394
  messages: [],
375
395
  isSendingMessage: !1,
376
396
  lastAIResMightSolveUserIssue: !1,
@@ -378,14 +398,14 @@ class j {
378
398
  }), this.sendMessageAbortController = new AbortController(), this.reset = () => {
379
399
  this.sendMessageAbortController.abort("Resetting chat"), this.state.reset();
380
400
  }, this.sendMessage = async (t) => {
381
- var r, g, f, p, m, S, b, C, U;
401
+ var l, c, f, m, C, d, x, S, k;
382
402
  if (!t.content.trim() && (!t.attachments || t.attachments.length === 0)) {
383
403
  console.warn("Cannot send an empty message of no content or attachments");
384
404
  return;
385
405
  }
386
- const s = this.state.get().isSendingMessage, e = ((r = this.sessionCtx.sessionState.get().session) == null ? void 0 : r.assignee.kind) === "ai", o = this.state.get().messages, l = o.length > 0 ? o[o.length - 1] : void 0;
406
+ const s = this.state.get().isSendingMessage, e = ((l = this.sessionCtx.sessionState.get().session) == null ? void 0 : l.assignee.kind) === "ai", o = this.state.get().messages, r = o.length > 0 ? o[o.length - 1] : void 0;
387
407
  if (e && s || // If last message is from user, then bot response did not arrive yet
388
- e && (l == null ? void 0 : l.type) === "FROM_USER") {
408
+ e && (r == null ? void 0 : r.type) === "FROM_USER") {
389
409
  console.warn("Cannot send messages while awaiting AI response");
390
410
  return;
391
411
  }
@@ -395,10 +415,10 @@ class j {
395
415
  const w = this.toUserMessage(
396
416
  t.content.trim(),
397
417
  t.attachments || void 0
398
- ), E = this.state.get().messages;
418
+ ), M = this.state.get().messages;
399
419
  if (this.state.setPartial({
400
- messages: [...E, w]
401
- }), !((g = this.sessionCtx.sessionState.get().session) != null && g.id)) {
420
+ messages: [...M, w]
421
+ }), !((c = this.sessionCtx.sessionState.get().session) != null && c.id)) {
402
422
  if (!await this.sessionCtx.createSession()) {
403
423
  console.error("Failed to create session");
404
424
  return;
@@ -407,43 +427,43 @@ class j {
407
427
  }
408
428
  const y = (f = this.sessionCtx.sessionState.get().session) == null ? void 0 : f.id;
409
429
  if (!y) return;
410
- const { data: d } = await this.api.sendMessage(
430
+ const { data: u } = await this.api.sendMessage(
411
431
  {
412
432
  uuid: w.id,
413
433
  bot_token: this.config.token,
414
434
  headers: this.config.headers,
415
435
  query_params: this.config.queryParams,
416
436
  session_id: y,
417
- user: (p = this.config.user) == null ? void 0 : p.data,
437
+ user: (m = this.config.user) == null ? void 0 : m.data,
418
438
  content: w.content,
419
439
  attachments: t.attachments,
420
440
  clientContext: this.config.context
421
441
  },
422
442
  this.sendMessageAbortController.signal
423
443
  );
424
- if (d != null && d.success) {
425
- const x = this.toBotMessage(d);
426
- if (x) {
427
- const P = this.state.get().messages;
428
- if (!!P.some(
429
- (T) => T.id === x.id
444
+ if (u != null && u.success) {
445
+ const v = this.toBotMessage(u);
446
+ if (v) {
447
+ const I = this.state.get().messages;
448
+ if (!!I.some(
449
+ (R) => R.id === v.id
430
450
  )) {
431
451
  this.state.setPartial({
432
- lastAIResMightSolveUserIssue: ((m = d.autopilotResponse) == null ? void 0 : m.mightSolveUserIssue) || ((S = d.uiResponse) == null ? void 0 : S.mightSolveUserIssue)
452
+ lastAIResMightSolveUserIssue: ((C = u.autopilotResponse) == null ? void 0 : C.mightSolveUserIssue) || ((d = u.uiResponse) == null ? void 0 : d.mightSolveUserIssue)
433
453
  });
434
454
  return;
435
455
  }
436
456
  this.state.setPartial({
437
- messages: [...P, x],
438
- lastAIResMightSolveUserIssue: ((b = d.autopilotResponse) == null ? void 0 : b.mightSolveUserIssue) || ((C = d.uiResponse) == null ? void 0 : C.mightSolveUserIssue)
457
+ messages: [...I, v],
458
+ lastAIResMightSolveUserIssue: ((x = u.autopilotResponse) == null ? void 0 : x.mightSolveUserIssue) || ((S = u.uiResponse) == null ? void 0 : S.mightSolveUserIssue)
439
459
  });
440
460
  }
441
461
  } else {
442
- const x = this.toBotErrorMessage(
443
- ((U = d == null ? void 0 : d.error) == null ? void 0 : U.message) || "Unknown error occurred"
444
- ), P = this.state.get().messages;
462
+ const v = this.toBotErrorMessage(
463
+ ((k = u == null ? void 0 : u.error) == null ? void 0 : k.message) || "Unknown error occurred"
464
+ ), I = this.state.get().messages;
445
465
  this.state.setPartial({
446
- messages: [...P, x]
466
+ messages: [...I, v]
447
467
  });
448
468
  }
449
469
  } catch (w) {
@@ -454,13 +474,13 @@ class j {
454
474
  }, this.toUserMessage = (t, s) => {
455
475
  const e = (() => {
456
476
  const o = this.contactCtx.state.get().extraCollectedData;
457
- return this.state.get().messages.length === 0 && o && Object.keys(o).length > 0 ? `${Object.entries(o).filter(([r, g]) => !!g).map(([r, g]) => `${r}: ${g}`).join(`
477
+ return this.state.get().messages.length === 0 && o && Object.keys(o).length > 0 ? `${Object.entries(o).filter(([l, c]) => !!c).map(([l, c]) => `${l}: ${c}`).join(`
458
478
  `)}
459
479
 
460
480
  ${t}` : t;
461
481
  })();
462
482
  return {
463
- id: I(),
483
+ id: P(),
464
484
  type: "FROM_USER",
465
485
  content: e,
466
486
  deliveredAt: (/* @__PURE__ */ new Date()).toISOString(),
@@ -471,7 +491,7 @@ ${t}` : t;
471
491
  var s;
472
492
  return t.success && t.autopilotResponse ? {
473
493
  type: "FROM_BOT",
474
- id: t.autopilotResponse.id || I(),
494
+ id: t.autopilotResponse.id || P(),
475
495
  timestamp: (/* @__PURE__ */ new Date()).toISOString(),
476
496
  component: "bot_message",
477
497
  agent: this.config.bot ? {
@@ -490,7 +510,7 @@ ${t}` : t;
490
510
  } : null;
491
511
  }, this.toBotErrorMessage = (t) => ({
492
512
  type: "FROM_BOT",
493
- id: I(),
513
+ id: P(),
494
514
  timestamp: (/* @__PURE__ */ new Date()).toISOString(),
495
515
  component: "TEXT",
496
516
  data: {
@@ -498,15 +518,15 @@ ${t}` : t;
498
518
  variant: "error",
499
519
  action: void 0
500
520
  }
501
- }), this.config = n, this.api = i, this.sessionCtx = c, this.contactCtx = a;
521
+ }), this.config = a, this.api = i, this.sessionCtx = h, this.contactCtx = n;
502
522
  }
503
523
  }
504
524
  class N {
505
525
  constructor({
506
- config: n,
526
+ config: a,
507
527
  contactCtx: i,
508
- sessionCtx: c,
509
- resetChat: a
528
+ sessionCtx: h,
529
+ resetChat: n
510
530
  }) {
511
531
  this.registerRoutingListener = () => {
512
532
  this.contactCtx.state.subscribe(({ contact: t }) => {
@@ -526,13 +546,13 @@ class N {
526
546
  this.sessionCtx.sessionState.setPartial({ session: s });
527
547
  }
528
548
  this.state.setPartial({ screen: "chat" });
529
- }, this.state = new v({
549
+ }, this.state = new b({
530
550
  screen: i.shouldCollectData() ? "welcome" : "sessions"
531
- }), this.config = n, this.contactCtx = i, this.sessionCtx = c, this.resetChat = a, this.registerRoutingListener();
551
+ }), this.config = a, this.contactCtx = i, this.sessionCtx = h, this.resetChat = n, this.registerRoutingListener();
532
552
  }
533
553
  }
534
- class G {
535
- constructor({ storage: n }) {
554
+ class H {
555
+ constructor({ storage: a }) {
536
556
  this.KEYS = {
537
557
  contactToken: "opencx__widget__contactToken",
538
558
  externalContactId: "opencx__widget__externalContactId"
@@ -540,39 +560,39 @@ class G {
540
560
  await this.storage.set(this.KEYS.contactToken, i);
541
561
  }, this.getContactToken = async () => this.storage.get(this.KEYS.contactToken), this.setExternalContactId = async (i) => {
542
562
  await this.storage.set(this.KEYS.externalContactId, i);
543
- }, this.getExternalContactId = async () => this.storage.get(this.KEYS.externalContactId), this.storage = n;
563
+ }, this.getExternalContactId = async () => this.storage.get(this.KEYS.externalContactId), this.storage = a;
544
564
  }
545
565
  }
546
- const u = class u {
566
+ const p = class p {
547
567
  constructor({
548
- config: n,
568
+ config: a,
549
569
  storage: i
550
570
  }) {
551
571
  if (this.resetChat = () => {
552
572
  this.sessionCtx.reset(), this.messageCtx.reset();
553
- }, !u.pollingIntervalsSeconds)
573
+ }, !p.pollingIntervalsSeconds)
554
574
  throw Error(
555
575
  "Widget polling values are not defined, did you call WidgetCtx.initialize()"
556
576
  );
557
- this.config = n, this.api = new k({ config: n }), this.storageCtx = i ? new G({ storage: i }) : void 0, this.contactCtx = new q({
577
+ this.config = a, this.api = new E({ config: a }), this.storageCtx = i ? new H({ storage: i }) : void 0, this.contactCtx = new B({
558
578
  api: this.api,
559
579
  config: this.config,
560
580
  storageCtx: this.storageCtx
561
- }), this.sessionCtx = new $({
581
+ }), this.sessionCtx = new q({
562
582
  api: this.api,
563
583
  contactCtx: this.contactCtx,
564
- sessionsPollingIntervalSeconds: u.pollingIntervalsSeconds.sessions
565
- }), this.messageCtx = new j({
584
+ sessionsPollingIntervalSeconds: p.pollingIntervalsSeconds.sessions
585
+ }), this.messageCtx = new $({
566
586
  config: this.config,
567
587
  api: this.api,
568
588
  sessionCtx: this.sessionCtx,
569
589
  contactCtx: this.contactCtx
570
- }), this.activeSessionPollingCtx = new B({
590
+ }), this.activeSessionPollingCtx = new _({
571
591
  api: this.api,
572
592
  config: this.config,
573
593
  sessionCtx: this.sessionCtx,
574
594
  messageCtx: this.messageCtx,
575
- sessionPollingIntervalSeconds: u.pollingIntervalsSeconds.session
595
+ sessionPollingIntervalSeconds: p.pollingIntervalsSeconds.session
576
596
  }), this.routerCtx = new N({
577
597
  config: this.config,
578
598
  contactCtx: this.contactCtx,
@@ -581,25 +601,25 @@ const u = class u {
581
601
  });
582
602
  }
583
603
  };
584
- u.pollingIntervalsSeconds = null, u.initialize = async ({
585
- config: n,
604
+ p.pollingIntervalsSeconds = null, p.initialize = async ({
605
+ config: a,
586
606
  storage: i
587
607
  }) => {
588
- var a, t;
589
- const c = await new k({
590
- config: n
608
+ var n, t;
609
+ const h = await new E({
610
+ config: a
591
611
  }).getExternalWidgetConfig();
592
- return u.pollingIntervalsSeconds = {
593
- session: ((a = c.data) == null ? void 0 : a.sessionPollingIntervalSeconds) || 10,
594
- sessions: ((t = c.data) == null ? void 0 : t.sessionsPollingIntervalSeconds) || 60
595
- }, new u({
596
- config: n,
612
+ return p.pollingIntervalsSeconds = {
613
+ session: ((n = h.data) == null ? void 0 : n.sessionPollingIntervalSeconds) || 10,
614
+ sessions: ((t = h.data) == null ? void 0 : t.sessionsPollingIntervalSeconds) || 60
615
+ }, new p({
616
+ config: a,
597
617
  storage: i
598
618
  });
599
619
  };
600
- let A = u;
620
+ let U = p;
601
621
  export {
602
- v as P,
603
- A as W
622
+ b as P,
623
+ U as W
604
624
  };
605
- //# sourceMappingURL=widget.ctx-QcHsFXpb.js.map
625
+ //# sourceMappingURL=widget.ctx-CMgNQpCm.js.map