@d-id/client-sdk 1.0.19-beta.9 → 1.0.19-beta.91

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