@d-id/client-sdk 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.js +233 -0
- package/dist/index.umd.cjs +1 -0
- package/dist/lib/api/agents.d.ts +8 -0
- package/dist/lib/api/clipStream.d.ts +2 -0
- package/dist/lib/api/getClient.d.ts +7 -0
- package/dist/lib/api/knowledge.d.ts +13 -0
- package/dist/lib/api/talkStream.d.ts +2 -0
- package/dist/lib/createStreamingManager.d.ts +8 -0
- package/dist/lib/index.d.ts +4 -0
- package/package.json +47 -0
package/dist/index.js
ADDED
|
@@ -0,0 +1,233 @@
|
|
|
1
|
+
function T(r) {
|
|
2
|
+
return r.type === "bearer" ? "Bearer " + r.token : r.type === "basic" ? "Basic " + btoa(`${r.username}:${r.password}`) : "Client-Key " + r.clientKey;
|
|
3
|
+
}
|
|
4
|
+
function p(r, o = "https://api.d-id.com") {
|
|
5
|
+
const n = async (e, t) => {
|
|
6
|
+
const s = await fetch(o + (e != null && e.startsWith("/") ? e : `/${e}`), {
|
|
7
|
+
...t,
|
|
8
|
+
headers: {
|
|
9
|
+
...t == null ? void 0 : t.headers,
|
|
10
|
+
Authorization: T(r),
|
|
11
|
+
"Content-Type": "application/json"
|
|
12
|
+
}
|
|
13
|
+
});
|
|
14
|
+
if (!s.ok)
|
|
15
|
+
throw new Error(s.statusText);
|
|
16
|
+
return s.json();
|
|
17
|
+
};
|
|
18
|
+
return {
|
|
19
|
+
get(e, t) {
|
|
20
|
+
return n(e, {
|
|
21
|
+
...t,
|
|
22
|
+
method: "GET"
|
|
23
|
+
});
|
|
24
|
+
},
|
|
25
|
+
post(e, t, s) {
|
|
26
|
+
return n(e, {
|
|
27
|
+
...s,
|
|
28
|
+
body: JSON.stringify(t),
|
|
29
|
+
method: "POST"
|
|
30
|
+
});
|
|
31
|
+
},
|
|
32
|
+
delete(e, t, s) {
|
|
33
|
+
return n(e, {
|
|
34
|
+
...s,
|
|
35
|
+
body: JSON.stringify(t),
|
|
36
|
+
method: "DELETE"
|
|
37
|
+
});
|
|
38
|
+
},
|
|
39
|
+
patch(e, t, s) {
|
|
40
|
+
return n(e, {
|
|
41
|
+
...s,
|
|
42
|
+
body: JSON.stringify(t),
|
|
43
|
+
method: "PATCH"
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
function K(r, o = "https://api.d-id.com") {
|
|
49
|
+
const n = p(r, `${o}/knowledge`);
|
|
50
|
+
return {
|
|
51
|
+
createKnowledge(e) {
|
|
52
|
+
return n.post("/", e);
|
|
53
|
+
},
|
|
54
|
+
getKnowledgeBase() {
|
|
55
|
+
return n.get("/");
|
|
56
|
+
},
|
|
57
|
+
getKnowledge(e) {
|
|
58
|
+
return n.get(`/${e}`);
|
|
59
|
+
},
|
|
60
|
+
deleteKnowledge(e) {
|
|
61
|
+
return n.delete(`/${e}`);
|
|
62
|
+
},
|
|
63
|
+
createDocument(e, t) {
|
|
64
|
+
return n.post(`/${e}/documents`, t);
|
|
65
|
+
},
|
|
66
|
+
deleteDocument(e, t) {
|
|
67
|
+
return n.delete(`/${e}/documents/${t}`);
|
|
68
|
+
},
|
|
69
|
+
getDocuments(e) {
|
|
70
|
+
return n.get(`/${e}/documents`);
|
|
71
|
+
},
|
|
72
|
+
getDocument(e, t) {
|
|
73
|
+
return n.get(`/${e}/documents/${t}`);
|
|
74
|
+
},
|
|
75
|
+
getRecords(e, t) {
|
|
76
|
+
return n.get(`/${e}/documents/${t}/records`);
|
|
77
|
+
},
|
|
78
|
+
query(e, t) {
|
|
79
|
+
return n.post(`/${e}/query`, {
|
|
80
|
+
query: t
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
function O(r, o = "https://api.d-id.com") {
|
|
86
|
+
const n = p(r, `${o}/agents`);
|
|
87
|
+
return {
|
|
88
|
+
create(e) {
|
|
89
|
+
return n.post("/me", e);
|
|
90
|
+
},
|
|
91
|
+
getAgents(e) {
|
|
92
|
+
return n.get(`/me${e ? `?tag=${e}` : ""}`);
|
|
93
|
+
},
|
|
94
|
+
getById(e) {
|
|
95
|
+
return n.get(`/${e}`);
|
|
96
|
+
},
|
|
97
|
+
delete(e) {
|
|
98
|
+
return n.delete(`/${e}`);
|
|
99
|
+
},
|
|
100
|
+
update(e, t) {
|
|
101
|
+
return n.patch(`/${e}`, t);
|
|
102
|
+
}
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
function k(r, o) {
|
|
106
|
+
const n = p(r, o);
|
|
107
|
+
return {
|
|
108
|
+
createStream(e) {
|
|
109
|
+
return n.post("/clips/streams", {
|
|
110
|
+
driver_id: e.driver_id,
|
|
111
|
+
presenter_id: e.presenter_id,
|
|
112
|
+
compatibility_mode: e.compatibility_mode
|
|
113
|
+
});
|
|
114
|
+
},
|
|
115
|
+
startConnection(e, t, s) {
|
|
116
|
+
return n.post(`/clips/streams/${e}/sdp`, {
|
|
117
|
+
session_id: s,
|
|
118
|
+
answer: t
|
|
119
|
+
});
|
|
120
|
+
},
|
|
121
|
+
addIceCandidate(e, t, s) {
|
|
122
|
+
return n.post(`/clips/streams/${e}/ice`, {
|
|
123
|
+
session_id: s,
|
|
124
|
+
...t
|
|
125
|
+
});
|
|
126
|
+
},
|
|
127
|
+
sendStreamRequest(e, t, s) {
|
|
128
|
+
return n.post(`/clips/streams/${e}`, {
|
|
129
|
+
session_id: t,
|
|
130
|
+
...s
|
|
131
|
+
});
|
|
132
|
+
},
|
|
133
|
+
close(e, t) {
|
|
134
|
+
return n.delete(`/clips/streams/${e}`, {
|
|
135
|
+
session_id: t
|
|
136
|
+
});
|
|
137
|
+
}
|
|
138
|
+
};
|
|
139
|
+
}
|
|
140
|
+
function A(r, o) {
|
|
141
|
+
const n = p(r, o);
|
|
142
|
+
return {
|
|
143
|
+
createStream(e) {
|
|
144
|
+
return n.post("/talks/streams", {
|
|
145
|
+
source_url: e.source_url,
|
|
146
|
+
driver_url: e.driver_url,
|
|
147
|
+
face: e.face,
|
|
148
|
+
config: e.config
|
|
149
|
+
});
|
|
150
|
+
},
|
|
151
|
+
startConnection(e, t, s) {
|
|
152
|
+
return n.post(`/talks/streams/${e}/sdp`, {
|
|
153
|
+
session_id: s,
|
|
154
|
+
answer: t
|
|
155
|
+
});
|
|
156
|
+
},
|
|
157
|
+
addIceCandidate(e, t, s) {
|
|
158
|
+
return n.post(`/talks/streams/${e}/ice`, {
|
|
159
|
+
session_id: s,
|
|
160
|
+
...t
|
|
161
|
+
});
|
|
162
|
+
},
|
|
163
|
+
sendStreamRequest(e, t, s) {
|
|
164
|
+
return n.post(`/talks/streams/${e}`, {
|
|
165
|
+
session_id: t,
|
|
166
|
+
...s
|
|
167
|
+
});
|
|
168
|
+
},
|
|
169
|
+
close(e, t) {
|
|
170
|
+
return n.delete(`/talks/streams/${e}`, {
|
|
171
|
+
session_id: t
|
|
172
|
+
});
|
|
173
|
+
}
|
|
174
|
+
};
|
|
175
|
+
}
|
|
176
|
+
var l = /* @__PURE__ */ ((r) => (r.Idle = "IDLE", r.Streaming = "STREAMING", r))(l || {}), C = /* @__PURE__ */ ((r) => (r.Clip = "clip", r.Talk = "talk", r))(C || {});
|
|
177
|
+
let w = !1;
|
|
178
|
+
const c = (r, o) => w && console.log(r, o), D = (window.RTCPeerConnection || window.webkitRTCPeerConnection || window.mozRTCPeerConnection).bind(window);
|
|
179
|
+
async function R(r, {
|
|
180
|
+
debug: o = !1,
|
|
181
|
+
callbacks: n,
|
|
182
|
+
auth: e,
|
|
183
|
+
baseURL: t = "https://api.d-id.com"
|
|
184
|
+
}) {
|
|
185
|
+
w = o;
|
|
186
|
+
let s = null;
|
|
187
|
+
const {
|
|
188
|
+
startConnection: $,
|
|
189
|
+
sendStreamRequest: f,
|
|
190
|
+
close: S,
|
|
191
|
+
createStream: h,
|
|
192
|
+
addIceCandidate: _
|
|
193
|
+
} = r.videoType === C.Clip ? k(e, t) : A(e, t), {
|
|
194
|
+
id: d,
|
|
195
|
+
offer: y,
|
|
196
|
+
ice_servers: I,
|
|
197
|
+
session_id: u
|
|
198
|
+
} = await h(r), a = new D({
|
|
199
|
+
iceServers: I
|
|
200
|
+
}), m = a.createDataChannel("JanusDataChannel");
|
|
201
|
+
if (!u)
|
|
202
|
+
throw new Error("Could not create session_id");
|
|
203
|
+
a.onicecandidate = (i) => {
|
|
204
|
+
c("peerConnection.onicecandidate", i), i.candidate && i.candidate.sdpMid && i.candidate.sdpMLineIndex !== null && _(d, {
|
|
205
|
+
candidate: i.candidate.candidate,
|
|
206
|
+
sdpMid: i.candidate.sdpMid,
|
|
207
|
+
sdpMLineIndex: i.candidate.sdpMLineIndex
|
|
208
|
+
}, u);
|
|
209
|
+
}, a.oniceconnectionstatechange = () => {
|
|
210
|
+
c("peerConnection.oniceconnectionstatechange => " + a.iceConnectionState), n.onConnectionStateChange(a.iceConnectionState);
|
|
211
|
+
}, a.ontrack = (i) => {
|
|
212
|
+
c("peerConnection.ontrack", i), s = i.streams[0], n.onSrcObjectReady(s);
|
|
213
|
+
}, m.onmessage = (i) => {
|
|
214
|
+
m.readyState === "open" && n.onStreamingStateChange(i.data === "stream/done" ? l.Idle : l.Streaming);
|
|
215
|
+
}, await a.setRemoteDescription(y), c("set remote description OK");
|
|
216
|
+
const g = await a.createAnswer();
|
|
217
|
+
return c("create answer OK"), await a.setLocalDescription(g), c("set local description OK"), await $(d, g, u), c("start connection OK"), {
|
|
218
|
+
speak(i) {
|
|
219
|
+
return f(d, u, i);
|
|
220
|
+
},
|
|
221
|
+
async terminate() {
|
|
222
|
+
d && (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 S(d, u), n.onConnectionStateChange("closed"), n.onStreamingStateChange(l.Idle));
|
|
223
|
+
},
|
|
224
|
+
sessionId: u,
|
|
225
|
+
streamId: d
|
|
226
|
+
};
|
|
227
|
+
}
|
|
228
|
+
export {
|
|
229
|
+
O as createAgentsApi,
|
|
230
|
+
p as createClient,
|
|
231
|
+
K as createKnowledgeApi,
|
|
232
|
+
R as createStreamingManager
|
|
233
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
(function(c,p){typeof exports=="object"&&typeof module<"u"?p(exports):typeof define=="function"&&define.amd?define(["exports"],p):(c=typeof globalThis<"u"?globalThis:c||self,p(c.index={}))})(this,function(c){"use strict";function p(r){return r.type==="bearer"?"Bearer "+r.token:r.type==="basic"?"Basic "+btoa(`${r.username}:${r.password}`):"Client-Key "+r.clientKey}function m(r,a="https://api.d-id.com"){const n=async(e,t)=>{const i=await fetch(a+(e!=null&&e.startsWith("/")?e:`/${e}`),{...t,headers:{...t==null?void 0:t.headers,Authorization:p(r),"Content-Type":"application/json"}});if(!i.ok)throw new Error(i.statusText);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 S(r,a="https://api.d-id.com"){const n=m(r,`${a}/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 h(r,a="https://api.d-id.com"){const n=m(r,`${a}/agents`);return{create(e){return n.post("/me",e)},getAgents(e){return n.get(`/me${e?`?tag=${e}`:""}`)},getById(e){return n.get(`/${e}`)},delete(e){return n.delete(`/${e}`)},update(e,t){return n.patch(`/${e}`,t)}}}function y(r,a){const n=m(r,a);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 _(r,a){const n=m(r,a);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 g=(r=>(r.Idle="IDLE",r.Streaming="STREAMING",r))(g||{}),f=(r=>(r.Clip="clip",r.Talk="talk",r))(f||{});let C=!1;const d=(r,a)=>C&&console.log(r,a),T=(window.RTCPeerConnection||window.webkitRTCPeerConnection||window.mozRTCPeerConnection).bind(window);async function A(r,{debug:a=!1,callbacks:n,auth:e,baseURL:t="https://api.d-id.com"}){C=a;let i=null;const{startConnection:I,sendStreamRequest:k,close:K,createStream:b,addIceCandidate:D}=r.videoType===f.Clip?y(e,t):_(e,t),{id:u,offer:O,ice_servers:M,session_id:l}=await b(r),o=new T({iceServers:M}),w=o.createDataChannel("JanusDataChannel");if(!l)throw new Error("Could not create session_id");o.onicecandidate=s=>{d("peerConnection.onicecandidate",s),s.candidate&&s.candidate.sdpMid&&s.candidate.sdpMLineIndex!==null&&D(u,{candidate:s.candidate.candidate,sdpMid:s.candidate.sdpMid,sdpMLineIndex:s.candidate.sdpMLineIndex},l)},o.oniceconnectionstatechange=()=>{d("peerConnection.oniceconnectionstatechange => "+o.iceConnectionState),n.onConnectionStateChange(o.iceConnectionState)},o.ontrack=s=>{d("peerConnection.ontrack",s),i=s.streams[0],n.onSrcObjectReady(i)},w.onmessage=s=>{w.readyState==="open"&&n.onStreamingStateChange(s.data==="stream/done"?g.Idle:g.Streaming)},await o.setRemoteDescription(O),d("set remote description OK");const $=await o.createAnswer();return d("create answer OK"),await o.setLocalDescription($),d("set local description OK"),await I(u,$,l),d("start connection OK"),{speak(s){return k(u,l,s)},async terminate(){u&&(i&&(i.getTracks().forEach(s=>s.stop()),i=null),o&&(o.close(),o.oniceconnectionstatechange=null,o.onnegotiationneeded=null,o.onicecandidate=null,o.ontrack=null),await K(u,l),n.onConnectionStateChange("closed"),n.onStreamingStateChange(g.Idle))},sessionId:l,streamId:u}}c.createAgentsApi=h,c.createClient=m,c.createKnowledgeApi=S,c.createStreamingManager=A,Object.defineProperty(c,Symbol.toStringTag,{value:"Module"})});
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { Agent, AgentPayload, Auth } from '@d-id/types';
|
|
2
|
+
export declare function createAgentsApi(auth: Auth, host?: string): {
|
|
3
|
+
create(payload: AgentPayload): Promise<Agent>;
|
|
4
|
+
getAgents(tag?: string): Promise<Agent[]>;
|
|
5
|
+
getById(id: string): Promise<Agent>;
|
|
6
|
+
delete(id: string): Promise<any>;
|
|
7
|
+
update(id: string, payload: AgentPayload): Promise<Agent>;
|
|
8
|
+
};
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { Auth } from '@d-id/types';
|
|
2
|
+
export declare function createClient(auth: Auth, host?: string): {
|
|
3
|
+
get<T = any>(url: string, options?: RequestInit): Promise<T>;
|
|
4
|
+
post<T_1 = any>(url: string, body: any, options?: RequestInit): Promise<T_1>;
|
|
5
|
+
delete<T_2 = any>(url: string, body?: any, options?: RequestInit): Promise<T_2>;
|
|
6
|
+
patch<T_3 = any>(url: string, body?: any, options?: RequestInit): Promise<T_3>;
|
|
7
|
+
};
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { Auth, CreateDocumentPayload, DocumentData, KnowledgeData, KnowledgePayload, QueryResult, RecordData } from '@d-id/types';
|
|
2
|
+
export declare function createKnowledgeApi(auth: Auth, host?: string): {
|
|
3
|
+
createKnowledge(payload: KnowledgePayload): Promise<KnowledgeData>;
|
|
4
|
+
getKnowledgeBase(): Promise<KnowledgeData[]>;
|
|
5
|
+
getKnowledge(knowledgeId: string): Promise<KnowledgeData>;
|
|
6
|
+
deleteKnowledge(knowledgeId: string): Promise<any>;
|
|
7
|
+
createDocument(knowledgeId: string, payload: CreateDocumentPayload): Promise<DocumentData>;
|
|
8
|
+
deleteDocument(knowledgeId: string, documentId: string): Promise<any>;
|
|
9
|
+
getDocuments(knowledgeId: string): Promise<DocumentData[]>;
|
|
10
|
+
getDocument(knowledgeId: string, documentId: string): Promise<DocumentData>;
|
|
11
|
+
getRecords(knowledgeId: string, documentId: string): Promise<RecordData[]>;
|
|
12
|
+
query(knowledgeId: string, query: string): Promise<QueryResult>;
|
|
13
|
+
};
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { CreateStreamOptions, ManagerOptions, PayloadType } from '@d-id/types';
|
|
2
|
+
export declare function createStreamingManager<T extends CreateStreamOptions>(agent: T, { debug, callbacks, auth, baseURL }: ManagerOptions): Promise<{
|
|
3
|
+
speak(payload: PayloadType<T>): Promise<import("@d-id/types").SendStreamPayloadResponse>;
|
|
4
|
+
terminate(): Promise<void>;
|
|
5
|
+
sessionId: string;
|
|
6
|
+
streamId: string;
|
|
7
|
+
}>;
|
|
8
|
+
export type StreamingManager<T extends CreateStreamOptions> = Awaited<ReturnType<typeof createStreamingManager<T>>>;
|
package/package.json
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@d-id/client-sdk",
|
|
3
|
+
"private": false,
|
|
4
|
+
"version": "1.0.0",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"description": "d-id client sdk",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "https://github.com/de-id/streaming-client"
|
|
10
|
+
},
|
|
11
|
+
"keywords": [
|
|
12
|
+
"d-id",
|
|
13
|
+
"sdk",
|
|
14
|
+
"client-sdk"
|
|
15
|
+
],
|
|
16
|
+
"license": "MIT",
|
|
17
|
+
"author": "d-id",
|
|
18
|
+
"files": [
|
|
19
|
+
"dist/*"
|
|
20
|
+
],
|
|
21
|
+
"main": "./dist/index.umd.cjs",
|
|
22
|
+
"module": "./dist/index.js",
|
|
23
|
+
"types": "./dist/lib/index.d.ts",
|
|
24
|
+
"exports": {
|
|
25
|
+
".": {
|
|
26
|
+
"types": "./dist/lib/index.d.ts",
|
|
27
|
+
"import": "./dist/index.js",
|
|
28
|
+
"require": "./dist/index.umd.cjs"
|
|
29
|
+
}
|
|
30
|
+
},
|
|
31
|
+
"scripts": {
|
|
32
|
+
"dev": "vite",
|
|
33
|
+
"build": "tsc && vite build",
|
|
34
|
+
"preview": "vite preview"
|
|
35
|
+
},
|
|
36
|
+
"dependencies": {},
|
|
37
|
+
"devDependencies": {
|
|
38
|
+
"@d-id/types": "*",
|
|
39
|
+
"@preact/preset-vite": "^2.5.0",
|
|
40
|
+
"glob": "^10.3.10",
|
|
41
|
+
"preact": "^10.5.13",
|
|
42
|
+
"typescript": "^5.2.2",
|
|
43
|
+
"vite": "^4.4.5",
|
|
44
|
+
"vite-plugin-dts": "^3.6.0",
|
|
45
|
+
"vite-plugin-html-env": "^1.2.8"
|
|
46
|
+
}
|
|
47
|
+
}
|