@d-id/client-sdk 1.0.19-beta.96 → 1.0.19-beta.97
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 +146 -140
- package/dist/index.umd.cjs +1 -1
- package/dist/src/types/entities/agents/chat.d.ts +1 -0
- 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 || {}), k = /* @__PURE__ */ ((e) => (e.Functional = "Functional", e.TextOnly = "TextOnly", e.Maintenance = "Maintenance", e.Playground = "Playground", e))(k || {}), D = /* @__PURE__ */ ((e) => (e.Embed = "embed", e.Query = "query", e.Partial = "partial", e.Answer = "answer", e.Complete = "done", e))(D || {}), 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 || {}),
|
|
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 || {}), k = /* @__PURE__ */ ((e) => (e.Functional = "Functional", e.TextOnly = "TextOnly", e.Maintenance = "Maintenance", e.Playground = "Playground", e))(k || {}), D = /* @__PURE__ */ ((e) => (e.Embed = "embed", e.Query = "query", e.Partial = "partial", e.Answer = "answer", e.Complete = "done", e))(D || {}), 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 || {}), v = /* @__PURE__ */ ((e) => (e.Clip = "clip", e.Talk = "talk", e))(v || {}), N = /* @__PURE__ */ ((e) => (e.Start = "START", e.Stop = "STOP", e))(N || {}), $ = /* @__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))($ || {}), S = /* @__PURE__ */ ((e) => (e[e.New = 0] = "New", e[e.Fail = 1] = "Fail", e[e.Connected = 2] = "Connected", e[e.Connecting = 3] = "Connecting", e[e.Terminating = 4] = "Terminating", e))(S || {}), 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 L = "https://api.d-id.com", oe = "wss://notifications.d-id.com", ce = "79f81a83a67430be2bc0fd61042b8faa", F = () => Math.random().toString(16).slice(2);
|
|
3
3
|
function Q() {
|
|
4
4
|
let e = window.localStorage.getItem("did_external_key_id");
|
|
@@ -113,12 +113,12 @@ function le(e) {
|
|
|
113
113
|
onMessage: s = null,
|
|
114
114
|
onOpen: o = null,
|
|
115
115
|
onClose: c = null,
|
|
116
|
-
onError:
|
|
117
|
-
} = a || {},
|
|
118
|
-
|
|
119
|
-
console.error(
|
|
120
|
-
},
|
|
121
|
-
o == null || o(
|
|
116
|
+
onError: u = null
|
|
117
|
+
} = a || {}, y = new WebSocket(`${r}?authorization=${X(n)}`);
|
|
118
|
+
y.onmessage = s, y.onclose = c, y.onerror = (M) => {
|
|
119
|
+
console.error(M), u == null || u("Websocket failed to connect", M), t(M);
|
|
120
|
+
}, y.onopen = (M) => {
|
|
121
|
+
o == null || o(M), i(y);
|
|
122
122
|
};
|
|
123
123
|
});
|
|
124
124
|
}
|
|
@@ -164,39 +164,39 @@ function me(e, i, t, a) {
|
|
|
164
164
|
presenter_id: n.presenter_id,
|
|
165
165
|
compatibility_mode: n.compatibility_mode,
|
|
166
166
|
stream_warmup: n.stream_warmup,
|
|
167
|
-
type:
|
|
167
|
+
type: v.Clip
|
|
168
168
|
});
|
|
169
169
|
},
|
|
170
170
|
startConnection(n, s, o) {
|
|
171
171
|
return r.post(`/streams/${n}/sdp`, {
|
|
172
172
|
session_id: o,
|
|
173
173
|
answer: s,
|
|
174
|
-
type:
|
|
174
|
+
type: v.Clip
|
|
175
175
|
});
|
|
176
176
|
},
|
|
177
177
|
addIceCandidate(n, s, o) {
|
|
178
178
|
return r.post(`/streams/${n}/ice`, {
|
|
179
179
|
session_id: o,
|
|
180
180
|
...s,
|
|
181
|
-
type:
|
|
181
|
+
type: v.Clip
|
|
182
182
|
});
|
|
183
183
|
},
|
|
184
184
|
sendStreamRequest(n, s, o) {
|
|
185
185
|
return r.post(`/streams/${n}`, {
|
|
186
186
|
session_id: s,
|
|
187
187
|
...o,
|
|
188
|
-
type:
|
|
188
|
+
type: v.Clip
|
|
189
189
|
});
|
|
190
190
|
},
|
|
191
191
|
close(n, s) {
|
|
192
192
|
return r.delete(`/streams/${n}`, {
|
|
193
193
|
session_id: s,
|
|
194
|
-
type:
|
|
194
|
+
type: v.Clip
|
|
195
195
|
});
|
|
196
196
|
}
|
|
197
197
|
};
|
|
198
198
|
}
|
|
199
|
-
function
|
|
199
|
+
function he(e, i, t, a) {
|
|
200
200
|
const r = V(e, `${i}/agents/${t}`, a);
|
|
201
201
|
return {
|
|
202
202
|
createStream(n, s) {
|
|
@@ -208,40 +208,40 @@ function we(e, i, t, a) {
|
|
|
208
208
|
compatibility_mode: n.compatibility_mode,
|
|
209
209
|
stream_warmup: n.stream_warmup,
|
|
210
210
|
output_resolution: n.output_resolution,
|
|
211
|
-
type:
|
|
211
|
+
type: v.Talk
|
|
212
212
|
}, s);
|
|
213
213
|
},
|
|
214
214
|
startConnection(n, s, o, c) {
|
|
215
215
|
return r.post(`/streams/${n}/sdp`, {
|
|
216
216
|
session_id: o,
|
|
217
217
|
answer: s,
|
|
218
|
-
type:
|
|
218
|
+
type: v.Talk
|
|
219
219
|
}, c);
|
|
220
220
|
},
|
|
221
221
|
addIceCandidate(n, s, o, c) {
|
|
222
222
|
return r.post(`/streams/${n}/ice`, {
|
|
223
223
|
session_id: o,
|
|
224
224
|
...s,
|
|
225
|
-
type:
|
|
225
|
+
type: v.Talk
|
|
226
226
|
}, c);
|
|
227
227
|
},
|
|
228
228
|
sendStreamRequest(n, s, o, c) {
|
|
229
229
|
return r.post(`/streams/${n}`, {
|
|
230
230
|
session_id: s,
|
|
231
231
|
...o,
|
|
232
|
-
type:
|
|
232
|
+
type: v.Talk
|
|
233
233
|
}, c);
|
|
234
234
|
},
|
|
235
235
|
close(n, s, o) {
|
|
236
236
|
return r.delete(`/streams/${n}`, {
|
|
237
237
|
session_id: s,
|
|
238
|
-
type:
|
|
238
|
+
type: v.Talk
|
|
239
239
|
}, o);
|
|
240
240
|
}
|
|
241
241
|
};
|
|
242
242
|
}
|
|
243
243
|
let Z = !1;
|
|
244
|
-
const
|
|
244
|
+
const R = (e, i) => Z && console.log(e, i), we = (window.RTCPeerConnection || window.webkitRTCPeerConnection || window.mozRTCPeerConnection).bind(window);
|
|
245
245
|
function q(e) {
|
|
246
246
|
switch (e) {
|
|
247
247
|
case "connected":
|
|
@@ -277,7 +277,7 @@ function pe(e, i, t) {
|
|
|
277
277
|
o(c) ? (n = 0, s || (i == null || i(N.Start), s = !0)) : s && (n++, n >= r && (i == null || i(N.Stop), s = !1));
|
|
278
278
|
}, 100);
|
|
279
279
|
}
|
|
280
|
-
async function
|
|
280
|
+
async function Me(e, i, {
|
|
281
281
|
debug: t = !1,
|
|
282
282
|
callbacks: a,
|
|
283
283
|
auth: r,
|
|
@@ -288,76 +288,76 @@ async function ye(e, i, {
|
|
|
288
288
|
let o;
|
|
289
289
|
const {
|
|
290
290
|
startConnection: c,
|
|
291
|
-
sendStreamRequest:
|
|
292
|
-
close:
|
|
293
|
-
createStream:
|
|
294
|
-
addIceCandidate:
|
|
295
|
-
} = i.videoType ===
|
|
291
|
+
sendStreamRequest: u,
|
|
292
|
+
close: y,
|
|
293
|
+
createStream: M,
|
|
294
|
+
addIceCandidate: E
|
|
295
|
+
} = i.videoType === v.Clip ? me(r, s, e, a.onError) : he(r, s, e, a.onError), {
|
|
296
296
|
id: _,
|
|
297
297
|
offer: K,
|
|
298
298
|
ice_servers: P,
|
|
299
299
|
session_id: I
|
|
300
|
-
} = await
|
|
300
|
+
} = await M(i), f = new we({
|
|
301
301
|
iceServers: P
|
|
302
|
-
}), d =
|
|
302
|
+
}), d = f.createDataChannel("JanusDataChannel");
|
|
303
303
|
if (!I)
|
|
304
304
|
throw new Error("Could not create session_id");
|
|
305
|
-
const m = pe(
|
|
306
|
-
|
|
307
|
-
|
|
305
|
+
const m = pe(f, a.onVideoStateChange);
|
|
306
|
+
f.onicecandidate = (g) => {
|
|
307
|
+
R("peerConnection.onicecandidate", g), g.candidate && g.candidate.sdpMid && g.candidate.sdpMLineIndex !== null ? E(_, {
|
|
308
308
|
candidate: g.candidate.candidate,
|
|
309
309
|
sdpMid: g.candidate.sdpMid,
|
|
310
310
|
sdpMLineIndex: g.candidate.sdpMLineIndex
|
|
311
|
-
}, I) :
|
|
311
|
+
}, I) : E(_, {
|
|
312
312
|
candidate: null
|
|
313
313
|
}, I);
|
|
314
|
-
},
|
|
315
|
-
var
|
|
316
|
-
|
|
317
|
-
const g = q(
|
|
314
|
+
}, f.oniceconnectionstatechange = () => {
|
|
315
|
+
var h;
|
|
316
|
+
R("peerConnection.oniceconnectionstatechange => " + f.iceConnectionState);
|
|
317
|
+
const g = q(f.iceConnectionState);
|
|
318
318
|
g === S.Connected ? o = setTimeout(() => {
|
|
319
|
-
var
|
|
320
|
-
(
|
|
321
|
-
}, 5e3) : (clearTimeout(o), (
|
|
322
|
-
},
|
|
323
|
-
var
|
|
324
|
-
|
|
319
|
+
var w;
|
|
320
|
+
(w = a.onConnectionStateChange) == null || w.call(a, S.Connected);
|
|
321
|
+
}, 5e3) : (clearTimeout(o), (h = a.onConnectionStateChange) == null || h.call(a, g));
|
|
322
|
+
}, f.ontrack = (g) => {
|
|
323
|
+
var h;
|
|
324
|
+
R("peerConnection.ontrack", g), (h = a.onSrcObjectReady) == null || h.call(a, g.streams[0]);
|
|
325
325
|
}, d.onmessage = (g) => {
|
|
326
|
-
var
|
|
326
|
+
var h;
|
|
327
327
|
if (d.readyState === "open") {
|
|
328
|
-
const [
|
|
329
|
-
|
|
328
|
+
const [w, p] = g.data.split(":");
|
|
329
|
+
w === $.StreamReady && (clearTimeout(o), (h = a.onConnectionStateChange) == null || h.call(a, S.Connected));
|
|
330
330
|
}
|
|
331
|
-
}, await
|
|
332
|
-
const
|
|
333
|
-
return
|
|
331
|
+
}, await f.setRemoteDescription(K), R("set remote description OK");
|
|
332
|
+
const l = await f.createAnswer();
|
|
333
|
+
return R("create answer OK"), await f.setLocalDescription(l), R("set local description OK"), await c(_, l, I), R("start connection OK"), {
|
|
334
334
|
/**
|
|
335
335
|
* Method to send request to server to get clip or talk depend on you payload
|
|
336
336
|
* @param payload
|
|
337
337
|
*/
|
|
338
338
|
speak(g) {
|
|
339
|
-
return
|
|
339
|
+
return u(_, I, g);
|
|
340
340
|
},
|
|
341
341
|
/**
|
|
342
342
|
* Method to close RTC connection
|
|
343
343
|
*/
|
|
344
344
|
async disconnect() {
|
|
345
|
-
var g,
|
|
345
|
+
var g, h, w;
|
|
346
346
|
if (_) {
|
|
347
|
-
if (
|
|
348
|
-
if (q(
|
|
347
|
+
if (f) {
|
|
348
|
+
if (q(f.iceConnectionState) === S.New) {
|
|
349
349
|
(g = a.onVideoStateChange) == null || g.call(a, N.Stop), clearInterval(m);
|
|
350
350
|
return;
|
|
351
351
|
}
|
|
352
|
-
|
|
352
|
+
f.close(), f.oniceconnectionstatechange = null, f.onnegotiationneeded = null, f.onicecandidate = null, f.ontrack = null;
|
|
353
353
|
}
|
|
354
354
|
try {
|
|
355
|
-
await
|
|
355
|
+
await y(_, I).catch((p) => {
|
|
356
356
|
});
|
|
357
357
|
} catch (p) {
|
|
358
|
-
|
|
358
|
+
R("Error on close stream connection", p);
|
|
359
359
|
}
|
|
360
|
-
(
|
|
360
|
+
(h = a.onConnectionStateChange) == null || h.call(a, S.New), (w = a.onVideoStateChange) == null || w.call(a, N.Stop), clearInterval(m);
|
|
361
361
|
}
|
|
362
362
|
},
|
|
363
363
|
/**
|
|
@@ -370,7 +370,7 @@ async function ye(e, i, {
|
|
|
370
370
|
streamId: _
|
|
371
371
|
};
|
|
372
372
|
}
|
|
373
|
-
function
|
|
373
|
+
function ye(e) {
|
|
374
374
|
var r, n;
|
|
375
375
|
const t = ((r = window == null ? void 0 : window.location) == null ? void 0 : r.hostname) === "studio.d-id.com" || ((n = window == null ? void 0 : window.location) == null ? void 0 : n.hostname) === "studio-staging.d-id.com" ? "studio" : window != null && window.hasOwnProperty("DID_AGENTS_API") ? "agents-ui" : "agents-sdk", a = {
|
|
376
376
|
token: e.token || "testKey",
|
|
@@ -407,11 +407,11 @@ function ve(e) {
|
|
|
407
407
|
}])
|
|
408
408
|
})
|
|
409
409
|
};
|
|
410
|
-
return fetch("https://api-js.mixpanel.com/track/?verbose=1&ip=1", c).then((
|
|
410
|
+
return fetch("https://api-js.mixpanel.com/track/?verbose=1&ip=1", c).then((u) => u.json()).catch((u) => console.error(u));
|
|
411
411
|
}
|
|
412
412
|
};
|
|
413
413
|
}
|
|
414
|
-
function
|
|
414
|
+
function ve(e) {
|
|
415
415
|
var a, r, n, s, o;
|
|
416
416
|
const i = () => /Mobi|Android/i.test(navigator.userAgent) ? "Mobile" : "Desktop";
|
|
417
417
|
return {
|
|
@@ -432,9 +432,9 @@ function Me(e) {
|
|
|
432
432
|
const Ce = 1080;
|
|
433
433
|
function _e(e, i) {
|
|
434
434
|
if (e.presenter) {
|
|
435
|
-
if (e.presenter.type ===
|
|
435
|
+
if (e.presenter.type === v.Clip)
|
|
436
436
|
return {
|
|
437
|
-
videoType:
|
|
437
|
+
videoType: v.Clip,
|
|
438
438
|
driver_id: e.presenter.driver_id,
|
|
439
439
|
presenter_id: e.presenter.presenter_id,
|
|
440
440
|
stream_warmup: !0
|
|
@@ -443,7 +443,7 @@ function _e(e, i) {
|
|
|
443
443
|
throw new Error("Presenter is not initialized");
|
|
444
444
|
const t = i || (e.presenter.stitch ? Ce : void 0);
|
|
445
445
|
return {
|
|
446
|
-
videoType:
|
|
446
|
+
videoType: v.Talk,
|
|
447
447
|
source_url: e.presenter.source_url,
|
|
448
448
|
stream_warmup: !0,
|
|
449
449
|
...t && {
|
|
@@ -454,14 +454,14 @@ function _e(e, i) {
|
|
|
454
454
|
function U(e, i, t, a, r) {
|
|
455
455
|
return new Promise(async (n, s) => {
|
|
456
456
|
let o = r;
|
|
457
|
-
const c = await
|
|
457
|
+
const c = await Me(e.id, _e(e, i.outputResolution), {
|
|
458
458
|
...i,
|
|
459
459
|
analytics: a,
|
|
460
460
|
callbacks: {
|
|
461
461
|
...i.callbacks,
|
|
462
|
-
onConnectionStateChange: async (
|
|
463
|
-
var
|
|
464
|
-
if (
|
|
462
|
+
onConnectionStateChange: async (u) => {
|
|
463
|
+
var y, M;
|
|
464
|
+
if (u === S.Connected)
|
|
465
465
|
try {
|
|
466
466
|
o || (o = await t.newChat(e.id), a.track("agent-chat", {
|
|
467
467
|
event: "created",
|
|
@@ -471,16 +471,16 @@ function U(e, i, t, a, r) {
|
|
|
471
471
|
chat: o,
|
|
472
472
|
streamingManager: c
|
|
473
473
|
});
|
|
474
|
-
} catch (
|
|
475
|
-
console.error(
|
|
474
|
+
} catch (E) {
|
|
475
|
+
console.error(E), s("Cannot create new chat");
|
|
476
476
|
}
|
|
477
477
|
else
|
|
478
|
-
|
|
479
|
-
(
|
|
478
|
+
u === S.Fail && s(new Error("Cannot create connection"));
|
|
479
|
+
(M = (y = i.callbacks).onConnectionStateChange) == null || M.call(y, u);
|
|
480
480
|
},
|
|
481
|
-
onVideoStateChange(
|
|
482
|
-
var
|
|
483
|
-
(
|
|
481
|
+
onVideoStateChange(u) {
|
|
482
|
+
var y, M;
|
|
483
|
+
(M = (y = i.callbacks).onVideoStateChange) == null || M.call(y, u);
|
|
484
484
|
}
|
|
485
485
|
}
|
|
486
486
|
}).catch(s);
|
|
@@ -504,7 +504,7 @@ function B(e) {
|
|
|
504
504
|
}];
|
|
505
505
|
}
|
|
506
506
|
async function Ie(e, i) {
|
|
507
|
-
var P, I,
|
|
507
|
+
var P, I, f;
|
|
508
508
|
const t = {
|
|
509
509
|
messages: [],
|
|
510
510
|
chatMode: i.mode || k.Functional
|
|
@@ -512,73 +512,77 @@ async function Ie(e, i) {
|
|
|
512
512
|
let a = -1;
|
|
513
513
|
const r = i.wsURL || oe, n = i.baseURL || L, s = i.mixpanelKey || ce, o = Y(i.auth, n, i.callbacks.onError), c = await o.getById(e);
|
|
514
514
|
t.messages = B(c), (I = (P = i.callbacks).onNewMessage) == null || I.call(P, t.messages);
|
|
515
|
-
const
|
|
515
|
+
const u = ye({
|
|
516
516
|
token: s,
|
|
517
517
|
agent: c,
|
|
518
518
|
...i
|
|
519
519
|
});
|
|
520
|
-
|
|
520
|
+
u.track("agent-sdk", {
|
|
521
521
|
event: "loaded",
|
|
522
|
-
...
|
|
522
|
+
...ve(c)
|
|
523
523
|
});
|
|
524
|
-
const
|
|
524
|
+
const y = {
|
|
525
525
|
onMessage: (d, m) => {
|
|
526
|
-
var
|
|
526
|
+
var l, g, h, w;
|
|
527
527
|
if ("content" in m) {
|
|
528
528
|
const {
|
|
529
529
|
content: p
|
|
530
530
|
} = m, C = t.messages[t.messages.length - 1];
|
|
531
|
-
(C == null ? void 0 : C.role) === "assistant" && (a < t.messages.length && (C.content = d === D.Partial ? C.content + p : p), d === D.Answer && (a = t.messages.length)), d === D.Answer &&
|
|
531
|
+
(C == null ? void 0 : C.role) === "assistant" && (a < t.messages.length && (C.content = d === D.Partial ? C.content + p : p), d === D.Answer && (a = t.messages.length)), d === D.Answer && u.track("agent-message-received", {
|
|
532
532
|
messages: t.messages.length
|
|
533
|
-
}), (g = (
|
|
533
|
+
}), (g = (l = i.callbacks).onNewMessage) == null || g.call(l, t.messages);
|
|
534
534
|
} else {
|
|
535
535
|
if ([$.StreamVideoCreated, $.StreamVideoDone, $.StreamVideoError, $.StreamVideoRejected].includes(d)) {
|
|
536
536
|
const p = d.split("/")[1];
|
|
537
|
-
|
|
537
|
+
u.track("agent-video", {
|
|
538
538
|
...m,
|
|
539
539
|
event: p
|
|
540
540
|
});
|
|
541
541
|
}
|
|
542
|
-
[$.StreamFailed, $.StreamVideoError, $.StreamVideoRejected].includes(d) && ((
|
|
542
|
+
[$.StreamFailed, $.StreamVideoError, $.StreamVideoRejected].includes(d) && ((w = (h = i.callbacks).onError) == null || w.call(h, new Error(`Stream failed with event ${d}`), {
|
|
543
543
|
data: m
|
|
544
544
|
}));
|
|
545
545
|
}
|
|
546
546
|
}
|
|
547
547
|
};
|
|
548
|
-
async function
|
|
549
|
-
var
|
|
550
|
-
const d = await j(i.auth, r,
|
|
548
|
+
async function M() {
|
|
549
|
+
var h, w, p, C, A;
|
|
550
|
+
const d = await j(i.auth, r, y), {
|
|
551
551
|
streamingManager: m,
|
|
552
|
-
chat:
|
|
553
|
-
} = await U(c, i, o,
|
|
554
|
-
a = -1, t.messages.length === 0 && (t.messages = B(c), (w = (
|
|
552
|
+
chat: l
|
|
553
|
+
} = await U(c, i, o, u, t.chat);
|
|
554
|
+
a = -1, t.messages.length === 0 && (t.messages = B(c), (w = (h = i.callbacks).onNewMessage) == null || w.call(h, t.messages)), l != null && l.id && l.id !== ((p = t.chat) == null ? void 0 : p.id) && ((A = (C = i.callbacks).onNewChat) == null || A.call(C, l == null ? void 0 : l.id)), t.streamingManager = m, t.socketManager = d, t.chat = l;
|
|
555
|
+
const g = t.chat.chatMode || k.Functional;
|
|
556
|
+
_(g), u.track("agent-chat", {
|
|
555
557
|
event: "connect",
|
|
556
|
-
chatId:
|
|
558
|
+
chatId: l.id,
|
|
557
559
|
agentId: c.id
|
|
558
560
|
});
|
|
559
561
|
}
|
|
560
|
-
async function
|
|
561
|
-
var d, m,
|
|
562
|
-
(d = t.socketManager) == null || d.disconnect(), await ((m = t.streamingManager) == null ? void 0 : m.disconnect()), delete t.streamingManager, delete t.socketManager, t.messages = B(c), (g = (
|
|
562
|
+
async function E() {
|
|
563
|
+
var d, m, l, g, h;
|
|
564
|
+
(d = t.socketManager) == null || d.disconnect(), await ((m = t.streamingManager) == null ? void 0 : m.disconnect()), delete t.streamingManager, delete t.socketManager, t.messages = B(c), (g = (l = i.callbacks).onNewMessage) == null || g.call(l, t.messages), u.track("agent-chat", {
|
|
563
565
|
event: "disconnect",
|
|
564
|
-
chatId: (
|
|
566
|
+
chatId: (h = t.chat) == null ? void 0 : h.id,
|
|
565
567
|
agentId: c.id
|
|
566
568
|
});
|
|
567
569
|
}
|
|
568
570
|
async function _(d) {
|
|
569
|
-
var m,
|
|
570
|
-
d !== t.chatMode && (
|
|
571
|
+
var m, l;
|
|
572
|
+
d !== t.chatMode && (u.track("agent-mode-change", {
|
|
571
573
|
mode: d
|
|
572
|
-
}), t.chatMode = d, t.chatMode !== k.Functional && await
|
|
574
|
+
}), t.chatMode = d, t.chatMode !== k.Functional && await E(), (l = (m = i.callbacks).onModeChange) == null || l.call(m, d));
|
|
573
575
|
}
|
|
574
576
|
async function K() {
|
|
575
577
|
if (!t.chat)
|
|
576
|
-
return
|
|
578
|
+
return M();
|
|
577
579
|
const {
|
|
578
580
|
streamingManager: d,
|
|
579
581
|
chat: m
|
|
580
|
-
} = await U(c, i, o,
|
|
581
|
-
t.streamingManager = d
|
|
582
|
+
} = await U(c, i, o, u, t.chat);
|
|
583
|
+
t.streamingManager = d;
|
|
584
|
+
const l = t.chat.chatMode || k.Functional;
|
|
585
|
+
_(l), u.track("agent-chat", {
|
|
582
586
|
event: "reconnect",
|
|
583
587
|
chatId: m.id,
|
|
584
588
|
agentId: c.id
|
|
@@ -586,28 +590,30 @@ async function Ie(e, i) {
|
|
|
586
590
|
}
|
|
587
591
|
return {
|
|
588
592
|
agent: c,
|
|
589
|
-
starterMessages: ((
|
|
590
|
-
connect:
|
|
591
|
-
disconnect:
|
|
593
|
+
starterMessages: ((f = c.knowledge) == null ? void 0 : f.starter_message) || [],
|
|
594
|
+
connect: M,
|
|
595
|
+
disconnect: E,
|
|
592
596
|
changeMode: _,
|
|
593
597
|
async reconnect() {
|
|
594
|
-
var
|
|
598
|
+
var h, w;
|
|
595
599
|
if (!t.chat)
|
|
596
|
-
return
|
|
597
|
-
(
|
|
598
|
-
const d = await j(i.auth, r,
|
|
600
|
+
return M();
|
|
601
|
+
(h = t.socketManager) == null || h.disconnect(), await ((w = t.streamingManager) == null ? void 0 : w.disconnect());
|
|
602
|
+
const d = await j(i.auth, r, y), {
|
|
599
603
|
streamingManager: m,
|
|
600
|
-
chat:
|
|
601
|
-
} = await U(c, i, o,
|
|
602
|
-
t.streamingManager = m, t.socketManager = d
|
|
604
|
+
chat: l
|
|
605
|
+
} = await U(c, i, o, u, t.chat);
|
|
606
|
+
t.streamingManager = m, t.socketManager = d;
|
|
607
|
+
const g = t.chat.chatMode || k.Functional;
|
|
608
|
+
_(g), u.track("agent-chat", {
|
|
603
609
|
event: "reconnect",
|
|
604
|
-
chatId:
|
|
610
|
+
chatId: l.id,
|
|
605
611
|
agentId: c.id
|
|
606
612
|
});
|
|
607
613
|
},
|
|
608
614
|
async chat(d, m = !1) {
|
|
609
|
-
var g,
|
|
610
|
-
const
|
|
615
|
+
var g, h, w, p, C, A, x, W, H;
|
|
616
|
+
const l = F();
|
|
611
617
|
try {
|
|
612
618
|
const O = Date.now();
|
|
613
619
|
if (d.length >= 800)
|
|
@@ -627,9 +633,9 @@ async function Ie(e, i) {
|
|
|
627
633
|
role: "user",
|
|
628
634
|
content: d,
|
|
629
635
|
created_at: new Date(O).toISOString()
|
|
630
|
-
}), (
|
|
636
|
+
}), (h = (g = i.callbacks).onNewMessage) == null || h.call(g, t.messages), t.chat || (t.chat = await o.newChat(c.id));
|
|
631
637
|
const b = {
|
|
632
|
-
id:
|
|
638
|
+
id: l,
|
|
633
639
|
role: "assistant",
|
|
634
640
|
content: "",
|
|
635
641
|
created_at: (/* @__PURE__ */ new Date()).toISOString(),
|
|
@@ -637,10 +643,10 @@ async function Ie(e, i) {
|
|
|
637
643
|
};
|
|
638
644
|
t.messages.push(b);
|
|
639
645
|
const J = t.messages.slice(0, -1);
|
|
640
|
-
let
|
|
646
|
+
let T;
|
|
641
647
|
try {
|
|
642
|
-
|
|
643
|
-
sessionId: (
|
|
648
|
+
T = await o.chat(c.id, t.chat.id, {
|
|
649
|
+
sessionId: (w = t.streamingManager) == null ? void 0 : w.sessionId,
|
|
644
650
|
streamId: (p = t.streamingManager) == null ? void 0 : p.streamId,
|
|
645
651
|
messages: J,
|
|
646
652
|
chatMode: t.chatMode,
|
|
@@ -648,9 +654,9 @@ async function Ie(e, i) {
|
|
|
648
654
|
});
|
|
649
655
|
} catch (z) {
|
|
650
656
|
if ((C = z == null ? void 0 : z.message) != null && C.includes("missing or invalid session_id"))
|
|
651
|
-
console.log("Invalid stream, try reconnect with new stream id"), await K(),
|
|
652
|
-
sessionId: (
|
|
653
|
-
streamId: (
|
|
657
|
+
console.log("Invalid stream, try reconnect with new stream id"), await K(), T = await o.chat(c.id, t.chat.id, {
|
|
658
|
+
sessionId: (A = t.streamingManager) == null ? void 0 : A.sessionId,
|
|
659
|
+
streamId: (x = t.streamingManager) == null ? void 0 : x.streamId,
|
|
654
660
|
messages: J,
|
|
655
661
|
chatMode: t.chatMode,
|
|
656
662
|
append_chat: m
|
|
@@ -658,44 +664,44 @@ async function Ie(e, i) {
|
|
|
658
664
|
else
|
|
659
665
|
throw z;
|
|
660
666
|
}
|
|
661
|
-
return
|
|
667
|
+
return u.track("agent-message-send", {
|
|
662
668
|
event: "success",
|
|
663
669
|
messages: t.messages.length + 1
|
|
664
|
-
}), b.context =
|
|
670
|
+
}), b.context = T.context, T.result && (b.content = T.result, b.matches = T.matches, u.track("agent-message-received", {
|
|
665
671
|
latency: Date.now() - O,
|
|
666
672
|
messages: t.messages.length
|
|
667
|
-
}), (H = (W = i.callbacks).onNewMessage) == null || H.call(W, t.messages)),
|
|
673
|
+
}), (H = (W = i.callbacks).onNewMessage) == null || H.call(W, t.messages)), T;
|
|
668
674
|
} catch (O) {
|
|
669
|
-
throw t.messages[t.messages.length - 1].id ===
|
|
675
|
+
throw t.messages[t.messages.length - 1].id === l && t.messages.pop(), u.track("agent-message-send", {
|
|
670
676
|
event: "error",
|
|
671
677
|
messages: t.messages.length
|
|
672
678
|
}), O;
|
|
673
679
|
}
|
|
674
680
|
},
|
|
675
|
-
rate(d, m,
|
|
676
|
-
var
|
|
677
|
-
const g = t.messages.find((
|
|
681
|
+
rate(d, m, l) {
|
|
682
|
+
var w, p, C, A;
|
|
683
|
+
const g = t.messages.find((x) => x.id === d);
|
|
678
684
|
if (t.chat) {
|
|
679
685
|
if (!g)
|
|
680
686
|
throw new Error("Message not found");
|
|
681
687
|
} else
|
|
682
688
|
throw new Error("Chat is not initialized");
|
|
683
|
-
const
|
|
684
|
-
return
|
|
685
|
-
event:
|
|
689
|
+
const h = ((w = g.matches) == null ? void 0 : w.map((x) => [x.document_id, x.id])) ?? [];
|
|
690
|
+
return u.track("agent-rate", {
|
|
691
|
+
event: l ? "update" : "create",
|
|
686
692
|
thumb: m === 1 ? "up" : "down",
|
|
687
693
|
knowledge_id: ((p = c.knowledge) == null ? void 0 : p.id) ?? "",
|
|
688
|
-
matches:
|
|
694
|
+
matches: h,
|
|
689
695
|
score: m
|
|
690
|
-
}),
|
|
696
|
+
}), l ? o.updateRating(c.id, t.chat.id, l, {
|
|
691
697
|
knowledge_id: ((C = c.knowledge) == null ? void 0 : C.id) ?? "",
|
|
692
698
|
message_id: d,
|
|
693
|
-
matches:
|
|
699
|
+
matches: h,
|
|
694
700
|
score: m
|
|
695
701
|
}) : o.createRating(c.id, t.chat.id, {
|
|
696
|
-
knowledge_id: ((
|
|
702
|
+
knowledge_id: ((A = c.knowledge) == null ? void 0 : A.id) ?? "",
|
|
697
703
|
message_id: d,
|
|
698
|
-
matches:
|
|
704
|
+
matches: h,
|
|
699
705
|
score: m
|
|
700
706
|
});
|
|
701
707
|
},
|
|
@@ -708,7 +714,7 @@ async function Ie(e, i) {
|
|
|
708
714
|
var m;
|
|
709
715
|
if (!t.chat)
|
|
710
716
|
throw new Error("Chat is not initialized");
|
|
711
|
-
return
|
|
717
|
+
return u.track("agent-rate-delete", {
|
|
712
718
|
type: "text",
|
|
713
719
|
chat_id: (m = t.chat) == null ? void 0 : m.id,
|
|
714
720
|
id: d
|
|
@@ -735,9 +741,9 @@ async function Ie(e, i) {
|
|
|
735
741
|
throw new Error("Presenter is not initialized");
|
|
736
742
|
throw new Error("Invalid payload");
|
|
737
743
|
}
|
|
738
|
-
const
|
|
739
|
-
return
|
|
740
|
-
script:
|
|
744
|
+
const l = m();
|
|
745
|
+
return u.track("agent-speak", l), t.streamingManager.speak({
|
|
746
|
+
script: l
|
|
741
747
|
});
|
|
742
748
|
}
|
|
743
749
|
};
|
|
@@ -755,7 +761,7 @@ export {
|
|
|
755
761
|
N as StreamingState,
|
|
756
762
|
ne as Subject,
|
|
757
763
|
G as UserPlan,
|
|
758
|
-
|
|
764
|
+
v as VideoType,
|
|
759
765
|
se as VoiceAccess,
|
|
760
766
|
Ie as createAgentManager,
|
|
761
767
|
Se as getAgent
|
package/dist/index.umd.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
(function(h,P){typeof exports=="object"&&typeof module<"u"?P(exports):typeof define=="function"&&define.amd?define(["exports"],P):(h=typeof globalThis<"u"?globalThis:h||self,P(h.index={}))})(this,function(h){"use strict";var P=(e=>(e.TRIAL="trial",e.BASIC="basic",e.ENTERPRISE="enterprise",e.LITE="lite",e.ADVANCED="advanced",e))(P||{}),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||{}),J=(e=>(e.Unrated="Unrated",e.Positive="Positive",e.Negative="Negative",e))(J||{}),A=(e=>(e.Functional="Functional",e.TextOnly="TextOnly",e.Maintenance="Maintenance",e.Playground="Playground",e))(A||{}),x=(e=>(e.Embed="embed",e.Query="query",e.Partial="partial",e.Answer="answer",e.Complete="done",e))(x||{}),q=(e=>(e.KnowledgeProcessing="knowledge/processing",e.KnowledgeIndexing="knowledge/indexing",e.KnowledgeFailed="knowledge/error",e.KnowledgeDone="knowledge/done",e))(q||{}),Q=(e=>(e.Knowledge="knowledge",e.Document="document",e.Record="record",e))(Q||{}),X=(e=>(e.Pdf="pdf",e.Text="text",e.Html="html",e.Word="word",e.Json="json",e.Markdown="markdown",e.Csv="csv",e.Excel="excel",e.Powerpoint="powerpoint",e.Archive="archive",e.Image="image",e.Audio="audio",e.Video="video",e))(X||{}),M=(e=>(e.Clip="clip",e.Talk="talk",e))(M||{}),b=(e=>(e.Start="START",e.Stop="STOP",e))(b||{}),k=(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))(k||{}),_=(e=>(e[e.New=0]="New",e[e.Fail=1]="Fail",e[e.Connected=2]="Connected",e[e.Connecting=3]="Connecting",e[e.Terminating=4]="Terminating",e))(_||{}),Y=(e=>(e.Amazon="amazon",e.Microsoft="microsoft",e.Afflorithmics="afflorithmics",e.Elevenlabs="elevenlabs",e))(Y||{}),Z=(e=>(e.Public="public",e.Premium="premium",e.Private="private",e))(Z||{});const L="https://api.d-id.com",ce="wss://notifications.d-id.com",de="79f81a83a67430be2bc0fd61042b8faa",D=()=>Math.random().toString(16).slice(2);function G(){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 ge=D();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}.${G()}_${ge}`;throw new Error(`Unknown auth type: ${e}`)}function V(e,i=L,t){const a=async(r,n)=>{const s=await fetch(i+(r!=null&&r.startsWith("/")?r:`/${r}`),{...n,headers:{...n==null?void 0:n.headers,Authorization:ee(e),"Content-Type":"application/json"}});if(!s.ok){let o=await s.text().catch(()=>"Failed to fetch");throw t&&t(new Error(o),{url:r,options:n,headers:s.headers}),new Error(o)}return s.json()};return{get(r,n){return a(r,{...n,method:"GET"})},post(r,n,s){return a(r,{...s,body:JSON.stringify(n),method:"POST"})},delete(r,n,s){return a(r,{...s,body:JSON.stringify(n),method:"DELETE"})},patch(r,n,s){return a(r,{...s,body:JSON.stringify(n),method:"PATCH"})}}}function te(e,i=L,t){const a=V(e,`${i}/agents`,t);return{create(r,n){return a.post("/",r,n)},getAgents(r,n){return a.get(`/${r?`?tag=${r}`:""}`,n).then(s=>s??[])},getById(r,n){return a.get(`/${r}`,n)},delete(r,n){return a.delete(`/${r}`,void 0,n)},update(r,n,s){return a.patch(`/${r}`,n,s)},newChat(r,n){return a.post(`/${r}/chat`,void 0,n)},chat(r,n,s,o){return a.post(`/${r}/chat/${n}`,s,o)},getChatMode(r,n,s){return a.get(`/${r}/chat/${n}/mode`,s)},createRating(r,n,s,o){return a.post(`/${r}/chat/${n}/ratings`,s,o)},updateRating(r,n,s,o,c){return a.patch(`/${r}/chat/${n}/ratings/${s}`,o,c)},deleteRating(r,n,s,o){return a.delete(`/${r}/chat/${n}/ratings/${s}`,o)}}}const le=e=>new Promise(i=>setTimeout(i,e));function ue(e){return new Promise((i,t)=>{const{callbacks:a,host:r,auth:n}=e,{onMessage:s=null,onOpen:o=null,onClose:c=null,onError:l=null}=a||{},C=new WebSocket(`${r}?authorization=${ee(n)}`);C.onmessage=s,C.onclose=c,C.onerror=v=>{console.error(v),l==null||l("Websocket failed to connect",v),t(v)},C.onopen=v=>{o==null||o(v),i(C)}})}async function me(e){const{retries:i=1}=e;let t=null;for(let a=0;(t==null?void 0:t.readyState)!==WebSocket.OPEN;a++)try{t=await ue(e)}catch(r){if(a===i)throw r;await le(a*500)}return t}async function ne(e,i,t){const a=t!=null&&t.onMessage?[t.onMessage]:[],r=await me({auth:e,host:i,callbacks:{onError:t==null?void 0:t.onError,onMessage:n=>{const s=JSON.parse(n.data);a.forEach(o=>o(s.event,s))}}});return{socket:r,disconnect:()=>r.close(),subscribeToEvents:n=>a.push(n)}}function we(e,i,t,a){const r=V(e,`${i}/agents/${t}`,a);return{createStream(n){return r.post("/streams",{driver_id:n.driver_id,presenter_id:n.presenter_id,compatibility_mode:n.compatibility_mode,stream_warmup:n.stream_warmup,type:M.Clip})},startConnection(n,s,o){return r.post(`/streams/${n}/sdp`,{session_id:o,answer:s,type:M.Clip})},addIceCandidate(n,s,o){return r.post(`/streams/${n}/ice`,{session_id:o,...s,type:M.Clip})},sendStreamRequest(n,s,o){return r.post(`/streams/${n}`,{session_id:s,...o,type:M.Clip})},close(n,s){return r.delete(`/streams/${n}`,{session_id:s,type:M.Clip})}}}function he(e,i,t,a){const r=V(e,`${i}/agents/${t}`,a);return{createStream(n,s){return r.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,type:M.Talk},s)},startConnection(n,s,o,c){return r.post(`/streams/${n}/sdp`,{session_id:o,answer:s,type:M.Talk},c)},addIceCandidate(n,s,o,c){return r.post(`/streams/${n}/ice`,{session_id:o,...s,type:M.Talk},c)},sendStreamRequest(n,s,o,c){return r.post(`/streams/${n}`,{session_id:s,...o,type:M.Talk},c)},close(n,s,o){return r.delete(`/streams/${n}`,{session_id:s,type:M.Talk},o)}}}let re=!1;const R=(e,i)=>re&&console.log(e,i),fe=(window.RTCPeerConnection||window.webkitRTCPeerConnection||window.mozRTCPeerConnection).bind(window);function ae(e){switch(e){case"connected":return _.Connected;case"checking":return _.Connecting;case"failed":return _.Fail;case"new":case"closed":case"disconnected":default:return _.New}}function pe(){let e=0;return i=>{for(const t of i.values())if(t&&t.type==="inbound-rtp"&&t.kind==="video"){const a=t.bytesReceived,r=a-e>0;return e=a,r}return!1}}function ye(e,i,t){const r=Math.max(Math.ceil(10),1);let n=0,s=!1;const o=pe();return setInterval(async()=>{const c=await e.getStats();o(c)?(n=0,s||(i==null||i(b.Start),s=!0)):s&&(n++,n>=r&&(i==null||i(b.Stop),s=!1))},100)}async function ve(e,i,{debug:t=!1,callbacks:a,auth:r,analytics:n,baseURL:s=L}){re=t;let o;const{startConnection:c,sendStreamRequest:l,close:C,createStream:v,addIceCandidate:E}=i.videoType===M.Clip?we(r,s,e,a.onError):he(r,s,e,a.onError),{id:I,offer:W,ice_servers:z,session_id:$}=await v(i),f=new fe({iceServers:z}),d=f.createDataChannel("JanusDataChannel");if(!$)throw new Error("Could not create session_id");const m=ye(f,a.onVideoStateChange);f.onicecandidate=g=>{R("peerConnection.onicecandidate",g),g.candidate&&g.candidate.sdpMid&&g.candidate.sdpMLineIndex!==null?E(I,{candidate:g.candidate.candidate,sdpMid:g.candidate.sdpMid,sdpMLineIndex:g.candidate.sdpMLineIndex},$):E(I,{candidate:null},$)},f.oniceconnectionstatechange=()=>{var w;R("peerConnection.oniceconnectionstatechange => "+f.iceConnectionState);const g=ae(f.iceConnectionState);g===_.Connected?o=setTimeout(()=>{var p;(p=a.onConnectionStateChange)==null||p.call(a,_.Connected)},5e3):(clearTimeout(o),(w=a.onConnectionStateChange)==null||w.call(a,g))},f.ontrack=g=>{var w;R("peerConnection.ontrack",g),(w=a.onSrcObjectReady)==null||w.call(a,g.streams[0])},d.onmessage=g=>{var w;if(d.readyState==="open"){const[p,y]=g.data.split(":");p===k.StreamReady&&(clearTimeout(o),(w=a.onConnectionStateChange)==null||w.call(a,_.Connected))}},await f.setRemoteDescription(W),R("set remote description OK");const u=await f.createAnswer();return R("create answer OK"),await f.setLocalDescription(u),R("set local description OK"),await c(I,u,$),R("start connection OK"),{speak(g){return l(I,$,g)},async disconnect(){var g,w,p;if(I){if(f){if(ae(f.iceConnectionState)===_.New){(g=a.onVideoStateChange)==null||g.call(a,b.Stop),clearInterval(m);return}f.close(),f.oniceconnectionstatechange=null,f.onnegotiationneeded=null,f.onicecandidate=null,f.ontrack=null}try{await C(I,$).catch(y=>{})}catch(y){R("Error on close stream connection",y)}(w=a.onConnectionStateChange)==null||w.call(a,_.New),(p=a.onVideoStateChange)==null||p.call(a,b.Stop),clearInterval(m)}},sessionId:$,streamId:I}}function Me(e){var r,n;const t=((r=window==null?void 0:window.location)==null?void 0:r.hostname)==="studio.d-id.com"||((n=window==null?void 0:window.location)==null?void 0:n.hostname)==="studio-staging.d-id.com"?"studio":window!=null&&window.hasOwnProperty("DID_AGENTS_API")?"agents-ui":"agents-sdk",a={token:e.token||"testKey",distinct_id:e.distinctId||G(),isEnabled:e.isEnabled??!0,agentId:e.agent.id,owner_id:e.agent.owner_id??""};return{...a,getRandom:()=>Math.random().toString(16).slice(2),track(s,o){if(!this.isEnabled)return Promise.reject("MixPanel analytics is disabled on creation");const c={method:"POST",headers:{"Content-Type":"application/x-www-form-urlencoded"},body:new URLSearchParams({data:JSON.stringify([{event:s,properties:{...o,...a,source:t,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",c).then(l=>l.json()).catch(l=>console.error(l))}}}function Ce(e){var a,r,n,s,o;const i=()=>/Mobi|Android/i.test(navigator.userAgent)?"Mobile":"Desktop";return{$os:`${(()=>{const c=navigator.platform;return c.toLowerCase().includes("win")?"Windows":c.toLowerCase().includes("mac")?"Mac OS X":c.toLowerCase().includes("linux")?"Linux":"Unknown"})()}`,isMobile:`${i()=="Mobile"}`,browser:navigator.userAgent,origin:window.location.origin,agentType:(a=e.presenter)==null?void 0:a.type,agentVoice:{voiceId:(n=(r=e.presenter)==null?void 0:r.voice)==null?void 0:n.voice_id,provider:(o=(s=e.presenter)==null?void 0:s.voice)==null?void 0:o.type}}}const Se=1080;function _e(e,i){if(e.presenter){if(e.presenter.type===M.Clip)return{videoType:M.Clip,driver_id:e.presenter.driver_id,presenter_id:e.presenter.presenter_id,stream_warmup:!0}}else throw new Error("Presenter is not initialized");const t=i||(e.presenter.stitch?Se:void 0);return{videoType:M.Talk,source_url:e.presenter.source_url,stream_warmup:!0,...t&&{output_resolution:t}}}function B(e,i,t,a,r){return new Promise(async(n,s)=>{let o=r;const c=await ve(e.id,_e(e,i.outputResolution),{...i,analytics:a,callbacks:{...i.callbacks,onConnectionStateChange:async l=>{var C,v;if(l===_.Connected)try{o||(o=await t.newChat(e.id),a.track("agent-chat",{event:"created",chat_id:o.id,agent_id:e.id})),c&&n({chat:o,streamingManager:c})}catch(E){console.error(E),s("Cannot create new chat")}else l===_.Fail&&s(new Error("Cannot create connection"));(v=(C=i.callbacks).onConnectionStateChange)==null||v.call(C,l)},onVideoStateChange(l){var C,v;(v=(C=i.callbacks).onVideoStateChange)==null||v.call(C,l)}}}).catch(s)})}function Ie(e,i,t){return te(i,t||L).getById(e)}function j(e){let i="";if(e.greetings&&e.greetings.length>0){const t=Math.floor(Math.random()*e.greetings.length);i=e.greetings[t]}else i=`Hi! I'm ${e.preview_name||"My Agent"}. How can I help you?`;return[{content:i,id:D(),role:"assistant",created_at:new Date().toISOString()}]}async function Ae(e,i){var z,$,f;const t={messages:[],chatMode:i.mode||A.Functional};let a=-1;const r=i.wsURL||ce,n=i.baseURL||L,s=i.mixpanelKey||de,o=te(i.auth,n,i.callbacks.onError),c=await o.getById(e);t.messages=j(c),($=(z=i.callbacks).onNewMessage)==null||$.call(z,t.messages);const l=Me({token:s,agent:c,...i});l.track("agent-sdk",{event:"loaded",...Ce(c)});const C={onMessage:(d,m)=>{var u,g,w,p;if("content"in m){const{content:y}=m,S=t.messages[t.messages.length-1];(S==null?void 0:S.role)==="assistant"&&(a<t.messages.length&&(S.content=d===x.Partial?S.content+y:y),d===x.Answer&&(a=t.messages.length)),d===x.Answer&&l.track("agent-message-received",{messages:t.messages.length}),(g=(u=i.callbacks).onNewMessage)==null||g.call(u,t.messages)}else{if([k.StreamVideoCreated,k.StreamVideoDone,k.StreamVideoError,k.StreamVideoRejected].includes(d)){const y=d.split("/")[1];l.track("agent-video",{...m,event:y})}[k.StreamFailed,k.StreamVideoError,k.StreamVideoRejected].includes(d)&&((p=(w=i.callbacks).onError)==null||p.call(w,new Error(`Stream failed with event ${d}`),{data:m}))}}};async function v(){var g,w,p,y,S;const d=await ne(i.auth,r,C),{streamingManager:m,chat:u}=await B(c,i,o,l,t.chat);a=-1,t.messages.length===0&&(t.messages=j(c),(w=(g=i.callbacks).onNewMessage)==null||w.call(g,t.messages)),u!=null&&u.id&&u.id!==((p=t.chat)==null?void 0:p.id)&&((S=(y=i.callbacks).onNewChat)==null||S.call(y,u==null?void 0:u.id)),t.streamingManager=m,t.socketManager=d,t.chat=u,I(A.Functional),l.track("agent-chat",{event:"connect",chatId:u.id,agentId:c.id})}async function E(){var d,m,u,g,w;(d=t.socketManager)==null||d.disconnect(),await((m=t.streamingManager)==null?void 0:m.disconnect()),delete t.streamingManager,delete t.socketManager,t.messages=j(c),(g=(u=i.callbacks).onNewMessage)==null||g.call(u,t.messages),l.track("agent-chat",{event:"disconnect",chatId:(w=t.chat)==null?void 0:w.id,agentId:c.id})}async function I(d){var m,u;d!==t.chatMode&&(l.track("agent-mode-change",{mode:d}),t.chatMode=d,t.chatMode!==A.Functional&&await E(),(u=(m=i.callbacks).onModeChange)==null||u.call(m,d))}async function W(){if(!t.chat)return v();const{streamingManager:d,chat:m}=await B(c,i,o,l,t.chat);t.streamingManager=d,I(A.Functional),l.track("agent-chat",{event:"reconnect",chatId:m.id,agentId:c.id})}return{agent:c,starterMessages:((f=c.knowledge)==null?void 0:f.starter_message)||[],connect:v,disconnect:E,changeMode:I,async reconnect(){var g,w;if(!t.chat)return v();(g=t.socketManager)==null||g.disconnect(),await((w=t.streamingManager)==null?void 0:w.disconnect());const d=await ne(i.auth,r,C),{streamingManager:m,chat:u}=await B(c,i,o,l,t.chat);t.streamingManager=m,t.socketManager=d,I(A.Functional),l.track("agent-chat",{event:"reconnect",chatId:u.id,agentId:c.id})},async chat(d,m=!1){var g,w,p,y,S,O,T,ie,se;const u=D();try{const F=Date.now();if(d.length>=800)throw new Error("Message cannot be more than 800 characters");if(d.length===0)throw new Error("Message cannot be empty");if(t.chatMode===A.Maintenance)throw new Error("Chat is in maintenance mode");if(![A.TextOnly,A.Playground].includes(t.chatMode)){if(!t.streamingManager)throw new Error("Streaming manager is not initialized");if(!t.chat)throw new Error("Chat is not initialized")}t.messages.push({id:D(),role:"user",content:d,created_at:new Date(F).toISOString()}),(w=(g=i.callbacks).onNewMessage)==null||w.call(g,t.messages),t.chat||(t.chat=await o.newChat(c.id));const K={id:u,role:"assistant",content:"",created_at:new Date().toISOString(),matches:[]};t.messages.push(K);const oe=t.messages.slice(0,-1);let N;try{N=await o.chat(c.id,t.chat.id,{sessionId:(p=t.streamingManager)==null?void 0:p.sessionId,streamId:(y=t.streamingManager)==null?void 0:y.streamId,messages:oe,chatMode:t.chatMode,append_chat:m})}catch(U){if((S=U==null?void 0:U.message)!=null&&S.includes("missing or invalid session_id"))console.log("Invalid stream, try reconnect with new stream id"),await W(),N=await o.chat(c.id,t.chat.id,{sessionId:(O=t.streamingManager)==null?void 0:O.sessionId,streamId:(T=t.streamingManager)==null?void 0:T.streamId,messages:oe,chatMode:t.chatMode,append_chat:m});else throw U}return l.track("agent-message-send",{event:"success",messages:t.messages.length+1}),K.context=N.context,N.result&&(K.content=N.result,K.matches=N.matches,l.track("agent-message-received",{latency:Date.now()-F,messages:t.messages.length}),(se=(ie=i.callbacks).onNewMessage)==null||se.call(ie,t.messages)),N}catch(F){throw t.messages[t.messages.length-1].id===u&&t.messages.pop(),l.track("agent-message-send",{event:"error",messages:t.messages.length}),F}},rate(d,m,u){var p,y,S,O;const g=t.messages.find(T=>T.id===d);if(t.chat){if(!g)throw new Error("Message not found")}else throw new Error("Chat is not initialized");const w=((p=g.matches)==null?void 0:p.map(T=>[T.document_id,T.id]))??[];return l.track("agent-rate",{event:u?"update":"create",thumb:m===1?"up":"down",knowledge_id:((y=c.knowledge)==null?void 0:y.id)??"",matches:w,score:m}),u?o.updateRating(c.id,t.chat.id,u,{knowledge_id:((S=c.knowledge)==null?void 0:S.id)??"",message_id:d,matches:w,score:m}):o.createRating(c.id,t.chat.id,{knowledge_id:((O=c.knowledge)==null?void 0:O.id)??"",message_id:d,matches:w,score:m})},getChatmode(){if(!t.chat)throw new Error("Chat is not initialized");return o.getChatMode(c.id,t.chat.id)},deleteRate(d){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:d}),o.deleteRating(c.id,t.chat.id,d)},speak(d){if(!t.streamingManager)throw new Error("Streaming manager is not initialized");function m(){if(c.presenter){if(d.type==="text")return{type:"text",provider:d.provider?d.provider:c.presenter.voice,input:d.input,ssml:d.ssml||!1};if(d.type==="audio")return{type:"audio",audio_url:d.audio_url}}else throw new Error("Presenter is not initialized");throw new Error("Invalid payload")}const u=m();return l.track("agent-speak",u),t.streamingManager.speak({script:u})}}}h.ChatMode=A,h.ChatProgress=x,h.ConnectionState=_,h.DocumentType=X,h.KnowledgeType=Q,h.PlanGroup=H,h.Providers=Y,h.RateState=J,h.StreamEvents=k,h.StreamingState=b,h.Subject=q,h.UserPlan=P,h.VideoType=M,h.VoiceAccess=Z,h.createAgentManager=Ae,h.getAgent=Ie,Object.defineProperty(h,Symbol.toStringTag,{value:"Module"})});
|
|
1
|
+
(function(f,b){typeof exports=="object"&&typeof module<"u"?b(exports):typeof define=="function"&&define.amd?define(["exports"],b):(f=typeof globalThis<"u"?globalThis:f||self,b(f.index={}))})(this,function(f){"use strict";var b=(e=>(e.TRIAL="trial",e.BASIC="basic",e.ENTERPRISE="enterprise",e.LITE="lite",e.ADVANCED="advanced",e))(b||{}),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||{}),J=(e=>(e.Unrated="Unrated",e.Positive="Positive",e.Negative="Negative",e))(J||{}),A=(e=>(e.Functional="Functional",e.TextOnly="TextOnly",e.Maintenance="Maintenance",e.Playground="Playground",e))(A||{}),L=(e=>(e.Embed="embed",e.Query="query",e.Partial="partial",e.Answer="answer",e.Complete="done",e))(L||{}),q=(e=>(e.KnowledgeProcessing="knowledge/processing",e.KnowledgeIndexing="knowledge/indexing",e.KnowledgeFailed="knowledge/error",e.KnowledgeDone="knowledge/done",e))(q||{}),Q=(e=>(e.Knowledge="knowledge",e.Document="document",e.Record="record",e))(Q||{}),X=(e=>(e.Pdf="pdf",e.Text="text",e.Html="html",e.Word="word",e.Json="json",e.Markdown="markdown",e.Csv="csv",e.Excel="excel",e.Powerpoint="powerpoint",e.Archive="archive",e.Image="image",e.Audio="audio",e.Video="video",e))(X||{}),v=(e=>(e.Clip="clip",e.Talk="talk",e))(v||{}),x=(e=>(e.Start="START",e.Stop="STOP",e))(x||{}),k=(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))(k||{}),_=(e=>(e[e.New=0]="New",e[e.Fail=1]="Fail",e[e.Connected=2]="Connected",e[e.Connecting=3]="Connecting",e[e.Terminating=4]="Terminating",e))(_||{}),Y=(e=>(e.Amazon="amazon",e.Microsoft="microsoft",e.Afflorithmics="afflorithmics",e.Elevenlabs="elevenlabs",e))(Y||{}),Z=(e=>(e.Public="public",e.Premium="premium",e.Private="private",e))(Z||{});const O="https://api.d-id.com",ce="wss://notifications.d-id.com",de="79f81a83a67430be2bc0fd61042b8faa",D=()=>Math.random().toString(16).slice(2);function G(){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 ge=D();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}.${G()}_${ge}`;throw new Error(`Unknown auth type: ${e}`)}function V(e,i=O,t){const a=async(r,n)=>{const s=await fetch(i+(r!=null&&r.startsWith("/")?r:`/${r}`),{...n,headers:{...n==null?void 0:n.headers,Authorization:ee(e),"Content-Type":"application/json"}});if(!s.ok){let o=await s.text().catch(()=>"Failed to fetch");throw t&&t(new Error(o),{url:r,options:n,headers:s.headers}),new Error(o)}return s.json()};return{get(r,n){return a(r,{...n,method:"GET"})},post(r,n,s){return a(r,{...s,body:JSON.stringify(n),method:"POST"})},delete(r,n,s){return a(r,{...s,body:JSON.stringify(n),method:"DELETE"})},patch(r,n,s){return a(r,{...s,body:JSON.stringify(n),method:"PATCH"})}}}function te(e,i=O,t){const a=V(e,`${i}/agents`,t);return{create(r,n){return a.post("/",r,n)},getAgents(r,n){return a.get(`/${r?`?tag=${r}`:""}`,n).then(s=>s??[])},getById(r,n){return a.get(`/${r}`,n)},delete(r,n){return a.delete(`/${r}`,void 0,n)},update(r,n,s){return a.patch(`/${r}`,n,s)},newChat(r,n){return a.post(`/${r}/chat`,void 0,n)},chat(r,n,s,o){return a.post(`/${r}/chat/${n}`,s,o)},getChatMode(r,n,s){return a.get(`/${r}/chat/${n}/mode`,s)},createRating(r,n,s,o){return a.post(`/${r}/chat/${n}/ratings`,s,o)},updateRating(r,n,s,o,c){return a.patch(`/${r}/chat/${n}/ratings/${s}`,o,c)},deleteRating(r,n,s,o){return a.delete(`/${r}/chat/${n}/ratings/${s}`,o)}}}const le=e=>new Promise(i=>setTimeout(i,e));function ue(e){return new Promise((i,t)=>{const{callbacks:a,host:r,auth:n}=e,{onMessage:s=null,onOpen:o=null,onClose:c=null,onError:u=null}=a||{},C=new WebSocket(`${r}?authorization=${ee(n)}`);C.onmessage=s,C.onclose=c,C.onerror=M=>{console.error(M),u==null||u("Websocket failed to connect",M),t(M)},C.onopen=M=>{o==null||o(M),i(C)}})}async function me(e){const{retries:i=1}=e;let t=null;for(let a=0;(t==null?void 0:t.readyState)!==WebSocket.OPEN;a++)try{t=await ue(e)}catch(r){if(a===i)throw r;await le(a*500)}return t}async function ne(e,i,t){const a=t!=null&&t.onMessage?[t.onMessage]:[],r=await me({auth:e,host:i,callbacks:{onError:t==null?void 0:t.onError,onMessage:n=>{const s=JSON.parse(n.data);a.forEach(o=>o(s.event,s))}}});return{socket:r,disconnect:()=>r.close(),subscribeToEvents:n=>a.push(n)}}function he(e,i,t,a){const r=V(e,`${i}/agents/${t}`,a);return{createStream(n){return r.post("/streams",{driver_id:n.driver_id,presenter_id:n.presenter_id,compatibility_mode:n.compatibility_mode,stream_warmup:n.stream_warmup,type:v.Clip})},startConnection(n,s,o){return r.post(`/streams/${n}/sdp`,{session_id:o,answer:s,type:v.Clip})},addIceCandidate(n,s,o){return r.post(`/streams/${n}/ice`,{session_id:o,...s,type:v.Clip})},sendStreamRequest(n,s,o){return r.post(`/streams/${n}`,{session_id:s,...o,type:v.Clip})},close(n,s){return r.delete(`/streams/${n}`,{session_id:s,type:v.Clip})}}}function we(e,i,t,a){const r=V(e,`${i}/agents/${t}`,a);return{createStream(n,s){return r.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,type:v.Talk},s)},startConnection(n,s,o,c){return r.post(`/streams/${n}/sdp`,{session_id:o,answer:s,type:v.Talk},c)},addIceCandidate(n,s,o,c){return r.post(`/streams/${n}/ice`,{session_id:o,...s,type:v.Talk},c)},sendStreamRequest(n,s,o,c){return r.post(`/streams/${n}`,{session_id:s,...o,type:v.Talk},c)},close(n,s,o){return r.delete(`/streams/${n}`,{session_id:s,type:v.Talk},o)}}}let re=!1;const R=(e,i)=>re&&console.log(e,i),fe=(window.RTCPeerConnection||window.webkitRTCPeerConnection||window.mozRTCPeerConnection).bind(window);function ae(e){switch(e){case"connected":return _.Connected;case"checking":return _.Connecting;case"failed":return _.Fail;case"new":case"closed":case"disconnected":default:return _.New}}function pe(){let e=0;return i=>{for(const t of i.values())if(t&&t.type==="inbound-rtp"&&t.kind==="video"){const a=t.bytesReceived,r=a-e>0;return e=a,r}return!1}}function ye(e,i,t){const r=Math.max(Math.ceil(10),1);let n=0,s=!1;const o=pe();return setInterval(async()=>{const c=await e.getStats();o(c)?(n=0,s||(i==null||i(x.Start),s=!0)):s&&(n++,n>=r&&(i==null||i(x.Stop),s=!1))},100)}async function Me(e,i,{debug:t=!1,callbacks:a,auth:r,analytics:n,baseURL:s=O}){re=t;let o;const{startConnection:c,sendStreamRequest:u,close:C,createStream:M,addIceCandidate:T}=i.videoType===v.Clip?he(r,s,e,a.onError):we(r,s,e,a.onError),{id:I,offer:W,ice_servers:z,session_id:$}=await M(i),p=new fe({iceServers:z}),d=p.createDataChannel("JanusDataChannel");if(!$)throw new Error("Could not create session_id");const m=ye(p,a.onVideoStateChange);p.onicecandidate=g=>{R("peerConnection.onicecandidate",g),g.candidate&&g.candidate.sdpMid&&g.candidate.sdpMLineIndex!==null?T(I,{candidate:g.candidate.candidate,sdpMid:g.candidate.sdpMid,sdpMLineIndex:g.candidate.sdpMLineIndex},$):T(I,{candidate:null},$)},p.oniceconnectionstatechange=()=>{var h;R("peerConnection.oniceconnectionstatechange => "+p.iceConnectionState);const g=ae(p.iceConnectionState);g===_.Connected?o=setTimeout(()=>{var w;(w=a.onConnectionStateChange)==null||w.call(a,_.Connected)},5e3):(clearTimeout(o),(h=a.onConnectionStateChange)==null||h.call(a,g))},p.ontrack=g=>{var h;R("peerConnection.ontrack",g),(h=a.onSrcObjectReady)==null||h.call(a,g.streams[0])},d.onmessage=g=>{var h;if(d.readyState==="open"){const[w,y]=g.data.split(":");w===k.StreamReady&&(clearTimeout(o),(h=a.onConnectionStateChange)==null||h.call(a,_.Connected))}},await p.setRemoteDescription(W),R("set remote description OK");const l=await p.createAnswer();return R("create answer OK"),await p.setLocalDescription(l),R("set local description OK"),await c(I,l,$),R("start connection OK"),{speak(g){return u(I,$,g)},async disconnect(){var g,h,w;if(I){if(p){if(ae(p.iceConnectionState)===_.New){(g=a.onVideoStateChange)==null||g.call(a,x.Stop),clearInterval(m);return}p.close(),p.oniceconnectionstatechange=null,p.onnegotiationneeded=null,p.onicecandidate=null,p.ontrack=null}try{await C(I,$).catch(y=>{})}catch(y){R("Error on close stream connection",y)}(h=a.onConnectionStateChange)==null||h.call(a,_.New),(w=a.onVideoStateChange)==null||w.call(a,x.Stop),clearInterval(m)}},sessionId:$,streamId:I}}function ve(e){var r,n;const t=((r=window==null?void 0:window.location)==null?void 0:r.hostname)==="studio.d-id.com"||((n=window==null?void 0:window.location)==null?void 0:n.hostname)==="studio-staging.d-id.com"?"studio":window!=null&&window.hasOwnProperty("DID_AGENTS_API")?"agents-ui":"agents-sdk",a={token:e.token||"testKey",distinct_id:e.distinctId||G(),isEnabled:e.isEnabled??!0,agentId:e.agent.id,owner_id:e.agent.owner_id??""};return{...a,getRandom:()=>Math.random().toString(16).slice(2),track(s,o){if(!this.isEnabled)return Promise.reject("MixPanel analytics is disabled on creation");const c={method:"POST",headers:{"Content-Type":"application/x-www-form-urlencoded"},body:new URLSearchParams({data:JSON.stringify([{event:s,properties:{...o,...a,source:t,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",c).then(u=>u.json()).catch(u=>console.error(u))}}}function Ce(e){var a,r,n,s,o;const i=()=>/Mobi|Android/i.test(navigator.userAgent)?"Mobile":"Desktop";return{$os:`${(()=>{const c=navigator.platform;return c.toLowerCase().includes("win")?"Windows":c.toLowerCase().includes("mac")?"Mac OS X":c.toLowerCase().includes("linux")?"Linux":"Unknown"})()}`,isMobile:`${i()=="Mobile"}`,browser:navigator.userAgent,origin:window.location.origin,agentType:(a=e.presenter)==null?void 0:a.type,agentVoice:{voiceId:(n=(r=e.presenter)==null?void 0:r.voice)==null?void 0:n.voice_id,provider:(o=(s=e.presenter)==null?void 0:s.voice)==null?void 0:o.type}}}const Se=1080;function _e(e,i){if(e.presenter){if(e.presenter.type===v.Clip)return{videoType:v.Clip,driver_id:e.presenter.driver_id,presenter_id:e.presenter.presenter_id,stream_warmup:!0}}else throw new Error("Presenter is not initialized");const t=i||(e.presenter.stitch?Se:void 0);return{videoType:v.Talk,source_url:e.presenter.source_url,stream_warmup:!0,...t&&{output_resolution:t}}}function B(e,i,t,a,r){return new Promise(async(n,s)=>{let o=r;const c=await Me(e.id,_e(e,i.outputResolution),{...i,analytics:a,callbacks:{...i.callbacks,onConnectionStateChange:async u=>{var C,M;if(u===_.Connected)try{o||(o=await t.newChat(e.id),a.track("agent-chat",{event:"created",chat_id:o.id,agent_id:e.id})),c&&n({chat:o,streamingManager:c})}catch(T){console.error(T),s("Cannot create new chat")}else u===_.Fail&&s(new Error("Cannot create connection"));(M=(C=i.callbacks).onConnectionStateChange)==null||M.call(C,u)},onVideoStateChange(u){var C,M;(M=(C=i.callbacks).onVideoStateChange)==null||M.call(C,u)}}}).catch(s)})}function Ie(e,i,t){return te(i,t||O).getById(e)}function j(e){let i="";if(e.greetings&&e.greetings.length>0){const t=Math.floor(Math.random()*e.greetings.length);i=e.greetings[t]}else i=`Hi! I'm ${e.preview_name||"My Agent"}. How can I help you?`;return[{content:i,id:D(),role:"assistant",created_at:new Date().toISOString()}]}async function Ae(e,i){var z,$,p;const t={messages:[],chatMode:i.mode||A.Functional};let a=-1;const r=i.wsURL||ce,n=i.baseURL||O,s=i.mixpanelKey||de,o=te(i.auth,n,i.callbacks.onError),c=await o.getById(e);t.messages=j(c),($=(z=i.callbacks).onNewMessage)==null||$.call(z,t.messages);const u=ve({token:s,agent:c,...i});u.track("agent-sdk",{event:"loaded",...Ce(c)});const C={onMessage:(d,m)=>{var l,g,h,w;if("content"in m){const{content:y}=m,S=t.messages[t.messages.length-1];(S==null?void 0:S.role)==="assistant"&&(a<t.messages.length&&(S.content=d===L.Partial?S.content+y:y),d===L.Answer&&(a=t.messages.length)),d===L.Answer&&u.track("agent-message-received",{messages:t.messages.length}),(g=(l=i.callbacks).onNewMessage)==null||g.call(l,t.messages)}else{if([k.StreamVideoCreated,k.StreamVideoDone,k.StreamVideoError,k.StreamVideoRejected].includes(d)){const y=d.split("/")[1];u.track("agent-video",{...m,event:y})}[k.StreamFailed,k.StreamVideoError,k.StreamVideoRejected].includes(d)&&((w=(h=i.callbacks).onError)==null||w.call(h,new Error(`Stream failed with event ${d}`),{data:m}))}}};async function M(){var h,w,y,S,E;const d=await ne(i.auth,r,C),{streamingManager:m,chat:l}=await B(c,i,o,u,t.chat);a=-1,t.messages.length===0&&(t.messages=j(c),(w=(h=i.callbacks).onNewMessage)==null||w.call(h,t.messages)),l!=null&&l.id&&l.id!==((y=t.chat)==null?void 0:y.id)&&((E=(S=i.callbacks).onNewChat)==null||E.call(S,l==null?void 0:l.id)),t.streamingManager=m,t.socketManager=d,t.chat=l;const g=t.chat.chatMode||A.Functional;I(g),u.track("agent-chat",{event:"connect",chatId:l.id,agentId:c.id})}async function T(){var d,m,l,g,h;(d=t.socketManager)==null||d.disconnect(),await((m=t.streamingManager)==null?void 0:m.disconnect()),delete t.streamingManager,delete t.socketManager,t.messages=j(c),(g=(l=i.callbacks).onNewMessage)==null||g.call(l,t.messages),u.track("agent-chat",{event:"disconnect",chatId:(h=t.chat)==null?void 0:h.id,agentId:c.id})}async function I(d){var m,l;d!==t.chatMode&&(u.track("agent-mode-change",{mode:d}),t.chatMode=d,t.chatMode!==A.Functional&&await T(),(l=(m=i.callbacks).onModeChange)==null||l.call(m,d))}async function W(){if(!t.chat)return M();const{streamingManager:d,chat:m}=await B(c,i,o,u,t.chat);t.streamingManager=d;const l=t.chat.chatMode||A.Functional;I(l),u.track("agent-chat",{event:"reconnect",chatId:m.id,agentId:c.id})}return{agent:c,starterMessages:((p=c.knowledge)==null?void 0:p.starter_message)||[],connect:M,disconnect:T,changeMode:I,async reconnect(){var h,w;if(!t.chat)return M();(h=t.socketManager)==null||h.disconnect(),await((w=t.streamingManager)==null?void 0:w.disconnect());const d=await ne(i.auth,r,C),{streamingManager:m,chat:l}=await B(c,i,o,u,t.chat);t.streamingManager=m,t.socketManager=d;const g=t.chat.chatMode||A.Functional;I(g),u.track("agent-chat",{event:"reconnect",chatId:l.id,agentId:c.id})},async chat(d,m=!1){var g,h,w,y,S,E,N,ie,se;const l=D();try{const F=Date.now();if(d.length>=800)throw new Error("Message cannot be more than 800 characters");if(d.length===0)throw new Error("Message cannot be empty");if(t.chatMode===A.Maintenance)throw new Error("Chat is in maintenance mode");if(![A.TextOnly,A.Playground].includes(t.chatMode)){if(!t.streamingManager)throw new Error("Streaming manager is not initialized");if(!t.chat)throw new Error("Chat is not initialized")}t.messages.push({id:D(),role:"user",content:d,created_at:new Date(F).toISOString()}),(h=(g=i.callbacks).onNewMessage)==null||h.call(g,t.messages),t.chat||(t.chat=await o.newChat(c.id));const K={id:l,role:"assistant",content:"",created_at:new Date().toISOString(),matches:[]};t.messages.push(K);const oe=t.messages.slice(0,-1);let P;try{P=await o.chat(c.id,t.chat.id,{sessionId:(w=t.streamingManager)==null?void 0:w.sessionId,streamId:(y=t.streamingManager)==null?void 0:y.streamId,messages:oe,chatMode:t.chatMode,append_chat:m})}catch(U){if((S=U==null?void 0:U.message)!=null&&S.includes("missing or invalid session_id"))console.log("Invalid stream, try reconnect with new stream id"),await W(),P=await o.chat(c.id,t.chat.id,{sessionId:(E=t.streamingManager)==null?void 0:E.sessionId,streamId:(N=t.streamingManager)==null?void 0:N.streamId,messages:oe,chatMode:t.chatMode,append_chat:m});else throw U}return u.track("agent-message-send",{event:"success",messages:t.messages.length+1}),K.context=P.context,P.result&&(K.content=P.result,K.matches=P.matches,u.track("agent-message-received",{latency:Date.now()-F,messages:t.messages.length}),(se=(ie=i.callbacks).onNewMessage)==null||se.call(ie,t.messages)),P}catch(F){throw t.messages[t.messages.length-1].id===l&&t.messages.pop(),u.track("agent-message-send",{event:"error",messages:t.messages.length}),F}},rate(d,m,l){var w,y,S,E;const g=t.messages.find(N=>N.id===d);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(N=>[N.document_id,N.id]))??[];return u.track("agent-rate",{event:l?"update":"create",thumb:m===1?"up":"down",knowledge_id:((y=c.knowledge)==null?void 0:y.id)??"",matches:h,score:m}),l?o.updateRating(c.id,t.chat.id,l,{knowledge_id:((S=c.knowledge)==null?void 0:S.id)??"",message_id:d,matches:h,score:m}):o.createRating(c.id,t.chat.id,{knowledge_id:((E=c.knowledge)==null?void 0:E.id)??"",message_id:d,matches:h,score:m})},getChatmode(){if(!t.chat)throw new Error("Chat is not initialized");return o.getChatMode(c.id,t.chat.id)},deleteRate(d){var m;if(!t.chat)throw new Error("Chat is not initialized");return u.track("agent-rate-delete",{type:"text",chat_id:(m=t.chat)==null?void 0:m.id,id:d}),o.deleteRating(c.id,t.chat.id,d)},speak(d){if(!t.streamingManager)throw new Error("Streaming manager is not initialized");function m(){if(c.presenter){if(d.type==="text")return{type:"text",provider:d.provider?d.provider:c.presenter.voice,input:d.input,ssml:d.ssml||!1};if(d.type==="audio")return{type:"audio",audio_url:d.audio_url}}else throw new Error("Presenter is not initialized");throw new Error("Invalid payload")}const l=m();return u.track("agent-speak",l),t.streamingManager.speak({script:l})}}}f.ChatMode=A,f.ChatProgress=L,f.ConnectionState=_,f.DocumentType=X,f.KnowledgeType=Q,f.PlanGroup=H,f.Providers=Y,f.RateState=J,f.StreamEvents=k,f.StreamingState=x,f.Subject=q,f.UserPlan=b,f.VideoType=v,f.VoiceAccess=Z,f.createAgentManager=Ae,f.getAgent=Ie,Object.defineProperty(f,Symbol.toStringTag,{value:"Module"})});
|