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