@d-id/client-sdk 1.0.19-beta.117 → 1.0.19-beta.118
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
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
var G = /* @__PURE__ */ ((e) => (e.TRIAL = "trial", e.BASIC = "basic", e.ENTERPRISE = "enterprise", e.LITE = "lite", e.ADVANCED = "advanced", e))(G || {}), ee = /* @__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))(ee || {}), te = /* @__PURE__ */ ((e) => (e.Unrated = "Unrated", e.Positive = "Positive", e.Negative = "Negative", e))(te || {}),
|
|
1
|
+
var G = /* @__PURE__ */ ((e) => (e.TRIAL = "trial", e.BASIC = "basic", e.ENTERPRISE = "enterprise", e.LITE = "lite", e.ADVANCED = "advanced", e))(G || {}), ee = /* @__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))(ee || {}), te = /* @__PURE__ */ ((e) => (e.Unrated = "Unrated", e.Positive = "Positive", e.Negative = "Negative", e))(te || {}), _ = /* @__PURE__ */ ((e) => (e.Functional = "Functional", e.TextOnly = "TextOnly", e.Maintenance = "Maintenance", e.Playground = "Playground", e.DirectPlayback = "DirectPlayback", e))(_ || {}), b = /* @__PURE__ */ ((e) => (e.Embed = "embed", e.Query = "query", e.Partial = "partial", e.Answer = "answer", e.Complete = "done", e))(b || {}), ne = /* @__PURE__ */ ((e) => (e.KnowledgeProcessing = "knowledge/processing", e.KnowledgeIndexing = "knowledge/indexing", e.KnowledgeFailed = "knowledge/error", e.KnowledgeDone = "knowledge/done", e))(ne || {}), re = /* @__PURE__ */ ((e) => (e.Knowledge = "knowledge", e.Document = "document", e.Record = "record", e))(re || {}), ae = /* @__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))(ae || {}), L = /* @__PURE__ */ ((e) => (e.Clip = "clip", e.Talk = "talk", e))(L || {}), x = /* @__PURE__ */ ((e) => (e.Start = "START", e.Stop = "STOP", e))(x || {}), U = /* @__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))(U || {}), y = /* @__PURE__ */ ((e) => (e.New = "new", e.Fail = "fail", e.Connected = "connected", e.Connecting = "connecting", e.Closed = "closed", e.Completed = "completed", e.Disconnected = "disconnected", e))(y || {}), ie = /* @__PURE__ */ ((e) => (e.Amazon = "amazon", e.Microsoft = "microsoft", e.Afflorithmics = "afflorithmics", e.Elevenlabs = "elevenlabs", e))(ie || {}), se = /* @__PURE__ */ ((e) => (e.Public = "public", e.Premium = "premium", e.Private = "private", e))(se || {});
|
|
2
2
|
const z = "https://api.d-id.com", oe = "wss://notifications.d-id.com", ce = "79f81a83a67430be2bc0fd61042b8faa", F = () => Math.random().toString(16).slice(2);
|
|
3
3
|
function J() {
|
|
4
4
|
let e = window.localStorage.getItem("did_external_key_id");
|
|
@@ -112,14 +112,14 @@ function me(e) {
|
|
|
112
112
|
onClose: m = null,
|
|
113
113
|
onError: d = null
|
|
114
114
|
} = a || {}, l = new WebSocket(`${t}?authorization=${j(r)}`);
|
|
115
|
-
l.onmessage = i, l.onclose = m, l.onerror = (
|
|
116
|
-
console.error(
|
|
117
|
-
}, l.onopen = (
|
|
118
|
-
s == null || s(
|
|
115
|
+
l.onmessage = i, l.onclose = m, l.onerror = (v) => {
|
|
116
|
+
console.error(v), d == null || d("Websocket failed to connect", v), o(v);
|
|
117
|
+
}, l.onopen = (v) => {
|
|
118
|
+
s == null || s(v), n(l);
|
|
119
119
|
};
|
|
120
120
|
});
|
|
121
121
|
}
|
|
122
|
-
async function
|
|
122
|
+
async function ge(e) {
|
|
123
123
|
const {
|
|
124
124
|
retries: n = 1
|
|
125
125
|
} = e;
|
|
@@ -134,8 +134,8 @@ async function ue(e) {
|
|
|
134
134
|
}
|
|
135
135
|
return o;
|
|
136
136
|
}
|
|
137
|
-
async function
|
|
138
|
-
const a = o != null && o.onMessage ? [o.onMessage] : [], t = await
|
|
137
|
+
async function ue(e, n, o) {
|
|
138
|
+
const a = o != null && o.onMessage ? [o.onMessage] : [], t = await ge({
|
|
139
139
|
auth: e,
|
|
140
140
|
host: n,
|
|
141
141
|
callbacks: {
|
|
@@ -158,39 +158,32 @@ function he(e, n, o, a) {
|
|
|
158
158
|
return {
|
|
159
159
|
createStream(r) {
|
|
160
160
|
return t.post("/streams", {
|
|
161
|
-
driver_id: r.driver_id,
|
|
162
|
-
presenter_id: r.presenter_id,
|
|
163
161
|
compatibility_mode: r.compatibility_mode,
|
|
164
162
|
stream_warmup: r.stream_warmup,
|
|
165
|
-
session_timeout: r.session_timeout
|
|
166
|
-
type: C.Clip
|
|
163
|
+
session_timeout: r.session_timeout
|
|
167
164
|
});
|
|
168
165
|
},
|
|
169
166
|
startConnection(r, i, s) {
|
|
170
167
|
return t.post(`/streams/${r}/sdp`, {
|
|
171
168
|
session_id: s,
|
|
172
|
-
answer: i
|
|
173
|
-
type: C.Clip
|
|
169
|
+
answer: i
|
|
174
170
|
});
|
|
175
171
|
},
|
|
176
172
|
addIceCandidate(r, i, s) {
|
|
177
173
|
return t.post(`/streams/${r}/ice`, {
|
|
178
174
|
session_id: s,
|
|
179
|
-
...i
|
|
180
|
-
type: C.Clip
|
|
175
|
+
...i
|
|
181
176
|
});
|
|
182
177
|
},
|
|
183
178
|
sendStreamRequest(r, i, s) {
|
|
184
179
|
return t.post(`/streams/${r}`, {
|
|
185
180
|
session_id: i,
|
|
186
|
-
...s
|
|
187
|
-
type: C.Clip
|
|
181
|
+
...s
|
|
188
182
|
});
|
|
189
183
|
},
|
|
190
184
|
close(r, i) {
|
|
191
185
|
return t.delete(`/streams/${r}`, {
|
|
192
|
-
session_id: i
|
|
193
|
-
type: C.Clip
|
|
186
|
+
session_id: i
|
|
194
187
|
});
|
|
195
188
|
}
|
|
196
189
|
};
|
|
@@ -200,69 +193,63 @@ function fe(e, n, o, a) {
|
|
|
200
193
|
return {
|
|
201
194
|
createStream(r, i) {
|
|
202
195
|
return t.post("/streams", {
|
|
203
|
-
source_url: r.source_url,
|
|
204
196
|
driver_url: r.driver_url,
|
|
205
197
|
face: r.face,
|
|
206
198
|
config: r.config,
|
|
207
199
|
compatibility_mode: r.compatibility_mode,
|
|
208
200
|
stream_warmup: r.stream_warmup,
|
|
209
201
|
output_resolution: r.output_resolution,
|
|
210
|
-
session_timeout: r.session_timeout
|
|
211
|
-
type: C.Talk
|
|
202
|
+
session_timeout: r.session_timeout
|
|
212
203
|
}, i);
|
|
213
204
|
},
|
|
214
205
|
startConnection(r, i, s, m) {
|
|
215
206
|
return t.post(`/streams/${r}/sdp`, {
|
|
216
207
|
session_id: s,
|
|
217
|
-
answer: i
|
|
218
|
-
type: C.Talk
|
|
208
|
+
answer: i
|
|
219
209
|
}, m);
|
|
220
210
|
},
|
|
221
211
|
addIceCandidate(r, i, s, m) {
|
|
222
212
|
return t.post(`/streams/${r}/ice`, {
|
|
223
213
|
session_id: s,
|
|
224
|
-
...i
|
|
225
|
-
type: C.Talk
|
|
214
|
+
...i
|
|
226
215
|
}, m);
|
|
227
216
|
},
|
|
228
217
|
sendStreamRequest(r, i, s, m) {
|
|
229
218
|
return t.post(`/streams/${r}`, {
|
|
230
219
|
session_id: i,
|
|
231
|
-
...s
|
|
232
|
-
type: C.Talk
|
|
220
|
+
...s
|
|
233
221
|
}, m);
|
|
234
222
|
},
|
|
235
223
|
close(r, i, s) {
|
|
236
224
|
return t.delete(`/streams/${r}`, {
|
|
237
|
-
session_id: i
|
|
238
|
-
type: C.Talk
|
|
225
|
+
session_id: i
|
|
239
226
|
}, s);
|
|
240
227
|
}
|
|
241
228
|
};
|
|
242
229
|
}
|
|
243
230
|
let Y = !1;
|
|
244
|
-
const
|
|
231
|
+
const A = (e, n) => Y && console.log(e, n), pe = (window.RTCPeerConnection || window.webkitRTCPeerConnection || window.mozRTCPeerConnection).bind(window);
|
|
245
232
|
function H(e) {
|
|
246
233
|
switch (e) {
|
|
247
234
|
case "connected":
|
|
248
|
-
return
|
|
235
|
+
return y.Connected;
|
|
249
236
|
case "checking":
|
|
250
|
-
return
|
|
237
|
+
return y.Connecting;
|
|
251
238
|
case "failed":
|
|
252
|
-
return
|
|
239
|
+
return y.Fail;
|
|
253
240
|
case "new":
|
|
254
|
-
return
|
|
241
|
+
return y.New;
|
|
255
242
|
case "closed":
|
|
256
|
-
return
|
|
243
|
+
return y.Closed;
|
|
257
244
|
case "disconnected":
|
|
258
|
-
return
|
|
245
|
+
return y.Disconnected;
|
|
259
246
|
case "completed":
|
|
260
|
-
return
|
|
247
|
+
return y.Completed;
|
|
261
248
|
default:
|
|
262
|
-
return
|
|
249
|
+
return y.New;
|
|
263
250
|
}
|
|
264
251
|
}
|
|
265
|
-
function
|
|
252
|
+
function ve() {
|
|
266
253
|
let e = 0;
|
|
267
254
|
return (n) => {
|
|
268
255
|
for (const o of n.values())
|
|
@@ -273,10 +260,10 @@ function ye() {
|
|
|
273
260
|
return !1;
|
|
274
261
|
};
|
|
275
262
|
}
|
|
276
|
-
function
|
|
263
|
+
function ye(e, n) {
|
|
277
264
|
const a = Math.max(Math.ceil(10), 1);
|
|
278
265
|
let t = 0, r = !1;
|
|
279
|
-
const i =
|
|
266
|
+
const i = ve();
|
|
280
267
|
return setInterval(async () => {
|
|
281
268
|
const s = await e.getStats();
|
|
282
269
|
i(s) ? (t = 0, r || (n == null || n(x.Start), r = !0)) : r && (t++, t >= a && (n == null || n(x.Stop), r = !1));
|
|
@@ -295,81 +282,81 @@ async function Ce(e, n, {
|
|
|
295
282
|
startConnection: m,
|
|
296
283
|
sendStreamRequest: d,
|
|
297
284
|
close: l,
|
|
298
|
-
createStream:
|
|
299
|
-
addIceCandidate:
|
|
300
|
-
} = n.videoType ===
|
|
285
|
+
createStream: v,
|
|
286
|
+
addIceCandidate: E
|
|
287
|
+
} = n.videoType === L.Clip ? he(t, r, e, a.onError) : fe(t, r, e, a.onError), {
|
|
301
288
|
id: M,
|
|
302
289
|
offer: O,
|
|
303
290
|
ice_servers: K,
|
|
304
|
-
session_id:
|
|
305
|
-
} = await
|
|
291
|
+
session_id: I
|
|
292
|
+
} = await v(n), p = new pe({
|
|
306
293
|
iceServers: K
|
|
307
294
|
}), c = p.createDataChannel("JanusDataChannel");
|
|
308
|
-
if (!
|
|
295
|
+
if (!I)
|
|
309
296
|
throw new Error("Could not create session_id");
|
|
310
|
-
const w =
|
|
311
|
-
p.onicecandidate = (
|
|
312
|
-
|
|
313
|
-
candidate:
|
|
314
|
-
sdpMid:
|
|
315
|
-
sdpMLineIndex:
|
|
316
|
-
},
|
|
297
|
+
const w = ye(p, a.onVideoStateChange);
|
|
298
|
+
p.onicecandidate = (g) => {
|
|
299
|
+
A("peerConnection.onicecandidate", g), g.candidate && g.candidate.sdpMid && g.candidate.sdpMLineIndex !== null ? E(M, {
|
|
300
|
+
candidate: g.candidate.candidate,
|
|
301
|
+
sdpMid: g.candidate.sdpMid,
|
|
302
|
+
sdpMLineIndex: g.candidate.sdpMLineIndex
|
|
303
|
+
}, I) : E(M, {
|
|
317
304
|
candidate: null
|
|
318
|
-
},
|
|
305
|
+
}, I);
|
|
319
306
|
}, p.oniceconnectionstatechange = () => {
|
|
320
|
-
var
|
|
321
|
-
|
|
322
|
-
const
|
|
323
|
-
|
|
307
|
+
var u;
|
|
308
|
+
A("peerConnection.oniceconnectionstatechange => " + p.iceConnectionState);
|
|
309
|
+
const g = H(p.iceConnectionState);
|
|
310
|
+
g === y.Connected ? s = setTimeout(() => {
|
|
324
311
|
var f;
|
|
325
|
-
return (f = a.onConnectionStateChange) == null ? void 0 : f.call(a,
|
|
326
|
-
}, i ? 5e3 : 0) : (clearTimeout(s), (
|
|
327
|
-
}, p.ontrack = (
|
|
328
|
-
var
|
|
329
|
-
|
|
330
|
-
}, c.onmessage = (
|
|
331
|
-
var
|
|
312
|
+
return (f = a.onConnectionStateChange) == null ? void 0 : f.call(a, y.Connected);
|
|
313
|
+
}, i ? 5e3 : 0) : (clearTimeout(s), (u = a.onConnectionStateChange) == null || u.call(a, g));
|
|
314
|
+
}, p.ontrack = (g) => {
|
|
315
|
+
var u;
|
|
316
|
+
A("peerConnection.ontrack", g), (u = a.onSrcObjectReady) == null || u.call(a, g.streams[0]);
|
|
317
|
+
}, c.onmessage = (g) => {
|
|
318
|
+
var u;
|
|
332
319
|
if (c.readyState === "open") {
|
|
333
|
-
const [f,
|
|
334
|
-
f === U.StreamReady && (clearTimeout(s), (
|
|
320
|
+
const [f, C] = g.data.split(":");
|
|
321
|
+
f === U.StreamReady && (clearTimeout(s), (u = a.onConnectionStateChange) == null || u.call(a, y.Connected));
|
|
335
322
|
}
|
|
336
|
-
}, await p.setRemoteDescription(O),
|
|
323
|
+
}, await p.setRemoteDescription(O), A("set remote description OK");
|
|
337
324
|
const h = await p.createAnswer();
|
|
338
|
-
return
|
|
325
|
+
return A("create answer OK"), await p.setLocalDescription(h), A("set local description OK"), await m(M, h, I), A("start connection OK"), {
|
|
339
326
|
/**
|
|
340
327
|
* Method to send request to server to get clip or talk depend on you payload
|
|
341
328
|
* @param payload
|
|
342
329
|
*/
|
|
343
|
-
speak(
|
|
344
|
-
return d(M,
|
|
330
|
+
speak(g) {
|
|
331
|
+
return d(M, I, g);
|
|
345
332
|
},
|
|
346
333
|
/**
|
|
347
334
|
* Method to close RTC connection
|
|
348
335
|
*/
|
|
349
336
|
async disconnect() {
|
|
350
|
-
var
|
|
337
|
+
var g, u;
|
|
351
338
|
if (M) {
|
|
352
339
|
const f = H(p.iceConnectionState);
|
|
353
340
|
if (p) {
|
|
354
|
-
if (f ===
|
|
355
|
-
(
|
|
341
|
+
if (f === y.New) {
|
|
342
|
+
(g = a.onVideoStateChange) == null || g.call(a, x.Stop), clearInterval(w);
|
|
356
343
|
return;
|
|
357
344
|
}
|
|
358
345
|
p.close(), p.oniceconnectionstatechange = null, p.onnegotiationneeded = null, p.onicecandidate = null, p.ontrack = null;
|
|
359
346
|
}
|
|
360
347
|
try {
|
|
361
|
-
f ===
|
|
348
|
+
f === y.Connected && await l(M, I).catch((C) => {
|
|
362
349
|
});
|
|
363
|
-
} catch (
|
|
364
|
-
|
|
350
|
+
} catch (C) {
|
|
351
|
+
A("Error on close stream connection", C);
|
|
365
352
|
}
|
|
366
|
-
(
|
|
353
|
+
(u = a.onVideoStateChange) == null || u.call(a, x.Stop), clearInterval(w);
|
|
367
354
|
}
|
|
368
355
|
},
|
|
369
356
|
/**
|
|
370
357
|
* Session identifier information, should be returned in the body of all streaming requests
|
|
371
358
|
*/
|
|
372
|
-
sessionId:
|
|
359
|
+
sessionId: I,
|
|
373
360
|
/**
|
|
374
361
|
* Id of current RTC stream
|
|
375
362
|
*/
|
|
@@ -377,7 +364,7 @@ async function Ce(e, n, {
|
|
|
377
364
|
};
|
|
378
365
|
}
|
|
379
366
|
let W = {};
|
|
380
|
-
function
|
|
367
|
+
function Se(e) {
|
|
381
368
|
const n = window != null && window.hasOwnProperty("DID_AGENTS_API") ? "agents-ui" : "agents-sdk", o = {
|
|
382
369
|
token: e.token || "testKey",
|
|
383
370
|
distinct_id: e.distinctId || J(),
|
|
@@ -428,9 +415,9 @@ function _e(e) {
|
|
|
428
415
|
if (s.events[r] = {
|
|
429
416
|
props: t
|
|
430
417
|
}, s.resolvedDependencies.push(r), i.every((d) => s.resolvedDependencies.includes(d))) {
|
|
431
|
-
const d = i.reduce((l,
|
|
418
|
+
const d = i.reduce((l, v) => s.events[v] ? {
|
|
432
419
|
...l,
|
|
433
|
-
...s.events[
|
|
420
|
+
...s.events[v].props
|
|
434
421
|
} : l, {});
|
|
435
422
|
this.track(a, d), s.resolvedDependencies = s.resolvedDependencies.filter((l) => !i.includes(l)), i.forEach((l) => {
|
|
436
423
|
delete s.events[l];
|
|
@@ -439,7 +426,7 @@ function _e(e) {
|
|
|
439
426
|
}
|
|
440
427
|
};
|
|
441
428
|
}
|
|
442
|
-
function
|
|
429
|
+
function Me(e) {
|
|
443
430
|
var a, t, r, i, s;
|
|
444
431
|
const n = () => /Mobi|Android/i.test(navigator.userAgent) ? "Mobile" : "Desktop";
|
|
445
432
|
return {
|
|
@@ -457,7 +444,7 @@ function Se(e) {
|
|
|
457
444
|
}
|
|
458
445
|
};
|
|
459
446
|
}
|
|
460
|
-
function
|
|
447
|
+
function _e(e, n, o) {
|
|
461
448
|
var d, l;
|
|
462
449
|
const {
|
|
463
450
|
event: a,
|
|
@@ -486,24 +473,16 @@ function Me(e, n, o) {
|
|
|
486
473
|
...o
|
|
487
474
|
};
|
|
488
475
|
}
|
|
489
|
-
let
|
|
476
|
+
let P = 0;
|
|
490
477
|
function ke(e, n) {
|
|
491
478
|
var o, a, t, r, i, s, m;
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
stream_warmup: (a = n == null ? void 0 : n.streamOptions) == null ? void 0 : a.streamWarmup,
|
|
500
|
-
compatibility_mode: (t = n == null ? void 0 : n.streamOptions) == null ? void 0 : t.compatibilityMode
|
|
501
|
-
};
|
|
502
|
-
} else
|
|
503
|
-
throw new Error("Presenter is not initialized");
|
|
504
|
-
return {
|
|
505
|
-
videoType: C.Talk,
|
|
506
|
-
source_url: e.presenter.source_url,
|
|
479
|
+
return e.presenter.type === L.Clip ? {
|
|
480
|
+
videoType: L.Clip,
|
|
481
|
+
session_timeout: (o = n == null ? void 0 : n.streamOptions) == null ? void 0 : o.sessionTimeout,
|
|
482
|
+
stream_warmup: (a = n == null ? void 0 : n.streamOptions) == null ? void 0 : a.streamWarmup,
|
|
483
|
+
compatibility_mode: (t = n == null ? void 0 : n.streamOptions) == null ? void 0 : t.compatibilityMode
|
|
484
|
+
} : {
|
|
485
|
+
videoType: L.Talk,
|
|
507
486
|
session_timeout: (r = n == null ? void 0 : n.streamOptions) == null ? void 0 : r.sessionTimeout,
|
|
508
487
|
stream_warmup: (i = n == null ? void 0 : n.streamOptions) == null ? void 0 : i.streamWarmup,
|
|
509
488
|
compatibility_mode: (s = n == null ? void 0 : n.streamOptions) == null ? void 0 : s.compatibilityMode,
|
|
@@ -511,7 +490,7 @@ function ke(e, n) {
|
|
|
511
490
|
};
|
|
512
491
|
}
|
|
513
492
|
function Z(e) {
|
|
514
|
-
return e ===
|
|
493
|
+
return e === _.Playground ? {
|
|
515
494
|
headers: {
|
|
516
495
|
[we]: "true"
|
|
517
496
|
}
|
|
@@ -541,7 +520,7 @@ async function Q(e, n, o, a) {
|
|
|
541
520
|
function Ie(e, n, o, a, t) {
|
|
542
521
|
return new Promise(async (r, i) => {
|
|
543
522
|
var m;
|
|
544
|
-
|
|
523
|
+
P = 0;
|
|
545
524
|
const s = await Ce(e.id, ke(e, n), {
|
|
546
525
|
...n,
|
|
547
526
|
analytics: a,
|
|
@@ -549,19 +528,19 @@ function Ie(e, n, o, a, t) {
|
|
|
549
528
|
callbacks: {
|
|
550
529
|
...n.callbacks,
|
|
551
530
|
onConnectionStateChange: async (d) => {
|
|
552
|
-
var l,
|
|
553
|
-
d ===
|
|
554
|
-
i(
|
|
531
|
+
var l, v;
|
|
532
|
+
d === y.Connected && (!t && n.mode !== _.DirectPlayback && (t = await Q(e.id, o, a, n.mode).catch((E) => {
|
|
533
|
+
i(E);
|
|
555
534
|
})), s ? r({
|
|
556
535
|
chat: t,
|
|
557
536
|
streamingManager: s
|
|
558
|
-
}) : t && i(new Error("Something went wrong while initializing the manager"))), (
|
|
537
|
+
}) : t && i(new Error("Something went wrong while initializing the manager"))), (v = (l = n.callbacks).onConnectionStateChange) == null || v.call(l, d);
|
|
559
538
|
},
|
|
560
539
|
onVideoStateChange(d) {
|
|
561
|
-
var l,
|
|
562
|
-
(
|
|
540
|
+
var l, v;
|
|
541
|
+
(v = (l = n.callbacks).onVideoStateChange) == null || v.call(l, d), P > 0 && d === x.Start && a.linkTrack("agent-video", {
|
|
563
542
|
event: "start",
|
|
564
|
-
latency: Date.now() -
|
|
543
|
+
latency: Date.now() - P
|
|
565
544
|
}, "start", [U.StreamVideoCreated]);
|
|
566
545
|
}
|
|
567
546
|
}
|
|
@@ -582,7 +561,7 @@ function q(e) {
|
|
|
582
561
|
created_at: (/* @__PURE__ */ new Date()).toISOString()
|
|
583
562
|
}];
|
|
584
563
|
}
|
|
585
|
-
function
|
|
564
|
+
function Re(e) {
|
|
586
565
|
if (e.answer !== void 0)
|
|
587
566
|
return e.answer;
|
|
588
567
|
let n = 0, o = "";
|
|
@@ -590,8 +569,8 @@ function Ee(e) {
|
|
|
590
569
|
o += e[n], n++;
|
|
591
570
|
return o;
|
|
592
571
|
}
|
|
593
|
-
function
|
|
594
|
-
if (!(e ===
|
|
572
|
+
function Ee(e, n, o, a, t) {
|
|
573
|
+
if (!(e === b.Partial || e === b.Answer))
|
|
595
574
|
return;
|
|
596
575
|
const r = a.messages[a.messages.length - 1];
|
|
597
576
|
if ((r == null ? void 0 : r.role) !== "assistant")
|
|
@@ -600,74 +579,74 @@ function Re(e, n, o, a, t) {
|
|
|
600
579
|
content: i,
|
|
601
580
|
sequence: s
|
|
602
581
|
} = n;
|
|
603
|
-
e ===
|
|
604
|
-
const m =
|
|
605
|
-
(r.content !== m || e ===
|
|
582
|
+
e === b.Partial ? o[s] = i : o.answer = i;
|
|
583
|
+
const m = Re(o);
|
|
584
|
+
(r.content !== m || e === b.Answer) && (r.content = m, t == null || t([...a.messages], e));
|
|
606
585
|
}
|
|
607
586
|
async function Ae(e, n) {
|
|
608
|
-
var K,
|
|
587
|
+
var K, I, p;
|
|
609
588
|
let o = {}, a = !0;
|
|
610
589
|
const t = {
|
|
611
590
|
messages: [],
|
|
612
|
-
chatMode: n.mode ||
|
|
591
|
+
chatMode: n.mode || _.Functional
|
|
613
592
|
}, r = n.baseURL || z, i = n.wsURL || oe, s = n.mixpanelKey || ce, m = X(n.auth, r, n.callbacks.onError), d = await m.getById(e);
|
|
614
|
-
t.messages = q(d), (
|
|
615
|
-
const l =
|
|
593
|
+
t.messages = q(d), (I = (K = n.callbacks).onNewMessage) == null || I.call(K, [...t.messages], "answer");
|
|
594
|
+
const l = Se({
|
|
616
595
|
token: s,
|
|
617
596
|
agent: d,
|
|
618
597
|
...n
|
|
619
598
|
});
|
|
620
599
|
l.track("agent-sdk", {
|
|
621
600
|
event: "loaded",
|
|
622
|
-
...
|
|
601
|
+
...Me(d)
|
|
623
602
|
});
|
|
624
|
-
const
|
|
603
|
+
const v = {
|
|
625
604
|
onMessage: (c, w) => {
|
|
626
|
-
var h,
|
|
605
|
+
var h, g;
|
|
627
606
|
if ("content" in w)
|
|
628
|
-
|
|
607
|
+
Ee(c, w, o, t, n.callbacks.onNewMessage), c === b.Answer && l.track("agent-message-received", {
|
|
629
608
|
messages: t.messages.length,
|
|
630
609
|
mode: t.chatMode
|
|
631
610
|
});
|
|
632
611
|
else {
|
|
633
|
-
const
|
|
612
|
+
const u = U, f = [u.StreamVideoDone, u.StreamVideoError, u.StreamVideoRejected], C = [u.StreamFailed, u.StreamVideoError, u.StreamVideoRejected], k = _e(w, d, {
|
|
634
613
|
mode: t.chatMode
|
|
635
614
|
});
|
|
636
|
-
if (c = c, c ===
|
|
637
|
-
l.linkTrack("agent-video",
|
|
615
|
+
if (c = c, c === u.StreamVideoCreated)
|
|
616
|
+
l.linkTrack("agent-video", k, u.StreamVideoCreated, ["start"]);
|
|
638
617
|
else if (f.includes(c)) {
|
|
639
618
|
const S = c.split("/")[1];
|
|
640
619
|
l.track("agent-video", {
|
|
641
|
-
...
|
|
620
|
+
...k,
|
|
642
621
|
event: S
|
|
643
622
|
});
|
|
644
623
|
}
|
|
645
|
-
|
|
624
|
+
C.includes(c) && ((g = (h = n.callbacks).onError) == null || g.call(h, new Error(`Stream failed with event ${c}`), {
|
|
646
625
|
data: w
|
|
647
|
-
})), w.event ===
|
|
626
|
+
})), w.event === u.StreamDone && M();
|
|
648
627
|
}
|
|
649
628
|
}
|
|
650
629
|
};
|
|
651
|
-
async function
|
|
652
|
-
var
|
|
653
|
-
|
|
654
|
-
const w = n.mode ===
|
|
655
|
-
throw O(
|
|
656
|
-
}), [
|
|
657
|
-
streamingManager:
|
|
630
|
+
async function E(c) {
|
|
631
|
+
var C, k, S, R, T;
|
|
632
|
+
P = 0, c && !a && (delete t.chat, t.messages = q(d), (k = (C = n.callbacks).onNewMessage) == null || k.call(C, [...t.messages], "answer"));
|
|
633
|
+
const w = n.mode === _.DirectPlayback ? Promise.resolve(void 0) : ue(n.auth, i, v), h = Ie(d, n, m, l, t.chat).catch(($) => {
|
|
634
|
+
throw O(_.Maintenance), $;
|
|
635
|
+
}), [g, {
|
|
636
|
+
streamingManager: u,
|
|
658
637
|
chat: f
|
|
659
638
|
}] = await Promise.all([w, h]);
|
|
660
|
-
f && f.id !== ((S = t.chat) == null ? void 0 : S.id) && ((
|
|
639
|
+
f && f.id !== ((S = t.chat) == null ? void 0 : S.id) && ((T = (R = n.callbacks).onNewChat) == null || T.call(R, f.id)), t.streamingManager = u, t.socketManager = g, t.chat = f, a = !1, O((f == null ? void 0 : f.chat_mode) ?? n.mode ?? _.Functional);
|
|
661
640
|
}
|
|
662
641
|
async function M() {
|
|
663
|
-
var c, w, h,
|
|
664
|
-
(c = t.socketManager) == null || c.disconnect(), await ((w = t.streamingManager) == null ? void 0 : w.disconnect()), delete t.streamingManager, delete t.socketManager, (
|
|
642
|
+
var c, w, h, g;
|
|
643
|
+
(c = t.socketManager) == null || c.disconnect(), await ((w = t.streamingManager) == null ? void 0 : w.disconnect()), delete t.streamingManager, delete t.socketManager, (g = (h = n.callbacks).onConnectionStateChange) == null || g.call(h, y.Disconnected);
|
|
665
644
|
}
|
|
666
645
|
async function O(c) {
|
|
667
646
|
var w, h;
|
|
668
647
|
c !== t.chatMode && (l.track("agent-mode-change", {
|
|
669
648
|
mode: c
|
|
670
|
-
}), t.chatMode = c, t.chatMode !==
|
|
649
|
+
}), t.chatMode = c, t.chatMode !== _.Functional && await M(), (h = (w = n.callbacks).onModeChange) == null || h.call(w, c));
|
|
671
650
|
}
|
|
672
651
|
return {
|
|
673
652
|
agent: d,
|
|
@@ -675,7 +654,7 @@ async function Ae(e, n) {
|
|
|
675
654
|
changeMode: O,
|
|
676
655
|
async connect() {
|
|
677
656
|
var c;
|
|
678
|
-
await
|
|
657
|
+
await E(!0), l.track("agent-chat", {
|
|
679
658
|
event: "connect",
|
|
680
659
|
chatId: (c = t.chat) == null ? void 0 : c.id,
|
|
681
660
|
agentId: d.id,
|
|
@@ -684,7 +663,7 @@ async function Ae(e, n) {
|
|
|
684
663
|
},
|
|
685
664
|
async reconnect() {
|
|
686
665
|
var c;
|
|
687
|
-
await M(), await
|
|
666
|
+
await M(), await E(!1), l.track("agent-chat", {
|
|
688
667
|
event: "reconnect",
|
|
689
668
|
chatId: (c = t.chat) == null ? void 0 : c.id,
|
|
690
669
|
agentId: d.id,
|
|
@@ -701,19 +680,19 @@ async function Ae(e, n) {
|
|
|
701
680
|
});
|
|
702
681
|
},
|
|
703
682
|
async chat(c) {
|
|
704
|
-
var h,
|
|
683
|
+
var h, g, u, f, C, k;
|
|
705
684
|
const w = F();
|
|
706
685
|
o = {};
|
|
707
686
|
try {
|
|
708
|
-
if (
|
|
687
|
+
if (P = Date.now(), n.mode === _.DirectPlayback)
|
|
709
688
|
throw new Error("Direct playback is enabled, chat is disabled");
|
|
710
689
|
if (c.length >= 800)
|
|
711
690
|
throw new Error("Message cannot be more than 800 characters");
|
|
712
691
|
if (c.length === 0)
|
|
713
692
|
throw new Error("Message cannot be empty");
|
|
714
|
-
if (t.chatMode ===
|
|
693
|
+
if (t.chatMode === _.Maintenance)
|
|
715
694
|
throw new Error("Chat is in maintenance mode");
|
|
716
|
-
if (![
|
|
695
|
+
if (![_.TextOnly, _.Playground].includes(t.chatMode))
|
|
717
696
|
if (t.streamingManager) {
|
|
718
697
|
if (!t.chat)
|
|
719
698
|
throw new Error("Chat is not initialized");
|
|
@@ -723,8 +702,8 @@ async function Ae(e, n) {
|
|
|
723
702
|
id: F(),
|
|
724
703
|
role: "user",
|
|
725
704
|
content: c,
|
|
726
|
-
created_at: new Date(
|
|
727
|
-
}), (
|
|
705
|
+
created_at: new Date(P).toISOString()
|
|
706
|
+
}), (g = (h = n.callbacks).onNewMessage) == null || g.call(h, [...t.messages], "user"), t.chat || (t.chat = await Q(d.id, m, l, t.chatMode), (f = (u = n.callbacks).onNewChat) == null || f.call(u, t.chat.id));
|
|
728
707
|
const S = {
|
|
729
708
|
id: w,
|
|
730
709
|
role: "assistant",
|
|
@@ -733,29 +712,29 @@ async function Ae(e, n) {
|
|
|
733
712
|
matches: []
|
|
734
713
|
}, R = [...t.messages];
|
|
735
714
|
t.messages.push(S);
|
|
736
|
-
const
|
|
737
|
-
var
|
|
738
|
-
return m.chat(d.id,
|
|
739
|
-
sessionId: (
|
|
715
|
+
const T = (D) => {
|
|
716
|
+
var N, B;
|
|
717
|
+
return m.chat(d.id, D, {
|
|
718
|
+
sessionId: (N = t.streamingManager) == null ? void 0 : N.sessionId,
|
|
740
719
|
streamId: (B = t.streamingManager) == null ? void 0 : B.streamId,
|
|
741
720
|
chatMode: t.chatMode,
|
|
742
721
|
messages: R
|
|
743
722
|
}, Z(t.chatMode));
|
|
744
|
-
},
|
|
745
|
-
var
|
|
746
|
-
if (!((
|
|
747
|
-
throw
|
|
748
|
-
return await M(), await
|
|
723
|
+
}, $ = await T(t.chat.id).catch(async (D) => {
|
|
724
|
+
var N;
|
|
725
|
+
if (!((N = D == null ? void 0 : D.message) != null && N.includes("missing or invalid session_id")))
|
|
726
|
+
throw D;
|
|
727
|
+
return await M(), await E(!1), T(t.chat.id);
|
|
749
728
|
});
|
|
750
729
|
return l.track("agent-message-send", {
|
|
751
730
|
event: "success",
|
|
752
731
|
mode: t.chatMode,
|
|
753
732
|
messages: t.messages.length + 1
|
|
754
|
-
}), S.context =
|
|
755
|
-
latency: Date.now() -
|
|
733
|
+
}), S.context = $.context, S.matches = $.matches, $.result && (S.content = $.result, l.track("agent-message-received", {
|
|
734
|
+
latency: Date.now() - P,
|
|
756
735
|
mode: t.chatMode,
|
|
757
736
|
messages: t.messages.length
|
|
758
|
-
}), (
|
|
737
|
+
}), (k = (C = n.callbacks).onNewMessage) == null || k.call(C, [...t.messages], "answer")), $;
|
|
759
738
|
} catch (S) {
|
|
760
739
|
throw t.messages[t.messages.length - 1].id === w && t.messages.pop(), l.track("agent-message-send", {
|
|
761
740
|
event: "error",
|
|
@@ -765,30 +744,30 @@ async function Ae(e, n) {
|
|
|
765
744
|
}
|
|
766
745
|
},
|
|
767
746
|
rate(c, w, h) {
|
|
768
|
-
var f,
|
|
769
|
-
const
|
|
747
|
+
var f, C, k, S;
|
|
748
|
+
const g = t.messages.find((R) => R.id === c);
|
|
770
749
|
if (t.chat) {
|
|
771
|
-
if (!
|
|
750
|
+
if (!g)
|
|
772
751
|
throw new Error("Message not found");
|
|
773
752
|
} else
|
|
774
753
|
throw new Error("Chat is not initialized");
|
|
775
|
-
const
|
|
754
|
+
const u = ((f = g.matches) == null ? void 0 : f.map((R) => [R.document_id, R.id])) ?? [];
|
|
776
755
|
return l.track("agent-rate", {
|
|
777
756
|
event: h ? "update" : "create",
|
|
778
757
|
thumb: w === 1 ? "up" : "down",
|
|
779
|
-
knowledge_id: ((
|
|
758
|
+
knowledge_id: ((C = d.knowledge) == null ? void 0 : C.id) ?? "",
|
|
780
759
|
mode: t.chatMode,
|
|
781
|
-
matches:
|
|
760
|
+
matches: u,
|
|
782
761
|
score: w
|
|
783
762
|
}), h ? m.updateRating(d.id, t.chat.id, h, {
|
|
784
|
-
knowledge_id: ((
|
|
763
|
+
knowledge_id: ((k = d.knowledge) == null ? void 0 : k.id) ?? "",
|
|
785
764
|
message_id: c,
|
|
786
|
-
matches:
|
|
765
|
+
matches: u,
|
|
787
766
|
score: w
|
|
788
767
|
}) : m.createRating(d.id, t.chat.id, {
|
|
789
768
|
knowledge_id: ((S = d.knowledge) == null ? void 0 : S.id) ?? "",
|
|
790
769
|
message_id: c,
|
|
791
|
-
matches:
|
|
770
|
+
matches: u,
|
|
792
771
|
score: w
|
|
793
772
|
});
|
|
794
773
|
},
|
|
@@ -843,9 +822,9 @@ function $e(e, n, o) {
|
|
|
843
822
|
return a(e);
|
|
844
823
|
}
|
|
845
824
|
export {
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
825
|
+
_ as ChatMode,
|
|
826
|
+
b as ChatProgress,
|
|
827
|
+
y as ConnectionState,
|
|
849
828
|
ae as DocumentType,
|
|
850
829
|
re as KnowledgeType,
|
|
851
830
|
ee as PlanGroup,
|
|
@@ -855,7 +834,7 @@ export {
|
|
|
855
834
|
x as StreamingState,
|
|
856
835
|
ne as Subject,
|
|
857
836
|
G as UserPlan,
|
|
858
|
-
|
|
837
|
+
L as VideoType,
|
|
859
838
|
se as VoiceAccess,
|
|
860
839
|
Ae as createAgentManager,
|
|
861
840
|
$e as getAgent
|
package/dist/index.umd.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
(function(p,N){typeof exports=="object"&&typeof module<"u"?N(exports):typeof define=="function"&&define.amd?define(["exports"],N):(p=typeof globalThis<"u"?globalThis:p||self,N(p.index={}))})(this,function(p){"use strict";var N=(e=>(e.TRIAL="trial",e.BASIC="basic",e.ENTERPRISE="enterprise",e.LITE="lite",e.ADVANCED="advanced",e))(N||{}),H=(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))(H||{}),q=(e=>(e.Unrated="Unrated",e.Positive="Positive",e.Negative="Negative",e))(q||{}),k=(e=>(e.Functional="Functional",e.TextOnly="TextOnly",e.Maintenance="Maintenance",e.Playground="Playground",e.DirectPlayback="DirectPlayback",e))(k||{}),T=(e=>(e.Embed="embed",e.Query="query",e.Partial="partial",e.Answer="answer",e.Complete="done",e))(T||{}),J=(e=>(e.KnowledgeProcessing="knowledge/processing",e.KnowledgeIndexing="knowledge/indexing",e.KnowledgeFailed="knowledge/error",e.KnowledgeDone="knowledge/done",e))(J||{}),X=(e=>(e.Knowledge="knowledge",e.Document="document",e.Record="record",e))(X||{}),Y=(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))(Y||{}),S=(e=>(e.Clip="clip",e.Talk="talk",e))(S||{}),b=(e=>(e.Start="START",e.Stop="STOP",e))(b||{}),x=(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))(x||{}),C=(e=>(e.New="new",e.Fail="fail",e.Connected="connected",e.Connecting="connecting",e.Closed="closed",e.Completed="completed",e.Disconnected="disconnected",e))(C||{}),Z=(e=>(e.Amazon="amazon",e.Microsoft="microsoft",e.Afflorithmics="afflorithmics",e.Elevenlabs="elevenlabs",e))(Z||{}),G=(e=>(e.Public="public",e.Premium="premium",e.Private="private",e))(G||{});const z="https://api.d-id.com",ce="wss://notifications.d-id.com",de="79f81a83a67430be2bc0fd61042b8faa",F=()=>Math.random().toString(16).slice(2);function Q(){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 le=F();function ee(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}.${Q()}_${le}`;throw new Error(`Unknown auth type: ${e}`)}function j(e,n=z,o){const i=async(t,r)=>{const a=await fetch(n+(t!=null&&t.startsWith("/")?t:`/${t}`),{...r,headers:{...r==null?void 0:r.headers,Authorization:ee(e),"Content-Type":"application/json"}});if(!a.ok){let s=await a.text().catch(()=>"Failed to fetch");throw o&&o(new Error(s),{url:t,options:r,headers:a.headers}),new Error(s)}return a.json()};return{get(t,r){return i(t,{...r,method:"GET"})},post(t,r,a){return i(t,{...a,body:JSON.stringify(r),method:"POST"})},delete(t,r,a){return i(t,{...a,body:JSON.stringify(r),method:"DELETE"})},patch(t,r,a){return i(t,{...a,body:JSON.stringify(r),method:"PATCH"})}}}function te(e,n=z,o){const i=j(e,`${n}/agents`,o);return{create(t,r){return i.post("/",t,r)},getAgents(t,r){return i.get(`/${t?`?tag=${t}`:""}`,r).then(a=>a??[])},getById(t,r){return i.get(`/${t}`,r)},delete(t,r){return i.delete(`/${t}`,void 0,r)},update(t,r,a){return i.patch(`/${t}`,r,a)},newChat(t,r){return i.post(`/${t}/chat`,void 0,r)},chat(t,r,a,s){return i.post(`/${t}/chat/${r}`,a,s)},createRating(t,r,a,s){return i.post(`/${t}/chat/${r}/ratings`,a,s)},updateRating(t,r,a,s,m){return i.patch(`/${t}/chat/${r}/ratings/${a}`,s,m)},deleteRating(t,r,a,s){return i.delete(`/${t}/chat/${r}/ratings/${a}`,s)}}}const me=e=>new Promise(n=>setTimeout(n,e));function ue(e){return new Promise((n,o)=>{const{callbacks:i,host:t,auth:r}=e,{onMessage:a=null,onOpen:s=null,onClose:m=null,onError:d=null}=i||{},l=new WebSocket(`${t}?authorization=${ee(r)}`);l.onmessage=a,l.onclose=m,l.onerror=v=>{console.error(v),d==null||d("Websocket failed to connect",v),o(v)},l.onopen=v=>{s==null||s(v),n(l)}})}async function ge(e){const{retries:n=1}=e;let o=null;for(let i=0;(o==null?void 0:o.readyState)!==WebSocket.OPEN;i++)try{o=await ue(e)}catch(t){if(i===n)throw t;await me(i*500)}return o}async function we(e,n,o){const i=o!=null&&o.onMessage?[o.onMessage]:[],t=await ge({auth:e,host:n,callbacks:{onError:o==null?void 0:o.onError,onMessage:r=>{const a=JSON.parse(r.data);i.forEach(s=>s(a.event,a))}}});return{socket:t,disconnect:()=>t.close(),subscribeToEvents:r=>i.push(r)}}const he="X-Playground-Chat";function fe(e,n,o,i){const t=j(e,`${n}/agents/${o}`,i);return{createStream(r){return t.post("/streams",{driver_id:r.driver_id,presenter_id:r.presenter_id,compatibility_mode:r.compatibility_mode,stream_warmup:r.stream_warmup,session_timeout:r.session_timeout,type:S.Clip})},startConnection(r,a,s){return t.post(`/streams/${r}/sdp`,{session_id:s,answer:a,type:S.Clip})},addIceCandidate(r,a,s){return t.post(`/streams/${r}/ice`,{session_id:s,...a,type:S.Clip})},sendStreamRequest(r,a,s){return t.post(`/streams/${r}`,{session_id:a,...s,type:S.Clip})},close(r,a){return t.delete(`/streams/${r}`,{session_id:a,type:S.Clip})}}}function pe(e,n,o,i){const t=j(e,`${n}/agents/${o}`,i);return{createStream(r,a){return t.post("/streams",{source_url:r.source_url,driver_url:r.driver_url,face:r.face,config:r.config,compatibility_mode:r.compatibility_mode,stream_warmup:r.stream_warmup,output_resolution:r.output_resolution,session_timeout:r.session_timeout,type:S.Talk},a)},startConnection(r,a,s,m){return t.post(`/streams/${r}/sdp`,{session_id:s,answer:a,type:S.Talk},m)},addIceCandidate(r,a,s,m){return t.post(`/streams/${r}/ice`,{session_id:s,...a,type:S.Talk},m)},sendStreamRequest(r,a,s,m){return t.post(`/streams/${r}`,{session_id:a,...s,type:S.Talk},m)},close(r,a,s){return t.delete(`/streams/${r}`,{session_id:a,type:S.Talk},s)}}}let ne=!1;const $=(e,n)=>ne&&console.log(e,n),ye=(window.RTCPeerConnection||window.webkitRTCPeerConnection||window.mozRTCPeerConnection).bind(window);function re(e){switch(e){case"connected":return C.Connected;case"checking":return C.Connecting;case"failed":return C.Fail;case"new":return C.New;case"closed":return C.Closed;case"disconnected":return C.Disconnected;case"completed":return C.Completed;default:return C.New}}function ve(){let e=0;return n=>{for(const o of n.values())if(o&&o.type==="inbound-rtp"&&o.kind==="video"){const i=o.bytesReceived,t=i-e>0;return e=i,t}return!1}}function Ce(e,n){const i=Math.max(Math.ceil(10),1);let t=0,r=!1;const a=ve();return setInterval(async()=>{const s=await e.getStats();a(s)?(t=0,r||(n==null||n(b.Start),r=!0)):r&&(t++,t>=i&&(n==null||n(b.Stop),r=!1))},100)}async function Se(e,n,{debug:o=!1,callbacks:i,auth:t,baseURL:r=z,warmup:a}){ne=o;let s;const{startConnection:m,sendStreamRequest:d,close:l,createStream:v,addIceCandidate:P}=n.videoType===S.Clip?fe(t,r,e,i.onError):pe(t,r,e,i.onError),{id:A,offer:K,ice_servers:W,session_id:I}=await v(n),y=new ye({iceServers:W}),c=y.createDataChannel("JanusDataChannel");if(!I)throw new Error("Could not create session_id");const w=Ce(y,i.onVideoStateChange);y.onicecandidate=u=>{$("peerConnection.onicecandidate",u),u.candidate&&u.candidate.sdpMid&&u.candidate.sdpMLineIndex!==null?P(A,{candidate:u.candidate.candidate,sdpMid:u.candidate.sdpMid,sdpMLineIndex:u.candidate.sdpMLineIndex},I):P(A,{candidate:null},I)},y.oniceconnectionstatechange=()=>{var g;$("peerConnection.oniceconnectionstatechange => "+y.iceConnectionState);const u=re(y.iceConnectionState);u===C.Connected?s=setTimeout(()=>{var f;return(f=i.onConnectionStateChange)==null?void 0:f.call(i,C.Connected)},a?5e3:0):(clearTimeout(s),(g=i.onConnectionStateChange)==null||g.call(i,u))},y.ontrack=u=>{var g;$("peerConnection.ontrack",u),(g=i.onSrcObjectReady)==null||g.call(i,u.streams[0])},c.onmessage=u=>{var g;if(c.readyState==="open"){const[f,_]=u.data.split(":");f===x.StreamReady&&(clearTimeout(s),(g=i.onConnectionStateChange)==null||g.call(i,C.Connected))}},await y.setRemoteDescription(K),$("set remote description OK");const h=await y.createAnswer();return $("create answer OK"),await y.setLocalDescription(h),$("set local description OK"),await m(A,h,I),$("start connection OK"),{speak(u){return d(A,I,u)},async disconnect(){var u,g;if(A){const f=re(y.iceConnectionState);if(y){if(f===C.New){(u=i.onVideoStateChange)==null||u.call(i,b.Stop),clearInterval(w);return}y.close(),y.oniceconnectionstatechange=null,y.onnegotiationneeded=null,y.onicecandidate=null,y.ontrack=null}try{f===C.Connected&&await l(A,I).catch(_=>{})}catch(_){$("Error on close stream connection",_)}(g=i.onVideoStateChange)==null||g.call(i,b.Stop),clearInterval(w)}},sessionId:I,streamId:A}}let B={};function _e(e){const n=window!=null&&window.hasOwnProperty("DID_AGENTS_API")?"agents-ui":"agents-sdk",o={token:e.token||"testKey",distinct_id:e.distinctId||Q(),agentId:e.agent.id,agentType:e.agent.presenter.type,owner_id:e.agent.owner_id??""};return{...o,isEnabled:e.isEnabled??!0,getRandom:()=>Math.random().toString(16).slice(2),track(i,t){if(!this.isEnabled)return Promise.reject("MixPanel analytics is disabled on creation");const{audioPath:r,...a}=t||{},s={method:"POST",headers:{"Content-Type":"application/x-www-form-urlencoded"},body:new URLSearchParams({data:JSON.stringify([{event:i,properties:{...a,...o,source:n,time:Date.now(),$insert_id:this.getRandom(),origin:window.location.href,"Screen Height":window.screen.height||window.innerWidth,"Screen Width":window.screen.width||window.innerHeight,"User Agent":navigator.userAgent}}])})};return fetch("https://api-js.mixpanel.com/track/?verbose=1&ip=1",s).then(m=>m.json()).catch(m=>console.error(m))},linkTrack(i,t,r,a){B[i]||(B[i]={events:{},resolvedDependencies:[]}),a.includes(r)||a.push(r);const s=B[i];if(s.events[r]={props:t},s.resolvedDependencies.push(r),a.every(d=>s.resolvedDependencies.includes(d))){const d=a.reduce((l,v)=>s.events[v]?{...l,...s.events[v].props}:l,{});this.track(i,d),s.resolvedDependencies=s.resolvedDependencies.filter(l=>!a.includes(l)),a.forEach(l=>{delete s.events[l]})}}}}function Me(e){var i,t,r,a,s;const n=()=>/Mobi|Android/i.test(navigator.userAgent)?"Mobile":"Desktop";return{$os:`${(()=>{const m=navigator.platform;return m.toLowerCase().includes("win")?"Windows":m.toLowerCase().includes("mac")?"Mac OS X":m.toLowerCase().includes("linux")?"Linux":"Unknown"})()}`,isMobile:`${n()=="Mobile"}`,browser:navigator.userAgent,origin:window.location.origin,agentType:(i=e.presenter)==null?void 0:i.type,agentVoice:{voiceId:(r=(t=e.presenter)==null?void 0:t.voice)==null?void 0:r.voice_id,provider:(s=(a=e.presenter)==null?void 0:a.voice)==null?void 0:s.type}}}function ke(e,n,o){var d,l;const{event:i,...t}=e,{template:r}=(n==null?void 0:n.llm)||{},{language:a}=((d=n==null?void 0:n.presenter)==null?void 0:d.voice)||{},{stitch:s}=(n==null?void 0:n.presenter)||{};return{...t,llm:{...t.llm,template:r},script:{...t.script,provider:{...(l=t==null?void 0:t.script)==null?void 0:l.provider,language:a}},stitch:s,...o}}let D=0;function Ae(e,n){var o,i,t,r,a,s,m;if(e.presenter){if(e.presenter.type===S.Clip)return{videoType:S.Clip,driver_id:e.presenter.driver_id,presenter_id:e.presenter.presenter_id,session_timeout:(o=n==null?void 0:n.streamOptions)==null?void 0:o.sessionTimeout,stream_warmup:(i=n==null?void 0:n.streamOptions)==null?void 0:i.streamWarmup,compatibility_mode:(t=n==null?void 0:n.streamOptions)==null?void 0:t.compatibilityMode}}else throw new Error("Presenter is not initialized");return{videoType:S.Talk,source_url:e.presenter.source_url,session_timeout:(r=n==null?void 0:n.streamOptions)==null?void 0:r.sessionTimeout,stream_warmup:(a=n==null?void 0:n.streamOptions)==null?void 0:a.streamWarmup,compatibility_mode:(s=n==null?void 0:n.streamOptions)==null?void 0:s.compatibilityMode,output_resolution:(m=n==null?void 0:n.streamOptions)==null?void 0:m.outputResolution}}function ie(e){return e===k.Playground?{headers:{[he]:"true"}}:{}}async function ae(e,n,o,i){try{const t=await n.newChat(e,ie(i));return o.track("agent-chat",{event:"created",chat_id:t.id,agent_id:e,mode:i}),t}catch(t){try{console.error(t);const r=JSON.parse(t.message);if((r==null?void 0:r.kind)==="InsufficientCreditsError")throw new Error("InsufficientCreditsError")}catch(r){console.error("Error parsing the error message:",r)}throw new Error("Cannot create new chat")}}function Ee(e,n,o,i,t){return new Promise(async(r,a)=>{var m;D=0;const s=await Se(e.id,Ae(e,n),{...n,analytics:i,warmup:(m=n.streamOptions)==null?void 0:m.streamWarmup,callbacks:{...n.callbacks,onConnectionStateChange:async d=>{var l,v;d===C.Connected&&(!t&&n.mode!==k.DirectPlayback&&(t=await ae(e.id,o,i,n.mode).catch(P=>{a(P)})),s?r({chat:t,streamingManager:s}):t&&a(new Error("Something went wrong while initializing the manager"))),(v=(l=n.callbacks).onConnectionStateChange)==null||v.call(l,d)},onVideoStateChange(d){var l,v;(v=(l=n.callbacks).onVideoStateChange)==null||v.call(l,d),D>0&&d===b.Start&&i.linkTrack("agent-video",{event:"start",latency:Date.now()-D},"start",[x.StreamVideoCreated])}}}).catch(a)})}function se(e){let n="";if(e.greetings&&e.greetings.length>0){const o=Math.floor(Math.random()*e.greetings.length);n=e.greetings[o]}else n=`Hi! I'm ${e.preview_name||"My Agent"}. How can I help you?`;return[{content:n,id:F(),role:"assistant",created_at:new Date().toISOString()}]}function Ie(e){if(e.answer!==void 0)return e.answer;let n=0,o="";for(;n in e;)o+=e[n],n++;return o}function Re(e,n,o,i,t){if(!(e===T.Partial||e===T.Answer))return;const r=i.messages[i.messages.length-1];if((r==null?void 0:r.role)!=="assistant")return;const{content:a,sequence:s}=n;e===T.Partial?o[s]=a:o.answer=a;const m=Ie(o);(r.content!==m||e===T.Answer)&&(r.content=m,t==null||t([...i.messages],e))}async function $e(e,n){var W,I,y;let o={},i=!0;const t={messages:[],chatMode:n.mode||k.Functional},r=n.baseURL||z,a=n.wsURL||ce,s=n.mixpanelKey||de,m=te(n.auth,r,n.callbacks.onError),d=await m.getById(e);t.messages=se(d),(I=(W=n.callbacks).onNewMessage)==null||I.call(W,[...t.messages],"answer");const l=_e({token:s,agent:d,...n});l.track("agent-sdk",{event:"loaded",...Me(d)});const v={onMessage:(c,w)=>{var h,u;if("content"in w)Re(c,w,o,t,n.callbacks.onNewMessage),c===T.Answer&&l.track("agent-message-received",{messages:t.messages.length,mode:t.chatMode});else{const g=x,f=[g.StreamVideoDone,g.StreamVideoError,g.StreamVideoRejected],_=[g.StreamFailed,g.StreamVideoError,g.StreamVideoRejected],E=ke(w,d,{mode:t.chatMode});if(c=c,c===g.StreamVideoCreated)l.linkTrack("agent-video",E,g.StreamVideoCreated,["start"]);else if(f.includes(c)){const M=c.split("/")[1];l.track("agent-video",{...E,event:M})}_.includes(c)&&((u=(h=n.callbacks).onError)==null||u.call(h,new Error(`Stream failed with event ${c}`),{data:w})),w.event===g.StreamDone&&A()}}};async function P(c){var _,E,M,R,U;D=0,c&&!i&&(delete t.chat,t.messages=se(d),(E=(_=n.callbacks).onNewMessage)==null||E.call(_,[...t.messages],"answer"));const w=n.mode===k.DirectPlayback?Promise.resolve(void 0):we(n.auth,a,v),h=Ee(d,n,m,l,t.chat).catch(O=>{throw K(k.Maintenance),O}),[u,{streamingManager:g,chat:f}]=await Promise.all([w,h]);f&&f.id!==((M=t.chat)==null?void 0:M.id)&&((U=(R=n.callbacks).onNewChat)==null||U.call(R,f.id)),t.streamingManager=g,t.socketManager=u,t.chat=f,i=!1,K((f==null?void 0:f.chat_mode)??n.mode??k.Functional)}async function A(){var c,w,h,u;(c=t.socketManager)==null||c.disconnect(),await((w=t.streamingManager)==null?void 0:w.disconnect()),delete t.streamingManager,delete t.socketManager,(u=(h=n.callbacks).onConnectionStateChange)==null||u.call(h,C.Disconnected)}async function K(c){var w,h;c!==t.chatMode&&(l.track("agent-mode-change",{mode:c}),t.chatMode=c,t.chatMode!==k.Functional&&await A(),(h=(w=n.callbacks).onModeChange)==null||h.call(w,c))}return{agent:d,starterMessages:((y=d.knowledge)==null?void 0:y.starter_message)||[],changeMode:K,async connect(){var c;await P(!0),l.track("agent-chat",{event:"connect",chatId:(c=t.chat)==null?void 0:c.id,agentId:d.id,mode:t.chatMode})},async reconnect(){var c;await A(),await P(!1),l.track("agent-chat",{event:"reconnect",chatId:(c=t.chat)==null?void 0:c.id,agentId:d.id,mode:t.chatMode})},async disconnect(){var c;await A(),l.track("agent-chat",{event:"disconnect",chatId:(c=t.chat)==null?void 0:c.id,agentId:d.id,mode:t.chatMode})},async chat(c){var h,u,g,f,_,E;const w=F();o={};try{if(D=Date.now(),n.mode===k.DirectPlayback)throw new Error("Direct playback is enabled, chat is disabled");if(c.length>=800)throw new Error("Message cannot be more than 800 characters");if(c.length===0)throw new Error("Message cannot be empty");if(t.chatMode===k.Maintenance)throw new Error("Chat is in maintenance mode");if(![k.TextOnly,k.Playground].includes(t.chatMode))if(t.streamingManager){if(!t.chat)throw new Error("Chat is not initialized")}else throw new Error("Streaming manager is not initialized");t.messages.push({id:F(),role:"user",content:c,created_at:new Date(D).toISOString()}),(u=(h=n.callbacks).onNewMessage)==null||u.call(h,[...t.messages],"user"),t.chat||(t.chat=await ae(d.id,m,l,t.chatMode),(f=(g=n.callbacks).onNewChat)==null||f.call(g,t.chat.id));const M={id:w,role:"assistant",content:"",created_at:new Date().toISOString(),matches:[]},R=[...t.messages];t.messages.push(M);const U=L=>{var V,oe;return m.chat(d.id,L,{sessionId:(V=t.streamingManager)==null?void 0:V.sessionId,streamId:(oe=t.streamingManager)==null?void 0:oe.streamId,chatMode:t.chatMode,messages:R},ie(t.chatMode))},O=await U(t.chat.id).catch(async L=>{var V;if(!((V=L==null?void 0:L.message)!=null&&V.includes("missing or invalid session_id")))throw L;return await A(),await P(!1),U(t.chat.id)});return l.track("agent-message-send",{event:"success",mode:t.chatMode,messages:t.messages.length+1}),M.context=O.context,M.matches=O.matches,O.result&&(M.content=O.result,l.track("agent-message-received",{latency:Date.now()-D,mode:t.chatMode,messages:t.messages.length}),(E=(_=n.callbacks).onNewMessage)==null||E.call(_,[...t.messages],"answer")),O}catch(M){throw t.messages[t.messages.length-1].id===w&&t.messages.pop(),l.track("agent-message-send",{event:"error",mode:t.chatMode,messages:t.messages.length}),M}},rate(c,w,h){var f,_,E,M;const u=t.messages.find(R=>R.id===c);if(t.chat){if(!u)throw new Error("Message not found")}else throw new Error("Chat is not initialized");const g=((f=u.matches)==null?void 0:f.map(R=>[R.document_id,R.id]))??[];return l.track("agent-rate",{event:h?"update":"create",thumb:w===1?"up":"down",knowledge_id:((_=d.knowledge)==null?void 0:_.id)??"",mode:t.chatMode,matches:g,score:w}),h?m.updateRating(d.id,t.chat.id,h,{knowledge_id:((E=d.knowledge)==null?void 0:E.id)??"",message_id:c,matches:g,score:w}):m.createRating(d.id,t.chat.id,{knowledge_id:((M=d.knowledge)==null?void 0:M.id)??"",message_id:c,matches:g,score:w})},deleteRate(c){var w;if(!t.chat)throw new Error("Chat is not initialized");return l.track("agent-rate-delete",{type:"text",chat_id:(w=t.chat)==null?void 0:w.id,id:c,mode:t.chatMode}),m.deleteRating(d.id,t.chat.id,c)},speak(c){if(!t.streamingManager)throw new Error("Please connect to the agent first");function w(){if(typeof c=="string"){if(!d.presenter.voice)throw new Error("Presenter voice is not initialized");return{type:"text",provider:d.presenter.voice,input:c,ssml:!1}}if(c.type==="text"&&!c.provider){if(!d.presenter.voice)throw new Error("Presenter voice is not initialized");return{type:"text",provider:d.presenter.voice,input:c.input,ssml:c.ssml}}return c}const h=w();return l.track("agent-speak",h),t.streamingManager.speak({script:h})}}}function Pe(e,n,o){const{getById:i}=te(n,o||z);return i(e)}p.ChatMode=k,p.ChatProgress=T,p.ConnectionState=C,p.DocumentType=Y,p.KnowledgeType=X,p.PlanGroup=H,p.Providers=Z,p.RateState=q,p.StreamEvents=x,p.StreamingState=b,p.Subject=J,p.UserPlan=N,p.VideoType=S,p.VoiceAccess=G,p.createAgentManager=$e,p.getAgent=Pe,Object.defineProperty(p,Symbol.toStringTag,{value:"Module"})});
|
|
1
|
+
(function(p,O){typeof exports=="object"&&typeof module<"u"?O(exports):typeof define=="function"&&define.amd?define(["exports"],O):(p=typeof globalThis<"u"?globalThis:p||self,O(p.index={}))})(this,function(p){"use strict";var O=(e=>(e.TRIAL="trial",e.BASIC="basic",e.ENTERPRISE="enterprise",e.LITE="lite",e.ADVANCED="advanced",e))(O||{}),H=(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))(H||{}),q=(e=>(e.Unrated="Unrated",e.Positive="Positive",e.Negative="Negative",e))(q||{}),_=(e=>(e.Functional="Functional",e.TextOnly="TextOnly",e.Maintenance="Maintenance",e.Playground="Playground",e.DirectPlayback="DirectPlayback",e))(_||{}),P=(e=>(e.Embed="embed",e.Query="query",e.Partial="partial",e.Answer="answer",e.Complete="done",e))(P||{}),J=(e=>(e.KnowledgeProcessing="knowledge/processing",e.KnowledgeIndexing="knowledge/indexing",e.KnowledgeFailed="knowledge/error",e.KnowledgeDone="knowledge/done",e))(J||{}),X=(e=>(e.Knowledge="knowledge",e.Document="document",e.Record="record",e))(X||{}),Y=(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))(Y||{}),N=(e=>(e.Clip="clip",e.Talk="talk",e))(N||{}),b=(e=>(e.Start="START",e.Stop="STOP",e))(b||{}),x=(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))(x||{}),C=(e=>(e.New="new",e.Fail="fail",e.Connected="connected",e.Connecting="connecting",e.Closed="closed",e.Completed="completed",e.Disconnected="disconnected",e))(C||{}),Z=(e=>(e.Amazon="amazon",e.Microsoft="microsoft",e.Afflorithmics="afflorithmics",e.Elevenlabs="elevenlabs",e))(Z||{}),G=(e=>(e.Public="public",e.Premium="premium",e.Private="private",e))(G||{});const z="https://api.d-id.com",ce="wss://notifications.d-id.com",de="79f81a83a67430be2bc0fd61042b8faa",F=()=>Math.random().toString(16).slice(2);function Q(){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 le=F();function ee(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}.${Q()}_${le}`;throw new Error(`Unknown auth type: ${e}`)}function j(e,n=z,o){const a=async(t,r)=>{const i=await fetch(n+(t!=null&&t.startsWith("/")?t:`/${t}`),{...r,headers:{...r==null?void 0:r.headers,Authorization:ee(e),"Content-Type":"application/json"}});if(!i.ok){let s=await i.text().catch(()=>"Failed to fetch");throw o&&o(new Error(s),{url:t,options:r,headers:i.headers}),new Error(s)}return i.json()};return{get(t,r){return a(t,{...r,method:"GET"})},post(t,r,i){return a(t,{...i,body:JSON.stringify(r),method:"POST"})},delete(t,r,i){return a(t,{...i,body:JSON.stringify(r),method:"DELETE"})},patch(t,r,i){return a(t,{...i,body:JSON.stringify(r),method:"PATCH"})}}}function te(e,n=z,o){const a=j(e,`${n}/agents`,o);return{create(t,r){return a.post("/",t,r)},getAgents(t,r){return a.get(`/${t?`?tag=${t}`:""}`,r).then(i=>i??[])},getById(t,r){return a.get(`/${t}`,r)},delete(t,r){return a.delete(`/${t}`,void 0,r)},update(t,r,i){return a.patch(`/${t}`,r,i)},newChat(t,r){return a.post(`/${t}/chat`,void 0,r)},chat(t,r,i,s){return a.post(`/${t}/chat/${r}`,i,s)},createRating(t,r,i,s){return a.post(`/${t}/chat/${r}/ratings`,i,s)},updateRating(t,r,i,s,m){return a.patch(`/${t}/chat/${r}/ratings/${i}`,s,m)},deleteRating(t,r,i,s){return a.delete(`/${t}/chat/${r}/ratings/${i}`,s)}}}const me=e=>new Promise(n=>setTimeout(n,e));function ge(e){return new Promise((n,o)=>{const{callbacks:a,host:t,auth:r}=e,{onMessage:i=null,onOpen:s=null,onClose:m=null,onError:d=null}=a||{},l=new WebSocket(`${t}?authorization=${ee(r)}`);l.onmessage=i,l.onclose=m,l.onerror=v=>{console.error(v),d==null||d("Websocket failed to connect",v),o(v)},l.onopen=v=>{s==null||s(v),n(l)}})}async function ue(e){const{retries:n=1}=e;let o=null;for(let a=0;(o==null?void 0:o.readyState)!==WebSocket.OPEN;a++)try{o=await ge(e)}catch(t){if(a===n)throw t;await me(a*500)}return o}async function we(e,n,o){const a=o!=null&&o.onMessage?[o.onMessage]:[],t=await ue({auth:e,host:n,callbacks:{onError:o==null?void 0:o.onError,onMessage:r=>{const i=JSON.parse(r.data);a.forEach(s=>s(i.event,i))}}});return{socket:t,disconnect:()=>t.close(),subscribeToEvents:r=>a.push(r)}}const he="X-Playground-Chat";function fe(e,n,o,a){const t=j(e,`${n}/agents/${o}`,a);return{createStream(r){return t.post("/streams",{compatibility_mode:r.compatibility_mode,stream_warmup:r.stream_warmup,session_timeout:r.session_timeout})},startConnection(r,i,s){return t.post(`/streams/${r}/sdp`,{session_id:s,answer:i})},addIceCandidate(r,i,s){return t.post(`/streams/${r}/ice`,{session_id:s,...i})},sendStreamRequest(r,i,s){return t.post(`/streams/${r}`,{session_id:i,...s})},close(r,i){return t.delete(`/streams/${r}`,{session_id:i})}}}function pe(e,n,o,a){const t=j(e,`${n}/agents/${o}`,a);return{createStream(r,i){return t.post("/streams",{driver_url:r.driver_url,face:r.face,config:r.config,compatibility_mode:r.compatibility_mode,stream_warmup:r.stream_warmup,output_resolution:r.output_resolution,session_timeout:r.session_timeout},i)},startConnection(r,i,s,m){return t.post(`/streams/${r}/sdp`,{session_id:s,answer:i},m)},addIceCandidate(r,i,s,m){return t.post(`/streams/${r}/ice`,{session_id:s,...i},m)},sendStreamRequest(r,i,s,m){return t.post(`/streams/${r}`,{session_id:i,...s},m)},close(r,i,s){return t.delete(`/streams/${r}`,{session_id:i},s)}}}let ne=!1;const E=(e,n)=>ne&&console.log(e,n),ye=(window.RTCPeerConnection||window.webkitRTCPeerConnection||window.mozRTCPeerConnection).bind(window);function re(e){switch(e){case"connected":return C.Connected;case"checking":return C.Connecting;case"failed":return C.Fail;case"new":return C.New;case"closed":return C.Closed;case"disconnected":return C.Disconnected;case"completed":return C.Completed;default:return C.New}}function ve(){let e=0;return n=>{for(const o of n.values())if(o&&o.type==="inbound-rtp"&&o.kind==="video"){const a=o.bytesReceived,t=a-e>0;return e=a,t}return!1}}function Ce(e,n){const a=Math.max(Math.ceil(10),1);let t=0,r=!1;const i=ve();return setInterval(async()=>{const s=await e.getStats();i(s)?(t=0,r||(n==null||n(b.Start),r=!0)):r&&(t++,t>=a&&(n==null||n(b.Stop),r=!1))},100)}async function Se(e,n,{debug:o=!1,callbacks:a,auth:t,baseURL:r=z,warmup:i}){ne=o;let s;const{startConnection:m,sendStreamRequest:d,close:l,createStream:v,addIceCandidate:$}=n.videoType===N.Clip?fe(t,r,e,a.onError):pe(t,r,e,a.onError),{id:k,offer:K,ice_servers:W,session_id:I}=await v(n),y=new ye({iceServers:W}),c=y.createDataChannel("JanusDataChannel");if(!I)throw new Error("Could not create session_id");const w=Ce(y,a.onVideoStateChange);y.onicecandidate=g=>{E("peerConnection.onicecandidate",g),g.candidate&&g.candidate.sdpMid&&g.candidate.sdpMLineIndex!==null?$(k,{candidate:g.candidate.candidate,sdpMid:g.candidate.sdpMid,sdpMLineIndex:g.candidate.sdpMLineIndex},I):$(k,{candidate:null},I)},y.oniceconnectionstatechange=()=>{var u;E("peerConnection.oniceconnectionstatechange => "+y.iceConnectionState);const g=re(y.iceConnectionState);g===C.Connected?s=setTimeout(()=>{var f;return(f=a.onConnectionStateChange)==null?void 0:f.call(a,C.Connected)},i?5e3:0):(clearTimeout(s),(u=a.onConnectionStateChange)==null||u.call(a,g))},y.ontrack=g=>{var u;E("peerConnection.ontrack",g),(u=a.onSrcObjectReady)==null||u.call(a,g.streams[0])},c.onmessage=g=>{var u;if(c.readyState==="open"){const[f,S]=g.data.split(":");f===x.StreamReady&&(clearTimeout(s),(u=a.onConnectionStateChange)==null||u.call(a,C.Connected))}},await y.setRemoteDescription(K),E("set remote description OK");const h=await y.createAnswer();return E("create answer OK"),await y.setLocalDescription(h),E("set local description OK"),await m(k,h,I),E("start connection OK"),{speak(g){return d(k,I,g)},async disconnect(){var g,u;if(k){const f=re(y.iceConnectionState);if(y){if(f===C.New){(g=a.onVideoStateChange)==null||g.call(a,b.Stop),clearInterval(w);return}y.close(),y.oniceconnectionstatechange=null,y.onnegotiationneeded=null,y.onicecandidate=null,y.ontrack=null}try{f===C.Connected&&await l(k,I).catch(S=>{})}catch(S){E("Error on close stream connection",S)}(u=a.onVideoStateChange)==null||u.call(a,b.Stop),clearInterval(w)}},sessionId:I,streamId:k}}let B={};function Me(e){const n=window!=null&&window.hasOwnProperty("DID_AGENTS_API")?"agents-ui":"agents-sdk",o={token:e.token||"testKey",distinct_id:e.distinctId||Q(),agentId:e.agent.id,agentType:e.agent.presenter.type,owner_id:e.agent.owner_id??""};return{...o,isEnabled:e.isEnabled??!0,getRandom:()=>Math.random().toString(16).slice(2),track(a,t){if(!this.isEnabled)return Promise.reject("MixPanel analytics is disabled on creation");const{audioPath:r,...i}=t||{},s={method:"POST",headers:{"Content-Type":"application/x-www-form-urlencoded"},body:new URLSearchParams({data:JSON.stringify([{event:a,properties:{...i,...o,source:n,time:Date.now(),$insert_id:this.getRandom(),origin:window.location.href,"Screen Height":window.screen.height||window.innerWidth,"Screen Width":window.screen.width||window.innerHeight,"User Agent":navigator.userAgent}}])})};return fetch("https://api-js.mixpanel.com/track/?verbose=1&ip=1",s).then(m=>m.json()).catch(m=>console.error(m))},linkTrack(a,t,r,i){B[a]||(B[a]={events:{},resolvedDependencies:[]}),i.includes(r)||i.push(r);const s=B[a];if(s.events[r]={props:t},s.resolvedDependencies.push(r),i.every(d=>s.resolvedDependencies.includes(d))){const d=i.reduce((l,v)=>s.events[v]?{...l,...s.events[v].props}:l,{});this.track(a,d),s.resolvedDependencies=s.resolvedDependencies.filter(l=>!i.includes(l)),i.forEach(l=>{delete s.events[l]})}}}}function _e(e){var a,t,r,i,s;const n=()=>/Mobi|Android/i.test(navigator.userAgent)?"Mobile":"Desktop";return{$os:`${(()=>{const m=navigator.platform;return m.toLowerCase().includes("win")?"Windows":m.toLowerCase().includes("mac")?"Mac OS X":m.toLowerCase().includes("linux")?"Linux":"Unknown"})()}`,isMobile:`${n()=="Mobile"}`,browser:navigator.userAgent,origin:window.location.origin,agentType:(a=e.presenter)==null?void 0:a.type,agentVoice:{voiceId:(r=(t=e.presenter)==null?void 0:t.voice)==null?void 0:r.voice_id,provider:(s=(i=e.presenter)==null?void 0:i.voice)==null?void 0:s.type}}}function ke(e,n,o){var d,l;const{event:a,...t}=e,{template:r}=(n==null?void 0:n.llm)||{},{language:i}=((d=n==null?void 0:n.presenter)==null?void 0:d.voice)||{},{stitch:s}=(n==null?void 0:n.presenter)||{};return{...t,llm:{...t.llm,template:r},script:{...t.script,provider:{...(l=t==null?void 0:t.script)==null?void 0:l.provider,language:i}},stitch:s,...o}}let D=0;function Ae(e,n){var o,a,t,r,i,s,m;return e.presenter.type===N.Clip?{videoType:N.Clip,session_timeout:(o=n==null?void 0:n.streamOptions)==null?void 0:o.sessionTimeout,stream_warmup:(a=n==null?void 0:n.streamOptions)==null?void 0:a.streamWarmup,compatibility_mode:(t=n==null?void 0:n.streamOptions)==null?void 0:t.compatibilityMode}:{videoType:N.Talk,session_timeout:(r=n==null?void 0:n.streamOptions)==null?void 0:r.sessionTimeout,stream_warmup:(i=n==null?void 0:n.streamOptions)==null?void 0:i.streamWarmup,compatibility_mode:(s=n==null?void 0:n.streamOptions)==null?void 0:s.compatibilityMode,output_resolution:(m=n==null?void 0:n.streamOptions)==null?void 0:m.outputResolution}}function ae(e){return e===_.Playground?{headers:{[he]:"true"}}:{}}async function ie(e,n,o,a){try{const t=await n.newChat(e,ae(a));return o.track("agent-chat",{event:"created",chat_id:t.id,agent_id:e,mode:a}),t}catch(t){try{console.error(t);const r=JSON.parse(t.message);if((r==null?void 0:r.kind)==="InsufficientCreditsError")throw new Error("InsufficientCreditsError")}catch(r){console.error("Error parsing the error message:",r)}throw new Error("Cannot create new chat")}}function Ie(e,n,o,a,t){return new Promise(async(r,i)=>{var m;D=0;const s=await Se(e.id,Ae(e,n),{...n,analytics:a,warmup:(m=n.streamOptions)==null?void 0:m.streamWarmup,callbacks:{...n.callbacks,onConnectionStateChange:async d=>{var l,v;d===C.Connected&&(!t&&n.mode!==_.DirectPlayback&&(t=await ie(e.id,o,a,n.mode).catch($=>{i($)})),s?r({chat:t,streamingManager:s}):t&&i(new Error("Something went wrong while initializing the manager"))),(v=(l=n.callbacks).onConnectionStateChange)==null||v.call(l,d)},onVideoStateChange(d){var l,v;(v=(l=n.callbacks).onVideoStateChange)==null||v.call(l,d),D>0&&d===b.Start&&a.linkTrack("agent-video",{event:"start",latency:Date.now()-D},"start",[x.StreamVideoCreated])}}}).catch(i)})}function se(e){let n="";if(e.greetings&&e.greetings.length>0){const o=Math.floor(Math.random()*e.greetings.length);n=e.greetings[o]}else n=`Hi! I'm ${e.preview_name||"My Agent"}. How can I help you?`;return[{content:n,id:F(),role:"assistant",created_at:new Date().toISOString()}]}function Re(e){if(e.answer!==void 0)return e.answer;let n=0,o="";for(;n in e;)o+=e[n],n++;return o}function Ee(e,n,o,a,t){if(!(e===P.Partial||e===P.Answer))return;const r=a.messages[a.messages.length-1];if((r==null?void 0:r.role)!=="assistant")return;const{content:i,sequence:s}=n;e===P.Partial?o[s]=i:o.answer=i;const m=Re(o);(r.content!==m||e===P.Answer)&&(r.content=m,t==null||t([...a.messages],e))}async function $e(e,n){var W,I,y;let o={},a=!0;const t={messages:[],chatMode:n.mode||_.Functional},r=n.baseURL||z,i=n.wsURL||ce,s=n.mixpanelKey||de,m=te(n.auth,r,n.callbacks.onError),d=await m.getById(e);t.messages=se(d),(I=(W=n.callbacks).onNewMessage)==null||I.call(W,[...t.messages],"answer");const l=Me({token:s,agent:d,...n});l.track("agent-sdk",{event:"loaded",..._e(d)});const v={onMessage:(c,w)=>{var h,g;if("content"in w)Ee(c,w,o,t,n.callbacks.onNewMessage),c===P.Answer&&l.track("agent-message-received",{messages:t.messages.length,mode:t.chatMode});else{const u=x,f=[u.StreamVideoDone,u.StreamVideoError,u.StreamVideoRejected],S=[u.StreamFailed,u.StreamVideoError,u.StreamVideoRejected],A=ke(w,d,{mode:t.chatMode});if(c=c,c===u.StreamVideoCreated)l.linkTrack("agent-video",A,u.StreamVideoCreated,["start"]);else if(f.includes(c)){const M=c.split("/")[1];l.track("agent-video",{...A,event:M})}S.includes(c)&&((g=(h=n.callbacks).onError)==null||g.call(h,new Error(`Stream failed with event ${c}`),{data:w})),w.event===u.StreamDone&&k()}}};async function $(c){var S,A,M,R,U;D=0,c&&!a&&(delete t.chat,t.messages=se(d),(A=(S=n.callbacks).onNewMessage)==null||A.call(S,[...t.messages],"answer"));const w=n.mode===_.DirectPlayback?Promise.resolve(void 0):we(n.auth,i,v),h=Ie(d,n,m,l,t.chat).catch(T=>{throw K(_.Maintenance),T}),[g,{streamingManager:u,chat:f}]=await Promise.all([w,h]);f&&f.id!==((M=t.chat)==null?void 0:M.id)&&((U=(R=n.callbacks).onNewChat)==null||U.call(R,f.id)),t.streamingManager=u,t.socketManager=g,t.chat=f,a=!1,K((f==null?void 0:f.chat_mode)??n.mode??_.Functional)}async function k(){var c,w,h,g;(c=t.socketManager)==null||c.disconnect(),await((w=t.streamingManager)==null?void 0:w.disconnect()),delete t.streamingManager,delete t.socketManager,(g=(h=n.callbacks).onConnectionStateChange)==null||g.call(h,C.Disconnected)}async function K(c){var w,h;c!==t.chatMode&&(l.track("agent-mode-change",{mode:c}),t.chatMode=c,t.chatMode!==_.Functional&&await k(),(h=(w=n.callbacks).onModeChange)==null||h.call(w,c))}return{agent:d,starterMessages:((y=d.knowledge)==null?void 0:y.starter_message)||[],changeMode:K,async connect(){var c;await $(!0),l.track("agent-chat",{event:"connect",chatId:(c=t.chat)==null?void 0:c.id,agentId:d.id,mode:t.chatMode})},async reconnect(){var c;await k(),await $(!1),l.track("agent-chat",{event:"reconnect",chatId:(c=t.chat)==null?void 0:c.id,agentId:d.id,mode:t.chatMode})},async disconnect(){var c;await k(),l.track("agent-chat",{event:"disconnect",chatId:(c=t.chat)==null?void 0:c.id,agentId:d.id,mode:t.chatMode})},async chat(c){var h,g,u,f,S,A;const w=F();o={};try{if(D=Date.now(),n.mode===_.DirectPlayback)throw new Error("Direct playback is enabled, chat is disabled");if(c.length>=800)throw new Error("Message cannot be more than 800 characters");if(c.length===0)throw new Error("Message cannot be empty");if(t.chatMode===_.Maintenance)throw new Error("Chat is in maintenance mode");if(![_.TextOnly,_.Playground].includes(t.chatMode))if(t.streamingManager){if(!t.chat)throw new Error("Chat is not initialized")}else throw new Error("Streaming manager is not initialized");t.messages.push({id:F(),role:"user",content:c,created_at:new Date(D).toISOString()}),(g=(h=n.callbacks).onNewMessage)==null||g.call(h,[...t.messages],"user"),t.chat||(t.chat=await ie(d.id,m,l,t.chatMode),(f=(u=n.callbacks).onNewChat)==null||f.call(u,t.chat.id));const M={id:w,role:"assistant",content:"",created_at:new Date().toISOString(),matches:[]},R=[...t.messages];t.messages.push(M);const U=L=>{var V,oe;return m.chat(d.id,L,{sessionId:(V=t.streamingManager)==null?void 0:V.sessionId,streamId:(oe=t.streamingManager)==null?void 0:oe.streamId,chatMode:t.chatMode,messages:R},ae(t.chatMode))},T=await U(t.chat.id).catch(async L=>{var V;if(!((V=L==null?void 0:L.message)!=null&&V.includes("missing or invalid session_id")))throw L;return await k(),await $(!1),U(t.chat.id)});return l.track("agent-message-send",{event:"success",mode:t.chatMode,messages:t.messages.length+1}),M.context=T.context,M.matches=T.matches,T.result&&(M.content=T.result,l.track("agent-message-received",{latency:Date.now()-D,mode:t.chatMode,messages:t.messages.length}),(A=(S=n.callbacks).onNewMessage)==null||A.call(S,[...t.messages],"answer")),T}catch(M){throw t.messages[t.messages.length-1].id===w&&t.messages.pop(),l.track("agent-message-send",{event:"error",mode:t.chatMode,messages:t.messages.length}),M}},rate(c,w,h){var f,S,A,M;const g=t.messages.find(R=>R.id===c);if(t.chat){if(!g)throw new Error("Message not found")}else throw new Error("Chat is not initialized");const u=((f=g.matches)==null?void 0:f.map(R=>[R.document_id,R.id]))??[];return l.track("agent-rate",{event:h?"update":"create",thumb:w===1?"up":"down",knowledge_id:((S=d.knowledge)==null?void 0:S.id)??"",mode:t.chatMode,matches:u,score:w}),h?m.updateRating(d.id,t.chat.id,h,{knowledge_id:((A=d.knowledge)==null?void 0:A.id)??"",message_id:c,matches:u,score:w}):m.createRating(d.id,t.chat.id,{knowledge_id:((M=d.knowledge)==null?void 0:M.id)??"",message_id:c,matches:u,score:w})},deleteRate(c){var w;if(!t.chat)throw new Error("Chat is not initialized");return l.track("agent-rate-delete",{type:"text",chat_id:(w=t.chat)==null?void 0:w.id,id:c,mode:t.chatMode}),m.deleteRating(d.id,t.chat.id,c)},speak(c){if(!t.streamingManager)throw new Error("Please connect to the agent first");function w(){if(typeof c=="string"){if(!d.presenter.voice)throw new Error("Presenter voice is not initialized");return{type:"text",provider:d.presenter.voice,input:c,ssml:!1}}if(c.type==="text"&&!c.provider){if(!d.presenter.voice)throw new Error("Presenter voice is not initialized");return{type:"text",provider:d.presenter.voice,input:c.input,ssml:c.ssml}}return c}const h=w();return l.track("agent-speak",h),t.streamingManager.speak({script:h})}}}function Pe(e,n,o){const{getById:a}=te(n,o||z);return a(e)}p.ChatMode=_,p.ChatProgress=P,p.ConnectionState=C,p.DocumentType=Y,p.KnowledgeType=X,p.PlanGroup=H,p.Providers=Z,p.RateState=q,p.StreamEvents=x,p.StreamingState=b,p.Subject=J,p.UserPlan=O,p.VideoType=N,p.VoiceAccess=G,p.createAgentManager=$e,p.getAgent=Pe,Object.defineProperty(p,Symbol.toStringTag,{value:"Module"})});
|
|
@@ -24,17 +24,6 @@ interface ClipConfig {
|
|
|
24
24
|
result_format?: 'mp4' | 'gif' | 'mov' | 'webm';
|
|
25
25
|
}
|
|
26
26
|
export interface CreateClipStreamRequest {
|
|
27
|
-
/**
|
|
28
|
-
* ID of the selected presenter.
|
|
29
|
-
* @example "rian-lZC6MmWfC1"
|
|
30
|
-
*/
|
|
31
|
-
presenter_id: string;
|
|
32
|
-
/**
|
|
33
|
-
* ID of the selected driver.
|
|
34
|
-
* If not provided a driver video will be selected for you from the predefined drivers bank.
|
|
35
|
-
* @example "mXra4jY38i"
|
|
36
|
-
*/
|
|
37
|
-
driver_id?: string;
|
|
38
27
|
/**
|
|
39
28
|
* Defines the video codec to be used in the stream.
|
|
40
29
|
* When set to on: VP8 will be used.
|
|
@@ -7,7 +7,7 @@ export declare function getAnaliticsInfo(agent: Agent): {
|
|
|
7
7
|
agentType: "talk" | "clip";
|
|
8
8
|
agentVoice: {
|
|
9
9
|
voiceId: string | undefined;
|
|
10
|
-
provider: import('../types/
|
|
10
|
+
provider: import('../types/index').Providers | undefined;
|
|
11
11
|
};
|
|
12
12
|
};
|
|
13
13
|
export declare function getStreamAnalyticsProps(data: any, agent: any, additionalProps: Record<string, any>): any;
|
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.118",
|
|
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
|
+
}
|