@d-id/client-sdk 1.0.19-beta.55 → 1.0.19-beta.57
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 +309 -286
- package/dist/index.umd.cjs +1 -1
- package/dist/src/auth/getAuthHeader.d.ts +1 -0
- package/dist/src/connectToSocket.d.ts +2 -3
- package/dist/src/createAgentManager.d.ts +1 -1
- package/dist/src/services/mixpanel.d.ts +3 -5
- package/dist/src/types/entities/agents/chat.d.ts +2 -0
- package/dist/src/types/entities/agents/manager.d.ts +11 -29
- package/dist/src/types/stream/stream.d.ts +3 -3
- package/package.json +2 -2
package/dist/index.js
CHANGED
|
@@ -1,26 +1,26 @@
|
|
|
1
|
-
var
|
|
2
|
-
const y = "https://api.d-id.com",
|
|
3
|
-
function
|
|
1
|
+
var j = /* @__PURE__ */ ((e) => (e.Amazon = "amazon", e.Microsoft = "microsoft", e.Afflorithmics = "afflorithmics", e.Elevenlabs = "elevenlabs", e))(j || {}), z = /* @__PURE__ */ ((e) => (e.Public = "public", e.Premium = "premium", e.Private = "private", e))(z || {}), P = /* @__PURE__ */ ((e) => (e.Start = "START", e.Stop = "STOP", e))(P || {}), 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 || {}), k = /* @__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))(k || {}), B = /* @__PURE__ */ ((e) => (e.TRIAL = "trial", e.BASIC = "basic", e.ENTERPRISE = "enterprise", e.LITE = "lite", e.ADVANCED = "advanced", e))(B || {}), F = /* @__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))(F || {}), 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 || {}), O = /* @__PURE__ */ ((e) => (e.Embed = "embed", e.Query = "query", e.Partial = "partial", e.Answer = "answer", e.Complete = "done", e))(O || {}), 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 || {}), x = /* @__PURE__ */ ((e) => (e.Clip = "clip", e.Talk = "talk", e))(x || {});
|
|
2
|
+
const y = "https://api.d-id.com", Y = "wss://notifications.d-id.com", Z = "79f81a83a67430be2bc0fd61042b8faa", N = () => Math.random().toString(16).slice(2);
|
|
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
|
|
8
|
-
function
|
|
7
|
+
let G = N();
|
|
8
|
+
function H(e) {
|
|
9
9
|
if (e.type === "bearer")
|
|
10
10
|
return `Bearer ${e.token}`;
|
|
11
11
|
if (e.type === "basic")
|
|
12
12
|
return `Basic ${btoa(`${e.username}:${e.password}`)}`;
|
|
13
13
|
if (e.type === "key")
|
|
14
|
-
return `Client-Key ${e.clientKey}.${
|
|
14
|
+
return `Client-Key ${e.clientKey}.${D()}_${G}`;
|
|
15
15
|
throw new Error(`Unknown auth type: ${e}`);
|
|
16
16
|
}
|
|
17
|
-
function K(e,
|
|
17
|
+
function K(e, a = y) {
|
|
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,
|
|
23
|
-
Authorization:
|
|
23
|
+
Authorization: H(e),
|
|
24
24
|
"Content-Type": "application/json"
|
|
25
25
|
}
|
|
26
26
|
});
|
|
@@ -60,8 +60,8 @@ function K(e, o = y) {
|
|
|
60
60
|
}
|
|
61
61
|
};
|
|
62
62
|
}
|
|
63
|
-
function
|
|
64
|
-
const t = K(e, `${
|
|
63
|
+
function W(e, a = y) {
|
|
64
|
+
const t = K(e, `${a}/agents`);
|
|
65
65
|
return {
|
|
66
66
|
create(n, r) {
|
|
67
67
|
return t.post("/", n, r);
|
|
@@ -86,8 +86,8 @@ function O(e, o = y) {
|
|
|
86
86
|
}
|
|
87
87
|
};
|
|
88
88
|
}
|
|
89
|
-
function
|
|
90
|
-
const t = K(e, `${
|
|
89
|
+
function ee(e, a = y) {
|
|
90
|
+
const t = K(e, `${a}/knowledge`);
|
|
91
91
|
return {
|
|
92
92
|
createKnowledge(n, r) {
|
|
93
93
|
return t.post("/", n, r);
|
|
@@ -123,8 +123,8 @@ function Z(e, o = y) {
|
|
|
123
123
|
}
|
|
124
124
|
};
|
|
125
125
|
}
|
|
126
|
-
function
|
|
127
|
-
const t = K(e, `${
|
|
126
|
+
function te(e, a = y) {
|
|
127
|
+
const t = K(e, `${a}/chats/ratings`);
|
|
128
128
|
return {
|
|
129
129
|
create(n, r) {
|
|
130
130
|
return t.post("/", n, r);
|
|
@@ -140,49 +140,49 @@ function G(e, o = y) {
|
|
|
140
140
|
}
|
|
141
141
|
};
|
|
142
142
|
}
|
|
143
|
-
const
|
|
144
|
-
function
|
|
145
|
-
return new Promise((
|
|
143
|
+
const ne = (e) => new Promise((a) => setTimeout(a, e));
|
|
144
|
+
function re(e) {
|
|
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
151
|
onMessage: d = null,
|
|
152
|
-
onOpen:
|
|
153
|
-
onClose:
|
|
154
|
-
onError:
|
|
155
|
-
} = n || {},
|
|
156
|
-
|
|
157
|
-
console.error(
|
|
158
|
-
},
|
|
159
|
-
|
|
152
|
+
onOpen: h = null,
|
|
153
|
+
onClose: f = null,
|
|
154
|
+
onError: o = null
|
|
155
|
+
} = n || {}, v = new WebSocket(`${r}?authorization=${H(i)}`);
|
|
156
|
+
v.onmessage = d, v.onclose = f, v.onerror = (m) => {
|
|
157
|
+
console.error(m), o == null || o(m), t(m);
|
|
158
|
+
}, v.onopen = (m) => {
|
|
159
|
+
h == null || h(m), a(v);
|
|
160
160
|
};
|
|
161
161
|
});
|
|
162
162
|
}
|
|
163
|
-
async function
|
|
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
|
-
t = await
|
|
170
|
+
t = await re(e);
|
|
171
171
|
} catch (r) {
|
|
172
|
-
if (n ===
|
|
172
|
+
if (n === a)
|
|
173
173
|
throw r;
|
|
174
|
-
await
|
|
174
|
+
await ne(n * 500);
|
|
175
175
|
}
|
|
176
176
|
return t;
|
|
177
177
|
}
|
|
178
|
-
async function
|
|
179
|
-
const n = t ? [t] : [], r = await
|
|
178
|
+
async function ae(e, a, t) {
|
|
179
|
+
const n = t ? [t] : [], r = await ie({
|
|
180
180
|
auth: e,
|
|
181
|
-
host:
|
|
181
|
+
host: a,
|
|
182
182
|
callbacks: {
|
|
183
183
|
onMessage: (i) => {
|
|
184
184
|
const d = JSON.parse(i.data);
|
|
185
|
-
n.forEach((
|
|
185
|
+
n.forEach((h) => h(d.event, d));
|
|
186
186
|
}
|
|
187
187
|
}
|
|
188
188
|
});
|
|
@@ -192,8 +192,8 @@ async function re(e, o, t) {
|
|
|
192
192
|
subscribeToEvents: (i) => n.push(i)
|
|
193
193
|
};
|
|
194
194
|
}
|
|
195
|
-
function
|
|
196
|
-
const t = K(e,
|
|
195
|
+
function se(e, a) {
|
|
196
|
+
const t = K(e, a);
|
|
197
197
|
return {
|
|
198
198
|
createStream(n) {
|
|
199
199
|
return t.post("/clips/streams", {
|
|
@@ -227,8 +227,8 @@ function ie(e, o) {
|
|
|
227
227
|
}
|
|
228
228
|
};
|
|
229
229
|
}
|
|
230
|
-
function oe(e,
|
|
231
|
-
const t = K(e,
|
|
230
|
+
function oe(e, a) {
|
|
231
|
+
const t = K(e, a);
|
|
232
232
|
return {
|
|
233
233
|
createStream(n, r) {
|
|
234
234
|
return t.post("/talks/streams", {
|
|
@@ -263,13 +263,13 @@ function oe(e, o) {
|
|
|
263
263
|
}
|
|
264
264
|
};
|
|
265
265
|
}
|
|
266
|
-
function
|
|
267
|
-
return e.map((t, n) => n === 0 ?
|
|
266
|
+
function de(e, a) {
|
|
267
|
+
return e.map((t, n) => n === 0 ? a ? {
|
|
268
268
|
index: n,
|
|
269
269
|
timestamp: t.timestamp,
|
|
270
|
-
bytesReceived: t.bytesReceived -
|
|
271
|
-
packetsReceived: t.packetsReceived -
|
|
272
|
-
packetsLost: t.packetsLost -
|
|
270
|
+
bytesReceived: t.bytesReceived - a.bytesReceived,
|
|
271
|
+
packetsReceived: t.packetsReceived - a.packetsReceived,
|
|
272
|
+
packetsLost: t.packetsLost - a.packetsLost,
|
|
273
273
|
jitter: t.jitter,
|
|
274
274
|
frameWidth: t.frameWidth,
|
|
275
275
|
frameHeight: t.frameHeight,
|
|
@@ -296,182 +296,177 @@ function ae(e, o) {
|
|
|
296
296
|
framesPerSecond: t.framesPerSecond
|
|
297
297
|
});
|
|
298
298
|
}
|
|
299
|
-
let
|
|
300
|
-
const
|
|
301
|
-
function
|
|
299
|
+
let U = !1;
|
|
300
|
+
const M = (e, a) => U && console.log(e, a), ce = (window.RTCPeerConnection || window.webkitRTCPeerConnection || window.mozRTCPeerConnection).bind(window);
|
|
301
|
+
function ge(e) {
|
|
302
302
|
switch (e) {
|
|
303
303
|
case "connected":
|
|
304
|
-
return
|
|
304
|
+
return k.Connected;
|
|
305
305
|
case "checking":
|
|
306
|
-
return
|
|
306
|
+
return k.Connecting;
|
|
307
307
|
case "failed":
|
|
308
|
-
return
|
|
308
|
+
return k.Fail;
|
|
309
309
|
case "new":
|
|
310
310
|
case "closed":
|
|
311
311
|
case "disconnected":
|
|
312
312
|
default:
|
|
313
|
-
return
|
|
313
|
+
return k.New;
|
|
314
314
|
}
|
|
315
315
|
}
|
|
316
|
-
function
|
|
316
|
+
function ue(e, a) {
|
|
317
317
|
let t = [], n = 0, r = 0, i;
|
|
318
318
|
return setInterval(() => {
|
|
319
|
-
e.getStats().then((
|
|
320
|
-
|
|
321
|
-
if (
|
|
322
|
-
if (r = t.length - 1,
|
|
323
|
-
const
|
|
324
|
-
let
|
|
325
|
-
i =
|
|
326
|
-
let
|
|
327
|
-
if (
|
|
319
|
+
e.getStats().then((h) => {
|
|
320
|
+
h.forEach((f) => {
|
|
321
|
+
if (f.type === "inbound-rtp" && f.kind === "video") {
|
|
322
|
+
if (r = t.length - 1, f && t[r]) {
|
|
323
|
+
const o = f.bytesReceived, v = t[r].bytesReceived;
|
|
324
|
+
let m = i;
|
|
325
|
+
i = o - v > 0;
|
|
326
|
+
let p;
|
|
327
|
+
if (m !== i) {
|
|
328
328
|
if (i)
|
|
329
329
|
n = t.length;
|
|
330
330
|
else {
|
|
331
|
-
const
|
|
332
|
-
|
|
331
|
+
const L = t.slice(n), c = n === 0 ? void 0 : t[n - 1];
|
|
332
|
+
p = de(L, c), p = p.sort((g, s) => s.packetsLost - g.packetsLost).slice(0, 5);
|
|
333
333
|
}
|
|
334
|
-
|
|
334
|
+
a == null || a(i ? P.Start : P.Stop, p);
|
|
335
335
|
}
|
|
336
336
|
}
|
|
337
|
-
t.push(
|
|
337
|
+
t.push(f);
|
|
338
338
|
}
|
|
339
339
|
});
|
|
340
340
|
});
|
|
341
341
|
}, 500);
|
|
342
342
|
}
|
|
343
|
-
async function
|
|
344
|
-
debug:
|
|
343
|
+
async function me(e, {
|
|
344
|
+
debug: a = !1,
|
|
345
345
|
callbacks: t,
|
|
346
346
|
auth: n,
|
|
347
347
|
analytics: r,
|
|
348
348
|
baseURL: i = y
|
|
349
349
|
}) {
|
|
350
|
-
|
|
350
|
+
U = a;
|
|
351
351
|
let d;
|
|
352
352
|
const {
|
|
353
|
-
startConnection:
|
|
354
|
-
sendStreamRequest:
|
|
355
|
-
close:
|
|
356
|
-
createStream:
|
|
357
|
-
addIceCandidate:
|
|
358
|
-
} = e.videoType === x.Clip ?
|
|
359
|
-
id:
|
|
360
|
-
offer:
|
|
361
|
-
ice_servers:
|
|
362
|
-
session_id:
|
|
363
|
-
} = await
|
|
364
|
-
iceServers:
|
|
365
|
-
}),
|
|
366
|
-
if (!
|
|
353
|
+
startConnection: h,
|
|
354
|
+
sendStreamRequest: f,
|
|
355
|
+
close: o,
|
|
356
|
+
createStream: v,
|
|
357
|
+
addIceCandidate: m
|
|
358
|
+
} = e.videoType === x.Clip ? se(n, i) : oe(n, i), {
|
|
359
|
+
id: p,
|
|
360
|
+
offer: L,
|
|
361
|
+
ice_servers: c,
|
|
362
|
+
session_id: g
|
|
363
|
+
} = await v(e), s = new ce({
|
|
364
|
+
iceServers: c
|
|
365
|
+
}), S = s.createDataChannel("JanusDataChannel");
|
|
366
|
+
if (!g)
|
|
367
367
|
throw new Error("Could not create session_id");
|
|
368
|
-
const
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
candidate:
|
|
372
|
-
sdpMid:
|
|
373
|
-
sdpMLineIndex:
|
|
374
|
-
},
|
|
368
|
+
const C = ue(s, t.onVideoStateChange);
|
|
369
|
+
s.onicecandidate = (u) => {
|
|
370
|
+
M("peerConnection.onicecandidate", u), u.candidate && u.candidate.sdpMid && u.candidate.sdpMLineIndex !== null ? m(p, {
|
|
371
|
+
candidate: u.candidate.candidate,
|
|
372
|
+
sdpMid: u.candidate.sdpMid,
|
|
373
|
+
sdpMLineIndex: u.candidate.sdpMLineIndex
|
|
374
|
+
}, g) : m(p, {
|
|
375
375
|
candidate: null
|
|
376
|
-
},
|
|
377
|
-
},
|
|
378
|
-
var
|
|
379
|
-
|
|
380
|
-
const
|
|
381
|
-
|
|
382
|
-
var
|
|
383
|
-
(
|
|
384
|
-
}, 5e3) : (clearTimeout(d), (
|
|
385
|
-
},
|
|
386
|
-
var
|
|
387
|
-
|
|
388
|
-
},
|
|
389
|
-
var
|
|
390
|
-
if (
|
|
391
|
-
const [
|
|
392
|
-
|
|
376
|
+
}, g);
|
|
377
|
+
}, s.oniceconnectionstatechange = () => {
|
|
378
|
+
var w;
|
|
379
|
+
M("peerConnection.oniceconnectionstatechange => " + s.iceConnectionState);
|
|
380
|
+
const u = ge(s.iceConnectionState);
|
|
381
|
+
u === k.Connected ? d = setTimeout(() => {
|
|
382
|
+
var _;
|
|
383
|
+
(_ = t.onConnectionStateChange) == null || _.call(t, k.Connected);
|
|
384
|
+
}, 5e3) : (clearTimeout(d), (w = t.onConnectionStateChange) == null || w.call(t, u));
|
|
385
|
+
}, s.ontrack = (u) => {
|
|
386
|
+
var w;
|
|
387
|
+
M("peerConnection.ontrack", u), (w = t.onSrcObjectReady) == null || w.call(t, u.streams[0]);
|
|
388
|
+
}, S.onmessage = (u) => {
|
|
389
|
+
var w, _, R;
|
|
390
|
+
if (S.readyState === "open") {
|
|
391
|
+
const [l, A] = u.data.split(":");
|
|
392
|
+
l === I.StreamStarted ? r == null || r.track("agent-video", {
|
|
393
393
|
event: "start",
|
|
394
|
-
...
|
|
395
|
-
})
|
|
394
|
+
...l
|
|
395
|
+
}) : l === I.StreamDone ? r == null || r.track("agent-video", {
|
|
396
396
|
event: "stop",
|
|
397
|
-
rtcStats:
|
|
398
|
-
...
|
|
399
|
-
})
|
|
400
|
-
event:
|
|
401
|
-
data:
|
|
402
|
-
}), clearInterval(
|
|
397
|
+
rtcStats: A ?? [],
|
|
398
|
+
...l
|
|
399
|
+
}) : l === I.StreamFailed ? ((w = t.onVideoStateChange) == null || w.call(t, P.Stop, {
|
|
400
|
+
event: l,
|
|
401
|
+
data: A
|
|
402
|
+
}), clearInterval(C)) : l === I.StreamReady ? (clearTimeout(d), (_ = t.onConnectionStateChange) == null || _.call(t, k.Connected)) : l === I.StreamCreated ? r == null || r.track("agent-video", {
|
|
403
403
|
event: "created",
|
|
404
|
-
...
|
|
405
|
-
})
|
|
404
|
+
...l
|
|
405
|
+
}) : l === I.StreamVideoCreated ? r == null || r.track("agent-video", {
|
|
406
406
|
event: "video-created",
|
|
407
|
-
...
|
|
408
|
-
})
|
|
407
|
+
...l
|
|
408
|
+
}) : l === I.StreamVideoDone ? r == null || r.track("agent-video", {
|
|
409
409
|
event: "video-done",
|
|
410
|
-
rtcStats:
|
|
411
|
-
...
|
|
412
|
-
})
|
|
410
|
+
rtcStats: A ?? [],
|
|
411
|
+
...l
|
|
412
|
+
}) : l === I.StreamVideoError ? r == null || r.track("agent-video", {
|
|
413
413
|
event: "video-error",
|
|
414
|
-
...
|
|
415
|
-
})
|
|
416
|
-
...c
|
|
417
|
-
})), (M = t.onMessage) == null || M.call(t, c, decodeURIComponent(C)));
|
|
414
|
+
...l
|
|
415
|
+
}) : (l === I.ChatAnswer && (r == null || r.track("agent-message-recieved ", l)), (R = t.onMessage) == null || R.call(t, l, decodeURIComponent(A)));
|
|
418
416
|
}
|
|
419
|
-
}, await
|
|
420
|
-
const
|
|
421
|
-
return
|
|
417
|
+
}, await s.setRemoteDescription(L), M("set remote description OK");
|
|
418
|
+
const $ = await s.createAnswer();
|
|
419
|
+
return M("create answer OK"), await s.setLocalDescription($), M("set local description OK"), await h(p, $, g), M("start connection OK"), {
|
|
422
420
|
/**
|
|
423
421
|
* Method to send request to server to get clip or talk depend on you payload
|
|
424
422
|
* @param payload
|
|
425
423
|
*/
|
|
426
|
-
speak(
|
|
427
|
-
return
|
|
424
|
+
speak(u) {
|
|
425
|
+
return f(p, g, u);
|
|
428
426
|
},
|
|
429
427
|
/**
|
|
430
428
|
* Method to close RTC connection
|
|
431
429
|
*/
|
|
432
430
|
async disconnect() {
|
|
433
|
-
var
|
|
434
|
-
|
|
435
|
-
}), (
|
|
431
|
+
var u, w;
|
|
432
|
+
p && (s && (s.close(), s.oniceconnectionstatechange = null, s.onnegotiationneeded = null, s.onicecandidate = null, s.ontrack = null), await o(p, g).catch((_) => {
|
|
433
|
+
}), (u = t.onConnectionStateChange) == null || u.call(t, k.New), (w = t.onVideoStateChange) == null || w.call(t, P.Stop), clearInterval(C));
|
|
436
434
|
},
|
|
437
435
|
/**
|
|
438
436
|
* Session identifier information, should be returned in the body of all streaming requests
|
|
439
437
|
*/
|
|
440
|
-
sessionId:
|
|
438
|
+
sessionId: g,
|
|
441
439
|
/**
|
|
442
440
|
* Id of current RTC stream
|
|
443
441
|
*/
|
|
444
|
-
streamId:
|
|
442
|
+
streamId: p
|
|
445
443
|
};
|
|
446
444
|
}
|
|
447
445
|
function le(e) {
|
|
448
|
-
const
|
|
446
|
+
const a = {
|
|
449
447
|
mixPanelKey: e.mixPanelKey || "testKey",
|
|
450
|
-
distinct_id: e.distinctId ||
|
|
448
|
+
distinct_id: e.distinctId || D(),
|
|
451
449
|
isEnabled: e.isEnabled ?? !0,
|
|
452
|
-
chatId: e.chatId,
|
|
453
450
|
agentId: e.agent.id,
|
|
454
451
|
owner_id: e.agent.owner_id ?? ""
|
|
455
452
|
};
|
|
456
453
|
return {
|
|
457
|
-
...
|
|
458
|
-
getRandom()
|
|
459
|
-
|
|
460
|
-
},
|
|
461
|
-
track(n, r) {
|
|
454
|
+
...a,
|
|
455
|
+
getRandom: () => Math.random().toString(16).slice(2),
|
|
456
|
+
track(t, n) {
|
|
462
457
|
if (!this.isEnabled)
|
|
463
458
|
return Promise.reject("MixPanel analytics is disabled on creation");
|
|
464
|
-
const
|
|
459
|
+
const r = {
|
|
465
460
|
method: "POST",
|
|
466
461
|
headers: {
|
|
467
462
|
"Content-Type": "application/x-www-form-urlencoded"
|
|
468
463
|
},
|
|
469
464
|
body: new URLSearchParams({
|
|
470
465
|
data: JSON.stringify([{
|
|
471
|
-
event:
|
|
466
|
+
event: t,
|
|
472
467
|
properties: {
|
|
473
|
-
...
|
|
474
|
-
...
|
|
468
|
+
...n,
|
|
469
|
+
...a,
|
|
475
470
|
time: Date.now(),
|
|
476
471
|
$insert_id: this.getRandom(),
|
|
477
472
|
origin: window.location.href,
|
|
@@ -482,19 +477,19 @@ function le(e) {
|
|
|
482
477
|
}])
|
|
483
478
|
})
|
|
484
479
|
};
|
|
485
|
-
return fetch("https://api-js.mixpanel.com/track/?verbose=1&ip=1",
|
|
480
|
+
return fetch("https://api-js.mixpanel.com/track/?verbose=1&ip=1", r).then((i) => i.json()).catch((i) => console.error(i));
|
|
486
481
|
}
|
|
487
482
|
};
|
|
488
483
|
}
|
|
489
|
-
function
|
|
484
|
+
function we(e) {
|
|
490
485
|
var n, r;
|
|
491
|
-
const
|
|
486
|
+
const a = () => /Mobi|Android/i.test(navigator.userAgent) ? "Mobile" : "Desktop";
|
|
492
487
|
return {
|
|
493
488
|
$os: `${(() => {
|
|
494
489
|
const i = navigator.platform;
|
|
495
490
|
return i.toLowerCase().includes("win") ? "Windows" : i.toLowerCase().includes("mac") ? "Mac OS X" : i.toLowerCase().includes("linux") ? "Linux" : "Unknown";
|
|
496
491
|
})()}`,
|
|
497
|
-
isMobile: `${
|
|
492
|
+
isMobile: `${a() == "Mobile"}`,
|
|
498
493
|
browser: navigator.userAgent,
|
|
499
494
|
origin: window.location.origin,
|
|
500
495
|
agentType: e.presenter.type,
|
|
@@ -504,11 +499,11 @@ function ge(e) {
|
|
|
504
499
|
}
|
|
505
500
|
};
|
|
506
501
|
}
|
|
507
|
-
function
|
|
502
|
+
function he(e, a) {
|
|
508
503
|
var t;
|
|
509
|
-
return (t = e.knowledge) != null && t.id ?
|
|
504
|
+
return (t = e.knowledge) != null && t.id ? a.getKnowledge(e.knowledge.id).then((n) => (n == null ? void 0 : n.starter_message) || []) : [];
|
|
510
505
|
}
|
|
511
|
-
function
|
|
506
|
+
function fe(e) {
|
|
512
507
|
return e.presenter.type === x.Clip ? {
|
|
513
508
|
videoType: x.Clip,
|
|
514
509
|
driver_id: e.presenter.driver_id,
|
|
@@ -518,174 +513,202 @@ function we(e) {
|
|
|
518
513
|
source_url: e.presenter.source_url
|
|
519
514
|
};
|
|
520
515
|
}
|
|
521
|
-
function
|
|
516
|
+
function pe(e, a, t, n, r) {
|
|
522
517
|
return new Promise(async (i, d) => {
|
|
523
|
-
const
|
|
524
|
-
...
|
|
525
|
-
analytics: n,
|
|
518
|
+
const h = await me(fe(e), {
|
|
519
|
+
...a,
|
|
526
520
|
callbacks: {
|
|
527
|
-
...
|
|
528
|
-
onConnectionStateChange: async (
|
|
529
|
-
var
|
|
530
|
-
if (
|
|
521
|
+
...a.callbacks,
|
|
522
|
+
onConnectionStateChange: async (f) => {
|
|
523
|
+
var o, v;
|
|
524
|
+
if (f === k.Connected)
|
|
531
525
|
try {
|
|
532
526
|
r || (r = await t.newChat(e.id), n.track("agent-chat", {
|
|
533
527
|
event: "created",
|
|
534
|
-
|
|
535
|
-
|
|
528
|
+
chat_id: r.id,
|
|
529
|
+
agent_id: e.id
|
|
536
530
|
})), i({
|
|
537
531
|
chat: r,
|
|
538
|
-
streamingManager:
|
|
532
|
+
streamingManager: h
|
|
539
533
|
});
|
|
540
|
-
} catch (
|
|
541
|
-
console.error(
|
|
534
|
+
} catch (m) {
|
|
535
|
+
console.error(m), d("Cannot create new chat");
|
|
542
536
|
}
|
|
543
537
|
else
|
|
544
|
-
|
|
545
|
-
(
|
|
538
|
+
f === k.Fail && d(new Error("Cannot create connection"));
|
|
539
|
+
(v = (o = a.callbacks).onConnectionStateChange) == null || v.call(o, f);
|
|
546
540
|
}
|
|
547
541
|
}
|
|
548
542
|
});
|
|
549
543
|
});
|
|
550
544
|
}
|
|
551
|
-
function
|
|
552
|
-
return
|
|
545
|
+
function ve(e, a, t) {
|
|
546
|
+
return W(a, t || y).getById(e);
|
|
553
547
|
}
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
548
|
+
function T(e) {
|
|
549
|
+
let a = "";
|
|
550
|
+
if (e.greetings && e.greetings.length > 0) {
|
|
551
|
+
const t = Math.floor(Math.random() * e.greetings.length);
|
|
552
|
+
a = e.greetings[t];
|
|
553
|
+
} else
|
|
554
|
+
a = `Hi! I'm ${e.preview_name}, welcome to agents. How can I help you?`;
|
|
555
|
+
return [{
|
|
556
|
+
content: a,
|
|
557
|
+
id: N(),
|
|
558
|
+
role: "assistant",
|
|
559
|
+
created_at: (/* @__PURE__ */ new Date()).toISOString()
|
|
560
|
+
}];
|
|
561
|
+
}
|
|
562
|
+
async function Se(e, a) {
|
|
563
|
+
var p, L;
|
|
564
|
+
const t = {
|
|
565
|
+
messages: []
|
|
566
|
+
}, n = a.wsURL || Y, r = a.baseURL || y, i = a.mixpanelKey || Z, d = W(a.auth, r), h = te(a.auth, r), f = ee(a.auth, r), o = await d.getById(e), v = await he(o, f);
|
|
567
|
+
t.messages = T(o), (L = (p = a.callbacks).onNewMessage) == null || L.call(p, t.messages);
|
|
568
|
+
const m = le({
|
|
559
569
|
mixPanelKey: i,
|
|
560
|
-
agent:
|
|
561
|
-
...
|
|
562
|
-
}), p = await re(o.auth, n, o.callbacks.onChatEvents);
|
|
563
|
-
let {
|
|
564
|
-
chat: g,
|
|
565
|
-
streamingManager: I
|
|
566
|
-
} = await P(s, o, d, u);
|
|
567
|
-
u.track("agent-sdk", {
|
|
568
|
-
event: "loaded",
|
|
569
|
-
...ge(s)
|
|
570
|
+
agent: o,
|
|
571
|
+
...a
|
|
570
572
|
});
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
573
|
+
return m.track("agent-sdk", {
|
|
574
|
+
event: "loaded",
|
|
575
|
+
...we(o)
|
|
576
|
+
}), {
|
|
577
|
+
agent: o,
|
|
578
|
+
starterMessages: v,
|
|
579
|
+
async connect() {
|
|
580
|
+
var S, C, $, u, w;
|
|
581
|
+
const c = await ae(a.auth, n, (_, R) => {
|
|
582
|
+
var l, A;
|
|
583
|
+
if ("content" in R) {
|
|
584
|
+
const {
|
|
585
|
+
content: b
|
|
586
|
+
} = R, E = t.messages[t.messages.length - 1];
|
|
587
|
+
(E == null ? void 0 : E.role) === "assistant" && (E.content = _ === O.Partial ? E.content + b : b), (A = (l = a.callbacks).onNewMessage) == null || A.call(l, t.messages);
|
|
588
|
+
}
|
|
589
|
+
}), {
|
|
590
|
+
streamingManager: g,
|
|
591
|
+
chat: s
|
|
592
|
+
} = await pe(o, a, d, m, t.chat);
|
|
593
|
+
t.messages = T(o), (C = (S = a.callbacks).onNewMessage) == null || C.call(S, t.messages), s != null && s.id && s.id !== (($ = t.chat) == null ? void 0 : $.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", {
|
|
594
|
+
event: "connect",
|
|
595
|
+
chatId: s.id,
|
|
596
|
+
agentId: o.id
|
|
581
597
|
});
|
|
582
|
-
const {
|
|
583
|
-
streamingManager: a
|
|
584
|
-
} = await P(s, o, d, u, g);
|
|
585
|
-
I = a;
|
|
586
598
|
},
|
|
587
|
-
disconnect() {
|
|
588
|
-
|
|
599
|
+
async disconnect() {
|
|
600
|
+
var c, g, s;
|
|
601
|
+
(c = t.socketManager) == null || c.disconnect(), await ((g = t.streamingManager) == null ? void 0 : g.disconnect()), m.track("agent-chat", {
|
|
589
602
|
event: "disconnect",
|
|
590
|
-
chatId:
|
|
591
|
-
agentId:
|
|
592
|
-
}), r.abort(), p.disconnect(), I.disconnect();
|
|
593
|
-
},
|
|
594
|
-
chat(a, v = !1) {
|
|
595
|
-
if (a.length === 0)
|
|
596
|
-
throw new Error("Messages cannot be empty");
|
|
597
|
-
return u.track("agent-message-send", {
|
|
598
|
-
event: "success",
|
|
599
|
-
messages: a.length + 1
|
|
600
|
-
}), a[a.length - 1].created_at = (/* @__PURE__ */ new Date()).toISOString(), a[a.length - 1].role || (a[a.length - 1].role = "user"), d.chat(s.id, g.id, {
|
|
601
|
-
sessionId: I.sessionId,
|
|
602
|
-
streamId: I.streamId,
|
|
603
|
-
messages: a,
|
|
604
|
-
append_chat: v
|
|
605
|
-
}, {
|
|
606
|
-
signal: r.signal
|
|
603
|
+
chatId: (s = t.chat) == null ? void 0 : s.id,
|
|
604
|
+
agentId: o.id
|
|
607
605
|
});
|
|
608
606
|
},
|
|
609
|
-
|
|
610
|
-
var S,
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
607
|
+
async chat(c, g = !1) {
|
|
608
|
+
var S, C;
|
|
609
|
+
if (c.length === 0)
|
|
610
|
+
throw new Error("Message cannot be empty");
|
|
611
|
+
if (!t.chat)
|
|
612
|
+
throw new Error("Chat is not initialized");
|
|
613
|
+
if (!t.streamingManager)
|
|
614
|
+
throw new Error("Streaming manager is not initialized");
|
|
615
|
+
m.track("agent-message-send", {
|
|
616
|
+
event: "success",
|
|
617
|
+
messages: t.messages.length + 1
|
|
618
|
+
}), t.messages.push({
|
|
619
|
+
id: N(),
|
|
620
|
+
role: "user",
|
|
621
|
+
content: c,
|
|
622
|
+
created_at: (/* @__PURE__ */ new Date()).toISOString()
|
|
623
|
+
});
|
|
624
|
+
const s = await d.chat(o.id, t.chat.id, {
|
|
625
|
+
sessionId: t.streamingManager.sessionId,
|
|
626
|
+
streamId: t.streamingManager.streamId,
|
|
627
|
+
messages: t.messages,
|
|
628
|
+
append_chat: g
|
|
629
|
+
});
|
|
630
|
+
return t.messages.push({
|
|
631
|
+
id: N(),
|
|
632
|
+
role: "assistant",
|
|
633
|
+
content: s.result ?? "",
|
|
634
|
+
created_at: (/* @__PURE__ */ new Date()).toISOString(),
|
|
635
|
+
matches: s.matches
|
|
636
|
+
}), (C = (S = a.callbacks).onNewMessage) == null || C.call(S, t.messages), s;
|
|
637
|
+
},
|
|
638
|
+
rate(c, g, s) {
|
|
639
|
+
var $, u, w, _;
|
|
640
|
+
const S = t.messages.find((R) => R.id === c);
|
|
641
|
+
if (t.chat) {
|
|
642
|
+
if (!S)
|
|
643
|
+
throw new Error("Message not found");
|
|
644
|
+
} else
|
|
645
|
+
throw new Error("Chat is not initialized");
|
|
646
|
+
const C = (($ = S.matches) == null ? void 0 : $.map((R) => [R.document_id, R.id])) ?? [];
|
|
647
|
+
return m.track("agent-rate", {
|
|
648
|
+
event: s ? "update" : "create",
|
|
649
|
+
thumb: g === 1 ? "up" : "down",
|
|
650
|
+
knowledge_id: ((u = o.knowledge) == null ? void 0 : u.id) ?? "",
|
|
651
|
+
matches: C,
|
|
652
|
+
score: g
|
|
653
|
+
}), s ? h.update(s, {
|
|
654
|
+
agent_id: o.id,
|
|
655
|
+
knowledge_id: ((w = o.knowledge) == null ? void 0 : w.id) ?? "",
|
|
656
|
+
chat_id: t.chat.id,
|
|
657
|
+
message_id: c,
|
|
658
|
+
matches: C,
|
|
659
|
+
score: g
|
|
660
|
+
}) : h.create({
|
|
661
|
+
agent_id: o.id,
|
|
662
|
+
knowledge_id: ((_ = o.knowledge) == null ? void 0 : _.id) ?? "",
|
|
663
|
+
chat_id: t.chat.id,
|
|
664
|
+
message_id: c,
|
|
665
|
+
matches: C,
|
|
666
|
+
score: g
|
|
630
667
|
});
|
|
631
668
|
},
|
|
632
|
-
deleteRate(
|
|
633
|
-
|
|
669
|
+
deleteRate(c) {
|
|
670
|
+
var g;
|
|
671
|
+
return m.track("agent-rate-delete", {
|
|
634
672
|
type: "text",
|
|
635
|
-
chat_id: g.id,
|
|
636
|
-
id:
|
|
637
|
-
}),
|
|
638
|
-
},
|
|
639
|
-
speak(
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
input: a.input,
|
|
646
|
-
ssml: a.ssml || !1
|
|
647
|
-
});
|
|
648
|
-
let k = s.presenter.voice;
|
|
649
|
-
return a.provider && (k = a.provider), {
|
|
673
|
+
chat_id: (g = t.chat) == null ? void 0 : g.id,
|
|
674
|
+
id: c
|
|
675
|
+
}), h.delete(c);
|
|
676
|
+
},
|
|
677
|
+
speak(c) {
|
|
678
|
+
if (!t.streamingManager)
|
|
679
|
+
throw new Error("Streaming manager is not initialized");
|
|
680
|
+
function g() {
|
|
681
|
+
if (c.type === "text")
|
|
682
|
+
return {
|
|
650
683
|
type: "text",
|
|
651
|
-
provider:
|
|
652
|
-
input:
|
|
653
|
-
ssml:
|
|
684
|
+
provider: c.provider ? c.provider : o.presenter.voice,
|
|
685
|
+
input: c.input,
|
|
686
|
+
ssml: c.ssml || !1
|
|
654
687
|
};
|
|
655
|
-
|
|
656
|
-
return
|
|
657
|
-
type: "audio",
|
|
658
|
-
audio_url: a.audio_url
|
|
659
|
-
}), {
|
|
688
|
+
if (c.type === "audio")
|
|
689
|
+
return {
|
|
660
690
|
type: "audio",
|
|
661
|
-
audio_url:
|
|
691
|
+
audio_url: c.audio_url
|
|
662
692
|
};
|
|
663
693
|
throw new Error("Invalid payload");
|
|
664
694
|
}
|
|
665
|
-
|
|
666
|
-
|
|
695
|
+
const s = g();
|
|
696
|
+
return m.track("agent-speak", s), t.streamingManager.speak({
|
|
697
|
+
script: s
|
|
667
698
|
});
|
|
668
|
-
},
|
|
669
|
-
async getStarterMessages() {
|
|
670
|
-
var a;
|
|
671
|
-
return (a = s.knowledge) != null && a.id ? w.getKnowledge(s.knowledge.id).then((v) => (v == null ? void 0 : v.starter_message) || []) : [];
|
|
672
|
-
},
|
|
673
|
-
track(a, v) {
|
|
674
|
-
return o.enableAnalitics ? u.track(a, v) : Promise.reject(new Error("Analytics was disabled on create step"));
|
|
675
699
|
}
|
|
676
700
|
};
|
|
677
701
|
}
|
|
678
702
|
export {
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
q as KnowledgeType,
|
|
703
|
+
q as ChatMode,
|
|
704
|
+
X as DocumentType,
|
|
705
|
+
Q as KnowledgeType,
|
|
683
706
|
F as PlanGroup,
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
707
|
+
j as Providers,
|
|
708
|
+
V as RateState,
|
|
709
|
+
J as Subject,
|
|
710
|
+
B as UserPlan,
|
|
711
|
+
z as VoiceAccess,
|
|
712
|
+
Se as createAgentManager,
|
|
713
|
+
ve as getAgent
|
|
691
714
|
};
|
package/dist/index.umd.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
(function(f,L){typeof exports=="object"&&typeof module<"u"?L(exports):typeof define=="function"&&define.amd?define(["exports"],L):(f=typeof globalThis<"u"?globalThis:f||self,L(f.index={}))})(this,function(f){"use strict";var L=(e=>(e.Amazon="amazon",e.Microsoft="microsoft",e.Afflorithmics="afflorithmics",e.Elevenlabs="elevenlabs",e))(L||{}),O=(e=>(e.Public="public",e.Premium="premium",e.Private="private",e))(O||{}),P=(e=>(e.Start="START",e.Stop="STOP",e))(P||{}),I=(e=>(e.ChatAnswer="chat/answer",e.ChatPartial="chat/partial",e.StreamDone="stream/done",e.StreamStarted="stream/started",e.StreamReady="stream/ready",e.StreamFailed="stream/error",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=(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||{}),x=(e=>(e.TRIAL="trial",e.BASIC="basic",e.ENTERPRISE="enterprise",e.LITE="lite",e.ADVANCED="advanced",e))(x||{}),D=(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))(D||{}),N=(e=>(e.Unrated="Unrated",e.Positive="Positive",e.Negative="Negative",e))(N||{}),j=(e=>(e.Functional="Functional",e.TextOnly="TextOnly",e.Maintenance="Maintenance",e))(j||{}),W=(e=>(e.Embed="embed",e.Query="query",e.Partial="partial",e.Answer="answer",e.Complete="done",e))(W||{}),U=(e=>(e.KnowledgeProcessing="knowledge/processing",e.KnowledgeIndexing="knowledge/indexing",e.KnowledgeFailed="knowledge/error",e.KnowledgeDone="knowledge/done",e))(U||{}),F=(e=>(e.Knowledge="knowledge",e.Document="document",e.Record="record",e))(F||{}),H=(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))(H||{}),b=(e=>(e.Clip="clip",e.Talk="talk",e))(b||{});const y="https://api.d-id.com",Q="wss://notifications.d-id.com",X="79f81a83a67430be2bc0fd61042b8faa",Y=()=>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 Z=Y();function B(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()}_${Z}`;throw new Error(`Unknown auth type: ${e}`)}function E(e,o=y){const t=async(n,r)=>{const i=await fetch(o+(n!=null&&n.startsWith("/")?n:`/${n}`),{...r,headers:{...r==null?void 0:r.headers,Authorization:B(e),"Content-Type":"application/json"}});if(!i.ok){let a=await i.text().catch(()=>"Failed to fetch");throw new Error(a)}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,o=y){const t=E(e,`${o}/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,a){return t.post(`/${n}/chat/${r}`,i,a)}}}function G(e,o=y){const t=E(e,`${o}/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 ee(e,o=y){const t=E(e,`${o}/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 te=e=>new Promise(o=>setTimeout(o,e));function ne(e){return new Promise((o,t)=>{const{callbacks:n,host:r,auth:i}=e,{onMessage:a=null,onOpen:h=null,onClose:w=null,onError:s=null}=n||{},u=new WebSocket(`${r}?authorization=${B(i)}`);u.onmessage=a,u.onclose=w,u.onerror=p=>{console.error(p),s==null||s(p),t(p)},u.onopen=p=>{h==null||h(p),o(u)}})}async function re(e){const{retries:o=1}=e;let t=null;for(let n=0;(t==null?void 0:t.readyState)!==WebSocket.OPEN;n++)try{t=await ne(e)}catch(r){if(n===o)throw r;await te(n*500)}return t}async function ie(e,o,t){const n=t?[t]:[],r=await re({auth:e,host:o,callbacks:{onMessage:i=>{const a=JSON.parse(i.data);n.forEach(h=>h(a.event,a))}}});return{socket:r,disconnect:()=>r.close(),subscribeToEvents:i=>n.push(i)}}function oe(e,o){const t=E(e,o);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,o){const t=E(e,o);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,a){return t.post(`/talks/streams/${n}/sdp`,{session_id:i,answer:r},a)},addIceCandidate(n,r,i,a){return t.post(`/talks/streams/${n}/ice`,{session_id:i,...r},a)},sendStreamRequest(n,r,i,a){return t.post(`/talks/streams/${n}`,{session_id:r,...i},a)},close(n,r,i){return t.delete(`/talks/streams/${n}`,{session_id:r},i)}}}function ae(e,o){return e.map((t,n)=>n===0?o?{index:n,timestamp:t.timestamp,bytesReceived:t.bytesReceived-o.bytesReceived,packetsReceived:t.packetsReceived-o.packetsReceived,packetsLost:t.packetsLost-o.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 z=!1;const M=(e,o)=>z&&console.log(e,o),se=(window.RTCPeerConnection||window.webkitRTCPeerConnection||window.mozRTCPeerConnection).bind(window);function ce(e){switch(e){case"connected":return R.Connected;case"checking":return R.Connecting;case"failed":return R.Fail;case"new":case"closed":case"disconnected":default:return R.New}}function ue(e,o){let t=[],n=0,r=0,i;return setInterval(()=>{e.getStats().then(h=>{h.forEach(w=>{if(w.type==="inbound-rtp"&&w.kind==="video"){if(r=t.length-1,w&&t[r]){const s=w.bytesReceived,u=t[r].bytesReceived;let p=i;i=s-u>0;let g;if(p!==i){if(i)n=t.length;else{const k=t.slice(n),K=n===0?void 0:t[n-1];g=ae(k,K),g=g.sort((v,m)=>m.packetsLost-v.packetsLost).slice(0,5)}o==null||o(i?P.Start:P.Stop,g)}}t.push(w)}})})},500)}async function le(e,{debug:o=!1,callbacks:t,auth:n,analytics:r,baseURL:i=y}){z=o;let a;const{startConnection:h,sendStreamRequest:w,close:s,createStream:u,addIceCandidate:p}=e.videoType===b.Clip?oe(n,i):de(n,i),{id:g,offer:k,ice_servers:K,session_id:v}=await u(e),m=new se({iceServers:K}),d=m.createDataChannel("JanusDataChannel");if(!v)throw new Error("Could not create session_id");const S=ue(m,t.onVideoStateChange);m.onicecandidate=l=>{M("peerConnection.onicecandidate",l),l.candidate&&l.candidate.sdpMid&&l.candidate.sdpMLineIndex!==null?p(g,{candidate:l.candidate.candidate,sdpMid:l.candidate.sdpMid,sdpMLineIndex:l.candidate.sdpMLineIndex},v):p(g,{candidate:null},v)},m.oniceconnectionstatechange=()=>{var C;M("peerConnection.oniceconnectionstatechange => "+m.iceConnectionState);const l=ce(m.iceConnectionState);l===R.Connected?a=setTimeout(()=>{var $;($=t.onConnectionStateChange)==null||$.call(t,R.Connected)},5e3):(clearTimeout(a),(C=t.onConnectionStateChange)==null||C.call(t,l))},m.ontrack=l=>{var C;M("peerConnection.ontrack",l),(C=t.onSrcObjectReady)==null||C.call(t,l.streams[0])},d.onmessage=l=>{var C,$,T;if(d.readyState==="open"){const[c,_]=l.data.split(":");c===I.StreamStarted?(console.log("StreamStarted",c,_),r==null||r.track("agent-video",{event:"start",...c})):c===I.StreamDone?(r==null||r.track("agent-video",{event:"stop",rtcStats:_??[],...c}),console.log("StreamDone")):c===I.StreamFailed?((C=t.onVideoStateChange)==null||C.call(t,P.Stop,{event:c,data:_}),clearInterval(S),console.log("StreamFailed")):c===I.StreamReady?(console.log("StreamReady"),clearTimeout(a),($=t.onConnectionStateChange)==null||$.call(t,R.Connected)):c===I.StreamCreated?(console.log("StreamStarted",c,_),r==null||r.track("agent-video",{event:"created",...c})):c===I.StreamVideoCreated?(console.log("StreamVideoCreated",c,_),r==null||r.track("agent-video",{event:"video-created",...c})):c===I.StreamVideoDone?(console.log("StreamVideoDone",c,_),r==null||r.track("agent-video",{event:"video-done",rtcStats:_??[],...c})):c===I.StreamVideoError?(console.log("StreamVideoError",c,_),r==null||r.track("agent-video",{event:"video-error",...c})):(c===I.ChatAnswer&&(r==null||r.track("agent-message-recieved ",{...c})),(T=t.onMessage)==null||T.call(t,c,decodeURIComponent(_)))}},await m.setRemoteDescription(k),M("set remote description OK");const A=await m.createAnswer();return M("create answer OK"),await m.setLocalDescription(A),M("set local description OK"),await h(g,A,v),M("start connection OK"),{speak(l){return w(g,v,l)},async disconnect(){var l,C;g&&(m&&(m.close(),m.oniceconnectionstatechange=null,m.onnegotiationneeded=null,m.onicecandidate=null,m.ontrack=null),await s(g,v).catch($=>{}),(l=t.onConnectionStateChange)==null||l.call(t,R.New),(C=t.onVideoStateChange)==null||C.call(t,P.Stop),clearInterval(S))},sessionId:v,streamId:g}}function ge(e){const o={mixPanelKey:e.mixPanelKey||"testKey",distinct_id:e.distinctId||V(),isEnabled:e.isEnabled??!0,chatId:e.chatId,agentId:e.agent.id,owner_id:e.agent.owner_id??""};return{...o,getRandom(){return Math.random().toString(16).slice(2)},track(n,r){if(!this.isEnabled)return Promise.reject("MixPanel analytics is disabled on creation");const i={method:"POST",headers:{"Content-Type":"application/x-www-form-urlencoded"},body:new URLSearchParams({data:JSON.stringify([{event:n,properties:{...r,...o,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",i).then(a=>a.json()).catch(a=>console.error(a))}}}function me(e){var n,r;const o=()=>/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:`${o()=="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,o){var t;return(t=e.knowledge)!=null&&t.id?o.getKnowledge(e.knowledge.id).then(n=>(n==null?void 0:n.starter_message)||[]):[]}function we(e){return e.presenter.type===b.Clip?{videoType:b.Clip,driver_id:e.presenter.driver_id,presenter_id:e.presenter.presenter_id}:{videoType:b.Talk,source_url:e.presenter.source_url}}function J(e,o,t,n,r){return new Promise(async(i,a)=>{const h=await le(we(e),{...o,analytics:n,callbacks:{...o.callbacks,onConnectionStateChange:async w=>{var s,u;if(w===R.Connected)try{r||(r=await t.newChat(e.id),n.track("agent-chat",{event:"created",chatId:r.id,agentId:e.id})),i({chat:r,streamingManager:h})}catch(p){console.error(p),a(new Error("Cannot create new chat"))}else w===R.Fail&&a(new Error("Cannot create connection"));(u=(s=o.callbacks).onConnectionStateChange)==null||u.call(s,w)}}})})}function he(e,o,t){return q(o,t||y).getById(e)}async function pe(e,o){var v,m;const t=o.baseURL||y,n=o.wsURL||Q,r=new AbortController,i=o.mixpanelKey||X,a=q(o.auth,t),h=ee(o.auth,t),w=G(o.auth,t),s=typeof e=="string"?await a.getById(e):e;(m=(v=o.callbacks)==null?void 0:v.onAgentReady)==null||m.call(v,s);const u=ge({mixPanelKey:i,agent:s,...o}),p=await ie(o.auth,n,o.callbacks.onChatEvents);let{chat:g,streamingManager:k}=await J(s,o,a,u);u.track("agent-sdk",{event:"loaded",...me(s)});const K=await fe(s,w);return{agent:s,chatId:g.id,starterMessages:K,async reconnect(){u.track("agent-chat",{event:"reconnect",chatId:g.id,agentId:typeof e=="string"?e:e.id});const{streamingManager:d}=await J(s,o,a,u,g);k=d},disconnect(){return u.track("agent-chat",{event:"disconnect",chatId:g.id,agentId:typeof e=="string"?e:e.id}),r.abort(),p.disconnect(),k.disconnect()},chat(d,S=!1){if(d.length===0)throw new Error("Messages cannot be empty");return u.track("agent-message-send",{event:"success",messages:d.length+1}),d[d.length-1].created_at=new Date().toISOString(),d[d.length-1].role||(d[d.length-1].role="user"),a.chat(s.id,g.id,{sessionId:k.sessionId,streamId:k.streamId,messages:d,append_chat:S},{signal:r.signal})},rate(d,S,A){var C,$,T,c;const l=((C=S.matches)==null?void 0:C.map(_=>[_.document_id,_.id]))??[];return u.track("agent-rate",{event:A?"update":"create",score:d,thumb:d===1?"up":"down",knowledge_id:(($=s.knowledge)==null?void 0:$.id)??"",matches:l}),A?h.update(A,{agent_id:s.id,knowledge_id:((T=s.knowledge)==null?void 0:T.id)??"",chat_id:g.id,score:d,matches:l}):h.create({agent_id:s.id,knowledge_id:((c=s.knowledge)==null?void 0:c.id)??"",chat_id:g.id,score:d,matches:l})},deleteRate(d){return u.track("agent-rate-delete",{type:"text",chat_id:g.id,id:d}),h.delete(d)},speak(d){function S(){if(d.type==="text"){u.track("agent-speak",{type:"text",provider:d.provider,input:d.input,ssml:d.ssml||!1});let A=s.presenter.voice;return d.provider&&(A=d.provider),{type:"text",provider:A,input:d.input,ssml:d.ssml||!1}}else if(d.type==="audio")return u.track("agent-speak",{type:"audio",audio_url:d.audio_url}),{type:"audio",audio_url:d.audio_url};throw new Error("Invalid payload")}return k.speak({script:S()})},async getStarterMessages(){var d;return(d=s.knowledge)!=null&&d.id?w.getKnowledge(s.knowledge.id).then(S=>(S==null?void 0:S.starter_message)||[]):[]},track(d,S){return o.enableAnalitics?u.track(d,S):Promise.reject(new Error("Analytics was disabled on create step"))}}}f.ChatMode=j,f.ChatProgress=W,f.DocumentType=H,f.KnowledgeType=F,f.PlanGroup=D,f.Providers=L,f.RateState=N,f.Subject=U,f.UserPlan=x,f.VoiceAccess=O,f.createAgentManager=pe,f.getAgent=he,Object.defineProperty(f,Symbol.toStringTag,{value:"Module"})});
|
|
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||{}),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||{}),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||{}),b=(e=>(e.Clip="clip",e.Talk="talk",e))(b||{});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 E=t.slice(n),c=n===0?void 0:t[n-1];v=ce(E,c),v=v.sort((g,s)=>s.packetsLost-g.packetsLost).slice(0,5)}a==null||a(i?T.Start:T.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===b.Clip?oe(n,i):de(n,i),{id:v,offer:E,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);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,T.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(E),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,T.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===b.Clip?{videoType:b.Clip,driver_id:e.presenter.driver_id,presenter_id:e.presenter.presenter_id}:{videoType:b.Talk,source_url:e.presenter.source_url}}function 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,E;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),(E=(v=a.callbacks).onNewMessage)==null||E.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,9 +1,8 @@
|
|
|
1
1
|
import { Auth } from './types/auth';
|
|
2
2
|
import { ChatProgressCallback } from '.';
|
|
3
|
-
interface SocketManager {
|
|
3
|
+
export interface SocketManager {
|
|
4
4
|
socket?: WebSocket;
|
|
5
5
|
disconnect: () => void;
|
|
6
6
|
subscribeToEvents: (data: any) => void;
|
|
7
7
|
}
|
|
8
|
-
export declare function
|
|
9
|
-
export {};
|
|
8
|
+
export declare function createSocketManager(auth: Auth, host: string, onMessage?: ChatProgressCallback): Promise<SocketManager>;
|
|
@@ -13,4 +13,4 @@ export declare function getAgent(agentId: string, auth: Auth, baseURL?: string):
|
|
|
13
13
|
* @example
|
|
14
14
|
* const agentManager = await createAgentManager('id-agent123', { auth: { type: 'key', clientKey: '123', externalId: '123' } });
|
|
15
15
|
*/
|
|
16
|
-
export declare function createAgentManager(agent: string
|
|
16
|
+
export declare function createAgentManager(agent: string, options: AgentManagerOptions): Promise<AgentManager>;
|
|
@@ -1,12 +1,11 @@
|
|
|
1
|
-
import { Agent } from
|
|
1
|
+
import { Agent } from '..';
|
|
2
2
|
export interface AnalyticsOptions {
|
|
3
3
|
mixPanelKey: string;
|
|
4
4
|
agent: Agent;
|
|
5
|
-
chatId?: string;
|
|
6
5
|
isEnabled?: boolean;
|
|
7
6
|
distinctId?: string;
|
|
8
7
|
}
|
|
9
|
-
export interface
|
|
8
|
+
export interface Analytics {
|
|
10
9
|
mixPanelKey: string;
|
|
11
10
|
distinct_id?: string;
|
|
12
11
|
isEnabled: boolean;
|
|
@@ -16,5 +15,4 @@ export interface AnalyticsProvider {
|
|
|
16
15
|
getRandom(): string;
|
|
17
16
|
track(event: string, props?: Record<string, any>): Promise<any>;
|
|
18
17
|
}
|
|
19
|
-
declare function
|
|
20
|
-
export default initializeAnalyticsProvider;
|
|
18
|
+
export declare function initializeAnalytics(config: AnalyticsOptions): Analytics;
|
|
@@ -15,9 +15,11 @@ export interface RatingEntity {
|
|
|
15
15
|
score: 1 | -1;
|
|
16
16
|
created_at: string;
|
|
17
17
|
modified_at: string;
|
|
18
|
+
message_id: string;
|
|
18
19
|
}
|
|
19
20
|
export type RatingPayload = Omit<RatingEntity, 'owner_id' | 'id' | 'created_at' | 'modified_at' | 'created_by' | 'external_id'>;
|
|
20
21
|
export interface Message {
|
|
22
|
+
id: string;
|
|
21
23
|
role?: 'system' | 'assistant' | 'user' | 'function' | 'tool';
|
|
22
24
|
content: string;
|
|
23
25
|
created_at?: string;
|
|
@@ -28,7 +28,7 @@ export declare enum ChatProgress {
|
|
|
28
28
|
*/
|
|
29
29
|
Complete = "done"
|
|
30
30
|
}
|
|
31
|
-
export type ChatProgressCallback = (progress: ChatProgress, data:
|
|
31
|
+
export type ChatProgressCallback = (progress: ChatProgress, data: any) => void;
|
|
32
32
|
export type ConnectionStateChangeCallback = (state: ConnectionState) => void;
|
|
33
33
|
export type VideoStateChangeCallback = (state: StreamingState, data: any) => void;
|
|
34
34
|
interface ManagerCallbacks {
|
|
@@ -52,15 +52,15 @@ interface ManagerCallbacks {
|
|
|
52
52
|
*/
|
|
53
53
|
onSrcObjectReady(srcObject: MediaStream): void;
|
|
54
54
|
/**
|
|
55
|
-
* Optional callback function that will be triggered each time
|
|
56
|
-
* @param
|
|
55
|
+
* Optional callback function that will be triggered each time new message is received
|
|
56
|
+
* @param messages - array of messages
|
|
57
57
|
*/
|
|
58
|
-
|
|
58
|
+
onNewMessage?(messages: Message[]): void;
|
|
59
59
|
/**
|
|
60
|
-
* Optional callback function that will be triggered
|
|
61
|
-
* @param
|
|
60
|
+
* Optional callback function that will be triggered each time new chat is created
|
|
61
|
+
* @param chatId - id of the new chat
|
|
62
62
|
*/
|
|
63
|
-
|
|
63
|
+
onNewChat?(chatId: string): void;
|
|
64
64
|
}
|
|
65
65
|
export interface AgentManagerOptions {
|
|
66
66
|
auth: Auth;
|
|
@@ -86,31 +86,26 @@ export interface AgentManager {
|
|
|
86
86
|
*/
|
|
87
87
|
starterMessages: string[];
|
|
88
88
|
/**
|
|
89
|
-
* Method to
|
|
90
|
-
* Since chat uses an RTC connection to communicate with the agent, it could be dropped and to continue to chat you need to reconnect
|
|
89
|
+
* Method to connect to stream and chat
|
|
91
90
|
*/
|
|
92
|
-
|
|
91
|
+
connect: () => Promise<void>;
|
|
93
92
|
/**
|
|
94
93
|
* Method to close all connections with agent, stream and web socket
|
|
95
94
|
*/
|
|
96
95
|
disconnect: () => Promise<void>;
|
|
97
|
-
/**
|
|
98
|
-
* ID of chat you are working on now
|
|
99
|
-
*/
|
|
100
|
-
chatId: string;
|
|
101
96
|
/**
|
|
102
97
|
* Method to send a chat message to existing chat with the agent
|
|
103
98
|
* @param messages
|
|
104
99
|
* @param append_chat: when true, append to existing agent chat, rather than creating a new one.
|
|
105
100
|
*/
|
|
106
|
-
chat: (
|
|
101
|
+
chat: (userMessage: string, append_chat?: boolean) => Promise<ChatResponse>;
|
|
107
102
|
/**
|
|
108
103
|
* Method to rate the answer in chat
|
|
109
104
|
* @param score: 1 | -1 - score of the answer. 1 for positive, -1 for negative
|
|
110
105
|
* @param matches - array of matches that were used to find the answer
|
|
111
106
|
* @param id - id of Rating entity. Leave it empty to create a new, one or pass it to work with the existing one
|
|
112
107
|
*/
|
|
113
|
-
rate: (score: 1 | -1,
|
|
108
|
+
rate: (messageId: string, score: 1 | -1, rateId?: string) => Promise<RatingEntity>;
|
|
114
109
|
/**
|
|
115
110
|
* Method to delete rating from answer in chat
|
|
116
111
|
* @param id - id of Rating entity.
|
|
@@ -121,18 +116,5 @@ export interface AgentManager {
|
|
|
121
116
|
* @param payload
|
|
122
117
|
*/
|
|
123
118
|
speak: (payload: SupportedStreamScipt) => Promise<SendStreamPayloadResponse>;
|
|
124
|
-
/**
|
|
125
|
-
* Optional callback function that will be triggered each time any changes happen in the chat
|
|
126
|
-
* @param callback
|
|
127
|
-
*/
|
|
128
|
-
getStarterMessages: () => Promise<string[]>;
|
|
129
|
-
/**
|
|
130
|
-
* TODO describe event and props from MixPanel Docs
|
|
131
|
-
* TODO add response
|
|
132
|
-
* @param event
|
|
133
|
-
* @param props
|
|
134
|
-
* @returns
|
|
135
|
-
*/
|
|
136
|
-
track: (event: string, props?: Record<string, any>) => Promise<any>;
|
|
137
119
|
}
|
|
138
120
|
export {};
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { Analytics } from '../../services/mixpanel';
|
|
2
2
|
import { Auth } from '../auth';
|
|
3
3
|
import { VideoType } from '../entities';
|
|
4
4
|
import { CreateClipStreamRequest, CreateTalkStreamRequest, SendClipStreamPayload, SendTalkStreamPayload } from './api';
|
|
@@ -13,8 +13,8 @@ export declare enum StreamEvents {
|
|
|
13
13
|
ChatPartial = "chat/partial",
|
|
14
14
|
StreamDone = "stream/done",
|
|
15
15
|
StreamStarted = "stream/started",
|
|
16
|
-
StreamReady = "stream/ready",
|
|
17
16
|
StreamFailed = "stream/error",
|
|
17
|
+
StreamReady = "stream/ready",
|
|
18
18
|
StreamCreated = "stream/created",
|
|
19
19
|
StreamVideoCreated = "stream-video/started",
|
|
20
20
|
StreamVideoDone = "stream-video/done",
|
|
@@ -55,7 +55,7 @@ export interface StreamingManagerOptions {
|
|
|
55
55
|
baseURL?: string;
|
|
56
56
|
debug?: boolean;
|
|
57
57
|
auth: Auth;
|
|
58
|
-
analytics?:
|
|
58
|
+
analytics?: Analytics;
|
|
59
59
|
}
|
|
60
60
|
export interface SlimRTCStatsReport {
|
|
61
61
|
index: number;
|
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.57",
|
|
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
|
+
}
|