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