@d-id/client-sdk 1.0.19-beta.111 → 1.0.19-beta.112
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 +168 -161
- package/dist/index.umd.cjs +1 -1
- package/dist/src/createStreamingManager.d.ts +1 -1
- package/dist/src/types/stream/stream.d.ts +4 -1
- package/package.json +1 -1
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 || {}), I = /* @__PURE__ */ ((e) => (e.Functional = "Functional", e.TextOnly = "TextOnly", e.Maintenance = "Maintenance", e.Playground = "Playground", e.DirectPlayback = "DirectPlayback", e))(I || {}), 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 || {}), _ = /* @__PURE__ */ ((e) => (e.Clip = "clip", e.Talk = "talk", e))(_ || {}), N = /* @__PURE__ */ ((e) => (e.Start = "START", e.Stop = "STOP", e))(N || {}), 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 || {}), v = /* @__PURE__ */ ((e) => (e.New = "new", e.Fail = "fail", e.Connected = "connected", e.Connecting = "connecting", e.Closed = "closed", e.Completed = "completed", e.Disconnected = "disconnected", e))(v || {}), 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");
|
|
@@ -91,8 +91,8 @@ function X(e, r = z, s) {
|
|
|
91
91
|
createRating(t, n, i, o) {
|
|
92
92
|
return a.post(`/${t}/chat/${n}/ratings`, i, o);
|
|
93
93
|
},
|
|
94
|
-
updateRating(t, n, i, o,
|
|
95
|
-
return a.patch(`/${t}/chat/${n}/ratings/${i}`, o,
|
|
94
|
+
updateRating(t, n, i, o, u) {
|
|
95
|
+
return a.patch(`/${t}/chat/${n}/ratings/${i}`, o, u);
|
|
96
96
|
},
|
|
97
97
|
deleteRating(t, n, i, o) {
|
|
98
98
|
return a.delete(`/${t}/chat/${n}/ratings/${i}`, o);
|
|
@@ -100,7 +100,7 @@ function X(e, r = z, s) {
|
|
|
100
100
|
};
|
|
101
101
|
}
|
|
102
102
|
const le = (e) => new Promise((r) => setTimeout(r, e));
|
|
103
|
-
function
|
|
103
|
+
function ue(e) {
|
|
104
104
|
return new Promise((r, s) => {
|
|
105
105
|
const {
|
|
106
106
|
callbacks: a,
|
|
@@ -109,24 +109,24 @@ function ge(e) {
|
|
|
109
109
|
} = e, {
|
|
110
110
|
onMessage: i = null,
|
|
111
111
|
onOpen: o = null,
|
|
112
|
-
onClose:
|
|
112
|
+
onClose: u = null,
|
|
113
113
|
onError: d = null
|
|
114
114
|
} = a || {}, l = new WebSocket(`${t}?authorization=${j(n)}`);
|
|
115
|
-
l.onmessage = i, l.onclose =
|
|
115
|
+
l.onmessage = i, l.onclose = u, l.onerror = (C) => {
|
|
116
116
|
console.error(C), d == null || d("Websocket failed to connect", C), s(C);
|
|
117
117
|
}, l.onopen = (C) => {
|
|
118
118
|
o == null || o(C), r(l);
|
|
119
119
|
};
|
|
120
120
|
});
|
|
121
121
|
}
|
|
122
|
-
async function
|
|
122
|
+
async function me(e) {
|
|
123
123
|
const {
|
|
124
124
|
retries: r = 1
|
|
125
125
|
} = e;
|
|
126
126
|
let s = null;
|
|
127
127
|
for (let a = 0; (s == null ? void 0 : s.readyState) !== WebSocket.OPEN; a++)
|
|
128
128
|
try {
|
|
129
|
-
s = await
|
|
129
|
+
s = await ue(e);
|
|
130
130
|
} catch (t) {
|
|
131
131
|
if (a === r)
|
|
132
132
|
throw t;
|
|
@@ -134,8 +134,8 @@ async function ue(e) {
|
|
|
134
134
|
}
|
|
135
135
|
return s;
|
|
136
136
|
}
|
|
137
|
-
async function
|
|
138
|
-
const a = s != null && s.onMessage ? [s.onMessage] : [], t = await
|
|
137
|
+
async function ge(e, r, s) {
|
|
138
|
+
const a = s != null && s.onMessage ? [s.onMessage] : [], t = await me({
|
|
139
139
|
auth: e,
|
|
140
140
|
host: r,
|
|
141
141
|
callbacks: {
|
|
@@ -163,34 +163,34 @@ function he(e, r, s, a) {
|
|
|
163
163
|
compatibility_mode: n.compatibility_mode,
|
|
164
164
|
stream_warmup: n.stream_warmup,
|
|
165
165
|
session_timeout: n.session_timeout,
|
|
166
|
-
type:
|
|
166
|
+
type: _.Clip
|
|
167
167
|
});
|
|
168
168
|
},
|
|
169
169
|
startConnection(n, i, o) {
|
|
170
170
|
return t.post(`/streams/${n}/sdp`, {
|
|
171
171
|
session_id: o,
|
|
172
172
|
answer: i,
|
|
173
|
-
type:
|
|
173
|
+
type: _.Clip
|
|
174
174
|
});
|
|
175
175
|
},
|
|
176
176
|
addIceCandidate(n, i, o) {
|
|
177
177
|
return t.post(`/streams/${n}/ice`, {
|
|
178
178
|
session_id: o,
|
|
179
179
|
...i,
|
|
180
|
-
type:
|
|
180
|
+
type: _.Clip
|
|
181
181
|
});
|
|
182
182
|
},
|
|
183
183
|
sendStreamRequest(n, i, o) {
|
|
184
184
|
return t.post(`/streams/${n}`, {
|
|
185
185
|
session_id: i,
|
|
186
186
|
...o,
|
|
187
|
-
type:
|
|
187
|
+
type: _.Clip
|
|
188
188
|
});
|
|
189
189
|
},
|
|
190
190
|
close(n, i) {
|
|
191
191
|
return t.delete(`/streams/${n}`, {
|
|
192
192
|
session_id: i,
|
|
193
|
-
type:
|
|
193
|
+
type: _.Clip
|
|
194
194
|
});
|
|
195
195
|
}
|
|
196
196
|
};
|
|
@@ -208,53 +208,58 @@ function fe(e, r, s, a) {
|
|
|
208
208
|
stream_warmup: n.stream_warmup,
|
|
209
209
|
output_resolution: n.output_resolution,
|
|
210
210
|
session_timeout: n.session_timeout,
|
|
211
|
-
type:
|
|
211
|
+
type: _.Talk
|
|
212
212
|
}, i);
|
|
213
213
|
},
|
|
214
|
-
startConnection(n, i, o,
|
|
214
|
+
startConnection(n, i, o, u) {
|
|
215
215
|
return t.post(`/streams/${n}/sdp`, {
|
|
216
216
|
session_id: o,
|
|
217
217
|
answer: i,
|
|
218
|
-
type:
|
|
219
|
-
},
|
|
218
|
+
type: _.Talk
|
|
219
|
+
}, u);
|
|
220
220
|
},
|
|
221
|
-
addIceCandidate(n, i, o,
|
|
221
|
+
addIceCandidate(n, i, o, u) {
|
|
222
222
|
return t.post(`/streams/${n}/ice`, {
|
|
223
223
|
session_id: o,
|
|
224
224
|
...i,
|
|
225
|
-
type:
|
|
226
|
-
},
|
|
225
|
+
type: _.Talk
|
|
226
|
+
}, u);
|
|
227
227
|
},
|
|
228
|
-
sendStreamRequest(n, i, o,
|
|
228
|
+
sendStreamRequest(n, i, o, u) {
|
|
229
229
|
return t.post(`/streams/${n}`, {
|
|
230
230
|
session_id: i,
|
|
231
231
|
...o,
|
|
232
|
-
type:
|
|
233
|
-
},
|
|
232
|
+
type: _.Talk
|
|
233
|
+
}, u);
|
|
234
234
|
},
|
|
235
235
|
close(n, i, o) {
|
|
236
236
|
return t.delete(`/streams/${n}`, {
|
|
237
237
|
session_id: i,
|
|
238
|
-
type:
|
|
238
|
+
type: _.Talk
|
|
239
239
|
}, o);
|
|
240
240
|
}
|
|
241
241
|
};
|
|
242
242
|
}
|
|
243
243
|
let Y = !1;
|
|
244
|
-
const
|
|
244
|
+
const $ = (e, r) => Y && console.log(e, r), pe = (window.RTCPeerConnection || window.webkitRTCPeerConnection || window.mozRTCPeerConnection).bind(window);
|
|
245
245
|
function W(e) {
|
|
246
246
|
switch (e) {
|
|
247
247
|
case "connected":
|
|
248
|
-
return
|
|
248
|
+
return v.Connected;
|
|
249
249
|
case "checking":
|
|
250
|
-
return
|
|
250
|
+
return v.Connecting;
|
|
251
251
|
case "failed":
|
|
252
|
-
return
|
|
252
|
+
return v.Fail;
|
|
253
253
|
case "new":
|
|
254
|
+
return v.New;
|
|
254
255
|
case "closed":
|
|
256
|
+
return v.Closed;
|
|
255
257
|
case "disconnected":
|
|
258
|
+
return v.Disconnected;
|
|
259
|
+
case "completed":
|
|
260
|
+
return v.Completed;
|
|
256
261
|
default:
|
|
257
|
-
return
|
|
262
|
+
return v.New;
|
|
258
263
|
}
|
|
259
264
|
}
|
|
260
265
|
function ye() {
|
|
@@ -268,7 +273,7 @@ function ye() {
|
|
|
268
273
|
return !1;
|
|
269
274
|
};
|
|
270
275
|
}
|
|
271
|
-
function
|
|
276
|
+
function Ce(e, r) {
|
|
272
277
|
const a = Math.max(Math.ceil(10), 1);
|
|
273
278
|
let t = 0, n = !1;
|
|
274
279
|
const i = ye();
|
|
@@ -277,23 +282,23 @@ function ve(e, r) {
|
|
|
277
282
|
i(o) ? (t = 0, n || (r == null || r(N.Start), n = !0)) : n && (t++, t >= a && (r == null || r(N.Stop), n = !1));
|
|
278
283
|
}, 100);
|
|
279
284
|
}
|
|
280
|
-
async function
|
|
285
|
+
async function ve(e, r, {
|
|
281
286
|
debug: s = !1,
|
|
282
287
|
callbacks: a,
|
|
283
288
|
auth: t,
|
|
284
|
-
|
|
285
|
-
|
|
289
|
+
baseURL: n = z,
|
|
290
|
+
warmup: i
|
|
286
291
|
}) {
|
|
287
292
|
Y = s;
|
|
288
293
|
let o;
|
|
289
294
|
const {
|
|
290
|
-
startConnection:
|
|
295
|
+
startConnection: u,
|
|
291
296
|
sendStreamRequest: d,
|
|
292
297
|
close: l,
|
|
293
298
|
createStream: C,
|
|
294
|
-
addIceCandidate:
|
|
295
|
-
} = r.videoType ===
|
|
296
|
-
id:
|
|
299
|
+
addIceCandidate: A
|
|
300
|
+
} = r.videoType === _.Clip ? he(t, n, e, a.onError) : fe(t, n, e, a.onError), {
|
|
301
|
+
id: M,
|
|
297
302
|
offer: O,
|
|
298
303
|
ice_servers: K,
|
|
299
304
|
session_id: R
|
|
@@ -302,62 +307,62 @@ async function Ce(e, r, {
|
|
|
302
307
|
}), c = p.createDataChannel("JanusDataChannel");
|
|
303
308
|
if (!R)
|
|
304
309
|
throw new Error("Could not create session_id");
|
|
305
|
-
const
|
|
306
|
-
p.onicecandidate = (
|
|
307
|
-
|
|
308
|
-
candidate:
|
|
309
|
-
sdpMid:
|
|
310
|
-
sdpMLineIndex:
|
|
311
|
-
}, R) :
|
|
310
|
+
const g = Ce(p, a.onVideoStateChange);
|
|
311
|
+
p.onicecandidate = (m) => {
|
|
312
|
+
$("peerConnection.onicecandidate", m), m.candidate && m.candidate.sdpMid && m.candidate.sdpMLineIndex !== null ? A(M, {
|
|
313
|
+
candidate: m.candidate.candidate,
|
|
314
|
+
sdpMid: m.candidate.sdpMid,
|
|
315
|
+
sdpMLineIndex: m.candidate.sdpMLineIndex
|
|
316
|
+
}, R) : A(M, {
|
|
312
317
|
candidate: null
|
|
313
318
|
}, R);
|
|
314
319
|
}, p.oniceconnectionstatechange = () => {
|
|
315
320
|
var h;
|
|
316
|
-
|
|
317
|
-
const
|
|
318
|
-
|
|
321
|
+
$("peerConnection.oniceconnectionstatechange => " + p.iceConnectionState);
|
|
322
|
+
const m = W(p.iceConnectionState);
|
|
323
|
+
m === v.Connected ? o = setTimeout(() => {
|
|
319
324
|
var w;
|
|
320
|
-
(w = a.onConnectionStateChange) == null
|
|
321
|
-
}, 5e3) : (clearTimeout(o), (h = a.onConnectionStateChange) == null || h.call(a,
|
|
322
|
-
}, p.ontrack = (
|
|
325
|
+
return (w = a.onConnectionStateChange) == null ? void 0 : w.call(a, v.Connected);
|
|
326
|
+
}, i ? 0 : 5e3) : (clearTimeout(o), (h = a.onConnectionStateChange) == null || h.call(a, m));
|
|
327
|
+
}, p.ontrack = (m) => {
|
|
323
328
|
var h;
|
|
324
|
-
|
|
325
|
-
}, c.onmessage = (
|
|
329
|
+
$("peerConnection.ontrack", m), (h = a.onSrcObjectReady) == null || h.call(a, m.streams[0]);
|
|
330
|
+
}, c.onmessage = (m) => {
|
|
326
331
|
var h;
|
|
327
332
|
if (c.readyState === "open") {
|
|
328
|
-
const [w, f] =
|
|
329
|
-
w === U.StreamReady && (clearTimeout(o), (h = a.onConnectionStateChange) == null || h.call(a,
|
|
333
|
+
const [w, f] = m.data.split(":");
|
|
334
|
+
w === U.StreamReady && (clearTimeout(o), (h = a.onConnectionStateChange) == null || h.call(a, v.Connected));
|
|
330
335
|
}
|
|
331
|
-
}, await p.setRemoteDescription(O),
|
|
336
|
+
}, await p.setRemoteDescription(O), $("set remote description OK");
|
|
332
337
|
const y = await p.createAnswer();
|
|
333
|
-
return
|
|
338
|
+
return $("create answer OK"), await p.setLocalDescription(y), $("set local description OK"), await u(M, y, R), $("start connection OK"), {
|
|
334
339
|
/**
|
|
335
340
|
* Method to send request to server to get clip or talk depend on you payload
|
|
336
341
|
* @param payload
|
|
337
342
|
*/
|
|
338
|
-
speak(
|
|
339
|
-
return d(
|
|
343
|
+
speak(m) {
|
|
344
|
+
return d(M, R, m);
|
|
340
345
|
},
|
|
341
346
|
/**
|
|
342
347
|
* Method to close RTC connection
|
|
343
348
|
*/
|
|
344
349
|
async disconnect() {
|
|
345
|
-
var
|
|
346
|
-
if (
|
|
350
|
+
var m, h, w;
|
|
351
|
+
if (M) {
|
|
347
352
|
if (p) {
|
|
348
|
-
if (W(p.iceConnectionState) ===
|
|
349
|
-
(
|
|
353
|
+
if (W(p.iceConnectionState) === v.New) {
|
|
354
|
+
(m = a.onVideoStateChange) == null || m.call(a, N.Stop), clearInterval(g);
|
|
350
355
|
return;
|
|
351
356
|
}
|
|
352
357
|
p.close(), p.oniceconnectionstatechange = null, p.onnegotiationneeded = null, p.onicecandidate = null, p.ontrack = null;
|
|
353
358
|
}
|
|
354
359
|
try {
|
|
355
|
-
await l(
|
|
360
|
+
await l(M, R).catch((f) => {
|
|
356
361
|
});
|
|
357
362
|
} catch (f) {
|
|
358
|
-
|
|
363
|
+
$("Error on close stream connection", f);
|
|
359
364
|
}
|
|
360
|
-
(h = a.onConnectionStateChange) == null || h.call(a,
|
|
365
|
+
(h = a.onConnectionStateChange) == null || h.call(a, v.New), (w = a.onVideoStateChange) == null || w.call(a, N.Stop), clearInterval(g);
|
|
361
366
|
}
|
|
362
367
|
},
|
|
363
368
|
/**
|
|
@@ -367,7 +372,7 @@ async function Ce(e, r, {
|
|
|
367
372
|
/**
|
|
368
373
|
* Id of current RTC stream
|
|
369
374
|
*/
|
|
370
|
-
streamId:
|
|
375
|
+
streamId: M
|
|
371
376
|
};
|
|
372
377
|
}
|
|
373
378
|
let V = {};
|
|
@@ -411,7 +416,7 @@ function _e(e) {
|
|
|
411
416
|
}])
|
|
412
417
|
})
|
|
413
418
|
};
|
|
414
|
-
return fetch("https://api-js.mixpanel.com/track/?verbose=1&ip=1", o).then((
|
|
419
|
+
return fetch("https://api-js.mixpanel.com/track/?verbose=1&ip=1", o).then((u) => u.json()).catch((u) => console.error(u));
|
|
415
420
|
},
|
|
416
421
|
linkTrack(a, t, n, i) {
|
|
417
422
|
V[a] || (V[a] = {
|
|
@@ -438,8 +443,8 @@ function Se(e) {
|
|
|
438
443
|
const r = () => /Mobi|Android/i.test(navigator.userAgent) ? "Mobile" : "Desktop";
|
|
439
444
|
return {
|
|
440
445
|
$os: `${(() => {
|
|
441
|
-
const
|
|
442
|
-
return
|
|
446
|
+
const u = navigator.platform;
|
|
447
|
+
return u.toLowerCase().includes("win") ? "Windows" : u.toLowerCase().includes("mac") ? "Mac OS X" : u.toLowerCase().includes("linux") ? "Linux" : "Unknown";
|
|
443
448
|
})()}`,
|
|
444
449
|
isMobile: `${r() == "Mobile"}`,
|
|
445
450
|
browser: navigator.userAgent,
|
|
@@ -481,13 +486,13 @@ function Me(e, r, s) {
|
|
|
481
486
|
};
|
|
482
487
|
}
|
|
483
488
|
const ke = 1080;
|
|
484
|
-
let
|
|
489
|
+
let T = 0;
|
|
485
490
|
function Ie(e, r) {
|
|
486
491
|
var s, a, t;
|
|
487
492
|
if (e.presenter) {
|
|
488
|
-
if (e.presenter.type ===
|
|
493
|
+
if (e.presenter.type === _.Clip)
|
|
489
494
|
return {
|
|
490
|
-
videoType:
|
|
495
|
+
videoType: _.Clip,
|
|
491
496
|
driver_id: e.presenter.driver_id,
|
|
492
497
|
presenter_id: e.presenter.presenter_id,
|
|
493
498
|
stream_warmup: !0
|
|
@@ -495,7 +500,7 @@ function Ie(e, r) {
|
|
|
495
500
|
} else
|
|
496
501
|
throw new Error("Presenter is not initialized");
|
|
497
502
|
return {
|
|
498
|
-
videoType:
|
|
503
|
+
videoType: _.Talk,
|
|
499
504
|
source_url: e.presenter.source_url,
|
|
500
505
|
session_timeout: (s = r == null ? void 0 : r.streamOptions) == null ? void 0 : s.session_timeout,
|
|
501
506
|
stream_warmup: ((a = r == null ? void 0 : r.streamOptions) == null ? void 0 : a.stream_warmup) ?? !0,
|
|
@@ -504,7 +509,7 @@ function Ie(e, r) {
|
|
|
504
509
|
};
|
|
505
510
|
}
|
|
506
511
|
function Z(e) {
|
|
507
|
-
return e ===
|
|
512
|
+
return e === I.Playground ? {
|
|
508
513
|
headers: {
|
|
509
514
|
[we]: "true"
|
|
510
515
|
}
|
|
@@ -533,26 +538,28 @@ async function Q(e, r, s, a) {
|
|
|
533
538
|
}
|
|
534
539
|
function Re(e, r, s, a, t) {
|
|
535
540
|
return new Promise(async (n, i) => {
|
|
536
|
-
|
|
537
|
-
|
|
541
|
+
var u;
|
|
542
|
+
T = 0;
|
|
543
|
+
const o = await ve(e.id, Ie(e, r), {
|
|
538
544
|
...r,
|
|
539
545
|
analytics: a,
|
|
546
|
+
warmup: (u = r.streamOptions) == null ? void 0 : u.stream_warmup,
|
|
540
547
|
callbacks: {
|
|
541
548
|
...r.callbacks,
|
|
542
|
-
onConnectionStateChange: async (
|
|
543
|
-
var
|
|
544
|
-
|
|
545
|
-
i(
|
|
549
|
+
onConnectionStateChange: async (d) => {
|
|
550
|
+
var l, C;
|
|
551
|
+
d === v.Connected && (!t && r.mode !== I.DirectPlayback && (t = await Q(e.id, s, a, r.mode).catch((A) => {
|
|
552
|
+
i(A);
|
|
546
553
|
})), o ? n({
|
|
547
554
|
chat: t,
|
|
548
555
|
streamingManager: o
|
|
549
|
-
}) : t && i(new Error("Something went wrong while initializing the manager"))), (
|
|
556
|
+
}) : t && i(new Error("Something went wrong while initializing the manager"))), (C = (l = r.callbacks).onConnectionStateChange) == null || C.call(l, d);
|
|
550
557
|
},
|
|
551
|
-
onVideoStateChange(
|
|
552
|
-
var
|
|
553
|
-
(
|
|
558
|
+
onVideoStateChange(d) {
|
|
559
|
+
var l, C;
|
|
560
|
+
(C = (l = r.callbacks).onVideoStateChange) == null || C.call(l, d), T > 0 && d === N.Start && a.linkTrack("agent-video", {
|
|
554
561
|
event: "start",
|
|
555
|
-
latency: Date.now() -
|
|
562
|
+
latency: Date.now() - T
|
|
556
563
|
}, "start", [U.StreamVideoCreated]);
|
|
557
564
|
}
|
|
558
565
|
}
|
|
@@ -592,16 +599,16 @@ function Ae(e, r, s, a, t) {
|
|
|
592
599
|
sequence: o
|
|
593
600
|
} = r;
|
|
594
601
|
e === b.Partial ? s[o] = i : s.answer = i;
|
|
595
|
-
const
|
|
596
|
-
(n.content !==
|
|
602
|
+
const u = Ee(s);
|
|
603
|
+
(n.content !== u || e === b.Answer) && (n.content = u, t == null || t(a.messages, e));
|
|
597
604
|
}
|
|
598
605
|
async function $e(e, r) {
|
|
599
606
|
var K, R, p;
|
|
600
607
|
let s = {}, a = !0;
|
|
601
608
|
const t = {
|
|
602
609
|
messages: [],
|
|
603
|
-
chatMode: r.mode ||
|
|
604
|
-
}, n = r.baseURL || z, i = r.wsURL || oe, o = r.mixpanelKey || ce,
|
|
610
|
+
chatMode: r.mode || I.Functional
|
|
611
|
+
}, n = r.baseURL || z, i = r.wsURL || oe, o = r.mixpanelKey || ce, u = X(r.auth, n, r.callbacks.onError), d = await u.getById(e);
|
|
605
612
|
t.messages = q(d), (R = (K = r.callbacks).onNewMessage) == null || R.call(K, t.messages, "answer");
|
|
606
613
|
const l = _e({
|
|
607
614
|
token: o,
|
|
@@ -613,52 +620,52 @@ async function $e(e, r) {
|
|
|
613
620
|
...Se(d)
|
|
614
621
|
});
|
|
615
622
|
const C = {
|
|
616
|
-
onMessage: (c,
|
|
617
|
-
var y,
|
|
618
|
-
if ("content" in
|
|
619
|
-
Ae(c,
|
|
623
|
+
onMessage: (c, g) => {
|
|
624
|
+
var y, m, h, w;
|
|
625
|
+
if ("content" in g)
|
|
626
|
+
Ae(c, g, s, t, r.callbacks.onNewMessage), c === b.Answer && l.track("agent-message-received", {
|
|
620
627
|
messages: t.messages.length,
|
|
621
628
|
mode: t.chatMode
|
|
622
629
|
});
|
|
623
630
|
else {
|
|
624
|
-
const f = U, E = [f.StreamVideoDone, f.StreamVideoError, f.StreamVideoRejected],
|
|
631
|
+
const f = U, E = [f.StreamVideoDone, f.StreamVideoError, f.StreamVideoRejected], S = [f.StreamFailed, f.StreamVideoError, f.StreamVideoRejected], k = Me(g, d, {
|
|
625
632
|
mode: t.chatMode
|
|
626
633
|
});
|
|
627
634
|
if (c = c, c === f.StreamVideoCreated)
|
|
628
|
-
l.linkTrack("agent-video",
|
|
635
|
+
l.linkTrack("agent-video", k, f.StreamVideoCreated, ["start"]);
|
|
629
636
|
else if (E.includes(c)) {
|
|
630
|
-
const
|
|
637
|
+
const P = c.split("/")[1];
|
|
631
638
|
l.track("agent-video", {
|
|
632
|
-
...
|
|
633
|
-
event:
|
|
639
|
+
...k,
|
|
640
|
+
event: P
|
|
634
641
|
});
|
|
635
642
|
}
|
|
636
|
-
|
|
637
|
-
data:
|
|
638
|
-
})),
|
|
643
|
+
S.includes(c) && ((m = (y = r.callbacks).onError) == null || m.call(y, new Error(`Stream failed with event ${c}`), {
|
|
644
|
+
data: g
|
|
645
|
+
})), g.event === f.StreamDone && ((w = (h = r.callbacks).onConnectionStateChange) == null || w.call(h, v.New));
|
|
639
646
|
}
|
|
640
647
|
}
|
|
641
648
|
};
|
|
642
|
-
async function
|
|
643
|
-
var f, E,
|
|
644
|
-
|
|
645
|
-
const
|
|
646
|
-
throw O(
|
|
647
|
-
}), [
|
|
649
|
+
async function A(c) {
|
|
650
|
+
var f, E, S, k, P;
|
|
651
|
+
T = 0, c && !a && (delete t.chat, t.messages = q(d), (E = (f = r.callbacks).onNewMessage) == null || E.call(f, t.messages, "answer"));
|
|
652
|
+
const g = r.mode === I.DirectPlayback ? Promise.resolve(void 0) : ge(r.auth, i, C), y = Re(d, r, u, l, t.chat).catch((D) => {
|
|
653
|
+
throw O(I.Maintenance), D;
|
|
654
|
+
}), [m, {
|
|
648
655
|
streamingManager: h,
|
|
649
656
|
chat: w
|
|
650
|
-
}] = await Promise.all([
|
|
651
|
-
w && w.id !== ((
|
|
657
|
+
}] = await Promise.all([g, y]);
|
|
658
|
+
w && w.id !== ((S = t.chat) == null ? void 0 : S.id) && ((P = (k = r.callbacks).onNewChat) == null || P.call(k, w.id)), t.streamingManager = h, t.socketManager = m, t.chat = w, a = !1, O((w == null ? void 0 : w.chat_mode) ?? r.mode ?? I.Functional);
|
|
652
659
|
}
|
|
653
|
-
async function
|
|
654
|
-
var c,
|
|
655
|
-
(c = t.socketManager) == null || c.disconnect(), await ((
|
|
660
|
+
async function M() {
|
|
661
|
+
var c, g;
|
|
662
|
+
(c = t.socketManager) == null || c.disconnect(), await ((g = t.streamingManager) == null ? void 0 : g.disconnect()), delete t.streamingManager, delete t.socketManager;
|
|
656
663
|
}
|
|
657
664
|
async function O(c) {
|
|
658
|
-
var
|
|
665
|
+
var g, y;
|
|
659
666
|
c !== t.chatMode && (l.track("agent-mode-change", {
|
|
660
667
|
mode: c
|
|
661
|
-
}), t.chatMode = c, t.chatMode !==
|
|
668
|
+
}), t.chatMode = c, t.chatMode !== I.Functional && await M(), (y = (g = r.callbacks).onModeChange) == null || y.call(g, c));
|
|
662
669
|
}
|
|
663
670
|
return {
|
|
664
671
|
agent: d,
|
|
@@ -666,7 +673,7 @@ async function $e(e, r) {
|
|
|
666
673
|
changeMode: O,
|
|
667
674
|
async connect() {
|
|
668
675
|
var c;
|
|
669
|
-
await
|
|
676
|
+
await A(!0), l.track("agent-chat", {
|
|
670
677
|
event: "connect",
|
|
671
678
|
chatId: (c = t.chat) == null ? void 0 : c.id,
|
|
672
679
|
agentId: d.id,
|
|
@@ -675,7 +682,7 @@ async function $e(e, r) {
|
|
|
675
682
|
},
|
|
676
683
|
async reconnect() {
|
|
677
684
|
var c;
|
|
678
|
-
await
|
|
685
|
+
await M(), await A(!1), l.track("agent-chat", {
|
|
679
686
|
event: "reconnect",
|
|
680
687
|
chatId: (c = t.chat) == null ? void 0 : c.id,
|
|
681
688
|
agentId: d.id,
|
|
@@ -684,7 +691,7 @@ async function $e(e, r) {
|
|
|
684
691
|
},
|
|
685
692
|
async disconnect() {
|
|
686
693
|
var c;
|
|
687
|
-
await
|
|
694
|
+
await M(), l.track("agent-chat", {
|
|
688
695
|
event: "disconnect",
|
|
689
696
|
chatId: (c = t.chat) == null ? void 0 : c.id,
|
|
690
697
|
agentId: d.id,
|
|
@@ -692,112 +699,112 @@ async function $e(e, r) {
|
|
|
692
699
|
});
|
|
693
700
|
},
|
|
694
701
|
async chat(c) {
|
|
695
|
-
var y,
|
|
696
|
-
const
|
|
702
|
+
var y, m, h, w, f, E;
|
|
703
|
+
const g = F();
|
|
697
704
|
s = {};
|
|
698
705
|
try {
|
|
699
|
-
if (
|
|
706
|
+
if (T = Date.now(), r.mode === I.DirectPlayback)
|
|
700
707
|
throw new Error("Direct playback is enabled, chat is disabled");
|
|
701
708
|
if (c.length >= 800)
|
|
702
709
|
throw new Error("Message cannot be more than 800 characters");
|
|
703
710
|
if (c.length === 0)
|
|
704
711
|
throw new Error("Message cannot be empty");
|
|
705
|
-
if (t.chatMode ===
|
|
712
|
+
if (t.chatMode === I.Maintenance)
|
|
706
713
|
throw new Error("Chat is in maintenance mode");
|
|
707
|
-
if (![
|
|
714
|
+
if (![I.TextOnly, I.Playground].includes(t.chatMode))
|
|
708
715
|
if (t.streamingManager) {
|
|
709
716
|
if (!t.chat)
|
|
710
717
|
throw new Error("Chat is not initialized");
|
|
711
718
|
} else
|
|
712
719
|
throw new Error("Streaming manager is not initialized");
|
|
713
|
-
t.chat || (t.chat = await Q(d.id,
|
|
720
|
+
t.chat || (t.chat = await Q(d.id, u, l, t.chatMode), (m = (y = r.callbacks).onNewChat) == null || m.call(y, t.chat.id)), t.messages.push({
|
|
714
721
|
id: F(),
|
|
715
722
|
role: "user",
|
|
716
723
|
content: c,
|
|
717
|
-
created_at: new Date(
|
|
724
|
+
created_at: new Date(T).toISOString()
|
|
718
725
|
}), (w = (h = r.callbacks).onNewMessage) == null || w.call(h, t.messages, "user");
|
|
719
|
-
const
|
|
720
|
-
id:
|
|
726
|
+
const S = {
|
|
727
|
+
id: g,
|
|
721
728
|
role: "assistant",
|
|
722
729
|
content: "",
|
|
723
730
|
created_at: (/* @__PURE__ */ new Date()).toISOString(),
|
|
724
731
|
matches: []
|
|
725
|
-
},
|
|
726
|
-
t.messages.push(
|
|
727
|
-
const
|
|
732
|
+
}, k = [...t.messages];
|
|
733
|
+
t.messages.push(S);
|
|
734
|
+
const P = (x) => {
|
|
728
735
|
var L, H;
|
|
729
|
-
return
|
|
736
|
+
return u.chat(d.id, x, {
|
|
730
737
|
sessionId: (L = t.streamingManager) == null ? void 0 : L.sessionId,
|
|
731
738
|
streamId: (H = t.streamingManager) == null ? void 0 : H.streamId,
|
|
732
739
|
chatMode: t.chatMode,
|
|
733
|
-
messages:
|
|
740
|
+
messages: k
|
|
734
741
|
}, Z(t.chatMode));
|
|
735
|
-
},
|
|
742
|
+
}, D = await P(t.chat.id).catch(async (x) => {
|
|
736
743
|
var L;
|
|
737
744
|
if (!((L = x == null ? void 0 : x.message) != null && L.includes("missing or invalid session_id")))
|
|
738
745
|
throw x;
|
|
739
|
-
return await
|
|
746
|
+
return await M(), await A(!1), P(t.chat.id);
|
|
740
747
|
});
|
|
741
748
|
return l.track("agent-message-send", {
|
|
742
749
|
event: "success",
|
|
743
750
|
mode: t.chatMode,
|
|
744
751
|
messages: t.messages.length + 1
|
|
745
|
-
}),
|
|
746
|
-
latency: Date.now() -
|
|
752
|
+
}), S.context = D.context, S.matches = D.matches, D.result && (S.content = D.result, l.track("agent-message-received", {
|
|
753
|
+
latency: Date.now() - T,
|
|
747
754
|
mode: t.chatMode,
|
|
748
755
|
messages: t.messages.length
|
|
749
|
-
}), (E = (f = r.callbacks).onNewMessage) == null || E.call(f, t.messages, "answer")),
|
|
750
|
-
} catch (
|
|
751
|
-
throw t.messages[t.messages.length - 1].id ===
|
|
756
|
+
}), (E = (f = r.callbacks).onNewMessage) == null || E.call(f, t.messages, "answer")), D;
|
|
757
|
+
} catch (S) {
|
|
758
|
+
throw t.messages[t.messages.length - 1].id === g && t.messages.pop(), l.track("agent-message-send", {
|
|
752
759
|
event: "error",
|
|
753
760
|
mode: t.chatMode,
|
|
754
761
|
messages: t.messages.length
|
|
755
|
-
}),
|
|
762
|
+
}), S;
|
|
756
763
|
}
|
|
757
764
|
},
|
|
758
|
-
rate(c,
|
|
759
|
-
var w, f, E,
|
|
760
|
-
const
|
|
765
|
+
rate(c, g, y) {
|
|
766
|
+
var w, f, E, S;
|
|
767
|
+
const m = t.messages.find((k) => k.id === c);
|
|
761
768
|
if (t.chat) {
|
|
762
|
-
if (!
|
|
769
|
+
if (!m)
|
|
763
770
|
throw new Error("Message not found");
|
|
764
771
|
} else
|
|
765
772
|
throw new Error("Chat is not initialized");
|
|
766
|
-
const h = ((w =
|
|
773
|
+
const h = ((w = m.matches) == null ? void 0 : w.map((k) => [k.document_id, k.id])) ?? [];
|
|
767
774
|
return l.track("agent-rate", {
|
|
768
775
|
event: y ? "update" : "create",
|
|
769
|
-
thumb:
|
|
776
|
+
thumb: g === 1 ? "up" : "down",
|
|
770
777
|
knowledge_id: ((f = d.knowledge) == null ? void 0 : f.id) ?? "",
|
|
771
778
|
mode: t.chatMode,
|
|
772
779
|
matches: h,
|
|
773
|
-
score:
|
|
774
|
-
}), y ?
|
|
780
|
+
score: g
|
|
781
|
+
}), y ? u.updateRating(d.id, t.chat.id, y, {
|
|
775
782
|
knowledge_id: ((E = d.knowledge) == null ? void 0 : E.id) ?? "",
|
|
776
783
|
message_id: c,
|
|
777
784
|
matches: h,
|
|
778
|
-
score:
|
|
779
|
-
}) :
|
|
780
|
-
knowledge_id: ((
|
|
785
|
+
score: g
|
|
786
|
+
}) : u.createRating(d.id, t.chat.id, {
|
|
787
|
+
knowledge_id: ((S = d.knowledge) == null ? void 0 : S.id) ?? "",
|
|
781
788
|
message_id: c,
|
|
782
789
|
matches: h,
|
|
783
|
-
score:
|
|
790
|
+
score: g
|
|
784
791
|
});
|
|
785
792
|
},
|
|
786
793
|
deleteRate(c) {
|
|
787
|
-
var
|
|
794
|
+
var g;
|
|
788
795
|
if (!t.chat)
|
|
789
796
|
throw new Error("Chat is not initialized");
|
|
790
797
|
return l.track("agent-rate-delete", {
|
|
791
798
|
type: "text",
|
|
792
|
-
chat_id: (
|
|
799
|
+
chat_id: (g = t.chat) == null ? void 0 : g.id,
|
|
793
800
|
id: c,
|
|
794
801
|
mode: t.chatMode
|
|
795
|
-
}),
|
|
802
|
+
}), u.deleteRating(d.id, t.chat.id, c);
|
|
796
803
|
},
|
|
797
804
|
speak(c) {
|
|
798
805
|
if (!t.streamingManager)
|
|
799
806
|
throw new Error("Please connect to the agent first");
|
|
800
|
-
function
|
|
807
|
+
function g() {
|
|
801
808
|
if (typeof c == "string") {
|
|
802
809
|
if (!d.presenter)
|
|
803
810
|
throw new Error("Presenter is not initialized");
|
|
@@ -810,7 +817,7 @@ async function $e(e, r) {
|
|
|
810
817
|
}
|
|
811
818
|
return c;
|
|
812
819
|
}
|
|
813
|
-
const y =
|
|
820
|
+
const y = g();
|
|
814
821
|
return l.track("agent-speak", y), t.streamingManager.speak({
|
|
815
822
|
script: y
|
|
816
823
|
});
|
|
@@ -824,9 +831,9 @@ function Pe(e, r, s) {
|
|
|
824
831
|
return a(e);
|
|
825
832
|
}
|
|
826
833
|
export {
|
|
827
|
-
|
|
834
|
+
I as ChatMode,
|
|
828
835
|
b as ChatProgress,
|
|
829
|
-
|
|
836
|
+
v as ConnectionState,
|
|
830
837
|
ae as DocumentType,
|
|
831
838
|
re as KnowledgeType,
|
|
832
839
|
ee as PlanGroup,
|
|
@@ -836,7 +843,7 @@ export {
|
|
|
836
843
|
N as StreamingState,
|
|
837
844
|
ne as Subject,
|
|
838
845
|
G as UserPlan,
|
|
839
|
-
|
|
846
|
+
_ as VideoType,
|
|
840
847
|
se as VoiceAccess,
|
|
841
848
|
$e as createAgentManager,
|
|
842
849
|
Pe as getAgent
|
package/dist/index.umd.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
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||{}),W=(e=>(e.TRIAL="deid-trial",e.PRO="deid-pro",e.ENTERPRISE="deid-enterprise",e.LITE="deid-lite",e.ADVANCED="deid-advanced",e.BUILD="deid-api-build",e.LAUNCH="deid-api-launch",e.SCALE="deid-api-scale",e))(W||{}),q=(e=>(e.Unrated="Unrated",e.Positive="Positive",e.Negative="Negative",e))(q||{}),M=(e=>(e.Functional="Functional",e.TextOnly="TextOnly",e.Maintenance="Maintenance",e.Playground="Playground",e.DirectPlayback="DirectPlayback",e))(M||{}),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||{}),C=(e=>(e.Clip="clip",e.Talk="talk",e))(C||{}),T=(e=>(e.Start="START",e.Stop="STOP",e))(T||{}),z=(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))(z||{}),k=(e=>(e.New="new",e.Fail="fail",e.Connected="connected",e.Connecting="connecting",e.Terminating="terminating",e))(k||{}),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 K="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 B(e,r=K,s){const i=async(t,n)=>{const a=await fetch(r+(t!=null&&t.startsWith("/")?t:`/${t}`),{...n,headers:{...n==null?void 0:n.headers,Authorization:ee(e),"Content-Type":"application/json"}});if(!a.ok){let o=await a.text().catch(()=>"Failed to fetch");throw s&&s(new Error(o),{url:t,options:n,headers:a.headers}),new Error(o)}return a.json()};return{get(t,n){return i(t,{...n,method:"GET"})},post(t,n,a){return i(t,{...a,body:JSON.stringify(n),method:"POST"})},delete(t,n,a){return i(t,{...a,body:JSON.stringify(n),method:"DELETE"})},patch(t,n,a){return i(t,{...a,body:JSON.stringify(n),method:"PATCH"})}}}function te(e,r=K,s){const i=B(e,`${r}/agents`,s);return{create(t,n){return i.post("/",t,n)},getAgents(t,n){return i.get(`/${t?`?tag=${t}`:""}`,n).then(a=>a??[])},getById(t,n){return i.get(`/${t}`,n)},delete(t,n){return i.delete(`/${t}`,void 0,n)},update(t,n,a){return i.patch(`/${t}`,n,a)},newChat(t,n){return i.post(`/${t}/chat`,void 0,n)},chat(t,n,a,o){return i.post(`/${t}/chat/${n}`,a,o)},createRating(t,n,a,o){return i.post(`/${t}/chat/${n}/ratings`,a,o)},updateRating(t,n,a,o,g){return i.patch(`/${t}/chat/${n}/ratings/${a}`,o,g)},deleteRating(t,n,a,o){return i.delete(`/${t}/chat/${n}/ratings/${a}`,o)}}}const ge=e=>new Promise(r=>setTimeout(r,e));function ue(e){return new Promise((r,s)=>{const{callbacks:i,host:t,auth:n}=e,{onMessage:a=null,onOpen:o=null,onClose:g=null,onError:d=null}=i||{},l=new WebSocket(`${t}?authorization=${ee(n)}`);l.onmessage=a,l.onclose=g,l.onerror=S=>{console.error(S),d==null||d("Websocket failed to connect",S),s(S)},l.onopen=S=>{o==null||o(S),r(l)}})}async function me(e){const{retries:r=1}=e;let s=null;for(let i=0;(s==null?void 0:s.readyState)!==WebSocket.OPEN;i++)try{s=await ue(e)}catch(t){if(i===r)throw t;await ge(i*500)}return s}async function he(e,r,s){const i=s!=null&&s.onMessage?[s.onMessage]:[],t=await me({auth:e,host:r,callbacks:{onError:s==null?void 0:s.onError,onMessage:n=>{const a=JSON.parse(n.data);i.forEach(o=>o(a.event,a))}}});return{socket:t,disconnect:()=>t.close(),subscribeToEvents:n=>i.push(n)}}const we="X-Playground-Chat";function fe(e,r,s,i){const t=B(e,`${r}/agents/${s}`,i);return{createStream(n){return t.post("/streams",{driver_id:n.driver_id,presenter_id:n.presenter_id,compatibility_mode:n.compatibility_mode,stream_warmup:n.stream_warmup,session_timeout:n.session_timeout,type:C.Clip})},startConnection(n,a,o){return t.post(`/streams/${n}/sdp`,{session_id:o,answer:a,type:C.Clip})},addIceCandidate(n,a,o){return t.post(`/streams/${n}/ice`,{session_id:o,...a,type:C.Clip})},sendStreamRequest(n,a,o){return t.post(`/streams/${n}`,{session_id:a,...o,type:C.Clip})},close(n,a){return t.delete(`/streams/${n}`,{session_id:a,type:C.Clip})}}}function pe(e,r,s,i){const t=B(e,`${r}/agents/${s}`,i);return{createStream(n,a){return t.post("/streams",{source_url:n.source_url,driver_url:n.driver_url,face:n.face,config:n.config,compatibility_mode:n.compatibility_mode,stream_warmup:n.stream_warmup,output_resolution:n.output_resolution,session_timeout:n.session_timeout,type:C.Talk},a)},startConnection(n,a,o,g){return t.post(`/streams/${n}/sdp`,{session_id:o,answer:a,type:C.Talk},g)},addIceCandidate(n,a,o,g){return t.post(`/streams/${n}/ice`,{session_id:o,...a,type:C.Talk},g)},sendStreamRequest(n,a,o,g){return t.post(`/streams/${n}`,{session_id:a,...o,type:C.Talk},g)},close(n,a,o){return t.delete(`/streams/${n}`,{session_id:a,type:C.Talk},o)}}}let ne=!1;const $=(e,r)=>ne&&console.log(e,r),ye=(window.RTCPeerConnection||window.webkitRTCPeerConnection||window.mozRTCPeerConnection).bind(window);function re(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 ve(){let e=0;return r=>{for(const s of r.values())if(s&&s.type==="inbound-rtp"&&s.kind==="video"){const i=s.bytesReceived,t=i-e>0;return e=i,t}return!1}}function Ce(e,r){const i=Math.max(Math.ceil(10),1);let t=0,n=!1;const a=ve();return setInterval(async()=>{const o=await e.getStats();a(o)?(t=0,n||(r==null||r(T.Start),n=!0)):n&&(t++,t>=i&&(r==null||r(T.Stop),n=!1))},100)}async function Se(e,r,{debug:s=!1,callbacks:i,auth:t,analytics:n,baseURL:a=K}){ne=s;let o;const{startConnection:g,sendStreamRequest:d,close:l,createStream:S,addIceCandidate:L}=r.videoType===C.Clip?fe(t,a,e,i.onError):pe(t,a,e,i.onError),{id:R,offer:U,ice_servers:j,session_id:I}=await S(r),y=new ye({iceServers:j}),c=y.createDataChannel("JanusDataChannel");if(!I)throw new Error("Could not create session_id");const m=Ce(y,i.onVideoStateChange);y.onicecandidate=u=>{$("peerConnection.onicecandidate",u),u.candidate&&u.candidate.sdpMid&&u.candidate.sdpMLineIndex!==null?L(R,{candidate:u.candidate.candidate,sdpMid:u.candidate.sdpMid,sdpMLineIndex:u.candidate.sdpMLineIndex},I):L(R,{candidate:null},I)},y.oniceconnectionstatechange=()=>{var w;$("peerConnection.oniceconnectionstatechange => "+y.iceConnectionState);const u=re(y.iceConnectionState);u===k.Connected?o=setTimeout(()=>{var h;(h=i.onConnectionStateChange)==null||h.call(i,k.Connected)},5e3):(clearTimeout(o),(w=i.onConnectionStateChange)==null||w.call(i,u))},y.ontrack=u=>{var w;$("peerConnection.ontrack",u),(w=i.onSrcObjectReady)==null||w.call(i,u.streams[0])},c.onmessage=u=>{var w;if(c.readyState==="open"){const[h,f]=u.data.split(":");h===z.StreamReady&&(clearTimeout(o),(w=i.onConnectionStateChange)==null||w.call(i,k.Connected))}},await y.setRemoteDescription(U),$("set remote description OK");const v=await y.createAnswer();return $("create answer OK"),await y.setLocalDescription(v),$("set local description OK"),await g(R,v,I),$("start connection OK"),{speak(u){return d(R,I,u)},async disconnect(){var u,w,h;if(R){if(y){if(re(y.iceConnectionState)===k.New){(u=i.onVideoStateChange)==null||u.call(i,T.Stop),clearInterval(m);return}y.close(),y.oniceconnectionstatechange=null,y.onnegotiationneeded=null,y.onicecandidate=null,y.ontrack=null}try{await l(R,I).catch(f=>{})}catch(f){$("Error on close stream connection",f)}(w=i.onConnectionStateChange)==null||w.call(i,k.New),(h=i.onVideoStateChange)==null||h.call(i,T.Stop),clearInterval(m)}},sessionId:I,streamId:R}}let H={};function _e(e){const r=window!=null&&window.hasOwnProperty("DID_AGENTS_API")?"agents-ui":"agents-sdk",s={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{...s,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:n,...a}=t||{},o={method:"POST",headers:{"Content-Type":"application/x-www-form-urlencoded"},body:new URLSearchParams({data:JSON.stringify([{event:i,properties:{...a,...s,source:r,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",o).then(g=>g.json()).catch(g=>console.error(g))},linkTrack(i,t,n,a){H[i]||(H[i]={events:{},resolvedDependencies:[]}),a.includes(n)||a.push(n);const o=H[i];if(o.events[n]={props:t},o.resolvedDependencies.push(n),a.every(d=>o.resolvedDependencies.includes(d))){const d=a.reduce((l,S)=>o.events[S]?{...l,...o.events[S].props}:l,{});this.track(i,d),o.resolvedDependencies=o.resolvedDependencies.filter(l=>!a.includes(l)),a.forEach(l=>{delete o.events[l]})}}}}function Me(e){var i,t,n,a,o;const r=()=>/Mobi|Android/i.test(navigator.userAgent)?"Mobile":"Desktop";return{$os:`${(()=>{const g=navigator.platform;return g.toLowerCase().includes("win")?"Windows":g.toLowerCase().includes("mac")?"Mac OS X":g.toLowerCase().includes("linux")?"Linux":"Unknown"})()}`,isMobile:`${r()=="Mobile"}`,browser:navigator.userAgent,origin:window.location.origin,agentType:(i=e.presenter)==null?void 0:i.type,agentVoice:{voiceId:(n=(t=e.presenter)==null?void 0:t.voice)==null?void 0:n.voice_id,provider:(o=(a=e.presenter)==null?void 0:a.voice)==null?void 0:o.type}}}function ke(e,r,s){var d,l;const{event:i,...t}=e,{template:n}=(r==null?void 0:r.llm)||{},{language:a}=((d=r==null?void 0:r.presenter)==null?void 0:d.voice)||{},{stitch:o}=(r==null?void 0:r.presenter)||{};return{...t,llm:{...t.llm,template:n},script:{...t.script,provider:{...(l=t==null?void 0:t.script)==null?void 0:l.provider,language:a}},stitch:o,...s}}const Re=1080;let D=0;function Ae(e,r){var s,i,t;if(e.presenter){if(e.presenter.type===C.Clip)return{videoType:C.Clip,driver_id:e.presenter.driver_id,presenter_id:e.presenter.presenter_id,stream_warmup:!0}}else throw new Error("Presenter is not initialized");return{videoType:C.Talk,source_url:e.presenter.source_url,session_timeout:(s=r==null?void 0:r.streamOptions)==null?void 0:s.session_timeout,stream_warmup:((i=r==null?void 0:r.streamOptions)==null?void 0:i.stream_warmup)??!0,compatibility_mode:(t=r==null?void 0:r.streamOptions)==null?void 0:t.compatibility_mode,output_resolution:(r==null?void 0:r.outputResolution)||(e.presenter.stitch?Re:void 0)}}function ie(e){return e===M.Playground?{headers:{[we]:"true"}}:{}}async function ae(e,r,s,i){try{const t=await r.newChat(e,ie(i));return s.track("agent-chat",{event:"created",chat_id:t.id,agent_id:e,mode:i}),t}catch(t){try{console.error(t);const n=JSON.parse(t.message);if((n==null?void 0:n.kind)==="InsufficientCreditsError")throw new Error("InsufficientCreditsError")}catch(n){console.error("Error parsing the error message:",n)}throw new Error("Cannot create new chat")}}function Ie(e,r,s,i,t){return new Promise(async(n,a)=>{D=0;const o=await Se(e.id,Ae(e,r),{...r,analytics:i,callbacks:{...r.callbacks,onConnectionStateChange:async g=>{var d,l;g===k.Connected&&(!t&&r.mode!==M.DirectPlayback&&(t=await ae(e.id,s,i,r.mode).catch(S=>{a(S)})),o?n({chat:t,streamingManager:o}):t&&a(new Error("Something went wrong while initializing the manager"))),(l=(d=r.callbacks).onConnectionStateChange)==null||l.call(d,g)},onVideoStateChange(g){var d,l;(l=(d=r.callbacks).onVideoStateChange)==null||l.call(d,g),D>0&&g===T.Start&&i.linkTrack("agent-video",{event:"start",latency:Date.now()-D},"start",[z.StreamVideoCreated])}}}).catch(a)})}function se(e){let r="";if(e.greetings&&e.greetings.length>0){const s=Math.floor(Math.random()*e.greetings.length);r=e.greetings[s]}else r=`Hi! I'm ${e.preview_name||"My Agent"}. How can I help you?`;return[{content:r,id:F(),role:"assistant",created_at:new Date().toISOString()}]}function Ee(e){if(e.answer!==void 0)return e.answer;let r=0,s="";for(;r in e;)s+=e[r],r++;return s}function $e(e,r,s,i,t){if(!(e===P.Partial||e===P.Answer))return;const n=i.messages[i.messages.length-1];if((n==null?void 0:n.role)!=="assistant")return;const{content:a,sequence:o}=r;e===P.Partial?s[o]=a:s.answer=a;const g=Ee(s);(n.content!==g||e===P.Answer)&&(n.content=g,t==null||t(i.messages,e))}async function Pe(e,r){var j,I,y;let s={},i=!0;const t={messages:[],chatMode:r.mode||M.Functional},n=r.baseURL||K,a=r.wsURL||ce,o=r.mixpanelKey||de,g=te(r.auth,n,r.callbacks.onError),d=await g.getById(e);t.messages=se(d),(I=(j=r.callbacks).onNewMessage)==null||I.call(j,t.messages,"answer");const l=_e({token:o,agent:d,...r});l.track("agent-sdk",{event:"loaded",...Me(d)});const S={onMessage:(c,m)=>{var v,u,w,h;if("content"in m)$e(c,m,s,t,r.callbacks.onNewMessage),c===P.Answer&&l.track("agent-message-received",{messages:t.messages.length,mode:t.chatMode});else{const f=z,E=[f.StreamVideoDone,f.StreamVideoError,f.StreamVideoRejected],_=[f.StreamFailed,f.StreamVideoError,f.StreamVideoRejected],A=ke(m,d,{mode:t.chatMode});if(c=c,c===f.StreamVideoCreated)l.linkTrack("agent-video",A,f.StreamVideoCreated,["start"]);else if(E.includes(c)){const b=c.split("/")[1];l.track("agent-video",{...A,event:b})}_.includes(c)&&((u=(v=r.callbacks).onError)==null||u.call(v,new Error(`Stream failed with event ${c}`),{data:m})),m.event===f.StreamDone&&((h=(w=r.callbacks).onConnectionStateChange)==null||h.call(w,k.New))}}};async function L(c){var f,E,_,A,b;D=0,c&&!i&&(delete t.chat,t.messages=se(d),(E=(f=r.callbacks).onNewMessage)==null||E.call(f,t.messages,"answer"));const m=r.mode===M.DirectPlayback?Promise.resolve(void 0):he(r.auth,a,S),v=Ie(d,r,g,l,t.chat).catch(N=>{throw U(M.Maintenance),N}),[u,{streamingManager:w,chat:h}]=await Promise.all([m,v]);h&&h.id!==((_=t.chat)==null?void 0:_.id)&&((b=(A=r.callbacks).onNewChat)==null||b.call(A,h.id)),t.streamingManager=w,t.socketManager=u,t.chat=h,i=!1,U((h==null?void 0:h.chat_mode)??r.mode??M.Functional)}async function R(){var c,m;(c=t.socketManager)==null||c.disconnect(),await((m=t.streamingManager)==null?void 0:m.disconnect()),delete t.streamingManager,delete t.socketManager}async function U(c){var m,v;c!==t.chatMode&&(l.track("agent-mode-change",{mode:c}),t.chatMode=c,t.chatMode!==M.Functional&&await R(),(v=(m=r.callbacks).onModeChange)==null||v.call(m,c))}return{agent:d,starterMessages:((y=d.knowledge)==null?void 0:y.starter_message)||[],changeMode:U,async connect(){var c;await L(!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 R(),await L(!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 R(),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 v,u,w,h,f,E;const m=F();s={};try{if(D=Date.now(),r.mode===M.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===M.Maintenance)throw new Error("Chat is in maintenance mode");if(![M.TextOnly,M.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.chat||(t.chat=await ae(d.id,g,l,t.chatMode),(u=(v=r.callbacks).onNewChat)==null||u.call(v,t.chat.id)),t.messages.push({id:F(),role:"user",content:c,created_at:new Date(D).toISOString()}),(h=(w=r.callbacks).onNewMessage)==null||h.call(w,t.messages,"user");const _={id:m,role:"assistant",content:"",created_at:new Date().toISOString(),matches:[]},A=[...t.messages];t.messages.push(_);const b=x=>{var V,oe;return g.chat(d.id,x,{sessionId:(V=t.streamingManager)==null?void 0:V.sessionId,streamId:(oe=t.streamingManager)==null?void 0:oe.streamId,chatMode:t.chatMode,messages:A},ie(t.chatMode))},N=await b(t.chat.id).catch(async x=>{var V;if(!((V=x==null?void 0:x.message)!=null&&V.includes("missing or invalid session_id")))throw x;return await R(),await L(!1),b(t.chat.id)});return l.track("agent-message-send",{event:"success",mode:t.chatMode,messages:t.messages.length+1}),_.context=N.context,_.matches=N.matches,N.result&&(_.content=N.result,l.track("agent-message-received",{latency:Date.now()-D,mode:t.chatMode,messages:t.messages.length}),(E=(f=r.callbacks).onNewMessage)==null||E.call(f,t.messages,"answer")),N}catch(_){throw t.messages[t.messages.length-1].id===m&&t.messages.pop(),l.track("agent-message-send",{event:"error",mode:t.chatMode,messages:t.messages.length}),_}},rate(c,m,v){var h,f,E,_;const u=t.messages.find(A=>A.id===c);if(t.chat){if(!u)throw new Error("Message not found")}else throw new Error("Chat is not initialized");const w=((h=u.matches)==null?void 0:h.map(A=>[A.document_id,A.id]))??[];return l.track("agent-rate",{event:v?"update":"create",thumb:m===1?"up":"down",knowledge_id:((f=d.knowledge)==null?void 0:f.id)??"",mode:t.chatMode,matches:w,score:m}),v?g.updateRating(d.id,t.chat.id,v,{knowledge_id:((E=d.knowledge)==null?void 0:E.id)??"",message_id:c,matches:w,score:m}):g.createRating(d.id,t.chat.id,{knowledge_id:((_=d.knowledge)==null?void 0:_.id)??"",message_id:c,matches:w,score:m})},deleteRate(c){var m;if(!t.chat)throw new Error("Chat is not initialized");return l.track("agent-rate-delete",{type:"text",chat_id:(m=t.chat)==null?void 0:m.id,id:c,mode:t.chatMode}),g.deleteRating(d.id,t.chat.id,c)},speak(c){if(!t.streamingManager)throw new Error("Please connect to the agent first");function m(){if(typeof c=="string"){if(!d.presenter)throw new Error("Presenter is not initialized");return{type:"text",provider:d.presenter.voice,input:c,ssml:!1}}return c}const v=m();return l.track("agent-speak",v),t.streamingManager.speak({script:v})}}}function Te(e,r,s){const{getById:i}=te(r,s||K);return i(e)}p.ChatMode=M,p.ChatProgress=P,p.ConnectionState=k,p.DocumentType=Y,p.KnowledgeType=X,p.PlanGroup=W,p.Providers=Z,p.RateState=q,p.StreamEvents=z,p.StreamingState=T,p.Subject=J,p.UserPlan=O,p.VideoType=C,p.VoiceAccess=G,p.createAgentManager=Pe,p.getAgent=Te,Object.defineProperty(p,Symbol.toStringTag,{value:"Module"})});
|
|
1
|
+
(function(p,L){typeof exports=="object"&&typeof module<"u"?L(exports):typeof define=="function"&&define.amd?define(["exports"],L):(p=typeof globalThis<"u"?globalThis:p||self,L(p.index={}))})(this,function(p){"use strict";var L=(e=>(e.TRIAL="trial",e.BASIC="basic",e.ENTERPRISE="enterprise",e.LITE="lite",e.ADVANCED="advanced",e))(L||{}),W=(e=>(e.TRIAL="deid-trial",e.PRO="deid-pro",e.ENTERPRISE="deid-enterprise",e.LITE="deid-lite",e.ADVANCED="deid-advanced",e.BUILD="deid-api-build",e.LAUNCH="deid-api-launch",e.SCALE="deid-api-scale",e))(W||{}),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||{}),_=(e=>(e.Clip="clip",e.Talk="talk",e))(_||{}),D=(e=>(e.Start="START",e.Stop="STOP",e))(D||{}),z=(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))(z||{}),S=(e=>(e.New="new",e.Fail="fail",e.Connected="connected",e.Connecting="connecting",e.Closed="closed",e.Completed="completed",e.Disconnected="disconnected",e))(S||{}),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 K="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 B(e,r=K,s){const i=async(t,n)=>{const a=await fetch(r+(t!=null&&t.startsWith("/")?t:`/${t}`),{...n,headers:{...n==null?void 0:n.headers,Authorization:ee(e),"Content-Type":"application/json"}});if(!a.ok){let o=await a.text().catch(()=>"Failed to fetch");throw s&&s(new Error(o),{url:t,options:n,headers:a.headers}),new Error(o)}return a.json()};return{get(t,n){return i(t,{...n,method:"GET"})},post(t,n,a){return i(t,{...a,body:JSON.stringify(n),method:"POST"})},delete(t,n,a){return i(t,{...a,body:JSON.stringify(n),method:"DELETE"})},patch(t,n,a){return i(t,{...a,body:JSON.stringify(n),method:"PATCH"})}}}function te(e,r=K,s){const i=B(e,`${r}/agents`,s);return{create(t,n){return i.post("/",t,n)},getAgents(t,n){return i.get(`/${t?`?tag=${t}`:""}`,n).then(a=>a??[])},getById(t,n){return i.get(`/${t}`,n)},delete(t,n){return i.delete(`/${t}`,void 0,n)},update(t,n,a){return i.patch(`/${t}`,n,a)},newChat(t,n){return i.post(`/${t}/chat`,void 0,n)},chat(t,n,a,o){return i.post(`/${t}/chat/${n}`,a,o)},createRating(t,n,a,o){return i.post(`/${t}/chat/${n}/ratings`,a,o)},updateRating(t,n,a,o,u){return i.patch(`/${t}/chat/${n}/ratings/${a}`,o,u)},deleteRating(t,n,a,o){return i.delete(`/${t}/chat/${n}/ratings/${a}`,o)}}}const ue=e=>new Promise(r=>setTimeout(r,e));function ge(e){return new Promise((r,s)=>{const{callbacks:i,host:t,auth:n}=e,{onMessage:a=null,onOpen:o=null,onClose:u=null,onError:d=null}=i||{},l=new WebSocket(`${t}?authorization=${ee(n)}`);l.onmessage=a,l.onclose=u,l.onerror=v=>{console.error(v),d==null||d("Websocket failed to connect",v),s(v)},l.onopen=v=>{o==null||o(v),r(l)}})}async function me(e){const{retries:r=1}=e;let s=null;for(let i=0;(s==null?void 0:s.readyState)!==WebSocket.OPEN;i++)try{s=await ge(e)}catch(t){if(i===r)throw t;await ue(i*500)}return s}async function we(e,r,s){const i=s!=null&&s.onMessage?[s.onMessage]:[],t=await me({auth:e,host:r,callbacks:{onError:s==null?void 0:s.onError,onMessage:n=>{const a=JSON.parse(n.data);i.forEach(o=>o(a.event,a))}}});return{socket:t,disconnect:()=>t.close(),subscribeToEvents:n=>i.push(n)}}const he="X-Playground-Chat";function fe(e,r,s,i){const t=B(e,`${r}/agents/${s}`,i);return{createStream(n){return t.post("/streams",{driver_id:n.driver_id,presenter_id:n.presenter_id,compatibility_mode:n.compatibility_mode,stream_warmup:n.stream_warmup,session_timeout:n.session_timeout,type:_.Clip})},startConnection(n,a,o){return t.post(`/streams/${n}/sdp`,{session_id:o,answer:a,type:_.Clip})},addIceCandidate(n,a,o){return t.post(`/streams/${n}/ice`,{session_id:o,...a,type:_.Clip})},sendStreamRequest(n,a,o){return t.post(`/streams/${n}`,{session_id:a,...o,type:_.Clip})},close(n,a){return t.delete(`/streams/${n}`,{session_id:a,type:_.Clip})}}}function pe(e,r,s,i){const t=B(e,`${r}/agents/${s}`,i);return{createStream(n,a){return t.post("/streams",{source_url:n.source_url,driver_url:n.driver_url,face:n.face,config:n.config,compatibility_mode:n.compatibility_mode,stream_warmup:n.stream_warmup,output_resolution:n.output_resolution,session_timeout:n.session_timeout,type:_.Talk},a)},startConnection(n,a,o,u){return t.post(`/streams/${n}/sdp`,{session_id:o,answer:a,type:_.Talk},u)},addIceCandidate(n,a,o,u){return t.post(`/streams/${n}/ice`,{session_id:o,...a,type:_.Talk},u)},sendStreamRequest(n,a,o,u){return t.post(`/streams/${n}`,{session_id:a,...o,type:_.Talk},u)},close(n,a,o){return t.delete(`/streams/${n}`,{session_id:a,type:_.Talk},o)}}}let ne=!1;const $=(e,r)=>ne&&console.log(e,r),ye=(window.RTCPeerConnection||window.webkitRTCPeerConnection||window.mozRTCPeerConnection).bind(window);function re(e){switch(e){case"connected":return S.Connected;case"checking":return S.Connecting;case"failed":return S.Fail;case"new":return S.New;case"closed":return S.Closed;case"disconnected":return S.Disconnected;case"completed":return S.Completed;default:return S.New}}function Ce(){let e=0;return r=>{for(const s of r.values())if(s&&s.type==="inbound-rtp"&&s.kind==="video"){const i=s.bytesReceived,t=i-e>0;return e=i,t}return!1}}function ve(e,r){const i=Math.max(Math.ceil(10),1);let t=0,n=!1;const a=Ce();return setInterval(async()=>{const o=await e.getStats();a(o)?(t=0,n||(r==null||r(D.Start),n=!0)):n&&(t++,t>=i&&(r==null||r(D.Stop),n=!1))},100)}async function Se(e,r,{debug:s=!1,callbacks:i,auth:t,baseURL:n=K,warmup:a}){ne=s;let o;const{startConnection:u,sendStreamRequest:d,close:l,createStream:v,addIceCandidate:P}=r.videoType===_.Clip?fe(t,n,e,i.onError):pe(t,n,e,i.onError),{id:R,offer:U,ice_servers:j,session_id:I}=await v(r),y=new ye({iceServers:j}),c=y.createDataChannel("JanusDataChannel");if(!I)throw new Error("Could not create session_id");const m=ve(y,i.onVideoStateChange);y.onicecandidate=g=>{$("peerConnection.onicecandidate",g),g.candidate&&g.candidate.sdpMid&&g.candidate.sdpMLineIndex!==null?P(R,{candidate:g.candidate.candidate,sdpMid:g.candidate.sdpMid,sdpMLineIndex:g.candidate.sdpMLineIndex},I):P(R,{candidate:null},I)},y.oniceconnectionstatechange=()=>{var h;$("peerConnection.oniceconnectionstatechange => "+y.iceConnectionState);const g=re(y.iceConnectionState);g===S.Connected?o=setTimeout(()=>{var w;return(w=i.onConnectionStateChange)==null?void 0:w.call(i,S.Connected)},a?0:5e3):(clearTimeout(o),(h=i.onConnectionStateChange)==null||h.call(i,g))},y.ontrack=g=>{var h;$("peerConnection.ontrack",g),(h=i.onSrcObjectReady)==null||h.call(i,g.streams[0])},c.onmessage=g=>{var h;if(c.readyState==="open"){const[w,f]=g.data.split(":");w===z.StreamReady&&(clearTimeout(o),(h=i.onConnectionStateChange)==null||h.call(i,S.Connected))}},await y.setRemoteDescription(U),$("set remote description OK");const C=await y.createAnswer();return $("create answer OK"),await y.setLocalDescription(C),$("set local description OK"),await u(R,C,I),$("start connection OK"),{speak(g){return d(R,I,g)},async disconnect(){var g,h,w;if(R){if(y){if(re(y.iceConnectionState)===S.New){(g=i.onVideoStateChange)==null||g.call(i,D.Stop),clearInterval(m);return}y.close(),y.oniceconnectionstatechange=null,y.onnegotiationneeded=null,y.onicecandidate=null,y.ontrack=null}try{await l(R,I).catch(f=>{})}catch(f){$("Error on close stream connection",f)}(h=i.onConnectionStateChange)==null||h.call(i,S.New),(w=i.onVideoStateChange)==null||w.call(i,D.Stop),clearInterval(m)}},sessionId:I,streamId:R}}let H={};function _e(e){const r=window!=null&&window.hasOwnProperty("DID_AGENTS_API")?"agents-ui":"agents-sdk",s={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{...s,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:n,...a}=t||{},o={method:"POST",headers:{"Content-Type":"application/x-www-form-urlencoded"},body:new URLSearchParams({data:JSON.stringify([{event:i,properties:{...a,...s,source:r,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",o).then(u=>u.json()).catch(u=>console.error(u))},linkTrack(i,t,n,a){H[i]||(H[i]={events:{},resolvedDependencies:[]}),a.includes(n)||a.push(n);const o=H[i];if(o.events[n]={props:t},o.resolvedDependencies.push(n),a.every(d=>o.resolvedDependencies.includes(d))){const d=a.reduce((l,v)=>o.events[v]?{...l,...o.events[v].props}:l,{});this.track(i,d),o.resolvedDependencies=o.resolvedDependencies.filter(l=>!a.includes(l)),a.forEach(l=>{delete o.events[l]})}}}}function Me(e){var i,t,n,a,o;const r=()=>/Mobi|Android/i.test(navigator.userAgent)?"Mobile":"Desktop";return{$os:`${(()=>{const u=navigator.platform;return u.toLowerCase().includes("win")?"Windows":u.toLowerCase().includes("mac")?"Mac OS X":u.toLowerCase().includes("linux")?"Linux":"Unknown"})()}`,isMobile:`${r()=="Mobile"}`,browser:navigator.userAgent,origin:window.location.origin,agentType:(i=e.presenter)==null?void 0:i.type,agentVoice:{voiceId:(n=(t=e.presenter)==null?void 0:t.voice)==null?void 0:n.voice_id,provider:(o=(a=e.presenter)==null?void 0:a.voice)==null?void 0:o.type}}}function ke(e,r,s){var d,l;const{event:i,...t}=e,{template:n}=(r==null?void 0:r.llm)||{},{language:a}=((d=r==null?void 0:r.presenter)==null?void 0:d.voice)||{},{stitch:o}=(r==null?void 0:r.presenter)||{};return{...t,llm:{...t.llm,template:n},script:{...t.script,provider:{...(l=t==null?void 0:t.script)==null?void 0:l.provider,language:a}},stitch:o,...s}}const Re=1080;let b=0;function Ae(e,r){var s,i,t;if(e.presenter){if(e.presenter.type===_.Clip)return{videoType:_.Clip,driver_id:e.presenter.driver_id,presenter_id:e.presenter.presenter_id,stream_warmup:!0}}else throw new Error("Presenter is not initialized");return{videoType:_.Talk,source_url:e.presenter.source_url,session_timeout:(s=r==null?void 0:r.streamOptions)==null?void 0:s.session_timeout,stream_warmup:((i=r==null?void 0:r.streamOptions)==null?void 0:i.stream_warmup)??!0,compatibility_mode:(t=r==null?void 0:r.streamOptions)==null?void 0:t.compatibility_mode,output_resolution:(r==null?void 0:r.outputResolution)||(e.presenter.stitch?Re:void 0)}}function ie(e){return e===k.Playground?{headers:{[he]:"true"}}:{}}async function ae(e,r,s,i){try{const t=await r.newChat(e,ie(i));return s.track("agent-chat",{event:"created",chat_id:t.id,agent_id:e,mode:i}),t}catch(t){try{console.error(t);const n=JSON.parse(t.message);if((n==null?void 0:n.kind)==="InsufficientCreditsError")throw new Error("InsufficientCreditsError")}catch(n){console.error("Error parsing the error message:",n)}throw new Error("Cannot create new chat")}}function Ie(e,r,s,i,t){return new Promise(async(n,a)=>{var u;b=0;const o=await Se(e.id,Ae(e,r),{...r,analytics:i,warmup:(u=r.streamOptions)==null?void 0:u.stream_warmup,callbacks:{...r.callbacks,onConnectionStateChange:async d=>{var l,v;d===S.Connected&&(!t&&r.mode!==k.DirectPlayback&&(t=await ae(e.id,s,i,r.mode).catch(P=>{a(P)})),o?n({chat:t,streamingManager:o}):t&&a(new Error("Something went wrong while initializing the manager"))),(v=(l=r.callbacks).onConnectionStateChange)==null||v.call(l,d)},onVideoStateChange(d){var l,v;(v=(l=r.callbacks).onVideoStateChange)==null||v.call(l,d),b>0&&d===D.Start&&i.linkTrack("agent-video",{event:"start",latency:Date.now()-b},"start",[z.StreamVideoCreated])}}}).catch(a)})}function se(e){let r="";if(e.greetings&&e.greetings.length>0){const s=Math.floor(Math.random()*e.greetings.length);r=e.greetings[s]}else r=`Hi! I'm ${e.preview_name||"My Agent"}. How can I help you?`;return[{content:r,id:F(),role:"assistant",created_at:new Date().toISOString()}]}function Ee(e){if(e.answer!==void 0)return e.answer;let r=0,s="";for(;r in e;)s+=e[r],r++;return s}function $e(e,r,s,i,t){if(!(e===T.Partial||e===T.Answer))return;const n=i.messages[i.messages.length-1];if((n==null?void 0:n.role)!=="assistant")return;const{content:a,sequence:o}=r;e===T.Partial?s[o]=a:s.answer=a;const u=Ee(s);(n.content!==u||e===T.Answer)&&(n.content=u,t==null||t(i.messages,e))}async function Pe(e,r){var j,I,y;let s={},i=!0;const t={messages:[],chatMode:r.mode||k.Functional},n=r.baseURL||K,a=r.wsURL||ce,o=r.mixpanelKey||de,u=te(r.auth,n,r.callbacks.onError),d=await u.getById(e);t.messages=se(d),(I=(j=r.callbacks).onNewMessage)==null||I.call(j,t.messages,"answer");const l=_e({token:o,agent:d,...r});l.track("agent-sdk",{event:"loaded",...Me(d)});const v={onMessage:(c,m)=>{var C,g,h,w;if("content"in m)$e(c,m,s,t,r.callbacks.onNewMessage),c===T.Answer&&l.track("agent-message-received",{messages:t.messages.length,mode:t.chatMode});else{const f=z,E=[f.StreamVideoDone,f.StreamVideoError,f.StreamVideoRejected],M=[f.StreamFailed,f.StreamVideoError,f.StreamVideoRejected],A=ke(m,d,{mode:t.chatMode});if(c=c,c===f.StreamVideoCreated)l.linkTrack("agent-video",A,f.StreamVideoCreated,["start"]);else if(E.includes(c)){const N=c.split("/")[1];l.track("agent-video",{...A,event:N})}M.includes(c)&&((g=(C=r.callbacks).onError)==null||g.call(C,new Error(`Stream failed with event ${c}`),{data:m})),m.event===f.StreamDone&&((w=(h=r.callbacks).onConnectionStateChange)==null||w.call(h,S.New))}}};async function P(c){var f,E,M,A,N;b=0,c&&!i&&(delete t.chat,t.messages=se(d),(E=(f=r.callbacks).onNewMessage)==null||E.call(f,t.messages,"answer"));const m=r.mode===k.DirectPlayback?Promise.resolve(void 0):we(r.auth,a,v),C=Ie(d,r,u,l,t.chat).catch(O=>{throw U(k.Maintenance),O}),[g,{streamingManager:h,chat:w}]=await Promise.all([m,C]);w&&w.id!==((M=t.chat)==null?void 0:M.id)&&((N=(A=r.callbacks).onNewChat)==null||N.call(A,w.id)),t.streamingManager=h,t.socketManager=g,t.chat=w,i=!1,U((w==null?void 0:w.chat_mode)??r.mode??k.Functional)}async function R(){var c,m;(c=t.socketManager)==null||c.disconnect(),await((m=t.streamingManager)==null?void 0:m.disconnect()),delete t.streamingManager,delete t.socketManager}async function U(c){var m,C;c!==t.chatMode&&(l.track("agent-mode-change",{mode:c}),t.chatMode=c,t.chatMode!==k.Functional&&await R(),(C=(m=r.callbacks).onModeChange)==null||C.call(m,c))}return{agent:d,starterMessages:((y=d.knowledge)==null?void 0:y.starter_message)||[],changeMode:U,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 R(),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 R(),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 C,g,h,w,f,E;const m=F();s={};try{if(b=Date.now(),r.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.chat||(t.chat=await ae(d.id,u,l,t.chatMode),(g=(C=r.callbacks).onNewChat)==null||g.call(C,t.chat.id)),t.messages.push({id:F(),role:"user",content:c,created_at:new Date(b).toISOString()}),(w=(h=r.callbacks).onNewMessage)==null||w.call(h,t.messages,"user");const M={id:m,role:"assistant",content:"",created_at:new Date().toISOString(),matches:[]},A=[...t.messages];t.messages.push(M);const N=x=>{var V,oe;return u.chat(d.id,x,{sessionId:(V=t.streamingManager)==null?void 0:V.sessionId,streamId:(oe=t.streamingManager)==null?void 0:oe.streamId,chatMode:t.chatMode,messages:A},ie(t.chatMode))},O=await N(t.chat.id).catch(async x=>{var V;if(!((V=x==null?void 0:x.message)!=null&&V.includes("missing or invalid session_id")))throw x;return await R(),await P(!1),N(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()-b,mode:t.chatMode,messages:t.messages.length}),(E=(f=r.callbacks).onNewMessage)==null||E.call(f,t.messages,"answer")),O}catch(M){throw t.messages[t.messages.length-1].id===m&&t.messages.pop(),l.track("agent-message-send",{event:"error",mode:t.chatMode,messages:t.messages.length}),M}},rate(c,m,C){var w,f,E,M;const g=t.messages.find(A=>A.id===c);if(t.chat){if(!g)throw new Error("Message not found")}else throw new Error("Chat is not initialized");const h=((w=g.matches)==null?void 0:w.map(A=>[A.document_id,A.id]))??[];return l.track("agent-rate",{event:C?"update":"create",thumb:m===1?"up":"down",knowledge_id:((f=d.knowledge)==null?void 0:f.id)??"",mode:t.chatMode,matches:h,score:m}),C?u.updateRating(d.id,t.chat.id,C,{knowledge_id:((E=d.knowledge)==null?void 0:E.id)??"",message_id:c,matches:h,score:m}):u.createRating(d.id,t.chat.id,{knowledge_id:((M=d.knowledge)==null?void 0:M.id)??"",message_id:c,matches:h,score:m})},deleteRate(c){var m;if(!t.chat)throw new Error("Chat is not initialized");return l.track("agent-rate-delete",{type:"text",chat_id:(m=t.chat)==null?void 0:m.id,id:c,mode:t.chatMode}),u.deleteRating(d.id,t.chat.id,c)},speak(c){if(!t.streamingManager)throw new Error("Please connect to the agent first");function m(){if(typeof c=="string"){if(!d.presenter)throw new Error("Presenter is not initialized");return{type:"text",provider:d.presenter.voice,input:c,ssml:!1}}return c}const C=m();return l.track("agent-speak",C),t.streamingManager.speak({script:C})}}}function Te(e,r,s){const{getById:i}=te(r,s||K);return i(e)}p.ChatMode=k,p.ChatProgress=T,p.ConnectionState=S,p.DocumentType=Y,p.KnowledgeType=X,p.PlanGroup=W,p.Providers=Z,p.RateState=q,p.StreamEvents=z,p.StreamingState=D,p.Subject=J,p.UserPlan=L,p.VideoType=_,p.VoiceAccess=G,p.createAgentManager=Pe,p.getAgent=Te,Object.defineProperty(p,Symbol.toStringTag,{value:"Module"})});
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { CreateStreamOptions, PayloadType, StreamingManagerOptions } from './types/index';
|
|
2
|
-
export declare function createStreamingManager<T extends CreateStreamOptions>(agentId: string, agent: T, { debug, callbacks, auth,
|
|
2
|
+
export declare function createStreamingManager<T extends CreateStreamOptions>(agentId: string, agent: T, { debug, callbacks, auth, baseURL, warmup }: StreamingManagerOptions): Promise<{
|
|
3
3
|
/**
|
|
4
4
|
* Method to send request to server to get clip or talk depend on you payload
|
|
5
5
|
* @param payload
|
|
@@ -26,7 +26,9 @@ export declare enum ConnectionState {
|
|
|
26
26
|
Fail = "fail",
|
|
27
27
|
Connected = "connected",
|
|
28
28
|
Connecting = "connecting",
|
|
29
|
-
|
|
29
|
+
Closed = "closed",
|
|
30
|
+
Completed = "completed",
|
|
31
|
+
Disconnected = "disconnected"
|
|
30
32
|
}
|
|
31
33
|
export interface ManagerCallbacks {
|
|
32
34
|
onMessage?: (event: string, data: string) => void;
|
|
@@ -55,6 +57,7 @@ export interface StreamingManagerOptions {
|
|
|
55
57
|
callbacks: ManagerCallbacks;
|
|
56
58
|
baseURL?: string;
|
|
57
59
|
debug?: boolean;
|
|
60
|
+
warmup?: boolean;
|
|
58
61
|
auth: Auth;
|
|
59
62
|
analytics: Analytics;
|
|
60
63
|
}
|