@d-id/client-sdk 1.0.19-beta.9 → 1.0.19-beta.90
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/README.md +4 -0
- package/dist/index.js +613 -404
- package/dist/index.umd.cjs +1 -1
- package/dist/src/{lib/api → api}/agents.d.ts +4 -1
- package/dist/src/api/clipStream.d.ts +2 -0
- package/dist/src/{lib/api → api}/getClient.d.ts +1 -1
- package/dist/src/{lib/api → api}/knowledge.d.ts +2 -1
- package/dist/src/api/talkStream.d.ts +2 -0
- package/dist/src/auth/getAuthHeader.d.ts +4 -0
- package/dist/src/connectToSocket.d.ts +11 -0
- package/dist/src/{lib/createAgentManager.d.ts → createAgentManager.d.ts} +4 -5
- package/dist/src/createStreamingManager.d.ts +21 -0
- package/dist/src/{lib/environment.d.ts → environment.d.ts} +0 -1
- package/dist/src/index.d.ts +2 -8
- package/dist/src/services/mixpanel.d.ts +18 -0
- package/dist/src/types/StreamScript.d.ts +1 -1
- package/dist/src/types/auth.d.ts +0 -2
- package/dist/src/types/entities/agents/agent.d.ts +24 -1
- package/dist/src/types/entities/agents/chat.d.ts +13 -6
- package/dist/src/types/entities/agents/knowledge.d.ts +1 -0
- package/dist/src/types/entities/agents/manager.d.ts +52 -24
- package/dist/src/types/entities/agents/presenter.d.ts +3 -0
- package/dist/src/types/face-rect.d.ts +6 -0
- package/dist/src/types/index.d.ts +2 -3
- package/dist/src/types/stream/api/clip.d.ts +6 -0
- package/dist/src/types/stream/api/talk.d.ts +4 -0
- package/dist/src/types/stream/rtc.d.ts +3 -3
- package/dist/src/types/stream/stream.d.ts +20 -4
- package/dist/src/utils/analytics.d.ts +12 -0
- package/dist/src/{lib/utils → utils}/webrtc.d.ts +1 -1
- package/package.json +10 -11
- package/dist/src/lib/api/clipStream.d.ts +0 -2
- package/dist/src/lib/api/ratings.d.ts +0 -7
- package/dist/src/lib/api/talkStream.d.ts +0 -2
- package/dist/src/lib/auth/getAuthHeader.d.ts +0 -2
- package/dist/src/lib/connectToSocket.d.ts +0 -9
- package/dist/src/lib/createStreamingManager.d.ts +0 -27
package/dist/index.js
CHANGED
|
@@ -1,506 +1,715 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
var j = /* @__PURE__ */ ((e) => (e.TRIAL = "trial", e.BASIC = "basic", e.ENTERPRISE = "enterprise", e.LITE = "lite", e.ADVANCED = "advanced", e))(j || {}), q = /* @__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))(q || {}), Q = /* @__PURE__ */ ((e) => (e.Unrated = "Unrated", e.Positive = "Positive", e.Negative = "Negative", e))(Q || {}), R = /* @__PURE__ */ ((e) => (e.Functional = "Functional", e.TextOnly = "TextOnly", e.Maintenance = "Maintenance", e.Playground = "Playground", e))(R || {}), b = /* @__PURE__ */ ((e) => (e.Embed = "embed", e.Query = "query", e.Partial = "partial", e.Answer = "answer", e.Complete = "done", e))(b || {}), X = /* @__PURE__ */ ((e) => (e.KnowledgeProcessing = "knowledge/processing", e.KnowledgeIndexing = "knowledge/indexing", e.KnowledgeFailed = "knowledge/error", e.KnowledgeDone = "knowledge/done", e))(X || {}), Y = /* @__PURE__ */ ((e) => (e.Knowledge = "knowledge", e.Document = "document", e.Record = "record", e))(Y || {}), Z = /* @__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))(Z || {}), y = /* @__PURE__ */ ((e) => (e.Clip = "clip", e.Talk = "talk", e))(y || {}), T = /* @__PURE__ */ ((e) => (e.Start = "START", e.Stop = "STOP", e))(T || {}), A = /* @__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))(A || {}), M = /* @__PURE__ */ ((e) => (e[e.New = 0] = "New", e[e.Fail = 1] = "Fail", e[e.Connected = 2] = "Connected", e[e.Connecting = 3] = "Connecting", e[e.Terminating = 4] = "Terminating", e))(M || {}), G = /* @__PURE__ */ ((e) => (e.Amazon = "amazon", e.Microsoft = "microsoft", e.Afflorithmics = "afflorithmics", e.Elevenlabs = "elevenlabs", e))(G || {}), ee = /* @__PURE__ */ ((e) => (e.Public = "public", e.Premium = "premium", e.Private = "private", e))(ee || {});
|
|
2
|
+
const N = "https://api.d-id.com", te = "wss://notifications.d-id.com", ne = "79f81a83a67430be2bc0fd61042b8faa", z = () => Math.random().toString(16).slice(2);
|
|
3
|
+
function W() {
|
|
4
|
+
let e = window.localStorage.getItem("did_external_key_id");
|
|
5
|
+
return e || (e = Math.random().toString(16).slice(2), window.localStorage.setItem("did_external_key_id", e)), e;
|
|
6
|
+
}
|
|
7
|
+
let re = z();
|
|
8
|
+
function H(e) {
|
|
3
9
|
if (e.type === "bearer")
|
|
4
10
|
return `Bearer ${e.token}`;
|
|
5
11
|
if (e.type === "basic")
|
|
6
12
|
return `Basic ${btoa(`${e.username}:${e.password}`)}`;
|
|
7
13
|
if (e.type === "key")
|
|
8
|
-
return `Client-Key ${e.clientKey}.${
|
|
14
|
+
return `Client-Key ${e.clientKey}.${W()}_${re}`;
|
|
9
15
|
throw new Error(`Unknown auth type: ${e}`);
|
|
10
16
|
}
|
|
11
|
-
function
|
|
12
|
-
const
|
|
13
|
-
const
|
|
14
|
-
...
|
|
17
|
+
function F(e, a = N) {
|
|
18
|
+
const t = async (n, r) => {
|
|
19
|
+
const i = await fetch(a + (n != null && n.startsWith("/") ? n : `/${n}`), {
|
|
20
|
+
...r,
|
|
15
21
|
headers: {
|
|
16
|
-
...
|
|
17
|
-
Authorization:
|
|
22
|
+
...r == null ? void 0 : r.headers,
|
|
23
|
+
Authorization: H(e),
|
|
18
24
|
"Content-Type": "application/json"
|
|
19
25
|
}
|
|
20
26
|
});
|
|
21
|
-
if (!
|
|
22
|
-
let
|
|
23
|
-
throw new Error(
|
|
27
|
+
if (!i.ok) {
|
|
28
|
+
let o = await i.text().catch(() => "Failed to fetch");
|
|
29
|
+
throw new Error(o);
|
|
24
30
|
}
|
|
25
|
-
return
|
|
31
|
+
return i.json();
|
|
26
32
|
};
|
|
27
33
|
return {
|
|
28
|
-
get(
|
|
29
|
-
return
|
|
30
|
-
...
|
|
34
|
+
get(n, r) {
|
|
35
|
+
return t(n, {
|
|
36
|
+
...r,
|
|
31
37
|
method: "GET"
|
|
32
38
|
});
|
|
33
39
|
},
|
|
34
|
-
post(
|
|
35
|
-
return
|
|
36
|
-
...
|
|
37
|
-
body: JSON.stringify(
|
|
40
|
+
post(n, r, i) {
|
|
41
|
+
return t(n, {
|
|
42
|
+
...i,
|
|
43
|
+
body: JSON.stringify(r),
|
|
38
44
|
method: "POST"
|
|
39
45
|
});
|
|
40
46
|
},
|
|
41
|
-
delete(
|
|
42
|
-
return
|
|
43
|
-
...
|
|
44
|
-
body: JSON.stringify(
|
|
47
|
+
delete(n, r, i) {
|
|
48
|
+
return t(n, {
|
|
49
|
+
...i,
|
|
50
|
+
body: JSON.stringify(r),
|
|
45
51
|
method: "DELETE"
|
|
46
52
|
});
|
|
47
53
|
},
|
|
48
|
-
patch(
|
|
49
|
-
return
|
|
50
|
-
...
|
|
51
|
-
body: JSON.stringify(
|
|
54
|
+
patch(n, r, i) {
|
|
55
|
+
return t(n, {
|
|
56
|
+
...i,
|
|
57
|
+
body: JSON.stringify(r),
|
|
52
58
|
method: "PATCH"
|
|
53
59
|
});
|
|
54
60
|
}
|
|
55
61
|
};
|
|
56
62
|
}
|
|
57
|
-
function
|
|
58
|
-
const
|
|
63
|
+
function J(e, a = N) {
|
|
64
|
+
const t = F(e, `${a}/agents`);
|
|
59
65
|
return {
|
|
60
|
-
create(
|
|
61
|
-
return
|
|
62
|
-
},
|
|
63
|
-
getAgents(t, n) {
|
|
64
|
-
return r.get(`/${t ? `?tag=${t}` : ""}`, n).then((a) => a ?? []);
|
|
65
|
-
},
|
|
66
|
-
getById(t, n) {
|
|
67
|
-
return r.get(`/${t}`, n);
|
|
68
|
-
},
|
|
69
|
-
delete(t, n) {
|
|
70
|
-
return r.delete(`/${t}`, void 0, n);
|
|
66
|
+
create(n, r) {
|
|
67
|
+
return t.post("/", n, r);
|
|
71
68
|
},
|
|
72
|
-
|
|
73
|
-
return
|
|
69
|
+
getAgents(n, r) {
|
|
70
|
+
return t.get(`/${n ? `?tag=${n}` : ""}`, r).then((i) => i ?? []);
|
|
74
71
|
},
|
|
75
|
-
|
|
76
|
-
return
|
|
72
|
+
getById(n, r) {
|
|
73
|
+
return t.get(`/${n}`, r);
|
|
77
74
|
},
|
|
78
|
-
|
|
79
|
-
return
|
|
80
|
-
}
|
|
81
|
-
};
|
|
82
|
-
}
|
|
83
|
-
function F(e, s = C) {
|
|
84
|
-
const r = A(e, `${s}/knowledge`);
|
|
85
|
-
return {
|
|
86
|
-
createKnowledge(t, n) {
|
|
87
|
-
return r.post("/", t, n);
|
|
75
|
+
delete(n, r) {
|
|
76
|
+
return t.delete(`/${n}`, void 0, r);
|
|
88
77
|
},
|
|
89
|
-
|
|
90
|
-
return
|
|
78
|
+
update(n, r, i) {
|
|
79
|
+
return t.patch(`/${n}`, r, i);
|
|
91
80
|
},
|
|
92
|
-
|
|
93
|
-
return
|
|
81
|
+
newChat(n, r) {
|
|
82
|
+
return t.post(`/${n}/chat`, void 0, r);
|
|
94
83
|
},
|
|
95
|
-
|
|
96
|
-
return
|
|
84
|
+
chat(n, r, i, o) {
|
|
85
|
+
return t.post(`/${n}/chat/${r}`, i, o);
|
|
97
86
|
},
|
|
98
|
-
|
|
99
|
-
return
|
|
87
|
+
createRating(n, r, i, o) {
|
|
88
|
+
return t.post(`/${n}/chat/${r}/ratings`, i, o);
|
|
100
89
|
},
|
|
101
|
-
|
|
102
|
-
return
|
|
90
|
+
updateRating(n, r, i, o, d) {
|
|
91
|
+
return t.patch(`/${n}/chat/${r}/ratings/${i}`, o, d);
|
|
103
92
|
},
|
|
104
|
-
|
|
105
|
-
return
|
|
106
|
-
},
|
|
107
|
-
getDocument(t, n, a) {
|
|
108
|
-
return r.get(`/${t}/documents/${n}`, a);
|
|
109
|
-
},
|
|
110
|
-
getRecords(t, n, a) {
|
|
111
|
-
return r.get(`/${t}/documents/${n}/records`, a);
|
|
112
|
-
},
|
|
113
|
-
query(t, n, a) {
|
|
114
|
-
return r.post(`/${t}/query`, {
|
|
115
|
-
query: n
|
|
116
|
-
}, a);
|
|
93
|
+
deleteRating(n, r, i, o) {
|
|
94
|
+
return t.delete(`/${n}/chat/${r}/ratings/${i}`, o);
|
|
117
95
|
}
|
|
118
96
|
};
|
|
119
97
|
}
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
return {
|
|
123
|
-
create(t, n) {
|
|
124
|
-
return r.post("/", t, n);
|
|
125
|
-
},
|
|
126
|
-
getByKnowledge(t, n) {
|
|
127
|
-
return r.get(`/${t}`, n).then((a) => a ?? []);
|
|
128
|
-
},
|
|
129
|
-
update(t, n, a) {
|
|
130
|
-
return r.patch(`/${t}`, n, a);
|
|
131
|
-
},
|
|
132
|
-
delete(t, n) {
|
|
133
|
-
return r.delete(`/${t}`, n);
|
|
134
|
-
}
|
|
135
|
-
};
|
|
136
|
-
}
|
|
137
|
-
const V = (e) => new Promise((s) => setTimeout(s, e));
|
|
138
|
-
function G(e) {
|
|
139
|
-
return new Promise((s, r) => {
|
|
98
|
+
const ae = (e) => new Promise((a) => setTimeout(a, e));
|
|
99
|
+
function ie(e) {
|
|
100
|
+
return new Promise((a, t) => {
|
|
140
101
|
const {
|
|
141
|
-
callbacks:
|
|
142
|
-
host:
|
|
143
|
-
auth:
|
|
102
|
+
callbacks: n,
|
|
103
|
+
host: r,
|
|
104
|
+
auth: i
|
|
144
105
|
} = e, {
|
|
145
|
-
onMessage:
|
|
146
|
-
onOpen:
|
|
106
|
+
onMessage: o = null,
|
|
107
|
+
onOpen: d = null,
|
|
147
108
|
onClose: g = null,
|
|
148
|
-
onError:
|
|
149
|
-
} =
|
|
150
|
-
|
|
151
|
-
console.
|
|
152
|
-
},
|
|
153
|
-
|
|
109
|
+
onError: u = null
|
|
110
|
+
} = n || {}, f = new WebSocket(`${r}?authorization=${H(i)}`);
|
|
111
|
+
f.onmessage = o, f.onclose = g, f.onerror = (p) => {
|
|
112
|
+
console.error(p), u == null || u("Websocket failed to connect", p), t(p);
|
|
113
|
+
}, f.onopen = (p) => {
|
|
114
|
+
d == null || d(p), a(f);
|
|
154
115
|
};
|
|
155
116
|
});
|
|
156
117
|
}
|
|
157
|
-
async function
|
|
118
|
+
async function se(e) {
|
|
158
119
|
const {
|
|
159
|
-
retries:
|
|
120
|
+
retries: a = 1
|
|
160
121
|
} = e;
|
|
161
|
-
let
|
|
162
|
-
for (let
|
|
122
|
+
let t = null;
|
|
123
|
+
for (let n = 0; (t == null ? void 0 : t.readyState) !== WebSocket.OPEN; n++)
|
|
163
124
|
try {
|
|
164
|
-
|
|
165
|
-
} catch (
|
|
166
|
-
if (
|
|
167
|
-
throw
|
|
168
|
-
await
|
|
125
|
+
t = await ie(e);
|
|
126
|
+
} catch (r) {
|
|
127
|
+
if (n === a)
|
|
128
|
+
throw r;
|
|
129
|
+
await ae(n * 500);
|
|
169
130
|
}
|
|
170
|
-
return
|
|
131
|
+
return t;
|
|
171
132
|
}
|
|
172
|
-
async function
|
|
173
|
-
const
|
|
133
|
+
async function K(e, a, t) {
|
|
134
|
+
const n = t != null && t.onMessage ? [t.onMessage] : [], r = await se({
|
|
174
135
|
auth: e,
|
|
175
|
-
host:
|
|
136
|
+
host: a,
|
|
176
137
|
callbacks: {
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
138
|
+
onError: t == null ? void 0 : t.onError,
|
|
139
|
+
onMessage: (i) => {
|
|
140
|
+
const o = JSON.parse(i.data);
|
|
141
|
+
n.forEach((d) => d(o.event, o));
|
|
180
142
|
}
|
|
181
143
|
}
|
|
182
144
|
});
|
|
183
145
|
return {
|
|
184
|
-
socket:
|
|
185
|
-
|
|
186
|
-
subscribeToEvents: (
|
|
146
|
+
socket: r,
|
|
147
|
+
disconnect: () => r.close(),
|
|
148
|
+
subscribeToEvents: (i) => n.push(i)
|
|
187
149
|
};
|
|
188
150
|
}
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
return e.presenter.type === y.Clip ? {
|
|
192
|
-
videoType: y.Clip,
|
|
193
|
-
driver_id: e.presenter.driver_id,
|
|
194
|
-
presenter_id: e.presenter.presenter_id
|
|
195
|
-
} : {
|
|
196
|
-
videoType: y.Talk,
|
|
197
|
-
source_url: e.presenter.source_url
|
|
198
|
-
};
|
|
199
|
-
}
|
|
200
|
-
function T(e, s, r, t) {
|
|
201
|
-
return new Promise(async (n, a) => {
|
|
202
|
-
const i = await ce(re(e), {
|
|
203
|
-
...s,
|
|
204
|
-
callbacks: {
|
|
205
|
-
...s.callbacks,
|
|
206
|
-
onConnectionStateChange: async (l) => {
|
|
207
|
-
var g, f;
|
|
208
|
-
l === "connected" ? (t || (t = await r.newChat(e.id)), n({
|
|
209
|
-
chat: t,
|
|
210
|
-
streamingManager: i
|
|
211
|
-
})) : l === "failed" && a(new Error("Cannot create connection")), (f = (g = s.callbacks).onConnectionStateChange) == null || f.call(g, l);
|
|
212
|
-
},
|
|
213
|
-
// TODO remove when webscoket will return partial
|
|
214
|
-
onMessage: (l, g) => {
|
|
215
|
-
var f, m;
|
|
216
|
-
l === I.ChatAnswer && (console.log("ChatAnswer", l, g), (m = (f = s.callbacks).onChatEvents) == null || m.call(f, E.Answer, {
|
|
217
|
-
content: g,
|
|
218
|
-
event: E.Answer
|
|
219
|
-
}));
|
|
220
|
-
}
|
|
221
|
-
}
|
|
222
|
-
});
|
|
223
|
-
});
|
|
224
|
-
}
|
|
225
|
-
function de(e, s, r) {
|
|
226
|
-
return q(s, r || C).getById(e);
|
|
227
|
-
}
|
|
228
|
-
async function le(e, s) {
|
|
229
|
-
const r = s.baseURL || C, t = s.wsURL || J, n = new AbortController(), a = q(s.auth, r), i = D(s.auth, r), l = F(s.auth, r);
|
|
230
|
-
console.log(`AUTH: ${s.auth.clientKey},${s.auth.externalId}, ${t}`);
|
|
231
|
-
const g = await a.getById(e), f = await X(s.auth, t, s.callbacks.onChatEvents);
|
|
232
|
-
let {
|
|
233
|
-
chat: m,
|
|
234
|
-
streamingManager: d
|
|
235
|
-
} = await T(g, s, a);
|
|
151
|
+
function oe(e, a, t) {
|
|
152
|
+
const n = F(e, `${a}/agents/${t}`);
|
|
236
153
|
return {
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
terminate() {
|
|
245
|
-
return n.abort(), f.terminate(), d.terminate();
|
|
246
|
-
},
|
|
247
|
-
chatId: m.id,
|
|
248
|
-
chat(c) {
|
|
249
|
-
return a.chat(e, m.id, {
|
|
250
|
-
sessionId: d.sessionId,
|
|
251
|
-
streamId: d.streamId,
|
|
252
|
-
messages: c
|
|
253
|
-
}, {
|
|
254
|
-
signal: n.signal
|
|
255
|
-
});
|
|
256
|
-
},
|
|
257
|
-
rate(c, p) {
|
|
258
|
-
return p ? i.update(p, c) : i.create(c);
|
|
259
|
-
},
|
|
260
|
-
deleteRate(c) {
|
|
261
|
-
return i.delete(c);
|
|
262
|
-
},
|
|
263
|
-
speak(c) {
|
|
264
|
-
let p;
|
|
265
|
-
return c.type === "text" ? p = {
|
|
266
|
-
script: {
|
|
267
|
-
type: "text",
|
|
268
|
-
provider: c.provider,
|
|
269
|
-
input: c.input,
|
|
270
|
-
ssml: c.ssml || !1
|
|
271
|
-
}
|
|
272
|
-
} : c.type === "audio" && (p = {
|
|
273
|
-
script: {
|
|
274
|
-
type: "audio",
|
|
275
|
-
audio_url: c.audio_url
|
|
276
|
-
}
|
|
277
|
-
}), d.speak(p);
|
|
278
|
-
},
|
|
279
|
-
getStarterMessages() {
|
|
280
|
-
var c, p;
|
|
281
|
-
return (c = g.knowledge) != null && c.id ? l.getKnowledge((p = g.knowledge) == null ? void 0 : p.id).then((w) => (w == null ? void 0 : w.starter_message) || []) : Promise.resolve([]);
|
|
282
|
-
}
|
|
283
|
-
};
|
|
284
|
-
}
|
|
285
|
-
function ae(e, s) {
|
|
286
|
-
const r = A(e, s);
|
|
287
|
-
return {
|
|
288
|
-
createStream(t) {
|
|
289
|
-
return r.post("/clips/streams", {
|
|
290
|
-
driver_id: t.driver_id,
|
|
291
|
-
presenter_id: t.presenter_id,
|
|
292
|
-
compatibility_mode: t.compatibility_mode
|
|
154
|
+
createStream(r) {
|
|
155
|
+
return n.post("/streams", {
|
|
156
|
+
driver_id: r.driver_id,
|
|
157
|
+
presenter_id: r.presenter_id,
|
|
158
|
+
compatibility_mode: r.compatibility_mode,
|
|
159
|
+
stream_warmup: r.stream_warmup,
|
|
160
|
+
type: y.Clip
|
|
293
161
|
});
|
|
294
162
|
},
|
|
295
|
-
startConnection(
|
|
296
|
-
return
|
|
297
|
-
session_id:
|
|
298
|
-
answer:
|
|
163
|
+
startConnection(r, i, o) {
|
|
164
|
+
return n.post(`/streams/${r}/sdp`, {
|
|
165
|
+
session_id: o,
|
|
166
|
+
answer: i,
|
|
167
|
+
type: y.Clip
|
|
299
168
|
});
|
|
300
169
|
},
|
|
301
|
-
addIceCandidate(
|
|
302
|
-
return
|
|
303
|
-
session_id:
|
|
304
|
-
...
|
|
170
|
+
addIceCandidate(r, i, o) {
|
|
171
|
+
return n.post(`/streams/${r}/ice`, {
|
|
172
|
+
session_id: o,
|
|
173
|
+
...i,
|
|
174
|
+
type: y.Clip
|
|
305
175
|
});
|
|
306
176
|
},
|
|
307
|
-
sendStreamRequest(
|
|
308
|
-
return
|
|
309
|
-
session_id:
|
|
310
|
-
...
|
|
177
|
+
sendStreamRequest(r, i, o) {
|
|
178
|
+
return n.post(`/streams/${r}`, {
|
|
179
|
+
session_id: i,
|
|
180
|
+
...o,
|
|
181
|
+
type: y.Clip
|
|
311
182
|
});
|
|
312
183
|
},
|
|
313
|
-
close(
|
|
314
|
-
return
|
|
315
|
-
session_id:
|
|
184
|
+
close(r, i) {
|
|
185
|
+
return n.delete(`/streams/${r}`, {
|
|
186
|
+
session_id: i,
|
|
187
|
+
type: y.Clip
|
|
316
188
|
});
|
|
317
189
|
}
|
|
318
190
|
};
|
|
319
191
|
}
|
|
320
|
-
function
|
|
321
|
-
const
|
|
192
|
+
function ce(e, a, t) {
|
|
193
|
+
const n = F(e, `${a}/agents/${t}`);
|
|
322
194
|
return {
|
|
323
|
-
createStream(
|
|
324
|
-
return
|
|
325
|
-
source_url:
|
|
326
|
-
driver_url:
|
|
327
|
-
face:
|
|
328
|
-
config:
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
session_id: a,
|
|
334
|
-
answer: n
|
|
335
|
-
}, i);
|
|
336
|
-
},
|
|
337
|
-
addIceCandidate(t, n, a, i) {
|
|
338
|
-
return r.post(`/talks/streams/${t}/ice`, {
|
|
339
|
-
session_id: a,
|
|
340
|
-
...n
|
|
195
|
+
createStream(r, i) {
|
|
196
|
+
return n.post("/streams", {
|
|
197
|
+
source_url: r.source_url,
|
|
198
|
+
driver_url: r.driver_url,
|
|
199
|
+
face: r.face,
|
|
200
|
+
config: r.config,
|
|
201
|
+
compatibility_mode: r.compatibility_mode,
|
|
202
|
+
stream_warmup: r.stream_warmup,
|
|
203
|
+
output_resolution: r.output_resolution,
|
|
204
|
+
type: y.Talk
|
|
341
205
|
}, i);
|
|
342
206
|
},
|
|
343
|
-
|
|
344
|
-
return
|
|
345
|
-
session_id:
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
207
|
+
startConnection(r, i, o, d) {
|
|
208
|
+
return n.post(`/streams/${r}/sdp`, {
|
|
209
|
+
session_id: o,
|
|
210
|
+
answer: i,
|
|
211
|
+
type: y.Talk
|
|
212
|
+
}, d);
|
|
213
|
+
},
|
|
214
|
+
addIceCandidate(r, i, o, d) {
|
|
215
|
+
return n.post(`/streams/${r}/ice`, {
|
|
216
|
+
session_id: o,
|
|
217
|
+
...i,
|
|
218
|
+
type: y.Talk
|
|
219
|
+
}, d);
|
|
220
|
+
},
|
|
221
|
+
sendStreamRequest(r, i, o, d) {
|
|
222
|
+
return n.post(`/streams/${r}`, {
|
|
223
|
+
session_id: i,
|
|
224
|
+
...o,
|
|
225
|
+
type: y.Talk
|
|
226
|
+
}, d);
|
|
227
|
+
},
|
|
228
|
+
close(r, i, o) {
|
|
229
|
+
return n.delete(`/streams/${r}`, {
|
|
230
|
+
session_id: i,
|
|
231
|
+
type: y.Talk
|
|
232
|
+
}, o);
|
|
353
233
|
}
|
|
354
234
|
};
|
|
355
235
|
}
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
packetsLost: r.packetsLost,
|
|
373
|
-
jitter: r.jitter,
|
|
374
|
-
frameWidth: r.frameWidth,
|
|
375
|
-
frameHeight: r.frameHeight,
|
|
376
|
-
frameRate: r.frameRate
|
|
377
|
-
} : {
|
|
378
|
-
index: t,
|
|
379
|
-
timestamp: r.timestamp,
|
|
380
|
-
bytesReceived: r.bytesReceived - e[t - 1].bytesReceived,
|
|
381
|
-
packetsReceived: r.packetsReceived - e[t - 1].packetsReceived,
|
|
382
|
-
packetsLost: r.packetsLost - e[t - 1].packetsLost,
|
|
383
|
-
jitter: r.jitter,
|
|
384
|
-
frameWidth: r.frameWidth,
|
|
385
|
-
frameHeight: r.frameHeight,
|
|
386
|
-
frameRate: r.frameRate
|
|
387
|
-
});
|
|
236
|
+
let V = !1;
|
|
237
|
+
const k = (e, a) => V && console.log(e, a), de = (window.RTCPeerConnection || window.webkitRTCPeerConnection || window.mozRTCPeerConnection).bind(window);
|
|
238
|
+
function U(e) {
|
|
239
|
+
switch (e) {
|
|
240
|
+
case "connected":
|
|
241
|
+
return M.Connected;
|
|
242
|
+
case "checking":
|
|
243
|
+
return M.Connecting;
|
|
244
|
+
case "failed":
|
|
245
|
+
return M.Fail;
|
|
246
|
+
case "new":
|
|
247
|
+
case "closed":
|
|
248
|
+
case "disconnected":
|
|
249
|
+
default:
|
|
250
|
+
return M.New;
|
|
251
|
+
}
|
|
388
252
|
}
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
253
|
+
function ge() {
|
|
254
|
+
let e = 0;
|
|
255
|
+
return (a) => {
|
|
256
|
+
for (const t of a.values())
|
|
257
|
+
if (t && t.type === "inbound-rtp" && t.kind === "video") {
|
|
258
|
+
const n = t.bytesReceived, r = n - e > 0;
|
|
259
|
+
return e = n, r;
|
|
260
|
+
}
|
|
261
|
+
return !1;
|
|
262
|
+
};
|
|
263
|
+
}
|
|
264
|
+
function le(e, a, t) {
|
|
265
|
+
const r = Math.max(Math.ceil(10), 1);
|
|
266
|
+
let i = 0, o = !1;
|
|
267
|
+
const d = ge();
|
|
268
|
+
return setInterval(async () => {
|
|
269
|
+
const g = await e.getStats();
|
|
270
|
+
d(g) ? (i = 0, o || (a == null || a(T.Start), o = !0)) : o && (i++, i >= r && (a == null || a(T.Stop), o = !1));
|
|
271
|
+
}, 100);
|
|
272
|
+
}
|
|
273
|
+
async function ue(e, a, {
|
|
274
|
+
debug: t = !1,
|
|
275
|
+
callbacks: n,
|
|
276
|
+
auth: r,
|
|
277
|
+
analytics: i,
|
|
278
|
+
baseURL: o = N
|
|
396
279
|
}) {
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
let k = 0, W;
|
|
415
|
-
if (!w)
|
|
280
|
+
V = t;
|
|
281
|
+
let d;
|
|
282
|
+
const {
|
|
283
|
+
startConnection: g,
|
|
284
|
+
sendStreamRequest: u,
|
|
285
|
+
close: f,
|
|
286
|
+
createStream: p,
|
|
287
|
+
addIceCandidate: $
|
|
288
|
+
} = a.videoType === y.Clip ? oe(r, o, e) : ce(r, o, e), {
|
|
289
|
+
id: _,
|
|
290
|
+
offer: L,
|
|
291
|
+
ice_servers: O,
|
|
292
|
+
session_id: S
|
|
293
|
+
} = await p(a), s = new de({
|
|
294
|
+
iceServers: O
|
|
295
|
+
}), m = s.createDataChannel("JanusDataChannel");
|
|
296
|
+
if (!S)
|
|
416
297
|
throw new Error("Could not create session_id");
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
},
|
|
427
|
-
var
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
console.log("StreamDone", S, L), clearInterval(W);
|
|
443
|
-
const K = $.slice(k);
|
|
444
|
-
if (K) {
|
|
445
|
-
const P = k === 0 ? void 0 : $[k - 1], R = ie(K, P);
|
|
446
|
-
k = $.length, (M = a.onVideoStateChange) == null || M.call(a, _.Stop, R.sort((B, N) => N.packetsLost - B.packetsLost).slice(0, 5));
|
|
447
|
-
}
|
|
448
|
-
} else
|
|
449
|
-
(U = a.onMessage) == null || U.call(a, S, decodeURIComponent(L));
|
|
298
|
+
const l = le(s, n.onVideoStateChange);
|
|
299
|
+
s.onicecandidate = (c) => {
|
|
300
|
+
k("peerConnection.onicecandidate", c), c.candidate && c.candidate.sdpMid && c.candidate.sdpMLineIndex !== null ? $(_, {
|
|
301
|
+
candidate: c.candidate.candidate,
|
|
302
|
+
sdpMid: c.candidate.sdpMid,
|
|
303
|
+
sdpMLineIndex: c.candidate.sdpMLineIndex
|
|
304
|
+
}, S) : $(_, {
|
|
305
|
+
candidate: null
|
|
306
|
+
}, S);
|
|
307
|
+
}, s.oniceconnectionstatechange = () => {
|
|
308
|
+
var w;
|
|
309
|
+
k("peerConnection.oniceconnectionstatechange => " + s.iceConnectionState);
|
|
310
|
+
const c = U(s.iceConnectionState);
|
|
311
|
+
c === M.Connected ? d = setTimeout(() => {
|
|
312
|
+
var v;
|
|
313
|
+
(v = n.onConnectionStateChange) == null || v.call(n, M.Connected);
|
|
314
|
+
}, 5e3) : (clearTimeout(d), (w = n.onConnectionStateChange) == null || w.call(n, c));
|
|
315
|
+
}, s.ontrack = (c) => {
|
|
316
|
+
var w;
|
|
317
|
+
k("peerConnection.ontrack", c), (w = n.onSrcObjectReady) == null || w.call(n, c.streams[0]);
|
|
318
|
+
}, m.onmessage = (c) => {
|
|
319
|
+
var w;
|
|
320
|
+
if (m.readyState === "open") {
|
|
321
|
+
const [v, C] = c.data.split(":");
|
|
322
|
+
v === A.StreamReady && (clearTimeout(d), (w = n.onConnectionStateChange) == null || w.call(n, M.Connected));
|
|
450
323
|
}
|
|
451
|
-
}, await
|
|
452
|
-
const
|
|
453
|
-
return
|
|
324
|
+
}, await s.setRemoteDescription(L), k("set remote description OK");
|
|
325
|
+
const h = await s.createAnswer();
|
|
326
|
+
return k("create answer OK"), await s.setLocalDescription(h), k("set local description OK"), await g(_, h, S), k("start connection OK"), {
|
|
454
327
|
/**
|
|
455
328
|
* Method to send request to server to get clip or talk depend on you payload
|
|
456
|
-
* @param payload
|
|
329
|
+
* @param payload
|
|
457
330
|
*/
|
|
458
|
-
speak(
|
|
459
|
-
return
|
|
331
|
+
speak(c) {
|
|
332
|
+
return u(_, S, c);
|
|
460
333
|
},
|
|
461
334
|
/**
|
|
462
335
|
* Method to close RTC connection
|
|
463
336
|
*/
|
|
464
|
-
async
|
|
465
|
-
var
|
|
466
|
-
|
|
467
|
-
|
|
337
|
+
async disconnect() {
|
|
338
|
+
var c, w, v;
|
|
339
|
+
if (_) {
|
|
340
|
+
if (s) {
|
|
341
|
+
if (U(s.iceConnectionState) === M.New) {
|
|
342
|
+
(c = n.onVideoStateChange) == null || c.call(n, T.Stop), clearInterval(l);
|
|
343
|
+
return;
|
|
344
|
+
}
|
|
345
|
+
s.close(), s.oniceconnectionstatechange = null, s.onnegotiationneeded = null, s.onicecandidate = null, s.ontrack = null;
|
|
346
|
+
}
|
|
347
|
+
try {
|
|
348
|
+
await f(_, S).catch((C) => {
|
|
349
|
+
});
|
|
350
|
+
} catch (C) {
|
|
351
|
+
k("Error on close stream connection", C);
|
|
352
|
+
}
|
|
353
|
+
(w = n.onConnectionStateChange) == null || w.call(n, M.New), (v = n.onVideoStateChange) == null || v.call(n, T.Stop), clearInterval(l);
|
|
354
|
+
}
|
|
468
355
|
},
|
|
469
356
|
/**
|
|
470
357
|
* Session identifier information, should be returned in the body of all streaming requests
|
|
471
358
|
*/
|
|
472
|
-
sessionId:
|
|
359
|
+
sessionId: S,
|
|
473
360
|
/**
|
|
474
361
|
* Id of current RTC stream
|
|
475
362
|
*/
|
|
476
|
-
streamId:
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
363
|
+
streamId: _
|
|
364
|
+
};
|
|
365
|
+
}
|
|
366
|
+
function me(e) {
|
|
367
|
+
const a = {
|
|
368
|
+
token: e.token || "testKey",
|
|
369
|
+
distinct_id: e.distinctId || W(),
|
|
370
|
+
isEnabled: e.isEnabled ?? !0,
|
|
371
|
+
agentId: e.agent.id,
|
|
372
|
+
owner_id: e.agent.owner_id ?? ""
|
|
373
|
+
};
|
|
374
|
+
return {
|
|
375
|
+
...a,
|
|
376
|
+
getRandom: () => Math.random().toString(16).slice(2),
|
|
377
|
+
track(t, n) {
|
|
378
|
+
if (!this.isEnabled)
|
|
379
|
+
return Promise.reject("MixPanel analytics is disabled on creation");
|
|
380
|
+
const r = {
|
|
381
|
+
method: "POST",
|
|
382
|
+
headers: {
|
|
383
|
+
"Content-Type": "application/x-www-form-urlencoded"
|
|
384
|
+
},
|
|
385
|
+
body: new URLSearchParams({
|
|
386
|
+
data: JSON.stringify([{
|
|
387
|
+
event: t,
|
|
388
|
+
properties: {
|
|
389
|
+
...n,
|
|
390
|
+
...a,
|
|
391
|
+
time: Date.now(),
|
|
392
|
+
$insert_id: this.getRandom(),
|
|
393
|
+
origin: window.location.href,
|
|
394
|
+
"Screen Height": window.screen.height || window.innerWidth,
|
|
395
|
+
"Screen Width": window.screen.width || window.innerHeight,
|
|
396
|
+
"User Agent": navigator.userAgent
|
|
397
|
+
}
|
|
398
|
+
}])
|
|
399
|
+
})
|
|
400
|
+
};
|
|
401
|
+
return fetch("https://api-js.mixpanel.com/track/?verbose=1&ip=1", r).then((i) => i.json()).catch((i) => console.error(i));
|
|
402
|
+
}
|
|
403
|
+
};
|
|
404
|
+
}
|
|
405
|
+
function we(e) {
|
|
406
|
+
var n, r, i, o, d;
|
|
407
|
+
const a = () => /Mobi|Android/i.test(navigator.userAgent) ? "Mobile" : "Desktop";
|
|
408
|
+
return {
|
|
409
|
+
$os: `${(() => {
|
|
410
|
+
const g = navigator.platform;
|
|
411
|
+
return g.toLowerCase().includes("win") ? "Windows" : g.toLowerCase().includes("mac") ? "Mac OS X" : g.toLowerCase().includes("linux") ? "Linux" : "Unknown";
|
|
412
|
+
})()}`,
|
|
413
|
+
isMobile: `${a() == "Mobile"}`,
|
|
414
|
+
browser: navigator.userAgent,
|
|
415
|
+
origin: window.location.origin,
|
|
416
|
+
agentType: (n = e.presenter) == null ? void 0 : n.type,
|
|
417
|
+
agentVoice: {
|
|
418
|
+
voiceId: (i = (r = e.presenter) == null ? void 0 : r.voice) == null ? void 0 : i.voice_id,
|
|
419
|
+
provider: (d = (o = e.presenter) == null ? void 0 : o.voice) == null ? void 0 : d.type
|
|
420
|
+
}
|
|
421
|
+
};
|
|
422
|
+
}
|
|
423
|
+
const he = 1080;
|
|
424
|
+
function fe(e, a) {
|
|
425
|
+
if (e.presenter) {
|
|
426
|
+
if (e.presenter.type === y.Clip)
|
|
427
|
+
return {
|
|
428
|
+
videoType: y.Clip,
|
|
429
|
+
driver_id: e.presenter.driver_id,
|
|
430
|
+
presenter_id: e.presenter.presenter_id,
|
|
431
|
+
stream_warmup: !0
|
|
432
|
+
};
|
|
433
|
+
} else
|
|
434
|
+
throw new Error("Presenter is not initialized");
|
|
435
|
+
const t = a || (e.presenter.stitch ? he : void 0);
|
|
436
|
+
return {
|
|
437
|
+
videoType: y.Talk,
|
|
438
|
+
source_url: e.presenter.source_url,
|
|
439
|
+
stream_warmup: !0,
|
|
440
|
+
...t && {
|
|
441
|
+
output_resolution: t
|
|
442
|
+
}
|
|
443
|
+
};
|
|
444
|
+
}
|
|
445
|
+
function B(e, a, t, n, r) {
|
|
446
|
+
return new Promise(async (i, o) => {
|
|
447
|
+
let d = r;
|
|
448
|
+
const g = await ue(e.id, fe(e, a.outputResolution), {
|
|
449
|
+
...a,
|
|
450
|
+
analytics: n,
|
|
451
|
+
callbacks: {
|
|
452
|
+
...a.callbacks,
|
|
453
|
+
onConnectionStateChange: async (u) => {
|
|
454
|
+
var f, p;
|
|
455
|
+
if (u === M.Connected)
|
|
456
|
+
try {
|
|
457
|
+
d || (d = await t.newChat(e.id), n.track("agent-chat", {
|
|
458
|
+
event: "created",
|
|
459
|
+
chat_id: d.id,
|
|
460
|
+
agent_id: e.id
|
|
461
|
+
})), g && i({
|
|
462
|
+
chat: d,
|
|
463
|
+
streamingManager: g
|
|
464
|
+
});
|
|
465
|
+
} catch ($) {
|
|
466
|
+
console.error($), o("Cannot create new chat");
|
|
467
|
+
}
|
|
468
|
+
else
|
|
469
|
+
u === M.Fail && o(new Error("Cannot create connection"));
|
|
470
|
+
(p = (f = a.callbacks).onConnectionStateChange) == null || p.call(f, u);
|
|
471
|
+
},
|
|
472
|
+
onVideoStateChange(u) {
|
|
473
|
+
var f, p;
|
|
474
|
+
(p = (f = a.callbacks).onVideoStateChange) == null || p.call(f, u);
|
|
475
|
+
}
|
|
476
|
+
}
|
|
477
|
+
}).catch(o);
|
|
478
|
+
});
|
|
479
|
+
}
|
|
480
|
+
function pe(e, a, t) {
|
|
481
|
+
return J(a, t || N).getById(e);
|
|
482
|
+
}
|
|
483
|
+
function D(e) {
|
|
484
|
+
let a = "";
|
|
485
|
+
if (e.greetings && e.greetings.length > 0) {
|
|
486
|
+
const t = Math.floor(Math.random() * e.greetings.length);
|
|
487
|
+
a = e.greetings[t];
|
|
488
|
+
} else
|
|
489
|
+
a = `Hi! I'm ${e.preview_name}, welcome to agents. How can I help you?`;
|
|
490
|
+
return [{
|
|
491
|
+
content: a,
|
|
492
|
+
id: z(),
|
|
493
|
+
role: "assistant",
|
|
494
|
+
created_at: (/* @__PURE__ */ new Date()).toISOString()
|
|
495
|
+
}];
|
|
496
|
+
}
|
|
497
|
+
async function ve(e, a) {
|
|
498
|
+
var L, O, S;
|
|
499
|
+
const t = {
|
|
500
|
+
messages: [],
|
|
501
|
+
chatMode: a.mode || R.Functional
|
|
502
|
+
};
|
|
503
|
+
let n = -1;
|
|
504
|
+
const r = a.wsURL || te, i = a.baseURL || N, o = a.mixpanelKey || ne, d = J(a.auth, i), g = await d.getById(e);
|
|
505
|
+
t.messages = D(g), (O = (L = a.callbacks).onNewMessage) == null || O.call(L, t.messages);
|
|
506
|
+
const u = me({
|
|
507
|
+
token: o,
|
|
508
|
+
agent: g,
|
|
509
|
+
...a
|
|
510
|
+
});
|
|
511
|
+
u.track("agent-sdk", {
|
|
512
|
+
event: "loaded",
|
|
513
|
+
...we(g)
|
|
514
|
+
});
|
|
515
|
+
const f = {
|
|
516
|
+
onMessage: (s, m) => {
|
|
517
|
+
var l, h;
|
|
518
|
+
if ("content" in m) {
|
|
519
|
+
const {
|
|
520
|
+
content: c
|
|
521
|
+
} = m, w = t.messages[t.messages.length - 1];
|
|
522
|
+
(w == null ? void 0 : w.role) === "assistant" && (n < t.messages.length && (w.content = s === b.Partial ? w.content + c : c), s === b.Answer && (n = t.messages.length)), s === b.Answer && u.track("agent-message-received", {
|
|
523
|
+
messages: t.messages.length
|
|
524
|
+
}), (h = (l = a.callbacks).onNewMessage) == null || h.call(l, t.messages);
|
|
525
|
+
} else if ([A.StreamVideoCreated, A.StreamVideoDone, A.StreamVideoError, A.StreamVideoRejected].includes(s)) {
|
|
526
|
+
const c = s.split("/")[1];
|
|
527
|
+
u.track("agent-video", {
|
|
528
|
+
...m,
|
|
529
|
+
event: c
|
|
530
|
+
});
|
|
531
|
+
}
|
|
532
|
+
}
|
|
533
|
+
};
|
|
534
|
+
async function p() {
|
|
535
|
+
var h, c, w, v, C;
|
|
536
|
+
const s = await K(a.auth, r, f), {
|
|
537
|
+
streamingManager: m,
|
|
538
|
+
chat: l
|
|
539
|
+
} = await B(g, a, d, u, t.chat);
|
|
540
|
+
n = -1, t.messages.length === 0 && (t.messages = D(g), (c = (h = a.callbacks).onNewMessage) == null || c.call(h, t.messages)), l != null && l.id && l.id !== ((w = t.chat) == null ? void 0 : w.id) && ((C = (v = a.callbacks).onNewChat) == null || C.call(v, l == null ? void 0 : l.id)), t.streamingManager = m, t.socketManager = s, t.chat = l, _(R.Functional), u.track("agent-chat", {
|
|
541
|
+
event: "connect",
|
|
542
|
+
chatId: l.id,
|
|
543
|
+
agentId: g.id
|
|
544
|
+
});
|
|
545
|
+
}
|
|
546
|
+
async function $() {
|
|
547
|
+
var s, m, l, h, c;
|
|
548
|
+
(s = t.socketManager) == null || s.disconnect(), await ((m = t.streamingManager) == null ? void 0 : m.disconnect()), delete t.streamingManager, delete t.socketManager, t.messages = D(g), (h = (l = a.callbacks).onNewMessage) == null || h.call(l, t.messages), u.track("agent-chat", {
|
|
549
|
+
event: "disconnect",
|
|
550
|
+
chatId: (c = t.chat) == null ? void 0 : c.id,
|
|
551
|
+
agentId: g.id
|
|
552
|
+
});
|
|
553
|
+
}
|
|
554
|
+
async function _(s) {
|
|
555
|
+
var m, l;
|
|
556
|
+
s !== t.chatMode && (u.track("agent-mode-change", {
|
|
557
|
+
mode: s
|
|
558
|
+
}), t.chatMode = s, t.chatMode !== R.Functional && await $(), (l = (m = a.callbacks).onModeChange) == null || l.call(m, s));
|
|
559
|
+
}
|
|
560
|
+
return {
|
|
561
|
+
agent: g,
|
|
562
|
+
starterMessages: ((S = g.knowledge) == null ? void 0 : S.starter_message) || [],
|
|
563
|
+
connect: p,
|
|
564
|
+
disconnect: $,
|
|
565
|
+
changeMode: _,
|
|
566
|
+
async reconnect() {
|
|
567
|
+
var h, c;
|
|
568
|
+
if (!t.chat)
|
|
569
|
+
return p();
|
|
570
|
+
(h = t.socketManager) == null || h.disconnect(), await ((c = t.streamingManager) == null ? void 0 : c.disconnect());
|
|
571
|
+
const s = await K(a.auth, r, f), {
|
|
572
|
+
streamingManager: m,
|
|
573
|
+
chat: l
|
|
574
|
+
} = await B(g, a, d, u, t.chat);
|
|
575
|
+
t.streamingManager = m, t.socketManager = s, _(R.Functional), u.track("agent-chat", {
|
|
576
|
+
event: "reconnect",
|
|
577
|
+
chatId: l.id,
|
|
578
|
+
agentId: g.id
|
|
579
|
+
});
|
|
580
|
+
},
|
|
581
|
+
async chat(s, m = !1) {
|
|
582
|
+
var h, c, w, v, C, E;
|
|
583
|
+
const l = z();
|
|
584
|
+
try {
|
|
585
|
+
const I = Date.now();
|
|
586
|
+
if (s.length >= 800)
|
|
587
|
+
throw new Error("Message cannot be more than 800 characters");
|
|
588
|
+
if (s.length === 0)
|
|
589
|
+
throw new Error("Message cannot be empty");
|
|
590
|
+
if (t.chatMode === R.Maintenance)
|
|
591
|
+
throw new Error("Chat is in maintenance mode");
|
|
592
|
+
if (![R.TextOnly, R.Playground].includes(t.chatMode)) {
|
|
593
|
+
if (!t.streamingManager)
|
|
594
|
+
throw new Error("Streaming manager is not initialized");
|
|
595
|
+
if (!t.chat)
|
|
596
|
+
throw new Error("Chat is not initialized");
|
|
597
|
+
}
|
|
598
|
+
t.messages.push({
|
|
599
|
+
id: z(),
|
|
600
|
+
role: "user",
|
|
601
|
+
content: s,
|
|
602
|
+
created_at: new Date(I).toISOString()
|
|
603
|
+
}), (c = (h = a.callbacks).onNewMessage) == null || c.call(h, t.messages), t.chat || (t.chat = await d.newChat(g.id));
|
|
604
|
+
const P = {
|
|
605
|
+
id: l,
|
|
606
|
+
role: "assistant",
|
|
607
|
+
content: "",
|
|
608
|
+
created_at: (/* @__PURE__ */ new Date()).toISOString(),
|
|
609
|
+
matches: []
|
|
610
|
+
};
|
|
611
|
+
t.messages.push(P);
|
|
612
|
+
const x = await d.chat(g.id, t.chat.id, {
|
|
613
|
+
sessionId: (w = t.streamingManager) == null ? void 0 : w.sessionId,
|
|
614
|
+
streamId: (v = t.streamingManager) == null ? void 0 : v.streamId,
|
|
615
|
+
messages: t.messages.slice(0, -1),
|
|
616
|
+
chatMode: t.chatMode,
|
|
617
|
+
append_chat: m
|
|
618
|
+
});
|
|
619
|
+
return u.track("agent-message-send", {
|
|
620
|
+
event: "success",
|
|
621
|
+
messages: t.messages.length + 1
|
|
622
|
+
}), P.context = x.context, x.result && (P.content = x.result, P.matches = x.matches, u.track("agent-message-received", {
|
|
623
|
+
latency: Date.now() - I,
|
|
624
|
+
messages: t.messages.length
|
|
625
|
+
}), (E = (C = a.callbacks).onNewMessage) == null || E.call(C, t.messages)), x;
|
|
626
|
+
} catch (I) {
|
|
627
|
+
throw t.messages[t.messages.length - 1].id === l && t.messages.pop(), u.track("agent-message-send", {
|
|
628
|
+
event: "error",
|
|
629
|
+
messages: t.messages.length
|
|
630
|
+
}), I;
|
|
631
|
+
}
|
|
632
|
+
},
|
|
633
|
+
rate(s, m, l) {
|
|
634
|
+
var w, v, C, E;
|
|
635
|
+
const h = t.messages.find((I) => I.id === s);
|
|
636
|
+
if (t.chat) {
|
|
637
|
+
if (!h)
|
|
638
|
+
throw new Error("Message not found");
|
|
639
|
+
} else
|
|
640
|
+
throw new Error("Chat is not initialized");
|
|
641
|
+
const c = ((w = h.matches) == null ? void 0 : w.map((I) => [I.document_id, I.id])) ?? [];
|
|
642
|
+
return u.track("agent-rate", {
|
|
643
|
+
event: l ? "update" : "create",
|
|
644
|
+
thumb: m === 1 ? "up" : "down",
|
|
645
|
+
knowledge_id: ((v = g.knowledge) == null ? void 0 : v.id) ?? "",
|
|
646
|
+
matches: c,
|
|
647
|
+
score: m
|
|
648
|
+
}), l ? d.updateRating(g.id, t.chat.id, l, {
|
|
649
|
+
knowledge_id: ((C = g.knowledge) == null ? void 0 : C.id) ?? "",
|
|
650
|
+
message_id: s,
|
|
651
|
+
matches: c,
|
|
652
|
+
score: m
|
|
653
|
+
}) : d.createRating(g.id, t.chat.id, {
|
|
654
|
+
knowledge_id: ((E = g.knowledge) == null ? void 0 : E.id) ?? "",
|
|
655
|
+
message_id: s,
|
|
656
|
+
matches: c,
|
|
657
|
+
score: m
|
|
658
|
+
});
|
|
659
|
+
},
|
|
660
|
+
deleteRate(s) {
|
|
661
|
+
var m;
|
|
662
|
+
if (!t.chat)
|
|
663
|
+
throw new Error("Chat is not initialized");
|
|
664
|
+
return u.track("agent-rate-delete", {
|
|
665
|
+
type: "text",
|
|
666
|
+
chat_id: (m = t.chat) == null ? void 0 : m.id,
|
|
667
|
+
id: s
|
|
668
|
+
}), d.deleteRating(g.id, t.chat.id, s);
|
|
669
|
+
},
|
|
670
|
+
speak(s) {
|
|
671
|
+
if (!t.streamingManager)
|
|
672
|
+
throw new Error("Streaming manager is not initialized");
|
|
673
|
+
function m() {
|
|
674
|
+
if (g.presenter) {
|
|
675
|
+
if (s.type === "text")
|
|
676
|
+
return {
|
|
677
|
+
type: "text",
|
|
678
|
+
provider: s.provider ? s.provider : g.presenter.voice,
|
|
679
|
+
input: s.input,
|
|
680
|
+
ssml: s.ssml || !1
|
|
681
|
+
};
|
|
682
|
+
if (s.type === "audio")
|
|
683
|
+
return {
|
|
684
|
+
type: "audio",
|
|
685
|
+
audio_url: s.audio_url
|
|
686
|
+
};
|
|
687
|
+
} else
|
|
688
|
+
throw new Error("Presenter is not initialized");
|
|
689
|
+
throw new Error("Invalid payload");
|
|
690
|
+
}
|
|
691
|
+
const l = m();
|
|
692
|
+
return u.track("agent-speak", l), t.streamingManager.speak({
|
|
693
|
+
script: l
|
|
694
|
+
});
|
|
484
695
|
}
|
|
485
696
|
};
|
|
486
697
|
}
|
|
487
698
|
export {
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
Y as
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
de as getAgent,
|
|
505
|
-
re as getAgentStreamArgs
|
|
699
|
+
R as ChatMode,
|
|
700
|
+
b as ChatProgress,
|
|
701
|
+
M as ConnectionState,
|
|
702
|
+
Z as DocumentType,
|
|
703
|
+
Y as KnowledgeType,
|
|
704
|
+
q as PlanGroup,
|
|
705
|
+
G as Providers,
|
|
706
|
+
Q as RateState,
|
|
707
|
+
A as StreamEvents,
|
|
708
|
+
T as StreamingState,
|
|
709
|
+
X as Subject,
|
|
710
|
+
j as UserPlan,
|
|
711
|
+
y as VideoType,
|
|
712
|
+
ee as VoiceAccess,
|
|
713
|
+
ve as createAgentManager,
|
|
714
|
+
pe as getAgent
|
|
506
715
|
};
|