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