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