@d-id/client-sdk 1.0.15 → 1.0.17

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
  };
@@ -0,0 +1,9 @@
1
+ import { Auth, RatingEntity } from '../../../../common/types/src/index';
2
+ type RatingPayload = Omit<RatingEntity, 'owner_id' | 'id' | 'created_at' | 'modified_at' | 'created_by' | 'external_id'>;
3
+ export declare function createRatingssApi(auth: Auth, host?: string): {
4
+ create(payload: RatingPayload, options?: RequestInit): Promise<RatingEntity>;
5
+ getByKnowledge(knowledgeId?: string, options?: RequestInit): Promise<RatingEntity[]>;
6
+ update(id: string, payload: Partial<RatingPayload>, options?: RequestInit): Promise<RatingEntity>;
7
+ delete(id: string, options?: RequestInit): Promise<RatingEntity>;
8
+ };
9
+ export {};
@@ -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,5 +1,6 @@
1
1
  export * from './api/agents';
2
2
  export * from './api/getClient';
3
3
  export * from './api/knowledge';
4
+ export * from './api/ratings';
4
5
  export * from './createAgentManager';
5
6
  export * from './createStreamingManager';
@@ -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;
@@ -1,8 +1,26 @@
1
+ export declare enum RateState {
2
+ Unrated = "Unrated",
3
+ Positive = "Positive",
4
+ Negative = "Negative"
5
+ }
6
+ export interface RatingEntity {
7
+ id: string;
8
+ owner_id: string;
9
+ agent_id: string;
10
+ matches: [string, string][];
11
+ knowledge_id: string;
12
+ external_id: string;
13
+ created_by: string;
14
+ chat_id: string;
15
+ score: 1 | -1;
16
+ created_at: string;
17
+ modified_at: string;
18
+ }
1
19
  export interface Message {
2
20
  role: 'system' | 'assistant' | 'user' | 'function' | 'tool';
3
21
  content: string;
4
- name?: string;
5
22
  created_at: string;
23
+ matches?: ChatResponse['matches'];
6
24
  }
7
25
  export interface ChatPayload {
8
26
  messages: Message[];
@@ -18,7 +36,7 @@ export interface IRetrivalMetadata {
18
36
  source_url: string;
19
37
  }
20
38
  export interface ChatResponse {
21
- result: string;
39
+ result?: string;
22
40
  documentIds?: string[];
23
41
  matches?: IRetrivalMetadata[];
24
42
  }
@@ -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,14 @@ export declare enum StreamingState {
7
7
  Start = "START",
8
8
  Stop = "STOP"
9
9
  }
10
+ export declare enum StreamEvents {
11
+ ChatAnswer = "chat/answer",
12
+ ChatPartial = "chat/partial",
13
+ StreamDone = "stream/done",
14
+ StreamStarted = "stream/started"
15
+ }
10
16
  export interface ManagerCallbacks {
17
+ onMessage?: (event: string, data: string) => void;
11
18
  onConnectionStateChange?: (state: RTCIceConnectionState) => void;
12
19
  onVideoStateChange?: (state: StreamingState) => void;
13
20
  onSrcObjectReady?: (value: MediaStream) => void;
package/dist/index.js CHANGED
@@ -1,13 +1,13 @@
1
- function T(o) {
1
+ function v(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 C(o, i = "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(i + (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: v(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, i = "https://api.d-id.com") {
51
+ const n = C(o, `${i}/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 x(o, i = "https://api.d-id.com") {
77
+ const n = C(o, `${i}/knowledge`);
78
78
  return {
79
79
  createKnowledge(e, t) {
80
80
  return n.post("/", e, t);
@@ -110,8 +110,25 @@ 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
+ function L(o, i = "https://api.d-id.com") {
114
+ const n = C(o, `${i}/chats/ratings`);
115
+ return {
116
+ create(e, t) {
117
+ return n.post("/", e, t);
118
+ },
119
+ getByKnowledge(e, t) {
120
+ return n.get(`/${e}`, t).then((r) => r ?? []);
121
+ },
122
+ update(e, t, r) {
123
+ return n.patch(`/${e}`, t, r);
124
+ },
125
+ delete(e, t) {
126
+ return n.delete(`/${e}`, t);
127
+ }
128
+ };
129
+ }
130
+ var h = /* @__PURE__ */ ((o) => (o.Start = "START", o.Stop = "STOP", o))(h || {}), _ = /* @__PURE__ */ ((o) => (o.ChatAnswer = "chat/answer", o.ChatPartial = "chat/partial", o.StreamDone = "stream/done", o.StreamStarted = "stream/started", o))(_ || {}), m = /* @__PURE__ */ ((o) => (o.Clip = "clip", o.Talk = "talk", o))(m || {});
131
+ function O(o) {
115
132
  return o.presenter.type === m.Clip ? {
116
133
  videoType: m.Clip,
117
134
  driver_id: o.presenter.driver_id,
@@ -121,20 +138,20 @@ function I(o) {
121
138
  source_url: o.presenter.source_url
122
139
  };
123
140
  }
124
- async function P(o, {
125
- callbacks: s,
141
+ async function J(o, {
142
+ callbacks: i,
126
143
  ...n
127
144
  }) {
128
- const e = new AbortController(), t = R(n.auth, n.baseURL), r = await t.getById(o), a = await t.newChat(o), {
145
+ const e = new AbortController(), t = M(n.auth, n.baseURL), r = await t.getById(o), a = await t.newChat(o), {
129
146
  terminate: w,
130
147
  sessionId: S,
131
148
  streamId: $
132
- } = await O(I(r), {
149
+ } = await j(O(r), {
133
150
  ...n,
134
151
  callbacks: {
135
- onSrcObjectReady: s.onSrcObjectReady,
136
- onVideoStateChange: s == null ? void 0 : s.onVideoStateChange,
137
- onConnectionStateChange: s.onConnectionStateChange
152
+ onSrcObjectReady: i.onSrcObjectReady,
153
+ onVideoStateChange: i == null ? void 0 : i.onVideoStateChange,
154
+ onConnectionStateChange: i.onConnectionStateChange
138
155
  }
139
156
  });
140
157
  return {
@@ -154,8 +171,8 @@ async function P(o, {
154
171
  }
155
172
  };
156
173
  }
157
- function v(o, s) {
158
- const n = h(o, s);
174
+ function P(o, i) {
175
+ const n = C(o, i);
159
176
  return {
160
177
  createStream(e) {
161
178
  return n.post("/clips/streams", {
@@ -189,8 +206,8 @@ function v(o, s) {
189
206
  }
190
207
  };
191
208
  }
192
- function K(o, s) {
193
- const n = h(o, s);
209
+ function q(o, i) {
210
+ const n = C(o, i);
194
211
  return {
195
212
  createStream(e, t) {
196
213
  return n.post("/talks/streams", {
@@ -225,65 +242,70 @@ function K(o, s) {
225
242
  }
226
243
  };
227
244
  }
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,
245
+ let l = !1;
246
+ const p = (o, i) => l && console.log(o, i), B = (window.RTCPeerConnection || window.webkitRTCPeerConnection || window.mozRTCPeerConnection).bind(window);
247
+ async function j(o, {
248
+ debug: i = !1,
232
249
  callbacks: n,
233
250
  auth: e,
234
251
  baseURL: t = "https://api.d-id.com"
235
252
  }) {
236
- _ = s;
253
+ l = i;
237
254
  const {
238
255
  startConnection: r,
239
256
  sendStreamRequest: a,
240
257
  close: w,
241
258
  createStream: S,
242
259
  addIceCandidate: $
243
- } = o.videoType === m.Clip ? v(e, t) : K(e, t), {
260
+ } = o.videoType === m.Clip ? P(e, t) : q(e, t), {
244
261
  id: c,
245
- offer: l,
246
- ice_servers: A,
262
+ offer: I,
263
+ ice_servers: D,
247
264
  session_id: g
248
- } = await S(o), d = new D({
249
- iceServers: A
250
- }), f = d.createDataChannel("JanusDataChannel");
265
+ } = await S(o), s = new B({
266
+ iceServers: D
267
+ }), A = s.createDataChannel("JanusDataChannel");
251
268
  if (!g)
252
269
  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, {
255
- candidate: i.candidate.candidate,
256
- sdpMid: i.candidate.sdpMid,
257
- sdpMLineIndex: i.candidate.sdpMLineIndex
270
+ s.onicecandidate = (d) => {
271
+ p("peerConnection.onicecandidate", d), d.candidate && d.candidate.sdpMid && d.candidate.sdpMLineIndex !== null && $(c, {
272
+ candidate: d.candidate.candidate,
273
+ sdpMid: d.candidate.sdpMid,
274
+ sdpMLineIndex: d.candidate.sdpMLineIndex
258
275
  }, g);
259
- }, d.oniceconnectionstatechange = () => {
260
- var i;
261
- p("peerConnection.oniceconnectionstatechange => " + d.iceConnectionState), (i = n.onConnectionStateChange) == null || i.call(n, d.iceConnectionState);
262
- }, d.ontrack = (i) => {
263
- var u;
264
- p("peerConnection.ontrack", i), (u = n.onSrcObjectReady) == null || u.call(n, i.streams[0]);
265
- }, f.onmessage = (i) => {
276
+ }, s.oniceconnectionstatechange = () => {
277
+ var d;
278
+ p("peerConnection.oniceconnectionstatechange => " + s.iceConnectionState), (d = n.onConnectionStateChange) == null || d.call(n, s.iceConnectionState);
279
+ }, s.ontrack = (d) => {
266
280
  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"), {
271
- speak(i) {
272
- return a(c, g, i);
281
+ p("peerConnection.ontrack", d), (u = n.onSrcObjectReady) == null || u.call(n, d.streams[0]);
282
+ }, A.onmessage = (d) => {
283
+ var u, f, R;
284
+ if (A.readyState === "open") {
285
+ const [y, K] = d.data.split(":");
286
+ y === _.StreamDone ? (u = n.onVideoStateChange) == null || u.call(n, h.Stop) : y === _.StreamStarted ? (f = n.onVideoStateChange) == null || f.call(n, h.Start) : (R = n.onMessage) == null || R.call(n, y, decodeURIComponent(K));
287
+ }
288
+ }, await s.setRemoteDescription(I), p("set remote description OK");
289
+ const T = await s.createAnswer();
290
+ return p("create answer OK"), await s.setLocalDescription(T), p("set local description OK"), await r(c, T, g), p("start connection OK"), {
291
+ speak(d) {
292
+ return a(c, g, d);
273
293
  },
274
294
  async terminate() {
275
- 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));
295
+ var d, u;
296
+ c && (s && (s.close(), s.oniceconnectionstatechange = null, s.onnegotiationneeded = null, s.onicecandidate = null, s.ontrack = null), await w(c, g).catch((f) => {
297
+ }), (d = n.onConnectionStateChange) == null || d.call(n, "closed"), (u = n.onVideoStateChange) == null || u.call(n, h.Stop));
277
298
  },
278
299
  sessionId: g,
279
300
  streamId: c
280
301
  };
281
302
  }
282
303
  export {
283
- P as createAgentManager,
284
- R as createAgentsApi,
285
- h as createClient,
286
- M as createKnowledgeApi,
287
- O as createStreamingManager,
288
- I as getAgentStreamArgs
304
+ J as createAgentManager,
305
+ M as createAgentsApi,
306
+ C as createClient,
307
+ x as createKnowledgeApi,
308
+ L as createRatingssApi,
309
+ j as createStreamingManager,
310
+ O as getAgentStreamArgs
289
311
  };
@@ -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,h){typeof exports=="object"&&typeof module<"u"?h(exports):typeof define=="function"&&define.amd?define(["exports"],h):(c=typeof globalThis<"u"?globalThis:c||self,h(c.index={}))})(this,function(c){"use strict";function h(i){return i.type==="bearer"?"Bearer "+i.token:i.type==="basic"?"Basic "+btoa(`${i.username}:${i.password}`):"Client-Key "+i.clientKey}function m(i,o="https://api.d-id.com"){const n=async(e,t)=>{const r=await fetch(o+(e!=null&&e.startsWith("/")?e:`/${e}`),{...t,headers:{...t==null?void 0:t.headers,Authorization:h(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 T(i,o="https://api.d-id.com"){const n=m(i,`${o}/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,o="https://api.d-id.com"){const n=m(i,`${o}/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)}}}function P(i,o="https://api.d-id.com"){const n=m(i,`${o}/chats/ratings`);return{create(e,t){return n.post("/",e,t)},getByKnowledge(e,t){return n.get(`/${e}`,t).then(r=>r??[])},update(e,t,r){return n.patch(`/${e}`,t,r)},delete(e,t){return n.delete(`/${e}`,t)}}}var w=(i=>(i.Start="START",i.Stop="STOP",i))(w||{}),S=(i=>(i.ChatAnswer="chat/answer",i.ChatPartial="chat/partial",i.StreamDone="stream/done",i.StreamStarted="stream/started",i))(S||{}),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 j(i,{callbacks:o,...n}){const e=new AbortController,t=T(n.auth,n.baseURL),r=await t.getById(i),a=await t.newChat(i),{terminate:$,sessionId:y,streamId:_}=await K(R(r),{...n,callbacks:{onSrcObjectReady:o.onSrcObjectReady,onVideoStateChange:o==null?void 0:o.onVideoStateChange,onConnectionStateChange:o.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 q(i,o){const n=m(i,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,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 B(i,o){const n=m(i,o);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,o)=>I&&console.log(i,o),L=(window.RTCPeerConnection||window.webkitRTCPeerConnection||window.mozRTCPeerConnection).bind(window);async function K(i,{debug:o=!1,callbacks:n,auth:e,baseURL:t="https://api.d-id.com"}){I=o;const{startConnection:r,sendStreamRequest:a,close:$,createStream:y,addIceCandidate:_}=i.videoType===f.Clip?q(e,t):B(e,t),{id:u,offer:J,ice_servers:V,session_id:C}=await y(i),s=new L({iceServers:V}),D=s.createDataChannel("JanusDataChannel");if(!C)throw new Error("Could not create session_id");s.onicecandidate=d=>{g("peerConnection.onicecandidate",d),d.candidate&&d.candidate.sdpMid&&d.candidate.sdpMLineIndex!==null&&_(u,{candidate:d.candidate.candidate,sdpMid:d.candidate.sdpMid,sdpMLineIndex:d.candidate.sdpMLineIndex},C)},s.oniceconnectionstatechange=()=>{var d;g("peerConnection.oniceconnectionstatechange => "+s.iceConnectionState),(d=n.onConnectionStateChange)==null||d.call(n,s.iceConnectionState)},s.ontrack=d=>{var p;g("peerConnection.ontrack",d),(p=n.onSrcObjectReady)==null||p.call(n,d.streams[0])},D.onmessage=d=>{var p,A,v;if(D.readyState==="open"){const[l,E]=d.data.split(":");l===S.StreamDone?(p=n.onVideoStateChange)==null||p.call(n,w.Stop):l===S.StreamStarted?(A=n.onVideoStateChange)==null||A.call(n,w.Start):(v=n.onMessage)==null||v.call(n,l,decodeURIComponent(E))}},await s.setRemoteDescription(J),g("set remote description OK");const M=await s.createAnswer();return g("create answer OK"),await s.setLocalDescription(M),g("set local description OK"),await r(u,M,C),g("start connection OK"),{speak(d){return a(u,C,d)},async terminate(){var d,p;u&&(s&&(s.close(),s.oniceconnectionstatechange=null,s.onnegotiationneeded=null,s.onicecandidate=null,s.ontrack=null),await $(u,C).catch(A=>{}),(d=n.onConnectionStateChange)==null||d.call(n,"closed"),(p=n.onVideoStateChange)==null||p.call(n,w.Stop))},sessionId:C,streamId:u}}c.createAgentManager=j,c.createAgentsApi=T,c.createClient=m,c.createKnowledgeApi=O,c.createRatingssApi=P,c.createStreamingManager=K,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.17",
5
5
  "type": "module",
6
6
  "description": "d-id client sdk",
7
7
  "repository": {