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