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

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 (44) 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/index.d.ts +2 -0
  9. package/dist/core/types/helpers.d.ts +1 -1
  10. package/dist/core/types/index.d.ts +2 -3
  11. package/dist/core/types/schemas-v2.d.ts +92 -91
  12. package/dist/core/types/schemas.d.ts +123 -69
  13. package/dist/{index-Cjs7ckCU.js → index-B-VyJuQ9.js} +964 -864
  14. package/dist/index-B-VyJuQ9.js.map +1 -0
  15. package/dist/index-D2PGs8oF.cjs +18 -0
  16. package/dist/index-D2PGs8oF.cjs.map +1 -0
  17. package/dist/index.cjs +1 -1
  18. package/dist/index.cjs.map +1 -1
  19. package/dist/index.js +337 -282
  20. package/dist/index.js.map +1 -1
  21. package/dist/package.json.d.ts +1 -1
  22. package/dist/react-web/hooks/useAbstractChat.d.ts +22 -21
  23. package/dist/react-web/hooks/useAxiosInstance.d.ts +35 -23
  24. package/dist/react-web/hooks/useUploadFiles.d.ts +1 -1
  25. package/dist/react-web/providers/ChatProvider.d.ts +24 -24
  26. package/dist/react-web/providers/ContactProvider.d.ts +4 -4
  27. package/dist/react.cjs +1 -1
  28. package/dist/react.js +1 -1
  29. package/dist/schemas-v2-BiJWHypc.cjs +2 -0
  30. package/dist/schemas-v2-BiJWHypc.cjs.map +1 -0
  31. package/dist/schemas-v2-DZ3nwfID.js +138 -0
  32. package/dist/schemas-v2-DZ3nwfID.js.map +1 -0
  33. package/dist/src/components/RenderFile.d.ts +1 -1
  34. package/dist/src/components/dialog.d.ts +2 -2
  35. package/dist-embed/script.js +107 -107
  36. package/dist-embed/script.js.map +1 -1
  37. package/package.json +1 -1
  38. package/dist/index-BVoLVQCZ.cjs +0 -18
  39. package/dist/index-BVoLVQCZ.cjs.map +0 -1
  40. package/dist/index-Cjs7ckCU.js.map +0 -1
  41. package/dist/schemas-BBAV6Sd_.js +0 -122
  42. package/dist/schemas-BBAV6Sd_.js.map +0 -1
  43. package/dist/schemas-wky4fpbc.cjs +0 -2
  44. package/dist/schemas-wky4fpbc.cjs.map +0 -1
package/dist/index.js CHANGED
@@ -1,22 +1,22 @@
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 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);
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
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 {
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
+ 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, P);
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
22
  const f = {
@@ -24,7 +24,7 @@ class v {
24
24
  timestamp: Date.now(),
25
25
  data: s
26
26
  };
27
- o.forEach((a) => a(f));
27
+ o.forEach((n) => n(f));
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
+ }), 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) {
@@ -62,66 +62,66 @@ 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, 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(), A = new WeakMap();
86
- function W(t) {
87
- return new v(t);
85
+ y = new WeakMap(), P = new WeakMap();
86
+ function ee(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 te 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 se 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 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 M {
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 M {
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 || L(),
150
+ id: t.publicId || O(),
151
151
  timestamp: t.sentAt || "",
152
152
  attachments: t.attachments || void 0
153
153
  };
@@ -171,28 +171,28 @@ function k(t) {
171
171
  }
172
172
  };
173
173
  }
174
- function G(t, e, s) {
175
- async function o(c) {
174
+ function X(t, e, s) {
175
+ async function o(i) {
176
176
  const l = e.getState().messages;
177
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, "");
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
- messages: g.map(k)
181
+ messages: g.map($)
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 c = l[l.length - 1], r = new Date(c.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,
189
+ sessionId: i.id,
190
+ lastMessageTimestamp: u,
191
191
  currentMessageCount: l.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($).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),
@@ -202,32 +202,32 @@ function G(t, e, s) {
202
202
  }));
203
203
  }
204
204
  }
205
- function f(c, l) {
205
+ function f(i, l) {
206
206
  return {
207
- id: L(),
207
+ id: O(),
208
208
  type: "FROM_USER",
209
- content: c,
209
+ content: i,
210
210
  deliveredAt: (/* @__PURE__ */ new Date()).toISOString(),
211
211
  attachments: l,
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 || O(),
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 l = i.uiResponse.value;
228
228
  return {
229
229
  type: "FROM_BOT",
230
- id: L(),
230
+ id: O(),
231
231
  timestamp: (/* @__PURE__ */ new Date()).toISOString(),
232
232
  component: l.name,
233
233
  data: l.request_response
@@ -235,14 +235,14 @@ function G(t, e, s) {
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: O(),
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
  };
@@ -250,34 +250,34 @@ function G(t, e, s) {
250
250
  return {
251
251
  fetchHistoryMessages: o,
252
252
  addUserMessage: f,
253
- addBotMessage: a,
254
- addErrorMessage: r
253
+ addBotMessage: n,
254
+ addErrorMessage: a
255
255
  };
256
256
  }
257
- function J(t, e, s, o, f, a) {
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() {
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
+ let i = null;
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
+ async function u() {
263
263
  if (l)
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 d = await l.getItem(c);
267
+ if (d) {
268
+ const m = JSON.parse(d);
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 (d) {
272
+ a == null || a.error("Error restoring session from storage:", d);
273
273
  }
274
274
  }
275
275
  function E() {
276
- l && (r == null || r.debug("Setting up session persistence"), e.subscribe(async (u) => {
276
+ l && (a == null || a.debug("Setting up session persistence"), e.subscribe(async (d) => {
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
+ 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
- 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 d = [];
294
+ d.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
- }, R.SESSION)
305
- ), u.push(
304
+ }, k.SESSION)
305
+ ), d.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
- }, R.MESSAGES)
315
- ), c = () => {
316
- r == null || r.debug("Stopping polling"), u.forEach(clearInterval);
314
+ }, k.MESSAGES)
315
+ ), i = () => {
316
+ a == null || a.debug("Stopping polling"), d.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 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: u instanceof Error ? u.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;
@@ -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 d = e.getState();
344
+ if (d != null && d.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 && l && L(l) && l.removeItem(c), 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,41 +359,52 @@ function J(t, e, s, o, f, a) {
359
359
  });
360
360
  }
361
361
  }
362
- function _() {
362
+ function T() {
363
363
  try {
364
- c && (c(), c = null), n && l && T(l) && l.removeItem(d), 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 (u) {
371
- console.error("Error in cleanup:", u), s.setStatePartial({
370
+ } catch (d) {
371
+ console.error("Error in cleanup:", d), s.setStatePartial({
372
372
  error: {
373
373
  hasError: !0,
374
- message: u instanceof Error ? u.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 n && T(l) && (i(), 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
- clearSession: b,
383
- cleanup: _,
384
- startPolling: g
391
+ clearSession: I,
392
+ cleanup: T,
393
+ startPolling: g,
394
+ fetchSession: F,
395
+ refetchSession: q
385
396
  };
386
397
  }
387
- function ae(t) {
388
- var c;
389
- const e = (c = t.platform) == null ? void 0 : c.logger;
398
+ function oe(t) {
399
+ var i;
400
+ const e = (i = t.platform) == null ? void 0 : i.logger;
390
401
  e == null || e.info("Initializing chat");
391
- const s = new v({
402
+ const s = new C({
392
403
  messages: [],
393
404
  keyboard: null,
394
405
  loading: { isLoading: !1 },
395
406
  error: { hasError: !1 }
396
- }), o = new v(null), f = G(t.api, s, e), a = J(
407
+ }), o = new C(null), f = X(t.api, s, e), n = z(
397
408
  t.api,
398
409
  o,
399
410
  s,
@@ -401,49 +412,53 @@ function ae(t) {
401
412
  t.config,
402
413
  t
403
414
  );
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))
415
+ async function a(l) {
416
+ var r;
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: d.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_agent" },
424
+ error: { hasError: !1 }
425
+ }) : s.setStatePartial({
426
+ loading: { isLoading: !0, reason: "sending_message_to_bot" },
412
427
  error: { hasError: !1 }
413
428
  });
414
- const i = f.addUserMessage(l.content, l.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
- messages: [...E, i]
431
+ messages: [...E, u]
417
432
  });
418
433
  const g = t.config.getConfig(), p = await t.api.handleMessage({
419
434
  bot_token: g.token,
420
435
  headers: g.headers,
421
436
  query_params: g.queryParams,
422
- session_id: d.id,
437
+ session_id: c.id,
423
438
  ...l
424
439
  });
425
440
  if (p.success) {
426
441
  e == null || e.debug("Message sent successfully");
427
- const b = f.addBotMessage(p);
428
- if (b) {
429
- const _ = s.getState().messages;
442
+ const I = f.addBotMessage(p);
443
+ if (I) {
444
+ const T = s.getState().messages;
430
445
  s.setStatePartial({
431
- messages: [..._, b]
446
+ messages: [...T, I]
432
447
  });
433
448
  }
434
449
  } else {
435
450
  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;
451
+ const I = f.addErrorMessage(((r = p.error) == null ? void 0 : r.message) || "Unknown error occurred"), T = s.getState().messages;
437
452
  s.setStatePartial({
438
- messages: [..._, b]
453
+ messages: [...T, I]
439
454
  });
440
455
  }
441
456
  return !0;
442
- } catch (i) {
443
- return e == null || e.error("Error sending message:", i), s.setStatePartial({
457
+ } catch (u) {
458
+ return e == null || e.error("Error sending message:", u), s.setStatePartial({
444
459
  error: {
445
460
  hasError: !0,
446
- message: i instanceof Error ? i.message : "Failed to send message",
461
+ message: u instanceof Error ? u.message : "Failed to send message",
447
462
  code: "MESSAGE_SEND_FAILED"
448
463
  }
449
464
  }), !1;
@@ -456,187 +471,186 @@ function ae(t) {
456
471
  return {
457
472
  chatState: s,
458
473
  sessionState: o,
459
- sendMessage: r,
460
- createSession: a.createSession,
461
- clearSession: a.clearSession,
462
- cleanup: a.cleanup
474
+ sendMessage: a,
475
+ createSession: n.createSession,
476
+ clearSession: n.clearSession,
477
+ cleanup: n.cleanup
463
478
  };
464
479
  }
465
- function ne(t) {
480
+ function ie(t) {
466
481
  const e = t.config.getConfig();
467
482
  t.config.getConfig;
468
- const s = `${e.token}:contact:${e.user.external_id}`, o = T(t.platform.storage) ? t.platform.storage : void 0;
483
+ const s = `${e.token}:contact:${e.user.external_id}`, o = L(t.platform.storage) ? t.platform.storage : void 0;
469
484
  let f = null;
470
- o && O(
485
+ o && A(
471
486
  async () => {
472
- const n = await o.getItem(s);
473
- return n ? JSON.parse(n) : null;
487
+ const r = await o.getItem(s);
488
+ return r ? JSON.parse(r) : null;
474
489
  },
475
490
  "Error loading contact from storage"
476
- ).then((n) => {
477
- n.success && (f = n.result);
491
+ ).then((r) => {
492
+ r.success && (f = r.result);
478
493
  });
479
- const a = new v({
494
+ const n = new C({
480
495
  contact: f,
481
496
  loading: { isLoading: !1 },
482
497
  error: { hasError: !1 }
483
498
  });
484
- T(o) && a.subscribe((n) => {
485
- O(
499
+ L(o) && n.subscribe((r) => {
500
+ A(
486
501
  async () => {
487
- n.contact ? await o.setItem(s, JSON.stringify(n.contact)) : o.removeItem(s);
502
+ r.contact ? await o.setItem(s, JSON.stringify(r.contact)) : o.removeItem(s);
488
503
  },
489
504
  "Error persisting contact state"
490
- ).then((i) => {
491
- i.success || a.setStatePartial({
505
+ ).then((u) => {
506
+ u.success || n.setStatePartial({
492
507
  error: {
493
508
  hasError: !0,
494
- message: i.error.message,
509
+ message: u.error.message,
495
510
  code: "CONTACT_PERSISTENCE_FAILED"
496
511
  }
497
512
  });
498
513
  });
499
514
  });
500
- async function r() {
515
+ async function a() {
501
516
  try {
502
- if (a.setStatePartial({
517
+ if (n.setStatePartial({
503
518
  loading: { isLoading: !0, reason: "loading_contact" },
504
519
  error: { hasError: !1 }
505
520
  }), o) {
506
- const n = await O(
521
+ const r = await A(
507
522
  async () => {
508
- const i = await o.getItem(s);
509
- return i ? JSON.parse(i) : null;
523
+ const u = await o.getItem(s);
524
+ return u ? JSON.parse(u) : null;
510
525
  },
511
526
  "Error loading contact"
512
527
  );
513
- if (!n.success)
514
- throw n.error;
515
- return a.setStatePartial({
516
- contact: n.result,
528
+ if (!r.success)
529
+ throw r.error;
530
+ return n.setStatePartial({
531
+ contact: r.result,
517
532
  error: { hasError: !1 }
518
- }), n.result;
533
+ }), r.result;
519
534
  }
520
- return a.getState().contact;
521
- } catch (n) {
522
- return a.setStatePartial({
535
+ return n.getState().contact;
536
+ } catch (r) {
537
+ return n.setStatePartial({
523
538
  error: {
524
539
  hasError: !0,
525
- message: n instanceof Error ? n.message : "Failed to load contact",
540
+ message: r instanceof Error ? r.message : "Failed to load contact",
526
541
  code: "CONTACT_LOAD_FAILED"
527
542
  }
528
543
  }), null;
529
544
  } finally {
530
- a.setStatePartial({
545
+ n.setStatePartial({
531
546
  loading: { isLoading: !1, reason: null }
532
547
  });
533
548
  }
534
549
  }
535
- async function c(n) {
550
+ async function i(r) {
536
551
  try {
537
- a.setStatePartial({
552
+ n.setStatePartial({
538
553
  loading: { isLoading: !0, reason: "saving_contact" },
539
554
  error: { hasError: !1 }
540
555
  });
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
556
+ const u = n.getState().contact, E = {
557
+ id: r.id || (u == null ? void 0 : u.id) || "",
558
+ name: r.name ?? (u == null ? void 0 : u.name) ?? null,
559
+ created_at: r.created_at || (u == null ? void 0 : u.created_at) || (/* @__PURE__ */ new Date()).toISOString(),
560
+ avatar_url: r.avatar_url ?? (u == null ? void 0 : u.avatar_url) ?? null,
561
+ email: r.email ?? (u == null ? void 0 : u.email) ?? null
547
562
  };
548
- if (T(o)) {
549
- const g = await O(
563
+ if (L(o)) {
564
+ const g = await A(
550
565
  () => o.setItem(s, JSON.stringify(E)),
551
566
  "Error saving contact"
552
567
  );
553
568
  if (!g.success)
554
569
  throw g.error;
555
570
  }
556
- return a.setStatePartial({
571
+ return n.setStatePartial({
557
572
  contact: E,
558
573
  error: { hasError: !1 }
559
574
  }), E;
560
- } catch (i) {
561
- return a.setStatePartial({
575
+ } catch (u) {
576
+ return n.setStatePartial({
562
577
  error: {
563
578
  hasError: !0,
564
- message: i instanceof Error ? i.message : "Failed to save contact",
579
+ message: u instanceof Error ? u.message : "Failed to save contact",
565
580
  code: "CONTACT_SAVE_FAILED"
566
581
  }
567
582
  }), null;
568
583
  } finally {
569
- a.setStatePartial({
584
+ n.setStatePartial({
570
585
  loading: { isLoading: !1, reason: null }
571
586
  });
572
587
  }
573
588
  }
574
589
  function l() {
575
- var i;
576
- return !((i = a.getState().contact) != null && i.id) && e.collectUserData ? {
590
+ var u;
591
+ return !((u = n.getState().contact) != null && u.id) && e.collectUserData ? {
577
592
  should: !0,
578
593
  reason: "No contact id and collectUserData is true"
579
594
  } : {
580
595
  should: !1
581
596
  };
582
597
  }
583
- async function d() {
598
+ async function c() {
584
599
  try {
585
- if (a.setStatePartial({
600
+ if (n.setStatePartial({
586
601
  loading: { isLoading: !0, reason: "cleaning_up" },
587
602
  error: { hasError: !1 }
588
603
  }), o) {
589
- const n = await O(
604
+ const r = await A(
590
605
  () => o.removeItem(s),
591
606
  "Error removing contact data"
592
607
  );
593
- if (!n.success)
594
- throw n.error;
608
+ if (!r.success)
609
+ throw r.error;
595
610
  }
596
- a.setState({
611
+ n.setState({
597
612
  contact: null,
598
613
  loading: { isLoading: !1 },
599
614
  error: { hasError: !1 }
600
- }), a.clear();
601
- } catch (n) {
602
- console.error("Error cleaning up contact:", n), a.setStatePartial({
615
+ }), n.clear();
616
+ } catch (r) {
617
+ console.error("Error cleaning up contact:", r), n.setStatePartial({
603
618
  error: {
604
619
  hasError: !0,
605
- message: n instanceof Error ? n.message : "Failed to cleanup contact data",
620
+ message: r instanceof Error ? r.message : "Failed to cleanup contact data",
606
621
  code: "CONTACT_CLEANUP_FAILED"
607
622
  }
608
623
  });
609
624
  } finally {
610
- a.setStatePartial({
625
+ n.setStatePartial({
611
626
  loading: { isLoading: !1, reason: null }
612
627
  });
613
628
  }
614
629
  }
615
630
  return {
616
- contactState: a,
631
+ contactState: n,
617
632
  shouldCollectData: l,
618
- loadContact: r,
619
- saveContact: c,
620
- cleanup: d
633
+ loadContact: a,
634
+ saveContact: i,
635
+ cleanup: c
621
636
  };
622
637
  }
623
- const X = {
638
+ const V = {
624
639
  messageArrived: "https://cloud.opencopilot.so/sfx/notification3.mp3"
625
- }, $ = {
640
+ }, x = {
626
641
  primaryColor: "hsl(211,65%,59%)",
627
642
  triggerOffset: "20px"
628
- }, z = 1e3 * 3;
629
- function oe(t) {
630
- var s, o, f, a, r, c, l, d, n;
643
+ }, K = 1e3 * 3;
644
+ function ce(t) {
645
+ var s, o, f, n, a, i, l, c, r;
631
646
  if (!t.token)
632
647
  throw new Error("Token is required");
633
- if (t.pollingInterval && t.pollingInterval < z)
648
+ if (t.pollingInterval && t.pollingInterval < K)
634
649
  throw new Error("Polling interval must be at least 3 seconds");
635
650
  const e = {
636
651
  ...t,
637
652
  collectUserData: t.collectUserData ?? !1,
638
653
  apiUrl: t.apiUrl ?? "https://api-v2.opencopilot.so/backend",
639
- socketUrl: t.socketUrl ?? "https://api-v2.opencopilot.so",
640
654
  pollingInterval: t.pollingInterval ?? 3e3,
641
655
  headers: t.headers ?? {},
642
656
  queryParams: t.queryParams ?? {},
@@ -645,22 +659,22 @@ function oe(t) {
645
659
  name: ((s = t.bot) == null ? void 0 : s.name) ?? "Bot",
646
660
  avatarUrl: (o = t.bot) == null ? void 0 : o.avatarUrl,
647
661
  id: ((f = t.bot) == null ? void 0 : f.id) ?? null,
648
- is_ai: ((a = t.bot) == null ? void 0 : a.is_ai) ?? !0
662
+ is_ai: ((n = t.bot) == null ? void 0 : n.is_ai) ?? !0
649
663
  },
650
664
  contactToken: t.contactToken,
651
665
  debug: t.debug ?? !1,
652
666
  language: t.language ?? "en",
653
667
  user: t.user ?? {},
654
668
  soundEffectFiles: {
655
- messageArrived: ((r = t.soundEffectFiles) == null ? void 0 : r.messageArrived) ?? X.messageArrived
669
+ messageArrived: ((a = t.soundEffectFiles) == null ? void 0 : a.messageArrived) ?? V.messageArrived
656
670
  },
657
671
  theme: {
658
- primaryColor: ((c = t.theme) == null ? void 0 : c.primaryColor) ?? $.primaryColor,
659
- triggerOffset: ((l = t.theme) == null ? void 0 : l.triggerOffset) ?? $.triggerOffset
672
+ primaryColor: ((i = t.theme) == null ? void 0 : i.primaryColor) ?? x.primaryColor,
673
+ triggerOffset: ((l = t.theme) == null ? void 0 : l.triggerOffset) ?? x.triggerOffset
660
674
  },
661
675
  settings: {
662
- persistSession: ((d = t.settings) == null ? void 0 : d.persistSession) ?? !1,
663
- useSoundEffects: ((n = t.settings) == null ? void 0 : n.useSoundEffects) ?? !1
676
+ persistSession: ((c = t.settings) == null ? void 0 : c.persistSession) ?? !1,
677
+ useSoundEffects: ((r = t.settings) == null ? void 0 : r.useSoundEffects) ?? !1
664
678
  }
665
679
  };
666
680
  return {
@@ -681,50 +695,50 @@ function oe(t) {
681
695
  getDebugMode: () => e.debug
682
696
  };
683
697
  }
684
- function V(t = {}) {
685
- const e = [], s = [], o = [], f = async (a, r = {}) => {
686
- let c = {
698
+ function Y(t = {}) {
699
+ const e = [], s = [], o = [], f = async (n, a = {}) => {
700
+ let i = {
687
701
  ...t,
688
- ...r,
702
+ ...a,
689
703
  headers: {
690
704
  ...t.headers,
691
- ...r.headers
705
+ ...a.headers
692
706
  }
693
707
  };
694
708
  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;
709
+ for (const u of e)
710
+ i = await u(i);
711
+ const l = i.params ? "?" + new URLSearchParams(i.params).toString() : "", c = i.baseURL ? `${i.baseURL}${n}${l}`.replace(/([^:]\/)\/+/g, "$1") : `${n}${l}`;
712
+ let r = await fetch(c, i);
713
+ for (const u of s)
714
+ r = await u(r);
715
+ return r;
702
716
  } catch (l) {
703
- let d = l;
704
- for (const n of o)
705
- d = await n(d);
706
- throw d;
717
+ let c = l;
718
+ for (const r of o)
719
+ c = await r(c);
720
+ throw c;
707
721
  }
708
722
  };
709
723
  return f.interceptors = {
710
724
  request: {
711
- use: (a) => (e.push(a), e.length - 1),
712
- eject: (a) => {
713
- e.splice(a, 1);
725
+ use: (n) => (e.push(n), e.length - 1),
726
+ eject: (n) => {
727
+ e.splice(n, 1);
714
728
  }
715
729
  },
716
730
  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);
731
+ use: (n, a) => (s.push(n), a && o.push(a), s.length - 1),
732
+ eject: (n) => {
733
+ s.splice(n, 1), o.splice(n, 1);
720
734
  }
721
735
  }
722
736
  }, f;
723
737
  }
724
- var I;
725
- class ie {
738
+ var b;
739
+ class le {
726
740
  constructor(e) {
727
- C(this, I);
741
+ M(this, b);
728
742
  this.options = e;
729
743
  const s = this.options.config.user, o = {
730
744
  claim: "",
@@ -737,19 +751,19 @@ class ie {
737
751
  "Content-Type": "application/json",
738
752
  Accept: "application/json"
739
753
  };
740
- this.options.config.contactToken && (f.Authorization = `Bearer ${this.options.config.contactToken}`), P(this, I, V({
754
+ this.options.config.contactToken && (f.Authorization = `Bearer ${this.options.config.contactToken}`), _(this, b, Y({
741
755
  baseURL: `${this.options.config.apiUrl}/widget/v2`,
742
756
  headers: f
743
757
  }));
744
758
  }
745
759
  async me() {
746
- return (await S(this, I).call(this, "/me")).json();
760
+ return (await h(this, b).call(this, "/me")).json();
747
761
  }
748
762
  async widgetPrelude() {
749
- return (await S(this, I).call(this, "/prelude")).json();
763
+ return (await h(this, b).call(this, "/prelude")).json();
750
764
  }
751
765
  async handleMessage(e) {
752
- return (await S(this, I).call(this, "/chat/send", {
766
+ return (await h(this, b).call(this, "/chat/send", {
753
767
  method: "POST",
754
768
  body: JSON.stringify(e)
755
769
  })).json();
@@ -758,54 +772,95 @@ class ie {
758
772
  const o = new URLSearchParams({
759
773
  lastMessageTimestamp: s || ""
760
774
  }), f = `/session/history/${e}?${o.toString()}`;
761
- return (await S(this, I).call(this, f, {
775
+ return (await h(this, b).call(this, f, {
762
776
  method: "GET"
763
777
  })).json();
764
778
  }
765
779
  async createSession() {
766
- return (await S(this, I).call(this, "/create-session", {
780
+ return (await h(this, b).call(this, "/create-session", {
767
781
  method: "POST"
768
782
  })).json();
769
783
  }
770
784
  async getSession(e) {
771
- return (await S(this, I).call(this, `/session/${e}`, {
785
+ return (await h(this, b).call(this, `/session/${e}`, {
772
786
  method: "GET"
773
787
  })).json();
774
788
  }
775
- async createContact(e) {
776
- return (await S(this, I).call(this, "/contact/upsert", {
777
- method: "POST",
778
- body: JSON.stringify(e)
779
- })).json();
789
+ }
790
+ b = new WeakMap();
791
+ function ue(t = {}) {
792
+ const {
793
+ level: e = "info",
794
+ prefix: s = "[openCx]",
795
+ enabled: o = !0
796
+ } = t;
797
+ let f = e;
798
+ const n = {
799
+ debug: 0,
800
+ info: 1,
801
+ warn: 2,
802
+ error: 3
803
+ };
804
+ function a(c) {
805
+ return o && n[c] >= n[f];
806
+ }
807
+ function i(c) {
808
+ return `${s} ${c}`;
809
+ }
810
+ function l(c) {
811
+ return c.map((r) => {
812
+ if (r instanceof Error)
813
+ return {
814
+ name: r.name,
815
+ message: r.message,
816
+ stack: r.stack
817
+ };
818
+ if (typeof r == "object")
819
+ try {
820
+ return JSON.stringify(r, null, 2);
821
+ } catch {
822
+ return r;
823
+ }
824
+ return r;
825
+ });
780
826
  }
827
+ return {
828
+ debug(c, ...r) {
829
+ a("debug") && console.debug(i(c), ...l(r));
830
+ },
831
+ info(c, ...r) {
832
+ a("info") && console.info(i(c), ...l(r));
833
+ },
834
+ warn(c, ...r) {
835
+ a("warn") && console.warn(i(c), ...l(r));
836
+ },
837
+ error(c, ...r) {
838
+ a("error") && console.error(i(c), ...l(r));
839
+ },
840
+ setLevel(c) {
841
+ f = c;
842
+ }
843
+ };
781
844
  }
782
- I = new WeakMap();
783
845
  export {
784
- ue as AIClosureType,
785
- ie as ApiCaller,
786
- ee as AuthenticationError,
787
- Z as ConnectionError,
788
- 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,
795
- B as SessionError,
796
- te as SessionNotDefinedError,
797
- ge as SessionStatus,
798
- se as TransportError,
799
- Se as chatAttachmentSchema,
800
- he as chatHistoryMessageSchema,
801
- pe as chatSessionSchema,
802
- Ee as consumerSchema,
803
- ae as createChat,
804
- oe as createConfig,
805
- ne as createContact,
806
- W as createPubSub,
807
- T as isStorageAvailable,
808
- O as safeStorageOperation,
809
- ye as structuredSocketMessageSchema
846
+ le as ApiCaller,
847
+ se as AuthenticationError,
848
+ te as ConnectionError,
849
+ ne as FileUploadError,
850
+ G as LifecycleEvent,
851
+ me as MessageTypeEnum,
852
+ v as OpenCXError,
853
+ C as PubSub,
854
+ J as SessionError,
855
+ re as SessionNotDefinedError,
856
+ ae as TransportError,
857
+ ge as chatAttachmentSchema,
858
+ oe as createChat,
859
+ ce as createConfig,
860
+ ie as createContact,
861
+ ue as createLogger,
862
+ ee as createPubSub,
863
+ L as isStorageAvailable,
864
+ A as safeStorageOperation
810
865
  };
811
866
  //# sourceMappingURL=index.js.map