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