@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.
- package/dist/apps/sdk/lib/api/agents.d.ts +3 -3
- package/dist/apps/sdk/lib/api/getClient.d.ts +1 -1
- package/dist/apps/sdk/lib/auth/getAuthHeader.d.ts +2 -0
- package/dist/apps/sdk/lib/connectToSocket.d.ts +15 -0
- package/dist/apps/sdk/lib/index.d.ts +2 -1
- package/dist/common/types/src/entities/agents/agent.d.ts +0 -11
- package/dist/common/types/src/entities/agents/chat.d.ts +21 -0
- package/dist/common/types/src/entities/agents/index.d.ts +1 -0
- package/dist/index.js +175 -129
- package/dist/index.umd.cjs +1 -1
- package/package.json +9 -4
|
@@ -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<
|
|
9
|
-
chat(agentId: string, payload: ChatPayload): Promise<
|
|
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
|
|
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,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 {};
|
|
@@ -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
|
+
}
|
package/dist/index.js
CHANGED
|
@@ -1,246 +1,292 @@
|
|
|
1
|
-
function
|
|
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
|
|
5
|
-
const
|
|
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
|
-
...
|
|
7
|
+
...n,
|
|
8
8
|
headers: {
|
|
9
|
-
...
|
|
10
|
-
Authorization:
|
|
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
|
|
15
|
+
let l = await s.text().catch(() => "Failed to fetch");
|
|
16
16
|
try {
|
|
17
|
-
const
|
|
18
|
-
|
|
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(
|
|
21
|
+
throw new Error(l);
|
|
22
22
|
}
|
|
23
23
|
return s.json();
|
|
24
24
|
};
|
|
25
25
|
return {
|
|
26
|
-
get(e,
|
|
27
|
-
return
|
|
28
|
-
...
|
|
26
|
+
get(e, n) {
|
|
27
|
+
return t(e, {
|
|
28
|
+
...n,
|
|
29
29
|
method: "GET"
|
|
30
30
|
});
|
|
31
31
|
},
|
|
32
|
-
post(e,
|
|
33
|
-
return
|
|
32
|
+
post(e, n, s) {
|
|
33
|
+
return t(e, {
|
|
34
34
|
...s,
|
|
35
|
-
body: JSON.stringify(
|
|
35
|
+
body: JSON.stringify(n),
|
|
36
36
|
method: "POST"
|
|
37
37
|
});
|
|
38
38
|
},
|
|
39
|
-
delete(e,
|
|
40
|
-
return
|
|
39
|
+
delete(e, n, s) {
|
|
40
|
+
return t(e, {
|
|
41
41
|
...s,
|
|
42
|
-
body: JSON.stringify(
|
|
42
|
+
body: JSON.stringify(n),
|
|
43
43
|
method: "DELETE"
|
|
44
44
|
});
|
|
45
45
|
},
|
|
46
|
-
patch(e,
|
|
47
|
-
return
|
|
46
|
+
patch(e, n, s) {
|
|
47
|
+
return t(e, {
|
|
48
48
|
...s,
|
|
49
|
-
body: JSON.stringify(
|
|
49
|
+
body: JSON.stringify(n),
|
|
50
50
|
method: "PATCH"
|
|
51
51
|
});
|
|
52
52
|
}
|
|
53
53
|
};
|
|
54
54
|
}
|
|
55
|
-
function
|
|
56
|
-
const
|
|
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
|
|
85
|
+
return t.post("/", e);
|
|
60
86
|
},
|
|
61
87
|
getKnowledgeBase() {
|
|
62
|
-
return
|
|
88
|
+
return t.get("/");
|
|
63
89
|
},
|
|
64
90
|
getKnowledge(e) {
|
|
65
|
-
return
|
|
91
|
+
return t.get(`/${e}`);
|
|
66
92
|
},
|
|
67
93
|
deleteKnowledge(e) {
|
|
68
|
-
return
|
|
94
|
+
return t.delete(`/${e}`);
|
|
69
95
|
},
|
|
70
|
-
createDocument(e,
|
|
71
|
-
return
|
|
96
|
+
createDocument(e, n) {
|
|
97
|
+
return t.post(`/${e}/documents`, n);
|
|
72
98
|
},
|
|
73
|
-
deleteDocument(e,
|
|
74
|
-
return
|
|
99
|
+
deleteDocument(e, n) {
|
|
100
|
+
return t.delete(`/${e}/documents/${n}`);
|
|
75
101
|
},
|
|
76
102
|
getDocuments(e) {
|
|
77
|
-
return
|
|
103
|
+
return t.get(`/${e}/documents`);
|
|
78
104
|
},
|
|
79
|
-
getDocument(e,
|
|
80
|
-
return
|
|
105
|
+
getDocument(e, n) {
|
|
106
|
+
return t.get(`/${e}/documents/${n}`);
|
|
81
107
|
},
|
|
82
|
-
getRecords(e,
|
|
83
|
-
return
|
|
108
|
+
getRecords(e, n) {
|
|
109
|
+
return t.get(`/${e}/documents/${n}/records`);
|
|
84
110
|
},
|
|
85
|
-
query(e,
|
|
86
|
-
return
|
|
87
|
-
query:
|
|
111
|
+
query(e, n) {
|
|
112
|
+
return t.post(`/${e}/query`, {
|
|
113
|
+
query: n
|
|
88
114
|
});
|
|
89
115
|
}
|
|
90
116
|
};
|
|
91
117
|
}
|
|
92
|
-
function
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
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
|
|
119
|
-
const
|
|
162
|
+
function K(r, o) {
|
|
163
|
+
const t = $(r, o);
|
|
120
164
|
return {
|
|
121
165
|
createStream(e) {
|
|
122
|
-
return
|
|
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,
|
|
129
|
-
return
|
|
172
|
+
startConnection(e, n, s) {
|
|
173
|
+
return t.post(`/clips/streams/${e}/sdp`, {
|
|
130
174
|
session_id: s,
|
|
131
|
-
answer:
|
|
175
|
+
answer: n
|
|
132
176
|
});
|
|
133
177
|
},
|
|
134
|
-
addIceCandidate(e,
|
|
135
|
-
return
|
|
178
|
+
addIceCandidate(e, n, s) {
|
|
179
|
+
return t.post(`/clips/streams/${e}/ice`, {
|
|
136
180
|
session_id: s,
|
|
137
|
-
...
|
|
181
|
+
...n
|
|
138
182
|
});
|
|
139
183
|
},
|
|
140
|
-
sendStreamRequest(e,
|
|
141
|
-
return
|
|
142
|
-
session_id:
|
|
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,
|
|
147
|
-
return
|
|
148
|
-
session_id:
|
|
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
|
|
154
|
-
const
|
|
197
|
+
function D(r, o) {
|
|
198
|
+
const t = $(r, o);
|
|
155
199
|
return {
|
|
156
200
|
createStream(e) {
|
|
157
|
-
return
|
|
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,
|
|
165
|
-
return
|
|
208
|
+
startConnection(e, n, s) {
|
|
209
|
+
return t.post(`/talks/streams/${e}/sdp`, {
|
|
166
210
|
session_id: s,
|
|
167
|
-
answer:
|
|
211
|
+
answer: n
|
|
168
212
|
});
|
|
169
213
|
},
|
|
170
|
-
addIceCandidate(e,
|
|
171
|
-
return
|
|
214
|
+
addIceCandidate(e, n, s) {
|
|
215
|
+
return t.post(`/talks/streams/${e}/ice`, {
|
|
172
216
|
session_id: s,
|
|
173
|
-
...
|
|
217
|
+
...n
|
|
174
218
|
});
|
|
175
219
|
},
|
|
176
|
-
sendStreamRequest(e,
|
|
177
|
-
return
|
|
178
|
-
session_id:
|
|
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,
|
|
183
|
-
return
|
|
184
|
-
session_id:
|
|
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
|
|
190
|
-
let
|
|
191
|
-
const
|
|
192
|
-
async function
|
|
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:
|
|
238
|
+
callbacks: t,
|
|
195
239
|
auth: e,
|
|
196
|
-
baseURL:
|
|
240
|
+
baseURL: n = "https://api.d-id.com"
|
|
197
241
|
}) {
|
|
198
|
-
|
|
242
|
+
S = o;
|
|
199
243
|
let s = null;
|
|
200
244
|
const {
|
|
201
|
-
startConnection:
|
|
202
|
-
sendStreamRequest:
|
|
203
|
-
close:
|
|
204
|
-
createStream:
|
|
205
|
-
addIceCandidate:
|
|
206
|
-
} = r.videoType ===
|
|
207
|
-
id:
|
|
208
|
-
offer:
|
|
209
|
-
ice_servers:
|
|
210
|
-
session_id:
|
|
211
|
-
} = await
|
|
212
|
-
iceServers:
|
|
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 (!
|
|
258
|
+
if (!m)
|
|
215
259
|
throw new Error("Could not create session_id");
|
|
216
260
|
a.onicecandidate = (i) => {
|
|
217
|
-
|
|
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
|
-
},
|
|
265
|
+
}, m);
|
|
222
266
|
}, a.oniceconnectionstatechange = () => {
|
|
223
|
-
|
|
267
|
+
p("peerConnection.oniceconnectionstatechange => " + a.iceConnectionState), t.onConnectionStateChange(a.iceConnectionState);
|
|
224
268
|
}, a.ontrack = (i) => {
|
|
225
|
-
|
|
269
|
+
p("peerConnection.ontrack", i), s = i.streams[0], t.onSrcObjectReady(s);
|
|
226
270
|
}, C.onmessage = (i) => {
|
|
227
|
-
C.readyState === "open" &&
|
|
228
|
-
}, await a.setRemoteDescription(
|
|
229
|
-
const
|
|
230
|
-
return
|
|
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
|
|
276
|
+
return d(c, m, i);
|
|
233
277
|
},
|
|
234
278
|
async terminate() {
|
|
235
|
-
|
|
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:
|
|
238
|
-
streamId:
|
|
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
|
-
|
|
244
|
-
|
|
245
|
-
|
|
289
|
+
$ as createClient,
|
|
290
|
+
R as createKnowledgeApi,
|
|
291
|
+
P as createStreamingManager
|
|
246
292
|
};
|
package/dist/index.umd.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
(function(
|
|
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.
|
|
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
|
}
|