@d-id/client-sdk 1.0.19-beta.141 → 1.0.19-beta.143
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 +351 -325
- package/dist/index.umd.cjs +1 -1
- package/dist/src/utils/retryOperation.d.ts +37 -0
- package/package.json +2 -2
package/dist/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
var
|
|
2
|
-
const
|
|
1
|
+
var re = /* @__PURE__ */ ((e) => (e.TRIAL = "trial", e.BASIC = "basic", e.ENTERPRISE = "enterprise", e.LITE = "lite", e.ADVANCED = "advanced", e))(re || {}), ae = /* @__PURE__ */ ((e) => (e.TRIAL = "deid-trial", e.PRO = "deid-pro", e.ENTERPRISE = "deid-enterprise", e.LITE = "deid-lite", e.ADVANCED = "deid-advanced", e.BUILD = "deid-api-build", e.LAUNCH = "deid-api-launch", e.SCALE = "deid-api-scale", e))(ae || {}), ie = /* @__PURE__ */ ((e) => (e.Created = "created", e.Started = "started", e.Done = "done", e.Error = "error", e.Rejected = "rejected", e.Ready = "ready", e))(ie || {}), se = /* @__PURE__ */ ((e) => (e.Unrated = "Unrated", e.Positive = "Positive", e.Negative = "Negative", e))(se || {}), I = /* @__PURE__ */ ((e) => (e.Functional = "Functional", e.TextOnly = "TextOnly", e.Maintenance = "Maintenance", e.Playground = "Playground", e.DirectPlayback = "DirectPlayback", e))(I || {}), L = /* @__PURE__ */ ((e) => (e.Embed = "embed", e.Query = "query", e.Partial = "partial", e.Answer = "answer", e.Complete = "done", e))(L || {}), oe = /* @__PURE__ */ ((e) => (e.KnowledgeProcessing = "knowledge/processing", e.KnowledgeIndexing = "knowledge/indexing", e.KnowledgeFailed = "knowledge/error", e.KnowledgeDone = "knowledge/done", e))(oe || {}), ce = /* @__PURE__ */ ((e) => (e.Knowledge = "knowledge", e.Document = "document", e.Record = "record", e))(ce || {}), de = /* @__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))(de || {}), O = /* @__PURE__ */ ((e) => (e.Clip = "clip", e.Talk = "talk", e))(O || {});
|
|
2
|
+
const le = (e) => {
|
|
3
3
|
switch (e) {
|
|
4
4
|
case "clip":
|
|
5
5
|
return "clip";
|
|
@@ -9,32 +9,66 @@ const me = (e) => {
|
|
|
9
9
|
throw new Error(`Unknown video type: ${e}`);
|
|
10
10
|
}
|
|
11
11
|
};
|
|
12
|
-
var
|
|
13
|
-
const
|
|
14
|
-
function
|
|
12
|
+
var F = /* @__PURE__ */ ((e) => (e.Start = "START", e.Stop = "STOP", e))(F || {}), j = /* @__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))(j || {}), M = /* @__PURE__ */ ((e) => (e.New = "new", e.Fail = "fail", e.Connected = "connected", e.Connecting = "connecting", e.Closed = "closed", e.Completed = "completed", e.Disconnected = "disconnected", e))(M || {}), me = /* @__PURE__ */ ((e) => (e.Amazon = "amazon", e.Microsoft = "microsoft", e.Afflorithmics = "afflorithmics", e.Elevenlabs = "elevenlabs", e))(me || {}), ge = /* @__PURE__ */ ((e) => (e.Public = "public", e.Premium = "premium", e.Private = "private", e))(ge || {});
|
|
13
|
+
const U = "https://api.d-id.com", ue = "wss://notifications.d-id.com", he = "79f81a83a67430be2bc0fd61042b8faa";
|
|
14
|
+
async function X(e, n) {
|
|
15
|
+
const s = {
|
|
16
|
+
limit: (n == null ? void 0 : n.limit) ?? 3,
|
|
17
|
+
delayMs: (n == null ? void 0 : n.delayMs) ?? 0,
|
|
18
|
+
timeout: (n == null ? void 0 : n.timeout) ?? 3e4,
|
|
19
|
+
timeoutErrorMessage: (n == null ? void 0 : n.timeoutErrorMessage) || "Timeout error",
|
|
20
|
+
shouldRetryFn: (n == null ? void 0 : n.shouldRetryFn) ?? function() {
|
|
21
|
+
return !0;
|
|
22
|
+
}
|
|
23
|
+
};
|
|
24
|
+
let a;
|
|
25
|
+
for (let t = 1; t <= s.limit; t++)
|
|
26
|
+
try {
|
|
27
|
+
if (!s.timeout)
|
|
28
|
+
return await e();
|
|
29
|
+
let r;
|
|
30
|
+
const i = new Promise((l, d) => {
|
|
31
|
+
r = setTimeout(() => d(new Error(s.timeoutErrorMessage)), s.timeout);
|
|
32
|
+
});
|
|
33
|
+
return await Promise.race([e().then((l) => (clearTimeout(r), l)), i]);
|
|
34
|
+
} catch (r) {
|
|
35
|
+
if (a = r, !s.shouldRetryFn(r) || t >= s.limit)
|
|
36
|
+
throw r;
|
|
37
|
+
s.delayMs > 0 && await new Promise((i) => setTimeout(i, s.delayMs));
|
|
38
|
+
}
|
|
39
|
+
throw a;
|
|
40
|
+
}
|
|
41
|
+
const V = () => Math.random().toString(16).slice(2);
|
|
42
|
+
function Y() {
|
|
15
43
|
let e = window.localStorage.getItem("did_external_key_id");
|
|
16
44
|
return e || (e = Math.random().toString(16).slice(2), window.localStorage.setItem("did_external_key_id", e)), e;
|
|
17
45
|
}
|
|
18
|
-
let
|
|
19
|
-
function
|
|
46
|
+
let we = V();
|
|
47
|
+
function Z(e) {
|
|
20
48
|
if (e.type === "bearer")
|
|
21
49
|
return `Bearer ${e.token}`;
|
|
22
50
|
if (e.type === "basic")
|
|
23
51
|
return `Basic ${btoa(`${e.username}:${e.password}`)}`;
|
|
24
52
|
if (e.type === "key")
|
|
25
|
-
return `Client-Key ${e.clientKey}.${
|
|
53
|
+
return `Client-Key ${e.clientKey}.${Y()}_${we}`;
|
|
26
54
|
throw new Error(`Unknown auth type: ${e}`);
|
|
27
55
|
}
|
|
28
|
-
|
|
56
|
+
const fe = (e) => X(e, {
|
|
57
|
+
limit: 3,
|
|
58
|
+
delayMs: 1e3,
|
|
59
|
+
timeout: 0,
|
|
60
|
+
shouldRetryFn: (n) => n.status === 429
|
|
61
|
+
});
|
|
62
|
+
function q(e, n = U, s) {
|
|
29
63
|
const a = async (t, r) => {
|
|
30
|
-
const i = await fetch(n + (t != null && t.startsWith("/") ? t : `/${t}`), {
|
|
64
|
+
const i = await fe(() => fetch(n + (t != null && t.startsWith("/") ? t : `/${t}`), {
|
|
31
65
|
...r,
|
|
32
66
|
headers: {
|
|
33
67
|
...r == null ? void 0 : r.headers,
|
|
34
|
-
Authorization:
|
|
68
|
+
Authorization: Z(e),
|
|
35
69
|
"Content-Type": "application/json"
|
|
36
70
|
}
|
|
37
|
-
});
|
|
71
|
+
}));
|
|
38
72
|
if (!i.ok) {
|
|
39
73
|
let o = await i.text().catch(() => "Failed to fetch");
|
|
40
74
|
throw s && s(new Error(o), {
|
|
@@ -75,8 +109,8 @@ function J(e, n = B, s) {
|
|
|
75
109
|
}
|
|
76
110
|
};
|
|
77
111
|
}
|
|
78
|
-
function
|
|
79
|
-
const a =
|
|
112
|
+
function G(e, n = U, s) {
|
|
113
|
+
const a = q(e, `${n}/agents`, s);
|
|
80
114
|
return {
|
|
81
115
|
create(t, r) {
|
|
82
116
|
return a.post("/", t, r);
|
|
@@ -124,16 +158,16 @@ function ve(e) {
|
|
|
124
158
|
onMessage: i = null,
|
|
125
159
|
onOpen: o = null,
|
|
126
160
|
onClose: l = null,
|
|
127
|
-
onError:
|
|
128
|
-
} = a || {}, u = new WebSocket(`${t}?authorization=${
|
|
129
|
-
u.onmessage = i, u.onclose = l, u.onerror = (
|
|
130
|
-
console.error(
|
|
131
|
-
}, u.onopen = (
|
|
132
|
-
o == null || o(
|
|
161
|
+
onError: d = null
|
|
162
|
+
} = a || {}, u = new WebSocket(`${t}?authorization=${Z(r)}`);
|
|
163
|
+
u.onmessage = i, u.onclose = l, u.onerror = (g) => {
|
|
164
|
+
console.error(g), d == null || d("Websocket failed to connect", g), s(g);
|
|
165
|
+
}, u.onopen = (g) => {
|
|
166
|
+
o == null || o(g), n(u);
|
|
133
167
|
};
|
|
134
168
|
});
|
|
135
169
|
}
|
|
136
|
-
async function
|
|
170
|
+
async function pe(e) {
|
|
137
171
|
const {
|
|
138
172
|
retries: n = 1
|
|
139
173
|
} = e;
|
|
@@ -148,8 +182,8 @@ async function Ce(e) {
|
|
|
148
182
|
}
|
|
149
183
|
return s;
|
|
150
184
|
}
|
|
151
|
-
async function
|
|
152
|
-
const a = s != null && s.onMessage ? [s.onMessage] : [], t = await
|
|
185
|
+
async function Ce(e, n, s) {
|
|
186
|
+
const a = s != null && s.onMessage ? [s.onMessage] : [], t = await pe({
|
|
153
187
|
auth: e,
|
|
154
188
|
host: n,
|
|
155
189
|
callbacks: {
|
|
@@ -166,9 +200,9 @@ async function _e(e, n, s) {
|
|
|
166
200
|
subscribeToEvents: (r) => a.push(r)
|
|
167
201
|
};
|
|
168
202
|
}
|
|
169
|
-
const
|
|
170
|
-
function
|
|
171
|
-
const t =
|
|
203
|
+
const _e = "X-Playground-Chat";
|
|
204
|
+
function Me(e, n, s, a) {
|
|
205
|
+
const t = q(e, `${n}/agents/${s}`, a);
|
|
172
206
|
return {
|
|
173
207
|
createStream(r) {
|
|
174
208
|
return t.post("/streams", {
|
|
@@ -204,8 +238,8 @@ function ke(e, n, s, a) {
|
|
|
204
238
|
}
|
|
205
239
|
};
|
|
206
240
|
}
|
|
207
|
-
function
|
|
208
|
-
const t =
|
|
241
|
+
function Se(e, n, s, a) {
|
|
242
|
+
const t = q(e, `${n}/agents/${s}`, a);
|
|
209
243
|
return {
|
|
210
244
|
createStream(r, i) {
|
|
211
245
|
return t.post("/streams", {
|
|
@@ -244,26 +278,26 @@ function Me(e, n, s, a) {
|
|
|
244
278
|
}
|
|
245
279
|
};
|
|
246
280
|
}
|
|
247
|
-
let
|
|
248
|
-
const
|
|
249
|
-
function
|
|
281
|
+
let Q = !1;
|
|
282
|
+
const x = (e, n) => Q && console.log(e, n), ke = (window.RTCPeerConnection || window.webkitRTCPeerConnection || window.mozRTCPeerConnection).bind(window);
|
|
283
|
+
function J(e) {
|
|
250
284
|
switch (e) {
|
|
251
285
|
case "connected":
|
|
252
|
-
return
|
|
286
|
+
return M.Connected;
|
|
253
287
|
case "checking":
|
|
254
|
-
return
|
|
288
|
+
return M.Connecting;
|
|
255
289
|
case "failed":
|
|
256
|
-
return
|
|
290
|
+
return M.Fail;
|
|
257
291
|
case "new":
|
|
258
|
-
return
|
|
292
|
+
return M.New;
|
|
259
293
|
case "closed":
|
|
260
|
-
return
|
|
294
|
+
return M.Closed;
|
|
261
295
|
case "disconnected":
|
|
262
|
-
return
|
|
296
|
+
return M.Disconnected;
|
|
263
297
|
case "completed":
|
|
264
|
-
return
|
|
298
|
+
return M.Completed;
|
|
265
299
|
default:
|
|
266
|
-
return
|
|
300
|
+
return M.New;
|
|
267
301
|
}
|
|
268
302
|
}
|
|
269
303
|
function Ee() {
|
|
@@ -277,106 +311,106 @@ function Ee() {
|
|
|
277
311
|
return !1;
|
|
278
312
|
};
|
|
279
313
|
}
|
|
280
|
-
function
|
|
314
|
+
function Re(e, n, s = !1, a, t) {
|
|
281
315
|
const i = Math.max(Math.ceil(10), 1);
|
|
282
316
|
let o = 0, l = !1;
|
|
283
|
-
const
|
|
317
|
+
const d = s ? 1 : 0;
|
|
284
318
|
let u = 0;
|
|
285
|
-
const
|
|
319
|
+
const g = Ee();
|
|
286
320
|
return setInterval(async () => {
|
|
287
|
-
const
|
|
288
|
-
|
|
321
|
+
const h = await e.getStats();
|
|
322
|
+
g(h) ? (o = 0, l || (n == null || n(F.Start), u >= d && !a() && t(), u++, l = !0)) : l && (o++, o >= i && (n == null || n(F.Stop), l = !1));
|
|
289
323
|
}, 100);
|
|
290
324
|
}
|
|
291
|
-
async function
|
|
325
|
+
async function Ie(e, n, {
|
|
292
326
|
debug: s = !1,
|
|
293
327
|
callbacks: a,
|
|
294
328
|
auth: t,
|
|
295
|
-
baseURL: r =
|
|
329
|
+
baseURL: r = U,
|
|
296
330
|
warmup: i
|
|
297
331
|
}) {
|
|
298
|
-
|
|
332
|
+
Q = s;
|
|
299
333
|
const {
|
|
300
334
|
startConnection: o,
|
|
301
335
|
sendStreamRequest: l,
|
|
302
|
-
close:
|
|
336
|
+
close: d,
|
|
303
337
|
createStream: u,
|
|
304
|
-
addIceCandidate:
|
|
305
|
-
} = n.videoType ===
|
|
306
|
-
id:
|
|
307
|
-
offer:
|
|
308
|
-
ice_servers:
|
|
338
|
+
addIceCandidate: g
|
|
339
|
+
} = n.videoType === O.Clip ? Me(t, r, e, a.onError) : Se(t, r, e, a.onError), {
|
|
340
|
+
id: h,
|
|
341
|
+
offer: C,
|
|
342
|
+
ice_servers: v,
|
|
309
343
|
session_id: _
|
|
310
|
-
} = await u(n),
|
|
311
|
-
iceServers:
|
|
312
|
-
}),
|
|
344
|
+
} = await u(n), y = new ke({
|
|
345
|
+
iceServers: v
|
|
346
|
+
}), $ = y.createDataChannel("JanusDataChannel");
|
|
313
347
|
if (!_)
|
|
314
348
|
throw new Error("Could not create session_id");
|
|
315
|
-
let
|
|
316
|
-
const c = () =>
|
|
317
|
-
var
|
|
318
|
-
|
|
319
|
-
},
|
|
320
|
-
|
|
321
|
-
var
|
|
322
|
-
|
|
349
|
+
let P = !1;
|
|
350
|
+
const c = () => P, w = () => {
|
|
351
|
+
var m;
|
|
352
|
+
P = !0, (m = a.onConnectionStateChange) == null || m.call(a, M.Connected);
|
|
353
|
+
}, p = Re(y, a.onVideoStateChange, i, c, w);
|
|
354
|
+
y.onicecandidate = (m) => {
|
|
355
|
+
var f;
|
|
356
|
+
x("peerConnection.onicecandidate", m);
|
|
323
357
|
try {
|
|
324
|
-
|
|
325
|
-
candidate:
|
|
326
|
-
sdpMid:
|
|
327
|
-
sdpMLineIndex:
|
|
328
|
-
}, _) :
|
|
358
|
+
m.candidate && m.candidate.sdpMid && m.candidate.sdpMLineIndex !== null ? g(h, {
|
|
359
|
+
candidate: m.candidate.candidate,
|
|
360
|
+
sdpMid: m.candidate.sdpMid,
|
|
361
|
+
sdpMLineIndex: m.candidate.sdpMLineIndex
|
|
362
|
+
}, _) : g(h, {
|
|
329
363
|
candidate: null
|
|
330
364
|
}, _);
|
|
331
|
-
} catch (
|
|
332
|
-
(
|
|
333
|
-
streamId:
|
|
365
|
+
} catch (k) {
|
|
366
|
+
(f = a.onError) == null || f.call(a, k, {
|
|
367
|
+
streamId: h
|
|
334
368
|
});
|
|
335
369
|
}
|
|
336
|
-
},
|
|
337
|
-
if (
|
|
338
|
-
const [
|
|
339
|
-
|
|
370
|
+
}, $.onmessage = (m) => {
|
|
371
|
+
if ($.readyState === "open") {
|
|
372
|
+
const [f, k] = m.data.split(":");
|
|
373
|
+
f === j.StreamReady && !P && w();
|
|
340
374
|
}
|
|
341
|
-
},
|
|
342
|
-
var
|
|
343
|
-
|
|
344
|
-
const
|
|
345
|
-
|
|
346
|
-
},
|
|
347
|
-
var
|
|
348
|
-
|
|
349
|
-
}, await
|
|
350
|
-
const
|
|
351
|
-
return
|
|
375
|
+
}, y.oniceconnectionstatechange = () => {
|
|
376
|
+
var f;
|
|
377
|
+
x("peerConnection.oniceconnectionstatechange => " + y.iceConnectionState);
|
|
378
|
+
const m = J(y.iceConnectionState);
|
|
379
|
+
m !== M.Connected && ((f = a.onConnectionStateChange) == null || f.call(a, m));
|
|
380
|
+
}, y.ontrack = (m) => {
|
|
381
|
+
var f;
|
|
382
|
+
x("peerConnection.ontrack", m), (f = a.onSrcObjectReady) == null || f.call(a, m.streams[0]);
|
|
383
|
+
}, await y.setRemoteDescription(C), x("set remote description OK");
|
|
384
|
+
const S = await y.createAnswer();
|
|
385
|
+
return x("create answer OK"), await y.setLocalDescription(S), x("set local description OK"), await o(h, S, _), x("start connection OK"), {
|
|
352
386
|
/**
|
|
353
387
|
* Method to send request to server to get clip or talk depend on you payload
|
|
354
388
|
* @param payload
|
|
355
389
|
*/
|
|
356
|
-
speak(
|
|
357
|
-
return l(
|
|
390
|
+
speak(m) {
|
|
391
|
+
return l(h, _, m);
|
|
358
392
|
},
|
|
359
393
|
/**
|
|
360
394
|
* Method to close RTC connection
|
|
361
395
|
*/
|
|
362
396
|
async disconnect() {
|
|
363
|
-
var
|
|
364
|
-
if (
|
|
365
|
-
const
|
|
366
|
-
if (
|
|
367
|
-
if (
|
|
368
|
-
(
|
|
397
|
+
var m, f;
|
|
398
|
+
if (h) {
|
|
399
|
+
const k = J(y.iceConnectionState);
|
|
400
|
+
if (y) {
|
|
401
|
+
if (k === M.New) {
|
|
402
|
+
(m = a.onVideoStateChange) == null || m.call(a, F.Stop), clearInterval(p);
|
|
369
403
|
return;
|
|
370
404
|
}
|
|
371
|
-
|
|
405
|
+
y.close(), y.oniceconnectionstatechange = null, y.onnegotiationneeded = null, y.onicecandidate = null, y.ontrack = null;
|
|
372
406
|
}
|
|
373
407
|
try {
|
|
374
|
-
|
|
408
|
+
k === M.Connected && await d(h, _).catch((E) => {
|
|
375
409
|
});
|
|
376
|
-
} catch (
|
|
377
|
-
|
|
410
|
+
} catch (E) {
|
|
411
|
+
x("Error on close stream connection", E);
|
|
378
412
|
}
|
|
379
|
-
(
|
|
413
|
+
(f = a.onVideoStateChange) == null || f.call(a, F.Stop), clearInterval(p);
|
|
380
414
|
}
|
|
381
415
|
},
|
|
382
416
|
/**
|
|
@@ -386,15 +420,15 @@ async function $e(e, n, {
|
|
|
386
420
|
/**
|
|
387
421
|
* Id of current RTC stream
|
|
388
422
|
*/
|
|
389
|
-
streamId:
|
|
423
|
+
streamId: h
|
|
390
424
|
};
|
|
391
425
|
}
|
|
392
|
-
let
|
|
393
|
-
function
|
|
394
|
-
var r, i, o, l,
|
|
426
|
+
let H = {};
|
|
427
|
+
function $e(e) {
|
|
428
|
+
var r, i, o, l, d;
|
|
395
429
|
const n = window != null && window.hasOwnProperty("DID_AGENTS_API") ? "agents-ui" : "agents-sdk", s = e.agent.presenter, a = (r = e.agent.llm) == null ? void 0 : r.prompt_customization, t = {
|
|
396
430
|
token: e.token || "testKey",
|
|
397
|
-
distinct_id: e.distinctId ||
|
|
431
|
+
distinct_id: e.distinctId || Y(),
|
|
398
432
|
agentId: e.agent.id,
|
|
399
433
|
agentType: s.type === "clip" && s.presenter_id.startsWith("v2_") ? "clip_v2" : s.type,
|
|
400
434
|
owner_id: e.agent.owner_id ?? "",
|
|
@@ -405,7 +439,7 @@ function Ae(e) {
|
|
|
405
439
|
},
|
|
406
440
|
temperature: (o = e.agent.llm) == null ? void 0 : o.temperature,
|
|
407
441
|
knowledgeSource: a == null ? void 0 : a.knowledge_source,
|
|
408
|
-
starterQuestionsCount: (
|
|
442
|
+
starterQuestionsCount: (d = (l = e.agent.knowledge) == null ? void 0 : l.starter_message) == null ? void 0 : d.length,
|
|
409
443
|
topicsToAvoid: a == null ? void 0 : a.topics_to_avoid,
|
|
410
444
|
maxResponseLength: a == null ? void 0 : a.max_response_length
|
|
411
445
|
};
|
|
@@ -415,23 +449,23 @@ function Ae(e) {
|
|
|
415
449
|
isEnabled: e.isEnabled ?? !0,
|
|
416
450
|
getRandom: () => Math.random().toString(16).slice(2),
|
|
417
451
|
enrich(u) {
|
|
418
|
-
const
|
|
452
|
+
const g = {};
|
|
419
453
|
if (u && typeof u != "object")
|
|
420
454
|
throw new Error("properties must be a flat json object");
|
|
421
|
-
for (let
|
|
422
|
-
(typeof u[
|
|
455
|
+
for (let h in u)
|
|
456
|
+
(typeof u[h] == "string" || typeof u[h] == "number") && (g[h] = u[h]);
|
|
423
457
|
this.additionalProperties = {
|
|
424
458
|
...this.additionalProperties,
|
|
425
|
-
...
|
|
459
|
+
...g
|
|
426
460
|
};
|
|
427
461
|
},
|
|
428
|
-
track(u,
|
|
462
|
+
track(u, g) {
|
|
429
463
|
if (!this.isEnabled)
|
|
430
464
|
return Promise.resolve();
|
|
431
465
|
const {
|
|
432
|
-
audioPath:
|
|
433
|
-
...
|
|
434
|
-
} =
|
|
466
|
+
audioPath: h,
|
|
467
|
+
...C
|
|
468
|
+
} = g || {}, v = {
|
|
435
469
|
method: "POST",
|
|
436
470
|
headers: {
|
|
437
471
|
"Content-Type": "application/x-www-form-urlencoded"
|
|
@@ -441,7 +475,7 @@ function Ae(e) {
|
|
|
441
475
|
event: u,
|
|
442
476
|
properties: {
|
|
443
477
|
...this.additionalProperties,
|
|
444
|
-
...
|
|
478
|
+
...C,
|
|
445
479
|
...t,
|
|
446
480
|
source: n,
|
|
447
481
|
time: Date.now(),
|
|
@@ -454,29 +488,29 @@ function Ae(e) {
|
|
|
454
488
|
}])
|
|
455
489
|
})
|
|
456
490
|
};
|
|
457
|
-
return fetch("https://api-js.mixpanel.com/track/?verbose=1&ip=1",
|
|
491
|
+
return fetch("https://api-js.mixpanel.com/track/?verbose=1&ip=1", v).then((_) => _.json()).catch((_) => console.error(_));
|
|
458
492
|
},
|
|
459
|
-
linkTrack(u,
|
|
460
|
-
|
|
493
|
+
linkTrack(u, g, h, C) {
|
|
494
|
+
H[u] || (H[u] = {
|
|
461
495
|
events: {},
|
|
462
496
|
resolvedDependencies: []
|
|
463
|
-
}),
|
|
464
|
-
const
|
|
465
|
-
if (
|
|
466
|
-
props:
|
|
467
|
-
},
|
|
468
|
-
const
|
|
469
|
-
|
|
470
|
-
...
|
|
471
|
-
} :
|
|
472
|
-
this.track(u,
|
|
473
|
-
delete
|
|
497
|
+
}), C.includes(h) || C.push(h);
|
|
498
|
+
const v = H[u];
|
|
499
|
+
if (v.events[h] = {
|
|
500
|
+
props: g
|
|
501
|
+
}, v.resolvedDependencies.push(h), C.every((y) => v.resolvedDependencies.includes(y))) {
|
|
502
|
+
const y = C.reduce(($, P) => v.events[P] ? {
|
|
503
|
+
...$,
|
|
504
|
+
...v.events[P].props
|
|
505
|
+
} : $, {});
|
|
506
|
+
this.track(u, y), v.resolvedDependencies = v.resolvedDependencies.filter(($) => !C.includes($)), C.forEach(($) => {
|
|
507
|
+
delete v.events[$];
|
|
474
508
|
});
|
|
475
509
|
}
|
|
476
510
|
}
|
|
477
511
|
};
|
|
478
512
|
}
|
|
479
|
-
function
|
|
513
|
+
function Ae(e) {
|
|
480
514
|
var t, r, i, o;
|
|
481
515
|
const n = () => /Mobi|Android/i.test(navigator.userAgent) ? "Mobile" : "Desktop", s = () => {
|
|
482
516
|
const l = navigator.platform;
|
|
@@ -495,7 +529,7 @@ function be(e) {
|
|
|
495
529
|
};
|
|
496
530
|
}
|
|
497
531
|
function Pe(e, n, s) {
|
|
498
|
-
var
|
|
532
|
+
var d, u;
|
|
499
533
|
const {
|
|
500
534
|
event: a,
|
|
501
535
|
...t
|
|
@@ -503,7 +537,7 @@ function Pe(e, n, s) {
|
|
|
503
537
|
template: r
|
|
504
538
|
} = (n == null ? void 0 : n.llm) || {}, {
|
|
505
539
|
language: i
|
|
506
|
-
} = ((
|
|
540
|
+
} = ((d = n == null ? void 0 : n.presenter) == null ? void 0 : d.voice) || {}, {
|
|
507
541
|
stitch: o
|
|
508
542
|
} = (n == null ? void 0 : n.presenter) || {};
|
|
509
543
|
return {
|
|
@@ -523,12 +557,12 @@ function Pe(e, n, s) {
|
|
|
523
557
|
...s
|
|
524
558
|
};
|
|
525
559
|
}
|
|
526
|
-
let
|
|
527
|
-
const
|
|
528
|
-
function
|
|
560
|
+
let N = 0;
|
|
561
|
+
const be = 20 * 1e3;
|
|
562
|
+
function Te(e, n, s) {
|
|
529
563
|
var a, t, r, i, o;
|
|
530
564
|
return {
|
|
531
|
-
videoType:
|
|
565
|
+
videoType: le(e.presenter.type),
|
|
532
566
|
output_resolution: (a = n == null ? void 0 : n.streamOptions) == null ? void 0 : a.outputResolution,
|
|
533
567
|
session_timeout: (t = n == null ? void 0 : n.streamOptions) == null ? void 0 : t.sessionTimeout,
|
|
534
568
|
stream_warmup: (r = n == null ? void 0 : n.streamOptions) == null ? void 0 : r.streamWarmup,
|
|
@@ -536,18 +570,18 @@ function De(e, n, s) {
|
|
|
536
570
|
stream_greeting: (o = n == null ? void 0 : n.streamOptions) != null && o.streamGreeting ? s : void 0
|
|
537
571
|
};
|
|
538
572
|
}
|
|
539
|
-
function
|
|
540
|
-
return e ===
|
|
573
|
+
function ee(e) {
|
|
574
|
+
return e === I.Playground ? {
|
|
541
575
|
headers: {
|
|
542
|
-
[
|
|
576
|
+
[_e]: "true"
|
|
543
577
|
}
|
|
544
578
|
} : {};
|
|
545
579
|
}
|
|
546
|
-
async function
|
|
580
|
+
async function te(e, n, s, a, t) {
|
|
547
581
|
try {
|
|
548
582
|
const r = await n.newChat(e, {
|
|
549
583
|
persist: t ?? !1
|
|
550
|
-
},
|
|
584
|
+
}, ee(a));
|
|
551
585
|
return s.track("agent-chat", {
|
|
552
586
|
event: "created",
|
|
553
587
|
chat_id: r.id,
|
|
@@ -564,48 +598,48 @@ async function ae(e, n, s, a, t) {
|
|
|
564
598
|
throw (i == null ? void 0 : i.kind) === "InsufficientCreditsError" ? new Error("InsufficientCreditsError") : new Error("Cannot create new chat");
|
|
565
599
|
}
|
|
566
600
|
}
|
|
567
|
-
function
|
|
601
|
+
function De(e, n, s, a, t, r) {
|
|
568
602
|
return new Promise(async (i, o) => {
|
|
569
|
-
var
|
|
570
|
-
|
|
571
|
-
const l = setTimeout(() => {
|
|
572
|
-
u(new Error("Could not connect"));
|
|
573
|
-
}, Te), g = (w) => {
|
|
574
|
-
clearTimeout(l), i(w);
|
|
575
|
-
}, u = (w) => {
|
|
576
|
-
clearTimeout(l), o(w);
|
|
577
|
-
};
|
|
578
|
-
if (!t && n.mode !== $.DirectPlayback)
|
|
603
|
+
var d, u, g;
|
|
604
|
+
if (N = 0, !t && n.mode !== I.DirectPlayback)
|
|
579
605
|
try {
|
|
580
|
-
t = await
|
|
581
|
-
} catch (
|
|
582
|
-
return
|
|
606
|
+
t = await te(e.id, s, a, n.mode, n.persistentChat);
|
|
607
|
+
} catch (h) {
|
|
608
|
+
return o(h);
|
|
583
609
|
}
|
|
584
|
-
|
|
610
|
+
if ((t == null ? void 0 : t.chat_mode) === I.TextOnly)
|
|
611
|
+
return (u = (d = n.callbacks) == null ? void 0 : d.onError) == null || u.call(d, new Error(JSON.stringify({
|
|
612
|
+
kind: "InsufficientCreditsError"
|
|
613
|
+
})), {
|
|
614
|
+
fromSdk: !0
|
|
615
|
+
}), i({
|
|
616
|
+
chat: t
|
|
617
|
+
});
|
|
618
|
+
const l = await Ie(e.id, Te(e, n, r), {
|
|
585
619
|
...n,
|
|
586
620
|
analytics: a,
|
|
587
|
-
warmup: (
|
|
621
|
+
warmup: (g = n.streamOptions) == null ? void 0 : g.streamWarmup,
|
|
588
622
|
callbacks: {
|
|
589
623
|
...n.callbacks,
|
|
590
|
-
onConnectionStateChange: async (
|
|
591
|
-
var
|
|
592
|
-
|
|
624
|
+
onConnectionStateChange: async (h) => {
|
|
625
|
+
var C, v, _, y;
|
|
626
|
+
h === M.Connected ? l ? ((v = (C = n.callbacks).onConnectionStateChange) == null || v.call(C, h), i({
|
|
593
627
|
chat: t,
|
|
594
|
-
streamingManager:
|
|
595
|
-
})) : t &&
|
|
628
|
+
streamingManager: l
|
|
629
|
+
})) : t && o(new Error("Something went wrong while initializing the manager")) : (y = (_ = n.callbacks).onConnectionStateChange) == null || y.call(_, h);
|
|
596
630
|
},
|
|
597
|
-
onVideoStateChange(
|
|
598
|
-
var
|
|
599
|
-
(
|
|
631
|
+
onVideoStateChange(h) {
|
|
632
|
+
var C, v;
|
|
633
|
+
(v = (C = n.callbacks).onVideoStateChange) == null || v.call(C, h), N > 0 && h === F.Start && a.linkTrack("agent-video", {
|
|
600
634
|
event: "start",
|
|
601
|
-
latency: Date.now() -
|
|
602
|
-
}, "start", [
|
|
635
|
+
latency: Date.now() - N
|
|
636
|
+
}, "start", [j.StreamVideoCreated]);
|
|
603
637
|
}
|
|
604
638
|
}
|
|
605
|
-
}).catch(
|
|
639
|
+
}).catch(o);
|
|
606
640
|
});
|
|
607
641
|
}
|
|
608
|
-
function
|
|
642
|
+
function xe(e) {
|
|
609
643
|
var s;
|
|
610
644
|
const n = (s = e.greetings) == null ? void 0 : s.filter((a) => a.length > 0);
|
|
611
645
|
if (n && n.length > 0) {
|
|
@@ -614,15 +648,15 @@ function Ne(e) {
|
|
|
614
648
|
} else
|
|
615
649
|
return `Hi! I'm ${e.preview_name || "My Agent"}. How can I help you?`;
|
|
616
650
|
}
|
|
617
|
-
function
|
|
651
|
+
function z(e, n) {
|
|
618
652
|
return n && n.length > 0 ? n : [{
|
|
619
653
|
content: e,
|
|
620
|
-
id:
|
|
654
|
+
id: V(),
|
|
621
655
|
role: "assistant",
|
|
622
656
|
created_at: (/* @__PURE__ */ new Date()).toISOString()
|
|
623
657
|
}];
|
|
624
658
|
}
|
|
625
|
-
function
|
|
659
|
+
function Ne(e) {
|
|
626
660
|
if (e.answer !== void 0)
|
|
627
661
|
return e.answer;
|
|
628
662
|
let n = 0, s = "";
|
|
@@ -631,7 +665,7 @@ function Oe(e) {
|
|
|
631
665
|
return s;
|
|
632
666
|
}
|
|
633
667
|
function Le(e, n, s, a, t) {
|
|
634
|
-
if (!(e ===
|
|
668
|
+
if (!(e === L.Partial || e === L.Answer))
|
|
635
669
|
return;
|
|
636
670
|
const r = a.messages[a.messages.length - 1];
|
|
637
671
|
if ((r == null ? void 0 : r.role) !== "assistant")
|
|
@@ -640,260 +674,252 @@ function Le(e, n, s, a, t) {
|
|
|
640
674
|
content: i,
|
|
641
675
|
sequence: o
|
|
642
676
|
} = n;
|
|
643
|
-
e ===
|
|
644
|
-
const l =
|
|
645
|
-
(r.content !== l || e ===
|
|
677
|
+
e === L.Partial ? s[o] = i : s.answer = i;
|
|
678
|
+
const l = Ne(s);
|
|
679
|
+
(r.content !== l || e === L.Answer) && (r.content = l, t == null || t([...a.messages], e));
|
|
646
680
|
}
|
|
647
|
-
async function
|
|
648
|
-
var
|
|
681
|
+
async function Fe(e, n) {
|
|
682
|
+
var y, $, P;
|
|
649
683
|
let s = {}, a = !0;
|
|
650
684
|
const t = {
|
|
651
685
|
messages: [],
|
|
652
|
-
chatMode: n.mode ||
|
|
653
|
-
}, r = n.baseURL ||
|
|
654
|
-
t.messages =
|
|
655
|
-
const
|
|
686
|
+
chatMode: n.mode || I.Functional
|
|
687
|
+
}, r = n.baseURL || U, i = n.wsURL || ue, o = n.mixpanelKey || he, l = G(n.auth, r, n.callbacks.onError), d = await l.getById(e), u = xe(d);
|
|
688
|
+
t.messages = z(u, n.initialMessages), ($ = (y = n.callbacks).onNewMessage) == null || $.call(y, [...t.messages], "answer");
|
|
689
|
+
const g = $e({
|
|
656
690
|
token: o,
|
|
657
|
-
agent:
|
|
691
|
+
agent: d,
|
|
658
692
|
isEnabled: n.enableAnalitics,
|
|
659
693
|
distinctId: n.distinctId
|
|
660
694
|
});
|
|
661
|
-
|
|
695
|
+
g.track("agent-sdk", {
|
|
662
696
|
event: "loaded",
|
|
663
|
-
...
|
|
697
|
+
...Ae(d)
|
|
664
698
|
});
|
|
665
|
-
const
|
|
666
|
-
onMessage: (c,
|
|
667
|
-
var
|
|
668
|
-
if ("content" in
|
|
669
|
-
Le(c,
|
|
699
|
+
const h = {
|
|
700
|
+
onMessage: (c, w) => {
|
|
701
|
+
var p, S;
|
|
702
|
+
if ("content" in w)
|
|
703
|
+
Le(c, w, s, t, n.callbacks.onNewMessage), c === L.Answer && g.track("agent-message-received", {
|
|
670
704
|
messages: t.messages.length,
|
|
671
705
|
mode: t.chatMode
|
|
672
706
|
});
|
|
673
707
|
else {
|
|
674
|
-
const
|
|
708
|
+
const m = j, f = [m.StreamVideoDone, m.StreamVideoError, m.StreamVideoRejected], k = [m.StreamFailed, m.StreamVideoError, m.StreamVideoRejected], E = Pe(w, d, {
|
|
675
709
|
mode: t.chatMode
|
|
676
710
|
});
|
|
677
|
-
if (c = c, c ===
|
|
678
|
-
|
|
679
|
-
else if (
|
|
680
|
-
const
|
|
681
|
-
|
|
682
|
-
...
|
|
683
|
-
event:
|
|
711
|
+
if (c = c, c === m.StreamVideoCreated)
|
|
712
|
+
g.linkTrack("agent-video", E, m.StreamVideoCreated, ["start"]);
|
|
713
|
+
else if (f.includes(c)) {
|
|
714
|
+
const R = c.split("/")[1];
|
|
715
|
+
g.track("agent-video", {
|
|
716
|
+
...E,
|
|
717
|
+
event: R
|
|
684
718
|
});
|
|
685
719
|
}
|
|
686
|
-
|
|
687
|
-
data:
|
|
688
|
-
})),
|
|
720
|
+
k.includes(c) && ((S = (p = n.callbacks).onError) == null || S.call(p, new Error(`Stream failed with event ${c}`), {
|
|
721
|
+
data: w
|
|
722
|
+
})), w.event === m.StreamDone && v();
|
|
689
723
|
}
|
|
690
724
|
}
|
|
691
725
|
};
|
|
692
|
-
async function
|
|
693
|
-
var
|
|
694
|
-
(
|
|
695
|
-
const h =
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
const d = M().catch((U) => {
|
|
711
|
-
var O, V;
|
|
712
|
-
throw _($.Maintenance), (V = (O = n.callbacks).onConnectionStateChange) == null || V.call(O, k.Fail), U;
|
|
713
|
-
}), [C, {
|
|
714
|
-
streamingManager: E,
|
|
715
|
-
chat: S
|
|
716
|
-
}] = await Promise.all([v, d]);
|
|
717
|
-
S && S.id !== ((T = t.chat) == null ? void 0 : T.id) && ((F = (x = n.callbacks).onNewChat) == null || F.call(x, S.id)), t.streamingManager = E, t.socketManager = C, t.chat = S, a = !1, _((S == null ? void 0 : S.chat_mode) ?? n.mode ?? $.Functional);
|
|
726
|
+
async function C(c) {
|
|
727
|
+
var k, E, R, b, K, D, T;
|
|
728
|
+
(E = (k = n.callbacks).onConnectionStateChange) == null || E.call(k, M.Connecting), N = 0, c && !a && (delete t.chat, t.messages = z(u), (b = (R = n.callbacks).onNewMessage) == null || b.call(R, [...t.messages], "answer"));
|
|
729
|
+
const w = n.mode === I.DirectPlayback ? Promise.resolve(void 0) : Ce(n.auth, i, h), p = X(() => De(d, n, l, g, t.chat, c ? u : void 0), {
|
|
730
|
+
limit: 3,
|
|
731
|
+
timeout: be,
|
|
732
|
+
timeoutErrorMessage: "Could not connect",
|
|
733
|
+
// Retry on all errors except for connection errors and rate limit errors, these are already handled in client level.
|
|
734
|
+
shouldRetryFn: (A) => (A == null ? void 0 : A.message) !== "Could not connect" && A.status !== 429,
|
|
735
|
+
delayMs: 1e3
|
|
736
|
+
}).catch((A) => {
|
|
737
|
+
var W, B;
|
|
738
|
+
throw _(I.Maintenance), (B = (W = n.callbacks).onConnectionStateChange) == null || B.call(W, M.Fail), A;
|
|
739
|
+
}), [S, {
|
|
740
|
+
streamingManager: m,
|
|
741
|
+
chat: f
|
|
742
|
+
}] = await Promise.all([w, p]);
|
|
743
|
+
f && f.id !== ((K = t.chat) == null ? void 0 : K.id) && ((T = (D = n.callbacks).onNewChat) == null || T.call(D, f.id)), t.streamingManager = m, t.socketManager = S, t.chat = f, a = !1, _((f == null ? void 0 : f.chat_mode) ?? n.mode ?? I.Functional);
|
|
718
744
|
}
|
|
719
|
-
async function
|
|
720
|
-
var c,
|
|
721
|
-
(c = t.socketManager) == null || c.disconnect(), await ((
|
|
745
|
+
async function v() {
|
|
746
|
+
var c, w, p, S;
|
|
747
|
+
(c = t.socketManager) == null || c.disconnect(), await ((w = t.streamingManager) == null ? void 0 : w.disconnect()), delete t.streamingManager, delete t.socketManager, (S = (p = n.callbacks).onConnectionStateChange) == null || S.call(p, M.Disconnected);
|
|
722
748
|
}
|
|
723
749
|
async function _(c) {
|
|
724
|
-
var
|
|
725
|
-
c !== t.chatMode && (
|
|
750
|
+
var w, p;
|
|
751
|
+
c !== t.chatMode && (g.track("agent-mode-change", {
|
|
726
752
|
mode: c
|
|
727
|
-
}), t.chatMode = c, t.chatMode !==
|
|
753
|
+
}), t.chatMode = c, t.chatMode !== I.Functional && await v(), (p = (w = n.callbacks).onModeChange) == null || p.call(w, c));
|
|
728
754
|
}
|
|
729
755
|
return {
|
|
730
|
-
agent:
|
|
731
|
-
starterMessages: ((
|
|
732
|
-
getSTTToken: () => l.getSTTToken(
|
|
756
|
+
agent: d,
|
|
757
|
+
starterMessages: ((P = d.knowledge) == null ? void 0 : P.starter_message) || [],
|
|
758
|
+
getSTTToken: () => l.getSTTToken(d.id),
|
|
733
759
|
changeMode: _,
|
|
734
|
-
enrichAnalytics:
|
|
760
|
+
enrichAnalytics: g.enrich,
|
|
735
761
|
async connect() {
|
|
736
762
|
var c;
|
|
737
|
-
await
|
|
763
|
+
await C(!0), g.track("agent-chat", {
|
|
738
764
|
event: "connect",
|
|
739
765
|
chatId: (c = t.chat) == null ? void 0 : c.id,
|
|
740
|
-
agentId:
|
|
766
|
+
agentId: d.id,
|
|
741
767
|
mode: t.chatMode
|
|
742
768
|
});
|
|
743
769
|
},
|
|
744
770
|
async reconnect() {
|
|
745
771
|
var c;
|
|
746
|
-
await
|
|
772
|
+
await v(), await C(!1), g.track("agent-chat", {
|
|
747
773
|
event: "reconnect",
|
|
748
774
|
chatId: (c = t.chat) == null ? void 0 : c.id,
|
|
749
|
-
agentId:
|
|
775
|
+
agentId: d.id,
|
|
750
776
|
mode: t.chatMode
|
|
751
777
|
});
|
|
752
778
|
},
|
|
753
779
|
async disconnect() {
|
|
754
780
|
var c;
|
|
755
|
-
await
|
|
781
|
+
await v(), g.track("agent-chat", {
|
|
756
782
|
event: "disconnect",
|
|
757
783
|
chatId: (c = t.chat) == null ? void 0 : c.id,
|
|
758
|
-
agentId:
|
|
784
|
+
agentId: d.id,
|
|
759
785
|
mode: t.chatMode
|
|
760
786
|
});
|
|
761
787
|
},
|
|
762
788
|
async chat(c) {
|
|
763
|
-
var
|
|
764
|
-
const
|
|
789
|
+
var p, S, m, f, k, E;
|
|
790
|
+
const w = V();
|
|
765
791
|
s = {};
|
|
766
792
|
try {
|
|
767
|
-
if (
|
|
793
|
+
if (N = Date.now(), n.mode === I.DirectPlayback)
|
|
768
794
|
throw new Error("Direct playback is enabled, chat is disabled");
|
|
769
795
|
if (c.length >= 800)
|
|
770
796
|
throw new Error("Message cannot be more than 800 characters");
|
|
771
797
|
if (c.length === 0)
|
|
772
798
|
throw new Error("Message cannot be empty");
|
|
773
|
-
if (t.chatMode ===
|
|
799
|
+
if (t.chatMode === I.Maintenance)
|
|
774
800
|
throw new Error("Chat is in maintenance mode");
|
|
775
|
-
if (![
|
|
801
|
+
if (![I.TextOnly, I.Playground].includes(t.chatMode))
|
|
776
802
|
if (t.streamingManager) {
|
|
777
803
|
if (!t.chat)
|
|
778
804
|
throw new Error("Chat is not initialized");
|
|
779
805
|
} else
|
|
780
806
|
throw new Error("Streaming manager is not initialized");
|
|
781
807
|
t.messages.push({
|
|
782
|
-
id:
|
|
808
|
+
id: V(),
|
|
783
809
|
role: "user",
|
|
784
810
|
content: c,
|
|
785
|
-
created_at: new Date(
|
|
786
|
-
}), (
|
|
787
|
-
const
|
|
788
|
-
id:
|
|
811
|
+
created_at: new Date(N).toISOString()
|
|
812
|
+
}), (S = (p = n.callbacks).onNewMessage) == null || S.call(p, [...t.messages], "user"), t.chat || (t.chat = await te(d.id, l, g, t.chatMode, n.persistentChat), (f = (m = n.callbacks).onNewChat) == null || f.call(m, t.chat.id));
|
|
813
|
+
const R = {
|
|
814
|
+
id: w,
|
|
789
815
|
role: "assistant",
|
|
790
816
|
content: "",
|
|
791
817
|
created_at: (/* @__PURE__ */ new Date()).toISOString(),
|
|
792
818
|
matches: []
|
|
793
|
-
},
|
|
794
|
-
t.messages.push(
|
|
795
|
-
const
|
|
796
|
-
var
|
|
797
|
-
return l.chat(
|
|
798
|
-
sessionId: (
|
|
799
|
-
streamId: (
|
|
819
|
+
}, b = [...t.messages];
|
|
820
|
+
t.messages.push(R);
|
|
821
|
+
const K = (T) => {
|
|
822
|
+
var A, W;
|
|
823
|
+
return l.chat(d.id, T, {
|
|
824
|
+
sessionId: (A = t.streamingManager) == null ? void 0 : A.sessionId,
|
|
825
|
+
streamId: (W = t.streamingManager) == null ? void 0 : W.streamId,
|
|
800
826
|
chatMode: t.chatMode,
|
|
801
|
-
messages:
|
|
802
|
-
matches:
|
|
803
|
-
...
|
|
804
|
-
}) =>
|
|
805
|
-
},
|
|
806
|
-
}, D = await
|
|
807
|
-
var
|
|
808
|
-
if (!((
|
|
827
|
+
messages: b.map(({
|
|
828
|
+
matches: B,
|
|
829
|
+
...ne
|
|
830
|
+
}) => ne)
|
|
831
|
+
}, ee(t.chatMode));
|
|
832
|
+
}, D = await K(t.chat.id).catch(async (T) => {
|
|
833
|
+
var A;
|
|
834
|
+
if (!((A = T == null ? void 0 : T.message) != null && A.includes("missing or invalid session_id")))
|
|
809
835
|
throw T;
|
|
810
|
-
return await
|
|
836
|
+
return await v(), await C(!1), K(t.chat.id);
|
|
811
837
|
});
|
|
812
|
-
return
|
|
838
|
+
return g.track("agent-message-send", {
|
|
813
839
|
event: "success",
|
|
814
840
|
mode: t.chatMode,
|
|
815
841
|
messages: t.messages.length + 1
|
|
816
|
-
}),
|
|
817
|
-
latency: Date.now() -
|
|
842
|
+
}), R.context = D.context, R.matches = D.matches, D.result && (R.content = D.result, g.track("agent-message-received", {
|
|
843
|
+
latency: Date.now() - N,
|
|
818
844
|
mode: t.chatMode,
|
|
819
845
|
messages: t.messages.length
|
|
820
|
-
}), (
|
|
821
|
-
} catch (
|
|
822
|
-
throw t.messages[t.messages.length - 1].id ===
|
|
846
|
+
}), (E = (k = n.callbacks).onNewMessage) == null || E.call(k, [...t.messages], "answer")), D;
|
|
847
|
+
} catch (R) {
|
|
848
|
+
throw t.messages[t.messages.length - 1].id === w && t.messages.pop(), g.track("agent-message-send", {
|
|
823
849
|
event: "error",
|
|
824
850
|
mode: t.chatMode,
|
|
825
851
|
messages: t.messages.length
|
|
826
|
-
}),
|
|
852
|
+
}), R;
|
|
827
853
|
}
|
|
828
854
|
},
|
|
829
|
-
rate(c,
|
|
830
|
-
var
|
|
831
|
-
const
|
|
855
|
+
rate(c, w, p) {
|
|
856
|
+
var f, k, E, R;
|
|
857
|
+
const S = t.messages.find((b) => b.id === c);
|
|
832
858
|
if (t.chat) {
|
|
833
|
-
if (!
|
|
859
|
+
if (!S)
|
|
834
860
|
throw new Error("Message not found");
|
|
835
861
|
} else
|
|
836
862
|
throw new Error("Chat is not initialized");
|
|
837
|
-
const
|
|
838
|
-
return
|
|
839
|
-
event:
|
|
840
|
-
thumb:
|
|
841
|
-
knowledge_id: ((
|
|
863
|
+
const m = ((f = S.matches) == null ? void 0 : f.map((b) => [b.document_id, b.id])) ?? [];
|
|
864
|
+
return g.track("agent-rate", {
|
|
865
|
+
event: p ? "update" : "create",
|
|
866
|
+
thumb: w === 1 ? "up" : "down",
|
|
867
|
+
knowledge_id: ((k = d.knowledge) == null ? void 0 : k.id) ?? "",
|
|
842
868
|
mode: t.chatMode,
|
|
843
|
-
matches:
|
|
844
|
-
score:
|
|
845
|
-
}),
|
|
846
|
-
knowledge_id: ((
|
|
869
|
+
matches: m,
|
|
870
|
+
score: w
|
|
871
|
+
}), p ? l.updateRating(d.id, t.chat.id, p, {
|
|
872
|
+
knowledge_id: ((E = d.knowledge) == null ? void 0 : E.id) ?? "",
|
|
847
873
|
message_id: c,
|
|
848
|
-
matches:
|
|
849
|
-
score:
|
|
850
|
-
}) : l.createRating(
|
|
851
|
-
knowledge_id: ((
|
|
874
|
+
matches: m,
|
|
875
|
+
score: w
|
|
876
|
+
}) : l.createRating(d.id, t.chat.id, {
|
|
877
|
+
knowledge_id: ((R = d.knowledge) == null ? void 0 : R.id) ?? "",
|
|
852
878
|
message_id: c,
|
|
853
|
-
matches:
|
|
854
|
-
score:
|
|
879
|
+
matches: m,
|
|
880
|
+
score: w
|
|
855
881
|
});
|
|
856
882
|
},
|
|
857
883
|
deleteRate(c) {
|
|
858
|
-
var
|
|
884
|
+
var w;
|
|
859
885
|
if (!t.chat)
|
|
860
886
|
throw new Error("Chat is not initialized");
|
|
861
|
-
return
|
|
887
|
+
return g.track("agent-rate-delete", {
|
|
862
888
|
type: "text",
|
|
863
|
-
chat_id: (
|
|
889
|
+
chat_id: (w = t.chat) == null ? void 0 : w.id,
|
|
864
890
|
id: c,
|
|
865
891
|
mode: t.chatMode
|
|
866
|
-
}), l.deleteRating(
|
|
892
|
+
}), l.deleteRating(d.id, t.chat.id, c);
|
|
867
893
|
},
|
|
868
894
|
speak(c) {
|
|
869
895
|
if (!t.streamingManager)
|
|
870
896
|
throw new Error("Please connect to the agent first");
|
|
871
|
-
function
|
|
897
|
+
function w() {
|
|
872
898
|
if (typeof c == "string") {
|
|
873
|
-
if (!
|
|
899
|
+
if (!d.presenter.voice)
|
|
874
900
|
throw new Error("Presenter voice is not initialized");
|
|
875
901
|
return {
|
|
876
902
|
type: "text",
|
|
877
|
-
provider:
|
|
903
|
+
provider: d.presenter.voice,
|
|
878
904
|
input: c,
|
|
879
905
|
ssml: !1
|
|
880
906
|
};
|
|
881
907
|
}
|
|
882
908
|
if (c.type === "text" && !c.provider) {
|
|
883
|
-
if (!
|
|
909
|
+
if (!d.presenter.voice)
|
|
884
910
|
throw new Error("Presenter voice is not initialized");
|
|
885
911
|
return {
|
|
886
912
|
type: "text",
|
|
887
|
-
provider:
|
|
913
|
+
provider: d.presenter.voice,
|
|
888
914
|
input: c.input,
|
|
889
915
|
ssml: c.ssml
|
|
890
916
|
};
|
|
891
917
|
}
|
|
892
918
|
return c;
|
|
893
919
|
}
|
|
894
|
-
const
|
|
895
|
-
return
|
|
896
|
-
script:
|
|
920
|
+
const p = w();
|
|
921
|
+
return g.track("agent-speak", p), t.streamingManager.speak({
|
|
922
|
+
script: p
|
|
897
923
|
});
|
|
898
924
|
}
|
|
899
925
|
};
|
|
@@ -901,26 +927,26 @@ async function je(e, n) {
|
|
|
901
927
|
function Ke(e, n, s) {
|
|
902
928
|
const {
|
|
903
929
|
getById: a
|
|
904
|
-
} =
|
|
930
|
+
} = G(n, s || U);
|
|
905
931
|
return a(e);
|
|
906
932
|
}
|
|
907
933
|
export {
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
|
|
913
|
-
|
|
914
|
-
|
|
915
|
-
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
|
|
921
|
-
|
|
922
|
-
|
|
923
|
-
|
|
934
|
+
ie as AgentStatus,
|
|
935
|
+
I as ChatMode,
|
|
936
|
+
L as ChatProgress,
|
|
937
|
+
M as ConnectionState,
|
|
938
|
+
de as DocumentType,
|
|
939
|
+
ce as KnowledgeType,
|
|
940
|
+
ae as PlanGroup,
|
|
941
|
+
me as Providers,
|
|
942
|
+
se as RateState,
|
|
943
|
+
j as StreamEvents,
|
|
944
|
+
F as StreamingState,
|
|
945
|
+
oe as Subject,
|
|
946
|
+
re as UserPlan,
|
|
947
|
+
O as VideoType,
|
|
948
|
+
ge as VoiceAccess,
|
|
949
|
+
Fe as createAgentManager,
|
|
924
950
|
Ke as getAgent,
|
|
925
|
-
|
|
951
|
+
le as mapVideoType
|
|
926
952
|
};
|
package/dist/index.umd.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
(function(v,W){typeof exports=="object"&&typeof module<"u"?W(exports):typeof define=="function"&&define.amd?define(["exports"],W):(v=typeof globalThis<"u"?globalThis:v||self,W(v.index={}))})(this,function(v){"use strict";var W=(e=>(e.TRIAL="trial",e.BASIC="basic",e.ENTERPRISE="enterprise",e.LITE="lite",e.ADVANCED="advanced",e))(W||{}),G=(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))(G||{}),Q=(e=>(e.Created="created",e.Started="started",e.Done="done",e.Error="error",e.Rejected="rejected",e.Ready="ready",e))(Q||{}),ee=(e=>(e.Unrated="Unrated",e.Positive="Positive",e.Negative="Negative",e))(ee||{}),$=(e=>(e.Functional="Functional",e.TextOnly="TextOnly",e.Maintenance="Maintenance",e.Playground="Playground",e.DirectPlayback="DirectPlayback",e))($||{}),x=(e=>(e.Embed="embed",e.Query="query",e.Partial="partial",e.Answer="answer",e.Complete="done",e))(x||{}),te=(e=>(e.KnowledgeProcessing="knowledge/processing",e.KnowledgeIndexing="knowledge/indexing",e.KnowledgeFailed="knowledge/error",e.KnowledgeDone="knowledge/done",e))(te||{}),ne=(e=>(e.Knowledge="knowledge",e.Document="document",e.Record="record",e))(ne||{}),re=(e=>(e.Pdf="pdf",e.Text="text",e.Html="html",e.Word="word",e.Json="json",e.Markdown="markdown",e.Csv="csv",e.Excel="excel",e.Powerpoint="powerpoint",e.Archive="archive",e.Image="image",e.Audio="audio",e.Video="video",e))(re||{}),X=(e=>(e.Clip="clip",e.Talk="talk",e))(X||{});const ae=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||{}),F=(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))(F||{}),M=(e=>(e.New="new",e.Fail="fail",e.Connected="connected",e.Connecting="connecting",e.Closed="closed",e.Completed="completed",e.Disconnected="disconnected",e))(M||{}),ie=(e=>(e.Amazon="amazon",e.Microsoft="microsoft",e.Afflorithmics="afflorithmics",e.Elevenlabs="elevenlabs",e))(ie||{}),se=(e=>(e.Public="public",e.Premium="premium",e.Private="private",e))(se||{});const U="https://api.d-id.com",fe="wss://notifications.d-id.com",pe="79f81a83a67430be2bc0fd61042b8faa",J=()=>Math.random().toString(16).slice(2);function oe(){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 ye=J();function ce(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}.${oe()}_${ye}`;throw new Error(`Unknown auth type: ${e}`)}function Y(e,n=U,s){const a=async(t,r)=>{const i=await fetch(n+(t!=null&&t.startsWith("/")?t:`/${t}`),{...r,headers:{...r==null?void 0:r.headers,Authorization:ce(e),"Content-Type":"application/json"}});if(!i.ok){let o=await i.text().catch(()=>"Failed to fetch");throw s&&s(new Error(o),{url:t,options:r,headers:i.headers}),new Error(o)}return i.json()};return{get(t,r){return a(t,{...r,method:"GET"})},post(t,r,i){return a(t,{...i,body:JSON.stringify(r),method:"POST"})},delete(t,r,i){return a(t,{...i,body:JSON.stringify(r),method:"DELETE"})},patch(t,r,i){return a(t,{...i,body:JSON.stringify(r),method:"PATCH"})}}}function de(e,n=U,s){const a=Y(e,`${n}/agents`,s);return{create(t,r){return a.post("/",t,r)},getAgents(t,r){return a.get(`/${t?`?tag=${t}`:""}`,r).then(i=>i??[])},getById(t,r){return a.get(`/${t}`,r)},delete(t,r){return a.delete(`/${t}`,void 0,r)},update(t,r,i){return a.patch(`/${t}`,r,i)},newChat(t,r,i){return a.post(`/${t}/chat`,r,i)},chat(t,r,i,o){return a.post(`/${t}/chat/${r}`,i,o)},createRating(t,r,i,o){return a.post(`/${t}/chat/${r}/ratings`,i,o)},updateRating(t,r,i,o,l){return a.patch(`/${t}/chat/${r}/ratings/${i}`,o,l)},deleteRating(t,r,i,o){return a.delete(`/${t}/chat/${r}/ratings/${i}`,o)},getSTTToken(t,r){return a.get(`/${t}/stt-token`,r)}}}const ve=e=>new Promise(n=>setTimeout(n,e));function Ce(e){return new Promise((n,s)=>{const{callbacks:a,host:t,auth:r}=e,{onMessage:i=null,onOpen:o=null,onClose:l=null,onError:g=null}=a||{},m=new WebSocket(`${t}?authorization=${ce(r)}`);m.onmessage=i,m.onclose=l,m.onerror=u=>{console.error(u),g==null||g("Websocket failed to connect",u),s(u)},m.onopen=u=>{o==null||o(u),n(m)}})}async function Se(e){const{retries:n=1}=e;let s=null;for(let a=0;(s==null?void 0:s.readyState)!==WebSocket.OPEN;a++)try{s=await Ce(e)}catch(t){if(a===n)throw t;await ve(a*500)}return s}async function _e(e,n,s){const a=s!=null&&s.onMessage?[s.onMessage]:[],t=await Se({auth:e,host:n,callbacks:{onError:s==null?void 0:s.onError,onMessage:r=>{const i=JSON.parse(r.data);a.forEach(o=>o(i.event,i))}}});return{socket:t,disconnect:()=>t.close(),subscribeToEvents:r=>a.push(r)}}const ke="X-Playground-Chat";function Me(e,n,s,a){const t=Y(e,`${n}/agents/${s}`,a);return{createStream(r){return t.post("/streams",{output_resolution:r.output_resolution,compatibility_mode:r.compatibility_mode,stream_warmup:r.stream_warmup,session_timeout:r.session_timeout,stream_greeting:r.stream_greeting})},startConnection(r,i,o){return t.post(`/streams/${r}/sdp`,{session_id:o,answer:i})},addIceCandidate(r,i,o){return t.post(`/streams/${r}/ice`,{session_id:o,...i})},sendStreamRequest(r,i,o){return t.post(`/streams/${r}`,{session_id:i,...o})},close(r,i){return t.delete(`/streams/${r}`,{session_id:i})}}}function Re(e,n,s,a){const t=Y(e,`${n}/agents/${s}`,a);return{createStream(r,i){return t.post("/streams",{driver_url:r.driver_url,face:r.face,config:r.config,output_resolution:r.output_resolution,compatibility_mode:r.compatibility_mode,stream_warmup:r.stream_warmup,session_timeout:r.session_timeout,stream_greeting:r.stream_greeting},i)},startConnection(r,i,o,l){return t.post(`/streams/${r}/sdp`,{session_id:o,answer:i},l)},addIceCandidate(r,i,o,l){return t.post(`/streams/${r}/ice`,{session_id:o,...i},l)},sendStreamRequest(r,i,o,l){return t.post(`/streams/${r}`,{session_id:i,...o},l)},close(r,i,o){return t.delete(`/streams/${r}`,{session_id:i},o)}}}let le=!1;const O=(e,n)=>le&&console.log(e,n),Ee=(window.RTCPeerConnection||window.webkitRTCPeerConnection||window.mozRTCPeerConnection).bind(window);function ge(e){switch(e){case"connected":return M.Connected;case"checking":return M.Connecting;case"failed":return M.Fail;case"new":return M.New;case"closed":return M.Closed;case"disconnected":return M.Disconnected;case"completed":return M.Completed;default:return M.New}}function Ie(){let e=0;return n=>{for(const s of n.values())if(s&&s.type==="inbound-rtp"&&s.kind==="video"){const a=s.bytesReceived,t=a-e>0;return e=a,t}return!1}}function Ae(e,n,s=!1,a,t){const i=Math.max(Math.ceil(10),1);let o=0,l=!1;const g=s?1:0;let m=0;const u=Ie();return setInterval(async()=>{const p=await e.getStats();u(p)?(o=0,l||(n==null||n(j.Start),m>=g&&!a()&&t(),m++,l=!0)):l&&(o++,o>=i&&(n==null||n(j.Stop),l=!1))},100)}async function $e(e,n,{debug:s=!1,callbacks:a,auth:t,baseURL:r=U,warmup:i}){le=s;const{startConnection:o,sendStreamRequest:l,close:g,createStream:m,addIceCandidate:u}=n.videoType===X.Clip?Me(t,r,e,a.onError):Re(t,r,e,a.onError),{id:p,offer:w,ice_servers:y,session_id:_}=await m(n),f=new Ee({iceServers:y}),E=f.createDataChannel("JanusDataChannel");if(!_)throw new Error("Could not create session_id");let T=!1;const c=()=>T,h=()=>{var d;T=!0,(d=a.onConnectionStateChange)==null||d.call(a,M.Connected)},C=Ae(f,a.onVideoStateChange,i,c,h);f.onicecandidate=d=>{var S;O("peerConnection.onicecandidate",d);try{d.candidate&&d.candidate.sdpMid&&d.candidate.sdpMLineIndex!==null?u(p,{candidate:d.candidate.candidate,sdpMid:d.candidate.sdpMid,sdpMLineIndex:d.candidate.sdpMLineIndex},_):u(p,{candidate:null},_)}catch(I){(S=a.onError)==null||S.call(a,I,{streamId:p})}},E.onmessage=d=>{if(E.readyState==="open"){const[S,I]=d.data.split(":");S===F.StreamReady&&!T&&h()}},f.oniceconnectionstatechange=()=>{var S;O("peerConnection.oniceconnectionstatechange => "+f.iceConnectionState);const d=ge(f.iceConnectionState);d!==M.Connected&&((S=a.onConnectionStateChange)==null||S.call(a,d))},f.ontrack=d=>{var S;O("peerConnection.ontrack",d),(S=a.onSrcObjectReady)==null||S.call(a,d.streams[0])},await f.setRemoteDescription(w),O("set remote description OK");const R=await f.createAnswer();return O("create answer OK"),await f.setLocalDescription(R),O("set local description OK"),await o(p,R,_),O("start connection OK"),{speak(d){return l(p,_,d)},async disconnect(){var d,S;if(p){const I=ge(f.iceConnectionState);if(f){if(I===M.New){(d=a.onVideoStateChange)==null||d.call(a,j.Stop),clearInterval(C);return}f.close(),f.oniceconnectionstatechange=null,f.onnegotiationneeded=null,f.onicecandidate=null,f.ontrack=null}try{I===M.Connected&&await g(p,_).catch(k=>{})}catch(k){O("Error on close stream connection",k)}(S=a.onVideoStateChange)==null||S.call(a,j.Stop),clearInterval(C)}},sessionId:_,streamId:p}}let Z={};function be(e){var r,i,o,l,g;const n=window!=null&&window.hasOwnProperty("DID_AGENTS_API")?"agents-ui":"agents-sdk",s=e.agent.presenter,a=(r=e.agent.llm)==null?void 0:r.prompt_customization,t={token:e.token||"testKey",distinct_id:e.distinctId||oe(),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:a==null?void 0:a.role,personality:a==null?void 0:a.personality,instructions:(i=e.agent.llm)==null?void 0:i.instructions},temperature:(o=e.agent.llm)==null?void 0:o.temperature,knowledgeSource:a==null?void 0:a.knowledge_source,starterQuestionsCount:(g=(l=e.agent.knowledge)==null?void 0:l.starter_message)==null?void 0:g.length,topicsToAvoid:a==null?void 0:a.topics_to_avoid,maxResponseLength:a==null?void 0:a.max_response_length};return{...t,additionalProperties:{},isEnabled:e.isEnabled??!0,getRandom:()=>Math.random().toString(16).slice(2),enrich(m){const u={};if(m&&typeof m!="object")throw new Error("properties must be a flat json object");for(let p in m)(typeof m[p]=="string"||typeof m[p]=="number")&&(u[p]=m[p]);this.additionalProperties={...this.additionalProperties,...u}},track(m,u){if(!this.isEnabled)return Promise.resolve();const{audioPath:p,...w}=u||{},y={method:"POST",headers:{"Content-Type":"application/x-www-form-urlencoded"},body:new URLSearchParams({data:JSON.stringify([{event:m,properties:{...this.additionalProperties,...w,...t,source:n,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",y).then(_=>_.json()).catch(_=>console.error(_))},linkTrack(m,u,p,w){Z[m]||(Z[m]={events:{},resolvedDependencies:[]}),w.includes(p)||w.push(p);const y=Z[m];if(y.events[p]={props:u},y.resolvedDependencies.push(p),w.every(f=>y.resolvedDependencies.includes(f))){const f=w.reduce((E,T)=>y.events[T]?{...E,...y.events[T].props}:E,{});this.track(m,f),y.resolvedDependencies=y.resolvedDependencies.filter(E=>!w.includes(E)),w.forEach(E=>{delete y.events[E]})}}}}function Te(e){var t,r,i,o;const n=()=>/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"},a=e.presenter;return{$os:`${s()}`,isMobile:`${n()=="Mobile"}`,browser:navigator.userAgent,origin:window.location.origin,agentType:a.type==="clip"&&a.presenter_id.startsWith("v2_")?"clip_v2":a.type,agentVoice:{voiceId:(r=(t=e.presenter)==null?void 0:t.voice)==null?void 0:r.voice_id,provider:(o=(i=e.presenter)==null?void 0:i.voice)==null?void 0:o.type}}}function Pe(e,n,s){var g,m;const{event:a,...t}=e,{template:r}=(n==null?void 0:n.llm)||{},{language:i}=((g=n==null?void 0:n.presenter)==null?void 0:g.voice)||{},{stitch:o}=(n==null?void 0:n.presenter)||{};return{...t,llm:{...t.llm,template:r},script:{...t.script,provider:{...(m=t==null?void 0:t.script)==null?void 0:m.provider,language:i}},stitch:o,...s}}let K=0;const De=20*1e3;function Oe(e,n,s){var a,t,r,i,o;return{videoType:ae(e.presenter.type),output_resolution:(a=n==null?void 0:n.streamOptions)==null?void 0:a.outputResolution,session_timeout:(t=n==null?void 0:n.streamOptions)==null?void 0:t.sessionTimeout,stream_warmup:(r=n==null?void 0:n.streamOptions)==null?void 0:r.streamWarmup,compatibility_mode:(i=n==null?void 0:n.streamOptions)==null?void 0:i.compatibilityMode,stream_greeting:(o=n==null?void 0:n.streamOptions)!=null&&o.streamGreeting?s:void 0}}function ue(e){return e===$.Playground?{headers:{[ke]:"true"}}:{}}async function me(e,n,s,a,t){try{const r=await n.newChat(e,{persist:t??!1},ue(a));return s.track("agent-chat",{event:"created",chat_id:r.id,agent_id:e,mode:a}),r}catch(r){let i;try{i=JSON.parse(r.message)}catch(o){console.error("Error parsing the error message:",o)}throw(i==null?void 0:i.kind)==="InsufficientCreditsError"?new Error("InsufficientCreditsError"):new Error("Cannot create new chat")}}function Ne(e,n,s,a,t,r){return new Promise(async(i,o)=>{var p;K=0;const l=setTimeout(()=>{m(new Error("Could not connect"))},De),g=w=>{clearTimeout(l),i(w)},m=w=>{clearTimeout(l),o(w)};if(!t&&n.mode!==$.DirectPlayback)try{t=await me(e.id,s,a,n.mode,n.persistentChat)}catch(w){return m(w)}const u=await $e(e.id,Oe(e,n,r),{...n,analytics:a,warmup:(p=n.streamOptions)==null?void 0:p.streamWarmup,callbacks:{...n.callbacks,onConnectionStateChange:async w=>{var y,_,f,E;w===M.Connected?u?((_=(y=n.callbacks).onConnectionStateChange)==null||_.call(y,w),g({chat:t,streamingManager:u})):t&&m(new Error("Something went wrong while initializing the manager")):(E=(f=n.callbacks).onConnectionStateChange)==null||E.call(f,w)},onVideoStateChange(w){var y,_;(_=(y=n.callbacks).onVideoStateChange)==null||_.call(y,w),K>0&&w===j.Start&&a.linkTrack("agent-video",{event:"start",latency:Date.now()-K},"start",[F.StreamVideoCreated])}}}).catch(m)})}function Le(e){var s;const n=(s=e.greetings)==null?void 0:s.filter(a=>a.length>0);if(n&&n.length>0){const a=Math.floor(Math.random()*n.length);return n[a]}else return`Hi! I'm ${e.preview_name||"My Agent"}. How can I help you?`}function he(e,n){return n&&n.length>0?n:[{content:e,id:J(),role:"assistant",created_at:new Date().toISOString()}]}function xe(e){if(e.answer!==void 0)return e.answer;let n=0,s="";for(;n in e;)s+=e[n],n++;return s}function je(e,n,s,a,t){if(!(e===x.Partial||e===x.Answer))return;const r=a.messages[a.messages.length-1];if((r==null?void 0:r.role)!=="assistant")return;const{content:i,sequence:o}=n;e===x.Partial?s[o]=i:s.answer=i;const l=xe(s);(r.content!==l||e===x.Answer)&&(r.content=l,t==null||t([...a.messages],e))}async function Ke(e,n){var f,E,T;let s={},a=!0;const t={messages:[],chatMode:n.mode||$.Functional},r=n.baseURL||U,i=n.wsURL||fe,o=n.mixpanelKey||pe,l=de(n.auth,r,n.callbacks.onError),g=await l.getById(e),m=Le(g);t.messages=he(m,n.initialMessages),(E=(f=n.callbacks).onNewMessage)==null||E.call(f,[...t.messages],"answer");const u=be({token:o,agent:g,isEnabled:n.enableAnalitics,distinctId:n.distinctId});u.track("agent-sdk",{event:"loaded",...Te(g)});const p={onMessage:(c,h)=>{var C,R;if("content"in h)je(c,h,s,t,n.callbacks.onNewMessage),c===x.Answer&&u.track("agent-message-received",{messages:t.messages.length,mode:t.chatMode});else{const d=F,S=[d.StreamVideoDone,d.StreamVideoError,d.StreamVideoRejected],I=[d.StreamFailed,d.StreamVideoError,d.StreamVideoRejected],k=Pe(h,g,{mode:t.chatMode});if(c=c,c===d.StreamVideoCreated)u.linkTrack("agent-video",k,d.StreamVideoCreated,["start"]);else if(S.includes(c)){const A=c.split("/")[1];u.track("agent-video",{...k,event:A})}I.includes(c)&&((R=(C=n.callbacks).onError)==null||R.call(C,new Error(`Stream failed with event ${c}`),{data:h})),h.event===d.StreamDone&&y()}}};async function w(c){var A,P,B,N,D,L,H;(P=(A=n.callbacks).onConnectionStateChange)==null||P.call(A,M.Connecting),K=0;const h=3;c&&!a&&(delete t.chat,t.messages=he(m),(N=(B=n.callbacks).onNewMessage)==null||N.call(B,[...t.messages],"answer"));const C=n.mode===$.DirectPlayback?Promise.resolve(void 0):_e(n.auth,i,p);async function R(){var q,V,z;for(let we=1;we<=h;we++)try{const b=await Ne(g,n,l,u,t.chat,c?m:void 0);return b.chat&&b.chat.id!==((q=t.chat)==null?void 0:q.id)&&(t.chat=b.chat,(z=(V=n.callbacks).onNewChat)==null||z.call(V,b.chat.id)),b}catch(b){if((b==null?void 0:b.message)!=="Could not connect")throw b}throw new Error("Could not connect")}const d=R().catch(q=>{var V,z;throw _($.Maintenance),(z=(V=n.callbacks).onConnectionStateChange)==null||z.call(V,M.Fail),q}),[S,{streamingManager:I,chat:k}]=await Promise.all([C,d]);k&&k.id!==((D=t.chat)==null?void 0:D.id)&&((H=(L=n.callbacks).onNewChat)==null||H.call(L,k.id)),t.streamingManager=I,t.socketManager=S,t.chat=k,a=!1,_((k==null?void 0:k.chat_mode)??n.mode??$.Functional)}async function y(){var c,h,C,R;(c=t.socketManager)==null||c.disconnect(),await((h=t.streamingManager)==null?void 0:h.disconnect()),delete t.streamingManager,delete t.socketManager,(R=(C=n.callbacks).onConnectionStateChange)==null||R.call(C,M.Disconnected)}async function _(c){var h,C;c!==t.chatMode&&(u.track("agent-mode-change",{mode:c}),t.chatMode=c,t.chatMode!==$.Functional&&await y(),(C=(h=n.callbacks).onModeChange)==null||C.call(h,c))}return{agent:g,starterMessages:((T=g.knowledge)==null?void 0:T.starter_message)||[],getSTTToken:()=>l.getSTTToken(g.id),changeMode:_,enrichAnalytics:u.enrich,async connect(){var c;await w(!0),u.track("agent-chat",{event:"connect",chatId:(c=t.chat)==null?void 0:c.id,agentId:g.id,mode:t.chatMode})},async reconnect(){var c;await y(),await w(!1),u.track("agent-chat",{event:"reconnect",chatId:(c=t.chat)==null?void 0:c.id,agentId:g.id,mode:t.chatMode})},async disconnect(){var c;await y(),u.track("agent-chat",{event:"disconnect",chatId:(c=t.chat)==null?void 0:c.id,agentId:g.id,mode:t.chatMode})},async chat(c){var C,R,d,S,I,k;const h=J();s={};try{if(K=Date.now(),n.mode===$.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(t.chatMode===$.Maintenance)throw new Error("Chat is in maintenance mode");if(![$.TextOnly,$.Playground].includes(t.chatMode))if(t.streamingManager){if(!t.chat)throw new Error("Chat is not initialized")}else throw new Error("Streaming manager is not initialized");t.messages.push({id:J(),role:"user",content:c,created_at:new Date(K).toISOString()}),(R=(C=n.callbacks).onNewMessage)==null||R.call(C,[...t.messages],"user"),t.chat||(t.chat=await me(g.id,l,u,t.chatMode,n.persistentChat),(S=(d=n.callbacks).onNewChat)==null||S.call(d,t.chat.id));const A={id:h,role:"assistant",content:"",created_at:new Date().toISOString(),matches:[]},P=[...t.messages];t.messages.push(A);const B=D=>{var L,H;return l.chat(g.id,D,{sessionId:(L=t.streamingManager)==null?void 0:L.sessionId,streamId:(H=t.streamingManager)==null?void 0:H.streamId,chatMode:t.chatMode,messages:P.map(({matches:q,...V})=>V)},ue(t.chatMode))},N=await B(t.chat.id).catch(async D=>{var L;if(!((L=D==null?void 0:D.message)!=null&&L.includes("missing or invalid session_id")))throw D;return await y(),await w(!1),B(t.chat.id)});return u.track("agent-message-send",{event:"success",mode:t.chatMode,messages:t.messages.length+1}),A.context=N.context,A.matches=N.matches,N.result&&(A.content=N.result,u.track("agent-message-received",{latency:Date.now()-K,mode:t.chatMode,messages:t.messages.length}),(k=(I=n.callbacks).onNewMessage)==null||k.call(I,[...t.messages],"answer")),N}catch(A){throw t.messages[t.messages.length-1].id===h&&t.messages.pop(),u.track("agent-message-send",{event:"error",mode:t.chatMode,messages:t.messages.length}),A}},rate(c,h,C){var S,I,k,A;const R=t.messages.find(P=>P.id===c);if(t.chat){if(!R)throw new Error("Message not found")}else throw new Error("Chat is not initialized");const d=((S=R.matches)==null?void 0:S.map(P=>[P.document_id,P.id]))??[];return u.track("agent-rate",{event:C?"update":"create",thumb:h===1?"up":"down",knowledge_id:((I=g.knowledge)==null?void 0:I.id)??"",mode:t.chatMode,matches:d,score:h}),C?l.updateRating(g.id,t.chat.id,C,{knowledge_id:((k=g.knowledge)==null?void 0:k.id)??"",message_id:c,matches:d,score:h}):l.createRating(g.id,t.chat.id,{knowledge_id:((A=g.knowledge)==null?void 0:A.id)??"",message_id:c,matches:d,score:h})},deleteRate(c){var h;if(!t.chat)throw new Error("Chat is not initialized");return u.track("agent-rate-delete",{type:"text",chat_id:(h=t.chat)==null?void 0:h.id,id:c,mode:t.chatMode}),l.deleteRating(g.id,t.chat.id,c)},speak(c){if(!t.streamingManager)throw new Error("Please connect to the agent first");function h(){if(typeof c=="string"){if(!g.presenter.voice)throw new Error("Presenter voice is not initialized");return{type:"text",provider:g.presenter.voice,input:c,ssml:!1}}if(c.type==="text"&&!c.provider){if(!g.presenter.voice)throw new Error("Presenter voice is not initialized");return{type:"text",provider:g.presenter.voice,input:c.input,ssml:c.ssml}}return c}const C=h();return u.track("agent-speak",C),t.streamingManager.speak({script:C})}}}function Ve(e,n,s){const{getById:a}=de(n,s||U);return a(e)}v.AgentStatus=Q,v.ChatMode=$,v.ChatProgress=x,v.ConnectionState=M,v.DocumentType=re,v.KnowledgeType=ne,v.PlanGroup=G,v.Providers=ie,v.RateState=ee,v.StreamEvents=F,v.StreamingState=j,v.Subject=te,v.UserPlan=W,v.VideoType=X,v.VoiceAccess=se,v.createAgentManager=Ke,v.getAgent=Ve,v.mapVideoType=ae,Object.defineProperty(v,Symbol.toStringTag,{value:"Module"})});
|
|
1
|
+
(function(p,V){typeof exports=="object"&&typeof module<"u"?V(exports):typeof define=="function"&&define.amd?define(["exports"],V):(p=typeof globalThis<"u"?globalThis:p||self,V(p.index={}))})(this,function(p){"use strict";var V=(e=>(e.TRIAL="trial",e.BASIC="basic",e.ENTERPRISE="enterprise",e.LITE="lite",e.ADVANCED="advanced",e))(V||{}),X=(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))(X||{}),Y=(e=>(e.Created="created",e.Started="started",e.Done="done",e.Error="error",e.Rejected="rejected",e.Ready="ready",e))(Y||{}),Z=(e=>(e.Unrated="Unrated",e.Positive="Positive",e.Negative="Negative",e))(Z||{}),R=(e=>(e.Functional="Functional",e.TextOnly="TextOnly",e.Maintenance="Maintenance",e.Playground="Playground",e.DirectPlayback="DirectPlayback",e))(R||{}),x=(e=>(e.Embed="embed",e.Query="query",e.Partial="partial",e.Answer="answer",e.Complete="done",e))(x||{}),G=(e=>(e.KnowledgeProcessing="knowledge/processing",e.KnowledgeIndexing="knowledge/indexing",e.KnowledgeFailed="knowledge/error",e.KnowledgeDone="knowledge/done",e))(G||{}),Q=(e=>(e.Knowledge="knowledge",e.Document="document",e.Record="record",e))(Q||{}),ee=(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))(ee||{}),q=(e=>(e.Clip="clip",e.Talk="talk",e))(q||{});const te=e=>{switch(e){case"clip":return"clip";case"talk":return"talk";default:throw new Error(`Unknown video type: ${e}`)}};var F=(e=>(e.Start="START",e.Stop="STOP",e))(F||{}),j=(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))(j||{}),M=(e=>(e.New="new",e.Fail="fail",e.Connected="connected",e.Connecting="connecting",e.Closed="closed",e.Completed="completed",e.Disconnected="disconnected",e))(M||{}),ne=(e=>(e.Amazon="amazon",e.Microsoft="microsoft",e.Afflorithmics="afflorithmics",e.Elevenlabs="elevenlabs",e))(ne||{}),re=(e=>(e.Public="public",e.Premium="premium",e.Private="private",e))(re||{});const U="https://api.d-id.com",ue="wss://notifications.d-id.com",he="79f81a83a67430be2bc0fd61042b8faa";async function ae(e,n){const s={limit:(n==null?void 0:n.limit)??3,delayMs:(n==null?void 0:n.delayMs)??0,timeout:(n==null?void 0:n.timeout)??3e4,timeoutErrorMessage:(n==null?void 0:n.timeoutErrorMessage)||"Timeout error",shouldRetryFn:(n==null?void 0:n.shouldRetryFn)??function(){return!0}};let a;for(let t=1;t<=s.limit;t++)try{if(!s.timeout)return await e();let r;const i=new Promise((l,d)=>{r=setTimeout(()=>d(new Error(s.timeoutErrorMessage)),s.timeout)});return await Promise.race([e().then(l=>(clearTimeout(r),l)),i])}catch(r){if(a=r,!s.shouldRetryFn(r)||t>=s.limit)throw r;s.delayMs>0&&await new Promise(i=>setTimeout(i,s.delayMs))}throw a}const H=()=>Math.random().toString(16).slice(2);function ie(){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 we=H();function se(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}.${ie()}_${we}`;throw new Error(`Unknown auth type: ${e}`)}const fe=e=>ae(e,{limit:3,delayMs:1e3,timeout:0,shouldRetryFn:n=>n.status===429});function J(e,n=U,s){const a=async(t,r)=>{const i=await fe(()=>fetch(n+(t!=null&&t.startsWith("/")?t:`/${t}`),{...r,headers:{...r==null?void 0:r.headers,Authorization:se(e),"Content-Type":"application/json"}}));if(!i.ok){let o=await i.text().catch(()=>"Failed to fetch");throw s&&s(new Error(o),{url:t,options:r,headers:i.headers}),new Error(o)}return i.json()};return{get(t,r){return a(t,{...r,method:"GET"})},post(t,r,i){return a(t,{...i,body:JSON.stringify(r),method:"POST"})},delete(t,r,i){return a(t,{...i,body:JSON.stringify(r),method:"DELETE"})},patch(t,r,i){return a(t,{...i,body:JSON.stringify(r),method:"PATCH"})}}}function oe(e,n=U,s){const a=J(e,`${n}/agents`,s);return{create(t,r){return a.post("/",t,r)},getAgents(t,r){return a.get(`/${t?`?tag=${t}`:""}`,r).then(i=>i??[])},getById(t,r){return a.get(`/${t}`,r)},delete(t,r){return a.delete(`/${t}`,void 0,r)},update(t,r,i){return a.patch(`/${t}`,r,i)},newChat(t,r,i){return a.post(`/${t}/chat`,r,i)},chat(t,r,i,o){return a.post(`/${t}/chat/${r}`,i,o)},createRating(t,r,i,o){return a.post(`/${t}/chat/${r}/ratings`,i,o)},updateRating(t,r,i,o,l){return a.patch(`/${t}/chat/${r}/ratings/${i}`,o,l)},deleteRating(t,r,i,o){return a.delete(`/${t}/chat/${r}/ratings/${i}`,o)},getSTTToken(t,r){return a.get(`/${t}/stt-token`,r)}}}const ye=e=>new Promise(n=>setTimeout(n,e));function ve(e){return new Promise((n,s)=>{const{callbacks:a,host:t,auth:r}=e,{onMessage:i=null,onOpen:o=null,onClose:l=null,onError:d=null}=a||{},u=new WebSocket(`${t}?authorization=${se(r)}`);u.onmessage=i,u.onclose=l,u.onerror=m=>{console.error(m),d==null||d("Websocket failed to connect",m),s(m)},u.onopen=m=>{o==null||o(m),n(u)}})}async function pe(e){const{retries:n=1}=e;let s=null;for(let a=0;(s==null?void 0:s.readyState)!==WebSocket.OPEN;a++)try{s=await ve(e)}catch(t){if(a===n)throw t;await ye(a*500)}return s}async function Ce(e,n,s){const a=s!=null&&s.onMessage?[s.onMessage]:[],t=await pe({auth:e,host:n,callbacks:{onError:s==null?void 0:s.onError,onMessage:r=>{const i=JSON.parse(r.data);a.forEach(o=>o(i.event,i))}}});return{socket:t,disconnect:()=>t.close(),subscribeToEvents:r=>a.push(r)}}const Se="X-Playground-Chat";function _e(e,n,s,a){const t=J(e,`${n}/agents/${s}`,a);return{createStream(r){return t.post("/streams",{output_resolution:r.output_resolution,compatibility_mode:r.compatibility_mode,stream_warmup:r.stream_warmup,session_timeout:r.session_timeout,stream_greeting:r.stream_greeting})},startConnection(r,i,o){return t.post(`/streams/${r}/sdp`,{session_id:o,answer:i})},addIceCandidate(r,i,o){return t.post(`/streams/${r}/ice`,{session_id:o,...i})},sendStreamRequest(r,i,o){return t.post(`/streams/${r}`,{session_id:i,...o})},close(r,i){return t.delete(`/streams/${r}`,{session_id:i})}}}function Me(e,n,s,a){const t=J(e,`${n}/agents/${s}`,a);return{createStream(r,i){return t.post("/streams",{driver_url:r.driver_url,face:r.face,config:r.config,output_resolution:r.output_resolution,compatibility_mode:r.compatibility_mode,stream_warmup:r.stream_warmup,session_timeout:r.session_timeout,stream_greeting:r.stream_greeting},i)},startConnection(r,i,o,l){return t.post(`/streams/${r}/sdp`,{session_id:o,answer:i},l)},addIceCandidate(r,i,o,l){return t.post(`/streams/${r}/ice`,{session_id:o,...i},l)},sendStreamRequest(r,i,o,l){return t.post(`/streams/${r}`,{session_id:i,...o},l)},close(r,i,o){return t.delete(`/streams/${r}`,{session_id:i},o)}}}let ce=!1;const N=(e,n)=>ce&&console.log(e,n),ke=(window.RTCPeerConnection||window.webkitRTCPeerConnection||window.mozRTCPeerConnection).bind(window);function de(e){switch(e){case"connected":return M.Connected;case"checking":return M.Connecting;case"failed":return M.Fail;case"new":return M.New;case"closed":return M.Closed;case"disconnected":return M.Disconnected;case"completed":return M.Completed;default:return M.New}}function Ee(){let e=0;return n=>{for(const s of n.values())if(s&&s.type==="inbound-rtp"&&s.kind==="video"){const a=s.bytesReceived,t=a-e>0;return e=a,t}return!1}}function Re(e,n,s=!1,a,t){const i=Math.max(Math.ceil(10),1);let o=0,l=!1;const d=s?1:0;let u=0;const m=Ee();return setInterval(async()=>{const h=await e.getStats();m(h)?(o=0,l||(n==null||n(F.Start),u>=d&&!a()&&t(),u++,l=!0)):l&&(o++,o>=i&&(n==null||n(F.Stop),l=!1))},100)}async function Ie(e,n,{debug:s=!1,callbacks:a,auth:t,baseURL:r=U,warmup:i}){ce=s;const{startConnection:o,sendStreamRequest:l,close:d,createStream:u,addIceCandidate:m}=n.videoType===q.Clip?_e(t,r,e,a.onError):Me(t,r,e,a.onError),{id:h,offer:S,ice_servers:v,session_id:_}=await u(n),y=new ke({iceServers:v}),$=y.createDataChannel("JanusDataChannel");if(!_)throw new Error("Could not create session_id");let T=!1;const c=()=>T,w=()=>{var g;T=!0,(g=a.onConnectionStateChange)==null||g.call(a,M.Connected)},C=Re(y,a.onVideoStateChange,i,c,w);y.onicecandidate=g=>{var f;N("peerConnection.onicecandidate",g);try{g.candidate&&g.candidate.sdpMid&&g.candidate.sdpMLineIndex!==null?m(h,{candidate:g.candidate.candidate,sdpMid:g.candidate.sdpMid,sdpMLineIndex:g.candidate.sdpMLineIndex},_):m(h,{candidate:null},_)}catch(E){(f=a.onError)==null||f.call(a,E,{streamId:h})}},$.onmessage=g=>{if($.readyState==="open"){const[f,E]=g.data.split(":");f===j.StreamReady&&!T&&w()}},y.oniceconnectionstatechange=()=>{var f;N("peerConnection.oniceconnectionstatechange => "+y.iceConnectionState);const g=de(y.iceConnectionState);g!==M.Connected&&((f=a.onConnectionStateChange)==null||f.call(a,g))},y.ontrack=g=>{var f;N("peerConnection.ontrack",g),(f=a.onSrcObjectReady)==null||f.call(a,g.streams[0])},await y.setRemoteDescription(S),N("set remote description OK");const k=await y.createAnswer();return N("create answer OK"),await y.setLocalDescription(k),N("set local description OK"),await o(h,k,_),N("start connection OK"),{speak(g){return l(h,_,g)},async disconnect(){var g,f;if(h){const E=de(y.iceConnectionState);if(y){if(E===M.New){(g=a.onVideoStateChange)==null||g.call(a,F.Stop),clearInterval(C);return}y.close(),y.oniceconnectionstatechange=null,y.onnegotiationneeded=null,y.onicecandidate=null,y.ontrack=null}try{E===M.Connected&&await d(h,_).catch(I=>{})}catch(I){N("Error on close stream connection",I)}(f=a.onVideoStateChange)==null||f.call(a,F.Stop),clearInterval(C)}},sessionId:_,streamId:h}}let z={};function Ae(e){var r,i,o,l,d;const n=window!=null&&window.hasOwnProperty("DID_AGENTS_API")?"agents-ui":"agents-sdk",s=e.agent.presenter,a=(r=e.agent.llm)==null?void 0:r.prompt_customization,t={token:e.token||"testKey",distinct_id:e.distinctId||ie(),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:a==null?void 0:a.role,personality:a==null?void 0:a.personality,instructions:(i=e.agent.llm)==null?void 0:i.instructions},temperature:(o=e.agent.llm)==null?void 0:o.temperature,knowledgeSource:a==null?void 0:a.knowledge_source,starterQuestionsCount:(d=(l=e.agent.knowledge)==null?void 0:l.starter_message)==null?void 0:d.length,topicsToAvoid:a==null?void 0:a.topics_to_avoid,maxResponseLength:a==null?void 0:a.max_response_length};return{...t,additionalProperties:{},isEnabled:e.isEnabled??!0,getRandom:()=>Math.random().toString(16).slice(2),enrich(u){const m={};if(u&&typeof u!="object")throw new Error("properties must be a flat json object");for(let h in u)(typeof u[h]=="string"||typeof u[h]=="number")&&(m[h]=u[h]);this.additionalProperties={...this.additionalProperties,...m}},track(u,m){if(!this.isEnabled)return Promise.resolve();const{audioPath:h,...S}=m||{},v={method:"POST",headers:{"Content-Type":"application/x-www-form-urlencoded"},body:new URLSearchParams({data:JSON.stringify([{event:u,properties:{...this.additionalProperties,...S,...t,source:n,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(_=>_.json()).catch(_=>console.error(_))},linkTrack(u,m,h,S){z[u]||(z[u]={events:{},resolvedDependencies:[]}),S.includes(h)||S.push(h);const v=z[u];if(v.events[h]={props:m},v.resolvedDependencies.push(h),S.every(y=>v.resolvedDependencies.includes(y))){const y=S.reduce(($,T)=>v.events[T]?{...$,...v.events[T].props}:$,{});this.track(u,y),v.resolvedDependencies=v.resolvedDependencies.filter($=>!S.includes($)),S.forEach($=>{delete v.events[$]})}}}}function $e(e){var t,r,i,o;const n=()=>/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"},a=e.presenter;return{$os:`${s()}`,isMobile:`${n()=="Mobile"}`,browser:navigator.userAgent,origin:window.location.origin,agentType:a.type==="clip"&&a.presenter_id.startsWith("v2_")?"clip_v2":a.type,agentVoice:{voiceId:(r=(t=e.presenter)==null?void 0:t.voice)==null?void 0:r.voice_id,provider:(o=(i=e.presenter)==null?void 0:i.voice)==null?void 0:o.type}}}function Pe(e,n,s){var d,u;const{event:a,...t}=e,{template:r}=(n==null?void 0:n.llm)||{},{language:i}=((d=n==null?void 0:n.presenter)==null?void 0:d.voice)||{},{stitch:o}=(n==null?void 0:n.presenter)||{};return{...t,llm:{...t.llm,template:r},script:{...t.script,provider:{...(u=t==null?void 0:t.script)==null?void 0:u.provider,language:i}},stitch:o,...s}}let K=0;const Te=20*1e3;function be(e,n,s){var a,t,r,i,o;return{videoType:te(e.presenter.type),output_resolution:(a=n==null?void 0:n.streamOptions)==null?void 0:a.outputResolution,session_timeout:(t=n==null?void 0:n.streamOptions)==null?void 0:t.sessionTimeout,stream_warmup:(r=n==null?void 0:n.streamOptions)==null?void 0:r.streamWarmup,compatibility_mode:(i=n==null?void 0:n.streamOptions)==null?void 0:i.compatibilityMode,stream_greeting:(o=n==null?void 0:n.streamOptions)!=null&&o.streamGreeting?s:void 0}}function le(e){return e===R.Playground?{headers:{[Se]:"true"}}:{}}async function ge(e,n,s,a,t){try{const r=await n.newChat(e,{persist:t??!1},le(a));return s.track("agent-chat",{event:"created",chat_id:r.id,agent_id:e,mode:a}),r}catch(r){let i;try{i=JSON.parse(r.message)}catch(o){console.error("Error parsing the error message:",o)}throw(i==null?void 0:i.kind)==="InsufficientCreditsError"?new Error("InsufficientCreditsError"):new Error("Cannot create new chat")}}function De(e,n,s,a,t,r){return new Promise(async(i,o)=>{var d,u,m;if(K=0,!t&&n.mode!==R.DirectPlayback)try{t=await ge(e.id,s,a,n.mode,n.persistentChat)}catch(h){return o(h)}if((t==null?void 0:t.chat_mode)===R.TextOnly)return(u=(d=n.callbacks)==null?void 0:d.onError)==null||u.call(d,new Error(JSON.stringify({kind:"InsufficientCreditsError"})),{fromSdk:!0}),i({chat:t});const l=await Ie(e.id,be(e,n,r),{...n,analytics:a,warmup:(m=n.streamOptions)==null?void 0:m.streamWarmup,callbacks:{...n.callbacks,onConnectionStateChange:async h=>{var S,v,_,y;h===M.Connected?l?((v=(S=n.callbacks).onConnectionStateChange)==null||v.call(S,h),i({chat:t,streamingManager:l})):t&&o(new Error("Something went wrong while initializing the manager")):(y=(_=n.callbacks).onConnectionStateChange)==null||y.call(_,h)},onVideoStateChange(h){var S,v;(v=(S=n.callbacks).onVideoStateChange)==null||v.call(S,h),K>0&&h===F.Start&&a.linkTrack("agent-video",{event:"start",latency:Date.now()-K},"start",[j.StreamVideoCreated])}}}).catch(o)})}function Ne(e){var s;const n=(s=e.greetings)==null?void 0:s.filter(a=>a.length>0);if(n&&n.length>0){const a=Math.floor(Math.random()*n.length);return n[a]}else return`Hi! I'm ${e.preview_name||"My Agent"}. How can I help you?`}function me(e,n){return n&&n.length>0?n:[{content:e,id:H(),role:"assistant",created_at:new Date().toISOString()}]}function Le(e){if(e.answer!==void 0)return e.answer;let n=0,s="";for(;n in e;)s+=e[n],n++;return s}function xe(e,n,s,a,t){if(!(e===x.Partial||e===x.Answer))return;const r=a.messages[a.messages.length-1];if((r==null?void 0:r.role)!=="assistant")return;const{content:i,sequence:o}=n;e===x.Partial?s[o]=i:s.answer=i;const l=Le(s);(r.content!==l||e===x.Answer)&&(r.content=l,t==null||t([...a.messages],e))}async function Fe(e,n){var y,$,T;let s={},a=!0;const t={messages:[],chatMode:n.mode||R.Functional},r=n.baseURL||U,i=n.wsURL||ue,o=n.mixpanelKey||he,l=oe(n.auth,r,n.callbacks.onError),d=await l.getById(e),u=Ne(d);t.messages=me(u,n.initialMessages),($=(y=n.callbacks).onNewMessage)==null||$.call(y,[...t.messages],"answer");const m=Ae({token:o,agent:d,isEnabled:n.enableAnalitics,distinctId:n.distinctId});m.track("agent-sdk",{event:"loaded",...$e(d)});const h={onMessage:(c,w)=>{var C,k;if("content"in w)xe(c,w,s,t,n.callbacks.onNewMessage),c===x.Answer&&m.track("agent-message-received",{messages:t.messages.length,mode:t.chatMode});else{const g=j,f=[g.StreamVideoDone,g.StreamVideoError,g.StreamVideoRejected],E=[g.StreamFailed,g.StreamVideoError,g.StreamVideoRejected],I=Pe(w,d,{mode:t.chatMode});if(c=c,c===g.StreamVideoCreated)m.linkTrack("agent-video",I,g.StreamVideoCreated,["start"]);else if(f.includes(c)){const A=c.split("/")[1];m.track("agent-video",{...I,event:A})}E.includes(c)&&((k=(C=n.callbacks).onError)==null||k.call(C,new Error(`Stream failed with event ${c}`),{data:w})),w.event===g.StreamDone&&v()}}};async function S(c){var E,I,A,b,W,L,D;(I=(E=n.callbacks).onConnectionStateChange)==null||I.call(E,M.Connecting),K=0,c&&!a&&(delete t.chat,t.messages=me(u),(b=(A=n.callbacks).onNewMessage)==null||b.call(A,[...t.messages],"answer"));const w=n.mode===R.DirectPlayback?Promise.resolve(void 0):Ce(n.auth,i,h),C=ae(()=>De(d,n,l,m,t.chat,c?u:void 0),{limit:3,timeout:Te,timeoutErrorMessage:"Could not connect",shouldRetryFn:P=>(P==null?void 0:P.message)!=="Could not connect"&&P.status!==429,delayMs:1e3}).catch(P=>{var B,O;throw _(R.Maintenance),(O=(B=n.callbacks).onConnectionStateChange)==null||O.call(B,M.Fail),P}),[k,{streamingManager:g,chat:f}]=await Promise.all([w,C]);f&&f.id!==((W=t.chat)==null?void 0:W.id)&&((D=(L=n.callbacks).onNewChat)==null||D.call(L,f.id)),t.streamingManager=g,t.socketManager=k,t.chat=f,a=!1,_((f==null?void 0:f.chat_mode)??n.mode??R.Functional)}async function v(){var c,w,C,k;(c=t.socketManager)==null||c.disconnect(),await((w=t.streamingManager)==null?void 0:w.disconnect()),delete t.streamingManager,delete t.socketManager,(k=(C=n.callbacks).onConnectionStateChange)==null||k.call(C,M.Disconnected)}async function _(c){var w,C;c!==t.chatMode&&(m.track("agent-mode-change",{mode:c}),t.chatMode=c,t.chatMode!==R.Functional&&await v(),(C=(w=n.callbacks).onModeChange)==null||C.call(w,c))}return{agent:d,starterMessages:((T=d.knowledge)==null?void 0:T.starter_message)||[],getSTTToken:()=>l.getSTTToken(d.id),changeMode:_,enrichAnalytics:m.enrich,async connect(){var c;await S(!0),m.track("agent-chat",{event:"connect",chatId:(c=t.chat)==null?void 0:c.id,agentId:d.id,mode:t.chatMode})},async reconnect(){var c;await v(),await S(!1),m.track("agent-chat",{event:"reconnect",chatId:(c=t.chat)==null?void 0:c.id,agentId:d.id,mode:t.chatMode})},async disconnect(){var c;await v(),m.track("agent-chat",{event:"disconnect",chatId:(c=t.chat)==null?void 0:c.id,agentId:d.id,mode:t.chatMode})},async chat(c){var C,k,g,f,E,I;const w=H();s={};try{if(K=Date.now(),n.mode===R.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(t.chatMode===R.Maintenance)throw new Error("Chat is in maintenance mode");if(![R.TextOnly,R.Playground].includes(t.chatMode))if(t.streamingManager){if(!t.chat)throw new Error("Chat is not initialized")}else throw new Error("Streaming manager is not initialized");t.messages.push({id:H(),role:"user",content:c,created_at:new Date(K).toISOString()}),(k=(C=n.callbacks).onNewMessage)==null||k.call(C,[...t.messages],"user"),t.chat||(t.chat=await ge(d.id,l,m,t.chatMode,n.persistentChat),(f=(g=n.callbacks).onNewChat)==null||f.call(g,t.chat.id));const A={id:w,role:"assistant",content:"",created_at:new Date().toISOString(),matches:[]},b=[...t.messages];t.messages.push(A);const W=D=>{var P,B;return l.chat(d.id,D,{sessionId:(P=t.streamingManager)==null?void 0:P.sessionId,streamId:(B=t.streamingManager)==null?void 0:B.streamId,chatMode:t.chatMode,messages:b.map(({matches:O,...Ve})=>Ve)},le(t.chatMode))},L=await W(t.chat.id).catch(async D=>{var P;if(!((P=D==null?void 0:D.message)!=null&&P.includes("missing or invalid session_id")))throw D;return await v(),await S(!1),W(t.chat.id)});return m.track("agent-message-send",{event:"success",mode:t.chatMode,messages:t.messages.length+1}),A.context=L.context,A.matches=L.matches,L.result&&(A.content=L.result,m.track("agent-message-received",{latency:Date.now()-K,mode:t.chatMode,messages:t.messages.length}),(I=(E=n.callbacks).onNewMessage)==null||I.call(E,[...t.messages],"answer")),L}catch(A){throw t.messages[t.messages.length-1].id===w&&t.messages.pop(),m.track("agent-message-send",{event:"error",mode:t.chatMode,messages:t.messages.length}),A}},rate(c,w,C){var f,E,I,A;const k=t.messages.find(b=>b.id===c);if(t.chat){if(!k)throw new Error("Message not found")}else throw new Error("Chat is not initialized");const g=((f=k.matches)==null?void 0:f.map(b=>[b.document_id,b.id]))??[];return m.track("agent-rate",{event:C?"update":"create",thumb:w===1?"up":"down",knowledge_id:((E=d.knowledge)==null?void 0:E.id)??"",mode:t.chatMode,matches:g,score:w}),C?l.updateRating(d.id,t.chat.id,C,{knowledge_id:((I=d.knowledge)==null?void 0:I.id)??"",message_id:c,matches:g,score:w}):l.createRating(d.id,t.chat.id,{knowledge_id:((A=d.knowledge)==null?void 0:A.id)??"",message_id:c,matches:g,score:w})},deleteRate(c){var w;if(!t.chat)throw new Error("Chat is not initialized");return m.track("agent-rate-delete",{type:"text",chat_id:(w=t.chat)==null?void 0:w.id,id:c,mode:t.chatMode}),l.deleteRating(d.id,t.chat.id,c)},speak(c){if(!t.streamingManager)throw new Error("Please connect to the agent first");function w(){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 C=w();return m.track("agent-speak",C),t.streamingManager.speak({script:C})}}}function Ke(e,n,s){const{getById:a}=oe(n,s||U);return a(e)}p.AgentStatus=Y,p.ChatMode=R,p.ChatProgress=x,p.ConnectionState=M,p.DocumentType=ee,p.KnowledgeType=Q,p.PlanGroup=X,p.Providers=ne,p.RateState=Z,p.StreamEvents=j,p.StreamingState=F,p.Subject=G,p.UserPlan=V,p.VideoType=q,p.VoiceAccess=re,p.createAgentManager=Fe,p.getAgent=Ke,p.mapVideoType=te,Object.defineProperty(p,Symbol.toStringTag,{value:"Module"})});
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
interface RetryOptions {
|
|
2
|
+
limit?: number;
|
|
3
|
+
delayMs?: number;
|
|
4
|
+
timeout?: number;
|
|
5
|
+
timeoutErrorMessage?: string;
|
|
6
|
+
shouldRetryFn?: (error: any) => boolean;
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* Utility function to retry a promise operation with configurable retry logic
|
|
10
|
+
*
|
|
11
|
+
* @param operation - A function that returns a Promise to be retried
|
|
12
|
+
* @param userOptions - Configuration options for retry behavior
|
|
13
|
+
* @param userOptions.limit - Maximum number of retry attempts (default: 3)
|
|
14
|
+
* @param userOptions.delayMs - Delay between retries in milliseconds (default: 0, no delay)
|
|
15
|
+
* @param userOptions.timeout - Timeout for each attempt in milliseconds (default: 30000, set 0 to disable)
|
|
16
|
+
* @param userOptions.timeoutErrorMessage - Custom timeout error message
|
|
17
|
+
* @param userOptions.shouldRetryFn - Function to determine if retry should occur based on error, that will force throw even if limit is not reached when returns "false"
|
|
18
|
+
*
|
|
19
|
+
* @returns Promise that resolves with the operation result or rejects with the last error
|
|
20
|
+
*
|
|
21
|
+
* @throws {Error} Last error encountered after all retries are exhausted
|
|
22
|
+
* @throws {Error} Timeout error if operation exceeds specified timeout
|
|
23
|
+
*
|
|
24
|
+
* @example
|
|
25
|
+
* // With custom options
|
|
26
|
+
* const result = await retryOperation(
|
|
27
|
+
* async () => await fetch(url),
|
|
28
|
+
* {
|
|
29
|
+
* limit: 5,
|
|
30
|
+
* delayMs: 1000,
|
|
31
|
+
* timeout: 5000,
|
|
32
|
+
* shouldRetryFn: (error) => error.status === 429
|
|
33
|
+
* }
|
|
34
|
+
* );
|
|
35
|
+
*/
|
|
36
|
+
declare function retryOperation<T>(operation: () => Promise<T>, userOptions?: RetryOptions): Promise<T>;
|
|
37
|
+
export default retryOperation;
|
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.143",
|
|
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
|
+
}
|