@d-id/client-sdk 1.0.15 → 1.0.16
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 +1 -1
- package/dist/apps/sdk/lib/createAgentManager.d.ts +1 -1
- package/dist/common/types/src/StreamScript.d.ts +14 -2
- package/dist/common/types/src/entities/agents/chat.d.ts +1 -1
- package/dist/common/types/src/entities/knowledge/knowledge.d.ts +1 -0
- package/dist/common/types/src/stream/api/talk.d.ts +2 -0
- package/dist/common/types/src/stream/stream.d.ts +6 -0
- package/dist/index.js +56 -52
- package/dist/index.umd.cjs +1 -1
- package/package.json +1 -1
|
@@ -6,5 +6,5 @@ export declare function createAgentsApi(auth: Auth, host?: string): {
|
|
|
6
6
|
delete(id: string, options?: RequestInit): Promise<any>;
|
|
7
7
|
update(id: string, payload: AgentPayload, options?: RequestInit): Promise<Agent>;
|
|
8
8
|
newChat(agentId: string, options?: RequestInit): Promise<Chat>;
|
|
9
|
-
chat(agentId: string, chatId: string, payload: ChatPayload, options?: RequestInit): Promise<
|
|
9
|
+
chat(agentId: string, chatId: string, payload: ChatPayload, options?: RequestInit): Promise<ChatResponse>;
|
|
10
10
|
};
|
|
@@ -4,6 +4,6 @@ export declare function createAgentManager(agentId: string, { callbacks, ...opti
|
|
|
4
4
|
agent: Agent;
|
|
5
5
|
terminate(): Promise<void>;
|
|
6
6
|
chatId: string;
|
|
7
|
-
chat(messages: Message[]): Promise<
|
|
7
|
+
chat(messages: Message[]): Promise<import('../../../common/types/src/index').ChatResponse>;
|
|
8
8
|
}>;
|
|
9
9
|
export type AgentManager = Awaited<ReturnType<typeof createAgentManager>>;
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { Message } from './entities';
|
|
1
2
|
import { StreamTextToSpeechProviders } from './tts';
|
|
2
3
|
export type StreamScriptType = 'text' | 'audio';
|
|
3
4
|
export interface BaseStreamScript {
|
|
@@ -11,7 +12,7 @@ export interface Stream_Text_Script extends BaseStreamScript {
|
|
|
11
12
|
/**
|
|
12
13
|
* text-to-speech provider from list of supported providers. default is microsoft tts
|
|
13
14
|
*/
|
|
14
|
-
provider
|
|
15
|
+
provider: StreamTextToSpeechProviders;
|
|
15
16
|
/**
|
|
16
17
|
* The input text that will be synthesized to an audio file.
|
|
17
18
|
* Note that each provider has its own limitations on the text length.
|
|
@@ -37,4 +38,15 @@ export interface Stream_Audio_Script extends BaseStreamScript {
|
|
|
37
38
|
*/
|
|
38
39
|
audio_url: string;
|
|
39
40
|
}
|
|
40
|
-
export
|
|
41
|
+
export interface Stream_LLM_Script {
|
|
42
|
+
type: 'llm';
|
|
43
|
+
provider: StreamTextToSpeechProviders;
|
|
44
|
+
ssml?: boolean;
|
|
45
|
+
llm: {
|
|
46
|
+
messages: Message[];
|
|
47
|
+
provider: 'openai';
|
|
48
|
+
};
|
|
49
|
+
input?: string;
|
|
50
|
+
stream_audio?: boolean;
|
|
51
|
+
}
|
|
52
|
+
export type StreamScript = Stream_Text_Script | Stream_Audio_Script | Stream_LLM_Script;
|
|
@@ -7,7 +7,13 @@ export declare enum StreamingState {
|
|
|
7
7
|
Start = "START",
|
|
8
8
|
Stop = "STOP"
|
|
9
9
|
}
|
|
10
|
+
export declare enum StreamEvents {
|
|
11
|
+
ChatPartial = "chat/partial",
|
|
12
|
+
StreamDone = "stream/done",
|
|
13
|
+
StreamStarted = "stream/started"
|
|
14
|
+
}
|
|
10
15
|
export interface ManagerCallbacks {
|
|
16
|
+
onMessage?: (event: string, data: string) => void;
|
|
11
17
|
onConnectionStateChange?: (state: RTCIceConnectionState) => void;
|
|
12
18
|
onVideoStateChange?: (state: StreamingState) => void;
|
|
13
19
|
onSrcObjectReady?: (value: MediaStream) => void;
|
package/dist/index.js
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
|
-
function
|
|
1
|
+
function l(o) {
|
|
2
2
|
return o.type === "bearer" ? "Bearer " + o.token : o.type === "basic" ? "Basic " + btoa(`${o.username}:${o.password}`) : "Client-Key " + o.clientKey;
|
|
3
3
|
}
|
|
4
|
-
function h(o,
|
|
4
|
+
function h(o, d = "https://api.d-id.com") {
|
|
5
5
|
const n = async (e, t) => {
|
|
6
|
-
const r = await fetch(
|
|
6
|
+
const r = await fetch(d + (e != null && e.startsWith("/") ? e : `/${e}`), {
|
|
7
7
|
...t,
|
|
8
8
|
headers: {
|
|
9
9
|
...t == null ? void 0 : t.headers,
|
|
10
|
-
Authorization:
|
|
10
|
+
Authorization: l(o),
|
|
11
11
|
"Content-Type": "application/json"
|
|
12
12
|
}
|
|
13
13
|
});
|
|
@@ -47,8 +47,8 @@ function h(o, s = "https://api.d-id.com") {
|
|
|
47
47
|
}
|
|
48
48
|
};
|
|
49
49
|
}
|
|
50
|
-
function
|
|
51
|
-
const n = h(o, `${
|
|
50
|
+
function M(o, d = "https://api.d-id.com") {
|
|
51
|
+
const n = h(o, `${d}/agents`);
|
|
52
52
|
return {
|
|
53
53
|
create(e, t) {
|
|
54
54
|
return n.post("/", e, t);
|
|
@@ -73,8 +73,8 @@ function R(o, s = "https://api.d-id.com") {
|
|
|
73
73
|
}
|
|
74
74
|
};
|
|
75
75
|
}
|
|
76
|
-
function
|
|
77
|
-
const n = h(o, `${
|
|
76
|
+
function B(o, d = "https://api.d-id.com") {
|
|
77
|
+
const n = h(o, `${d}/knowledge`);
|
|
78
78
|
return {
|
|
79
79
|
createKnowledge(e, t) {
|
|
80
80
|
return n.post("/", e, t);
|
|
@@ -110,8 +110,8 @@ function M(o, s = "https://api.d-id.com") {
|
|
|
110
110
|
}
|
|
111
111
|
};
|
|
112
112
|
}
|
|
113
|
-
var C = /* @__PURE__ */ ((o) => (o.Start = "START", o.Stop = "STOP", o))(C || {}), m = /* @__PURE__ */ ((o) => (o.Clip = "clip", o.Talk = "talk", o))(m || {});
|
|
114
|
-
function
|
|
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
115
|
return o.presenter.type === m.Clip ? {
|
|
116
116
|
videoType: m.Clip,
|
|
117
117
|
driver_id: o.presenter.driver_id,
|
|
@@ -121,20 +121,20 @@ function I(o) {
|
|
|
121
121
|
source_url: o.presenter.source_url
|
|
122
122
|
};
|
|
123
123
|
}
|
|
124
|
-
async function
|
|
125
|
-
callbacks:
|
|
124
|
+
async function L(o, {
|
|
125
|
+
callbacks: d,
|
|
126
126
|
...n
|
|
127
127
|
}) {
|
|
128
|
-
const e = new AbortController(), t =
|
|
128
|
+
const e = new AbortController(), t = M(n.auth, n.baseURL), r = await t.getById(o), a = await t.newChat(o), {
|
|
129
129
|
terminate: w,
|
|
130
130
|
sessionId: S,
|
|
131
|
-
streamId:
|
|
132
|
-
} = await O(
|
|
131
|
+
streamId: f
|
|
132
|
+
} = await x(O(r), {
|
|
133
133
|
...n,
|
|
134
134
|
callbacks: {
|
|
135
|
-
onSrcObjectReady:
|
|
136
|
-
onVideoStateChange:
|
|
137
|
-
onConnectionStateChange:
|
|
135
|
+
onSrcObjectReady: d.onSrcObjectReady,
|
|
136
|
+
onVideoStateChange: d == null ? void 0 : d.onVideoStateChange,
|
|
137
|
+
onConnectionStateChange: d.onConnectionStateChange
|
|
138
138
|
}
|
|
139
139
|
});
|
|
140
140
|
return {
|
|
@@ -146,7 +146,7 @@ async function P(o, {
|
|
|
146
146
|
chat(c) {
|
|
147
147
|
return t.chat(o, a.id, {
|
|
148
148
|
sessionId: S,
|
|
149
|
-
streamId:
|
|
149
|
+
streamId: f,
|
|
150
150
|
messages: c
|
|
151
151
|
}, {
|
|
152
152
|
signal: e.signal
|
|
@@ -154,8 +154,8 @@ async function P(o, {
|
|
|
154
154
|
}
|
|
155
155
|
};
|
|
156
156
|
}
|
|
157
|
-
function
|
|
158
|
-
const n = h(o,
|
|
157
|
+
function P(o, d) {
|
|
158
|
+
const n = h(o, d);
|
|
159
159
|
return {
|
|
160
160
|
createStream(e) {
|
|
161
161
|
return n.post("/clips/streams", {
|
|
@@ -189,8 +189,8 @@ function v(o, s) {
|
|
|
189
189
|
}
|
|
190
190
|
};
|
|
191
191
|
}
|
|
192
|
-
function
|
|
193
|
-
const n = h(o,
|
|
192
|
+
function q(o, d) {
|
|
193
|
+
const n = h(o, d);
|
|
194
194
|
return {
|
|
195
195
|
createStream(e, t) {
|
|
196
196
|
return n.post("/talks/streams", {
|
|
@@ -225,65 +225,69 @@ function K(o, s) {
|
|
|
225
225
|
}
|
|
226
226
|
};
|
|
227
227
|
}
|
|
228
|
-
let
|
|
229
|
-
const p = (o,
|
|
230
|
-
async function
|
|
231
|
-
debug:
|
|
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
232
|
callbacks: n,
|
|
233
233
|
auth: e,
|
|
234
234
|
baseURL: t = "https://api.d-id.com"
|
|
235
235
|
}) {
|
|
236
|
-
|
|
236
|
+
I = d;
|
|
237
237
|
const {
|
|
238
238
|
startConnection: r,
|
|
239
239
|
sendStreamRequest: a,
|
|
240
240
|
close: w,
|
|
241
241
|
createStream: S,
|
|
242
|
-
addIceCandidate:
|
|
243
|
-
} = o.videoType === m.Clip ?
|
|
242
|
+
addIceCandidate: f
|
|
243
|
+
} = o.videoType === m.Clip ? P(e, t) : q(e, t), {
|
|
244
244
|
id: c,
|
|
245
|
-
offer:
|
|
246
|
-
ice_servers:
|
|
245
|
+
offer: D,
|
|
246
|
+
ice_servers: v,
|
|
247
247
|
session_id: g
|
|
248
|
-
} = await S(o),
|
|
249
|
-
iceServers:
|
|
250
|
-
}),
|
|
248
|
+
} = await S(o), s = new j({
|
|
249
|
+
iceServers: v
|
|
250
|
+
}), A = s.createDataChannel("JanusDataChannel");
|
|
251
251
|
if (!g)
|
|
252
252
|
throw new Error("Could not create session_id");
|
|
253
|
-
|
|
254
|
-
p("peerConnection.onicecandidate", i), i.candidate && i.candidate.sdpMid && i.candidate.sdpMLineIndex !== null &&
|
|
253
|
+
s.onicecandidate = (i) => {
|
|
254
|
+
p("peerConnection.onicecandidate", i), i.candidate && i.candidate.sdpMid && i.candidate.sdpMLineIndex !== null && f(c, {
|
|
255
255
|
candidate: i.candidate.candidate,
|
|
256
256
|
sdpMid: i.candidate.sdpMid,
|
|
257
257
|
sdpMLineIndex: i.candidate.sdpMLineIndex
|
|
258
258
|
}, g);
|
|
259
|
-
},
|
|
259
|
+
}, s.oniceconnectionstatechange = () => {
|
|
260
260
|
var i;
|
|
261
|
-
p("peerConnection.oniceconnectionstatechange => " +
|
|
262
|
-
},
|
|
261
|
+
p("peerConnection.oniceconnectionstatechange => " + s.iceConnectionState), (i = n.onConnectionStateChange) == null || i.call(n, s.iceConnectionState);
|
|
262
|
+
}, s.ontrack = (i) => {
|
|
263
263
|
var u;
|
|
264
264
|
p("peerConnection.ontrack", i), (u = n.onSrcObjectReady) == null || u.call(n, i.streams[0]);
|
|
265
|
-
},
|
|
266
|
-
var u;
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
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));
|
|
270
|
+
}
|
|
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"), {
|
|
271
274
|
speak(i) {
|
|
272
275
|
return a(c, g, i);
|
|
273
276
|
},
|
|
274
277
|
async terminate() {
|
|
275
278
|
var i, u;
|
|
276
|
-
c && (
|
|
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));
|
|
277
281
|
},
|
|
278
282
|
sessionId: g,
|
|
279
283
|
streamId: c
|
|
280
284
|
};
|
|
281
285
|
}
|
|
282
286
|
export {
|
|
283
|
-
|
|
284
|
-
|
|
287
|
+
L as createAgentManager,
|
|
288
|
+
M as createAgentsApi,
|
|
285
289
|
h as createClient,
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
290
|
+
B as createKnowledgeApi,
|
|
291
|
+
x as createStreamingManager,
|
|
292
|
+
O as getAgentStreamArgs
|
|
289
293
|
};
|
package/dist/index.umd.cjs
CHANGED
|
@@ -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
|
|
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"})});
|