@d-id/client-sdk 1.0.19-beta.47 → 1.0.19-beta.48
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 +540 -360
- package/dist/index.umd.cjs +701 -1
- package/dist/src/types/entities/agents/agent.d.ts +1 -0
- package/package.json +2 -2
package/dist/index.js
CHANGED
|
@@ -1,517 +1,697 @@
|
|
|
1
|
-
const
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
const didApiUrl = "https://api-dev.d-id.com";
|
|
2
|
+
const didSocketApiUrl = "wss://notifications-dev.d-id.com";
|
|
3
|
+
function getExternalId() {
|
|
4
|
+
let key = window.localStorage.getItem("did_external_key_id");
|
|
5
|
+
if (!key) {
|
|
6
|
+
key = Math.random().toString(16).slice(2);
|
|
7
|
+
window.localStorage.setItem("did_external_key_id", key);
|
|
8
|
+
}
|
|
9
|
+
return key;
|
|
5
10
|
}
|
|
6
|
-
function
|
|
7
|
-
if (
|
|
8
|
-
return `Bearer ${
|
|
9
|
-
if (
|
|
10
|
-
return `Basic ${btoa(`${
|
|
11
|
-
if (
|
|
12
|
-
return `Client-Key ${
|
|
13
|
-
|
|
11
|
+
function getAuthHeader(auth) {
|
|
12
|
+
if (auth.type === "bearer") {
|
|
13
|
+
return `Bearer ${auth.token}`;
|
|
14
|
+
} else if (auth.type === "basic") {
|
|
15
|
+
return `Basic ${btoa(`${auth.username}:${auth.password}`)}`;
|
|
16
|
+
} else if (auth.type === "key") {
|
|
17
|
+
return `Client-Key ${auth.clientKey}.${getExternalId()}`;
|
|
18
|
+
} else {
|
|
19
|
+
throw new Error(`Unknown auth type: ${auth}`);
|
|
20
|
+
}
|
|
14
21
|
}
|
|
15
|
-
function
|
|
16
|
-
const
|
|
17
|
-
const
|
|
18
|
-
...
|
|
22
|
+
function createClient(auth, host = didApiUrl) {
|
|
23
|
+
const client = async (url, options) => {
|
|
24
|
+
const request = await fetch(host + ((url == null ? void 0 : url.startsWith("/")) ? url : `/${url}`), {
|
|
25
|
+
...options,
|
|
19
26
|
headers: {
|
|
20
|
-
...
|
|
21
|
-
Authorization:
|
|
27
|
+
...options == null ? void 0 : options.headers,
|
|
28
|
+
Authorization: getAuthHeader(auth),
|
|
22
29
|
"Content-Type": "application/json"
|
|
23
30
|
}
|
|
24
31
|
});
|
|
25
|
-
if (!
|
|
26
|
-
let
|
|
27
|
-
throw new Error(
|
|
32
|
+
if (!request.ok) {
|
|
33
|
+
let error = await request.text().catch(() => "Failed to fetch");
|
|
34
|
+
throw new Error(error);
|
|
28
35
|
}
|
|
29
|
-
return
|
|
36
|
+
return request.json();
|
|
30
37
|
};
|
|
31
38
|
return {
|
|
32
|
-
get(
|
|
33
|
-
return
|
|
34
|
-
...
|
|
39
|
+
get(url, options) {
|
|
40
|
+
return client(url, {
|
|
41
|
+
...options,
|
|
35
42
|
method: "GET"
|
|
36
43
|
});
|
|
37
44
|
},
|
|
38
|
-
post(
|
|
39
|
-
return
|
|
40
|
-
...
|
|
41
|
-
body: JSON.stringify(
|
|
45
|
+
post(url, body, options) {
|
|
46
|
+
return client(url, {
|
|
47
|
+
...options,
|
|
48
|
+
body: JSON.stringify(body),
|
|
42
49
|
method: "POST"
|
|
43
50
|
});
|
|
44
51
|
},
|
|
45
|
-
delete(
|
|
46
|
-
return
|
|
47
|
-
...
|
|
48
|
-
body: JSON.stringify(
|
|
52
|
+
delete(url, body, options) {
|
|
53
|
+
return client(url, {
|
|
54
|
+
...options,
|
|
55
|
+
body: JSON.stringify(body),
|
|
49
56
|
method: "DELETE"
|
|
50
57
|
});
|
|
51
58
|
},
|
|
52
|
-
patch(
|
|
53
|
-
return
|
|
54
|
-
...
|
|
55
|
-
body: JSON.stringify(
|
|
59
|
+
patch(url, body, options) {
|
|
60
|
+
return client(url, {
|
|
61
|
+
...options,
|
|
62
|
+
body: JSON.stringify(body),
|
|
56
63
|
method: "PATCH"
|
|
57
64
|
});
|
|
58
65
|
}
|
|
59
66
|
};
|
|
60
67
|
}
|
|
61
|
-
function
|
|
62
|
-
const
|
|
68
|
+
function createAgentsApi(auth, host = didApiUrl) {
|
|
69
|
+
const client = createClient(auth, `${host}/agents`);
|
|
63
70
|
return {
|
|
64
|
-
create(
|
|
65
|
-
return
|
|
71
|
+
create(payload, options) {
|
|
72
|
+
return client.post(`/`, payload, options);
|
|
66
73
|
},
|
|
67
|
-
getAgents(
|
|
68
|
-
return
|
|
74
|
+
getAgents(tag, options) {
|
|
75
|
+
return client.get(`/${tag ? `?tag=${tag}` : ""}`, options).then((agents) => agents ?? []);
|
|
69
76
|
},
|
|
70
|
-
getById(
|
|
71
|
-
return
|
|
77
|
+
getById(id, options) {
|
|
78
|
+
return client.get(`/${id}`, options);
|
|
72
79
|
},
|
|
73
|
-
delete(
|
|
74
|
-
return
|
|
80
|
+
delete(id, options) {
|
|
81
|
+
return client.delete(`/${id}`, void 0, options);
|
|
75
82
|
},
|
|
76
|
-
update(
|
|
77
|
-
return
|
|
83
|
+
update(id, payload, options) {
|
|
84
|
+
return client.patch(`/${id}`, payload, options);
|
|
78
85
|
},
|
|
79
|
-
newChat(
|
|
80
|
-
return
|
|
86
|
+
newChat(agentId, options) {
|
|
87
|
+
return client.post(`/${agentId}/chat`, void 0, options);
|
|
81
88
|
},
|
|
82
|
-
chat(
|
|
83
|
-
return
|
|
89
|
+
chat(agentId, chatId, payload, options) {
|
|
90
|
+
return client.post(`/${agentId}/chat/${chatId}`, payload, options);
|
|
84
91
|
}
|
|
85
92
|
};
|
|
86
93
|
}
|
|
87
|
-
function
|
|
88
|
-
const
|
|
94
|
+
function createKnowledgeApi(auth, host = didApiUrl) {
|
|
95
|
+
const client = createClient(auth, `${host}/knowledge`);
|
|
89
96
|
return {
|
|
90
|
-
createKnowledge(
|
|
91
|
-
return
|
|
97
|
+
createKnowledge(payload, options) {
|
|
98
|
+
return client.post(`/`, payload, options);
|
|
92
99
|
},
|
|
93
|
-
getKnowledgeBase(
|
|
94
|
-
return
|
|
100
|
+
getKnowledgeBase(options) {
|
|
101
|
+
return client.get(`/`, options);
|
|
95
102
|
},
|
|
96
|
-
getKnowledge(
|
|
97
|
-
return
|
|
103
|
+
getKnowledge(knowledgeId, options) {
|
|
104
|
+
return client.get(`/${knowledgeId}`, options);
|
|
98
105
|
},
|
|
99
|
-
deleteKnowledge(
|
|
100
|
-
return
|
|
106
|
+
deleteKnowledge(knowledgeId, options) {
|
|
107
|
+
return client.delete(`/${knowledgeId}`, void 0, options);
|
|
101
108
|
},
|
|
102
|
-
createDocument(
|
|
103
|
-
return
|
|
109
|
+
createDocument(knowledgeId, payload, options) {
|
|
110
|
+
return client.post(`/${knowledgeId}/documents`, payload, options);
|
|
104
111
|
},
|
|
105
|
-
deleteDocument(
|
|
106
|
-
return
|
|
112
|
+
deleteDocument(knowledgeId, documentId, options) {
|
|
113
|
+
return client.delete(`/${knowledgeId}/documents/${documentId}`, void 0, options);
|
|
107
114
|
},
|
|
108
|
-
getDocuments(
|
|
109
|
-
return
|
|
115
|
+
getDocuments(knowledgeId, options) {
|
|
116
|
+
return client.get(`/${knowledgeId}/documents`, options);
|
|
110
117
|
},
|
|
111
|
-
getDocument(
|
|
112
|
-
return
|
|
118
|
+
getDocument(knowledgeId, documentId, options) {
|
|
119
|
+
return client.get(`/${knowledgeId}/documents/${documentId}`, options);
|
|
113
120
|
},
|
|
114
|
-
getRecords(
|
|
115
|
-
return
|
|
121
|
+
getRecords(knowledgeId, documentId, options) {
|
|
122
|
+
return client.get(`/${knowledgeId}/documents/${documentId}/records`, options);
|
|
116
123
|
},
|
|
117
|
-
query(
|
|
118
|
-
return
|
|
119
|
-
query
|
|
120
|
-
},
|
|
124
|
+
query(knowledgeId, query, options) {
|
|
125
|
+
return client.post(`/${knowledgeId}/query`, {
|
|
126
|
+
query
|
|
127
|
+
}, options);
|
|
121
128
|
}
|
|
122
129
|
};
|
|
123
130
|
}
|
|
124
|
-
function
|
|
125
|
-
const
|
|
131
|
+
function createRatingsApi(auth, host = didApiUrl) {
|
|
132
|
+
const client = createClient(auth, `${host}/chats/ratings`);
|
|
126
133
|
return {
|
|
127
|
-
create(
|
|
128
|
-
return
|
|
134
|
+
create(payload, options) {
|
|
135
|
+
return client.post(`/`, payload, options);
|
|
129
136
|
},
|
|
130
|
-
getByKnowledge(
|
|
131
|
-
return
|
|
137
|
+
getByKnowledge(knowledgeId, options) {
|
|
138
|
+
return client.get(`/${knowledgeId}`, options).then((ratings) => ratings ?? []);
|
|
132
139
|
},
|
|
133
|
-
update(
|
|
134
|
-
return
|
|
140
|
+
update(id, payload, options) {
|
|
141
|
+
return client.patch(`/${id}`, payload, options);
|
|
135
142
|
},
|
|
136
|
-
delete(
|
|
137
|
-
return
|
|
143
|
+
delete(id, options) {
|
|
144
|
+
return client.delete(`/${id}`, options);
|
|
138
145
|
}
|
|
139
146
|
};
|
|
140
147
|
}
|
|
141
|
-
const
|
|
142
|
-
function
|
|
143
|
-
return new Promise((
|
|
148
|
+
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
|
|
149
|
+
function connect(options) {
|
|
150
|
+
return new Promise((resolve, reject) => {
|
|
144
151
|
const {
|
|
145
|
-
callbacks
|
|
146
|
-
host
|
|
147
|
-
auth
|
|
148
|
-
} =
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
152
|
+
callbacks,
|
|
153
|
+
host,
|
|
154
|
+
auth
|
|
155
|
+
} = options;
|
|
156
|
+
const {
|
|
157
|
+
onMessage = null,
|
|
158
|
+
onOpen = null,
|
|
159
|
+
onClose = null,
|
|
160
|
+
onError = null
|
|
161
|
+
} = callbacks || {};
|
|
162
|
+
const socket = new WebSocket(`${host}?authorization=${getAuthHeader(auth)}`);
|
|
163
|
+
socket.onmessage = onMessage;
|
|
164
|
+
socket.onclose = onClose;
|
|
165
|
+
socket.onerror = (e) => {
|
|
166
|
+
console.error(e);
|
|
167
|
+
onError == null ? void 0 : onError(e);
|
|
168
|
+
reject(e);
|
|
169
|
+
};
|
|
170
|
+
socket.onopen = (e) => {
|
|
171
|
+
onOpen == null ? void 0 : onOpen(e);
|
|
172
|
+
resolve(socket);
|
|
158
173
|
};
|
|
159
174
|
});
|
|
160
175
|
}
|
|
161
|
-
async function
|
|
176
|
+
async function connectWithRetries(options) {
|
|
162
177
|
const {
|
|
163
|
-
retries
|
|
164
|
-
} =
|
|
165
|
-
let
|
|
166
|
-
for (let
|
|
178
|
+
retries = 1
|
|
179
|
+
} = options;
|
|
180
|
+
let socket = null;
|
|
181
|
+
for (let attempt = 0; (socket == null ? void 0 : socket.readyState) !== WebSocket.OPEN; attempt++) {
|
|
167
182
|
try {
|
|
168
|
-
|
|
169
|
-
} catch (
|
|
170
|
-
if (
|
|
171
|
-
throw
|
|
172
|
-
|
|
183
|
+
socket = await connect(options);
|
|
184
|
+
} catch (e) {
|
|
185
|
+
if (attempt === retries) {
|
|
186
|
+
throw e;
|
|
187
|
+
}
|
|
188
|
+
await sleep(attempt * 500);
|
|
173
189
|
}
|
|
174
|
-
|
|
190
|
+
}
|
|
191
|
+
return socket;
|
|
175
192
|
}
|
|
176
|
-
async function
|
|
177
|
-
const
|
|
178
|
-
|
|
179
|
-
|
|
193
|
+
async function SocketManager(auth, host, onMessage) {
|
|
194
|
+
const messageCallbacks = onMessage ? [onMessage] : [];
|
|
195
|
+
const socket = await connectWithRetries({
|
|
196
|
+
auth,
|
|
197
|
+
host,
|
|
180
198
|
callbacks: {
|
|
181
|
-
onMessage: (
|
|
182
|
-
const
|
|
183
|
-
|
|
199
|
+
onMessage: (event) => {
|
|
200
|
+
const parsedData = JSON.parse(event.data);
|
|
201
|
+
messageCallbacks.forEach((callback) => callback(parsedData.event, parsedData));
|
|
184
202
|
}
|
|
185
203
|
}
|
|
186
204
|
});
|
|
187
205
|
return {
|
|
188
|
-
socket
|
|
189
|
-
terminate: () =>
|
|
190
|
-
subscribeToEvents: (
|
|
206
|
+
socket,
|
|
207
|
+
terminate: () => socket.close(),
|
|
208
|
+
subscribeToEvents: (callback) => messageCallbacks.push(callback)
|
|
191
209
|
};
|
|
192
210
|
}
|
|
193
|
-
var
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
211
|
+
var Providers = /* @__PURE__ */ ((Providers2) => {
|
|
212
|
+
Providers2["Amazon"] = "amazon";
|
|
213
|
+
Providers2["Microsoft"] = "microsoft";
|
|
214
|
+
Providers2["Afflorithmics"] = "afflorithmics";
|
|
215
|
+
Providers2["Elevenlabs"] = "elevenlabs";
|
|
216
|
+
return Providers2;
|
|
217
|
+
})(Providers || {});
|
|
218
|
+
var VoiceAccess = /* @__PURE__ */ ((VoiceAccess2) => {
|
|
219
|
+
VoiceAccess2["Public"] = "public";
|
|
220
|
+
VoiceAccess2["Premium"] = "premium";
|
|
221
|
+
VoiceAccess2["Private"] = "private";
|
|
222
|
+
return VoiceAccess2;
|
|
223
|
+
})(VoiceAccess || {});
|
|
224
|
+
var StreamingState = /* @__PURE__ */ ((StreamingState2) => {
|
|
225
|
+
StreamingState2["Start"] = "START";
|
|
226
|
+
StreamingState2["Stop"] = "STOP";
|
|
227
|
+
return StreamingState2;
|
|
228
|
+
})(StreamingState || {});
|
|
229
|
+
var StreamEvents = /* @__PURE__ */ ((StreamEvents2) => {
|
|
230
|
+
StreamEvents2["ChatAnswer"] = "chat/answer";
|
|
231
|
+
StreamEvents2["ChatPartial"] = "chat/partial";
|
|
232
|
+
StreamEvents2["StreamDone"] = "stream/done";
|
|
233
|
+
StreamEvents2["StreamStarted"] = "stream/started";
|
|
234
|
+
StreamEvents2["StreamFailed"] = "stream/error";
|
|
235
|
+
return StreamEvents2;
|
|
236
|
+
})(StreamEvents || {});
|
|
237
|
+
var RateState = /* @__PURE__ */ ((RateState2) => {
|
|
238
|
+
RateState2["Unrated"] = "Unrated";
|
|
239
|
+
RateState2["Positive"] = "Positive";
|
|
240
|
+
RateState2["Negative"] = "Negative";
|
|
241
|
+
return RateState2;
|
|
242
|
+
})(RateState || {});
|
|
243
|
+
var ChatMode = /* @__PURE__ */ ((ChatMode2) => {
|
|
244
|
+
ChatMode2["Functional"] = "Functional";
|
|
245
|
+
ChatMode2["TextOnly"] = "TextOnly";
|
|
246
|
+
ChatMode2["Maintenance"] = "Maintenance";
|
|
247
|
+
return ChatMode2;
|
|
248
|
+
})(ChatMode || {});
|
|
249
|
+
var ChatProgress = /* @__PURE__ */ ((ChatProgress2) => {
|
|
250
|
+
ChatProgress2["Embed"] = "embed";
|
|
251
|
+
ChatProgress2["Query"] = "query";
|
|
252
|
+
ChatProgress2["Partial"] = "partial";
|
|
253
|
+
ChatProgress2["Answer"] = "answer";
|
|
254
|
+
ChatProgress2["Complete"] = "done";
|
|
255
|
+
return ChatProgress2;
|
|
256
|
+
})(ChatProgress || {});
|
|
257
|
+
var Subject = /* @__PURE__ */ ((Subject2) => {
|
|
258
|
+
Subject2["KnowledgeProcessing"] = "knowledge/processing";
|
|
259
|
+
Subject2["KnowledgeIndexing"] = "knowledge/indexing";
|
|
260
|
+
Subject2["KnowledgeFailed"] = "knowledge/error";
|
|
261
|
+
Subject2["KnowledgeDone"] = "knowledge/done";
|
|
262
|
+
return Subject2;
|
|
263
|
+
})(Subject || {});
|
|
264
|
+
var KnowledgeType = /* @__PURE__ */ ((KnowledgeType2) => {
|
|
265
|
+
KnowledgeType2["Knowledge"] = "knowledge";
|
|
266
|
+
KnowledgeType2["Document"] = "document";
|
|
267
|
+
KnowledgeType2["Record"] = "record";
|
|
268
|
+
return KnowledgeType2;
|
|
269
|
+
})(KnowledgeType || {});
|
|
270
|
+
var DocumentType = /* @__PURE__ */ ((DocumentType2) => {
|
|
271
|
+
DocumentType2["Pdf"] = "pdf";
|
|
272
|
+
DocumentType2["Text"] = "text";
|
|
273
|
+
DocumentType2["Html"] = "html";
|
|
274
|
+
DocumentType2["Word"] = "word";
|
|
275
|
+
DocumentType2["Json"] = "json";
|
|
276
|
+
DocumentType2["Markdown"] = "markdown";
|
|
277
|
+
DocumentType2["Csv"] = "csv";
|
|
278
|
+
DocumentType2["Excel"] = "excel";
|
|
279
|
+
DocumentType2["Powerpoint"] = "powerpoint";
|
|
280
|
+
DocumentType2["Archive"] = "archive";
|
|
281
|
+
DocumentType2["Image"] = "image";
|
|
282
|
+
DocumentType2["Audio"] = "audio";
|
|
283
|
+
DocumentType2["Video"] = "video";
|
|
284
|
+
return DocumentType2;
|
|
285
|
+
})(DocumentType || {});
|
|
286
|
+
var VideoType = /* @__PURE__ */ ((VideoType2) => {
|
|
287
|
+
VideoType2["Clip"] = "clip";
|
|
288
|
+
VideoType2["Talk"] = "talk";
|
|
289
|
+
return VideoType2;
|
|
290
|
+
})(VideoType || {});
|
|
291
|
+
function getAgentStreamArgs(agent) {
|
|
292
|
+
if (agent.presenter.type === VideoType.Clip) {
|
|
293
|
+
return {
|
|
294
|
+
videoType: VideoType.Clip,
|
|
295
|
+
driver_id: agent.presenter.driver_id,
|
|
296
|
+
presenter_id: agent.presenter.presenter_id
|
|
297
|
+
};
|
|
298
|
+
}
|
|
299
|
+
return {
|
|
300
|
+
videoType: VideoType.Talk,
|
|
301
|
+
source_url: agent.presenter.source_url
|
|
202
302
|
};
|
|
203
303
|
}
|
|
204
|
-
function
|
|
205
|
-
return new Promise(async (
|
|
206
|
-
const
|
|
207
|
-
...
|
|
304
|
+
function initializeStreamAndChat(agent, options, agentsApi, chat) {
|
|
305
|
+
return new Promise(async (resolve, reject) => {
|
|
306
|
+
const streamingManager = await createStreamingManager(getAgentStreamArgs(agent), {
|
|
307
|
+
...options,
|
|
208
308
|
callbacks: {
|
|
209
|
-
...
|
|
210
|
-
onConnectionStateChange: async (
|
|
211
|
-
var
|
|
212
|
-
if (
|
|
309
|
+
...options.callbacks,
|
|
310
|
+
onConnectionStateChange: async (state) => {
|
|
311
|
+
var _a, _b;
|
|
312
|
+
if (state === "connected") {
|
|
213
313
|
try {
|
|
214
|
-
|
|
215
|
-
chat
|
|
216
|
-
|
|
314
|
+
if (!chat) {
|
|
315
|
+
chat = await agentsApi.newChat(agent.id);
|
|
316
|
+
}
|
|
317
|
+
resolve({
|
|
318
|
+
chat,
|
|
319
|
+
streamingManager
|
|
217
320
|
});
|
|
218
|
-
} catch (
|
|
219
|
-
console.error(
|
|
321
|
+
} catch (error) {
|
|
322
|
+
console.error(error);
|
|
323
|
+
reject(new Error("Cannot create new chat"));
|
|
220
324
|
}
|
|
221
|
-
else
|
|
222
|
-
|
|
223
|
-
|
|
325
|
+
} else if (state === "failed") {
|
|
326
|
+
reject(new Error("Cannot create connection"));
|
|
327
|
+
}
|
|
328
|
+
(_b = (_a = options.callbacks).onConnectionStateChange) == null ? void 0 : _b.call(_a, state);
|
|
224
329
|
}
|
|
225
330
|
}
|
|
226
331
|
});
|
|
227
332
|
});
|
|
228
333
|
}
|
|
229
|
-
function
|
|
230
|
-
|
|
334
|
+
function getAgent(agentId, auth, baseURL) {
|
|
335
|
+
const url = baseURL || didApiUrl;
|
|
336
|
+
const agentsApi = createAgentsApi(auth, url);
|
|
337
|
+
return agentsApi.getById(agentId);
|
|
231
338
|
}
|
|
232
|
-
async function
|
|
233
|
-
var
|
|
234
|
-
const
|
|
235
|
-
|
|
236
|
-
const
|
|
339
|
+
async function createAgentManager(agent, options) {
|
|
340
|
+
var _a, _b;
|
|
341
|
+
const baseURL = options.baseURL || didApiUrl;
|
|
342
|
+
const wsURL = options.wsURL || didSocketApiUrl;
|
|
343
|
+
const abortController = new AbortController();
|
|
344
|
+
const agentsApi = createAgentsApi(options.auth, baseURL);
|
|
345
|
+
const ratingsAPI = createRatingsApi(options.auth, baseURL);
|
|
346
|
+
const knowledgeApi = createKnowledgeApi(options.auth, baseURL);
|
|
347
|
+
const agentInstance = typeof agent === "string" ? await agentsApi.getById(agent) : agent;
|
|
348
|
+
(_b = (_a = options.callbacks) == null ? void 0 : _a.onAgentReady) == null ? void 0 : _b.call(_a, agentInstance);
|
|
349
|
+
const socketManager = await SocketManager(options.auth, wsURL, options.callbacks.onChatEvents);
|
|
237
350
|
let {
|
|
238
|
-
chat
|
|
239
|
-
streamingManager
|
|
240
|
-
} = await
|
|
351
|
+
chat,
|
|
352
|
+
streamingManager
|
|
353
|
+
} = await initializeStreamAndChat(agentInstance, options, agentsApi);
|
|
241
354
|
return {
|
|
242
|
-
agent:
|
|
243
|
-
chatId:
|
|
355
|
+
agent: agentInstance,
|
|
356
|
+
chatId: chat.id,
|
|
244
357
|
async reconnectToChat() {
|
|
245
358
|
const {
|
|
246
|
-
streamingManager:
|
|
247
|
-
} = await
|
|
248
|
-
|
|
359
|
+
streamingManager: newStreamingManager
|
|
360
|
+
} = await initializeStreamAndChat(agentInstance, options, agentsApi, chat);
|
|
361
|
+
streamingManager = newStreamingManager;
|
|
249
362
|
},
|
|
250
363
|
terminate() {
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
364
|
+
abortController.abort();
|
|
365
|
+
socketManager.terminate();
|
|
366
|
+
return streamingManager.terminate();
|
|
367
|
+
},
|
|
368
|
+
chat(messages) {
|
|
369
|
+
return agentsApi.chat(agentInstance.id, chat.id, {
|
|
370
|
+
sessionId: streamingManager.sessionId,
|
|
371
|
+
streamId: streamingManager.streamId,
|
|
372
|
+
messages
|
|
258
373
|
}, {
|
|
259
|
-
signal:
|
|
374
|
+
signal: abortController.signal
|
|
260
375
|
});
|
|
261
376
|
},
|
|
262
|
-
rate(
|
|
263
|
-
|
|
377
|
+
rate(payload, id) {
|
|
378
|
+
if (id) {
|
|
379
|
+
return ratingsAPI.update(id, payload);
|
|
380
|
+
}
|
|
381
|
+
return ratingsAPI.create(payload);
|
|
264
382
|
},
|
|
265
|
-
deleteRate(
|
|
266
|
-
return
|
|
383
|
+
deleteRate(id) {
|
|
384
|
+
return ratingsAPI.delete(id);
|
|
267
385
|
},
|
|
268
|
-
speak(
|
|
269
|
-
function
|
|
270
|
-
if (
|
|
386
|
+
speak(payload) {
|
|
387
|
+
function getScript() {
|
|
388
|
+
if (payload.type === "text") {
|
|
271
389
|
return {
|
|
272
390
|
type: "text",
|
|
273
|
-
provider:
|
|
274
|
-
input:
|
|
275
|
-
ssml:
|
|
391
|
+
provider: payload.provider,
|
|
392
|
+
input: payload.input,
|
|
393
|
+
ssml: payload.ssml || false
|
|
276
394
|
};
|
|
277
|
-
if (
|
|
395
|
+
} else if (payload.type === "audio") {
|
|
278
396
|
return {
|
|
279
397
|
type: "audio",
|
|
280
|
-
audio_url:
|
|
398
|
+
audio_url: payload.audio_url
|
|
281
399
|
};
|
|
400
|
+
}
|
|
282
401
|
throw new Error("Invalid payload");
|
|
283
402
|
}
|
|
284
|
-
return
|
|
285
|
-
script:
|
|
403
|
+
return streamingManager.speak({
|
|
404
|
+
script: getScript()
|
|
286
405
|
});
|
|
287
406
|
},
|
|
288
407
|
async getStarterMessages() {
|
|
289
|
-
var
|
|
290
|
-
|
|
408
|
+
var _a2;
|
|
409
|
+
if (!((_a2 = agentInstance.knowledge) == null ? void 0 : _a2.id)) {
|
|
410
|
+
return [];
|
|
411
|
+
}
|
|
412
|
+
return knowledgeApi.getKnowledge(agentInstance.knowledge.id).then((knowledge) => (knowledge == null ? void 0 : knowledge.starter_message) || []);
|
|
291
413
|
}
|
|
292
414
|
};
|
|
293
415
|
}
|
|
294
|
-
function
|
|
295
|
-
const
|
|
416
|
+
function createApi$1(auth, host) {
|
|
417
|
+
const client = createClient(auth, host);
|
|
296
418
|
return {
|
|
297
|
-
createStream(
|
|
298
|
-
return
|
|
299
|
-
driver_id:
|
|
300
|
-
presenter_id:
|
|
301
|
-
compatibility_mode:
|
|
419
|
+
createStream(options) {
|
|
420
|
+
return client.post("/clips/streams", {
|
|
421
|
+
driver_id: options.driver_id,
|
|
422
|
+
presenter_id: options.presenter_id,
|
|
423
|
+
compatibility_mode: options.compatibility_mode
|
|
302
424
|
});
|
|
303
425
|
},
|
|
304
|
-
startConnection(
|
|
305
|
-
return
|
|
306
|
-
session_id:
|
|
307
|
-
answer
|
|
426
|
+
startConnection(streamId, answer, sessionId) {
|
|
427
|
+
return client.post(`/clips/streams/${streamId}/sdp`, {
|
|
428
|
+
session_id: sessionId,
|
|
429
|
+
answer
|
|
308
430
|
});
|
|
309
431
|
},
|
|
310
|
-
addIceCandidate(
|
|
311
|
-
return
|
|
312
|
-
session_id:
|
|
313
|
-
...
|
|
432
|
+
addIceCandidate(streamId, candidate, sessionId) {
|
|
433
|
+
return client.post(`/clips/streams/${streamId}/ice`, {
|
|
434
|
+
session_id: sessionId,
|
|
435
|
+
...candidate
|
|
314
436
|
});
|
|
315
437
|
},
|
|
316
|
-
sendStreamRequest(
|
|
317
|
-
return
|
|
318
|
-
session_id:
|
|
319
|
-
...
|
|
438
|
+
sendStreamRequest(streamId, sessionId, payload) {
|
|
439
|
+
return client.post(`/clips/streams/${streamId}`, {
|
|
440
|
+
session_id: sessionId,
|
|
441
|
+
...payload
|
|
320
442
|
});
|
|
321
443
|
},
|
|
322
|
-
close(
|
|
323
|
-
return
|
|
324
|
-
session_id:
|
|
444
|
+
close(streamId, sessionId) {
|
|
445
|
+
return client.delete(`/clips/streams/${streamId}`, {
|
|
446
|
+
session_id: sessionId
|
|
325
447
|
});
|
|
326
448
|
}
|
|
327
449
|
};
|
|
328
450
|
}
|
|
329
|
-
function
|
|
330
|
-
const
|
|
451
|
+
function createApi(auth, host) {
|
|
452
|
+
const client = createClient(auth, host);
|
|
331
453
|
return {
|
|
332
|
-
createStream(
|
|
333
|
-
return
|
|
334
|
-
source_url:
|
|
335
|
-
driver_url:
|
|
336
|
-
face:
|
|
337
|
-
config:
|
|
338
|
-
},
|
|
339
|
-
},
|
|
340
|
-
startConnection(
|
|
341
|
-
return
|
|
342
|
-
session_id:
|
|
343
|
-
answer
|
|
344
|
-
},
|
|
345
|
-
},
|
|
346
|
-
addIceCandidate(
|
|
347
|
-
return
|
|
348
|
-
session_id:
|
|
349
|
-
...
|
|
350
|
-
},
|
|
351
|
-
},
|
|
352
|
-
sendStreamRequest(
|
|
353
|
-
return
|
|
354
|
-
session_id:
|
|
355
|
-
...
|
|
356
|
-
},
|
|
357
|
-
},
|
|
358
|
-
close(
|
|
359
|
-
return
|
|
360
|
-
session_id:
|
|
361
|
-
},
|
|
454
|
+
createStream(streamOptions, options) {
|
|
455
|
+
return client.post("/talks/streams", {
|
|
456
|
+
source_url: streamOptions.source_url,
|
|
457
|
+
driver_url: streamOptions.driver_url,
|
|
458
|
+
face: streamOptions.face,
|
|
459
|
+
config: streamOptions.config
|
|
460
|
+
}, options);
|
|
461
|
+
},
|
|
462
|
+
startConnection(streamId, answer, sessionId, options) {
|
|
463
|
+
return client.post(`/talks/streams/${streamId}/sdp`, {
|
|
464
|
+
session_id: sessionId,
|
|
465
|
+
answer
|
|
466
|
+
}, options);
|
|
467
|
+
},
|
|
468
|
+
addIceCandidate(streamId, candidate, sessionId, options) {
|
|
469
|
+
return client.post(`/talks/streams/${streamId}/ice`, {
|
|
470
|
+
session_id: sessionId,
|
|
471
|
+
...candidate
|
|
472
|
+
}, options);
|
|
473
|
+
},
|
|
474
|
+
sendStreamRequest(streamId, sessionId, payload, options) {
|
|
475
|
+
return client.post(`/talks/streams/${streamId}`, {
|
|
476
|
+
session_id: sessionId,
|
|
477
|
+
...payload
|
|
478
|
+
}, options);
|
|
479
|
+
},
|
|
480
|
+
close(streamId, sessionId, options) {
|
|
481
|
+
return client.delete(`/talks/streams/${streamId}`, {
|
|
482
|
+
session_id: sessionId
|
|
483
|
+
}, options);
|
|
362
484
|
}
|
|
363
485
|
};
|
|
364
486
|
}
|
|
365
|
-
function
|
|
366
|
-
return
|
|
367
|
-
index
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
487
|
+
function createVideoStatsReport(stats, previousStats) {
|
|
488
|
+
return stats.map((report, index) => {
|
|
489
|
+
if (index === 0) {
|
|
490
|
+
if (!previousStats) {
|
|
491
|
+
return {
|
|
492
|
+
index,
|
|
493
|
+
timestamp: report.timestamp,
|
|
494
|
+
bytesReceived: report.bytesReceived,
|
|
495
|
+
packetsReceived: report.packetsReceived,
|
|
496
|
+
packetsLost: report.packetsLost,
|
|
497
|
+
jitter: report.jitter,
|
|
498
|
+
frameWidth: report.frameWidth,
|
|
499
|
+
frameHeight: report.frameHeight,
|
|
500
|
+
framesPerSecond: report.framesPerSecond
|
|
501
|
+
};
|
|
502
|
+
}
|
|
503
|
+
return {
|
|
504
|
+
index,
|
|
505
|
+
timestamp: report.timestamp,
|
|
506
|
+
bytesReceived: report.bytesReceived - previousStats.bytesReceived,
|
|
507
|
+
packetsReceived: report.packetsReceived - previousStats.packetsReceived,
|
|
508
|
+
packetsLost: report.packetsLost - previousStats.packetsLost,
|
|
509
|
+
jitter: report.jitter,
|
|
510
|
+
frameWidth: report.frameWidth,
|
|
511
|
+
frameHeight: report.frameHeight,
|
|
512
|
+
framesPerSecond: report.framesPerSecond
|
|
513
|
+
};
|
|
514
|
+
}
|
|
515
|
+
return {
|
|
516
|
+
index,
|
|
517
|
+
timestamp: report.timestamp,
|
|
518
|
+
bytesReceived: report.bytesReceived - stats[index - 1].bytesReceived,
|
|
519
|
+
packetsReceived: report.packetsReceived - stats[index - 1].packetsReceived,
|
|
520
|
+
packetsLost: report.packetsLost - stats[index - 1].packetsLost,
|
|
521
|
+
jitter: report.jitter,
|
|
522
|
+
frameWidth: report.frameWidth,
|
|
523
|
+
frameHeight: report.frameHeight,
|
|
524
|
+
framesPerSecond: report.framesPerSecond
|
|
525
|
+
};
|
|
396
526
|
});
|
|
397
527
|
}
|
|
398
|
-
let
|
|
399
|
-
const
|
|
400
|
-
|
|
401
|
-
|
|
528
|
+
let _debug = false;
|
|
529
|
+
const log = (message, extra) => _debug && console.log(message, extra);
|
|
530
|
+
const actualRTCPC = (window.RTCPeerConnection || window.webkitRTCPeerConnection || window.mozRTCPeerConnection).bind(window);
|
|
531
|
+
function pollStats(peerConnection, onVideoStateChange) {
|
|
532
|
+
let videoStats = [];
|
|
533
|
+
let videoStatsStartIndex = 0;
|
|
534
|
+
let videoStatsLastIndex = 0;
|
|
535
|
+
let isPlaying;
|
|
402
536
|
return setInterval(() => {
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
537
|
+
const stats = peerConnection.getStats();
|
|
538
|
+
stats.then((result) => {
|
|
539
|
+
result.forEach((report) => {
|
|
540
|
+
if (report.type === "inbound-rtp" && report.kind === "video") {
|
|
541
|
+
videoStatsLastIndex = videoStats.length - 1;
|
|
542
|
+
if (report && videoStats[videoStatsLastIndex]) {
|
|
543
|
+
const currBytesReceived = report.bytesReceived;
|
|
544
|
+
const lastBytesReceived = videoStats[videoStatsLastIndex].bytesReceived;
|
|
545
|
+
let prevPlaying = isPlaying;
|
|
546
|
+
isPlaying = currBytesReceived - lastBytesReceived > 0;
|
|
547
|
+
let videoStatsReport;
|
|
548
|
+
if (prevPlaying !== isPlaying) {
|
|
549
|
+
if (isPlaying) {
|
|
550
|
+
videoStatsStartIndex = videoStats.length;
|
|
551
|
+
} else {
|
|
552
|
+
const stats2 = videoStats.slice(videoStatsStartIndex);
|
|
553
|
+
const previousStats = videoStatsStartIndex === 0 ? void 0 : videoStats[videoStatsStartIndex - 1];
|
|
554
|
+
videoStatsReport = createVideoStatsReport(stats2, previousStats);
|
|
555
|
+
videoStatsReport = videoStatsReport.sort((a, b) => b.packetsLost - a.packetsLost).slice(0, 5);
|
|
417
556
|
}
|
|
418
|
-
|
|
557
|
+
onVideoStateChange == null ? void 0 : onVideoStateChange(isPlaying ? StreamingState.Start : StreamingState.Stop, videoStatsReport);
|
|
419
558
|
}
|
|
420
559
|
}
|
|
421
|
-
|
|
560
|
+
videoStats.push(report);
|
|
422
561
|
}
|
|
423
562
|
});
|
|
424
563
|
});
|
|
425
564
|
}, 500);
|
|
426
565
|
}
|
|
427
|
-
async function
|
|
428
|
-
debug
|
|
429
|
-
callbacks
|
|
430
|
-
auth
|
|
431
|
-
baseURL
|
|
566
|
+
async function createStreamingManager(agent, {
|
|
567
|
+
debug = false,
|
|
568
|
+
callbacks,
|
|
569
|
+
auth,
|
|
570
|
+
baseURL = didApiUrl
|
|
432
571
|
}) {
|
|
433
|
-
|
|
572
|
+
_debug = debug;
|
|
573
|
+
const {
|
|
574
|
+
startConnection,
|
|
575
|
+
sendStreamRequest,
|
|
576
|
+
close,
|
|
577
|
+
createStream,
|
|
578
|
+
addIceCandidate
|
|
579
|
+
} = agent.videoType === VideoType.Clip ? createApi$1(auth, baseURL) : createApi(auth, baseURL);
|
|
434
580
|
const {
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
} = await c(e), s = new V({
|
|
446
|
-
iceServers: p
|
|
447
|
-
}), g = s.createDataChannel("JanusDataChannel");
|
|
448
|
-
if (!w)
|
|
581
|
+
id: streamIdFromServer,
|
|
582
|
+
offer,
|
|
583
|
+
ice_servers,
|
|
584
|
+
session_id
|
|
585
|
+
} = await createStream(agent);
|
|
586
|
+
const peerConnection = new actualRTCPC({
|
|
587
|
+
iceServers: ice_servers
|
|
588
|
+
});
|
|
589
|
+
const pcDataChannel = peerConnection.createDataChannel("JanusDataChannel");
|
|
590
|
+
if (!session_id) {
|
|
449
591
|
throw new Error("Could not create session_id");
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
592
|
+
}
|
|
593
|
+
const videoStatsInterval = pollStats(peerConnection, callbacks.onVideoStateChange);
|
|
594
|
+
peerConnection.onicecandidate = (event) => {
|
|
595
|
+
log("peerConnection.onicecandidate", event);
|
|
596
|
+
if (event.candidate && event.candidate.sdpMid && event.candidate.sdpMLineIndex !== null) {
|
|
597
|
+
addIceCandidate(streamIdFromServer, {
|
|
598
|
+
candidate: event.candidate.candidate,
|
|
599
|
+
sdpMid: event.candidate.sdpMid,
|
|
600
|
+
sdpMLineIndex: event.candidate.sdpMLineIndex
|
|
601
|
+
}, session_id);
|
|
602
|
+
}
|
|
603
|
+
};
|
|
604
|
+
peerConnection.oniceconnectionstatechange = () => {
|
|
605
|
+
var _a;
|
|
606
|
+
log("peerConnection.oniceconnectionstatechange => " + peerConnection.iceConnectionState);
|
|
607
|
+
(_a = callbacks.onConnectionStateChange) == null ? void 0 : _a.call(callbacks, peerConnection.iceConnectionState);
|
|
608
|
+
};
|
|
609
|
+
peerConnection.ontrack = (event) => {
|
|
610
|
+
var _a;
|
|
611
|
+
log("peerConnection.ontrack", event);
|
|
612
|
+
(_a = callbacks.onSrcObjectReady) == null ? void 0 : _a.call(callbacks, event.streams[0]);
|
|
613
|
+
};
|
|
614
|
+
pcDataChannel.onmessage = (message) => {
|
|
615
|
+
var _a, _b;
|
|
616
|
+
if (pcDataChannel.readyState === "open") {
|
|
617
|
+
const [event, data] = message.data.split(":");
|
|
618
|
+
if (event === StreamEvents.StreamStarted) {
|
|
619
|
+
console.log("StreamStarted", event, data);
|
|
620
|
+
} else if (event === StreamEvents.StreamDone) {
|
|
621
|
+
console.log("StreamDone");
|
|
622
|
+
} else if (event === StreamEvents.StreamFailed) {
|
|
623
|
+
(_a = callbacks.onVideoStateChange) == null ? void 0 : _a.call(callbacks, StreamingState.Stop, {
|
|
624
|
+
event,
|
|
625
|
+
data
|
|
626
|
+
});
|
|
627
|
+
clearInterval(videoStatsInterval);
|
|
628
|
+
console.log("StreamFailed");
|
|
629
|
+
} else {
|
|
630
|
+
(_b = callbacks.onMessage) == null ? void 0 : _b.call(callbacks, event, decodeURIComponent(data));
|
|
631
|
+
}
|
|
471
632
|
}
|
|
472
|
-
}
|
|
473
|
-
|
|
474
|
-
|
|
633
|
+
};
|
|
634
|
+
await peerConnection.setRemoteDescription(offer);
|
|
635
|
+
log("set remote description OK");
|
|
636
|
+
const sessionClientAnswer = await peerConnection.createAnswer();
|
|
637
|
+
log("create answer OK");
|
|
638
|
+
await peerConnection.setLocalDescription(sessionClientAnswer);
|
|
639
|
+
log("set local description OK");
|
|
640
|
+
await startConnection(streamIdFromServer, sessionClientAnswer, session_id);
|
|
641
|
+
log("start connection OK");
|
|
642
|
+
return {
|
|
475
643
|
/**
|
|
476
644
|
* Method to send request to server to get clip or talk depend on you payload
|
|
477
645
|
* @param payload
|
|
478
646
|
*/
|
|
479
|
-
speak(
|
|
480
|
-
return
|
|
647
|
+
speak(payload) {
|
|
648
|
+
return sendStreamRequest(streamIdFromServer, session_id, payload);
|
|
481
649
|
},
|
|
482
650
|
/**
|
|
483
651
|
* Method to close RTC connection
|
|
484
652
|
*/
|
|
485
653
|
async terminate() {
|
|
486
|
-
var
|
|
487
|
-
|
|
488
|
-
|
|
654
|
+
var _a, _b;
|
|
655
|
+
if (streamIdFromServer) {
|
|
656
|
+
if (peerConnection) {
|
|
657
|
+
peerConnection.close();
|
|
658
|
+
peerConnection.oniceconnectionstatechange = null;
|
|
659
|
+
peerConnection.onnegotiationneeded = null;
|
|
660
|
+
peerConnection.onicecandidate = null;
|
|
661
|
+
peerConnection.ontrack = null;
|
|
662
|
+
}
|
|
663
|
+
await close(streamIdFromServer, session_id).catch((_) => {
|
|
664
|
+
});
|
|
665
|
+
(_a = callbacks.onConnectionStateChange) == null ? void 0 : _a.call(callbacks, "closed");
|
|
666
|
+
(_b = callbacks.onVideoStateChange) == null ? void 0 : _b.call(callbacks, StreamingState.Stop);
|
|
667
|
+
clearInterval(videoStatsInterval);
|
|
668
|
+
}
|
|
489
669
|
},
|
|
490
670
|
/**
|
|
491
671
|
* Session identifier information, should be returned in the body of all streaming requests
|
|
492
672
|
*/
|
|
493
|
-
sessionId:
|
|
673
|
+
sessionId: session_id,
|
|
494
674
|
/**
|
|
495
675
|
* Id of current RTC stream
|
|
496
676
|
*/
|
|
497
|
-
streamId:
|
|
677
|
+
streamId: streamIdFromServer
|
|
498
678
|
};
|
|
499
679
|
}
|
|
500
680
|
export {
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
681
|
+
ChatMode,
|
|
682
|
+
ChatProgress,
|
|
683
|
+
DocumentType,
|
|
684
|
+
KnowledgeType,
|
|
685
|
+
Providers,
|
|
686
|
+
RateState,
|
|
687
|
+
SocketManager,
|
|
688
|
+
Subject,
|
|
689
|
+
VoiceAccess,
|
|
690
|
+
createAgentManager,
|
|
691
|
+
createAgentsApi,
|
|
692
|
+
createClient,
|
|
693
|
+
createKnowledgeApi,
|
|
694
|
+
createRatingsApi,
|
|
695
|
+
createStreamingManager,
|
|
696
|
+
getAgent
|
|
517
697
|
};
|