@opencx/widget 3.0.92 → 3.0.94
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/designs.cjs +25 -25
- package/dist/designs.cjs.map +1 -1
- package/dist/designs.js +2344 -2321
- package/dist/designs.js.map +1 -1
- package/dist/index.cjs +1 -1
- package/dist/index.js +1 -1
- package/dist/react.cjs +1 -1
- package/dist/react.js +2 -2
- package/dist/src/headless/core/context/session.ctx.d.ts +4 -1
- package/dist/src/headless/core/types/widget-config.d.ts +17 -0
- package/dist/src/headless/core/utils/run-catching.d.ts +1 -0
- package/dist/{useModes-DeVvS6dM.js → useModes-B5-vdm1q.js} +3 -3
- package/dist/{useModes-DeVvS6dM.js.map → useModes-B5-vdm1q.js.map} +1 -1
- package/dist/{useModes-DR35n7ig.cjs → useModes-BzcZkifa.cjs} +2 -2
- package/dist/{useModes-DR35n7ig.cjs.map → useModes-BzcZkifa.cjs.map} +1 -1
- package/dist/widget.ctx-B-9oCDpY.cjs +5 -0
- package/dist/widget.ctx-B-9oCDpY.cjs.map +1 -0
- package/dist/{widget.ctx-uAWzif3-.js → widget.ctx-CfUvw50X.js} +156 -149
- package/dist/widget.ctx-CfUvw50X.js.map +1 -0
- package/dist-embed/script.js +81 -81
- package/dist-embed/script.js.map +1 -1
- package/package.json +1 -1
- package/dist/widget.ctx-6xcbtF7q.cjs +0 -5
- package/dist/widget.ctx-6xcbtF7q.cjs.map +0 -1
- package/dist/widget.ctx-uAWzif3-.js.map +0 -1
|
@@ -1,21 +1,21 @@
|
|
|
1
1
|
import M from "openapi-fetch";
|
|
2
2
|
import O from "lodash.isequal";
|
|
3
3
|
import { v4 as E } from "uuid";
|
|
4
|
-
const F = (
|
|
5
|
-
console.log(
|
|
6
|
-
}, _ = (
|
|
4
|
+
const F = (d) => {
|
|
5
|
+
console.log(d.error);
|
|
6
|
+
}, _ = (d) => {
|
|
7
7
|
const a = M({
|
|
8
|
-
baseUrl:
|
|
8
|
+
baseUrl: d.baseUrl
|
|
9
9
|
}), o = {
|
|
10
|
-
onRequest:
|
|
11
|
-
onResponse:
|
|
12
|
-
onError:
|
|
10
|
+
onRequest: d.onRequest,
|
|
11
|
+
onResponse: d.onResponse,
|
|
12
|
+
onError: d.onError || F
|
|
13
13
|
};
|
|
14
14
|
return a.use(o), a;
|
|
15
15
|
};
|
|
16
16
|
class T {
|
|
17
17
|
constructor({ config: a }) {
|
|
18
|
-
var
|
|
18
|
+
var g, e;
|
|
19
19
|
this.userToken = null, this.constructClientOptions = (t) => {
|
|
20
20
|
const s = this.config.apiUrl || "https://api.open.cx", i = {
|
|
21
21
|
"X-Bot-Token": this.config.token,
|
|
@@ -30,8 +30,8 @@ class T {
|
|
|
30
30
|
}) => _({
|
|
31
31
|
baseUrl: t,
|
|
32
32
|
onRequest: ({ request: i }) => {
|
|
33
|
-
Object.entries(s).forEach(([
|
|
34
|
-
|
|
33
|
+
Object.entries(s).forEach(([r, n]) => {
|
|
34
|
+
n && i.headers.set(r, n);
|
|
35
35
|
});
|
|
36
36
|
}
|
|
37
37
|
}), this.setAuthToken = (t) => {
|
|
@@ -58,9 +58,9 @@ class T {
|
|
|
58
58
|
lastMessageTimestamp: s,
|
|
59
59
|
abortSignal: i
|
|
60
60
|
}) => {
|
|
61
|
-
const
|
|
61
|
+
const r = s ? { lastMessageTimestamp: s } : void 0;
|
|
62
62
|
return await this.client.GET("/backend/widget/v2/poll/{sessionId}", {
|
|
63
|
-
params: { path: { sessionId: t }, query:
|
|
63
|
+
params: { path: { sessionId: t }, query: r },
|
|
64
64
|
signal: i
|
|
65
65
|
});
|
|
66
66
|
}, this.getSessions = async ({
|
|
@@ -74,49 +74,49 @@ class T {
|
|
|
74
74
|
file: t,
|
|
75
75
|
abortSignal: s,
|
|
76
76
|
onProgress: i
|
|
77
|
-
}) => new Promise((
|
|
77
|
+
}) => new Promise((r, n) => {
|
|
78
78
|
var v;
|
|
79
|
-
const
|
|
80
|
-
|
|
81
|
-
const
|
|
79
|
+
const h = new FormData();
|
|
80
|
+
h.append("file", t);
|
|
81
|
+
const c = new XMLHttpRequest();
|
|
82
82
|
if (s && (s.addEventListener("abort", () => {
|
|
83
|
-
|
|
83
|
+
c.abort(), n(new DOMException("Aborted", "AbortError"));
|
|
84
84
|
}), s.aborted)) {
|
|
85
|
-
|
|
85
|
+
n(new DOMException("Aborted", "AbortError"));
|
|
86
86
|
return;
|
|
87
87
|
}
|
|
88
|
-
|
|
88
|
+
c.upload.addEventListener("progress", (u) => {
|
|
89
89
|
if (u.lengthComputable && i) {
|
|
90
90
|
const S = Math.round(u.loaded / u.total * 100);
|
|
91
91
|
i(S);
|
|
92
92
|
}
|
|
93
|
-
}),
|
|
94
|
-
if (
|
|
93
|
+
}), c.addEventListener("load", () => {
|
|
94
|
+
if (c.status >= 200 && c.status < 300)
|
|
95
95
|
try {
|
|
96
|
-
const u = JSON.parse(
|
|
97
|
-
|
|
96
|
+
const u = JSON.parse(c.responseText);
|
|
97
|
+
r(u);
|
|
98
98
|
} catch (u) {
|
|
99
|
-
|
|
99
|
+
n(new Error(`Failed to parse response: ${u}`));
|
|
100
100
|
}
|
|
101
101
|
else
|
|
102
|
-
|
|
103
|
-
}),
|
|
104
|
-
|
|
105
|
-
}),
|
|
106
|
-
|
|
102
|
+
n(new Error(`Upload failed with status: ${c.status}`));
|
|
103
|
+
}), c.addEventListener("error", () => {
|
|
104
|
+
n(new Error("Network error occurred"));
|
|
105
|
+
}), c.addEventListener("timeout", () => {
|
|
106
|
+
n(new Error("Upload timed out"));
|
|
107
107
|
});
|
|
108
108
|
const { baseUrl: C } = this.constructClientOptions(this.userToken), p = `${C}/backend/widget/v2/upload`;
|
|
109
|
-
|
|
109
|
+
c.open("POST", p), c.setRequestHeader("X-Bot-Token", this.config.token), this.userToken ?? ((v = this.config.user) == null ? void 0 : v.token) ? c.setRequestHeader("Authorization", `Bearer ${this.userToken}`) : console.error("User token not set"), c.send(h);
|
|
110
110
|
}), this.vote = async (t) => await this.client.POST("/backend/widget/v2/chat/vote", { body: t }), this.resolveSession = async (t, s) => await this.client.POST("/backend/widget/v2/session/resolve", {
|
|
111
111
|
body: t,
|
|
112
112
|
signal: s
|
|
113
113
|
}), this.createStateCheckpoint = async (t) => await this.client.POST("/backend/widget/v2/checkpoint", {
|
|
114
114
|
body: t
|
|
115
|
-
}), this.config = a, this.userToken = ((
|
|
116
|
-
const { baseUrl: o, headers:
|
|
115
|
+
}), this.config = a, this.userToken = ((g = a.user) == null ? void 0 : g.token) || null;
|
|
116
|
+
const { baseUrl: o, headers: l } = this.constructClientOptions(
|
|
117
117
|
(e = a.user) == null ? void 0 : e.token
|
|
118
118
|
);
|
|
119
|
-
this.client = this.createOpenAPIClient({ baseUrl: o, headers:
|
|
119
|
+
this.client = this.createOpenAPIClient({ baseUrl: o, headers: l });
|
|
120
120
|
}
|
|
121
121
|
}
|
|
122
122
|
class P {
|
|
@@ -125,14 +125,14 @@ class P {
|
|
|
125
125
|
O(this.state, o) || (this.state = o, this.notifySubscribers(o));
|
|
126
126
|
}, this.setPartial = (o) => {
|
|
127
127
|
if (o == null) return;
|
|
128
|
-
const
|
|
129
|
-
this.set(
|
|
128
|
+
const l = { ...this.state, ...o };
|
|
129
|
+
this.set(l);
|
|
130
130
|
}, this.reset = () => {
|
|
131
131
|
this.set(this.initialState);
|
|
132
132
|
}, this.notifySubscribers = (o) => {
|
|
133
|
-
Array.from(this.subscribers).forEach((
|
|
133
|
+
Array.from(this.subscribers).forEach((g) => {
|
|
134
134
|
try {
|
|
135
|
-
|
|
135
|
+
g(o);
|
|
136
136
|
} catch (e) {
|
|
137
137
|
console.error(e);
|
|
138
138
|
}
|
|
@@ -152,7 +152,7 @@ class R {
|
|
|
152
152
|
this.abortController.abort("Resetting poller"), (a = this.stopPolling) == null || a.call(this), this.stopPolling = null;
|
|
153
153
|
}, this.stopPolling = null, this.startPolling = (a, o) => {
|
|
154
154
|
if (this.stopPolling) return;
|
|
155
|
-
const
|
|
155
|
+
const l = [], g = async () => {
|
|
156
156
|
this.abortController = new AbortController(), this.state.setPartial({ isPolling: !0 });
|
|
157
157
|
try {
|
|
158
158
|
await a(this.abortController.signal);
|
|
@@ -163,17 +163,20 @@ class R {
|
|
|
163
163
|
} finally {
|
|
164
164
|
this.state.setPartial({ isPolling: !1 });
|
|
165
165
|
}
|
|
166
|
-
this.abortController.signal.aborted ? console.log("Poller aborted, not scheduling anymore") :
|
|
166
|
+
this.abortController.signal.aborted ? console.log("Poller aborted, not scheduling anymore") : l.push(setTimeout(g, o));
|
|
167
167
|
};
|
|
168
|
-
|
|
169
|
-
|
|
168
|
+
g(), this.stopPolling = () => {
|
|
169
|
+
l.forEach(clearTimeout), this.state.reset();
|
|
170
170
|
};
|
|
171
171
|
};
|
|
172
172
|
}
|
|
173
173
|
}
|
|
174
|
-
function
|
|
174
|
+
function z(d) {
|
|
175
|
+
return d();
|
|
176
|
+
}
|
|
177
|
+
function D(d) {
|
|
175
178
|
try {
|
|
176
|
-
const a =
|
|
179
|
+
const a = d();
|
|
177
180
|
return a instanceof Promise ? a.then((o) => ({ data: o })).catch((o) => ({ error: o })) : { data: a };
|
|
178
181
|
} catch (a) {
|
|
179
182
|
return { error: a };
|
|
@@ -183,8 +186,8 @@ class L {
|
|
|
183
186
|
constructor({
|
|
184
187
|
api: a,
|
|
185
188
|
config: o,
|
|
186
|
-
sessionCtx:
|
|
187
|
-
messageCtx:
|
|
189
|
+
sessionCtx: l,
|
|
190
|
+
messageCtx: g,
|
|
188
191
|
sessionPollingIntervalSeconds: e
|
|
189
192
|
}) {
|
|
190
193
|
this.poller = new R(), this.registerPolling = () => {
|
|
@@ -194,24 +197,24 @@ class L {
|
|
|
194
197
|
}, this.sessionPollingIntervalSeconds * 1e3) : this.poller.reset();
|
|
195
198
|
});
|
|
196
199
|
}, this.hackAndSlash = async (t, s) => {
|
|
197
|
-
var
|
|
200
|
+
var h;
|
|
198
201
|
this.messageCtx.state.get().messages.length === 0 && this.messageCtx.state.setPartial({ isInitialFetchLoading: !0 });
|
|
199
|
-
const i = this.messageCtx.state.get().messages,
|
|
202
|
+
const i = this.messageCtx.state.get().messages, r = i.length > 0 ? (h = i[i.length - 1]) == null ? void 0 : h.timestamp : void 0, { data: n } = await this.api.pollSessionAndHistory({
|
|
200
203
|
sessionId: t,
|
|
201
204
|
abortSignal: s,
|
|
202
|
-
lastMessageTimestamp:
|
|
205
|
+
lastMessageTimestamp: r
|
|
203
206
|
});
|
|
204
|
-
if (
|
|
205
|
-
const
|
|
206
|
-
(x) => !
|
|
207
|
+
if (n != null && n.session && (this.sessionCtx.sessionState.setPartial({ session: n.session }), this.sessionCtx.setSessions([n.session])), n != null && n.history && n.history.length > 0) {
|
|
208
|
+
const c = this.messageCtx.state.get().messages, C = n.history.map(this.mapHistoryToMessage).filter(
|
|
209
|
+
(x) => !c.some((p) => p.id === x.id)
|
|
207
210
|
);
|
|
208
211
|
this.messageCtx.state.setPartial({
|
|
209
|
-
messages: [...
|
|
212
|
+
messages: [...c, ...C]
|
|
210
213
|
});
|
|
211
214
|
}
|
|
212
215
|
this.messageCtx.state.get().isInitialFetchLoading && this.messageCtx.state.setPartial({ isInitialFetchLoading: !1 });
|
|
213
216
|
}, this.mapHistoryToMessage = (t) => {
|
|
214
|
-
var
|
|
217
|
+
var r, n;
|
|
215
218
|
const s = {
|
|
216
219
|
id: t.publicId,
|
|
217
220
|
timestamp: t.sentAt || "",
|
|
@@ -246,9 +249,9 @@ class L {
|
|
|
246
249
|
component: "bot_message",
|
|
247
250
|
agent: {
|
|
248
251
|
id: null,
|
|
249
|
-
name: ((
|
|
252
|
+
name: ((r = this.config.bot) == null ? void 0 : r.name) || "",
|
|
250
253
|
isAi: !0,
|
|
251
|
-
avatar: ((
|
|
254
|
+
avatar: ((n = this.config.bot) == null ? void 0 : n.avatar) || ""
|
|
252
255
|
},
|
|
253
256
|
data: {
|
|
254
257
|
message: t.content.text || "",
|
|
@@ -262,35 +265,35 @@ class L {
|
|
|
262
265
|
const s = t.result;
|
|
263
266
|
if (s === null || typeof s != "object") return s;
|
|
264
267
|
if ("responseBodyText" in s && typeof s.responseBodyText == "string") {
|
|
265
|
-
const i = s.responseBodyText,
|
|
266
|
-
if (
|
|
268
|
+
const i = s.responseBodyText, r = D(() => JSON.parse(i)).data;
|
|
269
|
+
if (r) return r;
|
|
267
270
|
}
|
|
268
271
|
return t.result;
|
|
269
|
-
}, this.api = a, this.config = o, this.sessionCtx =
|
|
272
|
+
}, this.api = a, this.config = o, this.sessionCtx = l, this.messageCtx = g, this.sessionPollingIntervalSeconds = e, this.registerPolling();
|
|
270
273
|
}
|
|
271
274
|
}
|
|
272
275
|
class B {
|
|
273
276
|
constructor({
|
|
274
277
|
config: a,
|
|
275
278
|
api: o,
|
|
276
|
-
storageCtx:
|
|
279
|
+
storageCtx: l
|
|
277
280
|
}) {
|
|
278
|
-
var
|
|
281
|
+
var g;
|
|
279
282
|
this.shouldCollectData = () => {
|
|
280
283
|
var e;
|
|
281
284
|
return !!(!((e = this.state.get().contact) != null && e.token) && this.config.collectUserData);
|
|
282
285
|
}, this.autoCreateUnverifiedUserIfNotExists = async () => {
|
|
283
|
-
var e, t, s, i,
|
|
286
|
+
var e, t, s, i, r, n, h, c, C, x, p, b, v, u;
|
|
284
287
|
if (!((e = this.config.user) != null && e.token)) {
|
|
285
288
|
if (this.config.collectUserData && !((s = (t = this.config.user) == null ? void 0 : t.data) != null && s.email)) {
|
|
286
289
|
if ((i = this.config.extraDataCollectionFields) != null && i.length)
|
|
287
290
|
return;
|
|
288
|
-
const S = await ((
|
|
291
|
+
const S = await ((r = this.storageCtx) == null ? void 0 : r.getContactToken());
|
|
289
292
|
S && await this.setUnverifiedContact(S);
|
|
290
293
|
return;
|
|
291
294
|
}
|
|
292
|
-
if (!((
|
|
293
|
-
const S = await ((
|
|
295
|
+
if (!((h = (n = this.config.user) == null ? void 0 : n.data) != null && h.email)) {
|
|
296
|
+
const S = await ((c = this.storageCtx) == null ? void 0 : c.getContactToken());
|
|
294
297
|
if (S) {
|
|
295
298
|
await this.setUnverifiedContact(S);
|
|
296
299
|
return;
|
|
@@ -298,7 +301,7 @@ class B {
|
|
|
298
301
|
}
|
|
299
302
|
await this.createUnverifiedContact({
|
|
300
303
|
email: (x = (C = this.config.user) == null ? void 0 : C.data) == null ? void 0 : x.email,
|
|
301
|
-
non_verified_name: ((
|
|
304
|
+
non_verified_name: ((b = (p = this.config.user) == null ? void 0 : p.data) == null ? void 0 : b.name) || "Anonymous",
|
|
302
305
|
non_verified_custom_data: (u = (v = this.config.user) == null ? void 0 : v.data) == null ? void 0 : u.customData
|
|
303
306
|
});
|
|
304
307
|
}
|
|
@@ -315,11 +318,11 @@ class B {
|
|
|
315
318
|
this.state.setPartial({ isCreatingUnverifiedContact: !1 });
|
|
316
319
|
}
|
|
317
320
|
}, this.setUnverifiedContact = async (e) => {
|
|
318
|
-
var i,
|
|
319
|
-
const t = await ((i = this.storageCtx) == null ? void 0 : i.getExternalContactId()), s = ((
|
|
320
|
-
this.api.setAuthToken(e), await ((
|
|
321
|
-
}, this.config = a, this.storageCtx =
|
|
322
|
-
contact: (
|
|
321
|
+
var i, r, n, h;
|
|
322
|
+
const t = await ((i = this.storageCtx) == null ? void 0 : i.getExternalContactId()), s = ((r = this.config.user) == null ? void 0 : r.externalId) || t || E();
|
|
323
|
+
this.api.setAuthToken(e), await ((n = this.storageCtx) == null ? void 0 : n.setContactToken(e)), await ((h = this.storageCtx) == null ? void 0 : h.setExternalContactId(s)), this.state.setPartial({ contact: { token: e, externalId: s } });
|
|
324
|
+
}, this.config = a, this.storageCtx = l, this.api = o, this.state = new P({
|
|
325
|
+
contact: (g = a.user) != null && g.token ? {
|
|
323
326
|
token: a.user.token,
|
|
324
327
|
// Set optional externalId from config... not local storage
|
|
325
328
|
externalId: a.user.externalId
|
|
@@ -335,9 +338,10 @@ function y() {
|
|
|
335
338
|
}
|
|
336
339
|
class q {
|
|
337
340
|
constructor({
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
+
config: a,
|
|
342
|
+
api: o,
|
|
343
|
+
contactCtx: l,
|
|
344
|
+
sessionsPollingIntervalSeconds: g
|
|
341
345
|
}) {
|
|
342
346
|
this.sessionsRefresher = new R(), this.sessionState = new P({
|
|
343
347
|
session: null,
|
|
@@ -355,84 +359,85 @@ class q {
|
|
|
355
359
|
}), this.reset = async () => {
|
|
356
360
|
this.sessionState.reset();
|
|
357
361
|
}, this.registerSessionsRefresherWrapper = () => {
|
|
358
|
-
var
|
|
362
|
+
var e;
|
|
359
363
|
// If the widget config was initially provided with a contact token, no state change would be triggered, so we just fetch
|
|
360
|
-
(
|
|
361
|
-
|
|
364
|
+
(e = this.contactCtx.state.get().contact) != null && e.token && !this.sessionsState.get().didStartInitialFetch ? this.registerSessionsRefresher() : this.contactCtx.state.subscribe(({ contact: t }) => {
|
|
365
|
+
t != null && t.token && !this.sessionsState.get().didStartInitialFetch && this.registerSessionsRefresher();
|
|
362
366
|
});
|
|
363
367
|
}, this.registerSessionsRefresher = () => {
|
|
364
368
|
this.sessionsRefresher.startPolling(async () => {
|
|
365
369
|
this.sessionsState.get().didStartInitialFetch === !1 && this.sessionsState.setPartial({ didStartInitialFetch: !0 }), await this.refreshSessions(), this.sessionsState.get().isInitialFetchLoading === !0 && this.sessionsState.setPartial({ isInitialFetchLoading: !1 });
|
|
366
370
|
}, this.sessionsPollingIntervalSeconds * 1e3);
|
|
367
371
|
}, this.createSession = async () => {
|
|
368
|
-
var
|
|
372
|
+
var r;
|
|
369
373
|
this.sessionState.setPartial({ session: null, isCreatingSession: !0 });
|
|
370
|
-
const
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
+
const e = (r = this.contactCtx.state.get().contact) == null ? void 0 : r.externalId, t = {
|
|
375
|
+
...this.config.sessionCustomData,
|
|
376
|
+
...e ? { external_id: e } : {}
|
|
377
|
+
}, { data: s, error: i } = await this.api.createSession({
|
|
378
|
+
customData: Object.keys(t).length > 0 ? t : void 0
|
|
374
379
|
});
|
|
375
|
-
return
|
|
380
|
+
return s ? (this.sessionState.setPartial({ session: s, isCreatingSession: !1 }), s) : (this.sessionState.setPartial({ isCreatingSession: !1 }), console.error("Failed to create session:", i), null);
|
|
376
381
|
}, this.loadMoreSessions = async () => {
|
|
377
382
|
if (this.sessionsState.get().isLastPage) return;
|
|
378
|
-
const { data:
|
|
383
|
+
const { data: e } = await this.getSessions({
|
|
379
384
|
cursor: this.sessionsState.get().cursor
|
|
380
385
|
});
|
|
381
|
-
if (
|
|
382
|
-
const
|
|
383
|
-
(
|
|
386
|
+
if (e) {
|
|
387
|
+
const s = [...this.sessionsState.get().data, ...e.items].filter(
|
|
388
|
+
(i, r, n) => r === n.findIndex((h) => i.id === h.id)
|
|
384
389
|
);
|
|
385
390
|
this.sessionsState.setPartial({
|
|
386
|
-
data:
|
|
387
|
-
cursor:
|
|
388
|
-
isLastPage:
|
|
391
|
+
data: s,
|
|
392
|
+
cursor: e.next || void 0,
|
|
393
|
+
isLastPage: e.next === null
|
|
389
394
|
});
|
|
390
395
|
}
|
|
391
|
-
}, this.getSessions = async ({ cursor:
|
|
392
|
-
var
|
|
393
|
-
if (!((
|
|
394
|
-
const
|
|
396
|
+
}, this.getSessions = async ({ cursor: e }) => {
|
|
397
|
+
var s, i;
|
|
398
|
+
if (!((s = this.contactCtx.state.get().contact) != null && s.token)) return { data: null };
|
|
399
|
+
const t = (i = this.contactCtx.state.get().contact) == null ? void 0 : i.externalId;
|
|
395
400
|
return await this.api.getSessions({
|
|
396
|
-
cursor:
|
|
397
|
-
filters:
|
|
398
|
-
external_id:
|
|
401
|
+
cursor: e,
|
|
402
|
+
filters: t ? {
|
|
403
|
+
external_id: t
|
|
399
404
|
} : {}
|
|
400
405
|
});
|
|
401
|
-
}, this.setSessions = (
|
|
402
|
-
const
|
|
403
|
-
(
|
|
406
|
+
}, this.setSessions = (e) => {
|
|
407
|
+
const t = [...e, ...this.sessionsState.get().data].filter(
|
|
408
|
+
(s, i, r) => i === r.findIndex((n) => s.id === n.id)
|
|
404
409
|
);
|
|
405
|
-
this.sessionsState.setPartial({ data:
|
|
410
|
+
this.sessionsState.setPartial({ data: t });
|
|
406
411
|
}, this.refreshSessions = async () => {
|
|
407
|
-
const { data:
|
|
408
|
-
|
|
412
|
+
const { data: e } = await this.getSessions({ cursor: void 0 });
|
|
413
|
+
e && this.setSessions(e.items);
|
|
409
414
|
}, this.resolveSession = async () => {
|
|
410
|
-
const
|
|
411
|
-
if (!
|
|
415
|
+
const e = this.sessionState.get().session;
|
|
416
|
+
if (!e || !e.isOpened)
|
|
412
417
|
return { success: !1, error: "Session is not opened" };
|
|
413
418
|
this.sessionState.setPartial({ isResolvingSession: !0 });
|
|
414
|
-
const { data:
|
|
415
|
-
session_id:
|
|
419
|
+
const { data: t, error: s } = await this.api.resolveSession({
|
|
420
|
+
session_id: e.id
|
|
416
421
|
});
|
|
417
|
-
return
|
|
418
|
-
}, this.createStateCheckpoint = async (
|
|
419
|
-
var
|
|
420
|
-
const
|
|
421
|
-
if (!
|
|
422
|
-
const { data:
|
|
423
|
-
session_id:
|
|
424
|
-
payload:
|
|
422
|
+
return t ? (this.sessionState.setPartial({ session: t, isResolvingSession: !1 }), { success: !0, data: t }) : (this.sessionState.setPartial({ isResolvingSession: !1 }), { success: !1, error: s });
|
|
423
|
+
}, this.createStateCheckpoint = async (e) => {
|
|
424
|
+
var r;
|
|
425
|
+
const t = (r = this.sessionState.get().session) == null ? void 0 : r.id;
|
|
426
|
+
if (!t) return;
|
|
427
|
+
const { data: s, error: i } = await this.api.createStateCheckpoint({
|
|
428
|
+
session_id: t,
|
|
429
|
+
payload: e
|
|
425
430
|
});
|
|
426
|
-
return
|
|
427
|
-
}, this.
|
|
431
|
+
return s ? { data: s } : { success: !1, error: i };
|
|
432
|
+
}, this.config = a, this.api = o, this.contactCtx = l, this.sessionsPollingIntervalSeconds = g, this.registerSessionsRefresherWrapper();
|
|
428
433
|
}
|
|
429
434
|
}
|
|
430
435
|
class $ {
|
|
431
436
|
constructor({
|
|
432
437
|
config: a,
|
|
433
438
|
api: o,
|
|
434
|
-
sessionCtx:
|
|
435
|
-
contactCtx:
|
|
439
|
+
sessionCtx: l,
|
|
440
|
+
contactCtx: g
|
|
436
441
|
}) {
|
|
437
442
|
this.state = new P({
|
|
438
443
|
messages: [],
|
|
@@ -442,14 +447,14 @@ class $ {
|
|
|
442
447
|
}), this.sendMessageAbortController = new AbortController(), this.reset = () => {
|
|
443
448
|
this.sendMessageAbortController.abort("Resetting chat"), this.state.reset();
|
|
444
449
|
}, this.sendMessage = async (e) => {
|
|
445
|
-
var
|
|
450
|
+
var n, h, c, C, x, p, b, v;
|
|
446
451
|
if (!e.content.trim() && (!e.attachments || e.attachments.length === 0)) {
|
|
447
452
|
console.warn("Cannot send an empty message of no content or attachments");
|
|
448
453
|
return;
|
|
449
454
|
}
|
|
450
|
-
const t = this.state.get().isSendingMessage, s = ((
|
|
455
|
+
const t = this.state.get().isSendingMessage, s = ((n = this.sessionCtx.sessionState.get().session) == null ? void 0 : n.assignee.kind) === "ai", i = this.state.get().messages, r = i.length > 0 ? i[i.length - 1] : void 0;
|
|
451
456
|
if (s && t || // If last message is from user, then bot response did not arrive yet
|
|
452
|
-
s && (
|
|
457
|
+
s && (r == null ? void 0 : r.type) === "FROM_USER") {
|
|
453
458
|
console.warn("Cannot send messages while awaiting AI response");
|
|
454
459
|
return;
|
|
455
460
|
}
|
|
@@ -462,14 +467,14 @@ class $ {
|
|
|
462
467
|
), S = this.state.get().messages;
|
|
463
468
|
if (this.state.setPartial({
|
|
464
469
|
messages: [...S, u]
|
|
465
|
-
}), !((
|
|
470
|
+
}), !((h = this.sessionCtx.sessionState.get().session) != null && h.id)) {
|
|
466
471
|
if (!await this.sessionCtx.createSession()) {
|
|
467
472
|
console.error("Failed to create session");
|
|
468
473
|
return;
|
|
469
474
|
}
|
|
470
475
|
this.sessionCtx.refreshSessions();
|
|
471
476
|
}
|
|
472
|
-
const k = (
|
|
477
|
+
const k = (c = this.sessionCtx.sessionState.get().session) == null ? void 0 : c.id;
|
|
473
478
|
if (!k) return;
|
|
474
479
|
const { data: f } = await this.api.sendMessage(
|
|
475
480
|
{
|
|
@@ -492,11 +497,11 @@ class $ {
|
|
|
492
497
|
this.sendMessageAbortController.signal
|
|
493
498
|
);
|
|
494
499
|
if (f != null && f.success) {
|
|
495
|
-
const
|
|
496
|
-
if (
|
|
500
|
+
const w = this.toBotMessage(f);
|
|
501
|
+
if (w) {
|
|
497
502
|
const I = this.state.get().messages;
|
|
498
503
|
if (!!I.some(
|
|
499
|
-
(A) => A.id ===
|
|
504
|
+
(A) => A.id === w.id
|
|
500
505
|
)) {
|
|
501
506
|
this.state.setPartial({
|
|
502
507
|
lastAIResMightSolveUserIssue: ((C = f.autopilotResponse) == null ? void 0 : C.mightSolveUserIssue) || ((x = f.uiResponse) == null ? void 0 : x.mightSolveUserIssue)
|
|
@@ -504,17 +509,17 @@ class $ {
|
|
|
504
509
|
return;
|
|
505
510
|
}
|
|
506
511
|
this.state.setPartial({
|
|
507
|
-
messages: [...I,
|
|
508
|
-
lastAIResMightSolveUserIssue: ((p = f.autopilotResponse) == null ? void 0 : p.mightSolveUserIssue) || ((
|
|
512
|
+
messages: [...I, w],
|
|
513
|
+
lastAIResMightSolveUserIssue: ((p = f.autopilotResponse) == null ? void 0 : p.mightSolveUserIssue) || ((b = f.uiResponse) == null ? void 0 : b.mightSolveUserIssue)
|
|
509
514
|
});
|
|
510
515
|
}
|
|
511
516
|
f.session && this.sessionCtx.sessionState.setPartial({ session: f.session });
|
|
512
517
|
} else {
|
|
513
|
-
const
|
|
518
|
+
const w = this.toBotErrorMessage(
|
|
514
519
|
((v = f == null ? void 0 : f.error) == null ? void 0 : v.message) || "Unknown error occurred"
|
|
515
520
|
), I = this.state.get().messages;
|
|
516
521
|
this.state.setPartial({
|
|
517
|
-
messages: [...I,
|
|
522
|
+
messages: [...I, w]
|
|
518
523
|
});
|
|
519
524
|
}
|
|
520
525
|
} catch (u) {
|
|
@@ -525,7 +530,7 @@ class $ {
|
|
|
525
530
|
}, this.toUserMessage = (e, t) => {
|
|
526
531
|
const s = (() => {
|
|
527
532
|
const i = this.contactCtx.state.get().extraCollectedData;
|
|
528
|
-
return this.state.get().messages.length === 0 && i && Object.keys(i).length > 0 ? `${Object.entries(i).filter(([
|
|
533
|
+
return this.state.get().messages.length === 0 && i && Object.keys(i).length > 0 ? `${Object.entries(i).filter(([n, h]) => !!h).map(([n, h]) => `${n}: ${h}`).join(`
|
|
529
534
|
`)}
|
|
530
535
|
|
|
531
536
|
${e}` : e;
|
|
@@ -569,15 +574,15 @@ ${e}` : e;
|
|
|
569
574
|
variant: "error",
|
|
570
575
|
action: void 0
|
|
571
576
|
}
|
|
572
|
-
}), this.config = a, this.api = o, this.sessionCtx =
|
|
577
|
+
}), this.config = a, this.api = o, this.sessionCtx = l, this.contactCtx = g;
|
|
573
578
|
}
|
|
574
579
|
}
|
|
575
580
|
class N {
|
|
576
581
|
constructor({
|
|
577
582
|
config: a,
|
|
578
583
|
contactCtx: o,
|
|
579
|
-
sessionCtx:
|
|
580
|
-
resetChat:
|
|
584
|
+
sessionCtx: l,
|
|
585
|
+
resetChat: g
|
|
581
586
|
}) {
|
|
582
587
|
var e;
|
|
583
588
|
this.registerRoutingListener = () => {
|
|
@@ -588,14 +593,14 @@ class N {
|
|
|
588
593
|
});
|
|
589
594
|
}), this.sessionCtx.sessionsState.subscribe(
|
|
590
595
|
({ isInitialFetchLoading: t, data: s }) => {
|
|
591
|
-
var i,
|
|
596
|
+
var i, r, n, h;
|
|
592
597
|
if ((i = this.config.router) != null && i.chatScreenOnly && // Do not route to a chat if we are currently inside one already
|
|
593
598
|
// This also applies to newly created sessions; the new session will be in `sessionState` before it is refreshed and included in `sessionsState`
|
|
594
|
-
!((
|
|
595
|
-
const
|
|
596
|
-
return
|
|
599
|
+
!((r = this.sessionCtx.sessionState.get().session) != null && r.id)) {
|
|
600
|
+
const c = (n = s.find((C) => C.isOpened)) == null ? void 0 : n.id;
|
|
601
|
+
return c ? this.toChatScreen(c) : void 0;
|
|
597
602
|
}
|
|
598
|
-
s.length || ((
|
|
603
|
+
s.length || ((h = this.config.router) == null ? void 0 : h.goToChatIfNoSessions) !== !1 && !t && this.state.get().screen !== "chat" && this.toChatScreen();
|
|
599
604
|
}
|
|
600
605
|
);
|
|
601
606
|
}, this.toSessionsScreen = () => {
|
|
@@ -607,7 +612,7 @@ class N {
|
|
|
607
612
|
this.sessionCtx.sessionState.setPartial({ session: s });
|
|
608
613
|
}
|
|
609
614
|
this.state.setPartial({ screen: "chat" });
|
|
610
|
-
}, this.config = a, this.contactCtx = o, this.sessionCtx =
|
|
615
|
+
}, this.config = a, this.contactCtx = o, this.sessionCtx = l, this.resetChat = g, this.state = new P({
|
|
611
616
|
screen: this.contactCtx.shouldCollectData() ? "welcome" : (e = this.config.router) != null && e.chatScreenOnly ? "chat" : "sessions"
|
|
612
617
|
}), this.registerRoutingListener();
|
|
613
618
|
}
|
|
@@ -628,7 +633,7 @@ const m = class m {
|
|
|
628
633
|
constructor({
|
|
629
634
|
config: a,
|
|
630
635
|
storage: o,
|
|
631
|
-
modes:
|
|
636
|
+
modes: l
|
|
632
637
|
}) {
|
|
633
638
|
if (this.modes = [], this.resetChat = () => {
|
|
634
639
|
this.sessionCtx.reset(), this.messageCtx.reset();
|
|
@@ -636,11 +641,12 @@ const m = class m {
|
|
|
636
641
|
throw Error(
|
|
637
642
|
"Widget polling values are not defined, did you call WidgetCtx.initialize()"
|
|
638
643
|
);
|
|
639
|
-
this.config = a, this.api = new T({ config: a }), this.storageCtx = o ? new H({ storage: o }) : void 0, this.modes =
|
|
644
|
+
this.config = a, this.api = new T({ config: a }), this.storageCtx = o ? new H({ storage: o }) : void 0, this.modes = l, this.contactCtx = new B({
|
|
640
645
|
api: this.api,
|
|
641
646
|
config: this.config,
|
|
642
647
|
storageCtx: this.storageCtx
|
|
643
648
|
}), this.sessionCtx = new q({
|
|
649
|
+
config: this.config,
|
|
644
650
|
api: this.api,
|
|
645
651
|
contactCtx: this.contactCtx,
|
|
646
652
|
sessionsPollingIntervalSeconds: m.pollingIntervalsSeconds.sessions
|
|
@@ -667,22 +673,23 @@ m.pollingIntervalsSeconds = null, m.initialize = async ({
|
|
|
667
673
|
config: a,
|
|
668
674
|
storage: o
|
|
669
675
|
}) => {
|
|
670
|
-
var
|
|
671
|
-
const
|
|
676
|
+
var g, e, t;
|
|
677
|
+
const l = await new T({
|
|
672
678
|
config: a
|
|
673
679
|
}).getExternalWidgetConfig();
|
|
674
680
|
return m.pollingIntervalsSeconds = {
|
|
675
|
-
session: ((
|
|
676
|
-
sessions: ((e =
|
|
681
|
+
session: ((g = l.data) == null ? void 0 : g.sessionPollingIntervalSeconds) || 10,
|
|
682
|
+
sessions: ((e = l.data) == null ? void 0 : e.sessionsPollingIntervalSeconds) || 60
|
|
677
683
|
}, new m({
|
|
678
684
|
config: a,
|
|
679
685
|
storage: o,
|
|
680
|
-
modes: ((t =
|
|
686
|
+
modes: ((t = l.data) == null ? void 0 : t.modes) || []
|
|
681
687
|
});
|
|
682
688
|
};
|
|
683
689
|
let U = m;
|
|
684
690
|
export {
|
|
685
691
|
P,
|
|
686
|
-
U as W
|
|
692
|
+
U as W,
|
|
693
|
+
z as r
|
|
687
694
|
};
|
|
688
|
-
//# sourceMappingURL=widget.ctx-
|
|
695
|
+
//# sourceMappingURL=widget.ctx-CfUvw50X.js.map
|