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

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.
Files changed (43) hide show
  1. package/dist/basic.cjs +31 -31
  2. package/dist/basic.cjs.map +1 -1
  3. package/dist/basic.js +37 -38
  4. package/dist/basic.js.map +1 -1
  5. package/dist/core/client/api.d.ts +6 -15
  6. package/dist/core/client/chat.d.ts +10 -10
  7. package/dist/core/client/contact.d.ts +2 -2
  8. package/dist/core/types/helpers.d.ts +1 -1
  9. package/dist/core/types/index.d.ts +2 -3
  10. package/dist/core/types/schemas-v2.d.ts +92 -91
  11. package/dist/core/types/schemas.d.ts +123 -69
  12. package/dist/{index-ENMg5VOh.js → index-BWogWLb5.js} +964 -864
  13. package/dist/index-BWogWLb5.js.map +1 -0
  14. package/dist/index-D-1u4USy.cjs +18 -0
  15. package/dist/index-D-1u4USy.cjs.map +1 -0
  16. package/dist/index.cjs +1 -1
  17. package/dist/index.cjs.map +1 -1
  18. package/dist/index.js +190 -189
  19. package/dist/index.js.map +1 -1
  20. package/dist/package.json.d.ts +1 -1
  21. package/dist/react-web/hooks/useAbstractChat.d.ts +22 -21
  22. package/dist/react-web/hooks/useAxiosInstance.d.ts +35 -23
  23. package/dist/react-web/hooks/useUploadFiles.d.ts +1 -1
  24. package/dist/react-web/providers/ChatProvider.d.ts +24 -24
  25. package/dist/react-web/providers/ContactProvider.d.ts +4 -4
  26. package/dist/react.cjs +1 -1
  27. package/dist/react.js +1 -1
  28. package/dist/schemas-v2-BiJWHypc.cjs +2 -0
  29. package/dist/schemas-v2-BiJWHypc.cjs.map +1 -0
  30. package/dist/schemas-v2-DZ3nwfID.js +138 -0
  31. package/dist/schemas-v2-DZ3nwfID.js.map +1 -0
  32. package/dist/src/components/RenderFile.d.ts +1 -1
  33. package/dist/src/components/dialog.d.ts +2 -2
  34. package/dist-embed/script.js +107 -107
  35. package/dist-embed/script.js.map +1 -1
  36. package/package.json +1 -1
  37. package/dist/index-Bx4hrNc7.cjs +0 -18
  38. package/dist/index-Bx4hrNc7.cjs.map +0 -1
  39. package/dist/index-ENMg5VOh.js.map +0 -1
  40. package/dist/schemas-BBAV6Sd_.js +0 -122
  41. package/dist/schemas-BBAV6Sd_.js.map +0 -1
  42. package/dist/schemas-wky4fpbc.cjs +0 -2
  43. package/dist/schemas-wky4fpbc.cjs.map +0 -1
package/dist/index.js CHANGED
@@ -1,30 +1,30 @@
1
- var x = Object.defineProperty;
2
- var N = (t) => {
1
+ var j = Object.defineProperty;
2
+ var D = (t) => {
3
3
  throw TypeError(t);
4
4
  };
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 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;
5
+ var B = (t, e, s) => e in t ? j(t, e, { enumerable: !0, configurable: !0, writable: !0, value: s }) : t[e] = s;
6
+ var S = (t, e, s) => B(t, typeof e != "symbol" ? e + "" : e, s), R = (t, e, s) => e.has(t) || D("Cannot " + s);
7
+ var h = (t, e, s) => (R(t, e, "read from private field"), s ? s.call(t) : e.get(t)), M = (t, e, s) => e.has(t) ? D("Cannot add the same private member more than once") : e instanceof WeakSet ? e.add(t) : e.set(t, s), _ = (t, e, s, o) => (R(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 O } from "./schemas-v2-DZ3nwfID.js";
10
+ import { M as me, c as ge } from "./schemas-v2-DZ3nwfID.js";
11
+ var G = /* @__PURE__ */ ((t) => (t.INIT = "init", t.STATE_CHANGE = "stateChange", t.BEFORE_UPDATE = "beforeUpdate", t.AFTER_UPDATE = "afterUpdate", t.DESTROY = "destroy", t.ERROR = "error", t))(G || {}), y, P;
12
12
  class C {
13
13
  constructor(e) {
14
14
  S(this, "subscribers", /* @__PURE__ */ new Set());
15
15
  M(this, y);
16
16
  S(this, "initialState");
17
- M(this, L);
17
+ M(this, P);
18
18
  S(this, "lifecycleListeners", /* @__PURE__ */ new Map());
19
19
  S(this, "emitLifecycle", (e, s) => {
20
20
  const o = this.lifecycleListeners.get(e);
21
21
  if (o) {
22
- const d = {
22
+ const f = {
23
23
  type: e,
24
24
  timestamp: Date.now(),
25
25
  data: s
26
26
  };
27
- o.forEach((n) => n(d));
27
+ o.forEach((n) => n(f));
28
28
  }
29
29
  });
30
30
  /**
@@ -54,7 +54,7 @@ class C {
54
54
  this.emitLifecycle("beforeUpdate", {
55
55
  previousState: h(this, y),
56
56
  nextState: e
57
- }), j(h(this, y), e) || (P(this, y, e), P(this, L, Date.now()), this.emitLifecycle("stateChange", { state: e }), this.subscribers.forEach((s) => {
57
+ }), H(h(this, y), e) || (_(this, y, e), _(this, P, Date.now()), this.emitLifecycle("stateChange", { state: e }), this.subscribers.forEach((s) => {
58
58
  try {
59
59
  s(e);
60
60
  } catch (o) {
@@ -78,12 +78,12 @@ class C {
78
78
  S(this, "reset", () => {
79
79
  this.setState(this.initialState);
80
80
  });
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) });
81
+ S(this, "lastUpdated", () => h(this, P));
82
+ _(this, y, e), this.initialState = e, _(this, P, Date.now()), this.emitLifecycle("init", { initialState: h(this, y) });
83
83
  }
84
84
  }
85
- y = new WeakMap(), L = new WeakMap();
86
- function W(t) {
85
+ y = new WeakMap(), P = new WeakMap();
86
+ function ee(t) {
87
87
  return new C(t);
88
88
  }
89
89
  class v extends Error {
@@ -91,37 +91,37 @@ class v extends Error {
91
91
  super(e), this.name = "OpenCXError";
92
92
  }
93
93
  }
94
- class Z extends v {
94
+ class te extends v {
95
95
  constructor(e = "Connection failed") {
96
96
  super(e), this.name = "ConnectionError";
97
97
  }
98
98
  }
99
- class ee extends v {
99
+ class se extends v {
100
100
  constructor(e = "Authentication failed") {
101
101
  super(e), this.name = "AuthenticationError";
102
102
  }
103
103
  }
104
- class B extends v {
104
+ class J extends v {
105
105
  constructor(e = "Session error occurred") {
106
106
  super(e), this.name = "SessionError";
107
107
  }
108
108
  }
109
- class te extends B {
109
+ class re extends J {
110
110
  constructor(e = "Session not defined") {
111
111
  super(e), this.name = "SessionNotDefinedError";
112
112
  }
113
113
  }
114
- class se extends v {
114
+ class ae extends v {
115
115
  constructor(e = "Transport error occurred") {
116
116
  super(e), this.name = "TransportError";
117
117
  }
118
118
  }
119
- class re extends v {
119
+ class ne extends v {
120
120
  constructor(e = "File upload failed") {
121
121
  super(e), this.name = "FileUploadError";
122
122
  }
123
123
  }
124
- function T(t) {
124
+ function L(t) {
125
125
  if (!t) return !1;
126
126
  try {
127
127
  return typeof t.isAvailable == "function" ? t.isAvailable() : !0;
@@ -129,7 +129,7 @@ function T(t) {
129
129
  return !1;
130
130
  }
131
131
  }
132
- async function O(t, e) {
132
+ async function A(t, e) {
133
133
  try {
134
134
  return { success: !0, result: await t() };
135
135
  } catch (s) {
@@ -139,15 +139,15 @@ async function O(t, e) {
139
139
  };
140
140
  }
141
141
  }
142
- const R = {
142
+ const k = {
143
143
  SESSION: 1e4,
144
144
  // every 10 seconds
145
145
  MESSAGES: 5e3
146
146
  // every 5 seconds
147
147
  };
148
- function k(t) {
148
+ function $(t) {
149
149
  const e = {
150
- id: t.publicId || _(),
150
+ id: t.publicId || O(),
151
151
  timestamp: t.sentAt || "",
152
152
  attachments: t.attachments || void 0
153
153
  };
@@ -171,44 +171,44 @@ function k(t) {
171
171
  }
172
172
  };
173
173
  }
174
- function J(t, e, s) {
174
+ function X(t, e, s) {
175
175
  async function o(i) {
176
- const c = e.getState().messages;
177
- if (c.length === 0) {
176
+ const l = e.getState().messages;
177
+ if (l.length === 0) {
178
178
  s == null || s.debug("No messages yet, fetching all history", { sessionId: i.id });
179
179
  const g = await t.getSessionHistory(i.id, "");
180
180
  g && g.length > 0 && e.setStatePartial({
181
- messages: g.map(k)
181
+ messages: g.map($)
182
182
  });
183
183
  return;
184
184
  }
185
- const l = c[c.length - 1], r = new Date(l.timestamp);
185
+ const c = l[l.length - 1], r = new Date(c.timestamp);
186
186
  r.setSeconds(r.getSeconds() + 1);
187
187
  const u = r.toISOString();
188
188
  s == null || s.debug("Fetching history messages after timestamp", {
189
189
  sessionId: i.id,
190
190
  lastMessageTimestamp: u,
191
- currentMessageCount: c.length
191
+ currentMessageCount: l.length
192
192
  });
193
193
  const E = await t.getSessionHistory(i.id, u);
194
194
  if (E && E.length > 0) {
195
- const g = E.map(k).filter((p) => !c.some((I) => I.id === p.id));
195
+ const g = E.map($).filter((p) => !l.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: [...c, ...g]
201
+ messages: [...l, ...g]
202
202
  }));
203
203
  }
204
204
  }
205
- function d(i, c) {
205
+ function f(i, l) {
206
206
  return {
207
- id: _(),
207
+ id: O(),
208
208
  type: "FROM_USER",
209
209
  content: i,
210
210
  deliveredAt: (/* @__PURE__ */ new Date()).toISOString(),
211
- attachments: c,
211
+ attachments: l,
212
212
  timestamp: (/* @__PURE__ */ new Date()).toISOString()
213
213
  };
214
214
  }
@@ -216,7 +216,7 @@ function J(t, e, s) {
216
216
  if (i.success && i.autopilotResponse)
217
217
  return {
218
218
  type: "FROM_BOT",
219
- id: i.autopilotResponse.id || _(),
219
+ id: i.autopilotResponse.id || O(),
220
220
  timestamp: (/* @__PURE__ */ new Date()).toISOString(),
221
221
  component: "TEXT",
222
222
  data: {
@@ -224,13 +224,13 @@ function J(t, e, s) {
224
224
  }
225
225
  };
226
226
  if (i.success && i.uiResponse) {
227
- const c = i.uiResponse.value;
227
+ const l = i.uiResponse.value;
228
228
  return {
229
229
  type: "FROM_BOT",
230
- id: _(),
230
+ id: O(),
231
231
  timestamp: (/* @__PURE__ */ new Date()).toISOString(),
232
- component: c.name,
233
- data: c.request_response
232
+ component: l.name,
233
+ data: l.request_response
234
234
  };
235
235
  }
236
236
  return null;
@@ -238,7 +238,7 @@ function J(t, e, s) {
238
238
  function a(i) {
239
239
  return {
240
240
  type: "FROM_BOT",
241
- id: _(),
241
+ id: O(),
242
242
  timestamp: (/* @__PURE__ */ new Date()).toISOString(),
243
243
  component: "TEXT",
244
244
  data: {
@@ -249,33 +249,33 @@ function J(t, e, s) {
249
249
  }
250
250
  return {
251
251
  fetchHistoryMessages: o,
252
- addUserMessage: d,
252
+ addUserMessage: f,
253
253
  addBotMessage: n,
254
254
  addErrorMessage: a
255
255
  };
256
256
  }
257
- function G(t, e, s, o, d, n) {
258
- var F, U;
259
- const a = (F = n.platform) == null ? void 0 : F.logger;
257
+ function z(t, e, s, o, f, n) {
258
+ var U, N;
259
+ const a = (U = n.platform) == null ? void 0 : U.logger;
260
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;
261
+ const l = (N = n.platform) == null ? void 0 : N.storage, c = `${f.getConfig().user.external_id}:${f.getConfig().token}:session`, r = n.config.getSettings().persistSession;
262
262
  async function u() {
263
- if (c)
263
+ if (l)
264
264
  try {
265
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);
266
+ const d = await l.getItem(c);
267
+ if (d) {
268
+ const m = JSON.parse(d);
269
269
  a == null || a.info("Session restored from storage", { sessionId: m.id }), e.setState(m), await o.fetchHistoryMessages(m), g();
270
270
  }
271
- } catch (f) {
272
- a == null || a.error("Error restoring session from storage:", f);
271
+ } catch (d) {
272
+ a == null || a.error("Error restoring session from storage:", d);
273
273
  }
274
274
  }
275
275
  function E() {
276
- c && (a == null || a.debug("Setting up session persistence"), e.subscribe(async (f) => {
276
+ l && (a == null || a.debug("Setting up session persistence"), e.subscribe(async (d) => {
277
277
  try {
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"));
278
+ d ? (await l.setItem(c, JSON.stringify(d)), a == null || a.debug("Session persisted to storage", { sessionId: d.id })) : (await l.removeItem(c), a == null || a.debug("Session removed from storage"));
279
279
  } catch (m) {
280
280
  a == null || a.error("Error persisting session:", m), s.setStatePartial({
281
281
  error: {
@@ -290,8 +290,8 @@ function G(t, e, s, o, d, n) {
290
290
  function g() {
291
291
  if (i) return;
292
292
  a == null || a.debug("Starting polling");
293
- const f = [];
294
- f.push(
293
+ const d = [];
294
+ d.push(
295
295
  setInterval(async () => {
296
296
  const m = e.getState();
297
297
  if (m != null && m.id)
@@ -301,8 +301,8 @@ function G(t, e, s, o, d, n) {
301
301
  } catch (w) {
302
302
  a == null || a.error("Error polling session:", w);
303
303
  }
304
- }, R.SESSION)
305
- ), f.push(
304
+ }, k.SESSION)
305
+ ), d.push(
306
306
  setInterval(async () => {
307
307
  const m = e.getState();
308
308
  if (m != null && m.id)
@@ -311,9 +311,9 @@ function G(t, e, s, o, d, n) {
311
311
  } catch (w) {
312
312
  a == null || a.error("Error polling messages:", w);
313
313
  }
314
- }, R.MESSAGES)
314
+ }, k.MESSAGES)
315
315
  ), i = () => {
316
- a == null || a.debug("Stopping polling"), f.forEach(clearInterval);
316
+ a == null || a.debug("Stopping polling"), d.forEach(clearInterval);
317
317
  };
318
318
  }
319
319
  async function p() {
@@ -322,13 +322,13 @@ function G(t, e, s, o, d, n) {
322
322
  loading: { isLoading: !0, reason: "creating_session" },
323
323
  error: { hasError: !1 }
324
324
  });
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);
325
+ const d = await t.createSession();
326
+ return a == null || a.info("Session created successfully", { sessionId: d.id }), e.setState(d), g(), d;
327
+ } catch (d) {
328
+ a == null || a.error("Failed to create session:", d);
329
329
  const m = {
330
330
  hasError: !0,
331
- message: f instanceof Error ? f.message : "Failed to create session",
331
+ message: d instanceof Error ? d.message : "Failed to create session",
332
332
  code: "SESSION_CREATION_FAILED"
333
333
  };
334
334
  return s.setStatePartial({ error: m }), null;
@@ -340,10 +340,10 @@ function G(t, e, s, o, d, n) {
340
340
  }
341
341
  async function I() {
342
342
  var m;
343
- const f = e.getState();
344
- if (f != null && f.id)
343
+ const d = e.getState();
344
+ if (d != null && d.id)
345
345
  try {
346
- i && (i(), i = null), e.setState(null), r && c && T(c) && c.removeItem(l), s.setState({
346
+ i && (i(), i = null), e.setState(null), r && l && L(l) && l.removeItem(c), s.setState({
347
347
  messages: [],
348
348
  keyboard: null,
349
349
  loading: { isLoading: !1 },
@@ -359,32 +359,43 @@ function G(t, e, s, o, d, n) {
359
359
  });
360
360
  }
361
361
  }
362
- function A() {
362
+ function T() {
363
363
  try {
364
- i && (i(), i = null), r && c && T(c) && c.removeItem(l), s.setState({
364
+ i && (i(), i = null), r && l && L(l) && l.removeItem(c), 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 (f) {
371
- console.error("Error in cleanup:", f), s.setStatePartial({
370
+ } catch (d) {
371
+ console.error("Error in cleanup:", d), s.setStatePartial({
372
372
  error: {
373
373
  hasError: !0,
374
- message: f instanceof Error ? f.message : "Failed to cleanup",
374
+ message: d instanceof Error ? d.message : "Failed to cleanup",
375
375
  code: "SESSION_CLEAR_FAILED"
376
376
  }
377
377
  });
378
378
  }
379
379
  }
380
- return r && T(c) && (u(), E()), {
380
+ async function F(d) {
381
+ return t.getSession(d);
382
+ }
383
+ async function q() {
384
+ const d = e.getState();
385
+ if (!(d != null && d.id)) return;
386
+ const m = await F(d.id);
387
+ return m && e.setState(m), m;
388
+ }
389
+ return r && L(l) && (u(), E()), {
381
390
  createSession: p,
382
391
  clearSession: I,
383
- cleanup: A,
384
- startPolling: g
392
+ cleanup: T,
393
+ startPolling: g,
394
+ fetchSession: F,
395
+ refetchSession: q
385
396
  };
386
397
  }
387
- function ae(t) {
398
+ function oe(t) {
388
399
  var i;
389
400
  const e = (i = t.platform) == null ? void 0 : i.logger;
390
401
  e == null || e.info("Initializing chat");
@@ -393,25 +404,29 @@ function ae(t) {
393
404
  keyboard: null,
394
405
  loading: { isLoading: !1 },
395
406
  error: { hasError: !1 }
396
- }), o = new C(null), d = J(t.api, s, e), n = G(
407
+ }), o = new C(null), f = X(t.api, s, e), n = z(
397
408
  t.api,
398
409
  o,
399
410
  s,
400
- d,
411
+ f,
401
412
  t.config,
402
413
  t
403
414
  );
404
- async function a(c) {
415
+ async function a(l) {
405
416
  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))
417
+ let c = o.getState();
418
+ if (!(c != null && c.id) && (e == null || e.debug("No active session, creating new session"), c = await n.createSession(), !c))
408
419
  return !1;
420
+ c.assignee.kind === "ai" && (c = await n.refetchSession() ?? c);
409
421
  try {
410
- e == null || e.debug("Sending message", { sessionId: l.id }), s.setStatePartial({
411
- loading: { isLoading: !0, reason: "sending_message" },
422
+ e == null || e.debug("Sending message", { sessionId: c.id }), c.assignee.kind === "ai" ? s.setStatePartial({
423
+ loading: { isLoading: !0, reason: "sending_message_to_bot" },
424
+ error: { hasError: !1 }
425
+ }) : s.setStatePartial({
426
+ loading: { isLoading: !0, reason: "sending_message_to_agent" },
412
427
  error: { hasError: !1 }
413
428
  });
414
- const u = d.addUserMessage(c.content, c.attachments || void 0), E = s.getState().messages;
429
+ const u = f.addUserMessage(l.content, l.attachments || void 0), E = s.getState().messages;
415
430
  s.setStatePartial({
416
431
  messages: [...E, u]
417
432
  });
@@ -419,23 +434,24 @@ function ae(t) {
419
434
  bot_token: g.token,
420
435
  headers: g.headers,
421
436
  query_params: g.queryParams,
422
- session_id: l.id,
423
- ...c
437
+ session_id: c.id,
438
+ user: g.user,
439
+ ...l
424
440
  });
425
441
  if (p.success) {
426
442
  e == null || e.debug("Message sent successfully");
427
- const I = d.addBotMessage(p);
443
+ const I = f.addBotMessage(p);
428
444
  if (I) {
429
- const A = s.getState().messages;
445
+ const T = s.getState().messages;
430
446
  s.setStatePartial({
431
- messages: [...A, I]
447
+ messages: [...T, I]
432
448
  });
433
449
  }
434
450
  } else {
435
451
  e == null || e.warn("Message send failed", p.error);
436
- const I = d.addErrorMessage(((r = p.error) == null ? void 0 : r.message) || "Unknown error occurred"), A = s.getState().messages;
452
+ const I = f.addErrorMessage(((r = p.error) == null ? void 0 : r.message) || "Unknown error occurred"), T = s.getState().messages;
437
453
  s.setStatePartial({
438
- messages: [...A, I]
454
+ messages: [...T, I]
439
455
  });
440
456
  }
441
457
  return !0;
@@ -462,27 +478,27 @@ function ae(t) {
462
478
  cleanup: n.cleanup
463
479
  };
464
480
  }
465
- function ne(t) {
481
+ function ie(t) {
466
482
  const e = t.config.getConfig();
467
483
  t.config.getConfig;
468
- const s = `${e.token}:contact:${e.user.external_id}`, o = T(t.platform.storage) ? t.platform.storage : void 0;
469
- let d = null;
470
- o && O(
484
+ const s = `${e.token}:contact:${e.user.external_id}`, o = L(t.platform.storage) ? t.platform.storage : void 0;
485
+ let f = null;
486
+ o && A(
471
487
  async () => {
472
488
  const r = await o.getItem(s);
473
489
  return r ? JSON.parse(r) : null;
474
490
  },
475
491
  "Error loading contact from storage"
476
492
  ).then((r) => {
477
- r.success && (d = r.result);
493
+ r.success && (f = r.result);
478
494
  });
479
495
  const n = new C({
480
- contact: d,
496
+ contact: f,
481
497
  loading: { isLoading: !1 },
482
498
  error: { hasError: !1 }
483
499
  });
484
- T(o) && n.subscribe((r) => {
485
- O(
500
+ L(o) && n.subscribe((r) => {
501
+ A(
486
502
  async () => {
487
503
  r.contact ? await o.setItem(s, JSON.stringify(r.contact)) : o.removeItem(s);
488
504
  },
@@ -503,7 +519,7 @@ function ne(t) {
503
519
  loading: { isLoading: !0, reason: "loading_contact" },
504
520
  error: { hasError: !1 }
505
521
  }), o) {
506
- const r = await O(
522
+ const r = await A(
507
523
  async () => {
508
524
  const u = await o.getItem(s);
509
525
  return u ? JSON.parse(u) : null;
@@ -545,8 +561,8 @@ function ne(t) {
545
561
  avatar_url: r.avatar_url ?? (u == null ? void 0 : u.avatar_url) ?? null,
546
562
  email: r.email ?? (u == null ? void 0 : u.email) ?? null
547
563
  };
548
- if (T(o)) {
549
- const g = await O(
564
+ if (L(o)) {
565
+ const g = await A(
550
566
  () => o.setItem(s, JSON.stringify(E)),
551
567
  "Error saving contact"
552
568
  );
@@ -571,7 +587,7 @@ function ne(t) {
571
587
  });
572
588
  }
573
589
  }
574
- function c() {
590
+ function l() {
575
591
  var u;
576
592
  return !((u = n.getState().contact) != null && u.id) && e.collectUserData ? {
577
593
  should: !0,
@@ -580,13 +596,13 @@ function ne(t) {
580
596
  should: !1
581
597
  };
582
598
  }
583
- async function l() {
599
+ async function c() {
584
600
  try {
585
601
  if (n.setStatePartial({
586
602
  loading: { isLoading: !0, reason: "cleaning_up" },
587
603
  error: { hasError: !1 }
588
604
  }), o) {
589
- const r = await O(
605
+ const r = await A(
590
606
  () => o.removeItem(s),
591
607
  "Error removing contact data"
592
608
  );
@@ -614,29 +630,28 @@ function ne(t) {
614
630
  }
615
631
  return {
616
632
  contactState: n,
617
- shouldCollectData: c,
633
+ shouldCollectData: l,
618
634
  loadContact: a,
619
635
  saveContact: i,
620
- cleanup: l
636
+ cleanup: c
621
637
  };
622
638
  }
623
- const X = {
639
+ const V = {
624
640
  messageArrived: "https://cloud.opencopilot.so/sfx/notification3.mp3"
625
- }, $ = {
641
+ }, x = {
626
642
  primaryColor: "hsl(211,65%,59%)",
627
643
  triggerOffset: "20px"
628
- }, z = 1e3 * 3;
629
- function oe(t) {
630
- var s, o, d, n, a, i, c, l, r;
644
+ }, K = 1e3 * 3;
645
+ function ce(t) {
646
+ var s, o, f, n, a, i, l, c, r;
631
647
  if (!t.token)
632
648
  throw new Error("Token is required");
633
- if (t.pollingInterval && t.pollingInterval < z)
649
+ if (t.pollingInterval && t.pollingInterval < K)
634
650
  throw new Error("Polling interval must be at least 3 seconds");
635
651
  const e = {
636
652
  ...t,
637
653
  collectUserData: t.collectUserData ?? !1,
638
654
  apiUrl: t.apiUrl ?? "https://api-v2.opencopilot.so/backend",
639
- socketUrl: t.socketUrl ?? "https://api-v2.opencopilot.so",
640
655
  pollingInterval: t.pollingInterval ?? 3e3,
641
656
  headers: t.headers ?? {},
642
657
  queryParams: t.queryParams ?? {},
@@ -644,7 +659,7 @@ function oe(t) {
644
659
  bot: {
645
660
  name: ((s = t.bot) == null ? void 0 : s.name) ?? "Bot",
646
661
  avatarUrl: (o = t.bot) == null ? void 0 : o.avatarUrl,
647
- id: ((d = t.bot) == null ? void 0 : d.id) ?? null,
662
+ id: ((f = t.bot) == null ? void 0 : f.id) ?? null,
648
663
  is_ai: ((n = t.bot) == null ? void 0 : n.is_ai) ?? !0
649
664
  },
650
665
  contactToken: t.contactToken,
@@ -652,14 +667,14 @@ function oe(t) {
652
667
  language: t.language ?? "en",
653
668
  user: t.user ?? {},
654
669
  soundEffectFiles: {
655
- messageArrived: ((a = t.soundEffectFiles) == null ? void 0 : a.messageArrived) ?? X.messageArrived
670
+ messageArrived: ((a = t.soundEffectFiles) == null ? void 0 : a.messageArrived) ?? V.messageArrived
656
671
  },
657
672
  theme: {
658
- primaryColor: ((i = t.theme) == null ? void 0 : i.primaryColor) ?? $.primaryColor,
659
- triggerOffset: ((c = t.theme) == null ? void 0 : c.triggerOffset) ?? $.triggerOffset
673
+ primaryColor: ((i = t.theme) == null ? void 0 : i.primaryColor) ?? x.primaryColor,
674
+ triggerOffset: ((l = t.theme) == null ? void 0 : l.triggerOffset) ?? x.triggerOffset
660
675
  },
661
676
  settings: {
662
- persistSession: ((l = t.settings) == null ? void 0 : l.persistSession) ?? !1,
677
+ persistSession: ((c = t.settings) == null ? void 0 : c.persistSession) ?? !1,
663
678
  useSoundEffects: ((r = t.settings) == null ? void 0 : r.useSoundEffects) ?? !1
664
679
  }
665
680
  };
@@ -681,8 +696,8 @@ function oe(t) {
681
696
  getDebugMode: () => e.debug
682
697
  };
683
698
  }
684
- function V(t = {}) {
685
- const e = [], s = [], o = [], d = async (n, a = {}) => {
699
+ function Y(t = {}) {
700
+ const e = [], s = [], o = [], f = async (n, a = {}) => {
686
701
  let i = {
687
702
  ...t,
688
703
  ...a,
@@ -694,19 +709,19 @@ function V(t = {}) {
694
709
  try {
695
710
  for (const u of e)
696
711
  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);
712
+ const l = i.params ? "?" + new URLSearchParams(i.params).toString() : "", c = i.baseURL ? `${i.baseURL}${n}${l}`.replace(/([^:]\/)\/+/g, "$1") : `${n}${l}`;
713
+ let r = await fetch(c, i);
699
714
  for (const u of s)
700
715
  r = await u(r);
701
716
  return r;
702
- } catch (c) {
703
- let l = c;
717
+ } catch (l) {
718
+ let c = l;
704
719
  for (const r of o)
705
- l = await r(l);
706
- throw l;
720
+ c = await r(c);
721
+ throw c;
707
722
  }
708
723
  };
709
- return d.interceptors = {
724
+ return f.interceptors = {
710
725
  request: {
711
726
  use: (n) => (e.push(n), e.length - 1),
712
727
  eject: (n) => {
@@ -719,10 +734,10 @@ function V(t = {}) {
719
734
  s.splice(n, 1), o.splice(n, 1);
720
735
  }
721
736
  }
722
- }, d;
737
+ }, f;
723
738
  }
724
739
  var b;
725
- class ie {
740
+ class le {
726
741
  constructor(e) {
727
742
  M(this, b);
728
743
  this.options = e;
@@ -731,15 +746,15 @@ class ie {
731
746
  value: ""
732
747
  };
733
748
  s != null && s.email ? (o.claim = "email", o.value = s.email) : s != null && s.phone && (o.claim = "phone", o.value = s.phone);
734
- const d = {
749
+ const f = {
735
750
  "X-Bot-Token": this.options.config.token,
736
751
  "X-Consumer-Id": `${o.claim}:${o.value}`,
737
752
  "Content-Type": "application/json",
738
753
  Accept: "application/json"
739
754
  };
740
- this.options.config.contactToken && (d.Authorization = `Bearer ${this.options.config.contactToken}`), P(this, b, V({
755
+ this.options.config.contactToken && (f.Authorization = `Bearer ${this.options.config.contactToken}`), _(this, b, Y({
741
756
  baseURL: `${this.options.config.apiUrl}/widget/v2`,
742
- headers: d
757
+ headers: f
743
758
  }));
744
759
  }
745
760
  async me() {
@@ -757,8 +772,8 @@ class ie {
757
772
  async getSessionHistory(e, s) {
758
773
  const o = new URLSearchParams({
759
774
  lastMessageTimestamp: s || ""
760
- }), d = `/session/history/${e}?${o.toString()}`;
761
- return (await h(this, b).call(this, d, {
775
+ }), f = `/session/history/${e}?${o.toString()}`;
776
+ return (await h(this, b).call(this, f, {
762
777
  method: "GET"
763
778
  })).json();
764
779
  }
@@ -772,35 +787,29 @@ class ie {
772
787
  method: "GET"
773
788
  })).json();
774
789
  }
775
- async createContact(e) {
776
- return (await h(this, b).call(this, "/contact/upsert", {
777
- method: "POST",
778
- body: JSON.stringify(e)
779
- })).json();
780
- }
781
790
  }
782
791
  b = new WeakMap();
783
- function ce(t = {}) {
792
+ function ue(t = {}) {
784
793
  const {
785
794
  level: e = "info",
786
795
  prefix: s = "[openCx]",
787
796
  enabled: o = !0
788
797
  } = t;
789
- let d = e;
798
+ let f = e;
790
799
  const n = {
791
800
  debug: 0,
792
801
  info: 1,
793
802
  warn: 2,
794
803
  error: 3
795
804
  };
796
- function a(l) {
797
- return o && n[l] >= n[d];
805
+ function a(c) {
806
+ return o && n[c] >= n[f];
798
807
  }
799
- function i(l) {
800
- return `${s} ${l}`;
808
+ function i(c) {
809
+ return `${s} ${c}`;
801
810
  }
802
- function c(l) {
803
- return l.map((r) => {
811
+ function l(c) {
812
+ return c.map((r) => {
804
813
  if (r instanceof Error)
805
814
  return {
806
815
  name: r.name,
@@ -817,50 +826,42 @@ function ce(t = {}) {
817
826
  });
818
827
  }
819
828
  return {
820
- debug(l, ...r) {
821
- a("debug") && console.debug(i(l), ...c(r));
829
+ debug(c, ...r) {
830
+ a("debug") && console.debug(i(c), ...l(r));
822
831
  },
823
- info(l, ...r) {
824
- a("info") && console.info(i(l), ...c(r));
832
+ info(c, ...r) {
833
+ a("info") && console.info(i(c), ...l(r));
825
834
  },
826
- warn(l, ...r) {
827
- a("warn") && console.warn(i(l), ...c(r));
835
+ warn(c, ...r) {
836
+ a("warn") && console.warn(i(c), ...l(r));
828
837
  },
829
- error(l, ...r) {
830
- a("error") && console.error(i(l), ...c(r));
838
+ error(c, ...r) {
839
+ a("error") && console.error(i(c), ...l(r));
831
840
  },
832
- setLevel(l) {
833
- d = l;
841
+ setLevel(c) {
842
+ f = c;
834
843
  }
835
844
  };
836
845
  }
837
846
  export {
838
- de as AIClosureType,
839
- ie as ApiCaller,
840
- ee as AuthenticationError,
841
- Z as ConnectionError,
842
- re as FileUploadError,
843
- H as LifecycleEvent,
844
- fe as MessageTypeEnum,
847
+ le as ApiCaller,
848
+ se as AuthenticationError,
849
+ te as ConnectionError,
850
+ ne as FileUploadError,
851
+ G as LifecycleEvent,
852
+ me as MessageTypeEnum,
845
853
  v as OpenCXError,
846
854
  C as PubSub,
847
- me as SentimentEnum,
848
- ge as SessionChannel,
849
- B as SessionError,
850
- te as SessionNotDefinedError,
851
- he as SessionStatus,
852
- se as TransportError,
853
- Se as chatAttachmentSchema,
854
- pe as chatHistoryMessageSchema,
855
- Ee as chatSessionSchema,
856
- ye as consumerSchema,
857
- ae as createChat,
858
- oe as createConfig,
859
- ne as createContact,
860
- ce as createLogger,
861
- W as createPubSub,
862
- T as isStorageAvailable,
863
- O as safeStorageOperation,
864
- be as structuredSocketMessageSchema
855
+ J as SessionError,
856
+ re as SessionNotDefinedError,
857
+ ae as TransportError,
858
+ ge as chatAttachmentSchema,
859
+ oe as createChat,
860
+ ce as createConfig,
861
+ ie as createContact,
862
+ ue as createLogger,
863
+ ee as createPubSub,
864
+ L as isStorageAvailable,
865
+ A as safeStorageOperation
865
866
  };
866
867
  //# sourceMappingURL=index.js.map