@opencx/widget 3.0.48 → 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,42 +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
- attachments: t.attachments
439
+ attachments: t.attachments,
440
+ clientContext: this.config.context
420
441
  },
421
442
  this.sendMessageAbortController.signal
422
443
  );
423
- if (d != null && d.success) {
424
- const x = this.toBotMessage(d);
425
- if (x) {
426
- const P = this.state.get().messages;
427
- if (!!P.some(
428
- (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
429
450
  )) {
430
451
  this.state.setPartial({
431
- 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)
432
453
  });
433
454
  return;
434
455
  }
435
456
  this.state.setPartial({
436
- messages: [...P, x],
437
- 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)
438
459
  });
439
460
  }
440
461
  } else {
441
- const x = this.toBotErrorMessage(
442
- ((U = d == null ? void 0 : d.error) == null ? void 0 : U.message) || "Unknown error occurred"
443
- ), 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;
444
465
  this.state.setPartial({
445
- messages: [...P, x]
466
+ messages: [...I, v]
446
467
  });
447
468
  }
448
469
  } catch (w) {
@@ -453,13 +474,13 @@ class j {
453
474
  }, this.toUserMessage = (t, s) => {
454
475
  const e = (() => {
455
476
  const o = this.contactCtx.state.get().extraCollectedData;
456
- 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(`
457
478
  `)}
458
479
 
459
480
  ${t}` : t;
460
481
  })();
461
482
  return {
462
- id: I(),
483
+ id: P(),
463
484
  type: "FROM_USER",
464
485
  content: e,
465
486
  deliveredAt: (/* @__PURE__ */ new Date()).toISOString(),
@@ -470,7 +491,7 @@ ${t}` : t;
470
491
  var s;
471
492
  return t.success && t.autopilotResponse ? {
472
493
  type: "FROM_BOT",
473
- id: t.autopilotResponse.id || I(),
494
+ id: t.autopilotResponse.id || P(),
474
495
  timestamp: (/* @__PURE__ */ new Date()).toISOString(),
475
496
  component: "bot_message",
476
497
  agent: this.config.bot ? {
@@ -489,7 +510,7 @@ ${t}` : t;
489
510
  } : null;
490
511
  }, this.toBotErrorMessage = (t) => ({
491
512
  type: "FROM_BOT",
492
- id: I(),
513
+ id: P(),
493
514
  timestamp: (/* @__PURE__ */ new Date()).toISOString(),
494
515
  component: "TEXT",
495
516
  data: {
@@ -497,15 +518,15 @@ ${t}` : t;
497
518
  variant: "error",
498
519
  action: void 0
499
520
  }
500
- }), 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;
501
522
  }
502
523
  }
503
524
  class N {
504
525
  constructor({
505
- config: n,
526
+ config: a,
506
527
  contactCtx: i,
507
- sessionCtx: c,
508
- resetChat: a
528
+ sessionCtx: h,
529
+ resetChat: n
509
530
  }) {
510
531
  this.registerRoutingListener = () => {
511
532
  this.contactCtx.state.subscribe(({ contact: t }) => {
@@ -525,13 +546,13 @@ class N {
525
546
  this.sessionCtx.sessionState.setPartial({ session: s });
526
547
  }
527
548
  this.state.setPartial({ screen: "chat" });
528
- }, this.state = new v({
549
+ }, this.state = new b({
529
550
  screen: i.shouldCollectData() ? "welcome" : "sessions"
530
- }), 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();
531
552
  }
532
553
  }
533
- class G {
534
- constructor({ storage: n }) {
554
+ class H {
555
+ constructor({ storage: a }) {
535
556
  this.KEYS = {
536
557
  contactToken: "opencx__widget__contactToken",
537
558
  externalContactId: "opencx__widget__externalContactId"
@@ -539,39 +560,39 @@ class G {
539
560
  await this.storage.set(this.KEYS.contactToken, i);
540
561
  }, this.getContactToken = async () => this.storage.get(this.KEYS.contactToken), this.setExternalContactId = async (i) => {
541
562
  await this.storage.set(this.KEYS.externalContactId, i);
542
- }, 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;
543
564
  }
544
565
  }
545
- const u = class u {
566
+ const p = class p {
546
567
  constructor({
547
- config: n,
568
+ config: a,
548
569
  storage: i
549
570
  }) {
550
571
  if (this.resetChat = () => {
551
572
  this.sessionCtx.reset(), this.messageCtx.reset();
552
- }, !u.pollingIntervalsSeconds)
573
+ }, !p.pollingIntervalsSeconds)
553
574
  throw Error(
554
575
  "Widget polling values are not defined, did you call WidgetCtx.initialize()"
555
576
  );
556
- 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({
557
578
  api: this.api,
558
579
  config: this.config,
559
580
  storageCtx: this.storageCtx
560
- }), this.sessionCtx = new $({
581
+ }), this.sessionCtx = new q({
561
582
  api: this.api,
562
583
  contactCtx: this.contactCtx,
563
- sessionsPollingIntervalSeconds: u.pollingIntervalsSeconds.sessions
564
- }), this.messageCtx = new j({
584
+ sessionsPollingIntervalSeconds: p.pollingIntervalsSeconds.sessions
585
+ }), this.messageCtx = new $({
565
586
  config: this.config,
566
587
  api: this.api,
567
588
  sessionCtx: this.sessionCtx,
568
589
  contactCtx: this.contactCtx
569
- }), this.activeSessionPollingCtx = new B({
590
+ }), this.activeSessionPollingCtx = new _({
570
591
  api: this.api,
571
592
  config: this.config,
572
593
  sessionCtx: this.sessionCtx,
573
594
  messageCtx: this.messageCtx,
574
- sessionPollingIntervalSeconds: u.pollingIntervalsSeconds.session
595
+ sessionPollingIntervalSeconds: p.pollingIntervalsSeconds.session
575
596
  }), this.routerCtx = new N({
576
597
  config: this.config,
577
598
  contactCtx: this.contactCtx,
@@ -580,25 +601,25 @@ const u = class u {
580
601
  });
581
602
  }
582
603
  };
583
- u.pollingIntervalsSeconds = null, u.initialize = async ({
584
- config: n,
604
+ p.pollingIntervalsSeconds = null, p.initialize = async ({
605
+ config: a,
585
606
  storage: i
586
607
  }) => {
587
- var a, t;
588
- const c = await new k({
589
- config: n
608
+ var n, t;
609
+ const h = await new E({
610
+ config: a
590
611
  }).getExternalWidgetConfig();
591
- return u.pollingIntervalsSeconds = {
592
- session: ((a = c.data) == null ? void 0 : a.sessionPollingIntervalSeconds) || 10,
593
- sessions: ((t = c.data) == null ? void 0 : t.sessionsPollingIntervalSeconds) || 60
594
- }, new u({
595
- 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,
596
617
  storage: i
597
618
  });
598
619
  };
599
- let A = u;
620
+ let U = p;
600
621
  export {
601
- v as P,
602
- A as W
622
+ b as P,
623
+ U as W
603
624
  };
604
- //# sourceMappingURL=widget.ctx-CaItHAmP.js.map
625
+ //# sourceMappingURL=widget.ctx-CMgNQpCm.js.map