@d-id/client-sdk 1.0.18-beta.2 → 1.0.18-beta.3
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 +262 -271
- package/dist/index.umd.cjs +1 -1
- package/dist/src/index.d.ts +0 -1
- package/dist/src/lib/api/agents.d.ts +1 -1
- package/dist/src/lib/api/getClient.d.ts +1 -1
- package/dist/src/lib/api/ratings.d.ts +1 -1
- package/package.json +2 -2
package/dist/index.js
CHANGED
|
@@ -1,453 +1,444 @@
|
|
|
1
|
-
const f = "https://api-dev.d-id.com",
|
|
2
|
-
function
|
|
3
|
-
if (
|
|
4
|
-
return `Bearer ${
|
|
5
|
-
if (
|
|
6
|
-
return `Basic ${btoa(`${
|
|
7
|
-
if (
|
|
8
|
-
return `Client-Key ${
|
|
9
|
-
throw new Error(`Unknown auth type: ${
|
|
1
|
+
const f = "https://api-dev.d-id.com", K = "wss://notifications.d-id.com";
|
|
2
|
+
function R(r) {
|
|
3
|
+
if (r.type === "bearer")
|
|
4
|
+
return `Bearer ${r.token}`;
|
|
5
|
+
if (r.type === "basic")
|
|
6
|
+
return `Basic ${btoa(`${r.username}:${r.password}`)}`;
|
|
7
|
+
if (r.type === "key")
|
|
8
|
+
return `Client-Key ${r.clientKey}`;
|
|
9
|
+
throw new Error(`Unknown auth type: ${r}`);
|
|
10
10
|
}
|
|
11
|
-
function S(
|
|
12
|
-
const o = async (
|
|
13
|
-
const
|
|
14
|
-
...
|
|
11
|
+
function S(r, a = f) {
|
|
12
|
+
const o = async (e, t) => {
|
|
13
|
+
const n = await fetch(a + (e != null && e.startsWith("/") ? e : `/${e}`), {
|
|
14
|
+
...t,
|
|
15
15
|
headers: {
|
|
16
|
-
...
|
|
17
|
-
Authorization:
|
|
16
|
+
...t == null ? void 0 : t.headers,
|
|
17
|
+
Authorization: R(r),
|
|
18
18
|
"Content-Type": "application/json"
|
|
19
19
|
}
|
|
20
20
|
});
|
|
21
|
-
if (!
|
|
22
|
-
let c = await
|
|
21
|
+
if (!n.ok) {
|
|
22
|
+
let c = await n.text().catch(() => "Failed to fetch");
|
|
23
23
|
throw new Error(c);
|
|
24
24
|
}
|
|
25
|
-
return
|
|
25
|
+
return n.json();
|
|
26
26
|
};
|
|
27
27
|
return {
|
|
28
|
-
get(
|
|
29
|
-
return o(
|
|
30
|
-
...
|
|
28
|
+
get(e, t) {
|
|
29
|
+
return o(e, {
|
|
30
|
+
...t,
|
|
31
31
|
method: "GET"
|
|
32
32
|
});
|
|
33
33
|
},
|
|
34
|
-
post(t, n
|
|
35
|
-
return o(
|
|
36
|
-
...
|
|
37
|
-
body: JSON.stringify(
|
|
34
|
+
post(e, t, n) {
|
|
35
|
+
return o(e, {
|
|
36
|
+
...n,
|
|
37
|
+
body: JSON.stringify(t),
|
|
38
38
|
method: "POST"
|
|
39
39
|
});
|
|
40
40
|
},
|
|
41
|
-
delete(t, n
|
|
42
|
-
return o(
|
|
43
|
-
...
|
|
44
|
-
body: JSON.stringify(
|
|
41
|
+
delete(e, t, n) {
|
|
42
|
+
return o(e, {
|
|
43
|
+
...n,
|
|
44
|
+
body: JSON.stringify(t),
|
|
45
45
|
method: "DELETE"
|
|
46
46
|
});
|
|
47
47
|
},
|
|
48
|
-
patch(t, n
|
|
49
|
-
return o(
|
|
50
|
-
...
|
|
51
|
-
body: JSON.stringify(
|
|
48
|
+
patch(e, t, n) {
|
|
49
|
+
return o(e, {
|
|
50
|
+
...n,
|
|
51
|
+
body: JSON.stringify(t),
|
|
52
52
|
method: "PATCH"
|
|
53
53
|
});
|
|
54
54
|
}
|
|
55
55
|
};
|
|
56
56
|
}
|
|
57
|
-
function
|
|
58
|
-
const o = S(
|
|
57
|
+
function b(r, a = f) {
|
|
58
|
+
const o = S(r, `${a}/agents`);
|
|
59
59
|
return {
|
|
60
|
-
create(
|
|
61
|
-
return o.post("/",
|
|
60
|
+
create(e, t) {
|
|
61
|
+
return o.post("/", e, t);
|
|
62
62
|
},
|
|
63
|
-
getAgents(
|
|
64
|
-
return o.get(`/${
|
|
63
|
+
getAgents(e, t) {
|
|
64
|
+
return o.get(`/${e ? `?tag=${e}` : ""}`, t).then((n) => n ?? []);
|
|
65
65
|
},
|
|
66
|
-
getById(
|
|
67
|
-
return o.get(`/${
|
|
66
|
+
getById(e, t) {
|
|
67
|
+
return o.get(`/${e}`, t);
|
|
68
68
|
},
|
|
69
|
-
delete(
|
|
70
|
-
return o.delete(`/${
|
|
69
|
+
delete(e, t) {
|
|
70
|
+
return o.delete(`/${e}`, void 0, t);
|
|
71
71
|
},
|
|
72
|
-
update(t, n
|
|
73
|
-
return o.patch(`/${
|
|
72
|
+
update(e, t, n) {
|
|
73
|
+
return o.patch(`/${e}`, t, n);
|
|
74
74
|
},
|
|
75
|
-
newChat(
|
|
76
|
-
return o.post(`/${
|
|
75
|
+
newChat(e, t) {
|
|
76
|
+
return o.post(`/${e}/chat`, void 0, t);
|
|
77
77
|
},
|
|
78
|
-
chat(t, n,
|
|
79
|
-
return o.post(`/${
|
|
78
|
+
chat(e, t, n, c) {
|
|
79
|
+
return o.post(`/${e}/chat/${t}`, n, c);
|
|
80
80
|
}
|
|
81
81
|
};
|
|
82
82
|
}
|
|
83
|
-
function
|
|
84
|
-
const o = S(
|
|
83
|
+
function F(r, a = f) {
|
|
84
|
+
const o = S(r, `${a}/knowledge`);
|
|
85
85
|
return {
|
|
86
|
-
createKnowledge(
|
|
87
|
-
return o.post("/",
|
|
86
|
+
createKnowledge(e, t) {
|
|
87
|
+
return o.post("/", e, t);
|
|
88
88
|
},
|
|
89
|
-
getKnowledgeBase(
|
|
90
|
-
return o.get("/",
|
|
89
|
+
getKnowledgeBase(e) {
|
|
90
|
+
return o.get("/", e);
|
|
91
91
|
},
|
|
92
|
-
getKnowledge(
|
|
93
|
-
return o.get(`/${
|
|
92
|
+
getKnowledge(e, t) {
|
|
93
|
+
return o.get(`/${e}`, t);
|
|
94
94
|
},
|
|
95
|
-
deleteKnowledge(
|
|
96
|
-
return o.delete(`/${
|
|
95
|
+
deleteKnowledge(e, t) {
|
|
96
|
+
return o.delete(`/${e}`, void 0, t);
|
|
97
97
|
},
|
|
98
|
-
createDocument(t, n
|
|
99
|
-
return o.post(`/${
|
|
98
|
+
createDocument(e, t, n) {
|
|
99
|
+
return o.post(`/${e}/documents`, t, n);
|
|
100
100
|
},
|
|
101
|
-
deleteDocument(t, n
|
|
102
|
-
return o.delete(`/${
|
|
101
|
+
deleteDocument(e, t, n) {
|
|
102
|
+
return o.delete(`/${e}/documents/${t}`, void 0, n);
|
|
103
103
|
},
|
|
104
|
-
getDocuments(
|
|
105
|
-
return o.get(`/${
|
|
104
|
+
getDocuments(e, t) {
|
|
105
|
+
return o.get(`/${e}/documents`, t);
|
|
106
106
|
},
|
|
107
|
-
getDocument(t, n
|
|
108
|
-
return o.get(`/${
|
|
107
|
+
getDocument(e, t, n) {
|
|
108
|
+
return o.get(`/${e}/documents/${t}`, n);
|
|
109
109
|
},
|
|
110
|
-
getRecords(t, n
|
|
111
|
-
return o.get(`/${
|
|
110
|
+
getRecords(e, t, n) {
|
|
111
|
+
return o.get(`/${e}/documents/${t}/records`, n);
|
|
112
112
|
},
|
|
113
|
-
query(t, n
|
|
114
|
-
return o.post(`/${
|
|
115
|
-
query:
|
|
116
|
-
},
|
|
113
|
+
query(e, t, n) {
|
|
114
|
+
return o.post(`/${e}/query`, {
|
|
115
|
+
query: t
|
|
116
|
+
}, n);
|
|
117
117
|
}
|
|
118
118
|
};
|
|
119
119
|
}
|
|
120
|
-
const
|
|
121
|
-
function
|
|
120
|
+
const x = (r) => new Promise((a) => setTimeout(a, r));
|
|
121
|
+
function q(r) {
|
|
122
122
|
return new Promise((a, o) => {
|
|
123
123
|
const {
|
|
124
|
-
callbacks:
|
|
125
|
-
host:
|
|
126
|
-
auth:
|
|
127
|
-
} =
|
|
124
|
+
callbacks: e,
|
|
125
|
+
host: t,
|
|
126
|
+
auth: n
|
|
127
|
+
} = r, {
|
|
128
128
|
onMessage: c = null,
|
|
129
|
-
onOpen:
|
|
130
|
-
onClose:
|
|
131
|
-
onError:
|
|
132
|
-
} =
|
|
133
|
-
d.onmessage = c, d.onclose =
|
|
134
|
-
console.log(
|
|
135
|
-
}, d.onopen = (
|
|
136
|
-
|
|
129
|
+
onOpen: l = null,
|
|
130
|
+
onClose: C = null,
|
|
131
|
+
onError: p = null
|
|
132
|
+
} = e || {}, d = new WebSocket(`${t}?authorization=${R(n)}`);
|
|
133
|
+
d.onmessage = c, d.onclose = C, d.onerror = (s) => {
|
|
134
|
+
console.log(s), p == null || p(s), o(s);
|
|
135
|
+
}, d.onopen = (s) => {
|
|
136
|
+
l == null || l(s), a(d);
|
|
137
137
|
};
|
|
138
138
|
});
|
|
139
139
|
}
|
|
140
|
-
async function
|
|
140
|
+
async function B(r) {
|
|
141
141
|
const {
|
|
142
142
|
retries: a = 1
|
|
143
|
-
} =
|
|
143
|
+
} = r;
|
|
144
144
|
let o = null;
|
|
145
|
-
for (let
|
|
145
|
+
for (let e = 0; (o == null ? void 0 : o.readyState) !== WebSocket.OPEN; e++)
|
|
146
146
|
try {
|
|
147
|
-
o = await
|
|
148
|
-
} catch (
|
|
149
|
-
if (
|
|
150
|
-
throw
|
|
151
|
-
await
|
|
147
|
+
o = await q(r);
|
|
148
|
+
} catch (t) {
|
|
149
|
+
if (e === a)
|
|
150
|
+
throw t;
|
|
151
|
+
await x(e * 500);
|
|
152
152
|
}
|
|
153
153
|
return o;
|
|
154
154
|
}
|
|
155
|
-
async function
|
|
156
|
-
const o = [],
|
|
157
|
-
auth:
|
|
155
|
+
async function L(r, a = K) {
|
|
156
|
+
const o = [], e = await B({
|
|
157
|
+
auth: r,
|
|
158
158
|
host: a,
|
|
159
159
|
callbacks: {
|
|
160
|
-
onMessage: (
|
|
161
|
-
console.log("event",
|
|
160
|
+
onMessage: (t) => {
|
|
161
|
+
console.log("event", t), o.forEach((n) => n(t));
|
|
162
162
|
}
|
|
163
163
|
}
|
|
164
164
|
});
|
|
165
165
|
return {
|
|
166
|
-
socket:
|
|
167
|
-
terminate: () =>
|
|
168
|
-
subscribeToEvents: (
|
|
169
|
-
o.push(
|
|
166
|
+
socket: e,
|
|
167
|
+
terminate: () => e.close(),
|
|
168
|
+
subscribeToEvents: (t) => {
|
|
169
|
+
o.push(t);
|
|
170
170
|
}
|
|
171
171
|
};
|
|
172
172
|
}
|
|
173
|
-
var
|
|
174
|
-
function
|
|
175
|
-
const o = S(
|
|
173
|
+
var y = /* @__PURE__ */ ((r) => (r.Start = "START", r.Stop = "STOP", r))(y || {}), k = /* @__PURE__ */ ((r) => (r.ChatAnswer = "chat/answer", r.ChatPartial = "chat/partial", r.StreamDone = "stream/done", r.StreamStarted = "stream/started", r))(k || {}), $ = /* @__PURE__ */ ((r) => (r.Clip = "clip", r.Talk = "talk", r))($ || {});
|
|
174
|
+
function z(r, a = f) {
|
|
175
|
+
const o = S(r, `${a}/chats/ratings`);
|
|
176
176
|
return {
|
|
177
|
-
create(
|
|
178
|
-
return o.post("/",
|
|
177
|
+
create(e, t) {
|
|
178
|
+
return o.post("/", e, t);
|
|
179
179
|
},
|
|
180
|
-
getByKnowledge(
|
|
181
|
-
return o.get(`/${
|
|
180
|
+
getByKnowledge(e, t) {
|
|
181
|
+
return o.get(`/${e}`, t).then((n) => n ?? []);
|
|
182
182
|
},
|
|
183
|
-
update(t, n
|
|
184
|
-
return o.patch(`/${
|
|
183
|
+
update(e, t, n) {
|
|
184
|
+
return o.patch(`/${e}`, t, n);
|
|
185
185
|
},
|
|
186
|
-
delete(
|
|
187
|
-
return o.delete(`/${
|
|
186
|
+
delete(e, t) {
|
|
187
|
+
return o.delete(`/${e}`, t);
|
|
188
188
|
}
|
|
189
189
|
};
|
|
190
190
|
}
|
|
191
|
-
function
|
|
192
|
-
return
|
|
191
|
+
function U(r) {
|
|
192
|
+
return r.presenter.type === $.Clip ? {
|
|
193
193
|
videoType: $.Clip,
|
|
194
|
-
driver_id:
|
|
195
|
-
presenter_id:
|
|
194
|
+
driver_id: r.presenter.driver_id,
|
|
195
|
+
presenter_id: r.presenter.presenter_id
|
|
196
196
|
} : {
|
|
197
197
|
videoType: $.Talk,
|
|
198
|
-
source_url:
|
|
198
|
+
source_url: r.presenter.source_url
|
|
199
199
|
};
|
|
200
200
|
}
|
|
201
|
-
function
|
|
202
|
-
return new Promise(async (
|
|
203
|
-
var
|
|
204
|
-
const
|
|
205
|
-
a.callbacks.onConnectionStateChange = async (
|
|
206
|
-
if (
|
|
207
|
-
const
|
|
208
|
-
|
|
209
|
-
chat:
|
|
201
|
+
function P(r, a, o) {
|
|
202
|
+
return new Promise(async (e, t) => {
|
|
203
|
+
var l;
|
|
204
|
+
const n = ((l = a.callbacks) == null ? void 0 : l.onConnectionStateChange) ?? null;
|
|
205
|
+
a.callbacks.onConnectionStateChange = async (C) => {
|
|
206
|
+
if (n && n(C), C === "connected") {
|
|
207
|
+
const p = await o.newChat(r.id);
|
|
208
|
+
n && c.onCallback("onConnectionStateChange", n), e({
|
|
209
|
+
chat: p,
|
|
210
210
|
streamingAPI: c
|
|
211
211
|
});
|
|
212
212
|
} else
|
|
213
|
-
|
|
213
|
+
C === "failed" && t(new Error("Cannot create connection"));
|
|
214
214
|
};
|
|
215
|
-
const c = await
|
|
215
|
+
const c = await W(U(r), a);
|
|
216
216
|
});
|
|
217
217
|
}
|
|
218
|
-
async function
|
|
219
|
-
const o = a.baseURL || f,
|
|
220
|
-
return
|
|
218
|
+
async function H(r, a) {
|
|
219
|
+
const o = a.baseURL || f, e = new AbortController(), t = b(a.auth, o), n = z(a.auth, o), c = await t.getById(r), l = await L(a.auth);
|
|
220
|
+
return P(c, a, t).then((C) => {
|
|
221
221
|
let {
|
|
222
|
-
chat:
|
|
222
|
+
chat: p,
|
|
223
223
|
streamingAPI: d
|
|
224
|
-
} =
|
|
224
|
+
} = C;
|
|
225
225
|
return {
|
|
226
226
|
agent: c,
|
|
227
227
|
async reconnectToChat() {
|
|
228
|
-
await
|
|
229
|
-
|
|
228
|
+
await P(c, a, t).then((s) => {
|
|
229
|
+
p = s.chat, d = s.streamingAPI;
|
|
230
230
|
});
|
|
231
231
|
},
|
|
232
232
|
terminate() {
|
|
233
|
-
return
|
|
233
|
+
return e.abort(), l.terminate(), d.terminate();
|
|
234
234
|
},
|
|
235
|
-
chatId:
|
|
236
|
-
chat(
|
|
237
|
-
return
|
|
235
|
+
chatId: p.id,
|
|
236
|
+
chat(s) {
|
|
237
|
+
return t.chat(r, p.id, {
|
|
238
238
|
sessionId: d.sessionId,
|
|
239
239
|
streamId: d.streamId,
|
|
240
|
-
messages:
|
|
240
|
+
messages: s
|
|
241
241
|
}, {
|
|
242
|
-
signal:
|
|
242
|
+
signal: e.signal
|
|
243
243
|
});
|
|
244
244
|
},
|
|
245
|
-
rate(
|
|
246
|
-
return
|
|
245
|
+
rate(s, m) {
|
|
246
|
+
return m ? n.update(m, s) : n.create(s);
|
|
247
247
|
},
|
|
248
|
-
speak(
|
|
248
|
+
speak(s) {
|
|
249
249
|
if (!c)
|
|
250
250
|
throw new Error("Agent not initializated");
|
|
251
|
-
let
|
|
252
|
-
return
|
|
251
|
+
let m;
|
|
252
|
+
return s.type === "text" ? m = {
|
|
253
253
|
script: {
|
|
254
254
|
type: "text",
|
|
255
|
-
provider:
|
|
256
|
-
input:
|
|
257
|
-
ssml:
|
|
255
|
+
provider: s.provider,
|
|
256
|
+
input: s.input,
|
|
257
|
+
ssml: s.ssml || !1
|
|
258
258
|
}
|
|
259
|
-
} :
|
|
259
|
+
} : s.type === "audio" && (m = {
|
|
260
260
|
script: {
|
|
261
261
|
type: "audio",
|
|
262
|
-
audio_url:
|
|
262
|
+
audio_url: s.audio_url
|
|
263
263
|
}
|
|
264
|
-
}), d.speak(
|
|
264
|
+
}), d.speak(m);
|
|
265
265
|
},
|
|
266
|
-
onChatEvents(
|
|
267
|
-
|
|
266
|
+
onChatEvents(s) {
|
|
267
|
+
l.subscribeToEvents(s);
|
|
268
268
|
},
|
|
269
|
-
onConnectionEvents(
|
|
270
|
-
d.onCallback("onConnectionStateChange",
|
|
269
|
+
onConnectionEvents(s) {
|
|
270
|
+
d.onCallback("onConnectionStateChange", s);
|
|
271
271
|
},
|
|
272
|
-
onVideoEvents(
|
|
273
|
-
d.onCallback("onVideoStateChange",
|
|
272
|
+
onVideoEvents(s) {
|
|
273
|
+
d.onCallback("onVideoStateChange", s);
|
|
274
274
|
}
|
|
275
275
|
};
|
|
276
276
|
});
|
|
277
277
|
}
|
|
278
|
-
function
|
|
279
|
-
const o = S(
|
|
278
|
+
function J(r, a) {
|
|
279
|
+
const o = S(r, a);
|
|
280
280
|
return {
|
|
281
|
-
createStream(
|
|
281
|
+
createStream(e) {
|
|
282
282
|
return o.post("/clips/streams", {
|
|
283
|
-
driver_id:
|
|
284
|
-
presenter_id:
|
|
285
|
-
compatibility_mode:
|
|
283
|
+
driver_id: e.driver_id,
|
|
284
|
+
presenter_id: e.presenter_id,
|
|
285
|
+
compatibility_mode: e.compatibility_mode
|
|
286
286
|
});
|
|
287
287
|
},
|
|
288
|
-
startConnection(t, n
|
|
289
|
-
return o.post(`/clips/streams/${
|
|
290
|
-
session_id:
|
|
291
|
-
answer:
|
|
288
|
+
startConnection(e, t, n) {
|
|
289
|
+
return o.post(`/clips/streams/${e}/sdp`, {
|
|
290
|
+
session_id: n,
|
|
291
|
+
answer: t
|
|
292
292
|
});
|
|
293
293
|
},
|
|
294
|
-
addIceCandidate(t, n
|
|
295
|
-
return o.post(`/clips/streams/${
|
|
296
|
-
session_id:
|
|
297
|
-
...
|
|
294
|
+
addIceCandidate(e, t, n) {
|
|
295
|
+
return o.post(`/clips/streams/${e}/ice`, {
|
|
296
|
+
session_id: n,
|
|
297
|
+
...t
|
|
298
298
|
});
|
|
299
299
|
},
|
|
300
|
-
sendStreamRequest(t, n
|
|
301
|
-
return o.post(`/clips/streams/${
|
|
302
|
-
session_id:
|
|
303
|
-
...
|
|
300
|
+
sendStreamRequest(e, t, n) {
|
|
301
|
+
return o.post(`/clips/streams/${e}`, {
|
|
302
|
+
session_id: t,
|
|
303
|
+
...n
|
|
304
304
|
});
|
|
305
305
|
},
|
|
306
|
-
close(
|
|
307
|
-
return o.delete(`/clips/streams/${
|
|
308
|
-
session_id:
|
|
306
|
+
close(e, t) {
|
|
307
|
+
return o.delete(`/clips/streams/${e}`, {
|
|
308
|
+
session_id: t
|
|
309
309
|
});
|
|
310
310
|
}
|
|
311
311
|
};
|
|
312
312
|
}
|
|
313
|
-
function
|
|
314
|
-
const o = S(
|
|
313
|
+
function N(r, a) {
|
|
314
|
+
const o = S(r, a);
|
|
315
315
|
return {
|
|
316
|
-
createStream(
|
|
316
|
+
createStream(e, t) {
|
|
317
317
|
return o.post("/talks/streams", {
|
|
318
|
-
source_url:
|
|
319
|
-
driver_url:
|
|
320
|
-
face:
|
|
321
|
-
config:
|
|
322
|
-
},
|
|
323
|
-
},
|
|
324
|
-
startConnection(t, n,
|
|
325
|
-
return o.post(`/talks/streams/${
|
|
326
|
-
session_id:
|
|
327
|
-
answer:
|
|
318
|
+
source_url: e.source_url,
|
|
319
|
+
driver_url: e.driver_url,
|
|
320
|
+
face: e.face,
|
|
321
|
+
config: e.config
|
|
322
|
+
}, t);
|
|
323
|
+
},
|
|
324
|
+
startConnection(e, t, n, c) {
|
|
325
|
+
return o.post(`/talks/streams/${e}/sdp`, {
|
|
326
|
+
session_id: n,
|
|
327
|
+
answer: t
|
|
328
328
|
}, c);
|
|
329
329
|
},
|
|
330
|
-
addIceCandidate(t, n,
|
|
331
|
-
return o.post(`/talks/streams/${
|
|
332
|
-
session_id:
|
|
333
|
-
...
|
|
330
|
+
addIceCandidate(e, t, n, c) {
|
|
331
|
+
return o.post(`/talks/streams/${e}/ice`, {
|
|
332
|
+
session_id: n,
|
|
333
|
+
...t
|
|
334
334
|
}, c);
|
|
335
335
|
},
|
|
336
|
-
sendStreamRequest(t, n,
|
|
337
|
-
return o.post(`/talks/streams/${
|
|
338
|
-
session_id:
|
|
339
|
-
...
|
|
336
|
+
sendStreamRequest(e, t, n, c) {
|
|
337
|
+
return o.post(`/talks/streams/${e}`, {
|
|
338
|
+
session_id: t,
|
|
339
|
+
...n
|
|
340
340
|
}, c);
|
|
341
341
|
},
|
|
342
|
-
close(t, n
|
|
343
|
-
return o.delete(`/talks/streams/${
|
|
344
|
-
session_id:
|
|
345
|
-
},
|
|
342
|
+
close(e, t, n) {
|
|
343
|
+
return o.delete(`/talks/streams/${e}`, {
|
|
344
|
+
session_id: t
|
|
345
|
+
}, n);
|
|
346
346
|
}
|
|
347
347
|
};
|
|
348
348
|
}
|
|
349
|
-
let
|
|
350
|
-
const
|
|
351
|
-
async function
|
|
349
|
+
let M = !1;
|
|
350
|
+
const h = (r, a) => M && console.log(r, a), V = (window.RTCPeerConnection || window.webkitRTCPeerConnection || window.mozRTCPeerConnection).bind(window);
|
|
351
|
+
async function W(r, {
|
|
352
352
|
debug: a = !1,
|
|
353
353
|
callbacks: o,
|
|
354
|
-
auth:
|
|
355
|
-
baseURL:
|
|
354
|
+
auth: e,
|
|
355
|
+
baseURL: t = f
|
|
356
356
|
}) {
|
|
357
|
-
|
|
358
|
-
const
|
|
357
|
+
M = a;
|
|
358
|
+
const n = {
|
|
359
359
|
...o
|
|
360
360
|
}, {
|
|
361
361
|
startConnection: c,
|
|
362
|
-
sendStreamRequest:
|
|
363
|
-
close:
|
|
364
|
-
createStream:
|
|
362
|
+
sendStreamRequest: l,
|
|
363
|
+
close: C,
|
|
364
|
+
createStream: p,
|
|
365
365
|
addIceCandidate: d
|
|
366
|
-
} =
|
|
367
|
-
id:
|
|
368
|
-
offer:
|
|
369
|
-
ice_servers:
|
|
370
|
-
session_id:
|
|
371
|
-
} = await
|
|
372
|
-
iceServers:
|
|
373
|
-
}),
|
|
374
|
-
if (!
|
|
366
|
+
} = r.videoType === $.Clip ? J(e, t) : N(e, t), {
|
|
367
|
+
id: s,
|
|
368
|
+
offer: m,
|
|
369
|
+
ice_servers: D,
|
|
370
|
+
session_id: w
|
|
371
|
+
} = await p(r), u = new V({
|
|
372
|
+
iceServers: D
|
|
373
|
+
}), I = u.createDataChannel("JanusDataChannel");
|
|
374
|
+
if (!w)
|
|
375
375
|
throw new Error("Could not create session_id");
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
candidate:
|
|
379
|
-
sdpMid:
|
|
380
|
-
sdpMLineIndex:
|
|
381
|
-
},
|
|
382
|
-
},
|
|
383
|
-
var
|
|
384
|
-
|
|
385
|
-
},
|
|
386
|
-
var
|
|
387
|
-
|
|
388
|
-
},
|
|
389
|
-
var
|
|
390
|
-
if (
|
|
391
|
-
const [
|
|
392
|
-
|
|
376
|
+
u.onicecandidate = (i) => {
|
|
377
|
+
h("peerConnection.onicecandidate", i), i.candidate && i.candidate.sdpMid && i.candidate.sdpMLineIndex !== null && d(s, {
|
|
378
|
+
candidate: i.candidate.candidate,
|
|
379
|
+
sdpMid: i.candidate.sdpMid,
|
|
380
|
+
sdpMLineIndex: i.candidate.sdpMLineIndex
|
|
381
|
+
}, w);
|
|
382
|
+
}, u.oniceconnectionstatechange = () => {
|
|
383
|
+
var i;
|
|
384
|
+
h("peerConnection.oniceconnectionstatechange => " + u.iceConnectionState), (i = n.onConnectionStateChange) == null || i.call(n, u.iceConnectionState);
|
|
385
|
+
}, u.ontrack = (i) => {
|
|
386
|
+
var g;
|
|
387
|
+
h("peerConnection.ontrack", i), (g = n.onSrcObjectReady) == null || g.call(n, i.streams[0]);
|
|
388
|
+
}, I.onmessage = (i) => {
|
|
389
|
+
var g, _, v;
|
|
390
|
+
if (I.readyState === "open") {
|
|
391
|
+
const [A, E] = i.data.split(":");
|
|
392
|
+
A === k.StreamDone ? (g = n.onVideoStateChange) == null || g.call(n, y.Stop) : A === k.StreamStarted ? (_ = n.onVideoStateChange) == null || _.call(n, y.Start) : (v = n.onMessage) == null || v.call(n, A, decodeURIComponent(E));
|
|
393
393
|
}
|
|
394
|
-
}, await
|
|
395
|
-
const
|
|
396
|
-
return
|
|
394
|
+
}, await u.setRemoteDescription(m), h("set remote description OK");
|
|
395
|
+
const T = await u.createAnswer();
|
|
396
|
+
return h("create answer OK"), await u.setLocalDescription(T), h("set local description OK"), await c(s, T, w), h("start connection OK"), {
|
|
397
397
|
/**
|
|
398
398
|
* Method to send request to server to get clip or talk depend on you payload
|
|
399
399
|
* @param payload
|
|
400
400
|
*/
|
|
401
|
-
speak(
|
|
402
|
-
return
|
|
401
|
+
speak(i) {
|
|
402
|
+
return l(s, w, i);
|
|
403
403
|
},
|
|
404
404
|
/**
|
|
405
405
|
* Method to close RTC connection
|
|
406
406
|
*/
|
|
407
407
|
async terminate() {
|
|
408
|
-
var
|
|
409
|
-
|
|
410
|
-
}), (
|
|
408
|
+
var i, g;
|
|
409
|
+
s && (u && (u.close(), u.oniceconnectionstatechange = null, u.onnegotiationneeded = null, u.onicecandidate = null, u.ontrack = null), await C(s, w).catch((_) => {
|
|
410
|
+
}), (i = n.onConnectionStateChange) == null || i.call(n, "closed"), (g = n.onVideoStateChange) == null || g.call(n, y.Stop));
|
|
411
411
|
},
|
|
412
412
|
/**
|
|
413
413
|
* Session identifier information, should be returned in the body of all streaming requests
|
|
414
414
|
*/
|
|
415
|
-
sessionId:
|
|
415
|
+
sessionId: w,
|
|
416
416
|
/**
|
|
417
417
|
* Id of current RTC stream
|
|
418
418
|
*/
|
|
419
|
-
streamId:
|
|
419
|
+
streamId: s,
|
|
420
420
|
/**
|
|
421
421
|
* Method to add callback that will be trigered on supported events
|
|
422
422
|
* @param eventName
|
|
423
423
|
* @param callback
|
|
424
424
|
*/
|
|
425
|
-
onCallback(
|
|
426
|
-
|
|
425
|
+
onCallback(i, g) {
|
|
426
|
+
n[i] = g;
|
|
427
427
|
},
|
|
428
428
|
/**
|
|
429
429
|
* existing callback is internal method to pass calbacks added after create in new connection
|
|
430
430
|
*/
|
|
431
431
|
getCallbacks() {
|
|
432
|
-
return
|
|
432
|
+
return n;
|
|
433
433
|
}
|
|
434
434
|
};
|
|
435
435
|
}
|
|
436
436
|
export {
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
J as RateState,
|
|
441
|
-
B as SocketManager,
|
|
442
|
-
A as StreamEvents,
|
|
443
|
-
v as StreamingState,
|
|
444
|
-
W as Subject,
|
|
445
|
-
$ as VideoType,
|
|
446
|
-
N as VoiceAccess,
|
|
447
|
-
O as createAgentManager,
|
|
448
|
-
z as createAgentsApi,
|
|
437
|
+
L as SocketManager,
|
|
438
|
+
H as createAgentManager,
|
|
439
|
+
b as createAgentsApi,
|
|
449
440
|
S as createClient,
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
441
|
+
F as createKnowledgeApi,
|
|
442
|
+
W as createStreamingManager,
|
|
443
|
+
U as getAgentStreamArgs
|
|
453
444
|
};
|
package/dist/index.umd.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
(function(
|
|
1
|
+
(function(l,m){typeof exports=="object"&&typeof module<"u"?m(exports):typeof define=="function"&&define.amd?define(["exports"],m):(l=typeof globalThis<"u"?globalThis:l||self,m(l.index={}))})(this,function(l){"use strict";const m="https://api-dev.d-id.com",B="wss://notifications.d-id.com";function v(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}`)}function S(r,a=m){const o=async(e,t)=>{const n=await fetch(a+(e!=null&&e.startsWith("/")?e:`/${e}`),{...t,headers:{...t==null?void 0:t.headers,Authorization:v(r),"Content-Type":"application/json"}});if(!n.ok){let c=await n.text().catch(()=>"Failed to fetch");throw new Error(c)}return n.json()};return{get(e,t){return o(e,{...t,method:"GET"})},post(e,t,n){return o(e,{...n,body:JSON.stringify(t),method:"POST"})},delete(e,t,n){return o(e,{...n,body:JSON.stringify(t),method:"DELETE"})},patch(e,t,n){return o(e,{...n,body:JSON.stringify(t),method:"PATCH"})}}}function I(r,a=m){const o=S(r,`${a}/agents`);return{create(e,t){return o.post("/",e,t)},getAgents(e,t){return o.get(`/${e?`?tag=${e}`:""}`,t).then(n=>n??[])},getById(e,t){return o.get(`/${e}`,t)},delete(e,t){return o.delete(`/${e}`,void 0,t)},update(e,t,n){return o.patch(`/${e}`,t,n)},newChat(e,t){return o.post(`/${e}/chat`,void 0,t)},chat(e,t,n,c){return o.post(`/${e}/chat/${t}`,n,c)}}}function L(r,a=m){const o=S(r,`${a}/knowledge`);return{createKnowledge(e,t){return o.post("/",e,t)},getKnowledgeBase(e){return o.get("/",e)},getKnowledge(e,t){return o.get(`/${e}`,t)},deleteKnowledge(e,t){return o.delete(`/${e}`,void 0,t)},createDocument(e,t,n){return o.post(`/${e}/documents`,t,n)},deleteDocument(e,t,n){return o.delete(`/${e}/documents/${t}`,void 0,n)},getDocuments(e,t){return o.get(`/${e}/documents`,t)},getDocument(e,t,n){return o.get(`/${e}/documents/${t}`,n)},getRecords(e,t,n){return o.get(`/${e}/documents/${t}/records`,n)},query(e,t,n){return o.post(`/${e}/query`,{query:t},n)}}}const z=r=>new Promise(a=>setTimeout(a,r));function U(r){return new Promise((a,o)=>{const{callbacks:e,host:t,auth:n}=r,{onMessage:c=null,onOpen:g=null,onClose:h=null,onError:p=null}=e||{},d=new WebSocket(`${t}?authorization=${v(n)}`);d.onmessage=c,d.onclose=h,d.onerror=i=>{console.log(i),p==null||p(i),o(i)},d.onopen=i=>{g==null||g(i),a(d)}})}async function J(r){const{retries:a=1}=r;let o=null;for(let e=0;(o==null?void 0:o.readyState)!==WebSocket.OPEN;e++)try{o=await U(r)}catch(t){if(e===a)throw t;await z(e*500)}return o}async function P(r,a=B){const o=[],e=await J({auth:r,host:a,callbacks:{onMessage:t=>{console.log("event",t),o.forEach(n=>n(t))}}});return{socket:e,terminate:()=>e.close(),subscribeToEvents:t=>{o.push(t)}}}var A=(r=>(r.Start="START",r.Stop="STOP",r))(A||{}),_=(r=>(r.ChatAnswer="chat/answer",r.ChatPartial="chat/partial",r.StreamDone="stream/done",r.StreamStarted="stream/started",r))(_||{}),y=(r=>(r.Clip="clip",r.Talk="talk",r))(y||{});function N(r,a=m){const o=S(r,`${a}/chats/ratings`);return{create(e,t){return o.post("/",e,t)},getByKnowledge(e,t){return o.get(`/${e}`,t).then(n=>n??[])},update(e,t,n){return o.patch(`/${e}`,t,n)},delete(e,t){return o.delete(`/${e}`,t)}}}function M(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}}function R(r,a,o){return new Promise(async(e,t)=>{var g;const n=((g=a.callbacks)==null?void 0:g.onConnectionStateChange)??null;a.callbacks.onConnectionStateChange=async h=>{if(n&&n(h),h==="connected"){const p=await o.newChat(r.id);n&&c.onCallback("onConnectionStateChange",n),e({chat:p,streamingAPI:c})}else h==="failed"&&t(new Error("Cannot create connection"))};const c=await D(M(r),a)})}async function V(r,a){const o=a.baseURL||m,e=new AbortController,t=I(a.auth,o),n=N(a.auth,o),c=await t.getById(r),g=await P(a.auth);return R(c,a,t).then(h=>{let{chat:p,streamingAPI:d}=h;return{agent:c,async reconnectToChat(){await R(c,a,t).then(i=>{p=i.chat,d=i.streamingAPI})},terminate(){return e.abort(),g.terminate(),d.terminate()},chatId:p.id,chat(i){return t.chat(r,p.id,{sessionId:d.sessionId,streamId:d.streamId,messages:i},{signal:e.signal})},rate(i,f){return f?n.update(f,i):n.create(i)},speak(i){if(!c)throw new Error("Agent not initializated");let f;return i.type==="text"?f={script:{type:"text",provider:i.provider,input:i.input,ssml:i.ssml||!1}}:i.type==="audio"&&(f={script:{type:"audio",audio_url:i.audio_url}}),d.speak(f)},onChatEvents(i){g.subscribeToEvents(i)},onConnectionEvents(i){d.onCallback("onConnectionStateChange",i)},onVideoEvents(i){d.onCallback("onVideoStateChange",i)}}})}function W(r,a){const o=S(r,a);return{createStream(e){return o.post("/clips/streams",{driver_id:e.driver_id,presenter_id:e.presenter_id,compatibility_mode:e.compatibility_mode})},startConnection(e,t,n){return o.post(`/clips/streams/${e}/sdp`,{session_id:n,answer:t})},addIceCandidate(e,t,n){return o.post(`/clips/streams/${e}/ice`,{session_id:n,...t})},sendStreamRequest(e,t,n){return o.post(`/clips/streams/${e}`,{session_id:t,...n})},close(e,t){return o.delete(`/clips/streams/${e}`,{session_id:t})}}}function x(r,a){const o=S(r,a);return{createStream(e,t){return o.post("/talks/streams",{source_url:e.source_url,driver_url:e.driver_url,face:e.face,config:e.config},t)},startConnection(e,t,n,c){return o.post(`/talks/streams/${e}/sdp`,{session_id:n,answer:t},c)},addIceCandidate(e,t,n,c){return o.post(`/talks/streams/${e}/ice`,{session_id:n,...t},c)},sendStreamRequest(e,t,n,c){return o.post(`/talks/streams/${e}`,{session_id:t,...n},c)},close(e,t,n){return o.delete(`/talks/streams/${e}`,{session_id:t},n)}}}let K=!1;const w=(r,a)=>K&&console.log(r,a),F=(window.RTCPeerConnection||window.webkitRTCPeerConnection||window.mozRTCPeerConnection).bind(window);async function D(r,{debug:a=!1,callbacks:o,auth:e,baseURL:t=m}){K=a;const n={...o},{startConnection:c,sendStreamRequest:g,close:h,createStream:p,addIceCandidate:d}=r.videoType===y.Clip?W(e,t):x(e,t),{id:i,offer:f,ice_servers:H,session_id:$}=await p(r),u=new F({iceServers:H}),E=u.createDataChannel("JanusDataChannel");if(!$)throw new Error("Could not create session_id");u.onicecandidate=s=>{w("peerConnection.onicecandidate",s),s.candidate&&s.candidate.sdpMid&&s.candidate.sdpMLineIndex!==null&&d(i,{candidate:s.candidate.candidate,sdpMid:s.candidate.sdpMid,sdpMLineIndex:s.candidate.sdpMLineIndex},$)},u.oniceconnectionstatechange=()=>{var s;w("peerConnection.oniceconnectionstatechange => "+u.iceConnectionState),(s=n.onConnectionStateChange)==null||s.call(n,u.iceConnectionState)},u.ontrack=s=>{var C;w("peerConnection.ontrack",s),(C=n.onSrcObjectReady)==null||C.call(n,s.streams[0])},E.onmessage=s=>{var C,k,q;if(E.readyState==="open"){const[T,G]=s.data.split(":");T===_.StreamDone?(C=n.onVideoStateChange)==null||C.call(n,A.Stop):T===_.StreamStarted?(k=n.onVideoStateChange)==null||k.call(n,A.Start):(q=n.onMessage)==null||q.call(n,T,decodeURIComponent(G))}},await u.setRemoteDescription(f),w("set remote description OK");const b=await u.createAnswer();return w("create answer OK"),await u.setLocalDescription(b),w("set local description OK"),await c(i,b,$),w("start connection OK"),{speak(s){return g(i,$,s)},async terminate(){var s,C;i&&(u&&(u.close(),u.oniceconnectionstatechange=null,u.onnegotiationneeded=null,u.onicecandidate=null,u.ontrack=null),await h(i,$).catch(k=>{}),(s=n.onConnectionStateChange)==null||s.call(n,"closed"),(C=n.onVideoStateChange)==null||C.call(n,A.Stop))},sessionId:$,streamId:i,onCallback(s,C){n[s]=C},getCallbacks(){return n}}}l.SocketManager=P,l.createAgentManager=V,l.createAgentsApi=I,l.createClient=S,l.createKnowledgeApi=L,l.createStreamingManager=D,l.getAgentStreamArgs=M,Object.defineProperty(l,Symbol.toStringTag,{value:"Module"})});
|
package/dist/src/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Agent, AgentPayload, Auth, Chat, ChatPayload, ChatResponse } from '
|
|
1
|
+
import { Agent, AgentPayload, Auth, Chat, ChatPayload, ChatResponse } from '../../types/index';
|
|
2
2
|
export declare function createAgentsApi(auth: Auth, host?: string): {
|
|
3
3
|
create(payload: AgentPayload, options?: RequestInit): Promise<Agent>;
|
|
4
4
|
getAgents(tag?: string, options?: RequestInit): Promise<Agent[]>;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Auth } from '
|
|
1
|
+
import { Auth } from '../../types/index';
|
|
2
2
|
export declare function createClient(auth: Auth, host?: string): {
|
|
3
3
|
get<T = any>(url: string, options?: RequestInit): Promise<T>;
|
|
4
4
|
post<T_1 = any>(url: string, body?: any, options?: RequestInit): Promise<T_1>;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Auth, RatingEntity, RatingPayload } from '../../index';
|
|
1
|
+
import { Auth, RatingEntity, RatingPayload } from '../../types/index';
|
|
2
2
|
export declare function createRatingsApi(auth: Auth, host?: string): {
|
|
3
3
|
create(payload: RatingPayload, options?: RequestInit): Promise<RatingEntity>;
|
|
4
4
|
getByKnowledge(knowledgeId?: string, options?: RequestInit): Promise<RatingEntity[]>;
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@d-id/client-sdk",
|
|
3
3
|
"private": false,
|
|
4
|
-
"version": "1.0.18-beta.
|
|
4
|
+
"version": "1.0.18-beta.3",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"description": "d-id client sdk",
|
|
7
7
|
"repository": {
|
|
@@ -20,7 +20,7 @@
|
|
|
20
20
|
],
|
|
21
21
|
"main": "./dist/index.umd.cjs",
|
|
22
22
|
"module": "./dist/index.js",
|
|
23
|
-
"types": "./dist/index.d.ts",
|
|
23
|
+
"types": "./dist/types/index.d.ts",
|
|
24
24
|
"scripts": {
|
|
25
25
|
"dev": "vite",
|
|
26
26
|
"build": "node ./infra/build.js -m production",
|