@d-id/client-sdk 1.0.19-beta.103 → 1.0.19-beta.105

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