@d-id/client-sdk 1.0.19-beta.76 → 1.0.19-beta.77
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 +310 -325
- package/dist/index.umd.cjs +1 -1
- package/dist/src/services/mixpanel.d.ts +2 -2
- package/dist/src/types/stream/stream.d.ts +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -1,26 +1,26 @@
|
|
|
1
|
-
var
|
|
2
|
-
const
|
|
3
|
-
function
|
|
1
|
+
var U = /* @__PURE__ */ ((e) => (e.TRIAL = "trial", e.BASIC = "basic", e.ENTERPRISE = "enterprise", e.LITE = "lite", e.ADVANCED = "advanced", e))(U || {}), B = /* @__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))(B || {}), J = /* @__PURE__ */ ((e) => (e.Unrated = "Unrated", e.Positive = "Positive", e.Negative = "Negative", e))(J || {}), E = /* @__PURE__ */ ((e) => (e.Functional = "Functional", e.TextOnly = "TextOnly", e.Maintenance = "Maintenance", e))(E || {}), x = /* @__PURE__ */ ((e) => (e.Embed = "embed", e.Query = "query", e.Partial = "partial", e.Answer = "answer", e.Complete = "done", e))(x || {}), q = /* @__PURE__ */ ((e) => (e.KnowledgeProcessing = "knowledge/processing", e.KnowledgeIndexing = "knowledge/indexing", e.KnowledgeFailed = "knowledge/error", e.KnowledgeDone = "knowledge/done", e))(q || {}), V = /* @__PURE__ */ ((e) => (e.Knowledge = "knowledge", e.Document = "document", e.Record = "record", e))(V || {}), Q = /* @__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))(Q || {}), C = /* @__PURE__ */ ((e) => (e.Clip = "clip", e.Talk = "talk", e))(C || {}), N = /* @__PURE__ */ ((e) => (e.Start = "START", e.Stop = "STOP", e))(N || {}), I = /* @__PURE__ */ ((e) => (e.ChatAnswer = "chat/answer", e.ChatPartial = "chat/partial", e.StreamDone = "stream/done", e.StreamStarted = "stream/started", e.StreamFailed = "stream/error", e.StreamReady = "stream/ready", e.StreamCreated = "stream/created", e.StreamVideoCreated = "stream-video/started", e.StreamVideoDone = "stream-video/done", e.StreamVideoError = "stream-video/error", e.StreamVideoRejected = "stream-video/rejected", e))(I || {}), M = /* @__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))(M || {}), X = /* @__PURE__ */ ((e) => (e.Amazon = "amazon", e.Microsoft = "microsoft", e.Afflorithmics = "afflorithmics", e.Elevenlabs = "elevenlabs", e))(X || {}), Y = /* @__PURE__ */ ((e) => (e.Public = "public", e.Premium = "premium", e.Private = "private", e))(Y || {});
|
|
2
|
+
const T = "https://api.d-id.com", Z = "wss://notifications.d-id.com", G = "79f81a83a67430be2bc0fd61042b8faa", P = () => Math.random().toString(16).slice(2);
|
|
3
|
+
function D() {
|
|
4
4
|
let e = window.localStorage.getItem("did_external_key_id");
|
|
5
5
|
return e || (e = Math.random().toString(16).slice(2), window.localStorage.setItem("did_external_key_id", e)), e;
|
|
6
6
|
}
|
|
7
|
-
let ee =
|
|
8
|
-
function
|
|
7
|
+
let ee = P();
|
|
8
|
+
function H(e) {
|
|
9
9
|
if (e.type === "bearer")
|
|
10
10
|
return `Bearer ${e.token}`;
|
|
11
11
|
if (e.type === "basic")
|
|
12
12
|
return `Basic ${btoa(`${e.username}:${e.password}`)}`;
|
|
13
13
|
if (e.type === "key")
|
|
14
|
-
return `Client-Key ${e.clientKey}.${
|
|
14
|
+
return `Client-Key ${e.clientKey}.${D()}_${ee}`;
|
|
15
15
|
throw new Error(`Unknown auth type: ${e}`);
|
|
16
16
|
}
|
|
17
|
-
function
|
|
18
|
-
const t = async (n,
|
|
19
|
-
const i = await fetch(
|
|
20
|
-
...
|
|
17
|
+
function F(e, r = T) {
|
|
18
|
+
const t = async (n, a) => {
|
|
19
|
+
const i = await fetch(r + (n != null && n.startsWith("/") ? n : `/${n}`), {
|
|
20
|
+
...a,
|
|
21
21
|
headers: {
|
|
22
|
-
...
|
|
23
|
-
Authorization:
|
|
22
|
+
...a == null ? void 0 : a.headers,
|
|
23
|
+
Authorization: H(e),
|
|
24
24
|
"Content-Type": "application/json"
|
|
25
25
|
}
|
|
26
26
|
});
|
|
@@ -31,213 +31,213 @@ function K(e, a = P) {
|
|
|
31
31
|
return i.json();
|
|
32
32
|
};
|
|
33
33
|
return {
|
|
34
|
-
get(n,
|
|
34
|
+
get(n, a) {
|
|
35
35
|
return t(n, {
|
|
36
|
-
...
|
|
36
|
+
...a,
|
|
37
37
|
method: "GET"
|
|
38
38
|
});
|
|
39
39
|
},
|
|
40
|
-
post(n,
|
|
40
|
+
post(n, a, i) {
|
|
41
41
|
return t(n, {
|
|
42
42
|
...i,
|
|
43
|
-
body: JSON.stringify(
|
|
43
|
+
body: JSON.stringify(a),
|
|
44
44
|
method: "POST"
|
|
45
45
|
});
|
|
46
46
|
},
|
|
47
|
-
delete(n,
|
|
47
|
+
delete(n, a, i) {
|
|
48
48
|
return t(n, {
|
|
49
49
|
...i,
|
|
50
|
-
body: JSON.stringify(
|
|
50
|
+
body: JSON.stringify(a),
|
|
51
51
|
method: "DELETE"
|
|
52
52
|
});
|
|
53
53
|
},
|
|
54
|
-
patch(n,
|
|
54
|
+
patch(n, a, i) {
|
|
55
55
|
return t(n, {
|
|
56
56
|
...i,
|
|
57
|
-
body: JSON.stringify(
|
|
57
|
+
body: JSON.stringify(a),
|
|
58
58
|
method: "PATCH"
|
|
59
59
|
});
|
|
60
60
|
}
|
|
61
61
|
};
|
|
62
62
|
}
|
|
63
|
-
function
|
|
64
|
-
const t =
|
|
63
|
+
function K(e, r = T) {
|
|
64
|
+
const t = F(e, `${r}/agents`);
|
|
65
65
|
return {
|
|
66
|
-
create(n,
|
|
67
|
-
return t.post("/", n,
|
|
66
|
+
create(n, a) {
|
|
67
|
+
return t.post("/", n, a);
|
|
68
68
|
},
|
|
69
|
-
getAgents(n,
|
|
70
|
-
return t.get(`/${n ? `?tag=${n}` : ""}`,
|
|
69
|
+
getAgents(n, a) {
|
|
70
|
+
return t.get(`/${n ? `?tag=${n}` : ""}`, a).then((i) => i ?? []);
|
|
71
71
|
},
|
|
72
|
-
getById(n,
|
|
73
|
-
return t.get(`/${n}`,
|
|
72
|
+
getById(n, a) {
|
|
73
|
+
return t.get(`/${n}`, a);
|
|
74
74
|
},
|
|
75
|
-
delete(n,
|
|
76
|
-
return t.delete(`/${n}`, void 0,
|
|
75
|
+
delete(n, a) {
|
|
76
|
+
return t.delete(`/${n}`, void 0, a);
|
|
77
77
|
},
|
|
78
|
-
update(n,
|
|
79
|
-
return t.patch(`/${n}`,
|
|
78
|
+
update(n, a, i) {
|
|
79
|
+
return t.patch(`/${n}`, a, i);
|
|
80
80
|
},
|
|
81
|
-
newChat(n,
|
|
82
|
-
return t.post(`/${n}/chat`, void 0,
|
|
81
|
+
newChat(n, a) {
|
|
82
|
+
return t.post(`/${n}/chat`, void 0, a);
|
|
83
83
|
},
|
|
84
|
-
chat(n,
|
|
85
|
-
return t.post(`/${n}/chat/${
|
|
84
|
+
chat(n, a, i, o) {
|
|
85
|
+
return t.post(`/${n}/chat/${a}`, i, o);
|
|
86
86
|
},
|
|
87
|
-
createRating(n,
|
|
88
|
-
return t.post(`/${n}/chat/${
|
|
87
|
+
createRating(n, a, i, o) {
|
|
88
|
+
return t.post(`/${n}/chat/${a}/ratings`, i, o);
|
|
89
89
|
},
|
|
90
|
-
updateRating(n,
|
|
91
|
-
return t.patch(`/${n}/chat/${
|
|
90
|
+
updateRating(n, a, i, o, d) {
|
|
91
|
+
return t.patch(`/${n}/chat/${a}/ratings/${i}`, o, d);
|
|
92
92
|
},
|
|
93
|
-
deleteRating(n,
|
|
94
|
-
return t.delete(`/${n}/chat/${
|
|
93
|
+
deleteRating(n, a, i, o) {
|
|
94
|
+
return t.delete(`/${n}/chat/${a}/ratings/${i}`, o);
|
|
95
95
|
}
|
|
96
96
|
};
|
|
97
97
|
}
|
|
98
|
-
const te = (e) => new Promise((
|
|
98
|
+
const te = (e) => new Promise((r) => setTimeout(r, e));
|
|
99
99
|
function ne(e) {
|
|
100
|
-
return new Promise((
|
|
100
|
+
return new Promise((r, t) => {
|
|
101
101
|
const {
|
|
102
102
|
callbacks: n,
|
|
103
|
-
host:
|
|
103
|
+
host: a,
|
|
104
104
|
auth: i
|
|
105
105
|
} = e, {
|
|
106
106
|
onMessage: o = null,
|
|
107
|
-
onOpen:
|
|
107
|
+
onOpen: d = null,
|
|
108
108
|
onClose: g = null,
|
|
109
|
-
onError:
|
|
110
|
-
} = n || {},
|
|
111
|
-
|
|
112
|
-
console.error(
|
|
113
|
-
},
|
|
114
|
-
|
|
109
|
+
onError: l = null
|
|
110
|
+
} = n || {}, f = new WebSocket(`${a}?authorization=${H(i)}`);
|
|
111
|
+
f.onmessage = o, f.onclose = g, f.onerror = (p) => {
|
|
112
|
+
console.error(p), l == null || l("Websocket failed to connect", p), t(p);
|
|
113
|
+
}, f.onopen = (p) => {
|
|
114
|
+
d == null || d(p), r(f);
|
|
115
115
|
};
|
|
116
116
|
});
|
|
117
117
|
}
|
|
118
|
-
async function
|
|
118
|
+
async function ae(e) {
|
|
119
119
|
const {
|
|
120
|
-
retries:
|
|
120
|
+
retries: r = 1
|
|
121
121
|
} = e;
|
|
122
122
|
let t = null;
|
|
123
123
|
for (let n = 0; (t == null ? void 0 : t.readyState) !== WebSocket.OPEN; n++)
|
|
124
124
|
try {
|
|
125
125
|
t = await ne(e);
|
|
126
|
-
} catch (
|
|
127
|
-
if (n ===
|
|
128
|
-
throw
|
|
126
|
+
} catch (a) {
|
|
127
|
+
if (n === r)
|
|
128
|
+
throw a;
|
|
129
129
|
await te(n * 500);
|
|
130
130
|
}
|
|
131
131
|
return t;
|
|
132
132
|
}
|
|
133
|
-
async function
|
|
134
|
-
const n = t != null && t.onMessage ? [t.onMessage] : [],
|
|
133
|
+
async function W(e, r, t) {
|
|
134
|
+
const n = t != null && t.onMessage ? [t.onMessage] : [], a = await ae({
|
|
135
135
|
auth: e,
|
|
136
|
-
host:
|
|
136
|
+
host: r,
|
|
137
137
|
callbacks: {
|
|
138
138
|
onError: t == null ? void 0 : t.onError,
|
|
139
139
|
onMessage: (i) => {
|
|
140
140
|
const o = JSON.parse(i.data);
|
|
141
|
-
n.forEach((
|
|
141
|
+
n.forEach((d) => d(o.event, o));
|
|
142
142
|
}
|
|
143
143
|
}
|
|
144
144
|
});
|
|
145
145
|
return {
|
|
146
|
-
socket:
|
|
147
|
-
disconnect: () =>
|
|
146
|
+
socket: a,
|
|
147
|
+
disconnect: () => a.close(),
|
|
148
148
|
subscribeToEvents: (i) => n.push(i)
|
|
149
149
|
};
|
|
150
150
|
}
|
|
151
|
-
function
|
|
152
|
-
const n =
|
|
151
|
+
function re(e, r, t) {
|
|
152
|
+
const n = F(e, `${r}/agents/${t}`);
|
|
153
153
|
return {
|
|
154
|
-
createStream(
|
|
154
|
+
createStream(a) {
|
|
155
155
|
return n.post("/streams", {
|
|
156
|
-
driver_id:
|
|
157
|
-
presenter_id:
|
|
158
|
-
compatibility_mode:
|
|
159
|
-
stream_warmup:
|
|
160
|
-
type:
|
|
156
|
+
driver_id: a.driver_id,
|
|
157
|
+
presenter_id: a.presenter_id,
|
|
158
|
+
compatibility_mode: a.compatibility_mode,
|
|
159
|
+
stream_warmup: a.stream_warmup,
|
|
160
|
+
type: C.Clip
|
|
161
161
|
});
|
|
162
162
|
},
|
|
163
|
-
startConnection(
|
|
164
|
-
return n.post(`/streams/${
|
|
163
|
+
startConnection(a, i, o) {
|
|
164
|
+
return n.post(`/streams/${a}/sdp`, {
|
|
165
165
|
session_id: o,
|
|
166
166
|
answer: i,
|
|
167
|
-
type:
|
|
167
|
+
type: C.Clip
|
|
168
168
|
});
|
|
169
169
|
},
|
|
170
|
-
addIceCandidate(
|
|
171
|
-
return n.post(`/streams/${
|
|
170
|
+
addIceCandidate(a, i, o) {
|
|
171
|
+
return n.post(`/streams/${a}/ice`, {
|
|
172
172
|
session_id: o,
|
|
173
173
|
...i,
|
|
174
|
-
type:
|
|
174
|
+
type: C.Clip
|
|
175
175
|
});
|
|
176
176
|
},
|
|
177
|
-
sendStreamRequest(
|
|
178
|
-
return n.post(`/streams/${
|
|
177
|
+
sendStreamRequest(a, i, o) {
|
|
178
|
+
return n.post(`/streams/${a}`, {
|
|
179
179
|
session_id: i,
|
|
180
180
|
...o,
|
|
181
|
-
type:
|
|
181
|
+
type: C.Clip
|
|
182
182
|
});
|
|
183
183
|
},
|
|
184
|
-
close(
|
|
185
|
-
return n.delete(`/streams/${
|
|
184
|
+
close(a, i) {
|
|
185
|
+
return n.delete(`/streams/${a}`, {
|
|
186
186
|
session_id: i,
|
|
187
|
-
type:
|
|
187
|
+
type: C.Clip
|
|
188
188
|
});
|
|
189
189
|
}
|
|
190
190
|
};
|
|
191
191
|
}
|
|
192
|
-
function
|
|
193
|
-
const n =
|
|
192
|
+
function ie(e, r, t) {
|
|
193
|
+
const n = F(e, `${r}/agents/${t}`);
|
|
194
194
|
return {
|
|
195
|
-
createStream(
|
|
195
|
+
createStream(a, i) {
|
|
196
196
|
return n.post("/streams", {
|
|
197
|
-
source_url:
|
|
198
|
-
driver_url:
|
|
199
|
-
face:
|
|
200
|
-
config:
|
|
201
|
-
stream_warmup:
|
|
202
|
-
type:
|
|
197
|
+
source_url: a.source_url,
|
|
198
|
+
driver_url: a.driver_url,
|
|
199
|
+
face: a.face,
|
|
200
|
+
config: a.config,
|
|
201
|
+
stream_warmup: a.stream_warmup,
|
|
202
|
+
type: C.Talk
|
|
203
203
|
}, i);
|
|
204
204
|
},
|
|
205
|
-
startConnection(
|
|
206
|
-
return n.post(`/streams/${
|
|
205
|
+
startConnection(a, i, o, d) {
|
|
206
|
+
return n.post(`/streams/${a}/sdp`, {
|
|
207
207
|
session_id: o,
|
|
208
208
|
answer: i,
|
|
209
|
-
type:
|
|
210
|
-
},
|
|
209
|
+
type: C.Talk
|
|
210
|
+
}, d);
|
|
211
211
|
},
|
|
212
|
-
addIceCandidate(
|
|
213
|
-
return n.post(`/streams/${
|
|
212
|
+
addIceCandidate(a, i, o, d) {
|
|
213
|
+
return n.post(`/streams/${a}/ice`, {
|
|
214
214
|
session_id: o,
|
|
215
215
|
...i,
|
|
216
|
-
type:
|
|
217
|
-
},
|
|
216
|
+
type: C.Talk
|
|
217
|
+
}, d);
|
|
218
218
|
},
|
|
219
|
-
sendStreamRequest(
|
|
220
|
-
return n.post(`/streams/${
|
|
219
|
+
sendStreamRequest(a, i, o, d) {
|
|
220
|
+
return n.post(`/streams/${a}`, {
|
|
221
221
|
session_id: i,
|
|
222
222
|
...o,
|
|
223
|
-
type:
|
|
224
|
-
},
|
|
223
|
+
type: C.Talk
|
|
224
|
+
}, d);
|
|
225
225
|
},
|
|
226
|
-
close(
|
|
227
|
-
return n.delete(`/streams/${
|
|
226
|
+
close(a, i, o) {
|
|
227
|
+
return n.delete(`/streams/${a}`, {
|
|
228
228
|
session_id: i,
|
|
229
|
-
type:
|
|
229
|
+
type: C.Talk
|
|
230
230
|
}, o);
|
|
231
231
|
}
|
|
232
232
|
};
|
|
233
233
|
}
|
|
234
|
-
function se(e,
|
|
235
|
-
return e.map((t, n) => n === 0 ?
|
|
234
|
+
function se(e, r) {
|
|
235
|
+
return e.map((t, n) => n === 0 ? r ? {
|
|
236
236
|
index: n,
|
|
237
237
|
timestamp: t.timestamp,
|
|
238
|
-
bytesReceived: t.bytesReceived -
|
|
239
|
-
packetsReceived: t.packetsReceived -
|
|
240
|
-
packetsLost: t.packetsLost -
|
|
238
|
+
bytesReceived: t.bytesReceived - r.bytesReceived,
|
|
239
|
+
packetsReceived: t.packetsReceived - r.packetsReceived,
|
|
240
|
+
packetsLost: t.packetsLost - r.packetsLost,
|
|
241
241
|
jitter: t.jitter,
|
|
242
242
|
frameWidth: t.frameWidth,
|
|
243
243
|
frameHeight: t.frameHeight,
|
|
@@ -264,162 +264,146 @@ function se(e, a) {
|
|
|
264
264
|
framesPerSecond: t.framesPerSecond
|
|
265
265
|
});
|
|
266
266
|
}
|
|
267
|
-
let
|
|
268
|
-
const A = (e,
|
|
269
|
-
function
|
|
267
|
+
let j = !1;
|
|
268
|
+
const A = (e, r) => j && console.log(e, r), oe = (window.RTCPeerConnection || window.webkitRTCPeerConnection || window.mozRTCPeerConnection).bind(window);
|
|
269
|
+
function ce(e) {
|
|
270
270
|
switch (e) {
|
|
271
271
|
case "connected":
|
|
272
|
-
return
|
|
272
|
+
return M.Connected;
|
|
273
273
|
case "checking":
|
|
274
|
-
return
|
|
274
|
+
return M.Connecting;
|
|
275
275
|
case "failed":
|
|
276
|
-
return
|
|
276
|
+
return M.Fail;
|
|
277
277
|
case "new":
|
|
278
278
|
case "closed":
|
|
279
279
|
case "disconnected":
|
|
280
280
|
default:
|
|
281
|
-
return
|
|
281
|
+
return M.New;
|
|
282
282
|
}
|
|
283
283
|
}
|
|
284
|
-
function
|
|
285
|
-
let t = [], n = 0,
|
|
284
|
+
function de(e, r) {
|
|
285
|
+
let t = [], n = 0, a = 0, i = 0, o;
|
|
286
286
|
return setInterval(() => {
|
|
287
287
|
e.getStats().then((g) => {
|
|
288
|
-
g.forEach((
|
|
289
|
-
if (
|
|
290
|
-
if (
|
|
291
|
-
const
|
|
292
|
-
let
|
|
293
|
-
if (o =
|
|
288
|
+
g.forEach((l) => {
|
|
289
|
+
if (l.type === "inbound-rtp" && l.kind === "video") {
|
|
290
|
+
if (a = t.length - 1, l && t[a]) {
|
|
291
|
+
const f = l.bytesReceived, p = t[a].bytesReceived, k = o;
|
|
292
|
+
let y;
|
|
293
|
+
if (o = f - p > 0, i = o ? 0 : ++i, k !== o)
|
|
294
294
|
if (o)
|
|
295
|
-
n = t.length,
|
|
295
|
+
n = t.length, r == null || r(N.Start, y);
|
|
296
296
|
else {
|
|
297
|
-
const L = t.slice(n),
|
|
298
|
-
|
|
297
|
+
const L = t.slice(n), b = n === 0 ? void 0 : t[n - 1];
|
|
298
|
+
y = se(L, b), y = y.sort((S, s) => s.packetsLost - S.packetsLost).slice(0, 5);
|
|
299
299
|
}
|
|
300
|
-
!o && i === 3 && (
|
|
300
|
+
!o && i === 3 && (r == null || r(N.Stop, y));
|
|
301
301
|
}
|
|
302
|
-
t.push(
|
|
302
|
+
t.push(l);
|
|
303
303
|
}
|
|
304
304
|
});
|
|
305
305
|
});
|
|
306
306
|
}, 500);
|
|
307
307
|
}
|
|
308
|
-
async function ge(e,
|
|
308
|
+
async function ge(e, r, {
|
|
309
309
|
debug: t = !1,
|
|
310
310
|
callbacks: n,
|
|
311
|
-
auth:
|
|
311
|
+
auth: a,
|
|
312
312
|
analytics: i,
|
|
313
|
-
baseURL: o =
|
|
313
|
+
baseURL: o = T
|
|
314
314
|
}) {
|
|
315
|
-
|
|
316
|
-
let
|
|
315
|
+
j = t;
|
|
316
|
+
let d;
|
|
317
317
|
const {
|
|
318
318
|
startConnection: g,
|
|
319
|
-
sendStreamRequest:
|
|
320
|
-
close:
|
|
321
|
-
createStream:
|
|
322
|
-
addIceCandidate:
|
|
323
|
-
} =
|
|
324
|
-
id:
|
|
319
|
+
sendStreamRequest: l,
|
|
320
|
+
close: f,
|
|
321
|
+
createStream: p,
|
|
322
|
+
addIceCandidate: k
|
|
323
|
+
} = r.videoType === C.Clip ? re(a, o, e) : ie(a, o, e), {
|
|
324
|
+
id: y,
|
|
325
325
|
offer: L,
|
|
326
|
-
ice_servers:
|
|
327
|
-
session_id:
|
|
328
|
-
} = await
|
|
329
|
-
iceServers:
|
|
326
|
+
ice_servers: b,
|
|
327
|
+
session_id: S
|
|
328
|
+
} = await p(r), s = new oe({
|
|
329
|
+
iceServers: b
|
|
330
330
|
}), u = s.createDataChannel("JanusDataChannel");
|
|
331
|
-
if (!
|
|
331
|
+
if (!S)
|
|
332
332
|
throw new Error("Could not create session_id");
|
|
333
|
-
const
|
|
334
|
-
s.onicecandidate = (
|
|
335
|
-
A("peerConnection.onicecandidate",
|
|
336
|
-
candidate:
|
|
337
|
-
sdpMid:
|
|
338
|
-
sdpMLineIndex:
|
|
339
|
-
},
|
|
333
|
+
const m = de(s, n.onVideoStateChange);
|
|
334
|
+
s.onicecandidate = (c) => {
|
|
335
|
+
A("peerConnection.onicecandidate", c), c.candidate && c.candidate.sdpMid && c.candidate.sdpMLineIndex !== null ? k(y, {
|
|
336
|
+
candidate: c.candidate.candidate,
|
|
337
|
+
sdpMid: c.candidate.sdpMid,
|
|
338
|
+
sdpMLineIndex: c.candidate.sdpMLineIndex
|
|
339
|
+
}, S) : k(y, {
|
|
340
340
|
candidate: null
|
|
341
|
-
},
|
|
341
|
+
}, S);
|
|
342
342
|
}, s.oniceconnectionstatechange = () => {
|
|
343
343
|
var h;
|
|
344
344
|
A("peerConnection.oniceconnectionstatechange => " + s.iceConnectionState);
|
|
345
|
-
const
|
|
346
|
-
|
|
347
|
-
var
|
|
348
|
-
(
|
|
349
|
-
}, 5e3) : (clearTimeout(
|
|
350
|
-
}, s.ontrack = (
|
|
345
|
+
const c = ce(s.iceConnectionState);
|
|
346
|
+
c === M.Connected ? (i.track("agent-chat-loaded"), d = setTimeout(() => {
|
|
347
|
+
var w;
|
|
348
|
+
(w = n.onConnectionStateChange) == null || w.call(n, M.Connected);
|
|
349
|
+
}, 5e3)) : (clearTimeout(d), (h = n.onConnectionStateChange) == null || h.call(n, c));
|
|
350
|
+
}, s.ontrack = (c) => {
|
|
351
|
+
var h;
|
|
352
|
+
A("peerConnection.ontrack", c), (h = n.onSrcObjectReady) == null || h.call(n, c.streams[0]);
|
|
353
|
+
}, u.onmessage = (c) => {
|
|
351
354
|
var h;
|
|
352
|
-
A("peerConnection.ontrack", d), (h = n.onSrcObjectReady) == null || h.call(n, d.streams[0]);
|
|
353
|
-
}, u.onmessage = (d) => {
|
|
354
|
-
var h, S;
|
|
355
355
|
if (u.readyState === "open") {
|
|
356
|
-
const [
|
|
357
|
-
|
|
358
|
-
event:
|
|
359
|
-
...
|
|
360
|
-
}) :
|
|
361
|
-
event: "
|
|
362
|
-
|
|
363
|
-
}) : v === $.StreamFailed ? ((h = n.onVideoStateChange) == null || h.call(n, T.Stop, {
|
|
364
|
-
event: v,
|
|
365
|
-
data: y
|
|
366
|
-
}), clearInterval(l)) : v === $.StreamReady ? (clearTimeout(c), (S = n.onConnectionStateChange) == null || S.call(n, R.Connected)) : v === $.StreamCreated ? i == null || i.track("agent-video", {
|
|
367
|
-
event: "created",
|
|
368
|
-
...d
|
|
369
|
-
}) : v === $.StreamVideoCreated ? i == null || i.track("agent-video", {
|
|
370
|
-
event: "video-created",
|
|
371
|
-
...d
|
|
372
|
-
}) : v === $.StreamVideoDone ? i == null || i.track("agent-video", {
|
|
373
|
-
event: "video-done",
|
|
374
|
-
...d
|
|
375
|
-
}) : v === $.StreamVideoError && (i == null || i.track("agent-video", {
|
|
376
|
-
event: "video-error",
|
|
377
|
-
...d
|
|
378
|
-
}));
|
|
356
|
+
const [w, _] = c.data.split(":");
|
|
357
|
+
w === I.StreamVideoCreated || I.StreamVideoDone || I.StreamVideoError || I.StreamVideoRejected ? i.track("agent-video", {
|
|
358
|
+
event: w,
|
|
359
|
+
...c
|
|
360
|
+
}) : w === I.StreamFailed || w === I.StreamDone ? clearInterval(m) : w === I.StreamReady ? (clearTimeout(d), (h = n.onConnectionStateChange) == null || h.call(n, M.Connected)) : w === I.StreamCreated && i.track("agent-video", {
|
|
361
|
+
event: "created"
|
|
362
|
+
});
|
|
379
363
|
}
|
|
380
364
|
}, await s.setRemoteDescription(L), A("set remote description OK");
|
|
381
|
-
const
|
|
382
|
-
return A("create answer OK"), await s.setLocalDescription(
|
|
365
|
+
const v = await s.createAnswer();
|
|
366
|
+
return A("create answer OK"), await s.setLocalDescription(v), A("set local description OK"), await g(y, v, S), A("start connection OK"), {
|
|
383
367
|
/**
|
|
384
368
|
* Method to send request to server to get clip or talk depend on you payload
|
|
385
369
|
* @param payload
|
|
386
370
|
*/
|
|
387
|
-
speak(
|
|
388
|
-
return
|
|
371
|
+
speak(c) {
|
|
372
|
+
return l(y, S, c);
|
|
389
373
|
},
|
|
390
374
|
/**
|
|
391
375
|
* Method to close RTC connection
|
|
392
376
|
*/
|
|
393
377
|
async disconnect() {
|
|
394
|
-
var
|
|
395
|
-
|
|
396
|
-
}), (
|
|
378
|
+
var c, h;
|
|
379
|
+
y && (s && (s.close(), s.oniceconnectionstatechange = null, s.onnegotiationneeded = null, s.onicecandidate = null, s.ontrack = null), await f(y, S).catch((w) => {
|
|
380
|
+
}), (c = n.onConnectionStateChange) == null || c.call(n, M.New), (h = n.onVideoStateChange) == null || h.call(n, N.Stop), clearInterval(m));
|
|
397
381
|
},
|
|
398
382
|
/**
|
|
399
383
|
* Session identifier information, should be returned in the body of all streaming requests
|
|
400
384
|
*/
|
|
401
|
-
sessionId:
|
|
385
|
+
sessionId: S,
|
|
402
386
|
/**
|
|
403
387
|
* Id of current RTC stream
|
|
404
388
|
*/
|
|
405
|
-
streamId:
|
|
389
|
+
streamId: y
|
|
406
390
|
};
|
|
407
391
|
}
|
|
408
|
-
function
|
|
409
|
-
const
|
|
410
|
-
|
|
411
|
-
distinct_id: e.distinctId ||
|
|
392
|
+
function le(e) {
|
|
393
|
+
const r = {
|
|
394
|
+
token: e.token || "testKey",
|
|
395
|
+
distinct_id: e.distinctId || D(),
|
|
412
396
|
isEnabled: e.isEnabled ?? !0,
|
|
413
397
|
agentId: e.agent.id,
|
|
414
398
|
owner_id: e.agent.owner_id ?? ""
|
|
415
399
|
};
|
|
416
400
|
return {
|
|
417
|
-
...
|
|
401
|
+
...r,
|
|
418
402
|
getRandom: () => Math.random().toString(16).slice(2),
|
|
419
403
|
track(t, n) {
|
|
420
404
|
if (!this.isEnabled)
|
|
421
405
|
return Promise.reject("MixPanel analytics is disabled on creation");
|
|
422
|
-
const
|
|
406
|
+
const a = {
|
|
423
407
|
method: "POST",
|
|
424
408
|
headers: {
|
|
425
409
|
"Content-Type": "application/x-www-form-urlencoded"
|
|
@@ -429,7 +413,7 @@ function me(e) {
|
|
|
429
413
|
event: t,
|
|
430
414
|
properties: {
|
|
431
415
|
...n,
|
|
432
|
-
...
|
|
416
|
+
...r,
|
|
433
417
|
time: Date.now(),
|
|
434
418
|
$insert_id: this.getRandom(),
|
|
435
419
|
origin: window.location.href,
|
|
@@ -440,33 +424,33 @@ function me(e) {
|
|
|
440
424
|
}])
|
|
441
425
|
})
|
|
442
426
|
};
|
|
443
|
-
return fetch("https://api-js.mixpanel.com/track/?verbose=1&ip=1",
|
|
427
|
+
return fetch("https://api-js.mixpanel.com/track/?verbose=1&ip=1", a).then((i) => i.json()).catch((i) => console.error(i));
|
|
444
428
|
}
|
|
445
429
|
};
|
|
446
430
|
}
|
|
447
|
-
function
|
|
448
|
-
var n,
|
|
449
|
-
const
|
|
431
|
+
function me(e) {
|
|
432
|
+
var n, a, i, o, d;
|
|
433
|
+
const r = () => /Mobi|Android/i.test(navigator.userAgent) ? "Mobile" : "Desktop";
|
|
450
434
|
return {
|
|
451
435
|
$os: `${(() => {
|
|
452
436
|
const g = navigator.platform;
|
|
453
437
|
return g.toLowerCase().includes("win") ? "Windows" : g.toLowerCase().includes("mac") ? "Mac OS X" : g.toLowerCase().includes("linux") ? "Linux" : "Unknown";
|
|
454
438
|
})()}`,
|
|
455
|
-
isMobile: `${
|
|
439
|
+
isMobile: `${r() == "Mobile"}`,
|
|
456
440
|
browser: navigator.userAgent,
|
|
457
441
|
origin: window.location.origin,
|
|
458
442
|
agentType: (n = e.presenter) == null ? void 0 : n.type,
|
|
459
443
|
agentVoice: {
|
|
460
|
-
voiceId: (i = (
|
|
461
|
-
provider: (
|
|
444
|
+
voiceId: (i = (a = e.presenter) == null ? void 0 : a.voice) == null ? void 0 : i.voice_id,
|
|
445
|
+
provider: (d = (o = e.presenter) == null ? void 0 : o.voice) == null ? void 0 : d.type
|
|
462
446
|
}
|
|
463
447
|
};
|
|
464
448
|
}
|
|
465
449
|
function ue(e) {
|
|
466
450
|
if (e.presenter) {
|
|
467
|
-
if (e.presenter.type ===
|
|
451
|
+
if (e.presenter.type === C.Clip)
|
|
468
452
|
return {
|
|
469
|
-
videoType:
|
|
453
|
+
videoType: C.Clip,
|
|
470
454
|
driver_id: e.presenter.driver_id,
|
|
471
455
|
presenter_id: e.presenter.presenter_id,
|
|
472
456
|
stream_warmup: !0
|
|
@@ -474,153 +458,154 @@ function ue(e) {
|
|
|
474
458
|
} else
|
|
475
459
|
throw new Error("Presenter is not initialized");
|
|
476
460
|
return {
|
|
477
|
-
videoType:
|
|
461
|
+
videoType: C.Talk,
|
|
478
462
|
source_url: e.presenter.source_url,
|
|
479
463
|
stream_warmup: !0
|
|
480
464
|
};
|
|
481
465
|
}
|
|
482
|
-
function
|
|
466
|
+
function z(e, r, t, n, a) {
|
|
483
467
|
return new Promise(async (i, o) => {
|
|
484
|
-
let
|
|
468
|
+
let d = a;
|
|
485
469
|
const g = await ge(e.id, ue(e), {
|
|
486
|
-
...
|
|
470
|
+
...r,
|
|
487
471
|
callbacks: {
|
|
488
|
-
...
|
|
489
|
-
onConnectionStateChange: async (
|
|
490
|
-
var
|
|
491
|
-
if (
|
|
472
|
+
...r.callbacks,
|
|
473
|
+
onConnectionStateChange: async (l) => {
|
|
474
|
+
var f, p;
|
|
475
|
+
if (l === M.Connected)
|
|
492
476
|
try {
|
|
493
|
-
|
|
477
|
+
d || (d = await t.newChat(e.id), n.track("agent-chat", {
|
|
494
478
|
event: "created",
|
|
495
|
-
chat_id:
|
|
479
|
+
chat_id: d.id,
|
|
496
480
|
agent_id: e.id
|
|
497
481
|
})), g && i({
|
|
498
|
-
chat:
|
|
482
|
+
chat: d,
|
|
499
483
|
streamingManager: g
|
|
500
484
|
});
|
|
501
|
-
} catch (
|
|
502
|
-
console.error(
|
|
485
|
+
} catch (k) {
|
|
486
|
+
console.error(k), o("Cannot create new chat");
|
|
503
487
|
}
|
|
504
488
|
else
|
|
505
|
-
|
|
506
|
-
(
|
|
489
|
+
l === M.Fail && o(new Error("Cannot create connection"));
|
|
490
|
+
(p = (f = r.callbacks).onConnectionStateChange) == null || p.call(f, l);
|
|
507
491
|
},
|
|
508
|
-
onVideoStateChange(
|
|
509
|
-
var
|
|
492
|
+
onVideoStateChange(l, f) {
|
|
493
|
+
var p, k;
|
|
510
494
|
n.track("agent-video", {
|
|
511
|
-
event:
|
|
512
|
-
rtc_stats:
|
|
513
|
-
}), (
|
|
495
|
+
event: l,
|
|
496
|
+
rtc_stats: f ?? []
|
|
497
|
+
}), (k = (p = r.callbacks).onVideoStateChange) == null || k.call(p, l, f);
|
|
514
498
|
}
|
|
515
|
-
}
|
|
499
|
+
},
|
|
500
|
+
analytics: n
|
|
516
501
|
}).catch(o);
|
|
517
502
|
});
|
|
518
503
|
}
|
|
519
|
-
function he(e,
|
|
520
|
-
return
|
|
504
|
+
function he(e, r, t) {
|
|
505
|
+
return K(r, t || T).getById(e);
|
|
521
506
|
}
|
|
522
507
|
function O(e) {
|
|
523
|
-
let
|
|
508
|
+
let r = "";
|
|
524
509
|
if (e.greetings && e.greetings.length > 0) {
|
|
525
510
|
const t = Math.floor(Math.random() * e.greetings.length);
|
|
526
|
-
|
|
511
|
+
r = e.greetings[t];
|
|
527
512
|
} else
|
|
528
|
-
|
|
513
|
+
r = `Hi! I'm ${e.preview_name}, welcome to agents. How can I help you?`;
|
|
529
514
|
return [{
|
|
530
|
-
content:
|
|
531
|
-
id:
|
|
515
|
+
content: r,
|
|
516
|
+
id: P(),
|
|
532
517
|
role: "assistant",
|
|
533
518
|
created_at: (/* @__PURE__ */ new Date()).toISOString()
|
|
534
519
|
}];
|
|
535
520
|
}
|
|
536
|
-
async function we(e,
|
|
537
|
-
var L,
|
|
521
|
+
async function we(e, r) {
|
|
522
|
+
var L, b, S;
|
|
538
523
|
const t = {
|
|
539
524
|
messages: [],
|
|
540
|
-
chatMode:
|
|
525
|
+
chatMode: r.mode || E.Functional
|
|
541
526
|
};
|
|
542
527
|
let n = -1;
|
|
543
|
-
const
|
|
544
|
-
t.messages = O(g), (
|
|
545
|
-
const
|
|
546
|
-
|
|
528
|
+
const a = r.wsURL || Z, i = r.baseURL || T, o = r.mixpanelKey || G, d = K(r.auth, i), g = await d.getById(e);
|
|
529
|
+
t.messages = O(g), (b = (L = r.callbacks).onNewMessage) == null || b.call(L, t.messages);
|
|
530
|
+
const l = le({
|
|
531
|
+
token: o,
|
|
547
532
|
agent: g,
|
|
548
|
-
...
|
|
533
|
+
...r
|
|
549
534
|
});
|
|
550
|
-
|
|
535
|
+
l.track("agent-sdk", {
|
|
551
536
|
event: "loaded",
|
|
552
|
-
...
|
|
537
|
+
...me(g)
|
|
553
538
|
});
|
|
554
|
-
const
|
|
539
|
+
const f = {
|
|
555
540
|
onMessage: (s, u) => {
|
|
556
|
-
var
|
|
541
|
+
var m, v;
|
|
557
542
|
if ("content" in u) {
|
|
558
543
|
const {
|
|
559
|
-
content:
|
|
544
|
+
content: c
|
|
560
545
|
} = u, h = t.messages[t.messages.length - 1];
|
|
561
|
-
(h == null ? void 0 : h.role) === "assistant" && (n < t.messages.length && (h.content = s ===
|
|
546
|
+
(h == null ? void 0 : h.role) === "assistant" && (n < t.messages.length && (h.content = s === x.Partial ? h.content + c : c), s === x.Answer && (n = t.messages.length)), s === x.Complete && l.track("agent-message-received", {
|
|
562
547
|
messages: t.messages.length
|
|
563
|
-
}), (
|
|
548
|
+
}), (v = (m = r.callbacks).onNewMessage) == null || v.call(m, t.messages);
|
|
564
549
|
}
|
|
565
550
|
}
|
|
566
551
|
};
|
|
567
|
-
async function
|
|
568
|
-
var
|
|
569
|
-
const s = await
|
|
552
|
+
async function p() {
|
|
553
|
+
var v, c, h, w, _;
|
|
554
|
+
const s = await W(r.auth, a, f), {
|
|
570
555
|
streamingManager: u,
|
|
571
|
-
chat:
|
|
572
|
-
} = await
|
|
573
|
-
n = -1, t.messages = O(g), (
|
|
556
|
+
chat: m
|
|
557
|
+
} = await z(g, r, d, l, t.chat);
|
|
558
|
+
n = -1, t.messages = O(g), (c = (v = r.callbacks).onNewMessage) == null || c.call(v, t.messages), m != null && m.id && m.id !== ((h = t.chat) == null ? void 0 : h.id) && ((_ = (w = r.callbacks).onNewChat) == null || _.call(w, m == null ? void 0 : m.id)), t.streamingManager = u, t.socketManager = s, t.chat = m, y(E.Functional), l.track("agent-chat", {
|
|
574
559
|
event: "connect",
|
|
575
|
-
chatId:
|
|
560
|
+
chatId: m.id,
|
|
576
561
|
agentId: g.id
|
|
577
562
|
});
|
|
578
563
|
}
|
|
579
|
-
async function
|
|
580
|
-
var s, u,
|
|
581
|
-
(s = t.socketManager) == null || s.disconnect(), await ((u = t.streamingManager) == null ? void 0 : u.disconnect()), delete t.streamingManager, delete t.socketManager, t.messages = O(g), (
|
|
564
|
+
async function k() {
|
|
565
|
+
var s, u, m, v, c;
|
|
566
|
+
(s = t.socketManager) == null || s.disconnect(), await ((u = t.streamingManager) == null ? void 0 : u.disconnect()), delete t.streamingManager, delete t.socketManager, t.messages = O(g), (v = (m = r.callbacks).onNewMessage) == null || v.call(m, t.messages), l.track("agent-chat", {
|
|
582
567
|
event: "disconnect",
|
|
583
|
-
chatId: (
|
|
568
|
+
chatId: (c = t.chat) == null ? void 0 : c.id,
|
|
584
569
|
agentId: g.id
|
|
585
570
|
});
|
|
586
571
|
}
|
|
587
|
-
async function
|
|
588
|
-
var u,
|
|
589
|
-
s !== t.chatMode && (
|
|
572
|
+
async function y(s) {
|
|
573
|
+
var u, m;
|
|
574
|
+
s !== t.chatMode && (l.track("agent-mode-change", {
|
|
590
575
|
mode: s
|
|
591
|
-
}), t.chatMode = s, t.chatMode !== E.Functional && await
|
|
576
|
+
}), t.chatMode = s, t.chatMode !== E.Functional && await k(), (m = (u = r.callbacks).onModeChange) == null || m.call(u, s));
|
|
592
577
|
}
|
|
593
578
|
return {
|
|
594
579
|
agent: g,
|
|
595
|
-
starterMessages: ((
|
|
596
|
-
connect:
|
|
597
|
-
disconnect:
|
|
598
|
-
changeMode:
|
|
580
|
+
starterMessages: ((S = g.knowledge) == null ? void 0 : S.starter_message) || [],
|
|
581
|
+
connect: p,
|
|
582
|
+
disconnect: k,
|
|
583
|
+
changeMode: y,
|
|
599
584
|
async reconnect() {
|
|
600
585
|
if (!t.chat)
|
|
601
|
-
return
|
|
602
|
-
const s = await
|
|
586
|
+
return p();
|
|
587
|
+
const s = await W(r.auth, a, f), {
|
|
603
588
|
streamingManager: u,
|
|
604
|
-
chat:
|
|
605
|
-
} = await
|
|
606
|
-
t.streamingManager = u, t.socketManager = s,
|
|
589
|
+
chat: m
|
|
590
|
+
} = await z(g, r, d, l, t.chat);
|
|
591
|
+
t.streamingManager = u, t.socketManager = s, y(E.Functional), l.track("agent-chat", {
|
|
607
592
|
event: "reconnect",
|
|
608
|
-
chatId:
|
|
593
|
+
chatId: m.id,
|
|
609
594
|
agentId: g.id
|
|
610
595
|
});
|
|
611
596
|
},
|
|
612
597
|
async chat(s, u = !1) {
|
|
613
|
-
var
|
|
598
|
+
var m, v, c, h, w, _;
|
|
614
599
|
try {
|
|
615
600
|
if (s.length >= 800)
|
|
616
601
|
throw new Error("Message cannot be more than 800 characters");
|
|
617
|
-
const
|
|
602
|
+
const $ = Date.now();
|
|
618
603
|
if (t.messages.push({
|
|
619
|
-
id:
|
|
604
|
+
id: P(),
|
|
620
605
|
role: "user",
|
|
621
606
|
content: s,
|
|
622
|
-
created_at: new Date(
|
|
623
|
-
}), (
|
|
607
|
+
created_at: new Date($).toISOString()
|
|
608
|
+
}), (v = (m = r.callbacks).onNewMessage) == null || v.call(m, t.messages), s.length === 0)
|
|
624
609
|
throw new Error("Message cannot be empty");
|
|
625
610
|
if (t.chatMode === E.Maintenance)
|
|
626
611
|
throw new Error("Chat is in maintenance mode");
|
|
@@ -630,58 +615,58 @@ async function we(e, a) {
|
|
|
630
615
|
if (!t.chat)
|
|
631
616
|
throw new Error("Chat is not initialized");
|
|
632
617
|
} else
|
|
633
|
-
t.chat || (t.chat = await
|
|
634
|
-
const
|
|
635
|
-
sessionId: (
|
|
618
|
+
t.chat || (t.chat = await d.newChat(g.id));
|
|
619
|
+
const R = await d.chat(g.id, t.chat.id, {
|
|
620
|
+
sessionId: (c = t.streamingManager) == null ? void 0 : c.sessionId,
|
|
636
621
|
streamId: (h = t.streamingManager) == null ? void 0 : h.streamId,
|
|
637
622
|
messages: t.messages,
|
|
638
623
|
chatMode: t.chatMode,
|
|
639
624
|
append_chat: u
|
|
640
625
|
});
|
|
641
|
-
return
|
|
626
|
+
return l.track("agent-message-send", {
|
|
642
627
|
event: "success",
|
|
643
628
|
messages: t.messages.length + 1
|
|
644
629
|
}), t.messages.push({
|
|
645
|
-
id:
|
|
630
|
+
id: P(),
|
|
646
631
|
role: "assistant",
|
|
647
|
-
content:
|
|
632
|
+
content: R.result ?? "",
|
|
648
633
|
created_at: (/* @__PURE__ */ new Date()).toISOString(),
|
|
649
|
-
matches:
|
|
650
|
-
}),
|
|
651
|
-
latency: Date.now() -
|
|
634
|
+
matches: R.matches
|
|
635
|
+
}), R.result && l.track("agent-message-received", {
|
|
636
|
+
latency: Date.now() - $,
|
|
652
637
|
messages: t.messages.length
|
|
653
|
-
}), (
|
|
654
|
-
} catch (
|
|
655
|
-
throw
|
|
638
|
+
}), (_ = (w = r.callbacks).onNewMessage) == null || _.call(w, t.messages), R;
|
|
639
|
+
} catch ($) {
|
|
640
|
+
throw l.track("agent-message-send", {
|
|
656
641
|
event: "error",
|
|
657
642
|
messages: t.messages.length
|
|
658
|
-
}),
|
|
643
|
+
}), $;
|
|
659
644
|
}
|
|
660
645
|
},
|
|
661
|
-
rate(s, u,
|
|
662
|
-
var h,
|
|
663
|
-
const
|
|
646
|
+
rate(s, u, m) {
|
|
647
|
+
var h, w, _, $;
|
|
648
|
+
const v = t.messages.find((R) => R.id === s);
|
|
664
649
|
if (t.chat) {
|
|
665
|
-
if (!
|
|
650
|
+
if (!v)
|
|
666
651
|
throw new Error("Message not found");
|
|
667
652
|
} else
|
|
668
653
|
throw new Error("Chat is not initialized");
|
|
669
|
-
const
|
|
670
|
-
return
|
|
671
|
-
event:
|
|
654
|
+
const c = ((h = v.matches) == null ? void 0 : h.map((R) => [R.document_id, R.id])) ?? [];
|
|
655
|
+
return l.track("agent-rate", {
|
|
656
|
+
event: m ? "update" : "create",
|
|
672
657
|
thumb: u === 1 ? "up" : "down",
|
|
673
|
-
knowledge_id: ((
|
|
674
|
-
matches:
|
|
658
|
+
knowledge_id: ((w = g.knowledge) == null ? void 0 : w.id) ?? "",
|
|
659
|
+
matches: c,
|
|
675
660
|
score: u
|
|
676
|
-
}),
|
|
677
|
-
knowledge_id: ((
|
|
661
|
+
}), m ? d.updateRating(g.id, t.chat.id, m, {
|
|
662
|
+
knowledge_id: ((_ = g.knowledge) == null ? void 0 : _.id) ?? "",
|
|
678
663
|
message_id: s,
|
|
679
|
-
matches:
|
|
664
|
+
matches: c,
|
|
680
665
|
score: u
|
|
681
|
-
}) :
|
|
682
|
-
knowledge_id: ((
|
|
666
|
+
}) : d.createRating(g.id, t.chat.id, {
|
|
667
|
+
knowledge_id: (($ = g.knowledge) == null ? void 0 : $.id) ?? "",
|
|
683
668
|
message_id: s,
|
|
684
|
-
matches:
|
|
669
|
+
matches: c,
|
|
685
670
|
score: u
|
|
686
671
|
});
|
|
687
672
|
},
|
|
@@ -689,11 +674,11 @@ async function we(e, a) {
|
|
|
689
674
|
var u;
|
|
690
675
|
if (!t.chat)
|
|
691
676
|
throw new Error("Chat is not initialized");
|
|
692
|
-
return
|
|
677
|
+
return l.track("agent-rate-delete", {
|
|
693
678
|
type: "text",
|
|
694
679
|
chat_id: (u = t.chat) == null ? void 0 : u.id,
|
|
695
680
|
id: s
|
|
696
|
-
}),
|
|
681
|
+
}), d.deleteRating(g.id, t.chat.id, s);
|
|
697
682
|
},
|
|
698
683
|
speak(s) {
|
|
699
684
|
if (!t.streamingManager)
|
|
@@ -716,27 +701,27 @@ async function we(e, a) {
|
|
|
716
701
|
throw new Error("Presenter is not initialized");
|
|
717
702
|
throw new Error("Invalid payload");
|
|
718
703
|
}
|
|
719
|
-
const
|
|
720
|
-
return
|
|
721
|
-
script:
|
|
704
|
+
const m = u();
|
|
705
|
+
return l.track("agent-speak", m), t.streamingManager.speak({
|
|
706
|
+
script: m
|
|
722
707
|
});
|
|
723
708
|
}
|
|
724
709
|
};
|
|
725
710
|
}
|
|
726
711
|
export {
|
|
727
712
|
E as ChatMode,
|
|
728
|
-
|
|
729
|
-
|
|
713
|
+
x as ChatProgress,
|
|
714
|
+
M as ConnectionState,
|
|
730
715
|
Q as DocumentType,
|
|
731
716
|
V as KnowledgeType,
|
|
732
717
|
B as PlanGroup,
|
|
733
718
|
X as Providers,
|
|
734
719
|
J as RateState,
|
|
735
|
-
|
|
736
|
-
|
|
720
|
+
I as StreamEvents,
|
|
721
|
+
N as StreamingState,
|
|
737
722
|
q as Subject,
|
|
738
|
-
|
|
739
|
-
|
|
723
|
+
U as UserPlan,
|
|
724
|
+
C as VideoType,
|
|
740
725
|
Y as VoiceAccess,
|
|
741
726
|
we as createAgentManager,
|
|
742
727
|
he as getAgent
|
package/dist/index.umd.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
(function(w,P){typeof exports=="object"&&typeof module<"u"?P(exports):typeof define=="function"&&define.amd?define(["exports"],P):(w=typeof globalThis<"u"?globalThis:w||self,P(w.index={}))})(this,function(w){"use strict";var P=(e=>(e.TRIAL="trial",e.BASIC="basic",e.ENTERPRISE="enterprise",e.LITE="lite",e.ADVANCED="advanced",e))(P||{}),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||{}),z=(e=>(e.Unrated="Unrated",e.Positive="Positive",e.Negative="Negative",e))(z||{}),E=(e=>(e.Functional="Functional",e.TextOnly="TextOnly",e.Maintenance="Maintenance",e))(E||{}),O=(e=>(e.Embed="embed",e.Query="query",e.Partial="partial",e.Answer="answer",e.Complete="done",e))(O||{}),H=(e=>(e.KnowledgeProcessing="knowledge/processing",e.KnowledgeIndexing="knowledge/indexing",e.KnowledgeFailed="knowledge/error",e.KnowledgeDone="knowledge/done",e))(H||{}),j=(e=>(e.Knowledge="knowledge",e.Document="document",e.Record="record",e))(j||{}),U=(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))(U||{}),M=(e=>(e.Clip="clip",e.Talk="talk",e))(M||{}),L=(e=>(e.Start="START",e.Stop="STOP",e))(L||{}),I=(e=>(e.ChatAnswer="chat/answer",e.ChatPartial="chat/partial",e.StreamDone="stream/done",e.StreamStarted="stream/started",e.StreamFailed="stream/error",e.StreamReady="stream/ready",e.StreamCreated="stream/created",e.StreamVideoCreated="stream-video/started",e.StreamVideoDone="stream-video/done",e.StreamVideoError="stream-video/error",e.StreamVideoRejected="stream-video/rejected",e))(I||{}),_=(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))(_||{}),B=(e=>(e.Amazon="amazon",e.Microsoft="microsoft",e.Afflorithmics="afflorithmics",e.Elevenlabs="elevenlabs",e))(B||{}),J=(e=>(e.Public="public",e.Premium="premium",e.Private="private",e))(J||{});const K="https://api.d-id.com",G="wss://notifications.d-id.com",ee="79f81a83a67430be2bc0fd61042b8faa",x=()=>Math.random().toString(16).slice(2);function V(){let e=window.localStorage.getItem("did_external_key_id");return e||(e=Math.random().toString(16).slice(2),window.localStorage.setItem("did_external_key_id",e)),e}let te=x();function q(e){if(e.type==="bearer")return`Bearer ${e.token}`;if(e.type==="basic")return`Basic ${btoa(`${e.username}:${e.password}`)}`;if(e.type==="key")return`Client-Key ${e.clientKey}.${V()}_${te}`;throw new Error(`Unknown auth type: ${e}`)}function F(e,a=K){const t=async(n,r)=>{const i=await fetch(a+(n!=null&&n.startsWith("/")?n:`/${n}`),{...r,headers:{...r==null?void 0:r.headers,Authorization:q(e),"Content-Type":"application/json"}});if(!i.ok){let o=await i.text().catch(()=>"Failed to fetch");throw new Error(o)}return i.json()};return{get(n,r){return t(n,{...r,method:"GET"})},post(n,r,i){return t(n,{...i,body:JSON.stringify(r),method:"POST"})},delete(n,r,i){return t(n,{...i,body:JSON.stringify(r),method:"DELETE"})},patch(n,r,i){return t(n,{...i,body:JSON.stringify(r),method:"PATCH"})}}}function Q(e,a=K){const t=F(e,`${a}/agents`);return{create(n,r){return t.post("/",n,r)},getAgents(n,r){return t.get(`/${n?`?tag=${n}`:""}`,r).then(i=>i??[])},getById(n,r){return t.get(`/${n}`,r)},delete(n,r){return t.delete(`/${n}`,void 0,r)},update(n,r,i){return t.patch(`/${n}`,r,i)},newChat(n,r){return t.post(`/${n}/chat`,void 0,r)},chat(n,r,i,o){return t.post(`/${n}/chat/${r}`,i,o)},createRating(n,r,i,o){return t.post(`/${n}/chat/${r}/ratings`,i,o)},updateRating(n,r,i,o,c){return t.patch(`/${n}/chat/${r}/ratings/${i}`,o,c)},deleteRating(n,r,i,o){return t.delete(`/${n}/chat/${r}/ratings/${i}`,o)}}}const ne=e=>new Promise(a=>setTimeout(a,e));function re(e){return new Promise((a,t)=>{const{callbacks:n,host:r,auth:i}=e,{onMessage:o=null,onOpen:c=null,onClose:g=null,onError:m=null}=n||{},f=new WebSocket(`${r}?authorization=${q(i)}`);f.onmessage=o,f.onclose=g,f.onerror=p=>{console.error(p),m==null||m("Websocket failed to connect",p),t(p)},f.onopen=p=>{c==null||c(p),a(f)}})}async function ie(e){const{retries:a=1}=e;let t=null;for(let n=0;(t==null?void 0:t.readyState)!==WebSocket.OPEN;n++)try{t=await re(e)}catch(r){if(n===a)throw r;await ne(n*500)}return t}async function X(e,a,t){const n=t!=null&&t.onMessage?[t.onMessage]:[],r=await ie({auth:e,host:a,callbacks:{onError:t==null?void 0:t.onError,onMessage:i=>{const o=JSON.parse(i.data);n.forEach(c=>c(o.event,o))}}});return{socket:r,disconnect:()=>r.close(),subscribeToEvents:i=>n.push(i)}}function ae(e,a,t){const n=F(e,`${a}/agents/${t}`);return{createStream(r){return n.post("/streams",{driver_id:r.driver_id,presenter_id:r.presenter_id,compatibility_mode:r.compatibility_mode,stream_warmup:r.stream_warmup,type:M.Clip})},startConnection(r,i,o){return n.post(`/streams/${r}/sdp`,{session_id:o,answer:i,type:M.Clip})},addIceCandidate(r,i,o){return n.post(`/streams/${r}/ice`,{session_id:o,...i,type:M.Clip})},sendStreamRequest(r,i,o){return n.post(`/streams/${r}`,{session_id:i,...o,type:M.Clip})},close(r,i){return n.delete(`/streams/${r}`,{session_id:i,type:M.Clip})}}}function se(e,a,t){const n=F(e,`${a}/agents/${t}`);return{createStream(r,i){return n.post("/streams",{source_url:r.source_url,driver_url:r.driver_url,face:r.face,config:r.config,stream_warmup:r.stream_warmup,type:M.Talk},i)},startConnection(r,i,o,c){return n.post(`/streams/${r}/sdp`,{session_id:o,answer:i,type:M.Talk},c)},addIceCandidate(r,i,o,c){return n.post(`/streams/${r}/ice`,{session_id:o,...i,type:M.Talk},c)},sendStreamRequest(r,i,o,c){return n.post(`/streams/${r}`,{session_id:i,...o,type:M.Talk},c)},close(r,i,o){return n.delete(`/streams/${r}`,{session_id:i,type:M.Talk},o)}}}function oe(e,a){return e.map((t,n)=>n===0?a?{index:n,timestamp:t.timestamp,bytesReceived:t.bytesReceived-a.bytesReceived,packetsReceived:t.packetsReceived-a.packetsReceived,packetsLost:t.packetsLost-a.packetsLost,jitter:t.jitter,frameWidth:t.frameWidth,frameHeight:t.frameHeight,framesPerSecond:t.framesPerSecond}:{index:n,timestamp:t.timestamp,bytesReceived:t.bytesReceived,packetsReceived:t.packetsReceived,packetsLost:t.packetsLost,jitter:t.jitter,frameWidth:t.frameWidth,frameHeight:t.frameHeight,framesPerSecond:t.framesPerSecond}:{index:n,timestamp:t.timestamp,bytesReceived:t.bytesReceived-e[n-1].bytesReceived,packetsReceived:t.packetsReceived-e[n-1].packetsReceived,packetsLost:t.packetsLost-e[n-1].packetsLost,jitter:t.jitter,frameWidth:t.frameWidth,frameHeight:t.frameHeight,framesPerSecond:t.framesPerSecond})}let Y=!1;const T=(e,a)=>Y&&console.log(e,a),de=(window.RTCPeerConnection||window.webkitRTCPeerConnection||window.mozRTCPeerConnection).bind(window);function ce(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 ge(e,a){let t=[],n=0,r=0,i=0,o;return setInterval(()=>{e.getStats().then(g=>{g.forEach(m=>{if(m.type==="inbound-rtp"&&m.kind==="video"){if(r=t.length-1,m&&t[r]){const f=m.bytesReceived,p=t[r].bytesReceived,y=o;let S;if(o=f-p>0,i=o?0:++i,y!==o)if(o)n=t.length,a==null||a(L.Start,S);else{const b=t.slice(n),N=n===0?void 0:t[n-1];S=oe(b,N),S=S.sort((R,s)=>s.packetsLost-R.packetsLost).slice(0,5)}!o&&i===3&&(a==null||a(L.Stop,S))}t.push(m)}})})},500)}async function me(e,a,{debug:t=!1,callbacks:n,auth:r,analytics:i,baseURL:o=K}){Y=t;let c;const{startConnection:g,sendStreamRequest:m,close:f,createStream:p,addIceCandidate:y}=a.videoType===M.Clip?ae(r,o,e):se(r,o,e),{id:S,offer:b,ice_servers:N,session_id:R}=await p(a),s=new de({iceServers:N}),u=s.createDataChannel("JanusDataChannel");if(!R)throw new Error("Could not create session_id");const l=ge(s,n.onVideoStateChange);s.onicecandidate=d=>{T("peerConnection.onicecandidate",d),d.candidate&&d.candidate.sdpMid&&d.candidate.sdpMLineIndex!==null?y(S,{candidate:d.candidate.candidate,sdpMid:d.candidate.sdpMid,sdpMLineIndex:d.candidate.sdpMLineIndex},R):y(S,{candidate:null},R)},s.oniceconnectionstatechange=()=>{var h;T("peerConnection.oniceconnectionstatechange => "+s.iceConnectionState);const d=ce(s.iceConnectionState);d===_.Connected?c=setTimeout(()=>{var k;(k=n.onConnectionStateChange)==null||k.call(n,_.Connected)},5e3):(clearTimeout(c),(h=n.onConnectionStateChange)==null||h.call(n,d))},s.ontrack=d=>{var h;T("peerConnection.ontrack",d),(h=n.onSrcObjectReady)==null||h.call(n,d.streams[0])},u.onmessage=d=>{var h,k;if(u.readyState==="open"){const[C,A]=d.data.split(":");C===I.StreamStarted?i==null||i.track("agent-video",{event:"start",...d}):C===I.StreamDone?i==null||i.track("agent-video",{event:"stop",...d}):C===I.StreamFailed?((h=n.onVideoStateChange)==null||h.call(n,L.Stop,{event:C,data:A}),clearInterval(l)):C===I.StreamReady?(clearTimeout(c),(k=n.onConnectionStateChange)==null||k.call(n,_.Connected)):C===I.StreamCreated?i==null||i.track("agent-video",{event:"created",...d}):C===I.StreamVideoCreated?i==null||i.track("agent-video",{event:"video-created",...d}):C===I.StreamVideoDone?i==null||i.track("agent-video",{event:"video-done",...d}):C===I.StreamVideoError&&(i==null||i.track("agent-video",{event:"video-error",...d}))}},await s.setRemoteDescription(b),T("set remote description OK");const v=await s.createAnswer();return T("create answer OK"),await s.setLocalDescription(v),T("set local description OK"),await g(S,v,R),T("start connection OK"),{speak(d){return m(S,R,d)},async disconnect(){var d,h;S&&(s&&(s.close(),s.oniceconnectionstatechange=null,s.onnegotiationneeded=null,s.onicecandidate=null,s.ontrack=null),await f(S,R).catch(k=>{}),(d=n.onConnectionStateChange)==null||d.call(n,_.New),(h=n.onVideoStateChange)==null||h.call(n,L.Stop),clearInterval(l))},sessionId:R,streamId:S}}function le(e){const a={mixPanelKey:e.mixPanelKey||"testKey",distinct_id:e.distinctId||V(),isEnabled:e.isEnabled??!0,agentId:e.agent.id,owner_id:e.agent.owner_id??""};return{...a,getRandom:()=>Math.random().toString(16).slice(2),track(t,n){if(!this.isEnabled)return Promise.reject("MixPanel analytics is disabled on creation");const r={method:"POST",headers:{"Content-Type":"application/x-www-form-urlencoded"},body:new URLSearchParams({data:JSON.stringify([{event:t,properties:{...n,...a,time:Date.now(),$insert_id:this.getRandom(),origin:window.location.href,"Screen Height":window.screen.height||window.innerWidth,"Screen Width":window.screen.width||window.innerHeight,"User Agent":navigator.userAgent}}])})};return fetch("https://api-js.mixpanel.com/track/?verbose=1&ip=1",r).then(i=>i.json()).catch(i=>console.error(i))}}}function ue(e){var n,r,i,o,c;const a=()=>/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:`${a()=="Mobile"}`,browser:navigator.userAgent,origin:window.location.origin,agentType:(n=e.presenter)==null?void 0:n.type,agentVoice:{voiceId:(i=(r=e.presenter)==null?void 0:r.voice)==null?void 0:i.voice_id,provider:(c=(o=e.presenter)==null?void 0:o.voice)==null?void 0:c.type}}}function he(e){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");return{videoType:M.Talk,source_url:e.presenter.source_url,stream_warmup:!0}}function Z(e,a,t,n,r){return new Promise(async(i,o)=>{let c=r;const g=await me(e.id,he(e),{...a,callbacks:{...a.callbacks,onConnectionStateChange:async m=>{var f,p;if(m===_.Connected)try{c||(c=await t.newChat(e.id),n.track("agent-chat",{event:"created",chat_id:c.id,agent_id:e.id})),g&&i({chat:c,streamingManager:g})}catch(y){console.error(y),o("Cannot create new chat")}else m===_.Fail&&o(new Error("Cannot create connection"));(p=(f=a.callbacks).onConnectionStateChange)==null||p.call(f,m)},onVideoStateChange(m,f){var p,y;n.track("agent-video",{event:m,rtc_stats:f??[]}),(y=(p=a.callbacks).onVideoStateChange)==null||y.call(p,m,f)}}}).catch(o)})}function we(e,a,t){return Q(a,t||K).getById(e)}function D(e){let a="";if(e.greetings&&e.greetings.length>0){const t=Math.floor(Math.random()*e.greetings.length);a=e.greetings[t]}else a=`Hi! I'm ${e.preview_name}, welcome to agents. How can I help you?`;return[{content:a,id:x(),role:"assistant",created_at:new Date().toISOString()}]}async function fe(e,a){var b,N,R;const t={messages:[],chatMode:a.mode||E.Functional};let n=-1;const r=a.wsURL||G,i=a.baseURL||K,o=a.mixpanelKey||ee,c=Q(a.auth,i),g=await c.getById(e);t.messages=D(g),(N=(b=a.callbacks).onNewMessage)==null||N.call(b,t.messages);const m=le({mixPanelKey:o,agent:g,...a});m.track("agent-sdk",{event:"loaded",...ue(g)});const f={onMessage:(s,u)=>{var l,v;if("content"in u){const{content:d}=u,h=t.messages[t.messages.length-1];(h==null?void 0:h.role)==="assistant"&&(n<t.messages.length&&(h.content=s===O.Partial?h.content+d:d),s===O.Answer&&(n=t.messages.length)),s===O.Complete&&m.track("agent-message-received",{messages:t.messages.length}),(v=(l=a.callbacks).onNewMessage)==null||v.call(l,t.messages)}}};async function p(){var v,d,h,k,C;const s=await X(a.auth,r,f),{streamingManager:u,chat:l}=await Z(g,a,c,m,t.chat);n=-1,t.messages=D(g),(d=(v=a.callbacks).onNewMessage)==null||d.call(v,t.messages),l!=null&&l.id&&l.id!==((h=t.chat)==null?void 0:h.id)&&((C=(k=a.callbacks).onNewChat)==null||C.call(k,l==null?void 0:l.id)),t.streamingManager=u,t.socketManager=s,t.chat=l,S(E.Functional),m.track("agent-chat",{event:"connect",chatId:l.id,agentId:g.id})}async function y(){var s,u,l,v,d;(s=t.socketManager)==null||s.disconnect(),await((u=t.streamingManager)==null?void 0:u.disconnect()),delete t.streamingManager,delete t.socketManager,t.messages=D(g),(v=(l=a.callbacks).onNewMessage)==null||v.call(l,t.messages),m.track("agent-chat",{event:"disconnect",chatId:(d=t.chat)==null?void 0:d.id,agentId:g.id})}async function S(s){var u,l;s!==t.chatMode&&(m.track("agent-mode-change",{mode:s}),t.chatMode=s,t.chatMode!==E.Functional&&await y(),(l=(u=a.callbacks).onModeChange)==null||l.call(u,s))}return{agent:g,starterMessages:((R=g.knowledge)==null?void 0:R.starter_message)||[],connect:p,disconnect:y,changeMode:S,async reconnect(){if(!t.chat)return p();const s=await X(a.auth,r,f),{streamingManager:u,chat:l}=await Z(g,a,c,m,t.chat);t.streamingManager=u,t.socketManager=s,S(E.Functional),m.track("agent-chat",{event:"reconnect",chatId:l.id,agentId:g.id})},async chat(s,u=!1){var l,v,d,h,k,C;try{if(s.length>=800)throw new Error("Message cannot be more than 800 characters");const A=Date.now();if(t.messages.push({id:x(),role:"user",content:s,created_at:new Date(A).toISOString()}),(v=(l=a.callbacks).onNewMessage)==null||v.call(l,t.messages),s.length===0)throw new Error("Message cannot be empty");if(t.chatMode===E.Maintenance)throw new Error("Chat is in maintenance mode");if(t.chatMode!==E.TextOnly){if(!t.streamingManager)throw new Error("Streaming manager is not initialized");if(!t.chat)throw new Error("Chat is not initialized")}else t.chat||(t.chat=await c.newChat(g.id));const $=await c.chat(g.id,t.chat.id,{sessionId:(d=t.streamingManager)==null?void 0:d.sessionId,streamId:(h=t.streamingManager)==null?void 0:h.streamId,messages:t.messages,chatMode:t.chatMode,append_chat:u});return m.track("agent-message-send",{event:"success",messages:t.messages.length+1}),t.messages.push({id:x(),role:"assistant",content:$.result??"",created_at:new Date().toISOString(),matches:$.matches}),$.result&&m.track("agent-message-received",{latency:Date.now()-A,messages:t.messages.length}),(C=(k=a.callbacks).onNewMessage)==null||C.call(k,t.messages),$}catch(A){throw m.track("agent-message-send",{event:"error",messages:t.messages.length}),A}},rate(s,u,l){var h,k,C,A;const v=t.messages.find($=>$.id===s);if(t.chat){if(!v)throw new Error("Message not found")}else throw new Error("Chat is not initialized");const d=((h=v.matches)==null?void 0:h.map($=>[$.document_id,$.id]))??[];return m.track("agent-rate",{event:l?"update":"create",thumb:u===1?"up":"down",knowledge_id:((k=g.knowledge)==null?void 0:k.id)??"",matches:d,score:u}),l?c.updateRating(g.id,t.chat.id,l,{knowledge_id:((C=g.knowledge)==null?void 0:C.id)??"",message_id:s,matches:d,score:u}):c.createRating(g.id,t.chat.id,{knowledge_id:((A=g.knowledge)==null?void 0:A.id)??"",message_id:s,matches:d,score:u})},deleteRate(s){var u;if(!t.chat)throw new Error("Chat is not initialized");return m.track("agent-rate-delete",{type:"text",chat_id:(u=t.chat)==null?void 0:u.id,id:s}),c.deleteRating(g.id,t.chat.id,s)},speak(s){if(!t.streamingManager)throw new Error("Streaming manager is not initialized");function u(){if(g.presenter){if(s.type==="text")return{type:"text",provider:s.provider?s.provider:g.presenter.voice,input:s.input,ssml:s.ssml||!1};if(s.type==="audio")return{type:"audio",audio_url:s.audio_url}}else throw new Error("Presenter is not initialized");throw new Error("Invalid payload")}const l=u();return m.track("agent-speak",l),t.streamingManager.speak({script:l})}}}w.ChatMode=E,w.ChatProgress=O,w.ConnectionState=_,w.DocumentType=U,w.KnowledgeType=j,w.PlanGroup=W,w.Providers=B,w.RateState=z,w.StreamEvents=I,w.StreamingState=L,w.Subject=H,w.UserPlan=P,w.VideoType=M,w.VoiceAccess=J,w.createAgentManager=fe,w.getAgent=we,Object.defineProperty(w,Symbol.toStringTag,{value:"Module"})});
|
|
1
|
+
(function(w,b){typeof exports=="object"&&typeof module<"u"?b(exports):typeof define=="function"&&define.amd?define(["exports"],b):(w=typeof globalThis<"u"?globalThis:w||self,b(w.index={}))})(this,function(w){"use strict";var b=(e=>(e.TRIAL="trial",e.BASIC="basic",e.ENTERPRISE="enterprise",e.LITE="lite",e.ADVANCED="advanced",e))(b||{}),j=(e=>(e.TRIAL="deid-trial",e.PRO="deid-pro",e.ENTERPRISE="deid-enterprise",e.LITE="deid-lite",e.ADVANCED="deid-advanced",e.BUILD="deid-api-build",e.LAUNCH="deid-api-launch",e.SCALE="deid-api-scale",e))(j||{}),z=(e=>(e.Unrated="Unrated",e.Positive="Positive",e.Negative="Negative",e))(z||{}),$=(e=>(e.Functional="Functional",e.TextOnly="TextOnly",e.Maintenance="Maintenance",e))($||{}),N=(e=>(e.Embed="embed",e.Query="query",e.Partial="partial",e.Answer="answer",e.Complete="done",e))(N||{}),H=(e=>(e.KnowledgeProcessing="knowledge/processing",e.KnowledgeIndexing="knowledge/indexing",e.KnowledgeFailed="knowledge/error",e.KnowledgeDone="knowledge/done",e))(H||{}),x=(e=>(e.Knowledge="knowledge",e.Document="document",e.Record="record",e))(x||{}),U=(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))(U||{}),S=(e=>(e.Clip="clip",e.Talk="talk",e))(S||{}),O=(e=>(e.Start="START",e.Stop="STOP",e))(O||{}),R=(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))(R||{}),k=(e=>(e[e.New=0]="New",e[e.Fail=1]="Fail",e[e.Connected=2]="Connected",e[e.Connecting=3]="Connecting",e[e.Terminating=4]="Terminating",e))(k||{}),B=(e=>(e.Amazon="amazon",e.Microsoft="microsoft",e.Afflorithmics="afflorithmics",e.Elevenlabs="elevenlabs",e))(B||{}),J=(e=>(e.Public="public",e.Premium="premium",e.Private="private",e))(J||{});const F="https://api.d-id.com",G="wss://notifications.d-id.com",ee="79f81a83a67430be2bc0fd61042b8faa",D=()=>Math.random().toString(16).slice(2);function V(){let e=window.localStorage.getItem("did_external_key_id");return e||(e=Math.random().toString(16).slice(2),window.localStorage.setItem("did_external_key_id",e)),e}let te=D();function q(e){if(e.type==="bearer")return`Bearer ${e.token}`;if(e.type==="basic")return`Basic ${btoa(`${e.username}:${e.password}`)}`;if(e.type==="key")return`Client-Key ${e.clientKey}.${V()}_${te}`;throw new Error(`Unknown auth type: ${e}`)}function K(e,i=F){const t=async(n,a)=>{const r=await fetch(i+(n!=null&&n.startsWith("/")?n:`/${n}`),{...a,headers:{...a==null?void 0:a.headers,Authorization:q(e),"Content-Type":"application/json"}});if(!r.ok){let o=await r.text().catch(()=>"Failed to fetch");throw new Error(o)}return r.json()};return{get(n,a){return t(n,{...a,method:"GET"})},post(n,a,r){return t(n,{...r,body:JSON.stringify(a),method:"POST"})},delete(n,a,r){return t(n,{...r,body:JSON.stringify(a),method:"DELETE"})},patch(n,a,r){return t(n,{...r,body:JSON.stringify(a),method:"PATCH"})}}}function Q(e,i=F){const t=K(e,`${i}/agents`);return{create(n,a){return t.post("/",n,a)},getAgents(n,a){return t.get(`/${n?`?tag=${n}`:""}`,a).then(r=>r??[])},getById(n,a){return t.get(`/${n}`,a)},delete(n,a){return t.delete(`/${n}`,void 0,a)},update(n,a,r){return t.patch(`/${n}`,a,r)},newChat(n,a){return t.post(`/${n}/chat`,void 0,a)},chat(n,a,r,o){return t.post(`/${n}/chat/${a}`,r,o)},createRating(n,a,r,o){return t.post(`/${n}/chat/${a}/ratings`,r,o)},updateRating(n,a,r,o,d){return t.patch(`/${n}/chat/${a}/ratings/${r}`,o,d)},deleteRating(n,a,r,o){return t.delete(`/${n}/chat/${a}/ratings/${r}`,o)}}}const ne=e=>new Promise(i=>setTimeout(i,e));function ae(e){return new Promise((i,t)=>{const{callbacks:n,host:a,auth:r}=e,{onMessage:o=null,onOpen:d=null,onClose:g=null,onError:l=null}=n||{},p=new WebSocket(`${a}?authorization=${q(r)}`);p.onmessage=o,p.onclose=g,p.onerror=v=>{console.error(v),l==null||l("Websocket failed to connect",v),t(v)},p.onopen=v=>{d==null||d(v),i(p)}})}async function ie(e){const{retries:i=1}=e;let t=null;for(let n=0;(t==null?void 0:t.readyState)!==WebSocket.OPEN;n++)try{t=await ae(e)}catch(a){if(n===i)throw a;await ne(n*500)}return t}async function X(e,i,t){const n=t!=null&&t.onMessage?[t.onMessage]:[],a=await ie({auth:e,host:i,callbacks:{onError:t==null?void 0:t.onError,onMessage:r=>{const o=JSON.parse(r.data);n.forEach(d=>d(o.event,o))}}});return{socket:a,disconnect:()=>a.close(),subscribeToEvents:r=>n.push(r)}}function re(e,i,t){const n=K(e,`${i}/agents/${t}`);return{createStream(a){return n.post("/streams",{driver_id:a.driver_id,presenter_id:a.presenter_id,compatibility_mode:a.compatibility_mode,stream_warmup:a.stream_warmup,type:S.Clip})},startConnection(a,r,o){return n.post(`/streams/${a}/sdp`,{session_id:o,answer:r,type:S.Clip})},addIceCandidate(a,r,o){return n.post(`/streams/${a}/ice`,{session_id:o,...r,type:S.Clip})},sendStreamRequest(a,r,o){return n.post(`/streams/${a}`,{session_id:r,...o,type:S.Clip})},close(a,r){return n.delete(`/streams/${a}`,{session_id:r,type:S.Clip})}}}function se(e,i,t){const n=K(e,`${i}/agents/${t}`);return{createStream(a,r){return n.post("/streams",{source_url:a.source_url,driver_url:a.driver_url,face:a.face,config:a.config,stream_warmup:a.stream_warmup,type:S.Talk},r)},startConnection(a,r,o,d){return n.post(`/streams/${a}/sdp`,{session_id:o,answer:r,type:S.Talk},d)},addIceCandidate(a,r,o,d){return n.post(`/streams/${a}/ice`,{session_id:o,...r,type:S.Talk},d)},sendStreamRequest(a,r,o,d){return n.post(`/streams/${a}`,{session_id:r,...o,type:S.Talk},d)},close(a,r,o){return n.delete(`/streams/${a}`,{session_id:r,type:S.Talk},o)}}}function oe(e,i){return e.map((t,n)=>n===0?i?{index:n,timestamp:t.timestamp,bytesReceived:t.bytesReceived-i.bytesReceived,packetsReceived:t.packetsReceived-i.packetsReceived,packetsLost:t.packetsLost-i.packetsLost,jitter:t.jitter,frameWidth:t.frameWidth,frameHeight:t.frameHeight,framesPerSecond:t.framesPerSecond}:{index:n,timestamp:t.timestamp,bytesReceived:t.bytesReceived,packetsReceived:t.packetsReceived,packetsLost:t.packetsLost,jitter:t.jitter,frameWidth:t.frameWidth,frameHeight:t.frameHeight,framesPerSecond:t.framesPerSecond}:{index:n,timestamp:t.timestamp,bytesReceived:t.bytesReceived-e[n-1].bytesReceived,packetsReceived:t.packetsReceived-e[n-1].packetsReceived,packetsLost:t.packetsLost-e[n-1].packetsLost,jitter:t.jitter,frameWidth:t.frameWidth,frameHeight:t.frameHeight,framesPerSecond:t.framesPerSecond})}let Y=!1;const E=(e,i)=>Y&&console.log(e,i),ce=(window.RTCPeerConnection||window.webkitRTCPeerConnection||window.mozRTCPeerConnection).bind(window);function de(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 ge(e,i){let t=[],n=0,a=0,r=0,o;return setInterval(()=>{e.getStats().then(g=>{g.forEach(l=>{if(l.type==="inbound-rtp"&&l.kind==="video"){if(a=t.length-1,l&&t[a]){const p=l.bytesReceived,v=t[a].bytesReceived,M=o;let C;if(o=p-v>0,r=o?0:++r,M!==o)if(o)n=t.length,i==null||i(O.Start,C);else{const L=t.slice(n),P=n===0?void 0:t[n-1];C=oe(L,P),C=C.sort((_,s)=>s.packetsLost-_.packetsLost).slice(0,5)}!o&&r===3&&(i==null||i(O.Stop,C))}t.push(l)}})})},500)}async function le(e,i,{debug:t=!1,callbacks:n,auth:a,analytics:r,baseURL:o=F}){Y=t;let d;const{startConnection:g,sendStreamRequest:l,close:p,createStream:v,addIceCandidate:M}=i.videoType===S.Clip?re(a,o,e):se(a,o,e),{id:C,offer:L,ice_servers:P,session_id:_}=await v(i),s=new ce({iceServers:P}),u=s.createDataChannel("JanusDataChannel");if(!_)throw new Error("Could not create session_id");const m=ge(s,n.onVideoStateChange);s.onicecandidate=c=>{E("peerConnection.onicecandidate",c),c.candidate&&c.candidate.sdpMid&&c.candidate.sdpMLineIndex!==null?M(C,{candidate:c.candidate.candidate,sdpMid:c.candidate.sdpMid,sdpMLineIndex:c.candidate.sdpMLineIndex},_):M(C,{candidate:null},_)},s.oniceconnectionstatechange=()=>{var h;E("peerConnection.oniceconnectionstatechange => "+s.iceConnectionState);const c=de(s.iceConnectionState);c===k.Connected?(r.track("agent-chat-loaded"),d=setTimeout(()=>{var f;(f=n.onConnectionStateChange)==null||f.call(n,k.Connected)},5e3)):(clearTimeout(d),(h=n.onConnectionStateChange)==null||h.call(n,c))},s.ontrack=c=>{var h;E("peerConnection.ontrack",c),(h=n.onSrcObjectReady)==null||h.call(n,c.streams[0])},u.onmessage=c=>{var h;if(u.readyState==="open"){const[f,I]=c.data.split(":");f===R.StreamVideoCreated||R.StreamVideoDone||R.StreamVideoError||R.StreamVideoRejected?r.track("agent-video",{event:f,...c}):f===R.StreamFailed||f===R.StreamDone?clearInterval(m):f===R.StreamReady?(clearTimeout(d),(h=n.onConnectionStateChange)==null||h.call(n,k.Connected)):f===R.StreamCreated&&r.track("agent-video",{event:"created"})}},await s.setRemoteDescription(L),E("set remote description OK");const y=await s.createAnswer();return E("create answer OK"),await s.setLocalDescription(y),E("set local description OK"),await g(C,y,_),E("start connection OK"),{speak(c){return l(C,_,c)},async disconnect(){var c,h;C&&(s&&(s.close(),s.oniceconnectionstatechange=null,s.onnegotiationneeded=null,s.onicecandidate=null,s.ontrack=null),await p(C,_).catch(f=>{}),(c=n.onConnectionStateChange)==null||c.call(n,k.New),(h=n.onVideoStateChange)==null||h.call(n,O.Stop),clearInterval(m))},sessionId:_,streamId:C}}function me(e){const i={token:e.token||"testKey",distinct_id:e.distinctId||V(),isEnabled:e.isEnabled??!0,agentId:e.agent.id,owner_id:e.agent.owner_id??""};return{...i,getRandom:()=>Math.random().toString(16).slice(2),track(t,n){if(!this.isEnabled)return Promise.reject("MixPanel analytics is disabled on creation");const a={method:"POST",headers:{"Content-Type":"application/x-www-form-urlencoded"},body:new URLSearchParams({data:JSON.stringify([{event:t,properties:{...n,...i,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",a).then(r=>r.json()).catch(r=>console.error(r))}}}function ue(e){var n,a,r,o,d;const i=()=>/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:`${i()=="Mobile"}`,browser:navigator.userAgent,origin:window.location.origin,agentType:(n=e.presenter)==null?void 0:n.type,agentVoice:{voiceId:(r=(a=e.presenter)==null?void 0:a.voice)==null?void 0:r.voice_id,provider:(d=(o=e.presenter)==null?void 0:o.voice)==null?void 0:d.type}}}function he(e){if(e.presenter){if(e.presenter.type===S.Clip)return{videoType:S.Clip,driver_id:e.presenter.driver_id,presenter_id:e.presenter.presenter_id,stream_warmup:!0}}else throw new Error("Presenter is not initialized");return{videoType:S.Talk,source_url:e.presenter.source_url,stream_warmup:!0}}function Z(e,i,t,n,a){return new Promise(async(r,o)=>{let d=a;const g=await le(e.id,he(e),{...i,callbacks:{...i.callbacks,onConnectionStateChange:async l=>{var p,v;if(l===k.Connected)try{d||(d=await t.newChat(e.id),n.track("agent-chat",{event:"created",chat_id:d.id,agent_id:e.id})),g&&r({chat:d,streamingManager:g})}catch(M){console.error(M),o("Cannot create new chat")}else l===k.Fail&&o(new Error("Cannot create connection"));(v=(p=i.callbacks).onConnectionStateChange)==null||v.call(p,l)},onVideoStateChange(l,p){var v,M;n.track("agent-video",{event:l,rtc_stats:p??[]}),(M=(v=i.callbacks).onVideoStateChange)==null||M.call(v,l,p)}},analytics:n}).catch(o)})}function we(e,i,t){return Q(i,t||F).getById(e)}function W(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}, welcome to agents. How can I help you?`;return[{content:i,id:D(),role:"assistant",created_at:new Date().toISOString()}]}async function fe(e,i){var L,P,_;const t={messages:[],chatMode:i.mode||$.Functional};let n=-1;const a=i.wsURL||G,r=i.baseURL||F,o=i.mixpanelKey||ee,d=Q(i.auth,r),g=await d.getById(e);t.messages=W(g),(P=(L=i.callbacks).onNewMessage)==null||P.call(L,t.messages);const l=me({token:o,agent:g,...i});l.track("agent-sdk",{event:"loaded",...ue(g)});const p={onMessage:(s,u)=>{var m,y;if("content"in u){const{content:c}=u,h=t.messages[t.messages.length-1];(h==null?void 0:h.role)==="assistant"&&(n<t.messages.length&&(h.content=s===N.Partial?h.content+c:c),s===N.Answer&&(n=t.messages.length)),s===N.Complete&&l.track("agent-message-received",{messages:t.messages.length}),(y=(m=i.callbacks).onNewMessage)==null||y.call(m,t.messages)}}};async function v(){var y,c,h,f,I;const s=await X(i.auth,a,p),{streamingManager:u,chat:m}=await Z(g,i,d,l,t.chat);n=-1,t.messages=W(g),(c=(y=i.callbacks).onNewMessage)==null||c.call(y,t.messages),m!=null&&m.id&&m.id!==((h=t.chat)==null?void 0:h.id)&&((I=(f=i.callbacks).onNewChat)==null||I.call(f,m==null?void 0:m.id)),t.streamingManager=u,t.socketManager=s,t.chat=m,C($.Functional),l.track("agent-chat",{event:"connect",chatId:m.id,agentId:g.id})}async function M(){var s,u,m,y,c;(s=t.socketManager)==null||s.disconnect(),await((u=t.streamingManager)==null?void 0:u.disconnect()),delete t.streamingManager,delete t.socketManager,t.messages=W(g),(y=(m=i.callbacks).onNewMessage)==null||y.call(m,t.messages),l.track("agent-chat",{event:"disconnect",chatId:(c=t.chat)==null?void 0:c.id,agentId:g.id})}async function C(s){var u,m;s!==t.chatMode&&(l.track("agent-mode-change",{mode:s}),t.chatMode=s,t.chatMode!==$.Functional&&await M(),(m=(u=i.callbacks).onModeChange)==null||m.call(u,s))}return{agent:g,starterMessages:((_=g.knowledge)==null?void 0:_.starter_message)||[],connect:v,disconnect:M,changeMode:C,async reconnect(){if(!t.chat)return v();const s=await X(i.auth,a,p),{streamingManager:u,chat:m}=await Z(g,i,d,l,t.chat);t.streamingManager=u,t.socketManager=s,C($.Functional),l.track("agent-chat",{event:"reconnect",chatId:m.id,agentId:g.id})},async chat(s,u=!1){var m,y,c,h,f,I;try{if(s.length>=800)throw new Error("Message cannot be more than 800 characters");const T=Date.now();if(t.messages.push({id:D(),role:"user",content:s,created_at:new Date(T).toISOString()}),(y=(m=i.callbacks).onNewMessage)==null||y.call(m,t.messages),s.length===0)throw new Error("Message cannot be empty");if(t.chatMode===$.Maintenance)throw new Error("Chat is in maintenance mode");if(t.chatMode!==$.TextOnly){if(!t.streamingManager)throw new Error("Streaming manager is not initialized");if(!t.chat)throw new Error("Chat is not initialized")}else t.chat||(t.chat=await d.newChat(g.id));const A=await d.chat(g.id,t.chat.id,{sessionId:(c=t.streamingManager)==null?void 0:c.sessionId,streamId:(h=t.streamingManager)==null?void 0:h.streamId,messages:t.messages,chatMode:t.chatMode,append_chat:u});return l.track("agent-message-send",{event:"success",messages:t.messages.length+1}),t.messages.push({id:D(),role:"assistant",content:A.result??"",created_at:new Date().toISOString(),matches:A.matches}),A.result&&l.track("agent-message-received",{latency:Date.now()-T,messages:t.messages.length}),(I=(f=i.callbacks).onNewMessage)==null||I.call(f,t.messages),A}catch(T){throw l.track("agent-message-send",{event:"error",messages:t.messages.length}),T}},rate(s,u,m){var h,f,I,T;const y=t.messages.find(A=>A.id===s);if(t.chat){if(!y)throw new Error("Message not found")}else throw new Error("Chat is not initialized");const c=((h=y.matches)==null?void 0:h.map(A=>[A.document_id,A.id]))??[];return l.track("agent-rate",{event:m?"update":"create",thumb:u===1?"up":"down",knowledge_id:((f=g.knowledge)==null?void 0:f.id)??"",matches:c,score:u}),m?d.updateRating(g.id,t.chat.id,m,{knowledge_id:((I=g.knowledge)==null?void 0:I.id)??"",message_id:s,matches:c,score:u}):d.createRating(g.id,t.chat.id,{knowledge_id:((T=g.knowledge)==null?void 0:T.id)??"",message_id:s,matches:c,score:u})},deleteRate(s){var u;if(!t.chat)throw new Error("Chat is not initialized");return l.track("agent-rate-delete",{type:"text",chat_id:(u=t.chat)==null?void 0:u.id,id:s}),d.deleteRating(g.id,t.chat.id,s)},speak(s){if(!t.streamingManager)throw new Error("Streaming manager is not initialized");function u(){if(g.presenter){if(s.type==="text")return{type:"text",provider:s.provider?s.provider:g.presenter.voice,input:s.input,ssml:s.ssml||!1};if(s.type==="audio")return{type:"audio",audio_url:s.audio_url}}else throw new Error("Presenter is not initialized");throw new Error("Invalid payload")}const m=u();return l.track("agent-speak",m),t.streamingManager.speak({script:m})}}}w.ChatMode=$,w.ChatProgress=N,w.ConnectionState=k,w.DocumentType=U,w.KnowledgeType=x,w.PlanGroup=j,w.Providers=B,w.RateState=z,w.StreamEvents=R,w.StreamingState=O,w.Subject=H,w.UserPlan=b,w.VideoType=S,w.VoiceAccess=J,w.createAgentManager=fe,w.getAgent=we,Object.defineProperty(w,Symbol.toStringTag,{value:"Module"})});
|
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
import { Agent } from '..';
|
|
2
2
|
export interface AnalyticsOptions {
|
|
3
|
-
|
|
3
|
+
token: string;
|
|
4
4
|
agent: Agent;
|
|
5
5
|
isEnabled?: boolean;
|
|
6
6
|
distinctId?: string;
|
|
7
7
|
}
|
|
8
8
|
export interface Analytics {
|
|
9
|
-
|
|
9
|
+
token: string;
|
|
10
10
|
distinct_id?: string;
|
|
11
11
|
isEnabled: boolean;
|
|
12
12
|
chatId?: string;
|