@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.
@@ -1,23 +1,23 @@
1
- import { fetchEventSource as ce } from "@microsoft/fetch-event-source";
2
- function H() {
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((c) => c.toString(16).padStart(2, "0")).join("");
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 ae() {
11
+ function le() {
12
12
  if (localStorage.getItem("chatDeviceId"))
13
13
  return localStorage.getItem("chatDeviceId");
14
- const e = H();
14
+ const e = B();
15
15
  return localStorage.setItem("chatDeviceId", e), e;
16
16
  }
17
- async function ie(e) {
17
+ async function de(e) {
18
18
  return new Promise((t) => setTimeout(t, e));
19
19
  }
20
- function le(e) {
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 de(e) {
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 ue(e) {
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 w = {
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
- }, Y = 1e4, ge = 5e3, v = {
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 fe() {
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 r = fe();
72
+ let c = me();
73
73
  function U() {
74
- r.pingInterval && (clearInterval(r.pingInterval), r.pingInterval = null);
74
+ c.pingInterval && (clearInterval(c.pingInterval), c.pingInterval = null);
75
75
  }
76
- function pe() {
77
- U(), r.pingInterval = setInterval(() => {
78
- r.socket && r.socket.readyState === WebSocket.OPEN ? O({ type: "ping" }) : U();
79
- }, Y);
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 B() {
82
- U(), r.socketDisconnectedTimeout && (clearTimeout(r.socketDisconnectedTimeout), r.socketDisconnectedTimeout = null), r.socketConnectionTimeout && (clearTimeout(r.socketConnectionTimeout), r.socketConnectionTimeout = null);
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 Q() {
85
- r.socketDisconnected = !1, A("socket");
84
+ function j() {
85
+ c.socketDisconnected = !1, $("socket");
86
86
  }
87
- function Z() {
88
- r.socketDisconnected = !0, A("sse");
87
+ function X() {
88
+ c.socketDisconnected = !0, $("sse");
89
89
  }
90
- function me(e) {
90
+ function ke(e) {
91
91
  return new Promise((t, s) => {
92
- if (r.socket && (r.socket.readyState === WebSocket.CONNECTING || r.socket.readyState === WebSocket.OPEN)) {
93
- t(r.socket.readyState === WebSocket.OPEN);
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 c = N();
97
- if (!c || !c.endpoint) {
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 = de(c.endpoint);
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 = P(), i = new URLSearchParams({
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), c.token && i.set("token", c.token);
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
- r.socket = new WebSocket(u), r.socket.onopen = () => {
116
- r.previouslyConnected = !0, Q(), O({ type: "ping" }), clearTimeout(r.socketConnectionTimeout), pe(), t(!0);
117
- }, r.socket.onmessage = (l) => {
118
- const p = JSON.parse(l.data);
119
- Se(p);
120
- }, r.socket.onerror = (l) => {
121
- A("sse"), s(l);
122
- }, r.socket.onclose = (l) => {
123
- l.target === r.socket && (l.code === 1006 && (r.previouslyConnected ? Z() : k({
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(r.socketConnectionTimeout)), r.socket = null, B());
128
- }, r.previouslyConnected || (r.socketConnectionTimeout = setTimeout(() => {
129
- k({
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
- }, ge));
134
+ }, pe));
135
135
  });
136
136
  }
137
137
  function O(e) {
138
- r.socketDisconnected || !r.socket || r.socket.send(JSON.stringify({ ...e, eventId: e.eventId || H() }));
138
+ c.socketDisconnected || !c.socket || c.socket.send(JSON.stringify({ ...e, eventId: e.eventId || B() }));
139
139
  }
140
- function Se(e) {
140
+ function Ie(e) {
141
141
  switch (e.type) {
142
142
  case "pong": {
143
- r.socketDisconnected && Q(), r.socketDisconnectedTimeout && clearTimeout(r.socketDisconnectedTimeout), r.socketDisconnectedTimeout = setTimeout(() => {
144
- Z();
145
- }, Y + 1e3);
143
+ c.socketDisconnected && j(), c.socketDisconnectedTimeout && clearTimeout(c.socketDisconnectedTimeout), c.socketDisconnectedTimeout = setTimeout(() => {
144
+ X();
145
+ }, Q + 1e3);
146
146
  break;
147
147
  }
148
- case v.TYPING: {
149
- V(!0);
148
+ case D.TYPING: {
149
+ Y(!0);
150
150
  break;
151
151
  }
152
- case v.TYPING_STOP: {
153
- V(!1);
152
+ case D.TYPING_STOP: {
153
+ Y(!1);
154
154
  break;
155
155
  }
156
- case v.MESSAGE: {
156
+ case D.MESSAGE: {
157
157
  const { eventId: t, data: s } = e;
158
- t || k({
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 v.END: {
166
- j();
165
+ case D.END: {
166
+ ee();
167
167
  break;
168
168
  }
169
169
  }
170
170
  }
171
- function j() {
172
- r.socket && r.socket.close(1e3), r.previouslyConnected = !1, B(), r.socket = null, A("sse");
171
+ function ee() {
172
+ c.socket && c.socket.close(1e3), c.previouslyConnected = !1, Z(), c.socket = null, $("sse");
173
173
  }
174
- function ke() {
175
- return r.socket !== null && r.socket.readyState === WebSocket.OPEN && !r.socketDisconnected;
174
+ function Ce() {
175
+ return c.socket !== null && c.socket.readyState === WebSocket.OPEN && !c.socketDisconnected;
176
176
  }
177
- function X(e = {}) {
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 = X();
194
- function Me(e) {
193
+ let o = ne();
194
+ function Le(e) {
195
195
  o.callbacks = { ...o.callbacks, ...e };
196
196
  }
197
- function xe(e) {
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 Ie(e) {
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 qe(e = {}) {
207
+ async function ze(e = {}) {
208
208
  try {
209
209
  let t = null;
210
- o.authenticated ? t = o.configData : (t = await ye(o.credentials), o.authenticated = !0, o.configData = t);
211
- let s = [], c = "agent";
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 Ee(e.sessionId);
214
- s = f.messages, c = f.control || "agent";
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", P()), o.sseUrl = `${le(
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 = c, c === "human" && Ce({ text: "", html: "" }).catch(() => {
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: c,
223
+ control: a,
224
224
  configData: t
225
225
  };
226
226
  } catch (t) {
227
- throw ee(), t;
227
+ throw te(), t;
228
228
  }
229
229
  }
230
- function Le() {
231
- ee();
230
+ function Fe() {
231
+ te();
232
232
  }
233
- function ee() {
234
- o.abortController && o.abortController.abort(), j();
233
+ function te() {
234
+ o.abortController && o.abortController.abort(), ee();
235
235
  const { callbacks: e, credentials: t } = o;
236
- o = X(e), o.credentials = t;
236
+ o = ne(e), o.credentials = t;
237
237
  }
238
- function P() {
238
+ function A() {
239
239
  var e;
240
- return (e = o.credentials) != null && e.externalId ? o.credentials.externalId : ae();
240
+ return (e = o.credentials) != null && e.externalId ? o.credentials.externalId : le();
241
241
  }
242
- function k(e) {
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 V(e) {
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 A(e) {
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 Ce({ text: e, html: t, context: s }) {
255
- return new Promise((c, d) => {
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: w.USER,
262
+ role: y.USER,
263
263
  text: e,
264
264
  html: t,
265
265
  timestamp: (/* @__PURE__ */ new Date()).toISOString()
266
266
  };
267
- k(g), await ie(200);
267
+ I(g), await de(200);
268
268
  }
269
- if (o.transport === "socket" && ke()) {
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
- }), c(o.sessionId);
276
+ }), a(o.sessionId);
277
277
  return;
278
278
  }
279
279
  if (o.control === "agent") {
280
280
  const g = {
281
- role: w.ASSISTANT,
281
+ role: y.ASSISTANT,
282
282
  text: "",
283
283
  loading: !0
284
284
  };
285
- k(g);
285
+ I(g);
286
286
  }
287
- const p = new URL(o.sseUrl);
288
- o.sessionId && p.searchParams.set("sessionId", o.sessionId), o.requestId && p.searchParams.set("requestId", o.requestId), o.lastStreamId = void 0, o.abortController = new AbortController();
289
- const C = {
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 && (C.Authorization = `Bearer ${o.credentials.token}`), await ce(p.toString(), {
292
+ (f = o.credentials) != null && f.token && (h.Authorization = `Bearer ${o.credentials.token}`), await ie(S.toString(), {
293
293
  method: "POST",
294
- headers: C,
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 E, b, M, x, q, L, G, z, F, W, J, _, K;
308
- const a = JSON.parse(g.data);
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 = a.sessionId, o.requestId = a.requestId, a.control && (o.control = a.control, (b = (E = o.callbacks).onControlUpdate) == null || b.call(E, a.control));
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
- me({
312
+ ke({
313
313
  sessionId: o.sessionId,
314
- requestId: a.requestId
314
+ requestId: r.requestId
315
315
  });
316
316
  else if (g.event === "update")
317
- a.control && (o.control = a.control, (x = (M = o.callbacks).onControlUpdate) == null || x.call(M, a.control));
318
- else if (a.error) {
319
- const h = a.error && typeof a.error == "string" ? a.error : "Failed to connect to the system", S = o.messages.length - 1, m = {
320
- ...o.messages[S],
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
- errorText: h
331
+ done: !0
323
332
  };
324
333
  o.messages = o.messages.map(
325
- ($, re) => re === S ? m : $
326
- ), (L = (q = o.callbacks).onMessageUpdate) == null || L.call(q, S, m), d(new Error(h));
327
- } else if (g.event === "done")
328
- c(o.sessionId);
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: w.SUPERVISOR,
333
- text: a.message,
334
- sources: a.sources,
339
+ role: y.SUPERVISOR,
340
+ text: r.message,
341
+ sources: r.sources,
335
342
  done: !0
336
343
  };
337
- k(m), c(o.sessionId), o.sessionId = (G = a.sessionId) != null ? G : o.sessionId, o.requestId = (z = a.requestId) != null ? z : o.requestId;
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 (a.streamId !== void 0) {
347
+ if (r.streamId !== void 0) {
341
348
  if (o.lastStreamId === void 0)
342
- o.lastStreamId = a.streamId;
343
- else if (a.streamId !== o.lastStreamId) {
344
- o.lastStreamId = a.streamId;
349
+ o.lastStreamId = r.streamId;
350
+ else if (r.streamId !== o.lastStreamId) {
351
+ o.lastStreamId = r.streamId;
345
352
  const m = {
346
- role: w.ASSISTANT,
353
+ role: y.ASSISTANT,
347
354
  text: "",
348
355
  loading: !0
349
356
  };
350
- k(m);
357
+ I(m);
351
358
  }
352
359
  }
353
- const h = o.messages.length - 1, S = o.messages[h], R = {
354
- ...S,
360
+ const p = o.messages.length - 1, k = o.messages[p], w = {
361
+ ...k,
355
362
  loading: !1,
356
- text: (S.text || "") + a.message,
357
- sources: a.sources,
358
- done: (F = a.done) != null ? F : S.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, $) => $ === h ? R : m
362
- ), (J = (W = o.callbacks).onMessageUpdate) == null || J.call(W, h, R), a.done, o.sessionId = (_ = a.sessionId) != null ? _ : o.sessionId, o.requestId = (K = a.requestId) != null ? K : o.requestId;
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 p = "Failed to connect to the system", C = o.messages.length - 1, g = o.messages[C], a = {
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 || p,
381
+ errorText: g.done ? void 0 : l.message || S,
375
382
  done: !0
376
383
  };
377
384
  o.messages = o.messages.map(
378
- (E, b) => b === C ? a : E
379
- ), (u = (i = o.callbacks).onMessageUpdate) == null || u.call(i, C, a), d(l);
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 he = "Something went wrong initializing the chat", we = "Chat SDK not initialized";
385
- async function ye(e) {
386
- const { endpoint: t } = e, s = `${t}/config`, c = await fetch(s, {
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 (!c.ok) {
393
- const i = await c.json();
394
- throw new Error((i == null ? void 0 : i.error) || he);
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 c.json()).data;
403
+ return (await a.json()).data;
397
404
  }
398
- async function Ge() {
405
+ async function We() {
399
406
  const e = new URLSearchParams({
400
- externalId: P()
401
- }), t = await ne(`/sessions?${e.toString()}`, "GET");
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 Ee(e) {
413
+ async function ve(e) {
407
414
  var i;
408
415
  const t = new URLSearchParams({
409
416
  sessionId: e
410
- }), s = await ne(`/session?${t.toString()}`, "GET");
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 c = await s.json(), d = c == null ? void 0 : c.control, f = ((i = c == null ? void 0 : c.history) != null ? i : []).map((u) => ({
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 ? w.ASSISTANT : u.role,
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 ne(e, t = "GET", s = null) {
425
- const c = N(), { endpoint: d, token: f } = c || {};
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(we);
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 te(e = {}) {
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 = te();
456
- const be = {
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 ze(e) {
466
+ function Je(e) {
460
467
  n.callbacks = { ...n.callbacks, ...e };
461
468
  }
462
- function oe() {
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), D();
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 = te(e);
472
+ n = se(e);
466
473
  }
467
- function I(e) {
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 T(e) {
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 D() {
482
+ function R() {
476
483
  n.pingInterval && (clearInterval(n.pingInterval), n.pingInterval = null);
477
484
  }
478
- function ve() {
479
- D(), n.pingInterval = setInterval(() => {
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
- y(e);
494
+ b(e);
488
495
  } else
489
- D();
496
+ R();
490
497
  }, 1e4);
491
498
  }
492
- function Te() {
499
+ function Pe() {
493
500
  n.lastPongTime = Date.now();
494
501
  }
495
- function y(e) {
502
+ function b(e) {
496
503
  n.socket && n.socket.readyState === WebSocket.OPEN && n.socket.send(JSON.stringify(e));
497
504
  }
498
- async function De() {
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 Pe() {
509
- n.peerConnection = new RTCPeerConnection(be), n.peerConnection.onicecandidate = (e) => {
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 ? y({
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" ? I("connected") : (e === "disconnected" || e === "closed") && (I("disconnected"), se());
532
+ e === "connected" ? C("connected") : (e === "disconnected" || e === "closed") && (C("disconnected"), ae());
526
533
  }, n.peerConnection.oniceconnectionstatechange = () => {
527
534
  };
528
535
  }
529
- function Ae(e) {
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 c = N();
536
- if (!c || !c.endpoint) {
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 = ue(c.endpoint);
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 = P(), i = new URLSearchParams({
556
+ const f = A(), i = new URLSearchParams({
550
557
  externalId: f
551
558
  });
552
- e.sessionId && i.set("sessionId", e.sessionId), c.token && i.set("token", c.token);
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
- ve(), t(!0);
562
+ De(), t(!0);
556
563
  }, n.socket.onmessage = (l) => {
557
- const p = JSON.parse(l.data);
558
- Re(p);
564
+ const S = JSON.parse(l.data);
565
+ Ue(S);
559
566
  }, n.socket.onerror = (l) => {
560
- I("error"), T(l.message || "Unable to connect voice"), s(l);
567
+ C("error"), P(l.message || "Unable to connect voice"), s(l);
561
568
  }, n.socket.onclose = (l) => {
562
- D();
569
+ R();
563
570
  };
564
571
  });
565
572
  }
566
- function Re(e) {
573
+ function Ue(e) {
567
574
  switch (e.type) {
568
575
  case "pong":
569
- Te();
576
+ Pe();
570
577
  break;
571
578
  case "answer":
572
- $e(e.data);
579
+ Ne(e.data);
580
+ break;
581
+ case "connected":
582
+ Oe(e.data);
573
583
  break;
574
584
  case "ice":
575
- Ue(e.data);
585
+ Me(e.data);
576
586
  break;
577
587
  case "renegotiationOffer":
578
- Oe(e.data);
588
+ xe(e.data);
579
589
  break;
580
590
  case "end":
581
- se();
591
+ ae();
582
592
  break;
583
593
  case "error":
584
- I("error"), T(e.error || "Unable to connect voice");
594
+ C("error"), P(e.error || "Unable to connect voice");
585
595
  break;
586
596
  default:
587
597
  break;
588
598
  }
589
599
  }
590
- async function $e(e) {
600
+ function Oe(e) {
601
+ n.sessionId = e.sessionId, he(e.sessionId);
602
+ }
603
+ async function Ne(e) {
591
604
  try {
592
- if (n.sessionId = e.sessionId, Ie(e.sessionId), n.peerConnection) {
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
- y({
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 c = new RTCIceCandidate(JSON.parse(s));
609
- await n.peerConnection.addIceCandidate(c);
610
- } catch (c) {
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 Ue(e) {
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 Oe(e) {
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), y({
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 Fe(e = {}) {
662
+ async function _e(e = {}) {
650
663
  try {
651
664
  if (n.callStatus === "connecting" || n.callStatus === "connected")
652
665
  return;
653
- I("connecting"), T(null), n.sessionId = e.sessionId, await De(), Pe(), n.localStream.getTracks().forEach((s) => {
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 Ae(e);
668
+ }), await $e(e);
656
669
  const t = await n.peerConnection.createOffer();
657
- await n.peerConnection.setLocalDescription(t), y({
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
- I("error"), T(t.message || "Unable to connect voice"), oe();
677
+ C("error"), P(t.message || "Unable to connect voice"), ce();
665
678
  }
666
679
  }
667
- function se() {
668
- y({
680
+ function ae() {
681
+ b({
669
682
  type: "end"
670
- }), n.socket && (n.socket.close(), n.socket = null), I("disconnected"), n.peerConnection && (n.peerConnection.close(), n.peerConnection = null), n.localStream && (n.localStream.getTracks().forEach((e) => e.stop()), n.localStream = null), oe();
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 We() {
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 Je() {
693
+ function Ve() {
681
694
  return n.localStream;
682
695
  }
683
- function _e() {
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((c) => {
691
- c.type == "inbound-rtp" && e(c.totalAudioEnergy);
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 Ke() {
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((c) => {
706
- c.type == "outbound-rtp" && e(c.totalAudioEnergy);
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
- w as MESSAGE_ROLES,
715
- ye as authenticate,
716
- Le as disconnect,
717
- se as disconnectCall,
718
- Ge as getHistory,
719
- _e as getInboundAudioEnergy,
720
- Je as getLocalStream,
721
- Ke as getOutboundAudioEnergy,
722
- Ee as getSession,
723
- xe as initialize,
724
- Ce as sendMessage,
725
- ze as setCallCallbacks,
726
- Me as setCallbacks,
727
- Fe as startCall,
728
- qe as startChat,
729
- We as toggleMute
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