@d-id/client-sdk 1.0.19-beta.131 → 1.0.19-beta.133
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 +1005 -627
- package/dist/index.umd.cjs +1277 -1
- package/dist/src/api/agents.d.ts +2 -1
- package/dist/src/types/entities/agents/manager.d.ts +6 -0
- package/dist/src/types/index.d.ts +1 -0
- package/dist/src/types/stt.d.ts +4 -0
- package/package.json +2 -2
package/dist/index.js
CHANGED
|
@@ -1,422 +1,633 @@
|
|
|
1
|
-
var
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
var UserPlan = /* @__PURE__ */ ((UserPlan2) => {
|
|
2
|
+
UserPlan2["TRIAL"] = "trial";
|
|
3
|
+
UserPlan2["BASIC"] = "basic";
|
|
4
|
+
UserPlan2["ENTERPRISE"] = "enterprise";
|
|
5
|
+
UserPlan2["LITE"] = "lite";
|
|
6
|
+
UserPlan2["ADVANCED"] = "advanced";
|
|
7
|
+
return UserPlan2;
|
|
8
|
+
})(UserPlan || {});
|
|
9
|
+
var PlanGroup = /* @__PURE__ */ ((PlanGroup2) => {
|
|
10
|
+
PlanGroup2["TRIAL"] = "deid-trial";
|
|
11
|
+
PlanGroup2["PRO"] = "deid-pro";
|
|
12
|
+
PlanGroup2["ENTERPRISE"] = "deid-enterprise";
|
|
13
|
+
PlanGroup2["LITE"] = "deid-lite";
|
|
14
|
+
PlanGroup2["ADVANCED"] = "deid-advanced";
|
|
15
|
+
PlanGroup2["BUILD"] = "deid-api-build";
|
|
16
|
+
PlanGroup2["LAUNCH"] = "deid-api-launch";
|
|
17
|
+
PlanGroup2["SCALE"] = "deid-api-scale";
|
|
18
|
+
return PlanGroup2;
|
|
19
|
+
})(PlanGroup || {});
|
|
20
|
+
var AgentStatus = /* @__PURE__ */ ((AgentStatus2) => {
|
|
21
|
+
AgentStatus2["Created"] = "created";
|
|
22
|
+
AgentStatus2["Started"] = "started";
|
|
23
|
+
AgentStatus2["Done"] = "done";
|
|
24
|
+
AgentStatus2["Error"] = "error";
|
|
25
|
+
AgentStatus2["Rejected"] = "rejected";
|
|
26
|
+
AgentStatus2["Ready"] = "ready";
|
|
27
|
+
return AgentStatus2;
|
|
28
|
+
})(AgentStatus || {});
|
|
29
|
+
var RateState = /* @__PURE__ */ ((RateState2) => {
|
|
30
|
+
RateState2["Unrated"] = "Unrated";
|
|
31
|
+
RateState2["Positive"] = "Positive";
|
|
32
|
+
RateState2["Negative"] = "Negative";
|
|
33
|
+
return RateState2;
|
|
34
|
+
})(RateState || {});
|
|
35
|
+
var ChatMode = /* @__PURE__ */ ((ChatMode2) => {
|
|
36
|
+
ChatMode2["Functional"] = "Functional";
|
|
37
|
+
ChatMode2["TextOnly"] = "TextOnly";
|
|
38
|
+
ChatMode2["Maintenance"] = "Maintenance";
|
|
39
|
+
ChatMode2["Playground"] = "Playground";
|
|
40
|
+
ChatMode2["DirectPlayback"] = "DirectPlayback";
|
|
41
|
+
return ChatMode2;
|
|
42
|
+
})(ChatMode || {});
|
|
43
|
+
var ChatProgress = /* @__PURE__ */ ((ChatProgress2) => {
|
|
44
|
+
ChatProgress2["Embed"] = "embed";
|
|
45
|
+
ChatProgress2["Query"] = "query";
|
|
46
|
+
ChatProgress2["Partial"] = "partial";
|
|
47
|
+
ChatProgress2["Answer"] = "answer";
|
|
48
|
+
ChatProgress2["Complete"] = "done";
|
|
49
|
+
return ChatProgress2;
|
|
50
|
+
})(ChatProgress || {});
|
|
51
|
+
var Subject = /* @__PURE__ */ ((Subject2) => {
|
|
52
|
+
Subject2["KnowledgeProcessing"] = "knowledge/processing";
|
|
53
|
+
Subject2["KnowledgeIndexing"] = "knowledge/indexing";
|
|
54
|
+
Subject2["KnowledgeFailed"] = "knowledge/error";
|
|
55
|
+
Subject2["KnowledgeDone"] = "knowledge/done";
|
|
56
|
+
return Subject2;
|
|
57
|
+
})(Subject || {});
|
|
58
|
+
var KnowledgeType = /* @__PURE__ */ ((KnowledgeType2) => {
|
|
59
|
+
KnowledgeType2["Knowledge"] = "knowledge";
|
|
60
|
+
KnowledgeType2["Document"] = "document";
|
|
61
|
+
KnowledgeType2["Record"] = "record";
|
|
62
|
+
return KnowledgeType2;
|
|
63
|
+
})(KnowledgeType || {});
|
|
64
|
+
var DocumentType = /* @__PURE__ */ ((DocumentType2) => {
|
|
65
|
+
DocumentType2["Pdf"] = "pdf";
|
|
66
|
+
DocumentType2["Text"] = "text";
|
|
67
|
+
DocumentType2["Html"] = "html";
|
|
68
|
+
DocumentType2["Word"] = "word";
|
|
69
|
+
DocumentType2["Json"] = "json";
|
|
70
|
+
DocumentType2["Markdown"] = "markdown";
|
|
71
|
+
DocumentType2["Csv"] = "csv";
|
|
72
|
+
DocumentType2["Excel"] = "excel";
|
|
73
|
+
DocumentType2["Powerpoint"] = "powerpoint";
|
|
74
|
+
DocumentType2["Archive"] = "archive";
|
|
75
|
+
DocumentType2["Image"] = "image";
|
|
76
|
+
DocumentType2["Audio"] = "audio";
|
|
77
|
+
DocumentType2["Video"] = "video";
|
|
78
|
+
return DocumentType2;
|
|
79
|
+
})(DocumentType || {});
|
|
80
|
+
var VideoType = /* @__PURE__ */ ((VideoType2) => {
|
|
81
|
+
VideoType2["Clip"] = "clip";
|
|
82
|
+
VideoType2["Talk"] = "talk";
|
|
83
|
+
return VideoType2;
|
|
84
|
+
})(VideoType || {});
|
|
85
|
+
const mapVideoType = (type) => {
|
|
86
|
+
switch (type) {
|
|
4
87
|
case "clip":
|
|
5
88
|
return "clip";
|
|
6
89
|
case "talk":
|
|
7
90
|
return "talk";
|
|
8
91
|
default:
|
|
9
|
-
throw new Error(`Unknown video type: ${
|
|
92
|
+
throw new Error(`Unknown video type: ${type}`);
|
|
10
93
|
}
|
|
11
94
|
};
|
|
12
|
-
var
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
95
|
+
var StreamingState = /* @__PURE__ */ ((StreamingState2) => {
|
|
96
|
+
StreamingState2["Start"] = "START";
|
|
97
|
+
StreamingState2["Stop"] = "STOP";
|
|
98
|
+
return StreamingState2;
|
|
99
|
+
})(StreamingState || {});
|
|
100
|
+
var StreamEvents = /* @__PURE__ */ ((StreamEvents2) => {
|
|
101
|
+
StreamEvents2["ChatAnswer"] = "chat/answer";
|
|
102
|
+
StreamEvents2["ChatPartial"] = "chat/partial";
|
|
103
|
+
StreamEvents2["StreamDone"] = "stream/done";
|
|
104
|
+
StreamEvents2["StreamStarted"] = "stream/started";
|
|
105
|
+
StreamEvents2["StreamFailed"] = "stream/error";
|
|
106
|
+
StreamEvents2["StreamReady"] = "stream/ready";
|
|
107
|
+
StreamEvents2["StreamCreated"] = "stream/created";
|
|
108
|
+
StreamEvents2["StreamVideoCreated"] = "stream-video/started";
|
|
109
|
+
StreamEvents2["StreamVideoDone"] = "stream-video/done";
|
|
110
|
+
StreamEvents2["StreamVideoError"] = "stream-video/error";
|
|
111
|
+
StreamEvents2["StreamVideoRejected"] = "stream-video/rejected";
|
|
112
|
+
return StreamEvents2;
|
|
113
|
+
})(StreamEvents || {});
|
|
114
|
+
var ConnectionState = /* @__PURE__ */ ((ConnectionState2) => {
|
|
115
|
+
ConnectionState2["New"] = "new";
|
|
116
|
+
ConnectionState2["Fail"] = "fail";
|
|
117
|
+
ConnectionState2["Connected"] = "connected";
|
|
118
|
+
ConnectionState2["Connecting"] = "connecting";
|
|
119
|
+
ConnectionState2["Closed"] = "closed";
|
|
120
|
+
ConnectionState2["Completed"] = "completed";
|
|
121
|
+
ConnectionState2["Disconnected"] = "disconnected";
|
|
122
|
+
return ConnectionState2;
|
|
123
|
+
})(ConnectionState || {});
|
|
124
|
+
var Providers = /* @__PURE__ */ ((Providers2) => {
|
|
125
|
+
Providers2["Amazon"] = "amazon";
|
|
126
|
+
Providers2["Microsoft"] = "microsoft";
|
|
127
|
+
Providers2["Afflorithmics"] = "afflorithmics";
|
|
128
|
+
Providers2["Elevenlabs"] = "elevenlabs";
|
|
129
|
+
return Providers2;
|
|
130
|
+
})(Providers || {});
|
|
131
|
+
var VoiceAccess = /* @__PURE__ */ ((VoiceAccess2) => {
|
|
132
|
+
VoiceAccess2["Public"] = "public";
|
|
133
|
+
VoiceAccess2["Premium"] = "premium";
|
|
134
|
+
VoiceAccess2["Private"] = "private";
|
|
135
|
+
return VoiceAccess2;
|
|
136
|
+
})(VoiceAccess || {});
|
|
137
|
+
const didApiUrl = "https://api-dev.d-id.com";
|
|
138
|
+
const didSocketApiUrl = "wss://notifications-dev.d-id.com";
|
|
139
|
+
const mixpanelKey = "79f81a83a67430be2bc0fd61042b8faa";
|
|
140
|
+
const getRandom = () => Math.random().toString(16).slice(2);
|
|
141
|
+
function getExternalId() {
|
|
142
|
+
let key = window.localStorage.getItem("did_external_key_id");
|
|
143
|
+
if (!key) {
|
|
144
|
+
key = Math.random().toString(16).slice(2);
|
|
145
|
+
window.localStorage.setItem("did_external_key_id", key);
|
|
146
|
+
}
|
|
147
|
+
return key;
|
|
17
148
|
}
|
|
18
|
-
let
|
|
19
|
-
function
|
|
20
|
-
if (
|
|
21
|
-
return `Bearer ${
|
|
22
|
-
if (
|
|
23
|
-
return `Basic ${btoa(`${
|
|
24
|
-
if (
|
|
25
|
-
return `Client-Key ${
|
|
26
|
-
|
|
149
|
+
let sessionKey = getRandom();
|
|
150
|
+
function getAuthHeader(auth) {
|
|
151
|
+
if (auth.type === "bearer") {
|
|
152
|
+
return `Bearer ${auth.token}`;
|
|
153
|
+
} else if (auth.type === "basic") {
|
|
154
|
+
return `Basic ${btoa(`${auth.username}:${auth.password}`)}`;
|
|
155
|
+
} else if (auth.type === "key") {
|
|
156
|
+
return `Client-Key ${auth.clientKey}.${getExternalId()}_${sessionKey}`;
|
|
157
|
+
} else {
|
|
158
|
+
throw new Error(`Unknown auth type: ${auth}`);
|
|
159
|
+
}
|
|
27
160
|
}
|
|
28
|
-
function
|
|
29
|
-
const
|
|
30
|
-
const
|
|
31
|
-
...
|
|
161
|
+
function createClient(auth, host = didApiUrl, onError) {
|
|
162
|
+
const client = async (url, options) => {
|
|
163
|
+
const request = await fetch(host + ((url == null ? void 0 : url.startsWith("/")) ? url : `/${url}`), {
|
|
164
|
+
...options,
|
|
32
165
|
headers: {
|
|
33
|
-
...
|
|
34
|
-
Authorization:
|
|
166
|
+
...options == null ? void 0 : options.headers,
|
|
167
|
+
Authorization: getAuthHeader(auth),
|
|
35
168
|
"Content-Type": "application/json"
|
|
36
169
|
}
|
|
37
170
|
});
|
|
38
|
-
if (!
|
|
39
|
-
let
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
171
|
+
if (!request.ok) {
|
|
172
|
+
let error = await request.text().catch(() => "Failed to fetch");
|
|
173
|
+
if (onError) {
|
|
174
|
+
onError(new Error(error), {
|
|
175
|
+
url,
|
|
176
|
+
options,
|
|
177
|
+
headers: request.headers
|
|
178
|
+
});
|
|
179
|
+
}
|
|
180
|
+
throw new Error(error);
|
|
45
181
|
}
|
|
46
|
-
return
|
|
182
|
+
return request.json();
|
|
47
183
|
};
|
|
48
184
|
return {
|
|
49
|
-
get(
|
|
50
|
-
return
|
|
51
|
-
...
|
|
185
|
+
get(url, options) {
|
|
186
|
+
return client(url, {
|
|
187
|
+
...options,
|
|
52
188
|
method: "GET"
|
|
53
189
|
});
|
|
54
190
|
},
|
|
55
|
-
post(
|
|
56
|
-
return
|
|
57
|
-
...
|
|
58
|
-
body: JSON.stringify(
|
|
191
|
+
post(url, body, options) {
|
|
192
|
+
return client(url, {
|
|
193
|
+
...options,
|
|
194
|
+
body: JSON.stringify(body),
|
|
59
195
|
method: "POST"
|
|
60
196
|
});
|
|
61
197
|
},
|
|
62
|
-
delete(
|
|
63
|
-
return
|
|
64
|
-
...
|
|
65
|
-
body: JSON.stringify(
|
|
198
|
+
delete(url, body, options) {
|
|
199
|
+
return client(url, {
|
|
200
|
+
...options,
|
|
201
|
+
body: JSON.stringify(body),
|
|
66
202
|
method: "DELETE"
|
|
67
203
|
});
|
|
68
204
|
},
|
|
69
|
-
patch(
|
|
70
|
-
return
|
|
71
|
-
...
|
|
72
|
-
body: JSON.stringify(
|
|
205
|
+
patch(url, body, options) {
|
|
206
|
+
return client(url, {
|
|
207
|
+
...options,
|
|
208
|
+
body: JSON.stringify(body),
|
|
73
209
|
method: "PATCH"
|
|
74
210
|
});
|
|
75
211
|
}
|
|
76
212
|
};
|
|
77
213
|
}
|
|
78
|
-
function
|
|
79
|
-
const
|
|
214
|
+
function createAgentsApi(auth, host = didApiUrl, onError) {
|
|
215
|
+
const client = createClient(auth, `${host}/agents`, onError);
|
|
80
216
|
return {
|
|
81
|
-
create(
|
|
82
|
-
return
|
|
217
|
+
create(payload, options) {
|
|
218
|
+
return client.post(`/`, payload, options);
|
|
83
219
|
},
|
|
84
|
-
getAgents(
|
|
85
|
-
return
|
|
220
|
+
getAgents(tag, options) {
|
|
221
|
+
return client.get(`/${tag ? `?tag=${tag}` : ""}`, options).then((agents) => agents ?? []);
|
|
86
222
|
},
|
|
87
|
-
getById(
|
|
88
|
-
return
|
|
223
|
+
getById(id, options) {
|
|
224
|
+
return client.get(`/${id}`, options);
|
|
89
225
|
},
|
|
90
|
-
delete(
|
|
91
|
-
return
|
|
226
|
+
delete(id, options) {
|
|
227
|
+
return client.delete(`/${id}`, void 0, options);
|
|
92
228
|
},
|
|
93
|
-
update(
|
|
94
|
-
return
|
|
229
|
+
update(id, payload, options) {
|
|
230
|
+
return client.patch(`/${id}`, payload, options);
|
|
95
231
|
},
|
|
96
|
-
newChat(
|
|
97
|
-
return
|
|
232
|
+
newChat(agentId, payload, options) {
|
|
233
|
+
return client.post(`/${agentId}/chat`, payload, options);
|
|
98
234
|
},
|
|
99
|
-
chat(
|
|
100
|
-
return
|
|
235
|
+
chat(agentId, chatId, payload, options) {
|
|
236
|
+
return client.post(`/${agentId}/chat/${chatId}`, payload, options);
|
|
101
237
|
},
|
|
102
|
-
createRating(
|
|
103
|
-
return
|
|
238
|
+
createRating(agentId, chatId, payload, options) {
|
|
239
|
+
return client.post(`/${agentId}/chat/${chatId}/ratings`, payload, options);
|
|
104
240
|
},
|
|
105
|
-
updateRating(
|
|
106
|
-
return
|
|
241
|
+
updateRating(agentId, chatId, ratingId, payload, options) {
|
|
242
|
+
return client.patch(`/${agentId}/chat/${chatId}/ratings/${ratingId}`, payload, options);
|
|
107
243
|
},
|
|
108
|
-
deleteRating(
|
|
109
|
-
return
|
|
244
|
+
deleteRating(agentId, chatId, ratingId, options) {
|
|
245
|
+
return client.delete(`/${agentId}/chat/${chatId}/ratings/${ratingId}`, options);
|
|
246
|
+
},
|
|
247
|
+
getSTTToken(agentId, chatId, options) {
|
|
248
|
+
return client.get(`/${agentId}/chat/${chatId}/stt-token`, options);
|
|
110
249
|
}
|
|
111
250
|
};
|
|
112
251
|
}
|
|
113
|
-
const
|
|
114
|
-
function
|
|
115
|
-
return new Promise((
|
|
252
|
+
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
|
|
253
|
+
function connect(options) {
|
|
254
|
+
return new Promise((resolve, reject) => {
|
|
255
|
+
const {
|
|
256
|
+
callbacks,
|
|
257
|
+
host,
|
|
258
|
+
auth
|
|
259
|
+
} = options;
|
|
116
260
|
const {
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
261
|
+
onMessage = null,
|
|
262
|
+
onOpen = null,
|
|
263
|
+
onClose = null,
|
|
264
|
+
onError = null
|
|
265
|
+
} = callbacks || {};
|
|
266
|
+
const socket = new WebSocket(`${host}?authorization=${getAuthHeader(auth)}`);
|
|
267
|
+
socket.onmessage = onMessage;
|
|
268
|
+
socket.onclose = onClose;
|
|
269
|
+
socket.onerror = (e) => {
|
|
270
|
+
console.error(e);
|
|
271
|
+
onError == null ? void 0 : onError("Websocket failed to connect", e);
|
|
272
|
+
reject(e);
|
|
273
|
+
};
|
|
274
|
+
socket.onopen = (e) => {
|
|
275
|
+
onOpen == null ? void 0 : onOpen(e);
|
|
276
|
+
resolve(socket);
|
|
130
277
|
};
|
|
131
278
|
});
|
|
132
279
|
}
|
|
133
|
-
async function
|
|
280
|
+
async function connectWithRetries(options) {
|
|
134
281
|
const {
|
|
135
|
-
retries
|
|
136
|
-
} =
|
|
137
|
-
let
|
|
138
|
-
for (let
|
|
282
|
+
retries = 1
|
|
283
|
+
} = options;
|
|
284
|
+
let socket = null;
|
|
285
|
+
for (let attempt = 0; (socket == null ? void 0 : socket.readyState) !== WebSocket.OPEN; attempt++) {
|
|
139
286
|
try {
|
|
140
|
-
|
|
141
|
-
} catch (
|
|
142
|
-
if (
|
|
143
|
-
throw
|
|
144
|
-
|
|
287
|
+
socket = await connect(options);
|
|
288
|
+
} catch (e) {
|
|
289
|
+
if (attempt === retries) {
|
|
290
|
+
throw e;
|
|
291
|
+
}
|
|
292
|
+
await sleep(attempt * 500);
|
|
145
293
|
}
|
|
146
|
-
|
|
294
|
+
}
|
|
295
|
+
return socket;
|
|
147
296
|
}
|
|
148
|
-
async function
|
|
149
|
-
const
|
|
150
|
-
|
|
151
|
-
|
|
297
|
+
async function createSocketManager(auth, host, callback) {
|
|
298
|
+
const messageCallbacks = (callback == null ? void 0 : callback.onMessage) ? [callback.onMessage] : [];
|
|
299
|
+
const socket = await connectWithRetries({
|
|
300
|
+
auth,
|
|
301
|
+
host,
|
|
152
302
|
callbacks: {
|
|
153
|
-
onError:
|
|
154
|
-
onMessage: (
|
|
155
|
-
const
|
|
156
|
-
|
|
303
|
+
onError: callback == null ? void 0 : callback.onError,
|
|
304
|
+
onMessage: (event) => {
|
|
305
|
+
const parsedData = JSON.parse(event.data);
|
|
306
|
+
messageCallbacks.forEach((callback2) => callback2(parsedData.event, parsedData));
|
|
157
307
|
}
|
|
158
308
|
}
|
|
159
309
|
});
|
|
160
310
|
return {
|
|
161
|
-
socket
|
|
162
|
-
disconnect: () =>
|
|
163
|
-
subscribeToEvents: (
|
|
311
|
+
socket,
|
|
312
|
+
disconnect: () => socket.close(),
|
|
313
|
+
subscribeToEvents: (callback2) => messageCallbacks.push(callback2)
|
|
164
314
|
};
|
|
165
315
|
}
|
|
166
|
-
const
|
|
167
|
-
function
|
|
168
|
-
const
|
|
316
|
+
const PLAYGROUND_HEADER = "X-Playground-Chat";
|
|
317
|
+
function createApi$1(auth, host, agentId, onError) {
|
|
318
|
+
const client = createClient(auth, `${host}/agents/${agentId}`, onError);
|
|
169
319
|
return {
|
|
170
|
-
createStream(
|
|
171
|
-
return
|
|
172
|
-
output_resolution:
|
|
173
|
-
compatibility_mode:
|
|
174
|
-
stream_warmup:
|
|
175
|
-
session_timeout:
|
|
176
|
-
stream_greeting:
|
|
320
|
+
createStream(options) {
|
|
321
|
+
return client.post("/streams", {
|
|
322
|
+
output_resolution: options.output_resolution,
|
|
323
|
+
compatibility_mode: options.compatibility_mode,
|
|
324
|
+
stream_warmup: options.stream_warmup,
|
|
325
|
+
session_timeout: options.session_timeout,
|
|
326
|
+
stream_greeting: options.stream_greeting
|
|
177
327
|
});
|
|
178
328
|
},
|
|
179
|
-
startConnection(
|
|
180
|
-
return
|
|
181
|
-
session_id:
|
|
182
|
-
answer
|
|
329
|
+
startConnection(streamId, answer, sessionId) {
|
|
330
|
+
return client.post(`/streams/${streamId}/sdp`, {
|
|
331
|
+
session_id: sessionId,
|
|
332
|
+
answer
|
|
183
333
|
});
|
|
184
334
|
},
|
|
185
|
-
addIceCandidate(
|
|
186
|
-
return
|
|
187
|
-
session_id:
|
|
188
|
-
...
|
|
335
|
+
addIceCandidate(streamId, candidate, sessionId) {
|
|
336
|
+
return client.post(`/streams/${streamId}/ice`, {
|
|
337
|
+
session_id: sessionId,
|
|
338
|
+
...candidate
|
|
189
339
|
});
|
|
190
340
|
},
|
|
191
|
-
sendStreamRequest(
|
|
192
|
-
return
|
|
193
|
-
session_id:
|
|
194
|
-
...
|
|
341
|
+
sendStreamRequest(streamId, sessionId, payload) {
|
|
342
|
+
return client.post(`/streams/${streamId}`, {
|
|
343
|
+
session_id: sessionId,
|
|
344
|
+
...payload
|
|
195
345
|
});
|
|
196
346
|
},
|
|
197
|
-
close(
|
|
198
|
-
return
|
|
199
|
-
session_id:
|
|
347
|
+
close(streamId, sessionId) {
|
|
348
|
+
return client.delete(`/streams/${streamId}`, {
|
|
349
|
+
session_id: sessionId
|
|
200
350
|
});
|
|
201
351
|
}
|
|
202
352
|
};
|
|
203
353
|
}
|
|
204
|
-
function
|
|
205
|
-
const
|
|
354
|
+
function createApi(auth, host, agentId, onError) {
|
|
355
|
+
const client = createClient(auth, `${host}/agents/${agentId}`, onError);
|
|
206
356
|
return {
|
|
207
|
-
createStream(
|
|
208
|
-
return
|
|
209
|
-
driver_url:
|
|
210
|
-
face:
|
|
211
|
-
config:
|
|
212
|
-
output_resolution:
|
|
213
|
-
compatibility_mode:
|
|
214
|
-
stream_warmup:
|
|
215
|
-
session_timeout:
|
|
216
|
-
stream_greeting:
|
|
217
|
-
},
|
|
218
|
-
},
|
|
219
|
-
startConnection(
|
|
220
|
-
return
|
|
221
|
-
session_id:
|
|
222
|
-
answer
|
|
223
|
-
},
|
|
224
|
-
},
|
|
225
|
-
addIceCandidate(
|
|
226
|
-
return
|
|
227
|
-
session_id:
|
|
228
|
-
...
|
|
229
|
-
},
|
|
230
|
-
},
|
|
231
|
-
sendStreamRequest(
|
|
232
|
-
return
|
|
233
|
-
session_id:
|
|
234
|
-
...
|
|
235
|
-
},
|
|
236
|
-
},
|
|
237
|
-
close(
|
|
238
|
-
return
|
|
239
|
-
session_id:
|
|
240
|
-
},
|
|
357
|
+
createStream(streamOptions, options) {
|
|
358
|
+
return client.post("/streams", {
|
|
359
|
+
driver_url: streamOptions.driver_url,
|
|
360
|
+
face: streamOptions.face,
|
|
361
|
+
config: streamOptions.config,
|
|
362
|
+
output_resolution: streamOptions.output_resolution,
|
|
363
|
+
compatibility_mode: streamOptions.compatibility_mode,
|
|
364
|
+
stream_warmup: streamOptions.stream_warmup,
|
|
365
|
+
session_timeout: streamOptions.session_timeout,
|
|
366
|
+
stream_greeting: streamOptions.stream_greeting
|
|
367
|
+
}, options);
|
|
368
|
+
},
|
|
369
|
+
startConnection(streamId, answer, sessionId, options) {
|
|
370
|
+
return client.post(`/streams/${streamId}/sdp`, {
|
|
371
|
+
session_id: sessionId,
|
|
372
|
+
answer
|
|
373
|
+
}, options);
|
|
374
|
+
},
|
|
375
|
+
addIceCandidate(streamId, candidate, sessionId, options) {
|
|
376
|
+
return client.post(`/streams/${streamId}/ice`, {
|
|
377
|
+
session_id: sessionId,
|
|
378
|
+
...candidate
|
|
379
|
+
}, options);
|
|
380
|
+
},
|
|
381
|
+
sendStreamRequest(streamId, sessionId, payload, options) {
|
|
382
|
+
return client.post(`/streams/${streamId}`, {
|
|
383
|
+
session_id: sessionId,
|
|
384
|
+
...payload
|
|
385
|
+
}, options);
|
|
386
|
+
},
|
|
387
|
+
close(streamId, sessionId, options) {
|
|
388
|
+
return client.delete(`/streams/${streamId}`, {
|
|
389
|
+
session_id: sessionId
|
|
390
|
+
}, options);
|
|
241
391
|
}
|
|
242
392
|
};
|
|
243
393
|
}
|
|
244
|
-
let
|
|
245
|
-
const
|
|
246
|
-
|
|
247
|
-
|
|
394
|
+
let _debug = false;
|
|
395
|
+
const log = (message, extra) => _debug && console.log(message, extra);
|
|
396
|
+
const actualRTCPC = (window.RTCPeerConnection || window.webkitRTCPeerConnection || window.mozRTCPeerConnection).bind(window);
|
|
397
|
+
function mapConnectionState(state) {
|
|
398
|
+
switch (state) {
|
|
248
399
|
case "connected":
|
|
249
|
-
return
|
|
400
|
+
return ConnectionState.Connected;
|
|
250
401
|
case "checking":
|
|
251
|
-
return
|
|
402
|
+
return ConnectionState.Connecting;
|
|
252
403
|
case "failed":
|
|
253
|
-
return
|
|
404
|
+
return ConnectionState.Fail;
|
|
254
405
|
case "new":
|
|
255
|
-
return
|
|
406
|
+
return ConnectionState.New;
|
|
256
407
|
case "closed":
|
|
257
|
-
return
|
|
408
|
+
return ConnectionState.Closed;
|
|
258
409
|
case "disconnected":
|
|
259
|
-
return
|
|
410
|
+
return ConnectionState.Disconnected;
|
|
260
411
|
case "completed":
|
|
261
|
-
return
|
|
412
|
+
return ConnectionState.Completed;
|
|
262
413
|
default:
|
|
263
|
-
return
|
|
414
|
+
return ConnectionState.New;
|
|
264
415
|
}
|
|
265
416
|
}
|
|
266
|
-
function
|
|
267
|
-
let
|
|
268
|
-
return (
|
|
269
|
-
for (const
|
|
270
|
-
if (
|
|
271
|
-
const
|
|
272
|
-
|
|
417
|
+
function createVideoStatsAnalyzer() {
|
|
418
|
+
let lastBytesReceived = 0;
|
|
419
|
+
return (stats) => {
|
|
420
|
+
for (const report of stats.values()) {
|
|
421
|
+
if (report && report.type === "inbound-rtp" && report.kind === "video") {
|
|
422
|
+
const currBytesReceived = report.bytesReceived;
|
|
423
|
+
const isReceiving = currBytesReceived - lastBytesReceived > 0;
|
|
424
|
+
lastBytesReceived = currBytesReceived;
|
|
425
|
+
return isReceiving;
|
|
273
426
|
}
|
|
274
|
-
|
|
427
|
+
}
|
|
428
|
+
return false;
|
|
275
429
|
};
|
|
276
430
|
}
|
|
277
|
-
function
|
|
278
|
-
const
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
let
|
|
282
|
-
const
|
|
431
|
+
function pollStats(peerConnection, onVideoStateChange, warmup = false, getIsConnected, onConnected) {
|
|
432
|
+
const interval = 100;
|
|
433
|
+
const notReceivingIntervalsThreshold = Math.max(Math.ceil(1e3 / interval), 1);
|
|
434
|
+
let notReceivingNumIntervals = 0;
|
|
435
|
+
let isStreaming = false;
|
|
436
|
+
const streamsBeforeReady = warmup ? 1 : 0;
|
|
437
|
+
let streamsCount = 0;
|
|
438
|
+
const isReceivingVideoBytes = createVideoStatsAnalyzer();
|
|
283
439
|
return setInterval(async () => {
|
|
284
|
-
const
|
|
285
|
-
|
|
286
|
-
|
|
440
|
+
const stats = await peerConnection.getStats();
|
|
441
|
+
const isReceiving = isReceivingVideoBytes(stats);
|
|
442
|
+
if (isReceiving) {
|
|
443
|
+
notReceivingNumIntervals = 0;
|
|
444
|
+
if (!isStreaming) {
|
|
445
|
+
onVideoStateChange == null ? void 0 : onVideoStateChange(StreamingState.Start);
|
|
446
|
+
if (streamsCount >= streamsBeforeReady && !getIsConnected()) {
|
|
447
|
+
onConnected();
|
|
448
|
+
}
|
|
449
|
+
streamsCount++;
|
|
450
|
+
isStreaming = true;
|
|
451
|
+
}
|
|
452
|
+
} else if (isStreaming) {
|
|
453
|
+
notReceivingNumIntervals++;
|
|
454
|
+
if (notReceivingNumIntervals >= notReceivingIntervalsThreshold) {
|
|
455
|
+
onVideoStateChange == null ? void 0 : onVideoStateChange(StreamingState.Stop);
|
|
456
|
+
isStreaming = false;
|
|
457
|
+
}
|
|
458
|
+
}
|
|
459
|
+
}, interval);
|
|
287
460
|
}
|
|
288
|
-
async function
|
|
289
|
-
debug
|
|
290
|
-
callbacks
|
|
291
|
-
auth
|
|
292
|
-
baseURL
|
|
293
|
-
warmup
|
|
461
|
+
async function createStreamingManager(agentId, agent, {
|
|
462
|
+
debug = false,
|
|
463
|
+
callbacks,
|
|
464
|
+
auth,
|
|
465
|
+
baseURL = didApiUrl,
|
|
466
|
+
warmup
|
|
294
467
|
}) {
|
|
295
|
-
|
|
468
|
+
_debug = debug;
|
|
469
|
+
const {
|
|
470
|
+
startConnection,
|
|
471
|
+
sendStreamRequest,
|
|
472
|
+
close,
|
|
473
|
+
createStream,
|
|
474
|
+
addIceCandidate
|
|
475
|
+
} = agent.videoType === VideoType.Clip ? createApi$1(auth, baseURL, agentId, callbacks.onError) : createApi(auth, baseURL, agentId, callbacks.onError);
|
|
296
476
|
const {
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
} = await h(n), w = new Se({
|
|
308
|
-
iceServers: y
|
|
309
|
-
}), b = w.createDataChannel("JanusDataChannel");
|
|
310
|
-
if (!_)
|
|
477
|
+
id: streamIdFromServer,
|
|
478
|
+
offer,
|
|
479
|
+
ice_servers,
|
|
480
|
+
session_id
|
|
481
|
+
} = await createStream(agent);
|
|
482
|
+
const peerConnection = new actualRTCPC({
|
|
483
|
+
iceServers: ice_servers
|
|
484
|
+
});
|
|
485
|
+
const pcDataChannel = peerConnection.createDataChannel("JanusDataChannel");
|
|
486
|
+
if (!session_id) {
|
|
311
487
|
throw new Error("Could not create session_id");
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
488
|
+
}
|
|
489
|
+
let isConnected = false;
|
|
490
|
+
const getIsConnected = () => isConnected;
|
|
491
|
+
const onConnected = () => {
|
|
492
|
+
var _a;
|
|
493
|
+
isConnected = true;
|
|
494
|
+
(_a = callbacks.onConnectionStateChange) == null ? void 0 : _a.call(callbacks, ConnectionState.Connected);
|
|
495
|
+
};
|
|
496
|
+
const videoStatsInterval = pollStats(peerConnection, callbacks.onVideoStateChange, warmup, getIsConnected, onConnected);
|
|
497
|
+
peerConnection.onicecandidate = (event) => {
|
|
498
|
+
var _a;
|
|
499
|
+
log("peerConnection.onicecandidate", event);
|
|
320
500
|
try {
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
}
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
501
|
+
if (event.candidate && event.candidate.sdpMid && event.candidate.sdpMLineIndex !== null) {
|
|
502
|
+
addIceCandidate(streamIdFromServer, {
|
|
503
|
+
candidate: event.candidate.candidate,
|
|
504
|
+
sdpMid: event.candidate.sdpMid,
|
|
505
|
+
sdpMLineIndex: event.candidate.sdpMLineIndex
|
|
506
|
+
}, session_id);
|
|
507
|
+
} else {
|
|
508
|
+
addIceCandidate(streamIdFromServer, {
|
|
509
|
+
candidate: null
|
|
510
|
+
}, session_id);
|
|
511
|
+
}
|
|
512
|
+
} catch (e) {
|
|
513
|
+
(_a = callbacks.onError) == null ? void 0 : _a.call(callbacks, e, {
|
|
514
|
+
streamId: streamIdFromServer
|
|
331
515
|
});
|
|
332
516
|
}
|
|
333
|
-
}
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
517
|
+
};
|
|
518
|
+
pcDataChannel.onmessage = (message) => {
|
|
519
|
+
if (pcDataChannel.readyState === "open") {
|
|
520
|
+
const [event, _] = message.data.split(":");
|
|
521
|
+
if (event === StreamEvents.StreamReady && !isConnected) {
|
|
522
|
+
onConnected();
|
|
523
|
+
}
|
|
337
524
|
}
|
|
338
|
-
}
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
}
|
|
347
|
-
|
|
348
|
-
|
|
525
|
+
};
|
|
526
|
+
peerConnection.oniceconnectionstatechange = () => {
|
|
527
|
+
var _a;
|
|
528
|
+
log("peerConnection.oniceconnectionstatechange => " + peerConnection.iceConnectionState);
|
|
529
|
+
const newState = mapConnectionState(peerConnection.iceConnectionState);
|
|
530
|
+
if (newState !== ConnectionState.Connected) {
|
|
531
|
+
(_a = callbacks.onConnectionStateChange) == null ? void 0 : _a.call(callbacks, newState);
|
|
532
|
+
}
|
|
533
|
+
};
|
|
534
|
+
peerConnection.ontrack = (event) => {
|
|
535
|
+
var _a;
|
|
536
|
+
log("peerConnection.ontrack", event);
|
|
537
|
+
(_a = callbacks.onSrcObjectReady) == null ? void 0 : _a.call(callbacks, event.streams[0]);
|
|
538
|
+
};
|
|
539
|
+
await peerConnection.setRemoteDescription(offer);
|
|
540
|
+
log("set remote description OK");
|
|
541
|
+
const sessionClientAnswer = await peerConnection.createAnswer();
|
|
542
|
+
log("create answer OK");
|
|
543
|
+
await peerConnection.setLocalDescription(sessionClientAnswer);
|
|
544
|
+
log("set local description OK");
|
|
545
|
+
await startConnection(streamIdFromServer, sessionClientAnswer, session_id);
|
|
546
|
+
log("start connection OK");
|
|
547
|
+
return {
|
|
349
548
|
/**
|
|
350
549
|
* Method to send request to server to get clip or talk depend on you payload
|
|
351
550
|
* @param payload
|
|
352
551
|
*/
|
|
353
|
-
speak(
|
|
354
|
-
return
|
|
552
|
+
speak(payload) {
|
|
553
|
+
return sendStreamRequest(streamIdFromServer, session_id, payload);
|
|
355
554
|
},
|
|
356
555
|
/**
|
|
357
556
|
* Method to close RTC connection
|
|
358
557
|
*/
|
|
359
558
|
async disconnect() {
|
|
360
|
-
var
|
|
361
|
-
if (
|
|
362
|
-
const
|
|
363
|
-
if (
|
|
364
|
-
if (
|
|
365
|
-
(
|
|
559
|
+
var _a, _b;
|
|
560
|
+
if (streamIdFromServer) {
|
|
561
|
+
const state = mapConnectionState(peerConnection.iceConnectionState);
|
|
562
|
+
if (peerConnection) {
|
|
563
|
+
if (state === ConnectionState.New) {
|
|
564
|
+
(_a = callbacks.onVideoStateChange) == null ? void 0 : _a.call(callbacks, StreamingState.Stop);
|
|
565
|
+
clearInterval(videoStatsInterval);
|
|
366
566
|
return;
|
|
367
567
|
}
|
|
368
|
-
|
|
568
|
+
peerConnection.close();
|
|
569
|
+
peerConnection.oniceconnectionstatechange = null;
|
|
570
|
+
peerConnection.onnegotiationneeded = null;
|
|
571
|
+
peerConnection.onicecandidate = null;
|
|
572
|
+
peerConnection.ontrack = null;
|
|
369
573
|
}
|
|
370
574
|
try {
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
575
|
+
if (state === ConnectionState.Connected) {
|
|
576
|
+
await close(streamIdFromServer, session_id).catch((_) => {
|
|
577
|
+
});
|
|
578
|
+
}
|
|
579
|
+
} catch (e) {
|
|
580
|
+
log("Error on close stream connection", e);
|
|
375
581
|
}
|
|
376
|
-
(
|
|
582
|
+
(_b = callbacks.onVideoStateChange) == null ? void 0 : _b.call(callbacks, StreamingState.Stop);
|
|
583
|
+
clearInterval(videoStatsInterval);
|
|
377
584
|
}
|
|
378
585
|
},
|
|
379
586
|
/**
|
|
380
587
|
* Session identifier information, should be returned in the body of all streaming requests
|
|
381
588
|
*/
|
|
382
|
-
sessionId:
|
|
589
|
+
sessionId: session_id,
|
|
383
590
|
/**
|
|
384
591
|
* Id of current RTC stream
|
|
385
592
|
*/
|
|
386
|
-
streamId:
|
|
593
|
+
streamId: streamIdFromServer
|
|
387
594
|
};
|
|
388
595
|
}
|
|
389
|
-
let
|
|
390
|
-
function
|
|
391
|
-
const
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
596
|
+
let mixpanelEvents = {};
|
|
597
|
+
function initializeAnalytics(config) {
|
|
598
|
+
const source = (window == null ? void 0 : window.hasOwnProperty("DID_AGENTS_API")) ? "agents-ui" : "agents-sdk";
|
|
599
|
+
const presenter = config.agent.presenter;
|
|
600
|
+
const analyticProps = {
|
|
601
|
+
token: config.token || "testKey",
|
|
602
|
+
distinct_id: config.distinctId || getExternalId(),
|
|
603
|
+
agentId: config.agent.id,
|
|
604
|
+
agentType: presenter.type === "clip" && presenter.presenter_id.startsWith("v2_") ? "clip_v2" : presenter.type,
|
|
605
|
+
owner_id: config.agent.owner_id ?? ""
|
|
397
606
|
};
|
|
398
607
|
return {
|
|
399
|
-
...
|
|
400
|
-
isEnabled:
|
|
608
|
+
...analyticProps,
|
|
609
|
+
isEnabled: config.isEnabled ?? true,
|
|
401
610
|
getRandom: () => Math.random().toString(16).slice(2),
|
|
402
|
-
track(
|
|
403
|
-
if (!this.isEnabled)
|
|
404
|
-
return Promise.
|
|
611
|
+
track(event, props) {
|
|
612
|
+
if (!this.isEnabled) {
|
|
613
|
+
return Promise.resolve();
|
|
614
|
+
}
|
|
405
615
|
const {
|
|
406
|
-
audioPath
|
|
407
|
-
...
|
|
408
|
-
} =
|
|
616
|
+
audioPath,
|
|
617
|
+
...sendProps
|
|
618
|
+
} = props || {};
|
|
619
|
+
const options = {
|
|
409
620
|
method: "POST",
|
|
410
621
|
headers: {
|
|
411
622
|
"Content-Type": "application/x-www-form-urlencoded"
|
|
412
623
|
},
|
|
413
624
|
body: new URLSearchParams({
|
|
414
625
|
data: JSON.stringify([{
|
|
415
|
-
event
|
|
626
|
+
event,
|
|
416
627
|
properties: {
|
|
417
|
-
...
|
|
418
|
-
...
|
|
419
|
-
source
|
|
628
|
+
...sendProps,
|
|
629
|
+
...analyticProps,
|
|
630
|
+
source,
|
|
420
631
|
time: Date.now(),
|
|
421
632
|
$insert_id: this.getRandom(),
|
|
422
633
|
origin: window.location.href,
|
|
@@ -427,469 +638,636 @@ function Ie(e) {
|
|
|
427
638
|
}])
|
|
428
639
|
})
|
|
429
640
|
};
|
|
430
|
-
return fetch("https://api-js.mixpanel.com/track/?verbose=1&ip=1",
|
|
431
|
-
},
|
|
432
|
-
linkTrack(
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
641
|
+
return fetch("https://api-js.mixpanel.com/track/?verbose=1&ip=1", options).then((response) => response.json()).catch((err) => console.error(err));
|
|
642
|
+
},
|
|
643
|
+
linkTrack(mixpanelEvent, props, event, dependencies) {
|
|
644
|
+
if (!mixpanelEvents[mixpanelEvent]) {
|
|
645
|
+
mixpanelEvents[mixpanelEvent] = {
|
|
646
|
+
events: {},
|
|
647
|
+
resolvedDependencies: []
|
|
648
|
+
};
|
|
649
|
+
}
|
|
650
|
+
if (!dependencies.includes(event)) {
|
|
651
|
+
dependencies.push(event);
|
|
652
|
+
}
|
|
653
|
+
const linkedEvent = mixpanelEvents[mixpanelEvent];
|
|
654
|
+
linkedEvent.events[event] = {
|
|
655
|
+
props
|
|
656
|
+
};
|
|
657
|
+
linkedEvent.resolvedDependencies.push(event);
|
|
658
|
+
const allDependenciesResolved = dependencies.every((value) => linkedEvent.resolvedDependencies.includes(value));
|
|
659
|
+
if (allDependenciesResolved) {
|
|
660
|
+
const aggregatedProps = dependencies.reduce((acc, curr) => {
|
|
661
|
+
if (linkedEvent.events[curr]) {
|
|
662
|
+
return {
|
|
663
|
+
...acc,
|
|
664
|
+
...linkedEvent.events[curr].props
|
|
665
|
+
};
|
|
666
|
+
}
|
|
667
|
+
return acc;
|
|
668
|
+
}, {});
|
|
669
|
+
this.track(mixpanelEvent, aggregatedProps);
|
|
670
|
+
linkedEvent.resolvedDependencies = linkedEvent.resolvedDependencies.filter((event2) => !dependencies.includes(event2));
|
|
671
|
+
dependencies.forEach((event2) => {
|
|
672
|
+
delete linkedEvent.events[event2];
|
|
447
673
|
});
|
|
448
674
|
}
|
|
449
675
|
}
|
|
450
676
|
};
|
|
451
677
|
}
|
|
452
|
-
function
|
|
453
|
-
var
|
|
454
|
-
const
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
678
|
+
function getAnaliticsInfo(agent) {
|
|
679
|
+
var _a, _b, _c, _d;
|
|
680
|
+
const mobileOrDesktop = () => {
|
|
681
|
+
return /Mobi|Android/i.test(navigator.userAgent) ? "Mobile" : "Desktop";
|
|
682
|
+
};
|
|
683
|
+
const getUserOS = () => {
|
|
684
|
+
const platform = navigator.platform;
|
|
685
|
+
if (platform.toLowerCase().includes("win")) {
|
|
686
|
+
return "Windows";
|
|
687
|
+
} else if (platform.toLowerCase().includes("mac")) {
|
|
688
|
+
return "Mac OS X";
|
|
689
|
+
} else if (platform.toLowerCase().includes("linux")) {
|
|
690
|
+
return "Linux";
|
|
691
|
+
} else {
|
|
692
|
+
return "Unknown";
|
|
693
|
+
}
|
|
694
|
+
};
|
|
695
|
+
const presenter = agent.presenter;
|
|
458
696
|
return {
|
|
459
|
-
$os: `${
|
|
460
|
-
isMobile: `${
|
|
697
|
+
$os: `${getUserOS()}`,
|
|
698
|
+
isMobile: `${mobileOrDesktop() == "Mobile"}`,
|
|
461
699
|
browser: navigator.userAgent,
|
|
462
700
|
origin: window.location.origin,
|
|
463
|
-
agentType:
|
|
701
|
+
agentType: presenter.type === "clip" && presenter.presenter_id.startsWith("v2_") ? "clip_v2" : presenter.type,
|
|
464
702
|
agentVoice: {
|
|
465
|
-
voiceId: (
|
|
466
|
-
provider: (
|
|
703
|
+
voiceId: (_b = (_a = agent.presenter) == null ? void 0 : _a.voice) == null ? void 0 : _b.voice_id,
|
|
704
|
+
provider: (_d = (_c = agent.presenter) == null ? void 0 : _c.voice) == null ? void 0 : _d.type
|
|
467
705
|
}
|
|
468
706
|
};
|
|
469
707
|
}
|
|
470
|
-
function
|
|
471
|
-
var
|
|
708
|
+
function getStreamAnalyticsProps(data, agent, additionalProps) {
|
|
709
|
+
var _a, _b;
|
|
472
710
|
const {
|
|
473
|
-
event
|
|
474
|
-
...
|
|
475
|
-
} =
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
} = (
|
|
482
|
-
|
|
483
|
-
|
|
711
|
+
event,
|
|
712
|
+
...baseProps
|
|
713
|
+
} = data;
|
|
714
|
+
const {
|
|
715
|
+
template
|
|
716
|
+
} = (agent == null ? void 0 : agent.llm) || {};
|
|
717
|
+
const {
|
|
718
|
+
language
|
|
719
|
+
} = ((_a = agent == null ? void 0 : agent.presenter) == null ? void 0 : _a.voice) || {};
|
|
720
|
+
const {
|
|
721
|
+
stitch
|
|
722
|
+
} = (agent == null ? void 0 : agent.presenter) || {};
|
|
723
|
+
const props = {
|
|
724
|
+
...baseProps,
|
|
484
725
|
llm: {
|
|
485
|
-
...
|
|
486
|
-
template
|
|
726
|
+
...baseProps.llm,
|
|
727
|
+
template
|
|
487
728
|
},
|
|
488
729
|
script: {
|
|
489
|
-
...
|
|
730
|
+
...baseProps.script,
|
|
490
731
|
provider: {
|
|
491
|
-
...(
|
|
492
|
-
language
|
|
732
|
+
...(_b = baseProps == null ? void 0 : baseProps.script) == null ? void 0 : _b.provider,
|
|
733
|
+
language
|
|
493
734
|
}
|
|
494
735
|
},
|
|
495
|
-
stitch
|
|
496
|
-
...
|
|
736
|
+
stitch,
|
|
737
|
+
...additionalProps
|
|
497
738
|
};
|
|
739
|
+
return props;
|
|
498
740
|
}
|
|
499
|
-
let
|
|
500
|
-
const
|
|
501
|
-
function
|
|
502
|
-
var
|
|
741
|
+
let messageSentTimestamp = 0;
|
|
742
|
+
const connectionRetryTimeoutInMs = 20 * 1e3;
|
|
743
|
+
function getAgentStreamArgs(agent, chat, options, greeting) {
|
|
744
|
+
var _a, _b, _c, _d, _e;
|
|
503
745
|
return {
|
|
504
|
-
videoType:
|
|
505
|
-
output_resolution: (
|
|
506
|
-
session_timeout: (
|
|
507
|
-
stream_warmup: (
|
|
508
|
-
compatibility_mode: (
|
|
509
|
-
stream_greeting: (
|
|
746
|
+
videoType: mapVideoType(agent.presenter.type),
|
|
747
|
+
output_resolution: (_a = options == null ? void 0 : options.streamOptions) == null ? void 0 : _a.outputResolution,
|
|
748
|
+
session_timeout: (_b = options == null ? void 0 : options.streamOptions) == null ? void 0 : _b.sessionTimeout,
|
|
749
|
+
stream_warmup: (_c = options == null ? void 0 : options.streamOptions) == null ? void 0 : _c.streamWarmup,
|
|
750
|
+
compatibility_mode: (_d = options == null ? void 0 : options.streamOptions) == null ? void 0 : _d.compatibilityMode,
|
|
751
|
+
stream_greeting: ((_e = options == null ? void 0 : options.streamOptions) == null ? void 0 : _e.streamGreeting) && !chat ? greeting : void 0
|
|
510
752
|
};
|
|
511
753
|
}
|
|
512
|
-
function
|
|
513
|
-
return
|
|
754
|
+
function getRequestHeaders(chatMode) {
|
|
755
|
+
return chatMode === ChatMode.Playground ? {
|
|
514
756
|
headers: {
|
|
515
|
-
[
|
|
757
|
+
[PLAYGROUND_HEADER]: "true"
|
|
516
758
|
}
|
|
517
759
|
} : {};
|
|
518
760
|
}
|
|
519
|
-
async function
|
|
761
|
+
async function newChat(agentId, agentsApi, analytics, chatMode, persist) {
|
|
520
762
|
try {
|
|
521
|
-
const
|
|
522
|
-
persist:
|
|
523
|
-
},
|
|
524
|
-
|
|
763
|
+
const newChat2 = await agentsApi.newChat(agentId, {
|
|
764
|
+
persist: persist ?? false
|
|
765
|
+
}, getRequestHeaders(chatMode));
|
|
766
|
+
analytics.track("agent-chat", {
|
|
525
767
|
event: "created",
|
|
526
|
-
chat_id:
|
|
527
|
-
agent_id:
|
|
528
|
-
mode:
|
|
529
|
-
})
|
|
530
|
-
|
|
768
|
+
chat_id: newChat2.id,
|
|
769
|
+
agent_id: agentId,
|
|
770
|
+
mode: chatMode
|
|
771
|
+
});
|
|
772
|
+
return newChat2;
|
|
773
|
+
} catch (error) {
|
|
531
774
|
try {
|
|
532
|
-
console.error(
|
|
533
|
-
const
|
|
534
|
-
if ((
|
|
775
|
+
console.error(error);
|
|
776
|
+
const parsedError = JSON.parse(error.message);
|
|
777
|
+
if ((parsedError == null ? void 0 : parsedError.kind) === "InsufficientCreditsError") {
|
|
535
778
|
throw new Error("InsufficientCreditsError");
|
|
536
|
-
|
|
537
|
-
|
|
779
|
+
}
|
|
780
|
+
} catch (jsonError) {
|
|
781
|
+
console.error("Error parsing the error message:", jsonError);
|
|
538
782
|
}
|
|
539
783
|
throw new Error("Cannot create new chat");
|
|
540
784
|
}
|
|
541
785
|
}
|
|
542
|
-
function
|
|
543
|
-
return new Promise(async (
|
|
544
|
-
var
|
|
545
|
-
|
|
546
|
-
const
|
|
547
|
-
|
|
548
|
-
},
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
786
|
+
function initializeStreamAndChat(agent, options, agentsApi, analytics, chat, greeting) {
|
|
787
|
+
return new Promise(async (outerResolve, outerReject) => {
|
|
788
|
+
var _a;
|
|
789
|
+
messageSentTimestamp = 0;
|
|
790
|
+
const timeoutId = setTimeout(() => {
|
|
791
|
+
reject(new Error("Could not connect"));
|
|
792
|
+
}, connectionRetryTimeoutInMs);
|
|
793
|
+
const resolve = (value) => {
|
|
794
|
+
clearTimeout(timeoutId);
|
|
795
|
+
outerResolve(value);
|
|
796
|
+
};
|
|
797
|
+
const reject = (error) => {
|
|
798
|
+
clearTimeout(timeoutId);
|
|
799
|
+
outerReject(error);
|
|
552
800
|
};
|
|
553
|
-
let
|
|
554
|
-
!
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
801
|
+
let chatPromise;
|
|
802
|
+
if (!chat && options.mode !== ChatMode.DirectPlayback) {
|
|
803
|
+
chatPromise = newChat(agent.id, agentsApi, analytics, options.mode, options.persistentChat).catch((e) => {
|
|
804
|
+
reject(e);
|
|
805
|
+
});
|
|
806
|
+
}
|
|
807
|
+
const streamingManager = await createStreamingManager(agent.id, getAgentStreamArgs(agent, chat, options, greeting), {
|
|
808
|
+
...options,
|
|
809
|
+
analytics,
|
|
810
|
+
warmup: (_a = options.streamOptions) == null ? void 0 : _a.streamWarmup,
|
|
561
811
|
callbacks: {
|
|
562
|
-
...
|
|
563
|
-
onConnectionStateChange: async (
|
|
564
|
-
var
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
812
|
+
...options.callbacks,
|
|
813
|
+
onConnectionStateChange: async (state) => {
|
|
814
|
+
var _a2, _b, _c, _d;
|
|
815
|
+
if (state === ConnectionState.Connected) {
|
|
816
|
+
if (chatPromise) {
|
|
817
|
+
chat = await chatPromise;
|
|
818
|
+
}
|
|
819
|
+
if (streamingManager) {
|
|
820
|
+
(_b = (_a2 = options.callbacks).onConnectionStateChange) == null ? void 0 : _b.call(_a2, state);
|
|
821
|
+
resolve({
|
|
822
|
+
chat,
|
|
823
|
+
streamingManager
|
|
824
|
+
});
|
|
825
|
+
} else if (chat) {
|
|
826
|
+
reject(new Error("Something went wrong while initializing the manager"));
|
|
827
|
+
}
|
|
828
|
+
} else {
|
|
829
|
+
(_d = (_c = options.callbacks).onConnectionStateChange) == null ? void 0 : _d.call(_c, state);
|
|
830
|
+
}
|
|
569
831
|
},
|
|
570
|
-
onVideoStateChange(
|
|
571
|
-
var
|
|
572
|
-
(
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
832
|
+
onVideoStateChange(state) {
|
|
833
|
+
var _a2, _b;
|
|
834
|
+
(_b = (_a2 = options.callbacks).onVideoStateChange) == null ? void 0 : _b.call(_a2, state);
|
|
835
|
+
if (messageSentTimestamp > 0 && state === StreamingState.Start) {
|
|
836
|
+
analytics.linkTrack("agent-video", {
|
|
837
|
+
event: "start",
|
|
838
|
+
latency: Date.now() - messageSentTimestamp
|
|
839
|
+
}, "start", [StreamEvents.StreamVideoCreated]);
|
|
840
|
+
}
|
|
576
841
|
}
|
|
577
842
|
}
|
|
578
|
-
}).catch(
|
|
843
|
+
}).catch(reject);
|
|
579
844
|
});
|
|
580
845
|
}
|
|
581
|
-
function
|
|
582
|
-
var
|
|
583
|
-
const
|
|
584
|
-
if (
|
|
585
|
-
const
|
|
586
|
-
|
|
587
|
-
|
|
846
|
+
function getGreetings(agent) {
|
|
847
|
+
var _a;
|
|
848
|
+
const greetings = (_a = agent.greetings) == null ? void 0 : _a.filter((greeting) => greeting.length > 0);
|
|
849
|
+
if (greetings && greetings.length > 0) {
|
|
850
|
+
const randomIndex = Math.floor(Math.random() * greetings.length);
|
|
851
|
+
return greetings[randomIndex];
|
|
852
|
+
} else {
|
|
853
|
+
return `Hi! I'm ${agent.preview_name || "My Agent"}. How can I help you?`;
|
|
588
854
|
}
|
|
589
|
-
return `Hi! I'm ${e.preview_name || "My Agent"}. How can I help you?`;
|
|
590
855
|
}
|
|
591
|
-
function
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
856
|
+
function getInitialMessages(greeting, initialMessages) {
|
|
857
|
+
if (initialMessages && initialMessages.length > 0) {
|
|
858
|
+
return initialMessages;
|
|
859
|
+
}
|
|
860
|
+
return [{
|
|
861
|
+
content: greeting,
|
|
862
|
+
id: getRandom(),
|
|
595
863
|
role: "assistant",
|
|
596
864
|
created_at: (/* @__PURE__ */ new Date()).toISOString()
|
|
597
865
|
}];
|
|
598
866
|
}
|
|
599
|
-
function
|
|
600
|
-
if (
|
|
601
|
-
return
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
867
|
+
function getMessageContent(chatEventQueue) {
|
|
868
|
+
if (chatEventQueue["answer"] !== void 0) {
|
|
869
|
+
return chatEventQueue["answer"];
|
|
870
|
+
}
|
|
871
|
+
let currentSequence = 0;
|
|
872
|
+
let content = "";
|
|
873
|
+
while (currentSequence in chatEventQueue) {
|
|
874
|
+
content += chatEventQueue[currentSequence];
|
|
875
|
+
currentSequence++;
|
|
876
|
+
}
|
|
877
|
+
return content;
|
|
606
878
|
}
|
|
607
|
-
function
|
|
608
|
-
if (!(
|
|
879
|
+
function processChatEvent(event, data, chatEventQueue, items, onNewMessage) {
|
|
880
|
+
if (!(event === ChatProgress.Partial || event === ChatProgress.Answer)) {
|
|
609
881
|
return;
|
|
610
|
-
|
|
611
|
-
|
|
882
|
+
}
|
|
883
|
+
const lastMessage = items.messages[items.messages.length - 1];
|
|
884
|
+
if ((lastMessage == null ? void 0 : lastMessage.role) !== "assistant") {
|
|
612
885
|
return;
|
|
886
|
+
}
|
|
613
887
|
const {
|
|
614
|
-
content
|
|
615
|
-
sequence
|
|
616
|
-
} =
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
888
|
+
content,
|
|
889
|
+
sequence
|
|
890
|
+
} = data;
|
|
891
|
+
if (event === ChatProgress.Partial) {
|
|
892
|
+
chatEventQueue[sequence] = content;
|
|
893
|
+
} else {
|
|
894
|
+
chatEventQueue["answer"] = content;
|
|
895
|
+
}
|
|
896
|
+
const messageContent = getMessageContent(chatEventQueue);
|
|
897
|
+
if (lastMessage.content !== messageContent || event === ChatProgress.Answer) {
|
|
898
|
+
lastMessage.content = messageContent;
|
|
899
|
+
onNewMessage == null ? void 0 : onNewMessage([...items.messages], event);
|
|
900
|
+
}
|
|
620
901
|
}
|
|
621
|
-
async function
|
|
622
|
-
var
|
|
623
|
-
let
|
|
624
|
-
|
|
902
|
+
async function createAgentManager(agent, options) {
|
|
903
|
+
var _a, _b, _c;
|
|
904
|
+
let chatEventQueue = {};
|
|
905
|
+
let firstConnection = true;
|
|
906
|
+
const items = {
|
|
625
907
|
messages: [],
|
|
626
|
-
chatMode:
|
|
627
|
-
}
|
|
628
|
-
|
|
629
|
-
const
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
908
|
+
chatMode: options.mode || ChatMode.Functional
|
|
909
|
+
};
|
|
910
|
+
const baseURL = options.baseURL || didApiUrl;
|
|
911
|
+
const wsURL = options.wsURL || didSocketApiUrl;
|
|
912
|
+
const mxKey = options.mixpanelKey || mixpanelKey;
|
|
913
|
+
const agentsApi = createAgentsApi(options.auth, baseURL, options.callbacks.onError);
|
|
914
|
+
const agentInstance = await agentsApi.getById(agent);
|
|
915
|
+
const greeting = getGreetings(agentInstance);
|
|
916
|
+
items.messages = getInitialMessages(greeting, options.initialMessages);
|
|
917
|
+
(_b = (_a = options.callbacks).onNewMessage) == null ? void 0 : _b.call(_a, [...items.messages], "answer");
|
|
918
|
+
const analytics = initializeAnalytics({
|
|
919
|
+
token: mxKey,
|
|
920
|
+
agent: agentInstance,
|
|
921
|
+
isEnabled: options.enableAnalitics,
|
|
922
|
+
distinctId: options.distinctId
|
|
633
923
|
});
|
|
634
|
-
|
|
924
|
+
analytics.track("agent-sdk", {
|
|
635
925
|
event: "loaded",
|
|
636
|
-
|
|
926
|
+
...getAnaliticsInfo(agentInstance)
|
|
637
927
|
});
|
|
638
|
-
const
|
|
639
|
-
onMessage: (
|
|
640
|
-
var
|
|
641
|
-
if ("content" in
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
928
|
+
const socketManagerCallbacks = {
|
|
929
|
+
onMessage: (event, data) => {
|
|
930
|
+
var _a2, _b2;
|
|
931
|
+
if ("content" in data) {
|
|
932
|
+
processChatEvent(event, data, chatEventQueue, items, options.callbacks.onNewMessage);
|
|
933
|
+
if (event === ChatProgress.Answer) {
|
|
934
|
+
analytics.track("agent-message-received", {
|
|
935
|
+
messages: items.messages.length,
|
|
936
|
+
mode: items.chatMode
|
|
937
|
+
});
|
|
938
|
+
}
|
|
939
|
+
} else {
|
|
940
|
+
const SEvent = StreamEvents;
|
|
941
|
+
const completedEvents = [SEvent.StreamVideoDone, SEvent.StreamVideoError, SEvent.StreamVideoRejected];
|
|
942
|
+
const failedEvents = [SEvent.StreamFailed, SEvent.StreamVideoError, SEvent.StreamVideoRejected];
|
|
943
|
+
const props = getStreamAnalyticsProps(data, agentInstance, {
|
|
944
|
+
mode: items.chatMode
|
|
649
945
|
});
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
946
|
+
event = event;
|
|
947
|
+
if (event === SEvent.StreamVideoCreated) {
|
|
948
|
+
analytics.linkTrack("agent-video", props, SEvent.StreamVideoCreated, ["start"]);
|
|
949
|
+
} else if (completedEvents.includes(event)) {
|
|
950
|
+
const streamEvent = event.split("/")[1];
|
|
951
|
+
analytics.track("agent-video", {
|
|
952
|
+
...props,
|
|
953
|
+
event: streamEvent
|
|
954
|
+
});
|
|
955
|
+
}
|
|
956
|
+
if (failedEvents.includes(event)) {
|
|
957
|
+
(_b2 = (_a2 = options.callbacks).onError) == null ? void 0 : _b2.call(_a2, new Error(`Stream failed with event ${event}`), {
|
|
958
|
+
data
|
|
657
959
|
});
|
|
658
960
|
}
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
}
|
|
961
|
+
if (data.event === SEvent.StreamDone) {
|
|
962
|
+
disconnect();
|
|
963
|
+
}
|
|
662
964
|
}
|
|
663
965
|
}
|
|
664
966
|
};
|
|
665
|
-
async function
|
|
666
|
-
var
|
|
667
|
-
(
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
967
|
+
async function connect2(newChat2) {
|
|
968
|
+
var _a2, _b2, _c2, _d, _e, _f, _g;
|
|
969
|
+
(_b2 = (_a2 = options.callbacks).onConnectionStateChange) == null ? void 0 : _b2.call(_a2, ConnectionState.Connecting);
|
|
970
|
+
messageSentTimestamp = 0;
|
|
971
|
+
const maxRetries = 3;
|
|
972
|
+
if (newChat2 && !firstConnection) {
|
|
973
|
+
delete items.chat;
|
|
974
|
+
items.messages = getInitialMessages(greeting);
|
|
975
|
+
(_d = (_c2 = options.callbacks).onNewMessage) == null ? void 0 : _d.call(_c2, [...items.messages], "answer");
|
|
976
|
+
}
|
|
977
|
+
const websocketPromise = options.mode === ChatMode.DirectPlayback ? Promise.resolve(void 0) : createSocketManager(options.auth, wsURL, socketManagerCallbacks);
|
|
978
|
+
async function connectWithRetry() {
|
|
979
|
+
var _a3, _b3, _c3;
|
|
980
|
+
for (let attempt = 1; attempt <= maxRetries; attempt++) {
|
|
673
981
|
try {
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
982
|
+
const initializationResult = await initializeStreamAndChat(agentInstance, options, agentsApi, analytics, items.chat, newChat2 ? greeting : void 0);
|
|
983
|
+
if (initializationResult.chat && initializationResult.chat.id !== ((_a3 = items.chat) == null ? void 0 : _a3.id)) {
|
|
984
|
+
items.chat = initializationResult.chat;
|
|
985
|
+
(_c3 = (_b3 = options.callbacks).onNewChat) == null ? void 0 : _c3.call(_b3, initializationResult.chat.id);
|
|
986
|
+
}
|
|
987
|
+
return initializationResult;
|
|
988
|
+
} catch (e) {
|
|
989
|
+
if (!((e == null ? void 0 : e.message) === "Could not connect")) {
|
|
990
|
+
throw e;
|
|
991
|
+
}
|
|
678
992
|
}
|
|
993
|
+
}
|
|
679
994
|
throw new Error("Could not connect");
|
|
680
995
|
}
|
|
681
|
-
const
|
|
682
|
-
var
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
996
|
+
const initPromise = connectWithRetry().catch((e) => {
|
|
997
|
+
var _a3, _b3;
|
|
998
|
+
changeMode(ChatMode.Maintenance);
|
|
999
|
+
(_b3 = (_a3 = options.callbacks).onConnectionStateChange) == null ? void 0 : _b3.call(_a3, ConnectionState.Fail);
|
|
1000
|
+
throw e;
|
|
1001
|
+
});
|
|
1002
|
+
const [socketManager, {
|
|
1003
|
+
streamingManager,
|
|
1004
|
+
chat
|
|
1005
|
+
}] = await Promise.all([websocketPromise, initPromise]);
|
|
1006
|
+
if (chat && chat.id !== ((_e = items.chat) == null ? void 0 : _e.id)) {
|
|
1007
|
+
(_g = (_f = options.callbacks).onNewChat) == null ? void 0 : _g.call(_f, chat.id);
|
|
1008
|
+
}
|
|
1009
|
+
items.streamingManager = streamingManager;
|
|
1010
|
+
items.socketManager = socketManager;
|
|
1011
|
+
items.chat = chat;
|
|
1012
|
+
firstConnection = false;
|
|
1013
|
+
changeMode((chat == null ? void 0 : chat.chat_mode) ?? options.mode ?? ChatMode.Functional);
|
|
689
1014
|
}
|
|
690
|
-
async function
|
|
691
|
-
var
|
|
692
|
-
(
|
|
1015
|
+
async function disconnect() {
|
|
1016
|
+
var _a2, _b2, _c2, _d;
|
|
1017
|
+
(_a2 = items.socketManager) == null ? void 0 : _a2.disconnect();
|
|
1018
|
+
await ((_b2 = items.streamingManager) == null ? void 0 : _b2.disconnect());
|
|
1019
|
+
delete items.streamingManager;
|
|
1020
|
+
delete items.socketManager;
|
|
1021
|
+
(_d = (_c2 = options.callbacks).onConnectionStateChange) == null ? void 0 : _d.call(_c2, ConnectionState.Disconnected);
|
|
693
1022
|
}
|
|
694
|
-
async function
|
|
695
|
-
var
|
|
696
|
-
|
|
697
|
-
mode
|
|
698
|
-
|
|
1023
|
+
async function changeMode(mode) {
|
|
1024
|
+
var _a2, _b2;
|
|
1025
|
+
if (mode !== items.chatMode) {
|
|
1026
|
+
analytics.track("agent-mode-change", {
|
|
1027
|
+
mode
|
|
1028
|
+
});
|
|
1029
|
+
items.chatMode = mode;
|
|
1030
|
+
if (items.chatMode !== ChatMode.Functional) {
|
|
1031
|
+
await disconnect();
|
|
1032
|
+
}
|
|
1033
|
+
(_b2 = (_a2 = options.callbacks).onModeChange) == null ? void 0 : _b2.call(_a2, mode);
|
|
1034
|
+
}
|
|
699
1035
|
}
|
|
700
1036
|
return {
|
|
701
|
-
agent:
|
|
702
|
-
starterMessages: ((
|
|
703
|
-
|
|
1037
|
+
agent: agentInstance,
|
|
1038
|
+
starterMessages: ((_c = agentInstance.knowledge) == null ? void 0 : _c.starter_message) || [],
|
|
1039
|
+
getSTTToken: (options2) => {
|
|
1040
|
+
var _a2;
|
|
1041
|
+
return ((_a2 = items.chat) == null ? void 0 : _a2.id) ? agentsApi.getSTTToken(agentInstance.id, items.chat.id, options2) : Promise.resolve(void 0);
|
|
1042
|
+
},
|
|
1043
|
+
changeMode,
|
|
704
1044
|
async connect() {
|
|
705
|
-
var
|
|
706
|
-
await
|
|
1045
|
+
var _a2;
|
|
1046
|
+
await connect2(true);
|
|
1047
|
+
analytics.track("agent-chat", {
|
|
707
1048
|
event: "connect",
|
|
708
|
-
chatId: (
|
|
709
|
-
agentId:
|
|
710
|
-
mode:
|
|
1049
|
+
chatId: (_a2 = items.chat) == null ? void 0 : _a2.id,
|
|
1050
|
+
agentId: agentInstance.id,
|
|
1051
|
+
mode: items.chatMode
|
|
711
1052
|
});
|
|
712
1053
|
},
|
|
713
1054
|
async reconnect() {
|
|
714
|
-
var
|
|
715
|
-
await
|
|
1055
|
+
var _a2;
|
|
1056
|
+
await disconnect();
|
|
1057
|
+
await connect2(false);
|
|
1058
|
+
analytics.track("agent-chat", {
|
|
716
1059
|
event: "reconnect",
|
|
717
|
-
chatId: (
|
|
718
|
-
agentId:
|
|
719
|
-
mode:
|
|
1060
|
+
chatId: (_a2 = items.chat) == null ? void 0 : _a2.id,
|
|
1061
|
+
agentId: agentInstance.id,
|
|
1062
|
+
mode: items.chatMode
|
|
720
1063
|
});
|
|
721
1064
|
},
|
|
722
1065
|
async disconnect() {
|
|
723
|
-
var
|
|
724
|
-
await
|
|
1066
|
+
var _a2;
|
|
1067
|
+
await disconnect();
|
|
1068
|
+
analytics.track("agent-chat", {
|
|
725
1069
|
event: "disconnect",
|
|
726
|
-
chatId: (
|
|
727
|
-
agentId:
|
|
728
|
-
mode:
|
|
1070
|
+
chatId: (_a2 = items.chat) == null ? void 0 : _a2.id,
|
|
1071
|
+
agentId: agentInstance.id,
|
|
1072
|
+
mode: items.chatMode
|
|
729
1073
|
});
|
|
730
1074
|
},
|
|
731
|
-
async chat(
|
|
732
|
-
var
|
|
733
|
-
const
|
|
734
|
-
|
|
1075
|
+
async chat(userMessage) {
|
|
1076
|
+
var _a2, _b2, _c2, _d, _e, _f;
|
|
1077
|
+
const id = getRandom();
|
|
1078
|
+
chatEventQueue = {};
|
|
735
1079
|
try {
|
|
736
|
-
|
|
1080
|
+
messageSentTimestamp = Date.now();
|
|
1081
|
+
if (options.mode === ChatMode.DirectPlayback) {
|
|
737
1082
|
throw new Error("Direct playback is enabled, chat is disabled");
|
|
738
|
-
if (
|
|
1083
|
+
} else if (userMessage.length >= 800) {
|
|
739
1084
|
throw new Error("Message cannot be more than 800 characters");
|
|
740
|
-
if (
|
|
1085
|
+
} else if (userMessage.length === 0) {
|
|
741
1086
|
throw new Error("Message cannot be empty");
|
|
742
|
-
if (
|
|
1087
|
+
} else if (items.chatMode === ChatMode.Maintenance) {
|
|
743
1088
|
throw new Error("Chat is in maintenance mode");
|
|
744
|
-
if (![
|
|
745
|
-
if (
|
|
746
|
-
if (!t.chat)
|
|
747
|
-
throw new Error("Chat is not initialized");
|
|
748
|
-
} else
|
|
1089
|
+
} else if (![ChatMode.TextOnly, ChatMode.Playground].includes(items.chatMode)) {
|
|
1090
|
+
if (!items.streamingManager) {
|
|
749
1091
|
throw new Error("Streaming manager is not initialized");
|
|
750
|
-
|
|
751
|
-
|
|
1092
|
+
} else if (!items.chat) {
|
|
1093
|
+
throw new Error("Chat is not initialized");
|
|
1094
|
+
}
|
|
1095
|
+
}
|
|
1096
|
+
items.messages.push({
|
|
1097
|
+
id: getRandom(),
|
|
752
1098
|
role: "user",
|
|
753
|
-
content:
|
|
754
|
-
created_at: new Date(
|
|
755
|
-
})
|
|
756
|
-
|
|
757
|
-
|
|
1099
|
+
content: userMessage,
|
|
1100
|
+
created_at: new Date(messageSentTimestamp).toISOString()
|
|
1101
|
+
});
|
|
1102
|
+
(_b2 = (_a2 = options.callbacks).onNewMessage) == null ? void 0 : _b2.call(_a2, [...items.messages], "user");
|
|
1103
|
+
if (!items.chat) {
|
|
1104
|
+
items.chat = await newChat(agentInstance.id, agentsApi, analytics, items.chatMode, options.persistentChat);
|
|
1105
|
+
(_d = (_c2 = options.callbacks).onNewChat) == null ? void 0 : _d.call(_c2, items.chat.id);
|
|
1106
|
+
}
|
|
1107
|
+
const newMessage = {
|
|
1108
|
+
id,
|
|
758
1109
|
role: "assistant",
|
|
759
1110
|
content: "",
|
|
760
1111
|
created_at: (/* @__PURE__ */ new Date()).toISOString(),
|
|
761
1112
|
matches: []
|
|
762
|
-
}
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
1113
|
+
};
|
|
1114
|
+
const messages = [...items.messages];
|
|
1115
|
+
items.messages.push(newMessage);
|
|
1116
|
+
const sendChat = (chatId) => {
|
|
1117
|
+
var _a3, _b3;
|
|
1118
|
+
return agentsApi.chat(agentInstance.id, chatId, {
|
|
1119
|
+
sessionId: (_a3 = items.streamingManager) == null ? void 0 : _a3.sessionId,
|
|
1120
|
+
streamId: (_b3 = items.streamingManager) == null ? void 0 : _b3.streamId,
|
|
1121
|
+
chatMode: items.chatMode,
|
|
1122
|
+
messages: messages.map(({
|
|
1123
|
+
matches,
|
|
1124
|
+
...message
|
|
1125
|
+
}) => message)
|
|
1126
|
+
}, getRequestHeaders(items.chatMode));
|
|
1127
|
+
};
|
|
1128
|
+
const response = await sendChat(items.chat.id).catch(async (error) => {
|
|
1129
|
+
var _a3;
|
|
1130
|
+
if (!((_a3 = error == null ? void 0 : error.message) == null ? void 0 : _a3.includes("missing or invalid session_id"))) {
|
|
1131
|
+
throw error;
|
|
1132
|
+
}
|
|
1133
|
+
await disconnect();
|
|
1134
|
+
await connect2(false);
|
|
1135
|
+
return sendChat(items.chat.id);
|
|
780
1136
|
});
|
|
781
|
-
|
|
1137
|
+
analytics.track("agent-message-send", {
|
|
782
1138
|
event: "success",
|
|
783
|
-
mode:
|
|
784
|
-
messages:
|
|
785
|
-
})
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
1139
|
+
mode: items.chatMode,
|
|
1140
|
+
messages: items.messages.length + 1
|
|
1141
|
+
});
|
|
1142
|
+
newMessage.context = response.context;
|
|
1143
|
+
newMessage.matches = response.matches;
|
|
1144
|
+
if (response.result) {
|
|
1145
|
+
newMessage.content = response.result;
|
|
1146
|
+
analytics.track("agent-message-received", {
|
|
1147
|
+
latency: Date.now() - messageSentTimestamp,
|
|
1148
|
+
mode: items.chatMode,
|
|
1149
|
+
messages: items.messages.length
|
|
1150
|
+
});
|
|
1151
|
+
(_f = (_e = options.callbacks).onNewMessage) == null ? void 0 : _f.call(_e, [...items.messages], "answer");
|
|
1152
|
+
}
|
|
1153
|
+
return response;
|
|
1154
|
+
} catch (e) {
|
|
1155
|
+
if (items.messages[items.messages.length - 1].id === id) {
|
|
1156
|
+
items.messages.pop();
|
|
1157
|
+
}
|
|
1158
|
+
analytics.track("agent-message-send", {
|
|
792
1159
|
event: "error",
|
|
793
|
-
mode:
|
|
794
|
-
messages:
|
|
795
|
-
})
|
|
1160
|
+
mode: items.chatMode,
|
|
1161
|
+
messages: items.messages.length
|
|
1162
|
+
});
|
|
1163
|
+
throw e;
|
|
796
1164
|
}
|
|
797
1165
|
},
|
|
798
|
-
rate(
|
|
799
|
-
var
|
|
800
|
-
const
|
|
801
|
-
if (
|
|
802
|
-
if (!S)
|
|
803
|
-
throw new Error("Message not found");
|
|
804
|
-
} else
|
|
1166
|
+
rate(messageId, score, rateId) {
|
|
1167
|
+
var _a2, _b2, _c2, _d;
|
|
1168
|
+
const message = items.messages.find((message2) => message2.id === messageId);
|
|
1169
|
+
if (!items.chat) {
|
|
805
1170
|
throw new Error("Chat is not initialized");
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
1171
|
+
} else if (!message) {
|
|
1172
|
+
throw new Error("Message not found");
|
|
1173
|
+
}
|
|
1174
|
+
const matches = ((_a2 = message.matches) == null ? void 0 : _a2.map((match) => [match.document_id, match.id])) ?? [];
|
|
1175
|
+
analytics.track("agent-rate", {
|
|
1176
|
+
event: rateId ? "update" : "create",
|
|
1177
|
+
thumb: score === 1 ? "up" : "down",
|
|
1178
|
+
knowledge_id: ((_b2 = agentInstance.knowledge) == null ? void 0 : _b2.id) ?? "",
|
|
1179
|
+
mode: items.chatMode,
|
|
1180
|
+
matches,
|
|
1181
|
+
score
|
|
1182
|
+
});
|
|
1183
|
+
if (rateId) {
|
|
1184
|
+
return agentsApi.updateRating(agentInstance.id, items.chat.id, rateId, {
|
|
1185
|
+
knowledge_id: ((_c2 = agentInstance.knowledge) == null ? void 0 : _c2.id) ?? "",
|
|
1186
|
+
message_id: messageId,
|
|
1187
|
+
matches,
|
|
1188
|
+
score
|
|
1189
|
+
});
|
|
1190
|
+
}
|
|
1191
|
+
return agentsApi.createRating(agentInstance.id, items.chat.id, {
|
|
1192
|
+
knowledge_id: ((_d = agentInstance.knowledge) == null ? void 0 : _d.id) ?? "",
|
|
1193
|
+
message_id: messageId,
|
|
1194
|
+
matches,
|
|
1195
|
+
score
|
|
824
1196
|
});
|
|
825
1197
|
},
|
|
826
|
-
deleteRate(
|
|
827
|
-
var
|
|
828
|
-
if (!
|
|
1198
|
+
deleteRate(id) {
|
|
1199
|
+
var _a2;
|
|
1200
|
+
if (!items.chat) {
|
|
829
1201
|
throw new Error("Chat is not initialized");
|
|
830
|
-
|
|
1202
|
+
}
|
|
1203
|
+
analytics.track("agent-rate-delete", {
|
|
831
1204
|
type: "text",
|
|
832
|
-
chat_id: (
|
|
833
|
-
id
|
|
834
|
-
mode:
|
|
835
|
-
})
|
|
1205
|
+
chat_id: (_a2 = items.chat) == null ? void 0 : _a2.id,
|
|
1206
|
+
id,
|
|
1207
|
+
mode: items.chatMode
|
|
1208
|
+
});
|
|
1209
|
+
return agentsApi.deleteRating(agentInstance.id, items.chat.id, id);
|
|
836
1210
|
},
|
|
837
|
-
speak(
|
|
838
|
-
if (!
|
|
1211
|
+
speak(payload) {
|
|
1212
|
+
if (!items.streamingManager) {
|
|
839
1213
|
throw new Error("Please connect to the agent first");
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
1214
|
+
}
|
|
1215
|
+
function getScript() {
|
|
1216
|
+
if (typeof payload === "string") {
|
|
1217
|
+
if (!agentInstance.presenter.voice) {
|
|
843
1218
|
throw new Error("Presenter voice is not initialized");
|
|
1219
|
+
}
|
|
844
1220
|
return {
|
|
845
1221
|
type: "text",
|
|
846
|
-
provider:
|
|
847
|
-
input:
|
|
848
|
-
ssml:
|
|
1222
|
+
provider: agentInstance.presenter.voice,
|
|
1223
|
+
input: payload,
|
|
1224
|
+
ssml: false
|
|
849
1225
|
};
|
|
850
1226
|
}
|
|
851
|
-
if (
|
|
852
|
-
if (!
|
|
1227
|
+
if (payload.type === "text" && !payload.provider) {
|
|
1228
|
+
if (!agentInstance.presenter.voice) {
|
|
853
1229
|
throw new Error("Presenter voice is not initialized");
|
|
1230
|
+
}
|
|
854
1231
|
return {
|
|
855
1232
|
type: "text",
|
|
856
|
-
provider:
|
|
857
|
-
input:
|
|
858
|
-
ssml:
|
|
1233
|
+
provider: agentInstance.presenter.voice,
|
|
1234
|
+
input: payload.input,
|
|
1235
|
+
ssml: payload.ssml
|
|
859
1236
|
};
|
|
860
1237
|
}
|
|
861
|
-
return
|
|
1238
|
+
return payload;
|
|
862
1239
|
}
|
|
863
|
-
const
|
|
864
|
-
|
|
865
|
-
|
|
1240
|
+
const script = getScript();
|
|
1241
|
+
analytics.track("agent-speak", script);
|
|
1242
|
+
return items.streamingManager.speak({
|
|
1243
|
+
script
|
|
866
1244
|
});
|
|
867
1245
|
}
|
|
868
1246
|
};
|
|
869
1247
|
}
|
|
870
|
-
function
|
|
1248
|
+
function getAgent(agentId, auth, baseURL) {
|
|
871
1249
|
const {
|
|
872
|
-
getById
|
|
873
|
-
} =
|
|
874
|
-
return
|
|
1250
|
+
getById
|
|
1251
|
+
} = createAgentsApi(auth, baseURL || didApiUrl);
|
|
1252
|
+
return getById(agentId);
|
|
875
1253
|
}
|
|
876
1254
|
export {
|
|
877
|
-
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
1255
|
+
AgentStatus,
|
|
1256
|
+
ChatMode,
|
|
1257
|
+
ChatProgress,
|
|
1258
|
+
ConnectionState,
|
|
1259
|
+
DocumentType,
|
|
1260
|
+
KnowledgeType,
|
|
1261
|
+
PlanGroup,
|
|
1262
|
+
Providers,
|
|
1263
|
+
RateState,
|
|
1264
|
+
StreamEvents,
|
|
1265
|
+
StreamingState,
|
|
1266
|
+
Subject,
|
|
1267
|
+
UserPlan,
|
|
1268
|
+
VideoType,
|
|
1269
|
+
VoiceAccess,
|
|
1270
|
+
createAgentManager,
|
|
1271
|
+
getAgent,
|
|
1272
|
+
mapVideoType
|
|
895
1273
|
};
|