@origonai/web-chat-sdk 0.1.4 → 0.1.6
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/origon-chat-sdk.js +224 -235
- package/dist/origon-chat-sdk.js.map +1 -1
- package/package.json +2 -2
- package/src/chat.js +22 -28
- package/src/http.js +15 -1
- package/src/utils.js +13 -6
package/dist/origon-chat-sdk.js
CHANGED
|
@@ -2,7 +2,7 @@ import { fetchEventSource as ne } from "@microsoft/fetch-event-source";
|
|
|
2
2
|
function J() {
|
|
3
3
|
const e = Date.now(), t = new Uint8Array(16);
|
|
4
4
|
crypto.getRandomValues(t), t[0] = e >> 40 & 255, t[1] = e >> 32 & 255, t[2] = e >> 24 & 255, t[3] = e >> 16 & 255, t[4] = e >> 8 & 255, t[5] = e & 255, t[6] = t[6] & 15 | 112, t[8] = t[8] & 63 | 128;
|
|
5
|
-
const o = [...t].map((
|
|
5
|
+
const o = [...t].map((r) => r.toString(16).padStart(2, "0")).join("");
|
|
6
6
|
return `${o.slice(0, 8)}-${o.slice(8, 12)}-${o.slice(12, 16)}-${o.slice(
|
|
7
7
|
16,
|
|
8
8
|
20
|
|
@@ -20,20 +20,27 @@ async function oe(e) {
|
|
|
20
20
|
function se(e) {
|
|
21
21
|
let t;
|
|
22
22
|
try {
|
|
23
|
-
const o = new URL(e)
|
|
24
|
-
t = `
|
|
23
|
+
const o = new URL(e);
|
|
24
|
+
t = `https://${o.hostname}${o.pathname}/sse`;
|
|
25
25
|
} catch (o) {
|
|
26
|
-
console.error("Invalid base URL: ", e);
|
|
27
26
|
}
|
|
28
27
|
return t;
|
|
29
28
|
}
|
|
30
|
-
function
|
|
29
|
+
function ae(e) {
|
|
30
|
+
let t;
|
|
31
|
+
try {
|
|
32
|
+
const o = new URL(e);
|
|
33
|
+
t = `wss://${o.hostname}${o.pathname}/wss`;
|
|
34
|
+
} catch (o) {
|
|
35
|
+
}
|
|
36
|
+
return t;
|
|
37
|
+
}
|
|
38
|
+
function re(e) {
|
|
31
39
|
let t;
|
|
32
40
|
try {
|
|
33
41
|
const o = new URL(e);
|
|
34
42
|
t = `wss://${o.hostname}${o.pathname}/audio`;
|
|
35
43
|
} catch (o) {
|
|
36
|
-
console.error("getCallServerEndpoint: Invalid base URL: ", e);
|
|
37
44
|
}
|
|
38
45
|
return t;
|
|
39
46
|
}
|
|
@@ -46,7 +53,7 @@ const T = {
|
|
|
46
53
|
// this is human agent (dock side)
|
|
47
54
|
SYSTEM: "system"
|
|
48
55
|
// this is system message, for ex "Agent joined" / "Agent left"
|
|
49
|
-
}, W = 1e4,
|
|
56
|
+
}, W = 1e4, ce = 5e3, E = {
|
|
50
57
|
MESSAGE: "message",
|
|
51
58
|
TYPING: "typing",
|
|
52
59
|
TYPING_STOP: "typingOff",
|
|
@@ -62,37 +69,36 @@ function ie() {
|
|
|
62
69
|
socketConnectionTimeout: null
|
|
63
70
|
};
|
|
64
71
|
}
|
|
65
|
-
let
|
|
66
|
-
function
|
|
67
|
-
|
|
72
|
+
let a = ie();
|
|
73
|
+
function A() {
|
|
74
|
+
a.pingInterval && (clearInterval(a.pingInterval), a.pingInterval = null);
|
|
68
75
|
}
|
|
69
|
-
function
|
|
70
|
-
|
|
71
|
-
|
|
76
|
+
function le() {
|
|
77
|
+
A(), a.pingInterval = setInterval(() => {
|
|
78
|
+
a.socket && a.socket.readyState === WebSocket.OPEN ? U({ type: "ping" }) : A();
|
|
72
79
|
}, W);
|
|
73
80
|
}
|
|
74
81
|
function _() {
|
|
75
|
-
|
|
82
|
+
A(), a.socketDisconnectedTimeout && (clearTimeout(a.socketDisconnectedTimeout), a.socketDisconnectedTimeout = null), a.socketConnectionTimeout && (clearTimeout(a.socketConnectionTimeout), a.socketConnectionTimeout = null);
|
|
76
83
|
}
|
|
77
84
|
function K() {
|
|
78
|
-
|
|
85
|
+
a.socketDisconnected = !1, P("socket");
|
|
79
86
|
}
|
|
80
87
|
function B() {
|
|
81
|
-
|
|
88
|
+
a.socketDisconnected = !0, P("sse");
|
|
82
89
|
}
|
|
83
|
-
function
|
|
90
|
+
function de(e) {
|
|
84
91
|
return new Promise((t, o) => {
|
|
85
|
-
if (
|
|
86
|
-
|
|
92
|
+
if (a.socket && (a.socket.readyState === WebSocket.CONNECTING || a.socket.readyState === WebSocket.OPEN)) {
|
|
93
|
+
t(a.socket.readyState === WebSocket.OPEN);
|
|
87
94
|
return;
|
|
88
95
|
}
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
if (!a || !a.endpoint) {
|
|
96
|
+
const r = N();
|
|
97
|
+
if (!r || !r.endpoint) {
|
|
92
98
|
o(new Error("SDK not initialized. Please initialize SDK first."));
|
|
93
99
|
return;
|
|
94
100
|
}
|
|
95
|
-
const d =
|
|
101
|
+
const d = ae(r.endpoint);
|
|
96
102
|
if (!d) {
|
|
97
103
|
o(
|
|
98
104
|
new Error(
|
|
@@ -101,53 +107,53 @@ function le(e) {
|
|
|
101
107
|
);
|
|
102
108
|
return;
|
|
103
109
|
}
|
|
104
|
-
const u =
|
|
110
|
+
const u = $(), c = new URLSearchParams({
|
|
105
111
|
externalId: u
|
|
106
112
|
});
|
|
107
|
-
e.sessionId &&
|
|
108
|
-
const f = `${d}?${
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
},
|
|
112
|
-
const p = JSON.parse(
|
|
113
|
-
|
|
114
|
-
},
|
|
115
|
-
|
|
116
|
-
},
|
|
117
|
-
|
|
113
|
+
e.sessionId && c.set("sessionId", e.sessionId), e.requestId && c.set("requestId", e.requestId), r.token && c.set("token", r.token);
|
|
114
|
+
const f = `${d}?${c.toString()}`;
|
|
115
|
+
a.socket = new WebSocket(f), a.socket.onopen = () => {
|
|
116
|
+
a.previouslyConnected = !0, K(), U({ type: "ping" }), clearTimeout(a.socketConnectionTimeout), le(), t(!0);
|
|
117
|
+
}, a.socket.onmessage = (l) => {
|
|
118
|
+
const p = JSON.parse(l.data);
|
|
119
|
+
ue(p);
|
|
120
|
+
}, a.socket.onerror = (l) => {
|
|
121
|
+
P("sse"), o(l);
|
|
122
|
+
}, a.socket.onclose = (l) => {
|
|
123
|
+
l.target === a.socket && (l.code === 1006 && (a.previouslyConnected ? B() : h({
|
|
118
124
|
errorText: "Unable to establish connection",
|
|
119
125
|
done: !0,
|
|
120
126
|
timestamp: (/* @__PURE__ */ new Date()).toISOString()
|
|
121
|
-
}), clearTimeout(
|
|
122
|
-
},
|
|
123
|
-
|
|
127
|
+
}), clearTimeout(a.socketConnectionTimeout)), a.socket = null, _());
|
|
128
|
+
}, a.previouslyConnected || (a.socketConnectionTimeout = setTimeout(() => {
|
|
129
|
+
h({
|
|
124
130
|
errorText: "Unable to establish connection",
|
|
125
131
|
done: !0,
|
|
126
132
|
timestamp: (/* @__PURE__ */ new Date()).toISOString()
|
|
127
133
|
}), o(new Error("Socket connection timed out"));
|
|
128
|
-
},
|
|
134
|
+
}, ce));
|
|
129
135
|
});
|
|
130
136
|
}
|
|
131
|
-
function
|
|
132
|
-
|
|
137
|
+
function U(e) {
|
|
138
|
+
a.socketDisconnected || !a.socket || a.socket.send(JSON.stringify({ ...e, eventId: e.eventId || J() }));
|
|
133
139
|
}
|
|
134
|
-
function
|
|
135
|
-
switch (
|
|
140
|
+
function ue(e) {
|
|
141
|
+
switch (e.type) {
|
|
136
142
|
case "pong": {
|
|
137
|
-
|
|
138
|
-
|
|
143
|
+
a.socketDisconnected && K(), a.socketDisconnectedTimeout && clearTimeout(a.socketDisconnectedTimeout), a.socketDisconnectedTimeout = setTimeout(() => {
|
|
144
|
+
B();
|
|
139
145
|
}, W + 1e3);
|
|
140
146
|
break;
|
|
141
147
|
}
|
|
142
|
-
case
|
|
148
|
+
case E.TYPING: {
|
|
143
149
|
F(!0);
|
|
144
150
|
break;
|
|
145
151
|
}
|
|
146
|
-
case
|
|
152
|
+
case E.TYPING_STOP: {
|
|
147
153
|
F(!1);
|
|
148
154
|
break;
|
|
149
155
|
}
|
|
150
|
-
case
|
|
156
|
+
case E.MESSAGE: {
|
|
151
157
|
const { eventId: t, data: o } = e;
|
|
152
158
|
t || h({
|
|
153
159
|
...o,
|
|
@@ -156,17 +162,17 @@ function de(e) {
|
|
|
156
162
|
});
|
|
157
163
|
break;
|
|
158
164
|
}
|
|
159
|
-
case
|
|
165
|
+
case E.END: {
|
|
160
166
|
H();
|
|
161
167
|
break;
|
|
162
168
|
}
|
|
163
169
|
}
|
|
164
170
|
}
|
|
165
171
|
function H() {
|
|
166
|
-
|
|
172
|
+
a.socket && a.socket.close(1e3), a.previouslyConnected = !1, _(), a.socket = null, P("sse");
|
|
167
173
|
}
|
|
168
|
-
function
|
|
169
|
-
return
|
|
174
|
+
function ge() {
|
|
175
|
+
return a.socket !== null && a.socket.readyState === WebSocket.OPEN && !a.socketDisconnected;
|
|
170
176
|
}
|
|
171
177
|
function Y(e = {}) {
|
|
172
178
|
return {
|
|
@@ -187,43 +193,32 @@ let s = Y();
|
|
|
187
193
|
function Pe(e) {
|
|
188
194
|
s.callbacks = { ...s.callbacks, ...e };
|
|
189
195
|
}
|
|
190
|
-
function
|
|
191
|
-
|
|
196
|
+
function Oe(e) {
|
|
197
|
+
s.credentials = e, e.token && (s.authenticated = !0);
|
|
192
198
|
}
|
|
193
|
-
function
|
|
199
|
+
function N() {
|
|
194
200
|
return s.credentials;
|
|
195
201
|
}
|
|
196
|
-
function
|
|
202
|
+
function fe(e) {
|
|
197
203
|
var t, o;
|
|
198
204
|
e && e !== s.sessionId && (s.sessionId = e, (o = (t = s.callbacks).onSessionUpdate) == null || o.call(t, e));
|
|
199
205
|
}
|
|
200
|
-
async function
|
|
201
|
-
var t;
|
|
206
|
+
async function Re(e = {}) {
|
|
202
207
|
try {
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
text: i.text,
|
|
212
|
-
role: i.youtubeVideo ? T.BOT : i.role,
|
|
213
|
-
timestamp: i.timestamp,
|
|
214
|
-
video: i.youtubeVideo,
|
|
215
|
-
channel: i.channel,
|
|
216
|
-
done: !0
|
|
217
|
-
}));
|
|
218
|
-
}
|
|
219
|
-
const d = new URLSearchParams();
|
|
220
|
-
return s.credentials.token || d.set("externalId", P()), s.sseUrl = `${s.credentials.endpoint}?${d.toString()}`, s.sessionId = e.sessionId, s.messages = a, console.log("Chat initiated successfully"), {
|
|
208
|
+
let t = null;
|
|
209
|
+
s.authenticated ? t = s.configData : (t = await ke(s.credentials), s.authenticated = !0, s.configData = t);
|
|
210
|
+
let o = [];
|
|
211
|
+
e.sessionId && (o = await Se(e.sessionId));
|
|
212
|
+
const r = new URLSearchParams();
|
|
213
|
+
return s.credentials.token || r.set("externalId", $()), s.sseUrl = `${se(
|
|
214
|
+
s.credentials.endpoint
|
|
215
|
+
)}?${r.toString()}`, s.sessionId = e.sessionId, s.messages = o, {
|
|
221
216
|
sessionId: s.sessionId,
|
|
222
|
-
messages:
|
|
223
|
-
configData:
|
|
217
|
+
messages: o,
|
|
218
|
+
configData: t
|
|
224
219
|
};
|
|
225
|
-
} catch (
|
|
226
|
-
throw
|
|
220
|
+
} catch (t) {
|
|
221
|
+
throw V(), t;
|
|
227
222
|
}
|
|
228
223
|
}
|
|
229
224
|
function Ae() {
|
|
@@ -232,9 +227,9 @@ function Ae() {
|
|
|
232
227
|
function V() {
|
|
233
228
|
s.abortController && s.abortController.abort(), H();
|
|
234
229
|
const { callbacks: e, credentials: t } = s;
|
|
235
|
-
s = Y(e), s.credentials = t
|
|
230
|
+
s = Y(e), s.credentials = t;
|
|
236
231
|
}
|
|
237
|
-
function
|
|
232
|
+
function $() {
|
|
238
233
|
var e;
|
|
239
234
|
return (e = s.credentials) != null && e.externalId ? s.credentials.externalId : te();
|
|
240
235
|
}
|
|
@@ -246,29 +241,29 @@ function F(e) {
|
|
|
246
241
|
var t, o;
|
|
247
242
|
(o = (t = s.callbacks).onTyping) == null || o.call(t, e);
|
|
248
243
|
}
|
|
249
|
-
function
|
|
244
|
+
function P(e) {
|
|
250
245
|
var t, o;
|
|
251
|
-
|
|
246
|
+
s.transport = e, (o = (t = s.callbacks).onTransportUpdate) == null || o.call(t, e);
|
|
252
247
|
}
|
|
253
|
-
function
|
|
254
|
-
return new Promise((
|
|
248
|
+
function Ue({ text: e, html: t, context: o }) {
|
|
249
|
+
return new Promise((r, d) => {
|
|
255
250
|
(async () => {
|
|
256
|
-
var u,
|
|
251
|
+
var u, c, f;
|
|
257
252
|
try {
|
|
258
|
-
const
|
|
253
|
+
const l = {
|
|
259
254
|
role: T.USER,
|
|
260
255
|
text: e,
|
|
261
256
|
html: t,
|
|
262
257
|
timestamp: (/* @__PURE__ */ new Date()).toISOString()
|
|
263
258
|
};
|
|
264
|
-
if (h(
|
|
265
|
-
|
|
259
|
+
if (h(l), await oe(200), s.transport === "socket" && ge()) {
|
|
260
|
+
U({
|
|
266
261
|
type: "message",
|
|
267
262
|
data: {
|
|
268
263
|
text: e,
|
|
269
264
|
html: t
|
|
270
265
|
}
|
|
271
|
-
}),
|
|
266
|
+
}), r(s.sessionId);
|
|
272
267
|
return;
|
|
273
268
|
}
|
|
274
269
|
const p = {
|
|
@@ -293,24 +288,33 @@ function Ne({ text: e, html: t, context: o }) {
|
|
|
293
288
|
signal: s.abortController.signal,
|
|
294
289
|
onopen: async (g) => {
|
|
295
290
|
if (!g.ok)
|
|
296
|
-
throw
|
|
291
|
+
throw new Error("Failed to send message");
|
|
297
292
|
},
|
|
298
293
|
onmessage: (g) => {
|
|
299
|
-
var
|
|
300
|
-
const
|
|
294
|
+
var b, M, x, q, L, G, z;
|
|
295
|
+
const i = JSON.parse(g.data);
|
|
301
296
|
if (g.event === "connected")
|
|
302
|
-
s.sessionId =
|
|
297
|
+
s.sessionId = i.sessionId, s.requestId = i.requestId;
|
|
303
298
|
else if (g.event === "upgrade_to_websocket")
|
|
304
|
-
|
|
299
|
+
de({
|
|
305
300
|
sessionId: s.sessionId,
|
|
306
|
-
requestId:
|
|
301
|
+
requestId: i.requestId
|
|
307
302
|
});
|
|
308
|
-
else if (
|
|
309
|
-
|
|
303
|
+
else if (i.error) {
|
|
304
|
+
const I = i.error && typeof i.error == "string" ? i.error : "Failed to connect to the system", k = s.messages.length - 1, C = {
|
|
305
|
+
...s.messages[k],
|
|
306
|
+
loading: !1,
|
|
307
|
+
errorText: I
|
|
308
|
+
};
|
|
309
|
+
s.messages = s.messages.map(
|
|
310
|
+
(R, ee) => ee === k ? C : R
|
|
311
|
+
), (M = (b = s.callbacks).onMessageUpdate) == null || M.call(b, k, C), d(new Error(I));
|
|
312
|
+
} else if (i.message !== void 0) {
|
|
313
|
+
if (i.streamId !== void 0) {
|
|
310
314
|
if (s.lastStreamId === void 0)
|
|
311
|
-
s.lastStreamId =
|
|
312
|
-
else if (
|
|
313
|
-
s.lastStreamId =
|
|
315
|
+
s.lastStreamId = i.streamId;
|
|
316
|
+
else if (i.streamId !== s.lastStreamId) {
|
|
317
|
+
s.lastStreamId = i.streamId;
|
|
314
318
|
const C = {
|
|
315
319
|
role: T.BOT,
|
|
316
320
|
text: "",
|
|
@@ -322,22 +326,13 @@ function Ne({ text: e, html: t, context: o }) {
|
|
|
322
326
|
const I = s.messages.length - 1, k = s.messages[I], O = {
|
|
323
327
|
...k,
|
|
324
328
|
loading: !1,
|
|
325
|
-
text: (k.text || "") +
|
|
326
|
-
sources:
|
|
327
|
-
done: (
|
|
328
|
-
};
|
|
329
|
-
s.messages = s.messages.map(
|
|
330
|
-
(C, A) => A === I ? O : C
|
|
331
|
-
), (x = (M = s.callbacks).onMessageUpdate) == null || x.call(M, I, O), l.done && a(s.sessionId), s.sessionId = (q = l.session_id) != null ? q : s.sessionId, s.requestId = (G = l.requestId) != null ? G : s.requestId;
|
|
332
|
-
} else if (l.error) {
|
|
333
|
-
const I = "Failed to connect to the system", k = s.messages.length - 1, C = {
|
|
334
|
-
...s.messages[k],
|
|
335
|
-
loading: !1,
|
|
336
|
-
errorText: I
|
|
329
|
+
text: (k.text || "") + i.message,
|
|
330
|
+
sources: i.sources,
|
|
331
|
+
done: (x = i.done) != null ? x : k.done
|
|
337
332
|
};
|
|
338
333
|
s.messages = s.messages.map(
|
|
339
|
-
(
|
|
340
|
-
), (
|
|
334
|
+
(C, R) => R === I ? O : C
|
|
335
|
+
), (L = (q = s.callbacks).onMessageUpdate) == null || L.call(q, I, O), i.done && r(s.sessionId), s.sessionId = (G = i.session_id) != null ? G : s.sessionId, s.requestId = (z = i.requestId) != null ? z : s.requestId;
|
|
341
336
|
}
|
|
342
337
|
},
|
|
343
338
|
onerror: (g) => {
|
|
@@ -345,65 +340,74 @@ function Ne({ text: e, html: t, context: o }) {
|
|
|
345
340
|
},
|
|
346
341
|
openWhenHidden: !0
|
|
347
342
|
});
|
|
348
|
-
} catch (
|
|
349
|
-
console.error("Failed to send message: ", r);
|
|
343
|
+
} catch (l) {
|
|
350
344
|
const p = "Failed to connect to the system", m = s.messages.length - 1, y = s.messages[m], g = {
|
|
351
345
|
...y,
|
|
352
346
|
loading: !1,
|
|
353
|
-
errorText: y.done ? void 0 :
|
|
347
|
+
errorText: y.done ? void 0 : l.message || p,
|
|
354
348
|
done: !0
|
|
355
349
|
};
|
|
356
350
|
s.messages = s.messages.map(
|
|
357
|
-
(
|
|
358
|
-
), (f = (
|
|
351
|
+
(i, b) => b === m ? g : i
|
|
352
|
+
), (f = (c = s.callbacks).onMessageUpdate) == null || f.call(c, m, g), d(l);
|
|
359
353
|
}
|
|
360
354
|
})();
|
|
361
355
|
});
|
|
362
356
|
}
|
|
363
|
-
const
|
|
364
|
-
async function
|
|
365
|
-
const { endpoint: t } = e, o = `${t}/config`,
|
|
357
|
+
const pe = "Something went wrong initializing the chat", me = "Chat SDK not initialized";
|
|
358
|
+
async function ke(e) {
|
|
359
|
+
const { endpoint: t } = e, o = `${t}/config`, r = await fetch(o, {
|
|
366
360
|
method: "GET",
|
|
367
361
|
headers: {
|
|
368
362
|
"Content-Type": "application/json"
|
|
369
363
|
}
|
|
370
364
|
});
|
|
371
|
-
if (!
|
|
372
|
-
const
|
|
373
|
-
throw new Error((
|
|
365
|
+
if (!r.ok) {
|
|
366
|
+
const c = await r.json();
|
|
367
|
+
throw new Error((c == null ? void 0 : c.error) || pe);
|
|
374
368
|
}
|
|
375
|
-
return (await
|
|
369
|
+
return (await r.json()).data;
|
|
376
370
|
}
|
|
377
|
-
async function
|
|
371
|
+
async function Ne() {
|
|
378
372
|
const e = new URLSearchParams({
|
|
379
|
-
externalId:
|
|
373
|
+
externalId: $()
|
|
380
374
|
}), t = await Q(`/sessions?${e.toString()}`, "GET");
|
|
381
375
|
if (!t.ok)
|
|
382
376
|
throw new Error("Unable to load history, please try again later");
|
|
383
377
|
return t.json();
|
|
384
378
|
}
|
|
385
|
-
async function
|
|
379
|
+
async function Se(e) {
|
|
380
|
+
var u;
|
|
386
381
|
const t = new URLSearchParams({
|
|
387
382
|
sessionId: e
|
|
388
383
|
}), o = await Q(`/session?${t.toString()}`, "GET");
|
|
389
384
|
if (!o.ok)
|
|
390
385
|
throw new Error("Unable to load messages, please try again later");
|
|
391
|
-
|
|
386
|
+
const r = await o.json();
|
|
387
|
+
return ((u = r == null ? void 0 : r.sessionHistory) != null ? u : []).map((c) => ({
|
|
388
|
+
id: c.id,
|
|
389
|
+
text: c.text,
|
|
390
|
+
role: c.youtubeVideo ? T.BOT : c.role,
|
|
391
|
+
timestamp: c.timestamp,
|
|
392
|
+
video: c.youtubeVideo,
|
|
393
|
+
channel: c.channel,
|
|
394
|
+
done: !0
|
|
395
|
+
}));
|
|
392
396
|
}
|
|
393
397
|
async function Q(e, t = "GET", o = null) {
|
|
394
|
-
const
|
|
398
|
+
const r = N(), { endpoint: d, token: u } = r || {};
|
|
395
399
|
if (!d)
|
|
396
|
-
throw new Error(
|
|
397
|
-
const
|
|
400
|
+
throw new Error(me);
|
|
401
|
+
const c = `${d}${e}`, f = {
|
|
398
402
|
"Content-Type": "application/json"
|
|
399
403
|
};
|
|
400
|
-
return u && (f.Authorization = `Bearer ${u}`), fetch(
|
|
404
|
+
return u && (f.Authorization = `Bearer ${u}`), fetch(c, {
|
|
401
405
|
headers: f,
|
|
402
406
|
method: t,
|
|
403
407
|
body: o ? JSON.stringify(o) : null
|
|
404
408
|
});
|
|
405
409
|
}
|
|
406
|
-
function
|
|
410
|
+
function Z(e = {}) {
|
|
407
411
|
return {
|
|
408
412
|
sessionId: void 0,
|
|
409
413
|
socket: null,
|
|
@@ -421,17 +425,17 @@ function j(e = {}) {
|
|
|
421
425
|
pendingRemoteIceCandidates: []
|
|
422
426
|
};
|
|
423
427
|
}
|
|
424
|
-
let n =
|
|
425
|
-
const
|
|
428
|
+
let n = Z();
|
|
429
|
+
const Ie = {
|
|
426
430
|
iceServers: [{ urls: "stun:stun.l.google.com:19302" }, { urls: "stun:stun1.l.google.com:19302" }]
|
|
427
431
|
};
|
|
428
|
-
function
|
|
432
|
+
function Me(e) {
|
|
429
433
|
n.callbacks = { ...n.callbacks, ...e };
|
|
430
434
|
}
|
|
431
|
-
function
|
|
435
|
+
function j() {
|
|
432
436
|
n.peerConnection && (n.peerConnection.close(), n.peerConnection = null), n.localStream && (n.localStream.getTracks().forEach((t) => t.stop()), n.localStream = null), n.remoteStream && (n.remoteStream = null), n.remoteAudio && (n.remoteAudio.srcObject = null, n.remoteAudio.parentNode && n.remoteAudio.parentNode.removeChild(n.remoteAudio), n.remoteAudio = null), n.socket && (n.socket.close(), n.socket = null), D();
|
|
433
437
|
const e = n.callbacks;
|
|
434
|
-
n =
|
|
438
|
+
n = Z(e);
|
|
435
439
|
}
|
|
436
440
|
function S(e) {
|
|
437
441
|
var t, o;
|
|
@@ -444,7 +448,7 @@ function v(e) {
|
|
|
444
448
|
function D() {
|
|
445
449
|
n.pingInterval && (clearInterval(n.pingInterval), n.pingInterval = null);
|
|
446
450
|
}
|
|
447
|
-
function
|
|
451
|
+
function Ce() {
|
|
448
452
|
D(), n.pingInterval = setInterval(() => {
|
|
449
453
|
if (n.socket && n.socket.readyState === WebSocket.OPEN) {
|
|
450
454
|
n.pingCount++;
|
|
@@ -453,68 +457,60 @@ function Ie() {
|
|
|
453
457
|
timestamp: Date.now(),
|
|
454
458
|
count: n.pingCount
|
|
455
459
|
};
|
|
456
|
-
w(e)
|
|
460
|
+
w(e);
|
|
457
461
|
} else
|
|
458
|
-
|
|
462
|
+
D();
|
|
459
463
|
}, 1e4);
|
|
460
464
|
}
|
|
461
|
-
function
|
|
462
|
-
n.lastPongTime = Date.now()
|
|
465
|
+
function he() {
|
|
466
|
+
n.lastPongTime = Date.now();
|
|
463
467
|
}
|
|
464
468
|
function w(e) {
|
|
465
|
-
|
|
466
|
-
console.error("Failed to send event: no socket instance");
|
|
467
|
-
return;
|
|
468
|
-
}
|
|
469
|
-
if (n.socket.readyState !== WebSocket.OPEN) {
|
|
470
|
-
console.error("Failed to send event: socket state not open ", e);
|
|
471
|
-
return;
|
|
472
|
-
}
|
|
473
|
-
n.socket.send(JSON.stringify(e));
|
|
469
|
+
n.socket && n.socket.readyState === WebSocket.OPEN && n.socket.send(JSON.stringify(e));
|
|
474
470
|
}
|
|
475
|
-
async function
|
|
471
|
+
async function we() {
|
|
476
472
|
try {
|
|
477
473
|
n.localStream = await navigator.mediaDevices.getUserMedia({
|
|
478
474
|
audio: !0,
|
|
479
475
|
video: !1
|
|
480
|
-
})
|
|
476
|
+
});
|
|
481
477
|
} catch (e) {
|
|
482
|
-
throw
|
|
478
|
+
throw e;
|
|
483
479
|
}
|
|
484
480
|
}
|
|
485
|
-
function
|
|
486
|
-
n.peerConnection = new RTCPeerConnection(
|
|
481
|
+
function ye() {
|
|
482
|
+
n.peerConnection = new RTCPeerConnection(Ie), n.peerConnection.onicecandidate = (e) => {
|
|
487
483
|
if (e.candidate) {
|
|
488
484
|
const t = JSON.stringify(e.candidate);
|
|
489
|
-
n.peerConnection && n.peerConnection.remoteDescription ?
|
|
485
|
+
n.peerConnection && n.peerConnection.remoteDescription ? w({
|
|
490
486
|
type: "ice",
|
|
491
487
|
data: {
|
|
492
488
|
candidate: t
|
|
493
489
|
}
|
|
494
|
-
})
|
|
490
|
+
}) : n.localIceCandidates.push(t);
|
|
495
491
|
}
|
|
496
492
|
}, n.peerConnection.ontrack = (e) => {
|
|
497
|
-
|
|
493
|
+
n.remoteStream = e.streams[0], n.remoteAudio || (n.remoteAudio = document.createElement("audio"), n.remoteAudio.autoplay = !0, n.remoteAudio.controls = !1, document.body.appendChild(n.remoteAudio)), n.remoteAudio.srcObject = n.remoteStream, n.remoteAudio.play().then(() => {
|
|
494
|
+
}).catch((t) => {
|
|
495
|
+
});
|
|
498
496
|
}, n.peerConnection.onconnectionstatechange = () => {
|
|
499
497
|
const e = n.peerConnection.connectionState;
|
|
500
|
-
|
|
498
|
+
e === "connected" ? S("connected") : (e === "disconnected" || e === "closed") && (S("disconnected"), X());
|
|
501
499
|
}, n.peerConnection.oniceconnectionstatechange = () => {
|
|
502
|
-
console.log(`ICE connection state: ${n.peerConnection.iceConnectionState}`);
|
|
503
500
|
};
|
|
504
501
|
}
|
|
505
|
-
function
|
|
502
|
+
function be(e) {
|
|
506
503
|
return new Promise((t, o) => {
|
|
507
504
|
if (n.socket && (n.socket.readyState === WebSocket.CONNECTING || n.socket.readyState === WebSocket.OPEN)) {
|
|
508
|
-
|
|
505
|
+
t(n.socket.readyState === WebSocket.OPEN);
|
|
509
506
|
return;
|
|
510
507
|
}
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
if (!a || !a.endpoint) {
|
|
508
|
+
const r = N();
|
|
509
|
+
if (!r || !r.endpoint) {
|
|
514
510
|
o(new Error("SDK not initialized. Please initialize SDK first."));
|
|
515
511
|
return;
|
|
516
512
|
}
|
|
517
|
-
const d =
|
|
513
|
+
const d = re(r.endpoint);
|
|
518
514
|
if (!d) {
|
|
519
515
|
o(
|
|
520
516
|
new Error(
|
|
@@ -523,36 +519,36 @@ function ye(e) {
|
|
|
523
519
|
);
|
|
524
520
|
return;
|
|
525
521
|
}
|
|
526
|
-
const u =
|
|
522
|
+
const u = $(), c = new URLSearchParams({
|
|
527
523
|
externalId: u
|
|
528
524
|
});
|
|
529
|
-
e.sessionId &&
|
|
530
|
-
const f = `${d}?${
|
|
531
|
-
n.socket = new WebSocket(f), n.socket.onopen = (
|
|
532
|
-
|
|
533
|
-
}, n.socket.onmessage = (
|
|
534
|
-
const p = JSON.parse(
|
|
525
|
+
e.sessionId && c.set("sessionId", e.sessionId), r.token && c.set("token", r.token);
|
|
526
|
+
const f = `${d}?${c.toString()}`;
|
|
527
|
+
n.socket = new WebSocket(f), n.socket.onopen = (l) => {
|
|
528
|
+
Ce(), t(!0);
|
|
529
|
+
}, n.socket.onmessage = (l) => {
|
|
530
|
+
const p = JSON.parse(l.data);
|
|
535
531
|
Ee(p);
|
|
536
|
-
}, n.socket.onerror = (
|
|
537
|
-
|
|
538
|
-
}, n.socket.onclose = (
|
|
539
|
-
|
|
532
|
+
}, n.socket.onerror = (l) => {
|
|
533
|
+
S("error"), v(l.message || "Unable to connect voice"), o(l);
|
|
534
|
+
}, n.socket.onclose = (l) => {
|
|
535
|
+
D();
|
|
540
536
|
};
|
|
541
537
|
});
|
|
542
538
|
}
|
|
543
539
|
function Ee(e) {
|
|
544
|
-
switch (
|
|
540
|
+
switch (e.type) {
|
|
545
541
|
case "pong":
|
|
546
|
-
|
|
542
|
+
he();
|
|
547
543
|
break;
|
|
548
544
|
case "answer":
|
|
549
|
-
|
|
545
|
+
Te(e.data);
|
|
550
546
|
break;
|
|
551
547
|
case "ice":
|
|
552
|
-
|
|
548
|
+
ve(e.data);
|
|
553
549
|
break;
|
|
554
550
|
case "renegotiationOffer":
|
|
555
|
-
|
|
551
|
+
De(e.data);
|
|
556
552
|
break;
|
|
557
553
|
case "end":
|
|
558
554
|
X();
|
|
@@ -561,61 +557,57 @@ function Ee(e) {
|
|
|
561
557
|
S("error"), v(e.error || "Unable to connect voice");
|
|
562
558
|
break;
|
|
563
559
|
default:
|
|
564
|
-
console.log("Unknown call event type: ", e.type);
|
|
565
560
|
break;
|
|
566
561
|
}
|
|
567
562
|
}
|
|
568
|
-
async function
|
|
563
|
+
async function Te(e) {
|
|
569
564
|
try {
|
|
570
|
-
if (
|
|
565
|
+
if (n.sessionId = e.sessionId, fe(e.sessionId), n.peerConnection) {
|
|
571
566
|
const t = new RTCSessionDescription({
|
|
572
567
|
type: "answer",
|
|
573
568
|
sdp: e.sdp
|
|
574
569
|
});
|
|
575
|
-
|
|
570
|
+
await n.peerConnection.setRemoteDescription(t);
|
|
576
571
|
for (const o of n.localIceCandidates)
|
|
577
572
|
w({
|
|
578
573
|
type: "ice",
|
|
579
574
|
data: {
|
|
580
575
|
candidate: o
|
|
581
576
|
}
|
|
582
|
-
})
|
|
577
|
+
});
|
|
583
578
|
n.localIceCandidates = [];
|
|
584
579
|
for (const o of n.pendingRemoteIceCandidates)
|
|
585
580
|
try {
|
|
586
|
-
const
|
|
587
|
-
await n.peerConnection.addIceCandidate(
|
|
588
|
-
} catch (
|
|
589
|
-
console.error(`Failed to add pending ICE candidate: ${a.message}`);
|
|
581
|
+
const r = new RTCIceCandidate(JSON.parse(o));
|
|
582
|
+
await n.peerConnection.addIceCandidate(r);
|
|
583
|
+
} catch (r) {
|
|
590
584
|
}
|
|
591
585
|
n.pendingRemoteIceCandidates = [];
|
|
592
586
|
}
|
|
593
587
|
} catch (t) {
|
|
594
|
-
console.error(`Failed to handle answer: ${t.message}`);
|
|
595
588
|
}
|
|
596
589
|
}
|
|
597
|
-
async function
|
|
590
|
+
async function ve(e) {
|
|
598
591
|
try {
|
|
599
592
|
if (n.peerConnection) {
|
|
600
593
|
if (!n.peerConnection.remoteDescription) {
|
|
601
|
-
n.pendingRemoteIceCandidates.push(e.candidate)
|
|
594
|
+
n.pendingRemoteIceCandidates.push(e.candidate);
|
|
602
595
|
return;
|
|
603
596
|
}
|
|
604
597
|
const t = new RTCIceCandidate(JSON.parse(e.candidate));
|
|
605
|
-
await n.peerConnection.addIceCandidate(t)
|
|
598
|
+
await n.peerConnection.addIceCandidate(t);
|
|
606
599
|
}
|
|
607
600
|
} catch (t) {
|
|
608
|
-
console.error(`Failed to add ICE candidate: ${t.message}`);
|
|
609
601
|
}
|
|
610
602
|
}
|
|
611
|
-
async function
|
|
603
|
+
async function De(e) {
|
|
612
604
|
try {
|
|
613
|
-
if (
|
|
605
|
+
if (n.peerConnection) {
|
|
614
606
|
const t = new RTCSessionDescription({
|
|
615
607
|
type: "offer",
|
|
616
608
|
sdp: e.sdp
|
|
617
609
|
});
|
|
618
|
-
|
|
610
|
+
await n.peerConnection.setRemoteDescription(t);
|
|
619
611
|
const o = await n.peerConnection.createAnswer();
|
|
620
612
|
await n.peerConnection.setLocalDescription(o), w({
|
|
621
613
|
type: "renegotiationAnswer",
|
|
@@ -625,43 +617,40 @@ async function ve(e) {
|
|
|
625
617
|
});
|
|
626
618
|
}
|
|
627
619
|
} catch (t) {
|
|
628
|
-
console.error(`Failed to handle renegotiation offer: ${t.message}`);
|
|
629
620
|
}
|
|
630
621
|
}
|
|
631
|
-
async function
|
|
622
|
+
async function xe(e = {}) {
|
|
632
623
|
try {
|
|
633
|
-
if (n.callStatus === "connecting" || n.callStatus === "connected")
|
|
634
|
-
console.log(`Call already in ${n.callStatus} state`);
|
|
624
|
+
if (n.callStatus === "connecting" || n.callStatus === "connected")
|
|
635
625
|
return;
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
}), await ye(e);
|
|
626
|
+
S("connecting"), v(null), n.sessionId = e.sessionId, await we(), ye(), n.localStream.getTracks().forEach((o) => {
|
|
627
|
+
n.peerConnection.addTrack(o, n.localStream);
|
|
628
|
+
}), await be(e);
|
|
640
629
|
const t = await n.peerConnection.createOffer();
|
|
641
630
|
await n.peerConnection.setLocalDescription(t), w({
|
|
642
631
|
type: "offer",
|
|
643
632
|
data: {
|
|
644
633
|
sdp: t.sdp
|
|
645
634
|
}
|
|
646
|
-
})
|
|
635
|
+
});
|
|
647
636
|
} catch (t) {
|
|
648
|
-
|
|
637
|
+
S("error"), v(t.message || "Unable to connect voice"), j();
|
|
649
638
|
}
|
|
650
639
|
}
|
|
651
640
|
function X() {
|
|
652
641
|
w({
|
|
653
642
|
type: "end"
|
|
654
|
-
}), n.socket && (n.socket.close(), n.socket = null), S("disconnected"), n.peerConnection && (n.peerConnection.close(), n.peerConnection = null), n.localStream && (n.localStream.getTracks().forEach((e) => e.stop()), n.localStream = null),
|
|
643
|
+
}), n.socket && (n.socket.close(), n.socket = null), S("disconnected"), n.peerConnection && (n.peerConnection.close(), n.peerConnection = null), n.localStream && (n.localStream.getTracks().forEach((e) => e.stop()), n.localStream = null), j();
|
|
655
644
|
}
|
|
656
|
-
function
|
|
645
|
+
function qe() {
|
|
657
646
|
if (n.localStream) {
|
|
658
647
|
const e = n.localStream.getAudioTracks()[0];
|
|
659
648
|
if (e)
|
|
660
|
-
return e.enabled = !e.enabled, n.isMuted = !e.enabled,
|
|
649
|
+
return e.enabled = !e.enabled, n.isMuted = !e.enabled, n.isMuted;
|
|
661
650
|
}
|
|
662
651
|
return !1;
|
|
663
652
|
}
|
|
664
|
-
function
|
|
653
|
+
function Le() {
|
|
665
654
|
return n.localStream;
|
|
666
655
|
}
|
|
667
656
|
function Ge() {
|
|
@@ -671,23 +660,23 @@ function Ge() {
|
|
|
671
660
|
return;
|
|
672
661
|
}
|
|
673
662
|
n.peerConnection.getStats().then((o) => {
|
|
674
|
-
o.forEach((
|
|
675
|
-
|
|
663
|
+
o.forEach((r) => {
|
|
664
|
+
r.type == "inbound-rtp" && e(r.totalAudioEnergy);
|
|
676
665
|
}), t(new Error("no inbound-rtp stats found"));
|
|
677
666
|
}).catch((o) => {
|
|
678
667
|
t(o);
|
|
679
668
|
});
|
|
680
669
|
});
|
|
681
670
|
}
|
|
682
|
-
function
|
|
671
|
+
function ze() {
|
|
683
672
|
return new Promise((e, t) => {
|
|
684
673
|
if (!n.peerConnection) {
|
|
685
674
|
t(new Error("no peer connection"));
|
|
686
675
|
return;
|
|
687
676
|
}
|
|
688
677
|
n.peerConnection.getStats().then((o) => {
|
|
689
|
-
o.forEach((
|
|
690
|
-
|
|
678
|
+
o.forEach((r) => {
|
|
679
|
+
r.type == "outbound-rtp" && e(r.totalAudioEnergy);
|
|
691
680
|
}), t(new Error("no outbound-rtp stats found"));
|
|
692
681
|
}).catch((o) => {
|
|
693
682
|
t(o);
|
|
@@ -696,20 +685,20 @@ function Le() {
|
|
|
696
685
|
}
|
|
697
686
|
export {
|
|
698
687
|
T as MESSAGE_ROLES,
|
|
699
|
-
|
|
688
|
+
ke as authenticate,
|
|
700
689
|
Ae as disconnect,
|
|
701
690
|
X as disconnectCall,
|
|
702
|
-
|
|
691
|
+
Ne as getHistory,
|
|
703
692
|
Ge as getInboundAudioEnergy,
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
693
|
+
Le as getLocalStream,
|
|
694
|
+
Se as getMessages,
|
|
695
|
+
ze as getOutboundAudioEnergy,
|
|
696
|
+
Oe as initialize,
|
|
697
|
+
Ue as sendMessage,
|
|
698
|
+
Me as setCallCallbacks,
|
|
710
699
|
Pe as setCallbacks,
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
700
|
+
xe as startCall,
|
|
701
|
+
Re as startChat,
|
|
702
|
+
qe as toggleMute
|
|
714
703
|
};
|
|
715
704
|
//# sourceMappingURL=origon-chat-sdk.js.map
|