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