@d-id/client-sdk 1.0.19-beta.46 → 1.0.19-beta.48

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,526 +1,697 @@
1
- const S = "https://api.d-id.com", b = "wss://notifications.d-id.com";
2
- function W() {
3
- let e = window.localStorage.getItem("did_external_key_id");
4
- return e || (e = Math.random().toString(16).slice(2), window.localStorage.setItem("did_external_key_id", e)), e;
1
+ const didApiUrl = "https://api-dev.d-id.com";
2
+ const didSocketApiUrl = "wss://notifications-dev.d-id.com";
3
+ function getExternalId() {
4
+ let key = window.localStorage.getItem("did_external_key_id");
5
+ if (!key) {
6
+ key = Math.random().toString(16).slice(2);
7
+ window.localStorage.setItem("did_external_key_id", key);
8
+ }
9
+ return key;
5
10
  }
6
- function K(e) {
7
- if (e.type === "bearer")
8
- return `Bearer ${e.token}`;
9
- if (e.type === "basic")
10
- return `Basic ${btoa(`${e.username}:${e.password}`)}`;
11
- if (e.type === "key")
12
- return `Client-Key ${e.clientKey}.${W()}`;
13
- throw new Error(`Unknown auth type: ${e}`);
11
+ function getAuthHeader(auth) {
12
+ if (auth.type === "bearer") {
13
+ return `Bearer ${auth.token}`;
14
+ } else if (auth.type === "basic") {
15
+ return `Basic ${btoa(`${auth.username}:${auth.password}`)}`;
16
+ } else if (auth.type === "key") {
17
+ return `Client-Key ${auth.clientKey}.${getExternalId()}`;
18
+ } else {
19
+ throw new Error(`Unknown auth type: ${auth}`);
20
+ }
14
21
  }
15
- function _(e, a = S) {
16
- const n = async (t, r) => {
17
- const i = await fetch(a + (t != null && t.startsWith("/") ? t : `/${t}`), {
18
- ...r,
22
+ function createClient(auth, host = didApiUrl) {
23
+ const client = async (url, options) => {
24
+ const request = await fetch(host + ((url == null ? void 0 : url.startsWith("/")) ? url : `/${url}`), {
25
+ ...options,
19
26
  headers: {
20
- ...r == null ? void 0 : r.headers,
21
- Authorization: K(e),
27
+ ...options == null ? void 0 : options.headers,
28
+ Authorization: getAuthHeader(auth),
22
29
  "Content-Type": "application/json"
23
30
  }
24
31
  });
25
- if (!i.ok) {
26
- let o = await i.text().catch(() => "Failed to fetch");
27
- throw new Error(o);
32
+ if (!request.ok) {
33
+ let error = await request.text().catch(() => "Failed to fetch");
34
+ throw new Error(error);
28
35
  }
29
- return i.json();
36
+ return request.json();
30
37
  };
31
38
  return {
32
- get(t, r) {
33
- return n(t, {
34
- ...r,
39
+ get(url, options) {
40
+ return client(url, {
41
+ ...options,
35
42
  method: "GET"
36
43
  });
37
44
  },
38
- post(t, r, i) {
39
- return n(t, {
40
- ...i,
41
- body: JSON.stringify(r),
45
+ post(url, body, options) {
46
+ return client(url, {
47
+ ...options,
48
+ body: JSON.stringify(body),
42
49
  method: "POST"
43
50
  });
44
51
  },
45
- delete(t, r, i) {
46
- return n(t, {
47
- ...i,
48
- body: JSON.stringify(r),
52
+ delete(url, body, options) {
53
+ return client(url, {
54
+ ...options,
55
+ body: JSON.stringify(body),
49
56
  method: "DELETE"
50
57
  });
51
58
  },
52
- patch(t, r, i) {
53
- return n(t, {
54
- ...i,
55
- body: JSON.stringify(r),
59
+ patch(url, body, options) {
60
+ return client(url, {
61
+ ...options,
62
+ body: JSON.stringify(body),
56
63
  method: "PATCH"
57
64
  });
58
65
  }
59
66
  };
60
67
  }
61
- function E(e, a = S) {
62
- const n = _(e, `${a}/agents`);
68
+ function createAgentsApi(auth, host = didApiUrl) {
69
+ const client = createClient(auth, `${host}/agents`);
63
70
  return {
64
- create(t, r) {
65
- return n.post("/", t, r);
71
+ create(payload, options) {
72
+ return client.post(`/`, payload, options);
66
73
  },
67
- getAgents(t, r) {
68
- return n.get(`/${t ? `?tag=${t}` : ""}`, r).then((i) => i ?? []);
74
+ getAgents(tag, options) {
75
+ return client.get(`/${tag ? `?tag=${tag}` : ""}`, options).then((agents) => agents ?? []);
69
76
  },
70
- getById(t, r) {
71
- return n.get(`/${t}`, r);
77
+ getById(id, options) {
78
+ return client.get(`/${id}`, options);
72
79
  },
73
- delete(t, r) {
74
- return n.delete(`/${t}`, void 0, r);
80
+ delete(id, options) {
81
+ return client.delete(`/${id}`, void 0, options);
75
82
  },
76
- update(t, r, i) {
77
- return n.patch(`/${t}`, r, i);
83
+ update(id, payload, options) {
84
+ return client.patch(`/${id}`, payload, options);
78
85
  },
79
- newChat(t, r) {
80
- return n.post(`/${t}/chat`, void 0, r);
86
+ newChat(agentId, options) {
87
+ return client.post(`/${agentId}/chat`, void 0, options);
81
88
  },
82
- chat(t, r, i, o) {
83
- return n.post(`/${t}/chat/${r}`, i, o);
89
+ chat(agentId, chatId, payload, options) {
90
+ return client.post(`/${agentId}/chat/${chatId}`, payload, options);
84
91
  }
85
92
  };
86
93
  }
87
- function O(e, a = S) {
88
- const n = _(e, `${a}/knowledge`);
94
+ function createKnowledgeApi(auth, host = didApiUrl) {
95
+ const client = createClient(auth, `${host}/knowledge`);
89
96
  return {
90
- createKnowledge(t, r) {
91
- return n.post("/", t, r);
97
+ createKnowledge(payload, options) {
98
+ return client.post(`/`, payload, options);
92
99
  },
93
- getKnowledgeBase(t) {
94
- return n.get("/", t);
100
+ getKnowledgeBase(options) {
101
+ return client.get(`/`, options);
95
102
  },
96
- getKnowledge(t, r) {
97
- return n.get(`/${t}`, r);
103
+ getKnowledge(knowledgeId, options) {
104
+ return client.get(`/${knowledgeId}`, options);
98
105
  },
99
- deleteKnowledge(t, r) {
100
- return n.delete(`/${t}`, void 0, r);
106
+ deleteKnowledge(knowledgeId, options) {
107
+ return client.delete(`/${knowledgeId}`, void 0, options);
101
108
  },
102
- createDocument(t, r, i) {
103
- return n.post(`/${t}/documents`, r, i);
109
+ createDocument(knowledgeId, payload, options) {
110
+ return client.post(`/${knowledgeId}/documents`, payload, options);
104
111
  },
105
- deleteDocument(t, r, i) {
106
- return n.delete(`/${t}/documents/${r}`, void 0, i);
112
+ deleteDocument(knowledgeId, documentId, options) {
113
+ return client.delete(`/${knowledgeId}/documents/${documentId}`, void 0, options);
107
114
  },
108
- getDocuments(t, r) {
109
- return n.get(`/${t}/documents`, r);
115
+ getDocuments(knowledgeId, options) {
116
+ return client.get(`/${knowledgeId}/documents`, options);
110
117
  },
111
- getDocument(t, r, i) {
112
- return n.get(`/${t}/documents/${r}`, i);
118
+ getDocument(knowledgeId, documentId, options) {
119
+ return client.get(`/${knowledgeId}/documents/${documentId}`, options);
113
120
  },
114
- getRecords(t, r, i) {
115
- return n.get(`/${t}/documents/${r}/records`, i);
121
+ getRecords(knowledgeId, documentId, options) {
122
+ return client.get(`/${knowledgeId}/documents/${documentId}/records`, options);
116
123
  },
117
- query(t, r, i) {
118
- return n.post(`/${t}/query`, {
119
- query: r
120
- }, i);
124
+ query(knowledgeId, query, options) {
125
+ return client.post(`/${knowledgeId}/query`, {
126
+ query
127
+ }, options);
121
128
  }
122
129
  };
123
130
  }
124
- function B(e, a = S) {
125
- const n = _(e, `${a}/chats/ratings`);
131
+ function createRatingsApi(auth, host = didApiUrl) {
132
+ const client = createClient(auth, `${host}/chats/ratings`);
126
133
  return {
127
- create(t, r) {
128
- return n.post("/", t, r);
134
+ create(payload, options) {
135
+ return client.post(`/`, payload, options);
129
136
  },
130
- getByKnowledge(t, r) {
131
- return n.get(`/${t}`, r).then((i) => i ?? []);
137
+ getByKnowledge(knowledgeId, options) {
138
+ return client.get(`/${knowledgeId}`, options).then((ratings) => ratings ?? []);
132
139
  },
133
- update(t, r, i) {
134
- return n.patch(`/${t}`, r, i);
140
+ update(id, payload, options) {
141
+ return client.patch(`/${id}`, payload, options);
135
142
  },
136
- delete(t, r) {
137
- return n.delete(`/${t}`, r);
143
+ delete(id, options) {
144
+ return client.delete(`/${id}`, options);
138
145
  }
139
146
  };
140
147
  }
141
- const H = (e) => new Promise((a) => setTimeout(a, e));
142
- function U(e) {
143
- return new Promise((a, n) => {
148
+ const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
149
+ function connect(options) {
150
+ return new Promise((resolve, reject) => {
144
151
  const {
145
- callbacks: t,
146
- host: r,
147
- auth: i
148
- } = e, {
149
- onMessage: o = null,
150
- onOpen: u = null,
151
- onClose: c = null,
152
- onError: m = null
153
- } = t || {}, d = new WebSocket(`${r}?authorization=${K(i)}`);
154
- d.onmessage = o, d.onclose = c, d.onerror = (g) => {
155
- console.error(g), m == null || m(g), n(g);
156
- }, d.onopen = (g) => {
157
- u == null || u(g), a(d);
152
+ callbacks,
153
+ host,
154
+ auth
155
+ } = options;
156
+ const {
157
+ onMessage = null,
158
+ onOpen = null,
159
+ onClose = null,
160
+ onError = null
161
+ } = callbacks || {};
162
+ const socket = new WebSocket(`${host}?authorization=${getAuthHeader(auth)}`);
163
+ socket.onmessage = onMessage;
164
+ socket.onclose = onClose;
165
+ socket.onerror = (e) => {
166
+ console.error(e);
167
+ onError == null ? void 0 : onError(e);
168
+ reject(e);
169
+ };
170
+ socket.onopen = (e) => {
171
+ onOpen == null ? void 0 : onOpen(e);
172
+ resolve(socket);
158
173
  };
159
174
  });
160
175
  }
161
- async function F(e) {
176
+ async function connectWithRetries(options) {
162
177
  const {
163
- retries: a = 1
164
- } = e;
165
- let n = null;
166
- for (let t = 0; (n == null ? void 0 : n.readyState) !== WebSocket.OPEN; t++)
178
+ retries = 1
179
+ } = options;
180
+ let socket = null;
181
+ for (let attempt = 0; (socket == null ? void 0 : socket.readyState) !== WebSocket.OPEN; attempt++) {
167
182
  try {
168
- n = await U(e);
169
- } catch (r) {
170
- if (t === a)
171
- throw r;
172
- await H(t * 500);
183
+ socket = await connect(options);
184
+ } catch (e) {
185
+ if (attempt === retries) {
186
+ throw e;
187
+ }
188
+ await sleep(attempt * 500);
173
189
  }
174
- return n;
190
+ }
191
+ return socket;
175
192
  }
176
- async function T(e, a, n) {
177
- const t = n ? [n] : [], r = await F({
178
- auth: e,
179
- host: a,
193
+ async function SocketManager(auth, host, onMessage) {
194
+ const messageCallbacks = onMessage ? [onMessage] : [];
195
+ const socket = await connectWithRetries({
196
+ auth,
197
+ host,
180
198
  callbacks: {
181
- onMessage: (i) => {
182
- const o = JSON.parse(i.data);
183
- t.forEach((u) => u(o.event, o));
199
+ onMessage: (event) => {
200
+ const parsedData = JSON.parse(event.data);
201
+ messageCallbacks.forEach((callback) => callback(parsedData.event, parsedData));
184
202
  }
185
203
  }
186
204
  });
187
205
  return {
188
- socket: r,
189
- terminate: () => r.close(),
190
- subscribeToEvents: (i) => t.push(i)
206
+ socket,
207
+ terminate: () => socket.close(),
208
+ subscribeToEvents: (callback) => messageCallbacks.push(callback)
191
209
  };
192
210
  }
193
- var q = /* @__PURE__ */ ((e) => (e.Amazon = "amazon", e.Microsoft = "microsoft", e.Afflorithmics = "afflorithmics", e.Elevenlabs = "elevenlabs", e))(q || {}), N = /* @__PURE__ */ ((e) => (e.Public = "public", e.Premium = "premium", e.Private = "private", e))(N || {}), R = /* @__PURE__ */ ((e) => (e.Start = "START", e.Stop = "STOP", e))(R || {}), y = /* @__PURE__ */ ((e) => (e.ChatAnswer = "chat/answer", e.ChatPartial = "chat/partial", e.StreamDone = "stream/done", e.StreamStarted = "stream/started", e.StreamFailed = "stream/error", e))(y || {}), j = /* @__PURE__ */ ((e) => (e.Unrated = "Unrated", e.Positive = "Positive", e.Negative = "Negative", e))(j || {}), z = /* @__PURE__ */ ((e) => (e.Functional = "Functional", e.TextOnly = "TextOnly", e.Maintenance = "Maintenance", e))(z || {}), P = /* @__PURE__ */ ((e) => (e.Embed = "embed", e.Query = "query", e.Partial = "partial", e.Answer = "answer", e.Complete = "done", e))(P || {}), J = /* @__PURE__ */ ((e) => (e.KnowledgeProcessing = "knowledge/processing", e.KnowledgeIndexing = "knowledge/indexing", e.KnowledgeFailed = "knowledge/error", e.KnowledgeDone = "knowledge/done", e))(J || {}), D = /* @__PURE__ */ ((e) => (e.Knowledge = "knowledge", e.Document = "document", e.Record = "record", e))(D || {}), G = /* @__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))(G || {}), $ = /* @__PURE__ */ ((e) => (e.Clip = "clip", e.Talk = "talk", e))($ || {});
194
- function Q(e) {
195
- return e.presenter.type === $.Clip ? {
196
- videoType: $.Clip,
197
- driver_id: e.presenter.driver_id,
198
- presenter_id: e.presenter.presenter_id
199
- } : {
200
- videoType: $.Talk,
201
- source_url: e.presenter.source_url
211
+ var Providers = /* @__PURE__ */ ((Providers2) => {
212
+ Providers2["Amazon"] = "amazon";
213
+ Providers2["Microsoft"] = "microsoft";
214
+ Providers2["Afflorithmics"] = "afflorithmics";
215
+ Providers2["Elevenlabs"] = "elevenlabs";
216
+ return Providers2;
217
+ })(Providers || {});
218
+ var VoiceAccess = /* @__PURE__ */ ((VoiceAccess2) => {
219
+ VoiceAccess2["Public"] = "public";
220
+ VoiceAccess2["Premium"] = "premium";
221
+ VoiceAccess2["Private"] = "private";
222
+ return VoiceAccess2;
223
+ })(VoiceAccess || {});
224
+ var StreamingState = /* @__PURE__ */ ((StreamingState2) => {
225
+ StreamingState2["Start"] = "START";
226
+ StreamingState2["Stop"] = "STOP";
227
+ return StreamingState2;
228
+ })(StreamingState || {});
229
+ var StreamEvents = /* @__PURE__ */ ((StreamEvents2) => {
230
+ StreamEvents2["ChatAnswer"] = "chat/answer";
231
+ StreamEvents2["ChatPartial"] = "chat/partial";
232
+ StreamEvents2["StreamDone"] = "stream/done";
233
+ StreamEvents2["StreamStarted"] = "stream/started";
234
+ StreamEvents2["StreamFailed"] = "stream/error";
235
+ return StreamEvents2;
236
+ })(StreamEvents || {});
237
+ var RateState = /* @__PURE__ */ ((RateState2) => {
238
+ RateState2["Unrated"] = "Unrated";
239
+ RateState2["Positive"] = "Positive";
240
+ RateState2["Negative"] = "Negative";
241
+ return RateState2;
242
+ })(RateState || {});
243
+ var ChatMode = /* @__PURE__ */ ((ChatMode2) => {
244
+ ChatMode2["Functional"] = "Functional";
245
+ ChatMode2["TextOnly"] = "TextOnly";
246
+ ChatMode2["Maintenance"] = "Maintenance";
247
+ return ChatMode2;
248
+ })(ChatMode || {});
249
+ var ChatProgress = /* @__PURE__ */ ((ChatProgress2) => {
250
+ ChatProgress2["Embed"] = "embed";
251
+ ChatProgress2["Query"] = "query";
252
+ ChatProgress2["Partial"] = "partial";
253
+ ChatProgress2["Answer"] = "answer";
254
+ ChatProgress2["Complete"] = "done";
255
+ return ChatProgress2;
256
+ })(ChatProgress || {});
257
+ var Subject = /* @__PURE__ */ ((Subject2) => {
258
+ Subject2["KnowledgeProcessing"] = "knowledge/processing";
259
+ Subject2["KnowledgeIndexing"] = "knowledge/indexing";
260
+ Subject2["KnowledgeFailed"] = "knowledge/error";
261
+ Subject2["KnowledgeDone"] = "knowledge/done";
262
+ return Subject2;
263
+ })(Subject || {});
264
+ var KnowledgeType = /* @__PURE__ */ ((KnowledgeType2) => {
265
+ KnowledgeType2["Knowledge"] = "knowledge";
266
+ KnowledgeType2["Document"] = "document";
267
+ KnowledgeType2["Record"] = "record";
268
+ return KnowledgeType2;
269
+ })(KnowledgeType || {});
270
+ var DocumentType = /* @__PURE__ */ ((DocumentType2) => {
271
+ DocumentType2["Pdf"] = "pdf";
272
+ DocumentType2["Text"] = "text";
273
+ DocumentType2["Html"] = "html";
274
+ DocumentType2["Word"] = "word";
275
+ DocumentType2["Json"] = "json";
276
+ DocumentType2["Markdown"] = "markdown";
277
+ DocumentType2["Csv"] = "csv";
278
+ DocumentType2["Excel"] = "excel";
279
+ DocumentType2["Powerpoint"] = "powerpoint";
280
+ DocumentType2["Archive"] = "archive";
281
+ DocumentType2["Image"] = "image";
282
+ DocumentType2["Audio"] = "audio";
283
+ DocumentType2["Video"] = "video";
284
+ return DocumentType2;
285
+ })(DocumentType || {});
286
+ var VideoType = /* @__PURE__ */ ((VideoType2) => {
287
+ VideoType2["Clip"] = "clip";
288
+ VideoType2["Talk"] = "talk";
289
+ return VideoType2;
290
+ })(VideoType || {});
291
+ function getAgentStreamArgs(agent) {
292
+ if (agent.presenter.type === VideoType.Clip) {
293
+ return {
294
+ videoType: VideoType.Clip,
295
+ driver_id: agent.presenter.driver_id,
296
+ presenter_id: agent.presenter.presenter_id
297
+ };
298
+ }
299
+ return {
300
+ videoType: VideoType.Talk,
301
+ source_url: agent.presenter.source_url
202
302
  };
203
303
  }
204
- function L(e, a, n, t) {
205
- return new Promise(async (r, i) => {
206
- const o = await te(Q(e), {
207
- ...a,
304
+ function initializeStreamAndChat(agent, options, agentsApi, chat) {
305
+ return new Promise(async (resolve, reject) => {
306
+ const streamingManager = await createStreamingManager(getAgentStreamArgs(agent), {
307
+ ...options,
208
308
  callbacks: {
209
- ...a.callbacks,
210
- onConnectionStateChange: async (u) => {
211
- var c, m;
212
- if (u === "connected")
309
+ ...options.callbacks,
310
+ onConnectionStateChange: async (state) => {
311
+ var _a, _b;
312
+ if (state === "connected") {
213
313
  try {
214
- t || (t = await n.newChat(e.id)), r({
215
- chat: t,
216
- streamingManager: o
314
+ if (!chat) {
315
+ chat = await agentsApi.newChat(agent.id);
316
+ }
317
+ resolve({
318
+ chat,
319
+ streamingManager
217
320
  });
218
- } catch (d) {
219
- console.error(d), i(new Error("Cannot create new chat"));
321
+ } catch (error) {
322
+ console.error(error);
323
+ reject(new Error("Cannot create new chat"));
220
324
  }
221
- else
222
- u === "failed" && i(new Error("Cannot create connection"));
223
- (m = (c = a.callbacks).onConnectionStateChange) == null || m.call(c, u);
224
- },
225
- // TODO remove when webscoket will return partial
226
- onMessage: (u, c) => {
227
- var m, d;
228
- u === y.ChatAnswer && (console.log("ChatAnswer", u, c), (d = (m = a.callbacks).onChatEvents) == null || d.call(m, P.Answer, {
229
- content: c,
230
- event: P.Answer
231
- }));
325
+ } else if (state === "failed") {
326
+ reject(new Error("Cannot create connection"));
327
+ }
328
+ (_b = (_a = options.callbacks).onConnectionStateChange) == null ? void 0 : _b.call(_a, state);
232
329
  }
233
330
  }
234
331
  });
235
332
  });
236
333
  }
237
- function ne(e, a, n) {
238
- return E(a, n || S).getById(e);
334
+ function getAgent(agentId, auth, baseURL) {
335
+ const url = baseURL || didApiUrl;
336
+ const agentsApi = createAgentsApi(auth, url);
337
+ return agentsApi.getById(agentId);
239
338
  }
240
- async function re(e, a) {
241
- var w, p;
242
- const n = a.baseURL || S, t = a.wsURL || b, r = new AbortController(), i = E(a.auth, n), o = B(a.auth, n), u = O(a.auth, n), c = typeof e == "string" ? await i.getById(e) : e;
243
- (p = (w = a.callbacks) == null ? void 0 : w.onAgentReady) == null || p.call(w, c);
244
- const m = await T(a.auth, t, a.callbacks.onChatEvents);
339
+ async function createAgentManager(agent, options) {
340
+ var _a, _b;
341
+ const baseURL = options.baseURL || didApiUrl;
342
+ const wsURL = options.wsURL || didSocketApiUrl;
343
+ const abortController = new AbortController();
344
+ const agentsApi = createAgentsApi(options.auth, baseURL);
345
+ const ratingsAPI = createRatingsApi(options.auth, baseURL);
346
+ const knowledgeApi = createKnowledgeApi(options.auth, baseURL);
347
+ const agentInstance = typeof agent === "string" ? await agentsApi.getById(agent) : agent;
348
+ (_b = (_a = options.callbacks) == null ? void 0 : _a.onAgentReady) == null ? void 0 : _b.call(_a, agentInstance);
349
+ const socketManager = await SocketManager(options.auth, wsURL, options.callbacks.onChatEvents);
245
350
  let {
246
- chat: d,
247
- streamingManager: g
248
- } = await L(c, a, i);
351
+ chat,
352
+ streamingManager
353
+ } = await initializeStreamAndChat(agentInstance, options, agentsApi);
249
354
  return {
250
- agent: c,
251
- chatId: d.id,
355
+ agent: agentInstance,
356
+ chatId: chat.id,
252
357
  async reconnectToChat() {
253
358
  const {
254
- streamingManager: s
255
- } = await L(c, a, i, d);
256
- g = s;
359
+ streamingManager: newStreamingManager
360
+ } = await initializeStreamAndChat(agentInstance, options, agentsApi, chat);
361
+ streamingManager = newStreamingManager;
257
362
  },
258
363
  terminate() {
259
- return r.abort(), m.terminate(), g.terminate();
260
- },
261
- chat(s) {
262
- return i.chat(c.id, d.id, {
263
- sessionId: g.sessionId,
264
- streamId: g.streamId,
265
- messages: s
364
+ abortController.abort();
365
+ socketManager.terminate();
366
+ return streamingManager.terminate();
367
+ },
368
+ chat(messages) {
369
+ return agentsApi.chat(agentInstance.id, chat.id, {
370
+ sessionId: streamingManager.sessionId,
371
+ streamId: streamingManager.streamId,
372
+ messages
266
373
  }, {
267
- signal: r.signal
374
+ signal: abortController.signal
268
375
  });
269
376
  },
270
- rate(s, f) {
271
- return f ? o.update(f, s) : o.create(s);
377
+ rate(payload, id) {
378
+ if (id) {
379
+ return ratingsAPI.update(id, payload);
380
+ }
381
+ return ratingsAPI.create(payload);
272
382
  },
273
- deleteRate(s) {
274
- return o.delete(s);
383
+ deleteRate(id) {
384
+ return ratingsAPI.delete(id);
275
385
  },
276
- speak(s) {
277
- function f() {
278
- if (s.type === "text")
386
+ speak(payload) {
387
+ function getScript() {
388
+ if (payload.type === "text") {
279
389
  return {
280
390
  type: "text",
281
- provider: s.provider,
282
- input: s.input,
283
- ssml: s.ssml || !1
391
+ provider: payload.provider,
392
+ input: payload.input,
393
+ ssml: payload.ssml || false
284
394
  };
285
- if (s.type === "audio")
395
+ } else if (payload.type === "audio") {
286
396
  return {
287
397
  type: "audio",
288
- audio_url: s.audio_url
398
+ audio_url: payload.audio_url
289
399
  };
400
+ }
290
401
  throw new Error("Invalid payload");
291
402
  }
292
- return g.speak({
293
- script: f()
403
+ return streamingManager.speak({
404
+ script: getScript()
294
405
  });
295
406
  },
296
407
  async getStarterMessages() {
297
- var s;
298
- return (s = c.knowledge) != null && s.id ? u.getKnowledge(c.knowledge.id).then((f) => (f == null ? void 0 : f.starter_message) || []) : [];
408
+ var _a2;
409
+ if (!((_a2 = agentInstance.knowledge) == null ? void 0 : _a2.id)) {
410
+ return [];
411
+ }
412
+ return knowledgeApi.getKnowledge(agentInstance.knowledge.id).then((knowledge) => (knowledge == null ? void 0 : knowledge.starter_message) || []);
299
413
  }
300
414
  };
301
415
  }
302
- function X(e, a) {
303
- const n = _(e, a);
416
+ function createApi$1(auth, host) {
417
+ const client = createClient(auth, host);
304
418
  return {
305
- createStream(t) {
306
- return n.post("/clips/streams", {
307
- driver_id: t.driver_id,
308
- presenter_id: t.presenter_id,
309
- compatibility_mode: t.compatibility_mode
419
+ createStream(options) {
420
+ return client.post("/clips/streams", {
421
+ driver_id: options.driver_id,
422
+ presenter_id: options.presenter_id,
423
+ compatibility_mode: options.compatibility_mode
310
424
  });
311
425
  },
312
- startConnection(t, r, i) {
313
- return n.post(`/clips/streams/${t}/sdp`, {
314
- session_id: i,
315
- answer: r
426
+ startConnection(streamId, answer, sessionId) {
427
+ return client.post(`/clips/streams/${streamId}/sdp`, {
428
+ session_id: sessionId,
429
+ answer
316
430
  });
317
431
  },
318
- addIceCandidate(t, r, i) {
319
- return n.post(`/clips/streams/${t}/ice`, {
320
- session_id: i,
321
- ...r
432
+ addIceCandidate(streamId, candidate, sessionId) {
433
+ return client.post(`/clips/streams/${streamId}/ice`, {
434
+ session_id: sessionId,
435
+ ...candidate
322
436
  });
323
437
  },
324
- sendStreamRequest(t, r, i) {
325
- return n.post(`/clips/streams/${t}`, {
326
- session_id: r,
327
- ...i
438
+ sendStreamRequest(streamId, sessionId, payload) {
439
+ return client.post(`/clips/streams/${streamId}`, {
440
+ session_id: sessionId,
441
+ ...payload
328
442
  });
329
443
  },
330
- close(t, r) {
331
- return n.delete(`/clips/streams/${t}`, {
332
- session_id: r
444
+ close(streamId, sessionId) {
445
+ return client.delete(`/clips/streams/${streamId}`, {
446
+ session_id: sessionId
333
447
  });
334
448
  }
335
449
  };
336
450
  }
337
- function Y(e, a) {
338
- const n = _(e, a);
451
+ function createApi(auth, host) {
452
+ const client = createClient(auth, host);
339
453
  return {
340
- createStream(t, r) {
341
- return n.post("/talks/streams", {
342
- source_url: t.source_url,
343
- driver_url: t.driver_url,
344
- face: t.face,
345
- config: t.config
346
- }, r);
347
- },
348
- startConnection(t, r, i, o) {
349
- return n.post(`/talks/streams/${t}/sdp`, {
350
- session_id: i,
351
- answer: r
352
- }, o);
353
- },
354
- addIceCandidate(t, r, i, o) {
355
- return n.post(`/talks/streams/${t}/ice`, {
356
- session_id: i,
357
- ...r
358
- }, o);
359
- },
360
- sendStreamRequest(t, r, i, o) {
361
- return n.post(`/talks/streams/${t}`, {
362
- session_id: r,
363
- ...i
364
- }, o);
365
- },
366
- close(t, r, i) {
367
- return n.delete(`/talks/streams/${t}`, {
368
- session_id: r
369
- }, i);
454
+ createStream(streamOptions, options) {
455
+ return client.post("/talks/streams", {
456
+ source_url: streamOptions.source_url,
457
+ driver_url: streamOptions.driver_url,
458
+ face: streamOptions.face,
459
+ config: streamOptions.config
460
+ }, options);
461
+ },
462
+ startConnection(streamId, answer, sessionId, options) {
463
+ return client.post(`/talks/streams/${streamId}/sdp`, {
464
+ session_id: sessionId,
465
+ answer
466
+ }, options);
467
+ },
468
+ addIceCandidate(streamId, candidate, sessionId, options) {
469
+ return client.post(`/talks/streams/${streamId}/ice`, {
470
+ session_id: sessionId,
471
+ ...candidate
472
+ }, options);
473
+ },
474
+ sendStreamRequest(streamId, sessionId, payload, options) {
475
+ return client.post(`/talks/streams/${streamId}`, {
476
+ session_id: sessionId,
477
+ ...payload
478
+ }, options);
479
+ },
480
+ close(streamId, sessionId, options) {
481
+ return client.delete(`/talks/streams/${streamId}`, {
482
+ session_id: sessionId
483
+ }, options);
370
484
  }
371
485
  };
372
486
  }
373
- function Z(e, a) {
374
- return e.map((n, t) => t === 0 ? a ? {
375
- index: t,
376
- timestamp: n.timestamp,
377
- bytesReceived: n.bytesReceived - a.bytesReceived,
378
- packetsReceived: n.packetsReceived - a.packetsReceived,
379
- packetsLost: n.packetsLost - a.packetsLost,
380
- jitter: n.jitter,
381
- frameWidth: n.frameWidth,
382
- frameHeight: n.frameHeight,
383
- framesPerSecond: n.framesPerSecond
384
- } : {
385
- index: t,
386
- timestamp: n.timestamp,
387
- bytesReceived: n.bytesReceived,
388
- packetsReceived: n.packetsReceived,
389
- packetsLost: n.packetsLost,
390
- jitter: n.jitter,
391
- frameWidth: n.frameWidth,
392
- frameHeight: n.frameHeight,
393
- framesPerSecond: n.framesPerSecond
394
- } : {
395
- index: t,
396
- timestamp: n.timestamp,
397
- bytesReceived: n.bytesReceived - e[t - 1].bytesReceived,
398
- packetsReceived: n.packetsReceived - e[t - 1].packetsReceived,
399
- packetsLost: n.packetsLost - e[t - 1].packetsLost,
400
- jitter: n.jitter,
401
- frameWidth: n.frameWidth,
402
- frameHeight: n.frameHeight,
403
- framesPerSecond: n.framesPerSecond
487
+ function createVideoStatsReport(stats, previousStats) {
488
+ return stats.map((report, index) => {
489
+ if (index === 0) {
490
+ if (!previousStats) {
491
+ return {
492
+ index,
493
+ timestamp: report.timestamp,
494
+ bytesReceived: report.bytesReceived,
495
+ packetsReceived: report.packetsReceived,
496
+ packetsLost: report.packetsLost,
497
+ jitter: report.jitter,
498
+ frameWidth: report.frameWidth,
499
+ frameHeight: report.frameHeight,
500
+ framesPerSecond: report.framesPerSecond
501
+ };
502
+ }
503
+ return {
504
+ index,
505
+ timestamp: report.timestamp,
506
+ bytesReceived: report.bytesReceived - previousStats.bytesReceived,
507
+ packetsReceived: report.packetsReceived - previousStats.packetsReceived,
508
+ packetsLost: report.packetsLost - previousStats.packetsLost,
509
+ jitter: report.jitter,
510
+ frameWidth: report.frameWidth,
511
+ frameHeight: report.frameHeight,
512
+ framesPerSecond: report.framesPerSecond
513
+ };
514
+ }
515
+ return {
516
+ index,
517
+ timestamp: report.timestamp,
518
+ bytesReceived: report.bytesReceived - stats[index - 1].bytesReceived,
519
+ packetsReceived: report.packetsReceived - stats[index - 1].packetsReceived,
520
+ packetsLost: report.packetsLost - stats[index - 1].packetsLost,
521
+ jitter: report.jitter,
522
+ frameWidth: report.frameWidth,
523
+ frameHeight: report.frameHeight,
524
+ framesPerSecond: report.framesPerSecond
525
+ };
404
526
  });
405
527
  }
406
- let x = !1;
407
- const v = (e, a) => x && console.log(e, a), V = (window.RTCPeerConnection || window.webkitRTCPeerConnection || window.mozRTCPeerConnection).bind(window);
408
- function ee(e, a) {
409
- let n = [], t = 0, r = 0, i;
528
+ let _debug = false;
529
+ const log = (message, extra) => _debug && console.log(message, extra);
530
+ const actualRTCPC = (window.RTCPeerConnection || window.webkitRTCPeerConnection || window.mozRTCPeerConnection).bind(window);
531
+ function pollStats(peerConnection, onVideoStateChange) {
532
+ let videoStats = [];
533
+ let videoStatsStartIndex = 0;
534
+ let videoStatsLastIndex = 0;
535
+ let isPlaying;
410
536
  return setInterval(() => {
411
- e.getStats().then((u) => {
412
- u.forEach((c) => {
413
- if (c.type === "inbound-rtp" && c.kind === "video") {
414
- if (r = n.length - 1, c && n[r]) {
415
- const m = c.bytesReceived, d = n[r].bytesReceived;
416
- let g = i;
417
- i = m - d > 0;
418
- let w;
419
- if (g !== i) {
420
- if (i)
421
- t = n.length;
422
- else {
423
- const p = n.slice(t), s = t === 0 ? void 0 : n[t - 1];
424
- w = Z(p, s), w = w.sort((f, A) => A.packetsLost - f.packetsLost).slice(0, 5);
537
+ const stats = peerConnection.getStats();
538
+ stats.then((result) => {
539
+ result.forEach((report) => {
540
+ if (report.type === "inbound-rtp" && report.kind === "video") {
541
+ videoStatsLastIndex = videoStats.length - 1;
542
+ if (report && videoStats[videoStatsLastIndex]) {
543
+ const currBytesReceived = report.bytesReceived;
544
+ const lastBytesReceived = videoStats[videoStatsLastIndex].bytesReceived;
545
+ let prevPlaying = isPlaying;
546
+ isPlaying = currBytesReceived - lastBytesReceived > 0;
547
+ let videoStatsReport;
548
+ if (prevPlaying !== isPlaying) {
549
+ if (isPlaying) {
550
+ videoStatsStartIndex = videoStats.length;
551
+ } else {
552
+ const stats2 = videoStats.slice(videoStatsStartIndex);
553
+ const previousStats = videoStatsStartIndex === 0 ? void 0 : videoStats[videoStatsStartIndex - 1];
554
+ videoStatsReport = createVideoStatsReport(stats2, previousStats);
555
+ videoStatsReport = videoStatsReport.sort((a, b) => b.packetsLost - a.packetsLost).slice(0, 5);
425
556
  }
426
- a == null || a(i ? R.Start : R.Stop, w);
557
+ onVideoStateChange == null ? void 0 : onVideoStateChange(isPlaying ? StreamingState.Start : StreamingState.Stop, videoStatsReport);
427
558
  }
428
559
  }
429
- n.push(c);
560
+ videoStats.push(report);
430
561
  }
431
562
  });
432
563
  });
433
564
  }, 500);
434
565
  }
435
- async function te(e, {
436
- debug: a = !1,
437
- callbacks: n,
438
- auth: t,
439
- baseURL: r = S
566
+ async function createStreamingManager(agent, {
567
+ debug = false,
568
+ callbacks,
569
+ auth,
570
+ baseURL = didApiUrl
440
571
  }) {
441
- x = a;
572
+ _debug = debug;
573
+ const {
574
+ startConnection,
575
+ sendStreamRequest,
576
+ close,
577
+ createStream,
578
+ addIceCandidate
579
+ } = agent.videoType === VideoType.Clip ? createApi$1(auth, baseURL) : createApi(auth, baseURL);
442
580
  const {
443
- startConnection: i,
444
- sendStreamRequest: o,
445
- close: u,
446
- createStream: c,
447
- addIceCandidate: m
448
- } = e.videoType === $.Clip ? X(t, r) : Y(t, r), {
449
- id: d,
450
- offer: g,
451
- ice_servers: w,
452
- session_id: p
453
- } = await c(e), s = new V({
454
- iceServers: w
455
- }), f = s.createDataChannel("JanusDataChannel");
456
- if (!p)
581
+ id: streamIdFromServer,
582
+ offer,
583
+ ice_servers,
584
+ session_id
585
+ } = await createStream(agent);
586
+ const peerConnection = new actualRTCPC({
587
+ iceServers: ice_servers
588
+ });
589
+ const pcDataChannel = peerConnection.createDataChannel("JanusDataChannel");
590
+ if (!session_id) {
457
591
  throw new Error("Could not create session_id");
458
- const A = ee(s, n.onVideoStateChange);
459
- s.onicecandidate = (l) => {
460
- v("peerConnection.onicecandidate", l), l.candidate && l.candidate.sdpMid && l.candidate.sdpMLineIndex !== null && m(d, {
461
- candidate: l.candidate.candidate,
462
- sdpMid: l.candidate.sdpMid,
463
- sdpMLineIndex: l.candidate.sdpMLineIndex
464
- }, p);
465
- }, s.oniceconnectionstatechange = () => {
466
- var l;
467
- v("peerConnection.oniceconnectionstatechange => " + s.iceConnectionState), (l = n.onConnectionStateChange) == null || l.call(n, s.iceConnectionState);
468
- }, s.ontrack = (l) => {
469
- var h;
470
- v("peerConnection.ontrack", l), (h = n.onSrcObjectReady) == null || h.call(n, l.streams[0]);
471
- }, f.onmessage = (l) => {
472
- var h, k;
473
- if (f.readyState === "open") {
474
- const [C, I] = l.data.split(":");
475
- C === y.StreamStarted ? console.log("StreamStarted", C, I) : C === y.StreamDone ? console.log("StreamDone") : C === y.StreamFailed ? ((h = n.onVideoStateChange) == null || h.call(n, R.Stop, {
476
- event: C,
477
- data: I
478
- }), clearInterval(A), console.log("StreamFailed")) : (k = n.onMessage) == null || k.call(n, C, decodeURIComponent(I));
592
+ }
593
+ const videoStatsInterval = pollStats(peerConnection, callbacks.onVideoStateChange);
594
+ peerConnection.onicecandidate = (event) => {
595
+ log("peerConnection.onicecandidate", event);
596
+ if (event.candidate && event.candidate.sdpMid && event.candidate.sdpMLineIndex !== null) {
597
+ addIceCandidate(streamIdFromServer, {
598
+ candidate: event.candidate.candidate,
599
+ sdpMid: event.candidate.sdpMid,
600
+ sdpMLineIndex: event.candidate.sdpMLineIndex
601
+ }, session_id);
602
+ }
603
+ };
604
+ peerConnection.oniceconnectionstatechange = () => {
605
+ var _a;
606
+ log("peerConnection.oniceconnectionstatechange => " + peerConnection.iceConnectionState);
607
+ (_a = callbacks.onConnectionStateChange) == null ? void 0 : _a.call(callbacks, peerConnection.iceConnectionState);
608
+ };
609
+ peerConnection.ontrack = (event) => {
610
+ var _a;
611
+ log("peerConnection.ontrack", event);
612
+ (_a = callbacks.onSrcObjectReady) == null ? void 0 : _a.call(callbacks, event.streams[0]);
613
+ };
614
+ pcDataChannel.onmessage = (message) => {
615
+ var _a, _b;
616
+ if (pcDataChannel.readyState === "open") {
617
+ const [event, data] = message.data.split(":");
618
+ if (event === StreamEvents.StreamStarted) {
619
+ console.log("StreamStarted", event, data);
620
+ } else if (event === StreamEvents.StreamDone) {
621
+ console.log("StreamDone");
622
+ } else if (event === StreamEvents.StreamFailed) {
623
+ (_a = callbacks.onVideoStateChange) == null ? void 0 : _a.call(callbacks, StreamingState.Stop, {
624
+ event,
625
+ data
626
+ });
627
+ clearInterval(videoStatsInterval);
628
+ console.log("StreamFailed");
629
+ } else {
630
+ (_b = callbacks.onMessage) == null ? void 0 : _b.call(callbacks, event, decodeURIComponent(data));
631
+ }
479
632
  }
480
- }, await s.setRemoteDescription(g), v("set remote description OK");
481
- const M = await s.createAnswer();
482
- return v("create answer OK"), await s.setLocalDescription(M), v("set local description OK"), await i(d, M, p), v("start connection OK"), {
633
+ };
634
+ await peerConnection.setRemoteDescription(offer);
635
+ log("set remote description OK");
636
+ const sessionClientAnswer = await peerConnection.createAnswer();
637
+ log("create answer OK");
638
+ await peerConnection.setLocalDescription(sessionClientAnswer);
639
+ log("set local description OK");
640
+ await startConnection(streamIdFromServer, sessionClientAnswer, session_id);
641
+ log("start connection OK");
642
+ return {
483
643
  /**
484
644
  * Method to send request to server to get clip or talk depend on you payload
485
645
  * @param payload
486
646
  */
487
- speak(l) {
488
- return o(d, p, l);
647
+ speak(payload) {
648
+ return sendStreamRequest(streamIdFromServer, session_id, payload);
489
649
  },
490
650
  /**
491
651
  * Method to close RTC connection
492
652
  */
493
653
  async terminate() {
494
- var l, h;
495
- d && (s && (s.close(), s.oniceconnectionstatechange = null, s.onnegotiationneeded = null, s.onicecandidate = null, s.ontrack = null), await u(d, p).catch((k) => {
496
- }), (l = n.onConnectionStateChange) == null || l.call(n, "closed"), (h = n.onVideoStateChange) == null || h.call(n, R.Stop), clearInterval(A));
654
+ var _a, _b;
655
+ if (streamIdFromServer) {
656
+ if (peerConnection) {
657
+ peerConnection.close();
658
+ peerConnection.oniceconnectionstatechange = null;
659
+ peerConnection.onnegotiationneeded = null;
660
+ peerConnection.onicecandidate = null;
661
+ peerConnection.ontrack = null;
662
+ }
663
+ await close(streamIdFromServer, session_id).catch((_) => {
664
+ });
665
+ (_a = callbacks.onConnectionStateChange) == null ? void 0 : _a.call(callbacks, "closed");
666
+ (_b = callbacks.onVideoStateChange) == null ? void 0 : _b.call(callbacks, StreamingState.Stop);
667
+ clearInterval(videoStatsInterval);
668
+ }
497
669
  },
498
670
  /**
499
671
  * Session identifier information, should be returned in the body of all streaming requests
500
672
  */
501
- sessionId: p,
673
+ sessionId: session_id,
502
674
  /**
503
675
  * Id of current RTC stream
504
676
  */
505
- streamId: d
677
+ streamId: streamIdFromServer
506
678
  };
507
679
  }
508
680
  export {
509
- z as ChatMode,
510
- P as ChatProgress,
511
- G as DocumentType,
512
- D as KnowledgeType,
513
- q as Providers,
514
- j as RateState,
515
- T as SocketManager,
516
- y as StreamEvents,
517
- J as Subject,
518
- N as VoiceAccess,
519
- re as createAgentManager,
520
- E as createAgentsApi,
521
- _ as createClient,
522
- O as createKnowledgeApi,
523
- B as createRatingsApi,
524
- te as createStreamingManager,
525
- ne as getAgent
681
+ ChatMode,
682
+ ChatProgress,
683
+ DocumentType,
684
+ KnowledgeType,
685
+ Providers,
686
+ RateState,
687
+ SocketManager,
688
+ Subject,
689
+ VoiceAccess,
690
+ createAgentManager,
691
+ createAgentsApi,
692
+ createClient,
693
+ createKnowledgeApi,
694
+ createRatingsApi,
695
+ createStreamingManager,
696
+ getAgent
526
697
  };