@opencx/widget 2.6.1 → 2.6.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.
- package/dist/api-BEtFfKMB.cjs +2 -0
- package/dist/api-BEtFfKMB.cjs.map +1 -0
- package/dist/api-Cez5T-se.js +1393 -0
- package/dist/api-Cez5T-se.js.map +1 -0
- package/dist/basic.cjs +39 -39
- package/dist/basic.cjs.map +1 -1
- package/dist/basic.js +1214 -1213
- package/dist/basic.js.map +1 -1
- package/dist/core/client/api.d.ts +49 -3
- package/dist/core/client/chat.d.ts +10 -4
- package/dist/core/client/contact.d.ts +9 -3
- package/dist/core/client/index.d.ts +1 -1
- package/dist/core/index.d.ts +1 -1
- package/dist/core/types/contact.d.ts +2 -0
- package/dist/core/types/helpers.d.ts +2 -2
- package/dist/{index-B5YLfjom.cjs → index-BzidnDPX.cjs} +4 -4
- package/dist/index-BzidnDPX.cjs.map +1 -0
- package/dist/{index-EZN9_jan.js → index-DUlnC30_.js} +26 -26
- package/dist/index-DUlnC30_.js.map +1 -0
- package/dist/index.cjs +1 -1
- package/dist/index.js +6 -6
- package/dist/react-web/core-integration/ChatProvider.d.ts +11 -5
- package/dist/react-web/core-integration/hooks/useChatMessages.d.ts +10 -4
- package/dist/react-web/core-integration/hooks/useChatSession.d.ts +10 -4
- package/dist/react-web/core-integration/hooks/useContact.d.ts +10 -4
- package/dist/react-web/core-integration/hooks/usePreludeData.d.ts +1 -1
- package/dist/react.cjs +1 -1
- package/dist/react.js +1 -1
- package/dist-embed/script.js +30 -30
- package/dist-embed/script.js.map +1 -1
- package/package.json +2 -2
- package/dist/api-BwKgAJ6p.cjs +0 -2
- package/dist/api-BwKgAJ6p.cjs.map +0 -1
- package/dist/api-DRZ9vPwy.js +0 -1360
- package/dist/api-DRZ9vPwy.js.map +0 -1
- package/dist/index-B5YLfjom.cjs.map +0 -1
- package/dist/index-EZN9_jan.js.map +0 -1
|
@@ -0,0 +1,1393 @@
|
|
|
1
|
+
var ne = Object.defineProperty;
|
|
2
|
+
var Q = (t) => {
|
|
3
|
+
throw TypeError(t);
|
|
4
|
+
};
|
|
5
|
+
var ae = (t, e, s) => e in t ? ne(t, e, { enumerable: !0, configurable: !0, writable: !0, value: s }) : t[e] = s;
|
|
6
|
+
var S = (t, e, s) => ae(t, typeof e != "symbol" ? e + "" : e, s), V = (t, e, s) => e.has(t) || Q("Cannot " + s);
|
|
7
|
+
var y = (t, e, s) => (V(t, e, "read from private field"), s ? s.call(t) : e.get(t)), G = (t, e, s) => e.has(t) ? Q("Cannot add the same private member more than once") : e instanceof WeakSet ? e.add(t) : e.set(t, s), F = (t, e, s, r) => (V(t, e, "write to private field"), r ? r.call(t, s) : e.set(t, s), s);
|
|
8
|
+
import ie from "lodash.isequal";
|
|
9
|
+
import { v4 as oe } from "uuid";
|
|
10
|
+
class N extends Error {
|
|
11
|
+
constructor(e) {
|
|
12
|
+
super(e), this.name = "OpenCXError";
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
class xe extends N {
|
|
16
|
+
constructor(e = "Connection failed") {
|
|
17
|
+
super(e), this.name = "ConnectionError";
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
class Me extends N {
|
|
21
|
+
constructor(e = "Authentication failed") {
|
|
22
|
+
super(e), this.name = "AuthenticationError";
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
class le extends N {
|
|
26
|
+
constructor(e = "Session error occurred") {
|
|
27
|
+
super(e), this.name = "SessionError";
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
class Fe extends le {
|
|
31
|
+
constructor(e = "Session not defined") {
|
|
32
|
+
super(e), this.name = "SessionNotDefinedError";
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
class Ne extends N {
|
|
36
|
+
constructor(e = "Transport error occurred") {
|
|
37
|
+
super(e), this.name = "TransportError";
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
class ke extends N {
|
|
41
|
+
constructor(e = "File upload failed") {
|
|
42
|
+
super(e), this.name = "FileUploadError";
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
class ce extends N {
|
|
46
|
+
constructor(e = "Storage is not available") {
|
|
47
|
+
super(e), this.name = "StorageNotAvailableError";
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
class je extends N {
|
|
51
|
+
constructor(e = "External ID is not defined") {
|
|
52
|
+
super(e), this.name = "ExternalIdNotDefinedError";
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
var ue = /* @__PURE__ */ ((t) => (t.INIT = "init", t.STATE_CHANGE = "stateChange", t.BEFORE_UPDATE = "beforeUpdate", t.AFTER_UPDATE = "afterUpdate", t.DESTROY = "destroy", t.ERROR = "error", t))(ue || {}), _, k;
|
|
56
|
+
class X {
|
|
57
|
+
constructor(e) {
|
|
58
|
+
S(this, "subscribers", /* @__PURE__ */ new Set());
|
|
59
|
+
G(this, _);
|
|
60
|
+
S(this, "initialState");
|
|
61
|
+
G(this, k);
|
|
62
|
+
S(this, "lifecycleListeners", /* @__PURE__ */ new Map());
|
|
63
|
+
S(this, "emitLifecycle", (e, s) => {
|
|
64
|
+
const r = this.lifecycleListeners.get(e);
|
|
65
|
+
if (r) {
|
|
66
|
+
const a = {
|
|
67
|
+
type: e,
|
|
68
|
+
timestamp: Date.now(),
|
|
69
|
+
data: s
|
|
70
|
+
};
|
|
71
|
+
r.forEach((l) => {
|
|
72
|
+
try {
|
|
73
|
+
l(a);
|
|
74
|
+
} catch {
|
|
75
|
+
}
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
});
|
|
79
|
+
S(this, "notifySubscribers", (e) => {
|
|
80
|
+
Array.from(this.subscribers).forEach((r) => {
|
|
81
|
+
try {
|
|
82
|
+
r(e);
|
|
83
|
+
} catch (a) {
|
|
84
|
+
this.emitLifecycle("error", { error: a });
|
|
85
|
+
}
|
|
86
|
+
});
|
|
87
|
+
});
|
|
88
|
+
/**
|
|
89
|
+
* Subscribe to state changes
|
|
90
|
+
* @param callback Function to call when state changes
|
|
91
|
+
* @returns Unsubscribe function
|
|
92
|
+
*/
|
|
93
|
+
S(this, "subscribe", (e) => (this.subscribers.add(e), () => {
|
|
94
|
+
this.subscribers.delete(e);
|
|
95
|
+
}));
|
|
96
|
+
S(this, "onLifecycle", (e, s) => {
|
|
97
|
+
this.lifecycleListeners.has(e) || this.lifecycleListeners.set(e, /* @__PURE__ */ new Set());
|
|
98
|
+
const r = this.lifecycleListeners.get(e);
|
|
99
|
+
return r.add(s), () => {
|
|
100
|
+
r.delete(s), r.size === 0 && this.lifecycleListeners.delete(e);
|
|
101
|
+
};
|
|
102
|
+
});
|
|
103
|
+
/**
|
|
104
|
+
* Get the current state
|
|
105
|
+
*/
|
|
106
|
+
S(this, "getState", () => y(this, _));
|
|
107
|
+
/**
|
|
108
|
+
* Set the state and notify subscribers if the state changes
|
|
109
|
+
* @param newState The new state to set
|
|
110
|
+
*/
|
|
111
|
+
S(this, "setState", (e) => {
|
|
112
|
+
this.emitLifecycle("beforeUpdate", {
|
|
113
|
+
previousState: y(this, _),
|
|
114
|
+
nextState: e
|
|
115
|
+
}), ie(y(this, _), e) || (F(this, _, e), F(this, k, Date.now()), this.emitLifecycle("stateChange", { state: e }), this.notifySubscribers(e)), this.emitLifecycle("afterUpdate", { state: e });
|
|
116
|
+
});
|
|
117
|
+
S(this, "setStatePartial", (e) => {
|
|
118
|
+
if (e == null) return;
|
|
119
|
+
const s = { ...y(this, _), ...e };
|
|
120
|
+
this.setState(s);
|
|
121
|
+
});
|
|
122
|
+
/**
|
|
123
|
+
* Clear all subscriptions
|
|
124
|
+
*/
|
|
125
|
+
S(this, "clear", () => {
|
|
126
|
+
this.emitLifecycle(
|
|
127
|
+
"destroy"
|
|
128
|
+
/* DESTROY */
|
|
129
|
+
), this.subscribers = /* @__PURE__ */ new Set(), this.lifecycleListeners = /* @__PURE__ */ new Map();
|
|
130
|
+
});
|
|
131
|
+
S(this, "reset", () => {
|
|
132
|
+
this.setState(this.initialState);
|
|
133
|
+
});
|
|
134
|
+
S(this, "lastUpdated", () => y(this, k));
|
|
135
|
+
F(this, _, e), this.initialState = e, F(this, k, Date.now()), this.emitLifecycle("init", { initialState: y(this, _) });
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
_ = new WeakMap(), k = new WeakMap();
|
|
139
|
+
function qe(t) {
|
|
140
|
+
return new X(t);
|
|
141
|
+
}
|
|
142
|
+
function U(t) {
|
|
143
|
+
if (!t) return !1;
|
|
144
|
+
try {
|
|
145
|
+
return typeof t.isAvailable == "function" ? t.isAvailable() : !0;
|
|
146
|
+
} catch {
|
|
147
|
+
return !1;
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
async function Y(t, e) {
|
|
151
|
+
try {
|
|
152
|
+
return { success: !0, result: await t(), error: null };
|
|
153
|
+
} catch (s) {
|
|
154
|
+
return {
|
|
155
|
+
success: !1,
|
|
156
|
+
result: null,
|
|
157
|
+
error: {
|
|
158
|
+
message: s instanceof Error ? s.message || "Unknown error" : (s == null ? void 0 : s.toString()) || "Unknown error",
|
|
159
|
+
code: "STORAGE_OPERATION_FAILED",
|
|
160
|
+
context: e
|
|
161
|
+
}
|
|
162
|
+
};
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
function $e(t = {}) {
|
|
166
|
+
const {
|
|
167
|
+
level: e = "info",
|
|
168
|
+
prefix: s = "[openCx]",
|
|
169
|
+
enabled: r = !0
|
|
170
|
+
} = t;
|
|
171
|
+
let a = e;
|
|
172
|
+
const l = {
|
|
173
|
+
debug: 0,
|
|
174
|
+
info: 1,
|
|
175
|
+
warn: 2,
|
|
176
|
+
error: 3
|
|
177
|
+
};
|
|
178
|
+
function c(i) {
|
|
179
|
+
return r && l[i] >= l[a];
|
|
180
|
+
}
|
|
181
|
+
function n(i) {
|
|
182
|
+
return `${s} ${i}`;
|
|
183
|
+
}
|
|
184
|
+
function d(i) {
|
|
185
|
+
return i.map((o) => {
|
|
186
|
+
if (o instanceof Error)
|
|
187
|
+
return {
|
|
188
|
+
name: o.name,
|
|
189
|
+
message: o.message,
|
|
190
|
+
stack: o.stack
|
|
191
|
+
};
|
|
192
|
+
if (typeof o == "object")
|
|
193
|
+
try {
|
|
194
|
+
return JSON.stringify(o, null, 2);
|
|
195
|
+
} catch {
|
|
196
|
+
return o;
|
|
197
|
+
}
|
|
198
|
+
return o;
|
|
199
|
+
});
|
|
200
|
+
}
|
|
201
|
+
return {
|
|
202
|
+
debug(i, ...o) {
|
|
203
|
+
c("debug") && console.debug(n(i), ...d(o));
|
|
204
|
+
},
|
|
205
|
+
info(i, ...o) {
|
|
206
|
+
c("info") && console.info(n(i), ...d(o));
|
|
207
|
+
},
|
|
208
|
+
warn(i, ...o) {
|
|
209
|
+
c("warn") && console.warn(n(i), ...d(o));
|
|
210
|
+
},
|
|
211
|
+
error(i, ...o) {
|
|
212
|
+
c("error") && console.error(n(i), ...d(o));
|
|
213
|
+
},
|
|
214
|
+
setLevel(i) {
|
|
215
|
+
a = i;
|
|
216
|
+
}
|
|
217
|
+
};
|
|
218
|
+
}
|
|
219
|
+
function fe(t) {
|
|
220
|
+
if (!t) return !1;
|
|
221
|
+
try {
|
|
222
|
+
return typeof t.isAvailable == "function" ? t.isAvailable() : !0;
|
|
223
|
+
} catch {
|
|
224
|
+
return !1;
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
async function de(t, e) {
|
|
228
|
+
try {
|
|
229
|
+
return await t(), { success: !0, error: null };
|
|
230
|
+
} catch (s) {
|
|
231
|
+
return {
|
|
232
|
+
success: !1,
|
|
233
|
+
error: {
|
|
234
|
+
message: s instanceof Error ? s.message || "Unknown error" : (s == null ? void 0 : s.toString()) || "Unknown error",
|
|
235
|
+
code: "AUDIO_OPERATION_FAILED",
|
|
236
|
+
context: e
|
|
237
|
+
}
|
|
238
|
+
};
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
function B() {
|
|
242
|
+
return oe();
|
|
243
|
+
}
|
|
244
|
+
function ge({ config: t, api: e }, s) {
|
|
245
|
+
const r = new X({
|
|
246
|
+
contact: null,
|
|
247
|
+
loading: { isLoading: !1, reason: null },
|
|
248
|
+
error: { hasError: !1 }
|
|
249
|
+
});
|
|
250
|
+
function a() {
|
|
251
|
+
var d;
|
|
252
|
+
return !((d = r.getState().contact) != null && d.contactId) && t.getConfig().collectUserData ? {
|
|
253
|
+
should: !0,
|
|
254
|
+
reason: "No contact id and collectUserData is true"
|
|
255
|
+
} : {
|
|
256
|
+
should: !1
|
|
257
|
+
};
|
|
258
|
+
}
|
|
259
|
+
async function l() {
|
|
260
|
+
try {
|
|
261
|
+
r.setStatePartial({
|
|
262
|
+
loading: { isLoading: !0, reason: "cleaning_up" },
|
|
263
|
+
error: { hasError: !1 }
|
|
264
|
+
}), r.setState({
|
|
265
|
+
contact: null,
|
|
266
|
+
loading: { isLoading: !1, reason: null },
|
|
267
|
+
error: { hasError: !1 }
|
|
268
|
+
}), r.clear();
|
|
269
|
+
} catch (n) {
|
|
270
|
+
r.setStatePartial({
|
|
271
|
+
error: {
|
|
272
|
+
hasError: !0,
|
|
273
|
+
message: n instanceof Error ? n.message : "Failed to cleanup contact data",
|
|
274
|
+
code: "CONTACT_CLEANUP_FAILED"
|
|
275
|
+
}
|
|
276
|
+
});
|
|
277
|
+
} finally {
|
|
278
|
+
r.setStatePartial({
|
|
279
|
+
loading: { isLoading: !1, reason: null }
|
|
280
|
+
});
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
async function c(n) {
|
|
284
|
+
r.setStatePartial({
|
|
285
|
+
loading: { isLoading: !0, reason: "creating_unauthenticated_contact" },
|
|
286
|
+
error: { hasError: !1 }
|
|
287
|
+
});
|
|
288
|
+
const { data: d, error: i } = await e.createContact(n);
|
|
289
|
+
return d != null && d.contactId ? (r.setStatePartial({
|
|
290
|
+
contact: {
|
|
291
|
+
authenticationStatus: {
|
|
292
|
+
is: !1
|
|
293
|
+
},
|
|
294
|
+
contactId: d.contactId,
|
|
295
|
+
contactName: d.contactName
|
|
296
|
+
}
|
|
297
|
+
}), e.setUser({ email: n.email, name: n.name }), d) : (i && r.setStatePartial({
|
|
298
|
+
loading: { isLoading: !1, reason: null },
|
|
299
|
+
error: { hasError: !0, message: i == null ? void 0 : i.message, code: "CONTACT_CREATION_FAILED" }
|
|
300
|
+
}), null);
|
|
301
|
+
}
|
|
302
|
+
return {
|
|
303
|
+
contactState: r,
|
|
304
|
+
shouldCollectData: a,
|
|
305
|
+
cleanup: l,
|
|
306
|
+
createUnauthenticatedContact: c
|
|
307
|
+
};
|
|
308
|
+
}
|
|
309
|
+
const J = {
|
|
310
|
+
SESSION: 1e4,
|
|
311
|
+
// every 10 seconds
|
|
312
|
+
MESSAGES: 5e3
|
|
313
|
+
// every 5 seconds
|
|
314
|
+
};
|
|
315
|
+
function me(t) {
|
|
316
|
+
const e = {
|
|
317
|
+
id: t.publicId,
|
|
318
|
+
timestamp: t.sentAt || "",
|
|
319
|
+
attachments: t.attachments || void 0
|
|
320
|
+
};
|
|
321
|
+
return t.sender.kind === "user" ? {
|
|
322
|
+
...e,
|
|
323
|
+
type: "FROM_USER",
|
|
324
|
+
content: t.content.text || "",
|
|
325
|
+
deliveredAt: t.sentAt || ""
|
|
326
|
+
} : t.sender.kind === "agent" ? {
|
|
327
|
+
id: t.publicId,
|
|
328
|
+
type: "FROM_AGENT",
|
|
329
|
+
component: "agent_message",
|
|
330
|
+
data: {
|
|
331
|
+
message: t.content.text || ""
|
|
332
|
+
},
|
|
333
|
+
timestamp: t.sentAt || "",
|
|
334
|
+
attachments: t.attachments || void 0
|
|
335
|
+
} : {
|
|
336
|
+
...e,
|
|
337
|
+
type: "FROM_BOT",
|
|
338
|
+
component: "bot_message",
|
|
339
|
+
agent: {
|
|
340
|
+
id: null,
|
|
341
|
+
name: t.sender.name || "",
|
|
342
|
+
isAi: t.sender.kind === "ai",
|
|
343
|
+
avatar: t.sender.avatar || null
|
|
344
|
+
},
|
|
345
|
+
data: {
|
|
346
|
+
message: t.content.text
|
|
347
|
+
}
|
|
348
|
+
};
|
|
349
|
+
}
|
|
350
|
+
function Se(t, e, s, r, a) {
|
|
351
|
+
async function l(i) {
|
|
352
|
+
var b;
|
|
353
|
+
const o = e.getState().messages, m = (b = e.getState().messages.at(-1)) == null ? void 0 : b.timestamp, { data: f } = await t.getSessionHistory(
|
|
354
|
+
i.id,
|
|
355
|
+
m
|
|
356
|
+
);
|
|
357
|
+
if (f && f.length > 0) {
|
|
358
|
+
const w = f.map(me).filter(
|
|
359
|
+
(R) => !o.some((I) => I.id === R.id)
|
|
360
|
+
);
|
|
361
|
+
w.length > 0 && (s == null || s.debug("Adding new messages to state", {
|
|
362
|
+
count: w.length,
|
|
363
|
+
messageIds: w.map((I) => I.id),
|
|
364
|
+
messageTypes: w.map((I) => I.type)
|
|
365
|
+
}), r != null && r.getConfig().settings.useSoundEffects && (a != null && a.audio) && fe(a.audio) && w.filter(
|
|
366
|
+
(p) => p.type === "FROM_BOT"
|
|
367
|
+
).length > 0 && await de(
|
|
368
|
+
() => a.audio.playNotification(),
|
|
369
|
+
"Failed to play notification sound for new messages"
|
|
370
|
+
), e.setStatePartial({
|
|
371
|
+
messages: [...o, ...w]
|
|
372
|
+
}));
|
|
373
|
+
}
|
|
374
|
+
}
|
|
375
|
+
function c(i, o) {
|
|
376
|
+
return {
|
|
377
|
+
id: B(),
|
|
378
|
+
type: "FROM_USER",
|
|
379
|
+
content: i,
|
|
380
|
+
deliveredAt: (/* @__PURE__ */ new Date()).toISOString(),
|
|
381
|
+
attachments: o,
|
|
382
|
+
timestamp: (/* @__PURE__ */ new Date()).toISOString()
|
|
383
|
+
};
|
|
384
|
+
}
|
|
385
|
+
function n(i) {
|
|
386
|
+
if (i.success && i.autopilotResponse)
|
|
387
|
+
return {
|
|
388
|
+
type: "FROM_BOT",
|
|
389
|
+
id: i.autopilotResponse.id || B(),
|
|
390
|
+
timestamp: (/* @__PURE__ */ new Date()).toISOString(),
|
|
391
|
+
component: "bot_message",
|
|
392
|
+
data: {
|
|
393
|
+
message: i.autopilotResponse.value.content
|
|
394
|
+
}
|
|
395
|
+
};
|
|
396
|
+
if (i.success && i.uiResponse) {
|
|
397
|
+
const o = i.uiResponse.value;
|
|
398
|
+
return {
|
|
399
|
+
type: "FROM_BOT",
|
|
400
|
+
id: B(),
|
|
401
|
+
timestamp: (/* @__PURE__ */ new Date()).toISOString(),
|
|
402
|
+
component: o.name,
|
|
403
|
+
data: o.request_response
|
|
404
|
+
};
|
|
405
|
+
}
|
|
406
|
+
return null;
|
|
407
|
+
}
|
|
408
|
+
function d(i) {
|
|
409
|
+
return {
|
|
410
|
+
type: "FROM_BOT",
|
|
411
|
+
id: B(),
|
|
412
|
+
timestamp: (/* @__PURE__ */ new Date()).toISOString(),
|
|
413
|
+
component: "TEXT",
|
|
414
|
+
data: {
|
|
415
|
+
message: i,
|
|
416
|
+
variant: "error"
|
|
417
|
+
}
|
|
418
|
+
};
|
|
419
|
+
}
|
|
420
|
+
return {
|
|
421
|
+
fetchHistoryMessages: l,
|
|
422
|
+
toUserMessage: c,
|
|
423
|
+
toBotMessage: n,
|
|
424
|
+
addErrorMessage: d
|
|
425
|
+
};
|
|
426
|
+
}
|
|
427
|
+
function he(t, e, s, r, a, l) {
|
|
428
|
+
var D, L;
|
|
429
|
+
let c = null;
|
|
430
|
+
const n = (D = l.platform) == null ? void 0 : D.logger, d = (L = l.platform) == null ? void 0 : L.storage, i = () => a.getConfig().settings.persistSession;
|
|
431
|
+
if (i() && !U(d))
|
|
432
|
+
throw new ce();
|
|
433
|
+
i() && !a.getConfig().user.external_id && (n == null || n.error(
|
|
434
|
+
"session persistence is enabled but external id is not defined"
|
|
435
|
+
));
|
|
436
|
+
const { token: o, user: m } = a.getConfig(), f = `${m.external_id ?? m.email ?? "unknown"}:${o}:session`;
|
|
437
|
+
async function b() {
|
|
438
|
+
n == null || n.debug("Restoring session from storage", {
|
|
439
|
+
sessionStorageKey: f,
|
|
440
|
+
stroageAvailable: U(d)
|
|
441
|
+
}), U(d) && Y(async () => {
|
|
442
|
+
n == null || n.debug("Attempting to restore session from storage");
|
|
443
|
+
const u = await d.getItem(f);
|
|
444
|
+
if (u) {
|
|
445
|
+
const g = JSON.parse(u);
|
|
446
|
+
n == null || n.info("Session restored from storage", {
|
|
447
|
+
sessionId: g.id
|
|
448
|
+
}), e.setState(g), await r.fetchHistoryMessages(g), R();
|
|
449
|
+
}
|
|
450
|
+
}, "Error restoring session from storage");
|
|
451
|
+
}
|
|
452
|
+
function w() {
|
|
453
|
+
n == null || n.debug("Setting up session persistence", {
|
|
454
|
+
sessionStorageKey: f,
|
|
455
|
+
stroageAvailable: U(d)
|
|
456
|
+
}), U(d) && e.subscribe(async (u) => {
|
|
457
|
+
try {
|
|
458
|
+
u ? (await d.setItem(f, JSON.stringify(u)), n == null || n.debug("Session persisted to storage", {
|
|
459
|
+
sessionId: u.id
|
|
460
|
+
})) : (await d.removeItem(f), n == null || n.debug("Session removed from storage"));
|
|
461
|
+
} catch (g) {
|
|
462
|
+
n == null || n.error("Error persisting session:", g), s.setStatePartial({
|
|
463
|
+
error: {
|
|
464
|
+
hasError: !0,
|
|
465
|
+
message: g instanceof Error ? g.message : "Failed to persist session",
|
|
466
|
+
code: "SESSION_PERSISTENCE_FAILED"
|
|
467
|
+
}
|
|
468
|
+
});
|
|
469
|
+
}
|
|
470
|
+
});
|
|
471
|
+
}
|
|
472
|
+
function R() {
|
|
473
|
+
if (c) return;
|
|
474
|
+
n == null || n.debug("Starting polling");
|
|
475
|
+
const u = [];
|
|
476
|
+
u.push(
|
|
477
|
+
setInterval(async () => {
|
|
478
|
+
const g = e.getState();
|
|
479
|
+
if (g != null && g.id)
|
|
480
|
+
try {
|
|
481
|
+
const h = /* @__PURE__ */ new Date();
|
|
482
|
+
s.setStatePartial({
|
|
483
|
+
polling: {
|
|
484
|
+
...s.getState().polling,
|
|
485
|
+
session: {
|
|
486
|
+
isPolling: !0,
|
|
487
|
+
lastPollTime: h.toISOString(),
|
|
488
|
+
nextPollTime: new Date(
|
|
489
|
+
h.getTime() + J.SESSION
|
|
490
|
+
).toISOString(),
|
|
491
|
+
error: { hasError: !1 }
|
|
492
|
+
}
|
|
493
|
+
}
|
|
494
|
+
});
|
|
495
|
+
const { data: q } = await t.getSession(g.id);
|
|
496
|
+
q && e.setState(q), s.setStatePartial({
|
|
497
|
+
polling: {
|
|
498
|
+
...s.getState().polling,
|
|
499
|
+
session: {
|
|
500
|
+
...s.getState().polling.session,
|
|
501
|
+
isPolling: !1
|
|
502
|
+
}
|
|
503
|
+
}
|
|
504
|
+
});
|
|
505
|
+
} catch (h) {
|
|
506
|
+
n == null || n.error("Error polling session:", h), s.setStatePartial({
|
|
507
|
+
polling: {
|
|
508
|
+
...s.getState().polling,
|
|
509
|
+
session: {
|
|
510
|
+
...s.getState().polling.session,
|
|
511
|
+
isPolling: !1,
|
|
512
|
+
error: {
|
|
513
|
+
hasError: !0,
|
|
514
|
+
message: h instanceof Error ? h.message : "Failed to poll session",
|
|
515
|
+
code: "SESSION_POLLING_FAILED"
|
|
516
|
+
}
|
|
517
|
+
}
|
|
518
|
+
}
|
|
519
|
+
});
|
|
520
|
+
}
|
|
521
|
+
}, J.SESSION)
|
|
522
|
+
), u.push(
|
|
523
|
+
setInterval(async () => {
|
|
524
|
+
const g = e.getState();
|
|
525
|
+
if (g != null && g.id)
|
|
526
|
+
try {
|
|
527
|
+
const h = /* @__PURE__ */ new Date();
|
|
528
|
+
s.setStatePartial({
|
|
529
|
+
polling: {
|
|
530
|
+
...s.getState().polling,
|
|
531
|
+
messages: {
|
|
532
|
+
isPolling: !0,
|
|
533
|
+
lastPollTime: h.toISOString(),
|
|
534
|
+
nextPollTime: new Date(
|
|
535
|
+
h.getTime() + J.MESSAGES
|
|
536
|
+
).toISOString(),
|
|
537
|
+
error: { hasError: !1 }
|
|
538
|
+
}
|
|
539
|
+
}
|
|
540
|
+
}), await r.fetchHistoryMessages(g), s.setStatePartial({
|
|
541
|
+
polling: {
|
|
542
|
+
...s.getState().polling,
|
|
543
|
+
messages: {
|
|
544
|
+
...s.getState().polling.messages,
|
|
545
|
+
isPolling: !1
|
|
546
|
+
}
|
|
547
|
+
}
|
|
548
|
+
});
|
|
549
|
+
} catch (h) {
|
|
550
|
+
n == null || n.error("Error polling messages:", h), s.setStatePartial({
|
|
551
|
+
polling: {
|
|
552
|
+
...s.getState().polling,
|
|
553
|
+
messages: {
|
|
554
|
+
...s.getState().polling.messages,
|
|
555
|
+
isPolling: !1,
|
|
556
|
+
error: {
|
|
557
|
+
hasError: !0,
|
|
558
|
+
message: h instanceof Error ? h.message : "Failed to poll messages",
|
|
559
|
+
code: "MESSAGES_POLLING_FAILED"
|
|
560
|
+
}
|
|
561
|
+
}
|
|
562
|
+
}
|
|
563
|
+
});
|
|
564
|
+
}
|
|
565
|
+
}, J.MESSAGES)
|
|
566
|
+
), c = () => {
|
|
567
|
+
n == null || n.debug("Stopping polling"), u.forEach(clearInterval), s.setStatePartial({
|
|
568
|
+
polling: {
|
|
569
|
+
session: {
|
|
570
|
+
isPolling: !1,
|
|
571
|
+
lastPollTime: null,
|
|
572
|
+
nextPollTime: null,
|
|
573
|
+
error: { hasError: !1 }
|
|
574
|
+
},
|
|
575
|
+
messages: {
|
|
576
|
+
isPolling: !1,
|
|
577
|
+
lastPollTime: null,
|
|
578
|
+
nextPollTime: null,
|
|
579
|
+
error: { hasError: !1 }
|
|
580
|
+
}
|
|
581
|
+
}
|
|
582
|
+
});
|
|
583
|
+
};
|
|
584
|
+
}
|
|
585
|
+
async function I() {
|
|
586
|
+
n == null || n.info("Creating new session"), s.setStatePartial({
|
|
587
|
+
loading: { isLoading: !0, reason: "creating_session" },
|
|
588
|
+
error: { hasError: !1 }
|
|
589
|
+
});
|
|
590
|
+
const { data: u, error: g } = await t.createSession();
|
|
591
|
+
if (u)
|
|
592
|
+
return n == null || n.info("Session created successfully", { sessionId: u.id }), e.setState(u), s.setStatePartial({
|
|
593
|
+
loading: { isLoading: !1, reason: null }
|
|
594
|
+
}), R(), u;
|
|
595
|
+
n == null || n.error("Failed to create session:", g);
|
|
596
|
+
const h = {
|
|
597
|
+
hasError: !0,
|
|
598
|
+
message: g instanceof Error ? g.message : "Failed to create session",
|
|
599
|
+
code: "SESSION_CREATION_FAILED"
|
|
600
|
+
};
|
|
601
|
+
return s.setStatePartial({
|
|
602
|
+
error: h,
|
|
603
|
+
loading: { isLoading: !1, reason: null }
|
|
604
|
+
}), null;
|
|
605
|
+
}
|
|
606
|
+
async function p() {
|
|
607
|
+
var g;
|
|
608
|
+
const u = e.getState();
|
|
609
|
+
if (u != null && u.id)
|
|
610
|
+
try {
|
|
611
|
+
c && (c(), c = null), e.setState(null), i() && d && await d.removeItem(f), s.setState({
|
|
612
|
+
messages: [],
|
|
613
|
+
keyboard: null,
|
|
614
|
+
loading: { isLoading: !1, reason: null },
|
|
615
|
+
error: { hasError: !1 },
|
|
616
|
+
polling: {
|
|
617
|
+
session: {
|
|
618
|
+
isPolling: !1,
|
|
619
|
+
lastPollTime: null,
|
|
620
|
+
nextPollTime: null,
|
|
621
|
+
error: { hasError: !1 }
|
|
622
|
+
},
|
|
623
|
+
messages: {
|
|
624
|
+
isPolling: !1,
|
|
625
|
+
lastPollTime: null,
|
|
626
|
+
nextPollTime: null,
|
|
627
|
+
error: { hasError: !1 }
|
|
628
|
+
}
|
|
629
|
+
}
|
|
630
|
+
}), (g = l.onSessionDestroy) == null || g.call(l);
|
|
631
|
+
} catch (h) {
|
|
632
|
+
s.setStatePartial({
|
|
633
|
+
error: {
|
|
634
|
+
hasError: !0,
|
|
635
|
+
message: h instanceof Error ? h.message : "Failed to clear session",
|
|
636
|
+
code: "SESSION_CLEAR_FAILED"
|
|
637
|
+
}
|
|
638
|
+
});
|
|
639
|
+
}
|
|
640
|
+
}
|
|
641
|
+
function C(u = !1) {
|
|
642
|
+
try {
|
|
643
|
+
c && (c(), c = null), u && i() && U(d) && (console.log("removing session data", f), Y(
|
|
644
|
+
() => d.removeItem(f),
|
|
645
|
+
"Error removing session data"
|
|
646
|
+
)), s.setState({
|
|
647
|
+
messages: [],
|
|
648
|
+
keyboard: null,
|
|
649
|
+
loading: { isLoading: !1, reason: null },
|
|
650
|
+
error: { hasError: !1 },
|
|
651
|
+
polling: {
|
|
652
|
+
session: {
|
|
653
|
+
isPolling: !1,
|
|
654
|
+
lastPollTime: null,
|
|
655
|
+
nextPollTime: null,
|
|
656
|
+
error: { hasError: !1 }
|
|
657
|
+
},
|
|
658
|
+
messages: {
|
|
659
|
+
isPolling: !1,
|
|
660
|
+
lastPollTime: null,
|
|
661
|
+
nextPollTime: null,
|
|
662
|
+
error: { hasError: !1 }
|
|
663
|
+
}
|
|
664
|
+
}
|
|
665
|
+
}), e.setState(null), s.clear(), e.clear();
|
|
666
|
+
} catch (g) {
|
|
667
|
+
s.setStatePartial({
|
|
668
|
+
error: {
|
|
669
|
+
hasError: !0,
|
|
670
|
+
message: g instanceof Error ? g.message : "Failed to cleanup",
|
|
671
|
+
code: "SESSION_CLEAR_FAILED"
|
|
672
|
+
}
|
|
673
|
+
});
|
|
674
|
+
}
|
|
675
|
+
}
|
|
676
|
+
async function v(u) {
|
|
677
|
+
return t.getSession(u);
|
|
678
|
+
}
|
|
679
|
+
async function O() {
|
|
680
|
+
const u = e.getState();
|
|
681
|
+
if (!(u != null && u.id)) return;
|
|
682
|
+
const { data: g } = await v(u.id);
|
|
683
|
+
return g && e.setState(g), g;
|
|
684
|
+
}
|
|
685
|
+
return i() && U(d) && (n == null || n.debug("Initializing session persistence", {
|
|
686
|
+
sessionStorageKey: f,
|
|
687
|
+
stroageAvailable: U(d)
|
|
688
|
+
}), b(), w()), {
|
|
689
|
+
createSession: I,
|
|
690
|
+
clearSession: p,
|
|
691
|
+
cleanup: C,
|
|
692
|
+
startPolling: R,
|
|
693
|
+
fetchSession: v,
|
|
694
|
+
refetchSession: O,
|
|
695
|
+
sessionStorageKey: f
|
|
696
|
+
};
|
|
697
|
+
}
|
|
698
|
+
function ze(t) {
|
|
699
|
+
var w;
|
|
700
|
+
const e = (w = t.platform) == null ? void 0 : w.logger, s = {
|
|
701
|
+
messages: [],
|
|
702
|
+
keyboard: null,
|
|
703
|
+
loading: { isLoading: !1, reason: null },
|
|
704
|
+
error: { hasError: !1 },
|
|
705
|
+
polling: {
|
|
706
|
+
session: {
|
|
707
|
+
isPolling: !1,
|
|
708
|
+
lastPollTime: null,
|
|
709
|
+
nextPollTime: null,
|
|
710
|
+
error: { hasError: !1 }
|
|
711
|
+
},
|
|
712
|
+
messages: {
|
|
713
|
+
isPolling: !1,
|
|
714
|
+
lastPollTime: null,
|
|
715
|
+
nextPollTime: null,
|
|
716
|
+
error: { hasError: !1 }
|
|
717
|
+
}
|
|
718
|
+
}
|
|
719
|
+
}, r = new X(s), {
|
|
720
|
+
contactState: a,
|
|
721
|
+
cleanup: l,
|
|
722
|
+
shouldCollectData: c,
|
|
723
|
+
createUnauthenticatedContact: n
|
|
724
|
+
} = ge(
|
|
725
|
+
{ config: t.config, api: t.api },
|
|
726
|
+
t.platform
|
|
727
|
+
), d = new X(null), i = Se(
|
|
728
|
+
t.api,
|
|
729
|
+
r,
|
|
730
|
+
e,
|
|
731
|
+
t.config,
|
|
732
|
+
t.platform
|
|
733
|
+
), o = he(
|
|
734
|
+
t.api,
|
|
735
|
+
d,
|
|
736
|
+
r,
|
|
737
|
+
i,
|
|
738
|
+
t.config,
|
|
739
|
+
t
|
|
740
|
+
);
|
|
741
|
+
async function m(R, I) {
|
|
742
|
+
var v;
|
|
743
|
+
let p = d.getState(), C = !1;
|
|
744
|
+
if (!(p != null && p.id)) {
|
|
745
|
+
if (e == null || e.debug("No active session, creating new session"), p = await o.createSession(), !p)
|
|
746
|
+
return {
|
|
747
|
+
success: !1,
|
|
748
|
+
createdSession: C
|
|
749
|
+
};
|
|
750
|
+
C = !0;
|
|
751
|
+
}
|
|
752
|
+
p.assignee.kind === "ai" && (p = await o.refetchSession() ?? p);
|
|
753
|
+
try {
|
|
754
|
+
e == null || e.debug("Sending message", { sessionId: p.id }), p.assignee.kind === "ai" ? r.setStatePartial({
|
|
755
|
+
loading: { isLoading: !0, reason: "sending_message_to_bot" },
|
|
756
|
+
error: { hasError: !1 }
|
|
757
|
+
}) : r.setStatePartial({
|
|
758
|
+
loading: { isLoading: !0, reason: "sending_message_to_agent" },
|
|
759
|
+
error: { hasError: !1 }
|
|
760
|
+
});
|
|
761
|
+
const O = i.toUserMessage(
|
|
762
|
+
R.content,
|
|
763
|
+
R.attachments || void 0
|
|
764
|
+
), D = r.getState().messages;
|
|
765
|
+
r.setStatePartial({
|
|
766
|
+
messages: [...D, O]
|
|
767
|
+
});
|
|
768
|
+
const L = t.config.getConfig(), { data: u } = await t.api.handleMessage(
|
|
769
|
+
{
|
|
770
|
+
uuid: O.id,
|
|
771
|
+
bot_token: L.token,
|
|
772
|
+
headers: L.headers,
|
|
773
|
+
query_params: L.queryParams,
|
|
774
|
+
session_id: p.id,
|
|
775
|
+
user: L.user,
|
|
776
|
+
...R
|
|
777
|
+
},
|
|
778
|
+
I
|
|
779
|
+
);
|
|
780
|
+
if (u != null && u.success) {
|
|
781
|
+
e == null || e.debug("Message sent successfully");
|
|
782
|
+
const g = i.toBotMessage(u);
|
|
783
|
+
if (g) {
|
|
784
|
+
const h = r.getState().messages;
|
|
785
|
+
r.setStatePartial({
|
|
786
|
+
messages: [...h, g]
|
|
787
|
+
});
|
|
788
|
+
}
|
|
789
|
+
return {
|
|
790
|
+
success: !0,
|
|
791
|
+
createdSession: C,
|
|
792
|
+
botMessage: g
|
|
793
|
+
};
|
|
794
|
+
} else {
|
|
795
|
+
e == null || e.warn("Message send failed", u == null ? void 0 : u.error);
|
|
796
|
+
const g = i.addErrorMessage(
|
|
797
|
+
((v = u == null ? void 0 : u.error) == null ? void 0 : v.message) || "Unknown error occurred"
|
|
798
|
+
), h = r.getState().messages;
|
|
799
|
+
return r.setStatePartial({
|
|
800
|
+
messages: [...h, g]
|
|
801
|
+
}), {
|
|
802
|
+
success: !1,
|
|
803
|
+
createdSession: C,
|
|
804
|
+
error: u == null ? void 0 : u.error
|
|
805
|
+
};
|
|
806
|
+
}
|
|
807
|
+
} catch (O) {
|
|
808
|
+
return e == null || e.error("Error sending message:", O), r.setStatePartial({
|
|
809
|
+
error: {
|
|
810
|
+
hasError: !0,
|
|
811
|
+
message: O instanceof Error ? O.message : "Failed to send message",
|
|
812
|
+
code: "MESSAGE_SEND_FAILED"
|
|
813
|
+
}
|
|
814
|
+
}), {
|
|
815
|
+
success: !1,
|
|
816
|
+
createdSession: C,
|
|
817
|
+
error: O
|
|
818
|
+
};
|
|
819
|
+
} finally {
|
|
820
|
+
r.setStatePartial({
|
|
821
|
+
loading: { isLoading: !1, reason: null }
|
|
822
|
+
});
|
|
823
|
+
}
|
|
824
|
+
}
|
|
825
|
+
async function f() {
|
|
826
|
+
await o.clearSession(), await o.createSession();
|
|
827
|
+
}
|
|
828
|
+
const b = () => {
|
|
829
|
+
l(), o.cleanup();
|
|
830
|
+
};
|
|
831
|
+
return {
|
|
832
|
+
chatState: r,
|
|
833
|
+
sessionState: d,
|
|
834
|
+
sendMessage: m,
|
|
835
|
+
createSession: o.createSession,
|
|
836
|
+
clearSession: o.clearSession,
|
|
837
|
+
cleanup: b,
|
|
838
|
+
initialState: s,
|
|
839
|
+
sessionStorageKey: o.sessionStorageKey,
|
|
840
|
+
recreateSession: f,
|
|
841
|
+
contactState: a,
|
|
842
|
+
contactManager: {
|
|
843
|
+
shouldCollectData: c,
|
|
844
|
+
createUnauthenticatedContact: n
|
|
845
|
+
}
|
|
846
|
+
};
|
|
847
|
+
}
|
|
848
|
+
const pe = {
|
|
849
|
+
messageArrived: "https://cloud.opencopilot.so/sfx/notification3.mp3"
|
|
850
|
+
}, Ee = {
|
|
851
|
+
primaryColor: "hsl(211,65%,59%)",
|
|
852
|
+
triggerOffset: "20px"
|
|
853
|
+
}, te = {
|
|
854
|
+
persistSession: !1,
|
|
855
|
+
useSoundEffects: !1
|
|
856
|
+
}, ye = 1e3 * 3;
|
|
857
|
+
function be(t, e, s) {
|
|
858
|
+
const r = e.logger, a = e.storage;
|
|
859
|
+
let l = t ?? te;
|
|
860
|
+
async function c() {
|
|
861
|
+
if (!(!a || !U(a)))
|
|
862
|
+
try {
|
|
863
|
+
r == null || r.debug("Attempting to restore settings from storage");
|
|
864
|
+
const o = await a.getItem(s);
|
|
865
|
+
o && (l = JSON.parse(o), r == null || r.debug("Settings restored from storage", l));
|
|
866
|
+
} catch (o) {
|
|
867
|
+
r == null || r.error("Error restoring settings:", o);
|
|
868
|
+
}
|
|
869
|
+
}
|
|
870
|
+
async function n(o) {
|
|
871
|
+
if (!(!a || !U(a)))
|
|
872
|
+
try {
|
|
873
|
+
await a.setItem(s, JSON.stringify(o)), r == null || r.debug("Settings persisted to storage", o);
|
|
874
|
+
} catch (m) {
|
|
875
|
+
r == null || r.error("Error persisting settings:", m);
|
|
876
|
+
}
|
|
877
|
+
}
|
|
878
|
+
function d(o) {
|
|
879
|
+
l = {
|
|
880
|
+
...l,
|
|
881
|
+
...o
|
|
882
|
+
}, n(l);
|
|
883
|
+
}
|
|
884
|
+
function i() {
|
|
885
|
+
return l;
|
|
886
|
+
}
|
|
887
|
+
return c(), {
|
|
888
|
+
getSettings: i,
|
|
889
|
+
updateSettings: d
|
|
890
|
+
};
|
|
891
|
+
}
|
|
892
|
+
function He(t, e) {
|
|
893
|
+
var c, n, d, i, o;
|
|
894
|
+
if (!t.token)
|
|
895
|
+
throw new Error("Token is required");
|
|
896
|
+
if (t.pollingInterval && t.pollingInterval < ye)
|
|
897
|
+
throw new Error("Polling interval must be at least 3 seconds");
|
|
898
|
+
const s = {
|
|
899
|
+
...t,
|
|
900
|
+
collectUserData: t.collectUserData ?? !1,
|
|
901
|
+
initialMessages: t.initialMessages ?? [],
|
|
902
|
+
apiUrl: t.apiUrl ?? "https://api.open.cx",
|
|
903
|
+
pollingInterval: t.pollingInterval ?? 3e3,
|
|
904
|
+
headers: t.headers ?? {},
|
|
905
|
+
queryParams: t.queryParams ?? {},
|
|
906
|
+
pathParams: t.pathParams ?? {},
|
|
907
|
+
bot: {
|
|
908
|
+
name: ((c = t.bot) == null ? void 0 : c.name) ?? "Bot",
|
|
909
|
+
avatar: ((n = t.bot) == null ? void 0 : n.avatar) ?? null,
|
|
910
|
+
id: ((d = t.bot) == null ? void 0 : d.id) ?? null,
|
|
911
|
+
isAi: ((i = t.bot) == null ? void 0 : i.isAi) ?? !0
|
|
912
|
+
},
|
|
913
|
+
contactToken: t.contactToken,
|
|
914
|
+
debug: t.debug ?? !1,
|
|
915
|
+
language: t.language ?? "en",
|
|
916
|
+
user: t.user ?? {},
|
|
917
|
+
soundEffectFiles: Object.assign({}, pe, t.soundEffectFiles),
|
|
918
|
+
theme: Object.assign({}, Ee, t.theme),
|
|
919
|
+
settings: Object.assign({}, te, t.settings),
|
|
920
|
+
assets: {
|
|
921
|
+
organizationLogo: ((o = t.assets) == null ? void 0 : o.organizationLogo) ?? ""
|
|
922
|
+
}
|
|
923
|
+
}, r = `${s.token}:settings`, { getSettings: a, updateSettings: l } = be(s.settings, e, r);
|
|
924
|
+
return {
|
|
925
|
+
config: s,
|
|
926
|
+
updateSettings: l,
|
|
927
|
+
getConfig: () => ({
|
|
928
|
+
...s,
|
|
929
|
+
settings: a()
|
|
930
|
+
}),
|
|
931
|
+
getApiConfig: () => ({
|
|
932
|
+
apiUrl: s.apiUrl,
|
|
933
|
+
token: s.token,
|
|
934
|
+
headers: s.headers,
|
|
935
|
+
queryParams: s.queryParams,
|
|
936
|
+
pathParams: s.pathParams
|
|
937
|
+
}),
|
|
938
|
+
getBotConfig: () => s.bot,
|
|
939
|
+
getThemeConfig: () => s.theme,
|
|
940
|
+
getSoundEffects: () => s.soundEffectFiles,
|
|
941
|
+
getUser: () => s.user,
|
|
942
|
+
getLanguage: () => s.language,
|
|
943
|
+
getDebugMode: () => s.debug
|
|
944
|
+
};
|
|
945
|
+
}
|
|
946
|
+
const we = /\{[^{}]+\}/g, Pe = () => {
|
|
947
|
+
var t, e;
|
|
948
|
+
return typeof process == "object" && Number.parseInt((e = (t = process == null ? void 0 : process.versions) == null ? void 0 : t.node) == null ? void 0 : e.substring(0, 2)) >= 18 && process.versions.undici;
|
|
949
|
+
};
|
|
950
|
+
function Te() {
|
|
951
|
+
return Math.random().toString(36).slice(2, 11);
|
|
952
|
+
}
|
|
953
|
+
function Ae(t) {
|
|
954
|
+
let {
|
|
955
|
+
baseUrl: e = "",
|
|
956
|
+
Request: s = globalThis.Request,
|
|
957
|
+
fetch: r = globalThis.fetch,
|
|
958
|
+
querySerializer: a,
|
|
959
|
+
bodySerializer: l,
|
|
960
|
+
headers: c,
|
|
961
|
+
requestInitExt: n = void 0,
|
|
962
|
+
...d
|
|
963
|
+
} = { ...t };
|
|
964
|
+
n = Pe() ? n : void 0, e = ee(e);
|
|
965
|
+
const i = [];
|
|
966
|
+
async function o(m, f) {
|
|
967
|
+
const {
|
|
968
|
+
baseUrl: b,
|
|
969
|
+
fetch: w = r,
|
|
970
|
+
Request: R = s,
|
|
971
|
+
headers: I,
|
|
972
|
+
params: p = {},
|
|
973
|
+
parseAs: C = "json",
|
|
974
|
+
querySerializer: v,
|
|
975
|
+
bodySerializer: O = l ?? Oe,
|
|
976
|
+
body: D,
|
|
977
|
+
...L
|
|
978
|
+
} = f || {};
|
|
979
|
+
b && (e = ee(b));
|
|
980
|
+
let u = typeof a == "function" ? a : Z(a);
|
|
981
|
+
v && (u = typeof v == "function" ? v : Z({
|
|
982
|
+
...typeof a == "object" ? a : {},
|
|
983
|
+
...v
|
|
984
|
+
}));
|
|
985
|
+
const g = D === void 0 ? void 0 : O(D), h = (
|
|
986
|
+
// with no body, we should not to set Content-Type
|
|
987
|
+
g === void 0 || // if serialized body is FormData; browser will correctly set Content-Type & boundary expression
|
|
988
|
+
g instanceof FormData ? {} : {
|
|
989
|
+
"Content-Type": "application/json"
|
|
990
|
+
}
|
|
991
|
+
), q = {
|
|
992
|
+
redirect: "follow",
|
|
993
|
+
...d,
|
|
994
|
+
...L,
|
|
995
|
+
body: g,
|
|
996
|
+
headers: Ce(h, c, I, p.header)
|
|
997
|
+
};
|
|
998
|
+
let $, z, x = new s(Re(m, { baseUrl: e, params: p, querySerializer: u }), q);
|
|
999
|
+
for (const P in L)
|
|
1000
|
+
P in x || (x[P] = L[P]);
|
|
1001
|
+
if (i.length) {
|
|
1002
|
+
$ = Te(), z = Object.freeze({
|
|
1003
|
+
baseUrl: e,
|
|
1004
|
+
fetch: w,
|
|
1005
|
+
parseAs: C,
|
|
1006
|
+
querySerializer: u,
|
|
1007
|
+
bodySerializer: O
|
|
1008
|
+
});
|
|
1009
|
+
for (const P of i)
|
|
1010
|
+
if (P && typeof P == "object" && typeof P.onRequest == "function") {
|
|
1011
|
+
const T = await P.onRequest({
|
|
1012
|
+
request: x,
|
|
1013
|
+
schemaPath: m,
|
|
1014
|
+
params: p,
|
|
1015
|
+
options: z,
|
|
1016
|
+
id: $
|
|
1017
|
+
});
|
|
1018
|
+
if (T) {
|
|
1019
|
+
if (!(T instanceof s))
|
|
1020
|
+
throw new Error("onRequest: must return new Request() when modifying the request");
|
|
1021
|
+
x = T;
|
|
1022
|
+
}
|
|
1023
|
+
}
|
|
1024
|
+
}
|
|
1025
|
+
let E;
|
|
1026
|
+
try {
|
|
1027
|
+
E = await w(x, n);
|
|
1028
|
+
} catch (P) {
|
|
1029
|
+
let T = P;
|
|
1030
|
+
if (i.length)
|
|
1031
|
+
for (let M = i.length - 1; M >= 0; M--) {
|
|
1032
|
+
const H = i[M];
|
|
1033
|
+
if (H && typeof H == "object" && typeof H.onError == "function") {
|
|
1034
|
+
const j = await H.onError({
|
|
1035
|
+
request: x,
|
|
1036
|
+
error: T,
|
|
1037
|
+
schemaPath: m,
|
|
1038
|
+
params: p,
|
|
1039
|
+
options: z,
|
|
1040
|
+
id: $
|
|
1041
|
+
});
|
|
1042
|
+
if (j) {
|
|
1043
|
+
if (j instanceof Response) {
|
|
1044
|
+
T = void 0, E = j;
|
|
1045
|
+
break;
|
|
1046
|
+
}
|
|
1047
|
+
if (j instanceof Error) {
|
|
1048
|
+
T = j;
|
|
1049
|
+
continue;
|
|
1050
|
+
}
|
|
1051
|
+
throw new Error("onError: must return new Response() or instance of Error");
|
|
1052
|
+
}
|
|
1053
|
+
}
|
|
1054
|
+
}
|
|
1055
|
+
if (T)
|
|
1056
|
+
throw T;
|
|
1057
|
+
}
|
|
1058
|
+
if (i.length)
|
|
1059
|
+
for (let P = i.length - 1; P >= 0; P--) {
|
|
1060
|
+
const T = i[P];
|
|
1061
|
+
if (T && typeof T == "object" && typeof T.onResponse == "function") {
|
|
1062
|
+
const M = await T.onResponse({
|
|
1063
|
+
request: x,
|
|
1064
|
+
response: E,
|
|
1065
|
+
schemaPath: m,
|
|
1066
|
+
params: p,
|
|
1067
|
+
options: z,
|
|
1068
|
+
id: $
|
|
1069
|
+
});
|
|
1070
|
+
if (M) {
|
|
1071
|
+
if (!(M instanceof Response))
|
|
1072
|
+
throw new Error("onResponse: must return new Response() when modifying the response");
|
|
1073
|
+
E = M;
|
|
1074
|
+
}
|
|
1075
|
+
}
|
|
1076
|
+
}
|
|
1077
|
+
if (E.status === 204 || E.headers.get("Content-Length") === "0")
|
|
1078
|
+
return E.ok ? { data: void 0, response: E } : { error: void 0, response: E };
|
|
1079
|
+
if (E.ok)
|
|
1080
|
+
return C === "stream" ? { data: E.body, response: E } : { data: await E[C](), response: E };
|
|
1081
|
+
let K = await E.text();
|
|
1082
|
+
try {
|
|
1083
|
+
K = JSON.parse(K);
|
|
1084
|
+
} catch {
|
|
1085
|
+
}
|
|
1086
|
+
return { error: K, response: E };
|
|
1087
|
+
}
|
|
1088
|
+
return {
|
|
1089
|
+
request(m, f, b) {
|
|
1090
|
+
return o(f, { ...b, method: m.toUpperCase() });
|
|
1091
|
+
},
|
|
1092
|
+
/** Call a GET endpoint */
|
|
1093
|
+
GET(m, f) {
|
|
1094
|
+
return o(m, { ...f, method: "GET" });
|
|
1095
|
+
},
|
|
1096
|
+
/** Call a PUT endpoint */
|
|
1097
|
+
PUT(m, f) {
|
|
1098
|
+
return o(m, { ...f, method: "PUT" });
|
|
1099
|
+
},
|
|
1100
|
+
/** Call a POST endpoint */
|
|
1101
|
+
POST(m, f) {
|
|
1102
|
+
return o(m, { ...f, method: "POST" });
|
|
1103
|
+
},
|
|
1104
|
+
/** Call a DELETE endpoint */
|
|
1105
|
+
DELETE(m, f) {
|
|
1106
|
+
return o(m, { ...f, method: "DELETE" });
|
|
1107
|
+
},
|
|
1108
|
+
/** Call a OPTIONS endpoint */
|
|
1109
|
+
OPTIONS(m, f) {
|
|
1110
|
+
return o(m, { ...f, method: "OPTIONS" });
|
|
1111
|
+
},
|
|
1112
|
+
/** Call a HEAD endpoint */
|
|
1113
|
+
HEAD(m, f) {
|
|
1114
|
+
return o(m, { ...f, method: "HEAD" });
|
|
1115
|
+
},
|
|
1116
|
+
/** Call a PATCH endpoint */
|
|
1117
|
+
PATCH(m, f) {
|
|
1118
|
+
return o(m, { ...f, method: "PATCH" });
|
|
1119
|
+
},
|
|
1120
|
+
/** Call a TRACE endpoint */
|
|
1121
|
+
TRACE(m, f) {
|
|
1122
|
+
return o(m, { ...f, method: "TRACE" });
|
|
1123
|
+
},
|
|
1124
|
+
/** Register middleware */
|
|
1125
|
+
use(...m) {
|
|
1126
|
+
for (const f of m)
|
|
1127
|
+
if (f) {
|
|
1128
|
+
if (typeof f != "object" || !("onRequest" in f || "onResponse" in f || "onError" in f))
|
|
1129
|
+
throw new Error("Middleware must be an object with one of `onRequest()`, `onResponse() or `onError()`");
|
|
1130
|
+
i.push(f);
|
|
1131
|
+
}
|
|
1132
|
+
},
|
|
1133
|
+
/** Unregister middleware */
|
|
1134
|
+
eject(...m) {
|
|
1135
|
+
for (const f of m) {
|
|
1136
|
+
const b = i.indexOf(f);
|
|
1137
|
+
b !== -1 && i.splice(b, 1);
|
|
1138
|
+
}
|
|
1139
|
+
}
|
|
1140
|
+
};
|
|
1141
|
+
}
|
|
1142
|
+
function W(t, e, s) {
|
|
1143
|
+
if (e == null)
|
|
1144
|
+
return "";
|
|
1145
|
+
if (typeof e == "object")
|
|
1146
|
+
throw new Error(
|
|
1147
|
+
"Deeply-nested arrays/objects aren’t supported. Provide your own `querySerializer()` to handle these."
|
|
1148
|
+
);
|
|
1149
|
+
return `${t}=${(s == null ? void 0 : s.allowReserved) === !0 ? e : encodeURIComponent(e)}`;
|
|
1150
|
+
}
|
|
1151
|
+
function se(t, e, s) {
|
|
1152
|
+
if (!e || typeof e != "object")
|
|
1153
|
+
return "";
|
|
1154
|
+
const r = [], a = {
|
|
1155
|
+
simple: ",",
|
|
1156
|
+
label: ".",
|
|
1157
|
+
matrix: ";"
|
|
1158
|
+
}[s.style] || "&";
|
|
1159
|
+
if (s.style !== "deepObject" && s.explode === !1) {
|
|
1160
|
+
for (const n in e)
|
|
1161
|
+
r.push(n, s.allowReserved === !0 ? e[n] : encodeURIComponent(e[n]));
|
|
1162
|
+
const c = r.join(",");
|
|
1163
|
+
switch (s.style) {
|
|
1164
|
+
case "form":
|
|
1165
|
+
return `${t}=${c}`;
|
|
1166
|
+
case "label":
|
|
1167
|
+
return `.${c}`;
|
|
1168
|
+
case "matrix":
|
|
1169
|
+
return `;${t}=${c}`;
|
|
1170
|
+
default:
|
|
1171
|
+
return c;
|
|
1172
|
+
}
|
|
1173
|
+
}
|
|
1174
|
+
for (const c in e) {
|
|
1175
|
+
const n = s.style === "deepObject" ? `${t}[${c}]` : c;
|
|
1176
|
+
r.push(W(n, e[c], s));
|
|
1177
|
+
}
|
|
1178
|
+
const l = r.join(a);
|
|
1179
|
+
return s.style === "label" || s.style === "matrix" ? `${a}${l}` : l;
|
|
1180
|
+
}
|
|
1181
|
+
function re(t, e, s) {
|
|
1182
|
+
if (!Array.isArray(e))
|
|
1183
|
+
return "";
|
|
1184
|
+
if (s.explode === !1) {
|
|
1185
|
+
const l = { form: ",", spaceDelimited: "%20", pipeDelimited: "|" }[s.style] || ",", c = (s.allowReserved === !0 ? e : e.map((n) => encodeURIComponent(n))).join(l);
|
|
1186
|
+
switch (s.style) {
|
|
1187
|
+
case "simple":
|
|
1188
|
+
return c;
|
|
1189
|
+
case "label":
|
|
1190
|
+
return `.${c}`;
|
|
1191
|
+
case "matrix":
|
|
1192
|
+
return `;${t}=${c}`;
|
|
1193
|
+
default:
|
|
1194
|
+
return `${t}=${c}`;
|
|
1195
|
+
}
|
|
1196
|
+
}
|
|
1197
|
+
const r = { simple: ",", label: ".", matrix: ";" }[s.style] || "&", a = [];
|
|
1198
|
+
for (const l of e)
|
|
1199
|
+
s.style === "simple" || s.style === "label" ? a.push(s.allowReserved === !0 ? l : encodeURIComponent(l)) : a.push(W(t, l, s));
|
|
1200
|
+
return s.style === "label" || s.style === "matrix" ? `${r}${a.join(r)}` : a.join(r);
|
|
1201
|
+
}
|
|
1202
|
+
function Z(t) {
|
|
1203
|
+
return function(s) {
|
|
1204
|
+
const r = [];
|
|
1205
|
+
if (s && typeof s == "object")
|
|
1206
|
+
for (const a in s) {
|
|
1207
|
+
const l = s[a];
|
|
1208
|
+
if (l != null) {
|
|
1209
|
+
if (Array.isArray(l)) {
|
|
1210
|
+
if (l.length === 0)
|
|
1211
|
+
continue;
|
|
1212
|
+
r.push(
|
|
1213
|
+
re(a, l, {
|
|
1214
|
+
style: "form",
|
|
1215
|
+
explode: !0,
|
|
1216
|
+
...t == null ? void 0 : t.array,
|
|
1217
|
+
allowReserved: (t == null ? void 0 : t.allowReserved) || !1
|
|
1218
|
+
})
|
|
1219
|
+
);
|
|
1220
|
+
continue;
|
|
1221
|
+
}
|
|
1222
|
+
if (typeof l == "object") {
|
|
1223
|
+
r.push(
|
|
1224
|
+
se(a, l, {
|
|
1225
|
+
style: "deepObject",
|
|
1226
|
+
explode: !0,
|
|
1227
|
+
...t == null ? void 0 : t.object,
|
|
1228
|
+
allowReserved: (t == null ? void 0 : t.allowReserved) || !1
|
|
1229
|
+
})
|
|
1230
|
+
);
|
|
1231
|
+
continue;
|
|
1232
|
+
}
|
|
1233
|
+
r.push(W(a, l, t));
|
|
1234
|
+
}
|
|
1235
|
+
}
|
|
1236
|
+
return r.join("&");
|
|
1237
|
+
};
|
|
1238
|
+
}
|
|
1239
|
+
function Ie(t, e) {
|
|
1240
|
+
let s = t;
|
|
1241
|
+
for (const r of t.match(we) ?? []) {
|
|
1242
|
+
let a = r.substring(1, r.length - 1), l = !1, c = "simple";
|
|
1243
|
+
if (a.endsWith("*") && (l = !0, a = a.substring(0, a.length - 1)), a.startsWith(".") ? (c = "label", a = a.substring(1)) : a.startsWith(";") && (c = "matrix", a = a.substring(1)), !e || e[a] === void 0 || e[a] === null)
|
|
1244
|
+
continue;
|
|
1245
|
+
const n = e[a];
|
|
1246
|
+
if (Array.isArray(n)) {
|
|
1247
|
+
s = s.replace(r, re(a, n, { style: c, explode: l }));
|
|
1248
|
+
continue;
|
|
1249
|
+
}
|
|
1250
|
+
if (typeof n == "object") {
|
|
1251
|
+
s = s.replace(r, se(a, n, { style: c, explode: l }));
|
|
1252
|
+
continue;
|
|
1253
|
+
}
|
|
1254
|
+
if (c === "matrix") {
|
|
1255
|
+
s = s.replace(r, `;${W(a, n)}`);
|
|
1256
|
+
continue;
|
|
1257
|
+
}
|
|
1258
|
+
s = s.replace(r, c === "label" ? `.${encodeURIComponent(n)}` : encodeURIComponent(n));
|
|
1259
|
+
}
|
|
1260
|
+
return s;
|
|
1261
|
+
}
|
|
1262
|
+
function Oe(t) {
|
|
1263
|
+
return t instanceof FormData ? t : JSON.stringify(t);
|
|
1264
|
+
}
|
|
1265
|
+
function Re(t, e) {
|
|
1266
|
+
var a;
|
|
1267
|
+
let s = `${e.baseUrl}${t}`;
|
|
1268
|
+
(a = e.params) != null && a.path && (s = Ie(s, e.params.path));
|
|
1269
|
+
let r = e.querySerializer(e.params.query ?? {});
|
|
1270
|
+
return r.startsWith("?") && (r = r.substring(1)), r && (s += `?${r}`), s;
|
|
1271
|
+
}
|
|
1272
|
+
function Ce(...t) {
|
|
1273
|
+
const e = new Headers();
|
|
1274
|
+
for (const s of t) {
|
|
1275
|
+
if (!s || typeof s != "object")
|
|
1276
|
+
continue;
|
|
1277
|
+
const r = s instanceof Headers ? s.entries() : Object.entries(s);
|
|
1278
|
+
for (const [a, l] of r)
|
|
1279
|
+
if (l === null)
|
|
1280
|
+
e.delete(a);
|
|
1281
|
+
else if (Array.isArray(l))
|
|
1282
|
+
for (const c of l)
|
|
1283
|
+
e.append(a, c);
|
|
1284
|
+
else l !== void 0 && e.set(a, l);
|
|
1285
|
+
}
|
|
1286
|
+
return e;
|
|
1287
|
+
}
|
|
1288
|
+
function ee(t) {
|
|
1289
|
+
return t.endsWith("/") ? t.substring(0, t.length - 1) : t;
|
|
1290
|
+
}
|
|
1291
|
+
const Le = (t) => {
|
|
1292
|
+
console.log(t.error);
|
|
1293
|
+
}, ve = (t) => {
|
|
1294
|
+
const e = Ae({
|
|
1295
|
+
baseUrl: t.baseUrl
|
|
1296
|
+
}), s = {
|
|
1297
|
+
onRequest: t.onRequest,
|
|
1298
|
+
onResponse: t.onResponse,
|
|
1299
|
+
onError: t.onError || Le
|
|
1300
|
+
};
|
|
1301
|
+
return e.use(s), e;
|
|
1302
|
+
};
|
|
1303
|
+
var A;
|
|
1304
|
+
class Ge {
|
|
1305
|
+
constructor(e) {
|
|
1306
|
+
G(this, A);
|
|
1307
|
+
S(this, "createClient", (e) => {
|
|
1308
|
+
const s = {
|
|
1309
|
+
claim: "",
|
|
1310
|
+
value: ""
|
|
1311
|
+
};
|
|
1312
|
+
return e != null && e.email ? (s.claim = "email", s.value = e.email) : e != null && e.phone && (s.claim = "phone", s.value = e.phone), ve({
|
|
1313
|
+
baseUrl: this.options.config.apiUrl,
|
|
1314
|
+
onRequest: ({ request: r }) => {
|
|
1315
|
+
r.headers.set("X-Bot-Token", this.options.config.token), r.headers.set(
|
|
1316
|
+
"X-Consumer-Id",
|
|
1317
|
+
`${s.claim}:${s.value}`
|
|
1318
|
+
), r.headers.set("Content-Type", "application/json"), r.headers.set("Accept", "application/json"), this.options.config.contactToken && r.headers.set(
|
|
1319
|
+
"Authorization",
|
|
1320
|
+
`Bearer ${this.options.config.contactToken}`
|
|
1321
|
+
);
|
|
1322
|
+
}
|
|
1323
|
+
});
|
|
1324
|
+
});
|
|
1325
|
+
S(this, "setUser", (e) => {
|
|
1326
|
+
F(this, A, this.createClient(e));
|
|
1327
|
+
});
|
|
1328
|
+
S(this, "me", async () => await y(this, A).GET("/backend/widget/v2/me"));
|
|
1329
|
+
S(this, "widgetPrelude", async () => await y(this, A).GET("/backend/widget/v2/prelude", {
|
|
1330
|
+
params: { header: { "X-Bot-Token": this.options.config.token } }
|
|
1331
|
+
}));
|
|
1332
|
+
S(this, "handleMessage", async (e, s) => await y(this, A).POST("/backend/widget/v2/chat/send", {
|
|
1333
|
+
body: e,
|
|
1334
|
+
signal: s
|
|
1335
|
+
}));
|
|
1336
|
+
S(this, "getSessionHistory", async (e, s) => {
|
|
1337
|
+
const r = s ? { lastMessageTimestamp: s } : void 0;
|
|
1338
|
+
return await y(this, A).GET(
|
|
1339
|
+
"/backend/widget/v2/session/history/{sessionId}",
|
|
1340
|
+
{ params: { path: { sessionId: e }, query: r } }
|
|
1341
|
+
);
|
|
1342
|
+
});
|
|
1343
|
+
S(this, "createContact", async (e) => await y(this, A).POST("/backend/widget/v2/me/create", {
|
|
1344
|
+
params: { header: { "x-bot-token": this.options.config.token } },
|
|
1345
|
+
body: e
|
|
1346
|
+
}));
|
|
1347
|
+
S(this, "createSession", async () => await y(this, A).POST("/backend/widget/v2/create-session"));
|
|
1348
|
+
S(this, "getSession", async (e) => await y(this, A).GET("/backend/widget/v2/session/{sessionId}", {
|
|
1349
|
+
params: { path: { sessionId: e } }
|
|
1350
|
+
}));
|
|
1351
|
+
S(this, "uploadFile", async ({
|
|
1352
|
+
file: e,
|
|
1353
|
+
abortSignal: s
|
|
1354
|
+
}) => {
|
|
1355
|
+
const r = new FormData();
|
|
1356
|
+
return r.append("file", e.file), await y(this, A).POST("/backend/widget/v2/upload", {
|
|
1357
|
+
params: { query: { fileId: e.id, sessionId: "" } },
|
|
1358
|
+
body: r,
|
|
1359
|
+
signal: s,
|
|
1360
|
+
headers: {
|
|
1361
|
+
"Content-Type": "multipart/form-data"
|
|
1362
|
+
}
|
|
1363
|
+
});
|
|
1364
|
+
});
|
|
1365
|
+
S(this, "vote", async (e) => await y(this, A).POST("/backend/widget/v2/chat/vote", { body: e }));
|
|
1366
|
+
this.options = e, F(this, A, this.createClient(e.config.user));
|
|
1367
|
+
}
|
|
1368
|
+
}
|
|
1369
|
+
A = new WeakMap();
|
|
1370
|
+
export {
|
|
1371
|
+
Ge as A,
|
|
1372
|
+
xe as C,
|
|
1373
|
+
je as E,
|
|
1374
|
+
ke as F,
|
|
1375
|
+
ue as L,
|
|
1376
|
+
N as O,
|
|
1377
|
+
X as P,
|
|
1378
|
+
le as S,
|
|
1379
|
+
Ne as T,
|
|
1380
|
+
ge as a,
|
|
1381
|
+
He as b,
|
|
1382
|
+
ze as c,
|
|
1383
|
+
$e as d,
|
|
1384
|
+
Me as e,
|
|
1385
|
+
Fe as f,
|
|
1386
|
+
ce as g,
|
|
1387
|
+
qe as h,
|
|
1388
|
+
U as i,
|
|
1389
|
+
fe as j,
|
|
1390
|
+
de as k,
|
|
1391
|
+
Y as s
|
|
1392
|
+
};
|
|
1393
|
+
//# sourceMappingURL=api-Cez5T-se.js.map
|