@d-id/client-sdk 1.0.19-beta.11 → 1.0.19-beta.12
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 +159 -150
- package/dist/index.umd.cjs +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
const
|
|
2
|
-
function
|
|
1
|
+
const R = "https://api.d-id.com", J = "wss://notifications.d-id.com";
|
|
2
|
+
function q(e) {
|
|
3
3
|
if (e.type === "bearer")
|
|
4
4
|
return `Bearer ${e.token}`;
|
|
5
5
|
if (e.type === "basic")
|
|
@@ -8,19 +8,19 @@ function U(e) {
|
|
|
8
8
|
return `Client-Key ${e.clientKey}.${e.externalId}`;
|
|
9
9
|
throw new Error(`Unknown auth type: ${e}`);
|
|
10
10
|
}
|
|
11
|
-
function
|
|
11
|
+
function L(e, i = R) {
|
|
12
12
|
const n = async (t, r) => {
|
|
13
|
-
const a = await fetch(
|
|
13
|
+
const a = await fetch(i + (t != null && t.startsWith("/") ? t : `/${t}`), {
|
|
14
14
|
...r,
|
|
15
15
|
headers: {
|
|
16
16
|
...r == null ? void 0 : r.headers,
|
|
17
|
-
Authorization:
|
|
17
|
+
Authorization: q(e),
|
|
18
18
|
"Content-Type": "application/json"
|
|
19
19
|
}
|
|
20
20
|
});
|
|
21
21
|
if (!a.ok) {
|
|
22
|
-
let
|
|
23
|
-
throw new Error(
|
|
22
|
+
let s = await a.text().catch(() => "Failed to fetch");
|
|
23
|
+
throw new Error(s);
|
|
24
24
|
}
|
|
25
25
|
return a.json();
|
|
26
26
|
};
|
|
@@ -54,8 +54,8 @@ function I(e, s = S) {
|
|
|
54
54
|
}
|
|
55
55
|
};
|
|
56
56
|
}
|
|
57
|
-
function
|
|
58
|
-
const n =
|
|
57
|
+
function F(e, i = R) {
|
|
58
|
+
const n = L(e, `${i}/agents`);
|
|
59
59
|
return {
|
|
60
60
|
create(t, r) {
|
|
61
61
|
return n.post("/", t, r);
|
|
@@ -75,13 +75,13 @@ function B(e, s = S) {
|
|
|
75
75
|
newChat(t, r) {
|
|
76
76
|
return n.post(`/${t}/chat`, void 0, r);
|
|
77
77
|
},
|
|
78
|
-
chat(t, r, a,
|
|
79
|
-
return n.post(`/${t}/chat/${r}`, a,
|
|
78
|
+
chat(t, r, a, s) {
|
|
79
|
+
return n.post(`/${t}/chat/${r}`, a, s);
|
|
80
80
|
}
|
|
81
81
|
};
|
|
82
82
|
}
|
|
83
|
-
function D(e,
|
|
84
|
-
const n =
|
|
83
|
+
function D(e, i = R) {
|
|
84
|
+
const n = L(e, `${i}/knowledge`);
|
|
85
85
|
return {
|
|
86
86
|
createKnowledge(t, r) {
|
|
87
87
|
return n.post("/", t, r);
|
|
@@ -117,8 +117,8 @@ function D(e, s = S) {
|
|
|
117
117
|
}
|
|
118
118
|
};
|
|
119
119
|
}
|
|
120
|
-
function
|
|
121
|
-
const n =
|
|
120
|
+
function Y(e, i = R) {
|
|
121
|
+
const n = L(e, `${i}/chats/ratings`);
|
|
122
122
|
return {
|
|
123
123
|
create(t, r) {
|
|
124
124
|
return n.post("/", t, r);
|
|
@@ -134,49 +134,49 @@ function V(e, s = S) {
|
|
|
134
134
|
}
|
|
135
135
|
};
|
|
136
136
|
}
|
|
137
|
-
const
|
|
138
|
-
function
|
|
139
|
-
return new Promise((
|
|
137
|
+
const V = (e) => new Promise((i) => setTimeout(i, e));
|
|
138
|
+
function G(e) {
|
|
139
|
+
return new Promise((i, n) => {
|
|
140
140
|
const {
|
|
141
141
|
callbacks: t,
|
|
142
142
|
host: r,
|
|
143
143
|
auth: a
|
|
144
144
|
} = e, {
|
|
145
|
-
onMessage:
|
|
146
|
-
onOpen:
|
|
145
|
+
onMessage: s = null,
|
|
146
|
+
onOpen: u = null,
|
|
147
147
|
onClose: m = null,
|
|
148
|
-
onError:
|
|
149
|
-
} = t || {},
|
|
150
|
-
|
|
151
|
-
console.log(d),
|
|
152
|
-
},
|
|
153
|
-
|
|
148
|
+
onError: p = null
|
|
149
|
+
} = t || {}, f = new WebSocket(`${r}?authorization=${q(a)}`);
|
|
150
|
+
f.onmessage = s, f.onclose = m, f.onerror = (d) => {
|
|
151
|
+
console.log(d), p == null || p(d), n(d);
|
|
152
|
+
}, f.onopen = (d) => {
|
|
153
|
+
u == null || u(d), i(f);
|
|
154
154
|
};
|
|
155
155
|
});
|
|
156
156
|
}
|
|
157
|
-
async function
|
|
157
|
+
async function Q(e) {
|
|
158
158
|
const {
|
|
159
|
-
retries:
|
|
159
|
+
retries: i = 1
|
|
160
160
|
} = e;
|
|
161
161
|
let n = null;
|
|
162
162
|
for (let t = 0; (n == null ? void 0 : n.readyState) !== WebSocket.OPEN; t++)
|
|
163
163
|
try {
|
|
164
|
-
n = await
|
|
164
|
+
n = await G(e);
|
|
165
165
|
} catch (r) {
|
|
166
|
-
if (t ===
|
|
166
|
+
if (t === i)
|
|
167
167
|
throw r;
|
|
168
|
-
await
|
|
168
|
+
await V(t * 500);
|
|
169
169
|
}
|
|
170
170
|
return n;
|
|
171
171
|
}
|
|
172
|
-
async function
|
|
173
|
-
const t = n ? [n] : [], r = await
|
|
172
|
+
async function X(e, i, n) {
|
|
173
|
+
const t = n ? [n] : [], r = await Q({
|
|
174
174
|
auth: e,
|
|
175
|
-
host:
|
|
175
|
+
host: i,
|
|
176
176
|
callbacks: {
|
|
177
177
|
onMessage: (a) => {
|
|
178
|
-
const
|
|
179
|
-
t.forEach((
|
|
178
|
+
const s = JSON.parse(a.data);
|
|
179
|
+
t.forEach((u) => u(s.event, s));
|
|
180
180
|
}
|
|
181
181
|
}
|
|
182
182
|
});
|
|
@@ -186,65 +186,65 @@ async function Y(e, s, n) {
|
|
|
186
186
|
subscribeToEvents: (a) => t.push(a)
|
|
187
187
|
};
|
|
188
188
|
}
|
|
189
|
-
var Z = /* @__PURE__ */ ((e) => (e.Amazon = "amazon", e.Microsoft = "microsoft", e.Afflorithmics = "afflorithmics", e.Elevenlabs = "elevenlabs", e))(Z || {}), O = /* @__PURE__ */ ((e) => (e.Public = "public", e.Premium = "premium", e.Private = "private", e))(O || {}),
|
|
189
|
+
var Z = /* @__PURE__ */ ((e) => (e.Amazon = "amazon", e.Microsoft = "microsoft", e.Afflorithmics = "afflorithmics", e.Elevenlabs = "elevenlabs", e))(Z || {}), O = /* @__PURE__ */ ((e) => (e.Public = "public", e.Premium = "premium", e.Private = "private", e))(O || {}), _ = /* @__PURE__ */ ((e) => (e.Start = "START", e.Stop = "STOP", e))(_ || {}), M = /* @__PURE__ */ ((e) => (e.ChatAnswer = "chat/answer", e.ChatPartial = "chat/partial", e.StreamDone = "stream/done", e.StreamStarted = "stream/started", e.StreamFailed = "stream/error", e))(M || {}), j = /* @__PURE__ */ ((e) => (e.Unrated = "Unrated", e.Positive = "Positive", e.Negative = "Negative", e))(j || {}), ee = /* @__PURE__ */ ((e) => (e.Functional = "Functional", e.TextOnly = "TextOnly", e.Maintenance = "Maintenance", e))(ee || {}), W = /* @__PURE__ */ ((e) => (e.Embed = "embed", e.Query = "query", e.Partial = "partial", e.Answer = "answer", e.Complete = "done", e))(W || {}), 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 || {}), P = /* @__PURE__ */ ((e) => (e.Clip = "clip", e.Talk = "talk", e))(P || {});
|
|
190
190
|
function ae(e) {
|
|
191
|
-
return e.presenter.type ===
|
|
192
|
-
videoType:
|
|
191
|
+
return e.presenter.type === P.Clip ? {
|
|
192
|
+
videoType: P.Clip,
|
|
193
193
|
driver_id: e.presenter.driver_id,
|
|
194
194
|
presenter_id: e.presenter.presenter_id
|
|
195
195
|
} : {
|
|
196
|
-
videoType:
|
|
196
|
+
videoType: P.Talk,
|
|
197
197
|
source_url: e.presenter.source_url
|
|
198
198
|
};
|
|
199
199
|
}
|
|
200
|
-
function T(e,
|
|
200
|
+
function T(e, i, n, t) {
|
|
201
201
|
return new Promise(async (r, a) => {
|
|
202
|
-
const
|
|
203
|
-
...
|
|
202
|
+
const s = await de(ae(e), {
|
|
203
|
+
...i,
|
|
204
204
|
callbacks: {
|
|
205
|
-
...
|
|
206
|
-
onConnectionStateChange: async (
|
|
207
|
-
var m,
|
|
208
|
-
|
|
205
|
+
...i.callbacks,
|
|
206
|
+
onConnectionStateChange: async (u) => {
|
|
207
|
+
var m, p;
|
|
208
|
+
u === "connected" ? (t || (t = await n.newChat(e.id)), r({
|
|
209
209
|
chat: t,
|
|
210
|
-
streamingManager:
|
|
211
|
-
})) :
|
|
210
|
+
streamingManager: s
|
|
211
|
+
})) : u === "failed" && a(new Error("Cannot create connection")), (p = (m = i.callbacks).onConnectionStateChange) == null || p.call(m, u);
|
|
212
212
|
},
|
|
213
213
|
// TODO remove when webscoket will return partial
|
|
214
|
-
onMessage: (
|
|
215
|
-
var
|
|
216
|
-
|
|
214
|
+
onMessage: (u, m) => {
|
|
215
|
+
var p, f;
|
|
216
|
+
u === M.ChatAnswer && (console.log("ChatAnswer", u, m), (f = (p = i.callbacks).onChatEvents) == null || f.call(p, W.Answer, {
|
|
217
217
|
content: m,
|
|
218
|
-
event:
|
|
218
|
+
event: W.Answer
|
|
219
219
|
}));
|
|
220
220
|
}
|
|
221
221
|
}
|
|
222
222
|
});
|
|
223
223
|
});
|
|
224
224
|
}
|
|
225
|
-
function le(e,
|
|
226
|
-
return
|
|
225
|
+
function le(e, i, n) {
|
|
226
|
+
return F(i, n || R).getById(e);
|
|
227
227
|
}
|
|
228
|
-
async function ue(e,
|
|
229
|
-
const n =
|
|
228
|
+
async function ue(e, i) {
|
|
229
|
+
const n = i.baseURL || R, t = i.wsURL || J, r = new AbortController(), a = F(i.auth, n), s = Y(i.auth, n), u = D(i.auth, n), m = await a.getById(e), p = await X(i.auth, t, i.callbacks.onChatEvents);
|
|
230
230
|
let {
|
|
231
|
-
chat:
|
|
231
|
+
chat: f,
|
|
232
232
|
streamingManager: d
|
|
233
|
-
} = await T(m,
|
|
233
|
+
} = await T(m, i, a);
|
|
234
234
|
return {
|
|
235
235
|
agent: m,
|
|
236
236
|
async reconnectToChat() {
|
|
237
237
|
const {
|
|
238
238
|
streamingManager: c
|
|
239
|
-
} = await T(m,
|
|
239
|
+
} = await T(m, i, a, f);
|
|
240
240
|
d = c;
|
|
241
241
|
},
|
|
242
242
|
terminate() {
|
|
243
|
-
return r.abort(),
|
|
243
|
+
return r.abort(), p.terminate(), d.terminate();
|
|
244
244
|
},
|
|
245
|
-
chatId:
|
|
245
|
+
chatId: f.id,
|
|
246
246
|
chat(c) {
|
|
247
|
-
return a.chat(e,
|
|
247
|
+
return a.chat(e, f.id, {
|
|
248
248
|
sessionId: d.sessionId,
|
|
249
249
|
streamId: d.streamId,
|
|
250
250
|
messages: c
|
|
@@ -252,36 +252,36 @@ async function ue(e, s) {
|
|
|
252
252
|
signal: r.signal
|
|
253
253
|
});
|
|
254
254
|
},
|
|
255
|
-
rate(c,
|
|
256
|
-
return
|
|
255
|
+
rate(c, w) {
|
|
256
|
+
return w ? s.update(w, c) : s.create(c);
|
|
257
257
|
},
|
|
258
258
|
deleteRate(c) {
|
|
259
|
-
return
|
|
259
|
+
return s.delete(c);
|
|
260
260
|
},
|
|
261
261
|
speak(c) {
|
|
262
|
-
let
|
|
263
|
-
return c.type === "text" ?
|
|
262
|
+
let w;
|
|
263
|
+
return c.type === "text" ? w = {
|
|
264
264
|
script: {
|
|
265
265
|
type: "text",
|
|
266
266
|
provider: c.provider,
|
|
267
267
|
input: c.input,
|
|
268
268
|
ssml: c.ssml || !1
|
|
269
269
|
}
|
|
270
|
-
} : c.type === "audio" && (
|
|
270
|
+
} : c.type === "audio" && (w = {
|
|
271
271
|
script: {
|
|
272
272
|
type: "audio",
|
|
273
273
|
audio_url: c.audio_url
|
|
274
274
|
}
|
|
275
|
-
}), d.speak(
|
|
275
|
+
}), d.speak(w);
|
|
276
276
|
},
|
|
277
277
|
getStarterMessages() {
|
|
278
|
-
var c,
|
|
279
|
-
return (c = m.knowledge) != null && c.id ?
|
|
278
|
+
var c, w;
|
|
279
|
+
return (c = m.knowledge) != null && c.id ? u.getKnowledge((w = m.knowledge) == null ? void 0 : w.id).then((h) => (h == null ? void 0 : h.starter_message) || []) : Promise.resolve([]);
|
|
280
280
|
}
|
|
281
281
|
};
|
|
282
282
|
}
|
|
283
|
-
function
|
|
284
|
-
const n =
|
|
283
|
+
function ie(e, i) {
|
|
284
|
+
const n = L(e, i);
|
|
285
285
|
return {
|
|
286
286
|
createStream(t) {
|
|
287
287
|
return n.post("/clips/streams", {
|
|
@@ -315,8 +315,8 @@ function se(e, s) {
|
|
|
315
315
|
}
|
|
316
316
|
};
|
|
317
317
|
}
|
|
318
|
-
function
|
|
319
|
-
const n =
|
|
318
|
+
function se(e, i) {
|
|
319
|
+
const n = L(e, i);
|
|
320
320
|
return {
|
|
321
321
|
createStream(t, r) {
|
|
322
322
|
return n.post("/talks/streams", {
|
|
@@ -326,23 +326,23 @@ function ie(e, s) {
|
|
|
326
326
|
config: t.config
|
|
327
327
|
}, r);
|
|
328
328
|
},
|
|
329
|
-
startConnection(t, r, a,
|
|
329
|
+
startConnection(t, r, a, s) {
|
|
330
330
|
return n.post(`/talks/streams/${t}/sdp`, {
|
|
331
331
|
session_id: a,
|
|
332
332
|
answer: r
|
|
333
|
-
},
|
|
333
|
+
}, s);
|
|
334
334
|
},
|
|
335
|
-
addIceCandidate(t, r, a,
|
|
335
|
+
addIceCandidate(t, r, a, s) {
|
|
336
336
|
return n.post(`/talks/streams/${t}/ice`, {
|
|
337
337
|
session_id: a,
|
|
338
338
|
...r
|
|
339
|
-
},
|
|
339
|
+
}, s);
|
|
340
340
|
},
|
|
341
|
-
sendStreamRequest(t, r, a,
|
|
341
|
+
sendStreamRequest(t, r, a, s) {
|
|
342
342
|
return n.post(`/talks/streams/${t}`, {
|
|
343
343
|
session_id: r,
|
|
344
344
|
...a
|
|
345
|
-
},
|
|
345
|
+
}, s);
|
|
346
346
|
},
|
|
347
347
|
close(t, r, a) {
|
|
348
348
|
return n.delete(`/talks/streams/${t}`, {
|
|
@@ -351,13 +351,13 @@ function ie(e, s) {
|
|
|
351
351
|
}
|
|
352
352
|
};
|
|
353
353
|
}
|
|
354
|
-
function oe(e,
|
|
355
|
-
return e.map((n, t) => t === 0 ?
|
|
354
|
+
function oe(e, i) {
|
|
355
|
+
return e.map((n, t) => t === 0 ? i ? {
|
|
356
356
|
index: t,
|
|
357
357
|
timestamp: n.timestamp,
|
|
358
|
-
bytesReceived: n.bytesReceived -
|
|
359
|
-
packetsReceived: n.packetsReceived -
|
|
360
|
-
packetsLost: n.packetsLost -
|
|
358
|
+
bytesReceived: n.bytesReceived - i.bytesReceived,
|
|
359
|
+
packetsReceived: n.packetsReceived - i.packetsReceived,
|
|
360
|
+
packetsLost: n.packetsLost - i.packetsLost,
|
|
361
361
|
jitter: n.jitter,
|
|
362
362
|
frameWidth: n.frameWidth,
|
|
363
363
|
frameHeight: n.frameHeight,
|
|
@@ -384,90 +384,99 @@ function oe(e, s) {
|
|
|
384
384
|
frameRate: n.frameRate
|
|
385
385
|
});
|
|
386
386
|
}
|
|
387
|
-
let
|
|
388
|
-
const
|
|
387
|
+
let z = !1;
|
|
388
|
+
const y = (e, i) => z && console.log(e, i), ce = (window.RTCPeerConnection || window.webkitRTCPeerConnection || window.mozRTCPeerConnection).bind(window);
|
|
389
389
|
async function de(e, {
|
|
390
|
-
debug:
|
|
390
|
+
debug: i = !1,
|
|
391
391
|
callbacks: n,
|
|
392
392
|
auth: t,
|
|
393
|
-
baseURL: r =
|
|
393
|
+
baseURL: r = R
|
|
394
394
|
}) {
|
|
395
|
-
|
|
395
|
+
z = i;
|
|
396
396
|
const a = {
|
|
397
397
|
...n
|
|
398
398
|
}, {
|
|
399
|
-
startConnection:
|
|
400
|
-
sendStreamRequest:
|
|
399
|
+
startConnection: s,
|
|
400
|
+
sendStreamRequest: u,
|
|
401
401
|
close: m,
|
|
402
|
-
createStream:
|
|
403
|
-
addIceCandidate:
|
|
404
|
-
} = e.videoType ===
|
|
402
|
+
createStream: p,
|
|
403
|
+
addIceCandidate: f
|
|
404
|
+
} = e.videoType === P.Clip ? ie(t, r) : se(t, r), {
|
|
405
405
|
id: d,
|
|
406
406
|
offer: c,
|
|
407
|
-
ice_servers:
|
|
408
|
-
session_id:
|
|
409
|
-
} = await
|
|
410
|
-
iceServers:
|
|
411
|
-
}),
|
|
412
|
-
let
|
|
413
|
-
if (!
|
|
407
|
+
ice_servers: w,
|
|
408
|
+
session_id: h
|
|
409
|
+
} = await p(e), g = new ce({
|
|
410
|
+
iceServers: w
|
|
411
|
+
}), H = g.createDataChannel("JanusDataChannel"), l = [];
|
|
412
|
+
let A = 0, U = 0, K = 0, b, B;
|
|
413
|
+
if (!h)
|
|
414
414
|
throw new Error("Could not create session_id");
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
candidate:
|
|
418
|
-
sdpMid:
|
|
419
|
-
sdpMLineIndex:
|
|
420
|
-
},
|
|
421
|
-
},
|
|
422
|
-
var
|
|
423
|
-
|
|
424
|
-
},
|
|
425
|
-
var
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
415
|
+
g.onicecandidate = (o) => {
|
|
416
|
+
y("peerConnection.onicecandidate", o), o.candidate && o.candidate.sdpMid && o.candidate.sdpMLineIndex !== null && f(d, {
|
|
417
|
+
candidate: o.candidate.candidate,
|
|
418
|
+
sdpMid: o.candidate.sdpMid,
|
|
419
|
+
sdpMLineIndex: o.candidate.sdpMLineIndex
|
|
420
|
+
}, h);
|
|
421
|
+
}, g.oniceconnectionstatechange = () => {
|
|
422
|
+
var o;
|
|
423
|
+
y("peerConnection.oniceconnectionstatechange => " + g.iceConnectionState), (o = a.onConnectionStateChange) == null || o.call(a, g.iceConnectionState);
|
|
424
|
+
}, g.ontrack = (o) => {
|
|
425
|
+
var v;
|
|
426
|
+
y("peerConnection.ontrack", o), (v = a.onSrcObjectReady) == null || v.call(a, o.streams[0]), b = setInterval(() => {
|
|
427
|
+
var S, I, C, $;
|
|
428
|
+
if (l != null && l.length && U < (l == null ? void 0 : l.length)) {
|
|
429
|
+
const k = (((S = l.slice(-1)) == null ? void 0 : S[0].bytesReceived) ?? 1 / 0) > K;
|
|
430
|
+
K = ((I = l.slice(-1)) == null ? void 0 : I[0].bytesReceived) ?? 0, console.log("videoStatsStartIndex ", k, (C = l.slice(-1)) == null ? void 0 : C[0].bytesReceived, K, l), k ? console.log("PLAYYYY.....") : (($ = a.onVideoStateChange) == null || $.call(a, _.Stop), console.log("isPlaying NO by stats")), U = l == null ? void 0 : l.length;
|
|
431
|
+
}
|
|
432
|
+
}, 500);
|
|
433
|
+
}, H.onmessage = (o) => {
|
|
434
|
+
var v, S, I;
|
|
435
|
+
if (H.readyState === "open") {
|
|
436
|
+
const [C, $] = o.data.split(":");
|
|
437
|
+
if (C === M.StreamStarted)
|
|
438
|
+
console.log("StreamStarted", C, $), A = l.length, B = setInterval(() => {
|
|
439
|
+
g.getStats().then((x) => {
|
|
440
|
+
x.forEach((E) => {
|
|
441
|
+
E.type === "inbound-rtp" && E.kind === "video" && l.push(E);
|
|
436
442
|
});
|
|
437
443
|
});
|
|
438
|
-
}, 1e3), (
|
|
439
|
-
else if (C ===
|
|
440
|
-
console.log("StreamDone",
|
|
441
|
-
const
|
|
442
|
-
if (
|
|
443
|
-
const
|
|
444
|
-
|
|
444
|
+
}, 1e3), (v = a.onVideoStateChange) == null || v.call(a, _.Start);
|
|
445
|
+
else if (C === M.StreamDone) {
|
|
446
|
+
console.log("StreamDone"), clearInterval(B), clearInterval(b);
|
|
447
|
+
const k = l.slice(A);
|
|
448
|
+
if (k) {
|
|
449
|
+
const x = A === 0 ? void 0 : l[A - 1];
|
|
450
|
+
oe(k, x), A = l.length;
|
|
445
451
|
}
|
|
446
452
|
} else
|
|
447
|
-
C ===
|
|
453
|
+
C === M.StreamFailed ? (S = a.onVideoStateChange) == null || S.call(a, _.Stop, {
|
|
454
|
+
event: C,
|
|
455
|
+
data: $
|
|
456
|
+
}) : (I = a.onMessage) == null || I.call(a, C, decodeURIComponent($));
|
|
448
457
|
}
|
|
449
|
-
}, await
|
|
450
|
-
const
|
|
451
|
-
return
|
|
458
|
+
}, await g.setRemoteDescription(c), y("set remote description OK");
|
|
459
|
+
const N = await g.createAnswer();
|
|
460
|
+
return y("create answer OK"), await g.setLocalDescription(N), y("set local description OK"), await s(d, N, h), y("start connection OK"), {
|
|
452
461
|
/**
|
|
453
462
|
* Method to send request to server to get clip or talk depend on you payload
|
|
454
463
|
* @param payload
|
|
455
464
|
*/
|
|
456
|
-
speak(
|
|
457
|
-
return
|
|
465
|
+
speak(o) {
|
|
466
|
+
return u(d, h, o);
|
|
458
467
|
},
|
|
459
468
|
/**
|
|
460
469
|
* Method to close RTC connection
|
|
461
470
|
*/
|
|
462
471
|
async terminate() {
|
|
463
|
-
var
|
|
464
|
-
d && (
|
|
465
|
-
}), (
|
|
472
|
+
var o, v;
|
|
473
|
+
d && (g && (g.close(), g.oniceconnectionstatechange = null, g.onnegotiationneeded = null, g.onicecandidate = null, g.ontrack = null, clearInterval(b)), await m(d, h).catch((S) => {
|
|
474
|
+
}), (o = a.onConnectionStateChange) == null || o.call(a, "closed"), (v = a.onVideoStateChange) == null || v.call(a, _.Stop));
|
|
466
475
|
},
|
|
467
476
|
/**
|
|
468
477
|
* Session identifier information, should be returned in the body of all streaming requests
|
|
469
478
|
*/
|
|
470
|
-
sessionId:
|
|
479
|
+
sessionId: h,
|
|
471
480
|
/**
|
|
472
481
|
* Id of current RTC stream
|
|
473
482
|
*/
|
|
@@ -477,27 +486,27 @@ async function de(e, {
|
|
|
477
486
|
* @param eventName
|
|
478
487
|
* @param callback
|
|
479
488
|
*/
|
|
480
|
-
onCallback(
|
|
481
|
-
a[
|
|
489
|
+
onCallback(o, v) {
|
|
490
|
+
a[o] = v;
|
|
482
491
|
}
|
|
483
492
|
};
|
|
484
493
|
}
|
|
485
494
|
export {
|
|
486
495
|
ee as ChatMode,
|
|
487
|
-
|
|
496
|
+
W as ChatProgress,
|
|
488
497
|
re as DocumentType,
|
|
489
498
|
ne as KnowledgeType,
|
|
490
499
|
Z as Providers,
|
|
491
500
|
j as RateState,
|
|
492
|
-
|
|
493
|
-
|
|
501
|
+
X as SocketManager,
|
|
502
|
+
M as StreamEvents,
|
|
494
503
|
te as Subject,
|
|
495
504
|
O as VoiceAccess,
|
|
496
505
|
ue as createAgentManager,
|
|
497
|
-
|
|
498
|
-
|
|
506
|
+
F as createAgentsApi,
|
|
507
|
+
L as createClient,
|
|
499
508
|
D as createKnowledgeApi,
|
|
500
|
-
|
|
509
|
+
Y as createRatingsApi,
|
|
501
510
|
de as createStreamingManager,
|
|
502
511
|
le as getAgent,
|
|
503
512
|
ae as getAgentStreamArgs
|
package/dist/index.umd.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
(function(s,h){typeof exports=="object"&&typeof module<"u"?h(exports):typeof define=="function"&&define.amd?define(["exports"],h):(s=typeof globalThis<"u"?globalThis:s||self,h(s.index={}))})(this,function(s){"use strict";const h="https://api.d-id.com",te="wss://notifications.d-id.com";function W(e){if(e.type==="bearer")return`Bearer ${e.token}`;if(e.type==="basic")return`Basic ${btoa(`${e.username}:${e.password}`)}`;if(e.type==="key")return`Client-Key ${e.clientKey}.${e.externalId}`;throw new Error(`Unknown auth type: ${e}`)}function A(e,i=h){const n=async(t,r)=>{const a=await fetch(i+(t!=null&&t.startsWith("/")?t:`/${t}`),{...r,headers:{...r==null?void 0:r.headers,Authorization:W(e),"Content-Type":"application/json"}});if(!a.ok){let c=await a.text().catch(()=>"Failed to fetch");throw new Error(c)}return a.json()};return{get(t,r){return n(t,{...r,method:"GET"})},post(t,r,a){return n(t,{...a,body:JSON.stringify(r),method:"POST"})},delete(t,r,a){return n(t,{...a,body:JSON.stringify(r),method:"DELETE"})},patch(t,r,a){return n(t,{...a,body:JSON.stringify(r),method:"PATCH"})}}}function K(e,i=h){const n=A(e,`${i}/agents`);return{create(t,r){return n.post("/",t,r)},getAgents(t,r){return n.get(`/${t?`?tag=${t}`:""}`,r).then(a=>a??[])},getById(t,r){return n.get(`/${t}`,r)},delete(t,r){return n.delete(`/${t}`,void 0,r)},update(t,r,a){return n.patch(`/${t}`,r,a)},newChat(t,r){return n.post(`/${t}/chat`,void 0,r)},chat(t,r,a,c){return n.post(`/${t}/chat/${r}`,a,c)}}}function F(e,i=h){const n=A(e,`${i}/knowledge`);return{createKnowledge(t,r){return n.post("/",t,r)},getKnowledgeBase(t){return n.get("/",t)},getKnowledge(t,r){return n.get(`/${t}`,r)},deleteKnowledge(t,r){return n.delete(`/${t}`,void 0,r)},createDocument(t,r,a){return n.post(`/${t}/documents`,r,a)},deleteDocument(t,r,a){return n.delete(`/${t}/documents/${r}`,void 0,a)},getDocuments(t,r){return n.get(`/${t}/documents`,r)},getDocument(t,r,a){return n.get(`/${t}/documents/${r}`,a)},getRecords(t,r,a){return n.get(`/${t}/documents/${r}/records`,a)},query(t,r,a){return n.post(`/${t}/query`,{query:r},a)}}}function H(e,i=h){const n=A(e,`${i}/chats/ratings`);return{create(t,r){return n.post("/",t,r)},getByKnowledge(t,r){return n.get(`/${t}`,r).then(a=>a??[])},update(t,r,a){return n.patch(`/${t}`,r,a)},delete(t,r){return n.delete(`/${t}`,r)}}}const ne=e=>new Promise(i=>setTimeout(i,e));function re(e){return new Promise((i,n)=>{const{callbacks:t,host:r,auth:a}=e,{onMessage:c=null,onOpen:u=null,onClose:m=null,onError:w=null}=t||{},f=new WebSocket(`${r}?authorization=${W(a)}`);f.onmessage=c,f.onclose=m,f.onerror=l=>{console.log(l),w==null||w(l),n(l)},f.onopen=l=>{u==null||u(l),i(f)}})}async function ae(e){const{retries:i=1}=e;let n=null;for(let t=0;(n==null?void 0:n.readyState)!==WebSocket.OPEN;t++)try{n=await re(e)}catch(r){if(t===i)throw r;await ne(t*500)}return n}async function U(e,i,n){const t=n?[n]:[],r=await ae({auth:e,host:i,callbacks:{onMessage:a=>{const c=JSON.parse(a.data);t.forEach(u=>u(c.event,c))}}});return{socket:r,terminate:()=>r.close(),subscribeToEvents:a=>t.push(a)}}var B=(e=>(e.Amazon="amazon",e.Microsoft="microsoft",e.Afflorithmics="afflorithmics",e.Elevenlabs="elevenlabs",e))(B||{}),N=(e=>(e.Public="public",e.Premium="premium",e.Private="private",e))(N||{}),R=(e=>(e.Start="START",e.Stop="STOP",e))(R||{}),$=(e=>(e.ChatAnswer="chat/answer",e.ChatPartial="chat/partial",e.StreamDone="stream/done",e.StreamStarted="stream/started",e.StreamFailed="stream/error",e))($||{}),q=(e=>(e.Unrated="Unrated",e.Positive="Positive",e.Negative="Negative",e))(q||{}),z=(e=>(e.Functional="Functional",e.TextOnly="TextOnly",e.Maintenance="Maintenance",e))(z||{}),P=(e=>(e.Embed="embed",e.Query="query",e.Partial="partial",e.Answer="answer",e.Complete="done",e))(P||{}),J=(e=>(e.KnowledgeProcessing="knowledge/processing",e.KnowledgeIndexing="knowledge/indexing",e.KnowledgeFailed="knowledge/error",e.KnowledgeDone="knowledge/done",e))(J||{}),D=(e=>(e.Knowledge="knowledge",e.Document="document",e.Record="record",e))(D||{}),V=(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))(V||{}),y=(e=>(e.Clip="clip",e.Talk="talk",e))(y||{});function G(e){return e.presenter.type===y.Clip?{videoType:y.Clip,driver_id:e.presenter.driver_id,presenter_id:e.presenter.presenter_id}:{videoType:y.Talk,source_url:e.presenter.source_url}}function Q(e,i,n,t){return new Promise(async(r,a)=>{const c=await Y(G(e),{...i,callbacks:{...i.callbacks,onConnectionStateChange:async u=>{var m,w;u==="connected"?(t||(t=await n.newChat(e.id)),r({chat:t,streamingManager:c})):u==="failed"&&a(new Error("Cannot create connection")),(w=(m=i.callbacks).onConnectionStateChange)==null||w.call(m,u)},onMessage:(u,m)=>{var w,f;u===$.ChatAnswer&&(console.log("ChatAnswer",u,m),(f=(w=i.callbacks).onChatEvents)==null||f.call(w,P.Answer,{content:m,event:P.Answer}))}}})})}function ie(e,i,n){return K(i,n||h).getById(e)}async function se(e,i){const n=i.baseURL||h,t=i.wsURL||te,r=new AbortController,a=K(i.auth,n),c=H(i.auth,n),u=F(i.auth,n),m=await a.getById(e),w=await U(i.auth,t,i.callbacks.onChatEvents);let{chat:f,streamingManager:l}=await Q(m,i,a);return{agent:m,async reconnectToChat(){const{streamingManager:d}=await Q(m,i,a,f);l=d},terminate(){return r.abort(),w.terminate(),l.terminate()},chatId:f.id,chat(d){return a.chat(e,f.id,{sessionId:l.sessionId,streamId:l.streamId,messages:d},{signal:r.signal})},rate(d,p){return p?c.update(p,d):c.create(d)},deleteRate(d){return c.delete(d)},speak(d){let p;return d.type==="text"?p={script:{type:"text",provider:d.provider,input:d.input,ssml:d.ssml||!1}}:d.type==="audio"&&(p={script:{type:"audio",audio_url:d.audio_url}}),l.speak(p)},getStarterMessages(){var d,p;return(d=m.knowledge)!=null&&d.id?u.getKnowledge((p=m.knowledge)==null?void 0:p.id).then(C=>(C==null?void 0:C.starter_message)||[]):Promise.resolve([])}}}function oe(e,i){const n=A(e,i);return{createStream(t){return n.post("/clips/streams",{driver_id:t.driver_id,presenter_id:t.presenter_id,compatibility_mode:t.compatibility_mode})},startConnection(t,r,a){return n.post(`/clips/streams/${t}/sdp`,{session_id:a,answer:r})},addIceCandidate(t,r,a){return n.post(`/clips/streams/${t}/ice`,{session_id:a,...r})},sendStreamRequest(t,r,a){return n.post(`/clips/streams/${t}`,{session_id:r,...a})},close(t,r){return n.delete(`/clips/streams/${t}`,{session_id:r})}}}function ce(e,i){const n=A(e,i);return{createStream(t,r){return n.post("/talks/streams",{source_url:t.source_url,driver_url:t.driver_url,face:t.face,config:t.config},r)},startConnection(t,r,a,c){return n.post(`/talks/streams/${t}/sdp`,{session_id:a,answer:r},c)},addIceCandidate(t,r,a,c){return n.post(`/talks/streams/${t}/ice`,{session_id:a,...r},c)},sendStreamRequest(t,r,a,c){return n.post(`/talks/streams/${t}`,{session_id:r,...a},c)},close(t,r,a){return n.delete(`/talks/streams/${t}`,{session_id:r},a)}}}function de(e,i){return e.map((n,t)=>t===0?i?{index:t,timestamp:n.timestamp,bytesReceived:n.bytesReceived-i.bytesReceived,packetsReceived:n.packetsReceived-i.packetsReceived,packetsLost:n.packetsLost-i.packetsLost,jitter:n.jitter,frameWidth:n.frameWidth,frameHeight:n.frameHeight,frameRate:n.frameRate}:{index:t,timestamp:n.timestamp,bytesReceived:n.bytesReceived,packetsReceived:n.packetsReceived,packetsLost:n.packetsLost,jitter:n.jitter,frameWidth:n.frameWidth,frameHeight:n.frameHeight,frameRate:n.frameRate}:{index:t,timestamp:n.timestamp,bytesReceived:n.bytesReceived-e[t-1].bytesReceived,packetsReceived:n.packetsReceived-e[t-1].packetsReceived,packetsLost:n.packetsLost-e[t-1].packetsLost,jitter:n.jitter,frameWidth:n.frameWidth,frameHeight:n.frameHeight,frameRate:n.frameRate})}let X=!1;const S=(e,i)=>X&&console.log(e,i),le=(window.RTCPeerConnection||window.webkitRTCPeerConnection||window.mozRTCPeerConnection).bind(window);async function Y(e,{debug:i=!1,callbacks:n,auth:t,baseURL:r=h}){X=i;const a={...n},{startConnection:c,sendStreamRequest:u,close:m,createStream:w,addIceCandidate:f}=e.videoType===y.Clip?oe(t,r):ce(t,r),{id:l,offer:d,ice_servers:p,session_id:C}=await w(e),g=new le({iceServers:p}),Z=g.createDataChannel("JanusDataChannel"),_=[];let I=0,O;if(!C)throw new Error("Could not create session_id");g.onicecandidate=o=>{S("peerConnection.onicecandidate",o),o.candidate&&o.candidate.sdpMid&&o.candidate.sdpMLineIndex!==null&&f(l,{candidate:o.candidate.candidate,sdpMid:o.candidate.sdpMid,sdpMLineIndex:o.candidate.sdpMLineIndex},C)},g.oniceconnectionstatechange=()=>{var o;S("peerConnection.oniceconnectionstatechange => "+g.iceConnectionState),(o=a.onConnectionStateChange)==null||o.call(a,g.iceConnectionState)},g.ontrack=o=>{var v;S("peerConnection.ontrack",o),console.log("peerConnection.ontrack streams->",o.streams),(v=a.onSrcObjectReady)==null||v.call(a,o.streams[0])},Z.onmessage=o=>{var v,b,j,ee;if(Z.readyState==="open"){const[k,L]=o.data.split(":");if(k===$.StreamStarted)console.log("StreamStarted",k,L),I=_.length,O=setInterval(()=>{g.getStats().then(T=>{T.forEach(M=>{M.type==="inbound-rtp"&&M.kind==="video"&&_.push(M)})})},1e3),(v=a.onVideoStateChange)==null||v.call(a,R.Start);else if(k===$.StreamDone){console.log("StreamDone",k,L),clearInterval(O);const E=_.slice(I);if(E){const T=I===0?void 0:_[I-1],M=de(E,T);I=_.length,(b=a.onVideoStateChange)==null||b.call(a,R.Stop,M.sort((ue,ge)=>ge.packetsLost-ue.packetsLost).slice(0,5))}}else k===$.StreamFailed?(console.log("StreamFailed",k,L),(j=a.onVideoStateChange)==null||j.call(a,R.Stop,"BEN GOT FAILE")):(ee=a.onMessage)==null||ee.call(a,k,decodeURIComponent(L))}},await g.setRemoteDescription(d),S("set remote description OK");const x=await g.createAnswer();return S("create answer OK"),await g.setLocalDescription(x),S("set local description OK"),await c(l,x,C),S("start connection OK"),{speak(o){return u(l,C,o)},async terminate(){var o,v;l&&(g&&(g.close(),g.oniceconnectionstatechange=null,g.onnegotiationneeded=null,g.onicecandidate=null,g.ontrack=null),await m(l,C).catch(b=>{}),(o=a.onConnectionStateChange)==null||o.call(a,"closed"),(v=a.onVideoStateChange)==null||v.call(a,R.Stop))},sessionId:C,streamId:l,onCallback(o,v){a[o]=v}}}s.ChatMode=z,s.ChatProgress=P,s.DocumentType=V,s.KnowledgeType=D,s.Providers=B,s.RateState=q,s.SocketManager=U,s.StreamEvents=$,s.Subject=J,s.VoiceAccess=N,s.createAgentManager=se,s.createAgentsApi=K,s.createClient=A,s.createKnowledgeApi=F,s.createRatingsApi=H,s.createStreamingManager=Y,s.getAgent=ie,s.getAgentStreamArgs=G,Object.defineProperty(s,Symbol.toStringTag,{value:"Module"})});
|
|
1
|
+
(function(s,p){typeof exports=="object"&&typeof module<"u"?p(exports):typeof define=="function"&&define.amd?define(["exports"],p):(s=typeof globalThis<"u"?globalThis:s||self,p(s.index={}))})(this,function(s){"use strict";const p="https://api.d-id.com",re="wss://notifications.d-id.com";function B(e){if(e.type==="bearer")return`Bearer ${e.token}`;if(e.type==="basic")return`Basic ${btoa(`${e.username}:${e.password}`)}`;if(e.type==="key")return`Client-Key ${e.clientKey}.${e.externalId}`;throw new Error(`Unknown auth type: ${e}`)}function k(e,i=p){const n=async(t,r)=>{const a=await fetch(i+(t!=null&&t.startsWith("/")?t:`/${t}`),{...r,headers:{...r==null?void 0:r.headers,Authorization:B(e),"Content-Type":"application/json"}});if(!a.ok){let o=await a.text().catch(()=>"Failed to fetch");throw new Error(o)}return a.json()};return{get(t,r){return n(t,{...r,method:"GET"})},post(t,r,a){return n(t,{...a,body:JSON.stringify(r),method:"POST"})},delete(t,r,a){return n(t,{...a,body:JSON.stringify(r),method:"DELETE"})},patch(t,r,a){return n(t,{...a,body:JSON.stringify(r),method:"PATCH"})}}}function E(e,i=p){const n=k(e,`${i}/agents`);return{create(t,r){return n.post("/",t,r)},getAgents(t,r){return n.get(`/${t?`?tag=${t}`:""}`,r).then(a=>a??[])},getById(t,r){return n.get(`/${t}`,r)},delete(t,r){return n.delete(`/${t}`,void 0,r)},update(t,r,a){return n.patch(`/${t}`,r,a)},newChat(t,r){return n.post(`/${t}/chat`,void 0,r)},chat(t,r,a,o){return n.post(`/${t}/chat/${r}`,a,o)}}}function N(e,i=p){const n=k(e,`${i}/knowledge`);return{createKnowledge(t,r){return n.post("/",t,r)},getKnowledgeBase(t){return n.get("/",t)},getKnowledge(t,r){return n.get(`/${t}`,r)},deleteKnowledge(t,r){return n.delete(`/${t}`,void 0,r)},createDocument(t,r,a){return n.post(`/${t}/documents`,r,a)},deleteDocument(t,r,a){return n.delete(`/${t}/documents/${r}`,void 0,a)},getDocuments(t,r){return n.get(`/${t}/documents`,r)},getDocument(t,r,a){return n.get(`/${t}/documents/${r}`,a)},getRecords(t,r,a){return n.get(`/${t}/documents/${r}/records`,a)},query(t,r,a){return n.post(`/${t}/query`,{query:r},a)}}}function q(e,i=p){const n=k(e,`${i}/chats/ratings`);return{create(t,r){return n.post("/",t,r)},getByKnowledge(t,r){return n.get(`/${t}`,r).then(a=>a??[])},update(t,r,a){return n.patch(`/${t}`,r,a)},delete(t,r){return n.delete(`/${t}`,r)}}}const ae=e=>new Promise(i=>setTimeout(i,e));function ie(e){return new Promise((i,n)=>{const{callbacks:t,host:r,auth:a}=e,{onMessage:o=null,onOpen:g=null,onClose:f=null,onError:w=null}=t||{},h=new WebSocket(`${r}?authorization=${B(a)}`);h.onmessage=o,h.onclose=f,h.onerror=l=>{console.log(l),w==null||w(l),n(l)},h.onopen=l=>{g==null||g(l),i(h)}})}async function se(e){const{retries:i=1}=e;let n=null;for(let t=0;(n==null?void 0:n.readyState)!==WebSocket.OPEN;t++)try{n=await ie(e)}catch(r){if(t===i)throw r;await ae(t*500)}return n}async function F(e,i,n){const t=n?[n]:[],r=await se({auth:e,host:i,callbacks:{onMessage:a=>{const o=JSON.parse(a.data);t.forEach(g=>g(o.event,o))}}});return{socket:r,terminate:()=>r.close(),subscribeToEvents:a=>t.push(a)}}var z=(e=>(e.Amazon="amazon",e.Microsoft="microsoft",e.Afflorithmics="afflorithmics",e.Elevenlabs="elevenlabs",e))(z||{}),J=(e=>(e.Public="public",e.Premium="premium",e.Private="private",e))(J||{}),M=(e=>(e.Start="START",e.Stop="STOP",e))(M||{}),A=(e=>(e.ChatAnswer="chat/answer",e.ChatPartial="chat/partial",e.StreamDone="stream/done",e.StreamStarted="stream/started",e.StreamFailed="stream/error",e))(A||{}),D=(e=>(e.Unrated="Unrated",e.Positive="Positive",e.Negative="Negative",e))(D||{}),V=(e=>(e.Functional="Functional",e.TextOnly="TextOnly",e.Maintenance="Maintenance",e))(V||{}),L=(e=>(e.Embed="embed",e.Query="query",e.Partial="partial",e.Answer="answer",e.Complete="done",e))(L||{}),Y=(e=>(e.KnowledgeProcessing="knowledge/processing",e.KnowledgeIndexing="knowledge/indexing",e.KnowledgeFailed="knowledge/error",e.KnowledgeDone="knowledge/done",e))(Y||{}),G=(e=>(e.Knowledge="knowledge",e.Document="document",e.Record="record",e))(G||{}),Q=(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))(Q||{}),P=(e=>(e.Clip="clip",e.Talk="talk",e))(P||{});function X(e){return e.presenter.type===P.Clip?{videoType:P.Clip,driver_id:e.presenter.driver_id,presenter_id:e.presenter.presenter_id}:{videoType:P.Talk,source_url:e.presenter.source_url}}function Z(e,i,n,t){return new Promise(async(r,a)=>{const o=await O(X(e),{...i,callbacks:{...i.callbacks,onConnectionStateChange:async g=>{var f,w;g==="connected"?(t||(t=await n.newChat(e.id)),r({chat:t,streamingManager:o})):g==="failed"&&a(new Error("Cannot create connection")),(w=(f=i.callbacks).onConnectionStateChange)==null||w.call(f,g)},onMessage:(g,f)=>{var w,h;g===A.ChatAnswer&&(console.log("ChatAnswer",g,f),(h=(w=i.callbacks).onChatEvents)==null||h.call(w,L.Answer,{content:f,event:L.Answer}))}}})})}function oe(e,i,n){return E(i,n||p).getById(e)}async function ce(e,i){const n=i.baseURL||p,t=i.wsURL||re,r=new AbortController,a=E(i.auth,n),o=q(i.auth,n),g=N(i.auth,n),f=await a.getById(e),w=await F(i.auth,t,i.callbacks.onChatEvents);let{chat:h,streamingManager:l}=await Z(f,i,a);return{agent:f,async reconnectToChat(){const{streamingManager:d}=await Z(f,i,a,h);l=d},terminate(){return r.abort(),w.terminate(),l.terminate()},chatId:h.id,chat(d){return a.chat(e,h.id,{sessionId:l.sessionId,streamId:l.streamId,messages:d},{signal:r.signal})},rate(d,v){return v?o.update(v,d):o.create(d)},deleteRate(d){return o.delete(d)},speak(d){let v;return d.type==="text"?v={script:{type:"text",provider:d.provider,input:d.input,ssml:d.ssml||!1}}:d.type==="audio"&&(v={script:{type:"audio",audio_url:d.audio_url}}),l.speak(v)},getStarterMessages(){var d,v;return(d=f.knowledge)!=null&&d.id?g.getKnowledge((v=f.knowledge)==null?void 0:v.id).then(C=>(C==null?void 0:C.starter_message)||[]):Promise.resolve([])}}}function de(e,i){const n=k(e,i);return{createStream(t){return n.post("/clips/streams",{driver_id:t.driver_id,presenter_id:t.presenter_id,compatibility_mode:t.compatibility_mode})},startConnection(t,r,a){return n.post(`/clips/streams/${t}/sdp`,{session_id:a,answer:r})},addIceCandidate(t,r,a){return n.post(`/clips/streams/${t}/ice`,{session_id:a,...r})},sendStreamRequest(t,r,a){return n.post(`/clips/streams/${t}`,{session_id:r,...a})},close(t,r){return n.delete(`/clips/streams/${t}`,{session_id:r})}}}function le(e,i){const n=k(e,i);return{createStream(t,r){return n.post("/talks/streams",{source_url:t.source_url,driver_url:t.driver_url,face:t.face,config:t.config},r)},startConnection(t,r,a,o){return n.post(`/talks/streams/${t}/sdp`,{session_id:a,answer:r},o)},addIceCandidate(t,r,a,o){return n.post(`/talks/streams/${t}/ice`,{session_id:a,...r},o)},sendStreamRequest(t,r,a,o){return n.post(`/talks/streams/${t}`,{session_id:r,...a},o)},close(t,r,a){return n.delete(`/talks/streams/${t}`,{session_id:r},a)}}}function ue(e,i){return e.map((n,t)=>t===0?i?{index:t,timestamp:n.timestamp,bytesReceived:n.bytesReceived-i.bytesReceived,packetsReceived:n.packetsReceived-i.packetsReceived,packetsLost:n.packetsLost-i.packetsLost,jitter:n.jitter,frameWidth:n.frameWidth,frameHeight:n.frameHeight,frameRate:n.frameRate}:{index:t,timestamp:n.timestamp,bytesReceived:n.bytesReceived,packetsReceived:n.packetsReceived,packetsLost:n.packetsLost,jitter:n.jitter,frameWidth:n.frameWidth,frameHeight:n.frameHeight,frameRate:n.frameRate}:{index:t,timestamp:n.timestamp,bytesReceived:n.bytesReceived-e[t-1].bytesReceived,packetsReceived:n.packetsReceived-e[t-1].packetsReceived,packetsLost:n.packetsLost-e[t-1].packetsLost,jitter:n.jitter,frameWidth:n.frameWidth,frameHeight:n.frameHeight,frameRate:n.frameRate})}let x=!1;const R=(e,i)=>x&&console.log(e,i),ge=(window.RTCPeerConnection||window.webkitRTCPeerConnection||window.mozRTCPeerConnection).bind(window);async function O(e,{debug:i=!1,callbacks:n,auth:t,baseURL:r=p}){x=i;const a={...n},{startConnection:o,sendStreamRequest:g,close:f,createStream:w,addIceCandidate:h}=e.videoType===P.Clip?de(t,r):le(t,r),{id:l,offer:d,ice_servers:v,session_id:C}=await w(e),m=new ge({iceServers:v}),j=m.createDataChannel("JanusDataChannel"),u=[];let b=0,ee=0,T=0,W,te;if(!C)throw new Error("Could not create session_id");m.onicecandidate=c=>{R("peerConnection.onicecandidate",c),c.candidate&&c.candidate.sdpMid&&c.candidate.sdpMLineIndex!==null&&h(l,{candidate:c.candidate.candidate,sdpMid:c.candidate.sdpMid,sdpMLineIndex:c.candidate.sdpMLineIndex},C)},m.oniceconnectionstatechange=()=>{var c;R("peerConnection.oniceconnectionstatechange => "+m.iceConnectionState),(c=a.onConnectionStateChange)==null||c.call(a,m.iceConnectionState)},m.ontrack=c=>{var S;R("peerConnection.ontrack",c),(S=a.onSrcObjectReady)==null||S.call(a,c.streams[0]),W=setInterval(()=>{var $,K,y,I;if(u!=null&&u.length&&ee<(u==null?void 0:u.length)){const _=((($=u.slice(-1))==null?void 0:$[0].bytesReceived)??1/0)>T;T=((K=u.slice(-1))==null?void 0:K[0].bytesReceived)??0,console.log("videoStatsStartIndex ",_,(y=u.slice(-1))==null?void 0:y[0].bytesReceived,T,u),_?console.log("PLAYYYY....."):((I=a.onVideoStateChange)==null||I.call(a,M.Stop),console.log("isPlaying NO by stats")),ee=u==null?void 0:u.length}},500)},j.onmessage=c=>{var S,$,K;if(j.readyState==="open"){const[y,I]=c.data.split(":");if(y===A.StreamStarted)console.log("StreamStarted",y,I),b=u.length,te=setInterval(()=>{m.getStats().then(H=>{H.forEach(U=>{U.type==="inbound-rtp"&&U.kind==="video"&&u.push(U)})})},1e3),(S=a.onVideoStateChange)==null||S.call(a,M.Start);else if(y===A.StreamDone){console.log("StreamDone"),clearInterval(te),clearInterval(W);const _=u.slice(b);if(_){const H=b===0?void 0:u[b-1];ue(_,H),b=u.length}}else y===A.StreamFailed?($=a.onVideoStateChange)==null||$.call(a,M.Stop,{event:y,data:I}):(K=a.onMessage)==null||K.call(a,y,decodeURIComponent(I))}},await m.setRemoteDescription(d),R("set remote description OK");const ne=await m.createAnswer();return R("create answer OK"),await m.setLocalDescription(ne),R("set local description OK"),await o(l,ne,C),R("start connection OK"),{speak(c){return g(l,C,c)},async terminate(){var c,S;l&&(m&&(m.close(),m.oniceconnectionstatechange=null,m.onnegotiationneeded=null,m.onicecandidate=null,m.ontrack=null,clearInterval(W)),await f(l,C).catch($=>{}),(c=a.onConnectionStateChange)==null||c.call(a,"closed"),(S=a.onVideoStateChange)==null||S.call(a,M.Stop))},sessionId:C,streamId:l,onCallback(c,S){a[c]=S}}}s.ChatMode=V,s.ChatProgress=L,s.DocumentType=Q,s.KnowledgeType=G,s.Providers=z,s.RateState=D,s.SocketManager=F,s.StreamEvents=A,s.Subject=Y,s.VoiceAccess=J,s.createAgentManager=ce,s.createAgentsApi=E,s.createClient=k,s.createKnowledgeApi=N,s.createRatingsApi=q,s.createStreamingManager=O,s.getAgent=oe,s.getAgentStreamArgs=X,Object.defineProperty(s,Symbol.toStringTag,{value:"Module"})});
|