@d-id/client-sdk 1.0.10 → 1.0.12

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.
@@ -1,10 +1,10 @@
1
- import { Agent, AgentPayload, Auth, ChatPayload } from '../../../../common/types/src/index';
1
+ import { Agent, AgentPayload, Auth, Chat, ChatPayload } from '../../../../common/types/src/index';
2
2
  export declare function createAgentsApi(auth: Auth, host?: string): {
3
3
  create(payload: AgentPayload): Promise<Agent>;
4
4
  getAgents(tag?: string): Promise<Agent[]>;
5
5
  getById(id: string): Promise<Agent>;
6
6
  delete(id: string): Promise<any>;
7
7
  update(id: string, payload: AgentPayload): Promise<Agent>;
8
- newChat(agentId: string): Promise<Agent>;
9
- chat(agentId: string, payload: ChatPayload): Promise<Agent>;
8
+ newChat(agentId: string): Promise<Chat>;
9
+ chat(agentId: string, chatId: string, payload: ChatPayload): Promise<string>;
10
10
  };
@@ -1,7 +1,7 @@
1
1
  import { Auth } from '../../../../common/types/src/auth';
2
2
  export declare function createClient(auth: Auth, host?: string): {
3
3
  get<T = any>(url: string, options?: RequestInit): Promise<T>;
4
- post<T_1 = any>(url: string, body: any, options?: RequestInit): Promise<T_1>;
4
+ post<T_1 = any>(url: string, body?: any, options?: RequestInit): Promise<T_1>;
5
5
  delete<T_2 = any>(url: string, body?: any, options?: RequestInit): Promise<T_2>;
6
6
  patch<T_3 = any>(url: string, body?: any, options?: RequestInit): Promise<T_3>;
7
7
  };
@@ -0,0 +1,2 @@
1
+ import { Auth } from '../../../../common/types/src/auth';
2
+ export declare function getAuthHeader(auth: Auth): string;
@@ -0,0 +1,15 @@
1
+ import { Auth } from '../../../common/types/src/auth';
2
+ interface Options {
3
+ auth: Auth;
4
+ retries?: number;
5
+ callbacks: {
6
+ onMessage: (event: MessageEvent) => void;
7
+ onOpen?: (event: Event) => void;
8
+ onClose?: (event: CloseEvent) => void;
9
+ onError?: (event: Event) => void;
10
+ };
11
+ host?: string;
12
+ }
13
+ export declare function connect(options: Options): Promise<WebSocket>;
14
+ export declare function connectToSocket(options: Options): Promise<WebSocket>;
15
+ export {};
@@ -1,4 +1,5 @@
1
+ export * from './api/agents';
1
2
  export * from './api/getClient';
2
3
  export * from './api/knowledge';
3
- export * from './api/agents';
4
+ export * from './connectToSocket';
4
5
  export * from './createStreamingManager';
@@ -15,14 +15,3 @@ export interface Agent {
15
15
  preview_thumbnail?: string;
16
16
  }
17
17
  export type AgentPayload = Omit<Agent, 'type' | 'created_at' | 'modified_at' | 'id' | 'owner_id' | 'idle_video_url'>;
18
- export interface Message {
19
- role: 'system' | 'assistant' | 'user' | 'function' | 'tool';
20
- content: string;
21
- name?: string;
22
- }
23
- export interface ChatPayload {
24
- messages: Message[];
25
- chatId: string;
26
- streamId: string;
27
- sessionId: string;
28
- }
@@ -0,0 +1,21 @@
1
+ export interface Message {
2
+ role: 'system' | 'assistant' | 'user' | 'function' | 'tool';
3
+ content: string;
4
+ name?: string;
5
+ created_at: string;
6
+ }
7
+ export interface ChatPayload {
8
+ messages: Message[];
9
+ streamId: string;
10
+ sessionId: string;
11
+ }
12
+ export interface Chat {
13
+ agent_id: string;
14
+ created: Message[];
15
+ messages: string;
16
+ agent_id__created_at: string;
17
+ agent_id__modified_at: string;
18
+ modified: string;
19
+ owner_id: string;
20
+ id: string;
21
+ }
@@ -1 +1,2 @@
1
1
  export * from './agent';
2
+ export * from './chat';
package/dist/index.js CHANGED
@@ -1,246 +1,292 @@
1
- function k(r) {
1
+ function I(r) {
2
2
  return r.type === "bearer" ? "Bearer " + r.token : r.type === "basic" ? "Basic " + btoa(`${r.username}:${r.password}`) : "Client-Key " + r.clientKey;
3
3
  }
4
- function g(r, o = "https://api.d-id.com") {
5
- const n = async (e, t) => {
4
+ function $(r, o = "https://api.d-id.com") {
5
+ const t = async (e, n) => {
6
6
  const s = await fetch(o + (e != null && e.startsWith("/") ? e : `/${e}`), {
7
- ...t,
7
+ ...n,
8
8
  headers: {
9
- ...t == null ? void 0 : t.headers,
10
- Authorization: k(r),
9
+ ...n == null ? void 0 : n.headers,
10
+ Authorization: I(r),
11
11
  "Content-Type": "application/json"
12
12
  }
13
13
  });
14
14
  if (!s.ok) {
15
- let u = await s.text().catch(() => "Failed to fetch");
15
+ let l = await s.text().catch(() => "Failed to fetch");
16
16
  try {
17
- const c = JSON.parse(u);
18
- c.description ? u = c.description : c.message && (u = c.message);
17
+ const d = JSON.parse(l);
18
+ d.description ? l = d.description : d.message && (l = d.message);
19
19
  } catch {
20
20
  }
21
- throw new Error(u);
21
+ throw new Error(l);
22
22
  }
23
23
  return s.json();
24
24
  };
25
25
  return {
26
- get(e, t) {
27
- return n(e, {
28
- ...t,
26
+ get(e, n) {
27
+ return t(e, {
28
+ ...n,
29
29
  method: "GET"
30
30
  });
31
31
  },
32
- post(e, t, s) {
33
- return n(e, {
32
+ post(e, n, s) {
33
+ return t(e, {
34
34
  ...s,
35
- body: JSON.stringify(t),
35
+ body: JSON.stringify(n),
36
36
  method: "POST"
37
37
  });
38
38
  },
39
- delete(e, t, s) {
40
- return n(e, {
39
+ delete(e, n, s) {
40
+ return t(e, {
41
41
  ...s,
42
- body: JSON.stringify(t),
42
+ body: JSON.stringify(n),
43
43
  method: "DELETE"
44
44
  });
45
45
  },
46
- patch(e, t, s) {
47
- return n(e, {
46
+ patch(e, n, s) {
47
+ return t(e, {
48
48
  ...s,
49
- body: JSON.stringify(t),
49
+ body: JSON.stringify(n),
50
50
  method: "PATCH"
51
51
  });
52
52
  }
53
53
  };
54
54
  }
55
- function K(r, o = "https://api.d-id.com") {
56
- const n = g(r, `${o}/knowledge`);
55
+ function O(r, o = "https://api.d-id.com") {
56
+ const t = $(r, `${o}/agents`);
57
+ return {
58
+ create(e) {
59
+ return t.post("/", e);
60
+ },
61
+ getAgents(e) {
62
+ return t.get(`/${e ? `?tag=${e}` : ""}`);
63
+ },
64
+ getById(e) {
65
+ return t.get(`/${e}`);
66
+ },
67
+ delete(e) {
68
+ return t.delete(`/${e}`);
69
+ },
70
+ update(e, n) {
71
+ return t.patch(`/${e}`, n);
72
+ },
73
+ newChat(e) {
74
+ return t.post(`/${e}/chat`);
75
+ },
76
+ chat(e, n, s) {
77
+ return t.post(`/${e}/chat/${n}`, s);
78
+ }
79
+ };
80
+ }
81
+ function R(r, o = "https://api.d-id.com") {
82
+ const t = $(r, `${o}/knowledge`);
57
83
  return {
58
84
  createKnowledge(e) {
59
- return n.post("/", e);
85
+ return t.post("/", e);
60
86
  },
61
87
  getKnowledgeBase() {
62
- return n.get("/");
88
+ return t.get("/");
63
89
  },
64
90
  getKnowledge(e) {
65
- return n.get(`/${e}`);
91
+ return t.get(`/${e}`);
66
92
  },
67
93
  deleteKnowledge(e) {
68
- return n.delete(`/${e}`);
94
+ return t.delete(`/${e}`);
69
95
  },
70
- createDocument(e, t) {
71
- return n.post(`/${e}/documents`, t);
96
+ createDocument(e, n) {
97
+ return t.post(`/${e}/documents`, n);
72
98
  },
73
- deleteDocument(e, t) {
74
- return n.delete(`/${e}/documents/${t}`);
99
+ deleteDocument(e, n) {
100
+ return t.delete(`/${e}/documents/${n}`);
75
101
  },
76
102
  getDocuments(e) {
77
- return n.get(`/${e}/documents`);
103
+ return t.get(`/${e}/documents`);
78
104
  },
79
- getDocument(e, t) {
80
- return n.get(`/${e}/documents/${t}`);
105
+ getDocument(e, n) {
106
+ return t.get(`/${e}/documents/${n}`);
81
107
  },
82
- getRecords(e, t) {
83
- return n.get(`/${e}/documents/${t}/records`);
108
+ getRecords(e, n) {
109
+ return t.get(`/${e}/documents/${n}/records`);
84
110
  },
85
- query(e, t) {
86
- return n.post(`/${e}/query`, {
87
- query: t
111
+ query(e, n) {
112
+ return t.post(`/${e}/query`, {
113
+ query: n
88
114
  });
89
115
  }
90
116
  };
91
117
  }
92
- function O(r, o = "https://api.d-id.com") {
93
- const n = g(r, `${o}/agents`);
94
- return {
95
- create(e) {
96
- return n.post("/", e);
97
- },
98
- getAgents(e) {
99
- return n.get(`/${e ? `?tag=${e}` : ""}`);
100
- },
101
- getById(e) {
102
- return n.get(`/${e}`);
103
- },
104
- delete(e) {
105
- return n.delete(`/${e}`);
106
- },
107
- update(e, t) {
108
- return n.patch(`/${e}`, t);
109
- },
110
- newChat(e) {
111
- return n.patch(`/${e}/chat/new`);
112
- },
113
- chat(e, t) {
114
- return n.post(`/${e}/chat`, t);
118
+ function b(r) {
119
+ if (r.type === "bearer")
120
+ return `Bearer ${r.token}`;
121
+ if (r.type === "basic")
122
+ return `Basic ${btoa(`${r.username}:${r.password}`)}`;
123
+ if (r.type === "key")
124
+ return `Client-Key ${r.clientKey}`;
125
+ throw new Error(`Unknown auth type: ${r}`);
126
+ }
127
+ const T = (r) => new Promise((o) => setTimeout(o, r));
128
+ function A(r) {
129
+ return new Promise((o, t) => {
130
+ const {
131
+ callbacks: e,
132
+ host: n,
133
+ auth: s
134
+ } = r, {
135
+ onMessage: l,
136
+ onOpen: d,
137
+ onClose: f = null,
138
+ onError: g
139
+ } = e, u = new WebSocket(`${n}?authorization=${b(s)}`);
140
+ u.onmessage = l, u.onclose = f, u.onerror = (c) => {
141
+ console.log(c), g == null || g(c), t(c);
142
+ }, u.onopen = (c) => {
143
+ d == null || d(c), o(u);
144
+ };
145
+ });
146
+ }
147
+ async function M(r) {
148
+ const {
149
+ retries: o = 1
150
+ } = r;
151
+ let t = null;
152
+ for (let e = 0; (t == null ? void 0 : t.readyState) !== WebSocket.OPEN; e++)
153
+ try {
154
+ t = await A(r);
155
+ } catch (n) {
156
+ if (e === o)
157
+ throw n;
158
+ await T(e * 500);
115
159
  }
116
- };
160
+ return t;
117
161
  }
118
- function A(r, o) {
119
- const n = g(r, o);
162
+ function K(r, o) {
163
+ const t = $(r, o);
120
164
  return {
121
165
  createStream(e) {
122
- return n.post("/clips/streams", {
166
+ return t.post("/clips/streams", {
123
167
  driver_id: e.driver_id,
124
168
  presenter_id: e.presenter_id,
125
169
  compatibility_mode: e.compatibility_mode
126
170
  });
127
171
  },
128
- startConnection(e, t, s) {
129
- return n.post(`/clips/streams/${e}/sdp`, {
172
+ startConnection(e, n, s) {
173
+ return t.post(`/clips/streams/${e}/sdp`, {
130
174
  session_id: s,
131
- answer: t
175
+ answer: n
132
176
  });
133
177
  },
134
- addIceCandidate(e, t, s) {
135
- return n.post(`/clips/streams/${e}/ice`, {
178
+ addIceCandidate(e, n, s) {
179
+ return t.post(`/clips/streams/${e}/ice`, {
136
180
  session_id: s,
137
- ...t
181
+ ...n
138
182
  });
139
183
  },
140
- sendStreamRequest(e, t, s) {
141
- return n.post(`/clips/streams/${e}`, {
142
- session_id: t,
184
+ sendStreamRequest(e, n, s) {
185
+ return t.post(`/clips/streams/${e}`, {
186
+ session_id: n,
143
187
  ...s
144
188
  });
145
189
  },
146
- close(e, t) {
147
- return n.delete(`/clips/streams/${e}`, {
148
- session_id: t
190
+ close(e, n) {
191
+ return t.delete(`/clips/streams/${e}`, {
192
+ session_id: n
149
193
  });
150
194
  }
151
195
  };
152
196
  }
153
- function T(r, o) {
154
- const n = g(r, o);
197
+ function D(r, o) {
198
+ const t = $(r, o);
155
199
  return {
156
200
  createStream(e) {
157
- return n.post("/talks/streams", {
201
+ return t.post("/talks/streams", {
158
202
  source_url: e.source_url,
159
203
  driver_url: e.driver_url,
160
204
  face: e.face,
161
205
  config: e.config
162
206
  });
163
207
  },
164
- startConnection(e, t, s) {
165
- return n.post(`/talks/streams/${e}/sdp`, {
208
+ startConnection(e, n, s) {
209
+ return t.post(`/talks/streams/${e}/sdp`, {
166
210
  session_id: s,
167
- answer: t
211
+ answer: n
168
212
  });
169
213
  },
170
- addIceCandidate(e, t, s) {
171
- return n.post(`/talks/streams/${e}/ice`, {
214
+ addIceCandidate(e, n, s) {
215
+ return t.post(`/talks/streams/${e}/ice`, {
172
216
  session_id: s,
173
- ...t
217
+ ...n
174
218
  });
175
219
  },
176
- sendStreamRequest(e, t, s) {
177
- return n.post(`/talks/streams/${e}`, {
178
- session_id: t,
220
+ sendStreamRequest(e, n, s) {
221
+ return t.post(`/talks/streams/${e}`, {
222
+ session_id: n,
179
223
  ...s
180
224
  });
181
225
  },
182
- close(e, t) {
183
- return n.delete(`/talks/streams/${e}`, {
184
- session_id: t
226
+ close(e, n) {
227
+ return t.delete(`/talks/streams/${e}`, {
228
+ session_id: n
185
229
  });
186
230
  }
187
231
  };
188
232
  }
189
- var m = /* @__PURE__ */ ((r) => (r.Idle = "IDLE", r.Streaming = "STREAMING", r))(m || {}), f = /* @__PURE__ */ ((r) => (r.Clip = "clip", r.Talk = "talk", r))(f || {});
190
- let $ = !1;
191
- const d = (r, o) => $ && console.log(r, o), D = (window.RTCPeerConnection || window.webkitRTCPeerConnection || window.mozRTCPeerConnection).bind(window);
192
- async function E(r, {
233
+ var w = /* @__PURE__ */ ((r) => (r.Idle = "IDLE", r.Streaming = "STREAMING", r))(w || {}), h = /* @__PURE__ */ ((r) => (r.Clip = "clip", r.Talk = "talk", r))(h || {});
234
+ let S = !1;
235
+ const p = (r, o) => S && console.log(r, o), E = (window.RTCPeerConnection || window.webkitRTCPeerConnection || window.mozRTCPeerConnection).bind(window);
236
+ async function P(r, {
193
237
  debug: o = !1,
194
- callbacks: n,
238
+ callbacks: t,
195
239
  auth: e,
196
- baseURL: t = "https://api.d-id.com"
240
+ baseURL: n = "https://api.d-id.com"
197
241
  }) {
198
- $ = o;
242
+ S = o;
199
243
  let s = null;
200
244
  const {
201
- startConnection: u,
202
- sendStreamRequest: c,
203
- close: h,
204
- createStream: S,
205
- addIceCandidate: _
206
- } = r.videoType === f.Clip ? A(e, t) : T(e, t), {
207
- id: l,
208
- offer: y,
209
- ice_servers: I,
210
- session_id: p
211
- } = await S(r), a = new D({
212
- iceServers: I
245
+ startConnection: l,
246
+ sendStreamRequest: d,
247
+ close: f,
248
+ createStream: g,
249
+ addIceCandidate: u
250
+ } = r.videoType === h.Clip ? K(e, n) : D(e, n), {
251
+ id: c,
252
+ offer: _,
253
+ ice_servers: k,
254
+ session_id: m
255
+ } = await g(r), a = new E({
256
+ iceServers: k
213
257
  }), C = a.createDataChannel("JanusDataChannel");
214
- if (!p)
258
+ if (!m)
215
259
  throw new Error("Could not create session_id");
216
260
  a.onicecandidate = (i) => {
217
- d("peerConnection.onicecandidate", i), i.candidate && i.candidate.sdpMid && i.candidate.sdpMLineIndex !== null && _(l, {
261
+ p("peerConnection.onicecandidate", i), i.candidate && i.candidate.sdpMid && i.candidate.sdpMLineIndex !== null && u(c, {
218
262
  candidate: i.candidate.candidate,
219
263
  sdpMid: i.candidate.sdpMid,
220
264
  sdpMLineIndex: i.candidate.sdpMLineIndex
221
- }, p);
265
+ }, m);
222
266
  }, a.oniceconnectionstatechange = () => {
223
- d("peerConnection.oniceconnectionstatechange => " + a.iceConnectionState), n.onConnectionStateChange(a.iceConnectionState);
267
+ p("peerConnection.oniceconnectionstatechange => " + a.iceConnectionState), t.onConnectionStateChange(a.iceConnectionState);
224
268
  }, a.ontrack = (i) => {
225
- d("peerConnection.ontrack", i), s = i.streams[0], n.onSrcObjectReady(s);
269
+ p("peerConnection.ontrack", i), s = i.streams[0], t.onSrcObjectReady(s);
226
270
  }, C.onmessage = (i) => {
227
- C.readyState === "open" && n.onStreamingStateChange(i.data === "stream/done" ? m.Idle : m.Streaming);
228
- }, await a.setRemoteDescription(y), d("set remote description OK");
229
- const w = await a.createAnswer();
230
- return d("create answer OK"), await a.setLocalDescription(w), d("set local description OK"), await u(l, w, p), d("start connection OK"), {
271
+ C.readyState === "open" && t.onStreamingStateChange(i.data === "stream/done" ? w.Idle : w.Streaming);
272
+ }, await a.setRemoteDescription(_), p("set remote description OK");
273
+ const y = await a.createAnswer();
274
+ return p("create answer OK"), await a.setLocalDescription(y), p("set local description OK"), await l(c, y, m), p("start connection OK"), {
231
275
  speak(i) {
232
- return c(l, p, i);
276
+ return d(c, m, i);
233
277
  },
234
278
  async terminate() {
235
- l && (s && (s.getTracks().forEach((i) => i.stop()), s = null), a && (a.close(), a.oniceconnectionstatechange = null, a.onnegotiationneeded = null, a.onicecandidate = null, a.ontrack = null), await h(l, p), n.onConnectionStateChange("closed"), n.onStreamingStateChange(m.Idle));
279
+ c && (s && (s.getTracks().forEach((i) => i.stop()), s = null), a && (a.close(), a.oniceconnectionstatechange = null, a.onnegotiationneeded = null, a.onicecandidate = null, a.ontrack = null), await f(c, m), t.onConnectionStateChange("closed"), t.onStreamingStateChange(w.Idle));
236
280
  },
237
- sessionId: p,
238
- streamId: l
281
+ sessionId: m,
282
+ streamId: c
239
283
  };
240
284
  }
241
285
  export {
286
+ A as connect,
287
+ M as connectToSocket,
242
288
  O as createAgentsApi,
243
- g as createClient,
244
- K as createKnowledgeApi,
245
- E as createStreamingManager
289
+ $ as createClient,
290
+ R as createKnowledgeApi,
291
+ P as createStreamingManager
246
292
  };
@@ -1 +1 @@
1
- (function(c,g){typeof exports=="object"&&typeof module<"u"?g(exports):typeof define=="function"&&define.amd?define(["exports"],g):(c=typeof globalThis<"u"?globalThis:c||self,g(c.index={}))})(this,function(c){"use strict";function g(r){return r.type==="bearer"?"Bearer "+r.token:r.type==="basic"?"Basic "+btoa(`${r.username}:${r.password}`):"Client-Key "+r.clientKey}function f(r,o="https://api.d-id.com"){const n=async(e,t)=>{const i=await fetch(o+(e!=null&&e.startsWith("/")?e:`/${e}`),{...t,headers:{...t==null?void 0:t.headers,Authorization:g(r),"Content-Type":"application/json"}});if(!i.ok){let l=await i.text().catch(()=>"Failed to fetch");try{const u=JSON.parse(l);u.description?l=u.description:u.message&&(l=u.message)}catch{}throw new Error(l)}return i.json()};return{get(e,t){return n(e,{...t,method:"GET"})},post(e,t,i){return n(e,{...i,body:JSON.stringify(t),method:"POST"})},delete(e,t,i){return n(e,{...i,body:JSON.stringify(t),method:"DELETE"})},patch(e,t,i){return n(e,{...i,body:JSON.stringify(t),method:"PATCH"})}}}function y(r,o="https://api.d-id.com"){const n=f(r,`${o}/knowledge`);return{createKnowledge(e){return n.post("/",e)},getKnowledgeBase(){return n.get("/")},getKnowledge(e){return n.get(`/${e}`)},deleteKnowledge(e){return n.delete(`/${e}`)},createDocument(e,t){return n.post(`/${e}/documents`,t)},deleteDocument(e,t){return n.delete(`/${e}/documents/${t}`)},getDocuments(e){return n.get(`/${e}/documents`)},getDocument(e,t){return n.get(`/${e}/documents/${t}`)},getRecords(e,t){return n.get(`/${e}/documents/${t}/records`)},query(e,t){return n.post(`/${e}/query`,{query:t})}}}function _(r,o="https://api.d-id.com"){const n=f(r,`${o}/agents`);return{create(e){return n.post("/",e)},getAgents(e){return n.get(`/${e?`?tag=${e}`:""}`)},getById(e){return n.get(`/${e}`)},delete(e){return n.delete(`/${e}`)},update(e,t){return n.patch(`/${e}`,t)},newChat(e){return n.patch(`/${e}/chat/new`)},chat(e,t){return n.post(`/${e}/chat`,t)}}}function A(r,o){const n=f(r,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,i){return n.post(`/clips/streams/${e}/sdp`,{session_id:i,answer:t})},addIceCandidate(e,t,i){return n.post(`/clips/streams/${e}/ice`,{session_id:i,...t})},sendStreamRequest(e,t,i){return n.post(`/clips/streams/${e}`,{session_id:t,...i})},close(e,t){return n.delete(`/clips/streams/${e}`,{session_id:t})}}}function I(r,o){const n=f(r,o);return{createStream(e){return n.post("/talks/streams",{source_url:e.source_url,driver_url:e.driver_url,face:e.face,config:e.config})},startConnection(e,t,i){return n.post(`/talks/streams/${e}/sdp`,{session_id:i,answer:t})},addIceCandidate(e,t,i){return n.post(`/talks/streams/${e}/ice`,{session_id:i,...t})},sendStreamRequest(e,t,i){return n.post(`/talks/streams/${e}`,{session_id:t,...i})},close(e,t){return n.delete(`/talks/streams/${e}`,{session_id:t})}}}var C=(r=>(r.Idle="IDLE",r.Streaming="STREAMING",r))(C||{}),w=(r=>(r.Clip="clip",r.Talk="talk",r))(w||{});let h=!1;const d=(r,o)=>h&&console.log(r,o),T=(window.RTCPeerConnection||window.webkitRTCPeerConnection||window.mozRTCPeerConnection).bind(window);async function k(r,{debug:o=!1,callbacks:n,auth:e,baseURL:t="https://api.d-id.com"}){h=o;let i=null;const{startConnection:l,sendStreamRequest:u,close:K,createStream:O,addIceCandidate:b}=r.videoType===w.Clip?A(e,t):I(e,t),{id:p,offer:D,ice_servers:E,session_id:m}=await O(r),a=new T({iceServers:E}),$=a.createDataChannel("JanusDataChannel");if(!m)throw new Error("Could not create session_id");a.onicecandidate=s=>{d("peerConnection.onicecandidate",s),s.candidate&&s.candidate.sdpMid&&s.candidate.sdpMLineIndex!==null&&b(p,{candidate:s.candidate.candidate,sdpMid:s.candidate.sdpMid,sdpMLineIndex:s.candidate.sdpMLineIndex},m)},a.oniceconnectionstatechange=()=>{d("peerConnection.oniceconnectionstatechange => "+a.iceConnectionState),n.onConnectionStateChange(a.iceConnectionState)},a.ontrack=s=>{d("peerConnection.ontrack",s),i=s.streams[0],n.onSrcObjectReady(i)},$.onmessage=s=>{$.readyState==="open"&&n.onStreamingStateChange(s.data==="stream/done"?C.Idle:C.Streaming)},await a.setRemoteDescription(D),d("set remote description OK");const S=await a.createAnswer();return d("create answer OK"),await a.setLocalDescription(S),d("set local description OK"),await l(p,S,m),d("start connection OK"),{speak(s){return u(p,m,s)},async terminate(){p&&(i&&(i.getTracks().forEach(s=>s.stop()),i=null),a&&(a.close(),a.oniceconnectionstatechange=null,a.onnegotiationneeded=null,a.onicecandidate=null,a.ontrack=null),await K(p,m),n.onConnectionStateChange("closed"),n.onStreamingStateChange(C.Idle))},sessionId:m,streamId:p}}c.createAgentsApi=_,c.createClient=f,c.createKnowledgeApi=y,c.createStreamingManager=k,Object.defineProperty(c,Symbol.toStringTag,{value:"Module"})});
1
+ (function(d,f){typeof exports=="object"&&typeof module<"u"?f(exports):typeof define=="function"&&define.amd?define(["exports"],f):(d=typeof globalThis<"u"?globalThis:d||self,f(d.index={}))})(this,function(d){"use strict";function f(r){return r.type==="bearer"?"Bearer "+r.token:r.type==="basic"?"Basic "+btoa(`${r.username}:${r.password}`):"Client-Key "+r.clientKey}function w(r,s="https://api.d-id.com"){const t=async(e,n)=>{const o=await fetch(s+(e!=null&&e.startsWith("/")?e:`/${e}`),{...n,headers:{...n==null?void 0:n.headers,Authorization:f(r),"Content-Type":"application/json"}});if(!o.ok){let u=await o.text().catch(()=>"Failed to fetch");try{const l=JSON.parse(u);l.description?u=l.description:l.message&&(u=l.message)}catch{}throw new Error(u)}return o.json()};return{get(e,n){return t(e,{...n,method:"GET"})},post(e,n,o){return t(e,{...o,body:JSON.stringify(n),method:"POST"})},delete(e,n,o){return t(e,{...o,body:JSON.stringify(n),method:"DELETE"})},patch(e,n,o){return t(e,{...o,body:JSON.stringify(n),method:"PATCH"})}}}function b(r,s="https://api.d-id.com"){const t=w(r,`${s}/agents`);return{create(e){return t.post("/",e)},getAgents(e){return t.get(`/${e?`?tag=${e}`:""}`)},getById(e){return t.get(`/${e}`)},delete(e){return t.delete(`/${e}`)},update(e,n){return t.patch(`/${e}`,n)},newChat(e){return t.post(`/${e}/chat`)},chat(e,n,o){return t.post(`/${e}/chat/${n}`,o)}}}function A(r,s="https://api.d-id.com"){const t=w(r,`${s}/knowledge`);return{createKnowledge(e){return t.post("/",e)},getKnowledgeBase(){return t.get("/")},getKnowledge(e){return t.get(`/${e}`)},deleteKnowledge(e){return t.delete(`/${e}`)},createDocument(e,n){return t.post(`/${e}/documents`,n)},deleteDocument(e,n){return t.delete(`/${e}/documents/${n}`)},getDocuments(e){return t.get(`/${e}/documents`)},getDocument(e,n){return t.get(`/${e}/documents/${n}`)},getRecords(e,n){return t.get(`/${e}/documents/${n}/records`)},query(e,n){return t.post(`/${e}/query`,{query:n})}}}function I(r){if(r.type==="bearer")return`Bearer ${r.token}`;if(r.type==="basic")return`Basic ${btoa(`${r.username}:${r.password}`)}`;if(r.type==="key")return`Client-Key ${r.clientKey}`;throw new Error(`Unknown auth type: ${r}`)}const K=r=>new Promise(s=>setTimeout(s,r));function h(r){return new Promise((s,t)=>{const{callbacks:e,host:n,auth:o}=r,{onMessage:u,onOpen:l,onClose:y=null,onError:C}=e,m=new WebSocket(`${n}?authorization=${I(o)}`);m.onmessage=u,m.onclose=y,m.onerror=c=>{console.log(c),C==null||C(c),t(c)},m.onopen=c=>{l==null||l(c),s(m)}})}async function O(r){const{retries:s=1}=r;let t=null;for(let e=0;(t==null?void 0:t.readyState)!==WebSocket.OPEN;e++)try{t=await h(r)}catch(n){if(e===s)throw n;await K(e*500)}return t}function D(r,s){const t=w(r,s);return{createStream(e){return t.post("/clips/streams",{driver_id:e.driver_id,presenter_id:e.presenter_id,compatibility_mode:e.compatibility_mode})},startConnection(e,n,o){return t.post(`/clips/streams/${e}/sdp`,{session_id:o,answer:n})},addIceCandidate(e,n,o){return t.post(`/clips/streams/${e}/ice`,{session_id:o,...n})},sendStreamRequest(e,n,o){return t.post(`/clips/streams/${e}`,{session_id:n,...o})},close(e,n){return t.delete(`/clips/streams/${e}`,{session_id:n})}}}function E(r,s){const t=w(r,s);return{createStream(e){return t.post("/talks/streams",{source_url:e.source_url,driver_url:e.driver_url,face:e.face,config:e.config})},startConnection(e,n,o){return t.post(`/talks/streams/${e}/sdp`,{session_id:o,answer:n})},addIceCandidate(e,n,o){return t.post(`/talks/streams/${e}/ice`,{session_id:o,...n})},sendStreamRequest(e,n,o){return t.post(`/talks/streams/${e}`,{session_id:n,...o})},close(e,n){return t.delete(`/talks/streams/${e}`,{session_id:n})}}}var $=(r=>(r.Idle="IDLE",r.Streaming="STREAMING",r))($||{}),S=(r=>(r.Clip="clip",r.Talk="talk",r))(S||{});let _=!1;const p=(r,s)=>_&&console.log(r,s),M=(window.RTCPeerConnection||window.webkitRTCPeerConnection||window.mozRTCPeerConnection).bind(window);async function P(r,{debug:s=!1,callbacks:t,auth:e,baseURL:n="https://api.d-id.com"}){_=s;let o=null;const{startConnection:u,sendStreamRequest:l,close:y,createStream:C,addIceCandidate:m}=r.videoType===S.Clip?D(e,n):E(e,n),{id:c,offer:R,ice_servers:j,session_id:g}=await C(r),a=new M({iceServers:j}),k=a.createDataChannel("JanusDataChannel");if(!g)throw new Error("Could not create session_id");a.onicecandidate=i=>{p("peerConnection.onicecandidate",i),i.candidate&&i.candidate.sdpMid&&i.candidate.sdpMLineIndex!==null&&m(c,{candidate:i.candidate.candidate,sdpMid:i.candidate.sdpMid,sdpMLineIndex:i.candidate.sdpMLineIndex},g)},a.oniceconnectionstatechange=()=>{p("peerConnection.oniceconnectionstatechange => "+a.iceConnectionState),t.onConnectionStateChange(a.iceConnectionState)},a.ontrack=i=>{p("peerConnection.ontrack",i),o=i.streams[0],t.onSrcObjectReady(o)},k.onmessage=i=>{k.readyState==="open"&&t.onStreamingStateChange(i.data==="stream/done"?$.Idle:$.Streaming)},await a.setRemoteDescription(R),p("set remote description OK");const T=await a.createAnswer();return p("create answer OK"),await a.setLocalDescription(T),p("set local description OK"),await u(c,T,g),p("start connection OK"),{speak(i){return l(c,g,i)},async terminate(){c&&(o&&(o.getTracks().forEach(i=>i.stop()),o=null),a&&(a.close(),a.oniceconnectionstatechange=null,a.onnegotiationneeded=null,a.onicecandidate=null,a.ontrack=null),await y(c,g),t.onConnectionStateChange("closed"),t.onStreamingStateChange($.Idle))},sessionId:g,streamId:c}}d.connect=h,d.connectToSocket=O,d.createAgentsApi=b,d.createClient=w,d.createKnowledgeApi=A,d.createStreamingManager=P,Object.defineProperty(d,Symbol.toStringTag,{value:"Module"})});
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@d-id/client-sdk",
3
3
  "private": false,
4
- "version": "1.0.10",
4
+ "version": "1.0.12",
5
5
  "type": "module",
6
6
  "description": "d-id client sdk",
7
7
  "repository": {
@@ -23,7 +23,7 @@
23
23
  "types": "./dist/apps/sdk/lib/index.d.ts",
24
24
  "exports": {
25
25
  ".": {
26
- "types": "./dist/lib/index.d.ts",
26
+ "types": "./dist/apps/sdk/lib/index.d.ts",
27
27
  "import": "./dist/index.js",
28
28
  "require": "./dist/index.umd.cjs"
29
29
  }
@@ -31,6 +31,7 @@
31
31
  "scripts": {
32
32
  "dev": "vite",
33
33
  "build": "tsc && vite build",
34
+ "build:dev": "tsc && vite build --mode development",
34
35
  "dopublish": "yarn build && npm publish --access public",
35
36
  "preview": "vite preview"
36
37
  },
@@ -38,9 +39,13 @@
38
39
  "@preact/preset-vite": "^2.5.0",
39
40
  "glob": "^10.3.10",
40
41
  "preact": "^10.5.13",
41
- "typescript": "^5.2.2",
42
42
  "vite": "^4.4.5",
43
43
  "vite-plugin-dts": "^3.6.0",
44
- "vite-plugin-html-env": "^1.2.8"
44
+ "vite-plugin-html-env": "^1.2.8",
45
+ "@trivago/prettier-plugin-sort-imports": "^4.3.0",
46
+ "prettier": "^3.1.0",
47
+ "prettier-plugin-organize-imports": "^3.2.4",
48
+ "serverless": "3.32.2",
49
+ "typescript": "^5.3.2"
45
50
  }
46
51
  }