@d-id/client-sdk 1.0.19-beta.154 → 1.0.19-beta.155
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 +236 -235
- package/dist/index.umd.cjs +1 -1
- package/package.json +2 -2
package/dist/index.js
CHANGED
|
@@ -9,7 +9,7 @@ const ue = (e) => {
|
|
|
9
9
|
throw new Error(`Unknown video type: ${e}`);
|
|
10
10
|
}
|
|
11
11
|
};
|
|
12
|
-
var j = /* @__PURE__ */ ((e) => (e.Start = "START", e.Stop = "STOP", e))(j || {}),
|
|
12
|
+
var j = /* @__PURE__ */ ((e) => (e.Start = "START", e.Stop = "STOP", e))(j || {}), V = /* @__PURE__ */ ((e) => (e.ChatAnswer = "chat/answer", e.ChatPartial = "chat/partial", e.StreamDone = "stream/done", e.StreamStarted = "stream/started", e.StreamFailed = "stream/error", e.StreamReady = "stream/ready", e.StreamCreated = "stream/created", e.StreamVideoCreated = "stream-video/started", e.StreamVideoDone = "stream-video/done", e.StreamVideoError = "stream-video/error", e.StreamVideoRejected = "stream-video/rejected", e))(V || {}), D = /* @__PURE__ */ ((e) => (e.New = "new", e.Fail = "fail", e.Connected = "connected", e.Connecting = "connecting", e.Closed = "closed", e.Completed = "completed", e.Disconnected = "disconnected", e))(D || {}), fe = /* @__PURE__ */ ((e) => (e.Amazon = "amazon", e.Microsoft = "microsoft", e.Afflorithmics = "afflorithmics", e.Elevenlabs = "elevenlabs", e))(fe || {}), ge = /* @__PURE__ */ ((e) => (e.Public = "public", e.Premium = "premium", e.Private = "private", e))(ge || {});
|
|
13
13
|
const H = "https://api.d-id.com", he = "wss://notifications.d-id.com", we = "79f81a83a67430be2bc0fd61042b8faa";
|
|
14
14
|
async function Q(e, t) {
|
|
15
15
|
const s = {
|
|
@@ -27,14 +27,14 @@ async function Q(e, t) {
|
|
|
27
27
|
if (!s.timeout)
|
|
28
28
|
return await e();
|
|
29
29
|
let i;
|
|
30
|
-
const a = new Promise((
|
|
31
|
-
e().then((
|
|
32
|
-
clearTimeout(i), d
|
|
33
|
-
}).catch((
|
|
34
|
-
clearTimeout(i),
|
|
30
|
+
const a = new Promise((l, h) => {
|
|
31
|
+
e().then((d) => {
|
|
32
|
+
clearTimeout(i), l(d);
|
|
33
|
+
}).catch((d) => {
|
|
34
|
+
clearTimeout(i), h(d);
|
|
35
35
|
});
|
|
36
|
-
}), o = new Promise((
|
|
37
|
-
i = setTimeout(() =>
|
|
36
|
+
}), o = new Promise((l, h) => {
|
|
37
|
+
i = setTimeout(() => h(new Error(s.timeoutErrorMessage)), s.timeout);
|
|
38
38
|
});
|
|
39
39
|
return await Promise.race([a, o]);
|
|
40
40
|
} catch (i) {
|
|
@@ -44,12 +44,12 @@ async function Q(e, t) {
|
|
|
44
44
|
}
|
|
45
45
|
throw r;
|
|
46
46
|
}
|
|
47
|
-
const
|
|
47
|
+
const K = () => Math.random().toString(16).slice(2);
|
|
48
48
|
function G() {
|
|
49
49
|
let e = window.localStorage.getItem("did_external_key_id");
|
|
50
50
|
return e || (e = Math.random().toString(16).slice(2), window.localStorage.setItem("did_external_key_id", e)), e;
|
|
51
51
|
}
|
|
52
|
-
let pe =
|
|
52
|
+
let pe = K();
|
|
53
53
|
function ee(e) {
|
|
54
54
|
if (e.type === "bearer")
|
|
55
55
|
return `Bearer ${e.token}`;
|
|
@@ -70,7 +70,7 @@ function q(e, t = H, s) {
|
|
|
70
70
|
const {
|
|
71
71
|
skipErrorHandler: a,
|
|
72
72
|
...o
|
|
73
|
-
} = i || {},
|
|
73
|
+
} = i || {}, m = await ye(() => fetch(t + (n != null && n.startsWith("/") ? n : `/${n}`), {
|
|
74
74
|
...o,
|
|
75
75
|
headers: {
|
|
76
76
|
...o.headers,
|
|
@@ -78,15 +78,15 @@ function q(e, t = H, s) {
|
|
|
78
78
|
"Content-Type": "application/json"
|
|
79
79
|
}
|
|
80
80
|
}));
|
|
81
|
-
if (!
|
|
82
|
-
let
|
|
83
|
-
throw s && !a && s(new Error(
|
|
81
|
+
if (!m.ok) {
|
|
82
|
+
let l = await m.text().catch(() => "Failed to fetch");
|
|
83
|
+
throw s && !a && s(new Error(l), {
|
|
84
84
|
url: n,
|
|
85
85
|
options: o,
|
|
86
|
-
headers:
|
|
87
|
-
}), new Error(
|
|
86
|
+
headers: m.headers
|
|
87
|
+
}), new Error(l);
|
|
88
88
|
}
|
|
89
|
-
return
|
|
89
|
+
return m.json();
|
|
90
90
|
};
|
|
91
91
|
return {
|
|
92
92
|
get(n, i) {
|
|
@@ -145,8 +145,8 @@ function te(e, t = H, s) {
|
|
|
145
145
|
createRating(n, i, a, o) {
|
|
146
146
|
return r.post(`/${n}/chat/${i}/ratings`, a, o);
|
|
147
147
|
},
|
|
148
|
-
updateRating(n, i, a, o,
|
|
149
|
-
return r.patch(`/${n}/chat/${i}/ratings/${a}`, o,
|
|
148
|
+
updateRating(n, i, a, o, m) {
|
|
149
|
+
return r.patch(`/${n}/chat/${i}/ratings/${a}`, o, m);
|
|
150
150
|
},
|
|
151
151
|
deleteRating(n, i, a, o) {
|
|
152
152
|
return r.delete(`/${n}/chat/${i}/ratings/${a}`, o);
|
|
@@ -166,13 +166,13 @@ function ke(e) {
|
|
|
166
166
|
} = e, {
|
|
167
167
|
onMessage: a = null,
|
|
168
168
|
onOpen: o = null,
|
|
169
|
-
onClose:
|
|
170
|
-
onError:
|
|
171
|
-
} = r || {},
|
|
172
|
-
|
|
173
|
-
console.error(
|
|
174
|
-
},
|
|
175
|
-
o == null || o(
|
|
169
|
+
onClose: m = null,
|
|
170
|
+
onError: l = null
|
|
171
|
+
} = r || {}, h = new WebSocket(`${n}?authorization=${ee(i)}`);
|
|
172
|
+
h.onmessage = a, h.onclose = m, h.onerror = (d) => {
|
|
173
|
+
console.error(d), l == null || l("Websocket failed to connect", d), s(d);
|
|
174
|
+
}, h.onopen = (d) => {
|
|
175
|
+
o == null || o(d), t(h);
|
|
176
176
|
};
|
|
177
177
|
});
|
|
178
178
|
}
|
|
@@ -262,23 +262,23 @@ function _e(e, t, s, r) {
|
|
|
262
262
|
stream_greeting: i.stream_greeting
|
|
263
263
|
}, a);
|
|
264
264
|
},
|
|
265
|
-
startConnection(i, a, o,
|
|
265
|
+
startConnection(i, a, o, m) {
|
|
266
266
|
return n.post(`/streams/${i}/sdp`, {
|
|
267
267
|
session_id: o,
|
|
268
268
|
answer: a
|
|
269
|
-
},
|
|
269
|
+
}, m);
|
|
270
270
|
},
|
|
271
|
-
addIceCandidate(i, a, o,
|
|
271
|
+
addIceCandidate(i, a, o, m) {
|
|
272
272
|
return n.post(`/streams/${i}/ice`, {
|
|
273
273
|
session_id: o,
|
|
274
274
|
...a
|
|
275
|
-
},
|
|
275
|
+
}, m);
|
|
276
276
|
},
|
|
277
|
-
sendStreamRequest(i, a, o,
|
|
277
|
+
sendStreamRequest(i, a, o, m) {
|
|
278
278
|
return n.post(`/streams/${i}`, {
|
|
279
279
|
session_id: a,
|
|
280
280
|
...o
|
|
281
|
-
},
|
|
281
|
+
}, m);
|
|
282
282
|
},
|
|
283
283
|
close(i, a, o) {
|
|
284
284
|
return n.delete(`/streams/${i}`, {
|
|
@@ -427,17 +427,17 @@ function $e() {
|
|
|
427
427
|
}
|
|
428
428
|
function Ae(e, t, s = !1, r, n, i = !1) {
|
|
429
429
|
const o = Math.max(Math.ceil(10), 1);
|
|
430
|
-
let
|
|
431
|
-
const
|
|
432
|
-
let
|
|
433
|
-
const
|
|
430
|
+
let m = [], l, h = 0, d = !1;
|
|
431
|
+
const k = s ? 1 : 0;
|
|
432
|
+
let y = 0;
|
|
433
|
+
const C = $e();
|
|
434
434
|
return setInterval(async () => {
|
|
435
|
-
const
|
|
436
|
-
if (
|
|
437
|
-
|
|
438
|
-
else if (
|
|
439
|
-
const
|
|
440
|
-
t == null || t(j.Stop,
|
|
435
|
+
const w = await e.getStats(), f = C(w), M = Ee(w);
|
|
436
|
+
if (f)
|
|
437
|
+
h = 0, d || (t == null || t(j.Start), i && y >= k && !r() && n(), l = m[m.length - 1], m = [], y++, d = !0), m.push(M);
|
|
438
|
+
else if (d && (h++, h >= o)) {
|
|
439
|
+
const R = Ie(m, 100, l);
|
|
440
|
+
t == null || t(j.Stop, R), !i && !r() && n(), d = !1;
|
|
441
441
|
}
|
|
442
442
|
}, 100);
|
|
443
443
|
}
|
|
@@ -451,111 +451,112 @@ async function Te(e, t, {
|
|
|
451
451
|
ne = s;
|
|
452
452
|
const {
|
|
453
453
|
startConnection: o,
|
|
454
|
-
sendStreamRequest:
|
|
455
|
-
close:
|
|
456
|
-
createStream:
|
|
457
|
-
addIceCandidate:
|
|
454
|
+
sendStreamRequest: m,
|
|
455
|
+
close: l,
|
|
456
|
+
createStream: h,
|
|
457
|
+
addIceCandidate: d
|
|
458
458
|
} = t.videoType === Z.Clip ? Se(n, i, e, r.onError) : _e(n, i, e, r.onError), {
|
|
459
|
-
id:
|
|
460
|
-
offer:
|
|
461
|
-
ice_servers:
|
|
462
|
-
session_id:
|
|
463
|
-
} = await
|
|
464
|
-
iceServers:
|
|
465
|
-
}),
|
|
466
|
-
if (!
|
|
459
|
+
id: k,
|
|
460
|
+
offer: y,
|
|
461
|
+
ice_servers: C,
|
|
462
|
+
session_id: w
|
|
463
|
+
} = await h(t), f = new Pe({
|
|
464
|
+
iceServers: C
|
|
465
|
+
}), M = f.createDataChannel("JanusDataChannel");
|
|
466
|
+
if (!w)
|
|
467
467
|
throw new Error("Could not create session_id");
|
|
468
|
-
let
|
|
469
|
-
const c = () =>
|
|
468
|
+
let R = !1;
|
|
469
|
+
const c = () => R, g = () => {
|
|
470
470
|
var u;
|
|
471
|
-
|
|
472
|
-
}, v = Ae(
|
|
473
|
-
|
|
474
|
-
var
|
|
471
|
+
R = !0, (u = r.onConnectionStateChange) == null || u.call(r, D.Connected);
|
|
472
|
+
}, v = Ae(f, r.onVideoStateChange, a, c, g, !!t.stream_greeting);
|
|
473
|
+
f.onicecandidate = (u) => {
|
|
474
|
+
var p;
|
|
475
475
|
z("peerConnection.onicecandidate", u);
|
|
476
476
|
try {
|
|
477
|
-
u.candidate && u.candidate.sdpMid && u.candidate.sdpMLineIndex !== null ?
|
|
477
|
+
u.candidate && u.candidate.sdpMid && u.candidate.sdpMLineIndex !== null ? d(k, {
|
|
478
478
|
candidate: u.candidate.candidate,
|
|
479
479
|
sdpMid: u.candidate.sdpMid,
|
|
480
480
|
sdpMLineIndex: u.candidate.sdpMLineIndex
|
|
481
|
-
},
|
|
481
|
+
}, w) : d(k, {
|
|
482
482
|
candidate: null
|
|
483
|
-
},
|
|
484
|
-
} catch (
|
|
485
|
-
(
|
|
486
|
-
streamId:
|
|
483
|
+
}, w);
|
|
484
|
+
} catch (_) {
|
|
485
|
+
(p = r.onError) == null || p.call(r, _, {
|
|
486
|
+
streamId: k
|
|
487
487
|
});
|
|
488
488
|
}
|
|
489
|
-
},
|
|
490
|
-
!t.stream_warmup && !t.stream_greeting &&
|
|
491
|
-
},
|
|
492
|
-
var
|
|
493
|
-
z("peerConnection.oniceconnectionstatechange => " +
|
|
494
|
-
const u = X(
|
|
495
|
-
u !== D.Connected && ((
|
|
496
|
-
},
|
|
497
|
-
var
|
|
498
|
-
z("peerConnection.ontrack", u), (
|
|
499
|
-
}, await
|
|
500
|
-
const
|
|
501
|
-
return z("create answer OK"), await
|
|
489
|
+
}, M.onopen = () => {
|
|
490
|
+
!t.stream_warmup && !t.stream_greeting && g();
|
|
491
|
+
}, f.oniceconnectionstatechange = () => {
|
|
492
|
+
var p;
|
|
493
|
+
z("peerConnection.oniceconnectionstatechange => " + f.iceConnectionState);
|
|
494
|
+
const u = X(f.iceConnectionState);
|
|
495
|
+
u !== D.Connected && ((p = r.onConnectionStateChange) == null || p.call(r, u));
|
|
496
|
+
}, f.ontrack = (u) => {
|
|
497
|
+
var p;
|
|
498
|
+
z("peerConnection.ontrack", u), (p = r.onSrcObjectReady) == null || p.call(r, u.streams[0]);
|
|
499
|
+
}, await f.setRemoteDescription(y), z("set remote description OK");
|
|
500
|
+
const S = await f.createAnswer();
|
|
501
|
+
return z("create answer OK"), await f.setLocalDescription(S), z("set local description OK"), await o(k, S, w), z("start connection OK"), {
|
|
502
502
|
/**
|
|
503
503
|
* Method to send request to server to get clip or talk depend on you payload
|
|
504
504
|
* @param payload
|
|
505
505
|
*/
|
|
506
506
|
speak(u) {
|
|
507
|
-
return
|
|
507
|
+
return m(k, w, u);
|
|
508
508
|
},
|
|
509
509
|
/**
|
|
510
510
|
* Method to close RTC connection
|
|
511
511
|
*/
|
|
512
512
|
async disconnect() {
|
|
513
|
-
var u,
|
|
514
|
-
if (
|
|
515
|
-
const
|
|
516
|
-
if (
|
|
517
|
-
if (
|
|
513
|
+
var u, p;
|
|
514
|
+
if (k) {
|
|
515
|
+
const _ = X(f.iceConnectionState);
|
|
516
|
+
if (f) {
|
|
517
|
+
if (_ === D.New) {
|
|
518
518
|
(u = r.onVideoStateChange) == null || u.call(r, j.Stop), clearInterval(v);
|
|
519
519
|
return;
|
|
520
520
|
}
|
|
521
|
-
|
|
521
|
+
f.close(), f.oniceconnectionstatechange = null, f.onnegotiationneeded = null, f.onicecandidate = null, f.ontrack = null;
|
|
522
522
|
}
|
|
523
523
|
try {
|
|
524
|
-
|
|
524
|
+
_ === D.Connected && await l(k, w).catch((b) => {
|
|
525
525
|
});
|
|
526
526
|
} catch (b) {
|
|
527
527
|
z("Error on close stream connection", b);
|
|
528
528
|
}
|
|
529
|
-
(
|
|
529
|
+
(p = r.onVideoStateChange) == null || p.call(r, j.Stop), clearInterval(v);
|
|
530
530
|
}
|
|
531
531
|
},
|
|
532
532
|
/**
|
|
533
533
|
* Session identifier information, should be returned in the body of all streaming requests
|
|
534
534
|
*/
|
|
535
|
-
sessionId:
|
|
535
|
+
sessionId: w,
|
|
536
536
|
/**
|
|
537
537
|
* Id of current RTC stream
|
|
538
538
|
*/
|
|
539
|
-
streamId:
|
|
539
|
+
streamId: k
|
|
540
540
|
};
|
|
541
541
|
}
|
|
542
542
|
let U = {};
|
|
543
543
|
function ze(e) {
|
|
544
|
-
var i, a, o, l,
|
|
544
|
+
var i, a, o, m, l, h;
|
|
545
545
|
const t = window != null && window.hasOwnProperty("DID_AGENTS_API") ? "agents-ui" : "agents-sdk", s = e.agent.presenter, r = (i = e.agent.llm) == null ? void 0 : i.prompt_customization, n = {
|
|
546
546
|
token: e.token || "testKey",
|
|
547
547
|
distinct_id: e.distinctId || G(),
|
|
548
548
|
agentId: e.agent.id,
|
|
549
549
|
agentType: s.type === "clip" && s.presenter_id.startsWith("v2_") ? "clip_v2" : s.type,
|
|
550
550
|
owner_id: e.agent.owner_id ?? "",
|
|
551
|
+
promptVersion: (a = e.agent.llm) == null ? void 0 : a.prompt_version,
|
|
551
552
|
behavior: {
|
|
552
553
|
role: r == null ? void 0 : r.role,
|
|
553
554
|
personality: r == null ? void 0 : r.personality,
|
|
554
|
-
instructions: (
|
|
555
|
+
instructions: (o = e.agent.llm) == null ? void 0 : o.instructions
|
|
555
556
|
},
|
|
556
|
-
temperature: (
|
|
557
|
+
temperature: (m = e.agent.llm) == null ? void 0 : m.temperature,
|
|
557
558
|
knowledgeSource: r == null ? void 0 : r.knowledge_source,
|
|
558
|
-
starterQuestionsCount: (
|
|
559
|
+
starterQuestionsCount: (h = (l = e.agent.knowledge) == null ? void 0 : l.starter_message) == null ? void 0 : h.length,
|
|
559
560
|
topicsToAvoid: r == null ? void 0 : r.topics_to_avoid,
|
|
560
561
|
maxResponseLength: r == null ? void 0 : r.max_response_length
|
|
561
562
|
};
|
|
@@ -564,34 +565,34 @@ function ze(e) {
|
|
|
564
565
|
additionalProperties: {},
|
|
565
566
|
isEnabled: e.isEnabled ?? !0,
|
|
566
567
|
getRandom: () => Math.random().toString(16).slice(2),
|
|
567
|
-
enrich(
|
|
568
|
-
const
|
|
569
|
-
if (
|
|
568
|
+
enrich(d) {
|
|
569
|
+
const k = {};
|
|
570
|
+
if (d && typeof d != "object")
|
|
570
571
|
throw new Error("properties must be a flat json object");
|
|
571
|
-
for (let
|
|
572
|
-
(typeof
|
|
572
|
+
for (let y in d)
|
|
573
|
+
(typeof d[y] == "string" || typeof d[y] == "number") && (k[y] = d[y]);
|
|
573
574
|
this.additionalProperties = {
|
|
574
575
|
...this.additionalProperties,
|
|
575
|
-
...
|
|
576
|
+
...k
|
|
576
577
|
};
|
|
577
578
|
},
|
|
578
|
-
track(
|
|
579
|
+
track(d, k) {
|
|
579
580
|
if (!this.isEnabled)
|
|
580
581
|
return Promise.resolve();
|
|
581
582
|
const {
|
|
582
|
-
audioPath:
|
|
583
|
-
...
|
|
584
|
-
} =
|
|
583
|
+
audioPath: y,
|
|
584
|
+
...C
|
|
585
|
+
} = k || {}, w = {
|
|
585
586
|
method: "POST",
|
|
586
587
|
headers: {
|
|
587
588
|
"Content-Type": "application/x-www-form-urlencoded"
|
|
588
589
|
},
|
|
589
590
|
body: new URLSearchParams({
|
|
590
591
|
data: JSON.stringify([{
|
|
591
|
-
event:
|
|
592
|
+
event: d,
|
|
592
593
|
properties: {
|
|
593
594
|
...this.additionalProperties,
|
|
594
|
-
...
|
|
595
|
+
...C,
|
|
595
596
|
...n,
|
|
596
597
|
source: t,
|
|
597
598
|
time: Date.now(),
|
|
@@ -604,23 +605,23 @@ function ze(e) {
|
|
|
604
605
|
}])
|
|
605
606
|
})
|
|
606
607
|
};
|
|
607
|
-
return fetch("https://api-js.mixpanel.com/track/?verbose=1&ip=1",
|
|
608
|
+
return fetch("https://api-js.mixpanel.com/track/?verbose=1&ip=1", w).then((f) => f.json()).catch((f) => console.error(f));
|
|
608
609
|
},
|
|
609
|
-
linkTrack(
|
|
610
|
-
U[
|
|
610
|
+
linkTrack(d, k, y, C) {
|
|
611
|
+
U[d] || (U[d] = {
|
|
611
612
|
events: {},
|
|
612
613
|
resolvedDependencies: []
|
|
613
|
-
}),
|
|
614
|
-
const
|
|
615
|
-
if (
|
|
616
|
-
props:
|
|
617
|
-
},
|
|
618
|
-
const
|
|
614
|
+
}), C.includes(y) || C.push(y);
|
|
615
|
+
const w = U[d];
|
|
616
|
+
if (w.events[y] = {
|
|
617
|
+
props: k
|
|
618
|
+
}, w.resolvedDependencies.push(y), C.every((M) => w.resolvedDependencies.includes(M))) {
|
|
619
|
+
const M = C.reduce((R, c) => w.events[c] ? {
|
|
619
620
|
...R,
|
|
620
|
-
...
|
|
621
|
+
...w.events[c].props
|
|
621
622
|
} : R, {});
|
|
622
|
-
this.track(
|
|
623
|
-
delete
|
|
623
|
+
this.track(d, M), w.resolvedDependencies = w.resolvedDependencies.filter((R) => !C.includes(R)), C.forEach((R) => {
|
|
624
|
+
delete w.events[R];
|
|
624
625
|
});
|
|
625
626
|
}
|
|
626
627
|
}
|
|
@@ -629,8 +630,8 @@ function ze(e) {
|
|
|
629
630
|
function Le(e) {
|
|
630
631
|
var n, i, a, o;
|
|
631
632
|
const t = () => /Mobi|Android/i.test(navigator.userAgent) ? "Mobile" : "Desktop", s = () => {
|
|
632
|
-
const
|
|
633
|
-
return
|
|
633
|
+
const m = navigator.platform;
|
|
634
|
+
return m.toLowerCase().includes("win") ? "Windows" : m.toLowerCase().includes("mac") ? "Mac OS X" : m.toLowerCase().includes("linux") ? "Linux" : "Unknown";
|
|
634
635
|
}, r = e.presenter;
|
|
635
636
|
return {
|
|
636
637
|
$os: `${s()}`,
|
|
@@ -645,7 +646,7 @@ function Le(e) {
|
|
|
645
646
|
};
|
|
646
647
|
}
|
|
647
648
|
function je(e, t, s) {
|
|
648
|
-
var
|
|
649
|
+
var l, h;
|
|
649
650
|
const {
|
|
650
651
|
event: r,
|
|
651
652
|
...n
|
|
@@ -653,7 +654,7 @@ function je(e, t, s) {
|
|
|
653
654
|
template: i
|
|
654
655
|
} = (t == null ? void 0 : t.llm) || {}, {
|
|
655
656
|
language: a
|
|
656
|
-
} = ((
|
|
657
|
+
} = ((l = t == null ? void 0 : t.presenter) == null ? void 0 : l.voice) || {}, {
|
|
657
658
|
stitch: o
|
|
658
659
|
} = (t == null ? void 0 : t.presenter) || {};
|
|
659
660
|
return {
|
|
@@ -665,7 +666,7 @@ function je(e, t, s) {
|
|
|
665
666
|
script: {
|
|
666
667
|
...n.script,
|
|
667
668
|
provider: {
|
|
668
|
-
...(
|
|
669
|
+
...(h = n == null ? void 0 : n.script) == null ? void 0 : h.provider,
|
|
669
670
|
language: a
|
|
670
671
|
}
|
|
671
672
|
},
|
|
@@ -728,47 +729,47 @@ async function ae(e, t, s, r, n) {
|
|
|
728
729
|
}
|
|
729
730
|
function Fe(e, t, s, r, n, i) {
|
|
730
731
|
return new Promise(async (a, o) => {
|
|
731
|
-
var
|
|
732
|
+
var d, k, y, C, w;
|
|
732
733
|
L = 0;
|
|
733
|
-
const
|
|
734
|
+
const m = String(t.mode);
|
|
734
735
|
if (!n && t.mode !== I.DirectPlayback)
|
|
735
736
|
try {
|
|
736
737
|
n = await ae(e.id, s, r, t.mode, t.persistentChat);
|
|
737
|
-
} catch (
|
|
738
|
-
return o(
|
|
738
|
+
} catch (f) {
|
|
739
|
+
return o(f);
|
|
739
740
|
}
|
|
740
|
-
const
|
|
741
|
-
if (
|
|
741
|
+
const l = (n == null ? void 0 : n.chat_mode) || m;
|
|
742
|
+
if (l !== m && (t.mode = l, (k = (d = t.callbacks).onModeChange) == null || k.call(d, l), l === I.TextOnly && ((C = (y = t.callbacks) == null ? void 0 : y.onError) == null || C.call(y, new xe({
|
|
742
743
|
kind: "ChatModeDowngraded",
|
|
743
|
-
description: `Chat mode changed from ${
|
|
744
|
-
}), {}))),
|
|
744
|
+
description: `Chat mode changed from ${m} to ${l} when creating the chat`
|
|
745
|
+
}), {}))), l === I.TextOnly)
|
|
745
746
|
return a({
|
|
746
747
|
chat: n
|
|
747
748
|
});
|
|
748
|
-
const
|
|
749
|
+
const h = await Te(e.id, Be(e, t, i), {
|
|
749
750
|
...t,
|
|
750
751
|
analytics: r,
|
|
751
|
-
warmup: (
|
|
752
|
+
warmup: (w = t.streamOptions) == null ? void 0 : w.streamWarmup,
|
|
752
753
|
callbacks: {
|
|
753
754
|
...t.callbacks,
|
|
754
|
-
onConnectionStateChange: async (
|
|
755
|
-
var
|
|
756
|
-
|
|
755
|
+
onConnectionStateChange: async (f) => {
|
|
756
|
+
var M, R, c, g;
|
|
757
|
+
f === D.Connected ? h ? ((R = (M = t.callbacks).onConnectionStateChange) == null || R.call(M, f), a({
|
|
757
758
|
chat: n,
|
|
758
|
-
streamingManager:
|
|
759
|
-
})) : n && o(new Error("Something went wrong while initializing the manager")) : (
|
|
759
|
+
streamingManager: h
|
|
760
|
+
})) : n && o(new Error("Something went wrong while initializing the manager")) : (g = (c = t.callbacks).onConnectionStateChange) == null || g.call(c, f);
|
|
760
761
|
},
|
|
761
|
-
onVideoStateChange(
|
|
762
|
-
var
|
|
763
|
-
(c = (
|
|
762
|
+
onVideoStateChange(f, M) {
|
|
763
|
+
var R, c;
|
|
764
|
+
(c = (R = t.callbacks).onVideoStateChange) == null || c.call(R, f), L > 0 && (f === j.Start ? r.linkTrack("agent-video", {
|
|
764
765
|
event: "start",
|
|
765
766
|
latency: Date.now() - L
|
|
766
|
-
}, "start", [
|
|
767
|
+
}, "start", [V.StreamVideoCreated]) : f === j.Stop && r.linkTrack("agent-video", {
|
|
767
768
|
event: "stop",
|
|
768
769
|
is_greenscreen: e.presenter.type === "clip" && e.presenter.is_greenscreen,
|
|
769
770
|
background: e.presenter.type === "clip" && e.presenter.background,
|
|
770
|
-
...
|
|
771
|
-
}, "done", [
|
|
771
|
+
...M
|
|
772
|
+
}, "done", [V.StreamVideoDone]));
|
|
772
773
|
}
|
|
773
774
|
}
|
|
774
775
|
}).catch(o);
|
|
@@ -786,7 +787,7 @@ function He(e) {
|
|
|
786
787
|
function Y(e, t) {
|
|
787
788
|
return t && t.length > 0 ? t : [{
|
|
788
789
|
content: e,
|
|
789
|
-
id:
|
|
790
|
+
id: K(),
|
|
790
791
|
role: "assistant",
|
|
791
792
|
created_at: (/* @__PURE__ */ new Date()).toISOString()
|
|
792
793
|
}];
|
|
@@ -799,7 +800,7 @@ function We(e) {
|
|
|
799
800
|
s += e[t], t++;
|
|
800
801
|
return s;
|
|
801
802
|
}
|
|
802
|
-
function
|
|
803
|
+
function Ve(e, t, s, r, n) {
|
|
803
804
|
if (!(e === B.Partial || e === B.Answer))
|
|
804
805
|
return;
|
|
805
806
|
const i = r.messages[r.messages.length - 1];
|
|
@@ -810,61 +811,61 @@ function Ke(e, t, s, r, n) {
|
|
|
810
811
|
sequence: o
|
|
811
812
|
} = t;
|
|
812
813
|
e === B.Partial ? s[o] = a : s.answer = a;
|
|
813
|
-
const
|
|
814
|
-
(i.content !==
|
|
814
|
+
const m = We(s);
|
|
815
|
+
(i.content !== m || e === B.Answer) && (i.content = m, n == null || n([...r.messages], e));
|
|
815
816
|
}
|
|
816
|
-
async function
|
|
817
|
-
var
|
|
817
|
+
async function Ke(e, t) {
|
|
818
|
+
var f, M, R;
|
|
818
819
|
let s = {}, r = !0;
|
|
819
820
|
const n = {
|
|
820
821
|
messages: [],
|
|
821
822
|
chatMode: t.mode || I.Functional
|
|
822
|
-
}, i = t.baseURL || H, a = t.wsURL || he, o = t.mixpanelKey || we,
|
|
823
|
-
n.messages = Y(
|
|
824
|
-
const
|
|
823
|
+
}, i = t.baseURL || H, a = t.wsURL || he, o = t.mixpanelKey || we, m = te(t.auth, i, t.callbacks.onError), l = await m.getById(e), h = He(l);
|
|
824
|
+
n.messages = Y(h, t.initialMessages), (M = (f = t.callbacks).onNewMessage) == null || M.call(f, [...n.messages], "answer");
|
|
825
|
+
const d = ze({
|
|
825
826
|
token: o,
|
|
826
|
-
agent:
|
|
827
|
+
agent: l,
|
|
827
828
|
isEnabled: t.enableAnalitics,
|
|
828
829
|
distinctId: t.distinctId
|
|
829
830
|
});
|
|
830
|
-
|
|
831
|
+
d.track("agent-sdk", {
|
|
831
832
|
event: "loaded",
|
|
832
|
-
...Le(
|
|
833
|
+
...Le(l)
|
|
833
834
|
});
|
|
834
|
-
const
|
|
835
|
-
onMessage: (c,
|
|
836
|
-
var v,
|
|
837
|
-
if ("content" in
|
|
838
|
-
|
|
835
|
+
const k = {
|
|
836
|
+
onMessage: (c, g) => {
|
|
837
|
+
var v, S;
|
|
838
|
+
if ("content" in g)
|
|
839
|
+
Ve(c, g, s, n, t.callbacks.onNewMessage), c === B.Answer && d.track("agent-message-received", {
|
|
839
840
|
messages: n.messages.length,
|
|
840
841
|
mode: n.chatMode
|
|
841
842
|
});
|
|
842
843
|
else {
|
|
843
|
-
const u =
|
|
844
|
+
const u = V, p = [u.StreamVideoDone, u.StreamVideoError, u.StreamVideoRejected], _ = [u.StreamFailed, u.StreamVideoError, u.StreamVideoRejected], b = je(g, l, {
|
|
844
845
|
mode: n.chatMode
|
|
845
846
|
});
|
|
846
847
|
if (c = c, c === u.StreamVideoCreated)
|
|
847
|
-
|
|
848
|
-
else if (
|
|
848
|
+
d.linkTrack("agent-video", b, u.StreamVideoCreated, ["start"]);
|
|
849
|
+
else if (p.includes(c)) {
|
|
849
850
|
const E = c.split("/")[1];
|
|
850
|
-
|
|
851
|
+
_.includes(c) ? d.track("agent-video", {
|
|
851
852
|
...b,
|
|
852
853
|
event: E
|
|
853
|
-
}) :
|
|
854
|
+
}) : d.linkTrack("agent-video", {
|
|
854
855
|
...b,
|
|
855
856
|
event: E
|
|
856
857
|
}, c, ["done"]);
|
|
857
858
|
}
|
|
858
|
-
|
|
859
|
-
data:
|
|
860
|
-
})),
|
|
859
|
+
_.includes(c) && ((S = (v = t.callbacks).onError) == null || S.call(v, new Error(`Stream failed with event ${c}`), {
|
|
860
|
+
data: g
|
|
861
|
+
})), g.event === u.StreamDone && C();
|
|
861
862
|
}
|
|
862
863
|
}
|
|
863
864
|
};
|
|
864
|
-
async function
|
|
865
|
-
var
|
|
866
|
-
(b = (
|
|
867
|
-
const
|
|
865
|
+
async function y(c) {
|
|
866
|
+
var _, b, E, A, F, T, P;
|
|
867
|
+
(b = (_ = t.callbacks).onConnectionStateChange) == null || b.call(_, D.Connecting), L = 0, c && !r && (delete n.chat, n.messages = Y(h), (A = (E = t.callbacks).onNewMessage) == null || A.call(E, [...n.messages], "answer"));
|
|
868
|
+
const g = t.mode === I.DirectPlayback ? Promise.resolve(void 0) : Re(t.auth, a, k), v = Q(() => Fe(l, t, m, d, n.chat, c ? h : void 0), {
|
|
868
869
|
limit: 3,
|
|
869
870
|
timeout: Ne,
|
|
870
871
|
timeoutErrorMessage: "Timeout initializing the stream",
|
|
@@ -873,59 +874,59 @@ async function Ve(e, t) {
|
|
|
873
874
|
delayMs: 1e3
|
|
874
875
|
}).catch(($) => {
|
|
875
876
|
var x, N;
|
|
876
|
-
throw
|
|
877
|
-
}), [
|
|
877
|
+
throw w(I.Maintenance), (N = (x = t.callbacks).onConnectionStateChange) == null || N.call(x, D.Fail), $;
|
|
878
|
+
}), [S, {
|
|
878
879
|
streamingManager: u,
|
|
879
|
-
chat:
|
|
880
|
-
}] = await Promise.all([
|
|
881
|
-
|
|
880
|
+
chat: p
|
|
881
|
+
}] = await Promise.all([g, v]);
|
|
882
|
+
p && p.id !== ((F = n.chat) == null ? void 0 : F.id) && ((P = (T = t.callbacks).onNewChat) == null || P.call(T, p.id)), n.streamingManager = u, n.socketManager = S, n.chat = p, r = !1, w((p == null ? void 0 : p.chat_mode) ?? t.mode ?? I.Functional);
|
|
882
883
|
}
|
|
883
|
-
async function
|
|
884
|
-
var c,
|
|
885
|
-
(c = n.socketManager) == null || c.disconnect(), await ((
|
|
884
|
+
async function C() {
|
|
885
|
+
var c, g, v, S;
|
|
886
|
+
(c = n.socketManager) == null || c.disconnect(), await ((g = n.streamingManager) == null ? void 0 : g.disconnect()), delete n.streamingManager, delete n.socketManager, (S = (v = t.callbacks).onConnectionStateChange) == null || S.call(v, D.Disconnected);
|
|
886
887
|
}
|
|
887
|
-
async function
|
|
888
|
-
var
|
|
889
|
-
c !== n.chatMode && (
|
|
888
|
+
async function w(c) {
|
|
889
|
+
var g, v;
|
|
890
|
+
c !== n.chatMode && (d.track("agent-mode-change", {
|
|
890
891
|
mode: c
|
|
891
|
-
}), n.chatMode = c, n.chatMode !== I.Functional && await
|
|
892
|
+
}), n.chatMode = c, n.chatMode !== I.Functional && await C(), (v = (g = t.callbacks).onModeChange) == null || v.call(g, c));
|
|
892
893
|
}
|
|
893
894
|
return {
|
|
894
|
-
agent:
|
|
895
|
-
starterMessages: ((
|
|
896
|
-
getSTTToken: () =>
|
|
897
|
-
changeMode:
|
|
898
|
-
enrichAnalytics:
|
|
895
|
+
agent: l,
|
|
896
|
+
starterMessages: ((R = l.knowledge) == null ? void 0 : R.starter_message) || [],
|
|
897
|
+
getSTTToken: () => m.getSTTToken(l.id),
|
|
898
|
+
changeMode: w,
|
|
899
|
+
enrichAnalytics: d.enrich,
|
|
899
900
|
async connect() {
|
|
900
901
|
var c;
|
|
901
|
-
await
|
|
902
|
+
await y(!0), d.track("agent-chat", {
|
|
902
903
|
event: "connect",
|
|
903
904
|
chatId: (c = n.chat) == null ? void 0 : c.id,
|
|
904
|
-
agentId:
|
|
905
|
+
agentId: l.id,
|
|
905
906
|
mode: n.chatMode
|
|
906
907
|
});
|
|
907
908
|
},
|
|
908
909
|
async reconnect() {
|
|
909
910
|
var c;
|
|
910
|
-
await
|
|
911
|
+
await C(), await y(!1), d.track("agent-chat", {
|
|
911
912
|
event: "reconnect",
|
|
912
913
|
chatId: (c = n.chat) == null ? void 0 : c.id,
|
|
913
|
-
agentId:
|
|
914
|
+
agentId: l.id,
|
|
914
915
|
mode: n.chatMode
|
|
915
916
|
});
|
|
916
917
|
},
|
|
917
918
|
async disconnect() {
|
|
918
919
|
var c;
|
|
919
|
-
await
|
|
920
|
+
await C(), d.track("agent-chat", {
|
|
920
921
|
event: "disconnect",
|
|
921
922
|
chatId: (c = n.chat) == null ? void 0 : c.id,
|
|
922
|
-
agentId:
|
|
923
|
+
agentId: l.id,
|
|
923
924
|
mode: n.chatMode
|
|
924
925
|
});
|
|
925
926
|
},
|
|
926
927
|
async chat(c) {
|
|
927
|
-
var v,
|
|
928
|
-
const
|
|
928
|
+
var v, S, u, p, _, b;
|
|
929
|
+
const g = K();
|
|
929
930
|
s = {};
|
|
930
931
|
try {
|
|
931
932
|
if (L = Date.now(), t.mode === I.DirectPlayback)
|
|
@@ -943,13 +944,13 @@ async function Ve(e, t) {
|
|
|
943
944
|
} else
|
|
944
945
|
throw new Error("Streaming manager is not initialized");
|
|
945
946
|
n.messages.push({
|
|
946
|
-
id:
|
|
947
|
+
id: K(),
|
|
947
948
|
role: "user",
|
|
948
949
|
content: c,
|
|
949
950
|
created_at: new Date(L).toISOString()
|
|
950
|
-
}), (
|
|
951
|
+
}), (S = (v = t.callbacks).onNewMessage) == null || S.call(v, [...n.messages], "user"), n.chat || (n.chat = await ae(l.id, m, d, n.chatMode, t.persistentChat), (p = (u = t.callbacks).onNewChat) == null || p.call(u, n.chat.id));
|
|
951
952
|
const E = {
|
|
952
|
-
id:
|
|
953
|
+
id: g,
|
|
953
954
|
role: "assistant",
|
|
954
955
|
content: "",
|
|
955
956
|
created_at: (/* @__PURE__ */ new Date()).toISOString(),
|
|
@@ -958,7 +959,7 @@ async function Ve(e, t) {
|
|
|
958
959
|
n.messages.push(E);
|
|
959
960
|
const F = (P) => {
|
|
960
961
|
var $, x;
|
|
961
|
-
return
|
|
962
|
+
return m.chat(l.id, P, {
|
|
962
963
|
sessionId: ($ = n.streamingManager) == null ? void 0 : $.sessionId,
|
|
963
964
|
streamId: (x = n.streamingManager) == null ? void 0 : x.streamId,
|
|
964
965
|
chatMode: n.chatMode,
|
|
@@ -975,93 +976,93 @@ async function Ve(e, t) {
|
|
|
975
976
|
const $ = (N = P == null ? void 0 : P.message) == null ? void 0 : N.includes("missing or invalid session_id");
|
|
976
977
|
if (!((W = P == null ? void 0 : P.message) == null ? void 0 : W.includes("Stream Error")) && !$)
|
|
977
978
|
throw (O = (J = t.callbacks).onError) == null || O.call(J, P, {}), P;
|
|
978
|
-
return await
|
|
979
|
+
return await C(), await y(!1), F(n.chat.id);
|
|
979
980
|
});
|
|
980
|
-
return
|
|
981
|
+
return d.track("agent-message-send", {
|
|
981
982
|
event: "success",
|
|
982
983
|
mode: n.chatMode,
|
|
983
984
|
messages: n.messages.length + 1
|
|
984
|
-
}), E.context = T.context, E.matches = T.matches, T.result && (E.content = T.result,
|
|
985
|
+
}), E.context = T.context, E.matches = T.matches, T.result && (E.content = T.result, d.track("agent-message-received", {
|
|
985
986
|
latency: Date.now() - L,
|
|
986
987
|
mode: n.chatMode,
|
|
987
988
|
messages: n.messages.length
|
|
988
|
-
}), (b = (
|
|
989
|
+
}), (b = (_ = t.callbacks).onNewMessage) == null || b.call(_, [...n.messages], "answer")), T;
|
|
989
990
|
} catch (E) {
|
|
990
|
-
throw n.messages[n.messages.length - 1].id ===
|
|
991
|
+
throw n.messages[n.messages.length - 1].id === g && n.messages.pop(), d.track("agent-message-send", {
|
|
991
992
|
event: "error",
|
|
992
993
|
mode: n.chatMode,
|
|
993
994
|
messages: n.messages.length
|
|
994
995
|
}), E;
|
|
995
996
|
}
|
|
996
997
|
},
|
|
997
|
-
rate(c,
|
|
998
|
-
var
|
|
999
|
-
const
|
|
998
|
+
rate(c, g, v) {
|
|
999
|
+
var p, _, b, E;
|
|
1000
|
+
const S = n.messages.find((A) => A.id === c);
|
|
1000
1001
|
if (n.chat) {
|
|
1001
|
-
if (!
|
|
1002
|
+
if (!S)
|
|
1002
1003
|
throw new Error("Message not found");
|
|
1003
1004
|
} else
|
|
1004
1005
|
throw new Error("Chat is not initialized");
|
|
1005
|
-
const u = ((
|
|
1006
|
-
return
|
|
1006
|
+
const u = ((p = S.matches) == null ? void 0 : p.map((A) => [A.document_id, A.id])) ?? [];
|
|
1007
|
+
return d.track("agent-rate", {
|
|
1007
1008
|
event: v ? "update" : "create",
|
|
1008
|
-
thumb:
|
|
1009
|
-
knowledge_id: ((
|
|
1009
|
+
thumb: g === 1 ? "up" : "down",
|
|
1010
|
+
knowledge_id: ((_ = l.knowledge) == null ? void 0 : _.id) ?? "",
|
|
1010
1011
|
mode: n.chatMode,
|
|
1011
1012
|
matches: u,
|
|
1012
|
-
score:
|
|
1013
|
-
}), v ?
|
|
1014
|
-
knowledge_id: ((b =
|
|
1013
|
+
score: g
|
|
1014
|
+
}), v ? m.updateRating(l.id, n.chat.id, v, {
|
|
1015
|
+
knowledge_id: ((b = l.knowledge) == null ? void 0 : b.id) ?? "",
|
|
1015
1016
|
message_id: c,
|
|
1016
1017
|
matches: u,
|
|
1017
|
-
score:
|
|
1018
|
-
}) :
|
|
1019
|
-
knowledge_id: ((E =
|
|
1018
|
+
score: g
|
|
1019
|
+
}) : m.createRating(l.id, n.chat.id, {
|
|
1020
|
+
knowledge_id: ((E = l.knowledge) == null ? void 0 : E.id) ?? "",
|
|
1020
1021
|
message_id: c,
|
|
1021
1022
|
matches: u,
|
|
1022
|
-
score:
|
|
1023
|
+
score: g
|
|
1023
1024
|
});
|
|
1024
1025
|
},
|
|
1025
1026
|
deleteRate(c) {
|
|
1026
|
-
var
|
|
1027
|
+
var g;
|
|
1027
1028
|
if (!n.chat)
|
|
1028
1029
|
throw new Error("Chat is not initialized");
|
|
1029
|
-
return
|
|
1030
|
+
return d.track("agent-rate-delete", {
|
|
1030
1031
|
type: "text",
|
|
1031
|
-
chat_id: (
|
|
1032
|
+
chat_id: (g = n.chat) == null ? void 0 : g.id,
|
|
1032
1033
|
id: c,
|
|
1033
1034
|
mode: n.chatMode
|
|
1034
|
-
}),
|
|
1035
|
+
}), m.deleteRating(l.id, n.chat.id, c);
|
|
1035
1036
|
},
|
|
1036
1037
|
speak(c) {
|
|
1037
1038
|
if (!n.streamingManager)
|
|
1038
1039
|
throw new Error("Please connect to the agent first");
|
|
1039
1040
|
L = Date.now();
|
|
1040
|
-
function
|
|
1041
|
+
function g() {
|
|
1041
1042
|
if (typeof c == "string") {
|
|
1042
|
-
if (!
|
|
1043
|
+
if (!l.presenter.voice)
|
|
1043
1044
|
throw new Error("Presenter voice is not initialized");
|
|
1044
1045
|
return {
|
|
1045
1046
|
type: "text",
|
|
1046
|
-
provider:
|
|
1047
|
+
provider: l.presenter.voice,
|
|
1047
1048
|
input: c,
|
|
1048
1049
|
ssml: !1
|
|
1049
1050
|
};
|
|
1050
1051
|
}
|
|
1051
1052
|
if (c.type === "text" && !c.provider) {
|
|
1052
|
-
if (!
|
|
1053
|
+
if (!l.presenter.voice)
|
|
1053
1054
|
throw new Error("Presenter voice is not initialized");
|
|
1054
1055
|
return {
|
|
1055
1056
|
type: "text",
|
|
1056
|
-
provider:
|
|
1057
|
+
provider: l.presenter.voice,
|
|
1057
1058
|
input: c.input,
|
|
1058
1059
|
ssml: c.ssml
|
|
1059
1060
|
};
|
|
1060
1061
|
}
|
|
1061
1062
|
return c;
|
|
1062
1063
|
}
|
|
1063
|
-
const v =
|
|
1064
|
-
return
|
|
1064
|
+
const v = g();
|
|
1065
|
+
return d.track("agent-speak", v), n.streamingManager.speak({
|
|
1065
1066
|
script: v
|
|
1066
1067
|
});
|
|
1067
1068
|
}
|
|
@@ -1083,13 +1084,13 @@ export {
|
|
|
1083
1084
|
se as PlanGroup,
|
|
1084
1085
|
fe as Providers,
|
|
1085
1086
|
ce as RateState,
|
|
1086
|
-
|
|
1087
|
+
V as StreamEvents,
|
|
1087
1088
|
j as StreamingState,
|
|
1088
1089
|
de as Subject,
|
|
1089
1090
|
ie as UserPlan,
|
|
1090
1091
|
Z as VideoType,
|
|
1091
1092
|
ge as VoiceAccess,
|
|
1092
|
-
|
|
1093
|
+
Ke as createAgentManager,
|
|
1093
1094
|
Ue as getAgent,
|
|
1094
1095
|
ue as mapVideoType
|
|
1095
1096
|
};
|
package/dist/index.umd.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
(function(p,F){typeof exports=="object"&&typeof module<"u"?F(exports):typeof define=="function"&&define.amd?define(["exports"],F):(p=typeof globalThis<"u"?globalThis:p||self,F(p.index={}))})(this,function(p){"use strict";var F=(e=>(e.TRIAL="trial",e.BASIC="basic",e.ENTERPRISE="enterprise",e.LITE="lite",e.ADVANCED="advanced",e))(F||{}),Y=(e=>(e.TRIAL="deid-trial",e.PRO="deid-pro",e.ENTERPRISE="deid-enterprise",e.LITE="deid-lite",e.ADVANCED="deid-advanced",e.BUILD="deid-api-build",e.LAUNCH="deid-api-launch",e.SCALE="deid-api-scale",e))(Y||{}),Z=(e=>(e.Created="created",e.Started="started",e.Done="done",e.Error="error",e.Rejected="rejected",e.Ready="ready",e))(Z||{}),G=(e=>(e.Unrated="Unrated",e.Positive="Positive",e.Negative="Negative",e))(G||{}),E=(e=>(e.Functional="Functional",e.TextOnly="TextOnly",e.Maintenance="Maintenance",e.Playground="Playground",e.DirectPlayback="DirectPlayback",e))(E||{}),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||{}),ee=(e=>(e.Knowledge="knowledge",e.Document="document",e.Record="record",e))(ee||{}),te=(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||{}),J=(e=>(e.Clip="clip",e.Talk="talk",e))(J||{});const ne=e=>{switch(e){case"clip":return"clip";case"talk":return"talk";default:throw new Error(`Unknown video type: ${e}`)}};var j=(e=>(e.Start="START",e.Stop="STOP",e))(j||{}),W=(e=>(e.ChatAnswer="chat/answer",e.ChatPartial="chat/partial",e.StreamDone="stream/done",e.StreamStarted="stream/started",e.StreamFailed="stream/error",e.StreamReady="stream/ready",e.StreamCreated="stream/created",e.StreamVideoCreated="stream-video/started",e.StreamVideoDone="stream-video/done",e.StreamVideoError="stream-video/error",e.StreamVideoRejected="stream-video/rejected",e))(W||{}),S=(e=>(e.New="new",e.Fail="fail",e.Connected="connected",e.Connecting="connecting",e.Closed="closed",e.Completed="completed",e.Disconnected="disconnected",e))(S||{}),re=(e=>(e.Amazon="amazon",e.Microsoft="microsoft",e.Afflorithmics="afflorithmics",e.Elevenlabs="elevenlabs",e))(re||{}),ae=(e=>(e.Public="public",e.Premium="premium",e.Private="private",e))(ae||{});const K="https://api.d-id.com",we="wss://notifications.d-id.com",ye="79f81a83a67430be2bc0fd61042b8faa";async function ie(e,t){const s={limit:(t==null?void 0:t.limit)??3,delayMs:(t==null?void 0:t.delayMs)??0,timeout:(t==null?void 0:t.timeout)??3e4,timeoutErrorMessage:(t==null?void 0:t.timeoutErrorMessage)||"Timeout error",shouldRetryFn:(t==null?void 0:t.shouldRetryFn)??function(){return!0}};let r;for(let n=1;n<=s.limit;n++)try{if(!s.timeout)return await e();let i;const a=new Promise((d,f)=>{e().then(m=>{clearTimeout(i),d(m)}).catch(m=>{clearTimeout(i),f(m)})}),o=new Promise((d,f)=>{i=setTimeout(()=>f(new Error(s.timeoutErrorMessage)),s.timeout)});return await Promise.race([a,o])}catch(i){if(r=i,!s.shouldRetryFn(i)||n>=s.limit)throw i;s.delayMs>0&&await new Promise(a=>setTimeout(a,s.delayMs))}throw r}const x=()=>Math.random().toString(16).slice(2);function se(){let e=window.localStorage.getItem("did_external_key_id");return e||(e=Math.random().toString(16).slice(2),window.localStorage.setItem("did_external_key_id",e)),e}let pe=x();function oe(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}.${se()}_${pe}`;throw new Error(`Unknown auth type: ${e}`)}const ve=e=>ie(e,{limit:3,delayMs:1e3,timeout:0,shouldRetryFn:t=>t.status===429});function O(e,t=K,s){const r=async(n,i)=>{const{skipErrorHandler:a,...o}=i||{},l=await ve(()=>fetch(t+(n!=null&&n.startsWith("/")?n:`/${n}`),{...o,headers:{...o.headers,Authorization:oe(e),"Content-Type":"application/json"}}));if(!l.ok){let d=await l.text().catch(()=>"Failed to fetch");throw s&&!a&&s(new Error(d),{url:n,options:o,headers:l.headers}),new Error(d)}return l.json()};return{get(n,i){return r(n,{...i,method:"GET"})},post(n,i,a){return r(n,{...a,body:JSON.stringify(i),method:"POST"})},delete(n,i,a){return r(n,{...a,body:JSON.stringify(i),method:"DELETE"})},patch(n,i,a){return r(n,{...a,body:JSON.stringify(i),method:"PATCH"})}}}function ce(e,t=K,s){const r=O(e,`${t}/agents`,s);return{create(n,i){return r.post("/",n,i)},getAgents(n,i){return r.get(`/${n?`?tag=${n}`:""}`,i).then(a=>a??[])},getById(n,i){return r.get(`/${n}`,i)},delete(n,i){return r.delete(`/${n}`,void 0,i)},update(n,i,a){return r.patch(`/${n}`,i,a)},newChat(n,i,a){return r.post(`/${n}/chat`,i,a)},chat(n,i,a,o){return r.post(`/${n}/chat/${i}`,a,o)},createRating(n,i,a,o){return r.post(`/${n}/chat/${i}/ratings`,a,o)},updateRating(n,i,a,o,l){return r.patch(`/${n}/chat/${i}/ratings/${a}`,o,l)},deleteRating(n,i,a,o){return r.delete(`/${n}/chat/${i}/ratings/${a}`,o)},getSTTToken(n,i){return r.get(`/${n}/stt-token`,i)}}}const ke=e=>new Promise(t=>setTimeout(t,e));function Ce(e){return new Promise((t,s)=>{const{callbacks:r,host:n,auth:i}=e,{onMessage:a=null,onOpen:o=null,onClose:l=null,onError:d=null}=r||{},f=new WebSocket(`${n}?authorization=${oe(i)}`);f.onmessage=a,f.onclose=l,f.onerror=m=>{console.error(m),d==null||d("Websocket failed to connect",m),s(m)},f.onopen=m=>{o==null||o(m),t(f)}})}async function Re(e){const{retries:t=1}=e;let s=null;for(let r=0;(s==null?void 0:s.readyState)!==WebSocket.OPEN;r++)try{s=await Ce(e)}catch(n){if(r===t)throw n;await ke(r*500)}return s}async function Se(e,t,s){const r=s!=null&&s.onMessage?[s.onMessage]:[],n=await Re({auth:e,host:t,callbacks:{onError:s==null?void 0:s.onError,onMessage:i=>{const a=JSON.parse(i.data);r.forEach(o=>o(a.event,a))}}});return{socket:n,disconnect:()=>n.close(),subscribeToEvents:i=>r.push(i)}}const De="X-Playground-Chat";function _e(e,t,s,r){const n=O(e,`${t}/agents/${s}`,r);return{createStream(i){return n.post("/streams",{output_resolution:i.output_resolution,compatibility_mode:i.compatibility_mode,stream_warmup:i.stream_warmup,session_timeout:i.session_timeout,stream_greeting:i.stream_greeting})},startConnection(i,a,o){return n.post(`/streams/${i}/sdp`,{session_id:o,answer:a})},addIceCandidate(i,a,o){return n.post(`/streams/${i}/ice`,{session_id:o,...a})},sendStreamRequest(i,a,o){return n.post(`/streams/${i}`,{session_id:a,...o})},close(i,a){return n.delete(`/streams/${i}`,{session_id:a})}}}function Me(e,t,s,r){const n=O(e,`${t}/agents/${s}`,r);return{createStream(i,a){return n.post("/streams",{driver_url:i.driver_url,face:i.face,config:i.config,output_resolution:i.output_resolution,compatibility_mode:i.compatibility_mode,stream_warmup:i.stream_warmup,session_timeout:i.session_timeout,stream_greeting:i.stream_greeting},a)},startConnection(i,a,o,l){return n.post(`/streams/${i}/sdp`,{session_id:o,answer:a},l)},addIceCandidate(i,a,o,l){return n.post(`/streams/${i}/ice`,{session_id:o,...a},l)},sendStreamRequest(i,a,o,l){return n.post(`/streams/${i}`,{session_id:a,...o},l)},close(i,a,o){return n.delete(`/streams/${i}`,{session_id:a},o)}}}function be(e,t,s){const r=(t.timestamp-e.timestamp)/1e3;return{duration:r,bytesReceived:t.bytesReceived-e.bytesReceived,bitrate:Math.round((t.bytesReceived-e.bytesReceived)*8/r),packetsReceived:t.packetsReceived-e.packetsReceived,packetsLost:t.packetsLost-e.packetsLost,framesDropped:t.framesDropped-e.framesDropped,framesDecoded:t.framesDecoded-e.framesDecoded,jitter:t.jitter,jitterBufferDelay:(t.jitterBufferDelay-e.jitterBufferDelay)/r,framesPerSecond:t.framesPerSecond,freezeCount:t.freezeCount-e.freezeCount,freezeDuration:t.freezeDuration-e.freezeDuration,lowFpsCount:s}}function Ee(e){return e.filter(t=>t.freezeCount>0||t.framesPerSecond<21||t.framesDropped>0||t.packetsLost>0).map(t=>{const{timestamp:s,...r}=t,n=[];return t.freezeCount>0&&n.push("freeze"),t.framesPerSecond<21&&n.push("low fps"),t.framesDropped>0&&n.push("frames dropped"),t.packetsLost>0&&n.push("packet loss"),{...r,causes:n}})}function Pe(e){let t="";for(const s of e.values())if(s&&s.type==="codec"&&s.mimeType.startsWith("video")&&(t=s.mimeType.split("/")[1]),s&&s.type==="inbound-rtp"&&s.kind==="video")return{codec:t,timestamp:s.timestamp,bytesReceived:s.bytesReceived,packetsReceived:s.packetsReceived,packetsLost:s.packetsLost,framesDropped:s.framesDropped,framesDecoded:s.framesDecoded,jitter:s.jitter,jitterBufferDelay:s.jitterBufferDelay,frameWidth:s.frameWidth,frameHeight:s.frameHeight,framesPerSecond:s.framesPerSecond,freezeCount:s.freezeCount,freezeDuration:s.totalFreezesDuration};return{}}function Ie(e,t,s){const r=e.map((a,o)=>o===0?s?{timestamp:a.timestamp,duration:0,bytesReceived:a.bytesReceived-s.bytesReceived,bitrate:(a.bytesReceived-s.bytesReceived)*8/(t/1e3),packetsReceived:a.packetsReceived-s.packetsReceived,packetsLost:a.packetsLost-s.packetsLost,framesDropped:a.framesDropped-s.framesDropped,framesDecoded:a.framesDecoded-s.framesDecoded,jitter:a.jitter,jitterBufferDelay:a.jitterBufferDelay-s.jitterBufferDelay,framesPerSecond:a.framesPerSecond,freezeCount:a.freezeCount-s.freezeCount,freezeDuration:a.freezeDuration-s.freezeDuration}:{timestamp:a.timestamp,duration:0,bytesReceived:a.bytesReceived,bitrate:a.bytesReceived*8/(t/1e3),packetsReceived:a.packetsReceived,packetsLost:a.packetsLost,framesDropped:a.framesDropped,framesDecoded:a.framesDecoded,jitter:a.jitter,jitterBufferDelay:a.jitterBufferDelay,framesPerSecond:a.framesPerSecond,freezeCount:a.freezeCount,freezeDuration:a.freezeDuration}:{timestamp:a.timestamp,duration:t*o/1e3,bytesReceived:a.bytesReceived-e[o-1].bytesReceived,bitrate:(a.bytesReceived-e[o-1].bytesReceived)*8/(t/1e3),packetsReceived:a.packetsReceived-e[o-1].packetsReceived,packetsLost:a.packetsLost-e[o-1].packetsLost,framesDropped:a.framesDropped-e[o-1].framesDropped,framesDecoded:a.framesDecoded-e[o-1].framesDecoded,jitter:a.jitter,jitterBufferDelay:a.jitterBufferDelay-e[o-1].jitterBufferDelay,framesPerSecond:a.framesPerSecond,freezeCount:a.freezeCount-e[o-1].freezeCount,freezeDuration:a.freezeDuration-e[o-1].freezeDuration}),n=Ee(r),i=n.reduce((a,o)=>a+(o.causes.includes("low fps")?1:0),0);return{webRTCStats:{anomalies:n,aggregateReport:be(e[0],e[e.length-1],i)},codec:e[0].codec,resolution:`${e[0].frameWidth}x${e[0].frameHeight}`}}let de=!1;const z=(e,t)=>de&&console.log(e,t),Te=(window.RTCPeerConnection||window.webkitRTCPeerConnection||window.mozRTCPeerConnection).bind(window);function le(e){switch(e){case"connected":return S.Connected;case"checking":return S.Connecting;case"failed":return S.Fail;case"new":return S.New;case"closed":return S.Closed;case"disconnected":return S.Disconnected;case"completed":return S.Completed;default:return S.New}}function Ae(){let e=0;return t=>{for(const s of t.values())if(s&&s.type==="inbound-rtp"&&s.kind==="video"){const r=s.bytesReceived,n=r-e>0;return e=r,n}return!1}}function $e(e,t,s=!1,r,n,i=!1){const o=Math.max(Math.ceil(10),1);let l=[],d,f=0,m=!1;const y=s?1:0;let C=0;const v=Ae();return setInterval(async()=>{const R=await e.getStats(),g=v(R),D=Pe(R);if(g)f=0,m||(t==null||t(j.Start),i&&C>=y&&!r()&&n(),d=l[l.length-1],l=[],C++,m=!0),l.push(D);else if(m&&(f++,f>=o)){const _=Ie(l,100,d);t==null||t(j.Stop,_),!i&&!r()&&n(),m=!1}},100)}async function je(e,t,{debug:s=!1,callbacks:r,auth:n,baseURL:i=K,warmup:a}){de=s;const{startConnection:o,sendStreamRequest:l,close:d,createStream:f,addIceCandidate:m}=t.videoType===J.Clip?_e(n,i,e,r.onError):Me(n,i,e,r.onError),{id:y,offer:C,ice_servers:v,session_id:R}=await f(t),g=new Te({iceServers:v}),D=g.createDataChannel("JanusDataChannel");if(!R)throw new Error("Could not create session_id");let _=!1;const c=()=>_,h=()=>{var u;_=!0,(u=r.onConnectionStateChange)==null||u.call(r,S.Connected)},k=$e(g,r.onVideoStateChange,a,c,h,!!t.stream_greeting);g.onicecandidate=u=>{var w;z("peerConnection.onicecandidate",u);try{u.candidate&&u.candidate.sdpMid&&u.candidate.sdpMLineIndex!==null?m(y,{candidate:u.candidate.candidate,sdpMid:u.candidate.sdpMid,sdpMLineIndex:u.candidate.sdpMLineIndex},R):m(y,{candidate:null},R)}catch(b){(w=r.onError)==null||w.call(r,b,{streamId:y})}},D.onopen=()=>{!t.stream_warmup&&!t.stream_greeting&&h()},g.oniceconnectionstatechange=()=>{var w;z("peerConnection.oniceconnectionstatechange => "+g.iceConnectionState);const u=le(g.iceConnectionState);u!==S.Connected&&((w=r.onConnectionStateChange)==null||w.call(r,u))},g.ontrack=u=>{var w;z("peerConnection.ontrack",u),(w=r.onSrcObjectReady)==null||w.call(r,u.streams[0])},await g.setRemoteDescription(C),z("set remote description OK");const M=await g.createAnswer();return z("create answer OK"),await g.setLocalDescription(M),z("set local description OK"),await o(y,M,R),z("start connection OK"),{speak(u){return l(y,R,u)},async disconnect(){var u,w;if(y){const b=le(g.iceConnectionState);if(g){if(b===S.New){(u=r.onVideoStateChange)==null||u.call(r,j.Stop),clearInterval(k);return}g.close(),g.oniceconnectionstatechange=null,g.onnegotiationneeded=null,g.onicecandidate=null,g.ontrack=null}try{b===S.Connected&&await d(y,R).catch(P=>{})}catch(P){z("Error on close stream connection",P)}(w=r.onVideoStateChange)==null||w.call(r,j.Stop),clearInterval(k)}},sessionId:R,streamId:y}}let X={};function ze(e){var i,a,o,l,d;const t=window!=null&&window.hasOwnProperty("DID_AGENTS_API")?"agents-ui":"agents-sdk",s=e.agent.presenter,r=(i=e.agent.llm)==null?void 0:i.prompt_customization,n={token:e.token||"testKey",distinct_id:e.distinctId||se(),agentId:e.agent.id,agentType:s.type==="clip"&&s.presenter_id.startsWith("v2_")?"clip_v2":s.type,owner_id:e.agent.owner_id??"",behavior:{role:r==null?void 0:r.role,personality:r==null?void 0:r.personality,instructions:(a=e.agent.llm)==null?void 0:a.instructions},temperature:(o=e.agent.llm)==null?void 0:o.temperature,knowledgeSource:r==null?void 0:r.knowledge_source,starterQuestionsCount:(d=(l=e.agent.knowledge)==null?void 0:l.starter_message)==null?void 0:d.length,topicsToAvoid:r==null?void 0:r.topics_to_avoid,maxResponseLength:r==null?void 0:r.max_response_length};return{...n,additionalProperties:{},isEnabled:e.isEnabled??!0,getRandom:()=>Math.random().toString(16).slice(2),enrich(f){const m={};if(f&&typeof f!="object")throw new Error("properties must be a flat json object");for(let y in f)(typeof f[y]=="string"||typeof f[y]=="number")&&(m[y]=f[y]);this.additionalProperties={...this.additionalProperties,...m}},track(f,m){if(!this.isEnabled)return Promise.resolve();const{audioPath:y,...C}=m||{},v={method:"POST",headers:{"Content-Type":"application/x-www-form-urlencoded"},body:new URLSearchParams({data:JSON.stringify([{event:f,properties:{...this.additionalProperties,...C,...n,source:t,time:Date.now(),$insert_id:this.getRandom(),origin:window.location.href,"Screen Height":window.screen.height||window.innerWidth,"Screen Width":window.screen.width||window.innerHeight,"User Agent":navigator.userAgent}}])})};return fetch("https://api-js.mixpanel.com/track/?verbose=1&ip=1",v).then(R=>R.json()).catch(R=>console.error(R))},linkTrack(f,m,y,C){X[f]||(X[f]={events:{},resolvedDependencies:[]}),C.includes(y)||C.push(y);const v=X[f];if(v.events[y]={props:m},v.resolvedDependencies.push(y),C.every(g=>v.resolvedDependencies.includes(g))){const g=C.reduce((D,_)=>v.events[_]?{...D,...v.events[_].props}:D,{});this.track(f,g),v.resolvedDependencies=v.resolvedDependencies.filter(D=>!C.includes(D)),C.forEach(D=>{delete v.events[D]})}}}}function Le(e){var n,i,a,o;const t=()=>/Mobi|Android/i.test(navigator.userAgent)?"Mobile":"Desktop",s=()=>{const l=navigator.platform;return l.toLowerCase().includes("win")?"Windows":l.toLowerCase().includes("mac")?"Mac OS X":l.toLowerCase().includes("linux")?"Linux":"Unknown"},r=e.presenter;return{$os:`${s()}`,isMobile:`${t()=="Mobile"}`,browser:navigator.userAgent,origin:window.location.origin,agentType:r.type==="clip"&&r.presenter_id.startsWith("v2_")?"clip_v2":r.type,agentVoice:{voiceId:(i=(n=e.presenter)==null?void 0:n.voice)==null?void 0:i.voice_id,provider:(o=(a=e.presenter)==null?void 0:a.voice)==null?void 0:o.type}}}function Ne(e,t,s){var d,f;const{event:r,...n}=e,{template:i}=(t==null?void 0:t.llm)||{},{language:a}=((d=t==null?void 0:t.presenter)==null?void 0:d.voice)||{},{stitch:o}=(t==null?void 0:t.presenter)||{};return{...n,llm:{...n.llm,template:i},script:{...n.script,provider:{...(f=n==null?void 0:n.script)==null?void 0:f.provider,language:a}},stitch:o,...s}}class Be extends Error{constructor({kind:t,description:s}){const r=JSON.stringify({kind:t,description:s});super(r)}}let L=0;const Fe=45*1e3;function Ve(e,t,s){var r,n,i,a,o;return{videoType:ne(e.presenter.type),output_resolution:(r=t==null?void 0:t.streamOptions)==null?void 0:r.outputResolution,session_timeout:(n=t==null?void 0:t.streamOptions)==null?void 0:n.sessionTimeout,stream_warmup:(i=t==null?void 0:t.streamOptions)==null?void 0:i.streamWarmup,compatibility_mode:(a=t==null?void 0:t.streamOptions)==null?void 0:a.compatibilityMode,stream_greeting:(o=t==null?void 0:t.streamOptions)!=null&&o.streamGreeting?s:void 0}}function me(e){return e===E.Playground?{headers:{[De]:"true"}}:{}}async function ue(e,t,s,r,n){try{const i=await t.newChat(e,{persist:n??!1},me(r));return s.track("agent-chat",{event:"created",chat_id:i.id,agent_id:e,mode:r}),i}catch(i){let a;try{a=JSON.parse(i.message)}catch(o){console.error("Error parsing the error message:",o)}throw(a==null?void 0:a.kind)==="InsufficientCreditsError"?new Error("InsufficientCreditsError"):new Error("Cannot create new chat")}}function He(e,t,s,r,n,i){return new Promise(async(a,o)=>{var m,y,C,v,R;L=0;const l=String(t.mode);if(!n&&t.mode!==E.DirectPlayback)try{n=await ue(e.id,s,r,t.mode,t.persistentChat)}catch(g){return o(g)}const d=(n==null?void 0:n.chat_mode)||l;if(d!==l&&(t.mode=d,(y=(m=t.callbacks).onModeChange)==null||y.call(m,d),d===E.TextOnly&&((v=(C=t.callbacks)==null?void 0:C.onError)==null||v.call(C,new Be({kind:"ChatModeDowngraded",description:`Chat mode changed from ${l} to ${d} when creating the chat`}),{}))),d===E.TextOnly)return a({chat:n});const f=await je(e.id,Ve(e,t,i),{...t,analytics:r,warmup:(R=t.streamOptions)==null?void 0:R.streamWarmup,callbacks:{...t.callbacks,onConnectionStateChange:async g=>{var D,_,c,h;g===S.Connected?f?((_=(D=t.callbacks).onConnectionStateChange)==null||_.call(D,g),a({chat:n,streamingManager:f})):n&&o(new Error("Something went wrong while initializing the manager")):(h=(c=t.callbacks).onConnectionStateChange)==null||h.call(c,g)},onVideoStateChange(g,D){var _,c;(c=(_=t.callbacks).onVideoStateChange)==null||c.call(_,g),L>0&&(g===j.Start?r.linkTrack("agent-video",{event:"start",latency:Date.now()-L},"start",[W.StreamVideoCreated]):g===j.Stop&&r.linkTrack("agent-video",{event:"stop",is_greenscreen:e.presenter.type==="clip"&&e.presenter.is_greenscreen,background:e.presenter.type==="clip"&&e.presenter.background,...D},"done",[W.StreamVideoDone]))}}}).catch(o)})}function We(e){var s;const t=(s=e.greetings)==null?void 0:s.filter(r=>r.length>0);if(t&&t.length>0){const r=Math.floor(Math.random()*t.length);return t[r]}else return`Hi! I'm ${e.preview_name||"My Agent"}. How can I help you?`}function fe(e,t){return t&&t.length>0?t:[{content:e,id:x(),role:"assistant",created_at:new Date().toISOString()}]}function Ke(e){if(e.answer!==void 0)return e.answer;let t=0,s="";for(;t in e;)s+=e[t],t++;return s}function Ue(e,t,s,r,n){if(!(e===B.Partial||e===B.Answer))return;const i=r.messages[r.messages.length-1];if((i==null?void 0:i.role)!=="assistant")return;const{content:a,sequence:o}=t;e===B.Partial?s[o]=a:s.answer=a;const l=Ke(s);(i.content!==l||e===B.Answer)&&(i.content=l,n==null||n([...r.messages],e))}async function xe(e,t){var g,D,_;let s={},r=!0;const n={messages:[],chatMode:t.mode||E.Functional},i=t.baseURL||K,a=t.wsURL||we,o=t.mixpanelKey||ye,l=ce(t.auth,i,t.callbacks.onError),d=await l.getById(e),f=We(d);n.messages=fe(f,t.initialMessages),(D=(g=t.callbacks).onNewMessage)==null||D.call(g,[...n.messages],"answer");const m=ze({token:o,agent:d,isEnabled:t.enableAnalitics,distinctId:t.distinctId});m.track("agent-sdk",{event:"loaded",...Le(d)});const y={onMessage:(c,h)=>{var k,M;if("content"in h)Ue(c,h,s,n,t.callbacks.onNewMessage),c===B.Answer&&m.track("agent-message-received",{messages:n.messages.length,mode:n.chatMode});else{const u=W,w=[u.StreamVideoDone,u.StreamVideoError,u.StreamVideoRejected],b=[u.StreamFailed,u.StreamVideoError,u.StreamVideoRejected],P=Ne(h,d,{mode:n.chatMode});if(c=c,c===u.StreamVideoCreated)m.linkTrack("agent-video",P,u.StreamVideoCreated,["start"]);else if(w.includes(c)){const I=c.split("/")[1];b.includes(c)?m.track("agent-video",{...P,event:I}):m.linkTrack("agent-video",{...P,event:I},c,["done"])}b.includes(c)&&((M=(k=t.callbacks).onError)==null||M.call(k,new Error(`Stream failed with event ${c}`),{data:h})),h.event===u.StreamDone&&v()}}};async function C(c){var b,P,I,$,U,N,T;(P=(b=t.callbacks).onConnectionStateChange)==null||P.call(b,S.Connecting),L=0,c&&!r&&(delete n.chat,n.messages=fe(f),($=(I=t.callbacks).onNewMessage)==null||$.call(I,[...n.messages],"answer"));const h=t.mode===E.DirectPlayback?Promise.resolve(void 0):Se(t.auth,a,y),k=ie(()=>He(d,t,l,m,n.chat,c?f:void 0),{limit:3,timeout:Fe,timeoutErrorMessage:"Timeout initializing the stream",shouldRetryFn:A=>(A==null?void 0:A.message)!=="Could not connect"&&A.status!==429,delayMs:1e3}).catch(A=>{var V,H;throw R(E.Maintenance),(H=(V=t.callbacks).onConnectionStateChange)==null||H.call(V,S.Fail),A}),[M,{streamingManager:u,chat:w}]=await Promise.all([h,k]);w&&w.id!==((U=n.chat)==null?void 0:U.id)&&((T=(N=t.callbacks).onNewChat)==null||T.call(N,w.id)),n.streamingManager=u,n.socketManager=M,n.chat=w,r=!1,R((w==null?void 0:w.chat_mode)??t.mode??E.Functional)}async function v(){var c,h,k,M;(c=n.socketManager)==null||c.disconnect(),await((h=n.streamingManager)==null?void 0:h.disconnect()),delete n.streamingManager,delete n.socketManager,(M=(k=t.callbacks).onConnectionStateChange)==null||M.call(k,S.Disconnected)}async function R(c){var h,k;c!==n.chatMode&&(m.track("agent-mode-change",{mode:c}),n.chatMode=c,n.chatMode!==E.Functional&&await v(),(k=(h=t.callbacks).onModeChange)==null||k.call(h,c))}return{agent:d,starterMessages:((_=d.knowledge)==null?void 0:_.starter_message)||[],getSTTToken:()=>l.getSTTToken(d.id),changeMode:R,enrichAnalytics:m.enrich,async connect(){var c;await C(!0),m.track("agent-chat",{event:"connect",chatId:(c=n.chat)==null?void 0:c.id,agentId:d.id,mode:n.chatMode})},async reconnect(){var c;await v(),await C(!1),m.track("agent-chat",{event:"reconnect",chatId:(c=n.chat)==null?void 0:c.id,agentId:d.id,mode:n.chatMode})},async disconnect(){var c;await v(),m.track("agent-chat",{event:"disconnect",chatId:(c=n.chat)==null?void 0:c.id,agentId:d.id,mode:n.chatMode})},async chat(c){var k,M,u,w,b,P;const h=x();s={};try{if(L=Date.now(),t.mode===E.DirectPlayback)throw new Error("Direct playback is enabled, chat is disabled");if(c.length>=800)throw new Error("Message cannot be more than 800 characters");if(c.length===0)throw new Error("Message cannot be empty");if(n.chatMode===E.Maintenance)throw new Error("Chat is in maintenance mode");if(![E.TextOnly,E.Playground].includes(n.chatMode))if(n.streamingManager){if(!n.chat)throw new Error("Chat is not initialized")}else throw new Error("Streaming manager is not initialized");n.messages.push({id:x(),role:"user",content:c,created_at:new Date(L).toISOString()}),(M=(k=t.callbacks).onNewMessage)==null||M.call(k,[...n.messages],"user"),n.chat||(n.chat=await ue(d.id,l,m,n.chatMode,t.persistentChat),(w=(u=t.callbacks).onNewChat)==null||w.call(u,n.chat.id));const I={id:h,role:"assistant",content:"",created_at:new Date().toISOString(),matches:[]},$=[...n.messages];n.messages.push(I);const U=T=>{var A,V;return l.chat(d.id,T,{sessionId:(A=n.streamingManager)==null?void 0:A.sessionId,streamId:(V=n.streamingManager)==null?void 0:V.streamId,chatMode:n.chatMode,messages:$.map(({matches:H,...q})=>q)},{...me(n.chatMode),skipErrorHandler:!0})},N=await U(n.chat.id).catch(async T=>{var H,q,ge,he;const A=(H=T==null?void 0:T.message)==null?void 0:H.includes("missing or invalid session_id");if(!((q=T==null?void 0:T.message)==null?void 0:q.includes("Stream Error"))&&!A)throw(he=(ge=t.callbacks).onError)==null||he.call(ge,T,{}),T;return await v(),await C(!1),U(n.chat.id)});return m.track("agent-message-send",{event:"success",mode:n.chatMode,messages:n.messages.length+1}),I.context=N.context,I.matches=N.matches,N.result&&(I.content=N.result,m.track("agent-message-received",{latency:Date.now()-L,mode:n.chatMode,messages:n.messages.length}),(P=(b=t.callbacks).onNewMessage)==null||P.call(b,[...n.messages],"answer")),N}catch(I){throw n.messages[n.messages.length-1].id===h&&n.messages.pop(),m.track("agent-message-send",{event:"error",mode:n.chatMode,messages:n.messages.length}),I}},rate(c,h,k){var w,b,P,I;const M=n.messages.find($=>$.id===c);if(n.chat){if(!M)throw new Error("Message not found")}else throw new Error("Chat is not initialized");const u=((w=M.matches)==null?void 0:w.map($=>[$.document_id,$.id]))??[];return m.track("agent-rate",{event:k?"update":"create",thumb:h===1?"up":"down",knowledge_id:((b=d.knowledge)==null?void 0:b.id)??"",mode:n.chatMode,matches:u,score:h}),k?l.updateRating(d.id,n.chat.id,k,{knowledge_id:((P=d.knowledge)==null?void 0:P.id)??"",message_id:c,matches:u,score:h}):l.createRating(d.id,n.chat.id,{knowledge_id:((I=d.knowledge)==null?void 0:I.id)??"",message_id:c,matches:u,score:h})},deleteRate(c){var h;if(!n.chat)throw new Error("Chat is not initialized");return m.track("agent-rate-delete",{type:"text",chat_id:(h=n.chat)==null?void 0:h.id,id:c,mode:n.chatMode}),l.deleteRating(d.id,n.chat.id,c)},speak(c){if(!n.streamingManager)throw new Error("Please connect to the agent first");L=Date.now();function h(){if(typeof c=="string"){if(!d.presenter.voice)throw new Error("Presenter voice is not initialized");return{type:"text",provider:d.presenter.voice,input:c,ssml:!1}}if(c.type==="text"&&!c.provider){if(!d.presenter.voice)throw new Error("Presenter voice is not initialized");return{type:"text",provider:d.presenter.voice,input:c.input,ssml:c.ssml}}return c}const k=h();return m.track("agent-speak",k),n.streamingManager.speak({script:k})}}}function qe(e,t,s){const{getById:r}=ce(t,s||K);return r(e)}p.AgentStatus=Z,p.ChatMode=E,p.ChatProgress=B,p.ConnectionState=S,p.DocumentType=te,p.KnowledgeType=ee,p.PlanGroup=Y,p.Providers=re,p.RateState=G,p.StreamEvents=W,p.StreamingState=j,p.Subject=Q,p.UserPlan=F,p.VideoType=J,p.VoiceAccess=ae,p.createAgentManager=xe,p.getAgent=qe,p.mapVideoType=ne,Object.defineProperty(p,Symbol.toStringTag,{value:"Module"})});
|
|
1
|
+
(function(v,F){typeof exports=="object"&&typeof module<"u"?F(exports):typeof define=="function"&&define.amd?define(["exports"],F):(v=typeof globalThis<"u"?globalThis:v||self,F(v.index={}))})(this,function(v){"use strict";var F=(e=>(e.TRIAL="trial",e.BASIC="basic",e.ENTERPRISE="enterprise",e.LITE="lite",e.ADVANCED="advanced",e))(F||{}),Y=(e=>(e.TRIAL="deid-trial",e.PRO="deid-pro",e.ENTERPRISE="deid-enterprise",e.LITE="deid-lite",e.ADVANCED="deid-advanced",e.BUILD="deid-api-build",e.LAUNCH="deid-api-launch",e.SCALE="deid-api-scale",e))(Y||{}),Z=(e=>(e.Created="created",e.Started="started",e.Done="done",e.Error="error",e.Rejected="rejected",e.Ready="ready",e))(Z||{}),G=(e=>(e.Unrated="Unrated",e.Positive="Positive",e.Negative="Negative",e))(G||{}),b=(e=>(e.Functional="Functional",e.TextOnly="TextOnly",e.Maintenance="Maintenance",e.Playground="Playground",e.DirectPlayback="DirectPlayback",e))(b||{}),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||{}),ee=(e=>(e.Knowledge="knowledge",e.Document="document",e.Record="record",e))(ee||{}),te=(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||{}),J=(e=>(e.Clip="clip",e.Talk="talk",e))(J||{});const ne=e=>{switch(e){case"clip":return"clip";case"talk":return"talk";default:throw new Error(`Unknown video type: ${e}`)}};var j=(e=>(e.Start="START",e.Stop="STOP",e))(j||{}),W=(e=>(e.ChatAnswer="chat/answer",e.ChatPartial="chat/partial",e.StreamDone="stream/done",e.StreamStarted="stream/started",e.StreamFailed="stream/error",e.StreamReady="stream/ready",e.StreamCreated="stream/created",e.StreamVideoCreated="stream-video/started",e.StreamVideoDone="stream-video/done",e.StreamVideoError="stream-video/error",e.StreamVideoRejected="stream-video/rejected",e))(W||{}),D=(e=>(e.New="new",e.Fail="fail",e.Connected="connected",e.Connecting="connecting",e.Closed="closed",e.Completed="completed",e.Disconnected="disconnected",e))(D||{}),re=(e=>(e.Amazon="amazon",e.Microsoft="microsoft",e.Afflorithmics="afflorithmics",e.Elevenlabs="elevenlabs",e))(re||{}),ae=(e=>(e.Public="public",e.Premium="premium",e.Private="private",e))(ae||{});const K="https://api.d-id.com",we="wss://notifications.d-id.com",ye="79f81a83a67430be2bc0fd61042b8faa";async function ie(e,t){const s={limit:(t==null?void 0:t.limit)??3,delayMs:(t==null?void 0:t.delayMs)??0,timeout:(t==null?void 0:t.timeout)??3e4,timeoutErrorMessage:(t==null?void 0:t.timeoutErrorMessage)||"Timeout error",shouldRetryFn:(t==null?void 0:t.shouldRetryFn)??function(){return!0}};let r;for(let n=1;n<=s.limit;n++)try{if(!s.timeout)return await e();let i;const a=new Promise((l,h)=>{e().then(d=>{clearTimeout(i),l(d)}).catch(d=>{clearTimeout(i),h(d)})}),o=new Promise((l,h)=>{i=setTimeout(()=>h(new Error(s.timeoutErrorMessage)),s.timeout)});return await Promise.race([a,o])}catch(i){if(r=i,!s.shouldRetryFn(i)||n>=s.limit)throw i;s.delayMs>0&&await new Promise(a=>setTimeout(a,s.delayMs))}throw r}const x=()=>Math.random().toString(16).slice(2);function se(){let e=window.localStorage.getItem("did_external_key_id");return e||(e=Math.random().toString(16).slice(2),window.localStorage.setItem("did_external_key_id",e)),e}let pe=x();function oe(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}.${se()}_${pe}`;throw new Error(`Unknown auth type: ${e}`)}const ve=e=>ie(e,{limit:3,delayMs:1e3,timeout:0,shouldRetryFn:t=>t.status===429});function O(e,t=K,s){const r=async(n,i)=>{const{skipErrorHandler:a,...o}=i||{},m=await ve(()=>fetch(t+(n!=null&&n.startsWith("/")?n:`/${n}`),{...o,headers:{...o.headers,Authorization:oe(e),"Content-Type":"application/json"}}));if(!m.ok){let l=await m.text().catch(()=>"Failed to fetch");throw s&&!a&&s(new Error(l),{url:n,options:o,headers:m.headers}),new Error(l)}return m.json()};return{get(n,i){return r(n,{...i,method:"GET"})},post(n,i,a){return r(n,{...a,body:JSON.stringify(i),method:"POST"})},delete(n,i,a){return r(n,{...a,body:JSON.stringify(i),method:"DELETE"})},patch(n,i,a){return r(n,{...a,body:JSON.stringify(i),method:"PATCH"})}}}function ce(e,t=K,s){const r=O(e,`${t}/agents`,s);return{create(n,i){return r.post("/",n,i)},getAgents(n,i){return r.get(`/${n?`?tag=${n}`:""}`,i).then(a=>a??[])},getById(n,i){return r.get(`/${n}`,i)},delete(n,i){return r.delete(`/${n}`,void 0,i)},update(n,i,a){return r.patch(`/${n}`,i,a)},newChat(n,i,a){return r.post(`/${n}/chat`,i,a)},chat(n,i,a,o){return r.post(`/${n}/chat/${i}`,a,o)},createRating(n,i,a,o){return r.post(`/${n}/chat/${i}/ratings`,a,o)},updateRating(n,i,a,o,m){return r.patch(`/${n}/chat/${i}/ratings/${a}`,o,m)},deleteRating(n,i,a,o){return r.delete(`/${n}/chat/${i}/ratings/${a}`,o)},getSTTToken(n,i){return r.get(`/${n}/stt-token`,i)}}}const ke=e=>new Promise(t=>setTimeout(t,e));function Ce(e){return new Promise((t,s)=>{const{callbacks:r,host:n,auth:i}=e,{onMessage:a=null,onOpen:o=null,onClose:m=null,onError:l=null}=r||{},h=new WebSocket(`${n}?authorization=${oe(i)}`);h.onmessage=a,h.onclose=m,h.onerror=d=>{console.error(d),l==null||l("Websocket failed to connect",d),s(d)},h.onopen=d=>{o==null||o(d),t(h)}})}async function Re(e){const{retries:t=1}=e;let s=null;for(let r=0;(s==null?void 0:s.readyState)!==WebSocket.OPEN;r++)try{s=await Ce(e)}catch(n){if(r===t)throw n;await ke(r*500)}return s}async function Se(e,t,s){const r=s!=null&&s.onMessage?[s.onMessage]:[],n=await Re({auth:e,host:t,callbacks:{onError:s==null?void 0:s.onError,onMessage:i=>{const a=JSON.parse(i.data);r.forEach(o=>o(a.event,a))}}});return{socket:n,disconnect:()=>n.close(),subscribeToEvents:i=>r.push(i)}}const De="X-Playground-Chat";function _e(e,t,s,r){const n=O(e,`${t}/agents/${s}`,r);return{createStream(i){return n.post("/streams",{output_resolution:i.output_resolution,compatibility_mode:i.compatibility_mode,stream_warmup:i.stream_warmup,session_timeout:i.session_timeout,stream_greeting:i.stream_greeting})},startConnection(i,a,o){return n.post(`/streams/${i}/sdp`,{session_id:o,answer:a})},addIceCandidate(i,a,o){return n.post(`/streams/${i}/ice`,{session_id:o,...a})},sendStreamRequest(i,a,o){return n.post(`/streams/${i}`,{session_id:a,...o})},close(i,a){return n.delete(`/streams/${i}`,{session_id:a})}}}function Me(e,t,s,r){const n=O(e,`${t}/agents/${s}`,r);return{createStream(i,a){return n.post("/streams",{driver_url:i.driver_url,face:i.face,config:i.config,output_resolution:i.output_resolution,compatibility_mode:i.compatibility_mode,stream_warmup:i.stream_warmup,session_timeout:i.session_timeout,stream_greeting:i.stream_greeting},a)},startConnection(i,a,o,m){return n.post(`/streams/${i}/sdp`,{session_id:o,answer:a},m)},addIceCandidate(i,a,o,m){return n.post(`/streams/${i}/ice`,{session_id:o,...a},m)},sendStreamRequest(i,a,o,m){return n.post(`/streams/${i}`,{session_id:a,...o},m)},close(i,a,o){return n.delete(`/streams/${i}`,{session_id:a},o)}}}function be(e,t,s){const r=(t.timestamp-e.timestamp)/1e3;return{duration:r,bytesReceived:t.bytesReceived-e.bytesReceived,bitrate:Math.round((t.bytesReceived-e.bytesReceived)*8/r),packetsReceived:t.packetsReceived-e.packetsReceived,packetsLost:t.packetsLost-e.packetsLost,framesDropped:t.framesDropped-e.framesDropped,framesDecoded:t.framesDecoded-e.framesDecoded,jitter:t.jitter,jitterBufferDelay:(t.jitterBufferDelay-e.jitterBufferDelay)/r,framesPerSecond:t.framesPerSecond,freezeCount:t.freezeCount-e.freezeCount,freezeDuration:t.freezeDuration-e.freezeDuration,lowFpsCount:s}}function Ee(e){return e.filter(t=>t.freezeCount>0||t.framesPerSecond<21||t.framesDropped>0||t.packetsLost>0).map(t=>{const{timestamp:s,...r}=t,n=[];return t.freezeCount>0&&n.push("freeze"),t.framesPerSecond<21&&n.push("low fps"),t.framesDropped>0&&n.push("frames dropped"),t.packetsLost>0&&n.push("packet loss"),{...r,causes:n}})}function Pe(e){let t="";for(const s of e.values())if(s&&s.type==="codec"&&s.mimeType.startsWith("video")&&(t=s.mimeType.split("/")[1]),s&&s.type==="inbound-rtp"&&s.kind==="video")return{codec:t,timestamp:s.timestamp,bytesReceived:s.bytesReceived,packetsReceived:s.packetsReceived,packetsLost:s.packetsLost,framesDropped:s.framesDropped,framesDecoded:s.framesDecoded,jitter:s.jitter,jitterBufferDelay:s.jitterBufferDelay,frameWidth:s.frameWidth,frameHeight:s.frameHeight,framesPerSecond:s.framesPerSecond,freezeCount:s.freezeCount,freezeDuration:s.totalFreezesDuration};return{}}function Ie(e,t,s){const r=e.map((a,o)=>o===0?s?{timestamp:a.timestamp,duration:0,bytesReceived:a.bytesReceived-s.bytesReceived,bitrate:(a.bytesReceived-s.bytesReceived)*8/(t/1e3),packetsReceived:a.packetsReceived-s.packetsReceived,packetsLost:a.packetsLost-s.packetsLost,framesDropped:a.framesDropped-s.framesDropped,framesDecoded:a.framesDecoded-s.framesDecoded,jitter:a.jitter,jitterBufferDelay:a.jitterBufferDelay-s.jitterBufferDelay,framesPerSecond:a.framesPerSecond,freezeCount:a.freezeCount-s.freezeCount,freezeDuration:a.freezeDuration-s.freezeDuration}:{timestamp:a.timestamp,duration:0,bytesReceived:a.bytesReceived,bitrate:a.bytesReceived*8/(t/1e3),packetsReceived:a.packetsReceived,packetsLost:a.packetsLost,framesDropped:a.framesDropped,framesDecoded:a.framesDecoded,jitter:a.jitter,jitterBufferDelay:a.jitterBufferDelay,framesPerSecond:a.framesPerSecond,freezeCount:a.freezeCount,freezeDuration:a.freezeDuration}:{timestamp:a.timestamp,duration:t*o/1e3,bytesReceived:a.bytesReceived-e[o-1].bytesReceived,bitrate:(a.bytesReceived-e[o-1].bytesReceived)*8/(t/1e3),packetsReceived:a.packetsReceived-e[o-1].packetsReceived,packetsLost:a.packetsLost-e[o-1].packetsLost,framesDropped:a.framesDropped-e[o-1].framesDropped,framesDecoded:a.framesDecoded-e[o-1].framesDecoded,jitter:a.jitter,jitterBufferDelay:a.jitterBufferDelay-e[o-1].jitterBufferDelay,framesPerSecond:a.framesPerSecond,freezeCount:a.freezeCount-e[o-1].freezeCount,freezeDuration:a.freezeDuration-e[o-1].freezeDuration}),n=Ee(r),i=n.reduce((a,o)=>a+(o.causes.includes("low fps")?1:0),0);return{webRTCStats:{anomalies:n,aggregateReport:be(e[0],e[e.length-1],i)},codec:e[0].codec,resolution:`${e[0].frameWidth}x${e[0].frameHeight}`}}let de=!1;const z=(e,t)=>de&&console.log(e,t),Te=(window.RTCPeerConnection||window.webkitRTCPeerConnection||window.mozRTCPeerConnection).bind(window);function le(e){switch(e){case"connected":return D.Connected;case"checking":return D.Connecting;case"failed":return D.Fail;case"new":return D.New;case"closed":return D.Closed;case"disconnected":return D.Disconnected;case"completed":return D.Completed;default:return D.New}}function Ae(){let e=0;return t=>{for(const s of t.values())if(s&&s.type==="inbound-rtp"&&s.kind==="video"){const r=s.bytesReceived,n=r-e>0;return e=r,n}return!1}}function $e(e,t,s=!1,r,n,i=!1){const o=Math.max(Math.ceil(10),1);let m=[],l,h=0,d=!1;const C=s?1:0;let p=0;const R=Ae();return setInterval(async()=>{const w=await e.getStats(),f=R(w),E=Pe(w);if(f)h=0,d||(t==null||t(j.Start),i&&p>=C&&!r()&&n(),l=m[m.length-1],m=[],p++,d=!0),m.push(E);else if(d&&(h++,h>=o)){const S=Ie(m,100,l);t==null||t(j.Stop,S),!i&&!r()&&n(),d=!1}},100)}async function je(e,t,{debug:s=!1,callbacks:r,auth:n,baseURL:i=K,warmup:a}){de=s;const{startConnection:o,sendStreamRequest:m,close:l,createStream:h,addIceCandidate:d}=t.videoType===J.Clip?_e(n,i,e,r.onError):Me(n,i,e,r.onError),{id:C,offer:p,ice_servers:R,session_id:w}=await h(t),f=new Te({iceServers:R}),E=f.createDataChannel("JanusDataChannel");if(!w)throw new Error("Could not create session_id");let S=!1;const c=()=>S,g=()=>{var u;S=!0,(u=r.onConnectionStateChange)==null||u.call(r,D.Connected)},k=$e(f,r.onVideoStateChange,a,c,g,!!t.stream_greeting);f.onicecandidate=u=>{var y;z("peerConnection.onicecandidate",u);try{u.candidate&&u.candidate.sdpMid&&u.candidate.sdpMLineIndex!==null?d(C,{candidate:u.candidate.candidate,sdpMid:u.candidate.sdpMid,sdpMLineIndex:u.candidate.sdpMLineIndex},w):d(C,{candidate:null},w)}catch(M){(y=r.onError)==null||y.call(r,M,{streamId:C})}},E.onopen=()=>{!t.stream_warmup&&!t.stream_greeting&&g()},f.oniceconnectionstatechange=()=>{var y;z("peerConnection.oniceconnectionstatechange => "+f.iceConnectionState);const u=le(f.iceConnectionState);u!==D.Connected&&((y=r.onConnectionStateChange)==null||y.call(r,u))},f.ontrack=u=>{var y;z("peerConnection.ontrack",u),(y=r.onSrcObjectReady)==null||y.call(r,u.streams[0])},await f.setRemoteDescription(p),z("set remote description OK");const _=await f.createAnswer();return z("create answer OK"),await f.setLocalDescription(_),z("set local description OK"),await o(C,_,w),z("start connection OK"),{speak(u){return m(C,w,u)},async disconnect(){var u,y;if(C){const M=le(f.iceConnectionState);if(f){if(M===D.New){(u=r.onVideoStateChange)==null||u.call(r,j.Stop),clearInterval(k);return}f.close(),f.oniceconnectionstatechange=null,f.onnegotiationneeded=null,f.onicecandidate=null,f.ontrack=null}try{M===D.Connected&&await l(C,w).catch(P=>{})}catch(P){z("Error on close stream connection",P)}(y=r.onVideoStateChange)==null||y.call(r,j.Stop),clearInterval(k)}},sessionId:w,streamId:C}}let X={};function ze(e){var i,a,o,m,l,h;const t=window!=null&&window.hasOwnProperty("DID_AGENTS_API")?"agents-ui":"agents-sdk",s=e.agent.presenter,r=(i=e.agent.llm)==null?void 0:i.prompt_customization,n={token:e.token||"testKey",distinct_id:e.distinctId||se(),agentId:e.agent.id,agentType:s.type==="clip"&&s.presenter_id.startsWith("v2_")?"clip_v2":s.type,owner_id:e.agent.owner_id??"",promptVersion:(a=e.agent.llm)==null?void 0:a.prompt_version,behavior:{role:r==null?void 0:r.role,personality:r==null?void 0:r.personality,instructions:(o=e.agent.llm)==null?void 0:o.instructions},temperature:(m=e.agent.llm)==null?void 0:m.temperature,knowledgeSource:r==null?void 0:r.knowledge_source,starterQuestionsCount:(h=(l=e.agent.knowledge)==null?void 0:l.starter_message)==null?void 0:h.length,topicsToAvoid:r==null?void 0:r.topics_to_avoid,maxResponseLength:r==null?void 0:r.max_response_length};return{...n,additionalProperties:{},isEnabled:e.isEnabled??!0,getRandom:()=>Math.random().toString(16).slice(2),enrich(d){const C={};if(d&&typeof d!="object")throw new Error("properties must be a flat json object");for(let p in d)(typeof d[p]=="string"||typeof d[p]=="number")&&(C[p]=d[p]);this.additionalProperties={...this.additionalProperties,...C}},track(d,C){if(!this.isEnabled)return Promise.resolve();const{audioPath:p,...R}=C||{},w={method:"POST",headers:{"Content-Type":"application/x-www-form-urlencoded"},body:new URLSearchParams({data:JSON.stringify([{event:d,properties:{...this.additionalProperties,...R,...n,source:t,time:Date.now(),$insert_id:this.getRandom(),origin:window.location.href,"Screen Height":window.screen.height||window.innerWidth,"Screen Width":window.screen.width||window.innerHeight,"User Agent":navigator.userAgent}}])})};return fetch("https://api-js.mixpanel.com/track/?verbose=1&ip=1",w).then(f=>f.json()).catch(f=>console.error(f))},linkTrack(d,C,p,R){X[d]||(X[d]={events:{},resolvedDependencies:[]}),R.includes(p)||R.push(p);const w=X[d];if(w.events[p]={props:C},w.resolvedDependencies.push(p),R.every(E=>w.resolvedDependencies.includes(E))){const E=R.reduce((S,c)=>w.events[c]?{...S,...w.events[c].props}:S,{});this.track(d,E),w.resolvedDependencies=w.resolvedDependencies.filter(S=>!R.includes(S)),R.forEach(S=>{delete w.events[S]})}}}}function Le(e){var n,i,a,o;const t=()=>/Mobi|Android/i.test(navigator.userAgent)?"Mobile":"Desktop",s=()=>{const m=navigator.platform;return m.toLowerCase().includes("win")?"Windows":m.toLowerCase().includes("mac")?"Mac OS X":m.toLowerCase().includes("linux")?"Linux":"Unknown"},r=e.presenter;return{$os:`${s()}`,isMobile:`${t()=="Mobile"}`,browser:navigator.userAgent,origin:window.location.origin,agentType:r.type==="clip"&&r.presenter_id.startsWith("v2_")?"clip_v2":r.type,agentVoice:{voiceId:(i=(n=e.presenter)==null?void 0:n.voice)==null?void 0:i.voice_id,provider:(o=(a=e.presenter)==null?void 0:a.voice)==null?void 0:o.type}}}function Ne(e,t,s){var l,h;const{event:r,...n}=e,{template:i}=(t==null?void 0:t.llm)||{},{language:a}=((l=t==null?void 0:t.presenter)==null?void 0:l.voice)||{},{stitch:o}=(t==null?void 0:t.presenter)||{};return{...n,llm:{...n.llm,template:i},script:{...n.script,provider:{...(h=n==null?void 0:n.script)==null?void 0:h.provider,language:a}},stitch:o,...s}}class Be extends Error{constructor({kind:t,description:s}){const r=JSON.stringify({kind:t,description:s});super(r)}}let L=0;const Fe=45*1e3;function Ve(e,t,s){var r,n,i,a,o;return{videoType:ne(e.presenter.type),output_resolution:(r=t==null?void 0:t.streamOptions)==null?void 0:r.outputResolution,session_timeout:(n=t==null?void 0:t.streamOptions)==null?void 0:n.sessionTimeout,stream_warmup:(i=t==null?void 0:t.streamOptions)==null?void 0:i.streamWarmup,compatibility_mode:(a=t==null?void 0:t.streamOptions)==null?void 0:a.compatibilityMode,stream_greeting:(o=t==null?void 0:t.streamOptions)!=null&&o.streamGreeting?s:void 0}}function me(e){return e===b.Playground?{headers:{[De]:"true"}}:{}}async function ue(e,t,s,r,n){try{const i=await t.newChat(e,{persist:n??!1},me(r));return s.track("agent-chat",{event:"created",chat_id:i.id,agent_id:e,mode:r}),i}catch(i){let a;try{a=JSON.parse(i.message)}catch(o){console.error("Error parsing the error message:",o)}throw(a==null?void 0:a.kind)==="InsufficientCreditsError"?new Error("InsufficientCreditsError"):new Error("Cannot create new chat")}}function He(e,t,s,r,n,i){return new Promise(async(a,o)=>{var d,C,p,R,w;L=0;const m=String(t.mode);if(!n&&t.mode!==b.DirectPlayback)try{n=await ue(e.id,s,r,t.mode,t.persistentChat)}catch(f){return o(f)}const l=(n==null?void 0:n.chat_mode)||m;if(l!==m&&(t.mode=l,(C=(d=t.callbacks).onModeChange)==null||C.call(d,l),l===b.TextOnly&&((R=(p=t.callbacks)==null?void 0:p.onError)==null||R.call(p,new Be({kind:"ChatModeDowngraded",description:`Chat mode changed from ${m} to ${l} when creating the chat`}),{}))),l===b.TextOnly)return a({chat:n});const h=await je(e.id,Ve(e,t,i),{...t,analytics:r,warmup:(w=t.streamOptions)==null?void 0:w.streamWarmup,callbacks:{...t.callbacks,onConnectionStateChange:async f=>{var E,S,c,g;f===D.Connected?h?((S=(E=t.callbacks).onConnectionStateChange)==null||S.call(E,f),a({chat:n,streamingManager:h})):n&&o(new Error("Something went wrong while initializing the manager")):(g=(c=t.callbacks).onConnectionStateChange)==null||g.call(c,f)},onVideoStateChange(f,E){var S,c;(c=(S=t.callbacks).onVideoStateChange)==null||c.call(S,f),L>0&&(f===j.Start?r.linkTrack("agent-video",{event:"start",latency:Date.now()-L},"start",[W.StreamVideoCreated]):f===j.Stop&&r.linkTrack("agent-video",{event:"stop",is_greenscreen:e.presenter.type==="clip"&&e.presenter.is_greenscreen,background:e.presenter.type==="clip"&&e.presenter.background,...E},"done",[W.StreamVideoDone]))}}}).catch(o)})}function We(e){var s;const t=(s=e.greetings)==null?void 0:s.filter(r=>r.length>0);if(t&&t.length>0){const r=Math.floor(Math.random()*t.length);return t[r]}else return`Hi! I'm ${e.preview_name||"My Agent"}. How can I help you?`}function fe(e,t){return t&&t.length>0?t:[{content:e,id:x(),role:"assistant",created_at:new Date().toISOString()}]}function Ke(e){if(e.answer!==void 0)return e.answer;let t=0,s="";for(;t in e;)s+=e[t],t++;return s}function Ue(e,t,s,r,n){if(!(e===B.Partial||e===B.Answer))return;const i=r.messages[r.messages.length-1];if((i==null?void 0:i.role)!=="assistant")return;const{content:a,sequence:o}=t;e===B.Partial?s[o]=a:s.answer=a;const m=Ke(s);(i.content!==m||e===B.Answer)&&(i.content=m,n==null||n([...r.messages],e))}async function xe(e,t){var f,E,S;let s={},r=!0;const n={messages:[],chatMode:t.mode||b.Functional},i=t.baseURL||K,a=t.wsURL||we,o=t.mixpanelKey||ye,m=ce(t.auth,i,t.callbacks.onError),l=await m.getById(e),h=We(l);n.messages=fe(h,t.initialMessages),(E=(f=t.callbacks).onNewMessage)==null||E.call(f,[...n.messages],"answer");const d=ze({token:o,agent:l,isEnabled:t.enableAnalitics,distinctId:t.distinctId});d.track("agent-sdk",{event:"loaded",...Le(l)});const C={onMessage:(c,g)=>{var k,_;if("content"in g)Ue(c,g,s,n,t.callbacks.onNewMessage),c===B.Answer&&d.track("agent-message-received",{messages:n.messages.length,mode:n.chatMode});else{const u=W,y=[u.StreamVideoDone,u.StreamVideoError,u.StreamVideoRejected],M=[u.StreamFailed,u.StreamVideoError,u.StreamVideoRejected],P=Ne(g,l,{mode:n.chatMode});if(c=c,c===u.StreamVideoCreated)d.linkTrack("agent-video",P,u.StreamVideoCreated,["start"]);else if(y.includes(c)){const I=c.split("/")[1];M.includes(c)?d.track("agent-video",{...P,event:I}):d.linkTrack("agent-video",{...P,event:I},c,["done"])}M.includes(c)&&((_=(k=t.callbacks).onError)==null||_.call(k,new Error(`Stream failed with event ${c}`),{data:g})),g.event===u.StreamDone&&R()}}};async function p(c){var M,P,I,$,U,N,T;(P=(M=t.callbacks).onConnectionStateChange)==null||P.call(M,D.Connecting),L=0,c&&!r&&(delete n.chat,n.messages=fe(h),($=(I=t.callbacks).onNewMessage)==null||$.call(I,[...n.messages],"answer"));const g=t.mode===b.DirectPlayback?Promise.resolve(void 0):Se(t.auth,a,C),k=ie(()=>He(l,t,m,d,n.chat,c?h:void 0),{limit:3,timeout:Fe,timeoutErrorMessage:"Timeout initializing the stream",shouldRetryFn:A=>(A==null?void 0:A.message)!=="Could not connect"&&A.status!==429,delayMs:1e3}).catch(A=>{var V,H;throw w(b.Maintenance),(H=(V=t.callbacks).onConnectionStateChange)==null||H.call(V,D.Fail),A}),[_,{streamingManager:u,chat:y}]=await Promise.all([g,k]);y&&y.id!==((U=n.chat)==null?void 0:U.id)&&((T=(N=t.callbacks).onNewChat)==null||T.call(N,y.id)),n.streamingManager=u,n.socketManager=_,n.chat=y,r=!1,w((y==null?void 0:y.chat_mode)??t.mode??b.Functional)}async function R(){var c,g,k,_;(c=n.socketManager)==null||c.disconnect(),await((g=n.streamingManager)==null?void 0:g.disconnect()),delete n.streamingManager,delete n.socketManager,(_=(k=t.callbacks).onConnectionStateChange)==null||_.call(k,D.Disconnected)}async function w(c){var g,k;c!==n.chatMode&&(d.track("agent-mode-change",{mode:c}),n.chatMode=c,n.chatMode!==b.Functional&&await R(),(k=(g=t.callbacks).onModeChange)==null||k.call(g,c))}return{agent:l,starterMessages:((S=l.knowledge)==null?void 0:S.starter_message)||[],getSTTToken:()=>m.getSTTToken(l.id),changeMode:w,enrichAnalytics:d.enrich,async connect(){var c;await p(!0),d.track("agent-chat",{event:"connect",chatId:(c=n.chat)==null?void 0:c.id,agentId:l.id,mode:n.chatMode})},async reconnect(){var c;await R(),await p(!1),d.track("agent-chat",{event:"reconnect",chatId:(c=n.chat)==null?void 0:c.id,agentId:l.id,mode:n.chatMode})},async disconnect(){var c;await R(),d.track("agent-chat",{event:"disconnect",chatId:(c=n.chat)==null?void 0:c.id,agentId:l.id,mode:n.chatMode})},async chat(c){var k,_,u,y,M,P;const g=x();s={};try{if(L=Date.now(),t.mode===b.DirectPlayback)throw new Error("Direct playback is enabled, chat is disabled");if(c.length>=800)throw new Error("Message cannot be more than 800 characters");if(c.length===0)throw new Error("Message cannot be empty");if(n.chatMode===b.Maintenance)throw new Error("Chat is in maintenance mode");if(![b.TextOnly,b.Playground].includes(n.chatMode))if(n.streamingManager){if(!n.chat)throw new Error("Chat is not initialized")}else throw new Error("Streaming manager is not initialized");n.messages.push({id:x(),role:"user",content:c,created_at:new Date(L).toISOString()}),(_=(k=t.callbacks).onNewMessage)==null||_.call(k,[...n.messages],"user"),n.chat||(n.chat=await ue(l.id,m,d,n.chatMode,t.persistentChat),(y=(u=t.callbacks).onNewChat)==null||y.call(u,n.chat.id));const I={id:g,role:"assistant",content:"",created_at:new Date().toISOString(),matches:[]},$=[...n.messages];n.messages.push(I);const U=T=>{var A,V;return m.chat(l.id,T,{sessionId:(A=n.streamingManager)==null?void 0:A.sessionId,streamId:(V=n.streamingManager)==null?void 0:V.streamId,chatMode:n.chatMode,messages:$.map(({matches:H,...q})=>q)},{...me(n.chatMode),skipErrorHandler:!0})},N=await U(n.chat.id).catch(async T=>{var H,q,ge,he;const A=(H=T==null?void 0:T.message)==null?void 0:H.includes("missing or invalid session_id");if(!((q=T==null?void 0:T.message)==null?void 0:q.includes("Stream Error"))&&!A)throw(he=(ge=t.callbacks).onError)==null||he.call(ge,T,{}),T;return await R(),await p(!1),U(n.chat.id)});return d.track("agent-message-send",{event:"success",mode:n.chatMode,messages:n.messages.length+1}),I.context=N.context,I.matches=N.matches,N.result&&(I.content=N.result,d.track("agent-message-received",{latency:Date.now()-L,mode:n.chatMode,messages:n.messages.length}),(P=(M=t.callbacks).onNewMessage)==null||P.call(M,[...n.messages],"answer")),N}catch(I){throw n.messages[n.messages.length-1].id===g&&n.messages.pop(),d.track("agent-message-send",{event:"error",mode:n.chatMode,messages:n.messages.length}),I}},rate(c,g,k){var y,M,P,I;const _=n.messages.find($=>$.id===c);if(n.chat){if(!_)throw new Error("Message not found")}else throw new Error("Chat is not initialized");const u=((y=_.matches)==null?void 0:y.map($=>[$.document_id,$.id]))??[];return d.track("agent-rate",{event:k?"update":"create",thumb:g===1?"up":"down",knowledge_id:((M=l.knowledge)==null?void 0:M.id)??"",mode:n.chatMode,matches:u,score:g}),k?m.updateRating(l.id,n.chat.id,k,{knowledge_id:((P=l.knowledge)==null?void 0:P.id)??"",message_id:c,matches:u,score:g}):m.createRating(l.id,n.chat.id,{knowledge_id:((I=l.knowledge)==null?void 0:I.id)??"",message_id:c,matches:u,score:g})},deleteRate(c){var g;if(!n.chat)throw new Error("Chat is not initialized");return d.track("agent-rate-delete",{type:"text",chat_id:(g=n.chat)==null?void 0:g.id,id:c,mode:n.chatMode}),m.deleteRating(l.id,n.chat.id,c)},speak(c){if(!n.streamingManager)throw new Error("Please connect to the agent first");L=Date.now();function g(){if(typeof c=="string"){if(!l.presenter.voice)throw new Error("Presenter voice is not initialized");return{type:"text",provider:l.presenter.voice,input:c,ssml:!1}}if(c.type==="text"&&!c.provider){if(!l.presenter.voice)throw new Error("Presenter voice is not initialized");return{type:"text",provider:l.presenter.voice,input:c.input,ssml:c.ssml}}return c}const k=g();return d.track("agent-speak",k),n.streamingManager.speak({script:k})}}}function qe(e,t,s){const{getById:r}=ce(t,s||K);return r(e)}v.AgentStatus=Z,v.ChatMode=b,v.ChatProgress=B,v.ConnectionState=D,v.DocumentType=te,v.KnowledgeType=ee,v.PlanGroup=Y,v.Providers=re,v.RateState=G,v.StreamEvents=W,v.StreamingState=j,v.Subject=Q,v.UserPlan=F,v.VideoType=J,v.VoiceAccess=ae,v.createAgentManager=xe,v.getAgent=qe,v.mapVideoType=ne,Object.defineProperty(v,Symbol.toStringTag,{value:"Module"})});
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@d-id/client-sdk",
|
|
3
3
|
"private": false,
|
|
4
|
-
"version": "1.0.19-beta.
|
|
4
|
+
"version": "1.0.19-beta.155",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"description": "d-id client sdk",
|
|
7
7
|
"repository": {
|
|
@@ -44,4 +44,4 @@
|
|
|
44
44
|
"vite": "^5.1.4",
|
|
45
45
|
"vite-plugin-dts": "^3.7.3"
|
|
46
46
|
}
|
|
47
|
-
}
|
|
47
|
+
}
|