@d-id/client-sdk 1.0.19-beta.64 → 1.0.19-beta.65
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 +225 -227
- package/dist/index.umd.cjs +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
var B = /* @__PURE__ */ ((e) => (e.Amazon = "amazon", e.Microsoft = "microsoft", e.Afflorithmics = "afflorithmics", e.Elevenlabs = "elevenlabs", e))(B || {}), F = /* @__PURE__ */ ((e) => (e.Public = "public", e.Premium = "premium", e.Private = "private", e))(F || {}),
|
|
2
|
-
const
|
|
1
|
+
var B = /* @__PURE__ */ ((e) => (e.Amazon = "amazon", e.Microsoft = "microsoft", e.Afflorithmics = "afflorithmics", e.Elevenlabs = "elevenlabs", e))(B || {}), F = /* @__PURE__ */ ((e) => (e.Public = "public", e.Premium = "premium", e.Private = "private", e))(F || {}), E = /* @__PURE__ */ ((e) => (e.Start = "START", e.Stop = "STOP", e))(E || {}), I = /* @__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))(I || {}), R = /* @__PURE__ */ ((e) => (e[e.New = 0] = "New", e[e.Fail = 1] = "Fail", e[e.Connected = 2] = "Connected", e[e.Connecting = 3] = "Connecting", e[e.Terminating = 4] = "Terminating", e))(R || {}), U = /* @__PURE__ */ ((e) => (e.TRIAL = "trial", e.BASIC = "basic", e.ENTERPRISE = "enterprise", e.LITE = "lite", e.ADVANCED = "advanced", e))(U || {}), j = /* @__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))(j || {}), V = /* @__PURE__ */ ((e) => (e.Unrated = "Unrated", e.Positive = "Positive", e.Negative = "Negative", e))(V || {}), q = /* @__PURE__ */ ((e) => (e.Functional = "Functional", e.TextOnly = "TextOnly", e.Maintenance = "Maintenance", e))(q || {}), N = /* @__PURE__ */ ((e) => (e.Embed = "embed", e.Query = "query", e.Partial = "partial", e.Answer = "answer", e.Complete = "done", e))(N || {}), J = /* @__PURE__ */ ((e) => (e.KnowledgeProcessing = "knowledge/processing", e.KnowledgeIndexing = "knowledge/indexing", e.KnowledgeFailed = "knowledge/error", e.KnowledgeDone = "knowledge/done", e))(J || {}), Q = /* @__PURE__ */ ((e) => (e.Knowledge = "knowledge", e.Document = "document", e.Record = "record", e))(Q || {}), X = /* @__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))(X || {}), L = /* @__PURE__ */ ((e) => (e.Clip = "clip", e.Talk = "talk", e))(L || {});
|
|
2
|
+
const M = "https://api.d-id.com", Y = "wss://notifications.d-id.com", Z = "79f81a83a67430be2bc0fd61042b8faa", K = () => Math.random().toString(16).slice(2);
|
|
3
3
|
function D() {
|
|
4
4
|
let e = window.localStorage.getItem("did_external_key_id");
|
|
5
5
|
return e || (e = Math.random().toString(16).slice(2), window.localStorage.setItem("did_external_key_id", e)), e;
|
|
@@ -14,9 +14,9 @@ function W(e) {
|
|
|
14
14
|
return `Client-Key ${e.clientKey}.${D()}_${G}`;
|
|
15
15
|
throw new Error(`Unknown auth type: ${e}`);
|
|
16
16
|
}
|
|
17
|
-
function
|
|
17
|
+
function x(e, a = M) {
|
|
18
18
|
const t = async (n, r) => {
|
|
19
|
-
const i = await fetch(
|
|
19
|
+
const i = await fetch(a + (n != null && n.startsWith("/") ? n : `/${n}`), {
|
|
20
20
|
...r,
|
|
21
21
|
headers: {
|
|
22
22
|
...r == null ? void 0 : r.headers,
|
|
@@ -25,8 +25,8 @@ function P(e, s = A) {
|
|
|
25
25
|
}
|
|
26
26
|
});
|
|
27
27
|
if (!i.ok) {
|
|
28
|
-
let
|
|
29
|
-
throw new Error(
|
|
28
|
+
let g = await i.text().catch(() => "Failed to fetch");
|
|
29
|
+
throw new Error(g);
|
|
30
30
|
}
|
|
31
31
|
return i.json();
|
|
32
32
|
};
|
|
@@ -60,8 +60,8 @@ function P(e, s = A) {
|
|
|
60
60
|
}
|
|
61
61
|
};
|
|
62
62
|
}
|
|
63
|
-
function H(e,
|
|
64
|
-
const t =
|
|
63
|
+
function H(e, a = M) {
|
|
64
|
+
const t = x(e, `${a}/agents`);
|
|
65
65
|
return {
|
|
66
66
|
create(n, r) {
|
|
67
67
|
return t.post("/", n, r);
|
|
@@ -81,13 +81,13 @@ function H(e, s = A) {
|
|
|
81
81
|
newChat(n, r) {
|
|
82
82
|
return t.post(`/${n}/chat`, void 0, r);
|
|
83
83
|
},
|
|
84
|
-
chat(n, r, i,
|
|
85
|
-
return t.post(`/${n}/chat/${r}`, i,
|
|
84
|
+
chat(n, r, i, g) {
|
|
85
|
+
return t.post(`/${n}/chat/${r}`, i, g);
|
|
86
86
|
}
|
|
87
87
|
};
|
|
88
88
|
}
|
|
89
|
-
function ee(e,
|
|
90
|
-
const t =
|
|
89
|
+
function ee(e, a = M) {
|
|
90
|
+
const t = x(e, `${a}/knowledge`);
|
|
91
91
|
return {
|
|
92
92
|
createKnowledge(n, r) {
|
|
93
93
|
return t.post("/", n, r);
|
|
@@ -123,8 +123,8 @@ function ee(e, s = A) {
|
|
|
123
123
|
}
|
|
124
124
|
};
|
|
125
125
|
}
|
|
126
|
-
function te(e,
|
|
127
|
-
const t =
|
|
126
|
+
function te(e, a = M) {
|
|
127
|
+
const t = x(e, `${a}/chats/ratings`);
|
|
128
128
|
return {
|
|
129
129
|
create(n, r) {
|
|
130
130
|
return t.post("/", n, r);
|
|
@@ -140,50 +140,50 @@ function te(e, s = A) {
|
|
|
140
140
|
}
|
|
141
141
|
};
|
|
142
142
|
}
|
|
143
|
-
const ne = (e) => new Promise((
|
|
143
|
+
const ne = (e) => new Promise((a) => setTimeout(a, e));
|
|
144
144
|
function re(e) {
|
|
145
|
-
return new Promise((
|
|
145
|
+
return new Promise((a, t) => {
|
|
146
146
|
const {
|
|
147
147
|
callbacks: n,
|
|
148
148
|
host: r,
|
|
149
149
|
auth: i
|
|
150
150
|
} = e, {
|
|
151
|
-
onMessage:
|
|
151
|
+
onMessage: g = null,
|
|
152
152
|
onOpen: w = null,
|
|
153
153
|
onClose: v = null,
|
|
154
|
-
onError:
|
|
155
|
-
} = n || {},
|
|
156
|
-
|
|
157
|
-
console.error(
|
|
158
|
-
},
|
|
159
|
-
w == null || w(
|
|
154
|
+
onError: d = null
|
|
155
|
+
} = n || {}, h = new WebSocket(`${r}?authorization=${W(i)}`);
|
|
156
|
+
h.onmessage = g, h.onclose = v, h.onerror = (c) => {
|
|
157
|
+
console.error(c), d == null || d("Websocket failed to connect", c), t(c);
|
|
158
|
+
}, h.onopen = (c) => {
|
|
159
|
+
w == null || w(c), a(h);
|
|
160
160
|
};
|
|
161
161
|
});
|
|
162
162
|
}
|
|
163
163
|
async function ie(e) {
|
|
164
164
|
const {
|
|
165
|
-
retries:
|
|
165
|
+
retries: a = 1
|
|
166
166
|
} = e;
|
|
167
167
|
let t = null;
|
|
168
168
|
for (let n = 0; (t == null ? void 0 : t.readyState) !== WebSocket.OPEN; n++)
|
|
169
169
|
try {
|
|
170
170
|
t = await re(e);
|
|
171
171
|
} catch (r) {
|
|
172
|
-
if (n ===
|
|
172
|
+
if (n === a)
|
|
173
173
|
throw r;
|
|
174
174
|
await ne(n * 500);
|
|
175
175
|
}
|
|
176
176
|
return t;
|
|
177
177
|
}
|
|
178
|
-
async function
|
|
178
|
+
async function ae(e, a, t) {
|
|
179
179
|
const n = t != null && t.onMessage ? [t.onMessage] : [], r = await ie({
|
|
180
180
|
auth: e,
|
|
181
|
-
host:
|
|
181
|
+
host: a,
|
|
182
182
|
callbacks: {
|
|
183
183
|
onError: t == null ? void 0 : t.onError,
|
|
184
184
|
onMessage: (i) => {
|
|
185
|
-
const
|
|
186
|
-
n.forEach((w) => w(
|
|
185
|
+
const g = JSON.parse(i.data);
|
|
186
|
+
n.forEach((w) => w(g.event, g));
|
|
187
187
|
}
|
|
188
188
|
}
|
|
189
189
|
});
|
|
@@ -193,8 +193,8 @@ async function se(e, s, t) {
|
|
|
193
193
|
subscribeToEvents: (i) => n.push(i)
|
|
194
194
|
};
|
|
195
195
|
}
|
|
196
|
-
function
|
|
197
|
-
const t =
|
|
196
|
+
function se(e, a) {
|
|
197
|
+
const t = x(e, a);
|
|
198
198
|
return {
|
|
199
199
|
createStream(n) {
|
|
200
200
|
return t.post("/clips/streams", {
|
|
@@ -228,8 +228,8 @@ function ae(e, s) {
|
|
|
228
228
|
}
|
|
229
229
|
};
|
|
230
230
|
}
|
|
231
|
-
function oe(e,
|
|
232
|
-
const t =
|
|
231
|
+
function oe(e, a) {
|
|
232
|
+
const t = x(e, a);
|
|
233
233
|
return {
|
|
234
234
|
createStream(n, r) {
|
|
235
235
|
return t.post("/talks/streams", {
|
|
@@ -239,23 +239,23 @@ function oe(e, s) {
|
|
|
239
239
|
config: n.config
|
|
240
240
|
}, r);
|
|
241
241
|
},
|
|
242
|
-
startConnection(n, r, i,
|
|
242
|
+
startConnection(n, r, i, g) {
|
|
243
243
|
return t.post(`/talks/streams/${n}/sdp`, {
|
|
244
244
|
session_id: i,
|
|
245
245
|
answer: r
|
|
246
|
-
},
|
|
246
|
+
}, g);
|
|
247
247
|
},
|
|
248
|
-
addIceCandidate(n, r, i,
|
|
248
|
+
addIceCandidate(n, r, i, g) {
|
|
249
249
|
return t.post(`/talks/streams/${n}/ice`, {
|
|
250
250
|
session_id: i,
|
|
251
251
|
...r
|
|
252
|
-
},
|
|
252
|
+
}, g);
|
|
253
253
|
},
|
|
254
|
-
sendStreamRequest(n, r, i,
|
|
254
|
+
sendStreamRequest(n, r, i, g) {
|
|
255
255
|
return t.post(`/talks/streams/${n}`, {
|
|
256
256
|
session_id: r,
|
|
257
257
|
...i
|
|
258
|
-
},
|
|
258
|
+
}, g);
|
|
259
259
|
},
|
|
260
260
|
close(n, r, i) {
|
|
261
261
|
return t.delete(`/talks/streams/${n}`, {
|
|
@@ -264,13 +264,13 @@ function oe(e, s) {
|
|
|
264
264
|
}
|
|
265
265
|
};
|
|
266
266
|
}
|
|
267
|
-
function de(e,
|
|
268
|
-
return e.map((t, n) => n === 0 ?
|
|
267
|
+
function de(e, a) {
|
|
268
|
+
return e.map((t, n) => n === 0 ? a ? {
|
|
269
269
|
index: n,
|
|
270
270
|
timestamp: t.timestamp,
|
|
271
|
-
bytesReceived: t.bytesReceived -
|
|
272
|
-
packetsReceived: t.packetsReceived -
|
|
273
|
-
packetsLost: t.packetsLost -
|
|
271
|
+
bytesReceived: t.bytesReceived - a.bytesReceived,
|
|
272
|
+
packetsReceived: t.packetsReceived - a.packetsReceived,
|
|
273
|
+
packetsLost: t.packetsLost - a.packetsLost,
|
|
274
274
|
jitter: t.jitter,
|
|
275
275
|
frameWidth: t.frameWidth,
|
|
276
276
|
frameHeight: t.frameHeight,
|
|
@@ -298,41 +298,41 @@ function de(e, s) {
|
|
|
298
298
|
});
|
|
299
299
|
}
|
|
300
300
|
let z = !1;
|
|
301
|
-
const
|
|
301
|
+
const $ = (e, a) => z && console.log(e, a), ce = (window.RTCPeerConnection || window.webkitRTCPeerConnection || window.mozRTCPeerConnection).bind(window);
|
|
302
302
|
function ge(e) {
|
|
303
303
|
switch (e) {
|
|
304
304
|
case "connected":
|
|
305
|
-
return
|
|
305
|
+
return R.Connected;
|
|
306
306
|
case "checking":
|
|
307
|
-
return
|
|
307
|
+
return R.Connecting;
|
|
308
308
|
case "failed":
|
|
309
|
-
return
|
|
309
|
+
return R.Fail;
|
|
310
310
|
case "new":
|
|
311
311
|
case "closed":
|
|
312
312
|
case "disconnected":
|
|
313
313
|
default:
|
|
314
|
-
return
|
|
314
|
+
return R.New;
|
|
315
315
|
}
|
|
316
316
|
}
|
|
317
|
-
function
|
|
317
|
+
function ue(e, a) {
|
|
318
318
|
let t = [], n = 0, r = 0, i;
|
|
319
319
|
return setInterval(() => {
|
|
320
320
|
e.getStats().then((w) => {
|
|
321
321
|
w.forEach((v) => {
|
|
322
322
|
if (v.type === "inbound-rtp" && v.kind === "video") {
|
|
323
323
|
if (r = t.length - 1, v && t[r]) {
|
|
324
|
-
const
|
|
325
|
-
let
|
|
326
|
-
i =
|
|
324
|
+
const d = v.bytesReceived, h = t[r].bytesReceived;
|
|
325
|
+
let c = i;
|
|
326
|
+
i = d - h > 0;
|
|
327
327
|
let l;
|
|
328
|
-
if (
|
|
328
|
+
if (c !== i) {
|
|
329
329
|
if (i)
|
|
330
330
|
n = t.length;
|
|
331
331
|
else {
|
|
332
|
-
const
|
|
333
|
-
l = de(
|
|
332
|
+
const A = t.slice(n), u = n === 0 ? void 0 : t[n - 1];
|
|
333
|
+
l = de(A, u), l = l.sort((m, s) => s.packetsLost - m.packetsLost).slice(0, 5);
|
|
334
334
|
}
|
|
335
|
-
|
|
335
|
+
a == null || a(i ? E.Start : E.Stop, l);
|
|
336
336
|
}
|
|
337
337
|
}
|
|
338
338
|
t.push(v);
|
|
@@ -341,102 +341,100 @@ function me(e, s) {
|
|
|
341
341
|
});
|
|
342
342
|
}, 500);
|
|
343
343
|
}
|
|
344
|
-
async function
|
|
345
|
-
debug:
|
|
344
|
+
async function me(e, {
|
|
345
|
+
debug: a = !1,
|
|
346
346
|
callbacks: t,
|
|
347
347
|
auth: n,
|
|
348
348
|
analytics: r,
|
|
349
|
-
baseURL: i =
|
|
349
|
+
baseURL: i = M
|
|
350
350
|
}) {
|
|
351
|
-
z =
|
|
352
|
-
let
|
|
351
|
+
z = a;
|
|
352
|
+
let g;
|
|
353
353
|
const {
|
|
354
354
|
startConnection: w,
|
|
355
355
|
sendStreamRequest: v,
|
|
356
|
-
close:
|
|
357
|
-
createStream:
|
|
358
|
-
addIceCandidate:
|
|
359
|
-
} = e.videoType === L.Clip ?
|
|
356
|
+
close: d,
|
|
357
|
+
createStream: h,
|
|
358
|
+
addIceCandidate: c
|
|
359
|
+
} = e.videoType === L.Clip ? se(n, i) : oe(n, i), {
|
|
360
360
|
id: l,
|
|
361
|
-
offer:
|
|
362
|
-
ice_servers:
|
|
363
|
-
session_id:
|
|
364
|
-
} = await
|
|
365
|
-
iceServers:
|
|
366
|
-
}),
|
|
367
|
-
if (!
|
|
361
|
+
offer: A,
|
|
362
|
+
ice_servers: u,
|
|
363
|
+
session_id: m
|
|
364
|
+
} = await h(e), s = new ce({
|
|
365
|
+
iceServers: u
|
|
366
|
+
}), _ = s.createDataChannel("JanusDataChannel");
|
|
367
|
+
if (!m)
|
|
368
368
|
throw new Error("Could not create session_id");
|
|
369
|
-
const S =
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
candidate:
|
|
373
|
-
sdpMid:
|
|
374
|
-
sdpMLineIndex:
|
|
375
|
-
},
|
|
369
|
+
const S = ue(s, t.onVideoStateChange);
|
|
370
|
+
s.onicecandidate = (o) => {
|
|
371
|
+
$("peerConnection.onicecandidate", o), o.candidate && o.candidate.sdpMid && o.candidate.sdpMLineIndex !== null ? c(l, {
|
|
372
|
+
candidate: o.candidate.candidate,
|
|
373
|
+
sdpMid: o.candidate.sdpMid,
|
|
374
|
+
sdpMLineIndex: o.candidate.sdpMLineIndex
|
|
375
|
+
}, m) : c(l, {
|
|
376
376
|
candidate: null
|
|
377
|
-
},
|
|
378
|
-
},
|
|
379
|
-
var
|
|
380
|
-
|
|
381
|
-
const
|
|
382
|
-
|
|
383
|
-
var
|
|
384
|
-
(
|
|
385
|
-
}, 5e3) : (clearTimeout(
|
|
386
|
-
},
|
|
387
|
-
var
|
|
388
|
-
|
|
389
|
-
},
|
|
390
|
-
var
|
|
391
|
-
if (
|
|
392
|
-
const [
|
|
393
|
-
|
|
377
|
+
}, m);
|
|
378
|
+
}, s.oniceconnectionstatechange = () => {
|
|
379
|
+
var f;
|
|
380
|
+
$("peerConnection.oniceconnectionstatechange => " + s.iceConnectionState);
|
|
381
|
+
const o = ge(s.iceConnectionState);
|
|
382
|
+
o === R.Connected ? g = setTimeout(() => {
|
|
383
|
+
var k;
|
|
384
|
+
(k = t.onConnectionStateChange) == null || k.call(t, R.Connected);
|
|
385
|
+
}, 5e3) : (clearTimeout(g), (f = t.onConnectionStateChange) == null || f.call(t, o));
|
|
386
|
+
}, s.ontrack = (o) => {
|
|
387
|
+
var f;
|
|
388
|
+
$("peerConnection.ontrack", o), (f = t.onSrcObjectReady) == null || f.call(t, o.streams[0]);
|
|
389
|
+
}, _.onmessage = (o) => {
|
|
390
|
+
var f, k;
|
|
391
|
+
if (_.readyState === "open") {
|
|
392
|
+
const [p, P] = o.data.split(":");
|
|
393
|
+
p === I.StreamStarted ? r == null || r.track("agent-video", {
|
|
394
394
|
event: "start",
|
|
395
|
-
...
|
|
396
|
-
}) :
|
|
395
|
+
...o
|
|
396
|
+
}) : p === I.StreamDone ? r == null || r.track("agent-video", {
|
|
397
397
|
event: "stop",
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
}), clearInterval(S)) : h === $.StreamReady ? (clearTimeout(c), (R = t.onConnectionStateChange) == null || R.call(t, I.Connected)) : h === $.StreamCreated ? r == null || r.track("agent-video", {
|
|
398
|
+
...o
|
|
399
|
+
}) : p === I.StreamFailed ? ((f = t.onVideoStateChange) == null || f.call(t, E.Stop, {
|
|
400
|
+
event: p,
|
|
401
|
+
data: P
|
|
402
|
+
}), clearInterval(S)) : p === I.StreamReady ? (clearTimeout(g), (k = t.onConnectionStateChange) == null || k.call(t, R.Connected)) : p === I.StreamCreated ? r == null || r.track("agent-video", {
|
|
404
403
|
event: "created",
|
|
405
|
-
...
|
|
406
|
-
}) :
|
|
404
|
+
...o
|
|
405
|
+
}) : p === I.StreamVideoCreated ? r == null || r.track("agent-video", {
|
|
407
406
|
event: "video-created",
|
|
408
|
-
...
|
|
409
|
-
}) :
|
|
407
|
+
...o
|
|
408
|
+
}) : p === I.StreamVideoDone ? r == null || r.track("agent-video", {
|
|
410
409
|
event: "video-done",
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
}) : h === $.StreamVideoError && (r == null || r.track("agent-video", {
|
|
410
|
+
...o
|
|
411
|
+
}) : p === I.StreamVideoError && (r == null || r.track("agent-video", {
|
|
414
412
|
event: "video-error",
|
|
415
|
-
...
|
|
413
|
+
...o
|
|
416
414
|
}));
|
|
417
415
|
}
|
|
418
|
-
}, await
|
|
419
|
-
const C = await
|
|
420
|
-
return
|
|
416
|
+
}, await s.setRemoteDescription(A), $("set remote description OK");
|
|
417
|
+
const C = await s.createAnswer();
|
|
418
|
+
return $("create answer OK"), await s.setLocalDescription(C), $("set local description OK"), await w(l, C, m), $("start connection OK"), {
|
|
421
419
|
/**
|
|
422
420
|
* Method to send request to server to get clip or talk depend on you payload
|
|
423
421
|
* @param payload
|
|
424
422
|
*/
|
|
425
|
-
speak(
|
|
426
|
-
return v(l,
|
|
423
|
+
speak(o) {
|
|
424
|
+
return v(l, m, o);
|
|
427
425
|
},
|
|
428
426
|
/**
|
|
429
427
|
* Method to close RTC connection
|
|
430
428
|
*/
|
|
431
429
|
async disconnect() {
|
|
432
|
-
var
|
|
433
|
-
l && (
|
|
434
|
-
}), (
|
|
430
|
+
var o, f;
|
|
431
|
+
l && (s && (s.close(), s.oniceconnectionstatechange = null, s.onnegotiationneeded = null, s.onicecandidate = null, s.ontrack = null), await d(l, m).catch((k) => {
|
|
432
|
+
}), (o = t.onConnectionStateChange) == null || o.call(t, R.New), (f = t.onVideoStateChange) == null || f.call(t, E.Stop), clearInterval(S));
|
|
435
433
|
},
|
|
436
434
|
/**
|
|
437
435
|
* Session identifier information, should be returned in the body of all streaming requests
|
|
438
436
|
*/
|
|
439
|
-
sessionId:
|
|
437
|
+
sessionId: m,
|
|
440
438
|
/**
|
|
441
439
|
* Id of current RTC stream
|
|
442
440
|
*/
|
|
@@ -444,7 +442,7 @@ async function ue(e, {
|
|
|
444
442
|
};
|
|
445
443
|
}
|
|
446
444
|
function le(e) {
|
|
447
|
-
const
|
|
445
|
+
const a = {
|
|
448
446
|
mixPanelKey: e.mixPanelKey || "testKey",
|
|
449
447
|
distinct_id: e.distinctId || D(),
|
|
450
448
|
isEnabled: e.isEnabled ?? !0,
|
|
@@ -452,7 +450,7 @@ function le(e) {
|
|
|
452
450
|
owner_id: e.agent.owner_id ?? ""
|
|
453
451
|
};
|
|
454
452
|
return {
|
|
455
|
-
...
|
|
453
|
+
...a,
|
|
456
454
|
getRandom: () => Math.random().toString(16).slice(2),
|
|
457
455
|
track(t, n) {
|
|
458
456
|
if (!this.isEnabled)
|
|
@@ -467,7 +465,7 @@ function le(e) {
|
|
|
467
465
|
event: t,
|
|
468
466
|
properties: {
|
|
469
467
|
...n,
|
|
470
|
-
...
|
|
468
|
+
...a,
|
|
471
469
|
time: Date.now(),
|
|
472
470
|
$insert_id: this.getRandom(),
|
|
473
471
|
origin: window.location.href,
|
|
@@ -484,13 +482,13 @@ function le(e) {
|
|
|
484
482
|
}
|
|
485
483
|
function we(e) {
|
|
486
484
|
var n, r;
|
|
487
|
-
const
|
|
485
|
+
const a = () => /Mobi|Android/i.test(navigator.userAgent) ? "Mobile" : "Desktop";
|
|
488
486
|
return {
|
|
489
487
|
$os: `${(() => {
|
|
490
488
|
const i = navigator.platform;
|
|
491
489
|
return i.toLowerCase().includes("win") ? "Windows" : i.toLowerCase().includes("mac") ? "Mac OS X" : i.toLowerCase().includes("linux") ? "Linux" : "Unknown";
|
|
492
490
|
})()}`,
|
|
493
|
-
isMobile: `${
|
|
491
|
+
isMobile: `${a() == "Mobile"}`,
|
|
494
492
|
browser: navigator.userAgent,
|
|
495
493
|
origin: window.location.origin,
|
|
496
494
|
agentType: e.presenter.type,
|
|
@@ -500,9 +498,9 @@ function we(e) {
|
|
|
500
498
|
}
|
|
501
499
|
};
|
|
502
500
|
}
|
|
503
|
-
function he(e,
|
|
501
|
+
function he(e, a) {
|
|
504
502
|
var t;
|
|
505
|
-
return (t = e.knowledge) != null && t.id ?
|
|
503
|
+
return (t = e.knowledge) != null && t.id ? a.getKnowledge(e.knowledge.id).then((n) => (n == null ? void 0 : n.starter_message) || []) : [];
|
|
506
504
|
}
|
|
507
505
|
function fe(e) {
|
|
508
506
|
return e.presenter.type === L.Clip ? {
|
|
@@ -514,16 +512,16 @@ function fe(e) {
|
|
|
514
512
|
source_url: e.presenter.source_url
|
|
515
513
|
};
|
|
516
514
|
}
|
|
517
|
-
function pe(e,
|
|
518
|
-
return new Promise(async (i,
|
|
515
|
+
function pe(e, a, t, n, r) {
|
|
516
|
+
return new Promise(async (i, g) => {
|
|
519
517
|
let w = r;
|
|
520
|
-
const v = await
|
|
521
|
-
...
|
|
518
|
+
const v = await me(fe(e), {
|
|
519
|
+
...a,
|
|
522
520
|
callbacks: {
|
|
523
|
-
...
|
|
524
|
-
onConnectionStateChange: async (
|
|
525
|
-
var
|
|
526
|
-
if (
|
|
521
|
+
...a.callbacks,
|
|
522
|
+
onConnectionStateChange: async (d) => {
|
|
523
|
+
var h, c;
|
|
524
|
+
if (d === R.Connected)
|
|
527
525
|
try {
|
|
528
526
|
w || (w = await t.newChat(e.id), n.track("agent-chat", {
|
|
529
527
|
event: "created",
|
|
@@ -534,94 +532,94 @@ function pe(e, s, t, n, r) {
|
|
|
534
532
|
streamingManager: v
|
|
535
533
|
});
|
|
536
534
|
} catch (l) {
|
|
537
|
-
console.error(l),
|
|
535
|
+
console.error(l), g("Cannot create new chat");
|
|
538
536
|
}
|
|
539
537
|
else
|
|
540
|
-
|
|
541
|
-
(
|
|
538
|
+
d === R.Fail && g(new Error("Cannot create connection"));
|
|
539
|
+
(c = (h = a.callbacks).onConnectionStateChange) == null || c.call(h, d);
|
|
542
540
|
},
|
|
543
|
-
onVideoStateChange(
|
|
544
|
-
var
|
|
541
|
+
onVideoStateChange(d, h) {
|
|
542
|
+
var c, l;
|
|
545
543
|
n.track("agent-video", {
|
|
546
|
-
event:
|
|
547
|
-
rtc_stats:
|
|
548
|
-
}), (l = (
|
|
544
|
+
event: d,
|
|
545
|
+
rtc_stats: h ?? []
|
|
546
|
+
}), (l = (c = a.callbacks).onVideoStateChange) == null || l.call(c, d, h);
|
|
549
547
|
}
|
|
550
548
|
}
|
|
551
|
-
}).catch(
|
|
549
|
+
}).catch(g);
|
|
552
550
|
});
|
|
553
551
|
}
|
|
554
|
-
function ve(e,
|
|
555
|
-
return H(
|
|
552
|
+
function ve(e, a, t) {
|
|
553
|
+
return H(a, t || M).getById(e);
|
|
556
554
|
}
|
|
557
555
|
function b(e) {
|
|
558
|
-
let
|
|
556
|
+
let a = "";
|
|
559
557
|
if (e.greetings && e.greetings.length > 0) {
|
|
560
558
|
const t = Math.floor(Math.random() * e.greetings.length);
|
|
561
|
-
|
|
559
|
+
a = e.greetings[t];
|
|
562
560
|
} else
|
|
563
|
-
|
|
561
|
+
a = `Hi! I'm ${e.preview_name}, welcome to agents. How can I help you?`;
|
|
564
562
|
return [{
|
|
565
|
-
content:
|
|
563
|
+
content: a,
|
|
566
564
|
id: K(),
|
|
567
565
|
role: "assistant",
|
|
568
566
|
created_at: (/* @__PURE__ */ new Date()).toISOString()
|
|
569
567
|
}];
|
|
570
568
|
}
|
|
571
|
-
async function Se(e,
|
|
572
|
-
var l,
|
|
569
|
+
async function Se(e, a) {
|
|
570
|
+
var l, A;
|
|
573
571
|
const t = {
|
|
574
572
|
messages: []
|
|
575
|
-
}, n =
|
|
576
|
-
t.messages = b(
|
|
577
|
-
const
|
|
573
|
+
}, n = a.wsURL || Y, r = a.baseURL || M, i = a.mixpanelKey || Z, g = H(a.auth, r), w = te(a.auth, r), v = ee(a.auth, r), d = await g.getById(e), h = await he(d, v);
|
|
574
|
+
t.messages = b(d), (A = (l = a.callbacks).onNewMessage) == null || A.call(l, t.messages);
|
|
575
|
+
const c = le({
|
|
578
576
|
mixPanelKey: i,
|
|
579
|
-
agent:
|
|
580
|
-
...
|
|
577
|
+
agent: d,
|
|
578
|
+
...a
|
|
581
579
|
});
|
|
582
|
-
return
|
|
580
|
+
return c.track("agent-sdk", {
|
|
583
581
|
event: "loaded",
|
|
584
|
-
...we(
|
|
582
|
+
...we(d)
|
|
585
583
|
}), {
|
|
586
|
-
agent:
|
|
587
|
-
starterMessages:
|
|
584
|
+
agent: d,
|
|
585
|
+
starterMessages: h,
|
|
588
586
|
async connect() {
|
|
589
|
-
var
|
|
590
|
-
const
|
|
591
|
-
onMessage: (
|
|
592
|
-
var
|
|
593
|
-
if ("content" in
|
|
587
|
+
var _, S, C, o, f;
|
|
588
|
+
const u = await ae(a.auth, n, {
|
|
589
|
+
onMessage: (k, p) => {
|
|
590
|
+
var P, T;
|
|
591
|
+
if ("content" in p) {
|
|
594
592
|
const {
|
|
595
593
|
content: O
|
|
596
|
-
} =
|
|
597
|
-
(
|
|
594
|
+
} = p, y = t.messages[t.messages.length - 1];
|
|
595
|
+
(y == null ? void 0 : y.role) === "assistant" && (y.content = k === N.Partial ? y.content + O : O), k === N.Complete && c.track("agent-message-received", {
|
|
598
596
|
messages: t.messages.length
|
|
599
|
-
}), (
|
|
597
|
+
}), (T = (P = a.callbacks).onNewMessage) == null || T.call(P, t.messages);
|
|
600
598
|
}
|
|
601
599
|
}
|
|
602
600
|
}), {
|
|
603
|
-
streamingManager:
|
|
604
|
-
chat:
|
|
605
|
-
} = await pe(
|
|
606
|
-
t.messages = b(
|
|
601
|
+
streamingManager: m,
|
|
602
|
+
chat: s
|
|
603
|
+
} = await pe(d, a, g, c, t.chat);
|
|
604
|
+
t.messages = b(d), (S = (_ = a.callbacks).onNewMessage) == null || S.call(_, t.messages), s != null && s.id && s.id !== ((C = t.chat) == null ? void 0 : C.id) && ((f = (o = a.callbacks).onNewChat) == null || f.call(o, s == null ? void 0 : s.id)), t.streamingManager = m, t.socketManager = u, t.chat = s, c.track("agent-chat", {
|
|
607
605
|
event: "connect",
|
|
608
|
-
chatId:
|
|
609
|
-
agentId:
|
|
606
|
+
chatId: s.id,
|
|
607
|
+
agentId: d.id
|
|
610
608
|
});
|
|
611
609
|
},
|
|
612
610
|
async disconnect() {
|
|
613
|
-
var
|
|
614
|
-
(
|
|
611
|
+
var u, m, s;
|
|
612
|
+
(u = t.socketManager) == null || u.disconnect(), await ((m = t.streamingManager) == null ? void 0 : m.disconnect()), c.track("agent-chat", {
|
|
615
613
|
event: "disconnect",
|
|
616
|
-
chatId: (
|
|
617
|
-
agentId:
|
|
614
|
+
chatId: (s = t.chat) == null ? void 0 : s.id,
|
|
615
|
+
agentId: d.id
|
|
618
616
|
});
|
|
619
617
|
},
|
|
620
|
-
async chat(
|
|
621
|
-
var
|
|
618
|
+
async chat(u, m = !1) {
|
|
619
|
+
var s, _;
|
|
622
620
|
try {
|
|
623
621
|
const S = Date.now();
|
|
624
|
-
if (
|
|
622
|
+
if (u.length === 0)
|
|
625
623
|
throw new Error("Message cannot be empty");
|
|
626
624
|
if (!t.chat)
|
|
627
625
|
throw new Error("Chat is not initialized");
|
|
@@ -630,16 +628,16 @@ async function Se(e, s) {
|
|
|
630
628
|
t.messages.push({
|
|
631
629
|
id: K(),
|
|
632
630
|
role: "user",
|
|
633
|
-
content:
|
|
631
|
+
content: u,
|
|
634
632
|
created_at: new Date(S).toISOString()
|
|
635
633
|
});
|
|
636
|
-
const C = await
|
|
634
|
+
const C = await g.chat(d.id, t.chat.id, {
|
|
637
635
|
sessionId: t.streamingManager.sessionId,
|
|
638
636
|
streamId: t.streamingManager.streamId,
|
|
639
637
|
messages: t.messages,
|
|
640
|
-
append_chat:
|
|
638
|
+
append_chat: m
|
|
641
639
|
});
|
|
642
|
-
return
|
|
640
|
+
return c.track("agent-message-send", {
|
|
643
641
|
event: "success",
|
|
644
642
|
messages: t.messages.length + 1
|
|
645
643
|
}), t.messages.push({
|
|
@@ -648,77 +646,77 @@ async function Se(e, s) {
|
|
|
648
646
|
content: C.result ?? "",
|
|
649
647
|
created_at: (/* @__PURE__ */ new Date()).toISOString(),
|
|
650
648
|
matches: C.matches
|
|
651
|
-
}), C.result &&
|
|
649
|
+
}), C.result && c.track("agent-message-received", {
|
|
652
650
|
latency: Date.now() - S,
|
|
653
651
|
messages: t.messages.length
|
|
654
|
-
}), (
|
|
652
|
+
}), (_ = (s = a.callbacks).onNewMessage) == null || _.call(s, t.messages), C;
|
|
655
653
|
} catch (S) {
|
|
656
|
-
throw
|
|
654
|
+
throw c.track("agent-message-send", {
|
|
657
655
|
event: "error",
|
|
658
656
|
messages: t.messages.length
|
|
659
657
|
}), S;
|
|
660
658
|
}
|
|
661
659
|
},
|
|
662
|
-
rate(
|
|
663
|
-
var C,
|
|
664
|
-
const
|
|
660
|
+
rate(u, m, s) {
|
|
661
|
+
var C, o, f, k;
|
|
662
|
+
const _ = t.messages.find((p) => p.id === u);
|
|
665
663
|
if (t.chat) {
|
|
666
|
-
if (!
|
|
664
|
+
if (!_)
|
|
667
665
|
throw new Error("Message not found");
|
|
668
666
|
} else
|
|
669
667
|
throw new Error("Chat is not initialized");
|
|
670
|
-
const S = ((C =
|
|
671
|
-
return
|
|
672
|
-
event:
|
|
673
|
-
thumb:
|
|
674
|
-
knowledge_id: ((
|
|
668
|
+
const S = ((C = _.matches) == null ? void 0 : C.map((p) => [p.document_id, p.id])) ?? [];
|
|
669
|
+
return c.track("agent-rate", {
|
|
670
|
+
event: s ? "update" : "create",
|
|
671
|
+
thumb: m === 1 ? "up" : "down",
|
|
672
|
+
knowledge_id: ((o = d.knowledge) == null ? void 0 : o.id) ?? "",
|
|
675
673
|
matches: S,
|
|
676
|
-
score:
|
|
677
|
-
}),
|
|
678
|
-
agent_id:
|
|
679
|
-
knowledge_id: ((
|
|
674
|
+
score: m
|
|
675
|
+
}), s ? w.update(s, {
|
|
676
|
+
agent_id: d.id,
|
|
677
|
+
knowledge_id: ((f = d.knowledge) == null ? void 0 : f.id) ?? "",
|
|
680
678
|
chat_id: t.chat.id,
|
|
681
|
-
message_id:
|
|
679
|
+
message_id: u,
|
|
682
680
|
matches: S,
|
|
683
|
-
score:
|
|
681
|
+
score: m
|
|
684
682
|
}) : w.create({
|
|
685
|
-
agent_id:
|
|
686
|
-
knowledge_id: ((
|
|
683
|
+
agent_id: d.id,
|
|
684
|
+
knowledge_id: ((k = d.knowledge) == null ? void 0 : k.id) ?? "",
|
|
687
685
|
chat_id: t.chat.id,
|
|
688
|
-
message_id:
|
|
686
|
+
message_id: u,
|
|
689
687
|
matches: S,
|
|
690
|
-
score:
|
|
688
|
+
score: m
|
|
691
689
|
});
|
|
692
690
|
},
|
|
693
|
-
deleteRate(
|
|
694
|
-
var
|
|
695
|
-
return
|
|
691
|
+
deleteRate(u) {
|
|
692
|
+
var m;
|
|
693
|
+
return c.track("agent-rate-delete", {
|
|
696
694
|
type: "text",
|
|
697
|
-
chat_id: (
|
|
698
|
-
id:
|
|
699
|
-
}), w.delete(
|
|
695
|
+
chat_id: (m = t.chat) == null ? void 0 : m.id,
|
|
696
|
+
id: u
|
|
697
|
+
}), w.delete(u);
|
|
700
698
|
},
|
|
701
|
-
speak(
|
|
699
|
+
speak(u) {
|
|
702
700
|
if (!t.streamingManager)
|
|
703
701
|
throw new Error("Streaming manager is not initialized");
|
|
704
|
-
function
|
|
705
|
-
if (
|
|
702
|
+
function m() {
|
|
703
|
+
if (u.type === "text")
|
|
706
704
|
return {
|
|
707
705
|
type: "text",
|
|
708
|
-
provider:
|
|
709
|
-
input:
|
|
710
|
-
ssml:
|
|
706
|
+
provider: u.provider ? u.provider : d.presenter.voice,
|
|
707
|
+
input: u.input,
|
|
708
|
+
ssml: u.ssml || !1
|
|
711
709
|
};
|
|
712
|
-
if (
|
|
710
|
+
if (u.type === "audio")
|
|
713
711
|
return {
|
|
714
712
|
type: "audio",
|
|
715
|
-
audio_url:
|
|
713
|
+
audio_url: u.audio_url
|
|
716
714
|
};
|
|
717
715
|
throw new Error("Invalid payload");
|
|
718
716
|
}
|
|
719
|
-
const
|
|
720
|
-
return
|
|
721
|
-
script:
|
|
717
|
+
const s = m();
|
|
718
|
+
return c.track("agent-speak", s), t.streamingManager.speak({
|
|
719
|
+
script: s
|
|
722
720
|
});
|
|
723
721
|
}
|
|
724
722
|
};
|
package/dist/index.umd.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
(function(v,L){typeof exports=="object"&&typeof module<"u"?L(exports):typeof define=="function"&&define.amd?define(["exports"],L):(v=typeof globalThis<"u"?globalThis:v||self,L(v.index={}))})(this,function(v){"use strict";var L=(e=>(e.Amazon="amazon",e.Microsoft="microsoft",e.Afflorithmics="afflorithmics",e.Elevenlabs="elevenlabs",e))(L||{}),x=(e=>(e.Public="public",e.Premium="premium",e.Private="private",e))(x||{}),T=(e=>(e.Start="START",e.Stop="STOP",e))(T||{}),$=(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))($||{}),I=(e=>(e[e.New=0]="New",e[e.Fail=1]="Fail",e[e.Connected=2]="Connected",e[e.Connecting=3]="Connecting",e[e.Terminating=4]="Terminating",e))(I||{}),D=(e=>(e.TRIAL="trial",e.BASIC="basic",e.ENTERPRISE="enterprise",e.LITE="lite",e.ADVANCED="advanced",e))(D||{}),W=(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))(W||{}),H=(e=>(e.Unrated="Unrated",e.Positive="Positive",e.Negative="Negative",e))(H||{}),j=(e=>(e.Functional="Functional",e.TextOnly="TextOnly",e.Maintenance="Maintenance",e))(j||{}),O=(e=>(e.Embed="embed",e.Query="query",e.Partial="partial",e.Answer="answer",e.Complete="done",e))(O||{}),U=(e=>(e.KnowledgeProcessing="knowledge/processing",e.KnowledgeIndexing="knowledge/indexing",e.KnowledgeFailed="knowledge/error",e.KnowledgeDone="knowledge/done",e))(U||{}),z=(e=>(e.Knowledge="knowledge",e.Document="document",e.Record="record",e))(z||{}),B=(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))(B||{}),E=(e=>(e.Clip="clip",e.Talk="talk",e))(E||{});const M="https://api.d-id.com",Z="wss://notifications.d-id.com",G="79f81a83a67430be2bc0fd61042b8faa",N=()=>Math.random().toString(16).slice(2);function F(){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 ee=N();function V(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}.${F()}_${ee}`;throw new Error(`Unknown auth type: ${e}`)}function K(e,a=M){const t=async(n,r)=>{const i=await fetch(a+(n!=null&&n.startsWith("/")?n:`/${n}`),{...r,headers:{...r==null?void 0:r.headers,Authorization:V(e),"Content-Type":"application/json"}});if(!i.ok){let c=await i.text().catch(()=>"Failed to fetch");throw new Error(c)}return i.json()};return{get(n,r){return t(n,{...r,method:"GET"})},post(n,r,i){return t(n,{...i,body:JSON.stringify(r),method:"POST"})},delete(n,r,i){return t(n,{...i,body:JSON.stringify(r),method:"DELETE"})},patch(n,r,i){return t(n,{...i,body:JSON.stringify(r),method:"PATCH"})}}}function q(e,a=M){const t=K(e,`${a}/agents`);return{create(n,r){return t.post("/",n,r)},getAgents(n,r){return t.get(`/${n?`?tag=${n}`:""}`,r).then(i=>i??[])},getById(n,r){return t.get(`/${n}`,r)},delete(n,r){return t.delete(`/${n}`,void 0,r)},update(n,r,i){return t.patch(`/${n}`,r,i)},newChat(n,r){return t.post(`/${n}/chat`,void 0,r)},chat(n,r,i,c){return t.post(`/${n}/chat/${r}`,i,c)}}}function te(e,a=M){const t=K(e,`${a}/knowledge`);return{createKnowledge(n,r){return t.post("/",n,r)},getKnowledgeBase(n){return t.get("/",n)},getKnowledge(n,r){return t.get(`/${n}`,r)},deleteKnowledge(n,r){return t.delete(`/${n}`,void 0,r)},createDocument(n,r,i){return t.post(`/${n}/documents`,r,i)},deleteDocument(n,r,i){return t.delete(`/${n}/documents/${r}`,void 0,i)},getDocuments(n,r){return t.get(`/${n}/documents`,r)},getDocument(n,r,i){return t.get(`/${n}/documents/${r}`,i)},getRecords(n,r,i){return t.get(`/${n}/documents/${r}/records`,i)},query(n,r,i){return t.post(`/${n}/query`,{query:r},i)}}}function ne(e,a=M){const t=K(e,`${a}/chats/ratings`);return{create(n,r){return t.post("/",n,r)},getByKnowledge(n,r){return t.get(`/${n}`,r).then(i=>i??[])},update(n,r,i){return t.patch(`/${n}`,r,i)},delete(n,r){return t.delete(`/${n}`,r)}}}const re=e=>new Promise(a=>setTimeout(a,e));function ie(e){return new Promise((a,t)=>{const{callbacks:n,host:r,auth:i}=e,{onMessage:c=null,onOpen:w=null,onClose:S=null,onError:o=null}=n||{},f=new WebSocket(`${r}?authorization=${V(i)}`);f.onmessage=c,f.onclose=S,f.onerror=d=>{console.error(d),o==null||o("Websocket failed to connect",d),t(d)},f.onopen=d=>{w==null||w(d),a(f)}})}async function ae(e){const{retries:a=1}=e;let t=null;for(let n=0;(t==null?void 0:t.readyState)!==WebSocket.OPEN;n++)try{t=await ie(e)}catch(r){if(n===a)throw r;await re(n*500)}return t}async function se(e,a,t){const n=t!=null&&t.onMessage?[t.onMessage]:[],r=await ae({auth:e,host:a,callbacks:{onError:t==null?void 0:t.onError,onMessage:i=>{const c=JSON.parse(i.data);n.forEach(w=>w(c.event,c))}}});return{socket:r,disconnect:()=>r.close(),subscribeToEvents:i=>n.push(i)}}function oe(e,a){const t=K(e,a);return{createStream(n){return t.post("/clips/streams",{driver_id:n.driver_id,presenter_id:n.presenter_id,compatibility_mode:n.compatibility_mode})},startConnection(n,r,i){return t.post(`/clips/streams/${n}/sdp`,{session_id:i,answer:r})},addIceCandidate(n,r,i){return t.post(`/clips/streams/${n}/ice`,{session_id:i,...r})},sendStreamRequest(n,r,i){return t.post(`/clips/streams/${n}`,{session_id:r,...i})},close(n,r){return t.delete(`/clips/streams/${n}`,{session_id:r})}}}function de(e,a){const t=K(e,a);return{createStream(n,r){return t.post("/talks/streams",{source_url:n.source_url,driver_url:n.driver_url,face:n.face,config:n.config},r)},startConnection(n,r,i,c){return t.post(`/talks/streams/${n}/sdp`,{session_id:i,answer:r},c)},addIceCandidate(n,r,i,c){return t.post(`/talks/streams/${n}/ice`,{session_id:i,...r},c)},sendStreamRequest(n,r,i,c){return t.post(`/talks/streams/${n}`,{session_id:r,...i},c)},close(n,r,i){return t.delete(`/talks/streams/${n}`,{session_id:r},i)}}}function ce(e,a){return e.map((t,n)=>n===0?a?{index:n,timestamp:t.timestamp,bytesReceived:t.bytesReceived-a.bytesReceived,packetsReceived:t.packetsReceived-a.packetsReceived,packetsLost:t.packetsLost-a.packetsLost,jitter:t.jitter,frameWidth:t.frameWidth,frameHeight:t.frameHeight,framesPerSecond:t.framesPerSecond}:{index:n,timestamp:t.timestamp,bytesReceived:t.bytesReceived,packetsReceived:t.packetsReceived,packetsLost:t.packetsLost,jitter:t.jitter,frameWidth:t.frameWidth,frameHeight:t.frameHeight,framesPerSecond:t.framesPerSecond}:{index:n,timestamp:t.timestamp,bytesReceived:t.bytesReceived-e[n-1].bytesReceived,packetsReceived:t.packetsReceived-e[n-1].packetsReceived,packetsLost:t.packetsLost-e[n-1].packetsLost,jitter:t.jitter,frameWidth:t.frameWidth,frameHeight:t.frameHeight,framesPerSecond:t.framesPerSecond})}let J=!1;const A=(e,a)=>J&&console.log(e,a),ge=(window.RTCPeerConnection||window.webkitRTCPeerConnection||window.mozRTCPeerConnection).bind(window);function ue(e){switch(e){case"connected":return I.Connected;case"checking":return I.Connecting;case"failed":return I.Fail;case"new":case"closed":case"disconnected":default:return I.New}}function me(e,a){let t=[],n=0,r=0,i;return setInterval(()=>{e.getStats().then(w=>{w.forEach(S=>{if(S.type==="inbound-rtp"&&S.kind==="video"){if(r=t.length-1,S&&t[r]){const o=S.bytesReceived,f=t[r].bytesReceived;let d=i;i=o-f>0;let l;if(d!==i){if(i)n=t.length;else{const P=t.slice(n),u=n===0?void 0:t[n-1];l=ce(P,u),l=l.sort((m,s)=>s.packetsLost-m.packetsLost).slice(0,5)}a==null||a(i?T.Start:T.Stop,l)}}t.push(S)}})})},500)}async function le(e,{debug:a=!1,callbacks:t,auth:n,analytics:r,baseURL:i=M}){J=a;let c;const{startConnection:w,sendStreamRequest:S,close:o,createStream:f,addIceCandidate:d}=e.videoType===E.Clip?oe(n,i):de(n,i),{id:l,offer:P,ice_servers:u,session_id:m}=await f(e),s=new ge({iceServers:u}),R=s.createDataChannel("JanusDataChannel");if(!m)throw new Error("Could not create session_id");const C=me(s,t.onVideoStateChange);s.onicecandidate=g=>{A("peerConnection.onicecandidate",g),g.candidate&&g.candidate.sdpMid&&g.candidate.sdpMLineIndex!==null?d(l,{candidate:g.candidate.candidate,sdpMid:g.candidate.sdpMid,sdpMLineIndex:g.candidate.sdpMLineIndex},m):d(l,{candidate:null},m)},s.oniceconnectionstatechange=()=>{var p;A("peerConnection.oniceconnectionstatechange => "+s.iceConnectionState);const g=ue(s.iceConnectionState);e.videoType===E.Talk&&g===I.Connected?c=setTimeout(()=>{var y;(y=t.onConnectionStateChange)==null||y.call(t,I.Connected)},5e3):(clearTimeout(c),(p=t.onConnectionStateChange)==null||p.call(t,g))},s.ontrack=g=>{var p;A("peerConnection.ontrack",g),(p=t.onSrcObjectReady)==null||p.call(t,g.streams[0])},R.onmessage=g=>{var p,y;if(R.readyState==="open"){const[h,k]=g.data.split(":");console.log("data",h,k,g),h===$.StreamStarted?r==null||r.track("agent-video",{event:"start",...k}):h===$.StreamDone?r==null||r.track("agent-video",{event:"stop",rtcStats:k??[],...k}):h===$.StreamFailed?((p=t.onVideoStateChange)==null||p.call(t,T.Stop,{event:h,data:k}),clearInterval(C)):h===$.StreamReady?(clearTimeout(c),(y=t.onConnectionStateChange)==null||y.call(t,I.Connected)):h===$.StreamCreated?r==null||r.track("agent-video",{event:"created",...k}):h===$.StreamVideoCreated?r==null||r.track("agent-video",{event:"video-created",...k}):h===$.StreamVideoDone?r==null||r.track("agent-video",{event:"video-done",rtcStats:k??[],...k}):h===$.StreamVideoError&&(r==null||r.track("agent-video",{event:"video-error",...k}))}},await s.setRemoteDescription(P),A("set remote description OK");const _=await s.createAnswer();return A("create answer OK"),await s.setLocalDescription(_),A("set local description OK"),await w(l,_,m),A("start connection OK"),{speak(g){return S(l,m,g)},async disconnect(){var g,p;l&&(s&&(s.close(),s.oniceconnectionstatechange=null,s.onnegotiationneeded=null,s.onicecandidate=null,s.ontrack=null),await o(l,m).catch(y=>{}),(g=t.onConnectionStateChange)==null||g.call(t,I.New),(p=t.onVideoStateChange)==null||p.call(t,T.Stop),clearInterval(C))},sessionId:m,streamId:l}}function we(e){const a={mixPanelKey:e.mixPanelKey||"testKey",distinct_id:e.distinctId||F(),isEnabled:e.isEnabled??!0,agentId:e.agent.id,owner_id:e.agent.owner_id??""};return{...a,getRandom:()=>Math.random().toString(16).slice(2),track(t,n){if(!this.isEnabled)return Promise.reject("MixPanel analytics is disabled on creation");const r={method:"POST",headers:{"Content-Type":"application/x-www-form-urlencoded"},body:new URLSearchParams({data:JSON.stringify([{event:t,properties:{...n,...a,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",r).then(i=>i.json()).catch(i=>console.error(i))}}}function he(e){var n,r;const a=()=>/Mobi|Android/i.test(navigator.userAgent)?"Mobile":"Desktop";return{$os:`${(()=>{const i=navigator.platform;return i.toLowerCase().includes("win")?"Windows":i.toLowerCase().includes("mac")?"Mac OS X":i.toLowerCase().includes("linux")?"Linux":"Unknown"})()}`,isMobile:`${a()=="Mobile"}`,browser:navigator.userAgent,origin:window.location.origin,agentType:e.presenter.type,agentVoice:{voiceId:(n=e.presenter.voice)==null?void 0:n.voice_id,provider:(r=e.presenter.voice)==null?void 0:r.type}}}function fe(e,a){var t;return(t=e.knowledge)!=null&&t.id?a.getKnowledge(e.knowledge.id).then(n=>(n==null?void 0:n.starter_message)||[]):[]}function pe(e){return e.presenter.type===E.Clip?{videoType:E.Clip,driver_id:e.presenter.driver_id,presenter_id:e.presenter.presenter_id}:{videoType:E.Talk,source_url:e.presenter.source_url}}function ve(e,a,t,n,r){return new Promise(async(i,c)=>{let w=r;const S=await le(pe(e),{...a,callbacks:{...a.callbacks,onConnectionStateChange:async o=>{var f,d;if(o===I.Connected)try{w||(w=await t.newChat(e.id),n.track("agent-chat",{event:"created",chat_id:w.id,agent_id:e.id})),S&&w&&i({chat:w,streamingManager:S})}catch(l){console.error(l),c("Cannot create new chat")}else o===I.Fail&&c(new Error("Cannot create connection"));(d=(f=a.callbacks).onConnectionStateChange)==null||d.call(f,o)},onVideoStateChange(o,f){var d,l;n.track("agent-video",{event:o,rtc_stats:f??[]}),(l=(d=a.callbacks).onVideoStateChange)==null||l.call(d,o,f)}}}).catch(c)})}function Se(e,a,t){return q(a,t||M).getById(e)}function Q(e){let a="";if(e.greetings&&e.greetings.length>0){const t=Math.floor(Math.random()*e.greetings.length);a=e.greetings[t]}else a=`Hi! I'm ${e.preview_name}, welcome to agents. How can I help you?`;return[{content:a,id:N(),role:"assistant",created_at:new Date().toISOString()}]}async function Ce(e,a){var l,P;const t={messages:[]},n=a.wsURL||Z,r=a.baseURL||M,i=a.mixpanelKey||G,c=q(a.auth,r),w=ne(a.auth,r),S=te(a.auth,r),o=await c.getById(e),f=await fe(o,S);t.messages=Q(o),(P=(l=a.callbacks).onNewMessage)==null||P.call(l,t.messages);const d=we({mixPanelKey:i,agent:o,...a});return d.track("agent-sdk",{event:"loaded",...he(o)}),{agent:o,starterMessages:f,async connect(){var R,C,_,g,p;const u=await se(a.auth,n,{onMessage:(y,h)=>{var k,X;if("content"in h){const{content:Y}=h,b=t.messages[t.messages.length-1];(b==null?void 0:b.role)==="assistant"&&(b.content=y===O.Partial?b.content+Y:Y),y===O.Complete&&d.track("agent-message-received",{messages:t.messages.length}),(X=(k=a.callbacks).onNewMessage)==null||X.call(k,t.messages)}}}),{streamingManager:m,chat:s}=await ve(o,a,c,d,t.chat);t.messages=Q(o),(C=(R=a.callbacks).onNewMessage)==null||C.call(R,t.messages),s!=null&&s.id&&s.id!==((_=t.chat)==null?void 0:_.id)&&((p=(g=a.callbacks).onNewChat)==null||p.call(g,s==null?void 0:s.id)),t.streamingManager=m,t.socketManager=u,t.chat=s,d.track("agent-chat",{event:"connect",chatId:s.id,agentId:o.id})},async disconnect(){var u,m,s;(u=t.socketManager)==null||u.disconnect(),await((m=t.streamingManager)==null?void 0:m.disconnect()),d.track("agent-chat",{event:"disconnect",chatId:(s=t.chat)==null?void 0:s.id,agentId:o.id})},async chat(u,m=!1){var s,R;try{const C=Date.now();if(u.length===0)throw new Error("Message cannot be empty");if(!t.chat)throw new Error("Chat is not initialized");if(!t.streamingManager)throw new Error("Streaming manager is not initialized");t.messages.push({id:N(),role:"user",content:u,created_at:new Date(C).toISOString()});const _=await c.chat(o.id,t.chat.id,{sessionId:t.streamingManager.sessionId,streamId:t.streamingManager.streamId,messages:t.messages,append_chat:m});return d.track("agent-message-send",{event:"success",messages:t.messages.length+1}),t.messages.push({id:N(),role:"assistant",content:_.result??"",created_at:new Date().toISOString(),matches:_.matches}),_.result&&d.track("agent-message-received",{latency:Date.now()-C,messages:t.messages.length}),(R=(s=a.callbacks).onNewMessage)==null||R.call(s,t.messages),_}catch(C){throw d.track("agent-message-send",{event:"error",messages:t.messages.length}),C}},rate(u,m,s){var _,g,p,y;const R=t.messages.find(h=>h.id===u);if(t.chat){if(!R)throw new Error("Message not found")}else throw new Error("Chat is not initialized");const C=((_=R.matches)==null?void 0:_.map(h=>[h.document_id,h.id]))??[];return d.track("agent-rate",{event:s?"update":"create",thumb:m===1?"up":"down",knowledge_id:((g=o.knowledge)==null?void 0:g.id)??"",matches:C,score:m}),s?w.update(s,{agent_id:o.id,knowledge_id:((p=o.knowledge)==null?void 0:p.id)??"",chat_id:t.chat.id,message_id:u,matches:C,score:m}):w.create({agent_id:o.id,knowledge_id:((y=o.knowledge)==null?void 0:y.id)??"",chat_id:t.chat.id,message_id:u,matches:C,score:m})},deleteRate(u){var m;return d.track("agent-rate-delete",{type:"text",chat_id:(m=t.chat)==null?void 0:m.id,id:u}),w.delete(u)},speak(u){if(!t.streamingManager)throw new Error("Streaming manager is not initialized");function m(){if(u.type==="text")return{type:"text",provider:u.provider?u.provider:o.presenter.voice,input:u.input,ssml:u.ssml||!1};if(u.type==="audio")return{type:"audio",audio_url:u.audio_url};throw new Error("Invalid payload")}const s=m();return d.track("agent-speak",s),t.streamingManager.speak({script:s})}}}v.ChatMode=j,v.DocumentType=B,v.KnowledgeType=z,v.PlanGroup=W,v.Providers=L,v.RateState=H,v.Subject=U,v.UserPlan=D,v.VoiceAccess=x,v.createAgentManager=Ce,v.getAgent=Se,Object.defineProperty(v,Symbol.toStringTag,{value:"Module"})});
|
|
1
|
+
(function(v,A){typeof exports=="object"&&typeof module<"u"?A(exports):typeof define=="function"&&define.amd?define(["exports"],A):(v=typeof globalThis<"u"?globalThis:v||self,A(v.index={}))})(this,function(v){"use strict";var A=(e=>(e.Amazon="amazon",e.Microsoft="microsoft",e.Afflorithmics="afflorithmics",e.Elevenlabs="elevenlabs",e))(A||{}),x=(e=>(e.Public="public",e.Premium="premium",e.Private="private",e))(x||{}),E=(e=>(e.Start="START",e.Stop="STOP",e))(E||{}),$=(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))($||{}),I=(e=>(e[e.New=0]="New",e[e.Fail=1]="Fail",e[e.Connected=2]="Connected",e[e.Connecting=3]="Connecting",e[e.Terminating=4]="Terminating",e))(I||{}),D=(e=>(e.TRIAL="trial",e.BASIC="basic",e.ENTERPRISE="enterprise",e.LITE="lite",e.ADVANCED="advanced",e))(D||{}),W=(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))(W||{}),H=(e=>(e.Unrated="Unrated",e.Positive="Positive",e.Negative="Negative",e))(H||{}),j=(e=>(e.Functional="Functional",e.TextOnly="TextOnly",e.Maintenance="Maintenance",e))(j||{}),O=(e=>(e.Embed="embed",e.Query="query",e.Partial="partial",e.Answer="answer",e.Complete="done",e))(O||{}),U=(e=>(e.KnowledgeProcessing="knowledge/processing",e.KnowledgeIndexing="knowledge/indexing",e.KnowledgeFailed="knowledge/error",e.KnowledgeDone="knowledge/done",e))(U||{}),z=(e=>(e.Knowledge="knowledge",e.Document="document",e.Record="record",e))(z||{}),B=(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))(B||{}),P=(e=>(e.Clip="clip",e.Talk="talk",e))(P||{});const y="https://api.d-id.com",Z="wss://notifications.d-id.com",G="79f81a83a67430be2bc0fd61042b8faa",b=()=>Math.random().toString(16).slice(2);function F(){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 ee=b();function V(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}.${F()}_${ee}`;throw new Error(`Unknown auth type: ${e}`)}function T(e,a=y){const t=async(n,r)=>{const i=await fetch(a+(n!=null&&n.startsWith("/")?n:`/${n}`),{...r,headers:{...r==null?void 0:r.headers,Authorization:V(e),"Content-Type":"application/json"}});if(!i.ok){let g=await i.text().catch(()=>"Failed to fetch");throw new Error(g)}return i.json()};return{get(n,r){return t(n,{...r,method:"GET"})},post(n,r,i){return t(n,{...i,body:JSON.stringify(r),method:"POST"})},delete(n,r,i){return t(n,{...i,body:JSON.stringify(r),method:"DELETE"})},patch(n,r,i){return t(n,{...i,body:JSON.stringify(r),method:"PATCH"})}}}function q(e,a=y){const t=T(e,`${a}/agents`);return{create(n,r){return t.post("/",n,r)},getAgents(n,r){return t.get(`/${n?`?tag=${n}`:""}`,r).then(i=>i??[])},getById(n,r){return t.get(`/${n}`,r)},delete(n,r){return t.delete(`/${n}`,void 0,r)},update(n,r,i){return t.patch(`/${n}`,r,i)},newChat(n,r){return t.post(`/${n}/chat`,void 0,r)},chat(n,r,i,g){return t.post(`/${n}/chat/${r}`,i,g)}}}function te(e,a=y){const t=T(e,`${a}/knowledge`);return{createKnowledge(n,r){return t.post("/",n,r)},getKnowledgeBase(n){return t.get("/",n)},getKnowledge(n,r){return t.get(`/${n}`,r)},deleteKnowledge(n,r){return t.delete(`/${n}`,void 0,r)},createDocument(n,r,i){return t.post(`/${n}/documents`,r,i)},deleteDocument(n,r,i){return t.delete(`/${n}/documents/${r}`,void 0,i)},getDocuments(n,r){return t.get(`/${n}/documents`,r)},getDocument(n,r,i){return t.get(`/${n}/documents/${r}`,i)},getRecords(n,r,i){return t.get(`/${n}/documents/${r}/records`,i)},query(n,r,i){return t.post(`/${n}/query`,{query:r},i)}}}function ne(e,a=y){const t=T(e,`${a}/chats/ratings`);return{create(n,r){return t.post("/",n,r)},getByKnowledge(n,r){return t.get(`/${n}`,r).then(i=>i??[])},update(n,r,i){return t.patch(`/${n}`,r,i)},delete(n,r){return t.delete(`/${n}`,r)}}}const re=e=>new Promise(a=>setTimeout(a,e));function ie(e){return new Promise((a,t)=>{const{callbacks:n,host:r,auth:i}=e,{onMessage:g=null,onOpen:w=null,onClose:S=null,onError:d=null}=n||{},h=new WebSocket(`${r}?authorization=${V(i)}`);h.onmessage=g,h.onclose=S,h.onerror=c=>{console.error(c),d==null||d("Websocket failed to connect",c),t(c)},h.onopen=c=>{w==null||w(c),a(h)}})}async function ae(e){const{retries:a=1}=e;let t=null;for(let n=0;(t==null?void 0:t.readyState)!==WebSocket.OPEN;n++)try{t=await ie(e)}catch(r){if(n===a)throw r;await re(n*500)}return t}async function se(e,a,t){const n=t!=null&&t.onMessage?[t.onMessage]:[],r=await ae({auth:e,host:a,callbacks:{onError:t==null?void 0:t.onError,onMessage:i=>{const g=JSON.parse(i.data);n.forEach(w=>w(g.event,g))}}});return{socket:r,disconnect:()=>r.close(),subscribeToEvents:i=>n.push(i)}}function oe(e,a){const t=T(e,a);return{createStream(n){return t.post("/clips/streams",{driver_id:n.driver_id,presenter_id:n.presenter_id,compatibility_mode:n.compatibility_mode})},startConnection(n,r,i){return t.post(`/clips/streams/${n}/sdp`,{session_id:i,answer:r})},addIceCandidate(n,r,i){return t.post(`/clips/streams/${n}/ice`,{session_id:i,...r})},sendStreamRequest(n,r,i){return t.post(`/clips/streams/${n}`,{session_id:r,...i})},close(n,r){return t.delete(`/clips/streams/${n}`,{session_id:r})}}}function de(e,a){const t=T(e,a);return{createStream(n,r){return t.post("/talks/streams",{source_url:n.source_url,driver_url:n.driver_url,face:n.face,config:n.config},r)},startConnection(n,r,i,g){return t.post(`/talks/streams/${n}/sdp`,{session_id:i,answer:r},g)},addIceCandidate(n,r,i,g){return t.post(`/talks/streams/${n}/ice`,{session_id:i,...r},g)},sendStreamRequest(n,r,i,g){return t.post(`/talks/streams/${n}`,{session_id:r,...i},g)},close(n,r,i){return t.delete(`/talks/streams/${n}`,{session_id:r},i)}}}function ce(e,a){return e.map((t,n)=>n===0?a?{index:n,timestamp:t.timestamp,bytesReceived:t.bytesReceived-a.bytesReceived,packetsReceived:t.packetsReceived-a.packetsReceived,packetsLost:t.packetsLost-a.packetsLost,jitter:t.jitter,frameWidth:t.frameWidth,frameHeight:t.frameHeight,framesPerSecond:t.framesPerSecond}:{index:n,timestamp:t.timestamp,bytesReceived:t.bytesReceived,packetsReceived:t.packetsReceived,packetsLost:t.packetsLost,jitter:t.jitter,frameWidth:t.frameWidth,frameHeight:t.frameHeight,framesPerSecond:t.framesPerSecond}:{index:n,timestamp:t.timestamp,bytesReceived:t.bytesReceived-e[n-1].bytesReceived,packetsReceived:t.packetsReceived-e[n-1].packetsReceived,packetsLost:t.packetsLost-e[n-1].packetsLost,jitter:t.jitter,frameWidth:t.frameWidth,frameHeight:t.frameHeight,framesPerSecond:t.framesPerSecond})}let J=!1;const M=(e,a)=>J&&console.log(e,a),ge=(window.RTCPeerConnection||window.webkitRTCPeerConnection||window.mozRTCPeerConnection).bind(window);function ue(e){switch(e){case"connected":return I.Connected;case"checking":return I.Connecting;case"failed":return I.Fail;case"new":case"closed":case"disconnected":default:return I.New}}function me(e,a){let t=[],n=0,r=0,i;return setInterval(()=>{e.getStats().then(w=>{w.forEach(S=>{if(S.type==="inbound-rtp"&&S.kind==="video"){if(r=t.length-1,S&&t[r]){const d=S.bytesReceived,h=t[r].bytesReceived;let c=i;i=d-h>0;let l;if(c!==i){if(i)n=t.length;else{const L=t.slice(n),u=n===0?void 0:t[n-1];l=ce(L,u),l=l.sort((m,s)=>s.packetsLost-m.packetsLost).slice(0,5)}a==null||a(i?E.Start:E.Stop,l)}}t.push(S)}})})},500)}async function le(e,{debug:a=!1,callbacks:t,auth:n,analytics:r,baseURL:i=y}){J=a;let g;const{startConnection:w,sendStreamRequest:S,close:d,createStream:h,addIceCandidate:c}=e.videoType===P.Clip?oe(n,i):de(n,i),{id:l,offer:L,ice_servers:u,session_id:m}=await h(e),s=new ge({iceServers:u}),k=s.createDataChannel("JanusDataChannel");if(!m)throw new Error("Could not create session_id");const C=me(s,t.onVideoStateChange);s.onicecandidate=o=>{M("peerConnection.onicecandidate",o),o.candidate&&o.candidate.sdpMid&&o.candidate.sdpMLineIndex!==null?c(l,{candidate:o.candidate.candidate,sdpMid:o.candidate.sdpMid,sdpMLineIndex:o.candidate.sdpMLineIndex},m):c(l,{candidate:null},m)},s.oniceconnectionstatechange=()=>{var f;M("peerConnection.oniceconnectionstatechange => "+s.iceConnectionState);const o=ue(s.iceConnectionState);o===I.Connected?g=setTimeout(()=>{var R;(R=t.onConnectionStateChange)==null||R.call(t,I.Connected)},5e3):(clearTimeout(g),(f=t.onConnectionStateChange)==null||f.call(t,o))},s.ontrack=o=>{var f;M("peerConnection.ontrack",o),(f=t.onSrcObjectReady)==null||f.call(t,o.streams[0])},k.onmessage=o=>{var f,R;if(k.readyState==="open"){const[p,N]=o.data.split(":");p===$.StreamStarted?r==null||r.track("agent-video",{event:"start",...o}):p===$.StreamDone?r==null||r.track("agent-video",{event:"stop",...o}):p===$.StreamFailed?((f=t.onVideoStateChange)==null||f.call(t,E.Stop,{event:p,data:N}),clearInterval(C)):p===$.StreamReady?(clearTimeout(g),(R=t.onConnectionStateChange)==null||R.call(t,I.Connected)):p===$.StreamCreated?r==null||r.track("agent-video",{event:"created",...o}):p===$.StreamVideoCreated?r==null||r.track("agent-video",{event:"video-created",...o}):p===$.StreamVideoDone?r==null||r.track("agent-video",{event:"video-done",...o}):p===$.StreamVideoError&&(r==null||r.track("agent-video",{event:"video-error",...o}))}},await s.setRemoteDescription(L),M("set remote description OK");const _=await s.createAnswer();return M("create answer OK"),await s.setLocalDescription(_),M("set local description OK"),await w(l,_,m),M("start connection OK"),{speak(o){return S(l,m,o)},async disconnect(){var o,f;l&&(s&&(s.close(),s.oniceconnectionstatechange=null,s.onnegotiationneeded=null,s.onicecandidate=null,s.ontrack=null),await d(l,m).catch(R=>{}),(o=t.onConnectionStateChange)==null||o.call(t,I.New),(f=t.onVideoStateChange)==null||f.call(t,E.Stop),clearInterval(C))},sessionId:m,streamId:l}}function we(e){const a={mixPanelKey:e.mixPanelKey||"testKey",distinct_id:e.distinctId||F(),isEnabled:e.isEnabled??!0,agentId:e.agent.id,owner_id:e.agent.owner_id??""};return{...a,getRandom:()=>Math.random().toString(16).slice(2),track(t,n){if(!this.isEnabled)return Promise.reject("MixPanel analytics is disabled on creation");const r={method:"POST",headers:{"Content-Type":"application/x-www-form-urlencoded"},body:new URLSearchParams({data:JSON.stringify([{event:t,properties:{...n,...a,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",r).then(i=>i.json()).catch(i=>console.error(i))}}}function he(e){var n,r;const a=()=>/Mobi|Android/i.test(navigator.userAgent)?"Mobile":"Desktop";return{$os:`${(()=>{const i=navigator.platform;return i.toLowerCase().includes("win")?"Windows":i.toLowerCase().includes("mac")?"Mac OS X":i.toLowerCase().includes("linux")?"Linux":"Unknown"})()}`,isMobile:`${a()=="Mobile"}`,browser:navigator.userAgent,origin:window.location.origin,agentType:e.presenter.type,agentVoice:{voiceId:(n=e.presenter.voice)==null?void 0:n.voice_id,provider:(r=e.presenter.voice)==null?void 0:r.type}}}function fe(e,a){var t;return(t=e.knowledge)!=null&&t.id?a.getKnowledge(e.knowledge.id).then(n=>(n==null?void 0:n.starter_message)||[]):[]}function pe(e){return e.presenter.type===P.Clip?{videoType:P.Clip,driver_id:e.presenter.driver_id,presenter_id:e.presenter.presenter_id}:{videoType:P.Talk,source_url:e.presenter.source_url}}function ve(e,a,t,n,r){return new Promise(async(i,g)=>{let w=r;const S=await le(pe(e),{...a,callbacks:{...a.callbacks,onConnectionStateChange:async d=>{var h,c;if(d===I.Connected)try{w||(w=await t.newChat(e.id),n.track("agent-chat",{event:"created",chat_id:w.id,agent_id:e.id})),S&&w&&i({chat:w,streamingManager:S})}catch(l){console.error(l),g("Cannot create new chat")}else d===I.Fail&&g(new Error("Cannot create connection"));(c=(h=a.callbacks).onConnectionStateChange)==null||c.call(h,d)},onVideoStateChange(d,h){var c,l;n.track("agent-video",{event:d,rtc_stats:h??[]}),(l=(c=a.callbacks).onVideoStateChange)==null||l.call(c,d,h)}}}).catch(g)})}function Se(e,a,t){return q(a,t||y).getById(e)}function Q(e){let a="";if(e.greetings&&e.greetings.length>0){const t=Math.floor(Math.random()*e.greetings.length);a=e.greetings[t]}else a=`Hi! I'm ${e.preview_name}, welcome to agents. How can I help you?`;return[{content:a,id:b(),role:"assistant",created_at:new Date().toISOString()}]}async function Ce(e,a){var l,L;const t={messages:[]},n=a.wsURL||Z,r=a.baseURL||y,i=a.mixpanelKey||G,g=q(a.auth,r),w=ne(a.auth,r),S=te(a.auth,r),d=await g.getById(e),h=await fe(d,S);t.messages=Q(d),(L=(l=a.callbacks).onNewMessage)==null||L.call(l,t.messages);const c=we({mixPanelKey:i,agent:d,...a});return c.track("agent-sdk",{event:"loaded",...he(d)}),{agent:d,starterMessages:h,async connect(){var k,C,_,o,f;const u=await se(a.auth,n,{onMessage:(R,p)=>{var N,X;if("content"in p){const{content:Y}=p,K=t.messages[t.messages.length-1];(K==null?void 0:K.role)==="assistant"&&(K.content=R===O.Partial?K.content+Y:Y),R===O.Complete&&c.track("agent-message-received",{messages:t.messages.length}),(X=(N=a.callbacks).onNewMessage)==null||X.call(N,t.messages)}}}),{streamingManager:m,chat:s}=await ve(d,a,g,c,t.chat);t.messages=Q(d),(C=(k=a.callbacks).onNewMessage)==null||C.call(k,t.messages),s!=null&&s.id&&s.id!==((_=t.chat)==null?void 0:_.id)&&((f=(o=a.callbacks).onNewChat)==null||f.call(o,s==null?void 0:s.id)),t.streamingManager=m,t.socketManager=u,t.chat=s,c.track("agent-chat",{event:"connect",chatId:s.id,agentId:d.id})},async disconnect(){var u,m,s;(u=t.socketManager)==null||u.disconnect(),await((m=t.streamingManager)==null?void 0:m.disconnect()),c.track("agent-chat",{event:"disconnect",chatId:(s=t.chat)==null?void 0:s.id,agentId:d.id})},async chat(u,m=!1){var s,k;try{const C=Date.now();if(u.length===0)throw new Error("Message cannot be empty");if(!t.chat)throw new Error("Chat is not initialized");if(!t.streamingManager)throw new Error("Streaming manager is not initialized");t.messages.push({id:b(),role:"user",content:u,created_at:new Date(C).toISOString()});const _=await g.chat(d.id,t.chat.id,{sessionId:t.streamingManager.sessionId,streamId:t.streamingManager.streamId,messages:t.messages,append_chat:m});return c.track("agent-message-send",{event:"success",messages:t.messages.length+1}),t.messages.push({id:b(),role:"assistant",content:_.result??"",created_at:new Date().toISOString(),matches:_.matches}),_.result&&c.track("agent-message-received",{latency:Date.now()-C,messages:t.messages.length}),(k=(s=a.callbacks).onNewMessage)==null||k.call(s,t.messages),_}catch(C){throw c.track("agent-message-send",{event:"error",messages:t.messages.length}),C}},rate(u,m,s){var _,o,f,R;const k=t.messages.find(p=>p.id===u);if(t.chat){if(!k)throw new Error("Message not found")}else throw new Error("Chat is not initialized");const C=((_=k.matches)==null?void 0:_.map(p=>[p.document_id,p.id]))??[];return c.track("agent-rate",{event:s?"update":"create",thumb:m===1?"up":"down",knowledge_id:((o=d.knowledge)==null?void 0:o.id)??"",matches:C,score:m}),s?w.update(s,{agent_id:d.id,knowledge_id:((f=d.knowledge)==null?void 0:f.id)??"",chat_id:t.chat.id,message_id:u,matches:C,score:m}):w.create({agent_id:d.id,knowledge_id:((R=d.knowledge)==null?void 0:R.id)??"",chat_id:t.chat.id,message_id:u,matches:C,score:m})},deleteRate(u){var m;return c.track("agent-rate-delete",{type:"text",chat_id:(m=t.chat)==null?void 0:m.id,id:u}),w.delete(u)},speak(u){if(!t.streamingManager)throw new Error("Streaming manager is not initialized");function m(){if(u.type==="text")return{type:"text",provider:u.provider?u.provider:d.presenter.voice,input:u.input,ssml:u.ssml||!1};if(u.type==="audio")return{type:"audio",audio_url:u.audio_url};throw new Error("Invalid payload")}const s=m();return c.track("agent-speak",s),t.streamingManager.speak({script:s})}}}v.ChatMode=j,v.DocumentType=B,v.KnowledgeType=z,v.PlanGroup=W,v.Providers=A,v.RateState=H,v.Subject=U,v.UserPlan=D,v.VoiceAccess=x,v.createAgentManager=Ce,v.getAgent=Se,Object.defineProperty(v,Symbol.toStringTag,{value:"Module"})});
|