@origonai/web-chat-sdk 1.0.8 → 1.0.9

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 ue } from "@microsoft/fetch-event-source";
2
- function Z() {
1
+ import { fetchEventSource as ge } from "@microsoft/fetch-event-source";
2
+ function j() {
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 Z() {
8
8
  20
9
9
  )}-${o.slice(20)}`;
10
10
  }
11
- function ge() {
11
+ function fe() {
12
12
  if (localStorage.getItem("chatDeviceId"))
13
13
  return localStorage.getItem("chatDeviceId");
14
- const e = Z();
14
+ const e = j();
15
15
  return localStorage.setItem("chatDeviceId", e), e;
16
16
  }
17
- async function fe(e) {
17
+ async function pe(e) {
18
18
  return new Promise((n) => setTimeout(n, e));
19
19
  }
20
- function pe(e) {
20
+ function me(e) {
21
21
  let n;
22
22
  try {
23
23
  const o = new URL(e);
@@ -26,7 +26,7 @@ function pe(e) {
26
26
  }
27
27
  return n;
28
28
  }
29
- function me(e) {
29
+ function Se(e) {
30
30
  let n;
31
31
  try {
32
32
  const o = new URL(e);
@@ -35,7 +35,7 @@ function me(e) {
35
35
  }
36
36
  return n;
37
37
  }
38
- function Se(e) {
38
+ function ke(e) {
39
39
  let n;
40
40
  try {
41
41
  const o = new URL(e);
@@ -53,13 +53,13 @@ const y = {
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
- }, j = 1e4, ke = 5e3, A = {
56
+ }, ee = 1e4, Ie = 5e3, R = {
57
57
  MESSAGE: "message",
58
58
  TYPING: "typing",
59
59
  TYPING_STOP: "typingOff",
60
60
  END: "end"
61
61
  };
62
- function Ie() {
62
+ function he() {
63
63
  return {
64
64
  socket: null,
65
65
  previouslyConnected: !1,
@@ -69,25 +69,25 @@ function Ie() {
69
69
  socketConnectionTimeout: null
70
70
  };
71
71
  }
72
- let a = Ie();
73
- function M() {
72
+ let a = he();
73
+ function x() {
74
74
  a.pingInterval && (clearInterval(a.pingInterval), a.pingInterval = null);
75
75
  }
76
- function he() {
77
- M(), a.pingInterval = setInterval(() => {
78
- a.socket && a.socket.readyState === WebSocket.OPEN ? x({ type: "ping" }) : M();
79
- }, j);
80
- }
81
- function ee() {
82
- M(), a.socketDisconnectedTimeout && (clearTimeout(a.socketDisconnectedTimeout), a.socketDisconnectedTimeout = null), a.socketConnectionTimeout && (clearTimeout(a.socketConnectionTimeout), a.socketConnectionTimeout = null);
76
+ function we() {
77
+ x(), a.pingInterval = setInterval(() => {
78
+ a.socket && a.socket.readyState === WebSocket.OPEN ? q({ type: "ping" }) : x();
79
+ }, ee);
83
80
  }
84
81
  function ne() {
85
- a.socketDisconnected = !1, O("socket");
82
+ x(), a.socketDisconnectedTimeout && (clearTimeout(a.socketDisconnectedTimeout), a.socketDisconnectedTimeout = null), a.socketConnectionTimeout && (clearTimeout(a.socketConnectionTimeout), a.socketConnectionTimeout = null);
86
83
  }
87
84
  function te() {
88
- a.socketDisconnected = !0, O("sse");
85
+ a.socketDisconnected = !1, N("socket");
86
+ }
87
+ function oe() {
88
+ a.socketDisconnected = !0, N("sse");
89
89
  }
90
- function we(e) {
90
+ function Ce(e) {
91
91
  return new Promise((n, o) => {
92
92
  if (a.socket && (a.socket.readyState === WebSocket.CONNECTING || a.socket.readyState === WebSocket.OPEN)) {
93
93
  n(a.socket.readyState === WebSocket.OPEN);
@@ -98,7 +98,7 @@ function we(e) {
98
98
  o(new Error("SDK not initialized. Please initialize SDK first."));
99
99
  return;
100
100
  }
101
- const l = me(r.endpoint);
101
+ const l = Se(r.endpoint);
102
102
  if (!l) {
103
103
  o(
104
104
  new Error(
@@ -107,53 +107,53 @@ function we(e) {
107
107
  );
108
108
  return;
109
109
  }
110
- const d = P(), c = new URLSearchParams({
110
+ const d = O(), c = new URLSearchParams({
111
111
  externalId: d
112
112
  });
113
113
  e.sessionId && c.set("sessionId", e.sessionId), e.requestId && c.set("requestId", e.requestId), r.token && c.set("token", r.token);
114
114
  const u = `${l}?${c.toString()}`;
115
115
  a.socket = new WebSocket(u), a.socket.onopen = () => {
116
- a.previouslyConnected = !0, ne(), x({ type: "ping" }), clearTimeout(a.socketConnectionTimeout), he(), n(!0);
117
- }, a.socket.onmessage = (f) => {
118
- const g = JSON.parse(f.data);
119
- Ce(g);
120
- }, a.socket.onerror = (f) => {
121
- O("sse"), o(f);
122
- }, a.socket.onclose = (f) => {
123
- f.target === a.socket && (f.code === 1006 && (a.previouslyConnected ? te() : h({
116
+ a.previouslyConnected = !0, te(), q({ type: "ping" }), clearTimeout(a.socketConnectionTimeout), we(), n(!0);
117
+ }, a.socket.onmessage = (g) => {
118
+ const f = JSON.parse(g.data);
119
+ Ee(f);
120
+ }, a.socket.onerror = (g) => {
121
+ N("sse"), o(g);
122
+ }, a.socket.onclose = (g) => {
123
+ g.target === a.socket && (g.code === 1006 && (a.previouslyConnected ? oe() : h({
124
124
  errorText: "Unable to establish connection",
125
125
  done: !0,
126
126
  timestamp: (/* @__PURE__ */ new Date()).toISOString()
127
- }), clearTimeout(a.socketConnectionTimeout)), a.socket = null, ee());
127
+ }), clearTimeout(a.socketConnectionTimeout)), a.socket = null, ne());
128
128
  }, a.previouslyConnected || (a.socketConnectionTimeout = setTimeout(() => {
129
129
  h({
130
130
  errorText: "Unable to establish connection",
131
131
  done: !0,
132
132
  timestamp: (/* @__PURE__ */ new Date()).toISOString()
133
133
  }), o(new Error("Socket connection timed out"));
134
- }, ke));
134
+ }, Ie));
135
135
  });
136
136
  }
137
- function x(e) {
138
- a.socketDisconnected || !a.socket || a.socket.send(JSON.stringify({ ...e, eventId: e.eventId || Z() }));
137
+ function q(e) {
138
+ a.socketDisconnected || !a.socket || a.socket.send(JSON.stringify({ ...e, eventId: e.eventId || j() }));
139
139
  }
140
- function Ce(e) {
140
+ function Ee(e) {
141
141
  switch (e.type) {
142
142
  case "pong": {
143
- a.socketDisconnected && ne(), a.socketDisconnectedTimeout && clearTimeout(a.socketDisconnectedTimeout), a.socketDisconnectedTimeout = setTimeout(() => {
144
- te();
145
- }, j + 1e3);
143
+ a.socketDisconnected && te(), a.socketDisconnectedTimeout && clearTimeout(a.socketDisconnectedTimeout), a.socketDisconnectedTimeout = setTimeout(() => {
144
+ oe();
145
+ }, ee + 1e3);
146
146
  break;
147
147
  }
148
- case A.TYPING: {
149
- X(!0);
148
+ case R.TYPING: {
149
+ Z(!0);
150
150
  break;
151
151
  }
152
- case A.TYPING_STOP: {
153
- X(!1);
152
+ case R.TYPING_STOP: {
153
+ Z(!1);
154
154
  break;
155
155
  }
156
- case A.MESSAGE: {
156
+ case R.MESSAGE: {
157
157
  const { eventId: n, data: o } = e;
158
158
  n || h({
159
159
  ...o,
@@ -162,19 +162,19 @@ function Ce(e) {
162
162
  });
163
163
  break;
164
164
  }
165
- case A.END: {
166
- oe();
165
+ case R.END: {
166
+ se();
167
167
  break;
168
168
  }
169
169
  }
170
170
  }
171
- function oe() {
172
- a.socket && a.socket.close(1e3), a.previouslyConnected = !1, ee(), a.socket = null, O("sse");
171
+ function se() {
172
+ a.socket && a.socket.close(1e3), a.previouslyConnected = !1, ne(), a.socket = null, N("sse");
173
173
  }
174
- function Ee() {
174
+ function ye() {
175
175
  return a.socket !== null && a.socket.readyState === WebSocket.OPEN && !a.socketDisconnected;
176
176
  }
177
- function se(e = {}) {
177
+ function re(e = {}) {
178
178
  return {
179
179
  credentials: void 0,
180
180
  authenticated: !1,
@@ -190,33 +190,33 @@ function se(e = {}) {
190
190
  control: "agent"
191
191
  };
192
192
  }
193
- let s = se();
194
- function Fe(e) {
193
+ let s = re();
194
+ function Ge(e) {
195
195
  s.callbacks = { ...s.callbacks, ...e };
196
196
  }
197
- function Ge(e) {
197
+ function Je(e) {
198
198
  s.credentials = e, e.token && (s.authenticated = !0);
199
199
  }
200
200
  function b() {
201
201
  return s.credentials;
202
202
  }
203
- function ye(e) {
203
+ function be(e) {
204
204
  var n, o;
205
205
  e && e !== s.sessionId && (s.sessionId = e, (o = (n = s.callbacks).onSessionUpdate) == null || o.call(n, e));
206
206
  }
207
- async function Je(e = {}) {
207
+ async function We(e = {}) {
208
208
  try {
209
209
  let n = null;
210
- s.authenticated ? n = s.configData : (n = await ve(s.credentials), s.authenticated = !0, s.configData = n);
210
+ s.authenticated ? n = s.configData : (n = await $e(s.credentials), s.authenticated = !0, s.configData = n);
211
211
  let o = [], r = "agent";
212
212
  if (e.sessionId) {
213
- const d = await $e(e.sessionId);
213
+ const d = await De(e.sessionId);
214
214
  o = d.messages, r = d.control || "agent";
215
215
  }
216
216
  const l = new URLSearchParams();
217
- return s.credentials.token || l.set("externalId", P()), s.sseUrl = `${pe(
217
+ return s.credentials.token || l.set("externalId", O()), s.sseUrl = `${me(
218
218
  s.credentials.endpoint
219
- )}?${l.toString()}`, s.sessionId = e.sessionId, s.messages = o, s.control = r, r === "human" && be({ text: "", html: "" }).catch(() => {
219
+ )}?${l.toString()}`, s.sessionId = e.sessionId, s.messages = o, s.control = r, r === "human" && Te({ text: "", html: "" }).catch(() => {
220
220
  }), {
221
221
  sessionId: s.sessionId,
222
222
  messages: o,
@@ -224,57 +224,58 @@ async function Je(e = {}) {
224
224
  configData: n
225
225
  };
226
226
  } catch (n) {
227
- throw re(), n;
227
+ throw ae(), n;
228
228
  }
229
229
  }
230
- function We() {
231
- re();
230
+ function _e() {
231
+ ae();
232
232
  }
233
- function re() {
234
- s.abortController && s.abortController.abort(), oe();
233
+ function ae() {
234
+ s.abortController && s.abortController.abort(), se();
235
235
  const { callbacks: e, credentials: n } = s;
236
- s = se(e), s.credentials = n;
236
+ s = re(e), s.credentials = n;
237
237
  }
238
- function P() {
238
+ function O() {
239
239
  var e;
240
- return (e = s.credentials) != null && e.externalId ? s.credentials.externalId : ge();
240
+ return (e = s.credentials) != null && e.externalId ? s.credentials.externalId : fe();
241
241
  }
242
242
  function h(e) {
243
243
  var n, o;
244
244
  s.messages = [...s.messages, e], (o = (n = s.callbacks).onMessageAdd) == null || o.call(n, e);
245
245
  }
246
- function X(e) {
246
+ function Z(e) {
247
247
  var n, o;
248
248
  (o = (n = s.callbacks).onTyping) == null || o.call(n, e);
249
249
  }
250
- function O(e) {
250
+ function N(e) {
251
251
  var n, o;
252
252
  s.transport = e, (o = (n = s.callbacks).onTransportUpdate) == null || o.call(n, e);
253
253
  }
254
- function be({ text: e, html: n, context: o, attachments: r }) {
255
- return new Promise((l, d) => {
254
+ function Te({ text: e, html: n, context: o, attachments: r, meta: l }) {
255
+ return new Promise((d, c) => {
256
256
  (async () => {
257
- var c, u, f;
257
+ var u, g, f;
258
258
  try {
259
- const g = !e && !n && !(r != null && r.length);
260
- if (!g) {
259
+ const I = !e && !n && !(r != null && r.length);
260
+ if (!I) {
261
261
  const p = {
262
262
  role: y.USER,
263
263
  text: e,
264
264
  html: n,
265
265
  timestamp: (/* @__PURE__ */ new Date()).toISOString(),
266
- attachments: r
266
+ attachments: r,
267
+ meta: l
267
268
  };
268
- h(p), await fe(200);
269
+ h(p), await pe(200);
269
270
  }
270
- if (s.transport === "socket" && Ee()) {
271
- x({
271
+ if (s.transport === "socket" && ye()) {
272
+ q({
272
273
  type: "message",
273
274
  data: {
274
275
  text: e,
275
276
  html: n
276
277
  }
277
- }), l(s.sessionId);
278
+ }), d(s.sessionId);
278
279
  return;
279
280
  }
280
281
  if (s.control === "agent") {
@@ -285,15 +286,15 @@ function be({ text: e, html: n, context: o, attachments: r }) {
285
286
  };
286
287
  h(p);
287
288
  }
288
- const I = new URL(s.sseUrl);
289
- s.sessionId && I.searchParams.set("sessionId", s.sessionId), s.requestId && I.searchParams.set("requestId", s.requestId), s.lastStreamId = void 0, s.abortController = new AbortController();
289
+ const v = new URL(s.sseUrl);
290
+ s.sessionId && v.searchParams.set("sessionId", s.sessionId), s.requestId && v.searchParams.set("requestId", s.requestId), s.lastStreamId = void 0, s.abortController = new AbortController();
290
291
  const C = {
291
292
  "Content-Type": "application/json"
292
293
  };
293
- (c = s.credentials) != null && c.token && (C.Authorization = `Bearer ${s.credentials.token}`), await ue(I.toString(), {
294
+ (u = s.credentials) != null && u.token && (C.Authorization = `Bearer ${s.credentials.token}`), await ge(v.toString(), {
294
295
  method: "POST",
295
296
  headers: C,
296
- body: g ? void 0 : JSON.stringify({
297
+ body: I ? l : JSON.stringify({
297
298
  message: e,
298
299
  html: n,
299
300
  context: o,
@@ -306,17 +307,17 @@ function be({ text: e, html: n, context: o, attachments: r }) {
306
307
  throw new Error("Failed to send message");
307
308
  },
308
309
  onmessage: (p) => {
309
- var $, D, q, L, z, F, G, J, W, _, K, H, V, B, Y, Q;
310
+ var D, A, L, z, F, G, J, W, _, K, H, V, B, Y, Q, X;
310
311
  const i = JSON.parse(p.data);
311
312
  if (p.event === "connected")
312
- s.sessionId = i.sessionId, s.requestId = i.requestId, i.control && (s.control = i.control, (D = ($ = s.callbacks).onControlUpdate) == null || D.call($, i.control));
313
+ s.sessionId = i.sessionId, s.requestId = i.requestId, i.control && (s.control = i.control, (A = (D = s.callbacks).onControlUpdate) == null || A.call(D, i.control));
313
314
  else if (p.event === "upgrade_to_websocket")
314
- we({
315
+ Ce({
315
316
  sessionId: s.sessionId,
316
317
  requestId: i.requestId
317
318
  });
318
319
  else if (p.event === "update")
319
- i.control && (s.control = i.control, (L = (q = s.callbacks).onControlUpdate) == null || L.call(q, i.control));
320
+ i.control && (s.control = i.control, (z = (L = s.callbacks).onControlUpdate) == null || z.call(L, i.control));
320
321
  else if (i.error) {
321
322
  const m = i.error && typeof i.error == "string" ? i.error : "Failed to connect to the system", k = s.messages.length - 1, S = {
322
323
  ...s.messages[k],
@@ -324,8 +325,8 @@ function be({ text: e, html: n, context: o, attachments: r }) {
324
325
  errorText: m
325
326
  };
326
327
  s.messages = s.messages.map(
327
- (v, de) => de === k ? S : v
328
- ), (F = (z = s.callbacks).onMessageUpdate) == null || F.call(z, k, S), d(new Error(m));
328
+ ($, ue) => ue === k ? S : $
329
+ ), (G = (F = s.callbacks).onMessageUpdate) == null || G.call(F, k, S), c(new Error(m));
329
330
  } else if (p.event === "done") {
330
331
  const m = s.messages.length - 1, E = {
331
332
  ...s.messages[m],
@@ -333,9 +334,9 @@ function be({ text: e, html: n, context: o, attachments: r }) {
333
334
  done: !0
334
335
  };
335
336
  s.messages = s.messages.map(
336
- (S, v) => v === m ? E : S
337
- ), (J = (G = s.callbacks).onMessageUpdate) == null || J.call(G, m, E), l(s.sessionId);
338
- } else if (i.message !== void 0 || ((W = i.attachments) == null ? void 0 : W.length) > 0) {
337
+ (S, $) => $ === m ? E : S
338
+ ), (W = (J = s.callbacks).onMessageUpdate) == null || W.call(J, m, E), d(s.sessionId);
339
+ } else if (i.message !== void 0 || ((_ = i.attachments) == null ? void 0 : _.length) > 0) {
339
340
  if (i.role === y.SUPERVISOR) {
340
341
  const S = {
341
342
  role: y.SUPERVISOR,
@@ -344,7 +345,7 @@ function be({ text: e, html: n, context: o, attachments: r }) {
344
345
  sources: i.sources,
345
346
  done: !0
346
347
  };
347
- h(S), l(s.sessionId), s.sessionId = (_ = i.sessionId) != null ? _ : s.sessionId, s.requestId = (K = i.requestId) != null ? K : s.requestId;
348
+ h(S), d(s.sessionId), s.sessionId = (K = i.sessionId) != null ? K : s.sessionId, s.requestId = (H = i.requestId) != null ? H : s.requestId;
348
349
  return;
349
350
  }
350
351
  if (i.streamId !== void 0) {
@@ -366,11 +367,11 @@ function be({ text: e, html: n, context: o, attachments: r }) {
366
367
  text: (k.text || "") + i.message,
367
368
  sources: i.sources,
368
369
  attachments: i.attachments,
369
- done: (H = i.done) != null ? H : k.done
370
+ done: (V = i.done) != null ? V : k.done
370
371
  };
371
372
  s.messages = s.messages.map(
372
- (S, v) => v === m ? E : S
373
- ), (B = (V = s.callbacks).onMessageUpdate) == null || B.call(V, m, E), s.sessionId = (Y = i.sessionId) != null ? Y : s.sessionId, s.requestId = (Q = i.requestId) != null ? Q : s.requestId;
373
+ (S, $) => $ === m ? E : S
374
+ ), (Y = (B = s.callbacks).onMessageUpdate) == null || Y.call(B, m, E), s.sessionId = (Q = i.sessionId) != null ? Q : s.sessionId, s.requestId = (X = i.requestId) != null ? X : s.requestId;
374
375
  }
375
376
  },
376
377
  onerror: (p) => {
@@ -378,22 +379,22 @@ function be({ text: e, html: n, context: o, attachments: r }) {
378
379
  },
379
380
  openWhenHidden: !0
380
381
  });
381
- } catch (g) {
382
- const I = "Failed to connect to the system", C = s.messages.length - 1, p = s.messages[C], i = {
382
+ } catch (I) {
383
+ const v = "Failed to connect to the system", C = s.messages.length - 1, p = s.messages[C], i = {
383
384
  ...p,
384
385
  loading: !1,
385
- errorText: p.done ? void 0 : g.message || I,
386
+ errorText: p.done ? void 0 : I.message || v,
386
387
  done: !0
387
388
  };
388
389
  s.messages = s.messages.map(
389
- ($, D) => D === C ? i : $
390
- ), (f = (u = s.callbacks).onMessageUpdate) == null || f.call(u, C, i), d(g);
390
+ (D, A) => A === C ? i : D
391
+ ), (f = (g = s.callbacks).onMessageUpdate) == null || f.call(g, C, i), c(I);
391
392
  }
392
393
  })();
393
394
  });
394
395
  }
395
- const Te = "Something went wrong initializing the chat", N = "Chat SDK not initialized";
396
- async function ve(e) {
396
+ const ve = "Something went wrong initializing the chat", M = "Chat SDK not initialized";
397
+ async function $e(e) {
397
398
  const { endpoint: n } = e, o = `${n}/config`, r = await fetch(o, {
398
399
  method: "GET",
399
400
  headers: {
@@ -402,23 +403,23 @@ async function ve(e) {
402
403
  });
403
404
  if (!r.ok) {
404
405
  const c = await r.json();
405
- throw new Error((c == null ? void 0 : c.error) || Te);
406
+ throw new Error((c == null ? void 0 : c.error) || ve);
406
407
  }
407
408
  return (await r.json()).data;
408
409
  }
409
- async function _e() {
410
+ async function Ke() {
410
411
  const e = new URLSearchParams({
411
- externalId: P()
412
- }), n = await ae(`/sessions?${e.toString()}`, "GET");
412
+ externalId: O()
413
+ }), n = await ce(`/sessions?${e.toString()}`, "GET");
413
414
  if (!n.ok)
414
415
  throw new Error("Unable to load history, please try again later");
415
416
  return n.json();
416
417
  }
417
- async function $e(e) {
418
+ async function De(e) {
418
419
  var c;
419
420
  const n = new URLSearchParams({
420
421
  sessionId: e
421
- }), o = await ae(`/session?${n.toString()}`, "GET");
422
+ }), o = await ce(`/session?${n.toString()}`, "GET");
422
423
  if (!o.ok)
423
424
  throw new Error("Unable to load messages, please try again later");
424
425
  const r = await o.json(), l = r == null ? void 0 : r.control, d = ((c = r == null ? void 0 : r.history) != null ? c : []).map((u) => ({
@@ -433,26 +434,26 @@ async function $e(e) {
433
434
  }));
434
435
  return { control: l, messages: d };
435
436
  }
436
- function Ke(e, n, o) {
437
+ function He(e, n, o) {
437
438
  const r = b(), { endpoint: l, token: d } = r || {};
438
439
  if (!l) {
439
- const g = new Error(N);
440
- return o && o(g, null), null;
440
+ const f = new Error(M);
441
+ return o && o(f, null), null;
441
442
  }
442
443
  const c = new XMLHttpRequest(), u = new FormData();
443
444
  u.append("file", e);
444
- const f = `${l}/upload`;
445
- return c.open("POST", f), d && c.setRequestHeader("Authorization", `Bearer ${d}`), n && c.upload.addEventListener("progress", (g) => {
446
- if (g.lengthComputable) {
447
- const I = g.loaded / g.total * 100;
448
- n(I, g.loaded, g.total);
445
+ const g = `${l}/upload`;
446
+ return c.open("POST", g), d && c.setRequestHeader("Authorization", `Bearer ${d}`), n && c.upload.addEventListener("progress", (f) => {
447
+ if (f.lengthComputable) {
448
+ const I = f.loaded / f.total * 100;
449
+ n(I, f.loaded, f.total);
449
450
  }
450
451
  }), c.addEventListener("load", () => {
451
452
  if (c.status >= 200 && c.status < 300)
452
453
  try {
453
- const g = JSON.parse(c.responseText);
454
- o && o(null, g);
455
- } catch (g) {
454
+ const f = JSON.parse(c.responseText);
455
+ o && o(null, f);
456
+ } catch (f) {
456
457
  o && o(new Error("Failed to parse response"), null);
457
458
  }
458
459
  else
@@ -463,10 +464,10 @@ function Ke(e, n, o) {
463
464
  o && o(new Error("Upload aborted"), null);
464
465
  }), c.send(u), c;
465
466
  }
466
- async function He(e) {
467
+ async function Ve(e) {
467
468
  const n = b(), { endpoint: o, token: r } = n || {};
468
469
  if (!o)
469
- throw new Error(N);
470
+ throw new Error(M);
470
471
  const l = `${o}/delete/${e}`, d = {};
471
472
  r && (d.Authorization = `Bearer ${r}`);
472
473
  const c = await fetch(l, {
@@ -477,16 +478,16 @@ async function He(e) {
477
478
  throw new Error(`Delete failed with status ${c.status}`);
478
479
  return { success: !0 };
479
480
  }
480
- function Ve(e) {
481
+ function Be(e) {
481
482
  const n = b(), { endpoint: o } = n || {};
482
483
  if (!o)
483
- throw new Error(N);
484
+ throw new Error(M);
484
485
  return `${o}/upload/${e}`;
485
486
  }
486
- async function ae(e, n = "GET", o = null) {
487
+ async function ce(e, n = "GET", o = null) {
487
488
  const r = b(), { endpoint: l, token: d } = r || {};
488
489
  if (!l)
489
- throw new Error(N);
490
+ throw new Error(M);
490
491
  const c = `${l}${e}`, u = {
491
492
  "Content-Type": "application/json"
492
493
  };
@@ -496,7 +497,7 @@ async function ae(e, n = "GET", o = null) {
496
497
  body: o ? JSON.stringify(o) : null
497
498
  });
498
499
  }
499
- function ce(e = {}) {
500
+ function ie(e = {}) {
500
501
  return {
501
502
  sessionId: void 0,
502
503
  socket: null,
@@ -514,31 +515,31 @@ function ce(e = {}) {
514
515
  pendingRemoteIceCandidates: []
515
516
  };
516
517
  }
517
- let t = ce();
518
- const De = {
518
+ let t = ie();
519
+ const Ae = {
519
520
  iceServers: [{ urls: "stun:stun.l.google.com:19302" }, { urls: "stun:stun1.l.google.com:19302" }]
520
521
  };
521
- function Be(e) {
522
+ function Ye(e) {
522
523
  t.callbacks = { ...t.callbacks, ...e };
523
524
  }
524
- function ie() {
525
- 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), U();
525
+ function le() {
526
+ 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();
526
527
  const e = t.callbacks;
527
- t = ce(e);
528
+ t = ie(e);
528
529
  }
529
530
  function w(e) {
530
531
  var n, o;
531
532
  t.callStatus = e, (o = (n = t.callbacks).onCallStatus) == null || o.call(n, e);
532
533
  }
533
- function R(e) {
534
+ function U(e) {
534
535
  var n, o;
535
536
  (o = (n = t.callbacks).onCallError) == null || o.call(n, e);
536
537
  }
537
- function U() {
538
+ function P() {
538
539
  t.pingInterval && (clearInterval(t.pingInterval), t.pingInterval = null);
539
540
  }
540
- function Ae() {
541
- U(), t.pingInterval = setInterval(() => {
541
+ function Re() {
542
+ P(), t.pingInterval = setInterval(() => {
542
543
  if (t.socket && t.socket.readyState === WebSocket.OPEN) {
543
544
  t.pingCount++;
544
545
  const e = {
@@ -548,16 +549,16 @@ function Ae() {
548
549
  };
549
550
  T(e);
550
551
  } else
551
- U();
552
+ P();
552
553
  }, 1e4);
553
554
  }
554
- function Re() {
555
+ function Ue() {
555
556
  t.lastPongTime = Date.now();
556
557
  }
557
558
  function T(e) {
558
559
  t.socket && t.socket.readyState === WebSocket.OPEN && t.socket.send(JSON.stringify(e));
559
560
  }
560
- async function Ue() {
561
+ async function Pe() {
561
562
  try {
562
563
  t.localStream = await navigator.mediaDevices.getUserMedia({
563
564
  audio: !0,
@@ -567,8 +568,8 @@ async function Ue() {
567
568
  throw e;
568
569
  }
569
570
  }
570
- function Pe() {
571
- t.peerConnection = new RTCPeerConnection(De), t.peerConnection.onicecandidate = (e) => {
571
+ function Oe() {
572
+ t.peerConnection = new RTCPeerConnection(Ae), t.peerConnection.onicecandidate = (e) => {
572
573
  if (e.candidate) {
573
574
  const n = JSON.stringify(e.candidate);
574
575
  t.peerConnection && t.peerConnection.remoteDescription ? T({
@@ -584,11 +585,11 @@ function Pe() {
584
585
  });
585
586
  }, t.peerConnection.onconnectionstatechange = () => {
586
587
  const e = t.peerConnection.connectionState;
587
- e === "connected" ? w("connected") : (e === "disconnected" || e === "closed") && (w("disconnected"), le());
588
+ e === "connected" ? w("connected") : (e === "disconnected" || e === "closed") && (w("disconnected"), de());
588
589
  }, t.peerConnection.oniceconnectionstatechange = () => {
589
590
  };
590
591
  }
591
- function Oe(e) {
592
+ function Ne(e) {
592
593
  return new Promise((n, o) => {
593
594
  if (t.socket && (t.socket.readyState === WebSocket.CONNECTING || t.socket.readyState === WebSocket.OPEN)) {
594
595
  n(t.socket.readyState === WebSocket.OPEN);
@@ -599,7 +600,7 @@ function Oe(e) {
599
600
  o(new Error("SDK not initialized. Please initialize SDK first."));
600
601
  return;
601
602
  }
602
- const l = Se(r.endpoint);
603
+ const l = ke(r.endpoint);
603
604
  if (!l) {
604
605
  o(
605
606
  new Error(
@@ -608,54 +609,54 @@ function Oe(e) {
608
609
  );
609
610
  return;
610
611
  }
611
- const d = P(), c = new URLSearchParams({
612
+ const d = O(), c = new URLSearchParams({
612
613
  externalId: d
613
614
  });
614
615
  e.sessionId && c.set("sessionId", e.sessionId), r.token && c.set("token", r.token);
615
616
  const u = `${l}?${c.toString()}`;
616
- t.socket = new WebSocket(u), t.socket.onopen = (f) => {
617
- Ae(), n(!0);
618
- }, t.socket.onmessage = (f) => {
619
- const g = JSON.parse(f.data);
620
- Ne(g);
621
- }, t.socket.onerror = (f) => {
622
- w("error"), R(f.message || "Unable to connect voice"), o(f);
623
- }, t.socket.onclose = (f) => {
624
- U();
617
+ t.socket = new WebSocket(u), t.socket.onopen = (g) => {
618
+ Re(), n(!0);
619
+ }, t.socket.onmessage = (g) => {
620
+ const f = JSON.parse(g.data);
621
+ Me(f);
622
+ }, t.socket.onerror = (g) => {
623
+ w("error"), U(g.message || "Unable to connect voice"), o(g);
624
+ }, t.socket.onclose = (g) => {
625
+ P();
625
626
  };
626
627
  });
627
628
  }
628
- function Ne(e) {
629
+ function Me(e) {
629
630
  switch (e.type) {
630
631
  case "pong":
631
- Re();
632
+ Ue();
632
633
  break;
633
634
  case "answer":
634
- xe(e.data);
635
+ qe(e.data);
635
636
  break;
636
637
  case "connected":
637
- Me(e.data);
638
+ xe(e.data);
638
639
  break;
639
640
  case "ice":
640
- qe(e.data);
641
+ Le(e.data);
641
642
  break;
642
643
  case "renegotiationOffer":
643
- Le(e.data);
644
+ ze(e.data);
644
645
  break;
645
646
  case "end":
646
- le();
647
+ de();
647
648
  break;
648
649
  case "error":
649
- w("error"), R(e.error || "Unable to connect voice");
650
+ w("error"), U(e.error || "Unable to connect voice");
650
651
  break;
651
652
  default:
652
653
  break;
653
654
  }
654
655
  }
655
- function Me(e) {
656
- t.sessionId = e.sessionId, ye(e.sessionId);
656
+ function xe(e) {
657
+ t.sessionId = e.sessionId, be(e.sessionId);
657
658
  }
658
- async function xe(e) {
659
+ async function qe(e) {
659
660
  try {
660
661
  if (t.peerConnection) {
661
662
  const n = new RTCSessionDescription({
@@ -682,7 +683,7 @@ async function xe(e) {
682
683
  } catch (n) {
683
684
  }
684
685
  }
685
- async function qe(e) {
686
+ async function Le(e) {
686
687
  try {
687
688
  if (t.peerConnection) {
688
689
  if (!t.peerConnection.remoteDescription) {
@@ -695,7 +696,7 @@ async function qe(e) {
695
696
  } catch (n) {
696
697
  }
697
698
  }
698
- async function Le(e) {
699
+ async function ze(e) {
699
700
  try {
700
701
  if (t.peerConnection) {
701
702
  const n = new RTCSessionDescription({
@@ -714,13 +715,13 @@ async function Le(e) {
714
715
  } catch (n) {
715
716
  }
716
717
  }
717
- async function Ye(e = {}) {
718
+ async function Qe(e = {}) {
718
719
  try {
719
720
  if (t.callStatus === "connecting" || t.callStatus === "connected")
720
721
  return;
721
- w("connecting"), R(null), t.sessionId = e.sessionId, await Ue(), Pe(), t.localStream.getTracks().forEach((o) => {
722
+ w("connecting"), U(null), t.sessionId = e.sessionId, await Pe(), Oe(), t.localStream.getTracks().forEach((o) => {
722
723
  t.peerConnection.addTrack(o, t.localStream);
723
- }), await Oe(e);
724
+ }), await Ne(e);
724
725
  const n = await t.peerConnection.createOffer();
725
726
  await t.peerConnection.setLocalDescription(n), T({
726
727
  type: "offer",
@@ -729,15 +730,15 @@ async function Ye(e = {}) {
729
730
  }
730
731
  });
731
732
  } catch (n) {
732
- w("error"), R(n.message || "Unable to connect voice"), ie();
733
+ w("error"), U(n.message || "Unable to connect voice"), le();
733
734
  }
734
735
  }
735
- function le() {
736
+ function de() {
736
737
  T({
737
738
  type: "end"
738
- }), t.socket && (t.socket.close(), t.socket = null), w("disconnected"), t.peerConnection && (t.peerConnection.close(), t.peerConnection = null), t.localStream && (t.localStream.getTracks().forEach((e) => e.stop()), t.localStream = null), ie();
739
+ }), t.socket && (t.socket.close(), t.socket = null), w("disconnected"), t.peerConnection && (t.peerConnection.close(), t.peerConnection = null), t.localStream && (t.localStream.getTracks().forEach((e) => e.stop()), t.localStream = null), le();
739
740
  }
740
- function Qe() {
741
+ function Xe() {
741
742
  if (t.localStream) {
742
743
  const e = t.localStream.getAudioTracks()[0];
743
744
  if (e)
@@ -745,10 +746,10 @@ function Qe() {
745
746
  }
746
747
  return !1;
747
748
  }
748
- function Xe() {
749
+ function Ze() {
749
750
  return t.localStream;
750
751
  }
751
- function Ze() {
752
+ function je() {
752
753
  return new Promise((e, n) => {
753
754
  if (!t.peerConnection) {
754
755
  n(new Error("no peer connection"));
@@ -763,7 +764,7 @@ function Ze() {
763
764
  });
764
765
  });
765
766
  }
766
- function je() {
767
+ function en() {
767
768
  return new Promise((e, n) => {
768
769
  if (!t.peerConnection) {
769
770
  n(new Error("no peer connection"));
@@ -780,23 +781,23 @@ function je() {
780
781
  }
781
782
  export {
782
783
  y as MESSAGE_ROLES,
783
- ve as authenticate,
784
- He as deleteAttachment,
785
- We as disconnect,
786
- le as disconnectCall,
787
- Ve as getAttachment,
788
- _e as getHistory,
789
- Ze as getInboundAudioEnergy,
790
- Xe as getLocalStream,
791
- je as getOutboundAudioEnergy,
792
- $e as getSession,
793
- Ge as initialize,
794
- be as sendMessage,
795
- Be as setCallCallbacks,
796
- Fe as setCallbacks,
797
- Ye as startCall,
798
- Je as startChat,
799
- Qe as toggleMute,
800
- Ke as uploadAttachment
784
+ $e as authenticate,
785
+ Ve as deleteAttachment,
786
+ _e as disconnect,
787
+ de as disconnectCall,
788
+ Be as getAttachment,
789
+ Ke as getHistory,
790
+ je as getInboundAudioEnergy,
791
+ Ze as getLocalStream,
792
+ en as getOutboundAudioEnergy,
793
+ De as getSession,
794
+ Je as initialize,
795
+ Te as sendMessage,
796
+ Ye as setCallCallbacks,
797
+ Ge as setCallbacks,
798
+ Qe as startCall,
799
+ We as startChat,
800
+ Xe as toggleMute,
801
+ He as uploadAttachment
801
802
  };
802
803
  //# sourceMappingURL=origon-chat-sdk.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"origon-chat-sdk.js","sources":["../src/utils.js","../src/constants.js","../src/socket.js","../src/chat.js","../src/http.js","../src/call.js"],"sourcesContent":["/**\n * Utility functions for the Chat SDK\n */\n\nexport function uuidv7() {\n const timestamp = Date.now()\n const bytes = new Uint8Array(16)\n crypto.getRandomValues(bytes)\n\n // Set timestamp (48 bits)\n bytes[0] = (timestamp >> 40) & 0xff\n bytes[1] = (timestamp >> 32) & 0xff\n bytes[2] = (timestamp >> 24) & 0xff\n bytes[3] = (timestamp >> 16) & 0xff\n bytes[4] = (timestamp >> 8) & 0xff\n bytes[5] = timestamp & 0xff\n\n // Set version 7 (0111)\n bytes[6] = (bytes[6] & 0x0f) | 0x70\n\n // Set variant (10xx)\n bytes[8] = (bytes[8] & 0x3f) | 0x80\n\n const hex = [...bytes].map((b) => b.toString(16).padStart(2, '0')).join('')\n return `${hex.slice(0, 8)}-${hex.slice(8, 12)}-${hex.slice(12, 16)}-${hex.slice(\n 16,\n 20\n )}-${hex.slice(20)}`\n}\n\nexport function getDeviceId() {\n if (localStorage.getItem('chatDeviceId')) {\n return localStorage.getItem('chatDeviceId')\n }\n\n const deviceId = uuidv7()\n localStorage.setItem('chatDeviceId', deviceId)\n return deviceId\n}\n\nexport function parseJwt(token) {\n try {\n const base64Url = token.split('.')[1]\n const base64 = base64Url.replace(/-/g, '+').replace(/_/g, '/')\n const jsonPayload = decodeURIComponent(\n atob(base64)\n .split('')\n .map(function (c) {\n return '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2)\n })\n .join('')\n )\n\n return JSON.parse(jsonPayload)\n } catch {\n return null\n }\n}\n\nexport async function sleep(ms) {\n return new Promise((resolve) => setTimeout(resolve, ms))\n}\n\nexport function getSseEndpoint(baseUrl) {\n let sseEndpoint\n try {\n const url = new URL(baseUrl)\n sseEndpoint = `https://${url.hostname}${url.pathname}/sse`\n } catch {\n console.error('SSE Invalid base URL: ', baseUrl)\n }\n return sseEndpoint\n}\n\nexport function getSocketEndpoint(baseUrl) {\n let socketEndpoint\n try {\n const url = new URL(baseUrl)\n socketEndpoint = `wss://${url.hostname}${url.pathname}/wss`\n } catch {\n console.error('Socket Invalid base URL: ', baseUrl)\n }\n return socketEndpoint\n}\n\nexport function getCallServerEndpoint(baseUrl) {\n let socketEndpoint\n try {\n const url = new URL(baseUrl)\n socketEndpoint = `wss://${url.hostname}${url.pathname}/audio`\n } catch {\n console.error('getCallServerEndpoint: Invalid base URL: ', baseUrl)\n }\n return socketEndpoint\n}\n","/**\n * Constants for the Chat SDK\n */\n\nexport const MESSAGE_ROLES = {\n ASSISTANT: 'assistant', // this can be automated or LLM AI Agent response\n USER: 'user', // this is widget user\n SUPERVISOR: 'supervisor', // this is human supervisor (ex. Samespace Dock agent, or Resolve human agent)\n SYSTEM: 'system' // this is system message, for ex \"Agent joined\" / \"Agent left\"\n}\n","/**\n * Socket Service for Chat SDK\n * Handles WebSocket connection for real-time chat functionality\n */\n\nimport { getSocketEndpoint, uuidv7 } from './utils.js'\nimport {\n getCredentials,\n getExternalId,\n addMessage,\n toggleTypingStatus,\n setTransport\n} from './chat.js'\n\nconst PING_INTERVAL = 10000\nconst SOCKET_TIMEOUT = 5000\n\n/**\n * @typedef {Object} SocketSession\n * @property {WebSocket} [socket]\n * @property {boolean} previouslyConnected\n * @property {boolean} socketDisconnected\n * @property {NodeJS.Timeout} [pingInterval]\n * @property {NodeJS.Timeout} [socketDisconnectedTimeout]\n * @property {NodeJS.Timeout} [socketConnectionTimeout]\n */\n\n/**\n * Socket Events\n */\nexport const SocketEvents = {\n MESSAGE: 'message',\n TYPING: 'typing',\n TYPING_STOP: 'typingOff',\n END: 'end'\n}\n\n/**\n * Create a new socket session\n * @returns {SocketSession}\n */\nfunction createSession() {\n return {\n socket: null,\n previouslyConnected: false,\n socketDisconnected: false,\n pingInterval: null,\n socketDisconnectedTimeout: null,\n socketConnectionTimeout: null\n }\n}\n\n/** @type {SocketSession} */\nlet currentSession = createSession()\n\n/**\n * Stop ping interval\n */\nfunction stopPingInterval() {\n if (currentSession.pingInterval) {\n clearInterval(currentSession.pingInterval)\n currentSession.pingInterval = null\n }\n}\n\n/**\n * Start ping interval\n */\nfunction startPingInterval() {\n stopPingInterval()\n\n currentSession.pingInterval = setInterval(() => {\n if (currentSession.socket && currentSession.socket.readyState === WebSocket.OPEN) {\n send({ type: 'ping' })\n console.log('Sending keep-alive ping')\n } else {\n console.log('Socket not open, stopping ping interval')\n stopPingInterval()\n }\n }, PING_INTERVAL)\n}\n\n/**\n * Clear all timeouts\n */\nfunction clearAllTimeouts() {\n stopPingInterval()\n if (currentSession.socketDisconnectedTimeout) {\n clearTimeout(currentSession.socketDisconnectedTimeout)\n currentSession.socketDisconnectedTimeout = null\n }\n if (currentSession.socketConnectionTimeout) {\n clearTimeout(currentSession.socketConnectionTimeout)\n currentSession.socketConnectionTimeout = null\n }\n}\n\n/**\n * Handle socket connected state\n */\nfunction handleSocketConnected() {\n console.log('handleSocketConnected')\n currentSession.socketDisconnected = false\n setTransport('socket')\n}\n\n/**\n * Handle socket disconnected state\n */\nfunction handleSocketDisconnected() {\n console.log('handleSocketDisconnected')\n currentSession.socketDisconnected = true\n setTransport('sse')\n}\n\n/**\n * Connect to socket\n * @param {{ token: string, sessionId?: string }} payload\n * @returns {Promise<boolean>}\n */\nexport function connectSocket(payload) {\n return new Promise((fulfill, reject) => {\n if (\n currentSession.socket &&\n (currentSession.socket.readyState === WebSocket.CONNECTING ||\n currentSession.socket.readyState === WebSocket.OPEN)\n ) {\n console.log('Socket in connecting/open state, returning.')\n fulfill(currentSession.socket.readyState === WebSocket.OPEN)\n return\n }\n\n console.log('Initializing socket connection..')\n const credentials = getCredentials()\n if (!credentials || !credentials.endpoint) {\n reject(new Error('SDK not initialized. Please initialize SDK first.'))\n return\n }\n\n const socketEndpoint = getSocketEndpoint(credentials.endpoint)\n if (!socketEndpoint) {\n reject(\n new Error(\n 'Invalid endpoint while initializing SDK. Please check the endpoint and try again.'\n )\n )\n return\n }\n\n const externalId = getExternalId()\n const queryParams = new URLSearchParams({\n externalId\n })\n\n if (payload.sessionId) {\n queryParams.set('sessionId', payload.sessionId)\n }\n if (payload.requestId) {\n queryParams.set('requestId', payload.requestId)\n }\n if (credentials.token) {\n queryParams.set('token', credentials.token)\n }\n\n const socketUrl = `${socketEndpoint}?${queryParams.toString()}`\n currentSession.socket = new WebSocket(socketUrl)\n\n currentSession.socket.onopen = () => {\n console.log('-------- socket connected --------')\n currentSession.previouslyConnected = true\n handleSocketConnected()\n send({ type: 'ping' })\n clearTimeout(currentSession.socketConnectionTimeout)\n startPingInterval()\n fulfill(true)\n }\n\n currentSession.socket.onmessage = (event) => {\n const message = JSON.parse(event.data)\n handleSocketEvent(message)\n }\n\n currentSession.socket.onerror = (error) => {\n console.error('Socket error:', error)\n setTransport('sse')\n reject(error)\n }\n\n currentSession.socket.onclose = (ws) => {\n console.log('-------- socket disconnected --------: ', ws.code, ws.reason)\n\n if (ws.target === currentSession.socket) {\n if (ws.code === 1006) {\n // abnormal closure\n if (currentSession.previouslyConnected) {\n handleSocketDisconnected()\n } else {\n addMessage({\n errorText: 'Unable to establish connection',\n done: true,\n timestamp: new Date().toISOString()\n })\n }\n clearTimeout(currentSession.socketConnectionTimeout)\n }\n\n currentSession.socket = null\n clearAllTimeouts()\n }\n }\n\n if (!currentSession.previouslyConnected) {\n currentSession.socketConnectionTimeout = setTimeout(() => {\n console.error('Socket connection timed out')\n addMessage({\n errorText: 'Unable to establish connection',\n done: true,\n timestamp: new Date().toISOString()\n })\n reject(new Error('Socket connection timed out'))\n }, SOCKET_TIMEOUT)\n }\n })\n}\n\n/**\n * Send data through socket\n * @param {Object} data\n */\nexport function send(data) {\n console.log('sending socket event: ', data.type)\n if (currentSession.socketDisconnected || !currentSession.socket) {\n return\n }\n currentSession.socket.send(JSON.stringify({ ...data, eventId: data.eventId || uuidv7() }))\n}\n\n/**\n * Send data through socket and wait for acknowledgment\n * @param {Object} data\n * @returns {Promise<Object>}\n */\nexport function sendWithAck(data) {\n return new Promise((resolve, reject) => {\n if (!currentSession.socket) {\n console.error('sendWithAck: socket instance not found or not connected')\n reject(new Error('Socket instance not found or not connected'))\n return\n }\n\n const autoRejectTimeout = setTimeout(() => {\n reject(new Error('Timeout'))\n }, 5000)\n\n const eventId = data.eventId || uuidv7()\n\n const onMessage = (event) => {\n const eventData = JSON.parse(event.data)\n if (eventData.eventId === eventId) {\n clearTimeout(autoRejectTimeout)\n currentSession.socket.removeEventListener('message', onMessage)\n if (eventData.data) {\n resolve(eventData.data)\n } else {\n reject(new Error(eventData.error?.message ?? 'Unknown error'))\n }\n }\n }\n currentSession.socket.addEventListener('message', onMessage)\n currentSession.socket.send(JSON.stringify({ ...data, eventId }))\n })\n}\n\n/**\n * Handle socket event\n * @param {Object} event\n */\nfunction handleSocketEvent(event) {\n console.log('received socket event: ', event.type)\n\n switch (event.type) {\n case 'pong': {\n if (currentSession.socketDisconnected) {\n handleSocketConnected()\n }\n if (currentSession.socketDisconnectedTimeout) {\n clearTimeout(currentSession.socketDisconnectedTimeout)\n }\n currentSession.socketDisconnectedTimeout = setTimeout(() => {\n console.log('---- socket ping timeout ----')\n handleSocketDisconnected()\n }, PING_INTERVAL + 1000)\n break\n }\n case SocketEvents.TYPING: {\n toggleTypingStatus(true)\n break\n }\n case SocketEvents.TYPING_STOP: {\n toggleTypingStatus(false)\n break\n }\n case SocketEvents.MESSAGE: {\n const { eventId, data } = event\n if (!eventId) {\n addMessage({\n ...data,\n done: true,\n timestamp: new Date().toISOString()\n })\n }\n break\n }\n case SocketEvents.END: {\n disconnect()\n break\n }\n default:\n break\n }\n}\n\n/**\n * Reconnect to socket\n */\nexport function reconnect() {\n if (currentSession.socket) {\n send({ type: 'ping' })\n }\n}\n\n/**\n * Disconnect socket\n */\nexport function disconnect() {\n console.log('Disconnecting socket')\n if (currentSession.socket) {\n currentSession.socket.close(1000)\n }\n currentSession.previouslyConnected = false\n clearAllTimeouts()\n currentSession.socket = null\n setTransport('sse')\n}\n\n/**\n * Check if socket is connected\n * @returns {boolean}\n */\nexport function isConnected() {\n return (\n currentSession.socket !== null &&\n currentSession.socket.readyState === WebSocket.OPEN &&\n !currentSession.socketDisconnected\n )\n}\n\n/**\n * Check if socket is disconnected\n * @returns {boolean}\n */\nexport function isDisconnected() {\n return currentSession.socketDisconnected\n}\n","/**\n * Chat Service for Chat SDK\n * Handles real-time chat functionality without depending on external state\n * Uses callbacks to communicate state changes to the consumer\n */\n\nimport { fetchEventSource } from '@microsoft/fetch-event-source'\nimport { getSession, authenticate } from './http.js'\nimport { getDeviceId, getSseEndpoint, sleep } from './utils.js'\nimport { MESSAGE_ROLES } from './constants.js'\nimport {\n connectSocket,\n send as socketSend,\n disconnect as disconnectSocket,\n isConnected as isSocketConnected\n} from './socket.js'\n\n/**\n * @typedef {Object} ChatCallbacks\n * @property {(message: Object) => void} [onMessageAdd] - Called when a new message is added\n * @property {(index: number, updatedMsg: Object) => void} [onMessageUpdate] - Called when an existing message is updated\n * @property {(sessionId: string) => void} [onSessionUpdate] - Called when session ID is updated\n * @property {(transport: TransportType) => void} [onTransportUpdate] - Called when transport type changes\n * @property {(control: 'agent' | 'human') => void} [onControlUpdate] - Called when control changes between agent and human\n */\n\n/**\n * @typedef {'sse' | 'socket'} TransportType\n */\n\n/**\n * @typedef {Object} ChatSession\n * @property {string} sessionId\n * @property {string} sseUrl\n * @property {string} [requestId]\n * @property {AbortController} [abortController]\n * @property {string} [lastStreamId]\n * @property {Array} messages\n * @property {ChatCallbacks} callbacks\n * @property {TransportType} transport\n */\n\n/**\n * Create a new chat session\n * @param {ChatCallbacks} [callbacks={}]\n * @returns {ChatSession}\n */\nfunction createSession(callbacks = {}) {\n return {\n credentials: undefined,\n authenticated: false,\n configData: undefined,\n sessionId: undefined,\n requestId: undefined,\n sseUrl: undefined,\n abortController: undefined,\n lastStreamId: undefined,\n messages: [],\n callbacks,\n transport: 'sse',\n control: 'agent'\n }\n}\n\n/** @type {ChatSession} */\nlet currentSession = createSession()\n\n/**\n * Set callbacks for the current session\n * @param {ChatCallbacks} callbacks\n */\nexport function setCallbacks(callbacks) {\n currentSession.callbacks = { ...currentSession.callbacks, ...callbacks }\n}\n\n/**\n * Initialize the chat session\n * @param {Object} credentials - Credentials for the chat\n */\nexport function initialize(credentials) {\n console.log('Initializing chat...', credentials)\n currentSession.credentials = credentials\n if (credentials.token) {\n currentSession.authenticated = true\n }\n}\n\n/**\n * Get current chat session credentials\n * @returns {{ endpoint: string, apiKey: string } | undefined}\n */\nexport function getCredentials() {\n return currentSession.credentials\n}\n\n/**\n * Update the session ID and notify via callback\n * @param {string} sessionId - The new session ID\n */\nexport function updateSessionId(sessionId) {\n if (sessionId && sessionId !== currentSession.sessionId) {\n currentSession.sessionId = sessionId\n currentSession.callbacks.onSessionUpdate?.(sessionId)\n }\n}\n\n/**\n * Initiate a new chat session or resume an existing one\n * @param {Object} credentials - Credentials for the chat\n * @param {Object} payload - Payload for the chat. It contains sessionId (optional)\n * @param {string} [payload.sessionId] - Optional session ID to resume\n * @returns {Promise<{ sessionId: string, messages: Array }>}\n */\nexport async function startChat(payload = {}) {\n try {\n console.log('startChat: ', payload, currentSession)\n\n let configData = null\n if (!currentSession.authenticated) {\n configData = await authenticate(currentSession.credentials)\n currentSession.authenticated = true\n currentSession.configData = configData\n } else {\n configData = currentSession.configData\n }\n\n let messages = []\n let control = 'agent'\n\n if (payload.sessionId) {\n const session = await getSession(payload.sessionId)\n messages = session.messages\n control = session.control || 'agent'\n }\n\n const searchParams = new URLSearchParams()\n if (!currentSession.credentials.token) {\n searchParams.set('externalId', getExternalId()) // externalId is needed only for public urls, not for internal chat (where token is provided)\n }\n currentSession.sseUrl = `${getSseEndpoint(\n currentSession.credentials.endpoint\n )}?${searchParams.toString()}`\n currentSession.sessionId = payload.sessionId\n currentSession.messages = messages\n currentSession.control = control\n\n if (control === 'human') {\n // Connect to SSE for incoming messages when control is human\n sendMessage({ text: '', html: '' }).catch(() => {})\n }\n\n console.log('Chat initiated successfully')\n\n return {\n sessionId: currentSession.sessionId,\n messages,\n control,\n configData\n }\n } catch (error) {\n console.error(`Failed to start chat: ${error.message}`)\n cleanup()\n throw error\n }\n}\n\n/**\n * Disconnect from the current chat session\n */\nexport function disconnect() {\n cleanup()\n}\n\n/**\n * Clean up the current session\n */\nfunction cleanup() {\n if (currentSession.abortController) {\n currentSession.abortController.abort()\n }\n disconnectSocket()\n\n const { callbacks, credentials } = currentSession\n currentSession = createSession(callbacks)\n currentSession.credentials = credentials\n\n console.log('Chat session cleaned up')\n}\n\nexport function getExternalId() {\n if (currentSession.credentials?.externalId) {\n return currentSession.credentials.externalId\n }\n return getDeviceId()\n}\n\n/**\n * Add a message to the chat\n * @param {Object} message - The message object to add\n */\nexport function addMessage(message) {\n currentSession.messages = [...currentSession.messages, message]\n currentSession.callbacks.onMessageAdd?.(message)\n}\n\nexport function toggleTypingStatus(isTyping) {\n currentSession.callbacks.onTyping?.(isTyping)\n}\n\n/**\n * Set the transport type\n * @param {TransportType} transport\n */\nexport function setTransport(transport) {\n console.log('Setting transport to:', transport)\n currentSession.transport = transport\n currentSession.callbacks.onTransportUpdate?.(transport)\n}\n\n/**\n * Get current transport type\n * @returns {TransportType}\n */\nexport function getTransport() {\n return currentSession.transport\n}\n\n/**\n * Send a message in the current chat session\n * @param {{ text: string, html?: string }} message\n * @returns {Promise<string>}\n */\nexport function sendMessage({ text, html, context, attachments }) {\n return new Promise((resolve, reject) => {\n ;(async () => {\n try {\n const isEmpty = !text && !html && !attachments?.length\n\n // Add user message only if there's content\n if (!isEmpty) {\n const userMessage = {\n role: MESSAGE_ROLES.USER,\n text,\n html,\n timestamp: new Date().toISOString(),\n attachments\n }\n addMessage(userMessage)\n await sleep(200)\n }\n\n // If transport is socket and socket is connected, use socket\n if (currentSession.transport === 'socket' && isSocketConnected()) {\n socketSend({\n type: 'message',\n data: {\n text,\n html\n }\n })\n resolve(currentSession.sessionId)\n return\n }\n\n if (currentSession.control === 'agent') {\n const loadingMessage = {\n role: MESSAGE_ROLES.ASSISTANT,\n text: '',\n loading: true\n }\n addMessage(loadingMessage)\n }\n\n const url = new URL(currentSession.sseUrl)\n if (currentSession.sessionId) {\n url.searchParams.set('sessionId', currentSession.sessionId)\n }\n if (currentSession.requestId) {\n url.searchParams.set('requestId', currentSession.requestId)\n }\n\n currentSession.lastStreamId = undefined\n\n // Create a new abort controller for this request\n currentSession.abortController = new AbortController()\n\n const headers = {\n 'Content-Type': 'application/json'\n }\n if (currentSession.credentials?.token) {\n headers.Authorization = `Bearer ${currentSession.credentials.token}`\n }\n\n await fetchEventSource(url.toString(), {\n method: 'POST',\n headers,\n body: isEmpty\n ? undefined\n : JSON.stringify({\n message: text,\n html,\n context,\n attachments\n }),\n signal: currentSession.abortController.signal,\n openWhenHidden: true,\n onopen: async (response) => {\n if (!response.ok) {\n console.error('Failed to send message bad response: ', response)\n throw new Error('Failed to send message')\n }\n },\n onmessage: (response) => {\n // console.log('Event: ', response)\n const data = JSON.parse(response.data)\n\n if (response.event === 'connected') {\n currentSession.sessionId = data.sessionId\n currentSession.requestId = data.requestId\n console.log('Connected: ', data)\n if (data.control) {\n currentSession.control = data.control\n currentSession.callbacks.onControlUpdate?.(data.control)\n }\n } else if (response.event === 'upgrade_to_websocket') {\n console.log('Upgrade to websocket: ', data)\n connectSocket({\n sessionId: currentSession.sessionId,\n requestId: data.requestId\n })\n } else if (response.event === 'update') {\n if (data.control) {\n currentSession.control = data.control\n currentSession.callbacks.onControlUpdate?.(data.control)\n }\n } else if (data.error) {\n const errorMessage =\n data.error && typeof data.error === 'string'\n ? data.error\n : 'Failed to connect to the system'\n const lastIndex = currentSession.messages.length - 1\n const lastMsg = currentSession.messages[lastIndex]\n const updatedMsg = {\n ...lastMsg,\n loading: false,\n errorText: errorMessage\n }\n currentSession.messages = currentSession.messages.map((msg, index) =>\n index === lastIndex ? updatedMsg : msg\n )\n currentSession.callbacks.onMessageUpdate?.(lastIndex, updatedMsg)\n reject(new Error(errorMessage))\n } else if (response.event === 'done') {\n console.log('Done: ', data)\n const lastIndex = currentSession.messages.length - 1\n const lastMsg = currentSession.messages[lastIndex]\n const updatedMsg = {\n ...lastMsg,\n loading: false,\n done: true\n }\n currentSession.messages = currentSession.messages.map((msg, index) =>\n index === lastIndex ? updatedMsg : msg\n )\n currentSession.callbacks.onMessageUpdate?.(lastIndex, updatedMsg)\n resolve(currentSession.sessionId)\n } else if (data.message !== undefined || data.attachments?.length > 0) {\n // If role is supervisor, treat it as a new message\n if (data.role === MESSAGE_ROLES.SUPERVISOR) {\n const supervisorMessage = {\n role: MESSAGE_ROLES.SUPERVISOR,\n text: data.message,\n attachments: data.attachments,\n sources: data.sources,\n done: true\n }\n addMessage(supervisorMessage)\n resolve(currentSession.sessionId)\n\n // Store session info for reuse\n currentSession.sessionId = data.sessionId ?? currentSession.sessionId\n currentSession.requestId = data.requestId ?? currentSession.requestId\n return\n }\n\n // If streamId changes, start a new assistant message\n if (data.streamId !== undefined) {\n if (currentSession.lastStreamId === undefined) {\n currentSession.lastStreamId = data.streamId\n } else if (data.streamId !== currentSession.lastStreamId) {\n currentSession.lastStreamId = data.streamId\n const newBotMessage = {\n role: MESSAGE_ROLES.ASSISTANT,\n text: '',\n loading: true\n }\n addMessage(newBotMessage)\n }\n }\n\n // Update the last message with new content\n const lastIndex = currentSession.messages.length - 1\n const lastMsg = currentSession.messages[lastIndex]\n const updatedMsg = {\n ...lastMsg,\n loading: false,\n text: (lastMsg.text || '') + data.message,\n sources: data.sources,\n attachments: data.attachments,\n done: data.done ?? lastMsg.done\n }\n currentSession.messages = currentSession.messages.map((msg, index) =>\n index === lastIndex ? updatedMsg : msg\n )\n\n currentSession.callbacks.onMessageUpdate?.(lastIndex, updatedMsg)\n\n // Store session info for reuse\n currentSession.sessionId = data.sessionId ?? currentSession.sessionId\n currentSession.requestId = data.requestId ?? currentSession.requestId\n }\n },\n onerror: (error) => {\n throw error // Rethrow to stop retries\n },\n openWhenHidden: true\n })\n } catch (error) {\n console.error('Failed to send message: ', error)\n const errorMessage = 'Failed to connect to the system'\n const lastIndex = currentSession.messages.length - 1\n const lastMsg = currentSession.messages[lastIndex]\n const updatedMsg = {\n ...lastMsg,\n loading: false,\n errorText: lastMsg.done ? undefined : error.message || errorMessage,\n done: true\n }\n currentSession.messages = currentSession.messages.map((msg, index) =>\n index === lastIndex ? updatedMsg : msg\n )\n currentSession.callbacks.onMessageUpdate?.(lastIndex, updatedMsg)\n reject(error)\n }\n })()\n })\n}\n","/**\n * API Service for Chat SDK\n * Handles all HTTP requests without depending on external state\n */\n\nimport { getCredentials, getExternalId } from './chat.js'\nimport { MESSAGE_ROLES } from './constants.js'\n\nconst AUTHENTICATION_ERROR = 'Something went wrong initializing the chat'\nconst INITIALIZATION_ERROR = 'Chat SDK not initialized'\n\n/**\n * Authenticate with the chat service\n * @param {{ endpoint: string }} credentials\n * @returns {Promise<object>} Authentication response data\n */\nexport async function authenticate(payload) {\n const { endpoint } = payload\n const url = `${endpoint}/config`\n\n const response = await fetch(url, {\n method: 'GET',\n headers: {\n 'Content-Type': 'application/json'\n }\n })\n\n if (!response.ok) {\n const errorPayload = await response.json()\n throw new Error(errorPayload?.error || AUTHENTICATION_ERROR)\n }\n\n const res = await response.json()\n const data = res.data\n\n return data\n}\n\n/**\n * Get chat history for the current device\n * @returns {Promise<{ sessions: Array }>}\n */\nexport async function getHistory() {\n const queryParams = new URLSearchParams({\n externalId: getExternalId()\n })\n const response = await fetchRequest(`/sessions?${queryParams.toString()}`, 'GET')\n\n if (!response.ok) {\n throw new Error('Unable to load history, please try again later')\n }\n\n return response.json()\n}\n\n/**\n * Get session data (control and history) for a specific session\n * @param {string} sessionId\n * @returns {Promise<{ control: string, messages: Array }>}\n */\nexport async function getSession(sessionId) {\n const queryParams = new URLSearchParams({\n sessionId\n })\n const response = await fetchRequest(`/session?${queryParams.toString()}`, 'GET')\n\n if (!response.ok) {\n throw new Error('Unable to load messages, please try again later')\n }\n\n const data = await response.json()\n const control = data?.control\n const messages = (data?.history ?? []).map((msg) => ({\n id: msg.id,\n text: msg.text,\n role: msg.youtubeVideo\n ? MESSAGE_ROLES.ASSISTANT // for youtube video messages, role is \"system\" from backend, we need to make it \"assistant\"\n : msg.role,\n timestamp: msg.timestamp,\n video: msg.youtubeVideo,\n attachments: msg.attachments,\n channel: msg.channel,\n done: true\n }))\n\n return { control, messages }\n}\n\n/**\n * Upload attachment with progress tracking\n * @param {File} file\n * @param {Function} onProgress - callback (percentComplete, loaded, total)\n * @param {Function} onComplete - callback (error, response)\n * @returns {XMLHttpRequest} xhr object to allow abort\n */\nexport function uploadAttachment(file, onProgress, onComplete) {\n const credentials = getCredentials()\n const { endpoint, token } = credentials || {}\n\n if (!endpoint) {\n const error = new Error(INITIALIZATION_ERROR)\n if (onComplete) {\n onComplete(error, null)\n }\n return null\n }\n\n const xhr = new XMLHttpRequest()\n const formData = new FormData()\n formData.append('file', file)\n\n const uploadUrl = `${endpoint}/upload`\n\n xhr.open('POST', uploadUrl)\n if (token) {\n xhr.setRequestHeader('Authorization', `Bearer ${token}`)\n }\n\n // Upload progress callback\n if (onProgress) {\n xhr.upload.addEventListener('progress', (event) => {\n if (event.lengthComputable) {\n const percentComplete = (event.loaded / event.total) * 100\n onProgress(percentComplete, event.loaded, event.total)\n }\n })\n }\n\n // Upload complete callback\n xhr.addEventListener('load', () => {\n if (xhr.status >= 200 && xhr.status < 300) {\n try {\n const response = JSON.parse(xhr.responseText)\n if (onComplete) {\n onComplete(null, response)\n }\n } catch (error) {\n if (onComplete) {\n onComplete(new Error('Failed to parse response'), null)\n }\n }\n } else {\n if (onComplete) {\n onComplete(new Error(`Upload failed with status ${xhr.status}`), null)\n }\n }\n })\n\n xhr.addEventListener('error', () => {\n if (onComplete) {\n onComplete(new Error('Network error during upload'), null)\n }\n })\n\n xhr.addEventListener('abort', () => {\n if (onComplete) {\n onComplete(new Error('Upload aborted'), null)\n }\n })\n\n xhr.send(formData)\n\n return xhr\n}\n\n/**\n * Delete attachment by mediaId\n * @param {string} mediaId\n * @returns {Promise<{ success: boolean }>}\n */\nexport async function deleteAttachment(mediaId) {\n const credentials = getCredentials()\n const { endpoint, token } = credentials || {}\n\n if (!endpoint) {\n throw new Error(INITIALIZATION_ERROR)\n }\n\n const deleteUrl = `${endpoint}/delete/${mediaId}`\n\n const headers = {}\n if (token) {\n headers.Authorization = `Bearer ${token}`\n }\n\n const response = await fetch(deleteUrl, {\n method: 'DELETE',\n headers\n })\n\n if (!response.ok) {\n throw new Error(`Delete failed with status ${response.status}`)\n }\n\n return { success: true }\n}\n\n/**\n * Get attachment URL by mediaId\n * @param {string} mediaId\n * @returns {string} Full URL to the attachment\n */\nexport function getAttachment(mediaId) {\n const credentials = getCredentials()\n const { endpoint } = credentials || {}\n\n if (!endpoint) {\n throw new Error(INITIALIZATION_ERROR)\n }\n\n return `${endpoint}/upload/${mediaId}`\n}\n\n/**\n * Internal fetch request helper\n * @param {string} pathname\n * @param {string} method\n * @param {object|null} body\n * @returns {Promise<Response>}\n */\nasync function fetchRequest(pathname, method = 'GET', body = null) {\n const credentials = getCredentials()\n\n const { endpoint, token } = credentials || {}\n if (!endpoint) {\n throw new Error(INITIALIZATION_ERROR)\n }\n\n const url = `${endpoint}${pathname}`\n\n const headers = {\n 'Content-Type': 'application/json'\n }\n if (token) {\n headers.Authorization = `Bearer ${token}`\n }\n\n return fetch(url, {\n headers,\n method,\n body: body ? JSON.stringify(body) : null\n })\n}\n","/**\n * Socket Service for Call SDK\n * Handles WebRTC call functionality without depending on external state\n * Uses callbacks to communicate state changes to the consumer\n */\n\nimport { getCallServerEndpoint } from './utils.js'\nimport { getCredentials, getExternalId, updateSessionId } from './chat.js'\n\n/**\n * @typedef {Object} CallCallbacks\n * @property {(status: string) => void} [onCallStatus] - Called when call status changes\n * @property {(error: string | null) => void} [onCallError] - Called when call error occurs\n */\n\n/**\n * @typedef {Object} CallSession\n * @property {string} [sessionId]\n * @property {WebSocket} [socket]\n * @property {RTCPeerConnection} [peerConnection]\n * @property {MediaStream} [localStream]\n * @property {MediaStream} [remoteStream]\n * @property {HTMLAudioElement} [remoteAudio]\n * @property {boolean} isMuted\n * @property {string} callStatus\n * @property {NodeJS.Timeout} [pingInterval]\n * @property {number} pingCount\n * @property {number | null} lastPongTime\n * @property {CallCallbacks} callbacks\n * @property {string[]} localIceCandidates - Queued local ICE candidates to send after remote description is set\n * @property {string[]} pendingRemoteIceCandidates - Queued remote ICE candidates to add after remote description is set\n */\n\n/**\n * Create a new call session\n * @param {CallCallbacks} [callbacks={}]\n * @returns {CallSession}\n */\nfunction createSession(callbacks = {}) {\n return {\n sessionId: undefined,\n socket: null,\n peerConnection: null,\n localStream: null,\n remoteStream: null,\n remoteAudio: null,\n isMuted: false,\n callStatus: 'disconnected',\n pingInterval: null,\n pingCount: 0,\n lastPongTime: null,\n callbacks,\n localIceCandidates: [],\n pendingRemoteIceCandidates: []\n }\n}\n\n/** @type {CallSession} */\nlet currentSession = createSession()\n\nconst rtcConfig = {\n iceServers: [{ urls: 'stun:stun.l.google.com:19302' }, { urls: 'stun:stun1.l.google.com:19302' }]\n}\n\n/**\n * Set callbacks for the current session\n * @param {CallCallbacks} callbacks\n */\nexport function setCallCallbacks(callbacks) {\n currentSession.callbacks = { ...currentSession.callbacks, ...callbacks }\n}\n\n/**\n * Clean up the current session\n */\nfunction cleanup() {\n if (currentSession.peerConnection) {\n currentSession.peerConnection.close()\n currentSession.peerConnection = null\n }\n\n if (currentSession.localStream) {\n currentSession.localStream.getTracks().forEach((track) => track.stop())\n currentSession.localStream = null\n }\n\n if (currentSession.remoteStream) {\n currentSession.remoteStream = null\n }\n\n if (currentSession.remoteAudio) {\n currentSession.remoteAudio.srcObject = null\n if (currentSession.remoteAudio.parentNode) {\n currentSession.remoteAudio.parentNode.removeChild(currentSession.remoteAudio)\n }\n currentSession.remoteAudio = null\n }\n\n if (currentSession.socket) {\n currentSession.socket.close()\n currentSession.socket = null\n }\n\n stopPingInterval()\n\n const callbacks = currentSession.callbacks\n currentSession = createSession(callbacks)\n\n console.log('Call session cleaned up')\n}\n\n/**\n * Update call status and notify callback\n * @param {string} status\n */\nfunction setCallStatus(status) {\n currentSession.callStatus = status\n currentSession.callbacks.onCallStatus?.(status)\n}\n\n/**\n * Update call error and notify callback\n * @param {string | null} error\n */\nfunction setCallError(error) {\n currentSession.callbacks.onCallError?.(error)\n}\n\n/**\n * Stop ping interval\n */\nfunction stopPingInterval() {\n if (currentSession.pingInterval) {\n clearInterval(currentSession.pingInterval)\n currentSession.pingInterval = null\n }\n}\n\n/**\n * Start ping interval\n */\nfunction startPingInterval() {\n stopPingInterval()\n\n currentSession.pingInterval = setInterval(() => {\n if (currentSession.socket && currentSession.socket.readyState === WebSocket.OPEN) {\n currentSession.pingCount++\n const pingMessage = {\n type: 'ping',\n timestamp: Date.now(),\n count: currentSession.pingCount\n }\n sendEvent(pingMessage)\n console.log(`Sending keep-alive ping #${currentSession.pingCount}`)\n } else {\n console.log('Socket not open, stopping ping interval')\n stopPingInterval()\n }\n }, 10000)\n}\n\n/**\n * Handle pong response\n */\nfunction handlePong() {\n currentSession.lastPongTime = Date.now()\n console.log(`Received pong #${currentSession.pingCount}`)\n}\n\n/**\n * Send event through socket\n * @param {Object} payload\n */\nfunction sendEvent(payload) {\n if (!currentSession.socket) {\n console.error('Failed to send event: no socket instance')\n return\n }\n if (currentSession.socket.readyState !== WebSocket.OPEN) {\n console.error('Failed to send event: socket state not open ', payload)\n return\n }\n\n currentSession.socket.send(JSON.stringify(payload))\n}\n\n/**\n * Get user media\n */\nasync function getUserMedia() {\n try {\n currentSession.localStream = await navigator.mediaDevices.getUserMedia({\n audio: true,\n video: false\n })\n console.log('Got audio media')\n } catch (error) {\n console.error(`Failed to get audio media: ${error.message}`)\n throw error\n }\n}\n\n/**\n * Create peer connection\n */\nfunction createPeerConnection() {\n currentSession.peerConnection = new RTCPeerConnection(rtcConfig)\n\n currentSession.peerConnection.onicecandidate = (event) => {\n if (event.candidate) {\n const candidateJson = JSON.stringify(event.candidate)\n // Queue local ICE candidates until remote description is set\n if (currentSession.peerConnection && currentSession.peerConnection.remoteDescription) {\n sendEvent({\n type: 'ice',\n data: {\n candidate: candidateJson\n }\n })\n console.log('Sent ICE candidate immediately')\n } else {\n currentSession.localIceCandidates.push(candidateJson)\n console.log('Queued local ICE candidate')\n }\n }\n }\n\n currentSession.peerConnection.ontrack = (event) => {\n console.log('Received remote audio stream')\n currentSession.remoteStream = event.streams[0]\n\n if (!currentSession.remoteAudio) {\n currentSession.remoteAudio = document.createElement('audio')\n currentSession.remoteAudio.autoplay = true\n currentSession.remoteAudio.controls = false\n document.body.appendChild(currentSession.remoteAudio)\n }\n currentSession.remoteAudio.srcObject = currentSession.remoteStream\n // explicitly kick off playback and catch any policy/gesture errors\n currentSession.remoteAudio\n .play()\n .then(() => console.log('🔊 remote audio playing'))\n .catch((err) => console.error('❌ playback error:', err))\n }\n\n currentSession.peerConnection.onconnectionstatechange = () => {\n const newState = currentSession.peerConnection.connectionState\n console.log(`Connection state: ${newState}`)\n\n if (newState === 'connected') {\n setCallStatus('connected')\n } else if (newState === 'disconnected' || newState === 'closed') {\n setCallStatus('disconnected')\n disconnectCall()\n }\n }\n\n currentSession.peerConnection.oniceconnectionstatechange = () => {\n console.log(`ICE connection state: ${currentSession.peerConnection.iceConnectionState}`)\n }\n}\n\n/**\n * Connect socket\n * @param {{ sessionId?: string }} payload\n */\nfunction connectSocket(payload) {\n return new Promise((fulfill, reject) => {\n if (\n currentSession.socket &&\n (currentSession.socket.readyState === WebSocket.CONNECTING ||\n currentSession.socket.readyState === WebSocket.OPEN)\n ) {\n console.log('Socket in connecting/open state, returning.')\n fulfill(currentSession.socket.readyState === WebSocket.OPEN)\n return\n }\n\n console.log('Initializing socket connection..')\n const credentials = getCredentials()\n if (!credentials || !credentials.endpoint) {\n reject(new Error('SDK not initialized. Please initialize SDK first.'))\n return\n }\n\n // Extract hostname from endpoint\n const socketEndpoint = getCallServerEndpoint(credentials.endpoint)\n if (!socketEndpoint) {\n reject(\n new Error(\n 'Invalid endpoint while initializing SDK. Please check the endpoint and try again.'\n )\n )\n return\n }\n\n const externalId = getExternalId()\n const queryParams = new URLSearchParams({\n externalId\n })\n if (payload.sessionId) {\n queryParams.set('sessionId', payload.sessionId)\n }\n if (credentials.token) {\n queryParams.set('token', credentials.token)\n }\n\n const socketUrl = `${socketEndpoint}?${queryParams.toString()}`\n currentSession.socket = new WebSocket(socketUrl)\n\n currentSession.socket.onopen = (event) => {\n console.log('Socket connection established: ', event)\n startPingInterval()\n fulfill(true)\n }\n\n currentSession.socket.onmessage = (event) => {\n const data = JSON.parse(event.data)\n handleCallServerEvent(data)\n }\n\n currentSession.socket.onerror = (error) => {\n console.error('Socket error: ', error)\n setCallStatus('error')\n setCallError(error.message || 'Unable to connect voice')\n reject(error)\n }\n\n currentSession.socket.onclose = (event) => {\n console.log('Socket connection closed: ', event)\n stopPingInterval()\n }\n })\n}\n\n/**\n * Handle call server event\n * @param {Object} action\n */\nfunction handleCallServerEvent(action) {\n console.log('Handling socket server event: ', action)\n\n switch (action.type) {\n case 'pong':\n handlePong()\n break\n\n case 'answer':\n handleAnswer(action.data)\n break\n\n case 'connected':\n handleConnected(action.data)\n break\n\n case 'ice':\n handleIceCandidate(action.data)\n break\n\n case 'renegotiationOffer':\n handleRenegotiationOffer(action.data)\n break\n case 'end':\n disconnectCall()\n break\n case 'error':\n setCallStatus('error')\n setCallError(action.error || 'Unable to connect voice')\n break\n\n default:\n console.log('Unknown call event type: ', action.type)\n break\n }\n}\n\n/**\n * Handle connected event\n * @param {Object} data\n */\nfunction handleConnected(data) {\n console.log('Received connected event')\n currentSession.sessionId = data.sessionId\n // Update chat session with the new sessionId and notify controller\n updateSessionId(data.sessionId)\n}\n\n/**\n * Handle answer\n * @param {Object} data\n */\nasync function handleAnswer(data) {\n try {\n console.log('Received answer')\n\n if (currentSession.peerConnection) {\n const answer = new RTCSessionDescription({\n type: 'answer',\n sdp: data.sdp\n })\n console.log('Setting remote description answer: ', answer)\n await currentSession.peerConnection.setRemoteDescription(answer)\n console.log('Remote description set')\n\n // Send all queued local ICE candidates\n for (const candidateJson of currentSession.localIceCandidates) {\n sendEvent({\n type: 'ice',\n data: {\n candidate: candidateJson\n }\n })\n console.log('Sent queued local ICE candidate')\n }\n currentSession.localIceCandidates = []\n\n // Process any pending remote ICE candidates\n for (const candidateJson of currentSession.pendingRemoteIceCandidates) {\n try {\n const candidate = new RTCIceCandidate(JSON.parse(candidateJson))\n await currentSession.peerConnection.addIceCandidate(candidate)\n console.log('Added pending remote ICE candidate')\n } catch (err) {\n console.error(`Failed to add pending ICE candidate: ${err.message}`)\n }\n }\n currentSession.pendingRemoteIceCandidates = []\n }\n } catch (error) {\n console.error(`Failed to handle answer: ${error.message}`)\n }\n}\n\n/**\n * Handle ICE candidate\n * @param {Object} data\n */\nasync function handleIceCandidate(data) {\n try {\n if (currentSession.peerConnection) {\n // Check if remote description is set\n if (!currentSession.peerConnection.remoteDescription) {\n // Queue the candidate until remote description is set\n currentSession.pendingRemoteIceCandidates.push(data.candidate)\n console.log('Queued remote ICE candidate - remote description not set')\n return\n }\n const candidate = new RTCIceCandidate(JSON.parse(data.candidate))\n await currentSession.peerConnection.addIceCandidate(candidate)\n console.log('Added ICE candidate')\n }\n } catch (error) {\n console.error(`Failed to add ICE candidate: ${error.message}`)\n }\n}\n\n/**\n * Handle renegotiation offer\n * @param {Object} data\n */\nasync function handleRenegotiationOffer(data) {\n try {\n console.log('Received renegotiation offer')\n\n if (currentSession.peerConnection) {\n const offer = new RTCSessionDescription({\n type: 'offer',\n sdp: data.sdp\n })\n console.log('Setting remote description offer: ', offer)\n await currentSession.peerConnection.setRemoteDescription(offer)\n console.log('Remote description set')\n\n const answer = await currentSession.peerConnection.createAnswer()\n await currentSession.peerConnection.setLocalDescription(answer)\n\n sendEvent({\n type: 'renegotiationAnswer',\n data: {\n sdp: answer.sdp\n }\n })\n }\n } catch (error) {\n console.error(`Failed to handle renegotiation offer: ${error.message}`)\n }\n}\n\n/**\n * Start a call\n * @param {{ sessionId?: string }} payload\n */\nexport async function startCall(payload = {}) {\n try {\n if (currentSession.callStatus === 'connecting' || currentSession.callStatus === 'connected') {\n console.log(`Call already in ${currentSession.callStatus} state`)\n return\n }\n\n console.log('Starting audio call...')\n setCallStatus('connecting')\n setCallError(null)\n\n currentSession.sessionId = payload.sessionId\n\n await getUserMedia()\n\n createPeerConnection()\n\n currentSession.localStream.getTracks().forEach((track) => {\n currentSession.peerConnection.addTrack(track, currentSession.localStream)\n console.log(`Added ${track.kind} track`)\n })\n await connectSocket(payload)\n const offer = await currentSession.peerConnection.createOffer()\n await currentSession.peerConnection.setLocalDescription(offer)\n\n sendEvent({\n type: 'offer',\n data: {\n sdp: offer.sdp\n }\n })\n\n console.log('Call initiated successfully')\n } catch (error) {\n console.log('error: ', error)\n console.error(`Failed to start call: ${error.message}`)\n setCallStatus('error')\n setCallError(error.message || 'Unable to connect voice')\n cleanup()\n }\n}\n\n/**\n * Disconnect call\n */\nexport function disconnectCall() {\n sendEvent({\n type: 'end'\n })\n if (currentSession.socket) {\n currentSession.socket.close()\n currentSession.socket = null\n }\n setCallStatus('disconnected')\n if (currentSession.peerConnection) {\n currentSession.peerConnection.close()\n currentSession.peerConnection = null\n }\n if (currentSession.localStream) {\n currentSession.localStream.getTracks().forEach((track) => track.stop())\n currentSession.localStream = null\n }\n cleanup()\n}\n\n/**\n * Toggle mute\n * @returns {boolean}\n */\nexport function toggleMute() {\n if (currentSession.localStream) {\n const audioTrack = currentSession.localStream.getAudioTracks()[0]\n if (audioTrack) {\n audioTrack.enabled = !audioTrack.enabled\n currentSession.isMuted = !audioTrack.enabled\n console.log(`Audio ${currentSession.isMuted ? 'muted' : 'unmuted'}`)\n return currentSession.isMuted\n }\n }\n return false\n}\n\n/**\n * Get local stream\n * @returns {MediaStream | null}\n */\nexport function getLocalStream() {\n return currentSession.localStream\n}\n\n/**\n * Get inbound audio energy\n * @returns {Promise<number>}\n */\nexport function getInboundAudioEnergy() {\n return new Promise((resolve, reject) => {\n if (!currentSession.peerConnection) {\n reject(new Error('no peer connection'))\n return\n }\n currentSession.peerConnection\n .getStats()\n .then((stats) => {\n stats.forEach((report) => {\n if (report.type == 'inbound-rtp') {\n resolve(report.totalAudioEnergy)\n }\n })\n reject(new Error('no inbound-rtp stats found'))\n })\n .catch((err) => {\n reject(err)\n })\n })\n}\n\n/**\n * Get outbound audio energy (not implemented in original, but may be needed)\n * @returns {Promise<number>}\n */\nexport function getOutboundAudioEnergy() {\n return new Promise((resolve, reject) => {\n if (!currentSession.peerConnection) {\n reject(new Error('no peer connection'))\n return\n }\n currentSession.peerConnection\n .getStats()\n .then((stats) => {\n stats.forEach((report) => {\n if (report.type == 'outbound-rtp') {\n resolve(report.totalAudioEnergy)\n }\n })\n reject(new Error('no outbound-rtp stats found'))\n })\n .catch((err) => {\n reject(err)\n })\n })\n}\n"],"names":["uuidv7","timestamp","bytes","hex","b","getDeviceId","deviceId","sleep","ms","resolve","getSseEndpoint","baseUrl","sseEndpoint","url","e","getSocketEndpoint","socketEndpoint","getCallServerEndpoint","MESSAGE_ROLES","PING_INTERVAL","SOCKET_TIMEOUT","SocketEvents","createSession","currentSession","stopPingInterval","startPingInterval","send","clearAllTimeouts","handleSocketConnected","setTransport","handleSocketDisconnected","connectSocket","payload","fulfill","reject","credentials","getCredentials","externalId","getExternalId","queryParams","socketUrl","event","message","handleSocketEvent","error","ws","addMessage","data","toggleTypingStatus","eventId","disconnect","isConnected","callbacks","setCallbacks","initialize","updateSessionId","sessionId","startChat","configData","authenticate","messages","control","session","getSession","searchParams","sendMessage","cleanup","disconnectSocket","isTyping","transport","text","html","context","attachments","isEmpty","userMessage","isSocketConnected","socketSend","loadingMessage","headers","fetchEventSource","response","errorMessage","lastIndex","updatedMsg","msg","index","_i","supervisorMessage","_j","_k","newBotMessage","lastMsg","_l","_o","_p","AUTHENTICATION_ERROR","INITIALIZATION_ERROR","endpoint","errorPayload","getHistory","fetchRequest","_a","uploadAttachment","file","onProgress","onComplete","token","xhr","formData","uploadUrl","percentComplete","deleteAttachment","mediaId","deleteUrl","getAttachment","pathname","method","body","rtcConfig","setCallCallbacks","track","setCallStatus","status","_b","setCallError","pingMessage","sendEvent","handlePong","getUserMedia","createPeerConnection","candidateJson","err","newState","disconnectCall","handleCallServerEvent","action","handleAnswer","handleConnected","handleIceCandidate","handleRenegotiationOffer","answer","candidate","offer","startCall","toggleMute","audioTrack","getLocalStream","getInboundAudioEnergy","stats","report","getOutboundAudio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,EAAO,IAAI,MAAM,6BAA6B,CAAC;AAAA,IACjD,CAAC,EACA,MAAM,CAACkG,MAAQ;AACd,MAAAlG,EAAOkG,CAAG;AAAA,IACZ,CAAC;AAAA,EACL,CAAC;AACH;"}
1
+ {"version":3,"file":"origon-chat-sdk.js","sources":["../src/utils.js","../src/constants.js","../src/socket.js","../src/chat.js","../src/http.js","../src/call.js"],"sourcesContent":["/**\n * Utility functions for the Chat SDK\n */\n\nexport function uuidv7() {\n const timestamp = Date.now()\n const bytes = new Uint8Array(16)\n crypto.getRandomValues(bytes)\n\n // Set timestamp (48 bits)\n bytes[0] = (timestamp >> 40) & 0xff\n bytes[1] = (timestamp >> 32) & 0xff\n bytes[2] = (timestamp >> 24) & 0xff\n bytes[3] = (timestamp >> 16) & 0xff\n bytes[4] = (timestamp >> 8) & 0xff\n bytes[5] = timestamp & 0xff\n\n // Set version 7 (0111)\n bytes[6] = (bytes[6] & 0x0f) | 0x70\n\n // Set variant (10xx)\n bytes[8] = (bytes[8] & 0x3f) | 0x80\n\n const hex = [...bytes].map((b) => b.toString(16).padStart(2, '0')).join('')\n return `${hex.slice(0, 8)}-${hex.slice(8, 12)}-${hex.slice(12, 16)}-${hex.slice(\n 16,\n 20\n )}-${hex.slice(20)}`\n}\n\nexport function getDeviceId() {\n if (localStorage.getItem('chatDeviceId')) {\n return localStorage.getItem('chatDeviceId')\n }\n\n const deviceId = uuidv7()\n localStorage.setItem('chatDeviceId', deviceId)\n return deviceId\n}\n\nexport function parseJwt(token) {\n try {\n const base64Url = token.split('.')[1]\n const base64 = base64Url.replace(/-/g, '+').replace(/_/g, '/')\n const jsonPayload = decodeURIComponent(\n atob(base64)\n .split('')\n .map(function (c) {\n return '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2)\n })\n .join('')\n )\n\n return JSON.parse(jsonPayload)\n } catch {\n return null\n }\n}\n\nexport async function sleep(ms) {\n return new Promise((resolve) => setTimeout(resolve, ms))\n}\n\nexport function getSseEndpoint(baseUrl) {\n let sseEndpoint\n try {\n const url = new URL(baseUrl)\n sseEndpoint = `https://${url.hostname}${url.pathname}/sse`\n } catch {\n console.error('SSE Invalid base URL: ', baseUrl)\n }\n return sseEndpoint\n}\n\nexport function getSocketEndpoint(baseUrl) {\n let socketEndpoint\n try {\n const url = new URL(baseUrl)\n socketEndpoint = `wss://${url.hostname}${url.pathname}/wss`\n } catch {\n console.error('Socket Invalid base URL: ', baseUrl)\n }\n return socketEndpoint\n}\n\nexport function getCallServerEndpoint(baseUrl) {\n let socketEndpoint\n try {\n const url = new URL(baseUrl)\n socketEndpoint = `wss://${url.hostname}${url.pathname}/audio`\n } catch {\n console.error('getCallServerEndpoint: Invalid base URL: ', baseUrl)\n }\n return socketEndpoint\n}\n","/**\n * Constants for the Chat SDK\n */\n\nexport const MESSAGE_ROLES = {\n ASSISTANT: 'assistant', // this can be automated or LLM AI Agent response\n USER: 'user', // this is widget user\n SUPERVISOR: 'supervisor', // this is human supervisor (ex. Samespace Dock agent, or Resolve human agent)\n SYSTEM: 'system' // this is system message, for ex \"Agent joined\" / \"Agent left\"\n}\n","/**\n * Socket Service for Chat SDK\n * Handles WebSocket connection for real-time chat functionality\n */\n\nimport { getSocketEndpoint, uuidv7 } from './utils.js'\nimport {\n getCredentials,\n getExternalId,\n addMessage,\n toggleTypingStatus,\n setTransport\n} from './chat.js'\n\nconst PING_INTERVAL = 10000\nconst SOCKET_TIMEOUT = 5000\n\n/**\n * @typedef {Object} SocketSession\n * @property {WebSocket} [socket]\n * @property {boolean} previouslyConnected\n * @property {boolean} socketDisconnected\n * @property {NodeJS.Timeout} [pingInterval]\n * @property {NodeJS.Timeout} [socketDisconnectedTimeout]\n * @property {NodeJS.Timeout} [socketConnectionTimeout]\n */\n\n/**\n * Socket Events\n */\nexport const SocketEvents = {\n MESSAGE: 'message',\n TYPING: 'typing',\n TYPING_STOP: 'typingOff',\n END: 'end'\n}\n\n/**\n * Create a new socket session\n * @returns {SocketSession}\n */\nfunction createSession() {\n return {\n socket: null,\n previouslyConnected: false,\n socketDisconnected: false,\n pingInterval: null,\n socketDisconnectedTimeout: null,\n socketConnectionTimeout: null\n }\n}\n\n/** @type {SocketSession} */\nlet currentSession = createSession()\n\n/**\n * Stop ping interval\n */\nfunction stopPingInterval() {\n if (currentSession.pingInterval) {\n clearInterval(currentSession.pingInterval)\n currentSession.pingInterval = null\n }\n}\n\n/**\n * Start ping interval\n */\nfunction startPingInterval() {\n stopPingInterval()\n\n currentSession.pingInterval = setInterval(() => {\n if (currentSession.socket && currentSession.socket.readyState === WebSocket.OPEN) {\n send({ type: 'ping' })\n console.log('Sending keep-alive ping')\n } else {\n console.log('Socket not open, stopping ping interval')\n stopPingInterval()\n }\n }, PING_INTERVAL)\n}\n\n/**\n * Clear all timeouts\n */\nfunction clearAllTimeouts() {\n stopPingInterval()\n if (currentSession.socketDisconnectedTimeout) {\n clearTimeout(currentSession.socketDisconnectedTimeout)\n currentSession.socketDisconnectedTimeout = null\n }\n if (currentSession.socketConnectionTimeout) {\n clearTimeout(currentSession.socketConnectionTimeout)\n currentSession.socketConnectionTimeout = null\n }\n}\n\n/**\n * Handle socket connected state\n */\nfunction handleSocketConnected() {\n console.log('handleSocketConnected')\n currentSession.socketDisconnected = false\n setTransport('socket')\n}\n\n/**\n * Handle socket disconnected state\n */\nfunction handleSocketDisconnected() {\n console.log('handleSocketDisconnected')\n currentSession.socketDisconnected = true\n setTransport('sse')\n}\n\n/**\n * Connect to socket\n * @param {{ token: string, sessionId?: string }} payload\n * @returns {Promise<boolean>}\n */\nexport function connectSocket(payload) {\n return new Promise((fulfill, reject) => {\n if (\n currentSession.socket &&\n (currentSession.socket.readyState === WebSocket.CONNECTING ||\n currentSession.socket.readyState === WebSocket.OPEN)\n ) {\n console.log('Socket in connecting/open state, returning.')\n fulfill(currentSession.socket.readyState === WebSocket.OPEN)\n return\n }\n\n console.log('Initializing socket connection..')\n const credentials = getCredentials()\n if (!credentials || !credentials.endpoint) {\n reject(new Error('SDK not initialized. Please initialize SDK first.'))\n return\n }\n\n const socketEndpoint = getSocketEndpoint(credentials.endpoint)\n if (!socketEndpoint) {\n reject(\n new Error(\n 'Invalid endpoint while initializing SDK. Please check the endpoint and try again.'\n )\n )\n return\n }\n\n const externalId = getExternalId()\n const queryParams = new URLSearchParams({\n externalId\n })\n\n if (payload.sessionId) {\n queryParams.set('sessionId', payload.sessionId)\n }\n if (payload.requestId) {\n queryParams.set('requestId', payload.requestId)\n }\n if (credentials.token) {\n queryParams.set('token', credentials.token)\n }\n\n const socketUrl = `${socketEndpoint}?${queryParams.toString()}`\n currentSession.socket = new WebSocket(socketUrl)\n\n currentSession.socket.onopen = () => {\n console.log('-------- socket connected --------')\n currentSession.previouslyConnected = true\n handleSocketConnected()\n send({ type: 'ping' })\n clearTimeout(currentSession.socketConnectionTimeout)\n startPingInterval()\n fulfill(true)\n }\n\n currentSession.socket.onmessage = (event) => {\n const message = JSON.parse(event.data)\n handleSocketEvent(message)\n }\n\n currentSession.socket.onerror = (error) => {\n console.error('Socket error:', error)\n setTransport('sse')\n reject(error)\n }\n\n currentSession.socket.onclose = (ws) => {\n console.log('-------- socket disconnected --------: ', ws.code, ws.reason)\n\n if (ws.target === currentSession.socket) {\n if (ws.code === 1006) {\n // abnormal closure\n if (currentSession.previouslyConnected) {\n handleSocketDisconnected()\n } else {\n addMessage({\n errorText: 'Unable to establish connection',\n done: true,\n timestamp: new Date().toISOString()\n })\n }\n clearTimeout(currentSession.socketConnectionTimeout)\n }\n\n currentSession.socket = null\n clearAllTimeouts()\n }\n }\n\n if (!currentSession.previouslyConnected) {\n currentSession.socketConnectionTimeout = setTimeout(() => {\n console.error('Socket connection timed out')\n addMessage({\n errorText: 'Unable to establish connection',\n done: true,\n timestamp: new Date().toISOString()\n })\n reject(new Error('Socket connection timed out'))\n }, SOCKET_TIMEOUT)\n }\n })\n}\n\n/**\n * Send data through socket\n * @param {Object} data\n */\nexport function send(data) {\n console.log('sending socket event: ', data.type)\n if (currentSession.socketDisconnected || !currentSession.socket) {\n return\n }\n currentSession.socket.send(JSON.stringify({ ...data, eventId: data.eventId || uuidv7() }))\n}\n\n/**\n * Send data through socket and wait for acknowledgment\n * @param {Object} data\n * @returns {Promise<Object>}\n */\nexport function sendWithAck(data) {\n return new Promise((resolve, reject) => {\n if (!currentSession.socket) {\n console.error('sendWithAck: socket instance not found or not connected')\n reject(new Error('Socket instance not found or not connected'))\n return\n }\n\n const autoRejectTimeout = setTimeout(() => {\n reject(new Error('Timeout'))\n }, 5000)\n\n const eventId = data.eventId || uuidv7()\n\n const onMessage = (event) => {\n const eventData = JSON.parse(event.data)\n if (eventData.eventId === eventId) {\n clearTimeout(autoRejectTimeout)\n currentSession.socket.removeEventListener('message', onMessage)\n if (eventData.data) {\n resolve(eventData.data)\n } else {\n reject(new Error(eventData.error?.message ?? 'Unknown error'))\n }\n }\n }\n currentSession.socket.addEventListener('message', onMessage)\n currentSession.socket.send(JSON.stringify({ ...data, eventId }))\n })\n}\n\n/**\n * Handle socket event\n * @param {Object} event\n */\nfunction handleSocketEvent(event) {\n console.log('received socket event: ', event.type)\n\n switch (event.type) {\n case 'pong': {\n if (currentSession.socketDisconnected) {\n handleSocketConnected()\n }\n if (currentSession.socketDisconnectedTimeout) {\n clearTimeout(currentSession.socketDisconnectedTimeout)\n }\n currentSession.socketDisconnectedTimeout = setTimeout(() => {\n console.log('---- socket ping timeout ----')\n handleSocketDisconnected()\n }, PING_INTERVAL + 1000)\n break\n }\n case SocketEvents.TYPING: {\n toggleTypingStatus(true)\n break\n }\n case SocketEvents.TYPING_STOP: {\n toggleTypingStatus(false)\n break\n }\n case SocketEvents.MESSAGE: {\n const { eventId, data } = event\n if (!eventId) {\n addMessage({\n ...data,\n done: true,\n timestamp: new Date().toISOString()\n })\n }\n break\n }\n case SocketEvents.END: {\n disconnect()\n break\n }\n default:\n break\n }\n}\n\n/**\n * Reconnect to socket\n */\nexport function reconnect() {\n if (currentSession.socket) {\n send({ type: 'ping' })\n }\n}\n\n/**\n * Disconnect socket\n */\nexport function disconnect() {\n console.log('Disconnecting socket')\n if (currentSession.socket) {\n currentSession.socket.close(1000)\n }\n currentSession.previouslyConnected = false\n clearAllTimeouts()\n currentSession.socket = null\n setTransport('sse')\n}\n\n/**\n * Check if socket is connected\n * @returns {boolean}\n */\nexport function isConnected() {\n return (\n currentSession.socket !== null &&\n currentSession.socket.readyState === WebSocket.OPEN &&\n !currentSession.socketDisconnected\n )\n}\n\n/**\n * Check if socket is disconnected\n * @returns {boolean}\n */\nexport function isDisconnected() {\n return currentSession.socketDisconnected\n}\n","/**\n * Chat Service for Chat SDK\n * Handles real-time chat functionality without depending on external state\n * Uses callbacks to communicate state changes to the consumer\n */\n\nimport { fetchEventSource } from '@microsoft/fetch-event-source'\nimport { getSession, authenticate } from './http.js'\nimport { getDeviceId, getSseEndpoint, sleep } from './utils.js'\nimport { MESSAGE_ROLES } from './constants.js'\nimport {\n connectSocket,\n send as socketSend,\n disconnect as disconnectSocket,\n isConnected as isSocketConnected\n} from './socket.js'\n\n/**\n * @typedef {Object} ChatCallbacks\n * @property {(message: Object) => void} [onMessageAdd] - Called when a new message is added\n * @property {(index: number, updatedMsg: Object) => void} [onMessageUpdate] - Called when an existing message is updated\n * @property {(sessionId: string) => void} [onSessionUpdate] - Called when session ID is updated\n * @property {(transport: TransportType) => void} [onTransportUpdate] - Called when transport type changes\n * @property {(control: 'agent' | 'human') => void} [onControlUpdate] - Called when control changes between agent and human\n */\n\n/**\n * @typedef {'sse' | 'socket'} TransportType\n */\n\n/**\n * @typedef {Object} ChatSession\n * @property {string} sessionId\n * @property {string} sseUrl\n * @property {string} [requestId]\n * @property {AbortController} [abortController]\n * @property {string} [lastStreamId]\n * @property {Array} messages\n * @property {ChatCallbacks} callbacks\n * @property {TransportType} transport\n */\n\n/**\n * Create a new chat session\n * @param {ChatCallbacks} [callbacks={}]\n * @returns {ChatSession}\n */\nfunction createSession(callbacks = {}) {\n return {\n credentials: undefined,\n authenticated: false,\n configData: undefined,\n sessionId: undefined,\n requestId: undefined,\n sseUrl: undefined,\n abortController: undefined,\n lastStreamId: undefined,\n messages: [],\n callbacks,\n transport: 'sse',\n control: 'agent'\n }\n}\n\n/** @type {ChatSession} */\nlet currentSession = createSession()\n\n/**\n * Set callbacks for the current session\n * @param {ChatCallbacks} callbacks\n */\nexport function setCallbacks(callbacks) {\n currentSession.callbacks = { ...currentSession.callbacks, ...callbacks }\n}\n\n/**\n * Initialize the chat session\n * @param {Object} credentials - Credentials for the chat\n */\nexport function initialize(credentials) {\n console.log('Initializing chat...', credentials)\n currentSession.credentials = credentials\n if (credentials.token) {\n currentSession.authenticated = true\n }\n}\n\n/**\n * Get current chat session credentials\n * @returns {{ endpoint: string, apiKey: string } | undefined}\n */\nexport function getCredentials() {\n return currentSession.credentials\n}\n\n/**\n * Update the session ID and notify via callback\n * @param {string} sessionId - The new session ID\n */\nexport function updateSessionId(sessionId) {\n if (sessionId && sessionId !== currentSession.sessionId) {\n currentSession.sessionId = sessionId\n currentSession.callbacks.onSessionUpdate?.(sessionId)\n }\n}\n\n/**\n * Initiate a new chat session or resume an existing one\n * @param {Object} credentials - Credentials for the chat\n * @param {Object} payload - Payload for the chat. It contains sessionId (optional)\n * @param {string} [payload.sessionId] - Optional session ID to resume\n * @returns {Promise<{ sessionId: string, messages: Array }>}\n */\nexport async function startChat(payload = {}) {\n try {\n console.log('startChat: ', payload, currentSession)\n\n let configData = null\n if (!currentSession.authenticated) {\n configData = await authenticate(currentSession.credentials)\n currentSession.authenticated = true\n currentSession.configData = configData\n } else {\n configData = currentSession.configData\n }\n\n let messages = []\n let control = 'agent'\n\n if (payload.sessionId) {\n const session = await getSession(payload.sessionId)\n messages = session.messages\n control = session.control || 'agent'\n }\n\n const searchParams = new URLSearchParams()\n if (!currentSession.credentials.token) {\n searchParams.set('externalId', getExternalId()) // externalId is needed only for public urls, not for internal chat (where token is provided)\n }\n currentSession.sseUrl = `${getSseEndpoint(\n currentSession.credentials.endpoint\n )}?${searchParams.toString()}`\n currentSession.sessionId = payload.sessionId\n currentSession.messages = messages\n currentSession.control = control\n\n if (control === 'human') {\n // Connect to SSE for incoming messages when control is human\n sendMessage({ text: '', html: '' }).catch(() => {})\n }\n\n console.log('Chat initiated successfully')\n\n return {\n sessionId: currentSession.sessionId,\n messages,\n control,\n configData\n }\n } catch (error) {\n console.error(`Failed to start chat: ${error.message}`)\n cleanup()\n throw error\n }\n}\n\n/**\n * Disconnect from the current chat session\n */\nexport function disconnect() {\n cleanup()\n}\n\n/**\n * Clean up the current session\n */\nfunction cleanup() {\n if (currentSession.abortController) {\n currentSession.abortController.abort()\n }\n disconnectSocket()\n\n const { callbacks, credentials } = currentSession\n currentSession = createSession(callbacks)\n currentSession.credentials = credentials\n\n console.log('Chat session cleaned up')\n}\n\nexport function getExternalId() {\n if (currentSession.credentials?.externalId) {\n return currentSession.credentials.externalId\n }\n return getDeviceId()\n}\n\n/**\n * Add a message to the chat\n * @param {Object} message - The message object to add\n */\nexport function addMessage(message) {\n currentSession.messages = [...currentSession.messages, message]\n currentSession.callbacks.onMessageAdd?.(message)\n}\n\nexport function toggleTypingStatus(isTyping) {\n currentSession.callbacks.onTyping?.(isTyping)\n}\n\n/**\n * Set the transport type\n * @param {TransportType} transport\n */\nexport function setTransport(transport) {\n console.log('Setting transport to:', transport)\n currentSession.transport = transport\n currentSession.callbacks.onTransportUpdate?.(transport)\n}\n\n/**\n * Get current transport type\n * @returns {TransportType}\n */\nexport function getTransport() {\n return currentSession.transport\n}\n\n/**\n * Send a message in the current chat session\n * @param {{ text: string, html?: string }} message\n * @returns {Promise<string>}\n */\nexport function sendMessage({ text, html, context, attachments, meta }) {\n return new Promise((resolve, reject) => {\n ;(async () => {\n try {\n const isEmpty = !text && !html && !attachments?.length\n\n // Add user message only if there's content\n if (!isEmpty) {\n const userMessage = {\n role: MESSAGE_ROLES.USER,\n text,\n html,\n timestamp: new Date().toISOString(),\n attachments,\n meta\n }\n addMessage(userMessage)\n await sleep(200)\n }\n\n // If transport is socket and socket is connected, use socket\n if (currentSession.transport === 'socket' && isSocketConnected()) {\n socketSend({\n type: 'message',\n data: {\n text,\n html\n }\n })\n resolve(currentSession.sessionId)\n return\n }\n\n if (currentSession.control === 'agent') {\n const loadingMessage = {\n role: MESSAGE_ROLES.ASSISTANT,\n text: '',\n loading: true\n }\n addMessage(loadingMessage)\n }\n\n const url = new URL(currentSession.sseUrl)\n if (currentSession.sessionId) {\n url.searchParams.set('sessionId', currentSession.sessionId)\n }\n if (currentSession.requestId) {\n url.searchParams.set('requestId', currentSession.requestId)\n }\n\n currentSession.lastStreamId = undefined\n\n // Create a new abort controller for this request\n currentSession.abortController = new AbortController()\n\n const headers = {\n 'Content-Type': 'application/json'\n }\n if (currentSession.credentials?.token) {\n headers.Authorization = `Bearer ${currentSession.credentials.token}`\n }\n\n await fetchEventSource(url.toString(), {\n method: 'POST',\n headers,\n body: isEmpty\n ? meta\n : JSON.stringify({\n message: text,\n html,\n context,\n attachments\n }),\n signal: currentSession.abortController.signal,\n openWhenHidden: true,\n onopen: async (response) => {\n if (!response.ok) {\n console.error('Failed to send message bad response: ', response)\n throw new Error('Failed to send message')\n }\n },\n onmessage: (response) => {\n // console.log('Event: ', response)\n const data = JSON.parse(response.data)\n\n if (response.event === 'connected') {\n currentSession.sessionId = data.sessionId\n currentSession.requestId = data.requestId\n console.log('Connected: ', data)\n if (data.control) {\n currentSession.control = data.control\n currentSession.callbacks.onControlUpdate?.(data.control)\n }\n } else if (response.event === 'upgrade_to_websocket') {\n console.log('Upgrade to websocket: ', data)\n connectSocket({\n sessionId: currentSession.sessionId,\n requestId: data.requestId\n })\n } else if (response.event === 'update') {\n if (data.control) {\n currentSession.control = data.control\n currentSession.callbacks.onControlUpdate?.(data.control)\n }\n } else if (data.error) {\n const errorMessage =\n data.error && typeof data.error === 'string'\n ? data.error\n : 'Failed to connect to the system'\n const lastIndex = currentSession.messages.length - 1\n const lastMsg = currentSession.messages[lastIndex]\n const updatedMsg = {\n ...lastMsg,\n loading: false,\n errorText: errorMessage\n }\n currentSession.messages = currentSession.messages.map((msg, index) =>\n index === lastIndex ? updatedMsg : msg\n )\n currentSession.callbacks.onMessageUpdate?.(lastIndex, updatedMsg)\n reject(new Error(errorMessage))\n } else if (response.event === 'done') {\n console.log('Done: ', data)\n const lastIndex = currentSession.messages.length - 1\n const lastMsg = currentSession.messages[lastIndex]\n const updatedMsg = {\n ...lastMsg,\n loading: false,\n done: true\n }\n currentSession.messages = currentSession.messages.map((msg, index) =>\n index === lastIndex ? updatedMsg : msg\n )\n currentSession.callbacks.onMessageUpdate?.(lastIndex, updatedMsg)\n resolve(currentSession.sessionId)\n } else if (data.message !== undefined || data.attachments?.length > 0) {\n // If role is supervisor, treat it as a new message\n if (data.role === MESSAGE_ROLES.SUPERVISOR) {\n const supervisorMessage = {\n role: MESSAGE_ROLES.SUPERVISOR,\n text: data.message,\n attachments: data.attachments,\n sources: data.sources,\n done: true\n }\n addMessage(supervisorMessage)\n resolve(currentSession.sessionId)\n\n // Store session info for reuse\n currentSession.sessionId = data.sessionId ?? currentSession.sessionId\n currentSession.requestId = data.requestId ?? currentSession.requestId\n return\n }\n\n // If streamId changes, start a new assistant message\n if (data.streamId !== undefined) {\n if (currentSession.lastStreamId === undefined) {\n currentSession.lastStreamId = data.streamId\n } else if (data.streamId !== currentSession.lastStreamId) {\n currentSession.lastStreamId = data.streamId\n const newBotMessage = {\n role: MESSAGE_ROLES.ASSISTANT,\n text: '',\n loading: true\n }\n addMessage(newBotMessage)\n }\n }\n\n // Update the last message with new content\n const lastIndex = currentSession.messages.length - 1\n const lastMsg = currentSession.messages[lastIndex]\n const updatedMsg = {\n ...lastMsg,\n loading: false,\n text: (lastMsg.text || '') + data.message,\n sources: data.sources,\n attachments: data.attachments,\n done: data.done ?? lastMsg.done\n }\n currentSession.messages = currentSession.messages.map((msg, index) =>\n index === lastIndex ? updatedMsg : msg\n )\n\n currentSession.callbacks.onMessageUpdate?.(lastIndex, updatedMsg)\n\n // Store session info for reuse\n currentSession.sessionId = data.sessionId ?? currentSession.sessionId\n currentSession.requestId = data.requestId ?? currentSession.requestId\n }\n },\n onerror: (error) => {\n throw error // Rethrow to stop retries\n },\n openWhenHidden: true\n })\n } catch (error) {\n console.error('Failed to send message: ', error)\n const errorMessage = 'Failed to connect to the system'\n const lastIndex = currentSession.messages.length - 1\n const lastMsg = currentSession.messages[lastIndex]\n const updatedMsg = {\n ...lastMsg,\n loading: false,\n errorText: lastMsg.done ? undefined : error.message || errorMessage,\n done: true\n }\n currentSession.messages = currentSession.messages.map((msg, index) =>\n index === lastIndex ? updatedMsg : msg\n )\n currentSession.callbacks.onMessageUpdate?.(lastIndex, updatedMsg)\n reject(error)\n }\n })()\n })\n}\n","/**\n * API Service for Chat SDK\n * Handles all HTTP requests without depending on external state\n */\n\nimport { getCredentials, getExternalId } from './chat.js'\nimport { MESSAGE_ROLES } from './constants.js'\n\nconst AUTHENTICATION_ERROR = 'Something went wrong initializing the chat'\nconst INITIALIZATION_ERROR = 'Chat SDK not initialized'\n\n/**\n * Authenticate with the chat service\n * @param {{ endpoint: string }} credentials\n * @returns {Promise<object>} Authentication response data\n */\nexport async function authenticate(payload) {\n const { endpoint } = payload\n const url = `${endpoint}/config`\n\n const response = await fetch(url, {\n method: 'GET',\n headers: {\n 'Content-Type': 'application/json'\n }\n })\n\n if (!response.ok) {\n const errorPayload = await response.json()\n throw new Error(errorPayload?.error || AUTHENTICATION_ERROR)\n }\n\n const res = await response.json()\n const data = res.data\n\n return data\n}\n\n/**\n * Get chat history for the current device\n * @returns {Promise<{ sessions: Array }>}\n */\nexport async function getHistory() {\n const queryParams = new URLSearchParams({\n externalId: getExternalId()\n })\n const response = await fetchRequest(`/sessions?${queryParams.toString()}`, 'GET')\n\n if (!response.ok) {\n throw new Error('Unable to load history, please try again later')\n }\n\n return response.json()\n}\n\n/**\n * Get session data (control and history) for a specific session\n * @param {string} sessionId\n * @returns {Promise<{ control: string, messages: Array }>}\n */\nexport async function getSession(sessionId) {\n const queryParams = new URLSearchParams({\n sessionId\n })\n const response = await fetchRequest(`/session?${queryParams.toString()}`, 'GET')\n\n if (!response.ok) {\n throw new Error('Unable to load messages, please try again later')\n }\n\n const data = await response.json()\n const control = data?.control\n const messages = (data?.history ?? []).map((msg) => ({\n id: msg.id,\n text: msg.text,\n role: msg.youtubeVideo\n ? MESSAGE_ROLES.ASSISTANT // for youtube video messages, role is \"system\" from backend, we need to make it \"assistant\"\n : msg.role,\n timestamp: msg.timestamp,\n video: msg.youtubeVideo,\n attachments: msg.attachments,\n channel: msg.channel,\n done: true\n }))\n\n return { control, messages }\n}\n\n/**\n * Upload attachment with progress tracking\n * @param {File} file\n * @param {Function} onProgress - callback (percentComplete, loaded, total)\n * @param {Function} onComplete - callback (error, response)\n * @returns {XMLHttpRequest} xhr object to allow abort\n */\nexport function uploadAttachment(file, onProgress, onComplete) {\n const credentials = getCredentials()\n const { endpoint, token } = credentials || {}\n\n if (!endpoint) {\n const error = new Error(INITIALIZATION_ERROR)\n if (onComplete) {\n onComplete(error, null)\n }\n return null\n }\n\n const xhr = new XMLHttpRequest()\n const formData = new FormData()\n formData.append('file', file)\n\n const uploadUrl = `${endpoint}/upload`\n\n xhr.open('POST', uploadUrl)\n if (token) {\n xhr.setRequestHeader('Authorization', `Bearer ${token}`)\n }\n\n // Upload progress callback\n if (onProgress) {\n xhr.upload.addEventListener('progress', (event) => {\n if (event.lengthComputable) {\n const percentComplete = (event.loaded / event.total) * 100\n onProgress(percentComplete, event.loaded, event.total)\n }\n })\n }\n\n // Upload complete callback\n xhr.addEventListener('load', () => {\n if (xhr.status >= 200 && xhr.status < 300) {\n try {\n const response = JSON.parse(xhr.responseText)\n if (onComplete) {\n onComplete(null, response)\n }\n } catch (error) {\n if (onComplete) {\n onComplete(new Error('Failed to parse response'), null)\n }\n }\n } else {\n if (onComplete) {\n onComplete(new Error(`Upload failed with status ${xhr.status}`), null)\n }\n }\n })\n\n xhr.addEventListener('error', () => {\n if (onComplete) {\n onComplete(new Error('Network error during upload'), null)\n }\n })\n\n xhr.addEventListener('abort', () => {\n if (onComplete) {\n onComplete(new Error('Upload aborted'), null)\n }\n })\n\n xhr.send(formData)\n\n return xhr\n}\n\n/**\n * Delete attachment by mediaId\n * @param {string} mediaId\n * @returns {Promise<{ success: boolean }>}\n */\nexport async function deleteAttachment(mediaId) {\n const credentials = getCredentials()\n const { endpoint, token } = credentials || {}\n\n if (!endpoint) {\n throw new Error(INITIALIZATION_ERROR)\n }\n\n const deleteUrl = `${endpoint}/delete/${mediaId}`\n\n const headers = {}\n if (token) {\n headers.Authorization = `Bearer ${token}`\n }\n\n const response = await fetch(deleteUrl, {\n method: 'DELETE',\n headers\n })\n\n if (!response.ok) {\n throw new Error(`Delete failed with status ${response.status}`)\n }\n\n return { success: true }\n}\n\n/**\n * Get attachment URL by mediaId\n * @param {string} mediaId\n * @returns {string} Full URL to the attachment\n */\nexport function getAttachment(mediaId) {\n const credentials = getCredentials()\n const { endpoint } = credentials || {}\n\n if (!endpoint) {\n throw new Error(INITIALIZATION_ERROR)\n }\n\n return `${endpoint}/upload/${mediaId}`\n}\n\n/**\n * Internal fetch request helper\n * @param {string} pathname\n * @param {string} method\n * @param {object|null} body\n * @returns {Promise<Response>}\n */\nasync function fetchRequest(pathname, method = 'GET', body = null) {\n const credentials = getCredentials()\n\n const { endpoint, token } = credentials || {}\n if (!endpoint) {\n throw new Error(INITIALIZATION_ERROR)\n }\n\n const url = `${endpoint}${pathname}`\n\n const headers = {\n 'Content-Type': 'application/json'\n }\n if (token) {\n headers.Authorization = `Bearer ${token}`\n }\n\n return fetch(url, {\n headers,\n method,\n body: body ? JSON.stringify(body) : null\n })\n}\n","/**\n * Socket Service for Call SDK\n * Handles WebRTC call functionality without depending on external state\n * Uses callbacks to communicate state changes to the consumer\n */\n\nimport { getCallServerEndpoint } from './utils.js'\nimport { getCredentials, getExternalId, updateSessionId } from './chat.js'\n\n/**\n * @typedef {Object} CallCallbacks\n * @property {(status: string) => void} [onCallStatus] - Called when call status changes\n * @property {(error: string | null) => void} [onCallError] - Called when call error occurs\n */\n\n/**\n * @typedef {Object} CallSession\n * @property {string} [sessionId]\n * @property {WebSocket} [socket]\n * @property {RTCPeerConnection} [peerConnection]\n * @property {MediaStream} [localStream]\n * @property {MediaStream} [remoteStream]\n * @property {HTMLAudioElement} [remoteAudio]\n * @property {boolean} isMuted\n * @property {string} callStatus\n * @property {NodeJS.Timeout} [pingInterval]\n * @property {number} pingCount\n * @property {number | null} lastPongTime\n * @property {CallCallbacks} callbacks\n * @property {string[]} localIceCandidates - Queued local ICE candidates to send after remote description is set\n * @property {string[]} pendingRemoteIceCandidates - Queued remote ICE candidates to add after remote description is set\n */\n\n/**\n * Create a new call session\n * @param {CallCallbacks} [callbacks={}]\n * @returns {CallSession}\n */\nfunction createSession(callbacks = {}) {\n return {\n sessionId: undefined,\n socket: null,\n peerConnection: null,\n localStream: null,\n remoteStream: null,\n remoteAudio: null,\n isMuted: false,\n callStatus: 'disconnected',\n pingInterval: null,\n pingCount: 0,\n lastPongTime: null,\n callbacks,\n localIceCandidates: [],\n pendingRemoteIceCandidates: []\n }\n}\n\n/** @type {CallSession} */\nlet currentSession = createSession()\n\nconst rtcConfig = {\n iceServers: [{ urls: 'stun:stun.l.google.com:19302' }, { urls: 'stun:stun1.l.google.com:19302' }]\n}\n\n/**\n * Set callbacks for the current session\n * @param {CallCallbacks} callbacks\n */\nexport function setCallCallbacks(callbacks) {\n currentSession.callbacks = { ...currentSession.callbacks, ...callbacks }\n}\n\n/**\n * Clean up the current session\n */\nfunction cleanup() {\n if (currentSession.peerConnection) {\n currentSession.peerConnection.close()\n currentSession.peerConnection = null\n }\n\n if (currentSession.localStream) {\n currentSession.localStream.getTracks().forEach((track) => track.stop())\n currentSession.localStream = null\n }\n\n if (currentSession.remoteStream) {\n currentSession.remoteStream = null\n }\n\n if (currentSession.remoteAudio) {\n currentSession.remoteAudio.srcObject = null\n if (currentSession.remoteAudio.parentNode) {\n currentSession.remoteAudio.parentNode.removeChild(currentSession.remoteAudio)\n }\n currentSession.remoteAudio = null\n }\n\n if (currentSession.socket) {\n currentSession.socket.close()\n currentSession.socket = null\n }\n\n stopPingInterval()\n\n const callbacks = currentSession.callbacks\n currentSession = createSession(callbacks)\n\n console.log('Call session cleaned up')\n}\n\n/**\n * Update call status and notify callback\n * @param {string} status\n */\nfunction setCallStatus(status) {\n currentSession.callStatus = status\n currentSession.callbacks.onCallStatus?.(status)\n}\n\n/**\n * Update call error and notify callback\n * @param {string | null} error\n */\nfunction setCallError(error) {\n currentSession.callbacks.onCallError?.(error)\n}\n\n/**\n * Stop ping interval\n */\nfunction stopPingInterval() {\n if (currentSession.pingInterval) {\n clearInterval(currentSession.pingInterval)\n currentSession.pingInterval = null\n }\n}\n\n/**\n * Start ping interval\n */\nfunction startPingInterval() {\n stopPingInterval()\n\n currentSession.pingInterval = setInterval(() => {\n if (currentSession.socket && currentSession.socket.readyState === WebSocket.OPEN) {\n currentSession.pingCount++\n const pingMessage = {\n type: 'ping',\n timestamp: Date.now(),\n count: currentSession.pingCount\n }\n sendEvent(pingMessage)\n console.log(`Sending keep-alive ping #${currentSession.pingCount}`)\n } else {\n console.log('Socket not open, stopping ping interval')\n stopPingInterval()\n }\n }, 10000)\n}\n\n/**\n * Handle pong response\n */\nfunction handlePong() {\n currentSession.lastPongTime = Date.now()\n console.log(`Received pong #${currentSession.pingCount}`)\n}\n\n/**\n * Send event through socket\n * @param {Object} payload\n */\nfunction sendEvent(payload) {\n if (!currentSession.socket) {\n console.error('Failed to send event: no socket instance')\n return\n }\n if (currentSession.socket.readyState !== WebSocket.OPEN) {\n console.error('Failed to send event: socket state not open ', payload)\n return\n }\n\n currentSession.socket.send(JSON.stringify(payload))\n}\n\n/**\n * Get user media\n */\nasync function getUserMedia() {\n try {\n currentSession.localStream = await navigator.mediaDevices.getUserMedia({\n audio: true,\n video: false\n })\n console.log('Got audio media')\n } catch (error) {\n console.error(`Failed to get audio media: ${error.message}`)\n throw error\n }\n}\n\n/**\n * Create peer connection\n */\nfunction createPeerConnection() {\n currentSession.peerConnection = new RTCPeerConnection(rtcConfig)\n\n currentSession.peerConnection.onicecandidate = (event) => {\n if (event.candidate) {\n const candidateJson = JSON.stringify(event.candidate)\n // Queue local ICE candidates until remote description is set\n if (currentSession.peerConnection && currentSession.peerConnection.remoteDescription) {\n sendEvent({\n type: 'ice',\n data: {\n candidate: candidateJson\n }\n })\n console.log('Sent ICE candidate immediately')\n } else {\n currentSession.localIceCandidates.push(candidateJson)\n console.log('Queued local ICE candidate')\n }\n }\n }\n\n currentSession.peerConnection.ontrack = (event) => {\n console.log('Received remote audio stream')\n currentSession.remoteStream = event.streams[0]\n\n if (!currentSession.remoteAudio) {\n currentSession.remoteAudio = document.createElement('audio')\n currentSession.remoteAudio.autoplay = true\n currentSession.remoteAudio.controls = false\n document.body.appendChild(currentSession.remoteAudio)\n }\n currentSession.remoteAudio.srcObject = currentSession.remoteStream\n // explicitly kick off playback and catch any policy/gesture errors\n currentSession.remoteAudio\n .play()\n .then(() => console.log('🔊 remote audio playing'))\n .catch((err) => console.error('❌ playback error:', err))\n }\n\n currentSession.peerConnection.onconnectionstatechange = () => {\n const newState = currentSession.peerConnection.connectionState\n console.log(`Connection state: ${newState}`)\n\n if (newState === 'connected') {\n setCallStatus('connected')\n } else if (newState === 'disconnected' || newState === 'closed') {\n setCallStatus('disconnected')\n disconnectCall()\n }\n }\n\n currentSession.peerConnection.oniceconnectionstatechange = () => {\n console.log(`ICE connection state: ${currentSession.peerConnection.iceConnectionState}`)\n }\n}\n\n/**\n * Connect socket\n * @param {{ sessionId?: string }} payload\n */\nfunction connectSocket(payload) {\n return new Promise((fulfill, reject) => {\n if (\n currentSession.socket &&\n (currentSession.socket.readyState === WebSocket.CONNECTING ||\n currentSession.socket.readyState === WebSocket.OPEN)\n ) {\n console.log('Socket in connecting/open state, returning.')\n fulfill(currentSession.socket.readyState === WebSocket.OPEN)\n return\n }\n\n console.log('Initializing socket connection..')\n const credentials = getCredentials()\n if (!credentials || !credentials.endpoint) {\n reject(new Error('SDK not initialized. Please initialize SDK first.'))\n return\n }\n\n // Extract hostname from endpoint\n const socketEndpoint = getCallServerEndpoint(credentials.endpoint)\n if (!socketEndpoint) {\n reject(\n new Error(\n 'Invalid endpoint while initializing SDK. Please check the endpoint and try again.'\n )\n )\n return\n }\n\n const externalId = getExternalId()\n const queryParams = new URLSearchParams({\n externalId\n })\n if (payload.sessionId) {\n queryParams.set('sessionId', payload.sessionId)\n }\n if (credentials.token) {\n queryParams.set('token', credentials.token)\n }\n\n const socketUrl = `${socketEndpoint}?${queryParams.toString()}`\n currentSession.socket = new WebSocket(socketUrl)\n\n currentSession.socket.onopen = (event) => {\n console.log('Socket connection established: ', event)\n startPingInterval()\n fulfill(true)\n }\n\n currentSession.socket.onmessage = (event) => {\n const data = JSON.parse(event.data)\n handleCallServerEvent(data)\n }\n\n currentSession.socket.onerror = (error) => {\n console.error('Socket error: ', error)\n setCallStatus('error')\n setCallError(error.message || 'Unable to connect voice')\n reject(error)\n }\n\n currentSession.socket.onclose = (event) => {\n console.log('Socket connection closed: ', event)\n stopPingInterval()\n }\n })\n}\n\n/**\n * Handle call server event\n * @param {Object} action\n */\nfunction handleCallServerEvent(action) {\n console.log('Handling socket server event: ', action)\n\n switch (action.type) {\n case 'pong':\n handlePong()\n break\n\n case 'answer':\n handleAnswer(action.data)\n break\n\n case 'connected':\n handleConnected(action.data)\n break\n\n case 'ice':\n handleIceCandidate(action.data)\n break\n\n case 'renegotiationOffer':\n handleRenegotiationOffer(action.data)\n break\n case 'end':\n disconnectCall()\n break\n case 'error':\n setCallStatus('error')\n setCallError(action.error || 'Unable to connect voice')\n break\n\n default:\n console.log('Unknown call event type: ', action.type)\n break\n }\n}\n\n/**\n * Handle connected event\n * @param {Object} data\n */\nfunction handleConnected(data) {\n console.log('Received connected event')\n currentSession.sessionId = data.sessionId\n // Update chat session with the new sessionId and notify controller\n updateSessionId(data.sessionId)\n}\n\n/**\n * Handle answer\n * @param {Object} data\n */\nasync function handleAnswer(data) {\n try {\n console.log('Received answer')\n\n if (currentSession.peerConnection) {\n const answer = new RTCSessionDescription({\n type: 'answer',\n sdp: data.sdp\n })\n console.log('Setting remote description answer: ', answer)\n await currentSession.peerConnection.setRemoteDescription(answer)\n console.log('Remote description set')\n\n // Send all queued local ICE candidates\n for (const candidateJson of currentSession.localIceCandidates) {\n sendEvent({\n type: 'ice',\n data: {\n candidate: candidateJson\n }\n })\n console.log('Sent queued local ICE candidate')\n }\n currentSession.localIceCandidates = []\n\n // Process any pending remote ICE candidates\n for (const candidateJson of currentSession.pendingRemoteIceCandidates) {\n try {\n const candidate = new RTCIceCandidate(JSON.parse(candidateJson))\n await currentSession.peerConnection.addIceCandidate(candidate)\n console.log('Added pending remote ICE candidate')\n } catch (err) {\n console.error(`Failed to add pending ICE candidate: ${err.message}`)\n }\n }\n currentSession.pendingRemoteIceCandidates = []\n }\n } catch (error) {\n console.error(`Failed to handle answer: ${error.message}`)\n }\n}\n\n/**\n * Handle ICE candidate\n * @param {Object} data\n */\nasync function handleIceCandidate(data) {\n try {\n if (currentSession.peerConnection) {\n // Check if remote description is set\n if (!currentSession.peerConnection.remoteDescription) {\n // Queue the candidate until remote description is set\n currentSession.pendingRemoteIceCandidates.push(data.candidate)\n console.log('Queued remote ICE candidate - remote description not set')\n return\n }\n const candidate = new RTCIceCandidate(JSON.parse(data.candidate))\n await currentSession.peerConnection.addIceCandidate(candidate)\n console.log('Added ICE candidate')\n }\n } catch (error) {\n console.error(`Failed to add ICE candidate: ${error.message}`)\n }\n}\n\n/**\n * Handle renegotiation offer\n * @param {Object} data\n */\nasync function handleRenegotiationOffer(data) {\n try {\n console.log('Received renegotiation offer')\n\n if (currentSession.peerConnection) {\n const offer = new RTCSessionDescription({\n type: 'offer',\n sdp: data.sdp\n })\n console.log('Setting remote description offer: ', offer)\n await currentSession.peerConnection.setRemoteDescription(offer)\n console.log('Remote description set')\n\n const answer = await currentSession.peerConnection.createAnswer()\n await currentSession.peerConnection.setLocalDescription(answer)\n\n sendEvent({\n type: 'renegotiationAnswer',\n data: {\n sdp: answer.sdp\n }\n })\n }\n } catch (error) {\n console.error(`Failed to handle renegotiation offer: ${error.message}`)\n }\n}\n\n/**\n * Start a call\n * @param {{ sessionId?: string }} payload\n */\nexport async function startCall(payload = {}) {\n try {\n if (currentSession.callStatus === 'connecting' || currentSession.callStatus === 'connected') {\n console.log(`Call already in ${currentSession.callStatus} state`)\n return\n }\n\n console.log('Starting audio call...')\n setCallStatus('connecting')\n setCallError(null)\n\n currentSession.sessionId = payload.sessionId\n\n await getUserMedia()\n\n createPeerConnection()\n\n currentSession.localStream.getTracks().forEach((track) => {\n currentSession.peerConnection.addTrack(track, currentSession.localStream)\n console.log(`Added ${track.kind} track`)\n })\n await connectSocket(payload)\n const offer = await currentSession.peerConnection.createOffer()\n await currentSession.peerConnection.setLocalDescription(offer)\n\n sendEvent({\n type: 'offer',\n data: {\n sdp: offer.sdp\n }\n })\n\n console.log('Call initiated successfully')\n } catch (error) {\n console.log('error: ', error)\n console.error(`Failed to start call: ${error.message}`)\n setCallStatus('error')\n setCallError(error.message || 'Unable to connect voice')\n cleanup()\n }\n}\n\n/**\n * Disconnect call\n */\nexport function disconnectCall() {\n sendEvent({\n type: 'end'\n })\n if (currentSession.socket) {\n currentSession.socket.close()\n currentSession.socket = null\n }\n setCallStatus('disconnected')\n if (currentSession.peerConnection) {\n currentSession.peerConnection.close()\n currentSession.peerConnection = null\n }\n if (currentSession.localStream) {\n currentSession.localStream.getTracks().forEach((track) => track.stop())\n currentSession.localStream = null\n }\n cleanup()\n}\n\n/**\n * Toggle mute\n * @returns {boolean}\n */\nexport function toggleMute() {\n if (currentSession.localStream) {\n const audioTrack = currentSession.localStream.getAudioTracks()[0]\n if (audioTrack) {\n audioTrack.enabled = !audioTrack.enabled\n currentSession.isMuted = !audioTrack.enabled\n console.log(`Audio ${currentSession.isMuted ? 'muted' : 'unmuted'}`)\n return currentSession.isMuted\n }\n }\n return false\n}\n\n/**\n * Get local stream\n * @returns {MediaStream | null}\n */\nexport function getLocalStream() {\n return currentSession.localStream\n}\n\n/**\n * Get inbound audio energy\n * @returns {Promise<number>}\n */\nexport function getInboundAudioEnergy() {\n return new Promise((resolve, reject) => {\n if (!currentSession.peerConnection) {\n reject(new Error('no peer connection'))\n return\n }\n currentSession.peerConnection\n .getStats()\n .then((stats) => {\n stats.forEach((report) => {\n if (report.type == 'inbound-rtp') {\n resolve(report.totalAudioEnergy)\n }\n })\n reject(new Error('no inbound-rtp stats found'))\n })\n .catch((err) => {\n reject(err)\n })\n })\n}\n\n/**\n * Get outbound audio energy (not implemented in original, but may be needed)\n * @returns {Promise<number>}\n */\nexport function getOutboundAudioEnergy() {\n return new Promise((resolve, reject) => {\n if (!currentSession.peerConnection) {\n reject(new Error('no peer connection'))\n return\n }\n currentSession.peerConnection\n .getStats()\n .then((stats) => {\n stats.forEach((report) => {\n if (report.type == 'outbound-rtp') {\n resolve(report.totalAudioEnergy)\n }\n })\n reject(new Error('no outbound-rtp stats found'))\n })\n .catch((err) => {\n reject(err)\n })\n })\n}\n"],"names":["uuidv7","timestamp","bytes","hex","b","getDeviceId","deviceId","sleep","ms","resolve","getSseEndpoint","baseUrl","sseEndpoint","url","e","getSocketEndpoint","socketEndpoint","getCallServerEndpoint","MESSAGE_ROLES","PING_INTERVAL","SOCKET_TIMEOUT","SocketEvents","createSession","currentSession","stopPingInterval","startPingInterval","send","clearAllTimeouts","handleSocketConnected","setTransport","handleSocketDisconnected","connectSocket","payload","fulfill","reject","credentials","getCredentials","externalId","getExternalId","queryParams","socketUrl","event","message","handleSocketEvent","error","ws","addMessage","data","toggleTypingStatus","eventId","disconnect","isConnected","callbacks","setCallbacks","initialize","updateSessionId","sessionId","startChat","configData","authenticate","messages","control","session","getSession","searchParams","sendMessage","cleanup","disconnectSocket","isTyping","transport","text","html","context","attachments","meta","isEmpty","userMessage","isSocketConnected","socketSend","loadingMessage","headers","fetchEventSource","response","errorMessage","lastIndex","updatedMsg","msg","index","_i","supervisorMessage","_j","_k","newBotMessage","lastMsg","_l","_o","_p","AUTHENTICATION_ERROR","INITIALIZATION_ERROR","endpoint","errorPayload","getHistory","fetchRequest","_a","uploadAttachment","file","onProgress","onComplete","token","xhr","formData","uploadUrl","percentComplete","deleteAttachment","mediaId","deleteUrl","getAttachment","pathname","method","body","rtcConfig","setCallCallbacks","track","setCallStatus","status","_b","setCallError","pingMessage","sendEvent","handlePong","getUserMedia","createPeerConnection","candidateJson","err","newState","disconnectCall","handleCallServerEvent","action","handleAnswer","handleConnected","handleIceCandidate","handleRenegotiationOffer","answer","candidate","offer","startCall","toggleMute","audioTrack","getLocalStream","getInboundAudioEnergy","stats","report","getOutboundAudioEnergy"],"mappings":";AAIO,SAASA,IAAS;AACvB,QAAMC,IAAY,KAAK,IAAG,GACpBC,IAAQ,IAAI,WAAW,EAAE;AAC/B,SAAO,gBAAgBA,CAAK,GAG5BA,EAAM,CAAC,IAAKD,KAAa,KAAM,KAC/BC,EAAM,CAAC,IAAKD,KAAa,KAAM,KAC/BC,EAAM,CAAC,IAAKD,KAAa,KAAM,KAC/BC,EAAM,CAAC,IAAKD,KAAa,KAAM,KAC/BC,EAAM,CAAC,IAAKD,KAAa,IAAK,KAC9BC,EAAM,CAAC,IAAID,IAAY,KAGvBC,EAAM,CAAC,IAAKA,EAAM,CAAC,IAAI,KAAQ,KAG/BA,EAAM,CAAC,IAAKA,EAAM,CAAC,IAAI,KAAQ;AAE/B,QAAMC,IAAM,CAAC,GAAGD,CAAK,EAAE,IAAI,CAACE,MAAMA,EAAE,SAAS,EAAE,EAAE,SAAS,GAAG,GAAG,CAAC,EAAE,KAAK,EAAE;AAC1E,SAAO,GAAGD,EAAI,MAAM,GAAG,CAAC,CAAC,IAAIA,EAAI,MAAM,GAAG,EAAE,CAAC,IAAIA,EAAI,MAAM,IAAI,EAAE,CAAC,IAAIA,EAAI;AAAA,IACxE;AAAA,IACA;AAAA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rH,EAAO,IAAI,MAAM,4BAA4B,CAAC;AAAA,IAChD,CAAC,EACA,MAAM,CAACmG,MAAQ;AACd,MAAAnG,EAAOmG,CAAG;AAAA,IACZ,CAAC;AAAA,EACL,CAAC;AACH;AAMO,SAASmB,KAAyB;AACvC,SAAO,IAAI,QAAQ,CAAC/I,GAASyB,MAAW;AACtC,QAAI,CAACX,EAAe,gBAAgB;AAClC,MAAAW,EAAO,IAAI,MAAM,oBAAoB,CAAC;AACtC;AAAA,IACF;AACA,IAAAX,EAAe,eACZ,SAAQ,EACR,KAAK,CAAC+H,MAAU;AACf,MAAAA,EAAM,QAAQ,CAACC,MAAW;AACxB,QAAIA,EAAO,QAAQ,kBACjB9I,EAAQ8I,EAAO,gBAAgB;AAAA,MAEnC,CAAC,GACDrH,EAAO,IAAI,MAAM,6BAA6B,CAAC;AAAA,IACjD,CAAC,EACA,MAAM,CAACmG,MAAQ;AACd,MAAAnG,EAAOmG,CAAG;AAAA,IACZ,CAAC;AAAA,EACL,CAAC;AACH;"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@origonai/web-chat-sdk",
3
- "version": "1.0.8",
3
+ "version": "1.0.9",
4
4
  "description": "Origon Web Chat SDK - Core chat functionality without UI dependencies",
5
5
  "type": "module",
6
6
  "main": "./dist/origon-chat-sdk.js",
package/src/chat.js CHANGED
@@ -230,7 +230,7 @@ export function getTransport() {
230
230
  * @param {{ text: string, html?: string }} message
231
231
  * @returns {Promise<string>}
232
232
  */
233
- export function sendMessage({ text, html, context, attachments }) {
233
+ export function sendMessage({ text, html, context, attachments, meta }) {
234
234
  return new Promise((resolve, reject) => {
235
235
  ;(async () => {
236
236
  try {
@@ -243,7 +243,8 @@ export function sendMessage({ text, html, context, attachments }) {
243
243
  text,
244
244
  html,
245
245
  timestamp: new Date().toISOString(),
246
- attachments
246
+ attachments,
247
+ meta
247
248
  }
248
249
  addMessage(userMessage)
249
250
  await sleep(200)
@@ -295,7 +296,7 @@ export function sendMessage({ text, html, context, attachments }) {
295
296
  method: 'POST',
296
297
  headers,
297
298
  body: isEmpty
298
- ? undefined
299
+ ? meta
299
300
  : JSON.stringify({
300
301
  message: text,
301
302
  html,