@d-id/client-sdk 0.0.1 → 0.0.2
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 +161 -116
- package/dist/index.umd.cjs +1 -1
- package/dist/lib/api/agents.d.ts +1 -1
- package/dist/lib/connectToSocket.d.ts +14 -0
- package/dist/lib/createAgentManager.d.ts +1 -1
- package/dist/lib/index.d.ts +1 -0
- package/dist/types/StreamScript.d.ts +14 -2
- package/dist/types/auth.d.ts +2 -0
- package/dist/types/entities/agents/chat.d.ts +1 -1
- package/dist/types/entities/knowledge/knowledge.d.ts +1 -0
- package/dist/types/stream/api/talk.d.ts +3 -1
- package/dist/types/stream/stream.d.ts +2 -2
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -1,21 +1,21 @@
|
|
|
1
|
-
function
|
|
2
|
-
return
|
|
1
|
+
function T(r) {
|
|
2
|
+
return r.type === "bearer" ? "Bearer " + r.token : r.type === "basic" ? "Basic " + btoa(`${r.username}:${r.password}`) : "Client-Key " + r.clientKey;
|
|
3
3
|
}
|
|
4
|
-
function
|
|
4
|
+
function f(r, i = "https://api.d-id.com") {
|
|
5
5
|
const n = async (e, t) => {
|
|
6
|
-
const
|
|
6
|
+
const o = await fetch(i + (e != null && e.startsWith("/") ? e : `/${e}`), {
|
|
7
7
|
...t,
|
|
8
8
|
headers: {
|
|
9
9
|
...t == null ? void 0 : t.headers,
|
|
10
|
-
Authorization:
|
|
10
|
+
Authorization: T(r),
|
|
11
11
|
"Content-Type": "application/json"
|
|
12
12
|
}
|
|
13
13
|
});
|
|
14
|
-
if (!
|
|
15
|
-
let a = await
|
|
14
|
+
if (!o.ok) {
|
|
15
|
+
let a = await o.text().catch(() => "Failed to fetch");
|
|
16
16
|
throw new Error(a);
|
|
17
17
|
}
|
|
18
|
-
return
|
|
18
|
+
return o.json();
|
|
19
19
|
};
|
|
20
20
|
return {
|
|
21
21
|
get(e, t) {
|
|
@@ -24,37 +24,37 @@ function h(o, s = "https://api.d-id.com") {
|
|
|
24
24
|
method: "GET"
|
|
25
25
|
});
|
|
26
26
|
},
|
|
27
|
-
post(e, t,
|
|
27
|
+
post(e, t, o) {
|
|
28
28
|
return n(e, {
|
|
29
|
-
...
|
|
29
|
+
...o,
|
|
30
30
|
body: JSON.stringify(t),
|
|
31
31
|
method: "POST"
|
|
32
32
|
});
|
|
33
33
|
},
|
|
34
|
-
delete(e, t,
|
|
34
|
+
delete(e, t, o) {
|
|
35
35
|
return n(e, {
|
|
36
|
-
...
|
|
36
|
+
...o,
|
|
37
37
|
body: JSON.stringify(t),
|
|
38
38
|
method: "DELETE"
|
|
39
39
|
});
|
|
40
40
|
},
|
|
41
|
-
patch(e, t,
|
|
41
|
+
patch(e, t, o) {
|
|
42
42
|
return n(e, {
|
|
43
|
-
...
|
|
43
|
+
...o,
|
|
44
44
|
body: JSON.stringify(t),
|
|
45
45
|
method: "PATCH"
|
|
46
46
|
});
|
|
47
47
|
}
|
|
48
48
|
};
|
|
49
49
|
}
|
|
50
|
-
function
|
|
51
|
-
const n =
|
|
50
|
+
function K(r, i = "https://api.d-id.com") {
|
|
51
|
+
const n = f(r, `${i}/agents`);
|
|
52
52
|
return {
|
|
53
53
|
create(e, t) {
|
|
54
54
|
return n.post("/", e, t);
|
|
55
55
|
},
|
|
56
56
|
getAgents(e, t) {
|
|
57
|
-
return n.get(`/${e ? `?tag=${e}` : ""}`, t).then((
|
|
57
|
+
return n.get(`/${e ? `?tag=${e}` : ""}`, t).then((o) => o ?? []);
|
|
58
58
|
},
|
|
59
59
|
getById(e, t) {
|
|
60
60
|
return n.get(`/${e}`, t);
|
|
@@ -62,19 +62,19 @@ function R(o, s = "https://api.d-id.com") {
|
|
|
62
62
|
delete(e, t) {
|
|
63
63
|
return n.delete(`/${e}`, void 0, t);
|
|
64
64
|
},
|
|
65
|
-
update(e, t,
|
|
66
|
-
return n.patch(`/${e}`, t,
|
|
65
|
+
update(e, t, o) {
|
|
66
|
+
return n.patch(`/${e}`, t, o);
|
|
67
67
|
},
|
|
68
68
|
newChat(e, t) {
|
|
69
69
|
return n.post(`/${e}/chat`, void 0, t);
|
|
70
70
|
},
|
|
71
|
-
chat(e, t,
|
|
72
|
-
return n.post(`/${e}/chat/${t}`,
|
|
71
|
+
chat(e, t, o, a) {
|
|
72
|
+
return n.post(`/${e}/chat/${t}`, o, a);
|
|
73
73
|
}
|
|
74
74
|
};
|
|
75
75
|
}
|
|
76
|
-
function
|
|
77
|
-
const n =
|
|
76
|
+
function E(r, i = "https://api.d-id.com") {
|
|
77
|
+
const n = f(r, `${i}/knowledge`);
|
|
78
78
|
return {
|
|
79
79
|
createKnowledge(e, t) {
|
|
80
80
|
return n.post("/", e, t);
|
|
@@ -88,65 +88,109 @@ function P(o, s = "https://api.d-id.com") {
|
|
|
88
88
|
deleteKnowledge(e, t) {
|
|
89
89
|
return n.delete(`/${e}`, void 0, t);
|
|
90
90
|
},
|
|
91
|
-
createDocument(e, t,
|
|
92
|
-
return n.post(`/${e}/documents`, t,
|
|
91
|
+
createDocument(e, t, o) {
|
|
92
|
+
return n.post(`/${e}/documents`, t, o);
|
|
93
93
|
},
|
|
94
|
-
deleteDocument(e, t,
|
|
95
|
-
return n.delete(`/${e}/documents/${t}`, void 0,
|
|
94
|
+
deleteDocument(e, t, o) {
|
|
95
|
+
return n.delete(`/${e}/documents/${t}`, void 0, o);
|
|
96
96
|
},
|
|
97
97
|
getDocuments(e, t) {
|
|
98
98
|
return n.get(`/${e}/documents`, t);
|
|
99
99
|
},
|
|
100
|
-
getDocument(e, t,
|
|
101
|
-
return n.get(`/${e}/documents/${t}`,
|
|
100
|
+
getDocument(e, t, o) {
|
|
101
|
+
return n.get(`/${e}/documents/${t}`, o);
|
|
102
102
|
},
|
|
103
|
-
getRecords(e, t,
|
|
104
|
-
return n.get(`/${e}/documents/${t}/records`,
|
|
103
|
+
getRecords(e, t, o) {
|
|
104
|
+
return n.get(`/${e}/documents/${t}/records`, o);
|
|
105
105
|
},
|
|
106
|
-
query(e, t,
|
|
106
|
+
query(e, t, o) {
|
|
107
107
|
return n.post(`/${e}/query`, {
|
|
108
108
|
query: t
|
|
109
|
-
},
|
|
109
|
+
}, o);
|
|
110
110
|
}
|
|
111
111
|
};
|
|
112
112
|
}
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
113
|
+
function R(r) {
|
|
114
|
+
if (r.type === "bearer")
|
|
115
|
+
return `Bearer ${r.token}`;
|
|
116
|
+
if (r.type === "basic")
|
|
117
|
+
return `Basic ${btoa(`${r.username}:${r.password}`)}`;
|
|
118
|
+
if (r.type === "key")
|
|
119
|
+
return `Client-Key ${r.clientKey}`;
|
|
120
|
+
throw new Error(`Unknown auth type: ${r}`);
|
|
121
|
+
}
|
|
122
|
+
const I = (r) => new Promise((i) => setTimeout(i, r));
|
|
123
|
+
function v(r) {
|
|
124
|
+
return new Promise((i, n) => {
|
|
125
|
+
const {
|
|
126
|
+
callbacks: e,
|
|
127
|
+
host: t,
|
|
128
|
+
auth: o
|
|
129
|
+
} = r, {
|
|
130
|
+
onMessage: a,
|
|
131
|
+
onOpen: p,
|
|
132
|
+
onClose: l = null,
|
|
133
|
+
onError: g
|
|
134
|
+
} = e, c = new WebSocket(`${t}?authorization=${R(o)}`);
|
|
135
|
+
c.onmessage = a, c.onclose = l, c.onerror = (m) => {
|
|
136
|
+
console.log(m), g == null || g(m), n(m);
|
|
137
|
+
}, c.onopen = (m) => {
|
|
138
|
+
p == null || p(m), i(c);
|
|
139
|
+
};
|
|
140
|
+
});
|
|
141
|
+
}
|
|
142
|
+
async function q(r) {
|
|
143
|
+
const {
|
|
144
|
+
retries: i = 1
|
|
145
|
+
} = r;
|
|
146
|
+
let n = null;
|
|
147
|
+
for (let e = 0; (n == null ? void 0 : n.readyState) !== WebSocket.OPEN; e++)
|
|
148
|
+
try {
|
|
149
|
+
n = await v(r);
|
|
150
|
+
} catch (t) {
|
|
151
|
+
if (e === i)
|
|
152
|
+
throw t;
|
|
153
|
+
await I(e * 500);
|
|
154
|
+
}
|
|
155
|
+
return n;
|
|
156
|
+
}
|
|
157
|
+
var h = /* @__PURE__ */ ((r) => (r.Start = "START", r.Stop = "STOP", r))(h || {}), $ = /* @__PURE__ */ ((r) => (r.Clip = "clip", r.Talk = "talk", r))($ || {});
|
|
158
|
+
function D(r) {
|
|
159
|
+
return r.presenter.type === $.Clip ? {
|
|
160
|
+
videoType: $.Clip,
|
|
161
|
+
driver_id: r.presenter.driver_id,
|
|
162
|
+
presenter_id: r.presenter.presenter_id
|
|
119
163
|
} : {
|
|
120
|
-
videoType:
|
|
121
|
-
source_url:
|
|
164
|
+
videoType: $.Talk,
|
|
165
|
+
source_url: r.presenter.source_url
|
|
122
166
|
};
|
|
123
167
|
}
|
|
124
|
-
async function
|
|
125
|
-
callbacks:
|
|
168
|
+
async function j(r, {
|
|
169
|
+
callbacks: i,
|
|
126
170
|
...n
|
|
127
171
|
}) {
|
|
128
|
-
const e = new AbortController(), t =
|
|
129
|
-
terminate:
|
|
130
|
-
sessionId:
|
|
131
|
-
streamId:
|
|
132
|
-
} = await D(
|
|
172
|
+
const e = new AbortController(), t = K(n.auth, n.baseURL), o = await t.getById(r), a = await t.newChat(r), {
|
|
173
|
+
terminate: p,
|
|
174
|
+
sessionId: l,
|
|
175
|
+
streamId: g
|
|
176
|
+
} = await B(D(o), {
|
|
133
177
|
...n,
|
|
134
178
|
callbacks: {
|
|
135
|
-
onSrcObjectReady:
|
|
136
|
-
onVideoStateChange:
|
|
137
|
-
onConnectionStateChange:
|
|
179
|
+
onSrcObjectReady: i.onSrcObjectReady,
|
|
180
|
+
onVideoStateChange: i == null ? void 0 : i.onVideoStateChange,
|
|
181
|
+
onConnectionStateChange: i.onConnectionStateChange
|
|
138
182
|
}
|
|
139
183
|
});
|
|
140
184
|
return {
|
|
141
|
-
agent:
|
|
185
|
+
agent: o,
|
|
142
186
|
terminate() {
|
|
143
|
-
return e.abort(),
|
|
187
|
+
return e.abort(), p();
|
|
144
188
|
},
|
|
145
189
|
chatId: a.id,
|
|
146
190
|
chat(c) {
|
|
147
|
-
return t.chat(
|
|
148
|
-
sessionId:
|
|
149
|
-
streamId:
|
|
191
|
+
return t.chat(r, a.id, {
|
|
192
|
+
sessionId: l,
|
|
193
|
+
streamId: g,
|
|
150
194
|
messages: c
|
|
151
195
|
}, {
|
|
152
196
|
signal: e.signal
|
|
@@ -154,8 +198,8 @@ async function M(o, {
|
|
|
154
198
|
}
|
|
155
199
|
};
|
|
156
200
|
}
|
|
157
|
-
function
|
|
158
|
-
const n =
|
|
201
|
+
function P(r, i) {
|
|
202
|
+
const n = f(r, i);
|
|
159
203
|
return {
|
|
160
204
|
createStream(e) {
|
|
161
205
|
return n.post("/clips/streams", {
|
|
@@ -164,22 +208,22 @@ function O(o, s) {
|
|
|
164
208
|
compatibility_mode: e.compatibility_mode
|
|
165
209
|
});
|
|
166
210
|
},
|
|
167
|
-
startConnection(e, t,
|
|
211
|
+
startConnection(e, t, o) {
|
|
168
212
|
return n.post(`/clips/streams/${e}/sdp`, {
|
|
169
|
-
session_id:
|
|
213
|
+
session_id: o,
|
|
170
214
|
answer: t
|
|
171
215
|
});
|
|
172
216
|
},
|
|
173
|
-
addIceCandidate(e, t,
|
|
217
|
+
addIceCandidate(e, t, o) {
|
|
174
218
|
return n.post(`/clips/streams/${e}/ice`, {
|
|
175
|
-
session_id:
|
|
219
|
+
session_id: o,
|
|
176
220
|
...t
|
|
177
221
|
});
|
|
178
222
|
},
|
|
179
|
-
sendStreamRequest(e, t,
|
|
223
|
+
sendStreamRequest(e, t, o) {
|
|
180
224
|
return n.post(`/clips/streams/${e}`, {
|
|
181
225
|
session_id: t,
|
|
182
|
-
...
|
|
226
|
+
...o
|
|
183
227
|
});
|
|
184
228
|
},
|
|
185
229
|
close(e, t) {
|
|
@@ -189,8 +233,8 @@ function O(o, s) {
|
|
|
189
233
|
}
|
|
190
234
|
};
|
|
191
235
|
}
|
|
192
|
-
function
|
|
193
|
-
const n =
|
|
236
|
+
function M(r, i) {
|
|
237
|
+
const n = f(r, i);
|
|
194
238
|
return {
|
|
195
239
|
createStream(e, t) {
|
|
196
240
|
return n.post("/talks/streams", {
|
|
@@ -200,90 +244,91 @@ function v(o, s) {
|
|
|
200
244
|
config: e.config
|
|
201
245
|
}, t);
|
|
202
246
|
},
|
|
203
|
-
startConnection(e, t,
|
|
247
|
+
startConnection(e, t, o, a) {
|
|
204
248
|
return n.post(`/talks/streams/${e}/sdp`, {
|
|
205
|
-
session_id:
|
|
249
|
+
session_id: o,
|
|
206
250
|
answer: t
|
|
207
251
|
}, a);
|
|
208
252
|
},
|
|
209
|
-
addIceCandidate(e, t,
|
|
253
|
+
addIceCandidate(e, t, o, a) {
|
|
210
254
|
return n.post(`/talks/streams/${e}/ice`, {
|
|
211
|
-
session_id:
|
|
255
|
+
session_id: o,
|
|
212
256
|
...t
|
|
213
257
|
}, a);
|
|
214
258
|
},
|
|
215
|
-
sendStreamRequest(e, t,
|
|
259
|
+
sendStreamRequest(e, t, o, a) {
|
|
216
260
|
return n.post(`/talks/streams/${e}`, {
|
|
217
261
|
session_id: t,
|
|
218
|
-
...
|
|
262
|
+
...o
|
|
219
263
|
}, a);
|
|
220
264
|
},
|
|
221
|
-
close(e, t,
|
|
265
|
+
close(e, t, o) {
|
|
222
266
|
return n.delete(`/talks/streams/${e}`, {
|
|
223
267
|
session_id: t
|
|
224
|
-
},
|
|
268
|
+
}, o);
|
|
225
269
|
}
|
|
226
270
|
};
|
|
227
271
|
}
|
|
228
272
|
let _ = !1;
|
|
229
|
-
const
|
|
230
|
-
async function
|
|
231
|
-
debug:
|
|
273
|
+
const C = (r, i) => _ && console.log(r, i), O = (window.RTCPeerConnection || window.webkitRTCPeerConnection || window.mozRTCPeerConnection).bind(window);
|
|
274
|
+
async function B(r, {
|
|
275
|
+
debug: i = !1,
|
|
232
276
|
callbacks: n,
|
|
233
277
|
auth: e,
|
|
234
278
|
baseURL: t = "https://api.d-id.com"
|
|
235
279
|
}) {
|
|
236
|
-
_ =
|
|
280
|
+
_ = i;
|
|
237
281
|
const {
|
|
238
|
-
startConnection:
|
|
282
|
+
startConnection: o,
|
|
239
283
|
sendStreamRequest: a,
|
|
240
|
-
close:
|
|
241
|
-
createStream:
|
|
242
|
-
addIceCandidate:
|
|
243
|
-
} =
|
|
284
|
+
close: p,
|
|
285
|
+
createStream: l,
|
|
286
|
+
addIceCandidate: g
|
|
287
|
+
} = r.videoType === $.Clip ? P(e, t) : M(e, t), {
|
|
244
288
|
id: c,
|
|
245
|
-
offer:
|
|
246
|
-
ice_servers:
|
|
247
|
-
session_id:
|
|
248
|
-
} = await
|
|
249
|
-
iceServers:
|
|
250
|
-
}),
|
|
251
|
-
if (!
|
|
289
|
+
offer: m,
|
|
290
|
+
ice_servers: A,
|
|
291
|
+
session_id: w
|
|
292
|
+
} = await l(r), d = new O({
|
|
293
|
+
iceServers: A
|
|
294
|
+
}), y = d.createDataChannel("JanusDataChannel");
|
|
295
|
+
if (!w)
|
|
252
296
|
throw new Error("Could not create session_id");
|
|
253
|
-
d.onicecandidate = (
|
|
254
|
-
|
|
255
|
-
candidate:
|
|
256
|
-
sdpMid:
|
|
257
|
-
sdpMLineIndex:
|
|
258
|
-
},
|
|
297
|
+
d.onicecandidate = (s) => {
|
|
298
|
+
C("peerConnection.onicecandidate", s), s.candidate && s.candidate.sdpMid && s.candidate.sdpMLineIndex !== null && g(c, {
|
|
299
|
+
candidate: s.candidate.candidate,
|
|
300
|
+
sdpMid: s.candidate.sdpMid,
|
|
301
|
+
sdpMLineIndex: s.candidate.sdpMLineIndex
|
|
302
|
+
}, w);
|
|
259
303
|
}, d.oniceconnectionstatechange = () => {
|
|
260
|
-
var
|
|
261
|
-
|
|
262
|
-
}, d.ontrack = (
|
|
304
|
+
var s;
|
|
305
|
+
C("peerConnection.oniceconnectionstatechange => " + d.iceConnectionState), (s = n.onConnectionStateChange) == null || s.call(n, d.iceConnectionState);
|
|
306
|
+
}, d.ontrack = (s) => {
|
|
263
307
|
var u;
|
|
264
|
-
|
|
265
|
-
},
|
|
308
|
+
C("peerConnection.ontrack", s), (u = n.onSrcObjectReady) == null || u.call(n, s.streams[0]);
|
|
309
|
+
}, y.onmessage = (s) => {
|
|
266
310
|
var u;
|
|
267
|
-
|
|
268
|
-
}, await d.setRemoteDescription(
|
|
269
|
-
const
|
|
270
|
-
return
|
|
271
|
-
speak(
|
|
272
|
-
return a(c,
|
|
311
|
+
y.readyState === "open" && ((u = n.onVideoStateChange) == null || u.call(n, s.data === "stream/done" ? h.Stop : h.Start));
|
|
312
|
+
}, await d.setRemoteDescription(m), C("set remote description OK");
|
|
313
|
+
const S = await d.createAnswer();
|
|
314
|
+
return C("create answer OK"), await d.setLocalDescription(S), C("set local description OK"), await o(c, S, w), C("start connection OK"), {
|
|
315
|
+
speak(s) {
|
|
316
|
+
return a(c, w, s);
|
|
273
317
|
},
|
|
274
318
|
async terminate() {
|
|
275
|
-
var
|
|
276
|
-
c && (d && (d.close(), d.oniceconnectionstatechange = null, d.onnegotiationneeded = null, d.onicecandidate = null, d.ontrack = null), await
|
|
319
|
+
var s, u;
|
|
320
|
+
c && (d && (d.close(), d.oniceconnectionstatechange = null, d.onnegotiationneeded = null, d.onicecandidate = null, d.ontrack = null), await p(c, w), (s = n.onConnectionStateChange) == null || s.call(n, "closed"), (u = n.onVideoStateChange) == null || u.call(n, h.Stop));
|
|
277
321
|
},
|
|
278
|
-
sessionId:
|
|
322
|
+
sessionId: w,
|
|
279
323
|
streamId: c
|
|
280
324
|
};
|
|
281
325
|
}
|
|
282
326
|
export {
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
327
|
+
q as connectToSocket,
|
|
328
|
+
j as createAgentManager,
|
|
329
|
+
K as createAgentsApi,
|
|
330
|
+
f as createClient,
|
|
331
|
+
E as createKnowledgeApi,
|
|
332
|
+
B as createStreamingManager,
|
|
333
|
+
D as getAgentStreamArgs
|
|
289
334
|
};
|
package/dist/index.umd.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
(function(
|
|
1
|
+
(function(u,w){typeof exports=="object"&&typeof module<"u"?w(exports):typeof define=="function"&&define.amd?define(["exports"],w):(u=typeof globalThis<"u"?globalThis:u||self,w(u.index={}))})(this,function(u){"use strict";function w(r){return r.type==="bearer"?"Bearer "+r.token:r.type==="basic"?"Basic "+btoa(`${r.username}:${r.password}`):"Client-Key "+r.clientKey}function h(r,i="https://api.d-id.com"){const n=async(e,t)=>{const o=await fetch(i+(e!=null&&e.startsWith("/")?e:`/${e}`),{...t,headers:{...t==null?void 0:t.headers,Authorization:w(r),"Content-Type":"application/json"}});if(!o.ok){let a=await o.text().catch(()=>"Failed to fetch");throw new Error(a)}return o.json()};return{get(e,t){return n(e,{...t,method:"GET"})},post(e,t,o){return n(e,{...o,body:JSON.stringify(t),method:"POST"})},delete(e,t,o){return n(e,{...o,body:JSON.stringify(t),method:"DELETE"})},patch(e,t,o){return n(e,{...o,body:JSON.stringify(t),method:"PATCH"})}}}function _(r,i="https://api.d-id.com"){const n=h(r,`${i}/agents`);return{create(e,t){return n.post("/",e,t)},getAgents(e,t){return n.get(`/${e?`?tag=${e}`:""}`,t).then(o=>o??[])},getById(e,t){return n.get(`/${e}`,t)},delete(e,t){return n.delete(`/${e}`,void 0,t)},update(e,t,o){return n.patch(`/${e}`,t,o)},newChat(e,t){return n.post(`/${e}/chat`,void 0,t)},chat(e,t,o,a){return n.post(`/${e}/chat/${t}`,o,a)}}}function I(r,i="https://api.d-id.com"){const n=h(r,`${i}/knowledge`);return{createKnowledge(e,t){return n.post("/",e,t)},getKnowledgeBase(e){return n.get("/",e)},getKnowledge(e,t){return n.get(`/${e}`,t)},deleteKnowledge(e,t){return n.delete(`/${e}`,void 0,t)},createDocument(e,t,o){return n.post(`/${e}/documents`,t,o)},deleteDocument(e,t,o){return n.delete(`/${e}/documents/${t}`,void 0,o)},getDocuments(e,t){return n.get(`/${e}/documents`,t)},getDocument(e,t,o){return n.get(`/${e}/documents/${t}`,o)},getRecords(e,t,o){return n.get(`/${e}/documents/${t}/records`,o)},query(e,t,o){return n.post(`/${e}/query`,{query:t},o)}}}function M(r){if(r.type==="bearer")return`Bearer ${r.token}`;if(r.type==="basic")return`Basic ${btoa(`${r.username}:${r.password}`)}`;if(r.type==="key")return`Client-Key ${r.clientKey}`;throw new Error(`Unknown auth type: ${r}`)}const P=r=>new Promise(i=>setTimeout(i,r));function D(r){return new Promise((i,n)=>{const{callbacks:e,host:t,auth:o}=r,{onMessage:a,onOpen:m,onClose:S=null,onError:C}=e,c=new WebSocket(`${t}?authorization=${M(o)}`);c.onmessage=a,c.onclose=S,c.onerror=f=>{console.log(f),C==null||C(f),n(f)},c.onopen=f=>{m==null||m(f),i(c)}})}async function O(r){const{retries:i=1}=r;let n=null;for(let e=0;(n==null?void 0:n.readyState)!==WebSocket.OPEN;e++)try{n=await D(r)}catch(t){if(e===i)throw t;await P(e*500)}return n}var $=(r=>(r.Start="START",r.Stop="STOP",r))($||{}),y=(r=>(r.Clip="clip",r.Talk="talk",r))(y||{});function A(r){return r.presenter.type===y.Clip?{videoType:y.Clip,driver_id:r.presenter.driver_id,presenter_id:r.presenter.presenter_id}:{videoType:y.Talk,source_url:r.presenter.source_url}}async function j(r,{callbacks:i,...n}){const e=new AbortController,t=_(n.auth,n.baseURL),o=await t.getById(r),a=await t.newChat(r),{terminate:m,sessionId:S,streamId:C}=await K(A(o),{...n,callbacks:{onSrcObjectReady:i.onSrcObjectReady,onVideoStateChange:i==null?void 0:i.onVideoStateChange,onConnectionStateChange:i.onConnectionStateChange}});return{agent:o,terminate(){return e.abort(),m()},chatId:a.id,chat(c){return t.chat(r,a.id,{sessionId:S,streamId:C,messages:c},{signal:e.signal})}}}function B(r,i){const n=h(r,i);return{createStream(e){return n.post("/clips/streams",{driver_id:e.driver_id,presenter_id:e.presenter_id,compatibility_mode:e.compatibility_mode})},startConnection(e,t,o){return n.post(`/clips/streams/${e}/sdp`,{session_id:o,answer:t})},addIceCandidate(e,t,o){return n.post(`/clips/streams/${e}/ice`,{session_id:o,...t})},sendStreamRequest(e,t,o){return n.post(`/clips/streams/${e}`,{session_id:t,...o})},close(e,t){return n.delete(`/clips/streams/${e}`,{session_id:t})}}}function E(r,i){const n=h(r,i);return{createStream(e,t){return n.post("/talks/streams",{source_url:e.source_url,driver_url:e.driver_url,face:e.face,config:e.config},t)},startConnection(e,t,o,a){return n.post(`/talks/streams/${e}/sdp`,{session_id:o,answer:t},a)},addIceCandidate(e,t,o,a){return n.post(`/talks/streams/${e}/ice`,{session_id:o,...t},a)},sendStreamRequest(e,t,o,a){return n.post(`/talks/streams/${e}`,{session_id:t,...o},a)},close(e,t,o){return n.delete(`/talks/streams/${e}`,{session_id:t},o)}}}let T=!1;const g=(r,i)=>T&&console.log(r,i),q=(window.RTCPeerConnection||window.webkitRTCPeerConnection||window.mozRTCPeerConnection).bind(window);async function K(r,{debug:i=!1,callbacks:n,auth:e,baseURL:t="https://api.d-id.com"}){T=i;const{startConnection:o,sendStreamRequest:a,close:m,createStream:S,addIceCandidate:C}=r.videoType===y.Clip?B(e,t):E(e,t),{id:c,offer:f,ice_servers:L,session_id:l}=await S(r),d=new q({iceServers:L}),R=d.createDataChannel("JanusDataChannel");if(!l)throw new Error("Could not create session_id");d.onicecandidate=s=>{g("peerConnection.onicecandidate",s),s.candidate&&s.candidate.sdpMid&&s.candidate.sdpMLineIndex!==null&&C(c,{candidate:s.candidate.candidate,sdpMid:s.candidate.sdpMid,sdpMLineIndex:s.candidate.sdpMLineIndex},l)},d.oniceconnectionstatechange=()=>{var s;g("peerConnection.oniceconnectionstatechange => "+d.iceConnectionState),(s=n.onConnectionStateChange)==null||s.call(n,d.iceConnectionState)},d.ontrack=s=>{var p;g("peerConnection.ontrack",s),(p=n.onSrcObjectReady)==null||p.call(n,s.streams[0])},R.onmessage=s=>{var p;R.readyState==="open"&&((p=n.onVideoStateChange)==null||p.call(n,s.data==="stream/done"?$.Stop:$.Start))},await d.setRemoteDescription(f),g("set remote description OK");const v=await d.createAnswer();return g("create answer OK"),await d.setLocalDescription(v),g("set local description OK"),await o(c,v,l),g("start connection OK"),{speak(s){return a(c,l,s)},async terminate(){var s,p;c&&(d&&(d.close(),d.oniceconnectionstatechange=null,d.onnegotiationneeded=null,d.onicecandidate=null,d.ontrack=null),await m(c,l),(s=n.onConnectionStateChange)==null||s.call(n,"closed"),(p=n.onVideoStateChange)==null||p.call(n,$.Stop))},sessionId:l,streamId:c}}u.connectToSocket=O,u.createAgentManager=j,u.createAgentsApi=_,u.createClient=h,u.createKnowledgeApi=I,u.createStreamingManager=K,u.getAgentStreamArgs=A,Object.defineProperty(u,Symbol.toStringTag,{value:"Module"})});
|
package/dist/lib/api/agents.d.ts
CHANGED
|
@@ -6,5 +6,5 @@ export declare function createAgentsApi(auth: Auth, host?: string): {
|
|
|
6
6
|
delete(id: string, options?: RequestInit): Promise<any>;
|
|
7
7
|
update(id: string, payload: AgentPayload, options?: RequestInit): Promise<Agent>;
|
|
8
8
|
newChat(agentId: string, options?: RequestInit): Promise<Chat>;
|
|
9
|
-
chat(agentId: string, chatId: string, payload: ChatPayload, options?: RequestInit): Promise<
|
|
9
|
+
chat(agentId: string, chatId: string, payload: ChatPayload, options?: RequestInit): Promise<ChatResponse>;
|
|
10
10
|
};
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { Auth } from '../types/auth';
|
|
2
|
+
interface Options {
|
|
3
|
+
auth: Auth;
|
|
4
|
+
retries?: number;
|
|
5
|
+
callbacks: {
|
|
6
|
+
onMessage: (event: MessageEvent) => void;
|
|
7
|
+
onOpen?: (event: Event) => void;
|
|
8
|
+
onClose?: (event: CloseEvent) => void;
|
|
9
|
+
onError?: (event: Event) => void;
|
|
10
|
+
};
|
|
11
|
+
host?: string;
|
|
12
|
+
}
|
|
13
|
+
export declare function connectToSocket(options: Options): Promise<WebSocket>;
|
|
14
|
+
export {};
|
|
@@ -4,6 +4,6 @@ export declare function createAgentManager(agentId: string, { callbacks, ...opti
|
|
|
4
4
|
agent: Agent;
|
|
5
5
|
terminate(): Promise<void>;
|
|
6
6
|
chatId: string;
|
|
7
|
-
chat(messages: Message[]): Promise<
|
|
7
|
+
chat(messages: Message[]): Promise<import('../types/index').ChatResponse>;
|
|
8
8
|
}>;
|
|
9
9
|
export type AgentManager = Awaited<ReturnType<typeof createAgentManager>>;
|
package/dist/lib/index.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { Message } from './entities';
|
|
1
2
|
import { StreamTextToSpeechProviders } from './tts';
|
|
2
3
|
export type StreamScriptType = 'text' | 'audio';
|
|
3
4
|
export interface BaseStreamScript {
|
|
@@ -11,7 +12,7 @@ export interface Stream_Text_Script extends BaseStreamScript {
|
|
|
11
12
|
/**
|
|
12
13
|
* text-to-speech provider from list of supported providers. default is microsoft tts
|
|
13
14
|
*/
|
|
14
|
-
provider
|
|
15
|
+
provider: StreamTextToSpeechProviders;
|
|
15
16
|
/**
|
|
16
17
|
* The input text that will be synthesized to an audio file.
|
|
17
18
|
* Note that each provider has its own limitations on the text length.
|
|
@@ -37,4 +38,15 @@ export interface Stream_Audio_Script extends BaseStreamScript {
|
|
|
37
38
|
*/
|
|
38
39
|
audio_url: string;
|
|
39
40
|
}
|
|
40
|
-
export
|
|
41
|
+
export interface Stream_LLM_Script {
|
|
42
|
+
type: 'llm';
|
|
43
|
+
provider: StreamTextToSpeechProviders;
|
|
44
|
+
ssml?: boolean;
|
|
45
|
+
llm: {
|
|
46
|
+
messages: Message[];
|
|
47
|
+
provider: 'openai';
|
|
48
|
+
};
|
|
49
|
+
input?: string;
|
|
50
|
+
stream_audio?: boolean;
|
|
51
|
+
}
|
|
52
|
+
export type StreamScript = Stream_Text_Script | Stream_Audio_Script | Stream_LLM_Script;
|
package/dist/types/auth.d.ts
CHANGED
|
@@ -10,6 +10,7 @@ export interface BasicAuth {
|
|
|
10
10
|
export interface ApiKeyAuth {
|
|
11
11
|
type: 'key';
|
|
12
12
|
clientKey: string;
|
|
13
|
+
externalId: string;
|
|
13
14
|
}
|
|
14
15
|
export type Auth = BearerToken | BasicAuth | ApiKeyAuth;
|
|
15
16
|
export interface GetAuthParams {
|
|
@@ -17,4 +18,5 @@ export interface GetAuthParams {
|
|
|
17
18
|
username?: string | null;
|
|
18
19
|
password?: string | null;
|
|
19
20
|
clientKey?: string | null;
|
|
21
|
+
externalId?: string | null;
|
|
20
22
|
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { StreamScript } from '
|
|
1
|
+
import { StreamScript } from '../../';
|
|
2
2
|
export interface CreateTalkStreamRequest {
|
|
3
3
|
source_url: string;
|
|
4
4
|
driver_url?: string;
|
|
@@ -42,4 +42,6 @@ export interface SendTalkStreamPayload {
|
|
|
42
42
|
};
|
|
43
43
|
user_data?: Record<string, any>;
|
|
44
44
|
name?: string;
|
|
45
|
+
audio_optimization?: number;
|
|
46
|
+
metadata: Record<string, any>;
|
|
45
47
|
}
|
|
@@ -4,8 +4,8 @@ import { CreateClipStreamRequest, CreateTalkStreamRequest, SendClipStreamPayload
|
|
|
4
4
|
import { ICreateStreamRequestResponse, IceCandidate, SendStreamPayloadResponse, Status } from './rtc';
|
|
5
5
|
export type CompatibilityMode = 'on' | 'off' | 'auto';
|
|
6
6
|
export declare enum StreamingState {
|
|
7
|
-
|
|
8
|
-
|
|
7
|
+
Start = "START",
|
|
8
|
+
Stop = "STOP"
|
|
9
9
|
}
|
|
10
10
|
export interface ManagerCallbacks {
|
|
11
11
|
onConnectionStateChange?: (state: RTCIceConnectionState) => void;
|