@d-id/client-sdk 0.0.1 → 0.0.2

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