@d-id/client-sdk 1.1.0-beta.2 → 1.1.0-beta.3
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 +1433 -839
- package/dist/index.umd.cjs +1789 -1
- package/dist/src/services/streaming-manager/index.d.ts +3 -2
- package/dist/src/services/streaming-manager/stats/poll.d.ts +2 -2
- package/dist/src/services/streaming-manager/stats/report.d.ts +3 -0
- package/dist/src/types/entities/agents/manager.d.ts +17 -3
- package/dist/src/types/stream/rtc.d.ts +1 -0
- package/dist/src/types/stream/stream.d.ts +20 -1
- package/dist/src/types/stream-script.d.ts +1 -1
- package/dist/src/utils/analytics.d.ts +2 -0
- package/package.json +2 -2
package/dist/index.js
CHANGED
|
@@ -1,320 +1,519 @@
|
|
|
1
|
-
var
|
|
2
|
-
var
|
|
3
|
-
var
|
|
4
|
-
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
3
|
+
var __publicField = (obj, key, value) => {
|
|
4
|
+
__defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
5
|
+
return value;
|
|
6
|
+
};
|
|
7
|
+
class BaseError extends Error {
|
|
5
8
|
constructor({
|
|
6
|
-
kind
|
|
7
|
-
description
|
|
8
|
-
error
|
|
9
|
+
kind,
|
|
10
|
+
description,
|
|
11
|
+
error
|
|
9
12
|
}) {
|
|
10
13
|
super(JSON.stringify({
|
|
11
|
-
kind
|
|
12
|
-
description
|
|
14
|
+
kind,
|
|
15
|
+
description
|
|
13
16
|
}));
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
this.kind =
|
|
17
|
+
__publicField(this, "kind");
|
|
18
|
+
__publicField(this, "description");
|
|
19
|
+
__publicField(this, "error");
|
|
20
|
+
this.kind = kind;
|
|
21
|
+
this.description = description;
|
|
22
|
+
this.error = error;
|
|
18
23
|
}
|
|
19
24
|
}
|
|
20
|
-
class
|
|
21
|
-
constructor(
|
|
25
|
+
class ChatCreationFailed extends BaseError {
|
|
26
|
+
constructor(mode, persistent) {
|
|
22
27
|
super({
|
|
23
28
|
kind: "ChatCreationFailed",
|
|
24
|
-
description: `Failed to create ${
|
|
29
|
+
description: `Failed to create ${persistent ? "persistent" : ""} chat, mode: ${mode}`
|
|
25
30
|
});
|
|
26
31
|
}
|
|
27
32
|
}
|
|
28
|
-
class
|
|
29
|
-
constructor(
|
|
33
|
+
class ChatModeDowngraded extends BaseError {
|
|
34
|
+
constructor(mode) {
|
|
30
35
|
super({
|
|
31
36
|
kind: "ChatModeDowngraded",
|
|
32
|
-
description: `Chat mode downgraded to ${
|
|
37
|
+
description: `Chat mode downgraded to ${mode}`
|
|
33
38
|
});
|
|
34
39
|
}
|
|
35
40
|
}
|
|
36
|
-
class
|
|
37
|
-
constructor(
|
|
41
|
+
class ValidationError extends BaseError {
|
|
42
|
+
constructor(message, key) {
|
|
38
43
|
super({
|
|
39
44
|
kind: "ValidationError",
|
|
40
|
-
description:
|
|
45
|
+
description: message
|
|
41
46
|
});
|
|
42
|
-
|
|
43
|
-
this.key =
|
|
47
|
+
__publicField(this, "key");
|
|
48
|
+
this.key = key;
|
|
44
49
|
}
|
|
45
50
|
}
|
|
46
|
-
class
|
|
47
|
-
constructor(
|
|
51
|
+
class WsError extends BaseError {
|
|
52
|
+
constructor(message) {
|
|
48
53
|
super({
|
|
49
54
|
kind: "WSError",
|
|
50
|
-
description:
|
|
55
|
+
description: message
|
|
51
56
|
});
|
|
52
57
|
}
|
|
53
58
|
}
|
|
54
|
-
var
|
|
55
|
-
|
|
56
|
-
|
|
59
|
+
var UserPlan = /* @__PURE__ */ ((UserPlan2) => {
|
|
60
|
+
UserPlan2["TRIAL"] = "trial";
|
|
61
|
+
UserPlan2["BASIC"] = "basic";
|
|
62
|
+
UserPlan2["ENTERPRISE"] = "enterprise";
|
|
63
|
+
UserPlan2["LITE"] = "lite";
|
|
64
|
+
UserPlan2["ADVANCED"] = "advanced";
|
|
65
|
+
return UserPlan2;
|
|
66
|
+
})(UserPlan || {});
|
|
67
|
+
var PlanGroup = /* @__PURE__ */ ((PlanGroup2) => {
|
|
68
|
+
PlanGroup2["TRIAL"] = "deid-trial";
|
|
69
|
+
PlanGroup2["PRO"] = "deid-pro";
|
|
70
|
+
PlanGroup2["ENTERPRISE"] = "deid-enterprise";
|
|
71
|
+
PlanGroup2["LITE"] = "deid-lite";
|
|
72
|
+
PlanGroup2["ADVANCED"] = "deid-advanced";
|
|
73
|
+
PlanGroup2["BUILD"] = "deid-api-build";
|
|
74
|
+
PlanGroup2["LAUNCH"] = "deid-api-launch";
|
|
75
|
+
PlanGroup2["SCALE"] = "deid-api-scale";
|
|
76
|
+
return PlanGroup2;
|
|
77
|
+
})(PlanGroup || {});
|
|
78
|
+
var AgentStatus = /* @__PURE__ */ ((AgentStatus2) => {
|
|
79
|
+
AgentStatus2["Created"] = "created";
|
|
80
|
+
AgentStatus2["Started"] = "started";
|
|
81
|
+
AgentStatus2["Done"] = "done";
|
|
82
|
+
AgentStatus2["Error"] = "error";
|
|
83
|
+
AgentStatus2["Rejected"] = "rejected";
|
|
84
|
+
AgentStatus2["Ready"] = "ready";
|
|
85
|
+
return AgentStatus2;
|
|
86
|
+
})(AgentStatus || {});
|
|
87
|
+
var RateState = /* @__PURE__ */ ((RateState2) => {
|
|
88
|
+
RateState2["Unrated"] = "Unrated";
|
|
89
|
+
RateState2["Positive"] = "Positive";
|
|
90
|
+
RateState2["Negative"] = "Negative";
|
|
91
|
+
return RateState2;
|
|
92
|
+
})(RateState || {});
|
|
93
|
+
var ChatMode = /* @__PURE__ */ ((ChatMode2) => {
|
|
94
|
+
ChatMode2["Functional"] = "Functional";
|
|
95
|
+
ChatMode2["TextOnly"] = "TextOnly";
|
|
96
|
+
ChatMode2["Maintenance"] = "Maintenance";
|
|
97
|
+
ChatMode2["Playground"] = "Playground";
|
|
98
|
+
ChatMode2["DirectPlayback"] = "DirectPlayback";
|
|
99
|
+
return ChatMode2;
|
|
100
|
+
})(ChatMode || {});
|
|
101
|
+
var ChatProgress = /* @__PURE__ */ ((ChatProgress2) => {
|
|
102
|
+
ChatProgress2["Embed"] = "embed";
|
|
103
|
+
ChatProgress2["Query"] = "query";
|
|
104
|
+
ChatProgress2["Partial"] = "partial";
|
|
105
|
+
ChatProgress2["Answer"] = "answer";
|
|
106
|
+
ChatProgress2["Complete"] = "done";
|
|
107
|
+
return ChatProgress2;
|
|
108
|
+
})(ChatProgress || {});
|
|
109
|
+
var Subject = /* @__PURE__ */ ((Subject2) => {
|
|
110
|
+
Subject2["KnowledgeProcessing"] = "knowledge/processing";
|
|
111
|
+
Subject2["KnowledgeIndexing"] = "knowledge/indexing";
|
|
112
|
+
Subject2["KnowledgeFailed"] = "knowledge/error";
|
|
113
|
+
Subject2["KnowledgeDone"] = "knowledge/done";
|
|
114
|
+
return Subject2;
|
|
115
|
+
})(Subject || {});
|
|
116
|
+
var KnowledgeType = /* @__PURE__ */ ((KnowledgeType2) => {
|
|
117
|
+
KnowledgeType2["Knowledge"] = "knowledge";
|
|
118
|
+
KnowledgeType2["Document"] = "document";
|
|
119
|
+
KnowledgeType2["Record"] = "record";
|
|
120
|
+
return KnowledgeType2;
|
|
121
|
+
})(KnowledgeType || {});
|
|
122
|
+
var DocumentType = /* @__PURE__ */ ((DocumentType2) => {
|
|
123
|
+
DocumentType2["Pdf"] = "pdf";
|
|
124
|
+
DocumentType2["Text"] = "text";
|
|
125
|
+
DocumentType2["Html"] = "html";
|
|
126
|
+
DocumentType2["Word"] = "word";
|
|
127
|
+
DocumentType2["Json"] = "json";
|
|
128
|
+
DocumentType2["Markdown"] = "markdown";
|
|
129
|
+
DocumentType2["Csv"] = "csv";
|
|
130
|
+
DocumentType2["Excel"] = "excel";
|
|
131
|
+
DocumentType2["Powerpoint"] = "powerpoint";
|
|
132
|
+
DocumentType2["Archive"] = "archive";
|
|
133
|
+
DocumentType2["Image"] = "image";
|
|
134
|
+
DocumentType2["Audio"] = "audio";
|
|
135
|
+
DocumentType2["Video"] = "video";
|
|
136
|
+
return DocumentType2;
|
|
137
|
+
})(DocumentType || {});
|
|
138
|
+
var VideoType = /* @__PURE__ */ ((VideoType2) => {
|
|
139
|
+
VideoType2["Clip"] = "clip";
|
|
140
|
+
VideoType2["Talk"] = "talk";
|
|
141
|
+
return VideoType2;
|
|
142
|
+
})(VideoType || {});
|
|
143
|
+
const mapVideoType = (type) => {
|
|
144
|
+
switch (type) {
|
|
57
145
|
case "clip":
|
|
58
146
|
return "clip";
|
|
59
147
|
case "talk":
|
|
60
148
|
return "talk";
|
|
61
149
|
default:
|
|
62
|
-
throw new Error(`Unknown video type: ${
|
|
150
|
+
throw new Error(`Unknown video type: ${type}`);
|
|
63
151
|
}
|
|
64
152
|
};
|
|
65
|
-
var
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
153
|
+
var StreamingState = /* @__PURE__ */ ((StreamingState2) => {
|
|
154
|
+
StreamingState2["Start"] = "START";
|
|
155
|
+
StreamingState2["Stop"] = "STOP";
|
|
156
|
+
return StreamingState2;
|
|
157
|
+
})(StreamingState || {});
|
|
158
|
+
var ConnectivityState = /* @__PURE__ */ ((ConnectivityState2) => {
|
|
159
|
+
ConnectivityState2["Strong"] = "STRONG";
|
|
160
|
+
ConnectivityState2["Weak"] = "WEAK";
|
|
161
|
+
ConnectivityState2["Unknown"] = "UNKNOWN";
|
|
162
|
+
return ConnectivityState2;
|
|
163
|
+
})(ConnectivityState || {});
|
|
164
|
+
var AgentActivityState = /* @__PURE__ */ ((AgentActivityState2) => {
|
|
165
|
+
AgentActivityState2["Idle"] = "IDLE";
|
|
166
|
+
AgentActivityState2["Talking"] = "TALKING";
|
|
167
|
+
return AgentActivityState2;
|
|
168
|
+
})(AgentActivityState || {});
|
|
169
|
+
const DataChannelSignalMap = {
|
|
170
|
+
"stream/started": "START",
|
|
171
|
+
"stream/done": "STOP"
|
|
172
|
+
/* Stop */
|
|
173
|
+
};
|
|
174
|
+
var StreamEvents = /* @__PURE__ */ ((StreamEvents2) => {
|
|
175
|
+
StreamEvents2["ChatAnswer"] = "chat/answer";
|
|
176
|
+
StreamEvents2["ChatPartial"] = "chat/partial";
|
|
177
|
+
StreamEvents2["StreamDone"] = "stream/done";
|
|
178
|
+
StreamEvents2["StreamStarted"] = "stream/started";
|
|
179
|
+
StreamEvents2["StreamFailed"] = "stream/error";
|
|
180
|
+
StreamEvents2["StreamReady"] = "stream/ready";
|
|
181
|
+
StreamEvents2["StreamCreated"] = "stream/created";
|
|
182
|
+
StreamEvents2["StreamVideoCreated"] = "stream-video/started";
|
|
183
|
+
StreamEvents2["StreamVideoDone"] = "stream-video/done";
|
|
184
|
+
StreamEvents2["StreamVideoError"] = "stream-video/error";
|
|
185
|
+
StreamEvents2["StreamVideoRejected"] = "stream-video/rejected";
|
|
186
|
+
return StreamEvents2;
|
|
187
|
+
})(StreamEvents || {});
|
|
188
|
+
var ConnectionState = /* @__PURE__ */ ((ConnectionState2) => {
|
|
189
|
+
ConnectionState2["New"] = "new";
|
|
190
|
+
ConnectionState2["Fail"] = "fail";
|
|
191
|
+
ConnectionState2["Connected"] = "connected";
|
|
192
|
+
ConnectionState2["Connecting"] = "connecting";
|
|
193
|
+
ConnectionState2["Closed"] = "closed";
|
|
194
|
+
ConnectionState2["Completed"] = "completed";
|
|
195
|
+
ConnectionState2["Disconnected"] = "disconnected";
|
|
196
|
+
return ConnectionState2;
|
|
197
|
+
})(ConnectionState || {});
|
|
198
|
+
var StreamType = /* @__PURE__ */ ((StreamType2) => {
|
|
199
|
+
StreamType2["Legacy"] = "legacy";
|
|
200
|
+
StreamType2["Fluent"] = "fluent";
|
|
201
|
+
return StreamType2;
|
|
202
|
+
})(StreamType || {});
|
|
203
|
+
var Providers = /* @__PURE__ */ ((Providers2) => {
|
|
204
|
+
Providers2["Amazon"] = "amazon";
|
|
205
|
+
Providers2["Microsoft"] = "microsoft";
|
|
206
|
+
Providers2["Afflorithmics"] = "afflorithmics";
|
|
207
|
+
Providers2["Elevenlabs"] = "elevenlabs";
|
|
208
|
+
return Providers2;
|
|
209
|
+
})(Providers || {});
|
|
210
|
+
var VoiceAccess = /* @__PURE__ */ ((VoiceAccess2) => {
|
|
211
|
+
VoiceAccess2["Public"] = "public";
|
|
212
|
+
VoiceAccess2["Premium"] = "premium";
|
|
213
|
+
VoiceAccess2["Private"] = "private";
|
|
214
|
+
return VoiceAccess2;
|
|
215
|
+
})(VoiceAccess || {});
|
|
216
|
+
const CONNECTION_RETRY_TIMEOUT_MS = 45 * 1e3;
|
|
217
|
+
const PLAYGROUND_HEADER = "X-Playground-Chat";
|
|
218
|
+
const didApiUrl = "https://api-dev.d-id.com";
|
|
219
|
+
const didSocketApiUrl = "wss://notifications-dev.d-id.com";
|
|
220
|
+
const mixpanelKey = "79f81a83a67430be2bc0fd61042b8faa";
|
|
221
|
+
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
|
|
222
|
+
const getRandom = () => Math.random().toString(16).slice(2);
|
|
223
|
+
function createRacePromise(timeout, timeoutErrorMessage) {
|
|
224
|
+
let timeId;
|
|
225
|
+
const promise = new Promise((_, reject) => {
|
|
226
|
+
timeId = setTimeout(() => reject(new Error(timeoutErrorMessage)), timeout);
|
|
227
|
+
});
|
|
69
228
|
return {
|
|
70
|
-
promise
|
|
71
|
-
|
|
72
|
-
}),
|
|
73
|
-
clear: () => clearTimeout(a)
|
|
229
|
+
promise,
|
|
230
|
+
clear: () => clearTimeout(timeId)
|
|
74
231
|
};
|
|
75
232
|
}
|
|
76
|
-
async function
|
|
77
|
-
const
|
|
78
|
-
limit: (
|
|
79
|
-
delayMs: (
|
|
80
|
-
timeout: (
|
|
81
|
-
timeoutErrorMessage: (
|
|
82
|
-
shouldRetryFn: (
|
|
83
|
-
onRetry: (
|
|
233
|
+
async function retryOperation(operation, userOptions) {
|
|
234
|
+
const options = {
|
|
235
|
+
limit: (userOptions == null ? void 0 : userOptions.limit) ?? 3,
|
|
236
|
+
delayMs: (userOptions == null ? void 0 : userOptions.delayMs) ?? 0,
|
|
237
|
+
timeout: (userOptions == null ? void 0 : userOptions.timeout) ?? 3e4,
|
|
238
|
+
timeoutErrorMessage: (userOptions == null ? void 0 : userOptions.timeoutErrorMessage) || "Timeout error",
|
|
239
|
+
shouldRetryFn: (userOptions == null ? void 0 : userOptions.shouldRetryFn) ?? (() => true),
|
|
240
|
+
onRetry: (userOptions == null ? void 0 : userOptions.onRetry) ?? (() => {
|
|
84
241
|
})
|
|
85
242
|
};
|
|
86
|
-
let
|
|
87
|
-
for (let
|
|
243
|
+
let lastError;
|
|
244
|
+
for (let attempt = 1; attempt <= options.limit; attempt++) {
|
|
88
245
|
try {
|
|
89
|
-
if (!
|
|
90
|
-
return await
|
|
246
|
+
if (!options.timeout) {
|
|
247
|
+
return await operation();
|
|
248
|
+
}
|
|
91
249
|
const {
|
|
92
|
-
promise
|
|
93
|
-
clear
|
|
94
|
-
} =
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
250
|
+
promise,
|
|
251
|
+
clear
|
|
252
|
+
} = createRacePromise(options.timeout, options.timeoutErrorMessage);
|
|
253
|
+
const operationPromise = operation().finally(clear);
|
|
254
|
+
return await Promise.race([operationPromise, promise]);
|
|
255
|
+
} catch (error) {
|
|
256
|
+
lastError = error;
|
|
257
|
+
if (!options.shouldRetryFn(error) || attempt >= options.limit) {
|
|
258
|
+
throw error;
|
|
259
|
+
}
|
|
260
|
+
await sleep(options.delayMs);
|
|
261
|
+
options.onRetry(error);
|
|
100
262
|
}
|
|
101
|
-
|
|
263
|
+
}
|
|
264
|
+
throw lastError;
|
|
102
265
|
}
|
|
103
|
-
function
|
|
104
|
-
let
|
|
105
|
-
|
|
266
|
+
function getExternalId() {
|
|
267
|
+
let key = window.localStorage.getItem("did_external_key_id");
|
|
268
|
+
if (!key) {
|
|
269
|
+
key = Math.random().toString(16).slice(2);
|
|
270
|
+
window.localStorage.setItem("did_external_key_id", key);
|
|
271
|
+
}
|
|
272
|
+
return key;
|
|
106
273
|
}
|
|
107
|
-
let
|
|
108
|
-
function
|
|
109
|
-
if (
|
|
110
|
-
return `Bearer ${
|
|
111
|
-
if (
|
|
112
|
-
return `Basic ${btoa(`${
|
|
113
|
-
if (
|
|
114
|
-
return `Client-Key ${
|
|
115
|
-
|
|
274
|
+
let sessionKey = getRandom();
|
|
275
|
+
function getAuthHeader(auth) {
|
|
276
|
+
if (auth.type === "bearer") {
|
|
277
|
+
return `Bearer ${auth.token}`;
|
|
278
|
+
} else if (auth.type === "basic") {
|
|
279
|
+
return `Basic ${btoa(`${auth.username}:${auth.password}`)}`;
|
|
280
|
+
} else if (auth.type === "key") {
|
|
281
|
+
return `Client-Key ${auth.clientKey}.${getExternalId()}_${sessionKey}`;
|
|
282
|
+
} else {
|
|
283
|
+
throw new Error(`Unknown auth type: ${auth}`);
|
|
284
|
+
}
|
|
116
285
|
}
|
|
117
|
-
const
|
|
286
|
+
const retryHttpTooManyRequests = (operation) => retryOperation(operation, {
|
|
118
287
|
limit: 3,
|
|
119
288
|
delayMs: 1e3,
|
|
120
289
|
timeout: 0,
|
|
121
|
-
shouldRetryFn: (
|
|
290
|
+
shouldRetryFn: (error) => error.status === 429
|
|
122
291
|
});
|
|
123
|
-
function
|
|
124
|
-
const
|
|
292
|
+
function createClient(auth, host = didApiUrl, onError) {
|
|
293
|
+
const client = async (url, options) => {
|
|
125
294
|
const {
|
|
126
|
-
skipErrorHandler
|
|
127
|
-
...
|
|
128
|
-
} =
|
|
129
|
-
|
|
295
|
+
skipErrorHandler,
|
|
296
|
+
...fetchOptions
|
|
297
|
+
} = options || {};
|
|
298
|
+
const request = await retryHttpTooManyRequests(() => fetch(host + ((url == null ? void 0 : url.startsWith("/")) ? url : `/${url}`), {
|
|
299
|
+
...fetchOptions,
|
|
130
300
|
headers: {
|
|
131
|
-
...
|
|
132
|
-
Authorization:
|
|
301
|
+
...fetchOptions.headers,
|
|
302
|
+
Authorization: getAuthHeader(auth),
|
|
133
303
|
"Content-Type": "application/json"
|
|
134
304
|
}
|
|
135
305
|
}));
|
|
136
|
-
if (!
|
|
137
|
-
let
|
|
138
|
-
const
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
306
|
+
if (!request.ok) {
|
|
307
|
+
let errorText = await request.text().catch(() => `Failed to fetch with status ${request.status}`);
|
|
308
|
+
const error = new Error(errorText);
|
|
309
|
+
if (onError && !skipErrorHandler) {
|
|
310
|
+
onError(error, {
|
|
311
|
+
url,
|
|
312
|
+
options: fetchOptions,
|
|
313
|
+
headers: request.headers
|
|
314
|
+
});
|
|
315
|
+
}
|
|
316
|
+
throw error;
|
|
144
317
|
}
|
|
145
|
-
return
|
|
318
|
+
return request.json();
|
|
146
319
|
};
|
|
147
320
|
return {
|
|
148
|
-
get(
|
|
149
|
-
return
|
|
150
|
-
...
|
|
321
|
+
get(url, options) {
|
|
322
|
+
return client(url, {
|
|
323
|
+
...options,
|
|
151
324
|
method: "GET"
|
|
152
325
|
});
|
|
153
326
|
},
|
|
154
|
-
post(
|
|
155
|
-
return
|
|
156
|
-
...
|
|
157
|
-
body: JSON.stringify(
|
|
327
|
+
post(url, body, options) {
|
|
328
|
+
return client(url, {
|
|
329
|
+
...options,
|
|
330
|
+
body: JSON.stringify(body),
|
|
158
331
|
method: "POST"
|
|
159
332
|
});
|
|
160
333
|
},
|
|
161
|
-
delete(
|
|
162
|
-
return
|
|
163
|
-
...
|
|
164
|
-
body: JSON.stringify(
|
|
334
|
+
delete(url, body, options) {
|
|
335
|
+
return client(url, {
|
|
336
|
+
...options,
|
|
337
|
+
body: JSON.stringify(body),
|
|
165
338
|
method: "DELETE"
|
|
166
339
|
});
|
|
167
340
|
},
|
|
168
|
-
patch(
|
|
169
|
-
return
|
|
170
|
-
...
|
|
171
|
-
body: JSON.stringify(
|
|
341
|
+
patch(url, body, options) {
|
|
342
|
+
return client(url, {
|
|
343
|
+
...options,
|
|
344
|
+
body: JSON.stringify(body),
|
|
172
345
|
method: "PATCH"
|
|
173
346
|
});
|
|
174
347
|
}
|
|
175
348
|
};
|
|
176
349
|
}
|
|
177
|
-
function
|
|
178
|
-
const
|
|
350
|
+
function createAgentsApi(auth, host = didApiUrl, onError) {
|
|
351
|
+
const client = createClient(auth, `${host}/agents`, onError);
|
|
179
352
|
return {
|
|
180
|
-
create(
|
|
181
|
-
return
|
|
353
|
+
create(payload, options) {
|
|
354
|
+
return client.post(`/`, payload, options);
|
|
182
355
|
},
|
|
183
|
-
getAgents(
|
|
184
|
-
return
|
|
356
|
+
getAgents(tag, options) {
|
|
357
|
+
return client.get(`/${tag ? `?tag=${tag}` : ""}`, options).then((agents) => agents ?? []);
|
|
185
358
|
},
|
|
186
|
-
getById(
|
|
187
|
-
return
|
|
359
|
+
getById(id, options) {
|
|
360
|
+
return client.get(`/${id}`, options);
|
|
188
361
|
},
|
|
189
|
-
delete(
|
|
190
|
-
return
|
|
362
|
+
delete(id, options) {
|
|
363
|
+
return client.delete(`/${id}`, void 0, options);
|
|
191
364
|
},
|
|
192
|
-
update(
|
|
193
|
-
return
|
|
365
|
+
update(id, payload, options) {
|
|
366
|
+
return client.patch(`/${id}`, payload, options);
|
|
194
367
|
},
|
|
195
|
-
newChat(
|
|
196
|
-
return
|
|
368
|
+
newChat(agentId, payload, options) {
|
|
369
|
+
return client.post(`/${agentId}/chat`, payload, options);
|
|
197
370
|
},
|
|
198
|
-
chat(
|
|
199
|
-
return
|
|
371
|
+
chat(agentId, chatId, payload, options) {
|
|
372
|
+
return client.post(`/${agentId}/chat/${chatId}`, payload, options);
|
|
200
373
|
},
|
|
201
|
-
createRating(
|
|
202
|
-
return
|
|
374
|
+
createRating(agentId, chatId, payload, options) {
|
|
375
|
+
return client.post(`/${agentId}/chat/${chatId}/ratings`, payload, options);
|
|
203
376
|
},
|
|
204
|
-
updateRating(
|
|
205
|
-
return
|
|
377
|
+
updateRating(agentId, chatId, ratingId, payload, options) {
|
|
378
|
+
return client.patch(`/${agentId}/chat/${chatId}/ratings/${ratingId}`, payload, options);
|
|
206
379
|
},
|
|
207
|
-
deleteRating(
|
|
208
|
-
return
|
|
380
|
+
deleteRating(agentId, chatId, ratingId, options) {
|
|
381
|
+
return client.delete(`/${agentId}/chat/${chatId}/ratings/${ratingId}`, options);
|
|
209
382
|
},
|
|
210
|
-
getSTTToken(
|
|
211
|
-
return
|
|
383
|
+
getSTTToken(agentId, options) {
|
|
384
|
+
return client.get(`/${agentId}/stt-token`, options);
|
|
212
385
|
}
|
|
213
386
|
};
|
|
214
387
|
}
|
|
215
|
-
const
|
|
216
|
-
function
|
|
217
|
-
var
|
|
218
|
-
const
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
388
|
+
const getAgentType = (presenter) => presenter.type === "clip" && presenter.presenter_id.startsWith("v2_") ? "clip_v2" : presenter.type;
|
|
389
|
+
function getAnalyticsInfo(agent) {
|
|
390
|
+
var _a, _b, _c, _d;
|
|
391
|
+
const mobileOrDesktop = () => {
|
|
392
|
+
return /Mobi|Android/i.test(navigator.userAgent) ? "Mobile" : "Desktop";
|
|
393
|
+
};
|
|
394
|
+
const getUserOS = () => {
|
|
395
|
+
const platform = navigator.platform;
|
|
396
|
+
if (platform.toLowerCase().includes("win")) {
|
|
397
|
+
return "Windows";
|
|
398
|
+
} else if (platform.toLowerCase().includes("mac")) {
|
|
399
|
+
return "Mac OS X";
|
|
400
|
+
} else if (platform.toLowerCase().includes("linux")) {
|
|
401
|
+
return "Linux";
|
|
402
|
+
} else {
|
|
403
|
+
return "Unknown";
|
|
404
|
+
}
|
|
405
|
+
};
|
|
406
|
+
const presenter = agent.presenter;
|
|
222
407
|
return {
|
|
223
|
-
$os: `${
|
|
224
|
-
isMobile: `${
|
|
408
|
+
$os: `${getUserOS()}`,
|
|
409
|
+
isMobile: `${mobileOrDesktop() == "Mobile"}`,
|
|
225
410
|
browser: navigator.userAgent,
|
|
226
411
|
origin: window.location.origin,
|
|
227
|
-
agentType:
|
|
412
|
+
agentType: getAgentType(presenter),
|
|
228
413
|
agentVoice: {
|
|
229
|
-
voiceId: (
|
|
230
|
-
provider: (
|
|
414
|
+
voiceId: (_b = (_a = agent.presenter) == null ? void 0 : _a.voice) == null ? void 0 : _b.voice_id,
|
|
415
|
+
provider: (_d = (_c = agent.presenter) == null ? void 0 : _c.voice) == null ? void 0 : _d.type
|
|
231
416
|
}
|
|
232
417
|
};
|
|
233
418
|
}
|
|
234
|
-
|
|
235
|
-
|
|
419
|
+
const sumFunc = (numbers) => numbers.reduce((total, aNumber) => total + aNumber, 0);
|
|
420
|
+
const average = (numbers) => sumFunc(numbers) / numbers.length;
|
|
421
|
+
function getStreamAnalyticsProps(data, agent, additionalProps) {
|
|
422
|
+
var _a, _b, _c;
|
|
236
423
|
const {
|
|
237
|
-
event
|
|
238
|
-
...
|
|
239
|
-
} =
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
424
|
+
event,
|
|
425
|
+
...baseProps
|
|
426
|
+
} = data;
|
|
427
|
+
const {
|
|
428
|
+
template
|
|
429
|
+
} = (agent == null ? void 0 : agent.llm) || {};
|
|
430
|
+
const {
|
|
431
|
+
language
|
|
432
|
+
} = ((_a = agent == null ? void 0 : agent.presenter) == null ? void 0 : _a.voice) || {};
|
|
433
|
+
const props = {
|
|
434
|
+
...baseProps,
|
|
246
435
|
llm: {
|
|
247
|
-
...
|
|
248
|
-
template
|
|
436
|
+
...baseProps.llm,
|
|
437
|
+
template
|
|
249
438
|
},
|
|
250
439
|
script: {
|
|
251
|
-
...
|
|
440
|
+
...baseProps.script,
|
|
252
441
|
provider: {
|
|
253
|
-
...(
|
|
254
|
-
language
|
|
442
|
+
...(_b = baseProps == null ? void 0 : baseProps.script) == null ? void 0 : _b.provider,
|
|
443
|
+
language
|
|
255
444
|
}
|
|
256
445
|
},
|
|
257
|
-
stitch: (
|
|
258
|
-
...
|
|
446
|
+
stitch: (agent == null ? void 0 : agent.presenter.type) === "talk" ? (_c = agent == null ? void 0 : agent.presenter) == null ? void 0 : _c.stitch : void 0,
|
|
447
|
+
...additionalProps
|
|
259
448
|
};
|
|
449
|
+
return props;
|
|
260
450
|
}
|
|
261
|
-
let
|
|
262
|
-
const
|
|
263
|
-
function
|
|
264
|
-
var
|
|
265
|
-
const
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
451
|
+
let mixpanelEvents = {};
|
|
452
|
+
const mixpanelUrl = "https://api-js.mixpanel.com/track/?verbose=1&ip=1";
|
|
453
|
+
function initializeAnalytics(config) {
|
|
454
|
+
var _a, _b, _c, _d, _e, _f;
|
|
455
|
+
const source = (window == null ? void 0 : window.hasOwnProperty("DID_AGENTS_API")) ? "agents-ui" : "agents-sdk";
|
|
456
|
+
const presenter = config.agent.presenter;
|
|
457
|
+
const promptCustomization = (_a = config.agent.llm) == null ? void 0 : _a.prompt_customization;
|
|
458
|
+
const analyticProps = {
|
|
459
|
+
token: config.token || "testKey",
|
|
460
|
+
distinct_id: config.distinctId || getExternalId(),
|
|
461
|
+
agentId: config.agent.id,
|
|
462
|
+
agentType: getAgentType(presenter),
|
|
463
|
+
owner_id: config.agent.owner_id ?? "",
|
|
464
|
+
promptVersion: (_b = config.agent.llm) == null ? void 0 : _b.prompt_version,
|
|
272
465
|
behavior: {
|
|
273
|
-
role:
|
|
274
|
-
personality:
|
|
275
|
-
instructions: (
|
|
466
|
+
role: promptCustomization == null ? void 0 : promptCustomization.role,
|
|
467
|
+
personality: promptCustomization == null ? void 0 : promptCustomization.personality,
|
|
468
|
+
instructions: (_c = config.agent.llm) == null ? void 0 : _c.instructions
|
|
276
469
|
},
|
|
277
|
-
temperature: (
|
|
278
|
-
knowledgeSource:
|
|
279
|
-
starterQuestionsCount: (
|
|
280
|
-
topicsToAvoid:
|
|
281
|
-
maxResponseLength:
|
|
470
|
+
temperature: (_d = config.agent.llm) == null ? void 0 : _d.temperature,
|
|
471
|
+
knowledgeSource: promptCustomization == null ? void 0 : promptCustomization.knowledge_source,
|
|
472
|
+
starterQuestionsCount: (_f = (_e = config.agent.knowledge) == null ? void 0 : _e.starter_message) == null ? void 0 : _f.length,
|
|
473
|
+
topicsToAvoid: promptCustomization == null ? void 0 : promptCustomization.topics_to_avoid,
|
|
474
|
+
maxResponseLength: promptCustomization == null ? void 0 : promptCustomization.max_response_length
|
|
282
475
|
};
|
|
283
476
|
return {
|
|
284
|
-
...
|
|
477
|
+
...analyticProps,
|
|
285
478
|
additionalProperties: {},
|
|
286
|
-
isEnabled:
|
|
479
|
+
isEnabled: config.isEnabled ?? true,
|
|
287
480
|
getRandom: () => Math.random().toString(16).slice(2),
|
|
288
|
-
enrich(
|
|
289
|
-
const
|
|
290
|
-
if (
|
|
481
|
+
enrich(properties) {
|
|
482
|
+
const props = {};
|
|
483
|
+
if (properties && typeof properties !== "object") {
|
|
291
484
|
throw new Error("properties must be a flat json object");
|
|
292
|
-
|
|
293
|
-
|
|
485
|
+
}
|
|
486
|
+
for (let prop in properties) {
|
|
487
|
+
if (typeof properties[prop] === "string" || typeof properties[prop] === "number") {
|
|
488
|
+
props[prop] = properties[prop];
|
|
489
|
+
}
|
|
490
|
+
}
|
|
294
491
|
this.additionalProperties = {
|
|
295
492
|
...this.additionalProperties,
|
|
296
|
-
...
|
|
493
|
+
...props
|
|
297
494
|
};
|
|
298
495
|
},
|
|
299
|
-
async track(
|
|
300
|
-
if (!this.isEnabled)
|
|
496
|
+
async track(event, props) {
|
|
497
|
+
if (!this.isEnabled) {
|
|
301
498
|
return Promise.resolve();
|
|
499
|
+
}
|
|
302
500
|
const {
|
|
303
|
-
audioPath
|
|
304
|
-
...
|
|
305
|
-
} =
|
|
501
|
+
audioPath,
|
|
502
|
+
...sendProps
|
|
503
|
+
} = props || {};
|
|
504
|
+
const options = {
|
|
306
505
|
method: "POST",
|
|
307
506
|
headers: {
|
|
308
507
|
"Content-Type": "application/x-www-form-urlencoded"
|
|
309
508
|
},
|
|
310
509
|
body: new URLSearchParams({
|
|
311
510
|
data: JSON.stringify([{
|
|
312
|
-
event
|
|
511
|
+
event,
|
|
313
512
|
properties: {
|
|
314
513
|
...this.additionalProperties,
|
|
315
|
-
...
|
|
316
|
-
...
|
|
317
|
-
source
|
|
514
|
+
...sendProps,
|
|
515
|
+
...analyticProps,
|
|
516
|
+
source,
|
|
318
517
|
time: Date.now(),
|
|
319
518
|
$insert_id: this.getRandom(),
|
|
320
519
|
origin: window.location.href,
|
|
@@ -326,866 +525,1261 @@ function je(e) {
|
|
|
326
525
|
})
|
|
327
526
|
};
|
|
328
527
|
try {
|
|
329
|
-
return await fetch(
|
|
330
|
-
} catch (
|
|
331
|
-
return console.error(
|
|
528
|
+
return await fetch(mixpanelUrl, options).then((res) => res.json());
|
|
529
|
+
} catch (err) {
|
|
530
|
+
return console.error(err);
|
|
332
531
|
}
|
|
333
532
|
},
|
|
334
|
-
linkTrack(
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
533
|
+
linkTrack(mixpanelEvent, props, event, dependencies) {
|
|
534
|
+
if (!mixpanelEvents[mixpanelEvent]) {
|
|
535
|
+
mixpanelEvents[mixpanelEvent] = {
|
|
536
|
+
events: {},
|
|
537
|
+
resolvedDependencies: []
|
|
538
|
+
};
|
|
539
|
+
}
|
|
540
|
+
if (!dependencies.includes(event)) {
|
|
541
|
+
dependencies.push(event);
|
|
542
|
+
}
|
|
543
|
+
const linkedEvent = mixpanelEvents[mixpanelEvent];
|
|
544
|
+
linkedEvent.events[event] = {
|
|
545
|
+
props
|
|
546
|
+
};
|
|
547
|
+
linkedEvent.resolvedDependencies.push(event);
|
|
548
|
+
const allDependenciesResolved = dependencies.every((value) => linkedEvent.resolvedDependencies.includes(value));
|
|
549
|
+
if (allDependenciesResolved) {
|
|
550
|
+
const aggregatedProps = dependencies.reduce((acc, curr) => {
|
|
551
|
+
if (linkedEvent.events[curr]) {
|
|
552
|
+
return {
|
|
553
|
+
...acc,
|
|
554
|
+
...linkedEvent.events[curr].props
|
|
555
|
+
};
|
|
556
|
+
}
|
|
557
|
+
return acc;
|
|
558
|
+
}, {});
|
|
559
|
+
this.track(mixpanelEvent, aggregatedProps);
|
|
560
|
+
linkedEvent.resolvedDependencies = linkedEvent.resolvedDependencies.filter((event2) => !dependencies.includes(event2));
|
|
561
|
+
dependencies.forEach((event2) => {
|
|
562
|
+
delete linkedEvent.events[event2];
|
|
349
563
|
});
|
|
350
564
|
}
|
|
351
565
|
}
|
|
352
566
|
};
|
|
353
567
|
}
|
|
354
|
-
function
|
|
355
|
-
let
|
|
568
|
+
function createTimestampTracker() {
|
|
569
|
+
let timestamp = 0;
|
|
356
570
|
return {
|
|
357
|
-
reset: () =>
|
|
358
|
-
update: () =>
|
|
359
|
-
get: (
|
|
571
|
+
reset: () => timestamp = 0,
|
|
572
|
+
update: () => timestamp = Date.now(),
|
|
573
|
+
get: (delta = false) => delta ? Date.now() - timestamp : timestamp
|
|
360
574
|
};
|
|
361
575
|
}
|
|
362
|
-
const
|
|
363
|
-
function
|
|
364
|
-
return
|
|
576
|
+
const timestampTracker = createTimestampTracker();
|
|
577
|
+
function getRequestHeaders(chatMode) {
|
|
578
|
+
return chatMode === ChatMode.Playground ? {
|
|
365
579
|
headers: {
|
|
366
|
-
[
|
|
580
|
+
[PLAYGROUND_HEADER]: "true"
|
|
367
581
|
}
|
|
368
582
|
} : {};
|
|
369
583
|
}
|
|
370
|
-
async function
|
|
584
|
+
async function createChat(agent, agentsApi, analytics, chatMode, persist = false, chat) {
|
|
371
585
|
try {
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
586
|
+
if (!chat && chatMode !== ChatMode.DirectPlayback) {
|
|
587
|
+
chat = await agentsApi.newChat(agent.id, {
|
|
588
|
+
persist
|
|
589
|
+
}, getRequestHeaders(chatMode));
|
|
590
|
+
analytics.track("agent-chat", {
|
|
591
|
+
event: "created",
|
|
592
|
+
chat_id: chat.id,
|
|
593
|
+
agent_id: agent.id,
|
|
594
|
+
mode: chatMode
|
|
595
|
+
});
|
|
596
|
+
}
|
|
597
|
+
return {
|
|
598
|
+
chat,
|
|
599
|
+
chatMode: (chat == null ? void 0 : chat.chat_mode) ?? chatMode
|
|
382
600
|
};
|
|
383
|
-
} catch (
|
|
601
|
+
} catch (error) {
|
|
384
602
|
try {
|
|
385
|
-
const
|
|
386
|
-
if ((
|
|
603
|
+
const parsedError = JSON.parse(error.message);
|
|
604
|
+
if ((parsedError == null ? void 0 : parsedError.kind) === "InsufficientCreditsError") {
|
|
387
605
|
throw new Error("InsufficientCreditsError");
|
|
388
|
-
|
|
389
|
-
|
|
606
|
+
}
|
|
607
|
+
} catch (e) {
|
|
608
|
+
console.error("Error parsing the error message:", e);
|
|
390
609
|
}
|
|
391
610
|
throw new Error("Cannot create new chat");
|
|
392
611
|
}
|
|
393
612
|
}
|
|
394
|
-
function
|
|
395
|
-
var
|
|
396
|
-
const
|
|
397
|
-
|
|
613
|
+
function getGreetings(agent) {
|
|
614
|
+
var _a;
|
|
615
|
+
const greetings = ((_a = agent.greetings) == null ? void 0 : _a.filter((greeting) => greeting.length > 0)) ?? [];
|
|
616
|
+
if (greetings.length > 0) {
|
|
617
|
+
return greetings[Math.floor(Math.random() * greetings.length)];
|
|
618
|
+
}
|
|
619
|
+
return `Hi! I'm ${agent.preview_name || "My Agent"}. How can I help you?`;
|
|
398
620
|
}
|
|
399
|
-
function
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
621
|
+
function getInitialMessages(content, initialMessages) {
|
|
622
|
+
if (initialMessages && initialMessages.length > 0) {
|
|
623
|
+
return initialMessages;
|
|
624
|
+
}
|
|
625
|
+
return [{
|
|
626
|
+
content,
|
|
627
|
+
id: getRandom(),
|
|
403
628
|
role: "assistant",
|
|
404
629
|
created_at: (/* @__PURE__ */ new Date()).toISOString()
|
|
405
630
|
}];
|
|
406
631
|
}
|
|
407
|
-
function
|
|
408
|
-
return new Promise((
|
|
632
|
+
function connect(options) {
|
|
633
|
+
return new Promise((resolve, reject) => {
|
|
634
|
+
const {
|
|
635
|
+
callbacks,
|
|
636
|
+
host,
|
|
637
|
+
auth
|
|
638
|
+
} = options;
|
|
409
639
|
const {
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
640
|
+
onMessage = null,
|
|
641
|
+
onOpen = null,
|
|
642
|
+
onClose = null,
|
|
643
|
+
onError = null
|
|
644
|
+
} = callbacks || {};
|
|
645
|
+
const socket = new WebSocket(`${host}?authorization=${getAuthHeader(auth)}`);
|
|
646
|
+
socket.onmessage = onMessage;
|
|
647
|
+
socket.onclose = onClose;
|
|
648
|
+
socket.onerror = (e) => {
|
|
649
|
+
console.error(e);
|
|
650
|
+
onError == null ? void 0 : onError("Websocket failed to connect", e);
|
|
651
|
+
reject(e);
|
|
652
|
+
};
|
|
653
|
+
socket.onopen = (e) => {
|
|
654
|
+
onOpen == null ? void 0 : onOpen(e);
|
|
655
|
+
resolve(socket);
|
|
423
656
|
};
|
|
424
657
|
});
|
|
425
658
|
}
|
|
426
|
-
async function
|
|
659
|
+
async function connectWithRetries(options) {
|
|
427
660
|
const {
|
|
428
|
-
retries
|
|
429
|
-
} =
|
|
430
|
-
let
|
|
431
|
-
for (let
|
|
661
|
+
retries = 1
|
|
662
|
+
} = options;
|
|
663
|
+
let socket = null;
|
|
664
|
+
for (let attempt = 0; (socket == null ? void 0 : socket.readyState) !== WebSocket.OPEN; attempt++) {
|
|
432
665
|
try {
|
|
433
|
-
|
|
434
|
-
} catch (
|
|
435
|
-
if (
|
|
436
|
-
throw
|
|
437
|
-
|
|
666
|
+
socket = await connect(options);
|
|
667
|
+
} catch (e) {
|
|
668
|
+
if (attempt === retries) {
|
|
669
|
+
throw e;
|
|
670
|
+
}
|
|
671
|
+
await sleep(attempt * 500);
|
|
438
672
|
}
|
|
439
|
-
|
|
673
|
+
}
|
|
674
|
+
return socket;
|
|
440
675
|
}
|
|
441
|
-
async function
|
|
442
|
-
const
|
|
443
|
-
|
|
444
|
-
|
|
676
|
+
async function createSocketManager(auth, host, callbacks) {
|
|
677
|
+
const messageCallbacks = (callbacks == null ? void 0 : callbacks.onMessage) ? [callbacks.onMessage] : [];
|
|
678
|
+
const socket = await connectWithRetries({
|
|
679
|
+
auth,
|
|
680
|
+
host,
|
|
445
681
|
callbacks: {
|
|
446
|
-
onError: (
|
|
447
|
-
var
|
|
448
|
-
return (
|
|
682
|
+
onError: (error) => {
|
|
683
|
+
var _a;
|
|
684
|
+
return (_a = callbacks.onError) == null ? void 0 : _a.call(callbacks, new WsError(error));
|
|
449
685
|
},
|
|
450
|
-
onMessage(
|
|
451
|
-
const
|
|
452
|
-
|
|
686
|
+
onMessage(event) {
|
|
687
|
+
const parsedData = JSON.parse(event.data);
|
|
688
|
+
messageCallbacks.forEach((callback) => callback(parsedData.event, parsedData));
|
|
453
689
|
}
|
|
454
690
|
}
|
|
455
691
|
});
|
|
456
692
|
return {
|
|
457
|
-
socket
|
|
458
|
-
disconnect: () =>
|
|
459
|
-
subscribeToEvents: (
|
|
693
|
+
socket,
|
|
694
|
+
disconnect: () => socket.close(),
|
|
695
|
+
subscribeToEvents: (callback) => messageCallbacks.push(callback)
|
|
460
696
|
};
|
|
461
697
|
}
|
|
462
|
-
function
|
|
463
|
-
if (
|
|
464
|
-
return
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
698
|
+
function getMessageContent(chatEventQueue) {
|
|
699
|
+
if (chatEventQueue["answer"] !== void 0) {
|
|
700
|
+
return chatEventQueue["answer"];
|
|
701
|
+
}
|
|
702
|
+
let currentSequence = 0;
|
|
703
|
+
let content = "";
|
|
704
|
+
while (currentSequence in chatEventQueue) {
|
|
705
|
+
content += chatEventQueue[currentSequence++];
|
|
706
|
+
}
|
|
707
|
+
return content;
|
|
469
708
|
}
|
|
470
|
-
function
|
|
471
|
-
const
|
|
472
|
-
if (!(
|
|
709
|
+
function processChatEvent(event, data, chatEventQueue, items, onNewMessage) {
|
|
710
|
+
const lastMessage = items.messages[items.messages.length - 1];
|
|
711
|
+
if (!(event === ChatProgress.Partial || event === ChatProgress.Answer) || (lastMessage == null ? void 0 : lastMessage.role) !== "assistant") {
|
|
473
712
|
return;
|
|
713
|
+
}
|
|
474
714
|
const {
|
|
475
|
-
content
|
|
476
|
-
sequence
|
|
477
|
-
} =
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
715
|
+
content,
|
|
716
|
+
sequence
|
|
717
|
+
} = data;
|
|
718
|
+
if (event === ChatProgress.Partial) {
|
|
719
|
+
chatEventQueue[sequence] = content;
|
|
720
|
+
} else {
|
|
721
|
+
chatEventQueue["answer"] = content;
|
|
722
|
+
}
|
|
723
|
+
const messageContent = getMessageContent(chatEventQueue);
|
|
724
|
+
if (lastMessage.content !== messageContent || event === ChatProgress.Answer) {
|
|
725
|
+
lastMessage.content = messageContent;
|
|
726
|
+
onNewMessage == null ? void 0 : onNewMessage([...items.messages], event);
|
|
727
|
+
}
|
|
481
728
|
}
|
|
482
|
-
function
|
|
483
|
-
let
|
|
729
|
+
function createMessageEventQueue(analytics, items, options, agentEntity, onStreamDone) {
|
|
730
|
+
let chatEventQueue = {};
|
|
484
731
|
return {
|
|
485
|
-
clearQueue: () =>
|
|
486
|
-
onMessage: (
|
|
487
|
-
var
|
|
488
|
-
if ("content" in
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
732
|
+
clearQueue: () => chatEventQueue = {},
|
|
733
|
+
onMessage: (event, data) => {
|
|
734
|
+
var _a, _b;
|
|
735
|
+
if ("content" in data) {
|
|
736
|
+
processChatEvent(event, data, chatEventQueue, items, options.callbacks.onNewMessage);
|
|
737
|
+
if (event === ChatProgress.Answer) {
|
|
738
|
+
analytics.track("agent-message-received", {
|
|
739
|
+
messages: items.messages.length,
|
|
740
|
+
mode: items.chatMode
|
|
741
|
+
});
|
|
742
|
+
}
|
|
743
|
+
} else {
|
|
744
|
+
const SEvent = StreamEvents;
|
|
745
|
+
const completedEvents = [SEvent.StreamVideoDone, SEvent.StreamVideoError, SEvent.StreamVideoRejected];
|
|
746
|
+
const failedEvents = [SEvent.StreamFailed, SEvent.StreamVideoError, SEvent.StreamVideoRejected];
|
|
747
|
+
const props = getStreamAnalyticsProps(data, agentEntity, {
|
|
748
|
+
mode: items.chatMode
|
|
496
749
|
});
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
}
|
|
750
|
+
event = event;
|
|
751
|
+
if (event === SEvent.StreamVideoCreated) {
|
|
752
|
+
analytics.linkTrack("agent-video", props, SEvent.StreamVideoCreated, ["start"]);
|
|
753
|
+
} else if (completedEvents.includes(event)) {
|
|
754
|
+
const streamEvent = event.split("/")[1];
|
|
755
|
+
if (failedEvents.includes(event)) {
|
|
756
|
+
analytics.track("agent-video", {
|
|
757
|
+
...props,
|
|
758
|
+
event: streamEvent
|
|
759
|
+
});
|
|
760
|
+
} else {
|
|
761
|
+
analytics.linkTrack("agent-video", {
|
|
762
|
+
...props,
|
|
763
|
+
event: streamEvent
|
|
764
|
+
}, event, ["done"]);
|
|
765
|
+
}
|
|
766
|
+
}
|
|
767
|
+
if (failedEvents.includes(event)) {
|
|
768
|
+
(_b = (_a = options.callbacks).onError) == null ? void 0 : _b.call(_a, new Error(`Stream failed with event ${event}`), {
|
|
769
|
+
data
|
|
770
|
+
});
|
|
771
|
+
}
|
|
772
|
+
if (data.event === SEvent.StreamDone) {
|
|
773
|
+
onStreamDone();
|
|
508
774
|
}
|
|
509
|
-
R.includes(r) && ((u = (c = a.callbacks).onError) == null || u.call(c, new Error(`Stream failed with event ${r}`), {
|
|
510
|
-
data: o
|
|
511
|
-
})), o.event === d.StreamDone && s();
|
|
512
775
|
}
|
|
513
776
|
}
|
|
514
777
|
};
|
|
515
778
|
}
|
|
516
|
-
function
|
|
517
|
-
const
|
|
779
|
+
function createClipApi(auth, host, agentId, onError) {
|
|
780
|
+
const client = createClient(auth, `${host}/agents/${agentId}`, onError);
|
|
518
781
|
return {
|
|
519
|
-
createStream(
|
|
520
|
-
return
|
|
521
|
-
output_resolution:
|
|
522
|
-
compatibility_mode:
|
|
523
|
-
stream_warmup:
|
|
524
|
-
session_timeout:
|
|
525
|
-
stream_greeting:
|
|
782
|
+
createStream(options) {
|
|
783
|
+
return client.post("/streams", {
|
|
784
|
+
output_resolution: options.output_resolution,
|
|
785
|
+
compatibility_mode: options.compatibility_mode,
|
|
786
|
+
stream_warmup: options.stream_warmup,
|
|
787
|
+
session_timeout: options.session_timeout,
|
|
788
|
+
stream_greeting: options.stream_greeting
|
|
526
789
|
});
|
|
527
790
|
},
|
|
528
|
-
startConnection(
|
|
529
|
-
return
|
|
530
|
-
session_id:
|
|
531
|
-
answer
|
|
791
|
+
startConnection(streamId, answer, sessionId) {
|
|
792
|
+
return client.post(`/streams/${streamId}/sdp`, {
|
|
793
|
+
session_id: sessionId,
|
|
794
|
+
answer
|
|
532
795
|
});
|
|
533
796
|
},
|
|
534
|
-
addIceCandidate(
|
|
535
|
-
return
|
|
536
|
-
session_id:
|
|
537
|
-
...
|
|
797
|
+
addIceCandidate(streamId, candidate, sessionId) {
|
|
798
|
+
return client.post(`/streams/${streamId}/ice`, {
|
|
799
|
+
session_id: sessionId,
|
|
800
|
+
...candidate
|
|
538
801
|
});
|
|
539
802
|
},
|
|
540
|
-
sendStreamRequest(
|
|
541
|
-
return
|
|
542
|
-
session_id:
|
|
543
|
-
...
|
|
803
|
+
sendStreamRequest(streamId, sessionId, payload) {
|
|
804
|
+
return client.post(`/streams/${streamId}`, {
|
|
805
|
+
session_id: sessionId,
|
|
806
|
+
...payload
|
|
544
807
|
});
|
|
545
808
|
},
|
|
546
|
-
close(
|
|
547
|
-
return
|
|
548
|
-
session_id:
|
|
809
|
+
close(streamId, sessionId) {
|
|
810
|
+
return client.delete(`/streams/${streamId}`, {
|
|
811
|
+
session_id: sessionId
|
|
549
812
|
});
|
|
550
813
|
}
|
|
551
814
|
};
|
|
552
815
|
}
|
|
553
|
-
function
|
|
554
|
-
const
|
|
816
|
+
function createTalkApi(auth, host, agentId, onError) {
|
|
817
|
+
const client = createClient(auth, `${host}/agents/${agentId}`, onError);
|
|
555
818
|
return {
|
|
556
|
-
createStream(
|
|
557
|
-
return
|
|
558
|
-
driver_url:
|
|
559
|
-
face:
|
|
560
|
-
config:
|
|
561
|
-
output_resolution:
|
|
562
|
-
compatibility_mode:
|
|
563
|
-
stream_warmup:
|
|
564
|
-
session_timeout:
|
|
565
|
-
stream_greeting:
|
|
566
|
-
},
|
|
819
|
+
createStream(streamOptions, options) {
|
|
820
|
+
return client.post("/streams", {
|
|
821
|
+
driver_url: streamOptions.driver_url,
|
|
822
|
+
face: streamOptions.face,
|
|
823
|
+
config: streamOptions.config,
|
|
824
|
+
output_resolution: streamOptions.output_resolution,
|
|
825
|
+
compatibility_mode: streamOptions.compatibility_mode,
|
|
826
|
+
stream_warmup: streamOptions.stream_warmup,
|
|
827
|
+
session_timeout: streamOptions.session_timeout,
|
|
828
|
+
stream_greeting: streamOptions.stream_greeting
|
|
829
|
+
}, options);
|
|
567
830
|
},
|
|
568
|
-
startConnection(
|
|
569
|
-
return
|
|
570
|
-
session_id:
|
|
571
|
-
answer
|
|
572
|
-
},
|
|
831
|
+
startConnection(streamId, answer, sessionId, options) {
|
|
832
|
+
return client.post(`/streams/${streamId}/sdp`, {
|
|
833
|
+
session_id: sessionId,
|
|
834
|
+
answer
|
|
835
|
+
}, options);
|
|
573
836
|
},
|
|
574
|
-
addIceCandidate(
|
|
575
|
-
return
|
|
576
|
-
session_id:
|
|
577
|
-
...
|
|
578
|
-
},
|
|
837
|
+
addIceCandidate(streamId, candidate, sessionId, options) {
|
|
838
|
+
return client.post(`/streams/${streamId}/ice`, {
|
|
839
|
+
session_id: sessionId,
|
|
840
|
+
...candidate
|
|
841
|
+
}, options);
|
|
579
842
|
},
|
|
580
|
-
sendStreamRequest(
|
|
581
|
-
return
|
|
582
|
-
session_id:
|
|
583
|
-
...
|
|
584
|
-
},
|
|
843
|
+
sendStreamRequest(streamId, sessionId, payload, options) {
|
|
844
|
+
return client.post(`/streams/${streamId}`, {
|
|
845
|
+
session_id: sessionId,
|
|
846
|
+
...payload
|
|
847
|
+
}, options);
|
|
585
848
|
},
|
|
586
|
-
close(
|
|
587
|
-
return
|
|
588
|
-
session_id:
|
|
589
|
-
},
|
|
849
|
+
close(streamId, sessionId, options) {
|
|
850
|
+
return client.delete(`/streams/${streamId}`, {
|
|
851
|
+
session_id: sessionId
|
|
852
|
+
}, options);
|
|
590
853
|
}
|
|
591
854
|
};
|
|
592
855
|
}
|
|
593
|
-
function
|
|
594
|
-
const
|
|
856
|
+
function createAggregateReport(start, end, lowFpsCount) {
|
|
857
|
+
const duration = (end.timestamp - start.timestamp) / 1e3;
|
|
595
858
|
return {
|
|
596
|
-
duration
|
|
597
|
-
bytesReceived:
|
|
598
|
-
bitrate: Math.round((
|
|
599
|
-
packetsReceived:
|
|
600
|
-
packetsLost:
|
|
601
|
-
framesDropped:
|
|
602
|
-
framesDecoded:
|
|
603
|
-
jitter:
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
859
|
+
duration,
|
|
860
|
+
bytesReceived: end.bytesReceived - start.bytesReceived,
|
|
861
|
+
bitrate: Math.round((end.bytesReceived - start.bytesReceived) * 8 / duration),
|
|
862
|
+
packetsReceived: end.packetsReceived - start.packetsReceived,
|
|
863
|
+
packetsLost: end.packetsLost - start.packetsLost,
|
|
864
|
+
framesDropped: end.framesDropped - start.framesDropped,
|
|
865
|
+
framesDecoded: end.framesDecoded - start.framesDecoded,
|
|
866
|
+
jitter: end.jitter,
|
|
867
|
+
avgJitterDelayInInterval: (end.jitterBufferDelay - start.jitterBufferDelay) / (end.jitterBufferEmittedCount - start.jitterBufferEmittedCount),
|
|
868
|
+
jitterBufferEmittedCount: end.jitterBufferEmittedCount - start.jitterBufferEmittedCount,
|
|
869
|
+
jitterBufferDelay: (end.jitterBufferDelay - start.jitterBufferDelay) / duration,
|
|
870
|
+
framesPerSecond: end.framesPerSecond,
|
|
871
|
+
freezeCount: end.freezeCount - start.freezeCount,
|
|
872
|
+
freezeDuration: end.freezeDuration - start.freezeDuration,
|
|
873
|
+
lowFpsCount
|
|
609
874
|
};
|
|
610
875
|
}
|
|
611
|
-
function
|
|
612
|
-
return
|
|
876
|
+
function extractAnomalies(stats) {
|
|
877
|
+
return stats.filter((report) => report.freezeCount > 0 || report.framesPerSecond < 21 || report.framesDropped > 0 || report.packetsLost > 0).map((report) => {
|
|
613
878
|
const {
|
|
614
|
-
timestamp
|
|
615
|
-
...
|
|
616
|
-
} =
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
causes
|
|
879
|
+
timestamp,
|
|
880
|
+
...updatedReport
|
|
881
|
+
} = report;
|
|
882
|
+
const causes = [];
|
|
883
|
+
if (report.freezeCount > 0) {
|
|
884
|
+
causes.push("freeze");
|
|
885
|
+
}
|
|
886
|
+
if (report.framesPerSecond < 21) {
|
|
887
|
+
causes.push("low fps");
|
|
888
|
+
}
|
|
889
|
+
if (report.framesDropped > 0) {
|
|
890
|
+
causes.push("frames dropped");
|
|
891
|
+
}
|
|
892
|
+
if (report.packetsLost > 0) {
|
|
893
|
+
causes.push("packet loss");
|
|
894
|
+
}
|
|
895
|
+
return {
|
|
896
|
+
...updatedReport,
|
|
897
|
+
causes
|
|
620
898
|
};
|
|
621
899
|
});
|
|
622
900
|
}
|
|
623
|
-
function
|
|
624
|
-
let
|
|
625
|
-
for (const
|
|
626
|
-
if (
|
|
901
|
+
function formatStats(stats) {
|
|
902
|
+
let codec = "";
|
|
903
|
+
for (const report of stats.values()) {
|
|
904
|
+
if (report && report.type === "codec" && report.mimeType.startsWith("video")) {
|
|
905
|
+
codec = report.mimeType.split("/")[1];
|
|
906
|
+
}
|
|
907
|
+
if (report && report.type === "inbound-rtp" && report.kind === "video") {
|
|
627
908
|
return {
|
|
628
|
-
codec
|
|
629
|
-
timestamp:
|
|
630
|
-
bytesReceived:
|
|
631
|
-
packetsReceived:
|
|
632
|
-
packetsLost:
|
|
633
|
-
framesDropped:
|
|
634
|
-
framesDecoded:
|
|
635
|
-
jitter:
|
|
636
|
-
jitterBufferDelay:
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
909
|
+
codec,
|
|
910
|
+
timestamp: report.timestamp,
|
|
911
|
+
bytesReceived: report.bytesReceived,
|
|
912
|
+
packetsReceived: report.packetsReceived,
|
|
913
|
+
packetsLost: report.packetsLost,
|
|
914
|
+
framesDropped: report.framesDropped,
|
|
915
|
+
framesDecoded: report.framesDecoded,
|
|
916
|
+
jitter: report.jitter,
|
|
917
|
+
jitterBufferDelay: report.jitterBufferDelay,
|
|
918
|
+
jitterBufferEmittedCount: report.jitterBufferEmittedCount,
|
|
919
|
+
avgJitterDelayInInterval: report.jitterBufferDelay / report.jitterBufferEmittedCount,
|
|
920
|
+
frameWidth: report.frameWidth,
|
|
921
|
+
frameHeight: report.frameHeight,
|
|
922
|
+
framesPerSecond: report.framesPerSecond,
|
|
923
|
+
freezeCount: report.freezeCount,
|
|
924
|
+
freezeDuration: report.totalFreezesDuration
|
|
642
925
|
};
|
|
926
|
+
}
|
|
927
|
+
}
|
|
643
928
|
return {};
|
|
644
929
|
}
|
|
645
|
-
function
|
|
646
|
-
const
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
930
|
+
function createVideoStatsReport(stats, interval2, previousStats) {
|
|
931
|
+
const differentialReport = stats.map((report, index) => {
|
|
932
|
+
if (index === 0) {
|
|
933
|
+
if (!previousStats) {
|
|
934
|
+
return {
|
|
935
|
+
timestamp: report.timestamp,
|
|
936
|
+
duration: 0,
|
|
937
|
+
bytesReceived: report.bytesReceived,
|
|
938
|
+
bitrate: report.bytesReceived * 8 / (interval2 / 1e3),
|
|
939
|
+
packetsReceived: report.packetsReceived,
|
|
940
|
+
packetsLost: report.packetsLost,
|
|
941
|
+
framesDropped: report.framesDropped,
|
|
942
|
+
framesDecoded: report.framesDecoded,
|
|
943
|
+
jitter: report.jitter,
|
|
944
|
+
jitterBufferDelay: report.jitterBufferDelay,
|
|
945
|
+
jitterBufferEmittedCount: report.jitterBufferEmittedCount,
|
|
946
|
+
avgJitterDelayInInterval: report.jitterBufferDelay / report.jitterBufferEmittedCount,
|
|
947
|
+
framesPerSecond: report.framesPerSecond,
|
|
948
|
+
freezeCount: report.freezeCount,
|
|
949
|
+
freezeDuration: report.freezeDuration
|
|
950
|
+
};
|
|
951
|
+
}
|
|
952
|
+
return {
|
|
953
|
+
timestamp: report.timestamp,
|
|
954
|
+
duration: 0,
|
|
955
|
+
bytesReceived: report.bytesReceived - previousStats.bytesReceived,
|
|
956
|
+
bitrate: (report.bytesReceived - previousStats.bytesReceived) * 8 / (interval2 / 1e3),
|
|
957
|
+
packetsReceived: report.packetsReceived - previousStats.packetsReceived,
|
|
958
|
+
packetsLost: report.packetsLost - previousStats.packetsLost,
|
|
959
|
+
framesDropped: report.framesDropped - previousStats.framesDropped,
|
|
960
|
+
framesDecoded: report.framesDecoded - previousStats.framesDecoded,
|
|
961
|
+
jitter: report.jitter,
|
|
962
|
+
jitterBufferDelay: report.jitterBufferDelay - previousStats.jitterBufferDelay,
|
|
963
|
+
jitterBufferEmittedCount: report.jitterBufferEmittedCount - previousStats.jitterBufferEmittedCount,
|
|
964
|
+
avgJitterDelayInInterval: (report.jitterBufferDelay - previousStats.jitterBufferDelay) / (report.jitterBufferEmittedCount - previousStats.jitterBufferEmittedCount),
|
|
965
|
+
framesPerSecond: report.framesPerSecond,
|
|
966
|
+
freezeCount: report.freezeCount - previousStats.freezeCount,
|
|
967
|
+
freezeDuration: report.freezeDuration - previousStats.freezeDuration
|
|
968
|
+
};
|
|
969
|
+
}
|
|
970
|
+
return {
|
|
971
|
+
timestamp: report.timestamp,
|
|
972
|
+
duration: interval2 * index / 1e3,
|
|
973
|
+
bytesReceived: report.bytesReceived - stats[index - 1].bytesReceived,
|
|
974
|
+
bitrate: (report.bytesReceived - stats[index - 1].bytesReceived) * 8 / (interval2 / 1e3),
|
|
975
|
+
packetsReceived: report.packetsReceived - stats[index - 1].packetsReceived,
|
|
976
|
+
packetsLost: report.packetsLost - stats[index - 1].packetsLost,
|
|
977
|
+
framesDropped: report.framesDropped - stats[index - 1].framesDropped,
|
|
978
|
+
framesDecoded: report.framesDecoded - stats[index - 1].framesDecoded,
|
|
979
|
+
jitter: report.jitter,
|
|
980
|
+
jitterBufferDelay: report.jitterBufferDelay - stats[index - 1].jitterBufferDelay,
|
|
981
|
+
jitterBufferEmittedCount: report.jitterBufferEmittedCount - stats[index - 1].jitterBufferEmittedCount,
|
|
982
|
+
avgJitterDelayInInterval: (report.jitterBufferDelay - stats[index - 1].jitterBufferDelay) / (report.jitterBufferEmittedCount - stats[index - 1].jitterBufferEmittedCount),
|
|
983
|
+
framesPerSecond: report.framesPerSecond,
|
|
984
|
+
freezeCount: report.freezeCount - stats[index - 1].freezeCount,
|
|
985
|
+
freezeDuration: report.freezeDuration - stats[index - 1].freezeDuration
|
|
986
|
+
};
|
|
987
|
+
});
|
|
988
|
+
const anomalies = extractAnomalies(differentialReport);
|
|
989
|
+
const lowFpsCount = anomalies.reduce((acc, report) => acc + (report.causes.includes("low fps") ? 1 : 0), 0);
|
|
990
|
+
const avgJittersSamples = differentialReport.map((stat) => stat.avgJitterDelayInInterval);
|
|
689
991
|
return {
|
|
690
992
|
webRTCStats: {
|
|
691
|
-
anomalies
|
|
692
|
-
aggregateReport:
|
|
993
|
+
anomalies,
|
|
994
|
+
aggregateReport: createAggregateReport(stats[0], stats[stats.length - 1], lowFpsCount),
|
|
995
|
+
minJitterDelayInInterval: Math.min(...avgJittersSamples),
|
|
996
|
+
maxJitterDelayInInterval: Math.max(...avgJittersSamples),
|
|
997
|
+
avgJitterDelayInInterval: average(avgJittersSamples)
|
|
693
998
|
},
|
|
694
|
-
codec:
|
|
695
|
-
resolution: `${
|
|
999
|
+
codec: stats[0].codec,
|
|
1000
|
+
resolution: `${stats[0].frameWidth}x${stats[0].frameHeight}`
|
|
696
1001
|
};
|
|
697
1002
|
}
|
|
698
|
-
const
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
1003
|
+
const interval = 100;
|
|
1004
|
+
const notReceivingIntervalsThreshold = Math.max(Math.ceil(400 / interval), 1);
|
|
1005
|
+
const LOW_JITTER_TRESHOLD = 0.25;
|
|
1006
|
+
const HIGH_JITTER_TRESHOLD = 0.28;
|
|
1007
|
+
function createVideoStatsAnalyzer() {
|
|
1008
|
+
let lastFramesReceived = 0;
|
|
1009
|
+
let prevDelay;
|
|
1010
|
+
let prevCount;
|
|
1011
|
+
let avgJitterDelayInInterval = 0;
|
|
1012
|
+
return (stats) => {
|
|
1013
|
+
for (const report of stats.values()) {
|
|
1014
|
+
if (report && report.type === "inbound-rtp" && report.kind === "video") {
|
|
1015
|
+
const delay = report.jitterBufferDelay;
|
|
1016
|
+
const count = report.jitterBufferEmittedCount;
|
|
1017
|
+
if (prevCount && count > prevCount) {
|
|
1018
|
+
const deltaDelay = delay - prevDelay;
|
|
1019
|
+
const deltaCount = count - prevCount;
|
|
1020
|
+
avgJitterDelayInInterval = deltaDelay / deltaCount;
|
|
1021
|
+
}
|
|
1022
|
+
prevDelay = delay;
|
|
1023
|
+
prevCount = count;
|
|
1024
|
+
const currFramesReceived = report.framesDecoded;
|
|
1025
|
+
const isReceiving = currFramesReceived - lastFramesReceived > 0;
|
|
1026
|
+
lastFramesReceived = currFramesReceived;
|
|
1027
|
+
return {
|
|
1028
|
+
isReceiving,
|
|
1029
|
+
avgJitterDelayInInterval,
|
|
1030
|
+
freezeCount: report.freezeCount
|
|
1031
|
+
};
|
|
706
1032
|
}
|
|
707
|
-
|
|
1033
|
+
}
|
|
1034
|
+
return {
|
|
1035
|
+
isReceiving: false,
|
|
1036
|
+
avgJitterDelayInInterval
|
|
1037
|
+
};
|
|
708
1038
|
};
|
|
709
1039
|
}
|
|
710
|
-
function
|
|
711
|
-
const
|
|
712
|
-
let
|
|
713
|
-
|
|
1040
|
+
function pollStats(peerConnection, getIsConnected, onConnected, onVideoStateChange, onConnectivityStateChange, warmup = false, shouldWaitForGreeting = false) {
|
|
1041
|
+
const streamsBeforeReady = warmup ? 1 : 0;
|
|
1042
|
+
let allStats = [];
|
|
1043
|
+
let previousStats;
|
|
1044
|
+
let notReceivingNumIntervals = 0;
|
|
1045
|
+
let isStreaming = false;
|
|
1046
|
+
let streamsCount = 0;
|
|
1047
|
+
let prevLowConnState = ConnectivityState.Unknown;
|
|
1048
|
+
let currLowConnState = ConnectivityState.Unknown;
|
|
1049
|
+
let currFreezeCount = 0;
|
|
1050
|
+
let prevFreezeCount = 0;
|
|
1051
|
+
const isReceivingVideoBytes = createVideoStatsAnalyzer();
|
|
714
1052
|
return setInterval(async () => {
|
|
715
|
-
const
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
1053
|
+
const stats = await peerConnection.getStats();
|
|
1054
|
+
const {
|
|
1055
|
+
isReceiving,
|
|
1056
|
+
avgJitterDelayInInterval,
|
|
1057
|
+
freezeCount
|
|
1058
|
+
} = isReceivingVideoBytes(stats);
|
|
1059
|
+
const slimStats = formatStats(stats);
|
|
1060
|
+
if (isReceiving) {
|
|
1061
|
+
notReceivingNumIntervals = 0;
|
|
1062
|
+
currFreezeCount = freezeCount - prevFreezeCount;
|
|
1063
|
+
currLowConnState = avgJitterDelayInInterval < LOW_JITTER_TRESHOLD ? ConnectivityState.Strong : avgJitterDelayInInterval > HIGH_JITTER_TRESHOLD && currFreezeCount > 1 ? ConnectivityState.Weak : prevLowConnState;
|
|
1064
|
+
if (currLowConnState !== prevLowConnState) {
|
|
1065
|
+
onConnectivityStateChange == null ? void 0 : onConnectivityStateChange(currLowConnState);
|
|
1066
|
+
prevLowConnState = currLowConnState;
|
|
1067
|
+
prevFreezeCount += currFreezeCount;
|
|
1068
|
+
currFreezeCount = 0;
|
|
1069
|
+
}
|
|
1070
|
+
if (!isStreaming) {
|
|
1071
|
+
onVideoStateChange == null ? void 0 : onVideoStateChange(StreamingState.Start);
|
|
1072
|
+
if (shouldWaitForGreeting && streamsCount >= streamsBeforeReady && !getIsConnected()) {
|
|
1073
|
+
onConnected();
|
|
1074
|
+
}
|
|
1075
|
+
previousStats = allStats[allStats.length - 1];
|
|
1076
|
+
allStats = [];
|
|
1077
|
+
streamsCount++;
|
|
1078
|
+
isStreaming = true;
|
|
1079
|
+
}
|
|
1080
|
+
allStats.push(slimStats);
|
|
1081
|
+
} else if (isStreaming) {
|
|
1082
|
+
notReceivingNumIntervals++;
|
|
1083
|
+
if (notReceivingNumIntervals >= notReceivingIntervalsThreshold) {
|
|
1084
|
+
const statsReport = createVideoStatsReport(allStats, interval, previousStats);
|
|
1085
|
+
onVideoStateChange == null ? void 0 : onVideoStateChange(StreamingState.Stop, statsReport);
|
|
1086
|
+
if (!shouldWaitForGreeting && !getIsConnected()) {
|
|
1087
|
+
onConnected();
|
|
1088
|
+
}
|
|
1089
|
+
isStreaming = false;
|
|
1090
|
+
}
|
|
721
1091
|
}
|
|
722
|
-
},
|
|
1092
|
+
}, interval);
|
|
723
1093
|
}
|
|
724
|
-
let
|
|
725
|
-
const
|
|
726
|
-
|
|
727
|
-
|
|
1094
|
+
let _debug = false;
|
|
1095
|
+
const log = (message, extra) => _debug && console.log(message, extra);
|
|
1096
|
+
const actualRTCPC = (window.RTCPeerConnection || window.webkitRTCPeerConnection || window.mozRTCPeerConnection).bind(window);
|
|
1097
|
+
function mapConnectionState(state) {
|
|
1098
|
+
switch (state) {
|
|
728
1099
|
case "connected":
|
|
729
|
-
return
|
|
1100
|
+
return ConnectionState.Connected;
|
|
730
1101
|
case "checking":
|
|
731
|
-
return
|
|
1102
|
+
return ConnectionState.Connecting;
|
|
732
1103
|
case "failed":
|
|
733
|
-
return
|
|
1104
|
+
return ConnectionState.Fail;
|
|
734
1105
|
case "new":
|
|
735
|
-
return
|
|
1106
|
+
return ConnectionState.New;
|
|
736
1107
|
case "closed":
|
|
737
|
-
return
|
|
1108
|
+
return ConnectionState.Closed;
|
|
738
1109
|
case "disconnected":
|
|
739
|
-
return
|
|
1110
|
+
return ConnectionState.Disconnected;
|
|
740
1111
|
case "completed":
|
|
741
|
-
return
|
|
1112
|
+
return ConnectionState.Completed;
|
|
742
1113
|
default:
|
|
743
|
-
return
|
|
1114
|
+
return ConnectionState.New;
|
|
1115
|
+
}
|
|
1116
|
+
}
|
|
1117
|
+
function handleLegacyStreamState({
|
|
1118
|
+
statsSignal,
|
|
1119
|
+
dataChannelSignal,
|
|
1120
|
+
onVideoStateChange,
|
|
1121
|
+
report
|
|
1122
|
+
}) {
|
|
1123
|
+
if (statsSignal === StreamingState.Start && dataChannelSignal === StreamingState.Start) {
|
|
1124
|
+
onVideoStateChange == null ? void 0 : onVideoStateChange(StreamingState.Start);
|
|
1125
|
+
} else if (statsSignal === StreamingState.Stop && dataChannelSignal === StreamingState.Stop) {
|
|
1126
|
+
onVideoStateChange == null ? void 0 : onVideoStateChange(StreamingState.Stop, report);
|
|
1127
|
+
}
|
|
1128
|
+
}
|
|
1129
|
+
function handleFluentStreamState({
|
|
1130
|
+
statsSignal,
|
|
1131
|
+
dataChannelSignal,
|
|
1132
|
+
onVideoStateChange,
|
|
1133
|
+
onAgentActivityStateChange,
|
|
1134
|
+
report
|
|
1135
|
+
}) {
|
|
1136
|
+
if (statsSignal === StreamingState.Start) {
|
|
1137
|
+
onVideoStateChange == null ? void 0 : onVideoStateChange(StreamingState.Start);
|
|
1138
|
+
} else if (statsSignal === StreamingState.Stop) {
|
|
1139
|
+
onVideoStateChange == null ? void 0 : onVideoStateChange(StreamingState.Stop, report);
|
|
1140
|
+
}
|
|
1141
|
+
if (dataChannelSignal === StreamingState.Start) {
|
|
1142
|
+
onAgentActivityStateChange == null ? void 0 : onAgentActivityStateChange(AgentActivityState.Talking);
|
|
1143
|
+
} else if (dataChannelSignal === StreamingState.Stop) {
|
|
1144
|
+
onAgentActivityStateChange == null ? void 0 : onAgentActivityStateChange(AgentActivityState.Idle);
|
|
744
1145
|
}
|
|
745
1146
|
}
|
|
746
|
-
function
|
|
747
|
-
|
|
1147
|
+
function handleStreamState({
|
|
1148
|
+
statsSignal,
|
|
1149
|
+
dataChannelSignal,
|
|
1150
|
+
onVideoStateChange,
|
|
1151
|
+
onAgentActivityStateChange,
|
|
1152
|
+
streamType,
|
|
1153
|
+
report
|
|
1154
|
+
}) {
|
|
1155
|
+
if (streamType === StreamType.Legacy) {
|
|
1156
|
+
handleLegacyStreamState({
|
|
1157
|
+
statsSignal,
|
|
1158
|
+
dataChannelSignal,
|
|
1159
|
+
onVideoStateChange,
|
|
1160
|
+
report
|
|
1161
|
+
});
|
|
1162
|
+
} else if (streamType === StreamType.Fluent) {
|
|
1163
|
+
handleFluentStreamState({
|
|
1164
|
+
statsSignal,
|
|
1165
|
+
dataChannelSignal,
|
|
1166
|
+
onVideoStateChange,
|
|
1167
|
+
onAgentActivityStateChange,
|
|
1168
|
+
report
|
|
1169
|
+
});
|
|
1170
|
+
}
|
|
748
1171
|
}
|
|
749
|
-
async function
|
|
750
|
-
debug
|
|
751
|
-
callbacks
|
|
752
|
-
auth
|
|
753
|
-
baseURL
|
|
754
|
-
warmup: r
|
|
1172
|
+
async function createStreamingManager(agentId, agent, {
|
|
1173
|
+
debug = false,
|
|
1174
|
+
callbacks,
|
|
1175
|
+
auth,
|
|
1176
|
+
baseURL = didApiUrl
|
|
755
1177
|
}) {
|
|
756
|
-
|
|
757
|
-
let
|
|
1178
|
+
_debug = debug;
|
|
1179
|
+
let isConnected = false;
|
|
1180
|
+
let isDatachannelOpen = false;
|
|
1181
|
+
let dataChannelSignal = StreamingState.Stop;
|
|
1182
|
+
let statsSignal = StreamingState.Stop;
|
|
1183
|
+
let connectivityState = ConnectivityState.Unknown;
|
|
1184
|
+
const {
|
|
1185
|
+
startConnection,
|
|
1186
|
+
sendStreamRequest,
|
|
1187
|
+
close,
|
|
1188
|
+
createStream,
|
|
1189
|
+
addIceCandidate
|
|
1190
|
+
} = agent.videoType === VideoType.Clip ? createClipApi(auth, baseURL, agentId, callbacks.onError) : createTalkApi(auth, baseURL, agentId, callbacks.onError);
|
|
758
1191
|
const {
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
} =
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
iceServers: P
|
|
771
|
-
}), v = m.createDataChannel("JanusDataChannel");
|
|
772
|
-
if (!l)
|
|
1192
|
+
id: streamIdFromServer,
|
|
1193
|
+
offer,
|
|
1194
|
+
ice_servers,
|
|
1195
|
+
session_id,
|
|
1196
|
+
fluent
|
|
1197
|
+
} = await createStream(agent);
|
|
1198
|
+
const peerConnection = new actualRTCPC({
|
|
1199
|
+
iceServers: ice_servers
|
|
1200
|
+
});
|
|
1201
|
+
const pcDataChannel = peerConnection.createDataChannel("JanusDataChannel");
|
|
1202
|
+
if (!session_id) {
|
|
773
1203
|
throw new Error("Could not create session_id");
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
var
|
|
780
|
-
|
|
1204
|
+
}
|
|
1205
|
+
const streamType = fluent ? StreamType.Fluent : StreamType.Legacy;
|
|
1206
|
+
const warmup = agent.stream_warmup && !fluent;
|
|
1207
|
+
const getIsConnected = () => isConnected;
|
|
1208
|
+
const onConnected = () => {
|
|
1209
|
+
var _a;
|
|
1210
|
+
isConnected = true;
|
|
1211
|
+
if (isDatachannelOpen) {
|
|
1212
|
+
(_a = callbacks.onConnectionStateChange) == null ? void 0 : _a.call(callbacks, ConnectionState.Connected);
|
|
1213
|
+
}
|
|
1214
|
+
};
|
|
1215
|
+
const videoStatsInterval = pollStats(peerConnection, getIsConnected, onConnected, (state, report) => handleStreamState({
|
|
1216
|
+
statsSignal: state,
|
|
1217
|
+
dataChannelSignal: streamType === StreamType.Legacy ? dataChannelSignal : void 0,
|
|
1218
|
+
onVideoStateChange: callbacks.onVideoStateChange,
|
|
1219
|
+
onAgentActivityStateChange: callbacks.onAgentActivityStateChange,
|
|
1220
|
+
report,
|
|
1221
|
+
streamType
|
|
1222
|
+
}), (state) => {
|
|
1223
|
+
var _a;
|
|
1224
|
+
return (_a = callbacks.onConnectivityStateChange) == null ? void 0 : _a.call(callbacks, connectivityState);
|
|
1225
|
+
}, warmup, !!agent.stream_greeting);
|
|
1226
|
+
peerConnection.onicecandidate = (event) => {
|
|
1227
|
+
var _a;
|
|
1228
|
+
log("peerConnection.onicecandidate", event);
|
|
781
1229
|
try {
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
}
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
1230
|
+
if (event.candidate && event.candidate.sdpMid && event.candidate.sdpMLineIndex !== null) {
|
|
1231
|
+
addIceCandidate(streamIdFromServer, {
|
|
1232
|
+
candidate: event.candidate.candidate,
|
|
1233
|
+
sdpMid: event.candidate.sdpMid,
|
|
1234
|
+
sdpMLineIndex: event.candidate.sdpMLineIndex
|
|
1235
|
+
}, session_id);
|
|
1236
|
+
} else {
|
|
1237
|
+
addIceCandidate(streamIdFromServer, {
|
|
1238
|
+
candidate: null
|
|
1239
|
+
}, session_id);
|
|
1240
|
+
}
|
|
1241
|
+
} catch (e) {
|
|
1242
|
+
(_a = callbacks.onError) == null ? void 0 : _a.call(callbacks, e, {
|
|
1243
|
+
streamId: streamIdFromServer
|
|
792
1244
|
});
|
|
793
1245
|
}
|
|
794
|
-
}
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
|
|
1246
|
+
};
|
|
1247
|
+
pcDataChannel.onopen = () => {
|
|
1248
|
+
isDatachannelOpen = true;
|
|
1249
|
+
if (!warmup && !agent.stream_greeting || isConnected) {
|
|
1250
|
+
onConnected();
|
|
1251
|
+
}
|
|
1252
|
+
};
|
|
1253
|
+
pcDataChannel.onmessage = (event) => {
|
|
1254
|
+
if (event.data in DataChannelSignalMap) {
|
|
1255
|
+
dataChannelSignal = DataChannelSignalMap[event.data];
|
|
1256
|
+
handleStreamState({
|
|
1257
|
+
statsSignal: streamType === StreamType.Legacy ? statsSignal : void 0,
|
|
1258
|
+
dataChannelSignal,
|
|
1259
|
+
onVideoStateChange: callbacks.onVideoStateChange,
|
|
1260
|
+
onAgentActivityStateChange: callbacks.onAgentActivityStateChange,
|
|
1261
|
+
streamType
|
|
1262
|
+
});
|
|
1263
|
+
}
|
|
1264
|
+
};
|
|
1265
|
+
peerConnection.oniceconnectionstatechange = () => {
|
|
1266
|
+
var _a;
|
|
1267
|
+
log("peerConnection.oniceconnectionstatechange => " + peerConnection.iceConnectionState);
|
|
1268
|
+
const newState = mapConnectionState(peerConnection.iceConnectionState);
|
|
1269
|
+
if (newState !== ConnectionState.Connected) {
|
|
1270
|
+
(_a = callbacks.onConnectionStateChange) == null ? void 0 : _a.call(callbacks, newState);
|
|
1271
|
+
}
|
|
1272
|
+
};
|
|
1273
|
+
peerConnection.ontrack = (event) => {
|
|
1274
|
+
var _a;
|
|
1275
|
+
log("peerConnection.ontrack", event);
|
|
1276
|
+
(_a = callbacks.onSrcObjectReady) == null ? void 0 : _a.call(callbacks, event.streams[0]);
|
|
1277
|
+
};
|
|
1278
|
+
await peerConnection.setRemoteDescription(offer);
|
|
1279
|
+
log("set remote description OK");
|
|
1280
|
+
const sessionClientAnswer = await peerConnection.createAnswer();
|
|
1281
|
+
log("create answer OK");
|
|
1282
|
+
await peerConnection.setLocalDescription(sessionClientAnswer);
|
|
1283
|
+
log("set local description OK");
|
|
1284
|
+
await startConnection(streamIdFromServer, sessionClientAnswer, session_id);
|
|
1285
|
+
log("start connection OK");
|
|
1286
|
+
return {
|
|
809
1287
|
/**
|
|
810
1288
|
* Method to send request to server to get clip or talk depend on you payload
|
|
811
1289
|
* @param payload
|
|
812
1290
|
*/
|
|
813
|
-
speak(
|
|
814
|
-
return
|
|
1291
|
+
speak(payload) {
|
|
1292
|
+
return sendStreamRequest(streamIdFromServer, session_id, payload);
|
|
815
1293
|
},
|
|
816
1294
|
/**
|
|
817
1295
|
* Method to close RTC connection
|
|
818
1296
|
*/
|
|
819
1297
|
async disconnect() {
|
|
820
|
-
var
|
|
821
|
-
if (
|
|
822
|
-
const
|
|
823
|
-
if (
|
|
824
|
-
if (
|
|
825
|
-
(
|
|
1298
|
+
var _a, _b, _c;
|
|
1299
|
+
if (streamIdFromServer) {
|
|
1300
|
+
const state = mapConnectionState(peerConnection.iceConnectionState);
|
|
1301
|
+
if (peerConnection) {
|
|
1302
|
+
if (state === ConnectionState.New) {
|
|
1303
|
+
(_a = callbacks.onVideoStateChange) == null ? void 0 : _a.call(callbacks, StreamingState.Stop);
|
|
1304
|
+
clearInterval(videoStatsInterval);
|
|
826
1305
|
return;
|
|
827
1306
|
}
|
|
828
|
-
|
|
1307
|
+
peerConnection.close();
|
|
1308
|
+
peerConnection.oniceconnectionstatechange = null;
|
|
1309
|
+
peerConnection.onnegotiationneeded = null;
|
|
1310
|
+
peerConnection.onicecandidate = null;
|
|
1311
|
+
peerConnection.ontrack = null;
|
|
829
1312
|
}
|
|
830
1313
|
try {
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
1314
|
+
if (state === ConnectionState.Connected) {
|
|
1315
|
+
await close(streamIdFromServer, session_id).catch((_) => {
|
|
1316
|
+
});
|
|
1317
|
+
}
|
|
1318
|
+
} catch (e) {
|
|
1319
|
+
log("Error on close stream connection", e);
|
|
835
1320
|
}
|
|
836
|
-
(
|
|
1321
|
+
(_b = callbacks.onVideoStateChange) == null ? void 0 : _b.call(callbacks, StreamingState.Stop);
|
|
1322
|
+
(_c = callbacks.onAgentActivityStateChange) == null ? void 0 : _c.call(callbacks, AgentActivityState.Idle);
|
|
1323
|
+
clearInterval(videoStatsInterval);
|
|
837
1324
|
}
|
|
838
1325
|
},
|
|
839
1326
|
/**
|
|
840
1327
|
* Session identifier information, should be returned in the body of all streaming requests
|
|
841
1328
|
*/
|
|
842
|
-
sessionId:
|
|
1329
|
+
sessionId: session_id,
|
|
843
1330
|
/**
|
|
844
1331
|
* Id of current RTC stream
|
|
845
1332
|
*/
|
|
846
|
-
streamId:
|
|
1333
|
+
streamId: streamIdFromServer,
|
|
1334
|
+
streamType
|
|
847
1335
|
};
|
|
848
1336
|
}
|
|
849
|
-
function
|
|
850
|
-
var
|
|
1337
|
+
function getAgentStreamArgs(agent, options, greeting) {
|
|
1338
|
+
var _a;
|
|
851
1339
|
const {
|
|
852
|
-
streamOptions
|
|
853
|
-
} =
|
|
1340
|
+
streamOptions
|
|
1341
|
+
} = options ?? {};
|
|
854
1342
|
return {
|
|
855
|
-
videoType:
|
|
856
|
-
output_resolution:
|
|
857
|
-
session_timeout:
|
|
858
|
-
stream_warmup:
|
|
859
|
-
compatibility_mode:
|
|
860
|
-
stream_greeting:
|
|
1343
|
+
videoType: mapVideoType(agent.presenter.type),
|
|
1344
|
+
output_resolution: streamOptions == null ? void 0 : streamOptions.outputResolution,
|
|
1345
|
+
session_timeout: streamOptions == null ? void 0 : streamOptions.sessionTimeout,
|
|
1346
|
+
stream_warmup: streamOptions == null ? void 0 : streamOptions.streamWarmup,
|
|
1347
|
+
compatibility_mode: streamOptions == null ? void 0 : streamOptions.compatibilityMode,
|
|
1348
|
+
stream_greeting: getAgentType(agent.presenter) !== "clip" && ((_a = options == null ? void 0 : options.streamOptions) == null ? void 0 : _a.streamGreeting) ? greeting : void 0
|
|
861
1349
|
};
|
|
862
1350
|
}
|
|
863
|
-
function
|
|
864
|
-
|
|
865
|
-
|
|
866
|
-
|
|
867
|
-
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
|
|
1351
|
+
function handleStateChange(state, agent, statsReport, analytics) {
|
|
1352
|
+
if (timestampTracker.get() > 0) {
|
|
1353
|
+
if (state === StreamingState.Start) {
|
|
1354
|
+
analytics.linkTrack("agent-video", {
|
|
1355
|
+
event: "start",
|
|
1356
|
+
latency: timestampTracker.get(true)
|
|
1357
|
+
}, "start", [StreamEvents.StreamVideoCreated]);
|
|
1358
|
+
} else if (state === StreamingState.Stop) {
|
|
1359
|
+
analytics.linkTrack("agent-video", {
|
|
1360
|
+
event: "stop",
|
|
1361
|
+
is_greenscreen: agent.presenter.type === "clip" && agent.presenter.is_greenscreen,
|
|
1362
|
+
background: agent.presenter.type === "clip" && agent.presenter.background,
|
|
1363
|
+
...statsReport
|
|
1364
|
+
}, "done", [StreamEvents.StreamVideoDone]);
|
|
1365
|
+
}
|
|
1366
|
+
}
|
|
873
1367
|
}
|
|
874
|
-
function
|
|
875
|
-
|
|
876
|
-
|
|
1368
|
+
function connectToManager(agent, options, analytics, greeting) {
|
|
1369
|
+
timestampTracker.reset();
|
|
1370
|
+
return new Promise(async (resolve, reject) => {
|
|
877
1371
|
try {
|
|
878
|
-
const
|
|
879
|
-
...
|
|
880
|
-
analytics
|
|
881
|
-
warmup: (r = t.streamOptions) == null ? void 0 : r.streamWarmup,
|
|
1372
|
+
const streamingManager = await createStreamingManager(agent.id, getAgentStreamArgs(agent, options, greeting), {
|
|
1373
|
+
...options,
|
|
1374
|
+
analytics,
|
|
882
1375
|
callbacks: {
|
|
883
|
-
...
|
|
884
|
-
onConnectionStateChange: (
|
|
885
|
-
var
|
|
886
|
-
(
|
|
1376
|
+
...options.callbacks,
|
|
1377
|
+
onConnectionStateChange: (state) => {
|
|
1378
|
+
var _a, _b;
|
|
1379
|
+
(_b = (_a = options.callbacks).onConnectionStateChange) == null ? void 0 : _b.call(_a, state);
|
|
1380
|
+
if (state === ConnectionState.Connected) {
|
|
1381
|
+
resolve(streamingManager);
|
|
1382
|
+
}
|
|
887
1383
|
},
|
|
888
|
-
onVideoStateChange: (
|
|
889
|
-
var
|
|
890
|
-
(
|
|
1384
|
+
onVideoStateChange: (state, statsReport) => {
|
|
1385
|
+
var _a, _b;
|
|
1386
|
+
(_b = (_a = options.callbacks).onVideoStateChange) == null ? void 0 : _b.call(_a, state);
|
|
1387
|
+
handleStateChange(state, agent, statsReport, analytics);
|
|
891
1388
|
}
|
|
892
1389
|
}
|
|
893
1390
|
});
|
|
894
|
-
} catch (
|
|
895
|
-
|
|
1391
|
+
} catch (error) {
|
|
1392
|
+
reject(error);
|
|
896
1393
|
}
|
|
897
1394
|
});
|
|
898
1395
|
}
|
|
899
|
-
async function
|
|
900
|
-
var
|
|
1396
|
+
async function initializeStreamAndChat(agent, options, agentsApi, analytics, chat, greeting) {
|
|
1397
|
+
var _a, _b, _c, _d;
|
|
901
1398
|
const {
|
|
902
|
-
chat:
|
|
903
|
-
chatMode
|
|
904
|
-
} = await
|
|
905
|
-
if (
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
1399
|
+
chat: newChat,
|
|
1400
|
+
chatMode
|
|
1401
|
+
} = await createChat(agent, agentsApi, analytics, options.mode, options.persistentChat, chat);
|
|
1402
|
+
if (chatMode && chatMode !== options.mode) {
|
|
1403
|
+
options.mode = chatMode;
|
|
1404
|
+
(_b = (_a = options.callbacks).onModeChange) == null ? void 0 : _b.call(_a, chatMode);
|
|
1405
|
+
if (chatMode === ChatMode.TextOnly) {
|
|
1406
|
+
(_d = (_c = options.callbacks).onError) == null ? void 0 : _d.call(_c, new ChatModeDowngraded(chatMode));
|
|
1407
|
+
return {
|
|
1408
|
+
chat: newChat
|
|
1409
|
+
};
|
|
1410
|
+
}
|
|
1411
|
+
}
|
|
1412
|
+
const streamingManager = await connectToManager(agent, options, analytics, greeting);
|
|
910
1413
|
return {
|
|
911
|
-
chat:
|
|
912
|
-
streamingManager
|
|
1414
|
+
chat: newChat,
|
|
1415
|
+
streamingManager
|
|
913
1416
|
};
|
|
914
1417
|
}
|
|
915
|
-
async function
|
|
916
|
-
var
|
|
917
|
-
let
|
|
918
|
-
const
|
|
1418
|
+
async function createAgentManager(agent, options) {
|
|
1419
|
+
var _a, _b, _c;
|
|
1420
|
+
let firstConnection = true;
|
|
1421
|
+
const mxKey = options.mixpanelKey || mixpanelKey;
|
|
1422
|
+
const wsURL = options.wsURL || didSocketApiUrl;
|
|
1423
|
+
const baseURL = options.baseURL || didApiUrl;
|
|
1424
|
+
const items = {
|
|
919
1425
|
messages: [],
|
|
920
|
-
chatMode:
|
|
921
|
-
}
|
|
922
|
-
|
|
923
|
-
|
|
924
|
-
|
|
925
|
-
|
|
926
|
-
|
|
927
|
-
|
|
928
|
-
|
|
929
|
-
|
|
930
|
-
|
|
931
|
-
|
|
1426
|
+
chatMode: options.mode || ChatMode.Functional
|
|
1427
|
+
};
|
|
1428
|
+
const agentsApi = createAgentsApi(options.auth, baseURL, options.callbacks.onError);
|
|
1429
|
+
const agentEntity = await agentsApi.getById(agent);
|
|
1430
|
+
const greeting = getGreetings(agentEntity);
|
|
1431
|
+
const analytics = initializeAnalytics({
|
|
1432
|
+
token: mxKey,
|
|
1433
|
+
agent: agentEntity,
|
|
1434
|
+
isEnabled: options.enableAnalitics,
|
|
1435
|
+
distinctId: options.distinctId
|
|
1436
|
+
});
|
|
1437
|
+
const {
|
|
1438
|
+
onMessage,
|
|
1439
|
+
clearQueue
|
|
1440
|
+
} = createMessageEventQueue(analytics, items, options, agentEntity, () => {
|
|
1441
|
+
var _a2;
|
|
1442
|
+
return (_a2 = items.socketManager) == null ? void 0 : _a2.disconnect();
|
|
932
1443
|
});
|
|
933
|
-
|
|
1444
|
+
items.messages = getInitialMessages(greeting, options.initialMessages);
|
|
1445
|
+
(_b = (_a = options.callbacks).onNewMessage) == null ? void 0 : _b.call(_a, [...items.messages], "answer");
|
|
1446
|
+
analytics.track("agent-sdk", {
|
|
934
1447
|
event: "loaded",
|
|
935
|
-
...
|
|
1448
|
+
...getAnalyticsInfo(agentEntity)
|
|
936
1449
|
});
|
|
937
|
-
async function
|
|
938
|
-
var
|
|
939
|
-
(
|
|
940
|
-
|
|
941
|
-
|
|
942
|
-
|
|
943
|
-
|
|
1450
|
+
async function connect2(newChat) {
|
|
1451
|
+
var _a2, _b2, _c2, _d, _e, _f, _g;
|
|
1452
|
+
(_b2 = (_a2 = options.callbacks).onConnectionStateChange) == null ? void 0 : _b2.call(_a2, ConnectionState.Connecting);
|
|
1453
|
+
timestampTracker.reset();
|
|
1454
|
+
if (newChat && !firstConnection) {
|
|
1455
|
+
delete items.chat;
|
|
1456
|
+
items.messages = getInitialMessages(greeting);
|
|
1457
|
+
(_d = (_c2 = options.callbacks).onNewMessage) == null ? void 0 : _d.call(_c2, [...items.messages], "answer");
|
|
1458
|
+
}
|
|
1459
|
+
const websocketPromise = options.mode === ChatMode.DirectPlayback ? Promise.resolve(void 0) : createSocketManager(options.auth, wsURL, {
|
|
1460
|
+
onMessage,
|
|
1461
|
+
onError: options.callbacks.onError
|
|
1462
|
+
});
|
|
1463
|
+
const initPromise = retryOperation(() => {
|
|
1464
|
+
return initializeStreamAndChat(agentEntity, options, agentsApi, analytics, items.chat, newChat ? greeting : void 0);
|
|
1465
|
+
}, {
|
|
944
1466
|
limit: 3,
|
|
945
|
-
timeout:
|
|
1467
|
+
timeout: CONNECTION_RETRY_TIMEOUT_MS,
|
|
946
1468
|
timeoutErrorMessage: "Timeout initializing the stream",
|
|
947
1469
|
// Retry on all errors except for connection errors and rate limit errors, these are already handled in client level.
|
|
948
|
-
shouldRetryFn: (
|
|
1470
|
+
shouldRetryFn: (error) => (error == null ? void 0 : error.message) !== "Could not connect" && error.status !== 429,
|
|
949
1471
|
delayMs: 1e3
|
|
950
|
-
}).catch((
|
|
951
|
-
var
|
|
952
|
-
|
|
953
|
-
|
|
954
|
-
|
|
955
|
-
|
|
956
|
-
|
|
957
|
-
|
|
1472
|
+
}).catch((e) => {
|
|
1473
|
+
var _a3, _b3;
|
|
1474
|
+
changeMode(ChatMode.Maintenance);
|
|
1475
|
+
(_b3 = (_a3 = options.callbacks).onConnectionStateChange) == null ? void 0 : _b3.call(_a3, ConnectionState.Fail);
|
|
1476
|
+
throw e;
|
|
1477
|
+
});
|
|
1478
|
+
const [socketManager, {
|
|
1479
|
+
streamingManager,
|
|
1480
|
+
chat
|
|
1481
|
+
}] = await Promise.all([websocketPromise, initPromise]);
|
|
1482
|
+
if (chat && chat.id !== ((_e = items.chat) == null ? void 0 : _e.id)) {
|
|
1483
|
+
(_g = (_f = options.callbacks).onNewChat) == null ? void 0 : _g.call(_f, chat.id);
|
|
1484
|
+
}
|
|
1485
|
+
items.streamingManager = streamingManager;
|
|
1486
|
+
items.socketManager = socketManager;
|
|
1487
|
+
items.chat = chat;
|
|
1488
|
+
firstConnection = false;
|
|
1489
|
+
changeMode((chat == null ? void 0 : chat.chat_mode) ?? options.mode ?? ChatMode.Functional);
|
|
958
1490
|
}
|
|
959
|
-
async function
|
|
960
|
-
var
|
|
961
|
-
(
|
|
1491
|
+
async function disconnect() {
|
|
1492
|
+
var _a2, _b2, _c2, _d;
|
|
1493
|
+
(_a2 = items.socketManager) == null ? void 0 : _a2.disconnect();
|
|
1494
|
+
await ((_b2 = items.streamingManager) == null ? void 0 : _b2.disconnect());
|
|
1495
|
+
delete items.streamingManager;
|
|
1496
|
+
delete items.socketManager;
|
|
1497
|
+
(_d = (_c2 = options.callbacks).onConnectionStateChange) == null ? void 0 : _d.call(_c2, ConnectionState.Disconnected);
|
|
962
1498
|
}
|
|
963
|
-
async function
|
|
964
|
-
var
|
|
965
|
-
|
|
966
|
-
mode
|
|
967
|
-
|
|
1499
|
+
async function changeMode(mode) {
|
|
1500
|
+
var _a2, _b2;
|
|
1501
|
+
if (mode !== items.chatMode) {
|
|
1502
|
+
analytics.track("agent-mode-change", {
|
|
1503
|
+
mode
|
|
1504
|
+
});
|
|
1505
|
+
items.chatMode = mode;
|
|
1506
|
+
if (items.chatMode !== ChatMode.Functional) {
|
|
1507
|
+
await disconnect();
|
|
1508
|
+
}
|
|
1509
|
+
(_b2 = (_a2 = options.callbacks).onModeChange) == null ? void 0 : _b2.call(_a2, mode);
|
|
1510
|
+
}
|
|
968
1511
|
}
|
|
969
1512
|
return {
|
|
970
|
-
agent:
|
|
971
|
-
|
|
972
|
-
|
|
973
|
-
|
|
974
|
-
|
|
1513
|
+
agent: agentEntity,
|
|
1514
|
+
getStreamType: () => {
|
|
1515
|
+
var _a2;
|
|
1516
|
+
return ((_a2 = items.streamingManager) == null ? void 0 : _a2.streamType) || StreamType.Legacy;
|
|
1517
|
+
},
|
|
1518
|
+
starterMessages: ((_c = agentEntity.knowledge) == null ? void 0 : _c.starter_message) || [],
|
|
1519
|
+
getSTTToken: () => agentsApi.getSTTToken(agentEntity.id),
|
|
1520
|
+
changeMode,
|
|
1521
|
+
enrichAnalytics: analytics.enrich,
|
|
975
1522
|
async connect() {
|
|
976
|
-
var
|
|
977
|
-
await
|
|
1523
|
+
var _a2;
|
|
1524
|
+
await connect2(true);
|
|
1525
|
+
analytics.track("agent-chat", {
|
|
978
1526
|
event: "connect",
|
|
979
|
-
chatId: (
|
|
980
|
-
agentId:
|
|
981
|
-
mode:
|
|
1527
|
+
chatId: (_a2 = items.chat) == null ? void 0 : _a2.id,
|
|
1528
|
+
agentId: agentEntity.id,
|
|
1529
|
+
mode: items.chatMode
|
|
982
1530
|
});
|
|
983
1531
|
},
|
|
984
1532
|
async reconnect() {
|
|
985
|
-
var
|
|
986
|
-
await
|
|
1533
|
+
var _a2;
|
|
1534
|
+
await disconnect();
|
|
1535
|
+
await connect2(false);
|
|
1536
|
+
analytics.track("agent-chat", {
|
|
987
1537
|
event: "reconnect",
|
|
988
|
-
chatId: (
|
|
989
|
-
agentId:
|
|
990
|
-
mode:
|
|
1538
|
+
chatId: (_a2 = items.chat) == null ? void 0 : _a2.id,
|
|
1539
|
+
agentId: agentEntity.id,
|
|
1540
|
+
mode: items.chatMode
|
|
991
1541
|
});
|
|
992
1542
|
},
|
|
993
1543
|
async disconnect() {
|
|
994
|
-
var
|
|
995
|
-
await
|
|
1544
|
+
var _a2;
|
|
1545
|
+
await disconnect();
|
|
1546
|
+
analytics.track("agent-chat", {
|
|
996
1547
|
event: "disconnect",
|
|
997
|
-
chatId: (
|
|
998
|
-
agentId:
|
|
999
|
-
mode:
|
|
1548
|
+
chatId: (_a2 = items.chat) == null ? void 0 : _a2.id,
|
|
1549
|
+
agentId: agentEntity.id,
|
|
1550
|
+
mode: items.chatMode
|
|
1000
1551
|
});
|
|
1001
1552
|
},
|
|
1002
|
-
async chat(
|
|
1003
|
-
var
|
|
1004
|
-
const
|
|
1005
|
-
if (
|
|
1006
|
-
throw new
|
|
1007
|
-
if (
|
|
1008
|
-
throw new
|
|
1009
|
-
if (
|
|
1010
|
-
throw new
|
|
1011
|
-
if (
|
|
1012
|
-
throw new
|
|
1013
|
-
if (![
|
|
1014
|
-
if (!
|
|
1015
|
-
throw new
|
|
1016
|
-
|
|
1017
|
-
|
|
1553
|
+
async chat(userMessage) {
|
|
1554
|
+
var _a2, _b2, _c2, _d, _e;
|
|
1555
|
+
const validateChatRequest = () => {
|
|
1556
|
+
if (options.mode === ChatMode.DirectPlayback) {
|
|
1557
|
+
throw new ValidationError("Direct playback is enabled, chat is disabled");
|
|
1558
|
+
} else if (userMessage.length >= 800) {
|
|
1559
|
+
throw new ValidationError("Message cannot be more than 800 characters");
|
|
1560
|
+
} else if (userMessage.length === 0) {
|
|
1561
|
+
throw new ValidationError("Message cannot be empty");
|
|
1562
|
+
} else if (items.chatMode === ChatMode.Maintenance) {
|
|
1563
|
+
throw new ValidationError("Chat is in maintenance mode");
|
|
1564
|
+
} else if (![ChatMode.TextOnly, ChatMode.Playground].includes(items.chatMode)) {
|
|
1565
|
+
if (!items.streamingManager) {
|
|
1566
|
+
throw new ValidationError("Streaming manager is not initialized");
|
|
1567
|
+
}
|
|
1568
|
+
if (!items.chat) {
|
|
1569
|
+
throw new ValidationError("Chat is not initialized");
|
|
1570
|
+
}
|
|
1018
1571
|
}
|
|
1019
|
-
}
|
|
1020
|
-
|
|
1021
|
-
|
|
1022
|
-
|
|
1023
|
-
|
|
1024
|
-
|
|
1025
|
-
|
|
1572
|
+
};
|
|
1573
|
+
const initializeChat = async () => {
|
|
1574
|
+
var _a3, _b3;
|
|
1575
|
+
if (!items.chat) {
|
|
1576
|
+
const newChat = await createChat(agentEntity, agentsApi, analytics, items.chatMode, options.persistentChat);
|
|
1577
|
+
if (!newChat.chat) {
|
|
1578
|
+
throw new ChatCreationFailed(items.chatMode, !!options.persistentChat);
|
|
1579
|
+
}
|
|
1580
|
+
items.chat = newChat.chat;
|
|
1581
|
+
(_b3 = (_a3 = options.callbacks).onNewChat) == null ? void 0 : _b3.call(_a3, items.chat.id);
|
|
1026
1582
|
}
|
|
1027
|
-
return
|
|
1028
|
-
}
|
|
1029
|
-
|
|
1030
|
-
return
|
|
1031
|
-
|
|
1032
|
-
|
|
1033
|
-
|
|
1034
|
-
|
|
1035
|
-
|
|
1036
|
-
|
|
1037
|
-
|
|
1583
|
+
return items.chat.id;
|
|
1584
|
+
};
|
|
1585
|
+
const sendChatRequest = async (messages, chatId) => {
|
|
1586
|
+
return retryOperation(() => {
|
|
1587
|
+
var _a3, _b3;
|
|
1588
|
+
return agentsApi.chat(agentEntity.id, chatId, {
|
|
1589
|
+
chatMode: items.chatMode,
|
|
1590
|
+
streamId: (_a3 = items.streamingManager) == null ? void 0 : _a3.streamId,
|
|
1591
|
+
sessionId: (_b3 = items.streamingManager) == null ? void 0 : _b3.sessionId,
|
|
1592
|
+
messages: messages.map(({
|
|
1593
|
+
matches,
|
|
1594
|
+
...message
|
|
1595
|
+
}) => message)
|
|
1596
|
+
}, {
|
|
1597
|
+
...getRequestHeaders(items.chatMode),
|
|
1598
|
+
skipErrorHandler: true
|
|
1599
|
+
});
|
|
1038
1600
|
}, {
|
|
1039
|
-
|
|
1040
|
-
|
|
1601
|
+
limit: 2,
|
|
1602
|
+
shouldRetryFn: (error) => {
|
|
1603
|
+
var _a3, _b3, _c3, _d2;
|
|
1604
|
+
const isInvalidSessionId = (_a3 = error == null ? void 0 : error.message) == null ? void 0 : _a3.includes("missing or invalid session_id");
|
|
1605
|
+
const isStreamError = (_b3 = error == null ? void 0 : error.message) == null ? void 0 : _b3.includes("Stream Error");
|
|
1606
|
+
if (!isStreamError && !isInvalidSessionId) {
|
|
1607
|
+
(_d2 = (_c3 = options.callbacks).onError) == null ? void 0 : _d2.call(_c3, error);
|
|
1608
|
+
return false;
|
|
1609
|
+
}
|
|
1610
|
+
return true;
|
|
1611
|
+
},
|
|
1612
|
+
onRetry: async () => {
|
|
1613
|
+
await disconnect();
|
|
1614
|
+
await connect2(false);
|
|
1615
|
+
}
|
|
1041
1616
|
});
|
|
1042
|
-
}
|
|
1043
|
-
limit: 2,
|
|
1044
|
-
shouldRetryFn: (E) => {
|
|
1045
|
-
var x, W, G, O;
|
|
1046
|
-
const j = (x = E == null ? void 0 : E.message) == null ? void 0 : x.includes("missing or invalid session_id");
|
|
1047
|
-
return !((W = E == null ? void 0 : E.message) == null ? void 0 : W.includes("Stream Error")) && !j ? ((O = (G = t.callbacks).onError) == null || O.call(G, E), !1) : !0;
|
|
1048
|
-
},
|
|
1049
|
-
onRetry: async () => {
|
|
1050
|
-
await y(), await h(!1);
|
|
1051
|
-
}
|
|
1052
|
-
});
|
|
1617
|
+
};
|
|
1053
1618
|
try {
|
|
1054
|
-
|
|
1055
|
-
|
|
1619
|
+
clearQueue();
|
|
1620
|
+
validateChatRequest();
|
|
1621
|
+
items.messages.push({
|
|
1622
|
+
id: getRandom(),
|
|
1056
1623
|
role: "user",
|
|
1057
|
-
content:
|
|
1058
|
-
created_at: new Date(
|
|
1059
|
-
})
|
|
1060
|
-
|
|
1061
|
-
|
|
1062
|
-
|
|
1624
|
+
content: userMessage,
|
|
1625
|
+
created_at: new Date(timestampTracker.update()).toISOString()
|
|
1626
|
+
});
|
|
1627
|
+
(_b2 = (_a2 = options.callbacks).onNewMessage) == null ? void 0 : _b2.call(_a2, [...items.messages], "user");
|
|
1628
|
+
const chatId = await initializeChat();
|
|
1629
|
+
const response = await sendChatRequest([...items.messages], chatId);
|
|
1630
|
+
items.messages.push({
|
|
1631
|
+
id: getRandom(),
|
|
1063
1632
|
role: "assistant",
|
|
1064
|
-
content:
|
|
1633
|
+
content: response.result || "",
|
|
1065
1634
|
created_at: (/* @__PURE__ */ new Date()).toISOString(),
|
|
1066
|
-
context:
|
|
1067
|
-
matches:
|
|
1068
|
-
})
|
|
1635
|
+
context: response.context,
|
|
1636
|
+
matches: response.matches
|
|
1637
|
+
});
|
|
1638
|
+
analytics.track("agent-message-send", {
|
|
1069
1639
|
event: "success",
|
|
1070
|
-
mode:
|
|
1071
|
-
messages:
|
|
1072
|
-
})
|
|
1073
|
-
|
|
1074
|
-
|
|
1075
|
-
|
|
1076
|
-
|
|
1077
|
-
|
|
1078
|
-
|
|
1640
|
+
mode: items.chatMode,
|
|
1641
|
+
messages: items.messages.length + 1
|
|
1642
|
+
});
|
|
1643
|
+
if (response.result) {
|
|
1644
|
+
(_d = (_c2 = options.callbacks).onNewMessage) == null ? void 0 : _d.call(_c2, [...items.messages], "answer");
|
|
1645
|
+
analytics.track("agent-message-received", {
|
|
1646
|
+
latency: timestampTracker.get(true),
|
|
1647
|
+
mode: items.chatMode,
|
|
1648
|
+
messages: items.messages.length
|
|
1649
|
+
});
|
|
1650
|
+
}
|
|
1651
|
+
return response;
|
|
1652
|
+
} catch (e) {
|
|
1653
|
+
if (((_e = items.messages[items.messages.length - 1]) == null ? void 0 : _e.role) === "assistant") {
|
|
1654
|
+
items.messages.pop();
|
|
1655
|
+
}
|
|
1656
|
+
analytics.track("agent-message-send", {
|
|
1079
1657
|
event: "error",
|
|
1080
|
-
mode:
|
|
1081
|
-
messages:
|
|
1082
|
-
})
|
|
1658
|
+
mode: items.chatMode,
|
|
1659
|
+
messages: items.messages.length
|
|
1660
|
+
});
|
|
1661
|
+
throw e;
|
|
1083
1662
|
}
|
|
1084
1663
|
},
|
|
1085
|
-
rate(
|
|
1086
|
-
var
|
|
1087
|
-
const
|
|
1088
|
-
if (
|
|
1089
|
-
if (!b)
|
|
1090
|
-
throw new Error("Message not found");
|
|
1091
|
-
} else
|
|
1664
|
+
rate(messageId, score, rateId) {
|
|
1665
|
+
var _a2, _b2, _c2, _d;
|
|
1666
|
+
const message = items.messages.find((message2) => message2.id === messageId);
|
|
1667
|
+
if (!items.chat) {
|
|
1092
1668
|
throw new Error("Chat is not initialized");
|
|
1093
|
-
|
|
1094
|
-
|
|
1095
|
-
|
|
1096
|
-
|
|
1097
|
-
|
|
1098
|
-
|
|
1099
|
-
|
|
1100
|
-
|
|
1101
|
-
|
|
1102
|
-
|
|
1103
|
-
|
|
1104
|
-
|
|
1105
|
-
|
|
1106
|
-
|
|
1107
|
-
|
|
1108
|
-
|
|
1109
|
-
|
|
1110
|
-
|
|
1669
|
+
} else if (!message) {
|
|
1670
|
+
throw new Error("Message not found");
|
|
1671
|
+
}
|
|
1672
|
+
const matches = ((_a2 = message.matches) == null ? void 0 : _a2.map((match) => [match.document_id, match.id])) ?? [];
|
|
1673
|
+
analytics.track("agent-rate", {
|
|
1674
|
+
event: rateId ? "update" : "create",
|
|
1675
|
+
thumb: score === 1 ? "up" : "down",
|
|
1676
|
+
knowledge_id: ((_b2 = agentEntity.knowledge) == null ? void 0 : _b2.id) ?? "",
|
|
1677
|
+
mode: items.chatMode,
|
|
1678
|
+
matches,
|
|
1679
|
+
score
|
|
1680
|
+
});
|
|
1681
|
+
if (rateId) {
|
|
1682
|
+
return agentsApi.updateRating(agentEntity.id, items.chat.id, rateId, {
|
|
1683
|
+
knowledge_id: ((_c2 = agentEntity.knowledge) == null ? void 0 : _c2.id) ?? "",
|
|
1684
|
+
message_id: messageId,
|
|
1685
|
+
matches,
|
|
1686
|
+
score
|
|
1687
|
+
});
|
|
1688
|
+
}
|
|
1689
|
+
return agentsApi.createRating(agentEntity.id, items.chat.id, {
|
|
1690
|
+
knowledge_id: ((_d = agentEntity.knowledge) == null ? void 0 : _d.id) ?? "",
|
|
1691
|
+
message_id: messageId,
|
|
1692
|
+
matches,
|
|
1693
|
+
score
|
|
1111
1694
|
});
|
|
1112
1695
|
},
|
|
1113
|
-
deleteRate(
|
|
1114
|
-
var
|
|
1115
|
-
if (!
|
|
1696
|
+
deleteRate(id) {
|
|
1697
|
+
var _a2;
|
|
1698
|
+
if (!items.chat) {
|
|
1116
1699
|
throw new Error("Chat is not initialized");
|
|
1117
|
-
|
|
1700
|
+
}
|
|
1701
|
+
analytics.track("agent-rate-delete", {
|
|
1118
1702
|
type: "text",
|
|
1119
|
-
chat_id: (
|
|
1120
|
-
id
|
|
1121
|
-
mode:
|
|
1122
|
-
})
|
|
1703
|
+
chat_id: (_a2 = items.chat) == null ? void 0 : _a2.id,
|
|
1704
|
+
id,
|
|
1705
|
+
mode: items.chatMode
|
|
1706
|
+
});
|
|
1707
|
+
return agentsApi.deleteRating(agentEntity.id, items.chat.id, id);
|
|
1123
1708
|
},
|
|
1124
|
-
speak(
|
|
1125
|
-
var
|
|
1126
|
-
if (!
|
|
1709
|
+
speak(payload) {
|
|
1710
|
+
var _a2;
|
|
1711
|
+
if (!items.streamingManager) {
|
|
1127
1712
|
throw new Error("Please connect to the agent first");
|
|
1128
|
-
|
|
1129
|
-
|
|
1130
|
-
|
|
1713
|
+
}
|
|
1714
|
+
function getScript() {
|
|
1715
|
+
if (typeof payload === "string") {
|
|
1716
|
+
if (!agentEntity.presenter.voice) {
|
|
1131
1717
|
throw new Error("Presenter voice is not initialized");
|
|
1718
|
+
}
|
|
1132
1719
|
return {
|
|
1133
1720
|
type: "text",
|
|
1134
|
-
provider:
|
|
1135
|
-
input:
|
|
1136
|
-
ssml:
|
|
1721
|
+
provider: agentEntity.presenter.voice,
|
|
1722
|
+
input: payload,
|
|
1723
|
+
ssml: false
|
|
1137
1724
|
};
|
|
1138
1725
|
}
|
|
1139
|
-
if (
|
|
1140
|
-
if (!
|
|
1726
|
+
if (payload.type === "text" && !payload.provider) {
|
|
1727
|
+
if (!agentEntity.presenter.voice) {
|
|
1141
1728
|
throw new Error("Presenter voice is not initialized");
|
|
1729
|
+
}
|
|
1142
1730
|
return {
|
|
1143
1731
|
type: "text",
|
|
1144
|
-
provider:
|
|
1145
|
-
input:
|
|
1146
|
-
ssml:
|
|
1732
|
+
provider: agentEntity.presenter.voice,
|
|
1733
|
+
input: payload.input,
|
|
1734
|
+
ssml: payload.ssml
|
|
1147
1735
|
};
|
|
1148
1736
|
}
|
|
1149
|
-
return
|
|
1737
|
+
return payload;
|
|
1150
1738
|
}
|
|
1151
|
-
const
|
|
1152
|
-
|
|
1153
|
-
|
|
1739
|
+
const script = getScript();
|
|
1740
|
+
analytics.track("agent-speak", script);
|
|
1741
|
+
timestampTracker.update();
|
|
1742
|
+
return items.streamingManager.speak({
|
|
1743
|
+
script,
|
|
1154
1744
|
metadata: {
|
|
1155
|
-
chat_id: (
|
|
1156
|
-
agent_id:
|
|
1745
|
+
chat_id: (_a2 = items.chat) == null ? void 0 : _a2.id,
|
|
1746
|
+
agent_id: agentEntity.id
|
|
1157
1747
|
}
|
|
1158
1748
|
});
|
|
1159
1749
|
}
|
|
1160
1750
|
};
|
|
1161
1751
|
}
|
|
1162
|
-
function
|
|
1752
|
+
function getAgent(agentId, auth, baseURL) {
|
|
1163
1753
|
const {
|
|
1164
|
-
getById
|
|
1165
|
-
} =
|
|
1166
|
-
return
|
|
1754
|
+
getById
|
|
1755
|
+
} = createAgentsApi(auth, baseURL || didApiUrl);
|
|
1756
|
+
return getById(agentId);
|
|
1167
1757
|
}
|
|
1168
1758
|
export {
|
|
1169
|
-
|
|
1170
|
-
|
|
1171
|
-
|
|
1172
|
-
|
|
1173
|
-
|
|
1174
|
-
|
|
1175
|
-
|
|
1176
|
-
|
|
1177
|
-
|
|
1178
|
-
|
|
1179
|
-
|
|
1180
|
-
|
|
1181
|
-
|
|
1182
|
-
|
|
1183
|
-
|
|
1184
|
-
|
|
1185
|
-
|
|
1186
|
-
|
|
1187
|
-
|
|
1188
|
-
|
|
1189
|
-
|
|
1190
|
-
|
|
1759
|
+
AgentActivityState,
|
|
1760
|
+
AgentStatus,
|
|
1761
|
+
ChatCreationFailed,
|
|
1762
|
+
ChatMode,
|
|
1763
|
+
ChatModeDowngraded,
|
|
1764
|
+
ChatProgress,
|
|
1765
|
+
ConnectionState,
|
|
1766
|
+
ConnectivityState,
|
|
1767
|
+
DataChannelSignalMap,
|
|
1768
|
+
DocumentType,
|
|
1769
|
+
KnowledgeType,
|
|
1770
|
+
PlanGroup,
|
|
1771
|
+
Providers,
|
|
1772
|
+
RateState,
|
|
1773
|
+
StreamEvents,
|
|
1774
|
+
StreamType,
|
|
1775
|
+
StreamingState,
|
|
1776
|
+
Subject,
|
|
1777
|
+
UserPlan,
|
|
1778
|
+
ValidationError,
|
|
1779
|
+
VideoType,
|
|
1780
|
+
VoiceAccess,
|
|
1781
|
+
WsError,
|
|
1782
|
+
createAgentManager,
|
|
1783
|
+
getAgent,
|
|
1784
|
+
mapVideoType
|
|
1191
1785
|
};
|