@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 CHANGED
@@ -1,425 +1,633 @@
1
- var ie = /* @__PURE__ */ ((e) => (e.TRIAL = "trial", e.BASIC = "basic", e.ENTERPRISE = "enterprise", e.LITE = "lite", e.ADVANCED = "advanced", e))(ie || {}), se = /* @__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))(se || {}), oe = /* @__PURE__ */ ((e) => (e.Created = "created", e.Started = "started", e.Done = "done", e.Error = "error", e.Rejected = "rejected", e.Ready = "ready", e))(oe || {}), ce = /* @__PURE__ */ ((e) => (e.Unrated = "Unrated", e.Positive = "Positive", e.Negative = "Negative", e))(ce || {}), I = /* @__PURE__ */ ((e) => (e.Functional = "Functional", e.TextOnly = "TextOnly", e.Maintenance = "Maintenance", e.Playground = "Playground", e.DirectPlayback = "DirectPlayback", e))(I || {}), z = /* @__PURE__ */ ((e) => (e.Embed = "embed", e.Query = "query", e.Partial = "partial", e.Answer = "answer", e.Complete = "done", e))(z || {}), de = /* @__PURE__ */ ((e) => (e.KnowledgeProcessing = "knowledge/processing", e.KnowledgeIndexing = "knowledge/indexing", e.KnowledgeFailed = "knowledge/error", e.KnowledgeDone = "knowledge/done", e))(de || {}), le = /* @__PURE__ */ ((e) => (e.Knowledge = "knowledge", e.Document = "document", e.Record = "record", e))(le || {}), ge = /* @__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))(ge || {}), G = /* @__PURE__ */ ((e) => (e.Clip = "clip", e.Talk = "talk", e))(G || {});
2
- const me = (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 K = /* @__PURE__ */ ((e) => (e.Start = "START", e.Stop = "STOP", e))(K || {}), H = /* @__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))(H || {}), S = /* @__PURE__ */ ((e) => (e.New = "new", e.Fail = "fail", e.Connected = "connected", e.Connecting = "connecting", e.Closed = "closed", e.Completed = "completed", e.Disconnected = "disconnected", e))(S || {}), ue = /* @__PURE__ */ ((e) => (e.Amazon = "amazon", e.Microsoft = "microsoft", e.Afflorithmics = "afflorithmics", e.Elevenlabs = "elevenlabs", e))(ue || {}), he = /* @__PURE__ */ ((e) => (e.Public = "public", e.Premium = "premium", e.Private = "private", e))(he || {});
13
- const j = "https://api.d-id.com", we = "wss://notifications.d-id.com", fe = "79f81a83a67430be2bc0fd61042b8faa", B = () => Math.random().toString(16).slice(2);
14
- function Q() {
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 pe = B();
19
- function ee(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}.${Q()}_${pe}`;
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 J(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: ee(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 te(e, n = j, s) {
79
- const a = J(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);
110
246
  },
111
- getSTTToken(t, r, i) {
112
- return a.get(`/${t}/chat/${r}/stt-token`, i);
247
+ getSTTToken(agentId, options) {
248
+ return client.get(`/${agentId}/stt-token`, options);
113
249
  }
114
250
  };
115
251
  }
116
- const ve = (e) => new Promise((n) => setTimeout(n, e));
117
- function ye(e) {
118
- 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) => {
119
255
  const {
120
- callbacks: a,
121
- host: t,
122
- auth: r
123
- } = e, {
124
- onMessage: i = null,
125
- onOpen: o = null,
126
- onClose: d = null,
127
- onError: g = null
128
- } = a || {}, h = new WebSocket(`${t}?authorization=${ee(r)}`);
129
- h.onmessage = i, h.onclose = d, h.onerror = (m) => {
130
- console.error(m), g == null || g("Websocket failed to connect", m), s(m);
131
- }, h.onopen = (m) => {
132
- o == null || o(m), n(h);
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 Ce(e) {
280
+ async function connectWithRetries(options) {
137
281
  const {
138
- retries: n = 1
139
- } = e;
140
- let s = null;
141
- 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++) {
142
286
  try {
143
- s = await ye(e);
144
- } catch (t) {
145
- if (a === n)
146
- throw t;
147
- await ve(a * 500);
287
+ socket = await connect(options);
288
+ } catch (e) {
289
+ if (attempt === retries) {
290
+ throw e;
291
+ }
292
+ await sleep(attempt * 500);
148
293
  }
149
- return s;
294
+ }
295
+ return socket;
150
296
  }
151
- async function _e(e, n, s) {
152
- const a = s != null && s.onMessage ? [s.onMessage] : [], t = await Ce({
153
- auth: e,
154
- 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,
155
302
  callbacks: {
156
- onError: s == null ? void 0 : s.onError,
157
- onMessage: (r) => {
158
- const i = JSON.parse(r.data);
159
- 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));
160
307
  }
161
308
  }
162
309
  });
163
310
  return {
164
- socket: t,
165
- disconnect: () => t.close(),
166
- subscribeToEvents: (r) => a.push(r)
311
+ socket,
312
+ disconnect: () => socket.close(),
313
+ subscribeToEvents: (callback2) => messageCallbacks.push(callback2)
167
314
  };
168
315
  }
169
- const Se = "X-Playground-Chat";
170
- function ke(e, n, s, a) {
171
- const t = J(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);
172
319
  return {
173
- createStream(r) {
174
- return t.post("/streams", {
175
- output_resolution: r.output_resolution,
176
- compatibility_mode: r.compatibility_mode,
177
- stream_warmup: r.stream_warmup,
178
- session_timeout: r.session_timeout,
179
- 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
180
327
  });
181
328
  },
182
- startConnection(r, i, o) {
183
- return t.post(`/streams/${r}/sdp`, {
184
- session_id: o,
185
- answer: i
329
+ startConnection(streamId, answer, sessionId) {
330
+ return client.post(`/streams/${streamId}/sdp`, {
331
+ session_id: sessionId,
332
+ answer
186
333
  });
187
334
  },
188
- addIceCandidate(r, i, o) {
189
- return t.post(`/streams/${r}/ice`, {
190
- session_id: o,
191
- ...i
335
+ addIceCandidate(streamId, candidate, sessionId) {
336
+ return client.post(`/streams/${streamId}/ice`, {
337
+ session_id: sessionId,
338
+ ...candidate
192
339
  });
193
340
  },
194
- sendStreamRequest(r, i, o) {
195
- return t.post(`/streams/${r}`, {
196
- session_id: i,
197
- ...o
341
+ sendStreamRequest(streamId, sessionId, payload) {
342
+ return client.post(`/streams/${streamId}`, {
343
+ session_id: sessionId,
344
+ ...payload
198
345
  });
199
346
  },
200
- close(r, i) {
201
- return t.delete(`/streams/${r}`, {
202
- session_id: i
347
+ close(streamId, sessionId) {
348
+ return client.delete(`/streams/${streamId}`, {
349
+ session_id: sessionId
203
350
  });
204
351
  }
205
352
  };
206
353
  }
207
- function Me(e, n, s, a) {
208
- const t = J(e, `${n}/agents/${s}`, a);
354
+ function createApi(auth, host, agentId, onError) {
355
+ const client = createClient(auth, `${host}/agents/${agentId}`, onError);
209
356
  return {
210
- createStream(r, i) {
211
- return t.post("/streams", {
212
- driver_url: r.driver_url,
213
- face: r.face,
214
- config: r.config,
215
- output_resolution: r.output_resolution,
216
- compatibility_mode: r.compatibility_mode,
217
- stream_warmup: r.stream_warmup,
218
- session_timeout: r.session_timeout,
219
- stream_greeting: r.stream_greeting
220
- }, i);
221
- },
222
- startConnection(r, i, o, d) {
223
- return t.post(`/streams/${r}/sdp`, {
224
- session_id: o,
225
- answer: i
226
- }, d);
227
- },
228
- addIceCandidate(r, i, o, d) {
229
- return t.post(`/streams/${r}/ice`, {
230
- session_id: o,
231
- ...i
232
- }, d);
233
- },
234
- sendStreamRequest(r, i, o, d) {
235
- return t.post(`/streams/${r}`, {
236
- session_id: i,
237
- ...o
238
- }, d);
239
- },
240
- close(r, i, o) {
241
- return t.delete(`/streams/${r}`, {
242
- session_id: i
243
- }, 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);
244
391
  }
245
392
  };
246
393
  }
247
- let ne = !1;
248
- const N = (e, n) => ne && console.log(e, n), Re = (window.RTCPeerConnection || window.webkitRTCPeerConnection || window.mozRTCPeerConnection).bind(window);
249
- function Y(e) {
250
- 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) {
251
399
  case "connected":
252
- return S.Connected;
400
+ return ConnectionState.Connected;
253
401
  case "checking":
254
- return S.Connecting;
402
+ return ConnectionState.Connecting;
255
403
  case "failed":
256
- return S.Fail;
404
+ return ConnectionState.Fail;
257
405
  case "new":
258
- return S.New;
406
+ return ConnectionState.New;
259
407
  case "closed":
260
- return S.Closed;
408
+ return ConnectionState.Closed;
261
409
  case "disconnected":
262
- return S.Disconnected;
410
+ return ConnectionState.Disconnected;
263
411
  case "completed":
264
- return S.Completed;
412
+ return ConnectionState.Completed;
265
413
  default:
266
- return S.New;
414
+ return ConnectionState.New;
267
415
  }
268
416
  }
269
- function Ie() {
270
- let e = 0;
271
- return (n) => {
272
- for (const s of n.values())
273
- if (s && s.type === "inbound-rtp" && s.kind === "video") {
274
- const a = s.bytesReceived, t = a - e > 0;
275
- 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;
276
426
  }
277
- return !1;
427
+ }
428
+ return false;
278
429
  };
279
430
  }
280
- function Ee(e, n, s = !1, a, t) {
281
- const i = Math.max(Math.ceil(10), 1);
282
- let o = 0, d = !1;
283
- const g = s ? 1 : 0;
284
- let h = 0;
285
- const m = Ie();
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 C = await e.getStats();
288
- m(C) ? (o = 0, d || (n == null || n(K.Start), h >= g && !a() && t(), h++, d = !0)) : d && (o++, o >= i && (n == null || n(K.Stop), d = !1));
289
- }, 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);
290
460
  }
291
- async function $e(e, n, {
292
- debug: s = !1,
293
- callbacks: a,
294
- auth: t,
295
- baseURL: r = j,
296
- warmup: i
461
+ async function createStreamingManager(agentId, agent, {
462
+ debug = false,
463
+ callbacks,
464
+ auth,
465
+ baseURL = didApiUrl,
466
+ warmup
297
467
  }) {
298
- ne = 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);
299
476
  const {
300
- startConnection: o,
301
- sendStreamRequest: d,
302
- close: g,
303
- createStream: h,
304
- addIceCandidate: m
305
- } = n.videoType === G.Clip ? ke(t, r, e, a.onError) : Me(t, r, e, a.onError), {
306
- id: C,
307
- offer: $,
308
- ice_servers: v,
309
- session_id: _
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
- let A = !1;
316
- const c = () => A, u = () => {
317
- var l;
318
- A = !0, (l = a.onConnectionStateChange) == null || l.call(a, S.Connected);
319
- }, f = Ee(w, a.onVideoStateChange, i, c, u);
320
- w.onicecandidate = (l) => {
321
- var p;
322
- N("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);
323
500
  try {
324
- l.candidate && l.candidate.sdpMid && l.candidate.sdpMLineIndex !== null ? m(C, {
325
- candidate: l.candidate.candidate,
326
- sdpMid: l.candidate.sdpMid,
327
- sdpMLineIndex: l.candidate.sdpMLineIndex
328
- }, _) : m(C, {
329
- candidate: null
330
- }, _);
331
- } catch (M) {
332
- (p = a.onError) == null || p.call(a, M, {
333
- 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
334
515
  });
335
516
  }
336
- }, D.onmessage = (l) => {
337
- if (D.readyState === "open") {
338
- const [p, M] = l.data.split(":");
339
- p === H.StreamReady && !A && 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
+ }
340
524
  }
341
- }, w.oniceconnectionstatechange = () => {
342
- var p;
343
- N("peerConnection.oniceconnectionstatechange => " + w.iceConnectionState);
344
- const l = Y(w.iceConnectionState);
345
- l !== S.Connected && ((p = a.onConnectionStateChange) == null || p.call(a, l));
346
- }, w.ontrack = (l) => {
347
- var p;
348
- N("peerConnection.ontrack", l), (p = a.onSrcObjectReady) == null || p.call(a, l.streams[0]);
349
- }, await w.setRemoteDescription($), N("set remote description OK");
350
- const k = await w.createAnswer();
351
- return N("create answer OK"), await w.setLocalDescription(k), N("set local description OK"), await o(C, k, _), N("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 {
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(l) {
357
- return d(C, _, l);
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 l, p;
364
- if (C) {
365
- const M = Y(w.iceConnectionState);
366
- if (w) {
367
- if (M === S.New) {
368
- (l = a.onVideoStateChange) == null || l.call(a, K.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);
369
566
  return;
370
567
  }
371
- 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;
372
573
  }
373
574
  try {
374
- M === S.Connected && await g(C, _).catch((y) => {
375
- });
376
- } catch (y) {
377
- N("Error on close stream connection", y);
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
- (p = a.onVideoStateChange) == null || p.call(a, K.Stop), clearInterval(f);
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: C
593
+ streamId: streamIdFromServer
390
594
  };
391
595
  }
392
- let q = {};
393
- function Ae(e) {
394
- const n = window != null && window.hasOwnProperty("DID_AGENTS_API") ? "agents-ui" : "agents-sdk", s = e.agent.presenter, a = {
395
- token: e.token || "testKey",
396
- distinct_id: e.distinctId || Q(),
397
- agentId: e.agent.id,
398
- agentType: s.type === "clip" && s.presenter_id.startsWith("v2_") ? "clip_v2" : s.type,
399
- 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 ?? ""
400
606
  };
401
607
  return {
402
- ...a,
403
- isEnabled: e.isEnabled ?? !0,
608
+ ...analyticProps,
609
+ isEnabled: config.isEnabled ?? true,
404
610
  getRandom: () => Math.random().toString(16).slice(2),
405
- track(t, r) {
406
- if (!this.isEnabled)
611
+ track(event, props) {
612
+ if (!this.isEnabled) {
407
613
  return Promise.resolve();
614
+ }
408
615
  const {
409
- audioPath: i,
410
- ...o
411
- } = r || {}, d = {
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: t,
626
+ event,
419
627
  properties: {
420
- ...o,
421
- ...a,
422
- source: n,
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", d).then((g) => g.json()).catch((g) => console.error(g));
434
- },
435
- linkTrack(t, r, i, o) {
436
- q[t] || (q[t] = {
437
- events: {},
438
- resolvedDependencies: []
439
- }), o.includes(i) || o.push(i);
440
- const d = q[t];
441
- if (d.events[i] = {
442
- props: r
443
- }, d.resolvedDependencies.push(i), o.every((h) => d.resolvedDependencies.includes(h))) {
444
- const h = o.reduce((m, C) => d.events[C] ? {
445
- ...m,
446
- ...d.events[C].props
447
- } : m, {});
448
- this.track(t, h), d.resolvedDependencies = d.resolvedDependencies.filter((m) => !o.includes(m)), o.forEach((m) => {
449
- 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];
450
673
  });
451
674
  }
452
675
  }
453
676
  };
454
677
  }
455
- function be(e) {
456
- var t, r, i, o;
457
- const n = () => /Mobi|Android/i.test(navigator.userAgent) ? "Mobile" : "Desktop", s = () => {
458
- const d = navigator.platform;
459
- return d.toLowerCase().includes("win") ? "Windows" : d.toLowerCase().includes("mac") ? "Mac OS X" : d.toLowerCase().includes("linux") ? "Linux" : "Unknown";
460
- }, 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;
461
696
  return {
462
- $os: `${s()}`,
463
- isMobile: `${n() == "Mobile"}`,
697
+ $os: `${getUserOS()}`,
698
+ isMobile: `${mobileOrDesktop() == "Mobile"}`,
464
699
  browser: navigator.userAgent,
465
700
  origin: window.location.origin,
466
- 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,
467
702
  agentVoice: {
468
- voiceId: (r = (t = e.presenter) == null ? void 0 : t.voice) == null ? void 0 : r.voice_id,
469
- 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
470
705
  }
471
706
  };
472
707
  }
473
- function Te(e, n, s) {
474
- var g, h;
708
+ function getStreamAnalyticsProps(data, agent, additionalProps) {
709
+ var _a, _b;
475
710
  const {
476
- event: a,
477
- ...t
478
- } = e, {
479
- template: r
480
- } = (n == null ? void 0 : n.llm) || {}, {
481
- language: i
482
- } = ((g = n == null ? void 0 : n.presenter) == null ? void 0 : g.voice) || {}, {
483
- stitch: o
484
- } = (n == null ? void 0 : n.presenter) || {};
485
- return {
486
- ...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,
487
725
  llm: {
488
- ...t.llm,
489
- template: r
726
+ ...baseProps.llm,
727
+ template
490
728
  },
491
729
  script: {
492
- ...t.script,
730
+ ...baseProps.script,
493
731
  provider: {
494
- ...(h = t == null ? void 0 : t.script) == null ? void 0 : h.provider,
495
- language: i
732
+ ...(_b = baseProps == null ? void 0 : baseProps.script) == null ? void 0 : _b.provider,
733
+ language
496
734
  }
497
735
  },
498
- stitch: o,
499
- ...s
736
+ stitch,
737
+ ...additionalProps
500
738
  };
739
+ return props;
501
740
  }
502
- let L = 0;
503
- const De = 20 * 1e3;
504
- function Pe(e, n, s, a) {
505
- 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;
506
745
  return {
507
- videoType: me(e.presenter.type),
508
- output_resolution: (t = s == null ? void 0 : s.streamOptions) == null ? void 0 : t.outputResolution,
509
- session_timeout: (r = s == null ? void 0 : s.streamOptions) == null ? void 0 : r.sessionTimeout,
510
- stream_warmup: (i = s == null ? void 0 : s.streamOptions) == null ? void 0 : i.streamWarmup,
511
- compatibility_mode: (o = s == null ? void 0 : s.streamOptions) == null ? void 0 : o.compatibilityMode,
512
- 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
513
752
  };
514
753
  }
515
- function re(e) {
516
- return e === I.Playground ? {
754
+ function getRequestHeaders(chatMode) {
755
+ return chatMode === ChatMode.Playground ? {
517
756
  headers: {
518
- [Se]: "true"
757
+ [PLAYGROUND_HEADER]: "true"
519
758
  }
520
759
  } : {};
521
760
  }
522
- async function ae(e, n, s, a, t) {
761
+ async function newChat(agentId, agentsApi, analytics, chatMode, persist) {
523
762
  try {
524
- const r = await n.newChat(e, {
525
- persist: t ?? !1
526
- }, re(a));
527
- return s.track("agent-chat", {
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: r.id,
530
- agent_id: e,
531
- mode: a
532
- }), r;
533
- } catch (r) {
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(r);
536
- const i = JSON.parse(r.message);
537
- 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") {
538
778
  throw new Error("InsufficientCreditsError");
539
- } catch (i) {
540
- console.error("Error parsing the error message:", i);
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 xe(e, n, s, a, t, r) {
546
- return new Promise(async (i, o) => {
547
- var $;
548
- L = 0;
549
- const d = setTimeout(() => {
550
- h(new Error("Could not connect"));
551
- }, De), g = (v) => {
552
- clearTimeout(d), i(v);
553
- }, h = (v) => {
554
- clearTimeout(d), o(v);
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
- let m;
557
- !t && n.mode !== I.DirectPlayback && (m = ae(e.id, s, a, n.mode, n.persistentChat).catch((v) => {
558
- h(v);
559
- }));
560
- const C = await $e(e.id, Pe(e, t, n, r), {
561
- ...n,
562
- analytics: a,
563
- warmup: ($ = n.streamOptions) == null ? void 0 : $.streamWarmup,
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
- ...n.callbacks,
566
- onConnectionStateChange: async (v) => {
567
- var _, w, D, A;
568
- v === S.Connected ? (m && (t = await m), C ? ((w = (_ = n.callbacks).onConnectionStateChange) == null || w.call(_, v), g({
569
- chat: t,
570
- streamingManager: C
571
- })) : t && h(new Error("Something went wrong while initializing the manager"))) : (A = (D = n.callbacks).onConnectionStateChange) == null || A.call(D, v);
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(v) {
574
- var _, w;
575
- (w = (_ = n.callbacks).onVideoStateChange) == null || w.call(_, v), L > 0 && v === K.Start && a.linkTrack("agent-video", {
576
- event: "start",
577
- latency: Date.now() - L
578
- }, "start", [H.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
+ }
579
841
  }
580
842
  }
581
- }).catch(h);
843
+ }).catch(reject);
582
844
  });
583
845
  }
584
- function Ne(e) {
585
- var s;
586
- const n = (s = e.greetings) == null ? void 0 : s.filter((a) => a.length > 0);
587
- if (n && n.length > 0) {
588
- const a = Math.floor(Math.random() * n.length);
589
- return n[a];
590
- } else
591
- return `Hi! I'm ${e.preview_name || "My Agent"}. How can I help you?`;
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 Z(e, n) {
594
- return n && n.length > 0 ? n : [{
595
- content: e,
596
- id: B(),
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 Oe(e) {
602
- if (e.answer !== void 0)
603
- return e.answer;
604
- let n = 0, s = "";
605
- for (; n in e; )
606
- s += e[n], n++;
607
- 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;
608
878
  }
609
- function Le(e, n, s, a, t) {
610
- if (!(e === z.Partial || e === z.Answer))
879
+ function processChatEvent(event, data, chatEventQueue, items, onNewMessage) {
880
+ if (!(event === ChatProgress.Partial || event === ChatProgress.Answer)) {
611
881
  return;
612
- const r = a.messages[a.messages.length - 1];
613
- 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") {
614
885
  return;
886
+ }
615
887
  const {
616
- content: i,
617
- sequence: o
618
- } = n;
619
- e === z.Partial ? s[o] = i : s.answer = i;
620
- const d = Oe(s);
621
- (r.content !== d || e === z.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
+ }
622
901
  }
623
- async function ze(e, n) {
624
- var w, D, A;
625
- let s = {}, a = !0;
626
- const t = {
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: n.mode || I.Functional
629
- }, r = n.baseURL || j, i = n.wsURL || we, o = n.mixpanelKey || fe, d = te(n.auth, r, n.callbacks.onError), g = await d.getById(e), h = Ne(g);
630
- t.messages = Z(h, n.initialMessages), (D = (w = n.callbacks).onNewMessage) == null || D.call(w, [...t.messages], "answer");
631
- const m = Ae({
632
- token: o,
633
- agent: g,
634
- isEnabled: n.enableAnalitics,
635
- distinctId: n.distinctId
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
- m.track("agent-sdk", {
924
+ analytics.track("agent-sdk", {
638
925
  event: "loaded",
639
- ...be(g)
926
+ ...getAnaliticsInfo(agentInstance)
640
927
  });
641
- const C = {
642
- onMessage: (c, u) => {
643
- var f, k;
644
- if ("content" in u)
645
- Le(c, u, s, t, n.callbacks.onNewMessage), c === z.Answer && m.track("agent-message-received", {
646
- messages: t.messages.length,
647
- mode: t.chatMode
648
- });
649
- else {
650
- const l = H, p = [l.StreamVideoDone, l.StreamVideoError, l.StreamVideoRejected], M = [l.StreamFailed, l.StreamVideoError, l.StreamVideoRejected], y = Te(u, g, {
651
- 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
652
945
  });
653
- if (c = c, c === l.StreamVideoCreated)
654
- m.linkTrack("agent-video", y, l.StreamVideoCreated, ["start"]);
655
- else if (p.includes(c)) {
656
- const R = c.split("/")[1];
657
- m.track("agent-video", {
658
- ...y,
659
- 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
660
959
  });
661
960
  }
662
- M.includes(c) && ((k = (f = n.callbacks).onError) == null || k.call(f, new Error(`Stream failed with event ${c}`), {
663
- data: u
664
- })), u.event === l.StreamDone && v();
961
+ if (data.event === SEvent.StreamDone) {
962
+ disconnect();
963
+ }
665
964
  }
666
965
  }
667
966
  };
668
- async function $(c) {
669
- var R, b, W, P, T, x, F;
670
- (b = (R = n.callbacks).onConnectionStateChange) == null || b.call(R, S.Connecting), L = 0;
671
- const u = 3;
672
- c && !a && (delete t.chat, t.messages = Z(h), (P = (W = n.callbacks).onNewMessage) == null || P.call(W, [...t.messages], "answer"));
673
- const f = n.mode === I.DirectPlayback ? Promise.resolve(void 0) : _e(n.auth, i, C);
674
- async function k() {
675
- var U, O, V;
676
- for (let X = 1; X <= u; X++)
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 E = await xe(g, n, d, m, t.chat, c ? h : void 0);
679
- return E.chat && E.chat.id !== ((U = t.chat) == null ? void 0 : U.id) && (t.chat = E.chat, (V = (O = n.callbacks).onNewChat) == null || V.call(O, E.chat.id)), E;
680
- } catch (E) {
681
- if ((E == null ? void 0 : E.message) !== "Could not connect")
682
- throw E;
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 l = k().catch((U) => {
687
- var O, V;
688
- throw _(I.Maintenance), (V = (O = n.callbacks).onConnectionStateChange) == null || V.call(O, S.Fail), U;
689
- }), [p, {
690
- streamingManager: M,
691
- chat: y
692
- }] = await Promise.all([f, l]);
693
- y && y.id !== ((T = t.chat) == null ? void 0 : T.id) && ((F = (x = n.callbacks).onNewChat) == null || F.call(x, y.id)), t.streamingManager = M, t.socketManager = p, t.chat = y, a = !1, _((y == null ? void 0 : y.chat_mode) ?? n.mode ?? I.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);
694
1014
  }
695
- async function v() {
696
- var c, u, f, k;
697
- (c = t.socketManager) == null || c.disconnect(), await ((u = t.streamingManager) == null ? void 0 : u.disconnect()), delete t.streamingManager, delete t.socketManager, (k = (f = n.callbacks).onConnectionStateChange) == null || k.call(f, S.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);
698
1022
  }
699
- async function _(c) {
700
- var u, f;
701
- c !== t.chatMode && (m.track("agent-mode-change", {
702
- mode: c
703
- }), t.chatMode = c, t.chatMode !== I.Functional && await v(), (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
+ }
704
1035
  }
705
1036
  return {
706
- agent: g,
707
- starterMessages: ((A = g.knowledge) == null ? void 0 : A.starter_message) || [],
708
- getSTTToken: async () => {
709
- var c;
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 c;
715
- await $(!0), m.track("agent-chat", {
1042
+ var _a2;
1043
+ await connect2(true);
1044
+ analytics.track("agent-chat", {
716
1045
  event: "connect",
717
- chatId: (c = t.chat) == null ? void 0 : c.id,
718
- agentId: g.id,
719
- mode: t.chatMode
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 c;
724
- await v(), await $(!1), m.track("agent-chat", {
1052
+ var _a2;
1053
+ await disconnect();
1054
+ await connect2(false);
1055
+ analytics.track("agent-chat", {
725
1056
  event: "reconnect",
726
- chatId: (c = t.chat) == null ? void 0 : c.id,
727
- agentId: g.id,
728
- mode: t.chatMode
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 c;
733
- await v(), m.track("agent-chat", {
1063
+ var _a2;
1064
+ await disconnect();
1065
+ analytics.track("agent-chat", {
734
1066
  event: "disconnect",
735
- chatId: (c = t.chat) == null ? void 0 : c.id,
736
- agentId: g.id,
737
- mode: t.chatMode
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(c) {
741
- var f, k, l, p, M, y;
742
- const u = B();
743
- s = {};
1072
+ async chat(userMessage) {
1073
+ var _a2, _b2, _c2, _d, _e, _f;
1074
+ const id = getRandom();
1075
+ chatEventQueue = {};
744
1076
  try {
745
- if (L = Date.now(), n.mode === I.DirectPlayback)
1077
+ messageSentTimestamp = Date.now();
1078
+ if (options.mode === ChatMode.DirectPlayback) {
746
1079
  throw new Error("Direct playback is enabled, chat is disabled");
747
- if (c.length >= 800)
1080
+ } else if (userMessage.length >= 800) {
748
1081
  throw new Error("Message cannot be more than 800 characters");
749
- if (c.length === 0)
1082
+ } else if (userMessage.length === 0) {
750
1083
  throw new Error("Message cannot be empty");
751
- if (t.chatMode === I.Maintenance)
1084
+ } else if (items.chatMode === ChatMode.Maintenance) {
752
1085
  throw new Error("Chat is in maintenance mode");
753
- if (![I.TextOnly, I.Playground].includes(t.chatMode))
754
- if (t.streamingManager) {
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
- t.messages.push({
760
- id: B(),
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: c,
763
- created_at: new Date(L).toISOString()
764
- }), (k = (f = n.callbacks).onNewMessage) == null || k.call(f, [...t.messages], "user"), t.chat || (t.chat = await ae(g.id, d, m, t.chatMode, n.persistentChat), (p = (l = n.callbacks).onNewChat) == null || p.call(l, t.chat.id));
765
- const R = {
766
- id: u,
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
- }, b = [...t.messages];
772
- t.messages.push(R);
773
- const W = (T) => {
774
- var x, F;
775
- return d.chat(g.id, T, {
776
- sessionId: (x = t.streamingManager) == null ? void 0 : x.sessionId,
777
- streamId: (F = t.streamingManager) == null ? void 0 : F.streamId,
778
- chatMode: t.chatMode,
779
- messages: b.map(({
780
- matches: U,
781
- ...O
782
- }) => O)
783
- }, re(t.chatMode));
784
- }, P = await W(t.chat.id).catch(async (T) => {
785
- var x;
786
- if (!((x = T == null ? void 0 : T.message) != null && x.includes("missing or invalid session_id")))
787
- throw T;
788
- return await v(), await $(!1), W(t.chat.id);
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
- return m.track("agent-message-send", {
1134
+ analytics.track("agent-message-send", {
791
1135
  event: "success",
792
- mode: t.chatMode,
793
- messages: t.messages.length + 1
794
- }), R.context = P.context, R.matches = P.matches, P.result && (R.content = P.result, m.track("agent-message-received", {
795
- latency: Date.now() - L,
796
- mode: t.chatMode,
797
- messages: t.messages.length
798
- }), (y = (M = n.callbacks).onNewMessage) == null || y.call(M, [...t.messages], "answer")), P;
799
- } catch (R) {
800
- throw t.messages[t.messages.length - 1].id === u && t.messages.pop(), m.track("agent-message-send", {
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: t.chatMode,
803
- messages: t.messages.length
804
- }), R;
1157
+ mode: items.chatMode,
1158
+ messages: items.messages.length
1159
+ });
1160
+ throw e;
805
1161
  }
806
1162
  },
807
- rate(c, u, f) {
808
- var p, M, y, R;
809
- const k = t.messages.find((b) => b.id === c);
810
- if (t.chat) {
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
- const l = ((p = k.matches) == null ? void 0 : p.map((b) => [b.document_id, b.id])) ?? [];
816
- return m.track("agent-rate", {
817
- event: f ? "update" : "create",
818
- thumb: u === 1 ? "up" : "down",
819
- knowledge_id: ((M = g.knowledge) == null ? void 0 : M.id) ?? "",
820
- mode: t.chatMode,
821
- matches: l,
822
- score: u
823
- }), f ? d.updateRating(g.id, t.chat.id, f, {
824
- knowledge_id: ((y = g.knowledge) == null ? void 0 : y.id) ?? "",
825
- message_id: c,
826
- matches: l,
827
- score: u
828
- }) : d.createRating(g.id, t.chat.id, {
829
- knowledge_id: ((R = g.knowledge) == null ? void 0 : R.id) ?? "",
830
- message_id: c,
831
- matches: l,
832
- score: u
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(c) {
836
- var u;
837
- if (!t.chat)
1195
+ deleteRate(id) {
1196
+ var _a2;
1197
+ if (!items.chat) {
838
1198
  throw new Error("Chat is not initialized");
839
- return m.track("agent-rate-delete", {
1199
+ }
1200
+ analytics.track("agent-rate-delete", {
840
1201
  type: "text",
841
- chat_id: (u = t.chat) == null ? void 0 : u.id,
842
- id: c,
843
- mode: t.chatMode
844
- }), d.deleteRating(g.id, t.chat.id, c);
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(c) {
847
- if (!t.streamingManager)
1208
+ speak(payload) {
1209
+ if (!items.streamingManager) {
848
1210
  throw new Error("Please connect to the agent first");
849
- function u() {
850
- if (typeof c == "string") {
851
- if (!g.presenter.voice)
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: g.presenter.voice,
856
- input: c,
857
- ssml: !1
1219
+ provider: agentInstance.presenter.voice,
1220
+ input: payload,
1221
+ ssml: false
858
1222
  };
859
1223
  }
860
- if (c.type === "text" && !c.provider) {
861
- if (!g.presenter.voice)
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: g.presenter.voice,
866
- input: c.input,
867
- ssml: c.ssml
1230
+ provider: agentInstance.presenter.voice,
1231
+ input: payload.input,
1232
+ ssml: payload.ssml
868
1233
  };
869
1234
  }
870
- return c;
1235
+ return payload;
871
1236
  }
872
- const f = u();
873
- return m.track("agent-speak", f), t.streamingManager.speak({
874
- script: f
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 Ke(e, n, s) {
1245
+ function getAgent(agentId, auth, baseURL) {
880
1246
  const {
881
- getById: a
882
- } = te(n, s || j);
883
- return a(e);
1247
+ getById
1248
+ } = createAgentsApi(auth, baseURL || didApiUrl);
1249
+ return getById(agentId);
884
1250
  }
885
1251
  export {
886
- oe as AgentStatus,
887
- I as ChatMode,
888
- z as ChatProgress,
889
- S as ConnectionState,
890
- ge as DocumentType,
891
- le as KnowledgeType,
892
- se as PlanGroup,
893
- ue as Providers,
894
- ce as RateState,
895
- H as StreamEvents,
896
- K as StreamingState,
897
- de as Subject,
898
- ie as UserPlan,
899
- G as VideoType,
900
- he as VoiceAccess,
901
- ze as createAgentManager,
902
- Ke as getAgent,
903
- me as mapVideoType
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
  };