@opencx/widget 3.0.48 → 3.0.50
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/README.md +3 -2
- package/dist/designs.cjs +1 -1
- package/dist/designs.js +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/__tests__/test-utils.d.ts +3 -0
- package/dist/src/headless/core/api/api-caller.d.ts +12 -9
- package/dist/src/headless/core/api/schema.d.ts +3 -0
- package/dist/src/headless/core/types/widget-config.d.ts +5 -0
- package/dist/{useUploadFiles-CHAAYfU0.cjs → useUploadFiles-14yeba8q.cjs} +3 -3
- package/dist/{useUploadFiles-CHAAYfU0.cjs.map → useUploadFiles-14yeba8q.cjs.map} +1 -1
- package/dist/{useUploadFiles-CUl6z4mJ.js → useUploadFiles-l6HSHHvN.js} +43 -46
- package/dist/{useUploadFiles-CUl6z4mJ.js.map → useUploadFiles-l6HSHHvN.js.map} +1 -1
- package/dist/{widget.ctx-CaItHAmP.js → widget.ctx-CMgNQpCm.js} +202 -181
- package/dist/widget.ctx-CMgNQpCm.js.map +1 -0
- package/dist/widget.ctx-DxGvxdZ2.cjs +5 -0
- package/dist/widget.ctx-DxGvxdZ2.cjs.map +1 -0
- package/dist-embed/script.js +114 -119
- package/dist-embed/script.js.map +1 -1
- package/package.json +1 -2
- package/dist/widget.ctx-BZXHyH9V.cjs +0 -5
- package/dist/widget.ctx-BZXHyH9V.cjs.map +0 -1
- package/dist/widget.ctx-CaItHAmP.js.map +0 -1
|
@@ -1,25 +1,24 @@
|
|
|
1
|
-
import F from "axios";
|
|
2
1
|
import O from "openapi-fetch";
|
|
3
|
-
import
|
|
4
|
-
import { v4 as
|
|
5
|
-
const
|
|
6
|
-
console.log(
|
|
7
|
-
},
|
|
8
|
-
const
|
|
9
|
-
baseUrl:
|
|
2
|
+
import F from "lodash.isequal";
|
|
3
|
+
import { v4 as A } from "uuid";
|
|
4
|
+
const L = (g) => {
|
|
5
|
+
console.log(g.error);
|
|
6
|
+
}, D = (g) => {
|
|
7
|
+
const a = O({
|
|
8
|
+
baseUrl: g.baseUrl
|
|
10
9
|
}), i = {
|
|
11
|
-
onRequest:
|
|
12
|
-
onResponse:
|
|
13
|
-
onError:
|
|
10
|
+
onRequest: g.onRequest,
|
|
11
|
+
onResponse: g.onResponse,
|
|
12
|
+
onError: g.onError || L
|
|
14
13
|
};
|
|
15
|
-
return
|
|
14
|
+
return a.use(i), a;
|
|
16
15
|
};
|
|
17
|
-
class
|
|
16
|
+
class E {
|
|
18
17
|
constructor({
|
|
19
|
-
config:
|
|
18
|
+
config: a
|
|
20
19
|
}) {
|
|
21
|
-
var
|
|
22
|
-
this.constructClientOptions = (t) => {
|
|
20
|
+
var n;
|
|
21
|
+
this.userToken = null, this.constructClientOptions = (t) => {
|
|
23
22
|
const s = this.config.apiUrl || "https://api.open.cx", e = {
|
|
24
23
|
"X-Bot-Token": this.config.token,
|
|
25
24
|
"Content-Type": "application/json",
|
|
@@ -30,22 +29,17 @@ class k {
|
|
|
30
29
|
}, this.createOpenAPIClient = ({
|
|
31
30
|
baseUrl: t,
|
|
32
31
|
headers: s
|
|
33
|
-
}) =>
|
|
32
|
+
}) => D({
|
|
34
33
|
baseUrl: t,
|
|
35
34
|
onRequest: ({ request: e }) => {
|
|
36
|
-
Object.entries(s).forEach(([o,
|
|
37
|
-
|
|
35
|
+
Object.entries(s).forEach(([o, r]) => {
|
|
36
|
+
r && e.headers.set(o, r);
|
|
38
37
|
});
|
|
39
38
|
}
|
|
40
|
-
}), this.createAxiosUploadClient = ({
|
|
41
|
-
baseUrl: t,
|
|
42
|
-
headers: s
|
|
43
|
-
}) => F.create({
|
|
44
|
-
baseURL: `${t}/backend/widget/v2/upload`,
|
|
45
|
-
headers: s
|
|
46
39
|
}), this.setAuthToken = (t) => {
|
|
40
|
+
this.userToken = t;
|
|
47
41
|
const { baseUrl: s, headers: e } = this.constructClientOptions(t);
|
|
48
|
-
this.client = this.createOpenAPIClient({ baseUrl: s, headers: e })
|
|
42
|
+
this.client = this.createOpenAPIClient({ baseUrl: s, headers: e });
|
|
49
43
|
}, this.getExternalWidgetConfig = async () => await this.client.GET("/backend/widget/v2/config", {
|
|
50
44
|
params: { header: { "X-Bot-Token": this.config.token } }
|
|
51
45
|
}), this.widgetPrelude = async () => await this.client.GET("/backend/widget/v2/prelude", {
|
|
@@ -78,60 +72,86 @@ class k {
|
|
|
78
72
|
}) => await this.client.GET("/backend/widget/v2/sessions", {
|
|
79
73
|
params: { query: { cursor: t, filters: JSON.stringify(s) } },
|
|
80
74
|
signal: e
|
|
81
|
-
}), this.uploadFile = async (
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
75
|
+
}), this.uploadFile = async ({
|
|
76
|
+
file: t,
|
|
77
|
+
abortSignal: s,
|
|
78
|
+
onProgress: e
|
|
79
|
+
}) => new Promise((o, r) => {
|
|
80
|
+
const l = new FormData();
|
|
81
|
+
l.append("file", t);
|
|
82
|
+
const c = new XMLHttpRequest();
|
|
83
|
+
if (s && (s.addEventListener("abort", () => {
|
|
84
|
+
c.abort(), r(new DOMException("Aborted", "AbortError"));
|
|
85
|
+
}), s.aborted)) {
|
|
86
|
+
r(new DOMException("Aborted", "AbortError"));
|
|
87
|
+
return;
|
|
88
|
+
}
|
|
89
|
+
c.upload.addEventListener("progress", (d) => {
|
|
90
|
+
if (d.lengthComputable && e) {
|
|
91
|
+
const x = Math.round(d.loaded / d.total * 100);
|
|
92
|
+
e(x);
|
|
93
|
+
}
|
|
94
|
+
}), c.addEventListener("load", () => {
|
|
95
|
+
if (c.status >= 200 && c.status < 300)
|
|
96
|
+
try {
|
|
97
|
+
const d = JSON.parse(c.responseText);
|
|
98
|
+
o(d);
|
|
99
|
+
} catch (d) {
|
|
100
|
+
r(new Error(`Failed to parse response: ${d}`));
|
|
101
|
+
}
|
|
102
|
+
else
|
|
103
|
+
r(new Error(`Upload failed with status: ${c.status}`));
|
|
104
|
+
}), c.addEventListener("error", () => {
|
|
105
|
+
r(new Error("Network error occurred"));
|
|
106
|
+
}), c.addEventListener("timeout", () => {
|
|
107
|
+
r(new Error("Upload timed out"));
|
|
89
108
|
});
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
109
|
+
const { baseUrl: f } = this.constructClientOptions(this.userToken), C = `${f}/backend/widget/v2/upload`;
|
|
110
|
+
c.open("POST", C), c.setRequestHeader("X-Bot-Token", this.config.token), this.userToken && c.setRequestHeader("Authorization", `Bearer ${this.userToken}`), c.send(l);
|
|
111
|
+
}), this.vote = async (t) => await this.client.POST("/backend/widget/v2/chat/vote", { body: t }), this.config = a;
|
|
112
|
+
const { baseUrl: i, headers: h } = this.constructClientOptions(
|
|
113
|
+
(n = a.user) == null ? void 0 : n.token
|
|
94
114
|
);
|
|
95
|
-
this.client = this.createOpenAPIClient({ baseUrl: i, headers:
|
|
115
|
+
this.client = this.createOpenAPIClient({ baseUrl: i, headers: h });
|
|
96
116
|
}
|
|
97
117
|
}
|
|
98
|
-
class
|
|
99
|
-
constructor(
|
|
118
|
+
class b {
|
|
119
|
+
constructor(a) {
|
|
100
120
|
this.subscribers = /* @__PURE__ */ new Set(), this.get = () => this.state, this.set = (i) => {
|
|
101
|
-
|
|
121
|
+
F(this.state, i) || (this.state = i, this.notifySubscribers(i));
|
|
102
122
|
}, this.setPartial = (i) => {
|
|
103
123
|
if (i == null) return;
|
|
104
|
-
const
|
|
105
|
-
this.set(
|
|
124
|
+
const h = { ...this.state, ...i };
|
|
125
|
+
this.set(h);
|
|
106
126
|
}, this.reset = () => {
|
|
107
127
|
this.set(this.initialState);
|
|
108
128
|
}, this.notifySubscribers = (i) => {
|
|
109
|
-
Array.from(this.subscribers).forEach((
|
|
129
|
+
Array.from(this.subscribers).forEach((n) => {
|
|
110
130
|
try {
|
|
111
|
-
|
|
131
|
+
n(i);
|
|
112
132
|
} catch (t) {
|
|
113
133
|
console.error(t);
|
|
114
134
|
}
|
|
115
135
|
});
|
|
116
136
|
}, this.subscribe = (i) => (this.subscribers.add(i), () => {
|
|
117
137
|
this.subscribers.delete(i);
|
|
118
|
-
}), this.state =
|
|
138
|
+
}), this.state = a, this.initialState = a;
|
|
119
139
|
}
|
|
120
140
|
}
|
|
121
|
-
class
|
|
141
|
+
class T {
|
|
122
142
|
constructor() {
|
|
123
|
-
this.state = new
|
|
143
|
+
this.state = new b({
|
|
124
144
|
isPolling: !1,
|
|
125
145
|
isError: !1
|
|
126
146
|
}), this.abortController = new AbortController(), this.reset = () => {
|
|
127
|
-
var
|
|
128
|
-
this.abortController.abort("Resetting poller"), (
|
|
129
|
-
}, this.stopPolling = null, this.startPolling = (
|
|
147
|
+
var a;
|
|
148
|
+
this.abortController.abort("Resetting poller"), (a = this.stopPolling) == null || a.call(this), this.stopPolling = null;
|
|
149
|
+
}, this.stopPolling = null, this.startPolling = (a, i) => {
|
|
130
150
|
if (this.stopPolling) return;
|
|
131
|
-
const
|
|
151
|
+
const h = [], n = async () => {
|
|
132
152
|
this.abortController = new AbortController(), this.state.setPartial({ isPolling: !0 });
|
|
133
153
|
try {
|
|
134
|
-
await
|
|
154
|
+
await a(this.abortController.signal);
|
|
135
155
|
} catch (t) {
|
|
136
156
|
if (this.abortController.signal.aborted)
|
|
137
157
|
return;
|
|
@@ -139,47 +159,47 @@ class M {
|
|
|
139
159
|
} finally {
|
|
140
160
|
this.state.setPartial({ isPolling: !1 });
|
|
141
161
|
}
|
|
142
|
-
this.abortController.signal.aborted ? console.log("Poller aborted, not scheduling anymore") :
|
|
162
|
+
this.abortController.signal.aborted ? console.log("Poller aborted, not scheduling anymore") : h.push(setTimeout(n, i));
|
|
143
163
|
};
|
|
144
|
-
|
|
145
|
-
|
|
164
|
+
n(), this.stopPolling = () => {
|
|
165
|
+
h.forEach(clearTimeout), this.state.reset();
|
|
146
166
|
};
|
|
147
167
|
};
|
|
148
168
|
}
|
|
149
169
|
}
|
|
150
|
-
class
|
|
170
|
+
class _ {
|
|
151
171
|
constructor({
|
|
152
|
-
api:
|
|
172
|
+
api: a,
|
|
153
173
|
config: i,
|
|
154
|
-
sessionCtx:
|
|
155
|
-
messageCtx:
|
|
174
|
+
sessionCtx: h,
|
|
175
|
+
messageCtx: n,
|
|
156
176
|
sessionPollingIntervalSeconds: t
|
|
157
177
|
}) {
|
|
158
|
-
this.poller = new
|
|
178
|
+
this.poller = new T(), this.registerPolling = () => {
|
|
159
179
|
this.sessionCtx.sessionState.subscribe(({ session: s }) => {
|
|
160
180
|
s != null && s.id ? this.poller.startPolling(async (e) => {
|
|
161
181
|
this.hackAndSlash(s.id, e);
|
|
162
182
|
}, this.sessionPollingIntervalSeconds * 1e3) : this.poller.reset();
|
|
163
183
|
});
|
|
164
184
|
}, this.hackAndSlash = async (s, e) => {
|
|
165
|
-
var
|
|
185
|
+
var c;
|
|
166
186
|
this.messageCtx.state.get().messages.length === 0 && this.messageCtx.state.setPartial({ isInitialFetchLoading: !0 });
|
|
167
|
-
const o = this.messageCtx.state.get().messages,
|
|
187
|
+
const o = this.messageCtx.state.get().messages, r = o.length > 0 ? (c = o[o.length - 1]) == null ? void 0 : c.timestamp : void 0, { data: l } = await this.api.pollSessionAndHistory({
|
|
168
188
|
sessionId: s,
|
|
169
189
|
abortSignal: e,
|
|
170
|
-
lastMessageTimestamp:
|
|
190
|
+
lastMessageTimestamp: r
|
|
171
191
|
});
|
|
172
|
-
if (
|
|
173
|
-
const f = this.messageCtx.state.get().messages,
|
|
174
|
-
(
|
|
192
|
+
if (l != null && l.session && this.sessionCtx.sessionState.setPartial({ session: l.session }), l != null && l.history && l.history.length > 0) {
|
|
193
|
+
const f = this.messageCtx.state.get().messages, m = l.history.map(this.mapHistoryToMessage).filter(
|
|
194
|
+
(C) => !f.some((d) => d.id === C.id)
|
|
175
195
|
);
|
|
176
196
|
this.messageCtx.state.setPartial({
|
|
177
|
-
messages: [...f, ...
|
|
197
|
+
messages: [...f, ...m]
|
|
178
198
|
});
|
|
179
199
|
}
|
|
180
200
|
this.messageCtx.state.get().isInitialFetchLoading && this.messageCtx.state.setPartial({ isInitialFetchLoading: !1 });
|
|
181
201
|
}, this.mapHistoryToMessage = (s) => {
|
|
182
|
-
var
|
|
202
|
+
var r, l;
|
|
183
203
|
const e = {
|
|
184
204
|
id: s.publicId,
|
|
185
205
|
timestamp: s.sentAt || "",
|
|
@@ -214,48 +234,48 @@ class B {
|
|
|
214
234
|
component: "bot_message",
|
|
215
235
|
agent: {
|
|
216
236
|
id: null,
|
|
217
|
-
name: ((
|
|
237
|
+
name: ((r = this.config.bot) == null ? void 0 : r.name) || "",
|
|
218
238
|
isAi: !0,
|
|
219
|
-
avatar: ((
|
|
239
|
+
avatar: ((l = this.config.bot) == null ? void 0 : l.avatar) || ""
|
|
220
240
|
},
|
|
221
241
|
data: {
|
|
222
242
|
message: s.content.text || "",
|
|
223
243
|
action: o ? { name: o.actionName, data: o.result } : void 0
|
|
224
244
|
}
|
|
225
245
|
};
|
|
226
|
-
}, this.api =
|
|
246
|
+
}, this.api = a, this.config = i, this.sessionCtx = h, this.messageCtx = n, this.sessionPollingIntervalSeconds = t, this.registerPolling();
|
|
227
247
|
}
|
|
228
248
|
}
|
|
229
|
-
class
|
|
249
|
+
class B {
|
|
230
250
|
constructor({
|
|
231
|
-
config:
|
|
251
|
+
config: a,
|
|
232
252
|
api: i,
|
|
233
|
-
storageCtx:
|
|
253
|
+
storageCtx: h
|
|
234
254
|
}) {
|
|
235
|
-
var
|
|
255
|
+
var n;
|
|
236
256
|
this.shouldCollectData = () => {
|
|
237
257
|
var t;
|
|
238
258
|
return !!(!((t = this.state.get().contact) != null && t.token) && this.config.collectUserData);
|
|
239
259
|
}, this.autoCreateUnverifiedUserIfNotExists = async () => {
|
|
240
|
-
var t, s, e, o,
|
|
260
|
+
var t, s, e, o, r, l, c, f, m, C, d, x;
|
|
241
261
|
if (!((t = this.config.user) != null && t.token)) {
|
|
242
262
|
if (this.config.collectUserData && !((e = (s = this.config.user) == null ? void 0 : s.data) != null && e.email)) {
|
|
243
263
|
if ((o = this.config.extraDataCollectionFields) != null && o.length)
|
|
244
264
|
return;
|
|
245
|
-
const
|
|
246
|
-
|
|
265
|
+
const S = await ((r = this.storageCtx) == null ? void 0 : r.getContactToken());
|
|
266
|
+
S && await this.setUnverifiedContact(S);
|
|
247
267
|
return;
|
|
248
268
|
}
|
|
249
|
-
if (!((
|
|
250
|
-
const
|
|
251
|
-
if (
|
|
252
|
-
await this.setUnverifiedContact(
|
|
269
|
+
if (!((c = (l = this.config.user) == null ? void 0 : l.data) != null && c.email)) {
|
|
270
|
+
const S = await ((f = this.storageCtx) == null ? void 0 : f.getContactToken());
|
|
271
|
+
if (S) {
|
|
272
|
+
await this.setUnverifiedContact(S);
|
|
253
273
|
return;
|
|
254
274
|
}
|
|
255
275
|
}
|
|
256
276
|
await this.createUnverifiedContact({
|
|
257
|
-
name: ((
|
|
258
|
-
email: (
|
|
277
|
+
name: ((C = (m = this.config.user) == null ? void 0 : m.data) == null ? void 0 : C.name) || "Anonymous",
|
|
278
|
+
email: (x = (d = this.config.user) == null ? void 0 : d.data) == null ? void 0 : x.email
|
|
259
279
|
});
|
|
260
280
|
}
|
|
261
281
|
}, this.createUnverifiedContact = async (t, s) => {
|
|
@@ -271,14 +291,14 @@ class q {
|
|
|
271
291
|
this.state.setPartial({ isCreatingUnverifiedContact: !1 });
|
|
272
292
|
}
|
|
273
293
|
}, this.setUnverifiedContact = async (t) => {
|
|
274
|
-
var o,
|
|
275
|
-
const s = await ((o = this.storageCtx) == null ? void 0 : o.getExternalContactId()), e = ((
|
|
276
|
-
this.api.setAuthToken(t), await ((
|
|
277
|
-
}, this.config =
|
|
278
|
-
contact: (
|
|
279
|
-
token:
|
|
294
|
+
var o, r, l, c;
|
|
295
|
+
const s = await ((o = this.storageCtx) == null ? void 0 : o.getExternalContactId()), e = ((r = this.config.user) == null ? void 0 : r.externalId) || s || A();
|
|
296
|
+
this.api.setAuthToken(t), await ((l = this.storageCtx) == null ? void 0 : l.setContactToken(t)), await ((c = this.storageCtx) == null ? void 0 : c.setExternalContactId(e)), this.state.setPartial({ contact: { token: t, externalId: e } });
|
|
297
|
+
}, this.config = a, this.storageCtx = h, this.api = i, this.state = new b({
|
|
298
|
+
contact: (n = a.user) != null && n.token ? {
|
|
299
|
+
token: a.user.token,
|
|
280
300
|
// Set optional externalId from config... not local storage
|
|
281
|
-
externalId:
|
|
301
|
+
externalId: a.user.externalId
|
|
282
302
|
} : null,
|
|
283
303
|
extraCollectedData: void 0,
|
|
284
304
|
isCreatingUnverifiedContact: !1,
|
|
@@ -286,19 +306,19 @@ class q {
|
|
|
286
306
|
}), this.autoCreateUnverifiedUserIfNotExists();
|
|
287
307
|
}
|
|
288
308
|
}
|
|
289
|
-
function
|
|
290
|
-
return
|
|
309
|
+
function P() {
|
|
310
|
+
return A();
|
|
291
311
|
}
|
|
292
|
-
class
|
|
312
|
+
class q {
|
|
293
313
|
constructor({
|
|
294
|
-
api:
|
|
314
|
+
api: a,
|
|
295
315
|
contactCtx: i,
|
|
296
|
-
sessionsPollingIntervalSeconds:
|
|
316
|
+
sessionsPollingIntervalSeconds: h
|
|
297
317
|
}) {
|
|
298
|
-
this.sessionsRefresher = new
|
|
318
|
+
this.sessionsRefresher = new T(), this.sessionState = new b({
|
|
299
319
|
session: null,
|
|
300
320
|
isCreatingSession: !1
|
|
301
|
-
}), this.sessionsState = new
|
|
321
|
+
}), this.sessionsState = new b({
|
|
302
322
|
data: [],
|
|
303
323
|
cursor: void 0,
|
|
304
324
|
isLastPage: !1,
|
|
@@ -310,9 +330,9 @@ class $ {
|
|
|
310
330
|
}), this.reset = async () => {
|
|
311
331
|
this.sessionState.reset();
|
|
312
332
|
}, this.registerSessionsRefresherWrapper = () => {
|
|
313
|
-
var
|
|
333
|
+
var n;
|
|
314
334
|
// If the widget config was initially provided with a contact token, no state change would be triggered, so we just fetch
|
|
315
|
-
(
|
|
335
|
+
(n = this.contactCtx.state.get().contact) != null && n.token && !this.sessionsState.get().didStartInitialFetch ? this.registerSessionsRefresher() : this.contactCtx.state.subscribe(({ contact: t }) => {
|
|
316
336
|
t != null && t.token && !this.sessionsState.get().didStartInitialFetch && this.registerSessionsRefresher();
|
|
317
337
|
});
|
|
318
338
|
}, this.registerSessionsRefresher = () => {
|
|
@@ -322,55 +342,55 @@ class $ {
|
|
|
322
342
|
}, this.createSession = async () => {
|
|
323
343
|
var e;
|
|
324
344
|
this.sessionState.setPartial({ session: null, isCreatingSession: !0 });
|
|
325
|
-
const
|
|
326
|
-
customData:
|
|
327
|
-
external_id:
|
|
345
|
+
const n = (e = this.contactCtx.state.get().contact) == null ? void 0 : e.externalId, { data: t, error: s } = await this.api.createSession({
|
|
346
|
+
customData: n ? {
|
|
347
|
+
external_id: n
|
|
328
348
|
} : void 0
|
|
329
349
|
});
|
|
330
350
|
return t ? (this.sessionState.setPartial({ session: t, isCreatingSession: !1 }), t) : (console.error("Failed to create session:", s), null);
|
|
331
351
|
}, this.loadMoreSessions = async () => {
|
|
332
352
|
if (this.sessionsState.get().isLastPage) return;
|
|
333
|
-
const { data:
|
|
353
|
+
const { data: n } = await this.getSessions({
|
|
334
354
|
cursor: this.sessionsState.get().cursor
|
|
335
355
|
});
|
|
336
|
-
if (
|
|
337
|
-
const s = [...this.sessionsState.get().data, ...
|
|
338
|
-
(e, o,
|
|
356
|
+
if (n) {
|
|
357
|
+
const s = [...this.sessionsState.get().data, ...n.items].filter(
|
|
358
|
+
(e, o, r) => o === r.findIndex((l) => e.id === l.id)
|
|
339
359
|
);
|
|
340
360
|
this.sessionsState.setPartial({
|
|
341
361
|
data: s,
|
|
342
|
-
cursor:
|
|
343
|
-
isLastPage:
|
|
362
|
+
cursor: n.next || void 0,
|
|
363
|
+
isLastPage: n.next === null
|
|
344
364
|
});
|
|
345
365
|
}
|
|
346
|
-
}, this.getSessions = async ({ cursor:
|
|
366
|
+
}, this.getSessions = async ({ cursor: n }) => {
|
|
347
367
|
var s, e;
|
|
348
368
|
if (!((s = this.contactCtx.state.get().contact) != null && s.token)) return { data: null };
|
|
349
369
|
const t = (e = this.contactCtx.state.get().contact) == null ? void 0 : e.externalId;
|
|
350
370
|
return await this.api.getSessions({
|
|
351
|
-
cursor:
|
|
371
|
+
cursor: n,
|
|
352
372
|
filters: t ? {
|
|
353
373
|
external_id: t
|
|
354
374
|
} : {}
|
|
355
375
|
});
|
|
356
376
|
}, this.refreshSessions = async () => {
|
|
357
|
-
const { data:
|
|
358
|
-
if (!
|
|
359
|
-
const t = [...
|
|
360
|
-
(s, e, o) => e === o.findIndex((
|
|
377
|
+
const { data: n } = await this.getSessions({ cursor: void 0 });
|
|
378
|
+
if (!n) return;
|
|
379
|
+
const t = [...n.items, ...this.sessionsState.get().data].filter(
|
|
380
|
+
(s, e, o) => e === o.findIndex((r) => s.id === r.id)
|
|
361
381
|
);
|
|
362
382
|
this.sessionsState.setPartial({ data: t });
|
|
363
|
-
}, this.api =
|
|
383
|
+
}, this.api = a, this.contactCtx = i, this.sessionsPollingIntervalSeconds = h, this.registerSessionsRefresherWrapper();
|
|
364
384
|
}
|
|
365
385
|
}
|
|
366
|
-
class
|
|
386
|
+
class $ {
|
|
367
387
|
constructor({
|
|
368
|
-
config:
|
|
388
|
+
config: a,
|
|
369
389
|
api: i,
|
|
370
|
-
sessionCtx:
|
|
371
|
-
contactCtx:
|
|
390
|
+
sessionCtx: h,
|
|
391
|
+
contactCtx: n
|
|
372
392
|
}) {
|
|
373
|
-
this.state = new
|
|
393
|
+
this.state = new b({
|
|
374
394
|
messages: [],
|
|
375
395
|
isSendingMessage: !1,
|
|
376
396
|
lastAIResMightSolveUserIssue: !1,
|
|
@@ -378,14 +398,14 @@ class j {
|
|
|
378
398
|
}), this.sendMessageAbortController = new AbortController(), this.reset = () => {
|
|
379
399
|
this.sendMessageAbortController.abort("Resetting chat"), this.state.reset();
|
|
380
400
|
}, this.sendMessage = async (t) => {
|
|
381
|
-
var
|
|
401
|
+
var l, c, f, m, C, d, x, S, k;
|
|
382
402
|
if (!t.content.trim() && (!t.attachments || t.attachments.length === 0)) {
|
|
383
403
|
console.warn("Cannot send an empty message of no content or attachments");
|
|
384
404
|
return;
|
|
385
405
|
}
|
|
386
|
-
const s = this.state.get().isSendingMessage, e = ((
|
|
406
|
+
const s = this.state.get().isSendingMessage, e = ((l = this.sessionCtx.sessionState.get().session) == null ? void 0 : l.assignee.kind) === "ai", o = this.state.get().messages, r = o.length > 0 ? o[o.length - 1] : void 0;
|
|
387
407
|
if (e && s || // If last message is from user, then bot response did not arrive yet
|
|
388
|
-
e && (
|
|
408
|
+
e && (r == null ? void 0 : r.type) === "FROM_USER") {
|
|
389
409
|
console.warn("Cannot send messages while awaiting AI response");
|
|
390
410
|
return;
|
|
391
411
|
}
|
|
@@ -395,10 +415,10 @@ class j {
|
|
|
395
415
|
const w = this.toUserMessage(
|
|
396
416
|
t.content.trim(),
|
|
397
417
|
t.attachments || void 0
|
|
398
|
-
),
|
|
418
|
+
), M = this.state.get().messages;
|
|
399
419
|
if (this.state.setPartial({
|
|
400
|
-
messages: [...
|
|
401
|
-
}), !((
|
|
420
|
+
messages: [...M, w]
|
|
421
|
+
}), !((c = this.sessionCtx.sessionState.get().session) != null && c.id)) {
|
|
402
422
|
if (!await this.sessionCtx.createSession()) {
|
|
403
423
|
console.error("Failed to create session");
|
|
404
424
|
return;
|
|
@@ -407,42 +427,43 @@ class j {
|
|
|
407
427
|
}
|
|
408
428
|
const y = (f = this.sessionCtx.sessionState.get().session) == null ? void 0 : f.id;
|
|
409
429
|
if (!y) return;
|
|
410
|
-
const { data:
|
|
430
|
+
const { data: u } = await this.api.sendMessage(
|
|
411
431
|
{
|
|
412
432
|
uuid: w.id,
|
|
413
433
|
bot_token: this.config.token,
|
|
414
434
|
headers: this.config.headers,
|
|
415
435
|
query_params: this.config.queryParams,
|
|
416
436
|
session_id: y,
|
|
417
|
-
user: (
|
|
437
|
+
user: (m = this.config.user) == null ? void 0 : m.data,
|
|
418
438
|
content: w.content,
|
|
419
|
-
attachments: t.attachments
|
|
439
|
+
attachments: t.attachments,
|
|
440
|
+
clientContext: this.config.context
|
|
420
441
|
},
|
|
421
442
|
this.sendMessageAbortController.signal
|
|
422
443
|
);
|
|
423
|
-
if (
|
|
424
|
-
const
|
|
425
|
-
if (
|
|
426
|
-
const
|
|
427
|
-
if (!!
|
|
428
|
-
(
|
|
444
|
+
if (u != null && u.success) {
|
|
445
|
+
const v = this.toBotMessage(u);
|
|
446
|
+
if (v) {
|
|
447
|
+
const I = this.state.get().messages;
|
|
448
|
+
if (!!I.some(
|
|
449
|
+
(R) => R.id === v.id
|
|
429
450
|
)) {
|
|
430
451
|
this.state.setPartial({
|
|
431
|
-
lastAIResMightSolveUserIssue: ((
|
|
452
|
+
lastAIResMightSolveUserIssue: ((C = u.autopilotResponse) == null ? void 0 : C.mightSolveUserIssue) || ((d = u.uiResponse) == null ? void 0 : d.mightSolveUserIssue)
|
|
432
453
|
});
|
|
433
454
|
return;
|
|
434
455
|
}
|
|
435
456
|
this.state.setPartial({
|
|
436
|
-
messages: [...
|
|
437
|
-
lastAIResMightSolveUserIssue: ((
|
|
457
|
+
messages: [...I, v],
|
|
458
|
+
lastAIResMightSolveUserIssue: ((x = u.autopilotResponse) == null ? void 0 : x.mightSolveUserIssue) || ((S = u.uiResponse) == null ? void 0 : S.mightSolveUserIssue)
|
|
438
459
|
});
|
|
439
460
|
}
|
|
440
461
|
} else {
|
|
441
|
-
const
|
|
442
|
-
((
|
|
443
|
-
),
|
|
462
|
+
const v = this.toBotErrorMessage(
|
|
463
|
+
((k = u == null ? void 0 : u.error) == null ? void 0 : k.message) || "Unknown error occurred"
|
|
464
|
+
), I = this.state.get().messages;
|
|
444
465
|
this.state.setPartial({
|
|
445
|
-
messages: [...
|
|
466
|
+
messages: [...I, v]
|
|
446
467
|
});
|
|
447
468
|
}
|
|
448
469
|
} catch (w) {
|
|
@@ -453,13 +474,13 @@ class j {
|
|
|
453
474
|
}, this.toUserMessage = (t, s) => {
|
|
454
475
|
const e = (() => {
|
|
455
476
|
const o = this.contactCtx.state.get().extraCollectedData;
|
|
456
|
-
return this.state.get().messages.length === 0 && o && Object.keys(o).length > 0 ? `${Object.entries(o).filter(([
|
|
477
|
+
return this.state.get().messages.length === 0 && o && Object.keys(o).length > 0 ? `${Object.entries(o).filter(([l, c]) => !!c).map(([l, c]) => `${l}: ${c}`).join(`
|
|
457
478
|
`)}
|
|
458
479
|
|
|
459
480
|
${t}` : t;
|
|
460
481
|
})();
|
|
461
482
|
return {
|
|
462
|
-
id:
|
|
483
|
+
id: P(),
|
|
463
484
|
type: "FROM_USER",
|
|
464
485
|
content: e,
|
|
465
486
|
deliveredAt: (/* @__PURE__ */ new Date()).toISOString(),
|
|
@@ -470,7 +491,7 @@ ${t}` : t;
|
|
|
470
491
|
var s;
|
|
471
492
|
return t.success && t.autopilotResponse ? {
|
|
472
493
|
type: "FROM_BOT",
|
|
473
|
-
id: t.autopilotResponse.id ||
|
|
494
|
+
id: t.autopilotResponse.id || P(),
|
|
474
495
|
timestamp: (/* @__PURE__ */ new Date()).toISOString(),
|
|
475
496
|
component: "bot_message",
|
|
476
497
|
agent: this.config.bot ? {
|
|
@@ -489,7 +510,7 @@ ${t}` : t;
|
|
|
489
510
|
} : null;
|
|
490
511
|
}, this.toBotErrorMessage = (t) => ({
|
|
491
512
|
type: "FROM_BOT",
|
|
492
|
-
id:
|
|
513
|
+
id: P(),
|
|
493
514
|
timestamp: (/* @__PURE__ */ new Date()).toISOString(),
|
|
494
515
|
component: "TEXT",
|
|
495
516
|
data: {
|
|
@@ -497,15 +518,15 @@ ${t}` : t;
|
|
|
497
518
|
variant: "error",
|
|
498
519
|
action: void 0
|
|
499
520
|
}
|
|
500
|
-
}), this.config =
|
|
521
|
+
}), this.config = a, this.api = i, this.sessionCtx = h, this.contactCtx = n;
|
|
501
522
|
}
|
|
502
523
|
}
|
|
503
524
|
class N {
|
|
504
525
|
constructor({
|
|
505
|
-
config:
|
|
526
|
+
config: a,
|
|
506
527
|
contactCtx: i,
|
|
507
|
-
sessionCtx:
|
|
508
|
-
resetChat:
|
|
528
|
+
sessionCtx: h,
|
|
529
|
+
resetChat: n
|
|
509
530
|
}) {
|
|
510
531
|
this.registerRoutingListener = () => {
|
|
511
532
|
this.contactCtx.state.subscribe(({ contact: t }) => {
|
|
@@ -525,13 +546,13 @@ class N {
|
|
|
525
546
|
this.sessionCtx.sessionState.setPartial({ session: s });
|
|
526
547
|
}
|
|
527
548
|
this.state.setPartial({ screen: "chat" });
|
|
528
|
-
}, this.state = new
|
|
549
|
+
}, this.state = new b({
|
|
529
550
|
screen: i.shouldCollectData() ? "welcome" : "sessions"
|
|
530
|
-
}), this.config =
|
|
551
|
+
}), this.config = a, this.contactCtx = i, this.sessionCtx = h, this.resetChat = n, this.registerRoutingListener();
|
|
531
552
|
}
|
|
532
553
|
}
|
|
533
|
-
class
|
|
534
|
-
constructor({ storage:
|
|
554
|
+
class H {
|
|
555
|
+
constructor({ storage: a }) {
|
|
535
556
|
this.KEYS = {
|
|
536
557
|
contactToken: "opencx__widget__contactToken",
|
|
537
558
|
externalContactId: "opencx__widget__externalContactId"
|
|
@@ -539,39 +560,39 @@ class G {
|
|
|
539
560
|
await this.storage.set(this.KEYS.contactToken, i);
|
|
540
561
|
}, this.getContactToken = async () => this.storage.get(this.KEYS.contactToken), this.setExternalContactId = async (i) => {
|
|
541
562
|
await this.storage.set(this.KEYS.externalContactId, i);
|
|
542
|
-
}, this.getExternalContactId = async () => this.storage.get(this.KEYS.externalContactId), this.storage =
|
|
563
|
+
}, this.getExternalContactId = async () => this.storage.get(this.KEYS.externalContactId), this.storage = a;
|
|
543
564
|
}
|
|
544
565
|
}
|
|
545
|
-
const
|
|
566
|
+
const p = class p {
|
|
546
567
|
constructor({
|
|
547
|
-
config:
|
|
568
|
+
config: a,
|
|
548
569
|
storage: i
|
|
549
570
|
}) {
|
|
550
571
|
if (this.resetChat = () => {
|
|
551
572
|
this.sessionCtx.reset(), this.messageCtx.reset();
|
|
552
|
-
}, !
|
|
573
|
+
}, !p.pollingIntervalsSeconds)
|
|
553
574
|
throw Error(
|
|
554
575
|
"Widget polling values are not defined, did you call WidgetCtx.initialize()"
|
|
555
576
|
);
|
|
556
|
-
this.config =
|
|
577
|
+
this.config = a, this.api = new E({ config: a }), this.storageCtx = i ? new H({ storage: i }) : void 0, this.contactCtx = new B({
|
|
557
578
|
api: this.api,
|
|
558
579
|
config: this.config,
|
|
559
580
|
storageCtx: this.storageCtx
|
|
560
|
-
}), this.sessionCtx = new
|
|
581
|
+
}), this.sessionCtx = new q({
|
|
561
582
|
api: this.api,
|
|
562
583
|
contactCtx: this.contactCtx,
|
|
563
|
-
sessionsPollingIntervalSeconds:
|
|
564
|
-
}), this.messageCtx = new
|
|
584
|
+
sessionsPollingIntervalSeconds: p.pollingIntervalsSeconds.sessions
|
|
585
|
+
}), this.messageCtx = new $({
|
|
565
586
|
config: this.config,
|
|
566
587
|
api: this.api,
|
|
567
588
|
sessionCtx: this.sessionCtx,
|
|
568
589
|
contactCtx: this.contactCtx
|
|
569
|
-
}), this.activeSessionPollingCtx = new
|
|
590
|
+
}), this.activeSessionPollingCtx = new _({
|
|
570
591
|
api: this.api,
|
|
571
592
|
config: this.config,
|
|
572
593
|
sessionCtx: this.sessionCtx,
|
|
573
594
|
messageCtx: this.messageCtx,
|
|
574
|
-
sessionPollingIntervalSeconds:
|
|
595
|
+
sessionPollingIntervalSeconds: p.pollingIntervalsSeconds.session
|
|
575
596
|
}), this.routerCtx = new N({
|
|
576
597
|
config: this.config,
|
|
577
598
|
contactCtx: this.contactCtx,
|
|
@@ -580,25 +601,25 @@ const u = class u {
|
|
|
580
601
|
});
|
|
581
602
|
}
|
|
582
603
|
};
|
|
583
|
-
|
|
584
|
-
config:
|
|
604
|
+
p.pollingIntervalsSeconds = null, p.initialize = async ({
|
|
605
|
+
config: a,
|
|
585
606
|
storage: i
|
|
586
607
|
}) => {
|
|
587
|
-
var
|
|
588
|
-
const
|
|
589
|
-
config:
|
|
608
|
+
var n, t;
|
|
609
|
+
const h = await new E({
|
|
610
|
+
config: a
|
|
590
611
|
}).getExternalWidgetConfig();
|
|
591
|
-
return
|
|
592
|
-
session: ((
|
|
593
|
-
sessions: ((t =
|
|
594
|
-
}, new
|
|
595
|
-
config:
|
|
612
|
+
return p.pollingIntervalsSeconds = {
|
|
613
|
+
session: ((n = h.data) == null ? void 0 : n.sessionPollingIntervalSeconds) || 10,
|
|
614
|
+
sessions: ((t = h.data) == null ? void 0 : t.sessionsPollingIntervalSeconds) || 60
|
|
615
|
+
}, new p({
|
|
616
|
+
config: a,
|
|
596
617
|
storage: i
|
|
597
618
|
});
|
|
598
619
|
};
|
|
599
|
-
let
|
|
620
|
+
let U = p;
|
|
600
621
|
export {
|
|
601
|
-
|
|
602
|
-
|
|
622
|
+
b as P,
|
|
623
|
+
U as W
|
|
603
624
|
};
|
|
604
|
-
//# sourceMappingURL=widget.ctx-
|
|
625
|
+
//# sourceMappingURL=widget.ctx-CMgNQpCm.js.map
|