@d-id/client-sdk 1.0.19-beta.109 → 1.0.19-beta.110

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -1,62 +1,62 @@
1
- var Q = /* @__PURE__ */ ((e) => (e.TRIAL = "trial", e.BASIC = "basic", e.ENTERPRISE = "enterprise", e.LITE = "lite", e.ADVANCED = "advanced", e))(Q || {}), G = /* @__PURE__ */ ((e) => (e.TRIAL = "deid-trial", e.PRO = "deid-pro", e.ENTERPRISE = "deid-enterprise", e.LITE = "deid-lite", e.ADVANCED = "deid-advanced", e.BUILD = "deid-api-build", e.LAUNCH = "deid-api-launch", e.SCALE = "deid-api-scale", e))(G || {}), ee = /* @__PURE__ */ ((e) => (e.Unrated = "Unrated", e.Positive = "Positive", e.Negative = "Negative", e))(ee || {}), M = /* @__PURE__ */ ((e) => (e.Functional = "Functional", e.TextOnly = "TextOnly", e.Maintenance = "Maintenance", e.Playground = "Playground", e.DirectPlayback = "DirectPlayback", e))(M || {}), N = /* @__PURE__ */ ((e) => (e.Embed = "embed", e.Query = "query", e.Partial = "partial", e.Answer = "answer", e.Complete = "done", e))(N || {}), te = /* @__PURE__ */ ((e) => (e.KnowledgeProcessing = "knowledge/processing", e.KnowledgeIndexing = "knowledge/indexing", e.KnowledgeFailed = "knowledge/error", e.KnowledgeDone = "knowledge/done", e))(te || {}), ne = /* @__PURE__ */ ((e) => (e.Knowledge = "knowledge", e.Document = "document", e.Record = "record", e))(ne || {}), re = /* @__PURE__ */ ((e) => (e.Pdf = "pdf", e.Text = "text", e.Html = "html", e.Word = "word", e.Json = "json", e.Markdown = "markdown", e.Csv = "csv", e.Excel = "excel", e.Powerpoint = "powerpoint", e.Archive = "archive", e.Image = "image", e.Audio = "audio", e.Video = "video", e))(re || {}), v = /* @__PURE__ */ ((e) => (e.Clip = "clip", e.Talk = "talk", e))(v || {}), x = /* @__PURE__ */ ((e) => (e.Start = "START", e.Stop = "STOP", e))(x || {}), F = /* @__PURE__ */ ((e) => (e.ChatAnswer = "chat/answer", e.ChatPartial = "chat/partial", e.StreamDone = "stream/done", e.StreamStarted = "stream/started", e.StreamFailed = "stream/error", e.StreamReady = "stream/ready", e.StreamCreated = "stream/created", e.StreamVideoCreated = "stream-video/started", e.StreamVideoDone = "stream-video/done", e.StreamVideoError = "stream-video/error", e.StreamVideoRejected = "stream-video/rejected", e))(F || {}), k = /* @__PURE__ */ ((e) => (e.New = "new", e.Fail = "fail", e.Connected = "connected", e.Connecting = "connecting", e.Terminating = "terminating", e))(k || {}), ae = /* @__PURE__ */ ((e) => (e.Amazon = "amazon", e.Microsoft = "microsoft", e.Afflorithmics = "afflorithmics", e.Elevenlabs = "elevenlabs", e))(ae || {}), ie = /* @__PURE__ */ ((e) => (e.Public = "public", e.Premium = "premium", e.Private = "private", e))(ie || {});
2
- const O = "https://api.d-id.com", se = "wss://notifications.d-id.com", oe = "79f81a83a67430be2bc0fd61042b8faa", K = () => Math.random().toString(16).slice(2);
3
- function q() {
1
+ var G = /* @__PURE__ */ ((e) => (e.TRIAL = "trial", e.BASIC = "basic", e.ENTERPRISE = "enterprise", e.LITE = "lite", e.ADVANCED = "advanced", e))(G || {}), ee = /* @__PURE__ */ ((e) => (e.TRIAL = "deid-trial", e.PRO = "deid-pro", e.ENTERPRISE = "deid-enterprise", e.LITE = "deid-lite", e.ADVANCED = "deid-advanced", e.BUILD = "deid-api-build", e.LAUNCH = "deid-api-launch", e.SCALE = "deid-api-scale", e))(ee || {}), te = /* @__PURE__ */ ((e) => (e.Unrated = "Unrated", e.Positive = "Positive", e.Negative = "Negative", e))(te || {}), k = /* @__PURE__ */ ((e) => (e.Functional = "Functional", e.TextOnly = "TextOnly", e.Maintenance = "Maintenance", e.Playground = "Playground", e.DirectPlayback = "DirectPlayback", e))(k || {}), L = /* @__PURE__ */ ((e) => (e.Embed = "embed", e.Query = "query", e.Partial = "partial", e.Answer = "answer", e.Complete = "done", e))(L || {}), ne = /* @__PURE__ */ ((e) => (e.KnowledgeProcessing = "knowledge/processing", e.KnowledgeIndexing = "knowledge/indexing", e.KnowledgeFailed = "knowledge/error", e.KnowledgeDone = "knowledge/done", e))(ne || {}), re = /* @__PURE__ */ ((e) => (e.Knowledge = "knowledge", e.Document = "document", e.Record = "record", e))(re || {}), ae = /* @__PURE__ */ ((e) => (e.Pdf = "pdf", e.Text = "text", e.Html = "html", e.Word = "word", e.Json = "json", e.Markdown = "markdown", e.Csv = "csv", e.Excel = "excel", e.Powerpoint = "powerpoint", e.Archive = "archive", e.Image = "image", e.Audio = "audio", e.Video = "video", e))(ae || {}), v = /* @__PURE__ */ ((e) => (e.Clip = "clip", e.Talk = "talk", e))(v || {}), b = /* @__PURE__ */ ((e) => (e.Start = "START", e.Stop = "STOP", e))(b || {}), U = /* @__PURE__ */ ((e) => (e.ChatAnswer = "chat/answer", e.ChatPartial = "chat/partial", e.StreamDone = "stream/done", e.StreamStarted = "stream/started", e.StreamFailed = "stream/error", e.StreamReady = "stream/ready", e.StreamCreated = "stream/created", e.StreamVideoCreated = "stream-video/started", e.StreamVideoDone = "stream-video/done", e.StreamVideoError = "stream-video/error", e.StreamVideoRejected = "stream-video/rejected", e))(U || {}), I = /* @__PURE__ */ ((e) => (e.New = "new", e.Fail = "fail", e.Connected = "connected", e.Connecting = "connecting", e.Terminating = "terminating", e))(I || {}), ie = /* @__PURE__ */ ((e) => (e.Amazon = "amazon", e.Microsoft = "microsoft", e.Afflorithmics = "afflorithmics", e.Elevenlabs = "elevenlabs", e))(ie || {}), se = /* @__PURE__ */ ((e) => (e.Public = "public", e.Premium = "premium", e.Private = "private", e))(se || {});
2
+ const z = "https://api.d-id.com", oe = "wss://notifications.d-id.com", ce = "79f81a83a67430be2bc0fd61042b8faa", F = () => Math.random().toString(16).slice(2);
3
+ function J() {
4
4
  let e = window.localStorage.getItem("did_external_key_id");
5
5
  return e || (e = Math.random().toString(16).slice(2), window.localStorage.setItem("did_external_key_id", e)), e;
6
6
  }
7
- let ce = K();
8
- function J(e) {
7
+ let de = F();
8
+ function j(e) {
9
9
  if (e.type === "bearer")
10
10
  return `Bearer ${e.token}`;
11
11
  if (e.type === "basic")
12
12
  return `Basic ${btoa(`${e.username}:${e.password}`)}`;
13
13
  if (e.type === "key")
14
- return `Client-Key ${e.clientKey}.${q()}_${ce}`;
14
+ return `Client-Key ${e.clientKey}.${J()}_${de}`;
15
15
  throw new Error(`Unknown auth type: ${e}`);
16
16
  }
17
- function V(e, r = O, c) {
18
- const t = async (a, n) => {
19
- const i = await fetch(r + (a != null && a.startsWith("/") ? a : `/${a}`), {
17
+ function B(e, r = z, s) {
18
+ const a = async (t, n) => {
19
+ const i = await fetch(r + (t != null && t.startsWith("/") ? t : `/${t}`), {
20
20
  ...n,
21
21
  headers: {
22
22
  ...n == null ? void 0 : n.headers,
23
- Authorization: J(e),
23
+ Authorization: j(e),
24
24
  "Content-Type": "application/json"
25
25
  }
26
26
  });
27
27
  if (!i.ok) {
28
- let s = await i.text().catch(() => "Failed to fetch");
29
- throw c && c(new Error(s), {
30
- url: a,
28
+ let o = await i.text().catch(() => "Failed to fetch");
29
+ throw s && s(new Error(o), {
30
+ url: t,
31
31
  options: n,
32
32
  headers: i.headers
33
- }), new Error(s);
33
+ }), new Error(o);
34
34
  }
35
35
  return i.json();
36
36
  };
37
37
  return {
38
- get(a, n) {
39
- return t(a, {
38
+ get(t, n) {
39
+ return a(t, {
40
40
  ...n,
41
41
  method: "GET"
42
42
  });
43
43
  },
44
- post(a, n, i) {
45
- return t(a, {
44
+ post(t, n, i) {
45
+ return a(t, {
46
46
  ...i,
47
47
  body: JSON.stringify(n),
48
48
  method: "POST"
49
49
  });
50
50
  },
51
- delete(a, n, i) {
52
- return t(a, {
51
+ delete(t, n, i) {
52
+ return a(t, {
53
53
  ...i,
54
54
  body: JSON.stringify(n),
55
55
  method: "DELETE"
56
56
  });
57
57
  },
58
- patch(a, n, i) {
59
- return t(a, {
58
+ patch(t, n, i) {
59
+ return a(t, {
60
60
  ...i,
61
61
  body: JSON.stringify(n),
62
62
  method: "PATCH"
@@ -64,100 +64,100 @@ function V(e, r = O, c) {
64
64
  }
65
65
  };
66
66
  }
67
- function j(e, r = O, c) {
68
- const t = V(e, `${r}/agents`, c);
67
+ function X(e, r = z, s) {
68
+ const a = B(e, `${r}/agents`, s);
69
69
  return {
70
- create(a, n) {
71
- return t.post("/", a, n);
70
+ create(t, n) {
71
+ return a.post("/", t, n);
72
72
  },
73
- getAgents(a, n) {
74
- return t.get(`/${a ? `?tag=${a}` : ""}`, n).then((i) => i ?? []);
73
+ getAgents(t, n) {
74
+ return a.get(`/${t ? `?tag=${t}` : ""}`, n).then((i) => i ?? []);
75
75
  },
76
- getById(a, n) {
77
- return t.get(`/${a}`, n);
76
+ getById(t, n) {
77
+ return a.get(`/${t}`, n);
78
78
  },
79
- delete(a, n) {
80
- return t.delete(`/${a}`, void 0, n);
79
+ delete(t, n) {
80
+ return a.delete(`/${t}`, void 0, n);
81
81
  },
82
- update(a, n, i) {
83
- return t.patch(`/${a}`, n, i);
82
+ update(t, n, i) {
83
+ return a.patch(`/${t}`, n, i);
84
84
  },
85
- newChat(a, n) {
86
- return t.post(`/${a}/chat`, void 0, n);
85
+ newChat(t, n) {
86
+ return a.post(`/${t}/chat`, void 0, n);
87
87
  },
88
- chat(a, n, i, s) {
89
- return t.post(`/${a}/chat/${n}`, i, s);
88
+ chat(t, n, i, o) {
89
+ return a.post(`/${t}/chat/${n}`, i, o);
90
90
  },
91
- createRating(a, n, i, s) {
92
- return t.post(`/${a}/chat/${n}/ratings`, i, s);
91
+ createRating(t, n, i, o) {
92
+ return a.post(`/${t}/chat/${n}/ratings`, i, o);
93
93
  },
94
- updateRating(a, n, i, s, d) {
95
- return t.patch(`/${a}/chat/${n}/ratings/${i}`, s, d);
94
+ updateRating(t, n, i, o, g) {
95
+ return a.patch(`/${t}/chat/${n}/ratings/${i}`, o, g);
96
96
  },
97
- deleteRating(a, n, i, s) {
98
- return t.delete(`/${a}/chat/${n}/ratings/${i}`, s);
97
+ deleteRating(t, n, i, o) {
98
+ return a.delete(`/${t}/chat/${n}/ratings/${i}`, o);
99
99
  }
100
100
  };
101
101
  }
102
- const de = (e) => new Promise((r) => setTimeout(r, e));
103
- function le(e) {
104
- return new Promise((r, c) => {
102
+ const le = (e) => new Promise((r) => setTimeout(r, e));
103
+ function ge(e) {
104
+ return new Promise((r, s) => {
105
105
  const {
106
- callbacks: t,
107
- host: a,
106
+ callbacks: a,
107
+ host: t,
108
108
  auth: n
109
109
  } = e, {
110
110
  onMessage: i = null,
111
- onOpen: s = null,
112
- onClose: d = null,
113
- onError: l = null
114
- } = t || {}, h = new WebSocket(`${a}?authorization=${J(n)}`);
115
- h.onmessage = i, h.onclose = d, h.onerror = (p) => {
116
- console.error(p), l == null || l("Websocket failed to connect", p), c(p);
117
- }, h.onopen = (p) => {
118
- s == null || s(p), r(h);
111
+ onOpen: o = null,
112
+ onClose: g = null,
113
+ onError: d = null
114
+ } = a || {}, l = new WebSocket(`${t}?authorization=${j(n)}`);
115
+ l.onmessage = i, l.onclose = g, l.onerror = (C) => {
116
+ console.error(C), d == null || d("Websocket failed to connect", C), s(C);
117
+ }, l.onopen = (C) => {
118
+ o == null || o(C), r(l);
119
119
  };
120
120
  });
121
121
  }
122
- async function ge(e) {
122
+ async function ue(e) {
123
123
  const {
124
124
  retries: r = 1
125
125
  } = e;
126
- let c = null;
127
- for (let t = 0; (c == null ? void 0 : c.readyState) !== WebSocket.OPEN; t++)
126
+ let s = null;
127
+ for (let a = 0; (s == null ? void 0 : s.readyState) !== WebSocket.OPEN; a++)
128
128
  try {
129
- c = await le(e);
130
- } catch (a) {
131
- if (t === r)
132
- throw a;
133
- await de(t * 500);
129
+ s = await ge(e);
130
+ } catch (t) {
131
+ if (a === r)
132
+ throw t;
133
+ await le(a * 500);
134
134
  }
135
- return c;
135
+ return s;
136
136
  }
137
- async function ue(e, r, c) {
138
- const t = c != null && c.onMessage ? [c.onMessage] : [], a = await ge({
137
+ async function me(e, r, s) {
138
+ const a = s != null && s.onMessage ? [s.onMessage] : [], t = await ue({
139
139
  auth: e,
140
140
  host: r,
141
141
  callbacks: {
142
- onError: c == null ? void 0 : c.onError,
142
+ onError: s == null ? void 0 : s.onError,
143
143
  onMessage: (n) => {
144
144
  const i = JSON.parse(n.data);
145
- t.forEach((s) => s(i.event, i));
145
+ a.forEach((o) => o(i.event, i));
146
146
  }
147
147
  }
148
148
  });
149
149
  return {
150
- socket: a,
151
- disconnect: () => a.close(),
152
- subscribeToEvents: (n) => t.push(n)
150
+ socket: t,
151
+ disconnect: () => t.close(),
152
+ subscribeToEvents: (n) => a.push(n)
153
153
  };
154
154
  }
155
- const me = "X-Playground-Chat";
156
- function we(e, r, c, t) {
157
- const a = V(e, `${r}/agents/${c}`, t);
155
+ const we = "X-Playground-Chat";
156
+ function he(e, r, s, a) {
157
+ const t = B(e, `${r}/agents/${s}`, a);
158
158
  return {
159
159
  createStream(n) {
160
- return a.post("/streams", {
160
+ return t.post("/streams", {
161
161
  driver_id: n.driver_id,
162
162
  presenter_id: n.presenter_id,
163
163
  compatibility_mode: n.compatibility_mode,
@@ -166,40 +166,40 @@ function we(e, r, c, t) {
166
166
  type: v.Clip
167
167
  });
168
168
  },
169
- startConnection(n, i, s) {
170
- return a.post(`/streams/${n}/sdp`, {
171
- session_id: s,
169
+ startConnection(n, i, o) {
170
+ return t.post(`/streams/${n}/sdp`, {
171
+ session_id: o,
172
172
  answer: i,
173
173
  type: v.Clip
174
174
  });
175
175
  },
176
- addIceCandidate(n, i, s) {
177
- return a.post(`/streams/${n}/ice`, {
178
- session_id: s,
176
+ addIceCandidate(n, i, o) {
177
+ return t.post(`/streams/${n}/ice`, {
178
+ session_id: o,
179
179
  ...i,
180
180
  type: v.Clip
181
181
  });
182
182
  },
183
- sendStreamRequest(n, i, s) {
184
- return a.post(`/streams/${n}`, {
183
+ sendStreamRequest(n, i, o) {
184
+ return t.post(`/streams/${n}`, {
185
185
  session_id: i,
186
- ...s,
186
+ ...o,
187
187
  type: v.Clip
188
188
  });
189
189
  },
190
190
  close(n, i) {
191
- return a.delete(`/streams/${n}`, {
191
+ return t.delete(`/streams/${n}`, {
192
192
  session_id: i,
193
193
  type: v.Clip
194
194
  });
195
195
  }
196
196
  };
197
197
  }
198
- function he(e, r, c, t) {
199
- const a = V(e, `${r}/agents/${c}`, t);
198
+ function fe(e, r, s, a) {
199
+ const t = B(e, `${r}/agents/${s}`, a);
200
200
  return {
201
201
  createStream(n, i) {
202
- return a.post("/streams", {
202
+ return t.post("/streams", {
203
203
  source_url: n.source_url,
204
204
  driver_url: n.driver_url,
205
205
  face: n.face,
@@ -211,153 +211,153 @@ function he(e, r, c, t) {
211
211
  type: v.Talk
212
212
  }, i);
213
213
  },
214
- startConnection(n, i, s, d) {
215
- return a.post(`/streams/${n}/sdp`, {
216
- session_id: s,
214
+ startConnection(n, i, o, g) {
215
+ return t.post(`/streams/${n}/sdp`, {
216
+ session_id: o,
217
217
  answer: i,
218
218
  type: v.Talk
219
- }, d);
219
+ }, g);
220
220
  },
221
- addIceCandidate(n, i, s, d) {
222
- return a.post(`/streams/${n}/ice`, {
223
- session_id: s,
221
+ addIceCandidate(n, i, o, g) {
222
+ return t.post(`/streams/${n}/ice`, {
223
+ session_id: o,
224
224
  ...i,
225
225
  type: v.Talk
226
- }, d);
226
+ }, g);
227
227
  },
228
- sendStreamRequest(n, i, s, d) {
229
- return a.post(`/streams/${n}`, {
228
+ sendStreamRequest(n, i, o, g) {
229
+ return t.post(`/streams/${n}`, {
230
230
  session_id: i,
231
- ...s,
231
+ ...o,
232
232
  type: v.Talk
233
- }, d);
233
+ }, g);
234
234
  },
235
- close(n, i, s) {
236
- return a.delete(`/streams/${n}`, {
235
+ close(n, i, o) {
236
+ return t.delete(`/streams/${n}`, {
237
237
  session_id: i,
238
238
  type: v.Talk
239
- }, s);
239
+ }, o);
240
240
  }
241
241
  };
242
242
  }
243
- let X = !1;
244
- const E = (e, r) => X && console.log(e, r), fe = (window.RTCPeerConnection || window.webkitRTCPeerConnection || window.mozRTCPeerConnection).bind(window);
245
- function H(e) {
243
+ let Y = !1;
244
+ const A = (e, r) => Y && console.log(e, r), pe = (window.RTCPeerConnection || window.webkitRTCPeerConnection || window.mozRTCPeerConnection).bind(window);
245
+ function W(e) {
246
246
  switch (e) {
247
247
  case "connected":
248
- return k.Connected;
248
+ return I.Connected;
249
249
  case "checking":
250
- return k.Connecting;
250
+ return I.Connecting;
251
251
  case "failed":
252
- return k.Fail;
252
+ return I.Fail;
253
253
  case "new":
254
254
  case "closed":
255
255
  case "disconnected":
256
256
  default:
257
- return k.New;
257
+ return I.New;
258
258
  }
259
259
  }
260
- function pe() {
260
+ function ye() {
261
261
  let e = 0;
262
262
  return (r) => {
263
- for (const c of r.values())
264
- if (c && c.type === "inbound-rtp" && c.kind === "video") {
265
- const t = c.bytesReceived, a = t - e > 0;
266
- return e = t, a;
263
+ for (const s of r.values())
264
+ if (s && s.type === "inbound-rtp" && s.kind === "video") {
265
+ const a = s.bytesReceived, t = a - e > 0;
266
+ return e = a, t;
267
267
  }
268
268
  return !1;
269
269
  };
270
270
  }
271
- function ye(e, r) {
272
- const t = Math.max(Math.ceil(10), 1);
273
- let a = 0, n = !1;
274
- const i = pe();
271
+ function ve(e, r) {
272
+ const a = Math.max(Math.ceil(10), 1);
273
+ let t = 0, n = !1;
274
+ const i = ye();
275
275
  return setInterval(async () => {
276
- const s = await e.getStats();
277
- i(s) ? (a = 0, n || (r == null || r(x.Start), n = !0)) : n && (a++, a >= t && (r == null || r(x.Stop), n = !1));
276
+ const o = await e.getStats();
277
+ i(o) ? (t = 0, n || (r == null || r(b.Start), n = !0)) : n && (t++, t >= a && (r == null || r(b.Stop), n = !1));
278
278
  }, 100);
279
279
  }
280
- async function ve(e, r, {
281
- debug: c = !1,
282
- callbacks: t,
283
- auth: a,
280
+ async function Ce(e, r, {
281
+ debug: s = !1,
282
+ callbacks: a,
283
+ auth: t,
284
284
  analytics: n,
285
- baseURL: i = O
285
+ baseURL: i = z
286
286
  }) {
287
- X = c;
288
- let s;
287
+ Y = s;
288
+ let o;
289
289
  const {
290
- startConnection: d,
291
- sendStreamRequest: l,
292
- close: h,
293
- createStream: p,
294
- addIceCandidate: A
295
- } = r.videoType === v.Clip ? we(a, i, e, t.onError) : he(a, i, e, t.onError), {
296
- id: I,
297
- offer: L,
298
- ice_servers: z,
290
+ startConnection: g,
291
+ sendStreamRequest: d,
292
+ close: l,
293
+ createStream: C,
294
+ addIceCandidate: T
295
+ } = r.videoType === v.Clip ? he(t, i, e, a.onError) : fe(t, i, e, a.onError), {
296
+ id: S,
297
+ offer: N,
298
+ ice_servers: K,
299
299
  session_id: R
300
- } = await p(r), o = new fe({
301
- iceServers: z
302
- }), u = o.createDataChannel("JanusDataChannel");
300
+ } = await C(r), p = new pe({
301
+ iceServers: K
302
+ }), c = p.createDataChannel("JanusDataChannel");
303
303
  if (!R)
304
304
  throw new Error("Could not create session_id");
305
- const f = ye(o, t.onVideoStateChange);
306
- o.onicecandidate = (g) => {
307
- E("peerConnection.onicecandidate", g), g.candidate && g.candidate.sdpMid && g.candidate.sdpMLineIndex !== null ? A(I, {
308
- candidate: g.candidate.candidate,
309
- sdpMid: g.candidate.sdpMid,
310
- sdpMLineIndex: g.candidate.sdpMLineIndex
311
- }, R) : A(I, {
305
+ const m = ve(p, a.onVideoStateChange);
306
+ p.onicecandidate = (u) => {
307
+ A("peerConnection.onicecandidate", u), u.candidate && u.candidate.sdpMid && u.candidate.sdpMLineIndex !== null ? T(S, {
308
+ candidate: u.candidate.candidate,
309
+ sdpMid: u.candidate.sdpMid,
310
+ sdpMLineIndex: u.candidate.sdpMLineIndex
311
+ }, R) : T(S, {
312
312
  candidate: null
313
313
  }, R);
314
- }, o.oniceconnectionstatechange = () => {
315
- var m;
316
- E("peerConnection.oniceconnectionstatechange => " + o.iceConnectionState);
317
- const g = H(o.iceConnectionState);
318
- g === k.Connected ? s = setTimeout(() => {
314
+ }, p.oniceconnectionstatechange = () => {
315
+ var h;
316
+ A("peerConnection.oniceconnectionstatechange => " + p.iceConnectionState);
317
+ const u = W(p.iceConnectionState);
318
+ u === I.Connected ? o = setTimeout(() => {
319
319
  var w;
320
- (w = t.onConnectionStateChange) == null || w.call(t, k.Connected);
321
- }, 5e3) : (clearTimeout(s), (m = t.onConnectionStateChange) == null || m.call(t, g));
322
- }, o.ontrack = (g) => {
323
- var m;
324
- E("peerConnection.ontrack", g), (m = t.onSrcObjectReady) == null || m.call(t, g.streams[0]);
325
- }, u.onmessage = (g) => {
326
- var m;
327
- if (u.readyState === "open") {
328
- const [w, y] = g.data.split(":");
329
- w === F.StreamReady && (clearTimeout(s), (m = t.onConnectionStateChange) == null || m.call(t, k.Connected));
320
+ (w = a.onConnectionStateChange) == null || w.call(a, I.Connected);
321
+ }, 5e3) : (clearTimeout(o), (h = a.onConnectionStateChange) == null || h.call(a, u));
322
+ }, p.ontrack = (u) => {
323
+ var h;
324
+ A("peerConnection.ontrack", u), (h = a.onSrcObjectReady) == null || h.call(a, u.streams[0]);
325
+ }, c.onmessage = (u) => {
326
+ var h;
327
+ if (c.readyState === "open") {
328
+ const [w, f] = u.data.split(":");
329
+ w === U.StreamReady && (clearTimeout(o), (h = a.onConnectionStateChange) == null || h.call(a, I.Connected));
330
330
  }
331
- }, await o.setRemoteDescription(L), E("set remote description OK");
332
- const _ = await o.createAnswer();
333
- return E("create answer OK"), await o.setLocalDescription(_), E("set local description OK"), await d(I, _, R), E("start connection OK"), {
331
+ }, await p.setRemoteDescription(N), A("set remote description OK");
332
+ const y = await p.createAnswer();
333
+ return A("create answer OK"), await p.setLocalDescription(y), A("set local description OK"), await g(S, y, R), A("start connection OK"), {
334
334
  /**
335
335
  * Method to send request to server to get clip or talk depend on you payload
336
336
  * @param payload
337
337
  */
338
- speak(g) {
339
- return l(I, R, g);
338
+ speak(u) {
339
+ return d(S, R, u);
340
340
  },
341
341
  /**
342
342
  * Method to close RTC connection
343
343
  */
344
344
  async disconnect() {
345
- var g, m, w;
346
- if (I) {
347
- if (o) {
348
- if (H(o.iceConnectionState) === k.New) {
349
- (g = t.onVideoStateChange) == null || g.call(t, x.Stop), clearInterval(f);
345
+ var u, h, w;
346
+ if (S) {
347
+ if (p) {
348
+ if (W(p.iceConnectionState) === I.New) {
349
+ (u = a.onVideoStateChange) == null || u.call(a, b.Stop), clearInterval(m);
350
350
  return;
351
351
  }
352
- o.close(), o.oniceconnectionstatechange = null, o.onnegotiationneeded = null, o.onicecandidate = null, o.ontrack = null;
352
+ p.close(), p.oniceconnectionstatechange = null, p.onnegotiationneeded = null, p.onicecandidate = null, p.ontrack = null;
353
353
  }
354
354
  try {
355
- await h(I, R).catch((y) => {
355
+ await l(S, R).catch((f) => {
356
356
  });
357
- } catch (y) {
358
- E("Error on close stream connection", y);
357
+ } catch (f) {
358
+ A("Error on close stream connection", f);
359
359
  }
360
- (m = t.onConnectionStateChange) == null || m.call(t, k.New), (w = t.onVideoStateChange) == null || w.call(t, x.Stop), clearInterval(f);
360
+ (h = a.onConnectionStateChange) == null || h.call(a, I.New), (w = a.onVideoStateChange) == null || w.call(a, b.Stop), clearInterval(m);
361
361
  }
362
362
  },
363
363
  /**
@@ -367,39 +367,39 @@ async function ve(e, r, {
367
367
  /**
368
368
  * Id of current RTC stream
369
369
  */
370
- streamId: I
370
+ streamId: S
371
371
  };
372
372
  }
373
- let U = {};
374
- function Ce(e) {
375
- const r = window != null && window.hasOwnProperty("DID_AGENTS_API") ? "agents-ui" : "agents-sdk", c = {
373
+ let V = {};
374
+ function _e(e) {
375
+ const r = window != null && window.hasOwnProperty("DID_AGENTS_API") ? "agents-ui" : "agents-sdk", s = {
376
376
  token: e.token || "testKey",
377
- distinct_id: e.distinctId || q(),
377
+ distinct_id: e.distinctId || J(),
378
378
  agentId: e.agent.id,
379
379
  agentType: e.agent.presenter.type,
380
380
  owner_id: e.agent.owner_id ?? ""
381
381
  };
382
382
  return {
383
- ...c,
383
+ ...s,
384
384
  isEnabled: e.isEnabled ?? !0,
385
385
  getRandom: () => Math.random().toString(16).slice(2),
386
- track(t, a) {
386
+ track(a, t) {
387
387
  if (!this.isEnabled)
388
388
  return Promise.reject("MixPanel analytics is disabled on creation");
389
389
  const {
390
390
  audioPath: n,
391
391
  ...i
392
- } = a || {}, s = {
392
+ } = t || {}, o = {
393
393
  method: "POST",
394
394
  headers: {
395
395
  "Content-Type": "application/x-www-form-urlencoded"
396
396
  },
397
397
  body: new URLSearchParams({
398
398
  data: JSON.stringify([{
399
- event: t,
399
+ event: a,
400
400
  properties: {
401
401
  ...i,
402
- ...c,
402
+ ...s,
403
403
  source: r,
404
404
  time: Date.now(),
405
405
  $insert_id: this.getRandom(),
@@ -411,79 +411,79 @@ function Ce(e) {
411
411
  }])
412
412
  })
413
413
  };
414
- return fetch("https://api-js.mixpanel.com/track/?verbose=1&ip=1", s).then((d) => d.json()).catch((d) => console.error(d));
414
+ return fetch("https://api-js.mixpanel.com/track/?verbose=1&ip=1", o).then((g) => g.json()).catch((g) => console.error(g));
415
415
  },
416
- linkTrack(t, a, n, i) {
417
- U[t] || (U[t] = {
416
+ linkTrack(a, t, n, i) {
417
+ V[a] || (V[a] = {
418
418
  events: {},
419
419
  resolvedDependencies: []
420
420
  }), i.includes(n) || i.push(n);
421
- const s = U[t];
422
- if (s.events[n] = {
423
- props: a
424
- }, s.resolvedDependencies.push(n), i.every((l) => s.resolvedDependencies.includes(l))) {
425
- const l = i.reduce((h, p) => s.events[p] ? {
426
- ...h,
427
- ...s.events[p].props
428
- } : h, {});
429
- this.track(t, l), s.resolvedDependencies = s.resolvedDependencies.filter((h) => !i.includes(h)), i.forEach((h) => {
430
- delete s.events[h];
421
+ const o = V[a];
422
+ if (o.events[n] = {
423
+ props: t
424
+ }, o.resolvedDependencies.push(n), i.every((d) => o.resolvedDependencies.includes(d))) {
425
+ const d = i.reduce((l, C) => o.events[C] ? {
426
+ ...l,
427
+ ...o.events[C].props
428
+ } : l, {});
429
+ this.track(a, d), o.resolvedDependencies = o.resolvedDependencies.filter((l) => !i.includes(l)), i.forEach((l) => {
430
+ delete o.events[l];
431
431
  });
432
432
  }
433
433
  }
434
434
  };
435
435
  }
436
- function _e(e) {
437
- var t, a, n, i, s;
436
+ function Se(e) {
437
+ var a, t, n, i, o;
438
438
  const r = () => /Mobi|Android/i.test(navigator.userAgent) ? "Mobile" : "Desktop";
439
439
  return {
440
440
  $os: `${(() => {
441
- const d = navigator.platform;
442
- return d.toLowerCase().includes("win") ? "Windows" : d.toLowerCase().includes("mac") ? "Mac OS X" : d.toLowerCase().includes("linux") ? "Linux" : "Unknown";
441
+ const g = navigator.platform;
442
+ return g.toLowerCase().includes("win") ? "Windows" : g.toLowerCase().includes("mac") ? "Mac OS X" : g.toLowerCase().includes("linux") ? "Linux" : "Unknown";
443
443
  })()}`,
444
444
  isMobile: `${r() == "Mobile"}`,
445
445
  browser: navigator.userAgent,
446
446
  origin: window.location.origin,
447
- agentType: (t = e.presenter) == null ? void 0 : t.type,
447
+ agentType: (a = e.presenter) == null ? void 0 : a.type,
448
448
  agentVoice: {
449
- voiceId: (n = (a = e.presenter) == null ? void 0 : a.voice) == null ? void 0 : n.voice_id,
450
- provider: (s = (i = e.presenter) == null ? void 0 : i.voice) == null ? void 0 : s.type
449
+ voiceId: (n = (t = e.presenter) == null ? void 0 : t.voice) == null ? void 0 : n.voice_id,
450
+ provider: (o = (i = e.presenter) == null ? void 0 : i.voice) == null ? void 0 : o.type
451
451
  }
452
452
  };
453
453
  }
454
- function Se(e, r, c) {
455
- var l, h;
454
+ function Me(e, r, s) {
455
+ var d, l;
456
456
  const {
457
- event: t,
458
- ...a
457
+ event: a,
458
+ ...t
459
459
  } = e, {
460
460
  template: n
461
461
  } = (r == null ? void 0 : r.llm) || {}, {
462
462
  language: i
463
- } = ((l = r == null ? void 0 : r.presenter) == null ? void 0 : l.voice) || {}, {
464
- stitch: s
463
+ } = ((d = r == null ? void 0 : r.presenter) == null ? void 0 : d.voice) || {}, {
464
+ stitch: o
465
465
  } = (r == null ? void 0 : r.presenter) || {};
466
466
  return {
467
- ...a,
467
+ ...t,
468
468
  llm: {
469
- ...a.llm,
469
+ ...t.llm,
470
470
  template: n
471
471
  },
472
472
  script: {
473
- ...a.script,
473
+ ...t.script,
474
474
  provider: {
475
- ...(h = a == null ? void 0 : a.script) == null ? void 0 : h.provider,
475
+ ...(l = t == null ? void 0 : t.script) == null ? void 0 : l.provider,
476
476
  language: i
477
477
  }
478
478
  },
479
- stitch: s,
480
- ...c
479
+ stitch: o,
480
+ ...s
481
481
  };
482
482
  }
483
- const Me = 1080;
483
+ const ke = 1080;
484
484
  let D = 0;
485
- function ke(e, r) {
486
- var c, t, a;
485
+ function Ie(e, r) {
486
+ var s, a, t;
487
487
  if (e.presenter) {
488
488
  if (e.presenter.type === v.Clip)
489
489
  return {
@@ -497,32 +497,32 @@ function ke(e, r) {
497
497
  return {
498
498
  videoType: v.Talk,
499
499
  source_url: e.presenter.source_url,
500
- session_timeout: (c = r == null ? void 0 : r.streamOptions) == null ? void 0 : c.session_timeout,
501
- stream_warmup: ((t = r == null ? void 0 : r.streamOptions) == null ? void 0 : t.stream_warmup) ?? !0,
502
- compatibility_mode: (a = r == null ? void 0 : r.streamOptions) == null ? void 0 : a.compatibility_mode,
503
- output_resolution: (r == null ? void 0 : r.outputResolution) || (e.presenter.stitch ? Me : void 0)
500
+ session_timeout: (s = r == null ? void 0 : r.streamOptions) == null ? void 0 : s.session_timeout,
501
+ stream_warmup: ((a = r == null ? void 0 : r.streamOptions) == null ? void 0 : a.stream_warmup) ?? !0,
502
+ compatibility_mode: (t = r == null ? void 0 : r.streamOptions) == null ? void 0 : t.compatibility_mode,
503
+ output_resolution: (r == null ? void 0 : r.outputResolution) || (e.presenter.stitch ? ke : void 0)
504
504
  };
505
505
  }
506
- function Y(e) {
507
- return e === M.Playground ? {
506
+ function Z(e) {
507
+ return e === k.Playground ? {
508
508
  headers: {
509
- [me]: "true"
509
+ [we]: "true"
510
510
  }
511
511
  } : {};
512
512
  }
513
- async function Z(e, r, c, t) {
513
+ async function Q(e, r, s, a) {
514
514
  try {
515
- const a = await r.newChat(e, Y(t));
516
- return c.track("agent-chat", {
515
+ const t = await r.newChat(e, Z(a));
516
+ return s.track("agent-chat", {
517
517
  event: "created",
518
- chat_id: a.id,
518
+ chat_id: t.id,
519
519
  agent_id: e,
520
- mode: t
521
- }), a;
522
- } catch (a) {
520
+ mode: a
521
+ }), t;
522
+ } catch (t) {
523
523
  try {
524
- console.error(a);
525
- const n = JSON.parse(a.message);
524
+ console.error(t);
525
+ const n = JSON.parse(t.message);
526
526
  if ((n == null ? void 0 : n.kind) === "InsufficientCreditsError")
527
527
  throw new Error("InsufficientCreditsError");
528
528
  } catch (n) {
@@ -531,313 +531,313 @@ async function Z(e, r, c, t) {
531
531
  throw new Error("Cannot create new chat");
532
532
  }
533
533
  }
534
- function Ie(e, r, c, t, a) {
534
+ function Re(e, r, s, a, t) {
535
535
  return new Promise(async (n, i) => {
536
536
  D = 0;
537
- const s = await ve(e.id, ke(e, r), {
537
+ const o = await Ce(e.id, Ie(e, r), {
538
538
  ...r,
539
- analytics: t,
539
+ analytics: a,
540
540
  callbacks: {
541
541
  ...r.callbacks,
542
- onConnectionStateChange: async (d) => {
543
- var l, h;
544
- d === k.Connected && (!a && r.mode !== M.DirectPlayback && (a = await Z(e.id, c, t, r.mode).catch((p) => {
545
- i(p);
546
- })), s ? n({
547
- chat: a,
548
- streamingManager: s
549
- }) : a && i(new Error("Something went wrong while initializing the manager"))), (h = (l = r.callbacks).onConnectionStateChange) == null || h.call(l, d);
542
+ onConnectionStateChange: async (g) => {
543
+ var d, l;
544
+ g === I.Connected && (!t && r.mode !== k.DirectPlayback && (t = await Q(e.id, s, a, r.mode).catch((C) => {
545
+ i(C);
546
+ })), o ? n({
547
+ chat: t,
548
+ streamingManager: o
549
+ }) : t && i(new Error("Something went wrong while initializing the manager"))), (l = (d = r.callbacks).onConnectionStateChange) == null || l.call(d, g);
550
550
  },
551
- onVideoStateChange(d) {
552
- var l, h;
553
- (h = (l = r.callbacks).onVideoStateChange) == null || h.call(l, d), D > 0 && d === x.Start && t.linkTrack("agent-video", {
551
+ onVideoStateChange(g) {
552
+ var d, l;
553
+ (l = (d = r.callbacks).onVideoStateChange) == null || l.call(d, g), D > 0 && g === b.Start && a.linkTrack("agent-video", {
554
554
  event: "start",
555
555
  latency: Date.now() - D
556
- }, "start", [F.StreamVideoCreated]);
556
+ }, "start", [U.StreamVideoCreated]);
557
557
  }
558
558
  }
559
559
  }).catch(i);
560
560
  });
561
561
  }
562
- function W(e) {
562
+ function q(e) {
563
563
  let r = "";
564
564
  if (e.greetings && e.greetings.length > 0) {
565
- const c = Math.floor(Math.random() * e.greetings.length);
566
- r = e.greetings[c];
565
+ const s = Math.floor(Math.random() * e.greetings.length);
566
+ r = e.greetings[s];
567
567
  } else
568
568
  r = `Hi! I'm ${e.preview_name || "My Agent"}. How can I help you?`;
569
569
  return [{
570
570
  content: r,
571
- id: K(),
571
+ id: F(),
572
572
  role: "assistant",
573
573
  created_at: (/* @__PURE__ */ new Date()).toISOString()
574
574
  }];
575
575
  }
576
- function Re(e) {
576
+ function Ee(e) {
577
577
  if (e.answer !== void 0)
578
578
  return e.answer;
579
- let r = 0, c = "";
579
+ let r = 0, s = "";
580
580
  for (; r in e; )
581
- c += e[r], r++;
582
- return c;
581
+ s += e[r], r++;
582
+ return s;
583
583
  }
584
- function Ee(e, r, c, t, a) {
585
- if (!(e === N.Partial || e === N.Answer))
584
+ function Ae(e, r, s, a, t) {
585
+ if (!(e === L.Partial || e === L.Answer))
586
586
  return;
587
- const n = t.messages[t.messages.length - 1];
587
+ const n = a.messages[a.messages.length - 1];
588
588
  if ((n == null ? void 0 : n.role) !== "assistant")
589
589
  return;
590
590
  const {
591
591
  content: i,
592
- sequence: s
592
+ sequence: o
593
593
  } = r;
594
- e === N.Partial ? c[s] = i : c.answer = i;
595
- const d = Re(c);
596
- n.content !== d && (n.content = d, a == null || a(t.messages, e));
594
+ e === L.Partial ? s[o] = i : s.answer = i;
595
+ const g = Ee(s);
596
+ n.content !== g && (n.content = g, t == null || t(a.messages, e));
597
597
  }
598
- async function Ae(e, r) {
599
- var L, z, R;
600
- let c = {};
598
+ async function $e(e, r) {
599
+ var K, R, p;
600
+ let s = {}, a = !0;
601
601
  const t = {
602
602
  messages: [],
603
- chatMode: r.mode || M.Functional
604
- }, a = r.baseURL || O, n = r.wsURL || se, i = r.mixpanelKey || oe, s = j(r.auth, a, r.callbacks.onError), d = await s.getById(e);
605
- t.messages = W(d), (z = (L = r.callbacks).onNewMessage) == null || z.call(L, t.messages, "answer");
606
- const l = Ce({
607
- token: i,
603
+ chatMode: r.mode || k.Functional
604
+ }, n = r.baseURL || z, i = r.wsURL || oe, o = r.mixpanelKey || ce, g = X(r.auth, n, r.callbacks.onError), d = await g.getById(e);
605
+ t.messages = q(d), (R = (K = r.callbacks).onNewMessage) == null || R.call(K, t.messages, "answer");
606
+ const l = _e({
607
+ token: o,
608
608
  agent: d,
609
609
  ...r
610
610
  });
611
611
  l.track("agent-sdk", {
612
612
  event: "loaded",
613
- ..._e(d)
613
+ ...Se(d)
614
614
  });
615
- const h = {
616
- onMessage: (o, u) => {
617
- var f, _, g, m;
618
- if ("content" in u)
619
- Ee(o, u, c, t, r.callbacks.onNewMessage), o === N.Answer && l.track("agent-message-received", {
615
+ const C = {
616
+ onMessage: (c, m) => {
617
+ var y, u, h, w;
618
+ if ("content" in m)
619
+ Ae(c, m, s, t, r.callbacks.onNewMessage), c === L.Answer && l.track("agent-message-received", {
620
620
  messages: t.messages.length,
621
621
  mode: t.chatMode
622
622
  });
623
623
  else {
624
- const w = F, y = [w.StreamVideoDone, w.StreamVideoError, w.StreamVideoRejected], C = [w.StreamFailed, w.StreamVideoError, w.StreamVideoRejected], S = Se(u, d, {
624
+ const f = U, E = [f.StreamVideoDone, f.StreamVideoError, f.StreamVideoRejected], _ = [f.StreamFailed, f.StreamVideoError, f.StreamVideoRejected], M = Me(m, d, {
625
625
  mode: t.chatMode
626
626
  });
627
- if (o = o, o === w.StreamVideoCreated)
628
- l.linkTrack("agent-video", S, w.StreamVideoCreated, ["start"]);
629
- else if (y.includes(o)) {
630
- const $ = o.split("/")[1];
627
+ if (c = c, c === f.StreamVideoCreated)
628
+ l.linkTrack("agent-video", M, f.StreamVideoCreated, ["start"]);
629
+ else if (E.includes(c)) {
630
+ const $ = c.split("/")[1];
631
631
  l.track("agent-video", {
632
- ...S,
632
+ ...M,
633
633
  event: $
634
634
  });
635
635
  }
636
- C.includes(o) && ((_ = (f = r.callbacks).onError) == null || _.call(f, new Error(`Stream failed with event ${o}`), {
637
- data: u
638
- })), u.event === w.StreamDone && ((m = (g = r.callbacks).onConnectionStateChange) == null || m.call(g, k.New));
636
+ _.includes(c) && ((u = (y = r.callbacks).onError) == null || u.call(y, new Error(`Stream failed with event ${c}`), {
637
+ data: m
638
+ })), m.event === f.StreamDone && ((w = (h = r.callbacks).onConnectionStateChange) == null || w.call(h, I.New));
639
639
  }
640
640
  }
641
641
  };
642
- async function p(o) {
643
- var w, y, C, S, $;
644
- D = 0, o && (delete t.chat, t.messages = W(d), (y = (w = r.callbacks).onNewMessage) == null || y.call(w, t.messages, "answer"));
645
- const u = r.mode === M.DirectPlayback ? Promise.resolve(void 0) : ue(r.auth, n, h), f = Ie(d, r, s, l, t.chat).catch((P) => {
646
- throw I(M.Maintenance), P;
647
- }), [_, {
648
- streamingManager: g,
649
- chat: m
650
- }] = await Promise.all([u, f]);
651
- m && m.id !== ((C = t.chat) == null ? void 0 : C.id) && (($ = (S = r.callbacks).onNewChat) == null || $.call(S, m.id)), t.streamingManager = g, t.socketManager = _, t.chat = m, I((m == null ? void 0 : m.chat_mode) ?? r.mode ?? M.Functional);
642
+ async function T(c) {
643
+ var f, E, _, M, $;
644
+ D = 0, c && !a && (delete t.chat, t.messages = q(d), (E = (f = r.callbacks).onNewMessage) == null || E.call(f, t.messages, "answer"));
645
+ const m = r.mode === k.DirectPlayback ? Promise.resolve(void 0) : me(r.auth, i, C), y = Re(d, r, g, l, t.chat).catch((P) => {
646
+ throw N(k.Maintenance), P;
647
+ }), [u, {
648
+ streamingManager: h,
649
+ chat: w
650
+ }] = await Promise.all([m, y]);
651
+ w && w.id !== ((_ = t.chat) == null ? void 0 : _.id) && (($ = (M = r.callbacks).onNewChat) == null || $.call(M, w.id)), t.streamingManager = h, t.socketManager = u, t.chat = w, a = !1, N((w == null ? void 0 : w.chat_mode) ?? r.mode ?? k.Functional);
652
652
  }
653
- async function A() {
654
- var o, u;
655
- (o = t.socketManager) == null || o.disconnect(), await ((u = t.streamingManager) == null ? void 0 : u.disconnect()), delete t.streamingManager, delete t.socketManager;
653
+ async function S() {
654
+ var c, m;
655
+ (c = t.socketManager) == null || c.disconnect(), await ((m = t.streamingManager) == null ? void 0 : m.disconnect()), delete t.streamingManager, delete t.socketManager;
656
656
  }
657
- async function I(o) {
658
- var u, f;
659
- o !== t.chatMode && (l.track("agent-mode-change", {
660
- mode: o
661
- }), t.chatMode = o, t.chatMode !== M.Functional && await A(), (f = (u = r.callbacks).onModeChange) == null || f.call(u, o));
657
+ async function N(c) {
658
+ var m, y;
659
+ c !== t.chatMode && (l.track("agent-mode-change", {
660
+ mode: c
661
+ }), t.chatMode = c, t.chatMode !== k.Functional && await S(), (y = (m = r.callbacks).onModeChange) == null || y.call(m, c));
662
662
  }
663
663
  return {
664
664
  agent: d,
665
- starterMessages: ((R = d.knowledge) == null ? void 0 : R.starter_message) || [],
666
- changeMode: I,
665
+ starterMessages: ((p = d.knowledge) == null ? void 0 : p.starter_message) || [],
666
+ changeMode: N,
667
667
  async connect() {
668
- var o;
669
- await p(!0), l.track("agent-chat", {
668
+ var c;
669
+ await T(!0), l.track("agent-chat", {
670
670
  event: "connect",
671
- chatId: (o = t.chat) == null ? void 0 : o.id,
671
+ chatId: (c = t.chat) == null ? void 0 : c.id,
672
672
  agentId: d.id,
673
673
  mode: t.chatMode
674
674
  });
675
675
  },
676
676
  async reconnect() {
677
- var o;
678
- await A(), await p(!1), l.track("agent-chat", {
677
+ var c;
678
+ await S(), await T(!1), l.track("agent-chat", {
679
679
  event: "reconnect",
680
- chatId: (o = t.chat) == null ? void 0 : o.id,
680
+ chatId: (c = t.chat) == null ? void 0 : c.id,
681
681
  agentId: d.id,
682
682
  mode: t.chatMode
683
683
  });
684
684
  },
685
685
  async disconnect() {
686
- var o;
687
- await A(), l.track("agent-chat", {
686
+ var c;
687
+ await S(), l.track("agent-chat", {
688
688
  event: "disconnect",
689
- chatId: (o = t.chat) == null ? void 0 : o.id,
689
+ chatId: (c = t.chat) == null ? void 0 : c.id,
690
690
  agentId: d.id,
691
691
  mode: t.chatMode
692
692
  });
693
693
  },
694
- async chat(o) {
695
- var f, _, g, m, w, y;
696
- const u = K();
697
- c = {};
694
+ async chat(c) {
695
+ var y, u, h, w, f, E;
696
+ const m = F();
697
+ s = {};
698
698
  try {
699
- if (D = Date.now(), r.mode === M.DirectPlayback)
699
+ if (D = Date.now(), r.mode === k.DirectPlayback)
700
700
  throw new Error("Direct playback is enabled, chat is disabled");
701
- if (o.length >= 800)
701
+ if (c.length >= 800)
702
702
  throw new Error("Message cannot be more than 800 characters");
703
- if (o.length === 0)
703
+ if (c.length === 0)
704
704
  throw new Error("Message cannot be empty");
705
- if (t.chatMode === M.Maintenance)
705
+ if (t.chatMode === k.Maintenance)
706
706
  throw new Error("Chat is in maintenance mode");
707
- if (![M.TextOnly, M.Playground].includes(t.chatMode))
707
+ if (![k.TextOnly, k.Playground].includes(t.chatMode))
708
708
  if (t.streamingManager) {
709
709
  if (!t.chat)
710
710
  throw new Error("Chat is not initialized");
711
711
  } else
712
712
  throw new Error("Streaming manager is not initialized");
713
- t.chat || (t.chat = await Z(d.id, s, l, t.chatMode), (_ = (f = r.callbacks).onNewChat) == null || _.call(f, t.chat.id)), t.messages.push({
714
- id: K(),
713
+ t.chat || (t.chat = await Q(d.id, g, l, t.chatMode), (u = (y = r.callbacks).onNewChat) == null || u.call(y, t.chat.id)), t.messages.push({
714
+ id: F(),
715
715
  role: "user",
716
- content: o,
716
+ content: c,
717
717
  created_at: new Date(D).toISOString()
718
- }), (m = (g = r.callbacks).onNewMessage) == null || m.call(g, t.messages, "user");
719
- const C = {
720
- id: u,
718
+ }), (w = (h = r.callbacks).onNewMessage) == null || w.call(h, t.messages, "user");
719
+ const _ = {
720
+ id: m,
721
721
  role: "assistant",
722
722
  content: "",
723
723
  created_at: (/* @__PURE__ */ new Date()).toISOString(),
724
724
  matches: []
725
- }, S = [...t.messages];
726
- t.messages.push(C);
727
- const $ = (T) => {
728
- var b, B;
729
- return s.chat(d.id, T, {
730
- sessionId: (b = t.streamingManager) == null ? void 0 : b.sessionId,
731
- streamId: (B = t.streamingManager) == null ? void 0 : B.streamId,
725
+ }, M = [...t.messages];
726
+ t.messages.push(_);
727
+ const $ = (x) => {
728
+ var O, H;
729
+ return g.chat(d.id, x, {
730
+ sessionId: (O = t.streamingManager) == null ? void 0 : O.sessionId,
731
+ streamId: (H = t.streamingManager) == null ? void 0 : H.streamId,
732
732
  chatMode: t.chatMode,
733
- messages: S
734
- }, Y(t.chatMode));
735
- }, P = await $(t.chat.id).catch(async (T) => {
736
- var b;
737
- if (!((b = T == null ? void 0 : T.message) != null && b.includes("missing or invalid session_id")))
738
- throw T;
739
- return await A(), await p(!1), $(t.chat.id);
733
+ messages: M
734
+ }, Z(t.chatMode));
735
+ }, P = await $(t.chat.id).catch(async (x) => {
736
+ var O;
737
+ if (!((O = x == null ? void 0 : x.message) != null && O.includes("missing or invalid session_id")))
738
+ throw x;
739
+ return await S(), await T(!1), $(t.chat.id);
740
740
  });
741
741
  return l.track("agent-message-send", {
742
742
  event: "success",
743
743
  mode: t.chatMode,
744
744
  messages: t.messages.length + 1
745
- }), C.context = P.context, C.matches = P.matches, P.result && (C.content = P.result, l.track("agent-message-received", {
745
+ }), _.context = P.context, _.matches = P.matches, P.result && (_.content = P.result, l.track("agent-message-received", {
746
746
  latency: Date.now() - D,
747
747
  mode: t.chatMode,
748
748
  messages: t.messages.length
749
- }), (y = (w = r.callbacks).onNewMessage) == null || y.call(w, t.messages, "answer")), P;
750
- } catch (C) {
751
- throw t.messages[t.messages.length - 1].id === u && t.messages.pop(), l.track("agent-message-send", {
749
+ }), (E = (f = r.callbacks).onNewMessage) == null || E.call(f, t.messages, "answer")), P;
750
+ } catch (_) {
751
+ throw t.messages[t.messages.length - 1].id === m && t.messages.pop(), l.track("agent-message-send", {
752
752
  event: "error",
753
753
  mode: t.chatMode,
754
754
  messages: t.messages.length
755
- }), C;
755
+ }), _;
756
756
  }
757
757
  },
758
- rate(o, u, f) {
759
- var m, w, y, C;
760
- const _ = t.messages.find((S) => S.id === o);
758
+ rate(c, m, y) {
759
+ var w, f, E, _;
760
+ const u = t.messages.find((M) => M.id === c);
761
761
  if (t.chat) {
762
- if (!_)
762
+ if (!u)
763
763
  throw new Error("Message not found");
764
764
  } else
765
765
  throw new Error("Chat is not initialized");
766
- const g = ((m = _.matches) == null ? void 0 : m.map((S) => [S.document_id, S.id])) ?? [];
766
+ const h = ((w = u.matches) == null ? void 0 : w.map((M) => [M.document_id, M.id])) ?? [];
767
767
  return l.track("agent-rate", {
768
- event: f ? "update" : "create",
769
- thumb: u === 1 ? "up" : "down",
770
- knowledge_id: ((w = d.knowledge) == null ? void 0 : w.id) ?? "",
768
+ event: y ? "update" : "create",
769
+ thumb: m === 1 ? "up" : "down",
770
+ knowledge_id: ((f = d.knowledge) == null ? void 0 : f.id) ?? "",
771
771
  mode: t.chatMode,
772
- matches: g,
773
- score: u
774
- }), f ? s.updateRating(d.id, t.chat.id, f, {
775
- knowledge_id: ((y = d.knowledge) == null ? void 0 : y.id) ?? "",
776
- message_id: o,
777
- matches: g,
778
- score: u
779
- }) : s.createRating(d.id, t.chat.id, {
780
- knowledge_id: ((C = d.knowledge) == null ? void 0 : C.id) ?? "",
781
- message_id: o,
782
- matches: g,
783
- score: u
772
+ matches: h,
773
+ score: m
774
+ }), y ? g.updateRating(d.id, t.chat.id, y, {
775
+ knowledge_id: ((E = d.knowledge) == null ? void 0 : E.id) ?? "",
776
+ message_id: c,
777
+ matches: h,
778
+ score: m
779
+ }) : g.createRating(d.id, t.chat.id, {
780
+ knowledge_id: ((_ = d.knowledge) == null ? void 0 : _.id) ?? "",
781
+ message_id: c,
782
+ matches: h,
783
+ score: m
784
784
  });
785
785
  },
786
- deleteRate(o) {
787
- var u;
786
+ deleteRate(c) {
787
+ var m;
788
788
  if (!t.chat)
789
789
  throw new Error("Chat is not initialized");
790
790
  return l.track("agent-rate-delete", {
791
791
  type: "text",
792
- chat_id: (u = t.chat) == null ? void 0 : u.id,
793
- id: o,
792
+ chat_id: (m = t.chat) == null ? void 0 : m.id,
793
+ id: c,
794
794
  mode: t.chatMode
795
- }), s.deleteRating(d.id, t.chat.id, o);
795
+ }), g.deleteRating(d.id, t.chat.id, c);
796
796
  },
797
- speak(o) {
797
+ speak(c) {
798
798
  if (!t.streamingManager)
799
799
  throw new Error("Please connect to the agent first");
800
- function u() {
801
- if (typeof o == "string") {
800
+ function m() {
801
+ if (typeof c == "string") {
802
802
  if (!d.presenter)
803
803
  throw new Error("Presenter is not initialized");
804
804
  return {
805
805
  type: "text",
806
806
  provider: d.presenter.voice,
807
- input: o,
807
+ input: c,
808
808
  ssml: !1
809
809
  };
810
810
  }
811
- return o;
811
+ return c;
812
812
  }
813
- const f = u();
814
- return l.track("agent-speak", f), t.streamingManager.speak({
815
- script: f
813
+ const y = m();
814
+ return l.track("agent-speak", y), t.streamingManager.speak({
815
+ script: y
816
816
  });
817
817
  }
818
818
  };
819
819
  }
820
- function $e(e, r, c) {
820
+ function Pe(e, r, s) {
821
821
  const {
822
- getById: t
823
- } = j(r, c || O);
824
- return t(e);
822
+ getById: a
823
+ } = X(r, s || z);
824
+ return a(e);
825
825
  }
826
826
  export {
827
- M as ChatMode,
828
- N as ChatProgress,
829
- k as ConnectionState,
830
- re as DocumentType,
831
- ne as KnowledgeType,
832
- G as PlanGroup,
833
- ae as Providers,
834
- ee as RateState,
835
- F as StreamEvents,
836
- x as StreamingState,
837
- te as Subject,
838
- Q as UserPlan,
827
+ k as ChatMode,
828
+ L as ChatProgress,
829
+ I as ConnectionState,
830
+ ae as DocumentType,
831
+ re as KnowledgeType,
832
+ ee as PlanGroup,
833
+ ie as Providers,
834
+ te as RateState,
835
+ U as StreamEvents,
836
+ b as StreamingState,
837
+ ne as Subject,
838
+ G as UserPlan,
839
839
  v as VideoType,
840
- ie as VoiceAccess,
841
- Ae as createAgentManager,
842
- $e as getAgent
840
+ se as VoiceAccess,
841
+ $e as createAgentManager,
842
+ Pe as getAgent
843
843
  };