@opencx/widget-core 4.0.54 → 4.0.55
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/index.cjs +2 -2
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +177 -185
- package/dist/index.js.map +1 -1
- package/dist/src/__tests__/test-utils.d.ts +10 -21
- package/dist/src/__tests__/test-utils.d.ts.map +1 -1
- package/dist/src/api/api-caller.d.ts +11 -23
- package/dist/src/api/api-caller.d.ts.map +1 -1
- package/dist/src/api/schema.d.ts +609 -831
- package/dist/src/api/schema.d.ts.map +1 -1
- package/dist/src/context/active-session-polling.ctx.d.ts +1 -2
- package/dist/src/context/active-session-polling.ctx.d.ts.map +1 -1
- package/dist/src/context/session.ctx.d.ts +8 -0
- package/dist/src/context/session.ctx.d.ts.map +1 -1
- package/dist/src/types/dtos.d.ts +1 -0
- package/dist/src/types/dtos.d.ts.map +1 -1
- package/package.json +4 -4
package/dist/index.js
CHANGED
|
@@ -1,21 +1,21 @@
|
|
|
1
1
|
import N from "openapi-fetch";
|
|
2
2
|
import V from "lodash.isequal";
|
|
3
3
|
import { v4 as L } from "uuid";
|
|
4
|
-
const H = (
|
|
5
|
-
console.log(
|
|
6
|
-
}, F = (
|
|
7
|
-
const
|
|
8
|
-
baseUrl:
|
|
4
|
+
const H = (h) => {
|
|
5
|
+
console.log(h.error);
|
|
6
|
+
}, F = (h) => {
|
|
7
|
+
const n = N({
|
|
8
|
+
baseUrl: h.baseUrl
|
|
9
9
|
}), i = {
|
|
10
|
-
onRequest:
|
|
11
|
-
onResponse:
|
|
12
|
-
onError:
|
|
10
|
+
onRequest: h.onRequest,
|
|
11
|
+
onResponse: h.onResponse,
|
|
12
|
+
onError: h.onError || H
|
|
13
13
|
};
|
|
14
|
-
return
|
|
14
|
+
return n.use(i), n;
|
|
15
15
|
};
|
|
16
16
|
class M {
|
|
17
|
-
constructor({ config:
|
|
18
|
-
var
|
|
17
|
+
constructor({ config: n }) {
|
|
18
|
+
var u, s;
|
|
19
19
|
this.userToken = null, this.constructClientOptions = (e) => {
|
|
20
20
|
const t = this.config.apiUrl || "https://api.open.cx", a = {
|
|
21
21
|
"X-Bot-Token": this.config.token,
|
|
@@ -30,8 +30,8 @@ class M {
|
|
|
30
30
|
}) => F({
|
|
31
31
|
baseUrl: e,
|
|
32
32
|
onRequest: ({ request: a }) => {
|
|
33
|
-
Object.entries(t).forEach(([
|
|
34
|
-
r && a.headers.set(
|
|
33
|
+
Object.entries(t).forEach(([o, r]) => {
|
|
34
|
+
r && a.headers.set(o, r);
|
|
35
35
|
});
|
|
36
36
|
}
|
|
37
37
|
}), this.setAuthToken = (e) => {
|
|
@@ -39,7 +39,7 @@ class M {
|
|
|
39
39
|
const { baseUrl: t, headers: a } = this.constructClientOptions(e);
|
|
40
40
|
this.client = this.createOpenAPIClient({ baseUrl: t, headers: a });
|
|
41
41
|
}, this.getExternalWidgetConfig = async () => await this.client.GET("/backend/widget/v2/config", {
|
|
42
|
-
params: { header: { "
|
|
42
|
+
params: { header: { "x-bot-token": this.config.token } }
|
|
43
43
|
}), this.sendMessage = async (e, t) => await this.client.POST("/backend/widget/v2/chat/send", {
|
|
44
44
|
body: e,
|
|
45
45
|
signal: t
|
|
@@ -53,15 +53,11 @@ class M {
|
|
|
53
53
|
body: e
|
|
54
54
|
}), this.pollSessionAndHistory = async ({
|
|
55
55
|
sessionId: e,
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
params: { path: { sessionId: e }, query: n },
|
|
62
|
-
signal: a
|
|
63
|
-
});
|
|
64
|
-
}, this.getSessions = async ({
|
|
56
|
+
abortSignal: t
|
|
57
|
+
}) => await this.client.GET("/backend/widget/v2/poll/{sessionId}", {
|
|
58
|
+
params: { path: { sessionId: e } },
|
|
59
|
+
signal: t
|
|
60
|
+
}), this.getSessions = async ({
|
|
65
61
|
cursor: e,
|
|
66
62
|
filters: t,
|
|
67
63
|
abortSignal: a
|
|
@@ -72,10 +68,10 @@ class M {
|
|
|
72
68
|
file: e,
|
|
73
69
|
abortSignal: t,
|
|
74
70
|
onProgress: a
|
|
75
|
-
}) => new Promise((
|
|
71
|
+
}) => new Promise((o, r) => {
|
|
76
72
|
var y;
|
|
77
|
-
const
|
|
78
|
-
|
|
73
|
+
const _ = new FormData();
|
|
74
|
+
_.append("file", e);
|
|
79
75
|
const c = new XMLHttpRequest();
|
|
80
76
|
if (t && (t.addEventListener("abort", () => {
|
|
81
77
|
c.abort(), r(new DOMException("Aborted", "AbortError"));
|
|
@@ -83,18 +79,18 @@ class M {
|
|
|
83
79
|
r(new DOMException("Aborted", "AbortError"));
|
|
84
80
|
return;
|
|
85
81
|
}
|
|
86
|
-
c.upload.addEventListener("progress", (
|
|
87
|
-
if (
|
|
88
|
-
const
|
|
89
|
-
a(
|
|
82
|
+
c.upload.addEventListener("progress", (m) => {
|
|
83
|
+
if (m.lengthComputable && a) {
|
|
84
|
+
const b = Math.round(m.loaded / m.total * 100);
|
|
85
|
+
a(b);
|
|
90
86
|
}
|
|
91
87
|
}), c.addEventListener("load", () => {
|
|
92
88
|
if (c.status >= 200 && c.status < 300)
|
|
93
89
|
try {
|
|
94
|
-
const
|
|
95
|
-
|
|
96
|
-
} catch (
|
|
97
|
-
r(new Error(`Failed to parse response: ${
|
|
90
|
+
const m = JSON.parse(c.responseText);
|
|
91
|
+
o(m);
|
|
92
|
+
} catch (m) {
|
|
93
|
+
r(new Error(`Failed to parse response: ${m}`));
|
|
98
94
|
}
|
|
99
95
|
else
|
|
100
96
|
r(new Error(`Upload failed with status: ${c.status}`));
|
|
@@ -103,25 +99,25 @@ class M {
|
|
|
103
99
|
}), c.addEventListener("timeout", () => {
|
|
104
100
|
r(new Error("Upload timed out"));
|
|
105
101
|
});
|
|
106
|
-
const { baseUrl:
|
|
107
|
-
c.open("POST",
|
|
102
|
+
const { baseUrl: p } = this.constructClientOptions(this.userToken), k = `${p}/backend/widget/v2/upload`;
|
|
103
|
+
c.open("POST", k), c.setRequestHeader("X-Bot-Token", this.config.token), this.userToken ?? ((y = this.config.user) == null ? void 0 : y.token) ? c.setRequestHeader("Authorization", `Bearer ${this.userToken}`) : console.error("User token not set"), c.send(_);
|
|
108
104
|
}), this.vote = async (e) => await this.client.POST("/backend/widget/v2/chat/vote", { body: e }), this.resolveSession = async (e, t) => await this.client.POST("/backend/widget/v2/session/resolve", {
|
|
109
105
|
body: e,
|
|
110
106
|
signal: t
|
|
111
107
|
}), this.createStateCheckpoint = async (e) => await this.client.POST("/backend/widget/v2/checkpoint", {
|
|
112
108
|
body: e
|
|
113
|
-
}), this.submitCsat = async (e) => await this.client.POST("/backend/widget/v2/submit-csat", { body: e }), this.config =
|
|
109
|
+
}), this.submitCsat = async (e) => await this.client.POST("/backend/widget/v2/submit-csat", { body: e }), this.config = n, this.userToken = ((u = n.user) == null ? void 0 : u.token) || null;
|
|
114
110
|
const { baseUrl: i, headers: l } = this.constructClientOptions(
|
|
115
|
-
(s =
|
|
111
|
+
(s = n.user) == null ? void 0 : s.token
|
|
116
112
|
);
|
|
117
113
|
this.client = this.createOpenAPIClient({ baseUrl: i, headers: l });
|
|
118
114
|
}
|
|
119
115
|
}
|
|
120
|
-
function K(
|
|
121
|
-
console.error(`Missing case for ${
|
|
116
|
+
function K(h, n) {
|
|
117
|
+
console.error(`Missing case for ${h} in ${n}`);
|
|
122
118
|
}
|
|
123
119
|
class S {
|
|
124
|
-
constructor(
|
|
120
|
+
constructor(n) {
|
|
125
121
|
this.subscribers = /* @__PURE__ */ new Set(), this.get = () => this.state, this.set = (i) => {
|
|
126
122
|
V(this.state, i) || (this.state = i, this.notifySubscribers(i));
|
|
127
123
|
}, this.setPartial = (i) => {
|
|
@@ -131,16 +127,16 @@ class S {
|
|
|
131
127
|
}, this.reset = () => {
|
|
132
128
|
this.set(this.initialState);
|
|
133
129
|
}, this.notifySubscribers = (i) => {
|
|
134
|
-
Array.from(this.subscribers).forEach((
|
|
130
|
+
Array.from(this.subscribers).forEach((u) => {
|
|
135
131
|
try {
|
|
136
|
-
|
|
132
|
+
u(i);
|
|
137
133
|
} catch (s) {
|
|
138
134
|
console.error(s);
|
|
139
135
|
}
|
|
140
136
|
});
|
|
141
137
|
}, this.subscribe = (i) => (this.subscribers.add(i), () => {
|
|
142
138
|
this.subscribers.delete(i);
|
|
143
|
-
}), this.state =
|
|
139
|
+
}), this.state = n, this.initialState = n;
|
|
144
140
|
}
|
|
145
141
|
}
|
|
146
142
|
class O {
|
|
@@ -149,14 +145,14 @@ class O {
|
|
|
149
145
|
isPolling: !1,
|
|
150
146
|
isError: !1
|
|
151
147
|
}), this.abortController = new AbortController(), this.reset = () => {
|
|
152
|
-
var
|
|
153
|
-
this.abortController.abort("Resetting poller"), (
|
|
154
|
-
}, this.stopPolling = null, this.startPolling = (
|
|
148
|
+
var n;
|
|
149
|
+
this.abortController.abort("Resetting poller"), (n = this.stopPolling) == null || n.call(this), this.stopPolling = null;
|
|
150
|
+
}, this.stopPolling = null, this.startPolling = (n, i) => {
|
|
155
151
|
if (this.stopPolling) return;
|
|
156
|
-
const l = [],
|
|
152
|
+
const l = [], u = async () => {
|
|
157
153
|
this.abortController = new AbortController(), this.state.setPartial({ isPolling: !0 });
|
|
158
154
|
try {
|
|
159
|
-
await
|
|
155
|
+
await n(this.abortController.signal);
|
|
160
156
|
} catch (s) {
|
|
161
157
|
if (this.abortController.signal.aborted)
|
|
162
158
|
return;
|
|
@@ -164,28 +160,28 @@ class O {
|
|
|
164
160
|
} finally {
|
|
165
161
|
this.state.setPartial({ isPolling: !1 });
|
|
166
162
|
}
|
|
167
|
-
this.abortController.signal.aborted ? console.log("Poller aborted, not scheduling anymore") : l.push(setTimeout(
|
|
163
|
+
this.abortController.signal.aborted ? console.log("Poller aborted, not scheduling anymore") : l.push(setTimeout(u, i));
|
|
168
164
|
};
|
|
169
|
-
|
|
165
|
+
u(), this.stopPolling = () => {
|
|
170
166
|
l.forEach(clearTimeout), this.state.reset();
|
|
171
167
|
};
|
|
172
168
|
};
|
|
173
169
|
}
|
|
174
170
|
}
|
|
175
|
-
function R(
|
|
171
|
+
function R(h) {
|
|
176
172
|
try {
|
|
177
|
-
const
|
|
178
|
-
return
|
|
179
|
-
} catch (
|
|
180
|
-
return { error:
|
|
173
|
+
const n = h();
|
|
174
|
+
return n instanceof Promise ? n.then((i) => ({ data: i })).catch((i) => ({ error: i })) : { data: n };
|
|
175
|
+
} catch (n) {
|
|
176
|
+
return { error: n };
|
|
181
177
|
}
|
|
182
178
|
}
|
|
183
179
|
class B {
|
|
184
180
|
constructor({
|
|
185
|
-
api:
|
|
181
|
+
api: n,
|
|
186
182
|
config: i,
|
|
187
183
|
sessionCtx: l,
|
|
188
|
-
messageCtx:
|
|
184
|
+
messageCtx: u,
|
|
189
185
|
sessionPollingIntervalSeconds: s
|
|
190
186
|
}) {
|
|
191
187
|
this.poller = new O(), this.fetchSessionAndFullHistoryAbortController = new AbortController(), this.registerPolling = () => {
|
|
@@ -198,8 +194,7 @@ class B {
|
|
|
198
194
|
try {
|
|
199
195
|
this.fetchSessionAndFullHistoryAbortController = new AbortController(), this.fetchSessionAndHistory({
|
|
200
196
|
sessionId: e.id,
|
|
201
|
-
abortSignal: this.fetchSessionAndFullHistoryAbortController.signal
|
|
202
|
-
fetchFullHistory: !0
|
|
197
|
+
abortSignal: this.fetchSessionAndFullHistoryAbortController.signal
|
|
203
198
|
});
|
|
204
199
|
} catch (t) {
|
|
205
200
|
this.fetchSessionAndFullHistoryAbortController.signal.aborted || console.error("Failed to fetch session and full history:", t);
|
|
@@ -209,30 +204,27 @@ class B {
|
|
|
209
204
|
});
|
|
210
205
|
}, this.fetchSessionAndHistory = async ({
|
|
211
206
|
sessionId: e,
|
|
212
|
-
abortSignal: t
|
|
213
|
-
fetchFullHistory: a = !1
|
|
207
|
+
abortSignal: t
|
|
214
208
|
}) => {
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
const r = this.messageCtx.state.get().messages, d = r.length > 0 ? ((v = r[r.length - 1]) == null ? void 0 : v.timestamp) ?? void 0 : void 0, { data: c } = await this.api.pollSessionAndHistory({
|
|
209
|
+
const a = this.messageCtx.state.get().messages.length === 0;
|
|
210
|
+
a && this.messageCtx.state.setPartial({ isInitialFetchLoading: !0 });
|
|
211
|
+
const { data: o } = await this.api.pollSessionAndHistory({
|
|
219
212
|
sessionId: e,
|
|
220
|
-
abortSignal: t
|
|
221
|
-
lastMessageTimestamp: a ? void 0 : d
|
|
213
|
+
abortSignal: t
|
|
222
214
|
});
|
|
223
|
-
if (
|
|
224
|
-
const
|
|
225
|
-
(
|
|
215
|
+
if (o != null && o.session && (this.sessionCtx.sessionState.setPartial({ session: o.session }), this.sessionCtx.setSessions([o.session])), o != null && o.history && o.history.length > 0) {
|
|
216
|
+
const r = this.messageCtx.state.get().messages, _ = o.history.map(this.mapHistoryToMessage).filter((c) => c !== null).filter(
|
|
217
|
+
(c) => !r.some((p) => p.id === c.id)
|
|
226
218
|
);
|
|
227
219
|
if (this.messageCtx.state.setPartial({
|
|
228
|
-
messages: [...
|
|
229
|
-
}),
|
|
220
|
+
messages: [...r, ..._]
|
|
221
|
+
}), a)
|
|
230
222
|
this.messageCtx.markAsDispatchedToOnMessageReceivedHook(
|
|
231
|
-
|
|
223
|
+
_.map((c) => c.id)
|
|
232
224
|
);
|
|
233
225
|
else
|
|
234
|
-
for (const
|
|
235
|
-
this.messageCtx.dispatchToOnMessageReceivedHook(
|
|
226
|
+
for (const c of _)
|
|
227
|
+
this.messageCtx.dispatchToOnMessageReceivedHook(c);
|
|
236
228
|
}
|
|
237
229
|
this.messageCtx.state.get().isInitialFetchLoading && this.messageCtx.state.setPartial({ isInitialFetchLoading: !1 });
|
|
238
230
|
}, this.mapHistoryToMessage = (e) => {
|
|
@@ -268,7 +260,7 @@ class B {
|
|
|
268
260
|
}
|
|
269
261
|
};
|
|
270
262
|
if (e.sender.kind === "ai") {
|
|
271
|
-
const
|
|
263
|
+
const o = e.actionCalls && e.actionCalls.length > 0 ? e.actionCalls[e.actionCalls.length - 1] : void 0;
|
|
272
264
|
return {
|
|
273
265
|
...t,
|
|
274
266
|
type: "AI",
|
|
@@ -283,16 +275,16 @@ class B {
|
|
|
283
275
|
},
|
|
284
276
|
data: {
|
|
285
277
|
message: e.content.text || "",
|
|
286
|
-
action:
|
|
287
|
-
name:
|
|
288
|
-
data: this.extractActionResult(
|
|
278
|
+
action: o ? {
|
|
279
|
+
name: o.actionName,
|
|
280
|
+
data: this.extractActionResult(o)
|
|
289
281
|
} : void 0
|
|
290
282
|
}
|
|
291
283
|
};
|
|
292
284
|
}
|
|
293
285
|
if (e.sender.kind === "system") {
|
|
294
|
-
const
|
|
295
|
-
return
|
|
286
|
+
const o = this.constructSystemMessage(e);
|
|
287
|
+
return o === null ? null : { ...o };
|
|
296
288
|
}
|
|
297
289
|
return null;
|
|
298
290
|
}, this.constructSystemMessage = (e) => {
|
|
@@ -342,44 +334,44 @@ class B {
|
|
|
342
334
|
const t = e.result;
|
|
343
335
|
if (t === null || typeof t != "object") return t;
|
|
344
336
|
if ("responseBodyText" in t && typeof t.responseBodyText == "string") {
|
|
345
|
-
const a = t.responseBodyText,
|
|
346
|
-
if (
|
|
337
|
+
const a = t.responseBodyText, o = R(() => JSON.parse(a)).data;
|
|
338
|
+
if (o) return o;
|
|
347
339
|
}
|
|
348
340
|
return e.result;
|
|
349
|
-
}, this.api =
|
|
341
|
+
}, this.api = n, this.config = i, this.sessionCtx = l, this.messageCtx = u, this.sessionPollingIntervalSeconds = s, this.registerPolling();
|
|
350
342
|
}
|
|
351
343
|
}
|
|
352
344
|
class J {
|
|
353
345
|
constructor({
|
|
354
|
-
config:
|
|
346
|
+
config: n,
|
|
355
347
|
api: i,
|
|
356
348
|
storageCtx: l
|
|
357
349
|
}) {
|
|
358
|
-
var
|
|
350
|
+
var u;
|
|
359
351
|
this.shouldCollectData = () => {
|
|
360
352
|
var s;
|
|
361
353
|
return !!(!((s = this.state.get().contact) != null && s.token) && this.config.collectUserData);
|
|
362
354
|
}, this.autoCreateUnverifiedUserIfNotExists = async () => {
|
|
363
|
-
var s, e, t, a,
|
|
355
|
+
var s, e, t, a, o, r, _, c, p, w, k, v, y, m;
|
|
364
356
|
if (!((s = this.config.user) != null && s.token)) {
|
|
365
357
|
if (this.config.collectUserData && !((t = (e = this.config.user) == null ? void 0 : e.data) != null && t.email)) {
|
|
366
358
|
if ((a = this.config.extraDataCollectionFields) != null && a.length)
|
|
367
359
|
return;
|
|
368
|
-
const
|
|
369
|
-
|
|
360
|
+
const b = await ((o = this.storageCtx) == null ? void 0 : o.getContactToken());
|
|
361
|
+
b && await this.setUnverifiedContact(b);
|
|
370
362
|
return;
|
|
371
363
|
}
|
|
372
|
-
if (!((
|
|
373
|
-
const
|
|
374
|
-
if (
|
|
375
|
-
await this.setUnverifiedContact(
|
|
364
|
+
if (!((_ = (r = this.config.user) == null ? void 0 : r.data) != null && _.email)) {
|
|
365
|
+
const b = await ((c = this.storageCtx) == null ? void 0 : c.getContactToken());
|
|
366
|
+
if (b) {
|
|
367
|
+
await this.setUnverifiedContact(b);
|
|
376
368
|
return;
|
|
377
369
|
}
|
|
378
370
|
}
|
|
379
371
|
await this.createUnverifiedContact({
|
|
380
|
-
email: (
|
|
381
|
-
non_verified_name: (
|
|
382
|
-
non_verified_custom_data: (
|
|
372
|
+
email: (w = (p = this.config.user) == null ? void 0 : p.data) == null ? void 0 : w.email,
|
|
373
|
+
non_verified_name: (v = (k = this.config.user) == null ? void 0 : k.data) == null ? void 0 : v.name,
|
|
374
|
+
non_verified_custom_data: (m = (y = this.config.user) == null ? void 0 : y.data) == null ? void 0 : m.customData
|
|
383
375
|
});
|
|
384
376
|
}
|
|
385
377
|
}, this.createUnverifiedContact = async (s, e) => {
|
|
@@ -395,14 +387,14 @@ class J {
|
|
|
395
387
|
this.state.setPartial({ isCreatingUnverifiedContact: !1 });
|
|
396
388
|
}
|
|
397
389
|
}, this.setUnverifiedContact = async (s) => {
|
|
398
|
-
var a,
|
|
399
|
-
const e = await ((a = this.storageCtx) == null ? void 0 : a.getExternalContactId()), t = ((
|
|
400
|
-
this.api.setAuthToken(s), await ((r = this.storageCtx) == null ? void 0 : r.setContactToken(s)), await ((
|
|
401
|
-
}, this.config =
|
|
402
|
-
contact: (
|
|
403
|
-
token:
|
|
390
|
+
var a, o, r, _;
|
|
391
|
+
const e = await ((a = this.storageCtx) == null ? void 0 : a.getExternalContactId()), t = ((o = this.config.user) == null ? void 0 : o.externalId) || e || L();
|
|
392
|
+
this.api.setAuthToken(s), await ((r = this.storageCtx) == null ? void 0 : r.setContactToken(s)), await ((_ = this.storageCtx) == null ? void 0 : _.setExternalContactId(t)), this.state.setPartial({ contact: { token: s, externalId: t } });
|
|
393
|
+
}, this.config = n, this.storageCtx = l, this.api = i, this.state = new S({
|
|
394
|
+
contact: (u = n.user) != null && u.token ? {
|
|
395
|
+
token: n.user.token,
|
|
404
396
|
// Set optional externalId from config... not local storage
|
|
405
|
-
externalId:
|
|
397
|
+
externalId: n.user.externalId
|
|
406
398
|
} : null,
|
|
407
399
|
extraCollectedData: void 0,
|
|
408
400
|
isCreatingUnverifiedContact: !1,
|
|
@@ -415,10 +407,10 @@ function C() {
|
|
|
415
407
|
}
|
|
416
408
|
class q {
|
|
417
409
|
constructor({
|
|
418
|
-
config:
|
|
410
|
+
config: n,
|
|
419
411
|
api: i,
|
|
420
412
|
sessionCtx: l,
|
|
421
|
-
messageCtx:
|
|
413
|
+
messageCtx: u
|
|
422
414
|
}) {
|
|
423
415
|
this.submitCsat = async (s) => {
|
|
424
416
|
var r;
|
|
@@ -443,21 +435,21 @@ class q {
|
|
|
443
435
|
}
|
|
444
436
|
]
|
|
445
437
|
});
|
|
446
|
-
const { data: a, error:
|
|
438
|
+
const { data: a, error: o } = await this.api.submitCsat({
|
|
447
439
|
...s,
|
|
448
440
|
system_message_uuid: t,
|
|
449
441
|
session_id: e
|
|
450
442
|
});
|
|
451
|
-
return { data: a, error:
|
|
452
|
-
}, this.config =
|
|
443
|
+
return { data: a, error: o };
|
|
444
|
+
}, this.config = n, this.api = i, this.sessionCtx = l, this.messageCtx = u;
|
|
453
445
|
}
|
|
454
446
|
}
|
|
455
447
|
class G {
|
|
456
448
|
constructor({
|
|
457
|
-
config:
|
|
449
|
+
config: n,
|
|
458
450
|
api: i,
|
|
459
451
|
contactCtx: l,
|
|
460
|
-
sessionsPollingIntervalSeconds:
|
|
452
|
+
sessionsPollingIntervalSeconds: u
|
|
461
453
|
}) {
|
|
462
454
|
this.sessionsRefresher = new O(), this.sessionState = new S({
|
|
463
455
|
session: null,
|
|
@@ -492,15 +484,15 @@ class G {
|
|
|
492
484
|
}
|
|
493
485
|
)
|
|
494
486
|
), this.createSession = async () => {
|
|
495
|
-
var
|
|
487
|
+
var o, r, _;
|
|
496
488
|
this.sessionState.setPartial({ session: null, isCreatingSession: !0 });
|
|
497
|
-
const s = (
|
|
489
|
+
const s = (o = this.contactCtx.state.get().contact) == null ? void 0 : o.externalId, e = {
|
|
498
490
|
...this.getParsedCustomData(),
|
|
499
491
|
...s ? { external_id: s } : {}
|
|
500
492
|
}, { data: t, error: a } = await this.api.createSession({
|
|
501
493
|
customData: Object.keys(e).length > 0 ? e : void 0
|
|
502
494
|
});
|
|
503
|
-
return t ? (this.sessionState.setPartial({ session: t, isCreatingSession: !1 }), (
|
|
495
|
+
return t ? (this.sessionState.setPartial({ session: t, isCreatingSession: !1 }), (_ = (r = this.config.hooks) == null ? void 0 : r.onSessionCreated) == null || _.call(r, { session: t }), t) : (this.sessionState.setPartial({ isCreatingSession: !1 }), console.error("Failed to create session:", a), null);
|
|
504
496
|
}, this.loadMoreSessions = async () => {
|
|
505
497
|
if (this.sessionsState.get().isLastPage) return;
|
|
506
498
|
const { data: s } = await this.getSessions({
|
|
@@ -508,7 +500,7 @@ class G {
|
|
|
508
500
|
});
|
|
509
501
|
if (s) {
|
|
510
502
|
const t = [...this.sessionsState.get().data, ...s.items].filter(
|
|
511
|
-
(a,
|
|
503
|
+
(a, o, r) => o === r.findIndex((_) => a.id === _.id)
|
|
512
504
|
);
|
|
513
505
|
this.sessionsState.setPartial({
|
|
514
506
|
data: t,
|
|
@@ -528,7 +520,7 @@ class G {
|
|
|
528
520
|
});
|
|
529
521
|
}, this.setSessions = (s) => {
|
|
530
522
|
const e = [...s, ...this.sessionsState.get().data].filter(
|
|
531
|
-
(t, a,
|
|
523
|
+
(t, a, o) => a === o.findIndex((r) => t.id === r.id)
|
|
532
524
|
);
|
|
533
525
|
this.sessionsState.setPartial({ data: e });
|
|
534
526
|
}, this.refreshSessions = async () => {
|
|
@@ -544,23 +536,23 @@ class G {
|
|
|
544
536
|
});
|
|
545
537
|
return e ? (this.sessionState.setPartial({ session: e, isResolvingSession: !1 }), { success: !0, data: e }) : (this.sessionState.setPartial({ isResolvingSession: !1 }), { success: !1, error: t });
|
|
546
538
|
}, this.createStateCheckpoint = async (s) => {
|
|
547
|
-
var
|
|
548
|
-
const e = (
|
|
539
|
+
var o;
|
|
540
|
+
const e = (o = this.sessionState.get().session) == null ? void 0 : o.id;
|
|
549
541
|
if (!e) return;
|
|
550
542
|
const { data: t, error: a } = await this.api.createStateCheckpoint({
|
|
551
543
|
session_id: e,
|
|
552
544
|
payload: s
|
|
553
545
|
});
|
|
554
546
|
return t != null && t.success ? { success: !0 } : { success: !1 };
|
|
555
|
-
}, this.config =
|
|
547
|
+
}, this.config = n, this.api = i, this.contactCtx = l, this.sessionsPollingIntervalSeconds = u, this.registerSessionsRefresherWrapper();
|
|
556
548
|
}
|
|
557
549
|
}
|
|
558
550
|
class W {
|
|
559
551
|
constructor({
|
|
560
|
-
config:
|
|
552
|
+
config: n,
|
|
561
553
|
api: i,
|
|
562
554
|
sessionCtx: l,
|
|
563
|
-
contactCtx:
|
|
555
|
+
contactCtx: u
|
|
564
556
|
}) {
|
|
565
557
|
this.state = new S({
|
|
566
558
|
messages: [],
|
|
@@ -580,7 +572,7 @@ class W {
|
|
|
580
572
|
(e) => this.messageIdsDispatchedToOnMessageReceivedHook.add(e)
|
|
581
573
|
);
|
|
582
574
|
}, this.sendMessage = async (s) => {
|
|
583
|
-
var t, a,
|
|
575
|
+
var t, a, o, r, _, c, p, w, k;
|
|
584
576
|
let e;
|
|
585
577
|
try {
|
|
586
578
|
if (!s.content.trim() && (!s.attachments || s.attachments.length === 0)) {
|
|
@@ -589,25 +581,25 @@ class W {
|
|
|
589
581
|
);
|
|
590
582
|
return;
|
|
591
583
|
}
|
|
592
|
-
const
|
|
593
|
-
if (this.config.disableSendingWhenAwaitingAIReply !== !1 && (
|
|
594
|
-
|
|
584
|
+
const v = this.sessionCtx.sessionState.get().session, m = (v == null ? void 0 : v.assignee.kind) === "ai", b = this.state.get().isSendingMessageToAI, x = this.state.get().messages.at(-1);
|
|
585
|
+
if (this.config.disableSendingWhenAwaitingAIReply !== !1 && (b || // If last message is from user, then bot response did not arrive yet
|
|
586
|
+
m && (x == null ? void 0 : x.type) === "USER")) {
|
|
595
587
|
console.warn("Cannot send messages while awaiting AI response");
|
|
596
588
|
return;
|
|
597
589
|
}
|
|
598
590
|
this.sendMessageAbortController.abort("Superseded by a newer message"), this.sendMessageAbortController = new AbortController(), e = this.sendMessageAbortController, this.state.setPartial({
|
|
599
591
|
lastAIResMightSolveUserIssue: !1,
|
|
600
592
|
isSendingMessage: !0,
|
|
601
|
-
isSendingMessageToAI: !!
|
|
593
|
+
isSendingMessageToAI: !!m || !v
|
|
602
594
|
});
|
|
603
|
-
const j = this.state.get().messages, P = !((t = this.sessionCtx.sessionState.get().session) != null && t.id) && j.length === 0 && ((a = this.config.advancedInitialMessages) == null ? void 0 : a.some((
|
|
604
|
-
(
|
|
595
|
+
const j = this.state.get().messages, P = !((t = this.sessionCtx.sessionState.get().session) != null && t.id) && j.length === 0 && ((a = this.config.advancedInitialMessages) == null ? void 0 : a.some((d) => d.persistent)), T = P ? (this.config.advancedInitialMessages || []).filter((d) => d.persistent).map(
|
|
596
|
+
(d) => ({
|
|
605
597
|
id: C(),
|
|
606
598
|
component: "bot_message",
|
|
607
599
|
type: "AI",
|
|
608
600
|
timestamp: (/* @__PURE__ */ new Date()).toISOString(),
|
|
609
601
|
data: {
|
|
610
|
-
message:
|
|
602
|
+
message: d.message
|
|
611
603
|
},
|
|
612
604
|
agent: this.config.bot ? { ...this.config.bot, isAi: !0, id: null } : void 0
|
|
613
605
|
})
|
|
@@ -621,7 +613,7 @@ class W {
|
|
|
621
613
|
...j,
|
|
622
614
|
I
|
|
623
615
|
]
|
|
624
|
-
}), !((
|
|
616
|
+
}), !((o = this.sessionCtx.sessionState.get().session) != null && o.id)) {
|
|
625
617
|
if (!await this.sessionCtx.createSession()) {
|
|
626
618
|
console.error("Failed to create session");
|
|
627
619
|
return;
|
|
@@ -630,7 +622,7 @@ class W {
|
|
|
630
622
|
}
|
|
631
623
|
const A = (r = this.sessionCtx.sessionState.get().session) == null ? void 0 : r.id;
|
|
632
624
|
if (!A) return;
|
|
633
|
-
const { data:
|
|
625
|
+
const { data: g } = await this.api.sendMessage(
|
|
634
626
|
{
|
|
635
627
|
uuid: I.id,
|
|
636
628
|
bot_token: this.config.token,
|
|
@@ -647,41 +639,41 @@ class W {
|
|
|
647
639
|
},
|
|
648
640
|
language: this.config.language,
|
|
649
641
|
exit_mode_prompt: s.exitModePrompt,
|
|
650
|
-
initial_messages: P ? T.map((
|
|
651
|
-
uuid:
|
|
652
|
-
content:
|
|
642
|
+
initial_messages: P ? T.map((d) => ({
|
|
643
|
+
uuid: d.id,
|
|
644
|
+
content: d.data.message
|
|
653
645
|
})) : void 0
|
|
654
646
|
},
|
|
655
647
|
e.signal
|
|
656
648
|
);
|
|
657
|
-
if (
|
|
658
|
-
const
|
|
659
|
-
if (
|
|
649
|
+
if (g != null && g.success) {
|
|
650
|
+
const d = this.toBotMessage(g);
|
|
651
|
+
if (d) {
|
|
660
652
|
const z = this.state.get().messages;
|
|
661
653
|
if (!!z.some(
|
|
662
|
-
(D) => D.id ===
|
|
654
|
+
(D) => D.id === d.id
|
|
663
655
|
)) {
|
|
664
656
|
this.state.setPartial({
|
|
665
|
-
lastAIResMightSolveUserIssue: ((
|
|
657
|
+
lastAIResMightSolveUserIssue: ((_ = g.autopilotResponse) == null ? void 0 : _.mightSolveUserIssue) || ((c = g.uiResponse) == null ? void 0 : c.mightSolveUserIssue)
|
|
666
658
|
});
|
|
667
659
|
return;
|
|
668
660
|
}
|
|
669
661
|
this.state.setPartial({
|
|
670
|
-
messages: [...z,
|
|
671
|
-
lastAIResMightSolveUserIssue: ((
|
|
672
|
-
}), this.dispatchToOnMessageReceivedHook(
|
|
662
|
+
messages: [...z, d],
|
|
663
|
+
lastAIResMightSolveUserIssue: ((p = g.autopilotResponse) == null ? void 0 : p.mightSolveUserIssue) || ((w = g.uiResponse) == null ? void 0 : w.mightSolveUserIssue)
|
|
664
|
+
}), this.dispatchToOnMessageReceivedHook(d);
|
|
673
665
|
}
|
|
674
|
-
|
|
666
|
+
g.session && this.sessionCtx.sessionState.setPartial({ session: g.session });
|
|
675
667
|
} else {
|
|
676
|
-
const
|
|
677
|
-
((
|
|
668
|
+
const d = this.toBotErrorMessage(
|
|
669
|
+
((k = g == null ? void 0 : g.error) == null ? void 0 : k.message) || "Something went wrong. Please refresh the page or try again."
|
|
678
670
|
), z = this.state.get().messages;
|
|
679
671
|
this.state.setPartial({
|
|
680
|
-
messages: [...z,
|
|
672
|
+
messages: [...z, d]
|
|
681
673
|
});
|
|
682
674
|
}
|
|
683
|
-
} catch (
|
|
684
|
-
e != null && e.signal.aborted || console.error("Failed to send message:",
|
|
675
|
+
} catch (v) {
|
|
676
|
+
e != null && e.signal.aborted || console.error("Failed to send message:", v);
|
|
685
677
|
} finally {
|
|
686
678
|
e != null && e.signal.aborted || this.state.setPartial({
|
|
687
679
|
isSendingMessage: !1,
|
|
@@ -691,7 +683,7 @@ class W {
|
|
|
691
683
|
}, this.toUserMessage = (s, e) => {
|
|
692
684
|
const t = (() => {
|
|
693
685
|
const a = this.contactCtx.state.get().extraCollectedData;
|
|
694
|
-
return this.state.get().messages.length === 0 && a && Object.keys(a).length > 0 ? `${Object.entries(a).filter(([r,
|
|
686
|
+
return this.state.get().messages.length === 0 && a && Object.keys(a).length > 0 ? `${Object.entries(a).filter(([r, _]) => !!_).map(([r, _]) => `${r}: ${_}`).join(`
|
|
695
687
|
`)}
|
|
696
688
|
|
|
697
689
|
${s}` : s;
|
|
@@ -737,15 +729,15 @@ ${s}` : s;
|
|
|
737
729
|
variant: "error",
|
|
738
730
|
action: void 0
|
|
739
731
|
}
|
|
740
|
-
}), this.config =
|
|
732
|
+
}), this.config = n, this.api = i, this.sessionCtx = l, this.contactCtx = u;
|
|
741
733
|
}
|
|
742
734
|
}
|
|
743
735
|
class Y {
|
|
744
736
|
constructor({
|
|
745
|
-
config:
|
|
737
|
+
config: n,
|
|
746
738
|
contactCtx: i,
|
|
747
739
|
sessionCtx: l,
|
|
748
|
-
resetChat:
|
|
740
|
+
resetChat: u
|
|
749
741
|
}) {
|
|
750
742
|
var s;
|
|
751
743
|
this.registerRoutingListener = () => {
|
|
@@ -756,37 +748,37 @@ class Y {
|
|
|
756
748
|
});
|
|
757
749
|
}), this.sessionCtx.sessionsState.subscribe(
|
|
758
750
|
({ isInitialFetchLoading: e, data: t }) => {
|
|
759
|
-
var a,
|
|
751
|
+
var a, o, r, _;
|
|
760
752
|
if ((a = this.config.router) != null && a.chatScreenOnly && // Do not route to a chat if we are currently inside one already
|
|
761
753
|
// This also applies to newly created sessions; the new session will be in `sessionState` before it is refreshed and included in `sessionsState`
|
|
762
|
-
!((
|
|
763
|
-
const c = (r = t.find((
|
|
754
|
+
!((o = this.sessionCtx.sessionState.get().session) != null && o.id)) {
|
|
755
|
+
const c = (r = t.find((p) => p.isOpened)) == null ? void 0 : r.id;
|
|
764
756
|
return c ? this.toChatScreen(c) : void 0;
|
|
765
757
|
}
|
|
766
|
-
t.length || ((
|
|
758
|
+
t.length || ((_ = this.config.router) == null ? void 0 : _.goToChatIfNoSessions) !== !1 && !e && this.state.get().screen !== "chat" && this.toChatScreen();
|
|
767
759
|
}
|
|
768
760
|
);
|
|
769
761
|
}, this.toSessionsScreen = () => {
|
|
770
762
|
this.resetChat(), this.state.setPartial({ screen: "sessions" });
|
|
771
763
|
}, this.toChatScreen = (e) => {
|
|
772
|
-
var a,
|
|
764
|
+
var a, o;
|
|
773
765
|
this.resetChat();
|
|
774
766
|
let t;
|
|
775
767
|
if (e) {
|
|
776
768
|
if (t = this.sessionCtx.sessionsState.get().data.find((r) => r.id === e), !t) return;
|
|
777
769
|
this.sessionCtx.sessionState.setPartial({ session: t });
|
|
778
770
|
}
|
|
779
|
-
this.state.setPartial({ screen: "chat" }), (
|
|
771
|
+
this.state.setPartial({ screen: "chat" }), (o = (a = this.config.hooks) == null ? void 0 : a.onNavigateToChat) == null || o.call(a, {
|
|
780
772
|
session: t
|
|
781
773
|
});
|
|
782
|
-
}, this.config =
|
|
774
|
+
}, this.config = n, this.contactCtx = i, this.sessionCtx = l, this.resetChat = u, this.state = new S({
|
|
783
775
|
screen: this.contactCtx.shouldCollectData() ? "welcome" : (s = this.config.router) != null && s.chatScreenOnly ? "chat" : "sessions"
|
|
784
776
|
}), this.registerRoutingListener();
|
|
785
777
|
}
|
|
786
778
|
}
|
|
787
779
|
class $ {
|
|
788
780
|
constructor({
|
|
789
|
-
storage:
|
|
781
|
+
storage: n,
|
|
790
782
|
config: i
|
|
791
783
|
}) {
|
|
792
784
|
this.KEYS = {
|
|
@@ -796,23 +788,23 @@ class $ {
|
|
|
796
788
|
await this.storage.set(this.KEYS.contactToken(this.config.token), l);
|
|
797
789
|
}, this.getContactToken = async () => this.storage.get(this.KEYS.contactToken(this.config.token)), this.setExternalContactId = async (l) => {
|
|
798
790
|
await this.storage.set(this.KEYS.externalContactId(this.config.token), l);
|
|
799
|
-
}, this.getExternalContactId = async () => this.storage.get(this.KEYS.externalContactId(this.config.token)), this.storage =
|
|
791
|
+
}, this.getExternalContactId = async () => this.storage.get(this.KEYS.externalContactId(this.config.token)), this.storage = n, this.config = i;
|
|
800
792
|
}
|
|
801
793
|
}
|
|
802
|
-
const
|
|
794
|
+
const f = class f {
|
|
803
795
|
constructor({
|
|
804
|
-
config:
|
|
796
|
+
config: n,
|
|
805
797
|
storage: i,
|
|
806
798
|
modes: l,
|
|
807
|
-
org:
|
|
799
|
+
org: u
|
|
808
800
|
}) {
|
|
809
801
|
if (this.modes = [], this.resetChat = () => {
|
|
810
802
|
this.sessionCtx.reset(), this.messageCtx.reset();
|
|
811
|
-
}, !
|
|
803
|
+
}, !f.pollingIntervalsSeconds)
|
|
812
804
|
throw Error(
|
|
813
805
|
"Widget polling values are not defined, did you call WidgetCtx.initialize()"
|
|
814
806
|
);
|
|
815
|
-
this.config =
|
|
807
|
+
this.config = n, this.org = u, this.api = new M({ config: n }), this.storageCtx = i ? new $({ storage: i, config: n }) : void 0, this.modes = l, this.contactCtx = new J({
|
|
816
808
|
api: this.api,
|
|
817
809
|
config: this.config,
|
|
818
810
|
storageCtx: this.storageCtx
|
|
@@ -820,7 +812,7 @@ const w = class w {
|
|
|
820
812
|
config: this.config,
|
|
821
813
|
api: this.api,
|
|
822
814
|
contactCtx: this.contactCtx,
|
|
823
|
-
sessionsPollingIntervalSeconds:
|
|
815
|
+
sessionsPollingIntervalSeconds: f.pollingIntervalsSeconds.sessions
|
|
824
816
|
}), this.messageCtx = new W({
|
|
825
817
|
config: this.config,
|
|
826
818
|
api: this.api,
|
|
@@ -836,7 +828,7 @@ const w = class w {
|
|
|
836
828
|
config: this.config,
|
|
837
829
|
sessionCtx: this.sessionCtx,
|
|
838
830
|
messageCtx: this.messageCtx,
|
|
839
|
-
sessionPollingIntervalSeconds:
|
|
831
|
+
sessionPollingIntervalSeconds: f.pollingIntervalsSeconds.session
|
|
840
832
|
}), this.routerCtx = new Y({
|
|
841
833
|
config: this.config,
|
|
842
834
|
contactCtx: this.contactCtx,
|
|
@@ -845,21 +837,21 @@ const w = class w {
|
|
|
845
837
|
});
|
|
846
838
|
}
|
|
847
839
|
};
|
|
848
|
-
|
|
849
|
-
config:
|
|
840
|
+
f.pollingIntervalsSeconds = null, f.initialize = async ({
|
|
841
|
+
config: n,
|
|
850
842
|
storage: i
|
|
851
843
|
}) => {
|
|
852
|
-
var
|
|
844
|
+
var u, s, e;
|
|
853
845
|
const l = await new M({
|
|
854
|
-
config:
|
|
846
|
+
config: n
|
|
855
847
|
}).getExternalWidgetConfig();
|
|
856
848
|
if (!l.data)
|
|
857
849
|
throw new Error("Failed to fetch widget config");
|
|
858
|
-
return
|
|
859
|
-
session: ((
|
|
850
|
+
return f.pollingIntervalsSeconds = {
|
|
851
|
+
session: ((u = l.data) == null ? void 0 : u.sessionPollingIntervalSeconds) || 10,
|
|
860
852
|
sessions: ((s = l.data) == null ? void 0 : s.sessionsPollingIntervalSeconds) || 60
|
|
861
|
-
}, new
|
|
862
|
-
config:
|
|
853
|
+
}, new f({
|
|
854
|
+
config: n,
|
|
863
855
|
storage: i,
|
|
864
856
|
modes: ((e = l.data) == null ? void 0 : e.modes) || [],
|
|
865
857
|
org: {
|
|
@@ -868,7 +860,7 @@ w.pollingIntervalsSeconds = null, w.initialize = async ({
|
|
|
868
860
|
}
|
|
869
861
|
});
|
|
870
862
|
};
|
|
871
|
-
let E =
|
|
863
|
+
let E = f;
|
|
872
864
|
const Z = {
|
|
873
865
|
write_a_message_placeholder: "اكتب رسالة...",
|
|
874
866
|
your_issue_has_been_resolved: "تم حل مشكلتك!",
|
|
@@ -1154,7 +1146,7 @@ const Z = {
|
|
|
1154
1146
|
csat_title: "会話はいかがでしたか?",
|
|
1155
1147
|
csat_submitted_title: "会話を次のように評価しました",
|
|
1156
1148
|
csat_feedback_placeholder: "詳しく教えてください...(任意)"
|
|
1157
|
-
},
|
|
1149
|
+
}, ue = {
|
|
1158
1150
|
write_a_message_placeholder: "메시지를 입력하세요...",
|
|
1159
1151
|
your_issue_has_been_resolved: "문제가 해결되었습니다!",
|
|
1160
1152
|
new_conversation: "새 대화",
|
|
@@ -1173,7 +1165,7 @@ const Z = {
|
|
|
1173
1165
|
csat_title: "대화는 어떠셨나요?",
|
|
1174
1166
|
csat_submitted_title: "대화를 다음과 같이 평가했습니다",
|
|
1175
1167
|
csat_feedback_placeholder: "더 알려주세요... (선택 사항)"
|
|
1176
|
-
},
|
|
1168
|
+
}, de = {
|
|
1177
1169
|
write_a_message_placeholder: "Schrijf een bericht...",
|
|
1178
1170
|
your_issue_has_been_resolved: "Uw probleem is opgelost!",
|
|
1179
1171
|
new_conversation: "Nieuw gesprek",
|
|
@@ -1611,13 +1603,13 @@ const Z = {
|
|
|
1611
1603
|
is: je,
|
|
1612
1604
|
it: _e,
|
|
1613
1605
|
ja: he,
|
|
1614
|
-
ko:
|
|
1606
|
+
ko: ue,
|
|
1615
1607
|
lb: Ae,
|
|
1616
1608
|
lt: Te,
|
|
1617
1609
|
lv: Pe,
|
|
1618
1610
|
mt: Me,
|
|
1619
1611
|
nb: Ee,
|
|
1620
|
-
nl:
|
|
1612
|
+
nl: de,
|
|
1621
1613
|
no: me,
|
|
1622
1614
|
pl: ge,
|
|
1623
1615
|
pt: pe,
|
|
@@ -1631,12 +1623,12 @@ const Z = {
|
|
|
1631
1623
|
vi: Se,
|
|
1632
1624
|
"zh-cn": Ce
|
|
1633
1625
|
}, Oe = Object.keys(U);
|
|
1634
|
-
function He(
|
|
1635
|
-
return Oe.includes(
|
|
1626
|
+
function He(h) {
|
|
1627
|
+
return Oe.includes(h);
|
|
1636
1628
|
}
|
|
1637
|
-
function Fe(
|
|
1629
|
+
function Fe(h, n, i) {
|
|
1638
1630
|
var l;
|
|
1639
|
-
return ((l = i == null ? void 0 : i[
|
|
1631
|
+
return ((l = i == null ? void 0 : i[n]) == null ? void 0 : l[h]) || U[n][h] || "";
|
|
1640
1632
|
}
|
|
1641
1633
|
export {
|
|
1642
1634
|
S as PrimitiveState,
|