@d-id/client-sdk 1.0.19-beta.135 → 1.0.19-beta.136
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 +1001 -635
- package/dist/index.umd.cjs +1274 -1
- package/dist/src/api/agents.d.ts +1 -1
- package/package.json +2 -2
package/dist/index.js
CHANGED
|
@@ -1,425 +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);
|
|
110
246
|
},
|
|
111
|
-
getSTTToken(
|
|
112
|
-
return
|
|
247
|
+
getSTTToken(agentId, options) {
|
|
248
|
+
return client.get(`/${agentId}/stt-token`, options);
|
|
113
249
|
}
|
|
114
250
|
};
|
|
115
251
|
}
|
|
116
|
-
const
|
|
117
|
-
function
|
|
118
|
-
return new Promise((
|
|
252
|
+
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
|
|
253
|
+
function connect(options) {
|
|
254
|
+
return new Promise((resolve, reject) => {
|
|
119
255
|
const {
|
|
120
|
-
callbacks
|
|
121
|
-
host
|
|
122
|
-
auth
|
|
123
|
-
} =
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
256
|
+
callbacks,
|
|
257
|
+
host,
|
|
258
|
+
auth
|
|
259
|
+
} = options;
|
|
260
|
+
const {
|
|
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);
|
|
133
277
|
};
|
|
134
278
|
});
|
|
135
279
|
}
|
|
136
|
-
async function
|
|
280
|
+
async function connectWithRetries(options) {
|
|
137
281
|
const {
|
|
138
|
-
retries
|
|
139
|
-
} =
|
|
140
|
-
let
|
|
141
|
-
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++) {
|
|
142
286
|
try {
|
|
143
|
-
|
|
144
|
-
} catch (
|
|
145
|
-
if (
|
|
146
|
-
throw
|
|
147
|
-
|
|
287
|
+
socket = await connect(options);
|
|
288
|
+
} catch (e) {
|
|
289
|
+
if (attempt === retries) {
|
|
290
|
+
throw e;
|
|
291
|
+
}
|
|
292
|
+
await sleep(attempt * 500);
|
|
148
293
|
}
|
|
149
|
-
|
|
294
|
+
}
|
|
295
|
+
return socket;
|
|
150
296
|
}
|
|
151
|
-
async function
|
|
152
|
-
const
|
|
153
|
-
|
|
154
|
-
|
|
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,
|
|
155
302
|
callbacks: {
|
|
156
|
-
onError:
|
|
157
|
-
onMessage: (
|
|
158
|
-
const
|
|
159
|
-
|
|
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));
|
|
160
307
|
}
|
|
161
308
|
}
|
|
162
309
|
});
|
|
163
310
|
return {
|
|
164
|
-
socket
|
|
165
|
-
disconnect: () =>
|
|
166
|
-
subscribeToEvents: (
|
|
311
|
+
socket,
|
|
312
|
+
disconnect: () => socket.close(),
|
|
313
|
+
subscribeToEvents: (callback2) => messageCallbacks.push(callback2)
|
|
167
314
|
};
|
|
168
315
|
}
|
|
169
|
-
const
|
|
170
|
-
function
|
|
171
|
-
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);
|
|
172
319
|
return {
|
|
173
|
-
createStream(
|
|
174
|
-
return
|
|
175
|
-
output_resolution:
|
|
176
|
-
compatibility_mode:
|
|
177
|
-
stream_warmup:
|
|
178
|
-
session_timeout:
|
|
179
|
-
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
|
|
180
327
|
});
|
|
181
328
|
},
|
|
182
|
-
startConnection(
|
|
183
|
-
return
|
|
184
|
-
session_id:
|
|
185
|
-
answer
|
|
329
|
+
startConnection(streamId, answer, sessionId) {
|
|
330
|
+
return client.post(`/streams/${streamId}/sdp`, {
|
|
331
|
+
session_id: sessionId,
|
|
332
|
+
answer
|
|
186
333
|
});
|
|
187
334
|
},
|
|
188
|
-
addIceCandidate(
|
|
189
|
-
return
|
|
190
|
-
session_id:
|
|
191
|
-
...
|
|
335
|
+
addIceCandidate(streamId, candidate, sessionId) {
|
|
336
|
+
return client.post(`/streams/${streamId}/ice`, {
|
|
337
|
+
session_id: sessionId,
|
|
338
|
+
...candidate
|
|
192
339
|
});
|
|
193
340
|
},
|
|
194
|
-
sendStreamRequest(
|
|
195
|
-
return
|
|
196
|
-
session_id:
|
|
197
|
-
...
|
|
341
|
+
sendStreamRequest(streamId, sessionId, payload) {
|
|
342
|
+
return client.post(`/streams/${streamId}`, {
|
|
343
|
+
session_id: sessionId,
|
|
344
|
+
...payload
|
|
198
345
|
});
|
|
199
346
|
},
|
|
200
|
-
close(
|
|
201
|
-
return
|
|
202
|
-
session_id:
|
|
347
|
+
close(streamId, sessionId) {
|
|
348
|
+
return client.delete(`/streams/${streamId}`, {
|
|
349
|
+
session_id: sessionId
|
|
203
350
|
});
|
|
204
351
|
}
|
|
205
352
|
};
|
|
206
353
|
}
|
|
207
|
-
function
|
|
208
|
-
const
|
|
354
|
+
function createApi(auth, host, agentId, onError) {
|
|
355
|
+
const client = createClient(auth, `${host}/agents/${agentId}`, onError);
|
|
209
356
|
return {
|
|
210
|
-
createStream(
|
|
211
|
-
return
|
|
212
|
-
driver_url:
|
|
213
|
-
face:
|
|
214
|
-
config:
|
|
215
|
-
output_resolution:
|
|
216
|
-
compatibility_mode:
|
|
217
|
-
stream_warmup:
|
|
218
|
-
session_timeout:
|
|
219
|
-
stream_greeting:
|
|
220
|
-
},
|
|
221
|
-
},
|
|
222
|
-
startConnection(
|
|
223
|
-
return
|
|
224
|
-
session_id:
|
|
225
|
-
answer
|
|
226
|
-
},
|
|
227
|
-
},
|
|
228
|
-
addIceCandidate(
|
|
229
|
-
return
|
|
230
|
-
session_id:
|
|
231
|
-
...
|
|
232
|
-
},
|
|
233
|
-
},
|
|
234
|
-
sendStreamRequest(
|
|
235
|
-
return
|
|
236
|
-
session_id:
|
|
237
|
-
...
|
|
238
|
-
},
|
|
239
|
-
},
|
|
240
|
-
close(
|
|
241
|
-
return
|
|
242
|
-
session_id:
|
|
243
|
-
},
|
|
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);
|
|
244
391
|
}
|
|
245
392
|
};
|
|
246
393
|
}
|
|
247
|
-
let
|
|
248
|
-
const
|
|
249
|
-
|
|
250
|
-
|
|
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) {
|
|
251
399
|
case "connected":
|
|
252
|
-
return
|
|
400
|
+
return ConnectionState.Connected;
|
|
253
401
|
case "checking":
|
|
254
|
-
return
|
|
402
|
+
return ConnectionState.Connecting;
|
|
255
403
|
case "failed":
|
|
256
|
-
return
|
|
404
|
+
return ConnectionState.Fail;
|
|
257
405
|
case "new":
|
|
258
|
-
return
|
|
406
|
+
return ConnectionState.New;
|
|
259
407
|
case "closed":
|
|
260
|
-
return
|
|
408
|
+
return ConnectionState.Closed;
|
|
261
409
|
case "disconnected":
|
|
262
|
-
return
|
|
410
|
+
return ConnectionState.Disconnected;
|
|
263
411
|
case "completed":
|
|
264
|
-
return
|
|
412
|
+
return ConnectionState.Completed;
|
|
265
413
|
default:
|
|
266
|
-
return
|
|
414
|
+
return ConnectionState.New;
|
|
267
415
|
}
|
|
268
416
|
}
|
|
269
|
-
function
|
|
270
|
-
let
|
|
271
|
-
return (
|
|
272
|
-
for (const
|
|
273
|
-
if (
|
|
274
|
-
const
|
|
275
|
-
|
|
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;
|
|
276
426
|
}
|
|
277
|
-
|
|
427
|
+
}
|
|
428
|
+
return false;
|
|
278
429
|
};
|
|
279
430
|
}
|
|
280
|
-
function
|
|
281
|
-
const
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
let
|
|
285
|
-
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();
|
|
286
439
|
return setInterval(async () => {
|
|
287
|
-
const
|
|
288
|
-
|
|
289
|
-
|
|
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);
|
|
290
460
|
}
|
|
291
|
-
async function
|
|
292
|
-
debug
|
|
293
|
-
callbacks
|
|
294
|
-
auth
|
|
295
|
-
baseURL
|
|
296
|
-
warmup
|
|
461
|
+
async function createStreamingManager(agentId, agent, {
|
|
462
|
+
debug = false,
|
|
463
|
+
callbacks,
|
|
464
|
+
auth,
|
|
465
|
+
baseURL = didApiUrl,
|
|
466
|
+
warmup
|
|
297
467
|
}) {
|
|
298
|
-
|
|
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);
|
|
299
476
|
const {
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
} = await h(n), w = new Re({
|
|
311
|
-
iceServers: v
|
|
312
|
-
}), D = w.createDataChannel("JanusDataChannel");
|
|
313
|
-
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) {
|
|
314
487
|
throw new Error("Could not create session_id");
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
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);
|
|
323
500
|
try {
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
}
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
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
|
|
334
515
|
});
|
|
335
516
|
}
|
|
336
|
-
}
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
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
|
+
}
|
|
340
524
|
}
|
|
341
|
-
}
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
}
|
|
350
|
-
|
|
351
|
-
|
|
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 {
|
|
352
548
|
/**
|
|
353
549
|
* Method to send request to server to get clip or talk depend on you payload
|
|
354
550
|
* @param payload
|
|
355
551
|
*/
|
|
356
|
-
speak(
|
|
357
|
-
return
|
|
552
|
+
speak(payload) {
|
|
553
|
+
return sendStreamRequest(streamIdFromServer, session_id, payload);
|
|
358
554
|
},
|
|
359
555
|
/**
|
|
360
556
|
* Method to close RTC connection
|
|
361
557
|
*/
|
|
362
558
|
async disconnect() {
|
|
363
|
-
var
|
|
364
|
-
if (
|
|
365
|
-
const
|
|
366
|
-
if (
|
|
367
|
-
if (
|
|
368
|
-
(
|
|
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);
|
|
369
566
|
return;
|
|
370
567
|
}
|
|
371
|
-
|
|
568
|
+
peerConnection.close();
|
|
569
|
+
peerConnection.oniceconnectionstatechange = null;
|
|
570
|
+
peerConnection.onnegotiationneeded = null;
|
|
571
|
+
peerConnection.onicecandidate = null;
|
|
572
|
+
peerConnection.ontrack = null;
|
|
372
573
|
}
|
|
373
574
|
try {
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
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);
|
|
378
581
|
}
|
|
379
|
-
(
|
|
582
|
+
(_b = callbacks.onVideoStateChange) == null ? void 0 : _b.call(callbacks, StreamingState.Stop);
|
|
583
|
+
clearInterval(videoStatsInterval);
|
|
380
584
|
}
|
|
381
585
|
},
|
|
382
586
|
/**
|
|
383
587
|
* Session identifier information, should be returned in the body of all streaming requests
|
|
384
588
|
*/
|
|
385
|
-
sessionId:
|
|
589
|
+
sessionId: session_id,
|
|
386
590
|
/**
|
|
387
591
|
* Id of current RTC stream
|
|
388
592
|
*/
|
|
389
|
-
streamId:
|
|
593
|
+
streamId: streamIdFromServer
|
|
390
594
|
};
|
|
391
595
|
}
|
|
392
|
-
let
|
|
393
|
-
function
|
|
394
|
-
const
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
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 ?? ""
|
|
400
606
|
};
|
|
401
607
|
return {
|
|
402
|
-
...
|
|
403
|
-
isEnabled:
|
|
608
|
+
...analyticProps,
|
|
609
|
+
isEnabled: config.isEnabled ?? true,
|
|
404
610
|
getRandom: () => Math.random().toString(16).slice(2),
|
|
405
|
-
track(
|
|
406
|
-
if (!this.isEnabled)
|
|
611
|
+
track(event, props) {
|
|
612
|
+
if (!this.isEnabled) {
|
|
407
613
|
return Promise.resolve();
|
|
614
|
+
}
|
|
408
615
|
const {
|
|
409
|
-
audioPath
|
|
410
|
-
...
|
|
411
|
-
} =
|
|
616
|
+
audioPath,
|
|
617
|
+
...sendProps
|
|
618
|
+
} = props || {};
|
|
619
|
+
const options = {
|
|
412
620
|
method: "POST",
|
|
413
621
|
headers: {
|
|
414
622
|
"Content-Type": "application/x-www-form-urlencoded"
|
|
415
623
|
},
|
|
416
624
|
body: new URLSearchParams({
|
|
417
625
|
data: JSON.stringify([{
|
|
418
|
-
event
|
|
626
|
+
event,
|
|
419
627
|
properties: {
|
|
420
|
-
...
|
|
421
|
-
...
|
|
422
|
-
source
|
|
628
|
+
...sendProps,
|
|
629
|
+
...analyticProps,
|
|
630
|
+
source,
|
|
423
631
|
time: Date.now(),
|
|
424
632
|
$insert_id: this.getRandom(),
|
|
425
633
|
origin: window.location.href,
|
|
@@ -430,475 +638,633 @@ function Ae(e) {
|
|
|
430
638
|
}])
|
|
431
639
|
})
|
|
432
640
|
};
|
|
433
|
-
return fetch("https://api-js.mixpanel.com/track/?verbose=1&ip=1",
|
|
434
|
-
},
|
|
435
|
-
linkTrack(
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
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];
|
|
450
673
|
});
|
|
451
674
|
}
|
|
452
675
|
}
|
|
453
676
|
};
|
|
454
677
|
}
|
|
455
|
-
function
|
|
456
|
-
var
|
|
457
|
-
const
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
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;
|
|
461
696
|
return {
|
|
462
|
-
$os: `${
|
|
463
|
-
isMobile: `${
|
|
697
|
+
$os: `${getUserOS()}`,
|
|
698
|
+
isMobile: `${mobileOrDesktop() == "Mobile"}`,
|
|
464
699
|
browser: navigator.userAgent,
|
|
465
700
|
origin: window.location.origin,
|
|
466
|
-
agentType:
|
|
701
|
+
agentType: presenter.type === "clip" && presenter.presenter_id.startsWith("v2_") ? "clip_v2" : presenter.type,
|
|
467
702
|
agentVoice: {
|
|
468
|
-
voiceId: (
|
|
469
|
-
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
|
|
470
705
|
}
|
|
471
706
|
};
|
|
472
707
|
}
|
|
473
|
-
function
|
|
474
|
-
var
|
|
708
|
+
function getStreamAnalyticsProps(data, agent, additionalProps) {
|
|
709
|
+
var _a, _b;
|
|
475
710
|
const {
|
|
476
|
-
event
|
|
477
|
-
...
|
|
478
|
-
} =
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
} = (
|
|
485
|
-
|
|
486
|
-
|
|
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,
|
|
487
725
|
llm: {
|
|
488
|
-
...
|
|
489
|
-
template
|
|
726
|
+
...baseProps.llm,
|
|
727
|
+
template
|
|
490
728
|
},
|
|
491
729
|
script: {
|
|
492
|
-
...
|
|
730
|
+
...baseProps.script,
|
|
493
731
|
provider: {
|
|
494
|
-
...(
|
|
495
|
-
language
|
|
732
|
+
...(_b = baseProps == null ? void 0 : baseProps.script) == null ? void 0 : _b.provider,
|
|
733
|
+
language
|
|
496
734
|
}
|
|
497
735
|
},
|
|
498
|
-
stitch
|
|
499
|
-
...
|
|
736
|
+
stitch,
|
|
737
|
+
...additionalProps
|
|
500
738
|
};
|
|
739
|
+
return props;
|
|
501
740
|
}
|
|
502
|
-
let
|
|
503
|
-
const
|
|
504
|
-
function
|
|
505
|
-
var
|
|
741
|
+
let messageSentTimestamp = 0;
|
|
742
|
+
const connectionRetryTimeoutInMs = 20 * 1e3;
|
|
743
|
+
function getAgentStreamArgs(agent, chat, options, greeting) {
|
|
744
|
+
var _a, _b, _c, _d, _e;
|
|
506
745
|
return {
|
|
507
|
-
videoType:
|
|
508
|
-
output_resolution: (
|
|
509
|
-
session_timeout: (
|
|
510
|
-
stream_warmup: (
|
|
511
|
-
compatibility_mode: (
|
|
512
|
-
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
|
|
513
752
|
};
|
|
514
753
|
}
|
|
515
|
-
function
|
|
516
|
-
return
|
|
754
|
+
function getRequestHeaders(chatMode) {
|
|
755
|
+
return chatMode === ChatMode.Playground ? {
|
|
517
756
|
headers: {
|
|
518
|
-
[
|
|
757
|
+
[PLAYGROUND_HEADER]: "true"
|
|
519
758
|
}
|
|
520
759
|
} : {};
|
|
521
760
|
}
|
|
522
|
-
async function
|
|
761
|
+
async function newChat(agentId, agentsApi, analytics, chatMode, persist) {
|
|
523
762
|
try {
|
|
524
|
-
const
|
|
525
|
-
persist:
|
|
526
|
-
},
|
|
527
|
-
|
|
763
|
+
const newChat2 = await agentsApi.newChat(agentId, {
|
|
764
|
+
persist: persist ?? false
|
|
765
|
+
}, getRequestHeaders(chatMode));
|
|
766
|
+
analytics.track("agent-chat", {
|
|
528
767
|
event: "created",
|
|
529
|
-
chat_id:
|
|
530
|
-
agent_id:
|
|
531
|
-
mode:
|
|
532
|
-
})
|
|
533
|
-
|
|
768
|
+
chat_id: newChat2.id,
|
|
769
|
+
agent_id: agentId,
|
|
770
|
+
mode: chatMode
|
|
771
|
+
});
|
|
772
|
+
return newChat2;
|
|
773
|
+
} catch (error) {
|
|
534
774
|
try {
|
|
535
|
-
console.error(
|
|
536
|
-
const
|
|
537
|
-
if ((
|
|
775
|
+
console.error(error);
|
|
776
|
+
const parsedError = JSON.parse(error.message);
|
|
777
|
+
if ((parsedError == null ? void 0 : parsedError.kind) === "InsufficientCreditsError") {
|
|
538
778
|
throw new Error("InsufficientCreditsError");
|
|
539
|
-
|
|
540
|
-
|
|
779
|
+
}
|
|
780
|
+
} catch (jsonError) {
|
|
781
|
+
console.error("Error parsing the error message:", jsonError);
|
|
541
782
|
}
|
|
542
783
|
throw new Error("Cannot create new chat");
|
|
543
784
|
}
|
|
544
785
|
}
|
|
545
|
-
function
|
|
546
|
-
return new Promise(async (
|
|
547
|
-
var
|
|
548
|
-
|
|
549
|
-
const
|
|
550
|
-
|
|
551
|
-
},
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
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);
|
|
555
796
|
};
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
}
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
analytics
|
|
563
|
-
|
|
797
|
+
const reject = (error) => {
|
|
798
|
+
clearTimeout(timeoutId);
|
|
799
|
+
outerReject(error);
|
|
800
|
+
};
|
|
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,
|
|
564
811
|
callbacks: {
|
|
565
|
-
...
|
|
566
|
-
onConnectionStateChange: async (
|
|
567
|
-
var
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
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
|
+
}
|
|
572
831
|
},
|
|
573
|
-
onVideoStateChange(
|
|
574
|
-
var
|
|
575
|
-
(
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
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
|
+
}
|
|
579
841
|
}
|
|
580
842
|
}
|
|
581
|
-
}).catch(
|
|
843
|
+
}).catch(reject);
|
|
582
844
|
});
|
|
583
845
|
}
|
|
584
|
-
function
|
|
585
|
-
var
|
|
586
|
-
const
|
|
587
|
-
if (
|
|
588
|
-
const
|
|
589
|
-
return
|
|
590
|
-
} else
|
|
591
|
-
return `Hi! I'm ${
|
|
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?`;
|
|
854
|
+
}
|
|
592
855
|
}
|
|
593
|
-
function
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
856
|
+
function getInitialMessages(greeting, initialMessages) {
|
|
857
|
+
if (initialMessages && initialMessages.length > 0) {
|
|
858
|
+
return initialMessages;
|
|
859
|
+
}
|
|
860
|
+
return [{
|
|
861
|
+
content: greeting,
|
|
862
|
+
id: getRandom(),
|
|
597
863
|
role: "assistant",
|
|
598
864
|
created_at: (/* @__PURE__ */ new Date()).toISOString()
|
|
599
865
|
}];
|
|
600
866
|
}
|
|
601
|
-
function
|
|
602
|
-
if (
|
|
603
|
-
return
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
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;
|
|
608
878
|
}
|
|
609
|
-
function
|
|
610
|
-
if (!(
|
|
879
|
+
function processChatEvent(event, data, chatEventQueue, items, onNewMessage) {
|
|
880
|
+
if (!(event === ChatProgress.Partial || event === ChatProgress.Answer)) {
|
|
611
881
|
return;
|
|
612
|
-
|
|
613
|
-
|
|
882
|
+
}
|
|
883
|
+
const lastMessage = items.messages[items.messages.length - 1];
|
|
884
|
+
if ((lastMessage == null ? void 0 : lastMessage.role) !== "assistant") {
|
|
614
885
|
return;
|
|
886
|
+
}
|
|
615
887
|
const {
|
|
616
|
-
content
|
|
617
|
-
sequence
|
|
618
|
-
} =
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
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
|
+
}
|
|
622
901
|
}
|
|
623
|
-
async function
|
|
624
|
-
var
|
|
625
|
-
let
|
|
626
|
-
|
|
902
|
+
async function createAgentManager(agent, options) {
|
|
903
|
+
var _a, _b, _c;
|
|
904
|
+
let chatEventQueue = {};
|
|
905
|
+
let firstConnection = true;
|
|
906
|
+
const items = {
|
|
627
907
|
messages: [],
|
|
628
|
-
chatMode:
|
|
629
|
-
}
|
|
630
|
-
|
|
631
|
-
const
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
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
|
|
636
923
|
});
|
|
637
|
-
|
|
924
|
+
analytics.track("agent-sdk", {
|
|
638
925
|
event: "loaded",
|
|
639
|
-
...
|
|
926
|
+
...getAnaliticsInfo(agentInstance)
|
|
640
927
|
});
|
|
641
|
-
const
|
|
642
|
-
onMessage: (
|
|
643
|
-
var
|
|
644
|
-
if ("content" in
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
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
|
|
652
945
|
});
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
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
|
|
660
959
|
});
|
|
661
960
|
}
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
}
|
|
961
|
+
if (data.event === SEvent.StreamDone) {
|
|
962
|
+
disconnect();
|
|
963
|
+
}
|
|
665
964
|
}
|
|
666
965
|
}
|
|
667
966
|
};
|
|
668
|
-
async function
|
|
669
|
-
var
|
|
670
|
-
(
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
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++) {
|
|
677
981
|
try {
|
|
678
|
-
const
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
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
|
+
}
|
|
683
992
|
}
|
|
993
|
+
}
|
|
684
994
|
throw new Error("Could not connect");
|
|
685
995
|
}
|
|
686
|
-
const
|
|
687
|
-
var
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
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);
|
|
694
1014
|
}
|
|
695
|
-
async function
|
|
696
|
-
var
|
|
697
|
-
(
|
|
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);
|
|
698
1022
|
}
|
|
699
|
-
async function
|
|
700
|
-
var
|
|
701
|
-
|
|
702
|
-
mode
|
|
703
|
-
|
|
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
|
+
}
|
|
704
1035
|
}
|
|
705
1036
|
return {
|
|
706
|
-
agent:
|
|
707
|
-
starterMessages: ((
|
|
708
|
-
getSTTToken:
|
|
709
|
-
|
|
710
|
-
return (c = t.chat) != null && c.id ? d.getSTTToken(g.id, t.chat.id) : void 0;
|
|
711
|
-
},
|
|
712
|
-
changeMode: _,
|
|
1037
|
+
agent: agentInstance,
|
|
1038
|
+
starterMessages: ((_c = agentInstance.knowledge) == null ? void 0 : _c.starter_message) || [],
|
|
1039
|
+
getSTTToken: () => agentsApi.getSTTToken(agentInstance.id),
|
|
1040
|
+
changeMode,
|
|
713
1041
|
async connect() {
|
|
714
|
-
var
|
|
715
|
-
await
|
|
1042
|
+
var _a2;
|
|
1043
|
+
await connect2(true);
|
|
1044
|
+
analytics.track("agent-chat", {
|
|
716
1045
|
event: "connect",
|
|
717
|
-
chatId: (
|
|
718
|
-
agentId:
|
|
719
|
-
mode:
|
|
1046
|
+
chatId: (_a2 = items.chat) == null ? void 0 : _a2.id,
|
|
1047
|
+
agentId: agentInstance.id,
|
|
1048
|
+
mode: items.chatMode
|
|
720
1049
|
});
|
|
721
1050
|
},
|
|
722
1051
|
async reconnect() {
|
|
723
|
-
var
|
|
724
|
-
await
|
|
1052
|
+
var _a2;
|
|
1053
|
+
await disconnect();
|
|
1054
|
+
await connect2(false);
|
|
1055
|
+
analytics.track("agent-chat", {
|
|
725
1056
|
event: "reconnect",
|
|
726
|
-
chatId: (
|
|
727
|
-
agentId:
|
|
728
|
-
mode:
|
|
1057
|
+
chatId: (_a2 = items.chat) == null ? void 0 : _a2.id,
|
|
1058
|
+
agentId: agentInstance.id,
|
|
1059
|
+
mode: items.chatMode
|
|
729
1060
|
});
|
|
730
1061
|
},
|
|
731
1062
|
async disconnect() {
|
|
732
|
-
var
|
|
733
|
-
await
|
|
1063
|
+
var _a2;
|
|
1064
|
+
await disconnect();
|
|
1065
|
+
analytics.track("agent-chat", {
|
|
734
1066
|
event: "disconnect",
|
|
735
|
-
chatId: (
|
|
736
|
-
agentId:
|
|
737
|
-
mode:
|
|
1067
|
+
chatId: (_a2 = items.chat) == null ? void 0 : _a2.id,
|
|
1068
|
+
agentId: agentInstance.id,
|
|
1069
|
+
mode: items.chatMode
|
|
738
1070
|
});
|
|
739
1071
|
},
|
|
740
|
-
async chat(
|
|
741
|
-
var
|
|
742
|
-
const
|
|
743
|
-
|
|
1072
|
+
async chat(userMessage) {
|
|
1073
|
+
var _a2, _b2, _c2, _d, _e, _f;
|
|
1074
|
+
const id = getRandom();
|
|
1075
|
+
chatEventQueue = {};
|
|
744
1076
|
try {
|
|
745
|
-
|
|
1077
|
+
messageSentTimestamp = Date.now();
|
|
1078
|
+
if (options.mode === ChatMode.DirectPlayback) {
|
|
746
1079
|
throw new Error("Direct playback is enabled, chat is disabled");
|
|
747
|
-
if (
|
|
1080
|
+
} else if (userMessage.length >= 800) {
|
|
748
1081
|
throw new Error("Message cannot be more than 800 characters");
|
|
749
|
-
if (
|
|
1082
|
+
} else if (userMessage.length === 0) {
|
|
750
1083
|
throw new Error("Message cannot be empty");
|
|
751
|
-
if (
|
|
1084
|
+
} else if (items.chatMode === ChatMode.Maintenance) {
|
|
752
1085
|
throw new Error("Chat is in maintenance mode");
|
|
753
|
-
if (![
|
|
754
|
-
if (
|
|
755
|
-
if (!t.chat)
|
|
756
|
-
throw new Error("Chat is not initialized");
|
|
757
|
-
} else
|
|
1086
|
+
} else if (![ChatMode.TextOnly, ChatMode.Playground].includes(items.chatMode)) {
|
|
1087
|
+
if (!items.streamingManager) {
|
|
758
1088
|
throw new Error("Streaming manager is not initialized");
|
|
759
|
-
|
|
760
|
-
|
|
1089
|
+
} else if (!items.chat) {
|
|
1090
|
+
throw new Error("Chat is not initialized");
|
|
1091
|
+
}
|
|
1092
|
+
}
|
|
1093
|
+
items.messages.push({
|
|
1094
|
+
id: getRandom(),
|
|
761
1095
|
role: "user",
|
|
762
|
-
content:
|
|
763
|
-
created_at: new Date(
|
|
764
|
-
})
|
|
765
|
-
|
|
766
|
-
|
|
1096
|
+
content: userMessage,
|
|
1097
|
+
created_at: new Date(messageSentTimestamp).toISOString()
|
|
1098
|
+
});
|
|
1099
|
+
(_b2 = (_a2 = options.callbacks).onNewMessage) == null ? void 0 : _b2.call(_a2, [...items.messages], "user");
|
|
1100
|
+
if (!items.chat) {
|
|
1101
|
+
items.chat = await newChat(agentInstance.id, agentsApi, analytics, items.chatMode, options.persistentChat);
|
|
1102
|
+
(_d = (_c2 = options.callbacks).onNewChat) == null ? void 0 : _d.call(_c2, items.chat.id);
|
|
1103
|
+
}
|
|
1104
|
+
const newMessage = {
|
|
1105
|
+
id,
|
|
767
1106
|
role: "assistant",
|
|
768
1107
|
content: "",
|
|
769
1108
|
created_at: (/* @__PURE__ */ new Date()).toISOString(),
|
|
770
1109
|
matches: []
|
|
771
|
-
}
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
1110
|
+
};
|
|
1111
|
+
const messages = [...items.messages];
|
|
1112
|
+
items.messages.push(newMessage);
|
|
1113
|
+
const sendChat = (chatId) => {
|
|
1114
|
+
var _a3, _b3;
|
|
1115
|
+
return agentsApi.chat(agentInstance.id, chatId, {
|
|
1116
|
+
sessionId: (_a3 = items.streamingManager) == null ? void 0 : _a3.sessionId,
|
|
1117
|
+
streamId: (_b3 = items.streamingManager) == null ? void 0 : _b3.streamId,
|
|
1118
|
+
chatMode: items.chatMode,
|
|
1119
|
+
messages: messages.map(({
|
|
1120
|
+
matches,
|
|
1121
|
+
...message
|
|
1122
|
+
}) => message)
|
|
1123
|
+
}, getRequestHeaders(items.chatMode));
|
|
1124
|
+
};
|
|
1125
|
+
const response = await sendChat(items.chat.id).catch(async (error) => {
|
|
1126
|
+
var _a3;
|
|
1127
|
+
if (!((_a3 = error == null ? void 0 : error.message) == null ? void 0 : _a3.includes("missing or invalid session_id"))) {
|
|
1128
|
+
throw error;
|
|
1129
|
+
}
|
|
1130
|
+
await disconnect();
|
|
1131
|
+
await connect2(false);
|
|
1132
|
+
return sendChat(items.chat.id);
|
|
789
1133
|
});
|
|
790
|
-
|
|
1134
|
+
analytics.track("agent-message-send", {
|
|
791
1135
|
event: "success",
|
|
792
|
-
mode:
|
|
793
|
-
messages:
|
|
794
|
-
})
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
1136
|
+
mode: items.chatMode,
|
|
1137
|
+
messages: items.messages.length + 1
|
|
1138
|
+
});
|
|
1139
|
+
newMessage.context = response.context;
|
|
1140
|
+
newMessage.matches = response.matches;
|
|
1141
|
+
if (response.result) {
|
|
1142
|
+
newMessage.content = response.result;
|
|
1143
|
+
analytics.track("agent-message-received", {
|
|
1144
|
+
latency: Date.now() - messageSentTimestamp,
|
|
1145
|
+
mode: items.chatMode,
|
|
1146
|
+
messages: items.messages.length
|
|
1147
|
+
});
|
|
1148
|
+
(_f = (_e = options.callbacks).onNewMessage) == null ? void 0 : _f.call(_e, [...items.messages], "answer");
|
|
1149
|
+
}
|
|
1150
|
+
return response;
|
|
1151
|
+
} catch (e) {
|
|
1152
|
+
if (items.messages[items.messages.length - 1].id === id) {
|
|
1153
|
+
items.messages.pop();
|
|
1154
|
+
}
|
|
1155
|
+
analytics.track("agent-message-send", {
|
|
801
1156
|
event: "error",
|
|
802
|
-
mode:
|
|
803
|
-
messages:
|
|
804
|
-
})
|
|
1157
|
+
mode: items.chatMode,
|
|
1158
|
+
messages: items.messages.length
|
|
1159
|
+
});
|
|
1160
|
+
throw e;
|
|
805
1161
|
}
|
|
806
1162
|
},
|
|
807
|
-
rate(
|
|
808
|
-
var
|
|
809
|
-
const
|
|
810
|
-
if (
|
|
811
|
-
if (!k)
|
|
812
|
-
throw new Error("Message not found");
|
|
813
|
-
} else
|
|
1163
|
+
rate(messageId, score, rateId) {
|
|
1164
|
+
var _a2, _b2, _c2, _d;
|
|
1165
|
+
const message = items.messages.find((message2) => message2.id === messageId);
|
|
1166
|
+
if (!items.chat) {
|
|
814
1167
|
throw new Error("Chat is not initialized");
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
1168
|
+
} else if (!message) {
|
|
1169
|
+
throw new Error("Message not found");
|
|
1170
|
+
}
|
|
1171
|
+
const matches = ((_a2 = message.matches) == null ? void 0 : _a2.map((match) => [match.document_id, match.id])) ?? [];
|
|
1172
|
+
analytics.track("agent-rate", {
|
|
1173
|
+
event: rateId ? "update" : "create",
|
|
1174
|
+
thumb: score === 1 ? "up" : "down",
|
|
1175
|
+
knowledge_id: ((_b2 = agentInstance.knowledge) == null ? void 0 : _b2.id) ?? "",
|
|
1176
|
+
mode: items.chatMode,
|
|
1177
|
+
matches,
|
|
1178
|
+
score
|
|
1179
|
+
});
|
|
1180
|
+
if (rateId) {
|
|
1181
|
+
return agentsApi.updateRating(agentInstance.id, items.chat.id, rateId, {
|
|
1182
|
+
knowledge_id: ((_c2 = agentInstance.knowledge) == null ? void 0 : _c2.id) ?? "",
|
|
1183
|
+
message_id: messageId,
|
|
1184
|
+
matches,
|
|
1185
|
+
score
|
|
1186
|
+
});
|
|
1187
|
+
}
|
|
1188
|
+
return agentsApi.createRating(agentInstance.id, items.chat.id, {
|
|
1189
|
+
knowledge_id: ((_d = agentInstance.knowledge) == null ? void 0 : _d.id) ?? "",
|
|
1190
|
+
message_id: messageId,
|
|
1191
|
+
matches,
|
|
1192
|
+
score
|
|
833
1193
|
});
|
|
834
1194
|
},
|
|
835
|
-
deleteRate(
|
|
836
|
-
var
|
|
837
|
-
if (!
|
|
1195
|
+
deleteRate(id) {
|
|
1196
|
+
var _a2;
|
|
1197
|
+
if (!items.chat) {
|
|
838
1198
|
throw new Error("Chat is not initialized");
|
|
839
|
-
|
|
1199
|
+
}
|
|
1200
|
+
analytics.track("agent-rate-delete", {
|
|
840
1201
|
type: "text",
|
|
841
|
-
chat_id: (
|
|
842
|
-
id
|
|
843
|
-
mode:
|
|
844
|
-
})
|
|
1202
|
+
chat_id: (_a2 = items.chat) == null ? void 0 : _a2.id,
|
|
1203
|
+
id,
|
|
1204
|
+
mode: items.chatMode
|
|
1205
|
+
});
|
|
1206
|
+
return agentsApi.deleteRating(agentInstance.id, items.chat.id, id);
|
|
845
1207
|
},
|
|
846
|
-
speak(
|
|
847
|
-
if (!
|
|
1208
|
+
speak(payload) {
|
|
1209
|
+
if (!items.streamingManager) {
|
|
848
1210
|
throw new Error("Please connect to the agent first");
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
|
|
1211
|
+
}
|
|
1212
|
+
function getScript() {
|
|
1213
|
+
if (typeof payload === "string") {
|
|
1214
|
+
if (!agentInstance.presenter.voice) {
|
|
852
1215
|
throw new Error("Presenter voice is not initialized");
|
|
1216
|
+
}
|
|
853
1217
|
return {
|
|
854
1218
|
type: "text",
|
|
855
|
-
provider:
|
|
856
|
-
input:
|
|
857
|
-
ssml:
|
|
1219
|
+
provider: agentInstance.presenter.voice,
|
|
1220
|
+
input: payload,
|
|
1221
|
+
ssml: false
|
|
858
1222
|
};
|
|
859
1223
|
}
|
|
860
|
-
if (
|
|
861
|
-
if (!
|
|
1224
|
+
if (payload.type === "text" && !payload.provider) {
|
|
1225
|
+
if (!agentInstance.presenter.voice) {
|
|
862
1226
|
throw new Error("Presenter voice is not initialized");
|
|
1227
|
+
}
|
|
863
1228
|
return {
|
|
864
1229
|
type: "text",
|
|
865
|
-
provider:
|
|
866
|
-
input:
|
|
867
|
-
ssml:
|
|
1230
|
+
provider: agentInstance.presenter.voice,
|
|
1231
|
+
input: payload.input,
|
|
1232
|
+
ssml: payload.ssml
|
|
868
1233
|
};
|
|
869
1234
|
}
|
|
870
|
-
return
|
|
1235
|
+
return payload;
|
|
871
1236
|
}
|
|
872
|
-
const
|
|
873
|
-
|
|
874
|
-
|
|
1237
|
+
const script = getScript();
|
|
1238
|
+
analytics.track("agent-speak", script);
|
|
1239
|
+
return items.streamingManager.speak({
|
|
1240
|
+
script
|
|
875
1241
|
});
|
|
876
1242
|
}
|
|
877
1243
|
};
|
|
878
1244
|
}
|
|
879
|
-
function
|
|
1245
|
+
function getAgent(agentId, auth, baseURL) {
|
|
880
1246
|
const {
|
|
881
|
-
getById
|
|
882
|
-
} =
|
|
883
|
-
return
|
|
1247
|
+
getById
|
|
1248
|
+
} = createAgentsApi(auth, baseURL || didApiUrl);
|
|
1249
|
+
return getById(agentId);
|
|
884
1250
|
}
|
|
885
1251
|
export {
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
|
|
1252
|
+
AgentStatus,
|
|
1253
|
+
ChatMode,
|
|
1254
|
+
ChatProgress,
|
|
1255
|
+
ConnectionState,
|
|
1256
|
+
DocumentType,
|
|
1257
|
+
KnowledgeType,
|
|
1258
|
+
PlanGroup,
|
|
1259
|
+
Providers,
|
|
1260
|
+
RateState,
|
|
1261
|
+
StreamEvents,
|
|
1262
|
+
StreamingState,
|
|
1263
|
+
Subject,
|
|
1264
|
+
UserPlan,
|
|
1265
|
+
VideoType,
|
|
1266
|
+
VoiceAccess,
|
|
1267
|
+
createAgentManager,
|
|
1268
|
+
getAgent,
|
|
1269
|
+
mapVideoType
|
|
904
1270
|
};
|