@d-id/client-sdk 1.0.16 → 1.0.18-beta.1
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/README.md +2 -0
- package/dist/index.js +297 -191
- package/dist/index.umd.cjs +1 -1
- package/dist/src/index.d.ts +3 -0
- package/dist/{apps/sdk → src}/lib/api/agents.d.ts +1 -1
- package/dist/{apps/sdk → src}/lib/api/clipStream.d.ts +1 -1
- package/dist/{apps/sdk → src}/lib/api/getClient.d.ts +1 -1
- package/dist/{apps/sdk → src}/lib/api/knowledge.d.ts +1 -1
- package/dist/src/lib/api/ratings.d.ts +7 -0
- package/dist/{apps/sdk → src}/lib/api/talkStream.d.ts +1 -1
- package/dist/{apps/sdk → src}/lib/auth/getAuthHeader.d.ts +1 -1
- package/dist/src/lib/connectToSocket.d.ts +21 -0
- package/dist/src/lib/constants.d.ts +3 -0
- package/dist/src/lib/createAgentManager.d.ts +15 -0
- package/dist/src/lib/createStreamingManager.d.ts +27 -0
- package/dist/{common/types/src → src/types}/StreamScript.d.ts +1 -0
- package/dist/{common/types/src → src/types}/auth.d.ts +2 -0
- package/dist/{common/types/src → src/types}/entities/agents/chat.d.ts +20 -1
- package/dist/src/types/entities/agents/manager.d.ts +110 -0
- package/dist/{common/types/src → src/types}/stream/api/clip.d.ts +1 -1
- package/dist/{common/types/src → src/types}/stream/api/talk.d.ts +1 -1
- package/dist/{common/types/src → src/types}/stream/stream.d.ts +3 -1
- package/package.json +8 -12
- package/dist/apps/sdk/lib/createAgentManager.d.ts +0 -9
- package/dist/apps/sdk/lib/createStreamingManager.d.ts +0 -8
- package/dist/apps/sdk/lib/index.d.ts +0 -5
- package/dist/common/types/src/entities/agents/manager.d.ts +0 -21
- /package/dist/{common/types/src → src/types}/entities/agents/agent.d.ts +0 -0
- /package/dist/{common/types/src → src/types}/entities/agents/index.d.ts +0 -0
- /package/dist/{common/types/src → src/types}/entities/agents/knowledge.d.ts +0 -0
- /package/dist/{common/types/src → src/types}/entities/agents/llm.d.ts +0 -0
- /package/dist/{common/types/src → src/types}/entities/agents/presenter.d.ts +0 -0
- /package/dist/{common/types/src → src/types}/entities/index.d.ts +0 -0
- /package/dist/{common/types/src → src/types}/entities/knowledge/document.d.ts +0 -0
- /package/dist/{common/types/src → src/types}/entities/knowledge/index.d.ts +0 -0
- /package/dist/{common/types/src → src/types}/entities/knowledge/knowledge.d.ts +0 -0
- /package/dist/{common/types/src → src/types}/entities/knowledge/record.d.ts +0 -0
- /package/dist/{common/types/src → src/types}/entities/knowledge/retrival.d.ts +0 -0
- /package/dist/{common/types/src → src/types}/entities/video.d.ts +0 -0
- /package/dist/{common/types/src → src/types}/index.d.ts +0 -0
- /package/dist/{common/types/src → src/types}/stream/api/index.d.ts +0 -0
- /package/dist/{common/types/src → src/types}/stream/index.d.ts +0 -0
- /package/dist/{common/types/src → src/types}/stream/rtc.d.ts +0 -0
- /package/dist/{common/types/src → src/types}/tts.d.ts +0 -0
package/README.md
ADDED
package/dist/index.js
CHANGED
|
@@ -1,293 +1,399 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
var K = /* @__PURE__ */ ((e) => (e.Amazon = "amazon", e.Microsoft = "microsoft", e.Afflorithmics = "afflorithmics", e.Elevenlabs = "elevenlabs", e))(K || {}), T = /* @__PURE__ */ ((e) => (e.Public = "public", e.Premium = "premium", e.Private = "private", e))(T || {}), S = /* @__PURE__ */ ((e) => (e.Start = "START", e.Stop = "STOP", e))(S || {}), A = /* @__PURE__ */ ((e) => (e.ChatAnswer = "chat/answer", e.ChatPartial = "chat/partial", e.StreamDone = "stream/done", e.StreamStarted = "stream/started", e))(A || {}), L = /* @__PURE__ */ ((e) => (e.Unrated = "Unrated", e.Positive = "Positive", e.Negative = "Negative", e))(L || {}), N = /* @__PURE__ */ ((e) => (e.KnowledgeProcessing = "knowledge/processing", e.KnowledgeIndexing = "knowledge/indexing", e.KnowledgeFailed = "knowledge/error", e.KnowledgeDone = "knowledge/done", e))(N || {}), U = /* @__PURE__ */ ((e) => (e.Knowledge = "knowledge", e.Document = "document", e.Record = "record", e))(U || {}), z = /* @__PURE__ */ ((e) => (e.Pdf = "pdf", e.Text = "text", e.Html = "html", e.Word = "word", e.Json = "json", e.Markdown = "markdown", e.Csv = "csv", e.Excel = "excel", e.Powerpoint = "powerpoint", e.Archive = "archive", e.Image = "image", e.Audio = "audio", e.Video = "video", e))(z || {}), k = /* @__PURE__ */ ((e) => (e.Clip = "clip", e.Talk = "talk", e))(k || {});
|
|
2
|
+
function b(e) {
|
|
3
|
+
if (e.type === "bearer")
|
|
4
|
+
return `Bearer ${e.token}`;
|
|
5
|
+
if (e.type === "basic")
|
|
6
|
+
return `Basic ${btoa(`${e.username}:${e.password}`)}`;
|
|
7
|
+
if (e.type === "key")
|
|
8
|
+
return `Client-Key ${e.clientKey}`;
|
|
9
|
+
throw new Error(`Unknown auth type: ${e}`);
|
|
3
10
|
}
|
|
4
|
-
function
|
|
5
|
-
const
|
|
6
|
-
const r = await fetch(
|
|
7
|
-
...
|
|
11
|
+
function v(e, s = "https://api.d-id.com") {
|
|
12
|
+
const a = async (t, n) => {
|
|
13
|
+
const r = await fetch(s + (t != null && t.startsWith("/") ? t : `/${t}`), {
|
|
14
|
+
...n,
|
|
8
15
|
headers: {
|
|
9
|
-
...
|
|
10
|
-
Authorization:
|
|
16
|
+
...n == null ? void 0 : n.headers,
|
|
17
|
+
Authorization: b(e),
|
|
11
18
|
"Content-Type": "application/json"
|
|
12
19
|
}
|
|
13
20
|
});
|
|
14
21
|
if (!r.ok) {
|
|
15
|
-
let
|
|
16
|
-
throw new Error(
|
|
22
|
+
let o = await r.text().catch(() => "Failed to fetch");
|
|
23
|
+
throw new Error(o);
|
|
17
24
|
}
|
|
18
25
|
return r.json();
|
|
19
26
|
};
|
|
20
27
|
return {
|
|
21
|
-
get(
|
|
22
|
-
return
|
|
23
|
-
...
|
|
28
|
+
get(t, n) {
|
|
29
|
+
return a(t, {
|
|
30
|
+
...n,
|
|
24
31
|
method: "GET"
|
|
25
32
|
});
|
|
26
33
|
},
|
|
27
|
-
post(
|
|
28
|
-
return
|
|
34
|
+
post(t, n, r) {
|
|
35
|
+
return a(t, {
|
|
29
36
|
...r,
|
|
30
|
-
body: JSON.stringify(
|
|
37
|
+
body: JSON.stringify(n),
|
|
31
38
|
method: "POST"
|
|
32
39
|
});
|
|
33
40
|
},
|
|
34
|
-
delete(
|
|
35
|
-
return
|
|
41
|
+
delete(t, n, r) {
|
|
42
|
+
return a(t, {
|
|
36
43
|
...r,
|
|
37
|
-
body: JSON.stringify(
|
|
44
|
+
body: JSON.stringify(n),
|
|
38
45
|
method: "DELETE"
|
|
39
46
|
});
|
|
40
47
|
},
|
|
41
|
-
patch(
|
|
42
|
-
return
|
|
48
|
+
patch(t, n, r) {
|
|
49
|
+
return a(t, {
|
|
43
50
|
...r,
|
|
44
|
-
body: JSON.stringify(
|
|
51
|
+
body: JSON.stringify(n),
|
|
45
52
|
method: "PATCH"
|
|
46
53
|
});
|
|
47
54
|
}
|
|
48
55
|
};
|
|
49
56
|
}
|
|
50
|
-
function
|
|
51
|
-
const
|
|
57
|
+
function B(e, s = "https://api.d-id.com") {
|
|
58
|
+
const a = v(e, `${s}/agents`);
|
|
52
59
|
return {
|
|
53
|
-
create(
|
|
54
|
-
return
|
|
60
|
+
create(t, n) {
|
|
61
|
+
return a.post("/", t, n);
|
|
55
62
|
},
|
|
56
|
-
getAgents(
|
|
57
|
-
return
|
|
63
|
+
getAgents(t, n) {
|
|
64
|
+
return a.get(`/${t ? `?tag=${t}` : ""}`, n).then((r) => r ?? []);
|
|
58
65
|
},
|
|
59
|
-
getById(
|
|
60
|
-
return
|
|
66
|
+
getById(t, n) {
|
|
67
|
+
return a.get(`/${t}`, n);
|
|
61
68
|
},
|
|
62
|
-
delete(
|
|
63
|
-
return
|
|
69
|
+
delete(t, n) {
|
|
70
|
+
return a.delete(`/${t}`, void 0, n);
|
|
64
71
|
},
|
|
65
|
-
update(
|
|
66
|
-
return
|
|
72
|
+
update(t, n, r) {
|
|
73
|
+
return a.patch(`/${t}`, n, r);
|
|
67
74
|
},
|
|
68
|
-
newChat(
|
|
69
|
-
return
|
|
75
|
+
newChat(t, n) {
|
|
76
|
+
return a.post(`/${t}/chat`, void 0, n);
|
|
70
77
|
},
|
|
71
|
-
chat(
|
|
72
|
-
return
|
|
78
|
+
chat(t, n, r, o) {
|
|
79
|
+
return a.post(`/${t}/chat/${n}`, r, o);
|
|
73
80
|
}
|
|
74
81
|
};
|
|
75
82
|
}
|
|
76
|
-
function
|
|
77
|
-
const
|
|
83
|
+
function J(e, s = "https://api.d-id.com") {
|
|
84
|
+
const a = v(e, `${s}/chats/ratings`);
|
|
78
85
|
return {
|
|
79
|
-
|
|
80
|
-
return
|
|
86
|
+
create(t, n) {
|
|
87
|
+
return a.post("/", t, n);
|
|
81
88
|
},
|
|
82
|
-
|
|
83
|
-
return
|
|
89
|
+
getByKnowledge(t, n) {
|
|
90
|
+
return a.get(`/${t}`, n).then((r) => r ?? []);
|
|
84
91
|
},
|
|
85
|
-
|
|
86
|
-
return
|
|
92
|
+
update(t, n, r) {
|
|
93
|
+
return a.patch(`/${t}`, n, r);
|
|
87
94
|
},
|
|
88
|
-
|
|
89
|
-
return
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
95
|
+
delete(t, n) {
|
|
96
|
+
return a.delete(`/${t}`, n);
|
|
97
|
+
}
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
const q = "wss://notifications-dev.d-id.com", H = (e) => new Promise((s) => setTimeout(s, e));
|
|
101
|
+
function W(e) {
|
|
102
|
+
return new Promise((s, a) => {
|
|
103
|
+
const {
|
|
104
|
+
callbacks: t,
|
|
105
|
+
host: n,
|
|
106
|
+
auth: r
|
|
107
|
+
} = e, {
|
|
108
|
+
onMessage: o = null,
|
|
109
|
+
onOpen: g = null,
|
|
110
|
+
onClose: C = null,
|
|
111
|
+
onError: l = null
|
|
112
|
+
} = t || {}, p = new WebSocket(`${n}?authorization=${b(r)}`);
|
|
113
|
+
p.onmessage = o, p.onclose = C, p.onerror = (u) => {
|
|
114
|
+
console.log(u), l == null || l(u), a(u);
|
|
115
|
+
}, p.onopen = (u) => {
|
|
116
|
+
g == null || g(u), s(p);
|
|
117
|
+
};
|
|
118
|
+
});
|
|
119
|
+
}
|
|
120
|
+
async function F(e) {
|
|
121
|
+
const {
|
|
122
|
+
retries: s = 1
|
|
123
|
+
} = e;
|
|
124
|
+
let a = null;
|
|
125
|
+
for (let t = 0; (a == null ? void 0 : a.readyState) !== WebSocket.OPEN; t++)
|
|
126
|
+
try {
|
|
127
|
+
a = await W(e);
|
|
128
|
+
} catch (n) {
|
|
129
|
+
if (t === s)
|
|
130
|
+
throw n;
|
|
131
|
+
await H(t * 500);
|
|
132
|
+
}
|
|
133
|
+
return a;
|
|
134
|
+
}
|
|
135
|
+
async function V(e, s = q) {
|
|
136
|
+
let a, t = [], n = {
|
|
137
|
+
terminate: () => {
|
|
138
|
+
var o;
|
|
139
|
+
return (o = n.socket) == null ? void 0 : o.close();
|
|
105
140
|
},
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
141
|
+
connect: async () => (a = await F({
|
|
142
|
+
auth: e,
|
|
143
|
+
host: s,
|
|
144
|
+
callbacks: {
|
|
145
|
+
onMessage: r
|
|
146
|
+
}
|
|
147
|
+
}), n.socket = a, a),
|
|
148
|
+
subscribeToEvents: (o) => {
|
|
149
|
+
n.socket ? t.push(o) : console.warn("Socket is not connected. Please call connect() first.");
|
|
110
150
|
}
|
|
111
151
|
};
|
|
152
|
+
function r(o) {
|
|
153
|
+
console.log("event", o), t.forEach((g) => g(o));
|
|
154
|
+
}
|
|
155
|
+
return n;
|
|
112
156
|
}
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
157
|
+
const G = {
|
|
158
|
+
baseURL: "https://api.d-id.com"
|
|
159
|
+
};
|
|
160
|
+
function M(e) {
|
|
161
|
+
return e.presenter.type === k.Clip ? {
|
|
162
|
+
videoType: k.Clip,
|
|
163
|
+
driver_id: e.presenter.driver_id,
|
|
164
|
+
presenter_id: e.presenter.presenter_id
|
|
119
165
|
} : {
|
|
120
|
-
videoType:
|
|
121
|
-
source_url:
|
|
166
|
+
videoType: k.Talk,
|
|
167
|
+
source_url: e.presenter.source_url
|
|
122
168
|
};
|
|
123
169
|
}
|
|
124
|
-
async function
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
terminate: w,
|
|
130
|
-
sessionId: S,
|
|
131
|
-
streamId: f
|
|
132
|
-
} = await x(O(r), {
|
|
133
|
-
...n,
|
|
134
|
-
callbacks: {
|
|
135
|
-
onSrcObjectReady: d.onSrcObjectReady,
|
|
136
|
-
onVideoStateChange: d == null ? void 0 : d.onVideoStateChange,
|
|
137
|
-
onConnectionStateChange: d.onConnectionStateChange
|
|
138
|
-
}
|
|
170
|
+
async function D(e, s) {
|
|
171
|
+
const a = s.baseURL ? s.baseURL : G.baseURL, t = new AbortController(), n = B(s.auth, a), r = await n.getById(e), o = await n.newChat(e), g = await J(s.auth, a), C = Q(s);
|
|
172
|
+
let l = await R(M(r), {
|
|
173
|
+
...s,
|
|
174
|
+
callbacks: C
|
|
139
175
|
});
|
|
140
|
-
|
|
176
|
+
const p = await V(s.auth);
|
|
177
|
+
return await p.connect(), {
|
|
141
178
|
agent: r,
|
|
179
|
+
async reconnectToChat() {
|
|
180
|
+
l = await R(M(r), {
|
|
181
|
+
...s,
|
|
182
|
+
callbacks: C
|
|
183
|
+
}), l.sessionId;
|
|
184
|
+
},
|
|
142
185
|
terminate() {
|
|
143
|
-
return
|
|
186
|
+
return t.abort(), p.terminate(), l.terminate();
|
|
144
187
|
},
|
|
145
|
-
chatId:
|
|
188
|
+
chatId: o.id,
|
|
146
189
|
chat(c) {
|
|
147
|
-
return
|
|
148
|
-
sessionId:
|
|
149
|
-
streamId:
|
|
190
|
+
return n.chat(e, o.id, {
|
|
191
|
+
sessionId: l.sessionId,
|
|
192
|
+
streamId: l.streamId,
|
|
150
193
|
messages: c
|
|
151
194
|
}, {
|
|
152
|
-
signal:
|
|
195
|
+
signal: t.signal
|
|
153
196
|
});
|
|
197
|
+
},
|
|
198
|
+
rate(c, h) {
|
|
199
|
+
return h ? g.update(h, c) : g.create(c);
|
|
200
|
+
},
|
|
201
|
+
speak(c) {
|
|
202
|
+
if (!r)
|
|
203
|
+
throw new Error("Agent not initializated");
|
|
204
|
+
let h;
|
|
205
|
+
return c.type === "text" ? h = {
|
|
206
|
+
script: {
|
|
207
|
+
type: "text",
|
|
208
|
+
provider: c.provider,
|
|
209
|
+
input: c.input,
|
|
210
|
+
ssml: c.ssml || !1
|
|
211
|
+
}
|
|
212
|
+
} : c.type === "audio" && (h = {
|
|
213
|
+
script: {
|
|
214
|
+
type: "audio",
|
|
215
|
+
audio_url: c.audio_url
|
|
216
|
+
}
|
|
217
|
+
}), l.speak(h);
|
|
218
|
+
},
|
|
219
|
+
onChatEvents(c) {
|
|
220
|
+
p.subscribeToEvents(c);
|
|
221
|
+
},
|
|
222
|
+
onConnectionEvents(c) {
|
|
223
|
+
l.addCallback("onConnectionStateChange", c);
|
|
224
|
+
},
|
|
225
|
+
onVideoEvents(c) {
|
|
226
|
+
l.addCallback("onVideoStateChange", c);
|
|
154
227
|
}
|
|
155
228
|
};
|
|
156
229
|
}
|
|
157
|
-
function
|
|
158
|
-
const
|
|
230
|
+
function Q(e) {
|
|
231
|
+
const s = {};
|
|
232
|
+
for (const a in e.callbacks)
|
|
233
|
+
e.callbacks[a] !== void 0 && e.callbacks[a] !== null && (s[a] = e.callbacks[a]);
|
|
234
|
+
return s;
|
|
235
|
+
}
|
|
236
|
+
function X(e, s) {
|
|
237
|
+
const a = v(e, s);
|
|
159
238
|
return {
|
|
160
|
-
createStream(
|
|
161
|
-
return
|
|
162
|
-
driver_id:
|
|
163
|
-
presenter_id:
|
|
164
|
-
compatibility_mode:
|
|
239
|
+
createStream(t) {
|
|
240
|
+
return a.post("/clips/streams", {
|
|
241
|
+
driver_id: t.driver_id,
|
|
242
|
+
presenter_id: t.presenter_id,
|
|
243
|
+
compatibility_mode: t.compatibility_mode
|
|
165
244
|
});
|
|
166
245
|
},
|
|
167
|
-
startConnection(
|
|
168
|
-
return
|
|
246
|
+
startConnection(t, n, r) {
|
|
247
|
+
return a.post(`/clips/streams/${t}/sdp`, {
|
|
169
248
|
session_id: r,
|
|
170
|
-
answer:
|
|
249
|
+
answer: n
|
|
171
250
|
});
|
|
172
251
|
},
|
|
173
|
-
addIceCandidate(
|
|
174
|
-
return
|
|
252
|
+
addIceCandidate(t, n, r) {
|
|
253
|
+
return a.post(`/clips/streams/${t}/ice`, {
|
|
175
254
|
session_id: r,
|
|
176
|
-
...
|
|
255
|
+
...n
|
|
177
256
|
});
|
|
178
257
|
},
|
|
179
|
-
sendStreamRequest(
|
|
180
|
-
return
|
|
181
|
-
session_id:
|
|
258
|
+
sendStreamRequest(t, n, r) {
|
|
259
|
+
return a.post(`/clips/streams/${t}`, {
|
|
260
|
+
session_id: n,
|
|
182
261
|
...r
|
|
183
262
|
});
|
|
184
263
|
},
|
|
185
|
-
close(
|
|
186
|
-
return
|
|
187
|
-
session_id:
|
|
264
|
+
close(t, n) {
|
|
265
|
+
return a.delete(`/clips/streams/${t}`, {
|
|
266
|
+
session_id: n
|
|
188
267
|
});
|
|
189
268
|
}
|
|
190
269
|
};
|
|
191
270
|
}
|
|
192
|
-
function
|
|
193
|
-
const
|
|
271
|
+
function Y(e, s) {
|
|
272
|
+
const a = v(e, s);
|
|
194
273
|
return {
|
|
195
|
-
createStream(
|
|
196
|
-
return
|
|
197
|
-
source_url:
|
|
198
|
-
driver_url:
|
|
199
|
-
face:
|
|
200
|
-
config:
|
|
201
|
-
},
|
|
274
|
+
createStream(t, n) {
|
|
275
|
+
return a.post("/talks/streams", {
|
|
276
|
+
source_url: t.source_url,
|
|
277
|
+
driver_url: t.driver_url,
|
|
278
|
+
face: t.face,
|
|
279
|
+
config: t.config
|
|
280
|
+
}, n);
|
|
202
281
|
},
|
|
203
|
-
startConnection(
|
|
204
|
-
return
|
|
282
|
+
startConnection(t, n, r, o) {
|
|
283
|
+
return a.post(`/talks/streams/${t}/sdp`, {
|
|
205
284
|
session_id: r,
|
|
206
|
-
answer:
|
|
207
|
-
},
|
|
285
|
+
answer: n
|
|
286
|
+
}, o);
|
|
208
287
|
},
|
|
209
|
-
addIceCandidate(
|
|
210
|
-
return
|
|
288
|
+
addIceCandidate(t, n, r, o) {
|
|
289
|
+
return a.post(`/talks/streams/${t}/ice`, {
|
|
211
290
|
session_id: r,
|
|
212
|
-
...
|
|
213
|
-
},
|
|
291
|
+
...n
|
|
292
|
+
}, o);
|
|
214
293
|
},
|
|
215
|
-
sendStreamRequest(
|
|
216
|
-
return
|
|
217
|
-
session_id:
|
|
294
|
+
sendStreamRequest(t, n, r, o) {
|
|
295
|
+
return a.post(`/talks/streams/${t}`, {
|
|
296
|
+
session_id: n,
|
|
218
297
|
...r
|
|
219
|
-
},
|
|
298
|
+
}, o);
|
|
220
299
|
},
|
|
221
|
-
close(
|
|
222
|
-
return
|
|
223
|
-
session_id:
|
|
300
|
+
close(t, n, r) {
|
|
301
|
+
return a.delete(`/talks/streams/${t}`, {
|
|
302
|
+
session_id: n
|
|
224
303
|
}, r);
|
|
225
304
|
}
|
|
226
305
|
};
|
|
227
306
|
}
|
|
228
|
-
let
|
|
229
|
-
const
|
|
230
|
-
async function
|
|
231
|
-
debug:
|
|
232
|
-
callbacks:
|
|
233
|
-
auth:
|
|
234
|
-
baseURL:
|
|
307
|
+
let x = !1;
|
|
308
|
+
const m = (e, s) => x && console.log(e, s), Z = (window.RTCPeerConnection || window.webkitRTCPeerConnection || window.mozRTCPeerConnection).bind(window);
|
|
309
|
+
async function R(e, {
|
|
310
|
+
debug: s = !1,
|
|
311
|
+
callbacks: a,
|
|
312
|
+
auth: t,
|
|
313
|
+
baseURL: n = "https://api.d-id.com"
|
|
235
314
|
}) {
|
|
236
|
-
|
|
237
|
-
const {
|
|
238
|
-
startConnection:
|
|
239
|
-
sendStreamRequest:
|
|
240
|
-
close:
|
|
241
|
-
createStream:
|
|
242
|
-
addIceCandidate:
|
|
243
|
-
} =
|
|
244
|
-
id:
|
|
245
|
-
offer:
|
|
246
|
-
ice_servers:
|
|
247
|
-
session_id:
|
|
248
|
-
} = await
|
|
249
|
-
iceServers:
|
|
250
|
-
}),
|
|
251
|
-
if (!
|
|
315
|
+
x = s;
|
|
316
|
+
const r = a || {}, {
|
|
317
|
+
startConnection: o,
|
|
318
|
+
sendStreamRequest: g,
|
|
319
|
+
close: C,
|
|
320
|
+
createStream: l,
|
|
321
|
+
addIceCandidate: p
|
|
322
|
+
} = e.videoType === k.Clip ? X(t, n) : Y(t, n), {
|
|
323
|
+
id: u,
|
|
324
|
+
offer: c,
|
|
325
|
+
ice_servers: h,
|
|
326
|
+
session_id: f
|
|
327
|
+
} = await l(e), d = new Z({
|
|
328
|
+
iceServers: h
|
|
329
|
+
}), I = d.createDataChannel("JanusDataChannel");
|
|
330
|
+
if (!f)
|
|
252
331
|
throw new Error("Could not create session_id");
|
|
253
|
-
|
|
254
|
-
|
|
332
|
+
d.onicecandidate = (i) => {
|
|
333
|
+
m("peerConnection.onicecandidate", i), i.candidate && i.candidate.sdpMid && i.candidate.sdpMLineIndex !== null && p(u, {
|
|
255
334
|
candidate: i.candidate.candidate,
|
|
256
335
|
sdpMid: i.candidate.sdpMid,
|
|
257
336
|
sdpMLineIndex: i.candidate.sdpMLineIndex
|
|
258
|
-
},
|
|
259
|
-
},
|
|
337
|
+
}, f);
|
|
338
|
+
}, d.oniceconnectionstatechange = () => {
|
|
260
339
|
var i;
|
|
261
|
-
|
|
262
|
-
},
|
|
263
|
-
var
|
|
264
|
-
|
|
265
|
-
},
|
|
266
|
-
var
|
|
267
|
-
if (
|
|
268
|
-
const [
|
|
269
|
-
|
|
340
|
+
m("peerConnection.oniceconnectionstatechange => " + d.iceConnectionState), (i = r.onConnectionStateChange) == null || i.call(r, d.iceConnectionState);
|
|
341
|
+
}, d.ontrack = (i) => {
|
|
342
|
+
var w;
|
|
343
|
+
m("peerConnection.ontrack", i), (w = r.onSrcObjectReady) == null || w.call(r, i.streams[0]);
|
|
344
|
+
}, I.onmessage = (i) => {
|
|
345
|
+
var w, _, y;
|
|
346
|
+
if (I.readyState === "open") {
|
|
347
|
+
const [$, E] = i.data.split(":");
|
|
348
|
+
$ === A.StreamDone ? (w = r.onVideoStateChange) == null || w.call(r, S.Stop) : $ === A.StreamStarted ? (_ = r.onVideoStateChange) == null || _.call(r, S.Start) : (y = r.onMessage) == null || y.call(r, $, decodeURIComponent(E));
|
|
270
349
|
}
|
|
271
|
-
}, await
|
|
272
|
-
const
|
|
273
|
-
return
|
|
350
|
+
}, await d.setRemoteDescription(c), m("set remote description OK");
|
|
351
|
+
const P = await d.createAnswer();
|
|
352
|
+
return m("create answer OK"), await d.setLocalDescription(P), m("set local description OK"), await o(u, P, f), m("start connection OK"), {
|
|
353
|
+
/**
|
|
354
|
+
* Method to send request to server to get clip or talk depend on you payload
|
|
355
|
+
* @param payload
|
|
356
|
+
*/
|
|
274
357
|
speak(i) {
|
|
275
|
-
return
|
|
358
|
+
return g(u, f, i);
|
|
276
359
|
},
|
|
360
|
+
/**
|
|
361
|
+
* Method to close RTC connection
|
|
362
|
+
*/
|
|
277
363
|
async terminate() {
|
|
278
|
-
var i,
|
|
279
|
-
|
|
280
|
-
}), (i =
|
|
364
|
+
var i, w;
|
|
365
|
+
u && (d && (d.close(), d.oniceconnectionstatechange = null, d.onnegotiationneeded = null, d.onicecandidate = null, d.ontrack = null), await C(u, f).catch((_) => {
|
|
366
|
+
}), (i = r.onConnectionStateChange) == null || i.call(r, "closed"), (w = r.onVideoStateChange) == null || w.call(r, S.Stop));
|
|
281
367
|
},
|
|
282
|
-
|
|
283
|
-
|
|
368
|
+
/**
|
|
369
|
+
* Session identifier information, should be returned in the body of all streaming requests
|
|
370
|
+
*/
|
|
371
|
+
sessionId: f,
|
|
372
|
+
/**
|
|
373
|
+
* Id of current RTC stream
|
|
374
|
+
*/
|
|
375
|
+
streamId: u,
|
|
376
|
+
/**
|
|
377
|
+
* Method to add callback that will be trigered on supported events
|
|
378
|
+
* @param eventName
|
|
379
|
+
* @param callback
|
|
380
|
+
*/
|
|
381
|
+
addCallback(i, w) {
|
|
382
|
+
r[i] = w;
|
|
383
|
+
}
|
|
284
384
|
};
|
|
285
385
|
}
|
|
286
386
|
export {
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
387
|
+
z as DocumentType,
|
|
388
|
+
U as KnowledgeType,
|
|
389
|
+
K as Providers,
|
|
390
|
+
L as RateState,
|
|
391
|
+
A as StreamEvents,
|
|
392
|
+
S as StreamingState,
|
|
393
|
+
N as Subject,
|
|
394
|
+
k as VideoType,
|
|
395
|
+
T as VoiceAccess,
|
|
396
|
+
D as createAgentManager,
|
|
397
|
+
R as createStreamingManager,
|
|
398
|
+
M as getAgentStreamArgs
|
|
293
399
|
};
|
package/dist/index.umd.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
(function(
|
|
1
|
+
(function(d,C){typeof exports=="object"&&typeof module<"u"?C(exports):typeof define=="function"&&define.amd?define(["exports"],C):(d=typeof globalThis<"u"?globalThis:d||self,C(d.index={}))})(this,function(d){"use strict";var C=(e=>(e.Amazon="amazon",e.Microsoft="microsoft",e.Afflorithmics="afflorithmics",e.Elevenlabs="elevenlabs",e))(C||{}),b=(e=>(e.Public="public",e.Premium="premium",e.Private="private",e))(b||{}),_=(e=>(e.Start="START",e.Stop="STOP",e))(_||{}),$=(e=>(e.ChatAnswer="chat/answer",e.ChatPartial="chat/partial",e.StreamDone="stream/done",e.StreamStarted="stream/started",e))($||{}),R=(e=>(e.Unrated="Unrated",e.Positive="Positive",e.Negative="Negative",e))(R||{}),T=(e=>(e.KnowledgeProcessing="knowledge/processing",e.KnowledgeIndexing="knowledge/indexing",e.KnowledgeFailed="knowledge/error",e.KnowledgeDone="knowledge/done",e))(T||{}),E=(e=>(e.Knowledge="knowledge",e.Document="document",e.Record="record",e))(E||{}),K=(e=>(e.Pdf="pdf",e.Text="text",e.Html="html",e.Word="word",e.Json="json",e.Markdown="markdown",e.Csv="csv",e.Excel="excel",e.Powerpoint="powerpoint",e.Archive="archive",e.Image="image",e.Audio="audio",e.Video="video",e))(K||{}),S=(e=>(e.Clip="clip",e.Talk="talk",e))(S||{});function L(e){if(e.type==="bearer")return`Bearer ${e.token}`;if(e.type==="basic")return`Basic ${btoa(`${e.username}:${e.password}`)}`;if(e.type==="key")return`Client-Key ${e.clientKey}`;throw new Error(`Unknown auth type: ${e}`)}function A(e,i="https://api.d-id.com"){const a=async(t,n)=>{const r=await fetch(i+(t!=null&&t.startsWith("/")?t:`/${t}`),{...n,headers:{...n==null?void 0:n.headers,Authorization:L(e),"Content-Type":"application/json"}});if(!r.ok){let o=await r.text().catch(()=>"Failed to fetch");throw new Error(o)}return r.json()};return{get(t,n){return a(t,{...n,method:"GET"})},post(t,n,r){return a(t,{...r,body:JSON.stringify(n),method:"POST"})},delete(t,n,r){return a(t,{...r,body:JSON.stringify(n),method:"DELETE"})},patch(t,n,r){return a(t,{...r,body:JSON.stringify(n),method:"PATCH"})}}}function J(e,i="https://api.d-id.com"){const a=A(e,`${i}/agents`);return{create(t,n){return a.post("/",t,n)},getAgents(t,n){return a.get(`/${t?`?tag=${t}`:""}`,n).then(r=>r??[])},getById(t,n){return a.get(`/${t}`,n)},delete(t,n){return a.delete(`/${t}`,void 0,n)},update(t,n,r){return a.patch(`/${t}`,n,r)},newChat(t,n){return a.post(`/${t}/chat`,void 0,n)},chat(t,n,r,o){return a.post(`/${t}/chat/${n}`,r,o)}}}function V(e,i="https://api.d-id.com"){const a=A(e,`${i}/chats/ratings`);return{create(t,n){return a.post("/",t,n)},getByKnowledge(t,n){return a.get(`/${t}`,n).then(r=>r??[])},update(t,n,r){return a.patch(`/${t}`,n,r)},delete(t,n){return a.delete(`/${t}`,n)}}}const q="wss://notifications-dev.d-id.com",x=e=>new Promise(i=>setTimeout(i,e));function H(e){return new Promise((i,a)=>{const{callbacks:t,host:n,auth:r}=e,{onMessage:o=null,onOpen:p=null,onClose:k=null,onError:u=null}=t||{},f=new WebSocket(`${n}?authorization=${L(r)}`);f.onmessage=o,f.onclose=k,f.onerror=g=>{console.log(g),u==null||u(g),a(g)},f.onopen=g=>{p==null||p(g),i(f)}})}async function W(e){const{retries:i=1}=e;let a=null;for(let t=0;(a==null?void 0:a.readyState)!==WebSocket.OPEN;t++)try{a=await H(e)}catch(n){if(t===i)throw n;await x(t*500)}return a}async function F(e,i=q){let a,t=[],n={terminate:()=>{var o;return(o=n.socket)==null?void 0:o.close()},connect:async()=>(a=await W({auth:e,host:i,callbacks:{onMessage:r}}),n.socket=a,a),subscribeToEvents:o=>{n.socket?t.push(o):console.warn("Socket is not connected. Please call connect() first.")}};function r(o){console.log("event",o),t.forEach(p=>p(o))}return n}const G={baseURL:"https://api.d-id.com"};function y(e){return e.presenter.type===S.Clip?{videoType:S.Clip,driver_id:e.presenter.driver_id,presenter_id:e.presenter.presenter_id}:{videoType:S.Talk,source_url:e.presenter.source_url}}async function Q(e,i){const a=i.baseURL?i.baseURL:G.baseURL,t=new AbortController,n=J(i.auth,a),r=await n.getById(e),o=await n.newChat(e),p=await V(i.auth,a),k=X(i);let u=await P(y(r),{...i,callbacks:k});const f=await F(i.auth);return await f.connect(),{agent:r,async reconnectToChat(){u=await P(y(r),{...i,callbacks:k}),u.sessionId},terminate(){return t.abort(),f.terminate(),u.terminate()},chatId:o.id,chat(c){return n.chat(e,o.id,{sessionId:u.sessionId,streamId:u.streamId,messages:c},{signal:t.signal})},rate(c,h){return h?p.update(h,c):p.create(c)},speak(c){if(!r)throw new Error("Agent not initializated");let h;return c.type==="text"?h={script:{type:"text",provider:c.provider,input:c.input,ssml:c.ssml||!1}}:c.type==="audio"&&(h={script:{type:"audio",audio_url:c.audio_url}}),u.speak(h)},onChatEvents(c){f.subscribeToEvents(c)},onConnectionEvents(c){u.addCallback("onConnectionStateChange",c)},onVideoEvents(c){u.addCallback("onVideoStateChange",c)}}}function X(e){const i={};for(const a in e.callbacks)e.callbacks[a]!==void 0&&e.callbacks[a]!==null&&(i[a]=e.callbacks[a]);return i}function Y(e,i){const a=A(e,i);return{createStream(t){return a.post("/clips/streams",{driver_id:t.driver_id,presenter_id:t.presenter_id,compatibility_mode:t.compatibility_mode})},startConnection(t,n,r){return a.post(`/clips/streams/${t}/sdp`,{session_id:r,answer:n})},addIceCandidate(t,n,r){return a.post(`/clips/streams/${t}/ice`,{session_id:r,...n})},sendStreamRequest(t,n,r){return a.post(`/clips/streams/${t}`,{session_id:n,...r})},close(t,n){return a.delete(`/clips/streams/${t}`,{session_id:n})}}}function Z(e,i){const a=A(e,i);return{createStream(t,n){return a.post("/talks/streams",{source_url:t.source_url,driver_url:t.driver_url,face:t.face,config:t.config},n)},startConnection(t,n,r,o){return a.post(`/talks/streams/${t}/sdp`,{session_id:r,answer:n},o)},addIceCandidate(t,n,r,o){return a.post(`/talks/streams/${t}/ice`,{session_id:r,...n},o)},sendStreamRequest(t,n,r,o){return a.post(`/talks/streams/${t}`,{session_id:n,...r},o)},close(t,n,r){return a.delete(`/talks/streams/${t}`,{session_id:n},r)}}}let N=!1;const w=(e,i)=>N&&console.log(e,i),D=(window.RTCPeerConnection||window.webkitRTCPeerConnection||window.mozRTCPeerConnection).bind(window);async function P(e,{debug:i=!1,callbacks:a,auth:t,baseURL:n="https://api.d-id.com"}){N=i;const r=a||{},{startConnection:o,sendStreamRequest:p,close:k,createStream:u,addIceCandidate:f}=e.videoType===S.Clip?Y(t,n):Z(t,n),{id:g,offer:c,ice_servers:h,session_id:v}=await u(e),l=new D({iceServers:h}),U=l.createDataChannel("JanusDataChannel");if(!v)throw new Error("Could not create session_id");l.onicecandidate=s=>{w("peerConnection.onicecandidate",s),s.candidate&&s.candidate.sdpMid&&s.candidate.sdpMLineIndex!==null&&f(g,{candidate:s.candidate.candidate,sdpMid:s.candidate.sdpMid,sdpMLineIndex:s.candidate.sdpMLineIndex},v)},l.oniceconnectionstatechange=()=>{var s;w("peerConnection.oniceconnectionstatechange => "+l.iceConnectionState),(s=r.onConnectionStateChange)==null||s.call(r,l.iceConnectionState)},l.ontrack=s=>{var m;w("peerConnection.ontrack",s),(m=r.onSrcObjectReady)==null||m.call(r,s.streams[0])},U.onmessage=s=>{var m,I,B;if(U.readyState==="open"){const[M,O]=s.data.split(":");M===$.StreamDone?(m=r.onVideoStateChange)==null||m.call(r,_.Stop):M===$.StreamStarted?(I=r.onVideoStateChange)==null||I.call(r,_.Start):(B=r.onMessage)==null||B.call(r,M,decodeURIComponent(O))}},await l.setRemoteDescription(c),w("set remote description OK");const z=await l.createAnswer();return w("create answer OK"),await l.setLocalDescription(z),w("set local description OK"),await o(g,z,v),w("start connection OK"),{speak(s){return p(g,v,s)},async terminate(){var s,m;g&&(l&&(l.close(),l.oniceconnectionstatechange=null,l.onnegotiationneeded=null,l.onicecandidate=null,l.ontrack=null),await k(g,v).catch(I=>{}),(s=r.onConnectionStateChange)==null||s.call(r,"closed"),(m=r.onVideoStateChange)==null||m.call(r,_.Stop))},sessionId:v,streamId:g,addCallback(s,m){r[s]=m}}}d.DocumentType=K,d.KnowledgeType=E,d.Providers=C,d.RateState=R,d.StreamEvents=$,d.StreamingState=_,d.Subject=T,d.VideoType=S,d.VoiceAccess=b,d.createAgentManager=Q,d.createStreamingManager=P,d.getAgentStreamArgs=y,Object.defineProperty(d,Symbol.toStringTag,{value:"Module"})});
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Agent, AgentPayload, Auth, Chat, ChatPayload, ChatResponse } from '
|
|
1
|
+
import { Agent, AgentPayload, Auth, Chat, ChatPayload, ChatResponse } from '../../../types/index';
|
|
2
2
|
export declare function createAgentsApi(auth: Auth, host?: string): {
|
|
3
3
|
create(payload: AgentPayload, options?: RequestInit): Promise<Agent>;
|
|
4
4
|
getAgents(tag?: string, options?: RequestInit): Promise<Agent[]>;
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { Auth, RtcApi } from '
|
|
1
|
+
import { Auth, RtcApi } from '../../types/index';
|
|
2
2
|
export declare function createApi(auth: Auth, host: string): RtcApi;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Auth } from '
|
|
1
|
+
import { Auth } from '../../../types/auth';
|
|
2
2
|
export declare function createClient(auth: Auth, host?: string): {
|
|
3
3
|
get<T = any>(url: string, options?: RequestInit): Promise<T>;
|
|
4
4
|
post<T_1 = any>(url: string, body?: any, options?: RequestInit): Promise<T_1>;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Auth, CreateDocumentPayload, DocumentData, KnowledgeData, KnowledgePayload, QueryResult, RecordData } from '
|
|
1
|
+
import { Auth, CreateDocumentPayload, DocumentData, KnowledgeData, KnowledgePayload, QueryResult, RecordData } from '../../types/index';
|
|
2
2
|
export declare function createKnowledgeApi(auth: Auth, host?: string): {
|
|
3
3
|
createKnowledge(payload: KnowledgePayload, options?: RequestInit): Promise<KnowledgeData>;
|
|
4
4
|
getKnowledgeBase(options?: RequestInit): Promise<KnowledgeData[]>;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { Auth, RatingEntity, RatingPayload } from '../../index';
|
|
2
|
+
export declare function createRatingssApi(auth: Auth, host?: string): {
|
|
3
|
+
create(payload: RatingPayload, options?: RequestInit): Promise<RatingEntity>;
|
|
4
|
+
getByKnowledge(knowledgeId?: string, options?: RequestInit): Promise<RatingEntity[]>;
|
|
5
|
+
update(id: string, payload: Partial<RatingPayload>, options?: RequestInit): Promise<RatingEntity>;
|
|
6
|
+
delete(id: string, options?: RequestInit): Promise<RatingEntity>;
|
|
7
|
+
};
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { Auth, RtcApi } from '
|
|
1
|
+
import { Auth, RtcApi } from '../../types/index';
|
|
2
2
|
export declare function createApi(auth: Auth, host: string): RtcApi;
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { Auth } from '
|
|
1
|
+
import { Auth } from '../../types/auth';
|
|
2
2
|
export declare function getAuthHeader(auth: Auth): string;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { Auth } from '../types/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
|
+
interface SocketManager {
|
|
14
|
+
socket?: WebSocket;
|
|
15
|
+
terminate: () => void;
|
|
16
|
+
connect: () => Promise<WebSocket>;
|
|
17
|
+
subscribeToEvents: (data: any) => void;
|
|
18
|
+
}
|
|
19
|
+
export declare function connectToSocket(options: Options): Promise<WebSocket>;
|
|
20
|
+
export declare function SocketManager(auth: Auth, host?: string): Promise<SocketManager>;
|
|
21
|
+
export {};
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { Agent, AgentsAPI, AgentManagerOptions, CreateStreamOptions } from '../types/index';
|
|
2
|
+
export declare function getAgentStreamArgs(agent: Agent): CreateStreamOptions;
|
|
3
|
+
/**
|
|
4
|
+
* Creates a new Agent Manager instance for interacting with an agent, chat, and related connections.
|
|
5
|
+
*
|
|
6
|
+
* @param {string} agentId - The ID of the agent to chat with.
|
|
7
|
+
* @param {AgentManagerOptions} options - Configurations for the Agent Manager API.
|
|
8
|
+
* * @returns {Promise<AgentsAPI>} - A promise that resolves to an instance of the AgentsAPI interface.
|
|
9
|
+
*
|
|
10
|
+
* @throws {Error} Throws an error if the agent is not initialized.
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* const agentManager = await createAgentManager('id-agent123', { auth: { type: 'key', clientKey: '123', externalId: '123' } });
|
|
14
|
+
*/
|
|
15
|
+
export declare function createAgentManager(agentId: string, options: AgentManagerOptions): Promise<AgentsAPI>;
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { CreateStreamOptions, ManagerCallbackKeys, PayloadType, StreamingManagerOptions } from '../types/index';
|
|
2
|
+
export declare function createStreamingManager<T extends CreateStreamOptions>(agent: T, { debug, callbacks, auth, baseURL }: StreamingManagerOptions): Promise<{
|
|
3
|
+
/**
|
|
4
|
+
* Method to send request to server to get clip or talk depend on you payload
|
|
5
|
+
* @param payload
|
|
6
|
+
*/
|
|
7
|
+
speak(payload: PayloadType<T>): Promise<import('../types/index').SendStreamPayloadResponse>;
|
|
8
|
+
/**
|
|
9
|
+
* Method to close RTC connection
|
|
10
|
+
*/
|
|
11
|
+
terminate(): Promise<void>;
|
|
12
|
+
/**
|
|
13
|
+
* Session identifier information, should be returned in the body of all streaming requests
|
|
14
|
+
*/
|
|
15
|
+
sessionId: string;
|
|
16
|
+
/**
|
|
17
|
+
* Id of current RTC stream
|
|
18
|
+
*/
|
|
19
|
+
streamId: string;
|
|
20
|
+
/**
|
|
21
|
+
* Method to add callback that will be trigered on supported events
|
|
22
|
+
* @param eventName
|
|
23
|
+
* @param callback
|
|
24
|
+
*/
|
|
25
|
+
addCallback(eventName: ManagerCallbackKeys, callback: Function): void;
|
|
26
|
+
}>;
|
|
27
|
+
export type StreamingManager<T extends CreateStreamOptions> = Awaited<ReturnType<typeof createStreamingManager<T>>>;
|
|
@@ -10,6 +10,7 @@ export interface BasicAuth {
|
|
|
10
10
|
export interface ApiKeyAuth {
|
|
11
11
|
type: 'key';
|
|
12
12
|
clientKey: string;
|
|
13
|
+
externalId: string;
|
|
13
14
|
}
|
|
14
15
|
export type Auth = BearerToken | BasicAuth | ApiKeyAuth;
|
|
15
16
|
export interface GetAuthParams {
|
|
@@ -17,4 +18,5 @@ export interface GetAuthParams {
|
|
|
17
18
|
username?: string | null;
|
|
18
19
|
password?: string | null;
|
|
19
20
|
clientKey?: string | null;
|
|
21
|
+
externalId?: string | null;
|
|
20
22
|
}
|
|
@@ -1,8 +1,27 @@
|
|
|
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
|
+
}
|
|
19
|
+
export type RatingPayload = Omit<RatingEntity, 'owner_id' | 'id' | 'created_at' | 'modified_at' | 'created_by' | 'external_id'>;
|
|
1
20
|
export interface Message {
|
|
2
21
|
role: 'system' | 'assistant' | 'user' | 'function' | 'tool';
|
|
3
22
|
content: string;
|
|
4
|
-
name?: string;
|
|
5
23
|
created_at: string;
|
|
24
|
+
matches?: ChatResponse['matches'];
|
|
6
25
|
}
|
|
7
26
|
export interface ChatPayload {
|
|
8
27
|
messages: Message[];
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
import { SupportedStreamScipt } from '../../../types/StreamScript';
|
|
2
|
+
import { Auth } from '../../auth';
|
|
3
|
+
import { SendStreamPayloadResponse, StreamingState } from '../../stream';
|
|
4
|
+
import { Agent } from './agent';
|
|
5
|
+
import { ChatResponse, Message, RatingEntity, RatingPayload } from './chat';
|
|
6
|
+
/**
|
|
7
|
+
* Types of events provided in Chat Progress Callback
|
|
8
|
+
*/
|
|
9
|
+
declare enum ChatProgress {
|
|
10
|
+
/**
|
|
11
|
+
* Chat was successfully embedded
|
|
12
|
+
*/
|
|
13
|
+
Embed = 0,
|
|
14
|
+
/**
|
|
15
|
+
* Server processing chat message
|
|
16
|
+
*/
|
|
17
|
+
Query = 1,
|
|
18
|
+
/**
|
|
19
|
+
* Server processed message and returned response
|
|
20
|
+
*/
|
|
21
|
+
Answer = 2,
|
|
22
|
+
/**
|
|
23
|
+
* Chat was closed
|
|
24
|
+
*/
|
|
25
|
+
Complete = 3
|
|
26
|
+
}
|
|
27
|
+
export type ChatProgressCallback = (progress: ChatProgress) => void;
|
|
28
|
+
export type ConnectionStateChangeCallback = (state: RTCIceConnectionState) => void;
|
|
29
|
+
export type VideoStateChangeCallback = (state: StreamingState) => void;
|
|
30
|
+
interface ManagerCallbacks {
|
|
31
|
+
/**
|
|
32
|
+
* This callback will be triggered each time the RTC connection changes state
|
|
33
|
+
* @param state
|
|
34
|
+
*/
|
|
35
|
+
onConnectionStateChange?(state: RTCIceConnectionState): void;
|
|
36
|
+
/**
|
|
37
|
+
* Optional callback function that will be triggered each time video events happen
|
|
38
|
+
* @param state
|
|
39
|
+
*/
|
|
40
|
+
onVideoStateChange?(state: StreamingState): void;
|
|
41
|
+
/**
|
|
42
|
+
* Callback function that will be triggered each time the video stream starts or stop
|
|
43
|
+
* @param srcObject
|
|
44
|
+
*/
|
|
45
|
+
onSrcObjectReady?(srcObject: MediaStream): void;
|
|
46
|
+
/**
|
|
47
|
+
* Optional callback function that will be triggered each time any changes happen in the chat
|
|
48
|
+
* @param progress
|
|
49
|
+
*/
|
|
50
|
+
onChatEvents?(progress: ChatProgress): void;
|
|
51
|
+
}
|
|
52
|
+
export interface AgentManagerOptions {
|
|
53
|
+
callbacks?: ManagerCallbacks;
|
|
54
|
+
baseURL?: string;
|
|
55
|
+
debug?: boolean;
|
|
56
|
+
auth: Auth;
|
|
57
|
+
}
|
|
58
|
+
export interface AgentsAPI {
|
|
59
|
+
/**
|
|
60
|
+
* Agent instance you are working with.
|
|
61
|
+
* To know more about agents go to https://docs.d-id.com/reference/agents
|
|
62
|
+
*/
|
|
63
|
+
agent: Agent;
|
|
64
|
+
/**
|
|
65
|
+
* Method to be reconnected to chat
|
|
66
|
+
* Since chat uses an RTC connection to communicate with the agent, it could be dropped and to continue to chat you need to reconnect
|
|
67
|
+
*/
|
|
68
|
+
reconnectToChat: () => Promise<void>;
|
|
69
|
+
/**
|
|
70
|
+
* Method to close all connections with agent, stream and web socket
|
|
71
|
+
*/
|
|
72
|
+
terminate: () => Promise<void>;
|
|
73
|
+
/**
|
|
74
|
+
* ID of chat you are working on now
|
|
75
|
+
*/
|
|
76
|
+
chatId: string;
|
|
77
|
+
/**
|
|
78
|
+
* Method to send a chat message to existing chat with the agent
|
|
79
|
+
* @param messages
|
|
80
|
+
*/
|
|
81
|
+
chat: (messages: Message[]) => Promise<ChatResponse>;
|
|
82
|
+
/**
|
|
83
|
+
* This method provides you the possibility to rate your chat experience
|
|
84
|
+
* TODO asks Sagi how it's work
|
|
85
|
+
* @param payload
|
|
86
|
+
* @param id - id of Rating entity. Leave it empty to create a new, one or pass it to work with the existing one
|
|
87
|
+
*/
|
|
88
|
+
rate: (payload: RatingPayload, id?: string) => Promise<RatingEntity>;
|
|
89
|
+
/**
|
|
90
|
+
* Method to make your agent read the text you provide or reproduce sound
|
|
91
|
+
* @param payload
|
|
92
|
+
*/
|
|
93
|
+
speak: (payload: SupportedStreamScipt) => Promise<SendStreamPayloadResponse>;
|
|
94
|
+
/**
|
|
95
|
+
* Optional callback function that will be triggered each time any changes happen in the chat
|
|
96
|
+
* @param callback
|
|
97
|
+
*/
|
|
98
|
+
onChatEvents: (callback: ChatProgressCallback) => void;
|
|
99
|
+
/**
|
|
100
|
+
* Optional callback function that will be triggered each time the RTC connection gets new status
|
|
101
|
+
* @param callback
|
|
102
|
+
*/
|
|
103
|
+
onConnectionEvents: (callback: ConnectionStateChangeCallback) => void;
|
|
104
|
+
/**
|
|
105
|
+
* Optional callback function that will be triggered each time video events happen
|
|
106
|
+
* @param callback
|
|
107
|
+
*/
|
|
108
|
+
onVideoEvents: (callback: VideoStateChangeCallback) => void;
|
|
109
|
+
}
|
|
110
|
+
export {};
|
|
@@ -8,6 +8,7 @@ export declare enum StreamingState {
|
|
|
8
8
|
Stop = "STOP"
|
|
9
9
|
}
|
|
10
10
|
export declare enum StreamEvents {
|
|
11
|
+
ChatAnswer = "chat/answer",
|
|
11
12
|
ChatPartial = "chat/partial",
|
|
12
13
|
StreamDone = "stream/done",
|
|
13
14
|
StreamStarted = "stream/started"
|
|
@@ -18,6 +19,7 @@ export interface ManagerCallbacks {
|
|
|
18
19
|
onVideoStateChange?: (state: StreamingState) => void;
|
|
19
20
|
onSrcObjectReady?: (value: MediaStream) => void;
|
|
20
21
|
}
|
|
22
|
+
export type ManagerCallbackKeys = keyof ManagerCallbacks;
|
|
21
23
|
export interface TalkStreamOptions extends CreateTalkStreamRequest {
|
|
22
24
|
videoType: VideoType.Talk;
|
|
23
25
|
}
|
|
@@ -34,7 +36,7 @@ export interface RtcApi {
|
|
|
34
36
|
close(streamId: string, sessionId: string): Promise<Status>;
|
|
35
37
|
}
|
|
36
38
|
export interface StreamingManagerOptions {
|
|
37
|
-
callbacks
|
|
39
|
+
callbacks?: ManagerCallbacks;
|
|
38
40
|
baseURL?: string;
|
|
39
41
|
debug?: boolean;
|
|
40
42
|
auth: Auth;
|
package/package.json
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@d-id/client-sdk",
|
|
3
3
|
"private": false,
|
|
4
|
-
"version": "1.0.
|
|
4
|
+
"version": "1.0.18-beta.1",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"description": "d-id client sdk",
|
|
7
7
|
"repository": {
|
|
8
8
|
"type": "git",
|
|
9
|
-
"url": "https://github.com/de-id/
|
|
9
|
+
"url": "https://github.com/de-id/agents-sdk"
|
|
10
10
|
},
|
|
11
11
|
"keywords": [
|
|
12
12
|
"d-id",
|
|
@@ -20,20 +20,15 @@
|
|
|
20
20
|
],
|
|
21
21
|
"main": "./dist/index.umd.cjs",
|
|
22
22
|
"module": "./dist/index.js",
|
|
23
|
-
"types": "./dist/
|
|
24
|
-
"exports": {
|
|
25
|
-
".": {
|
|
26
|
-
"types": "./dist/apps/sdk/lib/index.d.ts",
|
|
27
|
-
"import": "./dist/index.js",
|
|
28
|
-
"require": "./dist/index.umd.cjs"
|
|
29
|
-
}
|
|
30
|
-
},
|
|
23
|
+
"types": "./dist/index.d.ts",
|
|
31
24
|
"scripts": {
|
|
32
25
|
"dev": "vite",
|
|
33
26
|
"build": "node ./infra/build.js -m production",
|
|
34
27
|
"build:dev": "node ./infra/build.js -m development",
|
|
35
28
|
"deploy:prod": "node ./infra/deploy.js --version patch",
|
|
36
|
-
"preview": "vite preview"
|
|
29
|
+
"preview": "vite preview",
|
|
30
|
+
"test-build": "node .infra/build.js -m development",
|
|
31
|
+
"build:docs": "typedoc"
|
|
37
32
|
},
|
|
38
33
|
"devDependencies": {
|
|
39
34
|
"@preact/preset-vite": "^2.5.0",
|
|
@@ -44,9 +39,10 @@
|
|
|
44
39
|
"prettier": "^3.1.0",
|
|
45
40
|
"prettier-plugin-organize-imports": "^3.2.4",
|
|
46
41
|
"serverless": "3.32.2",
|
|
42
|
+
"typedoc": "^0.25.7",
|
|
47
43
|
"typescript": "^5.3.2",
|
|
48
44
|
"vite": "^4.4.5",
|
|
49
45
|
"vite-plugin-dts": "^3.6.0",
|
|
50
46
|
"vite-plugin-html-env": "^1.2.8"
|
|
51
47
|
}
|
|
52
|
-
}
|
|
48
|
+
}
|
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
import { Agent, AgentManagerOptions, CreateStreamOptions, Message } from '../../../common/types/src/index';
|
|
2
|
-
export declare function getAgentStreamArgs(agent: Agent): CreateStreamOptions;
|
|
3
|
-
export declare function createAgentManager(agentId: string, { callbacks, ...options }: AgentManagerOptions): Promise<{
|
|
4
|
-
agent: Agent;
|
|
5
|
-
terminate(): Promise<void>;
|
|
6
|
-
chatId: string;
|
|
7
|
-
chat(messages: Message[]): Promise<import('../../../common/types/src/index').ChatResponse>;
|
|
8
|
-
}>;
|
|
9
|
-
export type AgentManager = Awaited<ReturnType<typeof createAgentManager>>;
|
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
import { CreateStreamOptions, PayloadType, StreamingManagerOptions } from '../../../common/types/src/index';
|
|
2
|
-
export declare function createStreamingManager<T extends CreateStreamOptions>(agent: T, { debug, callbacks, auth, baseURL }: StreamingManagerOptions): Promise<{
|
|
3
|
-
speak(payload: PayloadType<T>): Promise<import('../../../common/types/src/index').SendStreamPayloadResponse>;
|
|
4
|
-
terminate(): Promise<void>;
|
|
5
|
-
sessionId: string;
|
|
6
|
-
streamId: string;
|
|
7
|
-
}>;
|
|
8
|
-
export type StreamingManager<T extends CreateStreamOptions> = Awaited<ReturnType<typeof createStreamingManager<T>>>;
|
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
import { Auth } from '../../auth';
|
|
2
|
-
import { StreamingState } from '../../stream';
|
|
3
|
-
declare enum ChatProgress {
|
|
4
|
-
Embed = 0,
|
|
5
|
-
Query = 1,
|
|
6
|
-
Answer = 2,
|
|
7
|
-
Complete = 3
|
|
8
|
-
}
|
|
9
|
-
interface ManagerCallbacks {
|
|
10
|
-
onConnectionStateChange?(state: RTCIceConnectionState): void;
|
|
11
|
-
onVideoStateChange?(state: StreamingState): void;
|
|
12
|
-
onSrcObjectReady?(srcObject: MediaStream): void;
|
|
13
|
-
onChatEvents?(progress: ChatProgress): void;
|
|
14
|
-
}
|
|
15
|
-
export interface AgentManagerOptions {
|
|
16
|
-
callbacks: ManagerCallbacks;
|
|
17
|
-
baseURL?: string;
|
|
18
|
-
debug?: boolean;
|
|
19
|
-
auth: Auth;
|
|
20
|
-
}
|
|
21
|
-
export {};
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|