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