@d-id/client-sdk 1.0.16 → 1.0.18-beta.1

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 (44) hide show
  1. package/README.md +2 -0
  2. package/dist/index.js +297 -191
  3. package/dist/index.umd.cjs +1 -1
  4. package/dist/src/index.d.ts +3 -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/src/lib/api/ratings.d.ts +7 -0
  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 +21 -0
  13. package/dist/src/lib/constants.d.ts +3 -0
  14. package/dist/src/lib/createAgentManager.d.ts +15 -0
  15. package/dist/src/lib/createStreamingManager.d.ts +27 -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/{common/types/src → src/types}/entities/agents/chat.d.ts +20 -1
  19. package/dist/src/types/entities/agents/manager.d.ts +110 -0
  20. package/dist/{common/types/src → src/types}/stream/api/clip.d.ts +1 -1
  21. package/dist/{common/types/src → src/types}/stream/api/talk.d.ts +1 -1
  22. package/dist/{common/types/src → src/types}/stream/stream.d.ts +3 -1
  23. package/package.json +8 -12
  24. package/dist/apps/sdk/lib/createAgentManager.d.ts +0 -9
  25. package/dist/apps/sdk/lib/createStreamingManager.d.ts +0 -8
  26. package/dist/apps/sdk/lib/index.d.ts +0 -5
  27. package/dist/common/types/src/entities/agents/manager.d.ts +0 -21
  28. /package/dist/{common/types/src → src/types}/entities/agents/agent.d.ts +0 -0
  29. /package/dist/{common/types/src → src/types}/entities/agents/index.d.ts +0 -0
  30. /package/dist/{common/types/src → src/types}/entities/agents/knowledge.d.ts +0 -0
  31. /package/dist/{common/types/src → src/types}/entities/agents/llm.d.ts +0 -0
  32. /package/dist/{common/types/src → src/types}/entities/agents/presenter.d.ts +0 -0
  33. /package/dist/{common/types/src → src/types}/entities/index.d.ts +0 -0
  34. /package/dist/{common/types/src → src/types}/entities/knowledge/document.d.ts +0 -0
  35. /package/dist/{common/types/src → src/types}/entities/knowledge/index.d.ts +0 -0
  36. /package/dist/{common/types/src → src/types}/entities/knowledge/knowledge.d.ts +0 -0
  37. /package/dist/{common/types/src → src/types}/entities/knowledge/record.d.ts +0 -0
  38. /package/dist/{common/types/src → src/types}/entities/knowledge/retrival.d.ts +0 -0
  39. /package/dist/{common/types/src → src/types}/entities/video.d.ts +0 -0
  40. /package/dist/{common/types/src → src/types}/index.d.ts +0 -0
  41. /package/dist/{common/types/src → src/types}/stream/api/index.d.ts +0 -0
  42. /package/dist/{common/types/src → src/types}/stream/index.d.ts +0 -0
  43. /package/dist/{common/types/src → src/types}/stream/rtc.d.ts +0 -0
  44. /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,293 +1,399 @@
1
- function l(o) {
2
- return o.type === "bearer" ? "Bearer " + o.token : o.type === "basic" ? "Basic " + btoa(`${o.username}:${o.password}`) : "Client-Key " + o.clientKey;
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}`);
3
10
  }
4
- function h(o, d = "https://api.d-id.com") {
5
- const n = async (e, t) => {
6
- const r = await fetch(d + (e != null && e.startsWith("/") ? e : `/${e}`), {
7
- ...t,
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,
8
15
  headers: {
9
- ...t == null ? void 0 : t.headers,
10
- Authorization: l(o),
16
+ ...n == null ? void 0 : n.headers,
17
+ Authorization: b(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 o = await r.text().catch(() => "Failed to fetch");
23
+ throw new Error(o);
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 a(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 a(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 a(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 a(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, d = "https://api.d-id.com") {
51
- const n = h(o, `${d}/agents`);
57
+ function B(e, s = "https://api.d-id.com") {
58
+ const a = v(e, `${s}/agents`);
52
59
  return {
53
- create(e, t) {
54
- return n.post("/", e, t);
60
+ create(t, n) {
61
+ return a.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 a.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 a.get(`/${t}`, n);
61
68
  },
62
- delete(e, t) {
63
- return n.delete(`/${e}`, void 0, t);
69
+ delete(t, n) {
70
+ return a.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 a.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 a.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, o) {
79
+ return a.post(`/${t}/chat/${n}`, r, o);
73
80
  }
74
81
  };
75
82
  }
76
- function B(o, d = "https://api.d-id.com") {
77
- const n = h(o, `${d}/knowledge`);
83
+ function J(e, s = "https://api.d-id.com") {
84
+ const a = v(e, `${s}/chats/ratings`);
78
85
  return {
79
- createKnowledge(e, t) {
80
- return n.post("/", e, t);
86
+ create(t, n) {
87
+ return a.post("/", t, n);
81
88
  },
82
- getKnowledgeBase(e) {
83
- return n.get("/", e);
89
+ getByKnowledge(t, n) {
90
+ return a.get(`/${t}`, n).then((r) => r ?? []);
84
91
  },
85
- getKnowledge(e, t) {
86
- return n.get(`/${e}`, t);
92
+ update(t, n, r) {
93
+ return a.patch(`/${t}`, n, r);
87
94
  },
88
- deleteKnowledge(e, t) {
89
- return n.delete(`/${e}`, void 0, t);
90
- },
91
- createDocument(e, t, r) {
92
- return n.post(`/${e}/documents`, t, r);
93
- },
94
- deleteDocument(e, t, r) {
95
- return n.delete(`/${e}/documents/${t}`, void 0, r);
96
- },
97
- getDocuments(e, t) {
98
- return n.get(`/${e}/documents`, t);
99
- },
100
- getDocument(e, t, r) {
101
- return n.get(`/${e}/documents/${t}`, r);
102
- },
103
- getRecords(e, t, r) {
104
- return n.get(`/${e}/documents/${t}/records`, r);
95
+ delete(t, n) {
96
+ return a.delete(`/${t}`, n);
97
+ }
98
+ };
99
+ }
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) => {
103
+ const {
104
+ callbacks: t,
105
+ host: n,
106
+ auth: r
107
+ } = e, {
108
+ onMessage: o = null,
109
+ onOpen: g = null,
110
+ 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);
117
+ };
118
+ });
119
+ }
120
+ async function F(e) {
121
+ 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++)
126
+ try {
127
+ a = await W(e);
128
+ } catch (n) {
129
+ if (t === s)
130
+ throw n;
131
+ await H(t * 500);
132
+ }
133
+ return a;
134
+ }
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();
105
140
  },
106
- query(e, t, r) {
107
- return n.post(`/${e}/query`, {
108
- query: t
109
- }, r);
141
+ connect: async () => (a = await F({
142
+ auth: e,
143
+ host: s,
144
+ callbacks: {
145
+ onMessage: r
146
+ }
147
+ }), n.socket = a, a),
148
+ subscribeToEvents: (o) => {
149
+ n.socket ? t.push(o) : console.warn("Socket is not connected. Please call connect() first.");
110
150
  }
111
151
  };
152
+ function r(o) {
153
+ console.log("event", o), t.forEach((g) => g(o));
154
+ }
155
+ return n;
112
156
  }
113
- var C = /* @__PURE__ */ ((o) => (o.Start = "START", o.Stop = "STOP", o))(C || {}), y = /* @__PURE__ */ ((o) => (o.ChatPartial = "chat/partial", o.StreamDone = "stream/done", o.StreamStarted = "stream/started", o))(y || {}), m = /* @__PURE__ */ ((o) => (o.Clip = "clip", o.Talk = "talk", o))(m || {});
114
- function O(o) {
115
- return o.presenter.type === m.Clip ? {
116
- videoType: m.Clip,
117
- driver_id: o.presenter.driver_id,
118
- presenter_id: o.presenter.presenter_id
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
119
165
  } : {
120
- videoType: m.Talk,
121
- source_url: o.presenter.source_url
166
+ videoType: k.Talk,
167
+ source_url: e.presenter.source_url
122
168
  };
123
169
  }
124
- async function L(o, {
125
- callbacks: d,
126
- ...n
127
- }) {
128
- const e = new AbortController(), t = M(n.auth, n.baseURL), r = await t.getById(o), a = await t.newChat(o), {
129
- terminate: w,
130
- sessionId: S,
131
- streamId: f
132
- } = await x(O(r), {
133
- ...n,
134
- callbacks: {
135
- onSrcObjectReady: d.onSrcObjectReady,
136
- onVideoStateChange: d == null ? void 0 : d.onVideoStateChange,
137
- onConnectionStateChange: d.onConnectionStateChange
138
- }
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
139
175
  });
140
- return {
176
+ const p = await V(s.auth);
177
+ return await p.connect(), {
141
178
  agent: r,
179
+ async reconnectToChat() {
180
+ l = await R(M(r), {
181
+ ...s,
182
+ callbacks: C
183
+ }), l.sessionId;
184
+ },
142
185
  terminate() {
143
- return e.abort(), w();
186
+ return t.abort(), p.terminate(), l.terminate();
144
187
  },
145
- chatId: a.id,
188
+ chatId: o.id,
146
189
  chat(c) {
147
- return t.chat(o, a.id, {
148
- sessionId: S,
149
- streamId: f,
190
+ return n.chat(e, o.id, {
191
+ sessionId: l.sessionId,
192
+ streamId: l.streamId,
150
193
  messages: c
151
194
  }, {
152
- signal: e.signal
195
+ signal: t.signal
153
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);
154
227
  }
155
228
  };
156
229
  }
157
- function P(o, d) {
158
- const n = h(o, d);
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;
235
+ }
236
+ function X(e, s) {
237
+ const a = v(e, s);
159
238
  return {
160
- createStream(e) {
161
- return n.post("/clips/streams", {
162
- driver_id: e.driver_id,
163
- presenter_id: e.presenter_id,
164
- compatibility_mode: e.compatibility_mode
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
165
244
  });
166
245
  },
167
- startConnection(e, t, r) {
168
- return n.post(`/clips/streams/${e}/sdp`, {
246
+ startConnection(t, n, r) {
247
+ return a.post(`/clips/streams/${t}/sdp`, {
169
248
  session_id: r,
170
- answer: t
249
+ answer: n
171
250
  });
172
251
  },
173
- addIceCandidate(e, t, r) {
174
- return n.post(`/clips/streams/${e}/ice`, {
252
+ addIceCandidate(t, n, r) {
253
+ return a.post(`/clips/streams/${t}/ice`, {
175
254
  session_id: r,
176
- ...t
255
+ ...n
177
256
  });
178
257
  },
179
- sendStreamRequest(e, t, r) {
180
- return n.post(`/clips/streams/${e}`, {
181
- session_id: t,
258
+ sendStreamRequest(t, n, r) {
259
+ return a.post(`/clips/streams/${t}`, {
260
+ session_id: n,
182
261
  ...r
183
262
  });
184
263
  },
185
- close(e, t) {
186
- return n.delete(`/clips/streams/${e}`, {
187
- session_id: t
264
+ close(t, n) {
265
+ return a.delete(`/clips/streams/${t}`, {
266
+ session_id: n
188
267
  });
189
268
  }
190
269
  };
191
270
  }
192
- function q(o, d) {
193
- const n = h(o, d);
271
+ function Y(e, s) {
272
+ const a = v(e, s);
194
273
  return {
195
- createStream(e, t) {
196
- return n.post("/talks/streams", {
197
- source_url: e.source_url,
198
- driver_url: e.driver_url,
199
- face: e.face,
200
- config: e.config
201
- }, t);
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);
202
281
  },
203
- startConnection(e, t, r, a) {
204
- return n.post(`/talks/streams/${e}/sdp`, {
282
+ startConnection(t, n, r, o) {
283
+ return a.post(`/talks/streams/${t}/sdp`, {
205
284
  session_id: r,
206
- answer: t
207
- }, a);
285
+ answer: n
286
+ }, o);
208
287
  },
209
- addIceCandidate(e, t, r, a) {
210
- return n.post(`/talks/streams/${e}/ice`, {
288
+ addIceCandidate(t, n, r, o) {
289
+ return a.post(`/talks/streams/${t}/ice`, {
211
290
  session_id: r,
212
- ...t
213
- }, a);
291
+ ...n
292
+ }, o);
214
293
  },
215
- sendStreamRequest(e, t, r, a) {
216
- return n.post(`/talks/streams/${e}`, {
217
- session_id: t,
294
+ sendStreamRequest(t, n, r, o) {
295
+ return a.post(`/talks/streams/${t}`, {
296
+ session_id: n,
218
297
  ...r
219
- }, a);
298
+ }, o);
220
299
  },
221
- close(e, t, r) {
222
- return n.delete(`/talks/streams/${e}`, {
223
- session_id: t
300
+ close(t, n, r) {
301
+ return a.delete(`/talks/streams/${t}`, {
302
+ session_id: n
224
303
  }, r);
225
304
  }
226
305
  };
227
306
  }
228
- let I = !1;
229
- const p = (o, d) => I && console.log(o, d), j = (window.RTCPeerConnection || window.webkitRTCPeerConnection || window.mozRTCPeerConnection).bind(window);
230
- async function x(o, {
231
- debug: d = !1,
232
- callbacks: n,
233
- auth: e,
234
- baseURL: t = "https://api.d-id.com"
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"
235
314
  }) {
236
- I = d;
237
- const {
238
- startConnection: r,
239
- sendStreamRequest: a,
240
- close: w,
241
- createStream: S,
242
- addIceCandidate: f
243
- } = o.videoType === m.Clip ? P(e, t) : q(e, t), {
244
- id: c,
245
- offer: D,
246
- ice_servers: v,
247
- session_id: g
248
- } = await S(o), s = new j({
249
- iceServers: v
250
- }), A = s.createDataChannel("JanusDataChannel");
251
- if (!g)
315
+ x = s;
316
+ const r = a || {}, {
317
+ startConnection: o,
318
+ sendStreamRequest: g,
319
+ 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)
252
331
  throw new Error("Could not create session_id");
253
- s.onicecandidate = (i) => {
254
- p("peerConnection.onicecandidate", i), i.candidate && i.candidate.sdpMid && i.candidate.sdpMLineIndex !== null && f(c, {
332
+ d.onicecandidate = (i) => {
333
+ m("peerConnection.onicecandidate", i), i.candidate && i.candidate.sdpMid && i.candidate.sdpMLineIndex !== null && p(u, {
255
334
  candidate: i.candidate.candidate,
256
335
  sdpMid: i.candidate.sdpMid,
257
336
  sdpMLineIndex: i.candidate.sdpMLineIndex
258
- }, g);
259
- }, s.oniceconnectionstatechange = () => {
337
+ }, f);
338
+ }, d.oniceconnectionstatechange = () => {
260
339
  var i;
261
- p("peerConnection.oniceconnectionstatechange => " + s.iceConnectionState), (i = n.onConnectionStateChange) == null || i.call(n, s.iceConnectionState);
262
- }, s.ontrack = (i) => {
263
- var u;
264
- p("peerConnection.ontrack", i), (u = n.onSrcObjectReady) == null || u.call(n, i.streams[0]);
265
- }, A.onmessage = (i) => {
266
- var u, $, R;
267
- if (A.readyState === "open") {
268
- const [_, K] = i.data.split(":");
269
- _ === y.StreamDone ? (u = n.onVideoStateChange) == null || u.call(n, C.Stop) : _ === y.StreamStarted ? ($ = n.onVideoStateChange) == null || $.call(n, C.Start) : (R = n.onMessage) == null || R.call(n, _, decodeURIComponent(K));
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]);
344
+ }, I.onmessage = (i) => {
345
+ var w, _, y;
346
+ 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));
270
349
  }
271
- }, await s.setRemoteDescription(D), p("set remote description OK");
272
- const T = await s.createAnswer();
273
- return p("create answer OK"), await s.setLocalDescription(T), p("set local description OK"), await r(c, T, g), p("start connection OK"), {
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"), {
353
+ /**
354
+ * Method to send request to server to get clip or talk depend on you payload
355
+ * @param payload
356
+ */
274
357
  speak(i) {
275
- return a(c, g, i);
358
+ return g(u, f, i);
276
359
  },
360
+ /**
361
+ * Method to close RTC connection
362
+ */
277
363
  async terminate() {
278
- var i, u;
279
- c && (s && (s.close(), s.oniceconnectionstatechange = null, s.onnegotiationneeded = null, s.onicecandidate = null, s.ontrack = null), await w(c, g).catch(($) => {
280
- }), (i = n.onConnectionStateChange) == null || i.call(n, "closed"), (u = n.onVideoStateChange) == null || u.call(n, C.Stop));
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));
281
367
  },
282
- sessionId: g,
283
- streamId: c
368
+ /**
369
+ * Session identifier information, should be returned in the body of all streaming requests
370
+ */
371
+ sessionId: f,
372
+ /**
373
+ * Id of current RTC stream
374
+ */
375
+ streamId: u,
376
+ /**
377
+ * Method to add callback that will be trigered on supported events
378
+ * @param eventName
379
+ * @param callback
380
+ */
381
+ addCallback(i, w) {
382
+ r[i] = w;
383
+ }
284
384
  };
285
385
  }
286
386
  export {
287
- L as createAgentManager,
288
- M as createAgentsApi,
289
- h as createClient,
290
- B as createKnowledgeApi,
291
- x as createStreamingManager,
292
- O as getAgentStreamArgs
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
293
399
  };
@@ -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 h(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 l(i,d="https://api.d-id.com"){const n=h(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 O(i,d="https://api.d-id.com"){const n=h(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 S=(i=>(i.Start="START",i.Stop="STOP",i))(S||{}),w=(i=>(i.ChatPartial="chat/partial",i.StreamDone="stream/done",i.StreamStarted="stream/started",i))(w||{}),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 P(i,{callbacks:d,...n}){const e=new AbortController,t=l(n.auth,n.baseURL),r=await t.getById(i),a=await t.newChat(i),{terminate:$,sessionId:y,streamId:_}=await v(R(r),{...n,callbacks:{onSrcObjectReady:d.onSrcObjectReady,onVideoStateChange:d==null?void 0:d.onVideoStateChange,onConnectionStateChange:d.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 j(i,d){const n=h(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 q(i,d){const n=h(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 I=!1;const g=(i,d)=>I&&console.log(i,d),B=(window.RTCPeerConnection||window.webkitRTCPeerConnection||window.mozRTCPeerConnection).bind(window);async function v(i,{debug:d=!1,callbacks:n,auth:e,baseURL:t="https://api.d-id.com"}){I=d;const{startConnection:r,sendStreamRequest:a,close:$,createStream:y,addIceCandidate:_}=i.videoType===f.Clip?j(e,t):q(e,t),{id:u,offer:L,ice_servers:E,session_id:m}=await y(i),s=new B({iceServers:E}),D=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&&_(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])},D.onmessage=o=>{var p,A,M;if(D.readyState==="open"){const[T,J]=o.data.split(":");T===w.StreamDone?(p=n.onVideoStateChange)==null||p.call(n,S.Stop):T===w.StreamStarted?(A=n.onVideoStateChange)==null||A.call(n,S.Start):(M=n.onMessage)==null||M.call(n,T,decodeURIComponent(J))}},await s.setRemoteDescription(L),g("set remote description OK");const K=await s.createAnswer();return g("create answer OK"),await s.setLocalDescription(K),g("set local description OK"),await r(u,K,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 $(u,m).catch(A=>{}),(o=n.onConnectionStateChange)==null||o.call(n,"closed"),(p=n.onVideoStateChange)==null||p.call(n,S.Stop))},sessionId:m,streamId:u}}c.createAgentManager=P,c.createAgentsApi=l,c.createClient=h,c.createKnowledgeApi=O,c.createStreamingManager=v,c.getAgentStreamArgs=R,Object.defineProperty(c,Symbol.toStringTag,{value:"Module"})});
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"})});
@@ -0,0 +1,3 @@
1
+ export * from './lib/createAgentManager';
2
+ export * from './lib/createStreamingManager';
3
+ 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[]>;
@@ -0,0 +1,7 @@
1
+ import { Auth, RatingEntity, RatingPayload } from '../../index';
2
+ export declare function createRatingssApi(auth: Auth, host?: string): {
3
+ create(payload: RatingPayload, options?: RequestInit): Promise<RatingEntity>;
4
+ getByKnowledge(knowledgeId?: string, options?: RequestInit): Promise<RatingEntity[]>;
5
+ update(id: string, payload: Partial<RatingPayload>, options?: RequestInit): Promise<RatingEntity>;
6
+ delete(id: string, options?: RequestInit): Promise<RatingEntity>;
7
+ };
@@ -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,21 @@
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
+ interface SocketManager {
14
+ socket?: WebSocket;
15
+ terminate: () => void;
16
+ connect: () => Promise<WebSocket>;
17
+ subscribeToEvents: (data: any) => void;
18
+ }
19
+ export declare function connectToSocket(options: Options): Promise<WebSocket>;
20
+ export declare function SocketManager(auth: Auth, host?: string): Promise<SocketManager>;
21
+ export {};
@@ -0,0 +1,3 @@
1
+ export declare const CONSTANTS: {
2
+ baseURL: string;
3
+ };
@@ -0,0 +1,15 @@
1
+ import { Agent, AgentsAPI, AgentManagerOptions, 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<AgentsAPI>} - 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<AgentsAPI>;
@@ -0,0 +1,27 @@
1
+ import { CreateStreamOptions, ManagerCallbackKeys, 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
+ addCallback(eventName: ManagerCallbackKeys, callback: Function): void;
26
+ }>;
27
+ export type StreamingManager<T extends CreateStreamOptions> = Awaited<ReturnType<typeof createStreamingManager<T>>>;
@@ -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
  }
@@ -1,8 +1,27 @@
1
+ export declare enum RateState {
2
+ Unrated = "Unrated",
3
+ Positive = "Positive",
4
+ Negative = "Negative"
5
+ }
6
+ export interface RatingEntity {
7
+ id: string;
8
+ owner_id: string;
9
+ agent_id: string;
10
+ matches: [string, string][];
11
+ knowledge_id: string;
12
+ external_id: string;
13
+ created_by: string;
14
+ chat_id: string;
15
+ score: 1 | -1;
16
+ created_at: string;
17
+ modified_at: string;
18
+ }
19
+ export type RatingPayload = Omit<RatingEntity, 'owner_id' | 'id' | 'created_at' | 'modified_at' | 'created_by' | 'external_id'>;
1
20
  export interface Message {
2
21
  role: 'system' | 'assistant' | 'user' | 'function' | 'tool';
3
22
  content: string;
4
- name?: string;
5
23
  created_at: string;
24
+ matches?: ChatResponse['matches'];
6
25
  }
7
26
  export interface ChatPayload {
8
27
  messages: Message[];
@@ -0,0 +1,110 @@
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 stop
43
+ * @param srcObject
44
+ */
45
+ onSrcObjectReady?(srcObject: MediaStream): void;
46
+ /**
47
+ * Optional callback function that will be triggered each time any changes happen in the chat
48
+ * @param progress
49
+ */
50
+ onChatEvents?(progress: ChatProgress): void;
51
+ }
52
+ export interface AgentManagerOptions {
53
+ callbacks?: ManagerCallbacks;
54
+ baseURL?: string;
55
+ debug?: boolean;
56
+ auth: Auth;
57
+ }
58
+ export interface AgentsAPI {
59
+ /**
60
+ * Agent instance you are working with.
61
+ * To know more about agents go to https://docs.d-id.com/reference/agents
62
+ */
63
+ agent: Agent;
64
+ /**
65
+ * Method to be reconnected to chat
66
+ * Since chat uses an RTC connection to communicate with the agent, it could be dropped and to continue to chat you need to reconnect
67
+ */
68
+ reconnectToChat: () => Promise<void>;
69
+ /**
70
+ * Method to close all connections with agent, stream and web socket
71
+ */
72
+ terminate: () => Promise<void>;
73
+ /**
74
+ * ID of chat you are working on now
75
+ */
76
+ chatId: string;
77
+ /**
78
+ * Method to send a chat message to existing chat with the agent
79
+ * @param messages
80
+ */
81
+ chat: (messages: Message[]) => Promise<ChatResponse>;
82
+ /**
83
+ * This method provides you the possibility to rate your chat experience
84
+ * TODO asks Sagi how it's work
85
+ * @param payload
86
+ * @param id - id of Rating entity. Leave it empty to create a new, one or pass it to work with the existing one
87
+ */
88
+ rate: (payload: RatingPayload, id?: string) => Promise<RatingEntity>;
89
+ /**
90
+ * Method to make your agent read the text you provide or reproduce sound
91
+ * @param payload
92
+ */
93
+ speak: (payload: SupportedStreamScipt) => Promise<SendStreamPayloadResponse>;
94
+ /**
95
+ * Optional callback function that will be triggered each time any changes happen in the chat
96
+ * @param callback
97
+ */
98
+ onChatEvents: (callback: ChatProgressCallback) => void;
99
+ /**
100
+ * Optional callback function that will be triggered each time the RTC connection gets new status
101
+ * @param callback
102
+ */
103
+ onConnectionEvents: (callback: ConnectionStateChangeCallback) => void;
104
+ /**
105
+ * Optional callback function that will be triggered each time video events happen
106
+ * @param callback
107
+ */
108
+ onVideoEvents: (callback: VideoStateChangeCallback) => void;
109
+ }
110
+ 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;
@@ -8,6 +8,7 @@ export declare enum StreamingState {
8
8
  Stop = "STOP"
9
9
  }
10
10
  export declare enum StreamEvents {
11
+ ChatAnswer = "chat/answer",
11
12
  ChatPartial = "chat/partial",
12
13
  StreamDone = "stream/done",
13
14
  StreamStarted = "stream/started"
@@ -18,6 +19,7 @@ export interface ManagerCallbacks {
18
19
  onVideoStateChange?: (state: StreamingState) => void;
19
20
  onSrcObjectReady?: (value: MediaStream) => void;
20
21
  }
22
+ export type ManagerCallbackKeys = keyof ManagerCallbacks;
21
23
  export interface TalkStreamOptions extends CreateTalkStreamRequest {
22
24
  videoType: VideoType.Talk;
23
25
  }
@@ -34,7 +36,7 @@ export interface RtcApi {
34
36
  close(streamId: string, sessionId: string): Promise<Status>;
35
37
  }
36
38
  export interface StreamingManagerOptions {
37
- callbacks: ManagerCallbacks;
39
+ callbacks?: ManagerCallbacks;
38
40
  baseURL?: string;
39
41
  debug?: boolean;
40
42
  auth: Auth;
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.16",
4
+ "version": "1.0.18-beta.1",
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,5 +0,0 @@
1
- export * from './api/agents';
2
- export * from './api/getClient';
3
- export * from './api/knowledge';
4
- export * from './createAgentManager';
5
- export * from './createStreamingManager';
@@ -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