@d-id/client-sdk 1.0.19-beta.1 → 1.0.19-beta.11
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 +257 -252
- package/dist/index.umd.cjs +1 -1
- package/dist/src/lib/createAgentManager.d.ts +2 -0
- package/dist/src/types/auth.d.ts +2 -2
- package/dist/src/types/entities/agents/chat.d.ts +6 -0
- package/dist/src/types/entities/agents/manager.d.ts +3 -3
- package/dist/src/types/stream/stream.d.ts +3 -2
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -1,243 +1,246 @@
|
|
|
1
|
-
const
|
|
2
|
-
function
|
|
1
|
+
const S = "https://api.d-id.com", J = "wss://notifications.d-id.com";
|
|
2
|
+
function U(e) {
|
|
3
3
|
if (e.type === "bearer")
|
|
4
4
|
return `Bearer ${e.token}`;
|
|
5
5
|
if (e.type === "basic")
|
|
6
6
|
return `Basic ${btoa(`${e.username}:${e.password}`)}`;
|
|
7
7
|
if (e.type === "key")
|
|
8
|
-
return `Client-Key ${e.clientKey}`;
|
|
8
|
+
return `Client-Key ${e.clientKey}.${e.externalId}`;
|
|
9
9
|
throw new Error(`Unknown auth type: ${e}`);
|
|
10
10
|
}
|
|
11
|
-
function
|
|
12
|
-
const
|
|
13
|
-
const a = await fetch(
|
|
14
|
-
...
|
|
11
|
+
function I(e, s = S) {
|
|
12
|
+
const n = async (t, r) => {
|
|
13
|
+
const a = await fetch(s + (t != null && t.startsWith("/") ? t : `/${t}`), {
|
|
14
|
+
...r,
|
|
15
15
|
headers: {
|
|
16
|
-
...
|
|
17
|
-
Authorization:
|
|
16
|
+
...r == null ? void 0 : r.headers,
|
|
17
|
+
Authorization: U(e),
|
|
18
18
|
"Content-Type": "application/json"
|
|
19
19
|
}
|
|
20
20
|
});
|
|
21
21
|
if (!a.ok) {
|
|
22
|
-
let
|
|
23
|
-
throw new Error(
|
|
22
|
+
let o = await a.text().catch(() => "Failed to fetch");
|
|
23
|
+
throw new Error(o);
|
|
24
24
|
}
|
|
25
25
|
return a.json();
|
|
26
26
|
};
|
|
27
27
|
return {
|
|
28
|
-
get(t,
|
|
29
|
-
return
|
|
30
|
-
...
|
|
28
|
+
get(t, r) {
|
|
29
|
+
return n(t, {
|
|
30
|
+
...r,
|
|
31
31
|
method: "GET"
|
|
32
32
|
});
|
|
33
33
|
},
|
|
34
|
-
post(t,
|
|
35
|
-
return
|
|
34
|
+
post(t, r, a) {
|
|
35
|
+
return n(t, {
|
|
36
36
|
...a,
|
|
37
|
-
body: JSON.stringify(
|
|
37
|
+
body: JSON.stringify(r),
|
|
38
38
|
method: "POST"
|
|
39
39
|
});
|
|
40
40
|
},
|
|
41
|
-
delete(t,
|
|
42
|
-
return
|
|
41
|
+
delete(t, r, a) {
|
|
42
|
+
return n(t, {
|
|
43
43
|
...a,
|
|
44
|
-
body: JSON.stringify(
|
|
44
|
+
body: JSON.stringify(r),
|
|
45
45
|
method: "DELETE"
|
|
46
46
|
});
|
|
47
47
|
},
|
|
48
|
-
patch(t,
|
|
49
|
-
return
|
|
48
|
+
patch(t, r, a) {
|
|
49
|
+
return n(t, {
|
|
50
50
|
...a,
|
|
51
|
-
body: JSON.stringify(
|
|
51
|
+
body: JSON.stringify(r),
|
|
52
52
|
method: "PATCH"
|
|
53
53
|
});
|
|
54
54
|
}
|
|
55
55
|
};
|
|
56
56
|
}
|
|
57
|
-
function
|
|
58
|
-
const
|
|
57
|
+
function B(e, s = S) {
|
|
58
|
+
const n = I(e, `${s}/agents`);
|
|
59
59
|
return {
|
|
60
|
-
create(t,
|
|
61
|
-
return
|
|
60
|
+
create(t, r) {
|
|
61
|
+
return n.post("/", t, r);
|
|
62
62
|
},
|
|
63
|
-
getAgents(t,
|
|
64
|
-
return
|
|
63
|
+
getAgents(t, r) {
|
|
64
|
+
return n.get(`/${t ? `?tag=${t}` : ""}`, r).then((a) => a ?? []);
|
|
65
65
|
},
|
|
66
|
-
getById(t,
|
|
67
|
-
return
|
|
66
|
+
getById(t, r) {
|
|
67
|
+
return n.get(`/${t}`, r);
|
|
68
68
|
},
|
|
69
|
-
delete(t,
|
|
70
|
-
return
|
|
69
|
+
delete(t, r) {
|
|
70
|
+
return n.delete(`/${t}`, void 0, r);
|
|
71
71
|
},
|
|
72
|
-
update(t,
|
|
73
|
-
return
|
|
72
|
+
update(t, r, a) {
|
|
73
|
+
return n.patch(`/${t}`, r, a);
|
|
74
74
|
},
|
|
75
|
-
newChat(t,
|
|
76
|
-
return
|
|
75
|
+
newChat(t, r) {
|
|
76
|
+
return n.post(`/${t}/chat`, void 0, r);
|
|
77
77
|
},
|
|
78
|
-
chat(t,
|
|
79
|
-
return
|
|
78
|
+
chat(t, r, a, o) {
|
|
79
|
+
return n.post(`/${t}/chat/${r}`, a, o);
|
|
80
80
|
}
|
|
81
81
|
};
|
|
82
82
|
}
|
|
83
|
-
function D(e,
|
|
84
|
-
const
|
|
83
|
+
function D(e, s = S) {
|
|
84
|
+
const n = I(e, `${s}/knowledge`);
|
|
85
85
|
return {
|
|
86
|
-
createKnowledge(t,
|
|
87
|
-
return
|
|
86
|
+
createKnowledge(t, r) {
|
|
87
|
+
return n.post("/", t, r);
|
|
88
88
|
},
|
|
89
89
|
getKnowledgeBase(t) {
|
|
90
|
-
return
|
|
90
|
+
return n.get("/", t);
|
|
91
91
|
},
|
|
92
|
-
getKnowledge(t,
|
|
93
|
-
return
|
|
92
|
+
getKnowledge(t, r) {
|
|
93
|
+
return n.get(`/${t}`, r);
|
|
94
94
|
},
|
|
95
|
-
deleteKnowledge(t,
|
|
96
|
-
return
|
|
95
|
+
deleteKnowledge(t, r) {
|
|
96
|
+
return n.delete(`/${t}`, void 0, r);
|
|
97
97
|
},
|
|
98
|
-
createDocument(t,
|
|
99
|
-
return
|
|
98
|
+
createDocument(t, r, a) {
|
|
99
|
+
return n.post(`/${t}/documents`, r, a);
|
|
100
100
|
},
|
|
101
|
-
deleteDocument(t,
|
|
102
|
-
return
|
|
101
|
+
deleteDocument(t, r, a) {
|
|
102
|
+
return n.delete(`/${t}/documents/${r}`, void 0, a);
|
|
103
103
|
},
|
|
104
|
-
getDocuments(t,
|
|
105
|
-
return
|
|
104
|
+
getDocuments(t, r) {
|
|
105
|
+
return n.get(`/${t}/documents`, r);
|
|
106
106
|
},
|
|
107
|
-
getDocument(t,
|
|
108
|
-
return
|
|
107
|
+
getDocument(t, r, a) {
|
|
108
|
+
return n.get(`/${t}/documents/${r}`, a);
|
|
109
109
|
},
|
|
110
|
-
getRecords(t,
|
|
111
|
-
return
|
|
110
|
+
getRecords(t, r, a) {
|
|
111
|
+
return n.get(`/${t}/documents/${r}/records`, a);
|
|
112
112
|
},
|
|
113
|
-
query(t,
|
|
114
|
-
return
|
|
115
|
-
query:
|
|
113
|
+
query(t, r, a) {
|
|
114
|
+
return n.post(`/${t}/query`, {
|
|
115
|
+
query: r
|
|
116
116
|
}, a);
|
|
117
117
|
}
|
|
118
118
|
};
|
|
119
119
|
}
|
|
120
|
-
function
|
|
121
|
-
const
|
|
120
|
+
function V(e, s = S) {
|
|
121
|
+
const n = I(e, `${s}/chats/ratings`);
|
|
122
122
|
return {
|
|
123
|
-
create(t,
|
|
124
|
-
return
|
|
123
|
+
create(t, r) {
|
|
124
|
+
return n.post("/", t, r);
|
|
125
125
|
},
|
|
126
|
-
getByKnowledge(t,
|
|
127
|
-
return
|
|
126
|
+
getByKnowledge(t, r) {
|
|
127
|
+
return n.get(`/${t}`, r).then((a) => a ?? []);
|
|
128
128
|
},
|
|
129
|
-
update(t,
|
|
130
|
-
return
|
|
129
|
+
update(t, r, a) {
|
|
130
|
+
return n.patch(`/${t}`, r, a);
|
|
131
131
|
},
|
|
132
|
-
delete(t,
|
|
133
|
-
return
|
|
132
|
+
delete(t, r) {
|
|
133
|
+
return n.delete(`/${t}`, r);
|
|
134
134
|
}
|
|
135
135
|
};
|
|
136
136
|
}
|
|
137
|
-
const
|
|
138
|
-
function
|
|
139
|
-
return new Promise((
|
|
137
|
+
const G = (e) => new Promise((s) => setTimeout(s, e));
|
|
138
|
+
function Q(e) {
|
|
139
|
+
return new Promise((s, n) => {
|
|
140
140
|
const {
|
|
141
141
|
callbacks: t,
|
|
142
|
-
host:
|
|
142
|
+
host: r,
|
|
143
143
|
auth: a
|
|
144
144
|
} = e, {
|
|
145
|
-
onMessage:
|
|
146
|
-
onOpen:
|
|
145
|
+
onMessage: o = null,
|
|
146
|
+
onOpen: l = null,
|
|
147
147
|
onClose: m = null,
|
|
148
148
|
onError: f = null
|
|
149
|
-
} = t || {}, g = new WebSocket(`${
|
|
150
|
-
g.onmessage =
|
|
151
|
-
console.log(d), f == null || f(d),
|
|
149
|
+
} = t || {}, g = new WebSocket(`${r}?authorization=${U(a)}`);
|
|
150
|
+
g.onmessage = o, g.onclose = m, g.onerror = (d) => {
|
|
151
|
+
console.log(d), f == null || f(d), n(d);
|
|
152
152
|
}, g.onopen = (d) => {
|
|
153
|
-
|
|
153
|
+
l == null || l(d), s(g);
|
|
154
154
|
};
|
|
155
155
|
});
|
|
156
156
|
}
|
|
157
|
-
async function
|
|
157
|
+
async function X(e) {
|
|
158
158
|
const {
|
|
159
|
-
retries:
|
|
159
|
+
retries: s = 1
|
|
160
160
|
} = e;
|
|
161
|
-
let
|
|
162
|
-
for (let t = 0; (
|
|
161
|
+
let n = null;
|
|
162
|
+
for (let t = 0; (n == null ? void 0 : n.readyState) !== WebSocket.OPEN; t++)
|
|
163
163
|
try {
|
|
164
|
-
|
|
165
|
-
} catch (
|
|
166
|
-
if (t ===
|
|
167
|
-
throw
|
|
168
|
-
await
|
|
164
|
+
n = await Q(e);
|
|
165
|
+
} catch (r) {
|
|
166
|
+
if (t === s)
|
|
167
|
+
throw r;
|
|
168
|
+
await G(t * 500);
|
|
169
169
|
}
|
|
170
|
-
return
|
|
170
|
+
return n;
|
|
171
171
|
}
|
|
172
|
-
async function
|
|
173
|
-
const t =
|
|
172
|
+
async function Y(e, s, n) {
|
|
173
|
+
const t = n ? [n] : [], r = await X({
|
|
174
174
|
auth: e,
|
|
175
|
-
host:
|
|
175
|
+
host: s,
|
|
176
176
|
callbacks: {
|
|
177
177
|
onMessage: (a) => {
|
|
178
|
-
const
|
|
179
|
-
t.forEach((
|
|
178
|
+
const o = JSON.parse(a.data);
|
|
179
|
+
t.forEach((l) => l(o.event, o));
|
|
180
180
|
}
|
|
181
181
|
}
|
|
182
182
|
});
|
|
183
183
|
return {
|
|
184
|
-
socket:
|
|
185
|
-
terminate: () =>
|
|
184
|
+
socket: r,
|
|
185
|
+
terminate: () => r.close(),
|
|
186
186
|
subscribeToEvents: (a) => t.push(a)
|
|
187
187
|
};
|
|
188
188
|
}
|
|
189
|
-
var
|
|
190
|
-
function
|
|
191
|
-
return e.presenter.type ===
|
|
192
|
-
videoType:
|
|
189
|
+
var Z = /* @__PURE__ */ ((e) => (e.Amazon = "amazon", e.Microsoft = "microsoft", e.Afflorithmics = "afflorithmics", e.Elevenlabs = "elevenlabs", e))(Z || {}), O = /* @__PURE__ */ ((e) => (e.Public = "public", e.Premium = "premium", e.Private = "private", e))(O || {}), y = /* @__PURE__ */ ((e) => (e.Start = "START", e.Stop = "STOP", e))(y || {}), A = /* @__PURE__ */ ((e) => (e.ChatAnswer = "chat/answer", e.ChatPartial = "chat/partial", e.StreamDone = "stream/done", e.StreamStarted = "stream/started", e.StreamFailed = "stream/error", e))(A || {}), j = /* @__PURE__ */ ((e) => (e.Unrated = "Unrated", e.Positive = "Positive", e.Negative = "Negative", e))(j || {}), ee = /* @__PURE__ */ ((e) => (e.Functional = "Functional", e.TextOnly = "TextOnly", e.Maintenance = "Maintenance", e))(ee || {}), E = /* @__PURE__ */ ((e) => (e.Embed = "embed", e.Query = "query", e.Partial = "partial", e.Answer = "answer", e.Complete = "done", e))(E || {}), te = /* @__PURE__ */ ((e) => (e.KnowledgeProcessing = "knowledge/processing", e.KnowledgeIndexing = "knowledge/indexing", e.KnowledgeFailed = "knowledge/error", e.KnowledgeDone = "knowledge/done", e))(te || {}), ne = /* @__PURE__ */ ((e) => (e.Knowledge = "knowledge", e.Document = "document", e.Record = "record", e))(ne || {}), re = /* @__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))(re || {}), _ = /* @__PURE__ */ ((e) => (e.Clip = "clip", e.Talk = "talk", e))(_ || {});
|
|
190
|
+
function ae(e) {
|
|
191
|
+
return e.presenter.type === _.Clip ? {
|
|
192
|
+
videoType: _.Clip,
|
|
193
193
|
driver_id: e.presenter.driver_id,
|
|
194
194
|
presenter_id: e.presenter.presenter_id
|
|
195
195
|
} : {
|
|
196
|
-
videoType:
|
|
196
|
+
videoType: _.Talk,
|
|
197
197
|
source_url: e.presenter.source_url
|
|
198
198
|
};
|
|
199
199
|
}
|
|
200
|
-
function
|
|
201
|
-
return new Promise(async (
|
|
202
|
-
const
|
|
203
|
-
...
|
|
200
|
+
function T(e, s, n, t) {
|
|
201
|
+
return new Promise(async (r, a) => {
|
|
202
|
+
const o = await de(ae(e), {
|
|
203
|
+
...s,
|
|
204
204
|
callbacks: {
|
|
205
|
-
...
|
|
206
|
-
onConnectionStateChange: async (
|
|
205
|
+
...s.callbacks,
|
|
206
|
+
onConnectionStateChange: async (l) => {
|
|
207
207
|
var m, f;
|
|
208
|
-
|
|
208
|
+
l === "connected" ? (t || (t = await n.newChat(e.id)), r({
|
|
209
209
|
chat: t,
|
|
210
|
-
streamingManager:
|
|
211
|
-
})) :
|
|
210
|
+
streamingManager: o
|
|
211
|
+
})) : l === "failed" && a(new Error("Cannot create connection")), (f = (m = s.callbacks).onConnectionStateChange) == null || f.call(m, l);
|
|
212
212
|
},
|
|
213
213
|
// TODO remove when webscoket will return partial
|
|
214
|
-
onMessage: (
|
|
214
|
+
onMessage: (l, m) => {
|
|
215
215
|
var f, g;
|
|
216
|
-
|
|
216
|
+
l === A.ChatAnswer && (console.log("ChatAnswer", l, m), (g = (f = s.callbacks).onChatEvents) == null || g.call(f, E.Answer, {
|
|
217
217
|
content: m,
|
|
218
|
-
event:
|
|
218
|
+
event: E.Answer
|
|
219
219
|
}));
|
|
220
220
|
}
|
|
221
221
|
}
|
|
222
222
|
});
|
|
223
223
|
});
|
|
224
224
|
}
|
|
225
|
-
|
|
226
|
-
|
|
225
|
+
function le(e, s, n) {
|
|
226
|
+
return B(s, n || S).getById(e);
|
|
227
|
+
}
|
|
228
|
+
async function ue(e, s) {
|
|
229
|
+
const n = s.baseURL || S, t = s.wsURL || J, r = new AbortController(), a = B(s.auth, n), o = V(s.auth, n), l = D(s.auth, n), m = await a.getById(e), f = await Y(s.auth, t, s.callbacks.onChatEvents);
|
|
227
230
|
let {
|
|
228
231
|
chat: g,
|
|
229
232
|
streamingManager: d
|
|
230
|
-
} = await
|
|
233
|
+
} = await T(m, s, a);
|
|
231
234
|
return {
|
|
232
235
|
agent: m,
|
|
233
236
|
async reconnectToChat() {
|
|
234
237
|
const {
|
|
235
238
|
streamingManager: c
|
|
236
|
-
} = await
|
|
239
|
+
} = await T(m, s, a, g);
|
|
237
240
|
d = c;
|
|
238
241
|
},
|
|
239
242
|
terminate() {
|
|
240
|
-
return
|
|
243
|
+
return r.abort(), f.terminate(), d.terminate();
|
|
241
244
|
},
|
|
242
245
|
chatId: g.id,
|
|
243
246
|
chat(c) {
|
|
@@ -246,14 +249,14 @@ async function ce(e, i) {
|
|
|
246
249
|
streamId: d.streamId,
|
|
247
250
|
messages: c
|
|
248
251
|
}, {
|
|
249
|
-
signal:
|
|
252
|
+
signal: r.signal
|
|
250
253
|
});
|
|
251
254
|
},
|
|
252
255
|
rate(c, p) {
|
|
253
|
-
return p ?
|
|
256
|
+
return p ? o.update(p, c) : o.create(c);
|
|
254
257
|
},
|
|
255
258
|
deleteRate(c) {
|
|
256
|
-
return
|
|
259
|
+
return o.delete(c);
|
|
257
260
|
},
|
|
258
261
|
speak(c) {
|
|
259
262
|
let p;
|
|
@@ -273,198 +276,198 @@ async function ce(e, i) {
|
|
|
273
276
|
},
|
|
274
277
|
getStarterMessages() {
|
|
275
278
|
var c, p;
|
|
276
|
-
return (c = m.knowledge) != null && c.id ?
|
|
279
|
+
return (c = m.knowledge) != null && c.id ? l.getKnowledge((p = m.knowledge) == null ? void 0 : p.id).then((w) => (w == null ? void 0 : w.starter_message) || []) : Promise.resolve([]);
|
|
277
280
|
}
|
|
278
281
|
};
|
|
279
282
|
}
|
|
280
|
-
function
|
|
281
|
-
const
|
|
283
|
+
function se(e, s) {
|
|
284
|
+
const n = I(e, s);
|
|
282
285
|
return {
|
|
283
286
|
createStream(t) {
|
|
284
|
-
return
|
|
287
|
+
return n.post("/clips/streams", {
|
|
285
288
|
driver_id: t.driver_id,
|
|
286
289
|
presenter_id: t.presenter_id,
|
|
287
290
|
compatibility_mode: t.compatibility_mode
|
|
288
291
|
});
|
|
289
292
|
},
|
|
290
|
-
startConnection(t,
|
|
291
|
-
return
|
|
293
|
+
startConnection(t, r, a) {
|
|
294
|
+
return n.post(`/clips/streams/${t}/sdp`, {
|
|
292
295
|
session_id: a,
|
|
293
|
-
answer:
|
|
296
|
+
answer: r
|
|
294
297
|
});
|
|
295
298
|
},
|
|
296
|
-
addIceCandidate(t,
|
|
297
|
-
return
|
|
299
|
+
addIceCandidate(t, r, a) {
|
|
300
|
+
return n.post(`/clips/streams/${t}/ice`, {
|
|
298
301
|
session_id: a,
|
|
299
|
-
...
|
|
302
|
+
...r
|
|
300
303
|
});
|
|
301
304
|
},
|
|
302
|
-
sendStreamRequest(t,
|
|
303
|
-
return
|
|
304
|
-
session_id:
|
|
305
|
+
sendStreamRequest(t, r, a) {
|
|
306
|
+
return n.post(`/clips/streams/${t}`, {
|
|
307
|
+
session_id: r,
|
|
305
308
|
...a
|
|
306
309
|
});
|
|
307
310
|
},
|
|
308
|
-
close(t,
|
|
309
|
-
return
|
|
310
|
-
session_id:
|
|
311
|
+
close(t, r) {
|
|
312
|
+
return n.delete(`/clips/streams/${t}`, {
|
|
313
|
+
session_id: r
|
|
311
314
|
});
|
|
312
315
|
}
|
|
313
316
|
};
|
|
314
317
|
}
|
|
315
|
-
function
|
|
316
|
-
const
|
|
318
|
+
function ie(e, s) {
|
|
319
|
+
const n = I(e, s);
|
|
317
320
|
return {
|
|
318
|
-
createStream(t,
|
|
319
|
-
return
|
|
321
|
+
createStream(t, r) {
|
|
322
|
+
return n.post("/talks/streams", {
|
|
320
323
|
source_url: t.source_url,
|
|
321
324
|
driver_url: t.driver_url,
|
|
322
325
|
face: t.face,
|
|
323
326
|
config: t.config
|
|
324
|
-
},
|
|
327
|
+
}, r);
|
|
325
328
|
},
|
|
326
|
-
startConnection(t,
|
|
327
|
-
return
|
|
329
|
+
startConnection(t, r, a, o) {
|
|
330
|
+
return n.post(`/talks/streams/${t}/sdp`, {
|
|
328
331
|
session_id: a,
|
|
329
|
-
answer:
|
|
330
|
-
},
|
|
332
|
+
answer: r
|
|
333
|
+
}, o);
|
|
331
334
|
},
|
|
332
|
-
addIceCandidate(t,
|
|
333
|
-
return
|
|
335
|
+
addIceCandidate(t, r, a, o) {
|
|
336
|
+
return n.post(`/talks/streams/${t}/ice`, {
|
|
334
337
|
session_id: a,
|
|
335
|
-
...
|
|
336
|
-
},
|
|
338
|
+
...r
|
|
339
|
+
}, o);
|
|
337
340
|
},
|
|
338
|
-
sendStreamRequest(t,
|
|
339
|
-
return
|
|
340
|
-
session_id:
|
|
341
|
+
sendStreamRequest(t, r, a, o) {
|
|
342
|
+
return n.post(`/talks/streams/${t}`, {
|
|
343
|
+
session_id: r,
|
|
341
344
|
...a
|
|
342
|
-
},
|
|
345
|
+
}, o);
|
|
343
346
|
},
|
|
344
|
-
close(t,
|
|
345
|
-
return
|
|
346
|
-
session_id:
|
|
347
|
+
close(t, r, a) {
|
|
348
|
+
return n.delete(`/talks/streams/${t}`, {
|
|
349
|
+
session_id: r
|
|
347
350
|
}, a);
|
|
348
351
|
}
|
|
349
352
|
};
|
|
350
353
|
}
|
|
351
|
-
function
|
|
352
|
-
return e.map((
|
|
354
|
+
function oe(e, s) {
|
|
355
|
+
return e.map((n, t) => t === 0 ? s ? {
|
|
353
356
|
index: t,
|
|
354
|
-
timestamp:
|
|
355
|
-
bytesReceived:
|
|
356
|
-
packetsReceived:
|
|
357
|
-
packetsLost:
|
|
358
|
-
jitter:
|
|
359
|
-
frameWidth:
|
|
360
|
-
frameHeight:
|
|
361
|
-
frameRate:
|
|
357
|
+
timestamp: n.timestamp,
|
|
358
|
+
bytesReceived: n.bytesReceived - s.bytesReceived,
|
|
359
|
+
packetsReceived: n.packetsReceived - s.packetsReceived,
|
|
360
|
+
packetsLost: n.packetsLost - s.packetsLost,
|
|
361
|
+
jitter: n.jitter,
|
|
362
|
+
frameWidth: n.frameWidth,
|
|
363
|
+
frameHeight: n.frameHeight,
|
|
364
|
+
frameRate: n.frameRate
|
|
362
365
|
} : {
|
|
363
366
|
index: t,
|
|
364
|
-
timestamp:
|
|
365
|
-
bytesReceived:
|
|
366
|
-
packetsReceived:
|
|
367
|
-
packetsLost:
|
|
368
|
-
jitter:
|
|
369
|
-
frameWidth:
|
|
370
|
-
frameHeight:
|
|
371
|
-
frameRate:
|
|
367
|
+
timestamp: n.timestamp,
|
|
368
|
+
bytesReceived: n.bytesReceived,
|
|
369
|
+
packetsReceived: n.packetsReceived,
|
|
370
|
+
packetsLost: n.packetsLost,
|
|
371
|
+
jitter: n.jitter,
|
|
372
|
+
frameWidth: n.frameWidth,
|
|
373
|
+
frameHeight: n.frameHeight,
|
|
374
|
+
frameRate: n.frameRate
|
|
372
375
|
} : {
|
|
373
376
|
index: t,
|
|
374
|
-
timestamp:
|
|
375
|
-
bytesReceived:
|
|
376
|
-
packetsReceived:
|
|
377
|
-
packetsLost:
|
|
378
|
-
jitter:
|
|
379
|
-
frameWidth:
|
|
380
|
-
frameHeight:
|
|
381
|
-
frameRate:
|
|
377
|
+
timestamp: n.timestamp,
|
|
378
|
+
bytesReceived: n.bytesReceived - e[t - 1].bytesReceived,
|
|
379
|
+
packetsReceived: n.packetsReceived - e[t - 1].packetsReceived,
|
|
380
|
+
packetsLost: n.packetsLost - e[t - 1].packetsLost,
|
|
381
|
+
jitter: n.jitter,
|
|
382
|
+
frameWidth: n.frameWidth,
|
|
383
|
+
frameHeight: n.frameHeight,
|
|
384
|
+
frameRate: n.frameRate
|
|
382
385
|
});
|
|
383
386
|
}
|
|
384
|
-
let
|
|
385
|
-
const v = (e,
|
|
386
|
-
async function
|
|
387
|
-
debug:
|
|
388
|
-
callbacks:
|
|
387
|
+
let N = !1;
|
|
388
|
+
const v = (e, s) => N && console.log(e, s), ce = (window.RTCPeerConnection || window.webkitRTCPeerConnection || window.mozRTCPeerConnection).bind(window);
|
|
389
|
+
async function de(e, {
|
|
390
|
+
debug: s = !1,
|
|
391
|
+
callbacks: n,
|
|
389
392
|
auth: t,
|
|
390
|
-
baseURL:
|
|
393
|
+
baseURL: r = S
|
|
391
394
|
}) {
|
|
392
|
-
|
|
395
|
+
N = s;
|
|
393
396
|
const a = {
|
|
394
|
-
...
|
|
397
|
+
...n
|
|
395
398
|
}, {
|
|
396
|
-
startConnection:
|
|
397
|
-
sendStreamRequest:
|
|
399
|
+
startConnection: o,
|
|
400
|
+
sendStreamRequest: l,
|
|
398
401
|
close: m,
|
|
399
402
|
createStream: f,
|
|
400
403
|
addIceCandidate: g
|
|
401
|
-
} = e.videoType ===
|
|
404
|
+
} = e.videoType === _.Clip ? se(t, r) : ie(t, r), {
|
|
402
405
|
id: d,
|
|
403
406
|
offer: c,
|
|
404
407
|
ice_servers: p,
|
|
405
|
-
session_id:
|
|
406
|
-
} = await f(e),
|
|
408
|
+
session_id: w
|
|
409
|
+
} = await f(e), u = new ce({
|
|
407
410
|
iceServers: p
|
|
408
|
-
}), b =
|
|
409
|
-
let $ = 0,
|
|
410
|
-
if (!
|
|
411
|
+
}), b = u.createDataChannel("JanusDataChannel"), k = [];
|
|
412
|
+
let $ = 0, x;
|
|
413
|
+
if (!w)
|
|
411
414
|
throw new Error("Could not create session_id");
|
|
412
|
-
|
|
413
|
-
v("peerConnection.onicecandidate",
|
|
414
|
-
candidate:
|
|
415
|
-
sdpMid:
|
|
416
|
-
sdpMLineIndex:
|
|
417
|
-
},
|
|
418
|
-
},
|
|
419
|
-
var
|
|
420
|
-
v("peerConnection.oniceconnectionstatechange => " +
|
|
421
|
-
},
|
|
422
|
-
var
|
|
423
|
-
v("peerConnection.ontrack",
|
|
424
|
-
}, b.onmessage = (
|
|
425
|
-
var
|
|
415
|
+
u.onicecandidate = (i) => {
|
|
416
|
+
v("peerConnection.onicecandidate", i), i.candidate && i.candidate.sdpMid && i.candidate.sdpMLineIndex !== null && g(d, {
|
|
417
|
+
candidate: i.candidate.candidate,
|
|
418
|
+
sdpMid: i.candidate.sdpMid,
|
|
419
|
+
sdpMLineIndex: i.candidate.sdpMLineIndex
|
|
420
|
+
}, w);
|
|
421
|
+
}, u.oniceconnectionstatechange = () => {
|
|
422
|
+
var i;
|
|
423
|
+
v("peerConnection.oniceconnectionstatechange => " + u.iceConnectionState), (i = a.onConnectionStateChange) == null || i.call(a, u.iceConnectionState);
|
|
424
|
+
}, u.ontrack = (i) => {
|
|
425
|
+
var h;
|
|
426
|
+
v("peerConnection.ontrack", i), console.log("peerConnection.ontrack streams->", i.streams), (h = a.onSrcObjectReady) == null || h.call(a, i.streams[0]);
|
|
427
|
+
}, b.onmessage = (i) => {
|
|
428
|
+
var h, L, F, H;
|
|
426
429
|
if (b.readyState === "open") {
|
|
427
|
-
const [
|
|
428
|
-
if (
|
|
429
|
-
$ = k.length,
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
430
|
+
const [C, M] = i.data.split(":");
|
|
431
|
+
if (C === A.StreamStarted)
|
|
432
|
+
console.log("StreamStarted", C, M), $ = k.length, x = setInterval(() => {
|
|
433
|
+
u.getStats().then((K) => {
|
|
434
|
+
K.forEach((R) => {
|
|
435
|
+
R.type === "inbound-rtp" && R.kind === "video" && k.push(R);
|
|
433
436
|
});
|
|
434
437
|
});
|
|
435
|
-
}, 1e3), (
|
|
436
|
-
else if (
|
|
437
|
-
clearInterval(
|
|
438
|
+
}, 1e3), (h = a.onVideoStateChange) == null || h.call(a, y.Start);
|
|
439
|
+
else if (C === A.StreamDone) {
|
|
440
|
+
console.log("StreamDone", C, M), clearInterval(x);
|
|
438
441
|
const P = k.slice($);
|
|
439
442
|
if (P) {
|
|
440
|
-
const
|
|
441
|
-
$ = k.length, (
|
|
443
|
+
const K = $ === 0 ? void 0 : k[$ - 1], R = oe(P, K);
|
|
444
|
+
$ = k.length, (L = a.onVideoStateChange) == null || L.call(a, y.Stop, R.sort((q, z) => z.packetsLost - q.packetsLost).slice(0, 5));
|
|
442
445
|
}
|
|
443
446
|
} else
|
|
444
|
-
(H = a.onMessage) == null || H.call(a,
|
|
447
|
+
C === A.StreamFailed ? (console.log("StreamFailed", C, M), (F = a.onVideoStateChange) == null || F.call(a, y.Stop, "BEN GOT FAILE")) : (H = a.onMessage) == null || H.call(a, C, decodeURIComponent(M));
|
|
445
448
|
}
|
|
446
|
-
}, await
|
|
447
|
-
const W = await
|
|
448
|
-
return v("create answer OK"), await
|
|
449
|
+
}, await u.setRemoteDescription(c), v("set remote description OK");
|
|
450
|
+
const W = await u.createAnswer();
|
|
451
|
+
return v("create answer OK"), await u.setLocalDescription(W), v("set local description OK"), await o(d, W, w), v("start connection OK"), {
|
|
449
452
|
/**
|
|
450
453
|
* Method to send request to server to get clip or talk depend on you payload
|
|
451
454
|
* @param payload
|
|
452
455
|
*/
|
|
453
|
-
speak(
|
|
454
|
-
return
|
|
456
|
+
speak(i) {
|
|
457
|
+
return l(d, w, i);
|
|
455
458
|
},
|
|
456
459
|
/**
|
|
457
460
|
* Method to close RTC connection
|
|
458
461
|
*/
|
|
459
462
|
async terminate() {
|
|
460
|
-
var
|
|
461
|
-
d && (
|
|
462
|
-
}), (
|
|
463
|
+
var i, h;
|
|
464
|
+
d && (u && (u.close(), u.oniceconnectionstatechange = null, u.onnegotiationneeded = null, u.onicecandidate = null, u.ontrack = null), await m(d, w).catch((L) => {
|
|
465
|
+
}), (i = a.onConnectionStateChange) == null || i.call(a, "closed"), (h = a.onVideoStateChange) == null || h.call(a, y.Stop));
|
|
463
466
|
},
|
|
464
467
|
/**
|
|
465
468
|
* Session identifier information, should be returned in the body of all streaming requests
|
|
466
469
|
*/
|
|
467
|
-
sessionId:
|
|
470
|
+
sessionId: w,
|
|
468
471
|
/**
|
|
469
472
|
* Id of current RTC stream
|
|
470
473
|
*/
|
|
@@ -474,26 +477,28 @@ async function oe(e, {
|
|
|
474
477
|
* @param eventName
|
|
475
478
|
* @param callback
|
|
476
479
|
*/
|
|
477
|
-
onCallback(
|
|
478
|
-
a[
|
|
480
|
+
onCallback(i, h) {
|
|
481
|
+
a[i] = h;
|
|
479
482
|
}
|
|
480
483
|
};
|
|
481
484
|
}
|
|
482
485
|
export {
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
486
|
+
ee as ChatMode,
|
|
487
|
+
E as ChatProgress,
|
|
488
|
+
re as DocumentType,
|
|
489
|
+
ne as KnowledgeType,
|
|
490
|
+
Z as Providers,
|
|
491
|
+
j as RateState,
|
|
492
|
+
Y as SocketManager,
|
|
489
493
|
A as StreamEvents,
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
494
|
+
te as Subject,
|
|
495
|
+
O as VoiceAccess,
|
|
496
|
+
ue as createAgentManager,
|
|
497
|
+
B as createAgentsApi,
|
|
498
|
+
I as createClient,
|
|
495
499
|
D as createKnowledgeApi,
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
500
|
+
V as createRatingsApi,
|
|
501
|
+
de as createStreamingManager,
|
|
502
|
+
le as getAgent,
|
|
503
|
+
ae as getAgentStreamArgs
|
|
499
504
|
};
|
package/dist/index.umd.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
(function(c,p){typeof exports=="object"&&typeof module<"u"?p(exports):typeof define=="function"&&define.amd?define(["exports"],p):(c=typeof globalThis<"u"?globalThis:c||self,p(c.index={}))})(this,function(c){"use strict";const p="https://api.d-id.com",O="wss://notifications.d-id.com";function E(e){if(e.type==="bearer")return`Bearer ${e.token}`;if(e.type==="basic")return`Basic ${btoa(`${e.username}:${e.password}`)}`;if(e.type==="key")return`Client-Key ${e.clientKey}`;throw new Error(`Unknown auth type: ${e}`)}function k(e,i=p){const a=async(t,n)=>{const r=await fetch(i+(t!=null&&t.startsWith("/")?t:`/${t}`),{...n,headers:{...n==null?void 0:n.headers,Authorization:E(e),"Content-Type":"application/json"}});if(!r.ok){let s=await r.text().catch(()=>"Failed to fetch");throw new Error(s)}return r.json()};return{get(t,n){return a(t,{...n,method:"GET"})},post(t,n,r){return a(t,{...r,body:JSON.stringify(n),method:"POST"})},delete(t,n,r){return a(t,{...r,body:JSON.stringify(n),method:"DELETE"})},patch(t,n,r){return a(t,{...r,body:JSON.stringify(n),method:"PATCH"})}}}function T(e,i=p){const a=k(e,`${i}/agents`);return{create(t,n){return a.post("/",t,n)},getAgents(t,n){return a.get(`/${t?`?tag=${t}`:""}`,n).then(r=>r??[])},getById(t,n){return a.get(`/${t}`,n)},delete(t,n){return a.delete(`/${t}`,void 0,n)},update(t,n,r){return a.patch(`/${t}`,n,r)},newChat(t,n){return a.post(`/${t}/chat`,void 0,n)},chat(t,n,r,s){return a.post(`/${t}/chat/${n}`,r,s)}}}function W(e,i=p){const a=k(e,`${i}/knowledge`);return{createKnowledge(t,n){return a.post("/",t,n)},getKnowledgeBase(t){return a.get("/",t)},getKnowledge(t,n){return a.get(`/${t}`,n)},deleteKnowledge(t,n){return a.delete(`/${t}`,void 0,n)},createDocument(t,n,r){return a.post(`/${t}/documents`,n,r)},deleteDocument(t,n,r){return a.delete(`/${t}/documents/${n}`,void 0,r)},getDocuments(t,n){return a.get(`/${t}/documents`,n)},getDocument(t,n,r){return a.get(`/${t}/documents/${n}`,r)},getRecords(t,n,r){return a.get(`/${t}/documents/${n}/records`,r)},query(t,n,r){return a.post(`/${t}/query`,{query:n},r)}}}function H(e,i=p){const a=k(e,`${i}/chats/ratings`);return{create(t,n){return a.post("/",t,n)},getByKnowledge(t,n){return a.get(`/${t}`,n).then(r=>r??[])},update(t,n,r){return a.patch(`/${t}`,n,r)},delete(t,n){return a.delete(`/${t}`,n)}}}const x=e=>new Promise(i=>setTimeout(i,e));function ee(e){return new Promise((i,a)=>{const{callbacks:t,host:n,auth:r}=e,{onMessage:s=null,onOpen:m=null,onClose:g=null,onError:h=null}=t||{},f=new WebSocket(`${n}?authorization=${E(r)}`);f.onmessage=s,f.onclose=g,f.onerror=l=>{console.log(l),h==null||h(l),a(l)},f.onopen=l=>{m==null||m(l),i(f)}})}async function te(e){const{retries:i=1}=e;let a=null;for(let t=0;(a==null?void 0:a.readyState)!==WebSocket.OPEN;t++)try{a=await ee(e)}catch(n){if(t===i)throw n;await x(t*500)}return a}async function U(e,i,a){const t=a?[a]:[],n=await te({auth:e,host:i,callbacks:{onMessage:r=>{const s=JSON.parse(r.data);t.forEach(m=>m(s.event,s))}}});return{socket:n,terminate:()=>n.close(),subscribeToEvents:r=>t.push(r)}}var q=(e=>(e.Amazon="amazon",e.Microsoft="microsoft",e.Afflorithmics="afflorithmics",e.Elevenlabs="elevenlabs",e))(q||{}),N=(e=>(e.Public="public",e.Premium="premium",e.Private="private",e))(N||{}),M=(e=>(e.Start="START",e.Stop="STOP",e))(M||{}),R=(e=>(e.ChatAnswer="chat/answer",e.ChatPartial="chat/partial",e.StreamDone="stream/done",e.StreamStarted="stream/started",e))(R||{}),z=(e=>(e.Unrated="Unrated",e.Positive="Positive",e.Negative="Negative",e))(z||{}),I=(e=>(e.Embed="embed",e.Query="query",e.Partial="partial",e.Answer="answer",e.Complete="done",e))(I||{}),B=(e=>(e.KnowledgeProcessing="knowledge/processing",e.KnowledgeIndexing="knowledge/indexing",e.KnowledgeFailed="knowledge/error",e.KnowledgeDone="knowledge/done",e))(B||{}),J=(e=>(e.Knowledge="knowledge",e.Document="document",e.Record="record",e))(J||{}),D=(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))(D||{}),$=(e=>(e.Clip="clip",e.Talk="talk",e))($||{});function F(e){return e.presenter.type===$.Clip?{videoType:$.Clip,driver_id:e.presenter.driver_id,presenter_id:e.presenter.presenter_id}:{videoType:$.Talk,source_url:e.presenter.source_url}}function V(e,i,a,t){return new Promise(async(n,r)=>{const s=await Q(F(e),{...i,callbacks:{...i.callbacks,onConnectionStateChange:async m=>{var g,h;m==="connected"?(t||(t=await a.newChat(e.id)),n({chat:t,streamingManager:s})):m==="failed"&&r(new Error("Cannot create connection")),(h=(g=i.callbacks).onConnectionStateChange)==null||h.call(g,m)},onMessage:(m,g)=>{var h,f;m===R.ChatPartial&&((f=(h=i.callbacks).onChatEvents)==null||f.call(h,I.Partial,{content:g,event:I.Partial}))}}})})}async function ne(e,i){const a=i.baseURL||p,t=i.wsURL||O,n=new AbortController,r=T(i.auth,a),s=H(i.auth,a),m=W(i.auth,a),g=await r.getById(e),h=await U(i.auth,t,i.callbacks.onChatEvents);let{chat:f,streamingManager:l}=await V(g,i,r);return{agent:g,async reconnectToChat(){const{streamingManager:d}=await V(g,i,r,f);l=d},terminate(){return n.abort(),h.terminate(),l.terminate()},chatId:f.id,chat(d){return r.chat(e,f.id,{sessionId:l.sessionId,streamId:l.streamId,messages:d},{signal:n.signal})},rate(d,w){return w?s.update(w,d):s.create(d)},deleteRate(d){return s.delete(d)},speak(d){let w;return d.type==="text"?w={script:{type:"text",provider:d.provider,input:d.input,ssml:d.ssml||!1}}:d.type==="audio"&&(w={script:{type:"audio",audio_url:d.audio_url}}),l.speak(w)},getStarterMessages(){var d,w;return(d=g.knowledge)!=null&&d.id?m.getKnowledge((w=g.knowledge)==null?void 0:w.id).then(v=>(v==null?void 0:v.starter_message)||[]):Promise.resolve([])}}}function ae(e,i){const a=k(e,i);return{createStream(t){return a.post("/clips/streams",{driver_id:t.driver_id,presenter_id:t.presenter_id,compatibility_mode:t.compatibility_mode})},startConnection(t,n,r){return a.post(`/clips/streams/${t}/sdp`,{session_id:r,answer:n})},addIceCandidate(t,n,r){return a.post(`/clips/streams/${t}/ice`,{session_id:r,...n})},sendStreamRequest(t,n,r){return a.post(`/clips/streams/${t}`,{session_id:n,...r})},close(t,n){return a.delete(`/clips/streams/${t}`,{session_id:n})}}}function re(e,i){const a=k(e,i);return{createStream(t,n){return a.post("/talks/streams",{source_url:t.source_url,driver_url:t.driver_url,face:t.face,config:t.config},n)},startConnection(t,n,r,s){return a.post(`/talks/streams/${t}/sdp`,{session_id:r,answer:n},s)},addIceCandidate(t,n,r,s){return a.post(`/talks/streams/${t}/ice`,{session_id:r,...n},s)},sendStreamRequest(t,n,r,s){return a.post(`/talks/streams/${t}`,{session_id:n,...r},s)},close(t,n,r){return a.delete(`/talks/streams/${t}`,{session_id:n},r)}}}function ie(e,i){return e.map((a,t)=>t===0?i?{index:t,timestamp:a.timestamp,bytesReceived:a.bytesReceived-i.bytesReceived,packetsReceived:a.packetsReceived-i.packetsReceived,packetsLost:a.packetsLost-i.packetsLost,jitter:a.jitter,frameWidth:a.frameWidth,frameHeight:a.frameHeight,frameRate:a.frameRate}:{index:t,timestamp:a.timestamp,bytesReceived:a.bytesReceived,packetsReceived:a.packetsReceived,packetsLost:a.packetsLost,jitter:a.jitter,frameWidth:a.frameWidth,frameHeight:a.frameHeight,frameRate:a.frameRate}:{index:t,timestamp:a.timestamp,bytesReceived:a.bytesReceived-e[t-1].bytesReceived,packetsReceived:a.packetsReceived-e[t-1].packetsReceived,packetsLost:a.packetsLost-e[t-1].packetsLost,jitter:a.jitter,frameWidth:a.frameWidth,frameHeight:a.frameHeight,frameRate:a.frameRate})}let G=!1;const S=(e,i)=>G&&console.log(e,i),se=(window.RTCPeerConnection||window.webkitRTCPeerConnection||window.mozRTCPeerConnection).bind(window);async function Q(e,{debug:i=!1,callbacks:a,auth:t,baseURL:n=p}){G=i;const r={...a},{startConnection:s,sendStreamRequest:m,close:g,createStream:h,addIceCandidate:f}=e.videoType===$.Clip?ae(t,n):re(t,n),{id:l,offer:d,ice_servers:w,session_id:v}=await h(e),u=new se({iceServers:w}),X=u.createDataChannel("JanusDataChannel"),y=[];let A=0,Y;if(!v)throw new Error("Could not create session_id");u.onicecandidate=o=>{S("peerConnection.onicecandidate",o),o.candidate&&o.candidate.sdpMid&&o.candidate.sdpMLineIndex!==null&&f(l,{candidate:o.candidate.candidate,sdpMid:o.candidate.sdpMid,sdpMLineIndex:o.candidate.sdpMLineIndex},v)},u.oniceconnectionstatechange=()=>{var o;S("peerConnection.oniceconnectionstatechange => "+u.iceConnectionState),(o=r.onConnectionStateChange)==null||o.call(r,u.iceConnectionState)},u.ontrack=o=>{var C;S("peerConnection.ontrack",o),(C=r.onSrcObjectReady)==null||C.call(r,o.streams[0])},X.onmessage=o=>{var C,P,j;if(X.readyState==="open"){const[b,oe]=o.data.split(":");if(b===R.StreamStarted)A=y.length,Y=setInterval(()=>{u.getStats().then(L=>{L.forEach(_=>{_.type==="inbound-rtp"&&_.kind==="video"&&y.push(_)})})},1e3),(C=r.onVideoStateChange)==null||C.call(r,M.Start);else if(b===R.StreamDone){clearInterval(Y);const K=y.slice(A);if(K){const L=A===0?void 0:y[A-1],_=ie(K,L);A=y.length,(P=r.onVideoStateChange)==null||P.call(r,M.Stop,_.sort((ce,de)=>de.packetsLost-ce.packetsLost).slice(0,5))}}else(j=r.onMessage)==null||j.call(r,b,decodeURIComponent(oe))}},await u.setRemoteDescription(d),S("set remote description OK");const Z=await u.createAnswer();return S("create answer OK"),await u.setLocalDescription(Z),S("set local description OK"),await s(l,Z,v),S("start connection OK"),{speak(o){return m(l,v,o)},async terminate(){var o,C;l&&(u&&(u.close(),u.oniceconnectionstatechange=null,u.onnegotiationneeded=null,u.onicecandidate=null,u.ontrack=null),await g(l,v).catch(P=>{}),(o=r.onConnectionStateChange)==null||o.call(r,"closed"),(C=r.onVideoStateChange)==null||C.call(r,M.Stop))},sessionId:v,streamId:l,onCallback(o,C){r[o]=C}}}c.ChatProgress=I,c.DocumentType=D,c.KnowledgeType=J,c.Providers=q,c.RateState=z,c.SocketManager=U,c.StreamEvents=R,c.Subject=B,c.VoiceAccess=N,c.createAgentManager=ne,c.createAgentsApi=T,c.createClient=k,c.createKnowledgeApi=W,c.createRatingsApi=H,c.createStreamingManager=Q,c.getAgentStreamArgs=F,Object.defineProperty(c,Symbol.toStringTag,{value:"Module"})});
|
|
1
|
+
(function(s,h){typeof exports=="object"&&typeof module<"u"?h(exports):typeof define=="function"&&define.amd?define(["exports"],h):(s=typeof globalThis<"u"?globalThis:s||self,h(s.index={}))})(this,function(s){"use strict";const h="https://api.d-id.com",te="wss://notifications.d-id.com";function W(e){if(e.type==="bearer")return`Bearer ${e.token}`;if(e.type==="basic")return`Basic ${btoa(`${e.username}:${e.password}`)}`;if(e.type==="key")return`Client-Key ${e.clientKey}.${e.externalId}`;throw new Error(`Unknown auth type: ${e}`)}function A(e,i=h){const n=async(t,r)=>{const a=await fetch(i+(t!=null&&t.startsWith("/")?t:`/${t}`),{...r,headers:{...r==null?void 0:r.headers,Authorization:W(e),"Content-Type":"application/json"}});if(!a.ok){let c=await a.text().catch(()=>"Failed to fetch");throw new Error(c)}return a.json()};return{get(t,r){return n(t,{...r,method:"GET"})},post(t,r,a){return n(t,{...a,body:JSON.stringify(r),method:"POST"})},delete(t,r,a){return n(t,{...a,body:JSON.stringify(r),method:"DELETE"})},patch(t,r,a){return n(t,{...a,body:JSON.stringify(r),method:"PATCH"})}}}function K(e,i=h){const n=A(e,`${i}/agents`);return{create(t,r){return n.post("/",t,r)},getAgents(t,r){return n.get(`/${t?`?tag=${t}`:""}`,r).then(a=>a??[])},getById(t,r){return n.get(`/${t}`,r)},delete(t,r){return n.delete(`/${t}`,void 0,r)},update(t,r,a){return n.patch(`/${t}`,r,a)},newChat(t,r){return n.post(`/${t}/chat`,void 0,r)},chat(t,r,a,c){return n.post(`/${t}/chat/${r}`,a,c)}}}function F(e,i=h){const n=A(e,`${i}/knowledge`);return{createKnowledge(t,r){return n.post("/",t,r)},getKnowledgeBase(t){return n.get("/",t)},getKnowledge(t,r){return n.get(`/${t}`,r)},deleteKnowledge(t,r){return n.delete(`/${t}`,void 0,r)},createDocument(t,r,a){return n.post(`/${t}/documents`,r,a)},deleteDocument(t,r,a){return n.delete(`/${t}/documents/${r}`,void 0,a)},getDocuments(t,r){return n.get(`/${t}/documents`,r)},getDocument(t,r,a){return n.get(`/${t}/documents/${r}`,a)},getRecords(t,r,a){return n.get(`/${t}/documents/${r}/records`,a)},query(t,r,a){return n.post(`/${t}/query`,{query:r},a)}}}function H(e,i=h){const n=A(e,`${i}/chats/ratings`);return{create(t,r){return n.post("/",t,r)},getByKnowledge(t,r){return n.get(`/${t}`,r).then(a=>a??[])},update(t,r,a){return n.patch(`/${t}`,r,a)},delete(t,r){return n.delete(`/${t}`,r)}}}const ne=e=>new Promise(i=>setTimeout(i,e));function re(e){return new Promise((i,n)=>{const{callbacks:t,host:r,auth:a}=e,{onMessage:c=null,onOpen:u=null,onClose:m=null,onError:w=null}=t||{},f=new WebSocket(`${r}?authorization=${W(a)}`);f.onmessage=c,f.onclose=m,f.onerror=l=>{console.log(l),w==null||w(l),n(l)},f.onopen=l=>{u==null||u(l),i(f)}})}async function ae(e){const{retries:i=1}=e;let n=null;for(let t=0;(n==null?void 0:n.readyState)!==WebSocket.OPEN;t++)try{n=await re(e)}catch(r){if(t===i)throw r;await ne(t*500)}return n}async function U(e,i,n){const t=n?[n]:[],r=await ae({auth:e,host:i,callbacks:{onMessage:a=>{const c=JSON.parse(a.data);t.forEach(u=>u(c.event,c))}}});return{socket:r,terminate:()=>r.close(),subscribeToEvents:a=>t.push(a)}}var B=(e=>(e.Amazon="amazon",e.Microsoft="microsoft",e.Afflorithmics="afflorithmics",e.Elevenlabs="elevenlabs",e))(B||{}),N=(e=>(e.Public="public",e.Premium="premium",e.Private="private",e))(N||{}),R=(e=>(e.Start="START",e.Stop="STOP",e))(R||{}),$=(e=>(e.ChatAnswer="chat/answer",e.ChatPartial="chat/partial",e.StreamDone="stream/done",e.StreamStarted="stream/started",e.StreamFailed="stream/error",e))($||{}),q=(e=>(e.Unrated="Unrated",e.Positive="Positive",e.Negative="Negative",e))(q||{}),z=(e=>(e.Functional="Functional",e.TextOnly="TextOnly",e.Maintenance="Maintenance",e))(z||{}),P=(e=>(e.Embed="embed",e.Query="query",e.Partial="partial",e.Answer="answer",e.Complete="done",e))(P||{}),J=(e=>(e.KnowledgeProcessing="knowledge/processing",e.KnowledgeIndexing="knowledge/indexing",e.KnowledgeFailed="knowledge/error",e.KnowledgeDone="knowledge/done",e))(J||{}),D=(e=>(e.Knowledge="knowledge",e.Document="document",e.Record="record",e))(D||{}),V=(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))(V||{}),y=(e=>(e.Clip="clip",e.Talk="talk",e))(y||{});function G(e){return e.presenter.type===y.Clip?{videoType:y.Clip,driver_id:e.presenter.driver_id,presenter_id:e.presenter.presenter_id}:{videoType:y.Talk,source_url:e.presenter.source_url}}function Q(e,i,n,t){return new Promise(async(r,a)=>{const c=await Y(G(e),{...i,callbacks:{...i.callbacks,onConnectionStateChange:async u=>{var m,w;u==="connected"?(t||(t=await n.newChat(e.id)),r({chat:t,streamingManager:c})):u==="failed"&&a(new Error("Cannot create connection")),(w=(m=i.callbacks).onConnectionStateChange)==null||w.call(m,u)},onMessage:(u,m)=>{var w,f;u===$.ChatAnswer&&(console.log("ChatAnswer",u,m),(f=(w=i.callbacks).onChatEvents)==null||f.call(w,P.Answer,{content:m,event:P.Answer}))}}})})}function ie(e,i,n){return K(i,n||h).getById(e)}async function se(e,i){const n=i.baseURL||h,t=i.wsURL||te,r=new AbortController,a=K(i.auth,n),c=H(i.auth,n),u=F(i.auth,n),m=await a.getById(e),w=await U(i.auth,t,i.callbacks.onChatEvents);let{chat:f,streamingManager:l}=await Q(m,i,a);return{agent:m,async reconnectToChat(){const{streamingManager:d}=await Q(m,i,a,f);l=d},terminate(){return r.abort(),w.terminate(),l.terminate()},chatId:f.id,chat(d){return a.chat(e,f.id,{sessionId:l.sessionId,streamId:l.streamId,messages:d},{signal:r.signal})},rate(d,p){return p?c.update(p,d):c.create(d)},deleteRate(d){return c.delete(d)},speak(d){let p;return d.type==="text"?p={script:{type:"text",provider:d.provider,input:d.input,ssml:d.ssml||!1}}:d.type==="audio"&&(p={script:{type:"audio",audio_url:d.audio_url}}),l.speak(p)},getStarterMessages(){var d,p;return(d=m.knowledge)!=null&&d.id?u.getKnowledge((p=m.knowledge)==null?void 0:p.id).then(C=>(C==null?void 0:C.starter_message)||[]):Promise.resolve([])}}}function oe(e,i){const n=A(e,i);return{createStream(t){return n.post("/clips/streams",{driver_id:t.driver_id,presenter_id:t.presenter_id,compatibility_mode:t.compatibility_mode})},startConnection(t,r,a){return n.post(`/clips/streams/${t}/sdp`,{session_id:a,answer:r})},addIceCandidate(t,r,a){return n.post(`/clips/streams/${t}/ice`,{session_id:a,...r})},sendStreamRequest(t,r,a){return n.post(`/clips/streams/${t}`,{session_id:r,...a})},close(t,r){return n.delete(`/clips/streams/${t}`,{session_id:r})}}}function ce(e,i){const n=A(e,i);return{createStream(t,r){return n.post("/talks/streams",{source_url:t.source_url,driver_url:t.driver_url,face:t.face,config:t.config},r)},startConnection(t,r,a,c){return n.post(`/talks/streams/${t}/sdp`,{session_id:a,answer:r},c)},addIceCandidate(t,r,a,c){return n.post(`/talks/streams/${t}/ice`,{session_id:a,...r},c)},sendStreamRequest(t,r,a,c){return n.post(`/talks/streams/${t}`,{session_id:r,...a},c)},close(t,r,a){return n.delete(`/talks/streams/${t}`,{session_id:r},a)}}}function de(e,i){return e.map((n,t)=>t===0?i?{index:t,timestamp:n.timestamp,bytesReceived:n.bytesReceived-i.bytesReceived,packetsReceived:n.packetsReceived-i.packetsReceived,packetsLost:n.packetsLost-i.packetsLost,jitter:n.jitter,frameWidth:n.frameWidth,frameHeight:n.frameHeight,frameRate:n.frameRate}:{index:t,timestamp:n.timestamp,bytesReceived:n.bytesReceived,packetsReceived:n.packetsReceived,packetsLost:n.packetsLost,jitter:n.jitter,frameWidth:n.frameWidth,frameHeight:n.frameHeight,frameRate:n.frameRate}:{index:t,timestamp:n.timestamp,bytesReceived:n.bytesReceived-e[t-1].bytesReceived,packetsReceived:n.packetsReceived-e[t-1].packetsReceived,packetsLost:n.packetsLost-e[t-1].packetsLost,jitter:n.jitter,frameWidth:n.frameWidth,frameHeight:n.frameHeight,frameRate:n.frameRate})}let X=!1;const S=(e,i)=>X&&console.log(e,i),le=(window.RTCPeerConnection||window.webkitRTCPeerConnection||window.mozRTCPeerConnection).bind(window);async function Y(e,{debug:i=!1,callbacks:n,auth:t,baseURL:r=h}){X=i;const a={...n},{startConnection:c,sendStreamRequest:u,close:m,createStream:w,addIceCandidate:f}=e.videoType===y.Clip?oe(t,r):ce(t,r),{id:l,offer:d,ice_servers:p,session_id:C}=await w(e),g=new le({iceServers:p}),Z=g.createDataChannel("JanusDataChannel"),_=[];let I=0,O;if(!C)throw new Error("Could not create session_id");g.onicecandidate=o=>{S("peerConnection.onicecandidate",o),o.candidate&&o.candidate.sdpMid&&o.candidate.sdpMLineIndex!==null&&f(l,{candidate:o.candidate.candidate,sdpMid:o.candidate.sdpMid,sdpMLineIndex:o.candidate.sdpMLineIndex},C)},g.oniceconnectionstatechange=()=>{var o;S("peerConnection.oniceconnectionstatechange => "+g.iceConnectionState),(o=a.onConnectionStateChange)==null||o.call(a,g.iceConnectionState)},g.ontrack=o=>{var v;S("peerConnection.ontrack",o),console.log("peerConnection.ontrack streams->",o.streams),(v=a.onSrcObjectReady)==null||v.call(a,o.streams[0])},Z.onmessage=o=>{var v,b,j,ee;if(Z.readyState==="open"){const[k,L]=o.data.split(":");if(k===$.StreamStarted)console.log("StreamStarted",k,L),I=_.length,O=setInterval(()=>{g.getStats().then(T=>{T.forEach(M=>{M.type==="inbound-rtp"&&M.kind==="video"&&_.push(M)})})},1e3),(v=a.onVideoStateChange)==null||v.call(a,R.Start);else if(k===$.StreamDone){console.log("StreamDone",k,L),clearInterval(O);const E=_.slice(I);if(E){const T=I===0?void 0:_[I-1],M=de(E,T);I=_.length,(b=a.onVideoStateChange)==null||b.call(a,R.Stop,M.sort((ue,ge)=>ge.packetsLost-ue.packetsLost).slice(0,5))}}else k===$.StreamFailed?(console.log("StreamFailed",k,L),(j=a.onVideoStateChange)==null||j.call(a,R.Stop,"BEN GOT FAILE")):(ee=a.onMessage)==null||ee.call(a,k,decodeURIComponent(L))}},await g.setRemoteDescription(d),S("set remote description OK");const x=await g.createAnswer();return S("create answer OK"),await g.setLocalDescription(x),S("set local description OK"),await c(l,x,C),S("start connection OK"),{speak(o){return u(l,C,o)},async terminate(){var o,v;l&&(g&&(g.close(),g.oniceconnectionstatechange=null,g.onnegotiationneeded=null,g.onicecandidate=null,g.ontrack=null),await m(l,C).catch(b=>{}),(o=a.onConnectionStateChange)==null||o.call(a,"closed"),(v=a.onVideoStateChange)==null||v.call(a,R.Stop))},sessionId:C,streamId:l,onCallback(o,v){a[o]=v}}}s.ChatMode=z,s.ChatProgress=P,s.DocumentType=V,s.KnowledgeType=D,s.Providers=B,s.RateState=q,s.SocketManager=U,s.StreamEvents=$,s.Subject=J,s.VoiceAccess=N,s.createAgentManager=se,s.createAgentsApi=K,s.createClient=A,s.createKnowledgeApi=F,s.createRatingsApi=H,s.createStreamingManager=Y,s.getAgent=ie,s.getAgentStreamArgs=G,Object.defineProperty(s,Symbol.toStringTag,{value:"Module"})});
|
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import { Agent, AgentManager, AgentManagerOptions, CreateStreamOptions } from '../types/index';
|
|
2
|
+
import { Auth } from '..';
|
|
2
3
|
export declare function getAgentStreamArgs(agent: Agent): CreateStreamOptions;
|
|
4
|
+
export declare function getAgent(agentId: string, auth: Auth, baseURL?: string): Promise<Agent>;
|
|
3
5
|
/**
|
|
4
6
|
* Creates a new Agent Manager instance for interacting with an agent, chat, and related connections.
|
|
5
7
|
*
|
package/dist/src/types/auth.d.ts
CHANGED
|
@@ -7,12 +7,12 @@ export interface BasicAuth {
|
|
|
7
7
|
username: string;
|
|
8
8
|
password: string;
|
|
9
9
|
}
|
|
10
|
-
export interface
|
|
10
|
+
export interface ClientKeyAuth {
|
|
11
11
|
type: 'key';
|
|
12
12
|
clientKey: string;
|
|
13
13
|
externalId: string;
|
|
14
14
|
}
|
|
15
|
-
export type Auth = BearerToken | BasicAuth |
|
|
15
|
+
export type Auth = BearerToken | BasicAuth | ClientKeyAuth;
|
|
16
16
|
export interface GetAuthParams {
|
|
17
17
|
token?: string | null;
|
|
18
18
|
username?: string | null;
|
|
@@ -36,10 +36,16 @@ export interface IRetrivalMetadata {
|
|
|
36
36
|
knowledge_id: string;
|
|
37
37
|
source_url: string;
|
|
38
38
|
}
|
|
39
|
+
export declare enum ChatMode {
|
|
40
|
+
Functional = "Functional",
|
|
41
|
+
TextOnly = "TextOnly",
|
|
42
|
+
Maintenance = "Maintenance"
|
|
43
|
+
}
|
|
39
44
|
export interface ChatResponse {
|
|
40
45
|
result?: string;
|
|
41
46
|
documentIds?: string[];
|
|
42
47
|
matches?: IRetrivalMetadata[];
|
|
48
|
+
chatMode?: ChatMode;
|
|
43
49
|
}
|
|
44
50
|
export interface Chat {
|
|
45
51
|
id: string;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { SupportedStreamScipt } from '../../../types/StreamScript';
|
|
2
2
|
import { Auth } from '../../auth';
|
|
3
|
-
import { SendStreamPayloadResponse,
|
|
3
|
+
import { SendStreamPayloadResponse, StreamingState } from '../../stream';
|
|
4
4
|
import { Agent } from './agent';
|
|
5
5
|
import { ChatResponse, Message, RatingEntity, RatingPayload } from './chat';
|
|
6
6
|
/**
|
|
@@ -30,7 +30,7 @@ export declare enum ChatProgress {
|
|
|
30
30
|
}
|
|
31
31
|
export type ChatProgressCallback = (progress: ChatProgress, data: string) => void;
|
|
32
32
|
export type ConnectionStateChangeCallback = (state: RTCIceConnectionState) => void;
|
|
33
|
-
export type VideoStateChangeCallback = (state: StreamingState,
|
|
33
|
+
export type VideoStateChangeCallback = (state: StreamingState, data: any) => void;
|
|
34
34
|
interface ManagerCallbacks {
|
|
35
35
|
/**
|
|
36
36
|
* Optional callback will be triggered each time the RTC connection changes state
|
|
@@ -41,7 +41,7 @@ interface ManagerCallbacks {
|
|
|
41
41
|
* Optional callback function that will be triggered each time video events happen
|
|
42
42
|
* @param state
|
|
43
43
|
*/
|
|
44
|
-
onVideoStateChange?(state: StreamingState): void;
|
|
44
|
+
onVideoStateChange?(state: StreamingState, data?: any): void;
|
|
45
45
|
/**
|
|
46
46
|
* Callback function that will be triggered each time the video stream starts or stops to update html element on webpage
|
|
47
47
|
* Required callback for SDK
|
|
@@ -11,12 +11,13 @@ export declare enum StreamEvents {
|
|
|
11
11
|
ChatAnswer = "chat/answer",
|
|
12
12
|
ChatPartial = "chat/partial",
|
|
13
13
|
StreamDone = "stream/done",
|
|
14
|
-
StreamStarted = "stream/started"
|
|
14
|
+
StreamStarted = "stream/started",
|
|
15
|
+
StreamFailed = "stream/error"
|
|
15
16
|
}
|
|
16
17
|
export interface ManagerCallbacks {
|
|
17
18
|
onMessage?: (event: string, data: string) => void;
|
|
18
19
|
onConnectionStateChange?: (state: RTCIceConnectionState) => void;
|
|
19
|
-
onVideoStateChange?: (state: StreamingState,
|
|
20
|
+
onVideoStateChange?: (state: StreamingState, data?: any) => void;
|
|
20
21
|
onSrcObjectReady?: (value: MediaStream) => void;
|
|
21
22
|
}
|
|
22
23
|
export type ManagerCallbackKeys = keyof ManagerCallbacks;
|