@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.
@@ -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<string | ChatResponse>;
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<string | import('../../../common/types/src/index').ChatResponse>;
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?: StreamTextToSpeechProviders;
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 type StreamScript = Stream_Text_Script | Stream_Audio_Script;
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;
@@ -18,7 +18,7 @@ export interface IRetrivalMetadata {
18
18
  source_url: string;
19
19
  }
20
20
  export interface ChatResponse {
21
- result: string;
21
+ result?: string;
22
22
  documentIds?: string[];
23
23
  matches?: IRetrivalMetadata[];
24
24
  }
@@ -31,5 +31,6 @@ export interface KnowledgeData {
31
31
  vector_store: string;
32
32
  description: string;
33
33
  name: string;
34
+ starter_message?: string[];
34
35
  }
35
36
  export type KnowledgePayload = Omit<KnowledgeData, 'created_by' | 'type' | 'created_at' | 'modified_at' | 'id' | 'owner_id'>;
@@ -42,4 +42,6 @@ export interface SendTalkStreamPayload {
42
42
  };
43
43
  user_data?: Record<string, any>;
44
44
  name?: string;
45
+ audio_optimization?: number;
46
+ metadata: Record<string, any>;
45
47
  }
@@ -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 T(o) {
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, s = "https://api.d-id.com") {
4
+ function h(o, d = "https://api.d-id.com") {
5
5
  const n = async (e, t) => {
6
- const r = await fetch(s + (e != null && e.startsWith("/") ? e : `/${e}`), {
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: T(o),
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 R(o, s = "https://api.d-id.com") {
51
- const n = h(o, `${s}/agents`);
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 M(o, s = "https://api.d-id.com") {
77
- const n = h(o, `${s}/knowledge`);
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 I(o) {
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 P(o, {
125
- callbacks: s,
124
+ async function L(o, {
125
+ callbacks: d,
126
126
  ...n
127
127
  }) {
128
- const e = new AbortController(), t = R(n.auth, n.baseURL), r = await t.getById(o), a = await t.newChat(o), {
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(I(r), {
131
+ streamId: f
132
+ } = await x(O(r), {
133
133
  ...n,
134
134
  callbacks: {
135
- onSrcObjectReady: s.onSrcObjectReady,
136
- onVideoStateChange: s == null ? void 0 : s.onVideoStateChange,
137
- onConnectionStateChange: s.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 v(o, s) {
158
- const n = h(o, s);
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 K(o, s) {
193
- const n = h(o, s);
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 _ = !1;
229
- const p = (o, s) => _ && console.log(o, s), D = (window.RTCPeerConnection || window.webkitRTCPeerConnection || window.mozRTCPeerConnection).bind(window);
230
- async function O(o, {
231
- debug: s = !1,
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
- _ = s;
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 ? v(e, t) : K(e, t), {
242
+ addIceCandidate: f
243
+ } = o.videoType === m.Clip ? P(e, t) : q(e, t), {
244
244
  id: c,
245
- offer: l,
246
- ice_servers: A,
245
+ offer: D,
246
+ ice_servers: v,
247
247
  session_id: g
248
- } = await S(o), d = new D({
249
- iceServers: A
250
- }), f = d.createDataChannel("JanusDataChannel");
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
- d.onicecandidate = (i) => {
254
- p("peerConnection.onicecandidate", i), i.candidate && i.candidate.sdpMid && i.candidate.sdpMLineIndex !== null && $(c, {
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
- }, d.oniceconnectionstatechange = () => {
259
+ }, s.oniceconnectionstatechange = () => {
260
260
  var i;
261
- p("peerConnection.oniceconnectionstatechange => " + d.iceConnectionState), (i = n.onConnectionStateChange) == null || i.call(n, d.iceConnectionState);
262
- }, d.ontrack = (i) => {
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
- }, f.onmessage = (i) => {
266
- var u;
267
- f.readyState === "open" && ((u = n.onVideoStateChange) == null || u.call(n, i.data === "stream/done" ? C.Stop : C.Start));
268
- }, await d.setRemoteDescription(l), p("set remote description OK");
269
- const y = await d.createAnswer();
270
- return p("create answer OK"), await d.setLocalDescription(y), p("set local description OK"), await r(c, y, g), p("start connection OK"), {
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 && (d && (d.close(), d.oniceconnectionstatechange = null, d.onnegotiationneeded = null, d.onicecandidate = null, d.ontrack = null), await w(c, g), (i = n.onConnectionStateChange) == null || i.call(n, "closed"), (u = n.onVideoStateChange) == null || u.call(n, C.Stop));
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
- P as createAgentManager,
284
- R as createAgentsApi,
287
+ L as createAgentManager,
288
+ M as createAgentsApi,
285
289
  h as createClient,
286
- M as createKnowledgeApi,
287
- O as createStreamingManager,
288
- I as getAgentStreamArgs
290
+ B as createKnowledgeApi,
291
+ x as createStreamingManager,
292
+ O as getAgentStreamArgs
289
293
  };
@@ -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 f(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 y(i,d="https://api.d-id.com"){const n=f(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 I(i,d="https://api.d-id.com"){const n=f(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 w=(i=>(i.Start="START",i.Stop="STOP",i))(w||{}),h=(i=>(i.Clip="clip",i.Talk="talk",i))(h||{});function _(i){return i.presenter.type===h.Clip?{videoType:h.Clip,driver_id:i.presenter.driver_id,presenter_id:i.presenter.presenter_id}:{videoType:h.Talk,source_url:i.presenter.source_url}}async function K(i,{callbacks:d,...n}){const e=new AbortController,t=y(n.auth,n.baseURL),r=await t.getById(i),a=await t.newChat(i),{terminate:S,sessionId:$,streamId:l}=await T(_(r),{...n,callbacks:{onSrcObjectReady:d.onSrcObjectReady,onVideoStateChange:d==null?void 0:d.onVideoStateChange,onConnectionStateChange:d.onConnectionStateChange}});return{agent:r,terminate(){return e.abort(),S()},chatId:a.id,chat(u){return t.chat(i,a.id,{sessionId:$,streamId:l,messages:u},{signal:e.signal})}}}function M(i,d){const n=f(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 D(i,d){const n=f(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 A=!1;const g=(i,d)=>A&&console.log(i,d),O=(window.RTCPeerConnection||window.webkitRTCPeerConnection||window.mozRTCPeerConnection).bind(window);async function T(i,{debug:d=!1,callbacks:n,auth:e,baseURL:t="https://api.d-id.com"}){A=d;const{startConnection:r,sendStreamRequest:a,close:S,createStream:$,addIceCandidate:l}=i.videoType===h.Clip?M(e,t):D(e,t),{id:u,offer:P,ice_servers:j,session_id:m}=await $(i),s=new O({iceServers:j}),R=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&&l(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])},R.onmessage=o=>{var p;R.readyState==="open"&&((p=n.onVideoStateChange)==null||p.call(n,o.data==="stream/done"?w.Stop:w.Start))},await s.setRemoteDescription(P),g("set remote description OK");const v=await s.createAnswer();return g("create answer OK"),await s.setLocalDescription(v),g("set local description OK"),await r(u,v,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 S(u,m),(o=n.onConnectionStateChange)==null||o.call(n,"closed"),(p=n.onVideoStateChange)==null||p.call(n,w.Stop))},sessionId:m,streamId:u}}c.createAgentManager=K,c.createAgentsApi=y,c.createClient=f,c.createKnowledgeApi=I,c.createStreamingManager=T,c.getAgentStreamArgs=_,Object.defineProperty(c,Symbol.toStringTag,{value:"Module"})});
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"})});
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.15",
4
+ "version": "1.0.16",
5
5
  "type": "module",
6
6
  "description": "d-id client sdk",
7
7
  "repository": {