@origonai/web-chat-sdk 1.0.12 → 1.0.13

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