@d-id/client-sdk 1.0.19-beta.29 → 1.0.19-beta.30
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 +289 -279
- package/dist/index.umd.cjs +1 -1
- package/dist/src/types/stream/stream.d.ts +2 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
const
|
|
2
|
-
function
|
|
1
|
+
const R = "https://api.d-id.com", G = "wss://notifications.d-id.com";
|
|
2
|
+
function N(e) {
|
|
3
3
|
if (e.type === "bearer")
|
|
4
4
|
return `Bearer ${e.token}`;
|
|
5
5
|
if (e.type === "basic")
|
|
@@ -8,265 +8,255 @@ function b(e) {
|
|
|
8
8
|
return `Client-Key ${e.clientKey}.${e.externalId}`;
|
|
9
9
|
throw new Error(`Unknown auth type: ${e}`);
|
|
10
10
|
}
|
|
11
|
-
function
|
|
12
|
-
const
|
|
11
|
+
function L(e, s = R) {
|
|
12
|
+
const n = async (t, r) => {
|
|
13
13
|
const a = await fetch(s + (t != null && t.startsWith("/") ? t : `/${t}`), {
|
|
14
|
-
...
|
|
14
|
+
...r,
|
|
15
15
|
headers: {
|
|
16
|
-
...
|
|
17
|
-
Authorization:
|
|
16
|
+
...r == null ? void 0 : r.headers,
|
|
17
|
+
Authorization: N(e),
|
|
18
18
|
"Content-Type": "application/json"
|
|
19
19
|
}
|
|
20
20
|
});
|
|
21
21
|
if (!a.ok) {
|
|
22
|
-
let
|
|
23
|
-
throw new Error(
|
|
22
|
+
let o = await a.text().catch(() => "Failed to fetch");
|
|
23
|
+
throw new Error(o);
|
|
24
24
|
}
|
|
25
25
|
return a.json();
|
|
26
26
|
};
|
|
27
27
|
return {
|
|
28
|
-
get(t,
|
|
29
|
-
return
|
|
30
|
-
...
|
|
28
|
+
get(t, r) {
|
|
29
|
+
return n(t, {
|
|
30
|
+
...r,
|
|
31
31
|
method: "GET"
|
|
32
32
|
});
|
|
33
33
|
},
|
|
34
|
-
post(t,
|
|
35
|
-
return
|
|
34
|
+
post(t, r, a) {
|
|
35
|
+
return n(t, {
|
|
36
36
|
...a,
|
|
37
|
-
body: JSON.stringify(
|
|
37
|
+
body: JSON.stringify(r),
|
|
38
38
|
method: "POST"
|
|
39
39
|
});
|
|
40
40
|
},
|
|
41
|
-
delete(t,
|
|
42
|
-
return
|
|
41
|
+
delete(t, r, a) {
|
|
42
|
+
return n(t, {
|
|
43
43
|
...a,
|
|
44
|
-
body: JSON.stringify(
|
|
44
|
+
body: JSON.stringify(r),
|
|
45
45
|
method: "DELETE"
|
|
46
46
|
});
|
|
47
47
|
},
|
|
48
|
-
patch(t,
|
|
49
|
-
return
|
|
48
|
+
patch(t, r, a) {
|
|
49
|
+
return n(t, {
|
|
50
50
|
...a,
|
|
51
|
-
body: JSON.stringify(
|
|
51
|
+
body: JSON.stringify(r),
|
|
52
52
|
method: "PATCH"
|
|
53
53
|
});
|
|
54
54
|
}
|
|
55
55
|
};
|
|
56
56
|
}
|
|
57
|
-
function
|
|
58
|
-
const
|
|
57
|
+
function z(e, s = R) {
|
|
58
|
+
const n = L(e, `${s}/agents`);
|
|
59
59
|
return {
|
|
60
|
-
create(t,
|
|
61
|
-
return
|
|
60
|
+
create(t, r) {
|
|
61
|
+
return n.post("/", t, r);
|
|
62
62
|
},
|
|
63
|
-
getAgents(t,
|
|
64
|
-
return
|
|
63
|
+
getAgents(t, r) {
|
|
64
|
+
return n.get(`/${t ? `?tag=${t}` : ""}`, r).then((a) => a ?? []);
|
|
65
65
|
},
|
|
66
|
-
getById(t,
|
|
67
|
-
return
|
|
66
|
+
getById(t, r) {
|
|
67
|
+
return n.get(`/${t}`, r);
|
|
68
68
|
},
|
|
69
|
-
delete(t,
|
|
70
|
-
return
|
|
69
|
+
delete(t, r) {
|
|
70
|
+
return n.delete(`/${t}`, void 0, r);
|
|
71
71
|
},
|
|
72
|
-
update(t,
|
|
73
|
-
return
|
|
72
|
+
update(t, r, a) {
|
|
73
|
+
return n.patch(`/${t}`, r, a);
|
|
74
74
|
},
|
|
75
|
-
newChat(t,
|
|
76
|
-
return
|
|
75
|
+
newChat(t, r) {
|
|
76
|
+
return n.post(`/${t}/chat`, void 0, r);
|
|
77
77
|
},
|
|
78
|
-
chat(t,
|
|
79
|
-
return
|
|
78
|
+
chat(t, r, a, o) {
|
|
79
|
+
return n.post(`/${t}/chat/${r}`, a, o);
|
|
80
80
|
}
|
|
81
81
|
};
|
|
82
82
|
}
|
|
83
|
-
function
|
|
84
|
-
const
|
|
83
|
+
function Q(e, s = R) {
|
|
84
|
+
const n = L(e, `${s}/knowledge`);
|
|
85
85
|
return {
|
|
86
|
-
createKnowledge(t,
|
|
87
|
-
return
|
|
86
|
+
createKnowledge(t, r) {
|
|
87
|
+
return n.post("/", t, r);
|
|
88
88
|
},
|
|
89
89
|
getKnowledgeBase(t) {
|
|
90
|
-
return
|
|
90
|
+
return n.get("/", t);
|
|
91
91
|
},
|
|
92
|
-
getKnowledge(t,
|
|
93
|
-
return
|
|
92
|
+
getKnowledge(t, r) {
|
|
93
|
+
return n.get(`/${t}`, r);
|
|
94
94
|
},
|
|
95
|
-
deleteKnowledge(t,
|
|
96
|
-
return
|
|
95
|
+
deleteKnowledge(t, r) {
|
|
96
|
+
return n.delete(`/${t}`, void 0, r);
|
|
97
97
|
},
|
|
98
|
-
createDocument(t,
|
|
99
|
-
return
|
|
98
|
+
createDocument(t, r, a) {
|
|
99
|
+
return n.post(`/${t}/documents`, r, a);
|
|
100
100
|
},
|
|
101
|
-
deleteDocument(t,
|
|
102
|
-
return
|
|
101
|
+
deleteDocument(t, r, a) {
|
|
102
|
+
return n.delete(`/${t}/documents/${r}`, void 0, a);
|
|
103
103
|
},
|
|
104
|
-
getDocuments(t,
|
|
105
|
-
return
|
|
104
|
+
getDocuments(t, r) {
|
|
105
|
+
return n.get(`/${t}/documents`, r);
|
|
106
106
|
},
|
|
107
|
-
getDocument(t,
|
|
108
|
-
return
|
|
107
|
+
getDocument(t, r, a) {
|
|
108
|
+
return n.get(`/${t}/documents/${r}`, a);
|
|
109
109
|
},
|
|
110
|
-
getRecords(t,
|
|
111
|
-
return
|
|
110
|
+
getRecords(t, r, a) {
|
|
111
|
+
return n.get(`/${t}/documents/${r}/records`, a);
|
|
112
112
|
},
|
|
113
|
-
query(t,
|
|
114
|
-
return
|
|
115
|
-
query:
|
|
113
|
+
query(t, r, a) {
|
|
114
|
+
return n.post(`/${t}/query`, {
|
|
115
|
+
query: r
|
|
116
116
|
}, a);
|
|
117
117
|
}
|
|
118
118
|
};
|
|
119
119
|
}
|
|
120
|
-
function
|
|
121
|
-
const
|
|
120
|
+
function X(e, s = R) {
|
|
121
|
+
const n = L(e, `${s}/chats/ratings`);
|
|
122
122
|
return {
|
|
123
|
-
create(t,
|
|
124
|
-
return
|
|
123
|
+
create(t, r) {
|
|
124
|
+
return n.post("/", t, r);
|
|
125
125
|
},
|
|
126
|
-
getByKnowledge(t,
|
|
127
|
-
return
|
|
126
|
+
getByKnowledge(t, r) {
|
|
127
|
+
return n.get(`/${t}`, r).then((a) => a ?? []);
|
|
128
128
|
},
|
|
129
|
-
update(t,
|
|
130
|
-
return
|
|
129
|
+
update(t, r, a) {
|
|
130
|
+
return n.patch(`/${t}`, r, a);
|
|
131
131
|
},
|
|
132
|
-
delete(t,
|
|
133
|
-
return
|
|
132
|
+
delete(t, r) {
|
|
133
|
+
return n.delete(`/${t}`, r);
|
|
134
134
|
}
|
|
135
135
|
};
|
|
136
136
|
}
|
|
137
|
-
const
|
|
138
|
-
function
|
|
139
|
-
return new Promise((s,
|
|
137
|
+
const Y = (e) => new Promise((s) => setTimeout(s, e));
|
|
138
|
+
function Z(e) {
|
|
139
|
+
return new Promise((s, n) => {
|
|
140
140
|
const {
|
|
141
141
|
callbacks: t,
|
|
142
|
-
host:
|
|
142
|
+
host: r,
|
|
143
143
|
auth: a
|
|
144
144
|
} = e, {
|
|
145
|
-
onMessage:
|
|
146
|
-
onOpen:
|
|
147
|
-
onClose:
|
|
145
|
+
onMessage: o = null,
|
|
146
|
+
onOpen: l = null,
|
|
147
|
+
onClose: g = null,
|
|
148
148
|
onError: f = null
|
|
149
|
-
} = t || {},
|
|
150
|
-
|
|
151
|
-
console.log(d),
|
|
152
|
-
},
|
|
153
|
-
|
|
149
|
+
} = t || {}, m = new WebSocket(`${r}?authorization=${N(a)}`);
|
|
150
|
+
m.onmessage = o, m.onclose = g, m.onerror = (d) => {
|
|
151
|
+
console.log(d), f == null || f(d), n(d);
|
|
152
|
+
}, m.onopen = (d) => {
|
|
153
|
+
l == null || l(d), s(m);
|
|
154
154
|
};
|
|
155
155
|
});
|
|
156
156
|
}
|
|
157
|
-
async function
|
|
157
|
+
async function O(e) {
|
|
158
158
|
const {
|
|
159
159
|
retries: s = 1
|
|
160
160
|
} = e;
|
|
161
|
-
let
|
|
162
|
-
for (let t = 0; (
|
|
161
|
+
let n = null;
|
|
162
|
+
for (let t = 0; (n == null ? void 0 : n.readyState) !== WebSocket.OPEN; t++)
|
|
163
163
|
try {
|
|
164
|
-
|
|
165
|
-
} catch (
|
|
164
|
+
n = await Z(e);
|
|
165
|
+
} catch (r) {
|
|
166
166
|
if (t === s)
|
|
167
|
-
throw
|
|
168
|
-
await
|
|
167
|
+
throw r;
|
|
168
|
+
await Y(t * 500);
|
|
169
169
|
}
|
|
170
|
-
return
|
|
170
|
+
return n;
|
|
171
171
|
}
|
|
172
|
-
async function
|
|
173
|
-
const t =
|
|
172
|
+
async function j(e, s, n) {
|
|
173
|
+
const t = n ? [n] : [], r = await O({
|
|
174
174
|
auth: e,
|
|
175
175
|
host: s,
|
|
176
176
|
callbacks: {
|
|
177
177
|
onMessage: (a) => {
|
|
178
|
-
const
|
|
179
|
-
|
|
178
|
+
const o = JSON.parse(a.data);
|
|
179
|
+
t.forEach((l) => l(o.event, o));
|
|
180
180
|
}
|
|
181
181
|
}
|
|
182
182
|
});
|
|
183
183
|
return {
|
|
184
|
-
socket:
|
|
185
|
-
terminate: () =>
|
|
184
|
+
socket: r,
|
|
185
|
+
terminate: () => r.close(),
|
|
186
186
|
subscribeToEvents: (a) => t.push(a)
|
|
187
187
|
};
|
|
188
188
|
}
|
|
189
|
-
var
|
|
190
|
-
function
|
|
191
|
-
return e.presenter.type ===
|
|
192
|
-
videoType:
|
|
189
|
+
var ee = /* @__PURE__ */ ((e) => (e.Amazon = "amazon", e.Microsoft = "microsoft", e.Afflorithmics = "afflorithmics", e.Elevenlabs = "elevenlabs", e))(ee || {}), te = /* @__PURE__ */ ((e) => (e.Public = "public", e.Premium = "premium", e.Private = "private", e))(te || {}), A = /* @__PURE__ */ ((e) => (e.Start = "START", e.Stop = "STOP", e))(A || {}), P = /* @__PURE__ */ ((e) => (e.ChatAnswer = "chat/answer", e.ChatPartial = "chat/partial", e.StreamDone = "stream/done", e.StreamStarted = "stream/started", e.StreamFailed = "stream/error", e))(P || {}), ne = /* @__PURE__ */ ((e) => (e.Unrated = "Unrated", e.Positive = "Positive", e.Negative = "Negative", e))(ne || {}), re = /* @__PURE__ */ ((e) => (e.Functional = "Functional", e.TextOnly = "TextOnly", e.Maintenance = "Maintenance", e))(re || {}), x = /* @__PURE__ */ ((e) => (e.Embed = "embed", e.Query = "query", e.Partial = "partial", e.Answer = "answer", e.Complete = "done", e))(x || {}), ae = /* @__PURE__ */ ((e) => (e.KnowledgeProcessing = "knowledge/processing", e.KnowledgeIndexing = "knowledge/indexing", e.KnowledgeFailed = "knowledge/error", e.KnowledgeDone = "knowledge/done", e))(ae || {}), se = /* @__PURE__ */ ((e) => (e.Knowledge = "knowledge", e.Document = "document", e.Record = "record", e))(se || {}), ie = /* @__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))(ie || {}), M = /* @__PURE__ */ ((e) => (e.Clip = "clip", e.Talk = "talk", e))(M || {});
|
|
190
|
+
function oe(e) {
|
|
191
|
+
return e.presenter.type === M.Clip ? {
|
|
192
|
+
videoType: M.Clip,
|
|
193
193
|
driver_id: e.presenter.driver_id,
|
|
194
194
|
presenter_id: e.presenter.presenter_id
|
|
195
195
|
} : {
|
|
196
|
-
videoType:
|
|
196
|
+
videoType: M.Talk,
|
|
197
197
|
source_url: e.presenter.source_url
|
|
198
198
|
};
|
|
199
199
|
}
|
|
200
|
-
function
|
|
201
|
-
return new Promise(async (
|
|
202
|
-
const
|
|
200
|
+
function F(e, s, n, t) {
|
|
201
|
+
return new Promise(async (r, a) => {
|
|
202
|
+
const o = await ge(oe(e), {
|
|
203
203
|
...s,
|
|
204
204
|
callbacks: {
|
|
205
205
|
...s.callbacks,
|
|
206
|
-
onConnectionStateChange: async (
|
|
207
|
-
var
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
streamingManager: i
|
|
213
|
-
});
|
|
214
|
-
} catch (l) {
|
|
215
|
-
console.error(l), a(new Error("Cannot create new chat"));
|
|
216
|
-
}
|
|
217
|
-
else
|
|
218
|
-
u === "failed" && a(new Error("Cannot create connection"));
|
|
219
|
-
(f = (m = s.callbacks).onConnectionStateChange) == null || f.call(m, u);
|
|
206
|
+
onConnectionStateChange: async (l) => {
|
|
207
|
+
var g, f;
|
|
208
|
+
l === "connected" ? (t || (t = await n.newChat(e.id)), r({
|
|
209
|
+
chat: t,
|
|
210
|
+
streamingManager: o
|
|
211
|
+
})) : l === "failed" && a(new Error("Cannot create connection")), (f = (g = s.callbacks).onConnectionStateChange) == null || f.call(g, l);
|
|
220
212
|
},
|
|
221
213
|
// TODO remove when webscoket will return partial
|
|
222
|
-
onMessage: (
|
|
223
|
-
var f,
|
|
224
|
-
|
|
225
|
-
content:
|
|
226
|
-
event:
|
|
214
|
+
onMessage: (l, g) => {
|
|
215
|
+
var f, m;
|
|
216
|
+
l === P.ChatAnswer && (console.log("ChatAnswer", l, g), (m = (f = s.callbacks).onChatEvents) == null || m.call(f, x.Answer, {
|
|
217
|
+
content: g,
|
|
218
|
+
event: x.Answer
|
|
227
219
|
}));
|
|
228
220
|
}
|
|
229
221
|
}
|
|
230
222
|
});
|
|
231
223
|
});
|
|
232
224
|
}
|
|
233
|
-
function
|
|
234
|
-
return
|
|
225
|
+
function fe(e, s, n) {
|
|
226
|
+
return z(s, n || R).getById(e);
|
|
235
227
|
}
|
|
236
|
-
async function
|
|
237
|
-
const
|
|
238
|
-
console.log(`AUTH: ${s.auth.clientKey},${s.auth.externalId}, ${t}`);
|
|
239
|
-
const m = await a.getById(e), f = await X(s.auth, t, s.callbacks.onChatEvents);
|
|
228
|
+
async function pe(e, s) {
|
|
229
|
+
const n = s.baseURL || R, t = s.wsURL || G, r = new AbortController(), a = z(s.auth, n), o = X(s.auth, n), l = Q(s.auth, n), g = await a.getById(e), f = await j(s.auth, t, s.callbacks.onChatEvents);
|
|
240
230
|
let {
|
|
241
|
-
chat:
|
|
231
|
+
chat: m,
|
|
242
232
|
streamingManager: d
|
|
243
|
-
} = await
|
|
233
|
+
} = await F(g, s, a);
|
|
244
234
|
return {
|
|
245
|
-
agent:
|
|
235
|
+
agent: g,
|
|
246
236
|
async reconnectToChat() {
|
|
247
237
|
const {
|
|
248
238
|
streamingManager: c
|
|
249
|
-
} = await
|
|
239
|
+
} = await F(g, s, a, m);
|
|
250
240
|
d = c;
|
|
251
241
|
},
|
|
252
242
|
terminate() {
|
|
253
|
-
return
|
|
243
|
+
return r.abort(), f.terminate(), d.terminate();
|
|
254
244
|
},
|
|
255
|
-
chatId:
|
|
245
|
+
chatId: m.id,
|
|
256
246
|
chat(c) {
|
|
257
|
-
return a.chat(e,
|
|
247
|
+
return a.chat(e, m.id, {
|
|
258
248
|
sessionId: d.sessionId,
|
|
259
249
|
streamId: d.streamId,
|
|
260
250
|
messages: c
|
|
261
251
|
}, {
|
|
262
|
-
signal:
|
|
252
|
+
signal: r.signal
|
|
263
253
|
});
|
|
264
254
|
},
|
|
265
255
|
rate(c, w) {
|
|
266
|
-
return w ?
|
|
256
|
+
return w ? o.update(w, c) : o.create(c);
|
|
267
257
|
},
|
|
268
258
|
deleteRate(c) {
|
|
269
|
-
return
|
|
259
|
+
return o.delete(c);
|
|
270
260
|
},
|
|
271
261
|
speak(c) {
|
|
272
262
|
let w;
|
|
@@ -286,198 +276,218 @@ async function le(e, s) {
|
|
|
286
276
|
},
|
|
287
277
|
getStarterMessages() {
|
|
288
278
|
var c, w;
|
|
289
|
-
return (c =
|
|
279
|
+
return (c = g.knowledge) != null && c.id ? l.getKnowledge((w = g.knowledge) == null ? void 0 : w.id).then((h) => (h == null ? void 0 : h.starter_message) || []) : Promise.resolve([]);
|
|
290
280
|
}
|
|
291
281
|
};
|
|
292
282
|
}
|
|
293
|
-
function
|
|
294
|
-
const
|
|
283
|
+
function ce(e, s) {
|
|
284
|
+
const n = L(e, s);
|
|
295
285
|
return {
|
|
296
286
|
createStream(t) {
|
|
297
|
-
return
|
|
287
|
+
return n.post("/clips/streams", {
|
|
298
288
|
driver_id: t.driver_id,
|
|
299
289
|
presenter_id: t.presenter_id,
|
|
300
290
|
compatibility_mode: t.compatibility_mode
|
|
301
291
|
});
|
|
302
292
|
},
|
|
303
|
-
startConnection(t,
|
|
304
|
-
return
|
|
293
|
+
startConnection(t, r, a) {
|
|
294
|
+
return n.post(`/clips/streams/${t}/sdp`, {
|
|
305
295
|
session_id: a,
|
|
306
|
-
answer:
|
|
296
|
+
answer: r
|
|
307
297
|
});
|
|
308
298
|
},
|
|
309
|
-
addIceCandidate(t,
|
|
310
|
-
return
|
|
299
|
+
addIceCandidate(t, r, a) {
|
|
300
|
+
return n.post(`/clips/streams/${t}/ice`, {
|
|
311
301
|
session_id: a,
|
|
312
|
-
...
|
|
302
|
+
...r
|
|
313
303
|
});
|
|
314
304
|
},
|
|
315
|
-
sendStreamRequest(t,
|
|
316
|
-
return
|
|
317
|
-
session_id:
|
|
305
|
+
sendStreamRequest(t, r, a) {
|
|
306
|
+
return n.post(`/clips/streams/${t}`, {
|
|
307
|
+
session_id: r,
|
|
318
308
|
...a
|
|
319
309
|
});
|
|
320
310
|
},
|
|
321
|
-
close(t,
|
|
322
|
-
return
|
|
323
|
-
session_id:
|
|
311
|
+
close(t, r) {
|
|
312
|
+
return n.delete(`/clips/streams/${t}`, {
|
|
313
|
+
session_id: r
|
|
324
314
|
});
|
|
325
315
|
}
|
|
326
316
|
};
|
|
327
317
|
}
|
|
328
|
-
function
|
|
329
|
-
const
|
|
318
|
+
function de(e, s) {
|
|
319
|
+
const n = L(e, s);
|
|
330
320
|
return {
|
|
331
|
-
createStream(t,
|
|
332
|
-
return
|
|
321
|
+
createStream(t, r) {
|
|
322
|
+
return n.post("/talks/streams", {
|
|
333
323
|
source_url: t.source_url,
|
|
334
324
|
driver_url: t.driver_url,
|
|
335
325
|
face: t.face,
|
|
336
326
|
config: t.config
|
|
337
|
-
},
|
|
327
|
+
}, r);
|
|
338
328
|
},
|
|
339
|
-
startConnection(t,
|
|
340
|
-
return
|
|
329
|
+
startConnection(t, r, a, o) {
|
|
330
|
+
return n.post(`/talks/streams/${t}/sdp`, {
|
|
341
331
|
session_id: a,
|
|
342
|
-
answer:
|
|
343
|
-
},
|
|
332
|
+
answer: r
|
|
333
|
+
}, o);
|
|
344
334
|
},
|
|
345
|
-
addIceCandidate(t,
|
|
346
|
-
return
|
|
335
|
+
addIceCandidate(t, r, a, o) {
|
|
336
|
+
return n.post(`/talks/streams/${t}/ice`, {
|
|
347
337
|
session_id: a,
|
|
348
|
-
...
|
|
349
|
-
},
|
|
338
|
+
...r
|
|
339
|
+
}, o);
|
|
350
340
|
},
|
|
351
|
-
sendStreamRequest(t,
|
|
352
|
-
return
|
|
353
|
-
session_id:
|
|
341
|
+
sendStreamRequest(t, r, a, o) {
|
|
342
|
+
return n.post(`/talks/streams/${t}`, {
|
|
343
|
+
session_id: r,
|
|
354
344
|
...a
|
|
355
|
-
},
|
|
345
|
+
}, o);
|
|
356
346
|
},
|
|
357
|
-
close(t,
|
|
358
|
-
return
|
|
359
|
-
session_id:
|
|
347
|
+
close(t, r, a) {
|
|
348
|
+
return n.delete(`/talks/streams/${t}`, {
|
|
349
|
+
session_id: r
|
|
360
350
|
}, a);
|
|
361
351
|
}
|
|
362
352
|
};
|
|
363
353
|
}
|
|
364
|
-
function
|
|
365
|
-
return e.map((
|
|
354
|
+
function le(e, s) {
|
|
355
|
+
return e.map((n, t) => t === 0 ? s ? {
|
|
366
356
|
index: t,
|
|
367
|
-
timestamp:
|
|
368
|
-
bytesReceived:
|
|
369
|
-
packetsReceived:
|
|
370
|
-
packetsLost:
|
|
371
|
-
jitter:
|
|
372
|
-
frameWidth:
|
|
373
|
-
frameHeight:
|
|
374
|
-
frameRate:
|
|
357
|
+
timestamp: n.timestamp,
|
|
358
|
+
bytesReceived: n.bytesReceived - s.bytesReceived,
|
|
359
|
+
packetsReceived: n.packetsReceived - s.packetsReceived,
|
|
360
|
+
packetsLost: n.packetsLost - s.packetsLost,
|
|
361
|
+
jitter: n.jitter,
|
|
362
|
+
frameWidth: n.frameWidth,
|
|
363
|
+
frameHeight: n.frameHeight,
|
|
364
|
+
frameRate: n.frameRate
|
|
375
365
|
} : {
|
|
376
366
|
index: t,
|
|
377
|
-
timestamp:
|
|
378
|
-
bytesReceived:
|
|
379
|
-
packetsReceived:
|
|
380
|
-
packetsLost:
|
|
381
|
-
jitter:
|
|
382
|
-
frameWidth:
|
|
383
|
-
frameHeight:
|
|
384
|
-
frameRate:
|
|
367
|
+
timestamp: n.timestamp,
|
|
368
|
+
bytesReceived: n.bytesReceived,
|
|
369
|
+
packetsReceived: n.packetsReceived,
|
|
370
|
+
packetsLost: n.packetsLost,
|
|
371
|
+
jitter: n.jitter,
|
|
372
|
+
frameWidth: n.frameWidth,
|
|
373
|
+
frameHeight: n.frameHeight,
|
|
374
|
+
frameRate: n.frameRate
|
|
385
375
|
} : {
|
|
386
376
|
index: t,
|
|
387
|
-
timestamp:
|
|
388
|
-
bytesReceived:
|
|
389
|
-
packetsReceived:
|
|
390
|
-
packetsLost:
|
|
391
|
-
jitter:
|
|
392
|
-
frameWidth:
|
|
393
|
-
frameHeight:
|
|
394
|
-
frameRate:
|
|
377
|
+
timestamp: n.timestamp,
|
|
378
|
+
bytesReceived: n.bytesReceived - e[t - 1].bytesReceived,
|
|
379
|
+
packetsReceived: n.packetsReceived - e[t - 1].packetsReceived,
|
|
380
|
+
packetsLost: n.packetsLost - e[t - 1].packetsLost,
|
|
381
|
+
jitter: n.jitter,
|
|
382
|
+
frameWidth: n.frameWidth,
|
|
383
|
+
frameHeight: n.frameHeight,
|
|
384
|
+
frameRate: n.frameRate
|
|
395
385
|
});
|
|
396
386
|
}
|
|
397
|
-
let
|
|
398
|
-
const
|
|
399
|
-
async function
|
|
387
|
+
let J = !1;
|
|
388
|
+
const y = (e, s) => J && console.log(e, s), ue = (window.RTCPeerConnection || window.webkitRTCPeerConnection || window.mozRTCPeerConnection).bind(window);
|
|
389
|
+
async function ge(e, {
|
|
400
390
|
debug: s = !1,
|
|
401
|
-
callbacks:
|
|
391
|
+
callbacks: n,
|
|
402
392
|
auth: t,
|
|
403
|
-
baseURL:
|
|
393
|
+
baseURL: r = R
|
|
404
394
|
}) {
|
|
405
|
-
|
|
395
|
+
J = s;
|
|
406
396
|
const a = {
|
|
407
|
-
...
|
|
397
|
+
...n
|
|
408
398
|
}, {
|
|
409
|
-
startConnection:
|
|
410
|
-
sendStreamRequest:
|
|
411
|
-
close:
|
|
399
|
+
startConnection: o,
|
|
400
|
+
sendStreamRequest: l,
|
|
401
|
+
close: g,
|
|
412
402
|
createStream: f,
|
|
413
|
-
addIceCandidate:
|
|
414
|
-
} = e.videoType ===
|
|
403
|
+
addIceCandidate: m
|
|
404
|
+
} = e.videoType === M.Clip ? ce(t, r) : de(t, r), {
|
|
415
405
|
id: d,
|
|
416
406
|
offer: c,
|
|
417
407
|
ice_servers: w,
|
|
418
|
-
session_id:
|
|
419
|
-
} = await f(e),
|
|
408
|
+
session_id: h
|
|
409
|
+
} = await f(e), u = new ue({
|
|
420
410
|
iceServers: w
|
|
421
|
-
}),
|
|
422
|
-
let
|
|
423
|
-
if (!
|
|
411
|
+
}), E = u.createDataChannel("JanusDataChannel");
|
|
412
|
+
let S = [], _ = 0, I = 0, k, W, B;
|
|
413
|
+
if (!h)
|
|
424
414
|
throw new Error("Could not create session_id");
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
415
|
+
(() => {
|
|
416
|
+
B = setInterval(() => {
|
|
417
|
+
u.getStats().then((p) => {
|
|
418
|
+
p.forEach((v) => {
|
|
419
|
+
var C, $;
|
|
420
|
+
if (v.type === "inbound-rtp" && v.kind === "video") {
|
|
421
|
+
if (I = S.length - 1, console.log("report ", I), v && S[I]) {
|
|
422
|
+
const U = v.bytesReceived, T = S[I].bytesReceived;
|
|
423
|
+
console.log("ontrack interval i <=", U, T);
|
|
424
|
+
let V = k;
|
|
425
|
+
k = U - T > 0;
|
|
426
|
+
let q;
|
|
427
|
+
if (V !== k) {
|
|
428
|
+
if ((C = a.onVideoStateChange) == null || C.call(a, k ? A.Start : A.Stop, {
|
|
429
|
+
isPlaying: k
|
|
430
|
+
}), k)
|
|
431
|
+
_ = S.length;
|
|
432
|
+
else {
|
|
433
|
+
const b = S.slice(_), K = _ === 0 ? void 0 : S[_ - 1];
|
|
434
|
+
q = le(b, K);
|
|
435
|
+
}
|
|
436
|
+
($ = a.onVideoStateChange) == null || $.call(a, A.Stop, q.sort((b, K) => K.packetsLost - b.packetsLost).slice(0, 5));
|
|
437
|
+
}
|
|
438
|
+
}
|
|
439
|
+
console.log(" ontrack interval isPlaying?", k), S.push(v);
|
|
440
|
+
}
|
|
441
|
+
});
|
|
442
|
+
});
|
|
443
|
+
}, 500);
|
|
444
|
+
})();
|
|
445
|
+
const D = () => {
|
|
446
|
+
clearInterval(W), console.log("clearInterval palying?");
|
|
447
|
+
};
|
|
448
|
+
u.onicecandidate = (i) => {
|
|
449
|
+
y("peerConnection.onicecandidate", i), i.candidate && i.candidate.sdpMid && i.candidate.sdpMLineIndex !== null && m(d, {
|
|
450
|
+
candidate: i.candidate.candidate,
|
|
451
|
+
sdpMid: i.candidate.sdpMid,
|
|
452
|
+
sdpMLineIndex: i.candidate.sdpMLineIndex
|
|
453
|
+
}, h);
|
|
454
|
+
}, u.oniceconnectionstatechange = () => {
|
|
455
|
+
var i;
|
|
456
|
+
y("peerConnection.oniceconnectionstatechange => " + u.iceConnectionState), (i = a.onConnectionStateChange) == null || i.call(a, u.iceConnectionState);
|
|
457
|
+
}, u.ontrack = (i) => {
|
|
458
|
+
var p;
|
|
459
|
+
y("peerConnection.ontrack", i), (p = a.onSrcObjectReady) == null || p.call(a, i.streams[0]);
|
|
460
|
+
}, E.onmessage = (i) => {
|
|
461
|
+
var p, v;
|
|
462
|
+
if (E.readyState === "open") {
|
|
463
|
+
const [C, $] = i.data.split(":");
|
|
464
|
+
C === P.StreamStarted ? console.log("StreamStarted", C, $) : C === P.StreamDone ? console.log("StreamDone") : C === P.StreamFailed ? ((p = a.onVideoStateChange) == null || p.call(a, A.Stop, {
|
|
465
|
+
event: C,
|
|
466
|
+
data: $
|
|
467
|
+
}), S = [], _ = 0, I = 0, k = !1) : (v = a.onMessage) == null || v.call(a, C, decodeURIComponent($));
|
|
458
468
|
}
|
|
459
|
-
}, await
|
|
460
|
-
const H = await
|
|
461
|
-
return
|
|
469
|
+
}, await u.setRemoteDescription(c), y("set remote description OK");
|
|
470
|
+
const H = await u.createAnswer();
|
|
471
|
+
return y("create answer OK"), await u.setLocalDescription(H), y("set local description OK"), await o(d, H, h), y("start connection OK"), {
|
|
462
472
|
/**
|
|
463
473
|
* Method to send request to server to get clip or talk depend on you payload
|
|
464
474
|
* @param payload
|
|
465
475
|
*/
|
|
466
|
-
speak(
|
|
467
|
-
return
|
|
476
|
+
speak(i) {
|
|
477
|
+
return l(d, h, i);
|
|
468
478
|
},
|
|
469
479
|
/**
|
|
470
480
|
* Method to close RTC connection
|
|
471
481
|
*/
|
|
472
482
|
async terminate() {
|
|
473
|
-
var
|
|
474
|
-
d && (
|
|
475
|
-
}), (
|
|
483
|
+
var i, p;
|
|
484
|
+
d && (u && (u.close(), u.oniceconnectionstatechange = null, u.onnegotiationneeded = null, u.onicecandidate = null, u.ontrack = null, clearInterval(W)), await g(d, h).catch((v) => {
|
|
485
|
+
}), (i = a.onConnectionStateChange) == null || i.call(a, "closed"), (p = a.onVideoStateChange) == null || p.call(a, A.Stop), D(), clearInterval(B));
|
|
476
486
|
},
|
|
477
487
|
/**
|
|
478
488
|
* Session identifier information, should be returned in the body of all streaming requests
|
|
479
489
|
*/
|
|
480
|
-
sessionId:
|
|
490
|
+
sessionId: h,
|
|
481
491
|
/**
|
|
482
492
|
* Id of current RTC stream
|
|
483
493
|
*/
|
|
@@ -487,28 +497,28 @@ async function ce(e, {
|
|
|
487
497
|
* @param eventName
|
|
488
498
|
* @param callback
|
|
489
499
|
*/
|
|
490
|
-
onCallback(
|
|
491
|
-
a[
|
|
500
|
+
onCallback(i, p) {
|
|
501
|
+
a[i] = p;
|
|
492
502
|
}
|
|
493
503
|
};
|
|
494
504
|
}
|
|
495
505
|
export {
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
506
|
+
re as ChatMode,
|
|
507
|
+
x as ChatProgress,
|
|
508
|
+
ie as DocumentType,
|
|
509
|
+
se as KnowledgeType,
|
|
510
|
+
ee as Providers,
|
|
511
|
+
ne as RateState,
|
|
512
|
+
j as SocketManager,
|
|
513
|
+
P as StreamEvents,
|
|
514
|
+
ae as Subject,
|
|
515
|
+
te as VoiceAccess,
|
|
516
|
+
pe as createAgentManager,
|
|
517
|
+
z as createAgentsApi,
|
|
518
|
+
L as createClient,
|
|
519
|
+
Q as createKnowledgeApi,
|
|
520
|
+
X as createRatingsApi,
|
|
521
|
+
ge as createStreamingManager,
|
|
522
|
+
fe as getAgent,
|
|
523
|
+
oe as getAgentStreamArgs
|
|
514
524
|
};
|
package/dist/index.umd.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
(function(s,w){typeof exports=="object"&&typeof module<"u"?w(exports):typeof define=="function"&&define.amd?define(["exports"],w):(s=typeof globalThis<"u"?globalThis:s||self,w(s.index={}))})(this,function(s){"use strict";const w="https://api.d-id.com",ee="wss://notifications.d-id.com";function b(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 $(e,i=w){const r=async(t,n)=>{const a=await fetch(i+(t!=null&&t.startsWith("/")?t:`/${t}`),{...n,headers:{...n==null?void 0:n.headers,Authorization:b(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,n){return r(t,{...n,method:"GET"})},post(t,n,a){return r(t,{...a,body:JSON.stringify(n),method:"POST"})},delete(t,n,a){return r(t,{...a,body:JSON.stringify(n),method:"DELETE"})},patch(t,n,a){return r(t,{...a,body:JSON.stringify(n),method:"PATCH"})}}}function K(e,i=w){const r=$(e,`${i}/agents`);return{create(t,n){return r.post("/",t,n)},getAgents(t,n){return r.get(`/${t?`?tag=${t}`:""}`,n).then(a=>a??[])},getById(t,n){return r.get(`/${t}`,n)},delete(t,n){return r.delete(`/${t}`,void 0,n)},update(t,n,a){return r.patch(`/${t}`,n,a)},newChat(t,n){return r.post(`/${t}/chat`,void 0,n)},chat(t,n,a,o){return r.post(`/${t}/chat/${n}`,a,o)}}}function H(e,i=w){const r=$(e,`${i}/knowledge`);return{createKnowledge(t,n){return r.post("/",t,n)},getKnowledgeBase(t){return r.get("/",t)},getKnowledge(t,n){return r.get(`/${t}`,n)},deleteKnowledge(t,n){return r.delete(`/${t}`,void 0,n)},createDocument(t,n,a){return r.post(`/${t}/documents`,n,a)},deleteDocument(t,n,a){return r.delete(`/${t}/documents/${n}`,void 0,a)},getDocuments(t,n){return r.get(`/${t}/documents`,n)},getDocument(t,n,a){return r.get(`/${t}/documents/${n}`,a)},getRecords(t,n,a){return r.get(`/${t}/documents/${n}/records`,a)},query(t,n,a){return r.post(`/${t}/query`,{query:n},a)}}}function U(e,i=w){const r=$(e,`${i}/chats/ratings`);return{create(t,n){return r.post("/",t,n)},getByKnowledge(t,n){return r.get(`/${t}`,n).then(a=>a??[])},update(t,n,a){return r.patch(`/${t}`,n,a)},delete(t,n){return r.delete(`/${t}`,n)}}}const te=e=>new Promise(i=>setTimeout(i,e));function ne(e){return new Promise((i,r)=>{const{callbacks:t,host:n,auth:a}=e,{onMessage:o=null,onOpen:g=null,onClose:f=null,onError:h=null}=t||{},u=new WebSocket(`${n}?authorization=${b(a)}`);console.log(`AUTH: ${n}?authorization=${b(a)}`),u.onmessage=o,u.onclose=f,u.onerror=l=>{console.log(l),console.log(`onerror: ${l}`),h==null||h(l),r(l)},u.onopen=l=>{g==null||g(l),console.log(`oneronopenror: ${u}`),i(u)}})}async function re(e){const{retries:i=1}=e;let r=null;for(let t=0;(r==null?void 0:r.readyState)!==WebSocket.OPEN;t++)try{r=await ne(e)}catch(n){if(t===i)throw n;await te(t*500)}return r}async function q(e,i,r){const t=r?[r]:[],n=await re({auth:e,host:i,callbacks:{onMessage:a=>{const o=JSON.parse(a.data);console.log("WS/",a.data),t.forEach(g=>g(o.event,o))}}});return{socket:n,terminate:()=>n.close(),subscribeToEvents:a=>t.push(a)}}var z=(e=>(e.Amazon="amazon",e.Microsoft="microsoft",e.Afflorithmics="afflorithmics",e.Elevenlabs="elevenlabs",e))(z||{}),B=(e=>(e.Public="public",e.Premium="premium",e.Private="private",e))(B||{}),M=(e=>(e.Start="START",e.Stop="STOP",e))(M||{}),k=(e=>(e.ChatAnswer="chat/answer",e.ChatPartial="chat/partial",e.StreamDone="stream/done",e.StreamStarted="stream/started",e))(k||{}),N=(e=>(e.Unrated="Unrated",e.Positive="Positive",e.Negative="Negative",e))(N||{}),J=(e=>(e.Functional="Functional",e.TextOnly="TextOnly",e.Maintenance="Maintenance",e))(J||{}),P=(e=>(e.Embed="embed",e.Query="query",e.Partial="partial",e.Answer="answer",e.Complete="done",e))(P||{}),D=(e=>(e.KnowledgeProcessing="knowledge/processing",e.KnowledgeIndexing="knowledge/indexing",e.KnowledgeFailed="knowledge/error",e.KnowledgeDone="knowledge/done",e))(D||{}),F=(e=>(e.Knowledge="knowledge",e.Document="document",e.Record="record",e))(F||{}),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||{}),A=(e=>(e.Clip="clip",e.Talk="talk",e))(A||{});function G(e){return e.presenter.type===A.Clip?{videoType:A.Clip,driver_id:e.presenter.driver_id,presenter_id:e.presenter.presenter_id}:{videoType:A.Talk,source_url:e.presenter.source_url}}function Q(e,i,r,t){return new Promise(async(n,a)=>{const o=await Y(G(e),{...i,callbacks:{...i.callbacks,onConnectionStateChange:async g=>{var f,h;if(g==="connected")try{t||(t=await r.newChat(e.id)),n({chat:t,streamingManager:o})}catch(u){console.error(u),a(new Error("Cannot create new chat"))}else g==="failed"&&a(new Error("Cannot create connection"));(h=(f=i.callbacks).onConnectionStateChange)==null||h.call(f,g)},onMessage:(g,f)=>{var h,u;g===k.ChatAnswer&&(console.log("ChatAnswer",g,f),(u=(h=i.callbacks).onChatEvents)==null||u.call(h,P.Answer,{content:f,event:P.Answer}))}}})})}function ae(e,i,r){return K(i,r||w).getById(e)}async function ie(e,i){const r=i.baseURL||w,t=i.wsURL||ee,n=new AbortController,a=K(i.auth,r),o=U(i.auth,r),g=H(i.auth,r);console.log(`AUTH: ${i.auth.clientKey},${i.auth.externalId}, ${t}`);const f=await a.getById(e),h=await q(i.auth,t,i.callbacks.onChatEvents);let{chat:u,streamingManager:l}=await Q(f,i,a);return{agent:f,async reconnectToChat(){const{streamingManager:d}=await Q(f,i,a,u);l=d},terminate(){return n.abort(),h.terminate(),l.terminate()},chatId:u.id,chat(d){return a.chat(e,u.id,{sessionId:l.sessionId,streamId:l.streamId,messages:d},{signal:n.signal})},rate(d,p){return p?o.update(p,d):o.create(d)},deleteRate(d){return o.delete(d)},speak(d){let p;return d.type==="text"?p={script:{type:"text",provider:d.provider,input:d.input,ssml:d.ssml||!1}}:d.type==="audio"&&(p={script:{type:"audio",audio_url:d.audio_url}}),l.speak(p)},getStarterMessages(){var d,p;return(d=f.knowledge)!=null&&d.id?g.getKnowledge((p=f.knowledge)==null?void 0:p.id).then(C=>(C==null?void 0:C.starter_message)||[]):Promise.resolve([])}}}function se(e,i){const r=$(e,i);return{createStream(t){return r.post("/clips/streams",{driver_id:t.driver_id,presenter_id:t.presenter_id,compatibility_mode:t.compatibility_mode})},startConnection(t,n,a){return r.post(`/clips/streams/${t}/sdp`,{session_id:a,answer:n})},addIceCandidate(t,n,a){return r.post(`/clips/streams/${t}/ice`,{session_id:a,...n})},sendStreamRequest(t,n,a){return r.post(`/clips/streams/${t}`,{session_id:n,...a})},close(t,n){return r.delete(`/clips/streams/${t}`,{session_id:n})}}}function oe(e,i){const r=$(e,i);return{createStream(t,n){return r.post("/talks/streams",{source_url:t.source_url,driver_url:t.driver_url,face:t.face,config:t.config},n)},startConnection(t,n,a,o){return r.post(`/talks/streams/${t}/sdp`,{session_id:a,answer:n},o)},addIceCandidate(t,n,a,o){return r.post(`/talks/streams/${t}/ice`,{session_id:a,...n},o)},sendStreamRequest(t,n,a,o){return r.post(`/talks/streams/${t}`,{session_id:n,...a},o)},close(t,n,a){return r.delete(`/talks/streams/${t}`,{session_id:n},a)}}}function ce(e,i){return e.map((r,t)=>t===0?i?{index:t,timestamp:r.timestamp,bytesReceived:r.bytesReceived-i.bytesReceived,packetsReceived:r.packetsReceived-i.packetsReceived,packetsLost:r.packetsLost-i.packetsLost,jitter:r.jitter,frameWidth:r.frameWidth,frameHeight:r.frameHeight,frameRate:r.frameRate}:{index:t,timestamp:r.timestamp,bytesReceived:r.bytesReceived,packetsReceived:r.packetsReceived,packetsLost:r.packetsLost,jitter:r.jitter,frameWidth:r.frameWidth,frameHeight:r.frameHeight,frameRate:r.frameRate}:{index:t,timestamp:r.timestamp,bytesReceived:r.bytesReceived-e[t-1].bytesReceived,packetsReceived:r.packetsReceived-e[t-1].packetsReceived,packetsLost:r.packetsLost-e[t-1].packetsLost,jitter:r.jitter,frameWidth:r.frameWidth,frameHeight:r.frameHeight,frameRate:r.frameRate})}let X=!1;const S=(e,i)=>X&&console.log(e,i),de=(window.RTCPeerConnection||window.webkitRTCPeerConnection||window.mozRTCPeerConnection).bind(window);async function Y(e,{debug:i=!1,callbacks:r,auth:t,baseURL:n=w}){X=i;const a={...r},{startConnection:o,sendStreamRequest:g,close:f,createStream:h,addIceCandidate:u}=e.videoType===A.Clip?se(t,n):oe(t,n),{id:l,offer:d,ice_servers:p,session_id:C}=await h(e),m=new de({iceServers:p}),Z=m.createDataChannel("JanusDataChannel"),y=[];let R=0,O;if(!C)throw new Error("Could not create session_id");m.onicecandidate=c=>{S("peerConnection.onicecandidate",c),c.candidate&&c.candidate.sdpMid&&c.candidate.sdpMLineIndex!==null&&u(l,{candidate:c.candidate.candidate,sdpMid:c.candidate.sdpMid,sdpMLineIndex:c.candidate.sdpMLineIndex},C)},m.oniceconnectionstatechange=()=>{var c;S("peerConnection.oniceconnectionstatechange => "+m.iceConnectionState),(c=a.onConnectionStateChange)==null||c.call(a,m.iceConnectionState)},m.ontrack=c=>{var v;S("peerConnection.ontrack",c),(v=a.onSrcObjectReady)==null||v.call(a,c.streams[0])},Z.onmessage=c=>{var v,L,j;if(Z.readyState==="open"){const[_,E]=c.data.split(":");if(_===k.StreamStarted)console.log("StreamStarted",_,E),R=y.length,O=setInterval(()=>{m.getStats().then(W=>{W.forEach(I=>{I.type==="inbound-rtp"&&I.kind==="video"&&y.push(I)})})},1e3),(v=a.onVideoStateChange)==null||v.call(a,M.Start);else if(_===k.StreamDone){console.log("StreamDone",_,E),clearInterval(O);const T=y.slice(R);if(T){const W=R===0?void 0:y[R-1],I=ce(T,W);R=y.length,(L=a.onVideoStateChange)==null||L.call(a,M.Stop,I.sort((le,ue)=>ue.packetsLost-le.packetsLost).slice(0,5))}}else(j=a.onMessage)==null||j.call(a,_,decodeURIComponent(E))}},await m.setRemoteDescription(d),S("set remote description OK");const x=await m.createAnswer();return S("create answer OK"),await m.setLocalDescription(x),S("set local description OK"),await o(l,x,C),S("start connection OK"),{speak(c){return g(l,C,c)},async terminate(){var c,v;l&&(m&&(m.close(),m.oniceconnectionstatechange=null,m.onnegotiationneeded=null,m.onicecandidate=null,m.ontrack=null),await f(l,C).catch(L=>{}),(c=a.onConnectionStateChange)==null||c.call(a,"closed"),(v=a.onVideoStateChange)==null||v.call(a,M.Stop))},sessionId:C,streamId:l,onCallback(c,v){a[c]=v}}}s.ChatMode=J,s.ChatProgress=P,s.DocumentType=V,s.KnowledgeType=F,s.Providers=z,s.RateState=N,s.SocketManager=q,s.StreamEvents=k,s.Subject=D,s.VoiceAccess=B,s.createAgentManager=ie,s.createAgentsApi=K,s.createClient=$,s.createKnowledgeApi=H,s.createRatingsApi=U,s.createStreamingManager=Y,s.getAgent=ae,s.getAgentStreamArgs=G,Object.defineProperty(s,Symbol.toStringTag,{value:"Module"})});
|
|
1
|
+
(function(s,p){typeof exports=="object"&&typeof module<"u"?p(exports):typeof define=="function"&&define.amd?define(["exports"],p):(s=typeof globalThis<"u"?globalThis:s||self,p(s.index={}))})(this,function(s){"use strict";const p="https://api.d-id.com",re="wss://notifications.d-id.com";function B(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 A(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:B(e),"Content-Type":"application/json"}});if(!r.ok){let c=await r.text().catch(()=>"Failed to fetch");throw new Error(c)}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 E(e,i=p){const n=A(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,c){return n.post(`/${t}/chat/${a}`,r,c)}}}function H(e,i=p){const n=A(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 U(e,i=p){const n=A(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 ie=e=>new Promise(i=>setTimeout(i,e));function se(e){return new Promise((i,n)=>{const{callbacks:t,host:a,auth:r}=e,{onMessage:c=null,onOpen:u=null,onClose:m=null,onError:w=null}=t||{},f=new WebSocket(`${a}?authorization=${B(r)}`);f.onmessage=c,f.onclose=m,f.onerror=l=>{console.log(l),w==null||w(l),n(l)},f.onopen=l=>{u==null||u(l),i(f)}})}async function oe(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 se(e)}catch(a){if(t===i)throw a;await ie(t*500)}return n}async function q(e,i,n){const t=n?[n]:[],a=await oe({auth:e,host:i,callbacks:{onMessage:r=>{const c=JSON.parse(r.data);t.forEach(u=>u(c.event,c))}}});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||{}),N=(e=>(e.Public="public",e.Premium="premium",e.Private="private",e))(N||{}),_=(e=>(e.Start="START",e.Stop="STOP",e))(_||{}),I=(e=>(e.ChatAnswer="chat/answer",e.ChatPartial="chat/partial",e.StreamDone="stream/done",e.StreamStarted="stream/started",e.StreamFailed="stream/error",e))(I||{}),z=(e=>(e.Unrated="Unrated",e.Positive="Positive",e.Negative="Negative",e))(z||{}),J=(e=>(e.Functional="Functional",e.TextOnly="TextOnly",e.Maintenance="Maintenance",e))(J||{}),K=(e=>(e.Embed="embed",e.Query="query",e.Partial="partial",e.Answer="answer",e.Complete="done",e))(K||{}),D=(e=>(e.KnowledgeProcessing="knowledge/processing",e.KnowledgeIndexing="knowledge/indexing",e.KnowledgeFailed="knowledge/error",e.KnowledgeDone="knowledge/done",e))(D||{}),V=(e=>(e.Knowledge="knowledge",e.Document="document",e.Record="record",e))(V||{}),G=(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||{}),M=(e=>(e.Clip="clip",e.Talk="talk",e))(M||{});function Q(e){return e.presenter.type===M.Clip?{videoType:M.Clip,driver_id:e.presenter.driver_id,presenter_id:e.presenter.presenter_id}:{videoType:M.Talk,source_url:e.presenter.source_url}}function X(e,i,n,t){return new Promise(async(a,r)=>{const c=await Z(Q(e),{...i,callbacks:{...i.callbacks,onConnectionStateChange:async u=>{var m,w;u==="connected"?(t||(t=await n.newChat(e.id)),a({chat:t,streamingManager:c})):u==="failed"&&r(new Error("Cannot create connection")),(w=(m=i.callbacks).onConnectionStateChange)==null||w.call(m,u)},onMessage:(u,m)=>{var w,f;u===I.ChatAnswer&&(console.log("ChatAnswer",u,m),(f=(w=i.callbacks).onChatEvents)==null||f.call(w,K.Answer,{content:m,event:K.Answer}))}}})})}function ce(e,i,n){return E(i,n||p).getById(e)}async function de(e,i){const n=i.baseURL||p,t=i.wsURL||re,a=new AbortController,r=E(i.auth,n),c=U(i.auth,n),u=H(i.auth,n),m=await r.getById(e),w=await q(i.auth,t,i.callbacks.onChatEvents);let{chat:f,streamingManager:l}=await X(m,i,r);return{agent:m,async reconnectToChat(){const{streamingManager:d}=await X(m,i,r,f);l=d},terminate(){return a.abort(),w.terminate(),l.terminate()},chatId:f.id,chat(d){return r.chat(e,f.id,{sessionId:l.sessionId,streamId:l.streamId,messages:d},{signal:a.signal})},rate(d,v){return v?c.update(v,d):c.create(d)},deleteRate(d){return c.delete(d)},speak(d){let v;return d.type==="text"?v={script:{type:"text",provider:d.provider,input:d.input,ssml:d.ssml||!1}}:d.type==="audio"&&(v={script:{type:"audio",audio_url:d.audio_url}}),l.speak(v)},getStarterMessages(){var d,v;return(d=m.knowledge)!=null&&d.id?u.getKnowledge((v=m.knowledge)==null?void 0:v.id).then(C=>(C==null?void 0:C.starter_message)||[]):Promise.resolve([])}}}function le(e,i){const n=A(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 ue(e,i){const n=A(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,c){return n.post(`/talks/streams/${t}/sdp`,{session_id:r,answer:a},c)},addIceCandidate(t,a,r,c){return n.post(`/talks/streams/${t}/ice`,{session_id:r,...a},c)},sendStreamRequest(t,a,r,c){return n.post(`/talks/streams/${t}`,{session_id:a,...r},c)},close(t,a,r){return n.delete(`/talks/streams/${t}`,{session_id:a},r)}}}function ge(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 Y=!1;const $=(e,i)=>Y&&console.log(e,i),me=(window.RTCPeerConnection||window.webkitRTCPeerConnection||window.mozRTCPeerConnection).bind(window);async function Z(e,{debug:i=!1,callbacks:n,auth:t,baseURL:a=p}){Y=i;const r={...n},{startConnection:c,sendStreamRequest:u,close:m,createStream:w,addIceCandidate:f}=e.videoType===M.Clip?le(t,a):ue(t,a),{id:l,offer:d,ice_servers:v,session_id:C}=await w(e),g=new me({iceServers:v}),x=g.createDataChannel("JanusDataChannel");let k=[],b=0,L=0,R,O,j;if(!C)throw new Error("Could not create session_id");(()=>{j=setInterval(()=>{g.getStats().then(h=>{h.forEach(S=>{var y,P;if(S.type==="inbound-rtp"&&S.kind==="video"){if(L=k.length-1,console.log("report ",L),S&&k[L]){const te=S.bytesReceived,ne=k[L].bytesReceived;console.log("ontrack interval i <=",te,ne);let we=R;R=te-ne>0;let ae;if(we!==R){if((y=r.onVideoStateChange)==null||y.call(r,R?_.Start:_.Stop,{isPlaying:R}),R)b=k.length;else{const T=k.slice(b),W=b===0?void 0:k[b-1];ae=ge(T,W)}(P=r.onVideoStateChange)==null||P.call(r,_.Stop,ae.sort((T,W)=>W.packetsLost-T.packetsLost).slice(0,5))}}console.log(" ontrack interval isPlaying?",R),k.push(S)}})})},500)})();const fe=()=>{clearInterval(O),console.log("clearInterval palying?")};g.onicecandidate=o=>{$("peerConnection.onicecandidate",o),o.candidate&&o.candidate.sdpMid&&o.candidate.sdpMLineIndex!==null&&f(l,{candidate:o.candidate.candidate,sdpMid:o.candidate.sdpMid,sdpMLineIndex:o.candidate.sdpMLineIndex},C)},g.oniceconnectionstatechange=()=>{var o;$("peerConnection.oniceconnectionstatechange => "+g.iceConnectionState),(o=r.onConnectionStateChange)==null||o.call(r,g.iceConnectionState)},g.ontrack=o=>{var h;$("peerConnection.ontrack",o),(h=r.onSrcObjectReady)==null||h.call(r,o.streams[0])},x.onmessage=o=>{var h,S;if(x.readyState==="open"){const[y,P]=o.data.split(":");y===I.StreamStarted?console.log("StreamStarted",y,P):y===I.StreamDone?console.log("StreamDone"):y===I.StreamFailed?((h=r.onVideoStateChange)==null||h.call(r,_.Stop,{event:y,data:P}),k=[],b=0,L=0,R=!1):(S=r.onMessage)==null||S.call(r,y,decodeURIComponent(P))}},await g.setRemoteDescription(d),$("set remote description OK");const ee=await g.createAnswer();return $("create answer OK"),await g.setLocalDescription(ee),$("set local description OK"),await c(l,ee,C),$("start connection OK"),{speak(o){return u(l,C,o)},async terminate(){var o,h;l&&(g&&(g.close(),g.oniceconnectionstatechange=null,g.onnegotiationneeded=null,g.onicecandidate=null,g.ontrack=null,clearInterval(O)),await m(l,C).catch(S=>{}),(o=r.onConnectionStateChange)==null||o.call(r,"closed"),(h=r.onVideoStateChange)==null||h.call(r,_.Stop),fe(),clearInterval(j))},sessionId:C,streamId:l,onCallback(o,h){r[o]=h}}}s.ChatMode=J,s.ChatProgress=K,s.DocumentType=G,s.KnowledgeType=V,s.Providers=F,s.RateState=z,s.SocketManager=q,s.StreamEvents=I,s.Subject=D,s.VoiceAccess=N,s.createAgentManager=de,s.createAgentsApi=E,s.createClient=A,s.createKnowledgeApi=H,s.createRatingsApi=U,s.createStreamingManager=Z,s.getAgent=ce,s.getAgentStreamArgs=Q,Object.defineProperty(s,Symbol.toStringTag,{value:"Module"})});
|
|
@@ -11,7 +11,8 @@ export declare enum StreamEvents {
|
|
|
11
11
|
ChatAnswer = "chat/answer",
|
|
12
12
|
ChatPartial = "chat/partial",
|
|
13
13
|
StreamDone = "stream/done",
|
|
14
|
-
StreamStarted = "stream/started"
|
|
14
|
+
StreamStarted = "stream/started",
|
|
15
|
+
StreamFailed = "stream/error"
|
|
15
16
|
}
|
|
16
17
|
export interface ManagerCallbacks {
|
|
17
18
|
onMessage?: (event: string, data: string) => void;
|