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