@opencx/widget 3.0.93 → 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 +1 -1
- package/dist/designs.js +2 -2
- 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 +6 -0
- package/dist/{useModes-BAsZP8Lq.js → useModes-B5-vdm1q.js} +3 -3
- package/dist/{useModes-BAsZP8Lq.js.map → useModes-B5-vdm1q.js.map} +1 -1
- package/dist/{useModes-C_mX92ur.cjs → useModes-BzcZkifa.cjs} +2 -2
- package/dist/{useModes-C_mX92ur.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-Djrb64eX.js → widget.ctx-CfUvw50X.js} +153 -150
- package/dist/widget.ctx-CfUvw50X.js.map +1 -0
- package/dist-embed/script.js +3 -3
- package/dist-embed/script.js.map +1 -1
- package/package.json +1 -1
- package/dist/widget.ctx-CxQoP5qn.cjs +0 -5
- package/dist/widget.ctx-CxQoP5qn.cjs.map +0 -1
- package/dist/widget.ctx-Djrb64eX.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,20 +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 z(
|
|
175
|
-
return
|
|
174
|
+
function z(d) {
|
|
175
|
+
return d();
|
|
176
176
|
}
|
|
177
|
-
function D(
|
|
177
|
+
function D(d) {
|
|
178
178
|
try {
|
|
179
|
-
const a =
|
|
179
|
+
const a = d();
|
|
180
180
|
return a instanceof Promise ? a.then((o) => ({ data: o })).catch((o) => ({ error: o })) : { data: a };
|
|
181
181
|
} catch (a) {
|
|
182
182
|
return { error: a };
|
|
@@ -186,8 +186,8 @@ class L {
|
|
|
186
186
|
constructor({
|
|
187
187
|
api: a,
|
|
188
188
|
config: o,
|
|
189
|
-
sessionCtx:
|
|
190
|
-
messageCtx:
|
|
189
|
+
sessionCtx: l,
|
|
190
|
+
messageCtx: g,
|
|
191
191
|
sessionPollingIntervalSeconds: e
|
|
192
192
|
}) {
|
|
193
193
|
this.poller = new R(), this.registerPolling = () => {
|
|
@@ -197,24 +197,24 @@ class L {
|
|
|
197
197
|
}, this.sessionPollingIntervalSeconds * 1e3) : this.poller.reset();
|
|
198
198
|
});
|
|
199
199
|
}, this.hackAndSlash = async (t, s) => {
|
|
200
|
-
var
|
|
200
|
+
var h;
|
|
201
201
|
this.messageCtx.state.get().messages.length === 0 && this.messageCtx.state.setPartial({ isInitialFetchLoading: !0 });
|
|
202
|
-
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({
|
|
203
203
|
sessionId: t,
|
|
204
204
|
abortSignal: s,
|
|
205
|
-
lastMessageTimestamp:
|
|
205
|
+
lastMessageTimestamp: r
|
|
206
206
|
});
|
|
207
|
-
if (
|
|
208
|
-
const
|
|
209
|
-
(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)
|
|
210
210
|
);
|
|
211
211
|
this.messageCtx.state.setPartial({
|
|
212
|
-
messages: [...
|
|
212
|
+
messages: [...c, ...C]
|
|
213
213
|
});
|
|
214
214
|
}
|
|
215
215
|
this.messageCtx.state.get().isInitialFetchLoading && this.messageCtx.state.setPartial({ isInitialFetchLoading: !1 });
|
|
216
216
|
}, this.mapHistoryToMessage = (t) => {
|
|
217
|
-
var
|
|
217
|
+
var r, n;
|
|
218
218
|
const s = {
|
|
219
219
|
id: t.publicId,
|
|
220
220
|
timestamp: t.sentAt || "",
|
|
@@ -249,9 +249,9 @@ class L {
|
|
|
249
249
|
component: "bot_message",
|
|
250
250
|
agent: {
|
|
251
251
|
id: null,
|
|
252
|
-
name: ((
|
|
252
|
+
name: ((r = this.config.bot) == null ? void 0 : r.name) || "",
|
|
253
253
|
isAi: !0,
|
|
254
|
-
avatar: ((
|
|
254
|
+
avatar: ((n = this.config.bot) == null ? void 0 : n.avatar) || ""
|
|
255
255
|
},
|
|
256
256
|
data: {
|
|
257
257
|
message: t.content.text || "",
|
|
@@ -265,35 +265,35 @@ class L {
|
|
|
265
265
|
const s = t.result;
|
|
266
266
|
if (s === null || typeof s != "object") return s;
|
|
267
267
|
if ("responseBodyText" in s && typeof s.responseBodyText == "string") {
|
|
268
|
-
const i = s.responseBodyText,
|
|
269
|
-
if (
|
|
268
|
+
const i = s.responseBodyText, r = D(() => JSON.parse(i)).data;
|
|
269
|
+
if (r) return r;
|
|
270
270
|
}
|
|
271
271
|
return t.result;
|
|
272
|
-
}, 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();
|
|
273
273
|
}
|
|
274
274
|
}
|
|
275
275
|
class B {
|
|
276
276
|
constructor({
|
|
277
277
|
config: a,
|
|
278
278
|
api: o,
|
|
279
|
-
storageCtx:
|
|
279
|
+
storageCtx: l
|
|
280
280
|
}) {
|
|
281
|
-
var
|
|
281
|
+
var g;
|
|
282
282
|
this.shouldCollectData = () => {
|
|
283
283
|
var e;
|
|
284
284
|
return !!(!((e = this.state.get().contact) != null && e.token) && this.config.collectUserData);
|
|
285
285
|
}, this.autoCreateUnverifiedUserIfNotExists = async () => {
|
|
286
|
-
var e, t, s, i,
|
|
286
|
+
var e, t, s, i, r, n, h, c, C, x, p, b, v, u;
|
|
287
287
|
if (!((e = this.config.user) != null && e.token)) {
|
|
288
288
|
if (this.config.collectUserData && !((s = (t = this.config.user) == null ? void 0 : t.data) != null && s.email)) {
|
|
289
289
|
if ((i = this.config.extraDataCollectionFields) != null && i.length)
|
|
290
290
|
return;
|
|
291
|
-
const S = await ((
|
|
291
|
+
const S = await ((r = this.storageCtx) == null ? void 0 : r.getContactToken());
|
|
292
292
|
S && await this.setUnverifiedContact(S);
|
|
293
293
|
return;
|
|
294
294
|
}
|
|
295
|
-
if (!((
|
|
296
|
-
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());
|
|
297
297
|
if (S) {
|
|
298
298
|
await this.setUnverifiedContact(S);
|
|
299
299
|
return;
|
|
@@ -301,7 +301,7 @@ class B {
|
|
|
301
301
|
}
|
|
302
302
|
await this.createUnverifiedContact({
|
|
303
303
|
email: (x = (C = this.config.user) == null ? void 0 : C.data) == null ? void 0 : x.email,
|
|
304
|
-
non_verified_name: ((
|
|
304
|
+
non_verified_name: ((b = (p = this.config.user) == null ? void 0 : p.data) == null ? void 0 : b.name) || "Anonymous",
|
|
305
305
|
non_verified_custom_data: (u = (v = this.config.user) == null ? void 0 : v.data) == null ? void 0 : u.customData
|
|
306
306
|
});
|
|
307
307
|
}
|
|
@@ -318,11 +318,11 @@ class B {
|
|
|
318
318
|
this.state.setPartial({ isCreatingUnverifiedContact: !1 });
|
|
319
319
|
}
|
|
320
320
|
}, this.setUnverifiedContact = async (e) => {
|
|
321
|
-
var i,
|
|
322
|
-
const t = await ((i = this.storageCtx) == null ? void 0 : i.getExternalContactId()), s = ((
|
|
323
|
-
this.api.setAuthToken(e), await ((
|
|
324
|
-
}, this.config = a, this.storageCtx =
|
|
325
|
-
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 ? {
|
|
326
326
|
token: a.user.token,
|
|
327
327
|
// Set optional externalId from config... not local storage
|
|
328
328
|
externalId: a.user.externalId
|
|
@@ -338,9 +338,10 @@ function y() {
|
|
|
338
338
|
}
|
|
339
339
|
class q {
|
|
340
340
|
constructor({
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
341
|
+
config: a,
|
|
342
|
+
api: o,
|
|
343
|
+
contactCtx: l,
|
|
344
|
+
sessionsPollingIntervalSeconds: g
|
|
344
345
|
}) {
|
|
345
346
|
this.sessionsRefresher = new R(), this.sessionState = new P({
|
|
346
347
|
session: null,
|
|
@@ -358,84 +359,85 @@ class q {
|
|
|
358
359
|
}), this.reset = async () => {
|
|
359
360
|
this.sessionState.reset();
|
|
360
361
|
}, this.registerSessionsRefresherWrapper = () => {
|
|
361
|
-
var
|
|
362
|
+
var e;
|
|
362
363
|
// If the widget config was initially provided with a contact token, no state change would be triggered, so we just fetch
|
|
363
|
-
(
|
|
364
|
-
|
|
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();
|
|
365
366
|
});
|
|
366
367
|
}, this.registerSessionsRefresher = () => {
|
|
367
368
|
this.sessionsRefresher.startPolling(async () => {
|
|
368
369
|
this.sessionsState.get().didStartInitialFetch === !1 && this.sessionsState.setPartial({ didStartInitialFetch: !0 }), await this.refreshSessions(), this.sessionsState.get().isInitialFetchLoading === !0 && this.sessionsState.setPartial({ isInitialFetchLoading: !1 });
|
|
369
370
|
}, this.sessionsPollingIntervalSeconds * 1e3);
|
|
370
371
|
}, this.createSession = async () => {
|
|
371
|
-
var
|
|
372
|
+
var r;
|
|
372
373
|
this.sessionState.setPartial({ session: null, isCreatingSession: !0 });
|
|
373
|
-
const
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
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
|
|
377
379
|
});
|
|
378
|
-
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);
|
|
379
381
|
}, this.loadMoreSessions = async () => {
|
|
380
382
|
if (this.sessionsState.get().isLastPage) return;
|
|
381
|
-
const { data:
|
|
383
|
+
const { data: e } = await this.getSessions({
|
|
382
384
|
cursor: this.sessionsState.get().cursor
|
|
383
385
|
});
|
|
384
|
-
if (
|
|
385
|
-
const
|
|
386
|
-
(
|
|
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)
|
|
387
389
|
);
|
|
388
390
|
this.sessionsState.setPartial({
|
|
389
|
-
data:
|
|
390
|
-
cursor:
|
|
391
|
-
isLastPage:
|
|
391
|
+
data: s,
|
|
392
|
+
cursor: e.next || void 0,
|
|
393
|
+
isLastPage: e.next === null
|
|
392
394
|
});
|
|
393
395
|
}
|
|
394
|
-
}, this.getSessions = async ({ cursor:
|
|
395
|
-
var
|
|
396
|
-
if (!((
|
|
397
|
-
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;
|
|
398
400
|
return await this.api.getSessions({
|
|
399
|
-
cursor:
|
|
400
|
-
filters:
|
|
401
|
-
external_id:
|
|
401
|
+
cursor: e,
|
|
402
|
+
filters: t ? {
|
|
403
|
+
external_id: t
|
|
402
404
|
} : {}
|
|
403
405
|
});
|
|
404
|
-
}, this.setSessions = (
|
|
405
|
-
const
|
|
406
|
-
(
|
|
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)
|
|
407
409
|
);
|
|
408
|
-
this.sessionsState.setPartial({ data:
|
|
410
|
+
this.sessionsState.setPartial({ data: t });
|
|
409
411
|
}, this.refreshSessions = async () => {
|
|
410
|
-
const { data:
|
|
411
|
-
|
|
412
|
+
const { data: e } = await this.getSessions({ cursor: void 0 });
|
|
413
|
+
e && this.setSessions(e.items);
|
|
412
414
|
}, this.resolveSession = async () => {
|
|
413
|
-
const
|
|
414
|
-
if (!
|
|
415
|
+
const e = this.sessionState.get().session;
|
|
416
|
+
if (!e || !e.isOpened)
|
|
415
417
|
return { success: !1, error: "Session is not opened" };
|
|
416
418
|
this.sessionState.setPartial({ isResolvingSession: !0 });
|
|
417
|
-
const { data:
|
|
418
|
-
session_id:
|
|
419
|
+
const { data: t, error: s } = await this.api.resolveSession({
|
|
420
|
+
session_id: e.id
|
|
419
421
|
});
|
|
420
|
-
return
|
|
421
|
-
}, this.createStateCheckpoint = async (
|
|
422
|
-
var
|
|
423
|
-
const
|
|
424
|
-
if (!
|
|
425
|
-
const { data:
|
|
426
|
-
session_id:
|
|
427
|
-
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
|
|
428
430
|
});
|
|
429
|
-
return
|
|
430
|
-
}, 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();
|
|
431
433
|
}
|
|
432
434
|
}
|
|
433
435
|
class $ {
|
|
434
436
|
constructor({
|
|
435
437
|
config: a,
|
|
436
438
|
api: o,
|
|
437
|
-
sessionCtx:
|
|
438
|
-
contactCtx:
|
|
439
|
+
sessionCtx: l,
|
|
440
|
+
contactCtx: g
|
|
439
441
|
}) {
|
|
440
442
|
this.state = new P({
|
|
441
443
|
messages: [],
|
|
@@ -445,14 +447,14 @@ class $ {
|
|
|
445
447
|
}), this.sendMessageAbortController = new AbortController(), this.reset = () => {
|
|
446
448
|
this.sendMessageAbortController.abort("Resetting chat"), this.state.reset();
|
|
447
449
|
}, this.sendMessage = async (e) => {
|
|
448
|
-
var
|
|
450
|
+
var n, h, c, C, x, p, b, v;
|
|
449
451
|
if (!e.content.trim() && (!e.attachments || e.attachments.length === 0)) {
|
|
450
452
|
console.warn("Cannot send an empty message of no content or attachments");
|
|
451
453
|
return;
|
|
452
454
|
}
|
|
453
|
-
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;
|
|
454
456
|
if (s && t || // If last message is from user, then bot response did not arrive yet
|
|
455
|
-
s && (
|
|
457
|
+
s && (r == null ? void 0 : r.type) === "FROM_USER") {
|
|
456
458
|
console.warn("Cannot send messages while awaiting AI response");
|
|
457
459
|
return;
|
|
458
460
|
}
|
|
@@ -465,14 +467,14 @@ class $ {
|
|
|
465
467
|
), S = this.state.get().messages;
|
|
466
468
|
if (this.state.setPartial({
|
|
467
469
|
messages: [...S, u]
|
|
468
|
-
}), !((
|
|
470
|
+
}), !((h = this.sessionCtx.sessionState.get().session) != null && h.id)) {
|
|
469
471
|
if (!await this.sessionCtx.createSession()) {
|
|
470
472
|
console.error("Failed to create session");
|
|
471
473
|
return;
|
|
472
474
|
}
|
|
473
475
|
this.sessionCtx.refreshSessions();
|
|
474
476
|
}
|
|
475
|
-
const k = (
|
|
477
|
+
const k = (c = this.sessionCtx.sessionState.get().session) == null ? void 0 : c.id;
|
|
476
478
|
if (!k) return;
|
|
477
479
|
const { data: f } = await this.api.sendMessage(
|
|
478
480
|
{
|
|
@@ -495,11 +497,11 @@ class $ {
|
|
|
495
497
|
this.sendMessageAbortController.signal
|
|
496
498
|
);
|
|
497
499
|
if (f != null && f.success) {
|
|
498
|
-
const
|
|
499
|
-
if (
|
|
500
|
+
const w = this.toBotMessage(f);
|
|
501
|
+
if (w) {
|
|
500
502
|
const I = this.state.get().messages;
|
|
501
503
|
if (!!I.some(
|
|
502
|
-
(A) => A.id ===
|
|
504
|
+
(A) => A.id === w.id
|
|
503
505
|
)) {
|
|
504
506
|
this.state.setPartial({
|
|
505
507
|
lastAIResMightSolveUserIssue: ((C = f.autopilotResponse) == null ? void 0 : C.mightSolveUserIssue) || ((x = f.uiResponse) == null ? void 0 : x.mightSolveUserIssue)
|
|
@@ -507,17 +509,17 @@ class $ {
|
|
|
507
509
|
return;
|
|
508
510
|
}
|
|
509
511
|
this.state.setPartial({
|
|
510
|
-
messages: [...I,
|
|
511
|
-
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)
|
|
512
514
|
});
|
|
513
515
|
}
|
|
514
516
|
f.session && this.sessionCtx.sessionState.setPartial({ session: f.session });
|
|
515
517
|
} else {
|
|
516
|
-
const
|
|
518
|
+
const w = this.toBotErrorMessage(
|
|
517
519
|
((v = f == null ? void 0 : f.error) == null ? void 0 : v.message) || "Unknown error occurred"
|
|
518
520
|
), I = this.state.get().messages;
|
|
519
521
|
this.state.setPartial({
|
|
520
|
-
messages: [...I,
|
|
522
|
+
messages: [...I, w]
|
|
521
523
|
});
|
|
522
524
|
}
|
|
523
525
|
} catch (u) {
|
|
@@ -528,7 +530,7 @@ class $ {
|
|
|
528
530
|
}, this.toUserMessage = (e, t) => {
|
|
529
531
|
const s = (() => {
|
|
530
532
|
const i = this.contactCtx.state.get().extraCollectedData;
|
|
531
|
-
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(`
|
|
532
534
|
`)}
|
|
533
535
|
|
|
534
536
|
${e}` : e;
|
|
@@ -572,15 +574,15 @@ ${e}` : e;
|
|
|
572
574
|
variant: "error",
|
|
573
575
|
action: void 0
|
|
574
576
|
}
|
|
575
|
-
}), this.config = a, this.api = o, this.sessionCtx =
|
|
577
|
+
}), this.config = a, this.api = o, this.sessionCtx = l, this.contactCtx = g;
|
|
576
578
|
}
|
|
577
579
|
}
|
|
578
580
|
class N {
|
|
579
581
|
constructor({
|
|
580
582
|
config: a,
|
|
581
583
|
contactCtx: o,
|
|
582
|
-
sessionCtx:
|
|
583
|
-
resetChat:
|
|
584
|
+
sessionCtx: l,
|
|
585
|
+
resetChat: g
|
|
584
586
|
}) {
|
|
585
587
|
var e;
|
|
586
588
|
this.registerRoutingListener = () => {
|
|
@@ -591,14 +593,14 @@ class N {
|
|
|
591
593
|
});
|
|
592
594
|
}), this.sessionCtx.sessionsState.subscribe(
|
|
593
595
|
({ isInitialFetchLoading: t, data: s }) => {
|
|
594
|
-
var i,
|
|
596
|
+
var i, r, n, h;
|
|
595
597
|
if ((i = this.config.router) != null && i.chatScreenOnly && // Do not route to a chat if we are currently inside one already
|
|
596
598
|
// This also applies to newly created sessions; the new session will be in `sessionState` before it is refreshed and included in `sessionsState`
|
|
597
|
-
!((
|
|
598
|
-
const
|
|
599
|
-
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;
|
|
600
602
|
}
|
|
601
|
-
s.length || ((
|
|
603
|
+
s.length || ((h = this.config.router) == null ? void 0 : h.goToChatIfNoSessions) !== !1 && !t && this.state.get().screen !== "chat" && this.toChatScreen();
|
|
602
604
|
}
|
|
603
605
|
);
|
|
604
606
|
}, this.toSessionsScreen = () => {
|
|
@@ -610,7 +612,7 @@ class N {
|
|
|
610
612
|
this.sessionCtx.sessionState.setPartial({ session: s });
|
|
611
613
|
}
|
|
612
614
|
this.state.setPartial({ screen: "chat" });
|
|
613
|
-
}, 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({
|
|
614
616
|
screen: this.contactCtx.shouldCollectData() ? "welcome" : (e = this.config.router) != null && e.chatScreenOnly ? "chat" : "sessions"
|
|
615
617
|
}), this.registerRoutingListener();
|
|
616
618
|
}
|
|
@@ -631,7 +633,7 @@ const m = class m {
|
|
|
631
633
|
constructor({
|
|
632
634
|
config: a,
|
|
633
635
|
storage: o,
|
|
634
|
-
modes:
|
|
636
|
+
modes: l
|
|
635
637
|
}) {
|
|
636
638
|
if (this.modes = [], this.resetChat = () => {
|
|
637
639
|
this.sessionCtx.reset(), this.messageCtx.reset();
|
|
@@ -639,11 +641,12 @@ const m = class m {
|
|
|
639
641
|
throw Error(
|
|
640
642
|
"Widget polling values are not defined, did you call WidgetCtx.initialize()"
|
|
641
643
|
);
|
|
642
|
-
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({
|
|
643
645
|
api: this.api,
|
|
644
646
|
config: this.config,
|
|
645
647
|
storageCtx: this.storageCtx
|
|
646
648
|
}), this.sessionCtx = new q({
|
|
649
|
+
config: this.config,
|
|
647
650
|
api: this.api,
|
|
648
651
|
contactCtx: this.contactCtx,
|
|
649
652
|
sessionsPollingIntervalSeconds: m.pollingIntervalsSeconds.sessions
|
|
@@ -670,17 +673,17 @@ m.pollingIntervalsSeconds = null, m.initialize = async ({
|
|
|
670
673
|
config: a,
|
|
671
674
|
storage: o
|
|
672
675
|
}) => {
|
|
673
|
-
var
|
|
674
|
-
const
|
|
676
|
+
var g, e, t;
|
|
677
|
+
const l = await new T({
|
|
675
678
|
config: a
|
|
676
679
|
}).getExternalWidgetConfig();
|
|
677
680
|
return m.pollingIntervalsSeconds = {
|
|
678
|
-
session: ((
|
|
679
|
-
sessions: ((e =
|
|
681
|
+
session: ((g = l.data) == null ? void 0 : g.sessionPollingIntervalSeconds) || 10,
|
|
682
|
+
sessions: ((e = l.data) == null ? void 0 : e.sessionsPollingIntervalSeconds) || 60
|
|
680
683
|
}, new m({
|
|
681
684
|
config: a,
|
|
682
685
|
storage: o,
|
|
683
|
-
modes: ((t =
|
|
686
|
+
modes: ((t = l.data) == null ? void 0 : t.modes) || []
|
|
684
687
|
});
|
|
685
688
|
};
|
|
686
689
|
let U = m;
|
|
@@ -689,4 +692,4 @@ export {
|
|
|
689
692
|
U as W,
|
|
690
693
|
z as r
|
|
691
694
|
};
|
|
692
|
-
//# sourceMappingURL=widget.ctx-
|
|
695
|
+
//# sourceMappingURL=widget.ctx-CfUvw50X.js.map
|