@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 CHANGED
@@ -1,422 +1,633 @@
1
- var re = /* @__PURE__ */ ((e) => (e.TRIAL = "trial", e.BASIC = "basic", e.ENTERPRISE = "enterprise", e.LITE = "lite", e.ADVANCED = "advanced", e))(re || {}), ae = /* @__PURE__ */ ((e) => (e.TRIAL = "deid-trial", e.PRO = "deid-pro", e.ENTERPRISE = "deid-enterprise", e.LITE = "deid-lite", e.ADVANCED = "deid-advanced", e.BUILD = "deid-api-build", e.LAUNCH = "deid-api-launch", e.SCALE = "deid-api-scale", e))(ae || {}), ie = /* @__PURE__ */ ((e) => (e.Created = "created", e.Started = "started", e.Done = "done", e.Error = "error", e.Rejected = "rejected", e.Ready = "ready", e))(ie || {}), se = /* @__PURE__ */ ((e) => (e.Unrated = "Unrated", e.Positive = "Positive", e.Negative = "Negative", e))(se || {}), E = /* @__PURE__ */ ((e) => (e.Functional = "Functional", e.TextOnly = "TextOnly", e.Maintenance = "Maintenance", e.Playground = "Playground", e.DirectPlayback = "DirectPlayback", e))(E || {}), L = /* @__PURE__ */ ((e) => (e.Embed = "embed", e.Query = "query", e.Partial = "partial", e.Answer = "answer", e.Complete = "done", e))(L || {}), oe = /* @__PURE__ */ ((e) => (e.KnowledgeProcessing = "knowledge/processing", e.KnowledgeIndexing = "knowledge/indexing", e.KnowledgeFailed = "knowledge/error", e.KnowledgeDone = "knowledge/done", e))(oe || {}), ce = /* @__PURE__ */ ((e) => (e.Knowledge = "knowledge", e.Document = "document", e.Record = "record", e))(ce || {}), de = /* @__PURE__ */ ((e) => (e.Pdf = "pdf", e.Text = "text", e.Html = "html", e.Word = "word", e.Json = "json", e.Markdown = "markdown", e.Csv = "csv", e.Excel = "excel", e.Powerpoint = "powerpoint", e.Archive = "archive", e.Image = "image", e.Audio = "audio", e.Video = "video", e))(de || {}), Y = /* @__PURE__ */ ((e) => (e.Clip = "clip", e.Talk = "talk", e))(Y || {});
2
- const le = (e) => {
3
- switch (e) {
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: ${e}`);
92
+ throw new Error(`Unknown video type: ${type}`);
10
93
  }
11
94
  };
12
- var z = /* @__PURE__ */ ((e) => (e.Start = "START", e.Stop = "STOP", e))(z || {}), V = /* @__PURE__ */ ((e) => (e.ChatAnswer = "chat/answer", e.ChatPartial = "chat/partial", e.StreamDone = "stream/done", e.StreamStarted = "stream/started", e.StreamFailed = "stream/error", e.StreamReady = "stream/ready", e.StreamCreated = "stream/created", e.StreamVideoCreated = "stream-video/started", e.StreamVideoDone = "stream-video/done", e.StreamVideoError = "stream-video/error", e.StreamVideoRejected = "stream-video/rejected", e))(V || {}), M = /* @__PURE__ */ ((e) => (e.New = "new", e.Fail = "fail", e.Connected = "connected", e.Connecting = "connecting", e.Closed = "closed", e.Completed = "completed", e.Disconnected = "disconnected", e))(M || {}), ge = /* @__PURE__ */ ((e) => (e.Amazon = "amazon", e.Microsoft = "microsoft", e.Afflorithmics = "afflorithmics", e.Elevenlabs = "elevenlabs", e))(ge || {}), me = /* @__PURE__ */ ((e) => (e.Public = "public", e.Premium = "premium", e.Private = "private", e))(me || {});
13
- const j = "https://api.d-id.com", ue = "wss://notifications.d-id.com", he = "79f81a83a67430be2bc0fd61042b8faa", U = () => Math.random().toString(16).slice(2);
14
- function Z() {
15
- let e = window.localStorage.getItem("did_external_key_id");
16
- return e || (e = Math.random().toString(16).slice(2), window.localStorage.setItem("did_external_key_id", e)), e;
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 we = U();
19
- function G(e) {
20
- if (e.type === "bearer")
21
- return `Bearer ${e.token}`;
22
- if (e.type === "basic")
23
- return `Basic ${btoa(`${e.username}:${e.password}`)}`;
24
- if (e.type === "key")
25
- return `Client-Key ${e.clientKey}.${Z()}_${we}`;
26
- throw new Error(`Unknown auth type: ${e}`);
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 H(e, n = j, s) {
29
- const a = async (t, r) => {
30
- const i = await fetch(n + (t != null && t.startsWith("/") ? t : `/${t}`), {
31
- ...r,
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
- ...r == null ? void 0 : r.headers,
34
- Authorization: G(e),
166
+ ...options == null ? void 0 : options.headers,
167
+ Authorization: getAuthHeader(auth),
35
168
  "Content-Type": "application/json"
36
169
  }
37
170
  });
38
- if (!i.ok) {
39
- let o = await i.text().catch(() => "Failed to fetch");
40
- throw s && s(new Error(o), {
41
- url: t,
42
- options: r,
43
- headers: i.headers
44
- }), new Error(o);
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 i.json();
182
+ return request.json();
47
183
  };
48
184
  return {
49
- get(t, r) {
50
- return a(t, {
51
- ...r,
185
+ get(url, options) {
186
+ return client(url, {
187
+ ...options,
52
188
  method: "GET"
53
189
  });
54
190
  },
55
- post(t, r, i) {
56
- return a(t, {
57
- ...i,
58
- body: JSON.stringify(r),
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(t, r, i) {
63
- return a(t, {
64
- ...i,
65
- body: JSON.stringify(r),
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(t, r, i) {
70
- return a(t, {
71
- ...i,
72
- body: JSON.stringify(r),
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 Q(e, n = j, s) {
79
- const a = H(e, `${n}/agents`, s);
214
+ function createAgentsApi(auth, host = didApiUrl, onError) {
215
+ const client = createClient(auth, `${host}/agents`, onError);
80
216
  return {
81
- create(t, r) {
82
- return a.post("/", t, r);
217
+ create(payload, options) {
218
+ return client.post(`/`, payload, options);
83
219
  },
84
- getAgents(t, r) {
85
- return a.get(`/${t ? `?tag=${t}` : ""}`, r).then((i) => i ?? []);
220
+ getAgents(tag, options) {
221
+ return client.get(`/${tag ? `?tag=${tag}` : ""}`, options).then((agents) => agents ?? []);
86
222
  },
87
- getById(t, r) {
88
- return a.get(`/${t}`, r);
223
+ getById(id, options) {
224
+ return client.get(`/${id}`, options);
89
225
  },
90
- delete(t, r) {
91
- return a.delete(`/${t}`, void 0, r);
226
+ delete(id, options) {
227
+ return client.delete(`/${id}`, void 0, options);
92
228
  },
93
- update(t, r, i) {
94
- return a.patch(`/${t}`, r, i);
229
+ update(id, payload, options) {
230
+ return client.patch(`/${id}`, payload, options);
95
231
  },
96
- newChat(t, r, i) {
97
- return a.post(`/${t}/chat`, r, i);
232
+ newChat(agentId, payload, options) {
233
+ return client.post(`/${agentId}/chat`, payload, options);
98
234
  },
99
- chat(t, r, i, o) {
100
- return a.post(`/${t}/chat/${r}`, i, o);
235
+ chat(agentId, chatId, payload, options) {
236
+ return client.post(`/${agentId}/chat/${chatId}`, payload, options);
101
237
  },
102
- createRating(t, r, i, o) {
103
- return a.post(`/${t}/chat/${r}/ratings`, i, o);
238
+ createRating(agentId, chatId, payload, options) {
239
+ return client.post(`/${agentId}/chat/${chatId}/ratings`, payload, options);
104
240
  },
105
- updateRating(t, r, i, o, d) {
106
- return a.patch(`/${t}/chat/${r}/ratings/${i}`, o, d);
241
+ updateRating(agentId, chatId, ratingId, payload, options) {
242
+ return client.patch(`/${agentId}/chat/${chatId}/ratings/${ratingId}`, payload, options);
107
243
  },
108
- deleteRating(t, r, i, o) {
109
- return a.delete(`/${t}/chat/${r}/ratings/${i}`, o);
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 fe = (e) => new Promise((n) => setTimeout(n, e));
114
- function pe(e) {
115
- return new Promise((n, s) => {
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
- callbacks: a,
118
- host: t,
119
- auth: r
120
- } = e, {
121
- onMessage: i = null,
122
- onOpen: o = null,
123
- onClose: d = null,
124
- onError: g = null
125
- } = a || {}, h = new WebSocket(`${t}?authorization=${G(r)}`);
126
- h.onmessage = i, h.onclose = d, h.onerror = (m) => {
127
- console.error(m), g == null || g("Websocket failed to connect", m), s(m);
128
- }, h.onopen = (m) => {
129
- o == null || o(m), n(h);
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 ye(e) {
280
+ async function connectWithRetries(options) {
134
281
  const {
135
- retries: n = 1
136
- } = e;
137
- let s = null;
138
- for (let a = 0; (s == null ? void 0 : s.readyState) !== WebSocket.OPEN; a++)
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
- s = await pe(e);
141
- } catch (t) {
142
- if (a === n)
143
- throw t;
144
- await fe(a * 500);
287
+ socket = await connect(options);
288
+ } catch (e) {
289
+ if (attempt === retries) {
290
+ throw e;
291
+ }
292
+ await sleep(attempt * 500);
145
293
  }
146
- return s;
294
+ }
295
+ return socket;
147
296
  }
148
- async function ve(e, n, s) {
149
- const a = s != null && s.onMessage ? [s.onMessage] : [], t = await ye({
150
- auth: e,
151
- host: n,
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: s == null ? void 0 : s.onError,
154
- onMessage: (r) => {
155
- const i = JSON.parse(r.data);
156
- a.forEach((o) => o(i.event, i));
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: t,
162
- disconnect: () => t.close(),
163
- subscribeToEvents: (r) => a.push(r)
311
+ socket,
312
+ disconnect: () => socket.close(),
313
+ subscribeToEvents: (callback2) => messageCallbacks.push(callback2)
164
314
  };
165
315
  }
166
- const Ce = "X-Playground-Chat";
167
- function _e(e, n, s, a) {
168
- const t = H(e, `${n}/agents/${s}`, a);
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(r) {
171
- return t.post("/streams", {
172
- output_resolution: r.output_resolution,
173
- compatibility_mode: r.compatibility_mode,
174
- stream_warmup: r.stream_warmup,
175
- session_timeout: r.session_timeout,
176
- stream_greeting: r.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(r, i, o) {
180
- return t.post(`/streams/${r}/sdp`, {
181
- session_id: o,
182
- answer: i
329
+ startConnection(streamId, answer, sessionId) {
330
+ return client.post(`/streams/${streamId}/sdp`, {
331
+ session_id: sessionId,
332
+ answer
183
333
  });
184
334
  },
185
- addIceCandidate(r, i, o) {
186
- return t.post(`/streams/${r}/ice`, {
187
- session_id: o,
188
- ...i
335
+ addIceCandidate(streamId, candidate, sessionId) {
336
+ return client.post(`/streams/${streamId}/ice`, {
337
+ session_id: sessionId,
338
+ ...candidate
189
339
  });
190
340
  },
191
- sendStreamRequest(r, i, o) {
192
- return t.post(`/streams/${r}`, {
193
- session_id: i,
194
- ...o
341
+ sendStreamRequest(streamId, sessionId, payload) {
342
+ return client.post(`/streams/${streamId}`, {
343
+ session_id: sessionId,
344
+ ...payload
195
345
  });
196
346
  },
197
- close(r, i) {
198
- return t.delete(`/streams/${r}`, {
199
- session_id: i
347
+ close(streamId, sessionId) {
348
+ return client.delete(`/streams/${streamId}`, {
349
+ session_id: sessionId
200
350
  });
201
351
  }
202
352
  };
203
353
  }
204
- function Me(e, n, s, a) {
205
- const t = H(e, `${n}/agents/${s}`, a);
354
+ function createApi(auth, host, agentId, onError) {
355
+ const client = createClient(auth, `${host}/agents/${agentId}`, onError);
206
356
  return {
207
- createStream(r, i) {
208
- return t.post("/streams", {
209
- driver_url: r.driver_url,
210
- face: r.face,
211
- config: r.config,
212
- output_resolution: r.output_resolution,
213
- compatibility_mode: r.compatibility_mode,
214
- stream_warmup: r.stream_warmup,
215
- session_timeout: r.session_timeout,
216
- stream_greeting: r.stream_greeting
217
- }, i);
218
- },
219
- startConnection(r, i, o, d) {
220
- return t.post(`/streams/${r}/sdp`, {
221
- session_id: o,
222
- answer: i
223
- }, d);
224
- },
225
- addIceCandidate(r, i, o, d) {
226
- return t.post(`/streams/${r}/ice`, {
227
- session_id: o,
228
- ...i
229
- }, d);
230
- },
231
- sendStreamRequest(r, i, o, d) {
232
- return t.post(`/streams/${r}`, {
233
- session_id: i,
234
- ...o
235
- }, d);
236
- },
237
- close(r, i, o) {
238
- return t.delete(`/streams/${r}`, {
239
- session_id: i
240
- }, o);
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 ee = !1;
245
- const O = (e, n) => ee && console.log(e, n), Se = (window.RTCPeerConnection || window.webkitRTCPeerConnection || window.mozRTCPeerConnection).bind(window);
246
- function J(e) {
247
- switch (e) {
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 M.Connected;
400
+ return ConnectionState.Connected;
250
401
  case "checking":
251
- return M.Connecting;
402
+ return ConnectionState.Connecting;
252
403
  case "failed":
253
- return M.Fail;
404
+ return ConnectionState.Fail;
254
405
  case "new":
255
- return M.New;
406
+ return ConnectionState.New;
256
407
  case "closed":
257
- return M.Closed;
408
+ return ConnectionState.Closed;
258
409
  case "disconnected":
259
- return M.Disconnected;
410
+ return ConnectionState.Disconnected;
260
411
  case "completed":
261
- return M.Completed;
412
+ return ConnectionState.Completed;
262
413
  default:
263
- return M.New;
414
+ return ConnectionState.New;
264
415
  }
265
416
  }
266
- function ke() {
267
- let e = 0;
268
- return (n) => {
269
- for (const s of n.values())
270
- if (s && s.type === "inbound-rtp" && s.kind === "video") {
271
- const a = s.bytesReceived, t = a - e > 0;
272
- return e = a, t;
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
- return !1;
427
+ }
428
+ return false;
275
429
  };
276
430
  }
277
- function Re(e, n, s = !1, a, t) {
278
- const i = Math.max(Math.ceil(10), 1);
279
- let o = 0, d = !1;
280
- const g = s ? 1 : 0;
281
- let h = 0;
282
- const m = ke();
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 C = await e.getStats();
285
- m(C) ? (o = 0, d || (n == null || n(z.Start), h >= g && !a() && t(), h++, d = !0)) : d && (o++, o >= i && (n == null || n(z.Stop), d = !1));
286
- }, 100);
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 Ee(e, n, {
289
- debug: s = !1,
290
- callbacks: a,
291
- auth: t,
292
- baseURL: r = j,
293
- warmup: i
461
+ async function createStreamingManager(agentId, agent, {
462
+ debug = false,
463
+ callbacks,
464
+ auth,
465
+ baseURL = didApiUrl,
466
+ warmup
294
467
  }) {
295
- ee = s;
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
- startConnection: o,
298
- sendStreamRequest: d,
299
- close: g,
300
- createStream: h,
301
- addIceCandidate: m
302
- } = n.videoType === Y.Clip ? _e(t, r, e, a.onError) : Me(t, r, e, a.onError), {
303
- id: C,
304
- offer: I,
305
- ice_servers: y,
306
- session_id: _
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
- let $ = !1;
313
- const c = () => $, u = () => {
314
- var l;
315
- $ = !0, (l = a.onConnectionStateChange) == null || l.call(a, M.Connected);
316
- }, f = Re(w, a.onVideoStateChange, i, c, u);
317
- w.onicecandidate = (l) => {
318
- var p;
319
- O("peerConnection.onicecandidate", l);
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
- l.candidate && l.candidate.sdpMid && l.candidate.sdpMLineIndex !== null ? m(C, {
322
- candidate: l.candidate.candidate,
323
- sdpMid: l.candidate.sdpMid,
324
- sdpMLineIndex: l.candidate.sdpMLineIndex
325
- }, _) : m(C, {
326
- candidate: null
327
- }, _);
328
- } catch (k) {
329
- (p = a.onError) == null || p.call(a, k, {
330
- streamId: C
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
- }, b.onmessage = (l) => {
334
- if (b.readyState === "open") {
335
- const [p, k] = l.data.split(":");
336
- p === V.StreamReady && !$ && u();
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
- }, w.oniceconnectionstatechange = () => {
339
- var p;
340
- O("peerConnection.oniceconnectionstatechange => " + w.iceConnectionState);
341
- const l = J(w.iceConnectionState);
342
- l !== M.Connected && ((p = a.onConnectionStateChange) == null || p.call(a, l));
343
- }, w.ontrack = (l) => {
344
- var p;
345
- O("peerConnection.ontrack", l), (p = a.onSrcObjectReady) == null || p.call(a, l.streams[0]);
346
- }, await w.setRemoteDescription(I), O("set remote description OK");
347
- const S = await w.createAnswer();
348
- return O("create answer OK"), await w.setLocalDescription(S), O("set local description OK"), await o(C, S, _), O("start connection OK"), {
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(l) {
354
- return d(C, _, l);
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 l, p;
361
- if (C) {
362
- const k = J(w.iceConnectionState);
363
- if (w) {
364
- if (k === M.New) {
365
- (l = a.onVideoStateChange) == null || l.call(a, z.Stop), clearInterval(f);
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
- w.close(), w.oniceconnectionstatechange = null, w.onnegotiationneeded = null, w.onicecandidate = null, w.ontrack = null;
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
- k === M.Connected && await g(C, _).catch((v) => {
372
- });
373
- } catch (v) {
374
- O("Error on close stream connection", v);
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
- (p = a.onVideoStateChange) == null || p.call(a, z.Stop), clearInterval(f);
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: C
593
+ streamId: streamIdFromServer
387
594
  };
388
595
  }
389
- let B = {};
390
- function Ie(e) {
391
- const n = window != null && window.hasOwnProperty("DID_AGENTS_API") ? "agents-ui" : "agents-sdk", s = e.agent.presenter, a = {
392
- token: e.token || "testKey",
393
- distinct_id: e.distinctId || Z(),
394
- agentId: e.agent.id,
395
- agentType: s.type === "clip" && s.presenter_id.startsWith("v2_") ? "clip_v2" : s.type,
396
- owner_id: e.agent.owner_id ?? ""
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
- ...a,
400
- isEnabled: e.isEnabled ?? !0,
608
+ ...analyticProps,
609
+ isEnabled: config.isEnabled ?? true,
401
610
  getRandom: () => Math.random().toString(16).slice(2),
402
- track(t, r) {
403
- if (!this.isEnabled)
404
- return Promise.reject("MixPanel analytics is disabled on creation");
611
+ track(event, props) {
612
+ if (!this.isEnabled) {
613
+ return Promise.resolve();
614
+ }
405
615
  const {
406
- audioPath: i,
407
- ...o
408
- } = r || {}, d = {
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: t,
626
+ event,
416
627
  properties: {
417
- ...o,
418
- ...a,
419
- source: n,
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", d).then((g) => g.json()).catch((g) => console.error(g));
431
- },
432
- linkTrack(t, r, i, o) {
433
- B[t] || (B[t] = {
434
- events: {},
435
- resolvedDependencies: []
436
- }), o.includes(i) || o.push(i);
437
- const d = B[t];
438
- if (d.events[i] = {
439
- props: r
440
- }, d.resolvedDependencies.push(i), o.every((h) => d.resolvedDependencies.includes(h))) {
441
- const h = o.reduce((m, C) => d.events[C] ? {
442
- ...m,
443
- ...d.events[C].props
444
- } : m, {});
445
- this.track(t, h), d.resolvedDependencies = d.resolvedDependencies.filter((m) => !o.includes(m)), o.forEach((m) => {
446
- delete d.events[m];
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 $e(e) {
453
- var t, r, i, o;
454
- const n = () => /Mobi|Android/i.test(navigator.userAgent) ? "Mobile" : "Desktop", s = () => {
455
- const d = navigator.platform;
456
- return d.toLowerCase().includes("win") ? "Windows" : d.toLowerCase().includes("mac") ? "Mac OS X" : d.toLowerCase().includes("linux") ? "Linux" : "Unknown";
457
- }, a = e.presenter;
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: `${s()}`,
460
- isMobile: `${n() == "Mobile"}`,
697
+ $os: `${getUserOS()}`,
698
+ isMobile: `${mobileOrDesktop() == "Mobile"}`,
461
699
  browser: navigator.userAgent,
462
700
  origin: window.location.origin,
463
- agentType: a.type === "clip" && a.presenter_id.startsWith("v2_") ? "clip_v2" : a.type,
701
+ agentType: presenter.type === "clip" && presenter.presenter_id.startsWith("v2_") ? "clip_v2" : presenter.type,
464
702
  agentVoice: {
465
- voiceId: (r = (t = e.presenter) == null ? void 0 : t.voice) == null ? void 0 : r.voice_id,
466
- provider: (o = (i = e.presenter) == null ? void 0 : i.voice) == null ? void 0 : o.type
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 Ae(e, n, s) {
471
- var g, h;
708
+ function getStreamAnalyticsProps(data, agent, additionalProps) {
709
+ var _a, _b;
472
710
  const {
473
- event: a,
474
- ...t
475
- } = e, {
476
- template: r
477
- } = (n == null ? void 0 : n.llm) || {}, {
478
- language: i
479
- } = ((g = n == null ? void 0 : n.presenter) == null ? void 0 : g.voice) || {}, {
480
- stitch: o
481
- } = (n == null ? void 0 : n.presenter) || {};
482
- return {
483
- ...t,
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
- ...t.llm,
486
- template: r
726
+ ...baseProps.llm,
727
+ template
487
728
  },
488
729
  script: {
489
- ...t.script,
730
+ ...baseProps.script,
490
731
  provider: {
491
- ...(h = t == null ? void 0 : t.script) == null ? void 0 : h.provider,
492
- language: i
732
+ ...(_b = baseProps == null ? void 0 : baseProps.script) == null ? void 0 : _b.provider,
733
+ language
493
734
  }
494
735
  },
495
- stitch: o,
496
- ...s
736
+ stitch,
737
+ ...additionalProps
497
738
  };
739
+ return props;
498
740
  }
499
- let N = 0;
500
- const Pe = 20 * 1e3;
501
- function De(e, n, s, a) {
502
- var t, r, i, o, d;
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: le(e.presenter.type),
505
- output_resolution: (t = s == null ? void 0 : s.streamOptions) == null ? void 0 : t.outputResolution,
506
- session_timeout: (r = s == null ? void 0 : s.streamOptions) == null ? void 0 : r.sessionTimeout,
507
- stream_warmup: (i = s == null ? void 0 : s.streamOptions) == null ? void 0 : i.streamWarmup,
508
- compatibility_mode: (o = s == null ? void 0 : s.streamOptions) == null ? void 0 : o.compatibilityMode,
509
- stream_greeting: (d = s == null ? void 0 : s.streamOptions) != null && d.streamGreeting && !n ? a : void 0
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 te(e) {
513
- return e === E.Playground ? {
754
+ function getRequestHeaders(chatMode) {
755
+ return chatMode === ChatMode.Playground ? {
514
756
  headers: {
515
- [Ce]: "true"
757
+ [PLAYGROUND_HEADER]: "true"
516
758
  }
517
759
  } : {};
518
760
  }
519
- async function ne(e, n, s, a, t) {
761
+ async function newChat(agentId, agentsApi, analytics, chatMode, persist) {
520
762
  try {
521
- const r = await n.newChat(e, {
522
- persist: t ?? !1
523
- }, te(a));
524
- return s.track("agent-chat", {
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: r.id,
527
- agent_id: e,
528
- mode: a
529
- }), r;
530
- } catch (r) {
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(r);
533
- const i = JSON.parse(r.message);
534
- if ((i == null ? void 0 : i.kind) === "InsufficientCreditsError")
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
- } catch (i) {
537
- console.error("Error parsing the error message:", i);
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 be(e, n, s, a, t, r) {
543
- return new Promise(async (i, o) => {
544
- var I;
545
- N = 0;
546
- const d = setTimeout(() => {
547
- h(new Error("Could not connect"));
548
- }, Pe), g = (y) => {
549
- clearTimeout(d), i(y);
550
- }, h = (y) => {
551
- clearTimeout(d), o(y);
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 m;
554
- !t && n.mode !== E.DirectPlayback && (m = ne(e.id, s, a, n.mode, n.persistentChat).catch((y) => {
555
- h(y);
556
- }));
557
- const C = await Ee(e.id, De(e, t, n, r), {
558
- ...n,
559
- analytics: a,
560
- warmup: (I = n.streamOptions) == null ? void 0 : I.streamWarmup,
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
- ...n.callbacks,
563
- onConnectionStateChange: async (y) => {
564
- var _, w, b, $;
565
- y === M.Connected ? (m && (t = await m), C ? ((w = (_ = n.callbacks).onConnectionStateChange) == null || w.call(_, y), g({
566
- chat: t,
567
- streamingManager: C
568
- })) : t && h(new Error("Something went wrong while initializing the manager"))) : ($ = (b = n.callbacks).onConnectionStateChange) == null || $.call(b, y);
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(y) {
571
- var _, w;
572
- (w = (_ = n.callbacks).onVideoStateChange) == null || w.call(_, y), N > 0 && y === z.Start && a.linkTrack("agent-video", {
573
- event: "start",
574
- latency: Date.now() - N
575
- }, "start", [V.StreamVideoCreated]);
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(h);
843
+ }).catch(reject);
579
844
  });
580
845
  }
581
- function xe(e) {
582
- var s;
583
- const n = (s = e.greetings) == null ? void 0 : s.filter((a) => a.length > 0);
584
- if (n && n.length > 0) {
585
- const a = Math.floor(Math.random() * n.length), t = n[a];
586
- if (t && t.length > 0)
587
- return t;
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 X(e, n) {
592
- return n && n.length > 0 ? n : [{
593
- content: e,
594
- id: U(),
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 Te(e) {
600
- if (e.answer !== void 0)
601
- return e.answer;
602
- let n = 0, s = "";
603
- for (; n in e; )
604
- s += e[n], n++;
605
- return s;
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 Oe(e, n, s, a, t) {
608
- if (!(e === L.Partial || e === L.Answer))
879
+ function processChatEvent(event, data, chatEventQueue, items, onNewMessage) {
880
+ if (!(event === ChatProgress.Partial || event === ChatProgress.Answer)) {
609
881
  return;
610
- const r = a.messages[a.messages.length - 1];
611
- if ((r == null ? void 0 : r.role) !== "assistant")
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: i,
615
- sequence: o
616
- } = n;
617
- e === L.Partial ? s[o] = i : s.answer = i;
618
- const d = Te(s);
619
- (r.content !== d || e === L.Answer) && (r.content = d, t == null || t([...a.messages], e));
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 Ne(e, n) {
622
- var w, b, $;
623
- let s = {}, a = !0;
624
- const t = {
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: n.mode || E.Functional
627
- }, r = n.baseURL || j, i = n.wsURL || ue, o = n.mixpanelKey || he, d = Q(n.auth, r, n.callbacks.onError), g = await d.getById(e), h = xe(g);
628
- t.messages = X(h, n.initialMessages), (b = (w = n.callbacks).onNewMessage) == null || b.call(w, [...t.messages], "answer");
629
- const m = Ie({
630
- token: o,
631
- agent: g,
632
- ...n
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
- m.track("agent-sdk", {
924
+ analytics.track("agent-sdk", {
635
925
  event: "loaded",
636
- ...$e(g)
926
+ ...getAnaliticsInfo(agentInstance)
637
927
  });
638
- const C = {
639
- onMessage: (c, u) => {
640
- var f, S;
641
- if ("content" in u)
642
- Oe(c, u, s, t, n.callbacks.onNewMessage), c === L.Answer && m.track("agent-message-received", {
643
- messages: t.messages.length,
644
- mode: t.chatMode
645
- });
646
- else {
647
- const l = V, p = [l.StreamVideoDone, l.StreamVideoError, l.StreamVideoRejected], k = [l.StreamFailed, l.StreamVideoError, l.StreamVideoRejected], v = Ae(u, g, {
648
- mode: t.chatMode
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
- if (c = c, c === l.StreamVideoCreated)
651
- m.linkTrack("agent-video", v, l.StreamVideoCreated, ["start"]);
652
- else if (p.includes(c)) {
653
- const R = c.split("/")[1];
654
- m.track("agent-video", {
655
- ...v,
656
- event: R
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
- k.includes(c) && ((S = (f = n.callbacks).onError) == null || S.call(f, new Error(`Stream failed with event ${c}`), {
660
- data: u
661
- })), u.event === l.StreamDone && y();
961
+ if (data.event === SEvent.StreamDone) {
962
+ disconnect();
963
+ }
662
964
  }
663
965
  }
664
966
  };
665
- async function I(c) {
666
- var R, A, K, x, P, T, W;
667
- (A = (R = n.callbacks).onConnectionStateChange) == null || A.call(R, M.Connecting), N = 0;
668
- const u = 3;
669
- c && !a && (delete t.chat, t.messages = X(h), (x = (K = n.callbacks).onNewMessage) == null || x.call(K, [...t.messages], "answer"));
670
- const f = n.mode === E.DirectPlayback ? Promise.resolve(void 0) : ve(n.auth, i, C);
671
- async function S() {
672
- for (let F = 1; F <= u; F++)
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
- return await be(g, n, d, m, t.chat, c ? h : void 0);
675
- } catch (D) {
676
- if ((D == null ? void 0 : D.message) !== "Could not connect")
677
- throw D;
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 l = S().catch((F) => {
682
- var D, q;
683
- throw _(E.Maintenance), (q = (D = n.callbacks).onConnectionStateChange) == null || q.call(D, M.Fail), F;
684
- }), [p, {
685
- streamingManager: k,
686
- chat: v
687
- }] = await Promise.all([f, l]);
688
- v && v.id !== ((P = t.chat) == null ? void 0 : P.id) && ((W = (T = n.callbacks).onNewChat) == null || W.call(T, v.id)), t.streamingManager = k, t.socketManager = p, t.chat = v, a = !1, _((v == null ? void 0 : v.chat_mode) ?? n.mode ?? E.Functional);
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 y() {
691
- var c, u, f, S;
692
- (c = t.socketManager) == null || c.disconnect(), await ((u = t.streamingManager) == null ? void 0 : u.disconnect()), delete t.streamingManager, delete t.socketManager, (S = (f = n.callbacks).onConnectionStateChange) == null || S.call(f, M.Disconnected);
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 _(c) {
695
- var u, f;
696
- c !== t.chatMode && (m.track("agent-mode-change", {
697
- mode: c
698
- }), t.chatMode = c, t.chatMode !== E.Functional && await y(), (f = (u = n.callbacks).onModeChange) == null || f.call(u, c));
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: g,
702
- starterMessages: (($ = g.knowledge) == null ? void 0 : $.starter_message) || [],
703
- changeMode: _,
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 c;
706
- await I(!0), m.track("agent-chat", {
1045
+ var _a2;
1046
+ await connect2(true);
1047
+ analytics.track("agent-chat", {
707
1048
  event: "connect",
708
- chatId: (c = t.chat) == null ? void 0 : c.id,
709
- agentId: g.id,
710
- mode: t.chatMode
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 c;
715
- await y(), await I(!1), m.track("agent-chat", {
1055
+ var _a2;
1056
+ await disconnect();
1057
+ await connect2(false);
1058
+ analytics.track("agent-chat", {
716
1059
  event: "reconnect",
717
- chatId: (c = t.chat) == null ? void 0 : c.id,
718
- agentId: g.id,
719
- mode: t.chatMode
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 c;
724
- await y(), m.track("agent-chat", {
1066
+ var _a2;
1067
+ await disconnect();
1068
+ analytics.track("agent-chat", {
725
1069
  event: "disconnect",
726
- chatId: (c = t.chat) == null ? void 0 : c.id,
727
- agentId: g.id,
728
- mode: t.chatMode
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(c) {
732
- var f, S, l, p, k, v;
733
- const u = U();
734
- s = {};
1075
+ async chat(userMessage) {
1076
+ var _a2, _b2, _c2, _d, _e, _f;
1077
+ const id = getRandom();
1078
+ chatEventQueue = {};
735
1079
  try {
736
- if (N = Date.now(), n.mode === E.DirectPlayback)
1080
+ messageSentTimestamp = Date.now();
1081
+ if (options.mode === ChatMode.DirectPlayback) {
737
1082
  throw new Error("Direct playback is enabled, chat is disabled");
738
- if (c.length >= 800)
1083
+ } else if (userMessage.length >= 800) {
739
1084
  throw new Error("Message cannot be more than 800 characters");
740
- if (c.length === 0)
1085
+ } else if (userMessage.length === 0) {
741
1086
  throw new Error("Message cannot be empty");
742
- if (t.chatMode === E.Maintenance)
1087
+ } else if (items.chatMode === ChatMode.Maintenance) {
743
1088
  throw new Error("Chat is in maintenance mode");
744
- if (![E.TextOnly, E.Playground].includes(t.chatMode))
745
- if (t.streamingManager) {
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
- t.messages.push({
751
- id: U(),
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: c,
754
- created_at: new Date(N).toISOString()
755
- }), (S = (f = n.callbacks).onNewMessage) == null || S.call(f, [...t.messages], "user"), t.chat || (t.chat = await ne(g.id, d, m, t.chatMode, n.persistentChat), (p = (l = n.callbacks).onNewChat) == null || p.call(l, t.chat.id));
756
- const R = {
757
- id: u,
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
- }, A = [...t.messages];
763
- t.messages.push(R);
764
- const K = (P) => {
765
- var T, W;
766
- return d.chat(g.id, P, {
767
- sessionId: (T = t.streamingManager) == null ? void 0 : T.sessionId,
768
- streamId: (W = t.streamingManager) == null ? void 0 : W.streamId,
769
- chatMode: t.chatMode,
770
- messages: A.map(({
771
- matches: F,
772
- ...D
773
- }) => D)
774
- }, te(t.chatMode));
775
- }, x = await K(t.chat.id).catch(async (P) => {
776
- var T;
777
- if (!((T = P == null ? void 0 : P.message) != null && T.includes("missing or invalid session_id")))
778
- throw P;
779
- return await y(), await I(!1), K(t.chat.id);
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
- return m.track("agent-message-send", {
1137
+ analytics.track("agent-message-send", {
782
1138
  event: "success",
783
- mode: t.chatMode,
784
- messages: t.messages.length + 1
785
- }), R.context = x.context, R.matches = x.matches, x.result && (R.content = x.result, m.track("agent-message-received", {
786
- latency: Date.now() - N,
787
- mode: t.chatMode,
788
- messages: t.messages.length
789
- }), (v = (k = n.callbacks).onNewMessage) == null || v.call(k, [...t.messages], "answer")), x;
790
- } catch (R) {
791
- throw t.messages[t.messages.length - 1].id === u && t.messages.pop(), m.track("agent-message-send", {
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: t.chatMode,
794
- messages: t.messages.length
795
- }), R;
1160
+ mode: items.chatMode,
1161
+ messages: items.messages.length
1162
+ });
1163
+ throw e;
796
1164
  }
797
1165
  },
798
- rate(c, u, f) {
799
- var p, k, v, R;
800
- const S = t.messages.find((A) => A.id === c);
801
- if (t.chat) {
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
- const l = ((p = S.matches) == null ? void 0 : p.map((A) => [A.document_id, A.id])) ?? [];
807
- return m.track("agent-rate", {
808
- event: f ? "update" : "create",
809
- thumb: u === 1 ? "up" : "down",
810
- knowledge_id: ((k = g.knowledge) == null ? void 0 : k.id) ?? "",
811
- mode: t.chatMode,
812
- matches: l,
813
- score: u
814
- }), f ? d.updateRating(g.id, t.chat.id, f, {
815
- knowledge_id: ((v = g.knowledge) == null ? void 0 : v.id) ?? "",
816
- message_id: c,
817
- matches: l,
818
- score: u
819
- }) : d.createRating(g.id, t.chat.id, {
820
- knowledge_id: ((R = g.knowledge) == null ? void 0 : R.id) ?? "",
821
- message_id: c,
822
- matches: l,
823
- score: u
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(c) {
827
- var u;
828
- if (!t.chat)
1198
+ deleteRate(id) {
1199
+ var _a2;
1200
+ if (!items.chat) {
829
1201
  throw new Error("Chat is not initialized");
830
- return m.track("agent-rate-delete", {
1202
+ }
1203
+ analytics.track("agent-rate-delete", {
831
1204
  type: "text",
832
- chat_id: (u = t.chat) == null ? void 0 : u.id,
833
- id: c,
834
- mode: t.chatMode
835
- }), d.deleteRating(g.id, t.chat.id, c);
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(c) {
838
- if (!t.streamingManager)
1211
+ speak(payload) {
1212
+ if (!items.streamingManager) {
839
1213
  throw new Error("Please connect to the agent first");
840
- function u() {
841
- if (typeof c == "string") {
842
- if (!g.presenter.voice)
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: g.presenter.voice,
847
- input: c,
848
- ssml: !1
1222
+ provider: agentInstance.presenter.voice,
1223
+ input: payload,
1224
+ ssml: false
849
1225
  };
850
1226
  }
851
- if (c.type === "text" && !c.provider) {
852
- if (!g.presenter.voice)
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: g.presenter.voice,
857
- input: c.input,
858
- ssml: c.ssml
1233
+ provider: agentInstance.presenter.voice,
1234
+ input: payload.input,
1235
+ ssml: payload.ssml
859
1236
  };
860
1237
  }
861
- return c;
1238
+ return payload;
862
1239
  }
863
- const f = u();
864
- return m.track("agent-speak", f), t.streamingManager.speak({
865
- script: f
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 Le(e, n, s) {
1248
+ function getAgent(agentId, auth, baseURL) {
871
1249
  const {
872
- getById: a
873
- } = Q(n, s || j);
874
- return a(e);
1250
+ getById
1251
+ } = createAgentsApi(auth, baseURL || didApiUrl);
1252
+ return getById(agentId);
875
1253
  }
876
1254
  export {
877
- ie as AgentStatus,
878
- E as ChatMode,
879
- L as ChatProgress,
880
- M as ConnectionState,
881
- de as DocumentType,
882
- ce as KnowledgeType,
883
- ae as PlanGroup,
884
- ge as Providers,
885
- se as RateState,
886
- V as StreamEvents,
887
- z as StreamingState,
888
- oe as Subject,
889
- re as UserPlan,
890
- Y as VideoType,
891
- me as VoiceAccess,
892
- Ne as createAgentManager,
893
- Le as getAgent,
894
- le as mapVideoType
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
  };