@d-id/client-sdk 1.0.19-beta.126 → 1.0.19-beta.128

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