@d-id/client-sdk 1.1.24 → 1.1.25
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 +1742 -26
- package/dist/index.umd.cjs +6 -6
- package/dist/src/services/interrupt/index.d.ts +2 -1
- package/dist/src/services/streaming-manager/livekit-manager.d.ts +11 -2
- package/package.json +1 -1
- package/dist/index-VqW3e8UX.js +0 -1425
- package/dist/livekit-manager-CQZ_EbD9.js +0 -256
package/dist/index.js
CHANGED
|
@@ -1,28 +1,1744 @@
|
|
|
1
|
-
|
|
1
|
+
var Ue = Object.defineProperty;
|
|
2
|
+
var We = (e, t, i) => t in e ? Ue(e, t, { enumerable: !0, configurable: !0, writable: !0, value: i }) : e[t] = i;
|
|
3
|
+
var ue = (e, t, i) => We(e, typeof t != "symbol" ? t + "" : t, i);
|
|
4
|
+
class ge extends Error {
|
|
5
|
+
constructor({ kind: i, description: n, error: c }) {
|
|
6
|
+
super(JSON.stringify({ kind: i, description: n }));
|
|
7
|
+
ue(this, "kind");
|
|
8
|
+
ue(this, "description");
|
|
9
|
+
ue(this, "error");
|
|
10
|
+
this.kind = i, this.description = n, this.error = c;
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
class Ve extends ge {
|
|
14
|
+
constructor(t, i) {
|
|
15
|
+
super({
|
|
16
|
+
kind: "ChatCreationFailed",
|
|
17
|
+
description: `Failed to create ${i ? "persistent" : ""} chat, mode: ${t}`
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
class Oe extends ge {
|
|
22
|
+
constructor(t) {
|
|
23
|
+
super({ kind: "ChatModeDowngraded", description: `Chat mode downgraded to ${t}` });
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
class se extends ge {
|
|
27
|
+
constructor(i, n) {
|
|
28
|
+
super({ kind: "ValidationError", description: i });
|
|
29
|
+
ue(this, "key");
|
|
30
|
+
this.key = n;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
class He extends ge {
|
|
34
|
+
constructor(t) {
|
|
35
|
+
super({ kind: "WSError", description: t });
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
var qe = /* @__PURE__ */ ((e) => (e.TRIAL = "trial", e.BASIC = "basic", e.ENTERPRISE = "enterprise", e.LITE = "lite", e.ADVANCED = "advanced", e))(qe || {}), Qe = /* @__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))(Qe || {}), Xe = /* @__PURE__ */ ((e) => (e.Created = "created", e.Started = "started", e.Done = "done", e.Error = "error", e.Rejected = "rejected", e.Ready = "ready", e))(Xe || {}), Ye = /* @__PURE__ */ ((e) => (e.Unrated = "Unrated", e.Positive = "Positive", e.Negative = "Negative", e))(Ye || {}), N = /* @__PURE__ */ ((e) => (e.Functional = "Functional", e.TextOnly = "TextOnly", e.Maintenance = "Maintenance", e.Playground = "Playground", e.DirectPlayback = "DirectPlayback", e.Off = "Off", e))(N || {}), Y = /* @__PURE__ */ ((e) => (e.Embed = "embed", e.Query = "query", e.Partial = "partial", e.Answer = "answer", e.Transcribe = "transcribe", e.Complete = "done", e))(Y || {}), Ze = /* @__PURE__ */ ((e) => (e.KnowledgeProcessing = "knowledge/processing", e.KnowledgeIndexing = "knowledge/indexing", e.KnowledgeFailed = "knowledge/error", e.KnowledgeDone = "knowledge/done", e))(Ze || {}), Ge = /* @__PURE__ */ ((e) => (e.Knowledge = "knowledge", e.Document = "document", e.Record = "record", e))(Ge || {}), et = /* @__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))(et || {}), $e = /* @__PURE__ */ ((e) => (e.Clip = "clip", e.Talk = "talk", e.Expressive = "expressive", e))($e || {});
|
|
39
|
+
const sn = (e) => {
|
|
40
|
+
switch (e) {
|
|
41
|
+
case "clip":
|
|
42
|
+
return "clip";
|
|
43
|
+
case "talk":
|
|
44
|
+
return "talk";
|
|
45
|
+
case "expressive":
|
|
46
|
+
return "expressive";
|
|
47
|
+
default:
|
|
48
|
+
throw new Error(`Unknown video type: ${e}`);
|
|
49
|
+
}
|
|
50
|
+
};
|
|
51
|
+
var k = /* @__PURE__ */ ((e) => (e.Start = "START", e.Stop = "STOP", e))(k || {}), ee = /* @__PURE__ */ ((e) => (e.Strong = "STRONG", e.Weak = "WEAK", e.Unknown = "UNKNOWN", e))(ee || {}), ie = /* @__PURE__ */ ((e) => (e.Idle = "IDLE", e.Talking = "TALKING", e))(ie || {}), B = /* @__PURE__ */ ((e) => (e.ChatAnswer = "chat/answer", e.ChatPartial = "chat/partial", e.ChatAudioTranscribed = "chat/audio-transcribed", e.StreamDone = "stream/done", e.StreamStarted = "stream/started", e.StreamFailed = "stream/error", e.StreamReady = "stream/ready", e.StreamCreated = "stream/created", e.StreamInterrupt = "stream/interrupt", e.StreamVideoCreated = "stream-video/started", e.StreamVideoDone = "stream-video/done", e.StreamVideoError = "stream-video/error", e.StreamVideoRejected = "stream-video/rejected", e))(B || {}), C = /* @__PURE__ */ ((e) => (e.New = "new", e.Fail = "fail", e.Connected = "connected", e.Connecting = "connecting", e.Closed = "closed", e.Completed = "completed", e.Disconnected = "disconnected", e))(C || {}), ne = /* @__PURE__ */ ((e) => (e.Legacy = "legacy", e.Fluent = "fluent", e))(ne || {}), he = /* @__PURE__ */ ((e) => (e.Livekit = "livekit", e))(he || {}), tt = /* @__PURE__ */ ((e) => (e.Amazon = "amazon", e.AzureOpenAi = "azure-openai", e.Microsoft = "microsoft", e.Afflorithmics = "afflorithmics", e.Elevenlabs = "elevenlabs", e))(tt || {}), nt = /* @__PURE__ */ ((e) => (e.Public = "public", e.Premium = "premium", e.Private = "private", e))(nt || {});
|
|
52
|
+
const rt = 45 * 1e3, it = "X-Playground-Chat", fe = "https://api.d-id.com", at = "wss://notifications.d-id.com", ot = "79f81a83a67430be2bc0fd61042b8faa", xe = (e) => new Promise((t) => setTimeout(t, e)), ce = (e = 16) => {
|
|
53
|
+
const t = new Uint8Array(e);
|
|
54
|
+
return window.crypto.getRandomValues(t), Array.from(t, (i) => i.toString(16).padStart(2, "0")).join("").slice(0, 13);
|
|
55
|
+
}, Fe = (e) => e.type === "clip" && e.presenter_id.startsWith("v2_") ? "clip_v2" : e.type, De = (e) => e === $e.Expressive, st = (e) => [N.TextOnly, N.Playground, N.Maintenance].includes(e), Be = (e) => e && [N.DirectPlayback, N.Off].includes(e);
|
|
56
|
+
function ct(e, t) {
|
|
57
|
+
let i;
|
|
58
|
+
return {
|
|
59
|
+
promise: new Promise((c, o) => {
|
|
60
|
+
i = setTimeout(() => o(new Error(t)), e);
|
|
61
|
+
}),
|
|
62
|
+
clear: () => clearTimeout(i)
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
async function ye(e, t) {
|
|
66
|
+
const i = {
|
|
67
|
+
limit: (t == null ? void 0 : t.limit) ?? 3,
|
|
68
|
+
delayMs: (t == null ? void 0 : t.delayMs) ?? 0,
|
|
69
|
+
timeout: (t == null ? void 0 : t.timeout) ?? 3e4,
|
|
70
|
+
timeoutErrorMessage: (t == null ? void 0 : t.timeoutErrorMessage) || "Timeout error",
|
|
71
|
+
shouldRetryFn: (t == null ? void 0 : t.shouldRetryFn) ?? (() => !0),
|
|
72
|
+
onRetry: (t == null ? void 0 : t.onRetry) ?? (() => {
|
|
73
|
+
})
|
|
74
|
+
};
|
|
75
|
+
let n;
|
|
76
|
+
for (let c = 1; c <= i.limit; c++)
|
|
77
|
+
try {
|
|
78
|
+
if (!i.timeout)
|
|
79
|
+
return await e();
|
|
80
|
+
const { promise: o, clear: s } = ct(i.timeout, i.timeoutErrorMessage), a = e().finally(s);
|
|
81
|
+
return await Promise.race([a, o]);
|
|
82
|
+
} catch (o) {
|
|
83
|
+
if (n = o, !i.shouldRetryFn(o) || c >= i.limit)
|
|
84
|
+
throw o;
|
|
85
|
+
await xe(i.delayMs), i.onRetry(o);
|
|
86
|
+
}
|
|
87
|
+
throw n;
|
|
88
|
+
}
|
|
89
|
+
function ve(e) {
|
|
90
|
+
if (e !== void 0)
|
|
91
|
+
return window.localStorage.setItem("did_external_key_id", e), e;
|
|
92
|
+
let t = window.localStorage.getItem("did_external_key_id");
|
|
93
|
+
if (!t) {
|
|
94
|
+
let i = ce();
|
|
95
|
+
window.localStorage.setItem("did_external_key_id", i), t = i;
|
|
96
|
+
}
|
|
97
|
+
return t;
|
|
98
|
+
}
|
|
99
|
+
let dt = ce();
|
|
100
|
+
function ze(e, t) {
|
|
101
|
+
if (e.type === "bearer")
|
|
102
|
+
return `Bearer ${e.token}`;
|
|
103
|
+
if (e.type === "basic")
|
|
104
|
+
return `Basic ${btoa(`${e.username}:${e.password}`)}`;
|
|
105
|
+
if (e.type === "key")
|
|
106
|
+
return `Client-Key ${e.clientKey}.${ve(t)}_${dt}`;
|
|
107
|
+
throw new Error(`Unknown auth type: ${e}`);
|
|
108
|
+
}
|
|
109
|
+
const ut = (e) => ye(e, {
|
|
110
|
+
limit: 3,
|
|
111
|
+
delayMs: 1e3,
|
|
112
|
+
timeout: 0,
|
|
113
|
+
shouldRetryFn: (t) => t.status === 429
|
|
114
|
+
});
|
|
115
|
+
function Me(e, t = fe, i, n) {
|
|
116
|
+
const c = async (o, s) => {
|
|
117
|
+
const { skipErrorHandler: a, ...r } = s || {}, d = await ut(
|
|
118
|
+
() => fetch(t + (o != null && o.startsWith("/") ? o : `/${o}`), {
|
|
119
|
+
...r,
|
|
120
|
+
headers: {
|
|
121
|
+
...r.headers,
|
|
122
|
+
Authorization: ze(e, n),
|
|
123
|
+
"Content-Type": "application/json"
|
|
124
|
+
}
|
|
125
|
+
})
|
|
126
|
+
);
|
|
127
|
+
if (!d.ok) {
|
|
128
|
+
let m = await d.text().catch(() => `Failed to fetch with status ${d.status}`);
|
|
129
|
+
const p = new Error(m);
|
|
130
|
+
throw i && !a && i(p, { url: o, options: r, headers: d.headers }), p;
|
|
131
|
+
}
|
|
132
|
+
return d.json();
|
|
133
|
+
};
|
|
134
|
+
return {
|
|
135
|
+
get(o, s) {
|
|
136
|
+
return c(o, { ...s, method: "GET" });
|
|
137
|
+
},
|
|
138
|
+
post(o, s, a) {
|
|
139
|
+
return c(o, { ...a, body: JSON.stringify(s), method: "POST" });
|
|
140
|
+
},
|
|
141
|
+
delete(o, s, a) {
|
|
142
|
+
return c(o, { ...a, body: JSON.stringify(s), method: "DELETE" });
|
|
143
|
+
},
|
|
144
|
+
patch(o, s, a) {
|
|
145
|
+
return c(o, { ...a, body: JSON.stringify(s), method: "PATCH" });
|
|
146
|
+
}
|
|
147
|
+
};
|
|
148
|
+
}
|
|
149
|
+
function lt(e, t = fe, i, n) {
|
|
150
|
+
const c = Me(e, `${t}/agents`, i, n);
|
|
151
|
+
return {
|
|
152
|
+
create(o, s) {
|
|
153
|
+
return c.post("/", o, s);
|
|
154
|
+
},
|
|
155
|
+
getAgents(o, s) {
|
|
156
|
+
return c.get(`/${o ? `?tag=${o}` : ""}`, s).then((a) => a ?? []);
|
|
157
|
+
},
|
|
158
|
+
getById(o, s) {
|
|
159
|
+
return c.get(`/${o}`, s);
|
|
160
|
+
},
|
|
161
|
+
delete(o, s) {
|
|
162
|
+
return c.delete(`/${o}`, void 0, s);
|
|
163
|
+
},
|
|
164
|
+
update(o, s, a) {
|
|
165
|
+
return c.patch(`/${o}`, s, a);
|
|
166
|
+
},
|
|
167
|
+
newChat(o, s, a) {
|
|
168
|
+
return c.post(`/${o}/chat`, s, a);
|
|
169
|
+
},
|
|
170
|
+
chat(o, s, a, r) {
|
|
171
|
+
return c.post(`/${o}/chat/${s}`, a, r);
|
|
172
|
+
},
|
|
173
|
+
createRating(o, s, a, r) {
|
|
174
|
+
return c.post(`/${o}/chat/${s}/ratings`, a, r);
|
|
175
|
+
},
|
|
176
|
+
updateRating(o, s, a, r, d) {
|
|
177
|
+
return c.patch(`/${o}/chat/${s}/ratings/${a}`, r, d);
|
|
178
|
+
},
|
|
179
|
+
deleteRating(o, s, a, r) {
|
|
180
|
+
return c.delete(`/${o}/chat/${s}/ratings/${a}`, r);
|
|
181
|
+
},
|
|
182
|
+
getSTTToken(o, s) {
|
|
183
|
+
return c.get(`/${o}/stt-token`, s);
|
|
184
|
+
}
|
|
185
|
+
};
|
|
186
|
+
}
|
|
187
|
+
function ft(e) {
|
|
188
|
+
var c, o, s, a;
|
|
189
|
+
const t = () => /Mobi|Android/i.test(navigator.userAgent) ? "Mobile" : "Desktop", i = () => {
|
|
190
|
+
const r = navigator.platform;
|
|
191
|
+
return r.toLowerCase().includes("win") ? "Windows" : r.toLowerCase().includes("mac") ? "Mac OS X" : r.toLowerCase().includes("linux") ? "Linux" : "Unknown";
|
|
192
|
+
}, n = e.presenter;
|
|
193
|
+
return {
|
|
194
|
+
$os: `${i()}`,
|
|
195
|
+
isMobile: `${t() == "Mobile"}`,
|
|
196
|
+
browser: navigator.userAgent,
|
|
197
|
+
origin: window.location.origin,
|
|
198
|
+
agentType: Fe(n),
|
|
199
|
+
agentVoice: {
|
|
200
|
+
voiceId: (o = (c = e.presenter) == null ? void 0 : c.voice) == null ? void 0 : o.voice_id,
|
|
201
|
+
provider: (a = (s = e.presenter) == null ? void 0 : s.voice) == null ? void 0 : a.type
|
|
202
|
+
}
|
|
203
|
+
};
|
|
204
|
+
}
|
|
205
|
+
function mt(e) {
|
|
206
|
+
var i, n, c, o, s, a;
|
|
207
|
+
const t = (i = e.llm) == null ? void 0 : i.prompt_customization;
|
|
208
|
+
return {
|
|
209
|
+
agentType: Fe(e.presenter),
|
|
210
|
+
owner_id: e.owner_id ?? "",
|
|
211
|
+
promptVersion: (n = e.llm) == null ? void 0 : n.prompt_version,
|
|
212
|
+
behavior: {
|
|
213
|
+
role: t == null ? void 0 : t.role,
|
|
214
|
+
personality: t == null ? void 0 : t.personality,
|
|
215
|
+
instructions: (c = e.llm) == null ? void 0 : c.instructions
|
|
216
|
+
},
|
|
217
|
+
temperature: (o = e.llm) == null ? void 0 : o.temperature,
|
|
218
|
+
knowledgeSource: t == null ? void 0 : t.knowledge_source,
|
|
219
|
+
starterQuestionsCount: (a = (s = e.knowledge) == null ? void 0 : s.starter_message) == null ? void 0 : a.length,
|
|
220
|
+
topicsToAvoid: t == null ? void 0 : t.topics_to_avoid,
|
|
221
|
+
maxResponseLength: t == null ? void 0 : t.max_response_length,
|
|
222
|
+
agentId: e.id,
|
|
223
|
+
access: e.access,
|
|
224
|
+
name: e.preview_name,
|
|
225
|
+
...e.access === "public" ? { from: "agent-template" } : {}
|
|
226
|
+
};
|
|
227
|
+
}
|
|
228
|
+
const gt = (e) => e.reduce((t, i) => t + i, 0), _e = (e) => gt(e) / e.length;
|
|
229
|
+
function ht(e, t, i) {
|
|
230
|
+
var r, d, m;
|
|
231
|
+
const { event: n, ...c } = e, { template: o } = (t == null ? void 0 : t.llm) || {}, { language: s } = ((r = t == null ? void 0 : t.presenter) == null ? void 0 : r.voice) || {};
|
|
232
|
+
return {
|
|
233
|
+
...c,
|
|
234
|
+
llm: { ...c.llm, template: o },
|
|
235
|
+
script: { ...c.script, provider: { ...(d = c == null ? void 0 : c.script) == null ? void 0 : d.provider, language: s } },
|
|
236
|
+
stitch: (t == null ? void 0 : t.presenter.type) === "talk" ? (m = t == null ? void 0 : t.presenter) == null ? void 0 : m.stitch : void 0,
|
|
237
|
+
...i
|
|
238
|
+
};
|
|
239
|
+
}
|
|
240
|
+
function Ae(e) {
|
|
241
|
+
"requestIdleCallback" in window ? requestIdleCallback(e, { timeout: 2e3 }) : setTimeout(e, 0);
|
|
242
|
+
}
|
|
243
|
+
let pe = {};
|
|
244
|
+
const pt = "https://api-js.mixpanel.com/track/?verbose=1&ip=1";
|
|
245
|
+
function wt(e) {
|
|
246
|
+
const t = window != null && window.hasOwnProperty("DID_AGENTS_API") ? "agents-ui" : "agents-sdk";
|
|
247
|
+
return {
|
|
248
|
+
token: e.token || "testKey",
|
|
249
|
+
distinct_id: ve(e.externalId),
|
|
250
|
+
agentId: e.agentId,
|
|
251
|
+
additionalProperties: {
|
|
252
|
+
id: ve(e.externalId),
|
|
253
|
+
...e.mixpanelAdditionalProperties || {}
|
|
254
|
+
},
|
|
255
|
+
isEnabled: e.isEnabled ?? !0,
|
|
256
|
+
getRandom: ce,
|
|
257
|
+
enrich(i) {
|
|
258
|
+
this.additionalProperties = { ...this.additionalProperties, ...i };
|
|
259
|
+
},
|
|
260
|
+
async track(i, n, c) {
|
|
261
|
+
if (!this.isEnabled)
|
|
262
|
+
return Promise.resolve();
|
|
263
|
+
const { audioPath: o, ...s } = n || {}, a = c || Date.now(), r = {
|
|
264
|
+
method: "POST",
|
|
265
|
+
headers: {
|
|
266
|
+
"Content-Type": "application/x-www-form-urlencoded"
|
|
267
|
+
},
|
|
268
|
+
body: new URLSearchParams({
|
|
269
|
+
data: JSON.stringify([
|
|
270
|
+
{
|
|
271
|
+
event: i,
|
|
272
|
+
properties: {
|
|
273
|
+
...this.additionalProperties,
|
|
274
|
+
...s,
|
|
275
|
+
agentId: this.agentId,
|
|
276
|
+
source: t,
|
|
277
|
+
token: this.token,
|
|
278
|
+
time: a,
|
|
279
|
+
$insert_id: this.getRandom(),
|
|
280
|
+
origin: window.location.href,
|
|
281
|
+
"Screen Height": window.screen.height || window.innerWidth,
|
|
282
|
+
"Screen Width": window.screen.width || window.innerHeight,
|
|
283
|
+
"User Agent": navigator.userAgent
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
])
|
|
287
|
+
})
|
|
288
|
+
};
|
|
289
|
+
return fetch(pt, {
|
|
290
|
+
...r,
|
|
291
|
+
keepalive: !0
|
|
292
|
+
}).catch((d) => console.error("Analytics tracking error:", d)), Promise.resolve();
|
|
293
|
+
},
|
|
294
|
+
linkTrack(i, n, c, o) {
|
|
295
|
+
pe[i] || (pe[i] = { events: {}, resolvedDependencies: [] }), o.includes(c) || o.push(c);
|
|
296
|
+
const s = pe[i];
|
|
297
|
+
if (s.events[c] = { props: n }, s.resolvedDependencies.push(c), o.every(
|
|
298
|
+
(r) => s.resolvedDependencies.includes(r)
|
|
299
|
+
)) {
|
|
300
|
+
const r = o.reduce((d, m) => s.events[m] ? { ...d, ...s.events[m].props } : d, {});
|
|
301
|
+
this.track(i, r), s.resolvedDependencies = s.resolvedDependencies.filter(
|
|
302
|
+
(d) => !o.includes(d)
|
|
303
|
+
), o.forEach((d) => {
|
|
304
|
+
delete s.events[d];
|
|
305
|
+
});
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
};
|
|
309
|
+
}
|
|
310
|
+
function Ee() {
|
|
311
|
+
let e = 0;
|
|
312
|
+
return {
|
|
313
|
+
reset: () => e = 0,
|
|
314
|
+
update: () => e = Date.now(),
|
|
315
|
+
get: (t = !1) => t ? Date.now() - e : e
|
|
316
|
+
};
|
|
317
|
+
}
|
|
318
|
+
const te = Ee(), Se = Ee(), Pe = Ee();
|
|
319
|
+
function Ne(e) {
|
|
320
|
+
return e === N.Playground ? { headers: { [it]: "true" } } : {};
|
|
321
|
+
}
|
|
322
|
+
async function Ke(e, t, i, n, c = !1, o) {
|
|
323
|
+
try {
|
|
324
|
+
return !o && !Be(n) && (o = await t.newChat(e.id, { persist: c }, Ne(n)), i.track("agent-chat", {
|
|
325
|
+
event: "created",
|
|
326
|
+
chatId: o.id,
|
|
327
|
+
mode: n
|
|
328
|
+
})), { chat: o, chatMode: (o == null ? void 0 : o.chat_mode) ?? n };
|
|
329
|
+
} catch (s) {
|
|
330
|
+
throw yt(s) === "InsufficientCreditsError" ? new Error("InsufficientCreditsError") : new Error("Cannot create new chat");
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
const yt = (e) => {
|
|
334
|
+
try {
|
|
335
|
+
const t = JSON.parse(e.message);
|
|
336
|
+
return t == null ? void 0 : t.kind;
|
|
337
|
+
} catch {
|
|
338
|
+
return "UnknownError";
|
|
339
|
+
}
|
|
340
|
+
};
|
|
341
|
+
function vt(e) {
|
|
342
|
+
return e && e.length > 0 ? e : [];
|
|
343
|
+
}
|
|
344
|
+
function St(e, t, i, n) {
|
|
345
|
+
const c = Me(e, `${t}/v2/agents/${i}`, n);
|
|
346
|
+
return {
|
|
347
|
+
async createStream(o) {
|
|
348
|
+
return c.post("/sessions", o);
|
|
349
|
+
}
|
|
350
|
+
};
|
|
351
|
+
}
|
|
352
|
+
const Je = (e, t) => (i, n) => e && console.log(`[${t}] ${i}`, n ?? "");
|
|
353
|
+
async function we() {
|
|
354
|
+
try {
|
|
355
|
+
return await import("./livekit-client.esm-C1GRQdZY.js");
|
|
356
|
+
} catch {
|
|
357
|
+
throw new Error(
|
|
358
|
+
"LiveKit client is required for this streaming manager. Please install it using: npm install livekit-client"
|
|
359
|
+
);
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
const Ct = {
|
|
363
|
+
excellent: ee.Strong,
|
|
364
|
+
good: ee.Strong,
|
|
365
|
+
poor: ee.Weak,
|
|
366
|
+
lost: ee.Unknown,
|
|
367
|
+
unknown: ee.Unknown
|
|
368
|
+
}, le = JSON.stringify({
|
|
369
|
+
kind: "InternalServerError",
|
|
370
|
+
description: "Stream Error"
|
|
371
|
+
});
|
|
372
|
+
var Ie = /* @__PURE__ */ ((e) => (e.Chat = "lk.chat", e.Speak = "did.speak", e.Interrupt = "did.interrupt", e))(Ie || {});
|
|
373
|
+
function Ce(e, t, i, n) {
|
|
374
|
+
var c, o;
|
|
375
|
+
throw t("Failed to connect to LiveKit room:", e), n(), (c = i.onConnectionStateChange) == null || c.call(i, C.Fail), (o = i.onError) == null || o.call(i, e, { sessionId: "" }), e;
|
|
376
|
+
}
|
|
377
|
+
async function kt(e, t, i) {
|
|
378
|
+
var V, Te;
|
|
379
|
+
const n = Je(i.debug || !1, "LiveKitStreamingManager"), { Room: c, RoomEvent: o, ConnectionState: s } = await we(), { callbacks: a, auth: r, baseURL: d, analytics: m, microphoneStream: p } = i;
|
|
380
|
+
let u = null, I = !1;
|
|
381
|
+
const x = ne.Fluent;
|
|
382
|
+
let J = !0, T = null, A = null;
|
|
383
|
+
u = new c({
|
|
384
|
+
adaptiveStream: !1,
|
|
385
|
+
// Must be false to use mediaStreamTrack directly
|
|
386
|
+
dynacast: !0
|
|
387
|
+
});
|
|
388
|
+
const H = St(r, d || fe, e, a.onError);
|
|
389
|
+
let S, U, W;
|
|
390
|
+
try {
|
|
391
|
+
const l = await H.createStream({
|
|
392
|
+
transport_provider: he.Livekit,
|
|
393
|
+
chat_persist: t.chat_persist ?? !0
|
|
394
|
+
}), { id: g, session_token: v, session_url: R } = l;
|
|
395
|
+
(V = a.onStreamCreated) == null || V.call(a, { session_id: g, stream_id: g, agent_id: e }), S = g, U = v, W = R, await u.prepareConnection(W, U);
|
|
396
|
+
} catch (l) {
|
|
397
|
+
Ce(l, n, a, () => {
|
|
398
|
+
J = !1;
|
|
399
|
+
});
|
|
400
|
+
}
|
|
401
|
+
if (!W || !U || !S)
|
|
402
|
+
return Promise.reject(new Error("Failed to initialize LiveKit stream"));
|
|
403
|
+
u.on(o.ConnectionStateChanged, q).on(o.ConnectionQualityChanged, ae).on(o.ActiveSpeakersChanged, f).on(o.ParticipantConnected, D).on(o.ParticipantDisconnected, w).on(o.TrackSubscribed, L).on(o.TrackUnsubscribed, M).on(o.DataReceived, _).on(o.MediaDevicesError, K).on(o.EncryptionError, Q).on(o.TrackSubscriptionFailed, X), (Te = a.onConnectionStateChange) == null || Te.call(a, C.New);
|
|
404
|
+
try {
|
|
405
|
+
await u.connect(W, U), n("LiveKit room joined successfully"), J = !1;
|
|
406
|
+
} catch (l) {
|
|
407
|
+
Ce(l, n, a, () => {
|
|
408
|
+
J = !1;
|
|
409
|
+
});
|
|
410
|
+
}
|
|
411
|
+
m.enrich({
|
|
412
|
+
"stream-type": x
|
|
413
|
+
});
|
|
414
|
+
function q(l) {
|
|
415
|
+
var g, v, R, b, O;
|
|
416
|
+
switch (n("Connection state changed:", l), l) {
|
|
417
|
+
case s.Connecting:
|
|
418
|
+
(g = a.onConnectionStateChange) == null || g.call(a, C.Connecting);
|
|
419
|
+
break;
|
|
420
|
+
case s.Connected:
|
|
421
|
+
n("LiveKit room connected successfully"), I = !0, J ? queueMicrotask(() => {
|
|
422
|
+
var F;
|
|
423
|
+
(F = a.onConnectionStateChange) == null || F.call(a, C.Connected);
|
|
424
|
+
}) : (v = a.onConnectionStateChange) == null || v.call(a, C.Connected);
|
|
425
|
+
break;
|
|
426
|
+
case s.Disconnected:
|
|
427
|
+
n("LiveKit room disconnected"), I = !1, (R = a.onConnectionStateChange) == null || R.call(a, C.Disconnected);
|
|
428
|
+
break;
|
|
429
|
+
case s.Reconnecting:
|
|
430
|
+
n("LiveKit room reconnecting..."), (b = a.onConnectionStateChange) == null || b.call(a, C.Connecting);
|
|
431
|
+
break;
|
|
432
|
+
case s.SignalReconnecting:
|
|
433
|
+
n("LiveKit room signal reconnecting..."), (O = a.onConnectionStateChange) == null || O.call(a, C.Connecting);
|
|
434
|
+
break;
|
|
435
|
+
}
|
|
436
|
+
}
|
|
437
|
+
function ae(l, g) {
|
|
438
|
+
var v;
|
|
439
|
+
n("Connection quality:", l), g != null && g.isLocal && ((v = a.onConnectivityStateChange) == null || v.call(a, Ct[l]));
|
|
440
|
+
}
|
|
441
|
+
function f(l) {
|
|
442
|
+
var v, R;
|
|
443
|
+
n("Active speakers changed:", l), l[0] ? (v = a.onAgentActivityStateChange) == null || v.call(a, ie.Talking) : (R = a.onAgentActivityStateChange) == null || R.call(a, ie.Idle);
|
|
444
|
+
}
|
|
445
|
+
function D(l) {
|
|
446
|
+
n("Participant connected:", l.identity);
|
|
447
|
+
}
|
|
448
|
+
function w(l) {
|
|
449
|
+
n("Participant disconnected:", l.identity), $();
|
|
450
|
+
}
|
|
451
|
+
function L(l, g, v) {
|
|
452
|
+
var b, O, F;
|
|
453
|
+
n(`Track subscribed: ${l.kind} from ${v.identity}`);
|
|
454
|
+
const R = l.mediaStreamTrack;
|
|
455
|
+
if (!R) {
|
|
456
|
+
n(`No mediaStreamTrack available for ${l.kind}`);
|
|
457
|
+
return;
|
|
458
|
+
}
|
|
459
|
+
T ? (T.addTrack(R), n(`Added ${l.kind} track to shared MediaStream`)) : (T = new MediaStream([R]), n(`Created shared MediaStream with ${l.kind} track`)), l.kind === "video" && ((b = a.onStreamReady) == null || b.call(a), (O = a.onSrcObjectReady) == null || O.call(a, T), (F = a.onVideoStateChange) == null || F.call(a, k.Start));
|
|
460
|
+
}
|
|
461
|
+
function M(l, g, v) {
|
|
462
|
+
var R;
|
|
463
|
+
n(`Track unsubscribed: ${l.kind} from ${v.identity}`), l.kind === "video" && ((R = a.onVideoStateChange) == null || R.call(a, k.Stop));
|
|
464
|
+
}
|
|
465
|
+
function _(l, g, v, R) {
|
|
466
|
+
var O, F, de;
|
|
467
|
+
const b = new TextDecoder().decode(l);
|
|
468
|
+
n("Data received:", b);
|
|
469
|
+
try {
|
|
470
|
+
const re = JSON.parse(b), me = R || re.subject;
|
|
471
|
+
if (me === B.ChatAnswer) {
|
|
472
|
+
const oe = Y.Answer;
|
|
473
|
+
(O = a.onMessage) == null || O.call(a, oe, {
|
|
474
|
+
event: oe,
|
|
475
|
+
...re
|
|
476
|
+
});
|
|
477
|
+
} else if ([B.StreamVideoCreated, B.StreamVideoDone].includes(me)) {
|
|
478
|
+
const oe = (re == null ? void 0 : re.role) || (g == null ? void 0 : g.identity) || "datachannel";
|
|
479
|
+
(F = a.onMessage) == null || F.call(a, me, {
|
|
480
|
+
[oe]: re
|
|
481
|
+
});
|
|
482
|
+
} else if (me === B.ChatAudioTranscribed) {
|
|
483
|
+
const oe = Y.Transcribe;
|
|
484
|
+
(de = a.onMessage) == null || de.call(a, oe, {
|
|
485
|
+
event: oe,
|
|
486
|
+
...re
|
|
487
|
+
});
|
|
488
|
+
}
|
|
489
|
+
} catch (re) {
|
|
490
|
+
n("Failed to parse data channel message:", re);
|
|
491
|
+
}
|
|
492
|
+
}
|
|
493
|
+
function K(l) {
|
|
494
|
+
var g;
|
|
495
|
+
n("Media devices error:", l), (g = a.onError) == null || g.call(a, new Error(le), { sessionId: S });
|
|
496
|
+
}
|
|
497
|
+
function Q(l) {
|
|
498
|
+
var g;
|
|
499
|
+
n("Encryption error:", l), (g = a.onError) == null || g.call(a, new Error(le), { sessionId: S });
|
|
500
|
+
}
|
|
501
|
+
function X(l, g, v) {
|
|
502
|
+
n("Track subscription failed:", { trackSid: l, participant: g, reason: v });
|
|
503
|
+
}
|
|
504
|
+
async function j(l) {
|
|
505
|
+
if (!u) return null;
|
|
506
|
+
const { Track: g } = await we(), v = u.localParticipant.audioTrackPublications;
|
|
507
|
+
if (v) {
|
|
508
|
+
for (const [R, b] of v)
|
|
509
|
+
if (b.source === g.Source.Microphone && b.track) {
|
|
510
|
+
const F = b.track.mediaStreamTrack;
|
|
511
|
+
if (F === l || F && F.id === l.id)
|
|
512
|
+
return b;
|
|
513
|
+
}
|
|
514
|
+
}
|
|
515
|
+
return null;
|
|
516
|
+
}
|
|
517
|
+
function z(l) {
|
|
518
|
+
if (!A || !A.track)
|
|
519
|
+
return !1;
|
|
520
|
+
const g = A.track.mediaStreamTrack;
|
|
521
|
+
return g !== l && (g == null ? void 0 : g.id) !== l.id;
|
|
522
|
+
}
|
|
523
|
+
async function G(l) {
|
|
524
|
+
var O, F;
|
|
525
|
+
if (!I || !u)
|
|
526
|
+
throw n("Room is not connected, cannot publish microphone stream"), new Error("Room is not connected");
|
|
527
|
+
const g = l.getAudioTracks();
|
|
528
|
+
if (g.length === 0) {
|
|
529
|
+
n("No audio track found in the provided MediaStream");
|
|
530
|
+
return;
|
|
531
|
+
}
|
|
532
|
+
const v = g[0], { Track: R } = await we(), b = await j(v);
|
|
533
|
+
if (b) {
|
|
534
|
+
n("Microphone track is already published, skipping", {
|
|
535
|
+
trackId: v.id,
|
|
536
|
+
publishedTrackId: (F = (O = b.track) == null ? void 0 : O.mediaStreamTrack) == null ? void 0 : F.id
|
|
537
|
+
}), A = b;
|
|
538
|
+
return;
|
|
539
|
+
}
|
|
540
|
+
z(v) && (n("Unpublishing existing microphone track before publishing new one"), await E()), n("Publishing microphone track from provided MediaStream", { trackId: v.id });
|
|
541
|
+
try {
|
|
542
|
+
A = await u.localParticipant.publishTrack(v, {
|
|
543
|
+
source: R.Source.Microphone
|
|
544
|
+
}), n("Microphone track published successfully", { trackSid: A.trackSid });
|
|
545
|
+
} catch (de) {
|
|
546
|
+
throw n("Failed to publish microphone track:", de), de;
|
|
547
|
+
}
|
|
548
|
+
}
|
|
549
|
+
async function E() {
|
|
550
|
+
if (!(!A || !A.track))
|
|
551
|
+
try {
|
|
552
|
+
u && (await u.localParticipant.unpublishTrack(A.track), n("Microphone track unpublished"));
|
|
553
|
+
} catch (l) {
|
|
554
|
+
n("Error unpublishing microphone track:", l);
|
|
555
|
+
} finally {
|
|
556
|
+
A = null;
|
|
557
|
+
}
|
|
558
|
+
}
|
|
559
|
+
function P() {
|
|
560
|
+
T && (T.getTracks().forEach((l) => l.stop()), T = null);
|
|
561
|
+
}
|
|
562
|
+
async function Z(l, g) {
|
|
563
|
+
var v, R;
|
|
564
|
+
if (!I || !u) {
|
|
565
|
+
n("Room is not connected for sending messages"), (v = a.onError) == null || v.call(a, new Error(le), {
|
|
566
|
+
sessionId: S
|
|
567
|
+
});
|
|
568
|
+
return;
|
|
569
|
+
}
|
|
570
|
+
try {
|
|
571
|
+
await u.localParticipant.sendText(l, { topic: g }), n("Message sent successfully:", l);
|
|
572
|
+
} catch (b) {
|
|
573
|
+
n("Failed to send message:", b), (R = a.onError) == null || R.call(a, new Error(le), { sessionId: S });
|
|
574
|
+
}
|
|
575
|
+
}
|
|
576
|
+
async function h(l) {
|
|
577
|
+
var g;
|
|
578
|
+
try {
|
|
579
|
+
const R = JSON.parse(l).topic;
|
|
580
|
+
return Z("", R);
|
|
581
|
+
} catch (v) {
|
|
582
|
+
n("Failed to send data channel message:", v), (g = a.onError) == null || g.call(a, new Error(le), { sessionId: S });
|
|
583
|
+
}
|
|
584
|
+
}
|
|
585
|
+
function y(l) {
|
|
586
|
+
return Z(
|
|
587
|
+
l,
|
|
588
|
+
"lk.chat"
|
|
589
|
+
/* Chat */
|
|
590
|
+
);
|
|
591
|
+
}
|
|
592
|
+
async function $() {
|
|
593
|
+
var l, g;
|
|
594
|
+
u && (await E(), await u.disconnect()), P(), I = !1, (l = a.onConnectionStateChange) == null || l.call(a, C.Disconnected), (g = a.onAgentActivityStateChange) == null || g.call(a, ie.Idle);
|
|
595
|
+
}
|
|
596
|
+
return {
|
|
597
|
+
speak(l) {
|
|
598
|
+
const g = typeof l == "string" ? l : JSON.stringify(l);
|
|
599
|
+
return Z(
|
|
600
|
+
g,
|
|
601
|
+
"did.speak"
|
|
602
|
+
/* Speak */
|
|
603
|
+
);
|
|
604
|
+
},
|
|
605
|
+
disconnect: $,
|
|
606
|
+
async reconnect() {
|
|
607
|
+
var l, g, v;
|
|
608
|
+
if ((u == null ? void 0 : u.state) === s.Connected) {
|
|
609
|
+
n("Room is already connected");
|
|
610
|
+
return;
|
|
611
|
+
}
|
|
612
|
+
if (!u || !W || !U)
|
|
613
|
+
throw n("Cannot reconnect: missing room, URL or token"), new Error("Cannot reconnect: session not available");
|
|
614
|
+
n("Reconnecting to LiveKit room, state:", u.state), (l = a.onConnectionStateChange) == null || l.call(a, C.Connecting);
|
|
615
|
+
try {
|
|
616
|
+
if (await u.connect(W, U), n("Room reconnected"), I = !0, u.remoteParticipants.size === 0) {
|
|
617
|
+
if (n("Waiting for agent to join..."), !await new Promise((b) => {
|
|
618
|
+
const O = setTimeout(() => {
|
|
619
|
+
u == null || u.off(o.ParticipantConnected, F), b(!1);
|
|
620
|
+
}, 5e3), F = () => {
|
|
621
|
+
clearTimeout(O), u == null || u.off(o.ParticipantConnected, F), b(!0);
|
|
622
|
+
};
|
|
623
|
+
u == null || u.on(o.ParticipantConnected, F);
|
|
624
|
+
}))
|
|
625
|
+
throw n("Agent did not join within timeout"), await u.disconnect(), new Error("Agent did not rejoin the room");
|
|
626
|
+
n("Agent joined");
|
|
627
|
+
}
|
|
628
|
+
(g = a.onConnectionStateChange) == null || g.call(a, C.Connected);
|
|
629
|
+
} catch (R) {
|
|
630
|
+
throw n("Failed to reconnect:", R), (v = a.onConnectionStateChange) == null || v.call(a, C.Fail), R;
|
|
631
|
+
}
|
|
632
|
+
},
|
|
633
|
+
sendDataChannelMessage: h,
|
|
634
|
+
sendTextMessage: y,
|
|
635
|
+
publishMicrophoneStream: G,
|
|
636
|
+
unpublishMicrophoneStream: E,
|
|
637
|
+
sessionId: S,
|
|
638
|
+
streamId: S,
|
|
639
|
+
streamType: x,
|
|
640
|
+
interruptAvailable: !0,
|
|
641
|
+
triggersAvailable: !1
|
|
642
|
+
};
|
|
643
|
+
}
|
|
644
|
+
const Rt = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
|
|
645
|
+
__proto__: null,
|
|
646
|
+
DataChannelTopic: Ie,
|
|
647
|
+
createLiveKitStreamingManager: kt,
|
|
648
|
+
handleInitError: Ce
|
|
649
|
+
}, Symbol.toStringTag, { value: "Module" }));
|
|
650
|
+
function Dt(e, t, i) {
|
|
651
|
+
if (!e)
|
|
652
|
+
throw new Error("Please connect to the agent first");
|
|
653
|
+
if (!e.interruptAvailable)
|
|
654
|
+
throw new Error("Interrupt is not enabled for this stream");
|
|
655
|
+
if (t !== ne.Fluent)
|
|
656
|
+
throw new Error("Interrupt only available for Fluent streams");
|
|
657
|
+
if (!i)
|
|
658
|
+
throw new Error("No active video to interrupt");
|
|
659
|
+
}
|
|
660
|
+
async function Mt(e, t) {
|
|
661
|
+
const i = {
|
|
662
|
+
type: B.StreamInterrupt,
|
|
663
|
+
videoId: t,
|
|
664
|
+
timestamp: Date.now()
|
|
665
|
+
};
|
|
666
|
+
e.sendDataChannelMessage(JSON.stringify(i));
|
|
667
|
+
}
|
|
668
|
+
async function Et(e) {
|
|
669
|
+
const t = {
|
|
670
|
+
topic: Ie.Interrupt
|
|
671
|
+
};
|
|
672
|
+
e.sendDataChannelMessage(JSON.stringify(t));
|
|
673
|
+
}
|
|
674
|
+
function It(e) {
|
|
675
|
+
return new Promise((t, i) => {
|
|
676
|
+
const { callbacks: n, host: c, auth: o, externalId: s } = e, { onMessage: a = null, onOpen: r = null, onClose: d = null, onError: m = null } = n || {}, p = new WebSocket(`${c}?authorization=${encodeURIComponent(ze(o, s))}`);
|
|
677
|
+
p.onmessage = a, p.onclose = d, p.onerror = (u) => {
|
|
678
|
+
console.error(u), m == null || m("Websocket failed to connect", u), i(u);
|
|
679
|
+
}, p.onopen = (u) => {
|
|
680
|
+
r == null || r(u), t(p);
|
|
681
|
+
};
|
|
682
|
+
});
|
|
683
|
+
}
|
|
684
|
+
async function Tt(e) {
|
|
685
|
+
const { retries: t = 1 } = e;
|
|
686
|
+
let i = null;
|
|
687
|
+
for (let n = 0; (i == null ? void 0 : i.readyState) !== WebSocket.OPEN; n++)
|
|
688
|
+
try {
|
|
689
|
+
i = await It(e);
|
|
690
|
+
} catch (c) {
|
|
691
|
+
if (n === t)
|
|
692
|
+
throw c;
|
|
693
|
+
await xe(n * 500);
|
|
694
|
+
}
|
|
695
|
+
return i;
|
|
696
|
+
}
|
|
697
|
+
async function _t(e, t, i, n) {
|
|
698
|
+
const c = i != null && i.onMessage ? [i.onMessage] : [], o = await Tt({
|
|
699
|
+
auth: e,
|
|
700
|
+
host: t,
|
|
701
|
+
externalId: n,
|
|
702
|
+
callbacks: {
|
|
703
|
+
onError: (s) => {
|
|
704
|
+
var a;
|
|
705
|
+
return (a = i.onError) == null ? void 0 : a.call(i, new He(s));
|
|
706
|
+
},
|
|
707
|
+
onMessage(s) {
|
|
708
|
+
const a = JSON.parse(s.data);
|
|
709
|
+
c.forEach((r) => r(a.event, a));
|
|
710
|
+
}
|
|
711
|
+
}
|
|
712
|
+
});
|
|
713
|
+
return {
|
|
714
|
+
socket: o,
|
|
715
|
+
disconnect: () => o.close(),
|
|
716
|
+
subscribeToEvents: (s) => c.push(s)
|
|
717
|
+
};
|
|
718
|
+
}
|
|
719
|
+
function At(e) {
|
|
720
|
+
if (e.answer !== void 0)
|
|
721
|
+
return e.answer;
|
|
722
|
+
let t = 0, i = "";
|
|
723
|
+
for (; t in e; )
|
|
724
|
+
i += e[t++];
|
|
725
|
+
return i;
|
|
726
|
+
}
|
|
727
|
+
function Pt(e, t, i) {
|
|
728
|
+
if (!e.content)
|
|
729
|
+
return;
|
|
730
|
+
const n = {
|
|
731
|
+
id: e.id || `user-${Date.now()}`,
|
|
732
|
+
role: e.role,
|
|
733
|
+
content: e.content,
|
|
734
|
+
created_at: e.created_at || (/* @__PURE__ */ new Date()).toISOString(),
|
|
735
|
+
transcribed: !0
|
|
736
|
+
};
|
|
737
|
+
t.messages.push(n), i == null || i([...t.messages], "user");
|
|
738
|
+
}
|
|
739
|
+
function bt(e, t, i, n, c) {
|
|
740
|
+
if (e === Y.Transcribe && t.content) {
|
|
741
|
+
Pt(t, n, c);
|
|
742
|
+
return;
|
|
743
|
+
}
|
|
744
|
+
if (!(e === Y.Partial || e === Y.Answer))
|
|
745
|
+
return;
|
|
746
|
+
const o = n.messages[n.messages.length - 1];
|
|
747
|
+
let s;
|
|
748
|
+
if (o != null && o.transcribed && o.role === "user")
|
|
749
|
+
e === Y.Answer && t.content, s = {
|
|
750
|
+
id: t.id || `assistant-${Date.now()}`,
|
|
751
|
+
role: t.role || "assistant",
|
|
752
|
+
content: t.content || "",
|
|
753
|
+
created_at: t.created_at || (/* @__PURE__ */ new Date()).toISOString()
|
|
754
|
+
}, n.messages.push(s);
|
|
755
|
+
else if ((o == null ? void 0 : o.role) === "assistant")
|
|
756
|
+
s = o;
|
|
757
|
+
else
|
|
758
|
+
return;
|
|
759
|
+
const { content: a, sequence: r } = t;
|
|
760
|
+
e === Y.Partial ? i[r] = a : i.answer = a;
|
|
761
|
+
const d = At(i);
|
|
762
|
+
(s.content !== d || e === Y.Answer) && (s.content = d, c == null || c([...n.messages], e));
|
|
763
|
+
}
|
|
764
|
+
function jt(e, t, i, n, c) {
|
|
765
|
+
let o = {};
|
|
766
|
+
return {
|
|
767
|
+
clearQueue: () => o = {},
|
|
768
|
+
onMessage: (s, a) => {
|
|
769
|
+
var r, d;
|
|
770
|
+
if ("content" in a) {
|
|
771
|
+
const m = s === B.ChatAnswer ? Y.Answer : s === B.ChatAudioTranscribed ? Y.Transcribe : s;
|
|
772
|
+
bt(m, a, o, t, i.callbacks.onNewMessage), m === Y.Answer && e.track("agent-message-received", {
|
|
773
|
+
messages: t.messages.length,
|
|
774
|
+
mode: t.chatMode
|
|
775
|
+
});
|
|
776
|
+
} else {
|
|
777
|
+
const m = B, p = [m.StreamVideoDone, m.StreamVideoError, m.StreamVideoRejected], u = [m.StreamFailed, m.StreamVideoError, m.StreamVideoRejected], I = ht(a, n, { mode: t.chatMode });
|
|
778
|
+
if (s = s, s === m.StreamVideoCreated)
|
|
779
|
+
e.linkTrack("agent-video", I, m.StreamVideoCreated, ["start"]);
|
|
780
|
+
else if (p.includes(s)) {
|
|
781
|
+
const x = s.split("/")[1];
|
|
782
|
+
u.includes(s) ? e.track("agent-video", { ...I, event: x }) : e.linkTrack("agent-video", { ...I, event: x }, s, ["done"]);
|
|
783
|
+
}
|
|
784
|
+
u.includes(s) && ((d = (r = i.callbacks).onError) == null || d.call(r, new Error(`Stream failed with event ${s}`), { data: a })), a.event === m.StreamDone && c();
|
|
785
|
+
}
|
|
786
|
+
}
|
|
787
|
+
};
|
|
788
|
+
}
|
|
789
|
+
function Lt(e, t, i, n) {
|
|
790
|
+
const c = Me(e, `${t}/agents/${i}`, n);
|
|
791
|
+
return {
|
|
792
|
+
createStream(o, s) {
|
|
793
|
+
return c.post("/streams", o, { signal: s });
|
|
794
|
+
},
|
|
795
|
+
startConnection(o, s, a, r) {
|
|
796
|
+
return c.post(
|
|
797
|
+
`/streams/${o}/sdp`,
|
|
798
|
+
{
|
|
799
|
+
session_id: a,
|
|
800
|
+
answer: s
|
|
801
|
+
},
|
|
802
|
+
{ signal: r }
|
|
803
|
+
);
|
|
804
|
+
},
|
|
805
|
+
addIceCandidate(o, s, a, r) {
|
|
806
|
+
return c.post(
|
|
807
|
+
`/streams/${o}/ice`,
|
|
808
|
+
{
|
|
809
|
+
session_id: a,
|
|
810
|
+
...s
|
|
811
|
+
},
|
|
812
|
+
{ signal: r }
|
|
813
|
+
);
|
|
814
|
+
},
|
|
815
|
+
sendStreamRequest(o, s, a) {
|
|
816
|
+
return c.post(`/streams/${o}`, {
|
|
817
|
+
session_id: s,
|
|
818
|
+
...a
|
|
819
|
+
});
|
|
820
|
+
},
|
|
821
|
+
close(o, s) {
|
|
822
|
+
return c.delete(`/streams/${o}`, { session_id: s });
|
|
823
|
+
}
|
|
824
|
+
};
|
|
825
|
+
}
|
|
826
|
+
function $t(e, t, i) {
|
|
827
|
+
const n = (t.timestamp - e.timestamp) / 1e3;
|
|
828
|
+
return {
|
|
829
|
+
duration: n,
|
|
830
|
+
bytesReceived: t.bytesReceived - e.bytesReceived,
|
|
831
|
+
bitrate: Math.round((t.bytesReceived - e.bytesReceived) * 8 / n),
|
|
832
|
+
packetsReceived: t.packetsReceived - e.packetsReceived,
|
|
833
|
+
packetsLost: t.packetsLost - e.packetsLost,
|
|
834
|
+
framesDropped: t.framesDropped - e.framesDropped,
|
|
835
|
+
framesDecoded: t.framesDecoded - e.framesDecoded,
|
|
836
|
+
jitter: t.jitter,
|
|
837
|
+
avgJitterDelayInInterval: (t.jitterBufferDelay - e.jitterBufferDelay) / (t.jitterBufferEmittedCount - e.jitterBufferEmittedCount),
|
|
838
|
+
jitterBufferEmittedCount: t.jitterBufferEmittedCount - e.jitterBufferEmittedCount,
|
|
839
|
+
jitterBufferDelay: (t.jitterBufferDelay - e.jitterBufferDelay) / n,
|
|
840
|
+
framesPerSecond: t.framesPerSecond,
|
|
841
|
+
freezeCount: t.freezeCount - e.freezeCount,
|
|
842
|
+
freezeDuration: t.freezeDuration - e.freezeDuration,
|
|
843
|
+
lowFpsCount: i
|
|
844
|
+
};
|
|
845
|
+
}
|
|
846
|
+
function xt(e) {
|
|
847
|
+
return e.filter(
|
|
848
|
+
(t) => t.freezeCount > 0 || t.framesPerSecond < 21 || t.framesDropped > 0 || t.packetsLost > 0
|
|
849
|
+
).map((t) => {
|
|
850
|
+
const { timestamp: i, ...n } = t, c = [];
|
|
851
|
+
return t.freezeCount > 0 && c.push("freeze"), t.framesPerSecond < 21 && c.push("low fps"), t.framesDropped > 0 && c.push("frames dropped"), t.packetsLost > 0 && c.push("packet loss"), {
|
|
852
|
+
...n,
|
|
853
|
+
causes: c
|
|
854
|
+
};
|
|
855
|
+
});
|
|
856
|
+
}
|
|
857
|
+
function Ft(e) {
|
|
858
|
+
let t = "", i = 0;
|
|
859
|
+
for (const n of e.values()) {
|
|
860
|
+
if (n && n.type === "codec" && n.mimeType.startsWith("video") && (t = n.mimeType.split("/")[1]), n && n.type === "candidate-pair") {
|
|
861
|
+
const c = n.currentRoundTripTime, s = n.nominated === !0;
|
|
862
|
+
c > 0 && (s || i === 0) && (i = c);
|
|
863
|
+
}
|
|
864
|
+
if (n && n.type === "inbound-rtp" && n.kind === "video")
|
|
865
|
+
return {
|
|
866
|
+
codec: t,
|
|
867
|
+
rtt: i,
|
|
868
|
+
timestamp: n.timestamp,
|
|
869
|
+
bytesReceived: n.bytesReceived,
|
|
870
|
+
packetsReceived: n.packetsReceived,
|
|
871
|
+
packetsLost: n.packetsLost,
|
|
872
|
+
framesDropped: n.framesDropped,
|
|
873
|
+
framesDecoded: n.framesDecoded,
|
|
874
|
+
jitter: n.jitter,
|
|
875
|
+
jitterBufferDelay: n.jitterBufferDelay,
|
|
876
|
+
jitterBufferEmittedCount: n.jitterBufferEmittedCount,
|
|
877
|
+
avgJitterDelayInInterval: n.jitterBufferDelay / n.jitterBufferEmittedCount,
|
|
878
|
+
frameWidth: n.frameWidth,
|
|
879
|
+
frameHeight: n.frameHeight,
|
|
880
|
+
framesPerSecond: n.framesPerSecond,
|
|
881
|
+
freezeCount: n.freezeCount,
|
|
882
|
+
freezeDuration: n.totalFreezesDuration
|
|
883
|
+
};
|
|
884
|
+
}
|
|
885
|
+
return {};
|
|
886
|
+
}
|
|
887
|
+
function Bt(e, t, i) {
|
|
888
|
+
const n = e.map((r, d) => d === 0 ? i ? {
|
|
889
|
+
timestamp: r.timestamp,
|
|
890
|
+
duration: 0,
|
|
891
|
+
rtt: r.rtt,
|
|
892
|
+
bytesReceived: r.bytesReceived - i.bytesReceived,
|
|
893
|
+
bitrate: (r.bytesReceived - i.bytesReceived) * 8 / (t / 1e3),
|
|
894
|
+
packetsReceived: r.packetsReceived - i.packetsReceived,
|
|
895
|
+
packetsLost: r.packetsLost - i.packetsLost,
|
|
896
|
+
framesDropped: r.framesDropped - i.framesDropped,
|
|
897
|
+
framesDecoded: r.framesDecoded - i.framesDecoded,
|
|
898
|
+
jitter: r.jitter,
|
|
899
|
+
jitterBufferDelay: r.jitterBufferDelay - i.jitterBufferDelay,
|
|
900
|
+
jitterBufferEmittedCount: r.jitterBufferEmittedCount - i.jitterBufferEmittedCount,
|
|
901
|
+
avgJitterDelayInInterval: (r.jitterBufferDelay - i.jitterBufferDelay) / (r.jitterBufferEmittedCount - i.jitterBufferEmittedCount),
|
|
902
|
+
framesPerSecond: r.framesPerSecond,
|
|
903
|
+
freezeCount: r.freezeCount - i.freezeCount,
|
|
904
|
+
freezeDuration: r.freezeDuration - i.freezeDuration
|
|
905
|
+
} : {
|
|
906
|
+
timestamp: r.timestamp,
|
|
907
|
+
rtt: r.rtt,
|
|
908
|
+
duration: 0,
|
|
909
|
+
bytesReceived: r.bytesReceived,
|
|
910
|
+
bitrate: r.bytesReceived * 8 / (t / 1e3),
|
|
911
|
+
packetsReceived: r.packetsReceived,
|
|
912
|
+
packetsLost: r.packetsLost,
|
|
913
|
+
framesDropped: r.framesDropped,
|
|
914
|
+
framesDecoded: r.framesDecoded,
|
|
915
|
+
jitter: r.jitter,
|
|
916
|
+
jitterBufferDelay: r.jitterBufferDelay,
|
|
917
|
+
jitterBufferEmittedCount: r.jitterBufferEmittedCount,
|
|
918
|
+
avgJitterDelayInInterval: r.jitterBufferDelay / r.jitterBufferEmittedCount,
|
|
919
|
+
framesPerSecond: r.framesPerSecond,
|
|
920
|
+
freezeCount: r.freezeCount,
|
|
921
|
+
freezeDuration: r.freezeDuration
|
|
922
|
+
} : {
|
|
923
|
+
timestamp: r.timestamp,
|
|
924
|
+
duration: t * d / 1e3,
|
|
925
|
+
rtt: r.rtt,
|
|
926
|
+
bytesReceived: r.bytesReceived - e[d - 1].bytesReceived,
|
|
927
|
+
bitrate: (r.bytesReceived - e[d - 1].bytesReceived) * 8 / (t / 1e3),
|
|
928
|
+
packetsReceived: r.packetsReceived - e[d - 1].packetsReceived,
|
|
929
|
+
packetsLost: r.packetsLost - e[d - 1].packetsLost,
|
|
930
|
+
framesDropped: r.framesDropped - e[d - 1].framesDropped,
|
|
931
|
+
framesDecoded: r.framesDecoded - e[d - 1].framesDecoded,
|
|
932
|
+
jitter: r.jitter,
|
|
933
|
+
jitterBufferDelay: r.jitterBufferDelay - e[d - 1].jitterBufferDelay,
|
|
934
|
+
jitterBufferEmittedCount: r.jitterBufferEmittedCount - e[d - 1].jitterBufferEmittedCount,
|
|
935
|
+
avgJitterDelayInInterval: (r.jitterBufferDelay - e[d - 1].jitterBufferDelay) / (r.jitterBufferEmittedCount - e[d - 1].jitterBufferEmittedCount),
|
|
936
|
+
framesPerSecond: r.framesPerSecond,
|
|
937
|
+
freezeCount: r.freezeCount - e[d - 1].freezeCount,
|
|
938
|
+
freezeDuration: r.freezeDuration - e[d - 1].freezeDuration
|
|
939
|
+
}), c = xt(n), o = c.reduce((r, d) => r + (d.causes.includes("low fps") ? 1 : 0), 0), s = n.filter((r) => !!r.avgJitterDelayInInterval).map((r) => r.avgJitterDelayInInterval), a = n.filter((r) => !!r.rtt).map((r) => r.rtt);
|
|
940
|
+
return {
|
|
941
|
+
webRTCStats: {
|
|
942
|
+
anomalies: c,
|
|
943
|
+
minRtt: Math.min(...a),
|
|
944
|
+
avgRtt: _e(a),
|
|
945
|
+
maxRtt: Math.max(...a),
|
|
946
|
+
aggregateReport: $t(e[0], e[e.length - 1], o),
|
|
947
|
+
minJitterDelayInInterval: Math.min(...s),
|
|
948
|
+
maxJitterDelayInInterval: Math.max(...s),
|
|
949
|
+
avgJitterDelayInInterval: _e(s)
|
|
950
|
+
},
|
|
951
|
+
codec: e[0].codec,
|
|
952
|
+
resolution: `${e[0].frameWidth}x${e[0].frameHeight}`
|
|
953
|
+
};
|
|
954
|
+
}
|
|
955
|
+
const ke = 100, zt = Math.max(Math.ceil(400 / ke), 1), Nt = 0.25, Kt = 0.28;
|
|
956
|
+
function Jt() {
|
|
957
|
+
let e = 0, t, i, n = 0;
|
|
958
|
+
return (c) => {
|
|
959
|
+
for (const o of c.values())
|
|
960
|
+
if (o && o.type === "inbound-rtp" && o.kind === "video") {
|
|
961
|
+
const s = o.jitterBufferDelay, a = o.jitterBufferEmittedCount;
|
|
962
|
+
if (i && a > i) {
|
|
963
|
+
const m = s - t, p = a - i;
|
|
964
|
+
n = m / p;
|
|
965
|
+
}
|
|
966
|
+
t = s, i = a;
|
|
967
|
+
const r = o.framesDecoded, d = r - e > 0;
|
|
968
|
+
return e = r, { isReceiving: d, avgJitterDelayInInterval: n, freezeCount: o.freezeCount };
|
|
969
|
+
}
|
|
970
|
+
return { isReceiving: !1, avgJitterDelayInInterval: n };
|
|
971
|
+
};
|
|
972
|
+
}
|
|
973
|
+
function Ut(e, t, i, n, c) {
|
|
974
|
+
let o = [], s, a = 0, r = !1, d = ee.Unknown, m = ee.Unknown, p = 0, u = 0;
|
|
975
|
+
const I = Jt();
|
|
976
|
+
return setInterval(async () => {
|
|
977
|
+
const x = await e.getStats(), { isReceiving: J, avgJitterDelayInInterval: T, freezeCount: A } = I(x), H = Ft(x);
|
|
978
|
+
if (J)
|
|
979
|
+
a = 0, p = A - u, m = T < Nt ? ee.Strong : T > Kt && p > 1 ? ee.Weak : d, m !== d && (c == null || c(m), d = m, u += p, p = 0), r || (n == null || n(k.Start), s = o[o.length - 1], o = [], r = !0), o.push(H);
|
|
980
|
+
else if (r && (a++, a >= zt)) {
|
|
981
|
+
const S = Bt(o, ke, s);
|
|
982
|
+
n == null || n(k.Stop, S), t() || i(), u = A, r = !1;
|
|
983
|
+
}
|
|
984
|
+
}, ke);
|
|
985
|
+
}
|
|
986
|
+
const Wt = (window.RTCPeerConnection || window.webkitRTCPeerConnection || window.mozRTCPeerConnection).bind(window);
|
|
987
|
+
function be(e) {
|
|
988
|
+
switch (e) {
|
|
989
|
+
case "connected":
|
|
990
|
+
return C.Connected;
|
|
991
|
+
case "checking":
|
|
992
|
+
return C.Connecting;
|
|
993
|
+
case "failed":
|
|
994
|
+
return C.Fail;
|
|
995
|
+
case "new":
|
|
996
|
+
return C.New;
|
|
997
|
+
case "closed":
|
|
998
|
+
return C.Closed;
|
|
999
|
+
case "disconnected":
|
|
1000
|
+
return C.Disconnected;
|
|
1001
|
+
case "completed":
|
|
1002
|
+
return C.Completed;
|
|
1003
|
+
default:
|
|
1004
|
+
return C.New;
|
|
1005
|
+
}
|
|
1006
|
+
}
|
|
1007
|
+
const Vt = (e) => (t) => {
|
|
1008
|
+
const [i, n = ""] = t.split(/:(.+)/);
|
|
1009
|
+
try {
|
|
1010
|
+
const c = JSON.parse(n);
|
|
1011
|
+
return e("parsed data channel message", { subject: i, data: c }), { subject: i, data: c };
|
|
1012
|
+
} catch (c) {
|
|
1013
|
+
return e("Failed to parse data channel message, returning data as string", { subject: i, rawData: n, error: c }), { subject: i, data: n };
|
|
1014
|
+
}
|
|
1015
|
+
};
|
|
1016
|
+
function Ot({
|
|
1017
|
+
statsSignal: e,
|
|
1018
|
+
dataChannelSignal: t,
|
|
1019
|
+
onVideoStateChange: i,
|
|
1020
|
+
report: n
|
|
1021
|
+
}) {
|
|
1022
|
+
e === k.Start && t === k.Start ? i == null || i(k.Start) : e === k.Stop && t === k.Stop && (i == null || i(k.Stop, n));
|
|
1023
|
+
}
|
|
1024
|
+
function Ht({
|
|
1025
|
+
statsSignal: e,
|
|
1026
|
+
dataChannelSignal: t,
|
|
1027
|
+
onVideoStateChange: i,
|
|
1028
|
+
onAgentActivityStateChange: n,
|
|
1029
|
+
report: c
|
|
1030
|
+
}) {
|
|
1031
|
+
e === k.Start ? i == null || i(k.Start) : e === k.Stop && (i == null || i(k.Stop, c)), t === k.Start ? n == null || n(ie.Talking) : t === k.Stop && (n == null || n(ie.Idle));
|
|
1032
|
+
}
|
|
1033
|
+
function je({
|
|
1034
|
+
statsSignal: e,
|
|
1035
|
+
dataChannelSignal: t,
|
|
1036
|
+
onVideoStateChange: i,
|
|
1037
|
+
onAgentActivityStateChange: n,
|
|
1038
|
+
streamType: c,
|
|
1039
|
+
report: o
|
|
1040
|
+
}) {
|
|
1041
|
+
c === ne.Legacy ? Ot({ statsSignal: e, dataChannelSignal: t, onVideoStateChange: i, report: o }) : c === ne.Fluent && Ht({
|
|
1042
|
+
statsSignal: e,
|
|
1043
|
+
dataChannelSignal: t,
|
|
1044
|
+
onVideoStateChange: i,
|
|
1045
|
+
onAgentActivityStateChange: n,
|
|
1046
|
+
report: o
|
|
1047
|
+
});
|
|
1048
|
+
}
|
|
1049
|
+
async function qt(e, t, { debug: i = !1, callbacks: n, auth: c, baseURL: o = fe, analytics: s }, a) {
|
|
1050
|
+
var Z;
|
|
1051
|
+
const r = Je(i, "WebRTCStreamingManager"), d = Vt(r);
|
|
1052
|
+
let m = !1, p = !1, u = k.Stop, I = k.Stop;
|
|
1053
|
+
const { startConnection: x, sendStreamRequest: J, close: T, createStream: A, addIceCandidate: H } = Lt(
|
|
1054
|
+
c,
|
|
1055
|
+
o,
|
|
1056
|
+
e,
|
|
1057
|
+
n.onError
|
|
1058
|
+
), {
|
|
1059
|
+
id: S,
|
|
1060
|
+
offer: U,
|
|
1061
|
+
ice_servers: W,
|
|
1062
|
+
session_id: q,
|
|
1063
|
+
fluent: ae,
|
|
1064
|
+
interrupt_enabled: f,
|
|
1065
|
+
triggers_enabled: D
|
|
1066
|
+
} = await A(t, a);
|
|
1067
|
+
(Z = n.onStreamCreated) == null || Z.call(n, { stream_id: S, session_id: q, agent_id: e });
|
|
1068
|
+
const w = new Wt({ iceServers: W }), L = w.createDataChannel("JanusDataChannel");
|
|
1069
|
+
if (!q)
|
|
1070
|
+
throw new Error("Could not create session_id");
|
|
1071
|
+
const M = ae ? ne.Fluent : ne.Legacy;
|
|
1072
|
+
s.enrich({
|
|
1073
|
+
"stream-type": M
|
|
1074
|
+
});
|
|
1075
|
+
const _ = t.stream_warmup && !ae, K = () => m, Q = () => {
|
|
1076
|
+
var h;
|
|
1077
|
+
m = !0, p && ((h = n.onConnectionStateChange) == null || h.call(n, C.Connected));
|
|
1078
|
+
}, X = Ut(
|
|
1079
|
+
w,
|
|
1080
|
+
K,
|
|
1081
|
+
Q,
|
|
1082
|
+
(h, y) => je({
|
|
1083
|
+
statsSignal: I = h,
|
|
1084
|
+
dataChannelSignal: M === ne.Legacy ? u : void 0,
|
|
1085
|
+
onVideoStateChange: n.onVideoStateChange,
|
|
1086
|
+
onAgentActivityStateChange: n.onAgentActivityStateChange,
|
|
1087
|
+
report: y,
|
|
1088
|
+
streamType: M
|
|
1089
|
+
}),
|
|
1090
|
+
(h) => {
|
|
1091
|
+
var y;
|
|
1092
|
+
return (y = n.onConnectivityStateChange) == null ? void 0 : y.call(n, h);
|
|
1093
|
+
}
|
|
1094
|
+
);
|
|
1095
|
+
w.onicecandidate = (h) => {
|
|
1096
|
+
var y;
|
|
1097
|
+
r("peerConnection.onicecandidate", h);
|
|
1098
|
+
try {
|
|
1099
|
+
h.candidate && h.candidate.sdpMid && h.candidate.sdpMLineIndex !== null ? H(
|
|
1100
|
+
S,
|
|
1101
|
+
{
|
|
1102
|
+
candidate: h.candidate.candidate,
|
|
1103
|
+
sdpMid: h.candidate.sdpMid,
|
|
1104
|
+
sdpMLineIndex: h.candidate.sdpMLineIndex
|
|
1105
|
+
},
|
|
1106
|
+
q,
|
|
1107
|
+
a
|
|
1108
|
+
) : H(S, { candidate: null }, q, a);
|
|
1109
|
+
} catch ($) {
|
|
1110
|
+
(y = n.onError) == null || y.call(n, $, { streamId: S });
|
|
1111
|
+
}
|
|
1112
|
+
}, L.onopen = () => {
|
|
1113
|
+
p = !0, (!_ || m) && Q();
|
|
1114
|
+
};
|
|
1115
|
+
const j = (h) => {
|
|
1116
|
+
var y;
|
|
1117
|
+
(y = n.onVideoIdChange) == null || y.call(n, h);
|
|
1118
|
+
};
|
|
1119
|
+
function z(h, y) {
|
|
1120
|
+
if (h === B.StreamStarted && typeof y == "object" && "metadata" in y) {
|
|
1121
|
+
const $ = y.metadata;
|
|
1122
|
+
j($.videoId);
|
|
1123
|
+
}
|
|
1124
|
+
h === B.StreamDone && j(null), u = h === B.StreamStarted ? k.Start : k.Stop, je({
|
|
1125
|
+
statsSignal: M === ne.Legacy ? I : void 0,
|
|
1126
|
+
dataChannelSignal: u,
|
|
1127
|
+
onVideoStateChange: n.onVideoStateChange,
|
|
1128
|
+
onAgentActivityStateChange: n.onAgentActivityStateChange,
|
|
1129
|
+
streamType: M
|
|
1130
|
+
});
|
|
1131
|
+
}
|
|
1132
|
+
function G(h, y) {
|
|
1133
|
+
var V;
|
|
1134
|
+
const $ = typeof y == "string" ? y : y == null ? void 0 : y.metadata;
|
|
1135
|
+
$ && s.enrich({ streamMetadata: $ }), (V = n.onStreamReady) == null || V.call(n);
|
|
1136
|
+
}
|
|
1137
|
+
const E = {
|
|
1138
|
+
[B.StreamStarted]: z,
|
|
1139
|
+
[B.StreamDone]: z,
|
|
1140
|
+
[B.StreamReady]: G
|
|
1141
|
+
};
|
|
1142
|
+
L.onmessage = (h) => {
|
|
1143
|
+
var V;
|
|
1144
|
+
const { subject: y, data: $ } = d(h.data);
|
|
1145
|
+
(V = E[y]) == null || V.call(E, y, $);
|
|
1146
|
+
}, w.oniceconnectionstatechange = () => {
|
|
1147
|
+
var y;
|
|
1148
|
+
r("peerConnection.oniceconnectionstatechange => " + w.iceConnectionState);
|
|
1149
|
+
const h = be(w.iceConnectionState);
|
|
1150
|
+
h !== C.Connected && ((y = n.onConnectionStateChange) == null || y.call(n, h));
|
|
1151
|
+
}, w.ontrack = (h) => {
|
|
1152
|
+
var y;
|
|
1153
|
+
r("peerConnection.ontrack", h), (y = n.onSrcObjectReady) == null || y.call(n, h.streams[0]);
|
|
1154
|
+
}, await w.setRemoteDescription(U), r("set remote description OK");
|
|
1155
|
+
const P = await w.createAnswer();
|
|
1156
|
+
return r("create answer OK"), await w.setLocalDescription(P), r("set local description OK"), await x(S, P, q, a), r("start connection OK"), {
|
|
1157
|
+
/**
|
|
1158
|
+
* Method to send request to server to get clip or talk depend on you payload
|
|
1159
|
+
* @param payload
|
|
1160
|
+
*/
|
|
1161
|
+
speak(h) {
|
|
1162
|
+
return J(S, q, h);
|
|
1163
|
+
},
|
|
1164
|
+
/**
|
|
1165
|
+
* Method to close RTC connection
|
|
1166
|
+
*/
|
|
1167
|
+
async disconnect() {
|
|
1168
|
+
var h;
|
|
1169
|
+
if (S) {
|
|
1170
|
+
const y = be(w.iceConnectionState);
|
|
1171
|
+
if (w) {
|
|
1172
|
+
if (y === C.New) {
|
|
1173
|
+
clearInterval(X);
|
|
1174
|
+
return;
|
|
1175
|
+
}
|
|
1176
|
+
w.close(), w.oniceconnectionstatechange = null, w.onnegotiationneeded = null, w.onicecandidate = null, w.ontrack = null;
|
|
1177
|
+
}
|
|
1178
|
+
try {
|
|
1179
|
+
y === C.Connected && await T(S, q).catch(($) => {
|
|
1180
|
+
});
|
|
1181
|
+
} catch ($) {
|
|
1182
|
+
r("Error on close stream connection", $);
|
|
1183
|
+
}
|
|
1184
|
+
(h = n.onAgentActivityStateChange) == null || h.call(n, ie.Idle), clearInterval(X);
|
|
1185
|
+
}
|
|
1186
|
+
},
|
|
1187
|
+
/**
|
|
1188
|
+
* Method to send data channel messages to the server
|
|
1189
|
+
*/
|
|
1190
|
+
sendDataChannelMessage(h) {
|
|
1191
|
+
var y, $;
|
|
1192
|
+
if (!m || L.readyState !== "open") {
|
|
1193
|
+
r("Data channel is not ready for sending messages"), (y = n.onError) == null || y.call(n, new Error("Data channel is not ready for sending messages"), {
|
|
1194
|
+
streamId: S
|
|
1195
|
+
});
|
|
1196
|
+
return;
|
|
1197
|
+
}
|
|
1198
|
+
try {
|
|
1199
|
+
L.send(h);
|
|
1200
|
+
} catch (V) {
|
|
1201
|
+
r("Error sending data channel message", V), ($ = n.onError) == null || $.call(n, V, { streamId: S });
|
|
1202
|
+
}
|
|
1203
|
+
},
|
|
1204
|
+
/**
|
|
1205
|
+
* Session identifier information, should be returned in the body of all streaming requests
|
|
1206
|
+
*/
|
|
1207
|
+
sessionId: q,
|
|
1208
|
+
/**
|
|
1209
|
+
* Id of current RTC stream
|
|
1210
|
+
*/
|
|
1211
|
+
streamId: S,
|
|
1212
|
+
streamType: M,
|
|
1213
|
+
interruptAvailable: f ?? !1,
|
|
1214
|
+
triggersAvailable: D ?? !1
|
|
1215
|
+
};
|
|
1216
|
+
}
|
|
1217
|
+
var Re = /* @__PURE__ */ ((e) => (e.V1 = "v1", e.V2 = "v2", e))(Re || {});
|
|
1218
|
+
async function Qt(e, t, i, n) {
|
|
1219
|
+
const c = e.id;
|
|
1220
|
+
switch (t.version) {
|
|
1221
|
+
case "v1": {
|
|
1222
|
+
const { version: o, ...s } = t;
|
|
1223
|
+
return qt(c, s, i, n);
|
|
1224
|
+
}
|
|
1225
|
+
case "v2": {
|
|
1226
|
+
const { version: o, ...s } = t;
|
|
1227
|
+
switch (s.transport_provider) {
|
|
1228
|
+
case he.Livekit:
|
|
1229
|
+
const { createLiveKitStreamingManager: a } = await Promise.resolve().then(() => Rt);
|
|
1230
|
+
return a(c, s, i);
|
|
1231
|
+
default:
|
|
1232
|
+
throw new Error(`Unsupported transport provider: ${s.transport_provider}`);
|
|
1233
|
+
}
|
|
1234
|
+
}
|
|
1235
|
+
default:
|
|
1236
|
+
throw new Error(`Invalid stream version: ${t.version}`);
|
|
1237
|
+
}
|
|
1238
|
+
}
|
|
1239
|
+
const Xt = "cht";
|
|
1240
|
+
function Yt() {
|
|
1241
|
+
return {
|
|
1242
|
+
transport_provider: he.Livekit
|
|
1243
|
+
};
|
|
1244
|
+
}
|
|
1245
|
+
function Zt(e) {
|
|
1246
|
+
var c, o;
|
|
1247
|
+
const { streamOptions: t } = e ?? {}, i = ((c = e == null ? void 0 : e.mixpanelAdditionalProperties) == null ? void 0 : c.plan) !== void 0 ? {
|
|
1248
|
+
plan: (o = e.mixpanelAdditionalProperties) == null ? void 0 : o.plan
|
|
1249
|
+
} : void 0;
|
|
1250
|
+
return { ...{
|
|
1251
|
+
output_resolution: t == null ? void 0 : t.outputResolution,
|
|
1252
|
+
session_timeout: t == null ? void 0 : t.sessionTimeout,
|
|
1253
|
+
stream_warmup: t == null ? void 0 : t.streamWarmup,
|
|
1254
|
+
compatibility_mode: t == null ? void 0 : t.compatibilityMode,
|
|
1255
|
+
fluent: t == null ? void 0 : t.fluent
|
|
1256
|
+
}, ...i && { end_user_data: i } };
|
|
1257
|
+
}
|
|
1258
|
+
function Gt(e, t) {
|
|
1259
|
+
return De(e.presenter.type) ? { version: Re.V2, ...Yt() } : { version: Re.V1, ...Zt(t) };
|
|
1260
|
+
}
|
|
1261
|
+
function en(e, t, i, n, c) {
|
|
1262
|
+
c === ne.Fluent ? tn(e, t, i, n, c) : rn(e, t, i, n, c);
|
|
1263
|
+
}
|
|
1264
|
+
function tn(e, t, i, n, c) {
|
|
1265
|
+
e === k.Start ? n.track("stream-session", { event: "start", "stream-type": c }) : e === k.Stop && n.track("stream-session", {
|
|
1266
|
+
event: "stop",
|
|
1267
|
+
is_greenscreen: t.presenter.type === "clip" && t.presenter.is_greenscreen,
|
|
1268
|
+
background: t.presenter.type === "clip" && t.presenter.background,
|
|
1269
|
+
"stream-type": c,
|
|
1270
|
+
...i
|
|
1271
|
+
});
|
|
1272
|
+
}
|
|
1273
|
+
function nn(e, t, i, n) {
|
|
1274
|
+
te.get() <= 0 || (e === k.Start ? i.linkTrack(
|
|
1275
|
+
"agent-video",
|
|
1276
|
+
{ event: "start", latency: te.get(!0), "stream-type": n },
|
|
1277
|
+
"start",
|
|
1278
|
+
[B.StreamVideoCreated]
|
|
1279
|
+
) : e === k.Stop && i.linkTrack(
|
|
1280
|
+
"agent-video",
|
|
1281
|
+
{
|
|
1282
|
+
event: "stop",
|
|
1283
|
+
is_greenscreen: t.presenter.type === "clip" && t.presenter.is_greenscreen,
|
|
1284
|
+
background: t.presenter.type === "clip" && t.presenter.background,
|
|
1285
|
+
"stream-type": n
|
|
1286
|
+
},
|
|
1287
|
+
"done",
|
|
1288
|
+
[B.StreamVideoDone]
|
|
1289
|
+
));
|
|
1290
|
+
}
|
|
1291
|
+
function rn(e, t, i, n, c) {
|
|
1292
|
+
te.get() <= 0 || (e === k.Start ? n.linkTrack(
|
|
1293
|
+
"agent-video",
|
|
1294
|
+
{ event: "start", latency: te.get(!0), "stream-type": c },
|
|
1295
|
+
"start",
|
|
1296
|
+
[B.StreamVideoCreated]
|
|
1297
|
+
) : e === k.Stop && n.linkTrack(
|
|
1298
|
+
"agent-video",
|
|
1299
|
+
{
|
|
1300
|
+
event: "stop",
|
|
1301
|
+
is_greenscreen: t.presenter.type === "clip" && t.presenter.is_greenscreen,
|
|
1302
|
+
background: t.presenter.type === "clip" && t.presenter.background,
|
|
1303
|
+
"stream-type": c,
|
|
1304
|
+
...i
|
|
1305
|
+
},
|
|
1306
|
+
"done",
|
|
1307
|
+
[B.StreamVideoDone]
|
|
1308
|
+
));
|
|
1309
|
+
}
|
|
1310
|
+
function Le(e, t, i, n) {
|
|
1311
|
+
return te.reset(), Pe.update(), new Promise(async (c, o) => {
|
|
1312
|
+
try {
|
|
1313
|
+
let s, a = !1;
|
|
1314
|
+
const r = Gt(e, t);
|
|
1315
|
+
i.enrich({
|
|
1316
|
+
"stream-version": r.version.toString()
|
|
1317
|
+
}), s = await Qt(
|
|
1318
|
+
e,
|
|
1319
|
+
r,
|
|
1320
|
+
{
|
|
1321
|
+
...t,
|
|
1322
|
+
analytics: i,
|
|
1323
|
+
callbacks: {
|
|
1324
|
+
...t.callbacks,
|
|
1325
|
+
onConnectionStateChange: (d) => {
|
|
1326
|
+
var m, p;
|
|
1327
|
+
(p = (m = t.callbacks).onConnectionStateChange) == null || p.call(m, d), d === C.Connected && (s ? c(s) : a = !0);
|
|
1328
|
+
},
|
|
1329
|
+
onVideoStateChange: (d, m) => {
|
|
1330
|
+
var p, u;
|
|
1331
|
+
(u = (p = t.callbacks).onVideoStateChange) == null || u.call(p, d), en(
|
|
1332
|
+
d,
|
|
1333
|
+
e,
|
|
1334
|
+
m,
|
|
1335
|
+
i,
|
|
1336
|
+
s.streamType
|
|
1337
|
+
);
|
|
1338
|
+
},
|
|
1339
|
+
onAgentActivityStateChange: (d) => {
|
|
1340
|
+
var m, p;
|
|
1341
|
+
(p = (m = t.callbacks).onAgentActivityStateChange) == null || p.call(m, d), d === ie.Talking ? Se.update() : Se.reset(), nn(
|
|
1342
|
+
d === ie.Talking ? k.Start : k.Stop,
|
|
1343
|
+
e,
|
|
1344
|
+
i,
|
|
1345
|
+
s.streamType
|
|
1346
|
+
);
|
|
1347
|
+
},
|
|
1348
|
+
onStreamReady: () => {
|
|
1349
|
+
const d = Pe.get(!0);
|
|
1350
|
+
i.track("agent-chat", { event: "ready", latency: d });
|
|
1351
|
+
}
|
|
1352
|
+
}
|
|
1353
|
+
},
|
|
1354
|
+
n
|
|
1355
|
+
), a && c(s);
|
|
1356
|
+
} catch (s) {
|
|
1357
|
+
o(s);
|
|
1358
|
+
}
|
|
1359
|
+
});
|
|
1360
|
+
}
|
|
1361
|
+
async function an(e, t, i, n, c) {
|
|
1362
|
+
var m, p, u, I;
|
|
1363
|
+
const o = async () => {
|
|
1364
|
+
if (De(e.presenter.type)) {
|
|
1365
|
+
const x = await Le(e, t, n), J = `${Xt}_${x.sessionId}`, T = (/* @__PURE__ */ new Date()).toISOString();
|
|
1366
|
+
return { chatResult: {
|
|
1367
|
+
chatMode: N.Functional,
|
|
1368
|
+
chat: {
|
|
1369
|
+
id: J,
|
|
1370
|
+
agent_id: e.id,
|
|
1371
|
+
owner_id: e.owner_id ?? "",
|
|
1372
|
+
created: T,
|
|
1373
|
+
modified: T,
|
|
1374
|
+
agent_id__created_at: T,
|
|
1375
|
+
agent_id__modified_at: T,
|
|
1376
|
+
chat_mode: N.Functional,
|
|
1377
|
+
messages: []
|
|
1378
|
+
}
|
|
1379
|
+
}, streamingManager: x };
|
|
1380
|
+
} else {
|
|
1381
|
+
const x = new AbortController(), J = x.signal;
|
|
1382
|
+
let T;
|
|
1383
|
+
try {
|
|
1384
|
+
const A = Ke(
|
|
1385
|
+
e,
|
|
1386
|
+
i,
|
|
1387
|
+
n,
|
|
1388
|
+
t.mode,
|
|
1389
|
+
t.persistentChat,
|
|
1390
|
+
c
|
|
1391
|
+
), H = Le(e, t, n, J).then((W) => (T = W, W)), [S, U] = await Promise.all([A, H]);
|
|
1392
|
+
return { chatResult: S, streamingManager: U };
|
|
1393
|
+
} catch (A) {
|
|
1394
|
+
throw x.abort(), T && await T.disconnect().catch(() => {
|
|
1395
|
+
}), A;
|
|
1396
|
+
}
|
|
1397
|
+
}
|
|
1398
|
+
}, { chatResult: s, streamingManager: a } = await o(), { chat: r, chatMode: d } = s;
|
|
1399
|
+
return d && t.mode !== void 0 && d !== t.mode && (t.mode = d, (p = (m = t.callbacks).onModeChange) == null || p.call(m, d), d !== N.Functional) ? ((I = (u = t.callbacks).onError) == null || I.call(u, new Oe(d)), a == null || a.disconnect(), { chat: r }) : { chat: r, streamingManager: a };
|
|
1400
|
+
}
|
|
1401
|
+
async function cn(e, t) {
|
|
1402
|
+
var W, q, ae;
|
|
1403
|
+
let i = !0, n = null;
|
|
1404
|
+
const c = t.mixpanelKey || ot, o = t.wsURL || at, s = t.baseURL || fe, a = t.mode || N.Functional, r = {
|
|
1405
|
+
messages: [],
|
|
1406
|
+
chatMode: a
|
|
1407
|
+
}, d = wt({
|
|
1408
|
+
token: c,
|
|
1409
|
+
agentId: e,
|
|
1410
|
+
isEnabled: t.enableAnalitics,
|
|
1411
|
+
externalId: t.externalId,
|
|
1412
|
+
mixpanelAdditionalProperties: t.mixpanelAdditionalProperties
|
|
1413
|
+
}), m = Date.now();
|
|
1414
|
+
Ae(() => {
|
|
1415
|
+
d.track("agent-sdk", { event: "init" }, m);
|
|
1416
|
+
});
|
|
1417
|
+
const p = lt(t.auth, s, t.callbacks.onError, t.externalId), u = await p.getById(e), I = De(u.presenter.type);
|
|
1418
|
+
d.enrich(mt(u));
|
|
1419
|
+
const { onMessage: x, clearQueue: J } = jt(
|
|
1420
|
+
d,
|
|
1421
|
+
r,
|
|
1422
|
+
t,
|
|
1423
|
+
u,
|
|
1424
|
+
() => {
|
|
1425
|
+
var f;
|
|
1426
|
+
return (f = r.socketManager) == null ? void 0 : f.disconnect();
|
|
1427
|
+
}
|
|
1428
|
+
);
|
|
1429
|
+
r.messages = vt(t.initialMessages), (q = (W = t.callbacks).onNewMessage) == null || q.call(W, [...r.messages], "answer");
|
|
1430
|
+
const T = (f) => {
|
|
1431
|
+
n = f;
|
|
1432
|
+
}, A = Date.now();
|
|
1433
|
+
Ae(() => {
|
|
1434
|
+
d.track("agent-sdk", { event: "loaded", ...ft(u) }, A);
|
|
1435
|
+
});
|
|
1436
|
+
async function H(f) {
|
|
1437
|
+
var K, Q, X, j, z, G, E;
|
|
1438
|
+
(Q = (K = t.callbacks).onConnectionStateChange) == null || Q.call(K, C.Connecting), te.reset(), f && !i && (delete r.chat, (j = (X = t.callbacks).onNewMessage) == null || j.call(X, [...r.messages], "answer"));
|
|
1439
|
+
const D = a === N.DirectPlayback || I ? Promise.resolve(void 0) : _t(
|
|
1440
|
+
t.auth,
|
|
1441
|
+
o,
|
|
1442
|
+
{ onMessage: x, onError: t.callbacks.onError },
|
|
1443
|
+
t.externalId
|
|
1444
|
+
), w = ye(
|
|
1445
|
+
() => an(
|
|
1446
|
+
u,
|
|
1447
|
+
{
|
|
1448
|
+
...t,
|
|
1449
|
+
mode: a,
|
|
1450
|
+
callbacks: { ...t.callbacks, onVideoIdChange: T, onMessage: x }
|
|
1451
|
+
},
|
|
1452
|
+
p,
|
|
1453
|
+
d,
|
|
1454
|
+
r.chat
|
|
1455
|
+
),
|
|
1456
|
+
{
|
|
1457
|
+
limit: 3,
|
|
1458
|
+
timeout: rt,
|
|
1459
|
+
timeoutErrorMessage: "Timeout initializing the stream",
|
|
1460
|
+
shouldRetryFn: (P) => (P == null ? void 0 : P.message) !== "Could not connect" && P.status !== 429 && (P == null ? void 0 : P.message) !== "InsufficientCreditsError",
|
|
1461
|
+
delayMs: 1e3
|
|
1462
|
+
}
|
|
1463
|
+
).catch((P) => {
|
|
1464
|
+
var Z, h;
|
|
1465
|
+
throw U(N.Maintenance), (h = (Z = t.callbacks).onConnectionStateChange) == null || h.call(Z, C.Fail), P;
|
|
1466
|
+
}), [L, { streamingManager: M, chat: _ }] = await Promise.all([D, w]);
|
|
1467
|
+
_ && _.id !== ((z = r.chat) == null ? void 0 : z.id) && ((E = (G = t.callbacks).onNewChat) == null || E.call(G, _.id)), r.streamingManager = M, r.socketManager = L, r.chat = _, i = !1, d.enrich({
|
|
1468
|
+
chatId: _ == null ? void 0 : _.id,
|
|
1469
|
+
streamId: M == null ? void 0 : M.streamId,
|
|
1470
|
+
mode: r.chatMode
|
|
1471
|
+
}), U((_ == null ? void 0 : _.chat_mode) ?? a);
|
|
1472
|
+
}
|
|
1473
|
+
async function S() {
|
|
1474
|
+
var f, D, w, L;
|
|
1475
|
+
(f = r.socketManager) == null || f.disconnect(), await ((D = r.streamingManager) == null ? void 0 : D.disconnect()), delete r.streamingManager, delete r.socketManager, (L = (w = t.callbacks).onConnectionStateChange) == null || L.call(w, C.Disconnected);
|
|
1476
|
+
}
|
|
1477
|
+
async function U(f) {
|
|
1478
|
+
var D, w;
|
|
1479
|
+
f !== r.chatMode && (d.track("agent-mode-change", { mode: f }), r.chatMode = f, r.chatMode !== N.Functional && await S(), (w = (D = t.callbacks).onModeChange) == null || w.call(D, f));
|
|
1480
|
+
}
|
|
1481
|
+
return {
|
|
1482
|
+
agent: u,
|
|
1483
|
+
getStreamType: () => {
|
|
1484
|
+
var f;
|
|
1485
|
+
return (f = r.streamingManager) == null ? void 0 : f.streamType;
|
|
1486
|
+
},
|
|
1487
|
+
getIsInterruptAvailable: () => {
|
|
1488
|
+
var f;
|
|
1489
|
+
return ((f = r.streamingManager) == null ? void 0 : f.interruptAvailable) ?? !1;
|
|
1490
|
+
},
|
|
1491
|
+
getIsTriggersAvailable: () => {
|
|
1492
|
+
var f;
|
|
1493
|
+
return ((f = r.streamingManager) == null ? void 0 : f.triggersAvailable) ?? !1;
|
|
1494
|
+
},
|
|
1495
|
+
starterMessages: ((ae = u.knowledge) == null ? void 0 : ae.starter_message) || [],
|
|
1496
|
+
getSTTToken: () => p.getSTTToken(u.id),
|
|
1497
|
+
changeMode: U,
|
|
1498
|
+
enrichAnalytics: d.enrich,
|
|
1499
|
+
async connect() {
|
|
1500
|
+
await H(!0), d.track("agent-chat", {
|
|
1501
|
+
event: "connect",
|
|
1502
|
+
mode: r.chatMode
|
|
1503
|
+
});
|
|
1504
|
+
},
|
|
1505
|
+
async reconnect() {
|
|
1506
|
+
const f = r.streamingManager;
|
|
1507
|
+
if (I && (f != null && f.reconnect)) {
|
|
1508
|
+
try {
|
|
1509
|
+
await f.reconnect(), d.track("agent-chat", {
|
|
1510
|
+
event: "reconnect",
|
|
1511
|
+
mode: r.chatMode
|
|
1512
|
+
});
|
|
1513
|
+
} catch {
|
|
1514
|
+
await S(), await H(!1);
|
|
1515
|
+
}
|
|
1516
|
+
return;
|
|
1517
|
+
}
|
|
1518
|
+
await S(), await H(!1), d.track("agent-chat", {
|
|
1519
|
+
event: "reconnect",
|
|
1520
|
+
mode: r.chatMode
|
|
1521
|
+
});
|
|
1522
|
+
},
|
|
1523
|
+
async disconnect() {
|
|
1524
|
+
await S(), d.track("agent-chat", {
|
|
1525
|
+
event: "disconnect",
|
|
1526
|
+
mode: r.chatMode
|
|
1527
|
+
});
|
|
1528
|
+
},
|
|
1529
|
+
async publishMicrophoneStream(f) {
|
|
1530
|
+
var D;
|
|
1531
|
+
if (!((D = r.streamingManager) != null && D.publishMicrophoneStream))
|
|
1532
|
+
throw new Error("publishMicrophoneStream is not available for this streaming manager");
|
|
1533
|
+
return r.streamingManager.publishMicrophoneStream(f);
|
|
1534
|
+
},
|
|
1535
|
+
async unpublishMicrophoneStream() {
|
|
1536
|
+
var f;
|
|
1537
|
+
if (!((f = r.streamingManager) != null && f.unpublishMicrophoneStream))
|
|
1538
|
+
throw new Error("unpublishMicrophoneStream is not available for this streaming manager");
|
|
1539
|
+
return r.streamingManager.unpublishMicrophoneStream();
|
|
1540
|
+
},
|
|
1541
|
+
async chat(f) {
|
|
1542
|
+
var M, _, K, Q, X;
|
|
1543
|
+
const D = () => {
|
|
1544
|
+
if (Be(a))
|
|
1545
|
+
throw new se(`${a} is enabled, chat is disabled`);
|
|
1546
|
+
if (f.length >= 800)
|
|
1547
|
+
throw new se("Message cannot be more than 800 characters");
|
|
1548
|
+
if (f.length === 0)
|
|
1549
|
+
throw new se("Message cannot be empty");
|
|
1550
|
+
if (r.chatMode === N.Maintenance)
|
|
1551
|
+
throw new se("Chat is in maintenance mode");
|
|
1552
|
+
if (![N.TextOnly, N.Playground].includes(r.chatMode)) {
|
|
1553
|
+
if (!r.streamingManager)
|
|
1554
|
+
throw new se("Streaming manager is not initialized");
|
|
1555
|
+
if (!r.chat)
|
|
1556
|
+
throw new se("Chat is not initialized");
|
|
1557
|
+
}
|
|
1558
|
+
}, w = async () => {
|
|
1559
|
+
var j, z;
|
|
1560
|
+
if (!r.chat) {
|
|
1561
|
+
const G = await Ke(
|
|
1562
|
+
u,
|
|
1563
|
+
p,
|
|
1564
|
+
d,
|
|
1565
|
+
r.chatMode,
|
|
1566
|
+
t.persistentChat
|
|
1567
|
+
);
|
|
1568
|
+
if (!G.chat)
|
|
1569
|
+
throw new Ve(r.chatMode, !!t.persistentChat);
|
|
1570
|
+
r.chat = G.chat, (z = (j = t.callbacks).onNewChat) == null || z.call(j, r.chat.id);
|
|
1571
|
+
}
|
|
1572
|
+
return r.chat.id;
|
|
1573
|
+
}, L = async (j, z) => ye(I ? async () => {
|
|
1574
|
+
var E, P;
|
|
1575
|
+
return await ((P = (E = r.streamingManager) == null ? void 0 : E.sendTextMessage) == null ? void 0 : P.call(E, f)), Promise.resolve({});
|
|
1576
|
+
} : async () => {
|
|
1577
|
+
var E, P;
|
|
1578
|
+
return p.chat(
|
|
1579
|
+
u.id,
|
|
1580
|
+
z,
|
|
1581
|
+
{
|
|
1582
|
+
chatMode: r.chatMode,
|
|
1583
|
+
streamId: (E = r.streamingManager) == null ? void 0 : E.streamId,
|
|
1584
|
+
sessionId: (P = r.streamingManager) == null ? void 0 : P.sessionId,
|
|
1585
|
+
messages: j.map(({ matches: Z, ...h }) => h)
|
|
1586
|
+
},
|
|
1587
|
+
{
|
|
1588
|
+
...Ne(r.chatMode),
|
|
1589
|
+
skipErrorHandler: !0
|
|
1590
|
+
}
|
|
1591
|
+
);
|
|
1592
|
+
}, {
|
|
1593
|
+
limit: 2,
|
|
1594
|
+
shouldRetryFn: (E) => {
|
|
1595
|
+
var h, y, $, V;
|
|
1596
|
+
const P = (h = E == null ? void 0 : E.message) == null ? void 0 : h.includes("missing or invalid session_id");
|
|
1597
|
+
return !((y = E == null ? void 0 : E.message) == null ? void 0 : y.includes("Stream Error")) && !P ? ((V = ($ = t.callbacks).onError) == null || V.call($, E), !1) : !0;
|
|
1598
|
+
},
|
|
1599
|
+
onRetry: async () => {
|
|
1600
|
+
await S(), await H(!1);
|
|
1601
|
+
}
|
|
1602
|
+
});
|
|
1603
|
+
try {
|
|
1604
|
+
J(), D(), r.messages.push({
|
|
1605
|
+
id: ce(),
|
|
1606
|
+
role: "user",
|
|
1607
|
+
content: f,
|
|
1608
|
+
created_at: new Date(te.update()).toISOString()
|
|
1609
|
+
}), (_ = (M = t.callbacks).onNewMessage) == null || _.call(M, [...r.messages], "user");
|
|
1610
|
+
const j = await w(), z = await L([...r.messages], j);
|
|
1611
|
+
return r.messages.push({
|
|
1612
|
+
id: ce(),
|
|
1613
|
+
role: "assistant",
|
|
1614
|
+
content: z.result || "",
|
|
1615
|
+
created_at: (/* @__PURE__ */ new Date()).toISOString(),
|
|
1616
|
+
context: z.context,
|
|
1617
|
+
matches: z.matches
|
|
1618
|
+
}), d.track("agent-message-send", {
|
|
1619
|
+
event: "success",
|
|
1620
|
+
messages: r.messages.length + 1
|
|
1621
|
+
}), z.result && ((Q = (K = t.callbacks).onNewMessage) == null || Q.call(K, [...r.messages], "answer"), d.track("agent-message-received", {
|
|
1622
|
+
latency: te.get(!0),
|
|
1623
|
+
messages: r.messages.length
|
|
1624
|
+
})), z;
|
|
1625
|
+
} catch (j) {
|
|
1626
|
+
throw ((X = r.messages[r.messages.length - 1]) == null ? void 0 : X.role) === "assistant" && r.messages.pop(), d.track("agent-message-send", {
|
|
1627
|
+
event: "error",
|
|
1628
|
+
messages: r.messages.length
|
|
1629
|
+
}), j;
|
|
1630
|
+
}
|
|
1631
|
+
},
|
|
1632
|
+
rate(f, D, w) {
|
|
1633
|
+
var _, K, Q, X;
|
|
1634
|
+
const L = r.messages.find((j) => j.id === f);
|
|
1635
|
+
if (r.chat) {
|
|
1636
|
+
if (!L)
|
|
1637
|
+
throw new Error("Message not found");
|
|
1638
|
+
} else throw new Error("Chat is not initialized");
|
|
1639
|
+
const M = ((_ = L.matches) == null ? void 0 : _.map((j) => [j.document_id, j.id])) ?? [];
|
|
1640
|
+
return d.track("agent-rate", {
|
|
1641
|
+
event: w ? "update" : "create",
|
|
1642
|
+
thumb: D === 1 ? "up" : "down",
|
|
1643
|
+
knowledge_id: ((K = u.knowledge) == null ? void 0 : K.id) ?? "",
|
|
1644
|
+
matches: M,
|
|
1645
|
+
score: D
|
|
1646
|
+
}), w ? p.updateRating(u.id, r.chat.id, w, {
|
|
1647
|
+
knowledge_id: ((Q = u.knowledge) == null ? void 0 : Q.id) ?? "",
|
|
1648
|
+
message_id: f,
|
|
1649
|
+
matches: M,
|
|
1650
|
+
score: D
|
|
1651
|
+
}) : p.createRating(u.id, r.chat.id, {
|
|
1652
|
+
knowledge_id: ((X = u.knowledge) == null ? void 0 : X.id) ?? "",
|
|
1653
|
+
message_id: f,
|
|
1654
|
+
matches: M,
|
|
1655
|
+
score: D
|
|
1656
|
+
});
|
|
1657
|
+
},
|
|
1658
|
+
deleteRate(f) {
|
|
1659
|
+
if (!r.chat)
|
|
1660
|
+
throw new Error("Chat is not initialized");
|
|
1661
|
+
return d.track("agent-rate-delete", { type: "text" }), p.deleteRating(u.id, r.chat.id, f);
|
|
1662
|
+
},
|
|
1663
|
+
async speak(f) {
|
|
1664
|
+
var M, _, K;
|
|
1665
|
+
function D() {
|
|
1666
|
+
if (typeof f == "string") {
|
|
1667
|
+
if (!u.presenter.voice)
|
|
1668
|
+
throw new Error("Presenter voice is not initialized");
|
|
1669
|
+
return {
|
|
1670
|
+
type: "text",
|
|
1671
|
+
provider: u.presenter.voice,
|
|
1672
|
+
input: f,
|
|
1673
|
+
ssml: !1
|
|
1674
|
+
};
|
|
1675
|
+
}
|
|
1676
|
+
if (f.type === "text" && !f.provider) {
|
|
1677
|
+
if (!u.presenter.voice)
|
|
1678
|
+
throw new Error("Presenter voice is not initialized");
|
|
1679
|
+
return {
|
|
1680
|
+
type: "text",
|
|
1681
|
+
provider: u.presenter.voice,
|
|
1682
|
+
input: f.input,
|
|
1683
|
+
ssml: f.ssml
|
|
1684
|
+
};
|
|
1685
|
+
}
|
|
1686
|
+
return f;
|
|
1687
|
+
}
|
|
1688
|
+
const w = D();
|
|
1689
|
+
if (d.track("agent-speak", w), te.update(), r.messages && w.type === "text" && (r.messages.push({
|
|
1690
|
+
id: ce(),
|
|
1691
|
+
role: "assistant",
|
|
1692
|
+
content: w.input,
|
|
1693
|
+
created_at: (/* @__PURE__ */ new Date()).toISOString()
|
|
1694
|
+
}), (_ = (M = t.callbacks).onNewMessage) == null || _.call(M, [...r.messages], "answer")), st(r.chatMode))
|
|
1695
|
+
return {
|
|
1696
|
+
duration: 0,
|
|
1697
|
+
video_id: "",
|
|
1698
|
+
status: "success"
|
|
1699
|
+
};
|
|
1700
|
+
if (!r.streamingManager)
|
|
1701
|
+
throw new Error("Please connect to the agent first");
|
|
1702
|
+
return r.streamingManager.speak({
|
|
1703
|
+
script: w,
|
|
1704
|
+
metadata: { chat_id: (K = r.chat) == null ? void 0 : K.id, agent_id: u.id }
|
|
1705
|
+
});
|
|
1706
|
+
},
|
|
1707
|
+
async interrupt({ type: f }) {
|
|
1708
|
+
var w, L, M;
|
|
1709
|
+
const D = r.messages[r.messages.length - 1];
|
|
1710
|
+
d.track("agent-video-interrupt", {
|
|
1711
|
+
type: f || "click",
|
|
1712
|
+
video_duration_to_interrupt: Se.get(!0),
|
|
1713
|
+
message_duration_to_interrupt: te.get(!0)
|
|
1714
|
+
}), D.interrupted = !0, (L = (w = t.callbacks).onNewMessage) == null || L.call(w, [...r.messages], "answer"), I ? Et(r.streamingManager) : (Dt(r.streamingManager, (M = r.streamingManager) == null ? void 0 : M.streamType, n), Mt(r.streamingManager, n));
|
|
1715
|
+
}
|
|
1716
|
+
};
|
|
1717
|
+
}
|
|
2
1718
|
export {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
1719
|
+
ie as AgentActivityState,
|
|
1720
|
+
Xe as AgentStatus,
|
|
1721
|
+
Ve as ChatCreationFailed,
|
|
1722
|
+
N as ChatMode,
|
|
1723
|
+
Oe as ChatModeDowngraded,
|
|
1724
|
+
Y as ChatProgress,
|
|
1725
|
+
C as ConnectionState,
|
|
1726
|
+
ee as ConnectivityState,
|
|
1727
|
+
et as DocumentType,
|
|
1728
|
+
Ge as KnowledgeType,
|
|
1729
|
+
Qe as PlanGroup,
|
|
1730
|
+
tt as Providers,
|
|
1731
|
+
Ye as RateState,
|
|
1732
|
+
B as StreamEvents,
|
|
1733
|
+
ne as StreamType,
|
|
1734
|
+
k as StreamingState,
|
|
1735
|
+
Ze as Subject,
|
|
1736
|
+
he as TransportProvider,
|
|
1737
|
+
qe as UserPlan,
|
|
1738
|
+
se as ValidationError,
|
|
1739
|
+
$e as VideoType,
|
|
1740
|
+
nt as VoiceAccess,
|
|
1741
|
+
He as WsError,
|
|
1742
|
+
cn as createAgentManager,
|
|
1743
|
+
sn as mapVideoType
|
|
28
1744
|
};
|