@opencx/widget 2.4.4-rn.0 → 2.4.4-rn.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -3,28 +3,28 @@ var N = (t) => {
3
3
  throw TypeError(t);
4
4
  };
5
5
  var q = (t, e, s) => e in t ? x(t, e, { enumerable: !0, configurable: !0, writable: !0, value: s }) : t[e] = s;
6
- var h = (t, e, s) => q(t, typeof e != "symbol" ? e + "" : e, s), D = (t, e, s) => e.has(t) || N("Cannot " + s);
7
- var S = (t, e, s) => (D(t, e, "read from private field"), s ? s.call(t) : e.get(t)), C = (t, e, s) => e.has(t) ? N("Cannot add the same private member more than once") : e instanceof WeakSet ? e.add(t) : e.set(t, s), P = (t, e, s, o) => (D(t, e, "write to private field"), o ? o.call(t, s) : e.set(t, s), s);
8
- import H from "lodash.isequal";
9
- import { g as L } from "./schemas-BBAV6Sd_.js";
10
- import { A as ue, M as de, b as fe, a as me, S as ge, d as Se, e as he, c as pe, f as Ee, s as ye } from "./schemas-BBAV6Sd_.js";
11
- var j = /* @__PURE__ */ ((t) => (t.INIT = "init", t.STATE_CHANGE = "stateChange", t.BEFORE_UPDATE = "beforeUpdate", t.AFTER_UPDATE = "afterUpdate", t.DESTROY = "destroy", t.ERROR = "error", t))(j || {}), y, A;
12
- class v {
6
+ var S = (t, e, s) => q(t, typeof e != "symbol" ? e + "" : e, s), D = (t, e, s) => e.has(t) || N("Cannot " + s);
7
+ var h = (t, e, s) => (D(t, e, "read from private field"), s ? s.call(t) : e.get(t)), M = (t, e, s) => e.has(t) ? N("Cannot add the same private member more than once") : e instanceof WeakSet ? e.add(t) : e.set(t, s), P = (t, e, s, o) => (D(t, e, "write to private field"), o ? o.call(t, s) : e.set(t, s), s);
8
+ import j from "lodash.isequal";
9
+ import { g as _ } from "./schemas-BBAV6Sd_.js";
10
+ import { A as de, M as fe, b as me, a as ge, S as he, d as Se, e as pe, c as Ee, f as ye, s as be } from "./schemas-BBAV6Sd_.js";
11
+ var H = /* @__PURE__ */ ((t) => (t.INIT = "init", t.STATE_CHANGE = "stateChange", t.BEFORE_UPDATE = "beforeUpdate", t.AFTER_UPDATE = "afterUpdate", t.DESTROY = "destroy", t.ERROR = "error", t))(H || {}), y, L;
12
+ class C {
13
13
  constructor(e) {
14
- h(this, "subscribers", /* @__PURE__ */ new Set());
15
- C(this, y);
16
- h(this, "initialState");
17
- C(this, A);
18
- h(this, "lifecycleListeners", /* @__PURE__ */ new Map());
19
- h(this, "emitLifecycle", (e, s) => {
14
+ S(this, "subscribers", /* @__PURE__ */ new Set());
15
+ M(this, y);
16
+ S(this, "initialState");
17
+ M(this, L);
18
+ S(this, "lifecycleListeners", /* @__PURE__ */ new Map());
19
+ S(this, "emitLifecycle", (e, s) => {
20
20
  const o = this.lifecycleListeners.get(e);
21
21
  if (o) {
22
- const f = {
22
+ const d = {
23
23
  type: e,
24
24
  timestamp: Date.now(),
25
25
  data: s
26
26
  };
27
- o.forEach((a) => a(f));
27
+ o.forEach((n) => n(d));
28
28
  }
29
29
  });
30
30
  /**
@@ -32,10 +32,10 @@ class v {
32
32
  * @param callback Function to call when state changes
33
33
  * @returns Unsubscribe function
34
34
  */
35
- h(this, "subscribe", (e) => (this.subscribers.add(e), e(S(this, y)), () => {
35
+ S(this, "subscribe", (e) => (this.subscribers.add(e), e(h(this, y)), () => {
36
36
  this.subscribers.delete(e);
37
37
  }));
38
- h(this, "onLifecycle", (e, s) => {
38
+ S(this, "onLifecycle", (e, s) => {
39
39
  this.lifecycleListeners.has(e) || this.lifecycleListeners.set(e, /* @__PURE__ */ new Set());
40
40
  const o = this.lifecycleListeners.get(e);
41
41
  return o.add(s), () => {
@@ -45,16 +45,16 @@ class v {
45
45
  /**
46
46
  * Get the current state
47
47
  */
48
- h(this, "getState", () => S(this, y));
48
+ S(this, "getState", () => h(this, y));
49
49
  /**
50
50
  * Set the state and notify subscribers if the state changes
51
51
  * @param newState The new state to set
52
52
  */
53
- h(this, "setState", (e) => {
53
+ S(this, "setState", (e) => {
54
54
  this.emitLifecycle("beforeUpdate", {
55
- previousState: S(this, y),
55
+ previousState: h(this, y),
56
56
  nextState: e
57
- }), H(S(this, y), e) || (P(this, y, e), P(this, A, Date.now()), this.emitLifecycle("stateChange", { state: e }), this.subscribers.forEach((s) => {
57
+ }), j(h(this, y), e) || (P(this, y, e), P(this, L, Date.now()), this.emitLifecycle("stateChange", { state: e }), this.subscribers.forEach((s) => {
58
58
  try {
59
59
  s(e);
60
60
  } catch (o) {
@@ -62,46 +62,46 @@ class v {
62
62
  }
63
63
  })), this.emitLifecycle("afterUpdate", { state: e });
64
64
  });
65
- h(this, "setStatePartial", (e) => {
66
- const s = { ...S(this, y), ...e };
65
+ S(this, "setStatePartial", (e) => {
66
+ const s = { ...h(this, y), ...e };
67
67
  this.setState(s);
68
68
  });
69
69
  /**
70
70
  * Clear all subscriptions
71
71
  */
72
- h(this, "clear", () => {
72
+ S(this, "clear", () => {
73
73
  this.emitLifecycle(
74
74
  "destroy"
75
75
  /* DESTROY */
76
76
  ), this.subscribers.clear(), this.lifecycleListeners.clear();
77
77
  });
78
- h(this, "reset", () => {
78
+ S(this, "reset", () => {
79
79
  this.setState(this.initialState);
80
80
  });
81
- h(this, "lastUpdated", () => S(this, A));
82
- P(this, y, e), this.initialState = e, P(this, A, Date.now()), this.emitLifecycle("init", { initialState: S(this, y) });
81
+ S(this, "lastUpdated", () => h(this, L));
82
+ P(this, y, e), this.initialState = e, P(this, L, Date.now()), this.emitLifecycle("init", { initialState: h(this, y) });
83
83
  }
84
84
  }
85
- y = new WeakMap(), A = new WeakMap();
85
+ y = new WeakMap(), L = new WeakMap();
86
86
  function W(t) {
87
- return new v(t);
87
+ return new C(t);
88
88
  }
89
- class M extends Error {
89
+ class v extends Error {
90
90
  constructor(e) {
91
91
  super(e), this.name = "OpenCXError";
92
92
  }
93
93
  }
94
- class Z extends M {
94
+ class Z extends v {
95
95
  constructor(e = "Connection failed") {
96
96
  super(e), this.name = "ConnectionError";
97
97
  }
98
98
  }
99
- class ee extends M {
99
+ class ee extends v {
100
100
  constructor(e = "Authentication failed") {
101
101
  super(e), this.name = "AuthenticationError";
102
102
  }
103
103
  }
104
- class B extends M {
104
+ class B extends v {
105
105
  constructor(e = "Session error occurred") {
106
106
  super(e), this.name = "SessionError";
107
107
  }
@@ -111,12 +111,12 @@ class te extends B {
111
111
  super(e), this.name = "SessionNotDefinedError";
112
112
  }
113
113
  }
114
- class se extends M {
114
+ class se extends v {
115
115
  constructor(e = "Transport error occurred") {
116
116
  super(e), this.name = "TransportError";
117
117
  }
118
118
  }
119
- class re extends M {
119
+ class re extends v {
120
120
  constructor(e = "File upload failed") {
121
121
  super(e), this.name = "FileUploadError";
122
122
  }
@@ -147,7 +147,7 @@ const R = {
147
147
  };
148
148
  function k(t) {
149
149
  const e = {
150
- id: t.publicId || L(),
150
+ id: t.publicId || _(),
151
151
  timestamp: t.sentAt || "",
152
152
  attachments: t.attachments || void 0
153
153
  };
@@ -171,113 +171,113 @@ function k(t) {
171
171
  }
172
172
  };
173
173
  }
174
- function G(t, e, s) {
175
- async function o(c) {
176
- const l = e.getState().messages;
177
- if (l.length === 0) {
178
- s == null || s.debug("No messages yet, fetching all history", { sessionId: c.id });
179
- const g = await t.getSessionHistory(c.id, "");
174
+ function J(t, e, s) {
175
+ async function o(i) {
176
+ const c = e.getState().messages;
177
+ if (c.length === 0) {
178
+ s == null || s.debug("No messages yet, fetching all history", { sessionId: i.id });
179
+ const g = await t.getSessionHistory(i.id, "");
180
180
  g && g.length > 0 && e.setStatePartial({
181
181
  messages: g.map(k)
182
182
  });
183
183
  return;
184
184
  }
185
- const d = l[l.length - 1], n = new Date(d.timestamp);
186
- n.setSeconds(n.getSeconds() + 1);
187
- const i = n.toISOString();
185
+ const l = c[c.length - 1], r = new Date(l.timestamp);
186
+ r.setSeconds(r.getSeconds() + 1);
187
+ const u = r.toISOString();
188
188
  s == null || s.debug("Fetching history messages after timestamp", {
189
- sessionId: c.id,
190
- lastMessageTimestamp: i,
191
- currentMessageCount: l.length
189
+ sessionId: i.id,
190
+ lastMessageTimestamp: u,
191
+ currentMessageCount: c.length
192
192
  });
193
- const E = await t.getSessionHistory(c.id, i);
193
+ const E = await t.getSessionHistory(i.id, u);
194
194
  if (E && E.length > 0) {
195
- const g = E.map(k).filter((p) => !l.some((b) => b.id === p.id));
195
+ const g = E.map(k).filter((p) => !c.some((I) => I.id === p.id));
196
196
  g.length > 0 && (s == null || s.debug("Adding new messages to state", {
197
197
  count: g.length,
198
198
  messageIds: g.map((p) => p.id),
199
199
  messageTypes: g.map((p) => p.type)
200
200
  }), e.setStatePartial({
201
- messages: [...l, ...g]
201
+ messages: [...c, ...g]
202
202
  }));
203
203
  }
204
204
  }
205
- function f(c, l) {
205
+ function d(i, c) {
206
206
  return {
207
- id: L(),
207
+ id: _(),
208
208
  type: "FROM_USER",
209
- content: c,
209
+ content: i,
210
210
  deliveredAt: (/* @__PURE__ */ new Date()).toISOString(),
211
- attachments: l,
211
+ attachments: c,
212
212
  timestamp: (/* @__PURE__ */ new Date()).toISOString()
213
213
  };
214
214
  }
215
- function a(c) {
216
- if (c.success && c.autopilotResponse)
215
+ function n(i) {
216
+ if (i.success && i.autopilotResponse)
217
217
  return {
218
218
  type: "FROM_BOT",
219
- id: c.autopilotResponse.id || L(),
219
+ id: i.autopilotResponse.id || _(),
220
220
  timestamp: (/* @__PURE__ */ new Date()).toISOString(),
221
221
  component: "TEXT",
222
222
  data: {
223
- message: c.autopilotResponse.value.content
223
+ message: i.autopilotResponse.value.content
224
224
  }
225
225
  };
226
- if (c.success && c.uiResponse) {
227
- const l = c.uiResponse.value;
226
+ if (i.success && i.uiResponse) {
227
+ const c = i.uiResponse.value;
228
228
  return {
229
229
  type: "FROM_BOT",
230
- id: L(),
230
+ id: _(),
231
231
  timestamp: (/* @__PURE__ */ new Date()).toISOString(),
232
- component: l.name,
233
- data: l.request_response
232
+ component: c.name,
233
+ data: c.request_response
234
234
  };
235
235
  }
236
236
  return null;
237
237
  }
238
- function r(c) {
238
+ function a(i) {
239
239
  return {
240
240
  type: "FROM_BOT",
241
- id: L(),
241
+ id: _(),
242
242
  timestamp: (/* @__PURE__ */ new Date()).toISOString(),
243
243
  component: "TEXT",
244
244
  data: {
245
- message: c,
245
+ message: i,
246
246
  variant: "error"
247
247
  }
248
248
  };
249
249
  }
250
250
  return {
251
251
  fetchHistoryMessages: o,
252
- addUserMessage: f,
253
- addBotMessage: a,
254
- addErrorMessage: r
252
+ addUserMessage: d,
253
+ addBotMessage: n,
254
+ addErrorMessage: a
255
255
  };
256
256
  }
257
- function J(t, e, s, o, f, a) {
257
+ function G(t, e, s, o, d, n) {
258
258
  var F, U;
259
- const r = (F = a.platform) == null ? void 0 : F.logger;
260
- let c = null;
261
- const l = (U = a.platform) == null ? void 0 : U.storage, d = `${f.getConfig().user.external_id}:${f.getConfig().token}:session`, n = a.config.getSettings().persistSession;
262
- async function i() {
263
- if (l)
259
+ const a = (F = n.platform) == null ? void 0 : F.logger;
260
+ let i = null;
261
+ const c = (U = n.platform) == null ? void 0 : U.storage, l = `${d.getConfig().user.external_id}:${d.getConfig().token}:session`, r = n.config.getSettings().persistSession;
262
+ async function u() {
263
+ if (c)
264
264
  try {
265
- r == null || r.debug("Attempting to restore session from storage");
266
- const u = await l.getItem(d);
267
- if (u) {
268
- const m = JSON.parse(u);
269
- r == null || r.info("Session restored from storage", { sessionId: m.id }), e.setState(m), await o.fetchHistoryMessages(m), g();
265
+ a == null || a.debug("Attempting to restore session from storage");
266
+ const f = await c.getItem(l);
267
+ if (f) {
268
+ const m = JSON.parse(f);
269
+ a == null || a.info("Session restored from storage", { sessionId: m.id }), e.setState(m), await o.fetchHistoryMessages(m), g();
270
270
  }
271
- } catch (u) {
272
- r == null || r.error("Error restoring session from storage:", u);
271
+ } catch (f) {
272
+ a == null || a.error("Error restoring session from storage:", f);
273
273
  }
274
274
  }
275
275
  function E() {
276
- l && (r == null || r.debug("Setting up session persistence"), e.subscribe(async (u) => {
276
+ c && (a == null || a.debug("Setting up session persistence"), e.subscribe(async (f) => {
277
277
  try {
278
- u ? (await l.setItem(d, JSON.stringify(u)), r == null || r.debug("Session persisted to storage", { sessionId: u.id })) : (await l.removeItem(d), r == null || r.debug("Session removed from storage"));
278
+ f ? (await c.setItem(l, JSON.stringify(f)), a == null || a.debug("Session persisted to storage", { sessionId: f.id })) : (await c.removeItem(l), a == null || a.debug("Session removed from storage"));
279
279
  } catch (m) {
280
- r == null || r.error("Error persisting session:", m), s.setStatePartial({
280
+ a == null || a.error("Error persisting session:", m), s.setStatePartial({
281
281
  error: {
282
282
  hasError: !0,
283
283
  message: m instanceof Error ? m.message : "Failed to persist session",
@@ -288,10 +288,10 @@ function J(t, e, s, o, f, a) {
288
288
  }));
289
289
  }
290
290
  function g() {
291
- if (c) return;
292
- r == null || r.debug("Starting polling");
293
- const u = [];
294
- u.push(
291
+ if (i) return;
292
+ a == null || a.debug("Starting polling");
293
+ const f = [];
294
+ f.push(
295
295
  setInterval(async () => {
296
296
  const m = e.getState();
297
297
  if (m != null && m.id)
@@ -299,36 +299,36 @@ function J(t, e, s, o, f, a) {
299
299
  const w = await t.getSession(m.id);
300
300
  w && e.setState(w);
301
301
  } catch (w) {
302
- r == null || r.error("Error polling session:", w);
302
+ a == null || a.error("Error polling session:", w);
303
303
  }
304
304
  }, R.SESSION)
305
- ), u.push(
305
+ ), f.push(
306
306
  setInterval(async () => {
307
307
  const m = e.getState();
308
308
  if (m != null && m.id)
309
309
  try {
310
310
  await o.fetchHistoryMessages(m);
311
311
  } catch (w) {
312
- r == null || r.error("Error polling messages:", w);
312
+ a == null || a.error("Error polling messages:", w);
313
313
  }
314
314
  }, R.MESSAGES)
315
- ), c = () => {
316
- r == null || r.debug("Stopping polling"), u.forEach(clearInterval);
315
+ ), i = () => {
316
+ a == null || a.debug("Stopping polling"), f.forEach(clearInterval);
317
317
  };
318
318
  }
319
319
  async function p() {
320
320
  try {
321
- r == null || r.info("Creating new session"), s.setStatePartial({
321
+ a == null || a.info("Creating new session"), s.setStatePartial({
322
322
  loading: { isLoading: !0, reason: "creating_session" },
323
323
  error: { hasError: !1 }
324
324
  });
325
- const u = await t.createSession();
326
- return r == null || r.info("Session created successfully", { sessionId: u.id }), e.setState(u), g(), u;
327
- } catch (u) {
328
- r == null || r.error("Failed to create session:", u);
325
+ const f = await t.createSession();
326
+ return a == null || a.info("Session created successfully", { sessionId: f.id }), e.setState(f), g(), f;
327
+ } catch (f) {
328
+ a == null || a.error("Failed to create session:", f);
329
329
  const m = {
330
330
  hasError: !0,
331
- message: u instanceof Error ? u.message : "Failed to create session",
331
+ message: f instanceof Error ? f.message : "Failed to create session",
332
332
  code: "SESSION_CREATION_FAILED"
333
333
  };
334
334
  return s.setStatePartial({ error: m }), null;
@@ -338,17 +338,17 @@ function J(t, e, s, o, f, a) {
338
338
  });
339
339
  }
340
340
  }
341
- async function b() {
341
+ async function I() {
342
342
  var m;
343
- const u = e.getState();
344
- if (u != null && u.id)
343
+ const f = e.getState();
344
+ if (f != null && f.id)
345
345
  try {
346
- c && (c(), c = null), e.setState(null), n && l && T(l) && l.removeItem(d), s.setState({
346
+ i && (i(), i = null), e.setState(null), r && c && T(c) && c.removeItem(l), s.setState({
347
347
  messages: [],
348
348
  keyboard: null,
349
349
  loading: { isLoading: !1 },
350
350
  error: { hasError: !1 }
351
- }), (m = a.onSessionDestroy) == null || m.call(a);
351
+ }), (m = n.onSessionDestroy) == null || m.call(n);
352
352
  } catch (w) {
353
353
  console.error("Error clearing session:", w), s.setStatePartial({
354
354
  error: {
@@ -359,91 +359,91 @@ function J(t, e, s, o, f, a) {
359
359
  });
360
360
  }
361
361
  }
362
- function _() {
362
+ function A() {
363
363
  try {
364
- c && (c(), c = null), n && l && T(l) && l.removeItem(d), s.setState({
364
+ i && (i(), i = null), r && c && T(c) && c.removeItem(l), s.setState({
365
365
  messages: [],
366
366
  keyboard: null,
367
367
  loading: { isLoading: !1 },
368
368
  error: { hasError: !1 }
369
369
  }), e.setState(null), s.clear(), e.clear();
370
- } catch (u) {
371
- console.error("Error in cleanup:", u), s.setStatePartial({
370
+ } catch (f) {
371
+ console.error("Error in cleanup:", f), s.setStatePartial({
372
372
  error: {
373
373
  hasError: !0,
374
- message: u instanceof Error ? u.message : "Failed to cleanup",
374
+ message: f instanceof Error ? f.message : "Failed to cleanup",
375
375
  code: "SESSION_CLEAR_FAILED"
376
376
  }
377
377
  });
378
378
  }
379
379
  }
380
- return n && T(l) && (i(), E()), {
380
+ return r && T(c) && (u(), E()), {
381
381
  createSession: p,
382
- clearSession: b,
383
- cleanup: _,
382
+ clearSession: I,
383
+ cleanup: A,
384
384
  startPolling: g
385
385
  };
386
386
  }
387
387
  function ae(t) {
388
- var c;
389
- const e = (c = t.platform) == null ? void 0 : c.logger;
388
+ var i;
389
+ const e = (i = t.platform) == null ? void 0 : i.logger;
390
390
  e == null || e.info("Initializing chat");
391
- const s = new v({
391
+ const s = new C({
392
392
  messages: [],
393
393
  keyboard: null,
394
394
  loading: { isLoading: !1 },
395
395
  error: { hasError: !1 }
396
- }), o = new v(null), f = G(t.api, s, e), a = J(
396
+ }), o = new C(null), d = J(t.api, s, e), n = G(
397
397
  t.api,
398
398
  o,
399
399
  s,
400
- f,
400
+ d,
401
401
  t.config,
402
402
  t
403
403
  );
404
- async function r(l) {
405
- var n;
406
- let d = o.getState();
407
- if (!(d != null && d.id) && (e == null || e.debug("No active session, creating new session"), d = await a.createSession(), !d))
404
+ async function a(c) {
405
+ var r;
406
+ let l = o.getState();
407
+ if (!(l != null && l.id) && (e == null || e.debug("No active session, creating new session"), l = await n.createSession(), !l))
408
408
  return !1;
409
409
  try {
410
- e == null || e.debug("Sending message", { sessionId: d.id }), s.setStatePartial({
410
+ e == null || e.debug("Sending message", { sessionId: l.id }), s.setStatePartial({
411
411
  loading: { isLoading: !0, reason: "sending_message" },
412
412
  error: { hasError: !1 }
413
413
  });
414
- const i = f.addUserMessage(l.content, l.attachments || void 0), E = s.getState().messages;
414
+ const u = d.addUserMessage(c.content, c.attachments || void 0), E = s.getState().messages;
415
415
  s.setStatePartial({
416
- messages: [...E, i]
416
+ messages: [...E, u]
417
417
  });
418
418
  const g = t.config.getConfig(), p = await t.api.handleMessage({
419
419
  bot_token: g.token,
420
420
  headers: g.headers,
421
421
  query_params: g.queryParams,
422
- session_id: d.id,
423
- ...l
422
+ session_id: l.id,
423
+ ...c
424
424
  });
425
425
  if (p.success) {
426
426
  e == null || e.debug("Message sent successfully");
427
- const b = f.addBotMessage(p);
428
- if (b) {
429
- const _ = s.getState().messages;
427
+ const I = d.addBotMessage(p);
428
+ if (I) {
429
+ const A = s.getState().messages;
430
430
  s.setStatePartial({
431
- messages: [..._, b]
431
+ messages: [...A, I]
432
432
  });
433
433
  }
434
434
  } else {
435
435
  e == null || e.warn("Message send failed", p.error);
436
- const b = f.addErrorMessage(((n = p.error) == null ? void 0 : n.message) || "Unknown error occurred"), _ = s.getState().messages;
436
+ const I = d.addErrorMessage(((r = p.error) == null ? void 0 : r.message) || "Unknown error occurred"), A = s.getState().messages;
437
437
  s.setStatePartial({
438
- messages: [..._, b]
438
+ messages: [...A, I]
439
439
  });
440
440
  }
441
441
  return !0;
442
- } catch (i) {
443
- return e == null || e.error("Error sending message:", i), s.setStatePartial({
442
+ } catch (u) {
443
+ return e == null || e.error("Error sending message:", u), s.setStatePartial({
444
444
  error: {
445
445
  hasError: !0,
446
- message: i instanceof Error ? i.message : "Failed to send message",
446
+ message: u instanceof Error ? u.message : "Failed to send message",
447
447
  code: "MESSAGE_SEND_FAILED"
448
448
  }
449
449
  }), !1;
@@ -456,94 +456,94 @@ function ae(t) {
456
456
  return {
457
457
  chatState: s,
458
458
  sessionState: o,
459
- sendMessage: r,
460
- createSession: a.createSession,
461
- clearSession: a.clearSession,
462
- cleanup: a.cleanup
459
+ sendMessage: a,
460
+ createSession: n.createSession,
461
+ clearSession: n.clearSession,
462
+ cleanup: n.cleanup
463
463
  };
464
464
  }
465
465
  function ne(t) {
466
466
  const e = t.config.getConfig();
467
467
  t.config.getConfig;
468
468
  const s = `${e.token}:contact:${e.user.external_id}`, o = T(t.platform.storage) ? t.platform.storage : void 0;
469
- let f = null;
469
+ let d = null;
470
470
  o && O(
471
471
  async () => {
472
- const n = await o.getItem(s);
473
- return n ? JSON.parse(n) : null;
472
+ const r = await o.getItem(s);
473
+ return r ? JSON.parse(r) : null;
474
474
  },
475
475
  "Error loading contact from storage"
476
- ).then((n) => {
477
- n.success && (f = n.result);
476
+ ).then((r) => {
477
+ r.success && (d = r.result);
478
478
  });
479
- const a = new v({
480
- contact: f,
479
+ const n = new C({
480
+ contact: d,
481
481
  loading: { isLoading: !1 },
482
482
  error: { hasError: !1 }
483
483
  });
484
- T(o) && a.subscribe((n) => {
484
+ T(o) && n.subscribe((r) => {
485
485
  O(
486
486
  async () => {
487
- n.contact ? await o.setItem(s, JSON.stringify(n.contact)) : o.removeItem(s);
487
+ r.contact ? await o.setItem(s, JSON.stringify(r.contact)) : o.removeItem(s);
488
488
  },
489
489
  "Error persisting contact state"
490
- ).then((i) => {
491
- i.success || a.setStatePartial({
490
+ ).then((u) => {
491
+ u.success || n.setStatePartial({
492
492
  error: {
493
493
  hasError: !0,
494
- message: i.error.message,
494
+ message: u.error.message,
495
495
  code: "CONTACT_PERSISTENCE_FAILED"
496
496
  }
497
497
  });
498
498
  });
499
499
  });
500
- async function r() {
500
+ async function a() {
501
501
  try {
502
- if (a.setStatePartial({
502
+ if (n.setStatePartial({
503
503
  loading: { isLoading: !0, reason: "loading_contact" },
504
504
  error: { hasError: !1 }
505
505
  }), o) {
506
- const n = await O(
506
+ const r = await O(
507
507
  async () => {
508
- const i = await o.getItem(s);
509
- return i ? JSON.parse(i) : null;
508
+ const u = await o.getItem(s);
509
+ return u ? JSON.parse(u) : null;
510
510
  },
511
511
  "Error loading contact"
512
512
  );
513
- if (!n.success)
514
- throw n.error;
515
- return a.setStatePartial({
516
- contact: n.result,
513
+ if (!r.success)
514
+ throw r.error;
515
+ return n.setStatePartial({
516
+ contact: r.result,
517
517
  error: { hasError: !1 }
518
- }), n.result;
518
+ }), r.result;
519
519
  }
520
- return a.getState().contact;
521
- } catch (n) {
522
- return a.setStatePartial({
520
+ return n.getState().contact;
521
+ } catch (r) {
522
+ return n.setStatePartial({
523
523
  error: {
524
524
  hasError: !0,
525
- message: n instanceof Error ? n.message : "Failed to load contact",
525
+ message: r instanceof Error ? r.message : "Failed to load contact",
526
526
  code: "CONTACT_LOAD_FAILED"
527
527
  }
528
528
  }), null;
529
529
  } finally {
530
- a.setStatePartial({
530
+ n.setStatePartial({
531
531
  loading: { isLoading: !1, reason: null }
532
532
  });
533
533
  }
534
534
  }
535
- async function c(n) {
535
+ async function i(r) {
536
536
  try {
537
- a.setStatePartial({
537
+ n.setStatePartial({
538
538
  loading: { isLoading: !0, reason: "saving_contact" },
539
539
  error: { hasError: !1 }
540
540
  });
541
- const i = a.getState().contact, E = {
542
- id: n.id || (i == null ? void 0 : i.id) || "",
543
- name: n.name ?? (i == null ? void 0 : i.name) ?? null,
544
- created_at: n.created_at || (i == null ? void 0 : i.created_at) || (/* @__PURE__ */ new Date()).toISOString(),
545
- avatar_url: n.avatar_url ?? (i == null ? void 0 : i.avatar_url) ?? null,
546
- email: n.email ?? (i == null ? void 0 : i.email) ?? null
541
+ const u = n.getState().contact, E = {
542
+ id: r.id || (u == null ? void 0 : u.id) || "",
543
+ name: r.name ?? (u == null ? void 0 : u.name) ?? null,
544
+ created_at: r.created_at || (u == null ? void 0 : u.created_at) || (/* @__PURE__ */ new Date()).toISOString(),
545
+ avatar_url: r.avatar_url ?? (u == null ? void 0 : u.avatar_url) ?? null,
546
+ email: r.email ?? (u == null ? void 0 : u.email) ?? null
547
547
  };
548
548
  if (T(o)) {
549
549
  const g = await O(
@@ -553,71 +553,71 @@ function ne(t) {
553
553
  if (!g.success)
554
554
  throw g.error;
555
555
  }
556
- return a.setStatePartial({
556
+ return n.setStatePartial({
557
557
  contact: E,
558
558
  error: { hasError: !1 }
559
559
  }), E;
560
- } catch (i) {
561
- return a.setStatePartial({
560
+ } catch (u) {
561
+ return n.setStatePartial({
562
562
  error: {
563
563
  hasError: !0,
564
- message: i instanceof Error ? i.message : "Failed to save contact",
564
+ message: u instanceof Error ? u.message : "Failed to save contact",
565
565
  code: "CONTACT_SAVE_FAILED"
566
566
  }
567
567
  }), null;
568
568
  } finally {
569
- a.setStatePartial({
569
+ n.setStatePartial({
570
570
  loading: { isLoading: !1, reason: null }
571
571
  });
572
572
  }
573
573
  }
574
- function l() {
575
- var i;
576
- return !((i = a.getState().contact) != null && i.id) && e.collectUserData ? {
574
+ function c() {
575
+ var u;
576
+ return !((u = n.getState().contact) != null && u.id) && e.collectUserData ? {
577
577
  should: !0,
578
578
  reason: "No contact id and collectUserData is true"
579
579
  } : {
580
580
  should: !1
581
581
  };
582
582
  }
583
- async function d() {
583
+ async function l() {
584
584
  try {
585
- if (a.setStatePartial({
585
+ if (n.setStatePartial({
586
586
  loading: { isLoading: !0, reason: "cleaning_up" },
587
587
  error: { hasError: !1 }
588
588
  }), o) {
589
- const n = await O(
589
+ const r = await O(
590
590
  () => o.removeItem(s),
591
591
  "Error removing contact data"
592
592
  );
593
- if (!n.success)
594
- throw n.error;
593
+ if (!r.success)
594
+ throw r.error;
595
595
  }
596
- a.setState({
596
+ n.setState({
597
597
  contact: null,
598
598
  loading: { isLoading: !1 },
599
599
  error: { hasError: !1 }
600
- }), a.clear();
601
- } catch (n) {
602
- console.error("Error cleaning up contact:", n), a.setStatePartial({
600
+ }), n.clear();
601
+ } catch (r) {
602
+ console.error("Error cleaning up contact:", r), n.setStatePartial({
603
603
  error: {
604
604
  hasError: !0,
605
- message: n instanceof Error ? n.message : "Failed to cleanup contact data",
605
+ message: r instanceof Error ? r.message : "Failed to cleanup contact data",
606
606
  code: "CONTACT_CLEANUP_FAILED"
607
607
  }
608
608
  });
609
609
  } finally {
610
- a.setStatePartial({
610
+ n.setStatePartial({
611
611
  loading: { isLoading: !1, reason: null }
612
612
  });
613
613
  }
614
614
  }
615
615
  return {
616
- contactState: a,
617
- shouldCollectData: l,
618
- loadContact: r,
619
- saveContact: c,
620
- cleanup: d
616
+ contactState: n,
617
+ shouldCollectData: c,
618
+ loadContact: a,
619
+ saveContact: i,
620
+ cleanup: l
621
621
  };
622
622
  }
623
623
  const X = {
@@ -627,7 +627,7 @@ const X = {
627
627
  triggerOffset: "20px"
628
628
  }, z = 1e3 * 3;
629
629
  function oe(t) {
630
- var s, o, f, a, r, c, l, d, n;
630
+ var s, o, d, n, a, i, c, l, r;
631
631
  if (!t.token)
632
632
  throw new Error("Token is required");
633
633
  if (t.pollingInterval && t.pollingInterval < z)
@@ -644,23 +644,23 @@ function oe(t) {
644
644
  bot: {
645
645
  name: ((s = t.bot) == null ? void 0 : s.name) ?? "Bot",
646
646
  avatarUrl: (o = t.bot) == null ? void 0 : o.avatarUrl,
647
- id: ((f = t.bot) == null ? void 0 : f.id) ?? null,
648
- is_ai: ((a = t.bot) == null ? void 0 : a.is_ai) ?? !0
647
+ id: ((d = t.bot) == null ? void 0 : d.id) ?? null,
648
+ is_ai: ((n = t.bot) == null ? void 0 : n.is_ai) ?? !0
649
649
  },
650
650
  contactToken: t.contactToken,
651
651
  debug: t.debug ?? !1,
652
652
  language: t.language ?? "en",
653
653
  user: t.user ?? {},
654
654
  soundEffectFiles: {
655
- messageArrived: ((r = t.soundEffectFiles) == null ? void 0 : r.messageArrived) ?? X.messageArrived
655
+ messageArrived: ((a = t.soundEffectFiles) == null ? void 0 : a.messageArrived) ?? X.messageArrived
656
656
  },
657
657
  theme: {
658
- primaryColor: ((c = t.theme) == null ? void 0 : c.primaryColor) ?? $.primaryColor,
659
- triggerOffset: ((l = t.theme) == null ? void 0 : l.triggerOffset) ?? $.triggerOffset
658
+ primaryColor: ((i = t.theme) == null ? void 0 : i.primaryColor) ?? $.primaryColor,
659
+ triggerOffset: ((c = t.theme) == null ? void 0 : c.triggerOffset) ?? $.triggerOffset
660
660
  },
661
661
  settings: {
662
- persistSession: ((d = t.settings) == null ? void 0 : d.persistSession) ?? !1,
663
- useSoundEffects: ((n = t.settings) == null ? void 0 : n.useSoundEffects) ?? !1
662
+ persistSession: ((l = t.settings) == null ? void 0 : l.persistSession) ?? !1,
663
+ useSoundEffects: ((r = t.settings) == null ? void 0 : r.useSoundEffects) ?? !1
664
664
  }
665
665
  };
666
666
  return {
@@ -682,74 +682,74 @@ function oe(t) {
682
682
  };
683
683
  }
684
684
  function V(t = {}) {
685
- const e = [], s = [], o = [], f = async (a, r = {}) => {
686
- let c = {
685
+ const e = [], s = [], o = [], d = async (n, a = {}) => {
686
+ let i = {
687
687
  ...t,
688
- ...r,
688
+ ...a,
689
689
  headers: {
690
690
  ...t.headers,
691
- ...r.headers
691
+ ...a.headers
692
692
  }
693
693
  };
694
694
  try {
695
- for (const i of e)
696
- c = await i(c);
697
- const l = c.params ? "?" + new URLSearchParams(c.params).toString() : "", d = c.baseURL ? `${c.baseURL}${a}${l}`.replace(/([^:]\/)\/+/g, "$1") : `${a}${l}`;
698
- let n = await fetch(d, c);
699
- for (const i of s)
700
- n = await i(n);
701
- return n;
702
- } catch (l) {
703
- let d = l;
704
- for (const n of o)
705
- d = await n(d);
706
- throw d;
695
+ for (const u of e)
696
+ i = await u(i);
697
+ const c = i.params ? "?" + new URLSearchParams(i.params).toString() : "", l = i.baseURL ? `${i.baseURL}${n}${c}`.replace(/([^:]\/)\/+/g, "$1") : `${n}${c}`;
698
+ let r = await fetch(l, i);
699
+ for (const u of s)
700
+ r = await u(r);
701
+ return r;
702
+ } catch (c) {
703
+ let l = c;
704
+ for (const r of o)
705
+ l = await r(l);
706
+ throw l;
707
707
  }
708
708
  };
709
- return f.interceptors = {
709
+ return d.interceptors = {
710
710
  request: {
711
- use: (a) => (e.push(a), e.length - 1),
712
- eject: (a) => {
713
- e.splice(a, 1);
711
+ use: (n) => (e.push(n), e.length - 1),
712
+ eject: (n) => {
713
+ e.splice(n, 1);
714
714
  }
715
715
  },
716
716
  response: {
717
- use: (a, r) => (s.push(a), r && o.push(r), s.length - 1),
718
- eject: (a) => {
719
- s.splice(a, 1), o.splice(a, 1);
717
+ use: (n, a) => (s.push(n), a && o.push(a), s.length - 1),
718
+ eject: (n) => {
719
+ s.splice(n, 1), o.splice(n, 1);
720
720
  }
721
721
  }
722
- }, f;
722
+ }, d;
723
723
  }
724
- var I;
724
+ var b;
725
725
  class ie {
726
726
  constructor(e) {
727
- C(this, I);
727
+ M(this, b);
728
728
  this.options = e;
729
729
  const s = this.options.config.user, o = {
730
730
  claim: "",
731
731
  value: ""
732
732
  };
733
733
  s != null && s.email ? (o.claim = "email", o.value = s.email) : s != null && s.phone && (o.claim = "phone", o.value = s.phone);
734
- const f = {
734
+ const d = {
735
735
  "X-Bot-Token": this.options.config.token,
736
736
  "X-Consumer-Id": `${o.claim}:${o.value}`,
737
737
  "Content-Type": "application/json",
738
738
  Accept: "application/json"
739
739
  };
740
- this.options.config.contactToken && (f.Authorization = `Bearer ${this.options.config.contactToken}`), P(this, I, V({
740
+ this.options.config.contactToken && (d.Authorization = `Bearer ${this.options.config.contactToken}`), P(this, b, V({
741
741
  baseURL: `${this.options.config.apiUrl}/widget/v2`,
742
- headers: f
742
+ headers: d
743
743
  }));
744
744
  }
745
745
  async me() {
746
- return (await S(this, I).call(this, "/me")).json();
746
+ return (await h(this, b).call(this, "/me")).json();
747
747
  }
748
748
  async widgetPrelude() {
749
- return (await S(this, I).call(this, "/prelude")).json();
749
+ return (await h(this, b).call(this, "/prelude")).json();
750
750
  }
751
751
  async handleMessage(e) {
752
- return (await S(this, I).call(this, "/chat/send", {
752
+ return (await h(this, b).call(this, "/chat/send", {
753
753
  method: "POST",
754
754
  body: JSON.stringify(e)
755
755
  })).json();
@@ -757,55 +757,110 @@ class ie {
757
757
  async getSessionHistory(e, s) {
758
758
  const o = new URLSearchParams({
759
759
  lastMessageTimestamp: s || ""
760
- }), f = `/session/history/${e}?${o.toString()}`;
761
- return (await S(this, I).call(this, f, {
760
+ }), d = `/session/history/${e}?${o.toString()}`;
761
+ return (await h(this, b).call(this, d, {
762
762
  method: "GET"
763
763
  })).json();
764
764
  }
765
765
  async createSession() {
766
- return (await S(this, I).call(this, "/create-session", {
766
+ return (await h(this, b).call(this, "/create-session", {
767
767
  method: "POST"
768
768
  })).json();
769
769
  }
770
770
  async getSession(e) {
771
- return (await S(this, I).call(this, `/session/${e}`, {
771
+ return (await h(this, b).call(this, `/session/${e}`, {
772
772
  method: "GET"
773
773
  })).json();
774
774
  }
775
775
  async createContact(e) {
776
- return (await S(this, I).call(this, "/contact/upsert", {
776
+ return (await h(this, b).call(this, "/contact/upsert", {
777
777
  method: "POST",
778
778
  body: JSON.stringify(e)
779
779
  })).json();
780
780
  }
781
781
  }
782
- I = new WeakMap();
782
+ b = new WeakMap();
783
+ function ce(t = {}) {
784
+ const {
785
+ level: e = "info",
786
+ prefix: s = "[openCx]",
787
+ enabled: o = !0
788
+ } = t;
789
+ let d = e;
790
+ const n = {
791
+ debug: 0,
792
+ info: 1,
793
+ warn: 2,
794
+ error: 3
795
+ };
796
+ function a(l) {
797
+ return o && n[l] >= n[d];
798
+ }
799
+ function i(l) {
800
+ return `${s} ${l}`;
801
+ }
802
+ function c(l) {
803
+ return l.map((r) => {
804
+ if (r instanceof Error)
805
+ return {
806
+ name: r.name,
807
+ message: r.message,
808
+ stack: r.stack
809
+ };
810
+ if (typeof r == "object")
811
+ try {
812
+ return JSON.stringify(r, null, 2);
813
+ } catch {
814
+ return r;
815
+ }
816
+ return r;
817
+ });
818
+ }
819
+ return {
820
+ debug(l, ...r) {
821
+ a("debug") && console.debug(i(l), ...c(r));
822
+ },
823
+ info(l, ...r) {
824
+ a("info") && console.info(i(l), ...c(r));
825
+ },
826
+ warn(l, ...r) {
827
+ a("warn") && console.warn(i(l), ...c(r));
828
+ },
829
+ error(l, ...r) {
830
+ a("error") && console.error(i(l), ...c(r));
831
+ },
832
+ setLevel(l) {
833
+ d = l;
834
+ }
835
+ };
836
+ }
783
837
  export {
784
- ue as AIClosureType,
838
+ de as AIClosureType,
785
839
  ie as ApiCaller,
786
840
  ee as AuthenticationError,
787
841
  Z as ConnectionError,
788
842
  re as FileUploadError,
789
- j as LifecycleEvent,
790
- de as MessageTypeEnum,
791
- M as OpenCXError,
792
- v as PubSub,
793
- fe as SentimentEnum,
794
- me as SessionChannel,
843
+ H as LifecycleEvent,
844
+ fe as MessageTypeEnum,
845
+ v as OpenCXError,
846
+ C as PubSub,
847
+ me as SentimentEnum,
848
+ ge as SessionChannel,
795
849
  B as SessionError,
796
850
  te as SessionNotDefinedError,
797
- ge as SessionStatus,
851
+ he as SessionStatus,
798
852
  se as TransportError,
799
853
  Se as chatAttachmentSchema,
800
- he as chatHistoryMessageSchema,
801
- pe as chatSessionSchema,
802
- Ee as consumerSchema,
854
+ pe as chatHistoryMessageSchema,
855
+ Ee as chatSessionSchema,
856
+ ye as consumerSchema,
803
857
  ae as createChat,
804
858
  oe as createConfig,
805
859
  ne as createContact,
860
+ ce as createLogger,
806
861
  W as createPubSub,
807
862
  T as isStorageAvailable,
808
863
  O as safeStorageOperation,
809
- ye as structuredSocketMessageSchema
864
+ be as structuredSocketMessageSchema
810
865
  };
811
866
  //# sourceMappingURL=index.js.map