@d-id/client-sdk 1.0.19-beta.36 → 1.0.19-beta.38
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 +208 -217
- package/dist/index.umd.cjs +1 -1
- package/dist/src/{lib/api → api}/agents.d.ts +1 -1
- package/dist/src/{lib/api → api}/clipStream.d.ts +1 -1
- package/dist/src/{lib/api → api}/getClient.d.ts +1 -1
- package/dist/src/{lib/api → api}/knowledge.d.ts +1 -1
- package/dist/src/{lib/api → api}/ratings.d.ts +1 -1
- package/dist/src/{lib/api → api}/talkStream.d.ts +1 -1
- package/dist/src/{lib/auth → auth}/getAuthHeader.d.ts +1 -1
- package/dist/src/{lib/connectToSocket.d.ts → connectToSocket.d.ts} +2 -2
- package/dist/src/{lib/createAgentManager.d.ts → createAgentManager.d.ts} +4 -4
- package/dist/src/{lib/createStreamingManager.d.ts → createStreamingManager.d.ts} +2 -8
- package/dist/src/{lib/environment.d.ts → environment.d.ts} +0 -1
- package/dist/src/index.d.ts +7 -7
- package/dist/src/types/auth.d.ts +0 -2
- package/dist/src/types/entities/agents/manager.d.ts +1 -1
- package/dist/src/{lib/utils → utils}/webrtc.d.ts +1 -1
- package/package.json +5 -6
package/dist/index.js
CHANGED
|
@@ -1,26 +1,30 @@
|
|
|
1
|
-
const
|
|
2
|
-
function
|
|
1
|
+
const C = "https://api.d-id.com", b = "wss://notifications.d-id.com";
|
|
2
|
+
function W() {
|
|
3
|
+
let e = window.localStorage.getItem("external_key_d_id");
|
|
4
|
+
return e || (e = Math.random().toString(16).slice(2), window.localStorage.setItem("external_key_d_id", e)), e;
|
|
5
|
+
}
|
|
6
|
+
function K(e) {
|
|
3
7
|
if (e.type === "bearer")
|
|
4
8
|
return `Bearer ${e.token}`;
|
|
5
9
|
if (e.type === "basic")
|
|
6
10
|
return `Basic ${btoa(`${e.username}:${e.password}`)}`;
|
|
7
11
|
if (e.type === "key")
|
|
8
|
-
return `Client-Key ${e.clientKey}.${
|
|
12
|
+
return `Client-Key ${e.clientKey}.${W()}`;
|
|
9
13
|
throw new Error(`Unknown auth type: ${e}`);
|
|
10
14
|
}
|
|
11
|
-
function
|
|
15
|
+
function _(e, i = C) {
|
|
12
16
|
const n = async (t, r) => {
|
|
13
|
-
const a = await fetch(
|
|
17
|
+
const a = await fetch(i + (t != null && t.startsWith("/") ? t : `/${t}`), {
|
|
14
18
|
...r,
|
|
15
19
|
headers: {
|
|
16
20
|
...r == null ? void 0 : r.headers,
|
|
17
|
-
Authorization:
|
|
21
|
+
Authorization: K(e),
|
|
18
22
|
"Content-Type": "application/json"
|
|
19
23
|
}
|
|
20
24
|
});
|
|
21
25
|
if (!a.ok) {
|
|
22
|
-
let
|
|
23
|
-
throw new Error(
|
|
26
|
+
let o = await a.text().catch(() => "Failed to fetch");
|
|
27
|
+
throw new Error(o);
|
|
24
28
|
}
|
|
25
29
|
return a.json();
|
|
26
30
|
};
|
|
@@ -54,8 +58,8 @@ function b(e, s = S) {
|
|
|
54
58
|
}
|
|
55
59
|
};
|
|
56
60
|
}
|
|
57
|
-
function
|
|
58
|
-
const n =
|
|
61
|
+
function E(e, i = C) {
|
|
62
|
+
const n = _(e, `${i}/agents`);
|
|
59
63
|
return {
|
|
60
64
|
create(t, r) {
|
|
61
65
|
return n.post("/", t, r);
|
|
@@ -75,13 +79,13 @@ function F(e, s = S) {
|
|
|
75
79
|
newChat(t, r) {
|
|
76
80
|
return n.post(`/${t}/chat`, void 0, r);
|
|
77
81
|
},
|
|
78
|
-
chat(t, r, a,
|
|
79
|
-
return n.post(`/${t}/chat/${r}`, a,
|
|
82
|
+
chat(t, r, a, o) {
|
|
83
|
+
return n.post(`/${t}/chat/${r}`, a, o);
|
|
80
84
|
}
|
|
81
85
|
};
|
|
82
86
|
}
|
|
83
|
-
function
|
|
84
|
-
const n =
|
|
87
|
+
function O(e, i = C) {
|
|
88
|
+
const n = _(e, `${i}/knowledge`);
|
|
85
89
|
return {
|
|
86
90
|
createKnowledge(t, r) {
|
|
87
91
|
return n.post("/", t, r);
|
|
@@ -117,8 +121,8 @@ function X(e, s = S) {
|
|
|
117
121
|
}
|
|
118
122
|
};
|
|
119
123
|
}
|
|
120
|
-
function
|
|
121
|
-
const n =
|
|
124
|
+
function B(e, i = C) {
|
|
125
|
+
const n = _(e, `${i}/chats/ratings`);
|
|
122
126
|
return {
|
|
123
127
|
create(t, r) {
|
|
124
128
|
return n.post("/", t, r);
|
|
@@ -134,49 +138,49 @@ function Y(e, s = S) {
|
|
|
134
138
|
}
|
|
135
139
|
};
|
|
136
140
|
}
|
|
137
|
-
const
|
|
138
|
-
function
|
|
139
|
-
return new Promise((
|
|
141
|
+
const H = (e) => new Promise((i) => setTimeout(i, e));
|
|
142
|
+
function U(e) {
|
|
143
|
+
return new Promise((i, n) => {
|
|
140
144
|
const {
|
|
141
145
|
callbacks: t,
|
|
142
146
|
host: r,
|
|
143
147
|
auth: a
|
|
144
148
|
} = e, {
|
|
145
|
-
onMessage:
|
|
149
|
+
onMessage: o = null,
|
|
146
150
|
onOpen: u = null,
|
|
147
|
-
onClose:
|
|
148
|
-
onError:
|
|
149
|
-
} = t || {},
|
|
150
|
-
|
|
151
|
-
console.
|
|
152
|
-
},
|
|
153
|
-
u == null || u(
|
|
151
|
+
onClose: c = null,
|
|
152
|
+
onError: m = null
|
|
153
|
+
} = t || {}, d = new WebSocket(`${r}?authorization=${K(a)}`);
|
|
154
|
+
d.onmessage = o, d.onclose = c, d.onerror = (g) => {
|
|
155
|
+
console.error(g), m == null || m(g), n(g);
|
|
156
|
+
}, d.onopen = (g) => {
|
|
157
|
+
u == null || u(g), i(d);
|
|
154
158
|
};
|
|
155
159
|
});
|
|
156
160
|
}
|
|
157
|
-
async function
|
|
161
|
+
async function F(e) {
|
|
158
162
|
const {
|
|
159
|
-
retries:
|
|
163
|
+
retries: i = 1
|
|
160
164
|
} = e;
|
|
161
165
|
let n = null;
|
|
162
166
|
for (let t = 0; (n == null ? void 0 : n.readyState) !== WebSocket.OPEN; t++)
|
|
163
167
|
try {
|
|
164
|
-
n = await
|
|
168
|
+
n = await U(e);
|
|
165
169
|
} catch (r) {
|
|
166
|
-
if (t ===
|
|
170
|
+
if (t === i)
|
|
167
171
|
throw r;
|
|
168
|
-
await
|
|
172
|
+
await H(t * 500);
|
|
169
173
|
}
|
|
170
174
|
return n;
|
|
171
175
|
}
|
|
172
|
-
async function
|
|
173
|
-
const t = n ? [n] : [], r = await
|
|
176
|
+
async function T(e, i, n) {
|
|
177
|
+
const t = n ? [n] : [], r = await F({
|
|
174
178
|
auth: e,
|
|
175
|
-
host:
|
|
179
|
+
host: i,
|
|
176
180
|
callbacks: {
|
|
177
181
|
onMessage: (a) => {
|
|
178
|
-
const
|
|
179
|
-
t.forEach((u) => u(
|
|
182
|
+
const o = JSON.parse(a.data);
|
|
183
|
+
t.forEach((u) => u(o.event, o));
|
|
180
184
|
}
|
|
181
185
|
}
|
|
182
186
|
});
|
|
@@ -186,117 +190,117 @@ async function ee(e, s, n) {
|
|
|
186
190
|
subscribeToEvents: (a) => t.push(a)
|
|
187
191
|
};
|
|
188
192
|
}
|
|
189
|
-
var
|
|
190
|
-
function
|
|
191
|
-
return e.presenter.type ===
|
|
192
|
-
videoType:
|
|
193
|
+
var q = /* @__PURE__ */ ((e) => (e.Amazon = "amazon", e.Microsoft = "microsoft", e.Afflorithmics = "afflorithmics", e.Elevenlabs = "elevenlabs", e))(q || {}), N = /* @__PURE__ */ ((e) => (e.Public = "public", e.Premium = "premium", e.Private = "private", e))(N || {}), $ = /* @__PURE__ */ ((e) => (e.Start = "START", e.Stop = "STOP", e))($ || {}), y = /* @__PURE__ */ ((e) => (e.ChatAnswer = "chat/answer", e.ChatPartial = "chat/partial", e.StreamDone = "stream/done", e.StreamStarted = "stream/started", e.StreamFailed = "stream/error", e))(y || {}), j = /* @__PURE__ */ ((e) => (e.Unrated = "Unrated", e.Positive = "Positive", e.Negative = "Negative", e))(j || {}), z = /* @__PURE__ */ ((e) => (e.Functional = "Functional", e.TextOnly = "TextOnly", e.Maintenance = "Maintenance", e))(z || {}), M = /* @__PURE__ */ ((e) => (e.Embed = "embed", e.Query = "query", e.Partial = "partial", e.Answer = "answer", e.Complete = "done", e))(M || {}), J = /* @__PURE__ */ ((e) => (e.KnowledgeProcessing = "knowledge/processing", e.KnowledgeIndexing = "knowledge/indexing", e.KnowledgeFailed = "knowledge/error", e.KnowledgeDone = "knowledge/done", e))(J || {}), D = /* @__PURE__ */ ((e) => (e.Knowledge = "knowledge", e.Document = "document", e.Record = "record", e))(D || {}), G = /* @__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))(G || {}), R = /* @__PURE__ */ ((e) => (e.Clip = "clip", e.Talk = "talk", e))(R || {});
|
|
194
|
+
function Q(e) {
|
|
195
|
+
return e.presenter.type === R.Clip ? {
|
|
196
|
+
videoType: R.Clip,
|
|
193
197
|
driver_id: e.presenter.driver_id,
|
|
194
198
|
presenter_id: e.presenter.presenter_id
|
|
195
199
|
} : {
|
|
196
|
-
videoType:
|
|
200
|
+
videoType: R.Talk,
|
|
197
201
|
source_url: e.presenter.source_url
|
|
198
202
|
};
|
|
199
203
|
}
|
|
200
|
-
function
|
|
204
|
+
function P(e, i, n, t) {
|
|
201
205
|
return new Promise(async (r, a) => {
|
|
202
|
-
const
|
|
203
|
-
...
|
|
206
|
+
const o = await te(Q(e), {
|
|
207
|
+
...i,
|
|
204
208
|
callbacks: {
|
|
205
|
-
...
|
|
209
|
+
...i.callbacks,
|
|
206
210
|
onConnectionStateChange: async (u) => {
|
|
207
|
-
var
|
|
211
|
+
var c, m;
|
|
208
212
|
if (u === "connected")
|
|
209
213
|
try {
|
|
210
214
|
t || (t = await n.newChat(e.id)), r({
|
|
211
215
|
chat: t,
|
|
212
|
-
streamingManager:
|
|
216
|
+
streamingManager: o
|
|
213
217
|
});
|
|
214
|
-
} catch (
|
|
215
|
-
console.error(
|
|
218
|
+
} catch (d) {
|
|
219
|
+
console.error(d), a(new Error("Cannot create new chat"));
|
|
216
220
|
}
|
|
217
221
|
else
|
|
218
222
|
u === "failed" && a(new Error("Cannot create connection"));
|
|
219
|
-
(
|
|
223
|
+
(m = (c = i.callbacks).onConnectionStateChange) == null || m.call(c, u);
|
|
220
224
|
},
|
|
221
225
|
// TODO remove when webscoket will return partial
|
|
222
|
-
onMessage: (u,
|
|
223
|
-
var
|
|
224
|
-
u ===
|
|
225
|
-
content:
|
|
226
|
-
event:
|
|
226
|
+
onMessage: (u, c) => {
|
|
227
|
+
var m, d;
|
|
228
|
+
u === y.ChatAnswer && (console.log("ChatAnswer", u, c), (d = (m = i.callbacks).onChatEvents) == null || d.call(m, M.Answer, {
|
|
229
|
+
content: c,
|
|
230
|
+
event: M.Answer
|
|
227
231
|
}));
|
|
228
232
|
}
|
|
229
233
|
}
|
|
230
234
|
});
|
|
231
235
|
});
|
|
232
236
|
}
|
|
233
|
-
function
|
|
234
|
-
return
|
|
237
|
+
function ne(e, i, n) {
|
|
238
|
+
return E(i, n || C).getById(e);
|
|
235
239
|
}
|
|
236
|
-
async function
|
|
237
|
-
var
|
|
238
|
-
const n =
|
|
239
|
-
(
|
|
240
|
-
const
|
|
240
|
+
async function re(e, i) {
|
|
241
|
+
var w, p;
|
|
242
|
+
const n = i.baseURL || C, t = i.wsURL || b, r = new AbortController(), a = E(i.auth, n), o = B(i.auth, n), u = O(i.auth, n), c = typeof e == "string" ? await a.getById(e) : e;
|
|
243
|
+
(p = (w = i.callbacks) == null ? void 0 : w.onAgentReady) == null || p.call(w, c);
|
|
244
|
+
const m = await T(i.auth, t, i.callbacks.onChatEvents);
|
|
241
245
|
let {
|
|
242
|
-
chat:
|
|
243
|
-
streamingManager:
|
|
244
|
-
} = await
|
|
246
|
+
chat: d,
|
|
247
|
+
streamingManager: g
|
|
248
|
+
} = await P(c, i, a);
|
|
245
249
|
return {
|
|
246
|
-
agent:
|
|
247
|
-
chatId:
|
|
250
|
+
agent: c,
|
|
251
|
+
chatId: d.id,
|
|
248
252
|
async reconnectToChat() {
|
|
249
253
|
const {
|
|
250
|
-
streamingManager:
|
|
251
|
-
} = await
|
|
252
|
-
|
|
254
|
+
streamingManager: s
|
|
255
|
+
} = await P(c, i, a, d);
|
|
256
|
+
g = s;
|
|
253
257
|
},
|
|
254
258
|
terminate() {
|
|
255
|
-
return r.abort(),
|
|
259
|
+
return r.abort(), m.terminate(), g.terminate();
|
|
256
260
|
},
|
|
257
|
-
chat(
|
|
258
|
-
return a.chat(
|
|
259
|
-
sessionId:
|
|
260
|
-
streamId:
|
|
261
|
-
messages:
|
|
261
|
+
chat(s) {
|
|
262
|
+
return a.chat(c.id, d.id, {
|
|
263
|
+
sessionId: g.sessionId,
|
|
264
|
+
streamId: g.streamId,
|
|
265
|
+
messages: s
|
|
262
266
|
}, {
|
|
263
267
|
signal: r.signal
|
|
264
268
|
});
|
|
265
269
|
},
|
|
266
|
-
rate(
|
|
267
|
-
return
|
|
270
|
+
rate(s, f) {
|
|
271
|
+
return f ? o.update(f, s) : o.create(s);
|
|
268
272
|
},
|
|
269
|
-
deleteRate(
|
|
270
|
-
return
|
|
273
|
+
deleteRate(s) {
|
|
274
|
+
return o.delete(s);
|
|
271
275
|
},
|
|
272
|
-
speak(
|
|
273
|
-
function
|
|
274
|
-
if (
|
|
276
|
+
speak(s) {
|
|
277
|
+
function f() {
|
|
278
|
+
if (s.type === "text")
|
|
275
279
|
return {
|
|
276
280
|
type: "text",
|
|
277
|
-
provider:
|
|
278
|
-
input:
|
|
279
|
-
ssml:
|
|
281
|
+
provider: s.provider,
|
|
282
|
+
input: s.input,
|
|
283
|
+
ssml: s.ssml || !1
|
|
280
284
|
};
|
|
281
|
-
if (
|
|
285
|
+
if (s.type === "audio")
|
|
282
286
|
return {
|
|
283
287
|
type: "audio",
|
|
284
|
-
audio_url:
|
|
288
|
+
audio_url: s.audio_url
|
|
285
289
|
};
|
|
286
290
|
throw new Error("Invalid payload");
|
|
287
291
|
}
|
|
288
|
-
return
|
|
289
|
-
script:
|
|
292
|
+
return g.speak({
|
|
293
|
+
script: f()
|
|
290
294
|
});
|
|
291
295
|
},
|
|
292
296
|
async getStarterMessages() {
|
|
293
|
-
var
|
|
294
|
-
return (
|
|
297
|
+
var s;
|
|
298
|
+
return (s = c.knowledge) != null && s.id ? u.getKnowledge(c.knowledge.id).then((f) => (f == null ? void 0 : f.starter_message) || []) : [];
|
|
295
299
|
}
|
|
296
300
|
};
|
|
297
301
|
}
|
|
298
|
-
function
|
|
299
|
-
const n =
|
|
302
|
+
function X(e, i) {
|
|
303
|
+
const n = _(e, i);
|
|
300
304
|
return {
|
|
301
305
|
createStream(t) {
|
|
302
306
|
return n.post("/clips/streams", {
|
|
@@ -330,8 +334,8 @@ function de(e, s) {
|
|
|
330
334
|
}
|
|
331
335
|
};
|
|
332
336
|
}
|
|
333
|
-
function
|
|
334
|
-
const n =
|
|
337
|
+
function Y(e, i) {
|
|
338
|
+
const n = _(e, i);
|
|
335
339
|
return {
|
|
336
340
|
createStream(t, r) {
|
|
337
341
|
return n.post("/talks/streams", {
|
|
@@ -341,23 +345,23 @@ function le(e, s) {
|
|
|
341
345
|
config: t.config
|
|
342
346
|
}, r);
|
|
343
347
|
},
|
|
344
|
-
startConnection(t, r, a,
|
|
348
|
+
startConnection(t, r, a, o) {
|
|
345
349
|
return n.post(`/talks/streams/${t}/sdp`, {
|
|
346
350
|
session_id: a,
|
|
347
351
|
answer: r
|
|
348
|
-
},
|
|
352
|
+
}, o);
|
|
349
353
|
},
|
|
350
|
-
addIceCandidate(t, r, a,
|
|
354
|
+
addIceCandidate(t, r, a, o) {
|
|
351
355
|
return n.post(`/talks/streams/${t}/ice`, {
|
|
352
356
|
session_id: a,
|
|
353
357
|
...r
|
|
354
|
-
},
|
|
358
|
+
}, o);
|
|
355
359
|
},
|
|
356
|
-
sendStreamRequest(t, r, a,
|
|
360
|
+
sendStreamRequest(t, r, a, o) {
|
|
357
361
|
return n.post(`/talks/streams/${t}`, {
|
|
358
362
|
session_id: r,
|
|
359
363
|
...a
|
|
360
|
-
},
|
|
364
|
+
}, o);
|
|
361
365
|
},
|
|
362
366
|
close(t, r, a) {
|
|
363
367
|
return n.delete(`/talks/streams/${t}`, {
|
|
@@ -366,13 +370,13 @@ function le(e, s) {
|
|
|
366
370
|
}
|
|
367
371
|
};
|
|
368
372
|
}
|
|
369
|
-
function
|
|
370
|
-
return e.map((n, t) => t === 0 ?
|
|
373
|
+
function Z(e, i) {
|
|
374
|
+
return e.map((n, t) => t === 0 ? i ? {
|
|
371
375
|
index: t,
|
|
372
376
|
timestamp: n.timestamp,
|
|
373
|
-
bytesReceived: n.bytesReceived -
|
|
374
|
-
packetsReceived: n.packetsReceived -
|
|
375
|
-
packetsLost: n.packetsLost -
|
|
377
|
+
bytesReceived: n.bytesReceived - i.bytesReceived,
|
|
378
|
+
packetsReceived: n.packetsReceived - i.packetsReceived,
|
|
379
|
+
packetsLost: n.packetsLost - i.packetsLost,
|
|
376
380
|
jitter: n.jitter,
|
|
377
381
|
frameWidth: n.frameWidth,
|
|
378
382
|
frameHeight: n.frameHeight,
|
|
@@ -399,137 +403,124 @@ function ue(e, s) {
|
|
|
399
403
|
frameRate: n.frameRate
|
|
400
404
|
});
|
|
401
405
|
}
|
|
402
|
-
let
|
|
403
|
-
const
|
|
404
|
-
|
|
405
|
-
|
|
406
|
+
let x = !1;
|
|
407
|
+
const v = (e, i) => x && console.log(e, i), V = (window.RTCPeerConnection || window.webkitRTCPeerConnection || window.mozRTCPeerConnection).bind(window);
|
|
408
|
+
function ee(e, i) {
|
|
409
|
+
let n = [], t = 0, r = 0, a;
|
|
410
|
+
return setInterval(() => {
|
|
411
|
+
e.getStats().then((u) => {
|
|
412
|
+
u.forEach((c) => {
|
|
413
|
+
if (c.type === "inbound-rtp" && c.kind === "video") {
|
|
414
|
+
if (r = n.length - 1, c && n[r]) {
|
|
415
|
+
const m = c.bytesReceived, d = n[r].bytesReceived;
|
|
416
|
+
let g = a;
|
|
417
|
+
a = m - d > 0;
|
|
418
|
+
let w;
|
|
419
|
+
if (g !== a) {
|
|
420
|
+
if (a)
|
|
421
|
+
t = n.length;
|
|
422
|
+
else {
|
|
423
|
+
const p = n.slice(t), s = t === 0 ? void 0 : n[t - 1];
|
|
424
|
+
w = Z(p, s), w = w.sort((f, A) => A.packetsLost - f.packetsLost).slice(0, 5);
|
|
425
|
+
}
|
|
426
|
+
i == null || i(a ? $.Start : $.Stop, w);
|
|
427
|
+
}
|
|
428
|
+
}
|
|
429
|
+
n.push(c);
|
|
430
|
+
}
|
|
431
|
+
});
|
|
432
|
+
});
|
|
433
|
+
}, 500);
|
|
434
|
+
}
|
|
435
|
+
async function te(e, {
|
|
436
|
+
debug: i = !1,
|
|
406
437
|
callbacks: n,
|
|
407
438
|
auth: t,
|
|
408
|
-
baseURL: r =
|
|
439
|
+
baseURL: r = C
|
|
409
440
|
}) {
|
|
410
|
-
|
|
411
|
-
const
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
close: g,
|
|
417
|
-
createStream: f,
|
|
441
|
+
x = i;
|
|
442
|
+
const {
|
|
443
|
+
startConnection: a,
|
|
444
|
+
sendStreamRequest: o,
|
|
445
|
+
close: u,
|
|
446
|
+
createStream: c,
|
|
418
447
|
addIceCandidate: m
|
|
419
|
-
} = e.videoType ===
|
|
420
|
-
id:
|
|
421
|
-
offer:
|
|
422
|
-
ice_servers:
|
|
423
|
-
session_id:
|
|
424
|
-
} = await
|
|
425
|
-
iceServers:
|
|
426
|
-
}),
|
|
427
|
-
|
|
428
|
-
if (!i)
|
|
448
|
+
} = e.videoType === R.Clip ? X(t, r) : Y(t, r), {
|
|
449
|
+
id: d,
|
|
450
|
+
offer: g,
|
|
451
|
+
ice_servers: w,
|
|
452
|
+
session_id: p
|
|
453
|
+
} = await c(e), s = new V({
|
|
454
|
+
iceServers: w
|
|
455
|
+
}), f = s.createDataChannel("JanusDataChannel");
|
|
456
|
+
if (!p)
|
|
429
457
|
throw new Error("Could not create session_id");
|
|
430
|
-
(
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
v.push(p);
|
|
452
|
-
}
|
|
453
|
-
});
|
|
454
|
-
});
|
|
455
|
-
}, 500);
|
|
456
|
-
})();
|
|
457
|
-
const q = () => {
|
|
458
|
-
clearInterval(x);
|
|
459
|
-
};
|
|
460
|
-
o.onicecandidate = (c) => {
|
|
461
|
-
C("peerConnection.onicecandidate", c), c.candidate && c.candidate.sdpMid && c.candidate.sdpMLineIndex !== null && m(l, {
|
|
462
|
-
candidate: c.candidate.candidate,
|
|
463
|
-
sdpMid: c.candidate.sdpMid,
|
|
464
|
-
sdpMLineIndex: c.candidate.sdpMLineIndex
|
|
465
|
-
}, i);
|
|
466
|
-
}, o.oniceconnectionstatechange = () => {
|
|
467
|
-
var c;
|
|
468
|
-
C("peerConnection.oniceconnectionstatechange => " + o.iceConnectionState), (c = a.onConnectionStateChange) == null || c.call(a, o.iceConnectionState);
|
|
469
|
-
}, o.ontrack = (c) => {
|
|
470
|
-
var w;
|
|
471
|
-
C("peerConnection.ontrack", c), (w = a.onSrcObjectReady) == null || w.call(a, c.streams[0]);
|
|
472
|
-
}, E.onmessage = (c) => {
|
|
473
|
-
var w, p;
|
|
474
|
-
if (E.readyState === "open") {
|
|
475
|
-
const [h, $] = c.data.split(":");
|
|
476
|
-
h === _.StreamStarted ? console.log("StreamStarted", h, $) : h === _.StreamDone ? console.log("StreamDone") : h === _.StreamFailed ? ((w = a.onVideoStateChange) == null || w.call(a, A.Stop, {
|
|
477
|
-
event: h,
|
|
478
|
-
data: $
|
|
479
|
-
}), v = [], R = 0, P = 0, y = !1, console.log("StreamFailed")) : (p = a.onMessage) == null || p.call(a, h, decodeURIComponent($));
|
|
458
|
+
const A = ee(s, n.onVideoStateChange);
|
|
459
|
+
s.onicecandidate = (l) => {
|
|
460
|
+
v("peerConnection.onicecandidate", l), l.candidate && l.candidate.sdpMid && l.candidate.sdpMLineIndex !== null && m(d, {
|
|
461
|
+
candidate: l.candidate.candidate,
|
|
462
|
+
sdpMid: l.candidate.sdpMid,
|
|
463
|
+
sdpMLineIndex: l.candidate.sdpMLineIndex
|
|
464
|
+
}, p);
|
|
465
|
+
}, s.oniceconnectionstatechange = () => {
|
|
466
|
+
var l;
|
|
467
|
+
v("peerConnection.oniceconnectionstatechange => " + s.iceConnectionState), (l = n.onConnectionStateChange) == null || l.call(n, s.iceConnectionState);
|
|
468
|
+
}, s.ontrack = (l) => {
|
|
469
|
+
var h;
|
|
470
|
+
v("peerConnection.ontrack", l), (h = n.onSrcObjectReady) == null || h.call(n, l.streams[0]);
|
|
471
|
+
}, f.onmessage = (l) => {
|
|
472
|
+
var h, k;
|
|
473
|
+
if (f.readyState === "open") {
|
|
474
|
+
const [S, I] = l.data.split(":");
|
|
475
|
+
S === y.StreamStarted ? console.log("StreamStarted", S, I) : S === y.StreamDone ? console.log("StreamDone") : S === y.StreamFailed ? ((h = n.onVideoStateChange) == null || h.call(n, $.Stop, {
|
|
476
|
+
event: S,
|
|
477
|
+
data: I
|
|
478
|
+
}), clearInterval(A), console.log("StreamFailed")) : (k = n.onMessage) == null || k.call(n, S, decodeURIComponent(I));
|
|
480
479
|
}
|
|
481
|
-
}, await
|
|
482
|
-
const
|
|
483
|
-
return
|
|
480
|
+
}, await s.setRemoteDescription(g), v("set remote description OK");
|
|
481
|
+
const L = await s.createAnswer();
|
|
482
|
+
return v("create answer OK"), await s.setLocalDescription(L), v("set local description OK"), await a(d, L, p), v("start connection OK"), {
|
|
484
483
|
/**
|
|
485
484
|
* Method to send request to server to get clip or talk depend on you payload
|
|
486
|
-
* @param payload
|
|
485
|
+
* @param payload
|
|
487
486
|
*/
|
|
488
|
-
speak(
|
|
489
|
-
return
|
|
487
|
+
speak(l) {
|
|
488
|
+
return o(d, p, l);
|
|
490
489
|
},
|
|
491
490
|
/**
|
|
492
491
|
* Method to close RTC connection
|
|
493
492
|
*/
|
|
494
493
|
async terminate() {
|
|
495
|
-
var
|
|
496
|
-
|
|
497
|
-
}), (
|
|
494
|
+
var l, h;
|
|
495
|
+
d && (s && (s.close(), s.oniceconnectionstatechange = null, s.onnegotiationneeded = null, s.onicecandidate = null, s.ontrack = null), await u(d, p).catch((k) => {
|
|
496
|
+
}), (l = n.onConnectionStateChange) == null || l.call(n, "closed"), (h = n.onVideoStateChange) == null || h.call(n, $.Stop), clearInterval(A));
|
|
498
497
|
},
|
|
499
498
|
/**
|
|
500
499
|
* Session identifier information, should be returned in the body of all streaming requests
|
|
501
500
|
*/
|
|
502
|
-
sessionId:
|
|
501
|
+
sessionId: p,
|
|
503
502
|
/**
|
|
504
503
|
* Id of current RTC stream
|
|
505
504
|
*/
|
|
506
|
-
streamId:
|
|
507
|
-
/**
|
|
508
|
-
* Method to add callback that will be trigered on supported events
|
|
509
|
-
* @param eventName
|
|
510
|
-
* @param callback
|
|
511
|
-
*/
|
|
512
|
-
onCallback(c, w) {
|
|
513
|
-
a[c] = w;
|
|
514
|
-
}
|
|
505
|
+
streamId: d
|
|
515
506
|
};
|
|
516
507
|
}
|
|
517
508
|
export {
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
509
|
+
z as ChatMode,
|
|
510
|
+
M as ChatProgress,
|
|
511
|
+
G as DocumentType,
|
|
512
|
+
D as KnowledgeType,
|
|
513
|
+
q as Providers,
|
|
514
|
+
j as RateState,
|
|
515
|
+
T as SocketManager,
|
|
516
|
+
y as StreamEvents,
|
|
517
|
+
J as Subject,
|
|
518
|
+
N as VoiceAccess,
|
|
519
|
+
re as createAgentManager,
|
|
520
|
+
E as createAgentsApi,
|
|
521
|
+
_ as createClient,
|
|
522
|
+
O as createKnowledgeApi,
|
|
523
|
+
B as createRatingsApi,
|
|
524
|
+
te as createStreamingManager,
|
|
525
|
+
ne as getAgent
|
|
535
526
|
};
|
package/dist/index.umd.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
(function(d,p){typeof exports=="object"&&typeof module<"u"?p(exports):typeof define=="function"&&define.amd?define(["exports"],p):(d=typeof globalThis<"u"?globalThis:d||self,p(d.index={}))})(this,function(d){"use strict";const p="https://api.d-id.com",j="wss://notifications.d-id.com";function W(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}.${e.externalId}`;throw new Error(`Unknown auth type: ${e}`)}function k(e,i=p){const n=async(t,a)=>{const r=await fetch(i+(t!=null&&t.startsWith("/")?t:`/${t}`),{...a,headers:{...a==null?void 0:a.headers,Authorization:W(e),"Content-Type":"application/json"}});if(!r.ok){let l=await r.text().catch(()=>"Failed to fetch");throw new Error(l)}return r.json()};return{get(t,a){return n(t,{...a,method:"GET"})},post(t,a,r){return n(t,{...r,body:JSON.stringify(a),method:"POST"})},delete(t,a,r){return n(t,{...r,body:JSON.stringify(a),method:"DELETE"})},patch(t,a,r){return n(t,{...r,body:JSON.stringify(a),method:"PATCH"})}}}function T(e,i=p){const n=k(e,`${i}/agents`);return{create(t,a){return n.post("/",t,a)},getAgents(t,a){return n.get(`/${t?`?tag=${t}`:""}`,a).then(r=>r??[])},getById(t,a){return n.get(`/${t}`,a)},delete(t,a){return n.delete(`/${t}`,void 0,a)},update(t,a,r){return n.patch(`/${t}`,a,r)},newChat(t,a){return n.post(`/${t}/chat`,void 0,a)},chat(t,a,r,l){return n.post(`/${t}/chat/${a}`,r,l)}}}function B(e,i=p){const n=k(e,`${i}/knowledge`);return{createKnowledge(t,a){return n.post("/",t,a)},getKnowledgeBase(t){return n.get("/",t)},getKnowledge(t,a){return n.get(`/${t}`,a)},deleteKnowledge(t,a){return n.delete(`/${t}`,void 0,a)},createDocument(t,a,r){return n.post(`/${t}/documents`,a,r)},deleteDocument(t,a,r){return n.delete(`/${t}/documents/${a}`,void 0,r)},getDocuments(t,a){return n.get(`/${t}/documents`,a)},getDocument(t,a,r){return n.get(`/${t}/documents/${a}`,r)},getRecords(t,a,r){return n.get(`/${t}/documents/${a}/records`,r)},query(t,a,r){return n.post(`/${t}/query`,{query:a},r)}}}function H(e,i=p){const n=k(e,`${i}/chats/ratings`);return{create(t,a){return n.post("/",t,a)},getByKnowledge(t,a){return n.get(`/${t}`,a).then(r=>r??[])},update(t,a,r){return n.patch(`/${t}`,a,r)},delete(t,a){return n.delete(`/${t}`,a)}}}const ee=e=>new Promise(i=>setTimeout(i,e));function te(e){return new Promise((i,n)=>{const{callbacks:t,host:a,auth:r}=e,{onMessage:l=null,onOpen:g=null,onClose:m=null,onError:w=null}=t||{},f=new WebSocket(`${a}?authorization=${W(r)}`);f.onmessage=l,f.onclose=m,f.onerror=u=>{console.log(u),w==null||w(u),n(u)},f.onopen=u=>{g==null||g(u),i(f)}})}async function ne(e){const{retries:i=1}=e;let n=null;for(let t=0;(n==null?void 0:n.readyState)!==WebSocket.OPEN;t++)try{n=await te(e)}catch(a){if(t===i)throw a;await ee(t*500)}return n}async function U(e,i,n){const t=n?[n]:[],a=await ne({auth:e,host:i,callbacks:{onMessage:r=>{const l=JSON.parse(r.data);t.forEach(g=>g(l.event,l))}}});return{socket:a,terminate:()=>a.close(),subscribeToEvents:r=>t.push(r)}}var F=(e=>(e.Amazon="amazon",e.Microsoft="microsoft",e.Afflorithmics="afflorithmics",e.Elevenlabs="elevenlabs",e))(F||{}),q=(e=>(e.Public="public",e.Premium="premium",e.Private="private",e))(q||{}),_=(e=>(e.Start="START",e.Stop="STOP",e))(_||{}),R=(e=>(e.ChatAnswer="chat/answer",e.ChatPartial="chat/partial",e.StreamDone="stream/done",e.StreamStarted="stream/started",e.StreamFailed="stream/error",e))(R||{}),N=(e=>(e.Unrated="Unrated",e.Positive="Positive",e.Negative="Negative",e))(N||{}),z=(e=>(e.Functional="Functional",e.TextOnly="TextOnly",e.Maintenance="Maintenance",e))(z||{}),P=(e=>(e.Embed="embed",e.Query="query",e.Partial="partial",e.Answer="answer",e.Complete="done",e))(P||{}),J=(e=>(e.KnowledgeProcessing="knowledge/processing",e.KnowledgeIndexing="knowledge/indexing",e.KnowledgeFailed="knowledge/error",e.KnowledgeDone="knowledge/done",e))(J||{}),D=(e=>(e.Knowledge="knowledge",e.Document="document",e.Record="record",e))(D||{}),V=(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))(V||{}),I=(e=>(e.Clip="clip",e.Talk="talk",e))(I||{});function ae(e){return e.presenter.type===I.Clip?{videoType:I.Clip,driver_id:e.presenter.driver_id,presenter_id:e.presenter.presenter_id}:{videoType:I.Talk,source_url:e.presenter.source_url}}function G(e,i,n,t){return new Promise(async(a,r)=>{const l=await X(ae(e),{...i,callbacks:{...i.callbacks,onConnectionStateChange:async g=>{var m,w;if(g==="connected")try{t||(t=await n.newChat(e.id)),a({chat:t,streamingManager:l})}catch(f){console.error(f),r(new Error("Cannot create new chat"))}else g==="failed"&&r(new Error("Cannot create connection"));(w=(m=i.callbacks).onConnectionStateChange)==null||w.call(m,g)},onMessage:(g,m)=>{var w,f;g===R.ChatAnswer&&(console.log("ChatAnswer",g,m),(f=(w=i.callbacks).onChatEvents)==null||f.call(w,P.Answer,{content:m,event:P.Answer}))}}})})}function re(e,i,n){return T(i,n||p).getById(e)}async function ie(e,i){var $,L;const n=i.baseURL||p,t=i.wsURL||j,a=new AbortController,r=T(i.auth,n),l=H(i.auth,n),g=B(i.auth,n),m=await r.getById(e);(L=($=i.callbacks)==null?void 0:$.onAgentReady)==null||L.call($,m);const w=await U(i.auth,t,i.callbacks.onChatEvents);let{chat:f,streamingManager:u}=await G(m,i,r);return{agent:m,chatId:f.id,async reconnectToChat(){const{streamingManager:s}=await G(m,i,r,f);u=s},terminate(){return a.abort(),w.terminate(),u.terminate()},chat(s){return r.chat(e,f.id,{sessionId:u.sessionId,streamId:u.streamId,messages:s},{signal:a.signal})},rate(s,c){return c?l.update(c,s):l.create(s)},deleteRate(s){return l.delete(s)},speak(s){function c(){if(s.type==="text")return{type:"text",provider:s.provider,input:s.input,ssml:s.ssml||!1};if(s.type==="audio")return{type:"audio",audio_url:s.audio_url};throw new Error("Invalid payload")}return u.speak({script:c()})},async getStarterMessages(){var s;return(s=m.knowledge)!=null&&s.id?g.getKnowledge(m.knowledge.id).then(c=>(c==null?void 0:c.starter_message)||[]):[]}}}function se(e,i){const n=k(e,i);return{createStream(t){return n.post("/clips/streams",{driver_id:t.driver_id,presenter_id:t.presenter_id,compatibility_mode:t.compatibility_mode})},startConnection(t,a,r){return n.post(`/clips/streams/${t}/sdp`,{session_id:r,answer:a})},addIceCandidate(t,a,r){return n.post(`/clips/streams/${t}/ice`,{session_id:r,...a})},sendStreamRequest(t,a,r){return n.post(`/clips/streams/${t}`,{session_id:a,...r})},close(t,a){return n.delete(`/clips/streams/${t}`,{session_id:a})}}}function ce(e,i){const n=k(e,i);return{createStream(t,a){return n.post("/talks/streams",{source_url:t.source_url,driver_url:t.driver_url,face:t.face,config:t.config},a)},startConnection(t,a,r,l){return n.post(`/talks/streams/${t}/sdp`,{session_id:r,answer:a},l)},addIceCandidate(t,a,r,l){return n.post(`/talks/streams/${t}/ice`,{session_id:r,...a},l)},sendStreamRequest(t,a,r,l){return n.post(`/talks/streams/${t}`,{session_id:a,...r},l)},close(t,a,r){return n.delete(`/talks/streams/${t}`,{session_id:a},r)}}}function oe(e,i){return e.map((n,t)=>t===0?i?{index:t,timestamp:n.timestamp,bytesReceived:n.bytesReceived-i.bytesReceived,packetsReceived:n.packetsReceived-i.packetsReceived,packetsLost:n.packetsLost-i.packetsLost,jitter:n.jitter,frameWidth:n.frameWidth,frameHeight:n.frameHeight,frameRate:n.frameRate}:{index:t,timestamp:n.timestamp,bytesReceived:n.bytesReceived,packetsReceived:n.packetsReceived,packetsLost:n.packetsLost,jitter:n.jitter,frameWidth:n.frameWidth,frameHeight:n.frameHeight,frameRate:n.frameRate}:{index:t,timestamp:n.timestamp,bytesReceived:n.bytesReceived-e[t-1].bytesReceived,packetsReceived:n.packetsReceived-e[t-1].packetsReceived,packetsLost:n.packetsLost-e[t-1].packetsLost,jitter:n.jitter,frameWidth:n.frameWidth,frameHeight:n.frameHeight,frameRate:n.frameRate})}let Q=!1;const y=(e,i)=>Q&&console.log(e,i),de=(window.RTCPeerConnection||window.webkitRTCPeerConnection||window.mozRTCPeerConnection).bind(window);async function X(e,{debug:i=!1,callbacks:n,auth:t,baseURL:a=p}){Q=i;const r={...n},{startConnection:l,sendStreamRequest:g,close:m,createStream:w,addIceCandidate:f}=e.videoType===I.Clip?se(t,a):ce(t,a),{id:u,offer:$,ice_servers:L,session_id:s}=await w(e),c=new de({iceServers:L}),Y=c.createDataChannel("JanusDataChannel");let S=[],b=0,K=0,A,Z,x;if(!s)throw new Error("Could not create session_id");(()=>{x=setInterval(()=>{c.getStats().then(h=>{h.forEach(v=>{var C;if(v.type==="inbound-rtp"&&v.kind==="video"){if(K=S.length-1,v&&S[K]){const M=v.bytesReceived,ue=S[K].bytesReceived;let ge=A;A=M-ue>0;let E;if(ge!==A){if(A)b=S.length;else{const me=S.slice(b),fe=b===0?void 0:S[b-1];E=oe(me,fe),E=E.sort((we,he)=>he.packetsLost-we.packetsLost).slice(0,5)}(C=r.onVideoStateChange)==null||C.call(r,A?_.Start:_.Stop,E)}}S.push(v)}})})},500)})();const le=()=>{clearInterval(Z)};c.onicecandidate=o=>{y("peerConnection.onicecandidate",o),o.candidate&&o.candidate.sdpMid&&o.candidate.sdpMLineIndex!==null&&f(u,{candidate:o.candidate.candidate,sdpMid:o.candidate.sdpMid,sdpMLineIndex:o.candidate.sdpMLineIndex},s)},c.oniceconnectionstatechange=()=>{var o;y("peerConnection.oniceconnectionstatechange => "+c.iceConnectionState),(o=r.onConnectionStateChange)==null||o.call(r,c.iceConnectionState)},c.ontrack=o=>{var h;y("peerConnection.ontrack",o),(h=r.onSrcObjectReady)==null||h.call(r,o.streams[0])},Y.onmessage=o=>{var h,v;if(Y.readyState==="open"){const[C,M]=o.data.split(":");C===R.StreamStarted?console.log("StreamStarted",C,M):C===R.StreamDone?console.log("StreamDone"):C===R.StreamFailed?((h=r.onVideoStateChange)==null||h.call(r,_.Stop,{event:C,data:M}),S=[],b=0,K=0,A=!1,console.log("StreamFailed")):(v=r.onMessage)==null||v.call(r,C,decodeURIComponent(M))}},await c.setRemoteDescription($),y("set remote description OK");const O=await c.createAnswer();return y("create answer OK"),await c.setLocalDescription(O),y("set local description OK"),await l(u,O,s),y("start connection OK"),{speak(o){return g(u,s,o)},async terminate(){var o,h;u&&(c&&(c.close(),c.oniceconnectionstatechange=null,c.onnegotiationneeded=null,c.onicecandidate=null,c.ontrack=null,clearInterval(Z)),await m(u,s).catch(v=>{}),(o=r.onConnectionStateChange)==null||o.call(r,"closed"),(h=r.onVideoStateChange)==null||h.call(r,_.Stop),le(),clearInterval(x))},sessionId:s,streamId:u,onCallback(o,h){r[o]=h}}}d.ChatMode=z,d.ChatProgress=P,d.DocumentType=V,d.KnowledgeType=D,d.Providers=F,d.RateState=N,d.SocketManager=U,d.StreamEvents=R,d.Subject=J,d.VoiceAccess=q,d.createAgentManager=ie,d.createAgentsApi=T,d.createClient=k,d.createKnowledgeApi=B,d.createRatingsApi=H,d.createStreamingManager=X,d.getAgent=re,Object.defineProperty(d,Symbol.toStringTag,{value:"Module"})});
|
|
1
|
+
(function(l,h){typeof exports=="object"&&typeof module<"u"?h(exports):typeof define=="function"&&define.amd?define(["exports"],h):(l=typeof globalThis<"u"?globalThis:l||self,h(l.index={}))})(this,function(l){"use strict";const h="https://api.d-id.com",D="wss://notifications.d-id.com";function G(){let e=window.localStorage.getItem("external_key_d_id");return e||(e=Math.random().toString(16).slice(2),window.localStorage.setItem("external_key_d_id",e)),e}function K(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()}`;throw new Error(`Unknown auth type: ${e}`)}function y(e,i=h){const n=async(t,r)=>{const a=await fetch(i+(t!=null&&t.startsWith("/")?t:`/${t}`),{...r,headers:{...r==null?void 0:r.headers,Authorization:K(e),"Content-Type":"application/json"}});if(!a.ok){let o=await a.text().catch(()=>"Failed to fetch");throw new Error(o)}return a.json()};return{get(t,r){return n(t,{...r,method:"GET"})},post(t,r,a){return n(t,{...a,body:JSON.stringify(r),method:"POST"})},delete(t,r,a){return n(t,{...a,body:JSON.stringify(r),method:"DELETE"})},patch(t,r,a){return n(t,{...a,body:JSON.stringify(r),method:"PATCH"})}}}function M(e,i=h){const n=y(e,`${i}/agents`);return{create(t,r){return n.post("/",t,r)},getAgents(t,r){return n.get(`/${t?`?tag=${t}`:""}`,r).then(a=>a??[])},getById(t,r){return n.get(`/${t}`,r)},delete(t,r){return n.delete(`/${t}`,void 0,r)},update(t,r,a){return n.patch(`/${t}`,r,a)},newChat(t,r){return n.post(`/${t}/chat`,void 0,r)},chat(t,r,a,o){return n.post(`/${t}/chat/${r}`,a,o)}}}function b(e,i=h){const n=y(e,`${i}/knowledge`);return{createKnowledge(t,r){return n.post("/",t,r)},getKnowledgeBase(t){return n.get("/",t)},getKnowledge(t,r){return n.get(`/${t}`,r)},deleteKnowledge(t,r){return n.delete(`/${t}`,void 0,r)},createDocument(t,r,a){return n.post(`/${t}/documents`,r,a)},deleteDocument(t,r,a){return n.delete(`/${t}/documents/${r}`,void 0,a)},getDocuments(t,r){return n.get(`/${t}/documents`,r)},getDocument(t,r,a){return n.get(`/${t}/documents/${r}`,a)},getRecords(t,r,a){return n.get(`/${t}/documents/${r}/records`,a)},query(t,r,a){return n.post(`/${t}/query`,{query:r},a)}}}function E(e,i=h){const n=y(e,`${i}/chats/ratings`);return{create(t,r){return n.post("/",t,r)},getByKnowledge(t,r){return n.get(`/${t}`,r).then(a=>a??[])},update(t,r,a){return n.patch(`/${t}`,r,a)},delete(t,r){return n.delete(`/${t}`,r)}}}const Q=e=>new Promise(i=>setTimeout(i,e));function x(e){return new Promise((i,n)=>{const{callbacks:t,host:r,auth:a}=e,{onMessage:o=null,onOpen:g=null,onClose:c=null,onError:m=null}=t||{},d=new WebSocket(`${r}?authorization=${K(a)}`);d.onmessage=o,d.onclose=c,d.onerror=f=>{console.error(f),m==null||m(f),n(f)},d.onopen=f=>{g==null||g(f),i(d)}})}async function X(e){const{retries:i=1}=e;let n=null;for(let t=0;(n==null?void 0:n.readyState)!==WebSocket.OPEN;t++)try{n=await x(e)}catch(r){if(t===i)throw r;await Q(t*500)}return n}async function T(e,i,n){const t=n?[n]:[],r=await X({auth:e,host:i,callbacks:{onMessage:a=>{const o=JSON.parse(a.data);t.forEach(g=>g(o.event,o))}}});return{socket:r,terminate:()=>r.close(),subscribeToEvents:a=>t.push(a)}}var O=(e=>(e.Amazon="amazon",e.Microsoft="microsoft",e.Afflorithmics="afflorithmics",e.Elevenlabs="elevenlabs",e))(O||{}),W=(e=>(e.Public="public",e.Premium="premium",e.Private="private",e))(W||{}),A=(e=>(e.Start="START",e.Stop="STOP",e))(A||{}),R=(e=>(e.ChatAnswer="chat/answer",e.ChatPartial="chat/partial",e.StreamDone="stream/done",e.StreamStarted="stream/started",e.StreamFailed="stream/error",e))(R||{}),j=(e=>(e.Unrated="Unrated",e.Positive="Positive",e.Negative="Negative",e))(j||{}),B=(e=>(e.Functional="Functional",e.TextOnly="TextOnly",e.Maintenance="Maintenance",e))(B||{}),k=(e=>(e.Embed="embed",e.Query="query",e.Partial="partial",e.Answer="answer",e.Complete="done",e))(k||{}),H=(e=>(e.KnowledgeProcessing="knowledge/processing",e.KnowledgeIndexing="knowledge/indexing",e.KnowledgeFailed="knowledge/error",e.KnowledgeDone="knowledge/done",e))(H||{}),U=(e=>(e.Knowledge="knowledge",e.Document="document",e.Record="record",e))(U||{}),F=(e=>(e.Pdf="pdf",e.Text="text",e.Html="html",e.Word="word",e.Json="json",e.Markdown="markdown",e.Csv="csv",e.Excel="excel",e.Powerpoint="powerpoint",e.Archive="archive",e.Image="image",e.Audio="audio",e.Video="video",e))(F||{}),_=(e=>(e.Clip="clip",e.Talk="talk",e))(_||{});function Y(e){return e.presenter.type===_.Clip?{videoType:_.Clip,driver_id:e.presenter.driver_id,presenter_id:e.presenter.presenter_id}:{videoType:_.Talk,source_url:e.presenter.source_url}}function q(e,i,n,t){return new Promise(async(r,a)=>{const o=await z(Y(e),{...i,callbacks:{...i.callbacks,onConnectionStateChange:async g=>{var c,m;if(g==="connected")try{t||(t=await n.newChat(e.id)),r({chat:t,streamingManager:o})}catch(d){console.error(d),a(new Error("Cannot create new chat"))}else g==="failed"&&a(new Error("Cannot create connection"));(m=(c=i.callbacks).onConnectionStateChange)==null||m.call(c,g)},onMessage:(g,c)=>{var m,d;g===R.ChatAnswer&&(console.log("ChatAnswer",g,c),(d=(m=i.callbacks).onChatEvents)==null||d.call(m,k.Answer,{content:c,event:k.Answer}))}}})})}function Z(e,i,n){return M(i,n||h).getById(e)}async function V(e,i){var p,v;const n=i.baseURL||h,t=i.wsURL||D,r=new AbortController,a=M(i.auth,n),o=E(i.auth,n),g=b(i.auth,n),c=typeof e=="string"?await a.getById(e):e;(v=(p=i.callbacks)==null?void 0:p.onAgentReady)==null||v.call(p,c);const m=await T(i.auth,t,i.callbacks.onChatEvents);let{chat:d,streamingManager:f}=await q(c,i,a);return{agent:c,chatId:d.id,async reconnectToChat(){const{streamingManager:s}=await q(c,i,a,d);f=s},terminate(){return r.abort(),m.terminate(),f.terminate()},chat(s){return a.chat(c.id,d.id,{sessionId:f.sessionId,streamId:f.streamId,messages:s},{signal:r.signal})},rate(s,w){return w?o.update(w,s):o.create(s)},deleteRate(s){return o.delete(s)},speak(s){function w(){if(s.type==="text")return{type:"text",provider:s.provider,input:s.input,ssml:s.ssml||!1};if(s.type==="audio")return{type:"audio",audio_url:s.audio_url};throw new Error("Invalid payload")}return f.speak({script:w()})},async getStarterMessages(){var s;return(s=c.knowledge)!=null&&s.id?g.getKnowledge(c.knowledge.id).then(w=>(w==null?void 0:w.starter_message)||[]):[]}}}function ee(e,i){const n=y(e,i);return{createStream(t){return n.post("/clips/streams",{driver_id:t.driver_id,presenter_id:t.presenter_id,compatibility_mode:t.compatibility_mode})},startConnection(t,r,a){return n.post(`/clips/streams/${t}/sdp`,{session_id:a,answer:r})},addIceCandidate(t,r,a){return n.post(`/clips/streams/${t}/ice`,{session_id:a,...r})},sendStreamRequest(t,r,a){return n.post(`/clips/streams/${t}`,{session_id:r,...a})},close(t,r){return n.delete(`/clips/streams/${t}`,{session_id:r})}}}function te(e,i){const n=y(e,i);return{createStream(t,r){return n.post("/talks/streams",{source_url:t.source_url,driver_url:t.driver_url,face:t.face,config:t.config},r)},startConnection(t,r,a,o){return n.post(`/talks/streams/${t}/sdp`,{session_id:a,answer:r},o)},addIceCandidate(t,r,a,o){return n.post(`/talks/streams/${t}/ice`,{session_id:a,...r},o)},sendStreamRequest(t,r,a,o){return n.post(`/talks/streams/${t}`,{session_id:r,...a},o)},close(t,r,a){return n.delete(`/talks/streams/${t}`,{session_id:r},a)}}}function ne(e,i){return e.map((n,t)=>t===0?i?{index:t,timestamp:n.timestamp,bytesReceived:n.bytesReceived-i.bytesReceived,packetsReceived:n.packetsReceived-i.packetsReceived,packetsLost:n.packetsLost-i.packetsLost,jitter:n.jitter,frameWidth:n.frameWidth,frameHeight:n.frameHeight,frameRate:n.frameRate}:{index:t,timestamp:n.timestamp,bytesReceived:n.bytesReceived,packetsReceived:n.packetsReceived,packetsLost:n.packetsLost,jitter:n.jitter,frameWidth:n.frameWidth,frameHeight:n.frameHeight,frameRate:n.frameRate}:{index:t,timestamp:n.timestamp,bytesReceived:n.bytesReceived-e[t-1].bytesReceived,packetsReceived:n.packetsReceived-e[t-1].packetsReceived,packetsLost:n.packetsLost-e[t-1].packetsLost,jitter:n.jitter,frameWidth:n.frameWidth,frameHeight:n.frameHeight,frameRate:n.frameRate})}let N=!1;const C=(e,i)=>N&&console.log(e,i),re=(window.RTCPeerConnection||window.webkitRTCPeerConnection||window.mozRTCPeerConnection).bind(window);function ae(e,i){let n=[],t=0,r=0,a;return setInterval(()=>{e.getStats().then(g=>{g.forEach(c=>{if(c.type==="inbound-rtp"&&c.kind==="video"){if(r=n.length-1,c&&n[r]){const m=c.bytesReceived,d=n[r].bytesReceived;let f=a;a=m-d>0;let p;if(f!==a){if(a)t=n.length;else{const v=n.slice(t),s=t===0?void 0:n[t-1];p=ne(v,s),p=p.sort((w,I)=>I.packetsLost-w.packetsLost).slice(0,5)}i==null||i(a?A.Start:A.Stop,p)}}n.push(c)}})})},500)}async function z(e,{debug:i=!1,callbacks:n,auth:t,baseURL:r=h}){N=i;const{startConnection:a,sendStreamRequest:o,close:g,createStream:c,addIceCandidate:m}=e.videoType===_.Clip?ee(t,r):te(t,r),{id:d,offer:f,ice_servers:p,session_id:v}=await c(e),s=new re({iceServers:p}),w=s.createDataChannel("JanusDataChannel");if(!v)throw new Error("Could not create session_id");const I=ae(s,n.onVideoStateChange);s.onicecandidate=u=>{C("peerConnection.onicecandidate",u),u.candidate&&u.candidate.sdpMid&&u.candidate.sdpMLineIndex!==null&&m(d,{candidate:u.candidate.candidate,sdpMid:u.candidate.sdpMid,sdpMLineIndex:u.candidate.sdpMLineIndex},v)},s.oniceconnectionstatechange=()=>{var u;C("peerConnection.oniceconnectionstatechange => "+s.iceConnectionState),(u=n.onConnectionStateChange)==null||u.call(n,s.iceConnectionState)},s.ontrack=u=>{var S;C("peerConnection.ontrack",u),(S=n.onSrcObjectReady)==null||S.call(n,u.streams[0])},w.onmessage=u=>{var S,P;if(w.readyState==="open"){const[$,L]=u.data.split(":");$===R.StreamStarted?console.log("StreamStarted",$,L):$===R.StreamDone?console.log("StreamDone"):$===R.StreamFailed?((S=n.onVideoStateChange)==null||S.call(n,A.Stop,{event:$,data:L}),clearInterval(I),console.log("StreamFailed")):(P=n.onMessage)==null||P.call(n,$,decodeURIComponent(L))}},await s.setRemoteDescription(f),C("set remote description OK");const J=await s.createAnswer();return C("create answer OK"),await s.setLocalDescription(J),C("set local description OK"),await a(d,J,v),C("start connection OK"),{speak(u){return o(d,v,u)},async terminate(){var u,S;d&&(s&&(s.close(),s.oniceconnectionstatechange=null,s.onnegotiationneeded=null,s.onicecandidate=null,s.ontrack=null),await g(d,v).catch(P=>{}),(u=n.onConnectionStateChange)==null||u.call(n,"closed"),(S=n.onVideoStateChange)==null||S.call(n,A.Stop),clearInterval(I))},sessionId:v,streamId:d}}l.ChatMode=B,l.ChatProgress=k,l.DocumentType=F,l.KnowledgeType=U,l.Providers=O,l.RateState=j,l.SocketManager=T,l.StreamEvents=R,l.Subject=H,l.VoiceAccess=W,l.createAgentManager=V,l.createAgentsApi=M,l.createClient=y,l.createKnowledgeApi=b,l.createRatingsApi=E,l.createStreamingManager=z,l.getAgent=Z,Object.defineProperty(l,Symbol.toStringTag,{value:"Module"})});
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Agent, AgentPayload, Auth, Chat, ChatPayload, ChatResponse } from '
|
|
1
|
+
import { Agent, AgentPayload, Auth, Chat, ChatPayload, ChatResponse } from '../types/index';
|
|
2
2
|
export declare function createAgentsApi(auth: Auth, host?: string): {
|
|
3
3
|
create(payload: AgentPayload, options?: RequestInit): Promise<Agent>;
|
|
4
4
|
getAgents(tag?: string, options?: RequestInit): Promise<Agent[]>;
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { Auth, RtcApi } from '
|
|
1
|
+
import { Auth, RtcApi } from '../types/index';
|
|
2
2
|
export declare function createApi(auth: Auth, host: string): RtcApi;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Auth } from '
|
|
1
|
+
import { Auth } from '../types/auth';
|
|
2
2
|
export declare function createClient(auth: Auth, host?: string): {
|
|
3
3
|
get<T = any>(url: string, options?: RequestInit): Promise<T>;
|
|
4
4
|
post<T_1 = any>(url: string, body?: any, options?: RequestInit): Promise<T_1>;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Auth, CreateDocumentPayload, DocumentData, KnowledgeData, KnowledgePayload, QueryResult, RecordData } from '
|
|
1
|
+
import { Auth, CreateDocumentPayload, DocumentData, KnowledgeData, KnowledgePayload, QueryResult, RecordData } from '../types/index';
|
|
2
2
|
export declare function createKnowledgeApi(auth: Auth, host?: string): {
|
|
3
3
|
createKnowledge(payload: KnowledgePayload, options?: RequestInit): Promise<KnowledgeData>;
|
|
4
4
|
getKnowledgeBase(options?: RequestInit): Promise<KnowledgeData[]>;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Auth, RatingEntity, RatingPayload } from '
|
|
1
|
+
import { Auth, RatingEntity, RatingPayload } from '../index';
|
|
2
2
|
export declare function createRatingsApi(auth: Auth, host?: string): {
|
|
3
3
|
create(payload: RatingPayload, options?: RequestInit): Promise<RatingEntity>;
|
|
4
4
|
getByKnowledge(knowledgeId?: string, options?: RequestInit): Promise<RatingEntity[]>;
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { Auth, RtcApi } from '
|
|
1
|
+
import { Auth, RtcApi } from '../types/index';
|
|
2
2
|
export declare function createApi(auth: Auth, host: string): RtcApi;
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { Auth } from '
|
|
1
|
+
import { Auth } from '../types/auth';
|
|
2
2
|
export declare function getAuthHeader(auth: Auth): string;
|
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import { Agent, AgentManager, AgentManagerOptions } from '
|
|
2
|
-
import { Auth } from '
|
|
1
|
+
import { Agent, AgentManager, AgentManagerOptions } from './types/index';
|
|
2
|
+
import { Auth } from '.';
|
|
3
3
|
export declare function getAgent(agentId: string, auth: Auth, baseURL?: string): Promise<Agent>;
|
|
4
4
|
/**
|
|
5
5
|
* Creates a new Agent Manager instance for interacting with an agent, chat, and related connections.
|
|
6
6
|
*
|
|
7
|
-
* @param {string}
|
|
7
|
+
* @param {string} agent - The ID or instance of the agent to chat with.
|
|
8
8
|
* @param {AgentManagerOptions} options - Configurations for the Agent Manager API.
|
|
9
9
|
* * @returns {Promise<AgentManager>} - A promise that resolves to an instance of the AgentsAPI interface.
|
|
10
10
|
*
|
|
@@ -13,4 +13,4 @@ export declare function getAgent(agentId: string, auth: Auth, baseURL?: string):
|
|
|
13
13
|
* @example
|
|
14
14
|
* const agentManager = await createAgentManager('id-agent123', { auth: { type: 'key', clientKey: '123', externalId: '123' } });
|
|
15
15
|
*/
|
|
16
|
-
export declare function createAgentManager(
|
|
16
|
+
export declare function createAgentManager(agent: string | Agent, options: AgentManagerOptions): Promise<AgentManager>;
|
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import { CreateStreamOptions,
|
|
1
|
+
import { CreateStreamOptions, PayloadType, StreamingManagerOptions } from './types/index';
|
|
2
2
|
export declare function createStreamingManager<T extends CreateStreamOptions>(agent: T, { debug, callbacks, auth, baseURL }: StreamingManagerOptions): Promise<{
|
|
3
3
|
/**
|
|
4
4
|
* Method to send request to server to get clip or talk depend on you payload
|
|
5
5
|
* @param payload
|
|
6
6
|
*/
|
|
7
|
-
speak(payload: PayloadType<T>): Promise<import('
|
|
7
|
+
speak(payload: PayloadType<T>): Promise<import('./types/index').SendStreamPayloadResponse>;
|
|
8
8
|
/**
|
|
9
9
|
* Method to close RTC connection
|
|
10
10
|
*/
|
|
@@ -17,11 +17,5 @@ export declare function createStreamingManager<T extends CreateStreamOptions>(ag
|
|
|
17
17
|
* Id of current RTC stream
|
|
18
18
|
*/
|
|
19
19
|
streamId: string;
|
|
20
|
-
/**
|
|
21
|
-
* Method to add callback that will be trigered on supported events
|
|
22
|
-
* @param eventName
|
|
23
|
-
* @param callback
|
|
24
|
-
*/
|
|
25
|
-
onCallback<T_1 extends keyof ManagerCallbacks>(eventName: T_1, callback: ManagerCallbacks[T_1]): void;
|
|
26
20
|
}>;
|
|
27
21
|
export type StreamingManager<T extends CreateStreamOptions> = Awaited<ReturnType<typeof createStreamingManager<T>>>;
|
package/dist/src/index.d.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
export * from './
|
|
2
|
-
export * from './
|
|
3
|
-
export * from './
|
|
4
|
-
export * from './
|
|
5
|
-
export * from './
|
|
6
|
-
export * from './
|
|
7
|
-
export * from './
|
|
1
|
+
export * from './api/agents';
|
|
2
|
+
export * from './api/getClient';
|
|
3
|
+
export * from './api/knowledge';
|
|
4
|
+
export * from './api/ratings';
|
|
5
|
+
export * from './connectToSocket';
|
|
6
|
+
export * from './createAgentManager';
|
|
7
|
+
export * from './createStreamingManager';
|
|
8
8
|
export * from './types/index';
|
package/dist/src/types/auth.d.ts
CHANGED
|
@@ -10,7 +10,6 @@ export interface BasicAuth {
|
|
|
10
10
|
export interface ClientKeyAuth {
|
|
11
11
|
type: 'key';
|
|
12
12
|
clientKey: string;
|
|
13
|
-
externalId: string;
|
|
14
13
|
}
|
|
15
14
|
export type Auth = BearerToken | BasicAuth | ClientKeyAuth;
|
|
16
15
|
export interface GetAuthParams {
|
|
@@ -18,5 +17,4 @@ export interface GetAuthParams {
|
|
|
18
17
|
username?: string | null;
|
|
19
18
|
password?: string | null;
|
|
20
19
|
clientKey?: string | null;
|
|
21
|
-
externalId?: string | null;
|
|
22
20
|
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { SupportedStreamScipt } from '
|
|
1
|
+
import { SupportedStreamScipt } from '../../StreamScript';
|
|
2
2
|
import { Auth } from '../../auth';
|
|
3
3
|
import { SendStreamPayloadResponse, StreamingState } from '../../stream';
|
|
4
4
|
import { Agent } from './agent';
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { SlimRTCStatsReport } from
|
|
1
|
+
import { SlimRTCStatsReport } from '../types';
|
|
2
2
|
export declare function createVideoStatsReport(stats: SlimRTCStatsReport[], previousStats?: SlimRTCStatsReport): SlimRTCStatsReport[];
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@d-id/client-sdk",
|
|
3
3
|
"private": false,
|
|
4
|
-
"version": "1.0.19-beta.
|
|
4
|
+
"version": "1.0.19-beta.38",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"description": "d-id client sdk",
|
|
7
7
|
"repository": {
|
|
@@ -25,7 +25,7 @@
|
|
|
25
25
|
"dev": "vite",
|
|
26
26
|
"build": "node ./infra/build.js -m production",
|
|
27
27
|
"build:dev": "node ./infra/build.js -m development",
|
|
28
|
-
"deploy:prod": "node ./infra/deploy.js --version
|
|
28
|
+
"deploy:prod": "node ./infra/deploy.js --version beta",
|
|
29
29
|
"preview": "vite preview",
|
|
30
30
|
"test-build": "node .infra/build.js -m development",
|
|
31
31
|
"build:docs": "typedoc"
|
|
@@ -40,9 +40,8 @@
|
|
|
40
40
|
"prettier-plugin-organize-imports": "^3.2.4",
|
|
41
41
|
"serverless": "3.32.2",
|
|
42
42
|
"typedoc": "^0.25.7",
|
|
43
|
-
"typescript": "^5.3.
|
|
44
|
-
"vite": "^
|
|
45
|
-
"vite-plugin-dts": "^3.
|
|
46
|
-
"vite-plugin-html-env": "^1.2.8"
|
|
43
|
+
"typescript": "^5.3.3",
|
|
44
|
+
"vite": "^5.1.4",
|
|
45
|
+
"vite-plugin-dts": "^3.7.3"
|
|
47
46
|
}
|
|
48
47
|
}
|