assemblyai 4.4.6 → 4.4.7
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/CHANGELOG.md +6 -0
- package/dist/assemblyai.umd.js +49 -9
- package/dist/assemblyai.umd.min.js +1 -1
- package/dist/browser.mjs +43 -8
- package/dist/bun.mjs +43 -8
- package/dist/deno.mjs +43 -8
- package/dist/index.cjs +49 -9
- package/dist/index.mjs +49 -9
- package/dist/node.cjs +43 -8
- package/dist/node.mjs +43 -8
- package/dist/polyfills/fetch/default.d.ts +1 -0
- package/dist/polyfills/fetch/workerd.d.ts +1 -0
- package/dist/services/transcripts/index.d.ts +16 -3
- package/dist/types/transcripts/index.d.ts +9 -0
- package/dist/workerd.mjs +719 -0
- package/package.json +6 -2
- package/src/polyfills/fetch/default.ts +3 -0
- package/src/polyfills/fetch/workerd.ts +1 -0
- package/src/services/base.ts +7 -7
- package/src/services/transcripts/index.ts +35 -2
- package/src/types/transcripts/index.ts +10 -0
package/dist/workerd.mjs
ADDED
|
@@ -0,0 +1,719 @@
|
|
|
1
|
+
import ws from 'ws';
|
|
2
|
+
|
|
3
|
+
const DEFAULT_FETCH_INIT = {};
|
|
4
|
+
|
|
5
|
+
const buildUserAgent = (userAgent) => defaultUserAgentString +
|
|
6
|
+
(userAgent === false
|
|
7
|
+
? ""
|
|
8
|
+
: " AssemblyAI/1.0 (" +
|
|
9
|
+
Object.entries({ ...defaultUserAgent, ...userAgent })
|
|
10
|
+
.map(([key, item]) => item ? `${key}=${item.name}/${item.version}` : "")
|
|
11
|
+
.join(" ") +
|
|
12
|
+
")");
|
|
13
|
+
let defaultUserAgentString = "";
|
|
14
|
+
if (typeof navigator !== "undefined" && navigator.userAgent) {
|
|
15
|
+
defaultUserAgentString += navigator.userAgent;
|
|
16
|
+
}
|
|
17
|
+
const defaultUserAgent = {
|
|
18
|
+
sdk: { name: "JavaScript", version: "4.4.7" },
|
|
19
|
+
};
|
|
20
|
+
if (typeof process !== "undefined") {
|
|
21
|
+
if (process.versions.node && defaultUserAgentString.indexOf("Node") === -1) {
|
|
22
|
+
defaultUserAgent.runtime_env = {
|
|
23
|
+
name: "Node",
|
|
24
|
+
version: process.versions.node,
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
if (process.versions.bun && defaultUserAgentString.indexOf("Bun") === -1) {
|
|
28
|
+
defaultUserAgent.runtime_env = {
|
|
29
|
+
name: "Bun",
|
|
30
|
+
version: process.versions.bun,
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
if (typeof Deno !== "undefined") {
|
|
35
|
+
if (process.versions.bun && defaultUserAgentString.indexOf("Deno") === -1) {
|
|
36
|
+
defaultUserAgent.runtime_env = { name: "Deno", version: Deno.version.deno };
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Base class for services that communicate with the API.
|
|
42
|
+
*/
|
|
43
|
+
class BaseService {
|
|
44
|
+
/**
|
|
45
|
+
* Create a new service.
|
|
46
|
+
* @param params - The parameters to use for the service.
|
|
47
|
+
*/
|
|
48
|
+
constructor(params) {
|
|
49
|
+
this.params = params;
|
|
50
|
+
if (params.userAgent === false) {
|
|
51
|
+
this.userAgent = undefined;
|
|
52
|
+
}
|
|
53
|
+
else {
|
|
54
|
+
this.userAgent = buildUserAgent(params.userAgent || {});
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
async fetch(input, init) {
|
|
58
|
+
init = { ...DEFAULT_FETCH_INIT, ...init };
|
|
59
|
+
let headers = {
|
|
60
|
+
Authorization: this.params.apiKey,
|
|
61
|
+
"Content-Type": "application/json",
|
|
62
|
+
};
|
|
63
|
+
if (DEFAULT_FETCH_INIT?.headers)
|
|
64
|
+
headers = { ...headers, ...DEFAULT_FETCH_INIT.headers };
|
|
65
|
+
if (init?.headers)
|
|
66
|
+
headers = { ...headers, ...init.headers };
|
|
67
|
+
if (this.userAgent) {
|
|
68
|
+
headers["User-Agent"] = this.userAgent;
|
|
69
|
+
// chromium browsers have a bug where the user agent can't be modified
|
|
70
|
+
if (typeof window !== "undefined" && "chrome" in window) {
|
|
71
|
+
headers["AssemblyAI-Agent"] =
|
|
72
|
+
this.userAgent;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
init.headers = headers;
|
|
76
|
+
if (!input.startsWith("http"))
|
|
77
|
+
input = this.params.baseUrl + input;
|
|
78
|
+
const response = await fetch(input, init);
|
|
79
|
+
if (response.status >= 400) {
|
|
80
|
+
let json;
|
|
81
|
+
const text = await response.text();
|
|
82
|
+
if (text) {
|
|
83
|
+
try {
|
|
84
|
+
json = JSON.parse(text);
|
|
85
|
+
}
|
|
86
|
+
catch {
|
|
87
|
+
/* empty */
|
|
88
|
+
}
|
|
89
|
+
if (json?.error)
|
|
90
|
+
throw new Error(json.error);
|
|
91
|
+
throw new Error(text);
|
|
92
|
+
}
|
|
93
|
+
throw new Error(`HTTP Error: ${response.status} ${response.statusText}`);
|
|
94
|
+
}
|
|
95
|
+
return response;
|
|
96
|
+
}
|
|
97
|
+
async fetchJson(input, init) {
|
|
98
|
+
const response = await this.fetch(input, init);
|
|
99
|
+
return response.json();
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
class LemurService extends BaseService {
|
|
104
|
+
summary(params) {
|
|
105
|
+
return this.fetchJson("/lemur/v3/generate/summary", {
|
|
106
|
+
method: "POST",
|
|
107
|
+
body: JSON.stringify(params),
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
questionAnswer(params) {
|
|
111
|
+
return this.fetchJson("/lemur/v3/generate/question-answer", {
|
|
112
|
+
method: "POST",
|
|
113
|
+
body: JSON.stringify(params),
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
actionItems(params) {
|
|
117
|
+
return this.fetchJson("/lemur/v3/generate/action-items", {
|
|
118
|
+
method: "POST",
|
|
119
|
+
body: JSON.stringify(params),
|
|
120
|
+
});
|
|
121
|
+
}
|
|
122
|
+
task(params) {
|
|
123
|
+
return this.fetchJson("/lemur/v3/generate/task", {
|
|
124
|
+
method: "POST",
|
|
125
|
+
body: JSON.stringify(params),
|
|
126
|
+
});
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* Delete the data for a previously submitted LeMUR request.
|
|
130
|
+
* @param id - ID of the LeMUR request
|
|
131
|
+
*/
|
|
132
|
+
purgeRequestData(id) {
|
|
133
|
+
return this.fetchJson(`/lemur/v3/${id}`, {
|
|
134
|
+
method: "DELETE",
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
const { WritableStream } = typeof window !== "undefined"
|
|
140
|
+
? window
|
|
141
|
+
: typeof global !== "undefined"
|
|
142
|
+
? global
|
|
143
|
+
: globalThis;
|
|
144
|
+
|
|
145
|
+
const factory = (url, params) => new ws(url, params);
|
|
146
|
+
|
|
147
|
+
var RealtimeErrorType;
|
|
148
|
+
(function (RealtimeErrorType) {
|
|
149
|
+
RealtimeErrorType[RealtimeErrorType["BadSampleRate"] = 4000] = "BadSampleRate";
|
|
150
|
+
RealtimeErrorType[RealtimeErrorType["AuthFailed"] = 4001] = "AuthFailed";
|
|
151
|
+
// Both InsufficientFunds and FreeAccount error use 4002
|
|
152
|
+
RealtimeErrorType[RealtimeErrorType["InsufficientFundsOrFreeAccount"] = 4002] = "InsufficientFundsOrFreeAccount";
|
|
153
|
+
RealtimeErrorType[RealtimeErrorType["NonexistentSessionId"] = 4004] = "NonexistentSessionId";
|
|
154
|
+
RealtimeErrorType[RealtimeErrorType["SessionExpired"] = 4008] = "SessionExpired";
|
|
155
|
+
RealtimeErrorType[RealtimeErrorType["ClosedSession"] = 4010] = "ClosedSession";
|
|
156
|
+
RealtimeErrorType[RealtimeErrorType["RateLimited"] = 4029] = "RateLimited";
|
|
157
|
+
RealtimeErrorType[RealtimeErrorType["UniqueSessionViolation"] = 4030] = "UniqueSessionViolation";
|
|
158
|
+
RealtimeErrorType[RealtimeErrorType["SessionTimeout"] = 4031] = "SessionTimeout";
|
|
159
|
+
RealtimeErrorType[RealtimeErrorType["AudioTooShort"] = 4032] = "AudioTooShort";
|
|
160
|
+
RealtimeErrorType[RealtimeErrorType["AudioTooLong"] = 4033] = "AudioTooLong";
|
|
161
|
+
RealtimeErrorType[RealtimeErrorType["BadJson"] = 4100] = "BadJson";
|
|
162
|
+
RealtimeErrorType[RealtimeErrorType["BadSchema"] = 4101] = "BadSchema";
|
|
163
|
+
RealtimeErrorType[RealtimeErrorType["TooManyStreams"] = 4102] = "TooManyStreams";
|
|
164
|
+
RealtimeErrorType[RealtimeErrorType["Reconnected"] = 4103] = "Reconnected";
|
|
165
|
+
RealtimeErrorType[RealtimeErrorType["ReconnectAttemptsExhausted"] = 1013] = "ReconnectAttemptsExhausted";
|
|
166
|
+
})(RealtimeErrorType || (RealtimeErrorType = {}));
|
|
167
|
+
const RealtimeErrorMessages = {
|
|
168
|
+
[RealtimeErrorType.BadSampleRate]: "Sample rate must be a positive integer",
|
|
169
|
+
[RealtimeErrorType.AuthFailed]: "Not Authorized",
|
|
170
|
+
[RealtimeErrorType.InsufficientFundsOrFreeAccount]: "Insufficient funds or you are using a free account. This feature is paid-only and requires you to add a credit card. Please visit https://assemblyai.com/dashboard/ to add a credit card to your account.",
|
|
171
|
+
[RealtimeErrorType.NonexistentSessionId]: "Session ID does not exist",
|
|
172
|
+
[RealtimeErrorType.SessionExpired]: "Session has expired",
|
|
173
|
+
[RealtimeErrorType.ClosedSession]: "Session is closed",
|
|
174
|
+
[RealtimeErrorType.RateLimited]: "Rate limited",
|
|
175
|
+
[RealtimeErrorType.UniqueSessionViolation]: "Unique session violation",
|
|
176
|
+
[RealtimeErrorType.SessionTimeout]: "Session Timeout",
|
|
177
|
+
[RealtimeErrorType.AudioTooShort]: "Audio too short",
|
|
178
|
+
[RealtimeErrorType.AudioTooLong]: "Audio too long",
|
|
179
|
+
[RealtimeErrorType.BadJson]: "Bad JSON",
|
|
180
|
+
[RealtimeErrorType.BadSchema]: "Bad schema",
|
|
181
|
+
[RealtimeErrorType.TooManyStreams]: "Too many streams",
|
|
182
|
+
[RealtimeErrorType.Reconnected]: "Reconnected",
|
|
183
|
+
[RealtimeErrorType.ReconnectAttemptsExhausted]: "Reconnect attempts exhausted",
|
|
184
|
+
};
|
|
185
|
+
class RealtimeError extends Error {
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
const defaultRealtimeUrl = "wss://api.assemblyai.com/v2/realtime/ws";
|
|
189
|
+
const forceEndOfUtteranceMessage = `{"force_end_utterance":true}`;
|
|
190
|
+
const terminateSessionMessage = `{"terminate_session":true}`;
|
|
191
|
+
class RealtimeTranscriber {
|
|
192
|
+
constructor(params) {
|
|
193
|
+
this.listeners = {};
|
|
194
|
+
this.realtimeUrl = params.realtimeUrl ?? defaultRealtimeUrl;
|
|
195
|
+
this.sampleRate = params.sampleRate ?? 16_000;
|
|
196
|
+
this.wordBoost = params.wordBoost;
|
|
197
|
+
this.encoding = params.encoding;
|
|
198
|
+
this.endUtteranceSilenceThreshold = params.endUtteranceSilenceThreshold;
|
|
199
|
+
this.disablePartialTranscripts = params.disablePartialTranscripts;
|
|
200
|
+
if ("token" in params && params.token)
|
|
201
|
+
this.token = params.token;
|
|
202
|
+
if ("apiKey" in params && params.apiKey)
|
|
203
|
+
this.apiKey = params.apiKey;
|
|
204
|
+
if (!(this.token || this.apiKey)) {
|
|
205
|
+
throw new Error("API key or temporary token is required.");
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
connectionUrl() {
|
|
209
|
+
const url = new URL(this.realtimeUrl);
|
|
210
|
+
if (url.protocol !== "wss:") {
|
|
211
|
+
throw new Error("Invalid protocol, must be wss");
|
|
212
|
+
}
|
|
213
|
+
const searchParams = new URLSearchParams();
|
|
214
|
+
if (this.token) {
|
|
215
|
+
searchParams.set("token", this.token);
|
|
216
|
+
}
|
|
217
|
+
searchParams.set("sample_rate", this.sampleRate.toString());
|
|
218
|
+
if (this.wordBoost && this.wordBoost.length > 0) {
|
|
219
|
+
searchParams.set("word_boost", JSON.stringify(this.wordBoost));
|
|
220
|
+
}
|
|
221
|
+
if (this.encoding) {
|
|
222
|
+
searchParams.set("encoding", this.encoding);
|
|
223
|
+
}
|
|
224
|
+
searchParams.set("enable_extra_session_information", "true");
|
|
225
|
+
if (this.disablePartialTranscripts) {
|
|
226
|
+
searchParams.set("disable_partial_transcripts", this.disablePartialTranscripts.toString());
|
|
227
|
+
}
|
|
228
|
+
url.search = searchParams.toString();
|
|
229
|
+
return url;
|
|
230
|
+
}
|
|
231
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
232
|
+
on(event, listener) {
|
|
233
|
+
this.listeners[event] = listener;
|
|
234
|
+
}
|
|
235
|
+
connect() {
|
|
236
|
+
return new Promise((resolve) => {
|
|
237
|
+
if (this.socket) {
|
|
238
|
+
throw new Error("Already connected");
|
|
239
|
+
}
|
|
240
|
+
const url = this.connectionUrl();
|
|
241
|
+
if (this.token) {
|
|
242
|
+
this.socket = factory(url.toString());
|
|
243
|
+
}
|
|
244
|
+
else {
|
|
245
|
+
this.socket = factory(url.toString(), {
|
|
246
|
+
headers: { Authorization: this.apiKey },
|
|
247
|
+
});
|
|
248
|
+
}
|
|
249
|
+
this.socket.binaryType = "arraybuffer";
|
|
250
|
+
this.socket.onopen = () => {
|
|
251
|
+
if (this.endUtteranceSilenceThreshold === undefined ||
|
|
252
|
+
this.endUtteranceSilenceThreshold === null) {
|
|
253
|
+
return;
|
|
254
|
+
}
|
|
255
|
+
this.configureEndUtteranceSilenceThreshold(this.endUtteranceSilenceThreshold);
|
|
256
|
+
};
|
|
257
|
+
this.socket.onclose = ({ code, reason }) => {
|
|
258
|
+
if (!reason) {
|
|
259
|
+
if (code in RealtimeErrorType) {
|
|
260
|
+
reason = RealtimeErrorMessages[code];
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
this.listeners.close?.(code, reason);
|
|
264
|
+
};
|
|
265
|
+
this.socket.onerror = (event) => {
|
|
266
|
+
if (event.error)
|
|
267
|
+
this.listeners.error?.(event.error);
|
|
268
|
+
else
|
|
269
|
+
this.listeners.error?.(new Error(event.message));
|
|
270
|
+
};
|
|
271
|
+
this.socket.onmessage = ({ data }) => {
|
|
272
|
+
const message = JSON.parse(data.toString());
|
|
273
|
+
if ("error" in message) {
|
|
274
|
+
this.listeners.error?.(new RealtimeError(message.error));
|
|
275
|
+
return;
|
|
276
|
+
}
|
|
277
|
+
switch (message.message_type) {
|
|
278
|
+
case "SessionBegins": {
|
|
279
|
+
const openObject = {
|
|
280
|
+
sessionId: message.session_id,
|
|
281
|
+
expiresAt: new Date(message.expires_at),
|
|
282
|
+
};
|
|
283
|
+
resolve(openObject);
|
|
284
|
+
this.listeners.open?.(openObject);
|
|
285
|
+
break;
|
|
286
|
+
}
|
|
287
|
+
case "PartialTranscript": {
|
|
288
|
+
// message.created is actually a string when coming from the socket
|
|
289
|
+
message.created = new Date(message.created);
|
|
290
|
+
this.listeners.transcript?.(message);
|
|
291
|
+
this.listeners["transcript.partial"]?.(message);
|
|
292
|
+
break;
|
|
293
|
+
}
|
|
294
|
+
case "FinalTranscript": {
|
|
295
|
+
// message.created is actually a string when coming from the socket
|
|
296
|
+
message.created = new Date(message.created);
|
|
297
|
+
this.listeners.transcript?.(message);
|
|
298
|
+
this.listeners["transcript.final"]?.(message);
|
|
299
|
+
break;
|
|
300
|
+
}
|
|
301
|
+
case "SessionInformation": {
|
|
302
|
+
this.listeners.session_information?.(message);
|
|
303
|
+
break;
|
|
304
|
+
}
|
|
305
|
+
case "SessionTerminated": {
|
|
306
|
+
this.sessionTerminatedResolve?.();
|
|
307
|
+
break;
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
};
|
|
311
|
+
});
|
|
312
|
+
}
|
|
313
|
+
sendAudio(audio) {
|
|
314
|
+
this.send(audio);
|
|
315
|
+
}
|
|
316
|
+
stream() {
|
|
317
|
+
return new WritableStream({
|
|
318
|
+
write: (chunk) => {
|
|
319
|
+
this.sendAudio(chunk);
|
|
320
|
+
},
|
|
321
|
+
});
|
|
322
|
+
}
|
|
323
|
+
/**
|
|
324
|
+
* Manually end an utterance
|
|
325
|
+
*/
|
|
326
|
+
forceEndUtterance() {
|
|
327
|
+
this.send(forceEndOfUtteranceMessage);
|
|
328
|
+
}
|
|
329
|
+
/**
|
|
330
|
+
* Configure the threshold for how long to wait before ending an utterance. Default is 700ms.
|
|
331
|
+
* @param threshold - The duration of the end utterance silence threshold in milliseconds.
|
|
332
|
+
* This value must be an integer between 0 and 20_000.
|
|
333
|
+
*/
|
|
334
|
+
configureEndUtteranceSilenceThreshold(threshold) {
|
|
335
|
+
this.send(`{"end_utterance_silence_threshold":${threshold}}`);
|
|
336
|
+
}
|
|
337
|
+
send(data) {
|
|
338
|
+
if (!this.socket || this.socket.readyState !== this.socket.OPEN) {
|
|
339
|
+
throw new Error("Socket is not open for communication");
|
|
340
|
+
}
|
|
341
|
+
this.socket.send(data);
|
|
342
|
+
}
|
|
343
|
+
async close(waitForSessionTermination = true) {
|
|
344
|
+
if (this.socket) {
|
|
345
|
+
if (this.socket.readyState === this.socket.OPEN) {
|
|
346
|
+
if (waitForSessionTermination) {
|
|
347
|
+
const sessionTerminatedPromise = new Promise((resolve) => {
|
|
348
|
+
this.sessionTerminatedResolve = resolve;
|
|
349
|
+
});
|
|
350
|
+
this.socket.send(terminateSessionMessage);
|
|
351
|
+
await sessionTerminatedPromise;
|
|
352
|
+
}
|
|
353
|
+
else {
|
|
354
|
+
this.socket.send(terminateSessionMessage);
|
|
355
|
+
}
|
|
356
|
+
}
|
|
357
|
+
if (this.socket?.removeAllListeners)
|
|
358
|
+
this.socket.removeAllListeners();
|
|
359
|
+
this.socket.close();
|
|
360
|
+
}
|
|
361
|
+
this.listeners = {};
|
|
362
|
+
this.socket = undefined;
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
/**
|
|
366
|
+
* @deprecated Use RealtimeTranscriber instead
|
|
367
|
+
*/
|
|
368
|
+
class RealtimeService extends RealtimeTranscriber {
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
class RealtimeTranscriberFactory extends BaseService {
|
|
372
|
+
constructor(params) {
|
|
373
|
+
super(params);
|
|
374
|
+
this.rtFactoryParams = params;
|
|
375
|
+
}
|
|
376
|
+
/**
|
|
377
|
+
* @deprecated Use transcriber(...) instead
|
|
378
|
+
*/
|
|
379
|
+
createService(params) {
|
|
380
|
+
return this.transcriber(params);
|
|
381
|
+
}
|
|
382
|
+
transcriber(params) {
|
|
383
|
+
const serviceParams = { ...params };
|
|
384
|
+
if (!serviceParams.token && !serviceParams.apiKey) {
|
|
385
|
+
serviceParams.apiKey = this.rtFactoryParams.apiKey;
|
|
386
|
+
}
|
|
387
|
+
return new RealtimeTranscriber(serviceParams);
|
|
388
|
+
}
|
|
389
|
+
async createTemporaryToken(params) {
|
|
390
|
+
const data = await this.fetchJson("/v2/realtime/token", {
|
|
391
|
+
method: "POST",
|
|
392
|
+
body: JSON.stringify(params),
|
|
393
|
+
});
|
|
394
|
+
return data.token;
|
|
395
|
+
}
|
|
396
|
+
}
|
|
397
|
+
/**
|
|
398
|
+
* @deprecated Use RealtimeTranscriberFactory instead
|
|
399
|
+
*/
|
|
400
|
+
class RealtimeServiceFactory extends RealtimeTranscriberFactory {
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
function getPath(path) {
|
|
404
|
+
if (path.startsWith("http"))
|
|
405
|
+
return null;
|
|
406
|
+
if (path.startsWith("https"))
|
|
407
|
+
return null;
|
|
408
|
+
if (path.startsWith("data:"))
|
|
409
|
+
return null;
|
|
410
|
+
if (path.startsWith("file://"))
|
|
411
|
+
return path.substring(7);
|
|
412
|
+
if (path.startsWith("file:"))
|
|
413
|
+
return path.substring(5);
|
|
414
|
+
return path;
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
class TranscriptService extends BaseService {
|
|
418
|
+
constructor(params, files) {
|
|
419
|
+
super(params);
|
|
420
|
+
this.files = files;
|
|
421
|
+
}
|
|
422
|
+
/**
|
|
423
|
+
* Transcribe an audio file. This will create a transcript and wait until the transcript status is "completed" or "error".
|
|
424
|
+
* @param params - The parameters to transcribe an audio file.
|
|
425
|
+
* @param options - The options to transcribe an audio file.
|
|
426
|
+
* @returns A promise that resolves to the transcript. The transcript status is "completed" or "error".
|
|
427
|
+
*/
|
|
428
|
+
async transcribe(params, options) {
|
|
429
|
+
deprecateConformer2(params);
|
|
430
|
+
const transcript = await this.submit(params);
|
|
431
|
+
return await this.waitUntilReady(transcript.id, options);
|
|
432
|
+
}
|
|
433
|
+
/**
|
|
434
|
+
* Submits a transcription job for an audio file. This will not wait until the transcript status is "completed" or "error".
|
|
435
|
+
* @param params - The parameters to start the transcription of an audio file.
|
|
436
|
+
* @returns A promise that resolves to the queued transcript.
|
|
437
|
+
*/
|
|
438
|
+
async submit(params) {
|
|
439
|
+
deprecateConformer2(params);
|
|
440
|
+
let audioUrl;
|
|
441
|
+
let transcriptParams = undefined;
|
|
442
|
+
if ("audio" in params) {
|
|
443
|
+
const { audio, ...audioTranscriptParams } = params;
|
|
444
|
+
if (typeof audio === "string") {
|
|
445
|
+
const path = getPath(audio);
|
|
446
|
+
if (path !== null) {
|
|
447
|
+
// audio is local path, upload local file
|
|
448
|
+
audioUrl = await this.files.upload(path);
|
|
449
|
+
}
|
|
450
|
+
else {
|
|
451
|
+
if (audio.startsWith("data:")) {
|
|
452
|
+
audioUrl = await this.files.upload(audio);
|
|
453
|
+
}
|
|
454
|
+
else {
|
|
455
|
+
// audio is not a local path, and not a data-URI, assume it's a normal URL
|
|
456
|
+
audioUrl = audio;
|
|
457
|
+
}
|
|
458
|
+
}
|
|
459
|
+
}
|
|
460
|
+
else {
|
|
461
|
+
// audio is of uploadable type
|
|
462
|
+
audioUrl = await this.files.upload(audio);
|
|
463
|
+
}
|
|
464
|
+
transcriptParams = { ...audioTranscriptParams, audio_url: audioUrl };
|
|
465
|
+
}
|
|
466
|
+
else {
|
|
467
|
+
transcriptParams = params;
|
|
468
|
+
}
|
|
469
|
+
const data = await this.fetchJson("/v2/transcript", {
|
|
470
|
+
method: "POST",
|
|
471
|
+
body: JSON.stringify(transcriptParams),
|
|
472
|
+
});
|
|
473
|
+
return data;
|
|
474
|
+
}
|
|
475
|
+
/**
|
|
476
|
+
* Create a transcript.
|
|
477
|
+
* @param params - The parameters to create a transcript.
|
|
478
|
+
* @param options - The options used for creating the new transcript.
|
|
479
|
+
* @returns A promise that resolves to the transcript.
|
|
480
|
+
* @deprecated Use `transcribe` instead to transcribe a audio file that includes polling, or `submit` to transcribe a audio file without polling.
|
|
481
|
+
*/
|
|
482
|
+
async create(params, options) {
|
|
483
|
+
deprecateConformer2(params);
|
|
484
|
+
const path = getPath(params.audio_url);
|
|
485
|
+
if (path !== null) {
|
|
486
|
+
const uploadUrl = await this.files.upload(path);
|
|
487
|
+
params.audio_url = uploadUrl;
|
|
488
|
+
}
|
|
489
|
+
const data = await this.fetchJson("/v2/transcript", {
|
|
490
|
+
method: "POST",
|
|
491
|
+
body: JSON.stringify(params),
|
|
492
|
+
});
|
|
493
|
+
if (options?.poll ?? true) {
|
|
494
|
+
return await this.waitUntilReady(data.id, options);
|
|
495
|
+
}
|
|
496
|
+
return data;
|
|
497
|
+
}
|
|
498
|
+
/**
|
|
499
|
+
* Wait until the transcript ready, either the status is "completed" or "error".
|
|
500
|
+
* @param transcriptId - The ID of the transcript.
|
|
501
|
+
* @param options - The options to wait until the transcript is ready.
|
|
502
|
+
* @returns A promise that resolves to the transcript. The transcript status is "completed" or "error".
|
|
503
|
+
*/
|
|
504
|
+
async waitUntilReady(transcriptId, options) {
|
|
505
|
+
const pollingInterval = options?.pollingInterval ?? 3_000;
|
|
506
|
+
const pollingTimeout = options?.pollingTimeout ?? -1;
|
|
507
|
+
const startTime = Date.now();
|
|
508
|
+
// eslint-disable-next-line no-constant-condition
|
|
509
|
+
while (true) {
|
|
510
|
+
const transcript = await this.get(transcriptId);
|
|
511
|
+
if (transcript.status === "completed" || transcript.status === "error") {
|
|
512
|
+
return transcript;
|
|
513
|
+
}
|
|
514
|
+
else if (pollingTimeout > 0 &&
|
|
515
|
+
Date.now() - startTime > pollingTimeout) {
|
|
516
|
+
throw new Error("Polling timeout");
|
|
517
|
+
}
|
|
518
|
+
else {
|
|
519
|
+
await new Promise((resolve) => setTimeout(resolve, pollingInterval));
|
|
520
|
+
}
|
|
521
|
+
}
|
|
522
|
+
}
|
|
523
|
+
/**
|
|
524
|
+
* Retrieve a transcript.
|
|
525
|
+
* @param id - The identifier of the transcript.
|
|
526
|
+
* @returns A promise that resolves to the transcript.
|
|
527
|
+
*/
|
|
528
|
+
get(id) {
|
|
529
|
+
return this.fetchJson(`/v2/transcript/${id}`);
|
|
530
|
+
}
|
|
531
|
+
/**
|
|
532
|
+
* Retrieves a page of transcript listings.
|
|
533
|
+
* @param params - The parameters to filter the transcript list by, or the URL to retrieve the transcript list from.
|
|
534
|
+
*/
|
|
535
|
+
async list(params) {
|
|
536
|
+
let url = "/v2/transcript";
|
|
537
|
+
if (typeof params === "string") {
|
|
538
|
+
url = params;
|
|
539
|
+
}
|
|
540
|
+
else if (params) {
|
|
541
|
+
url = `${url}?${new URLSearchParams(Object.keys(params).map((key) => [
|
|
542
|
+
key,
|
|
543
|
+
params[key]?.toString() || "",
|
|
544
|
+
]))}`;
|
|
545
|
+
}
|
|
546
|
+
const data = await this.fetchJson(url);
|
|
547
|
+
for (const transcriptListItem of data.transcripts) {
|
|
548
|
+
transcriptListItem.created = new Date(transcriptListItem.created);
|
|
549
|
+
if (transcriptListItem.completed) {
|
|
550
|
+
transcriptListItem.completed = new Date(transcriptListItem.completed);
|
|
551
|
+
}
|
|
552
|
+
}
|
|
553
|
+
return data;
|
|
554
|
+
}
|
|
555
|
+
/**
|
|
556
|
+
* Delete a transcript
|
|
557
|
+
* @param id - The identifier of the transcript.
|
|
558
|
+
* @returns A promise that resolves to the transcript.
|
|
559
|
+
*/
|
|
560
|
+
delete(id) {
|
|
561
|
+
return this.fetchJson(`/v2/transcript/${id}`, { method: "DELETE" });
|
|
562
|
+
}
|
|
563
|
+
/**
|
|
564
|
+
* Search through the transcript for a specific set of keywords.
|
|
565
|
+
* You can search for individual words, numbers, or phrases containing up to five words or numbers.
|
|
566
|
+
* @param id - The identifier of the transcript.
|
|
567
|
+
* @param words - Keywords to search for.
|
|
568
|
+
* @returns A promise that resolves to the sentences.
|
|
569
|
+
*/
|
|
570
|
+
wordSearch(id, words) {
|
|
571
|
+
const params = new URLSearchParams({ words: words.join(",") });
|
|
572
|
+
return this.fetchJson(`/v2/transcript/${id}/word-search?${params.toString()}`);
|
|
573
|
+
}
|
|
574
|
+
/**
|
|
575
|
+
* Retrieve all sentences of a transcript.
|
|
576
|
+
* @param id - The identifier of the transcript.
|
|
577
|
+
* @returns A promise that resolves to the sentences.
|
|
578
|
+
*/
|
|
579
|
+
sentences(id) {
|
|
580
|
+
return this.fetchJson(`/v2/transcript/${id}/sentences`);
|
|
581
|
+
}
|
|
582
|
+
/**
|
|
583
|
+
* Retrieve all paragraphs of a transcript.
|
|
584
|
+
* @param id - The identifier of the transcript.
|
|
585
|
+
* @returns A promise that resolves to the paragraphs.
|
|
586
|
+
*/
|
|
587
|
+
paragraphs(id) {
|
|
588
|
+
return this.fetchJson(`/v2/transcript/${id}/paragraphs`);
|
|
589
|
+
}
|
|
590
|
+
/**
|
|
591
|
+
* Retrieve subtitles of a transcript.
|
|
592
|
+
* @param id - The identifier of the transcript.
|
|
593
|
+
* @param format - The format of the subtitles.
|
|
594
|
+
* @param chars_per_caption - The maximum number of characters per caption.
|
|
595
|
+
* @returns A promise that resolves to the subtitles text.
|
|
596
|
+
*/
|
|
597
|
+
async subtitles(id, format = "srt", chars_per_caption) {
|
|
598
|
+
let url = `/v2/transcript/${id}/${format}`;
|
|
599
|
+
if (chars_per_caption) {
|
|
600
|
+
const params = new URLSearchParams();
|
|
601
|
+
params.set("chars_per_caption", chars_per_caption.toString());
|
|
602
|
+
url += `?${params.toString()}`;
|
|
603
|
+
}
|
|
604
|
+
const response = await this.fetch(url);
|
|
605
|
+
return await response.text();
|
|
606
|
+
}
|
|
607
|
+
/**
|
|
608
|
+
* Retrieve the redacted audio URL of a transcript.
|
|
609
|
+
* @param id - The identifier of the transcript.
|
|
610
|
+
* @returns A promise that resolves to the details of the redacted audio.
|
|
611
|
+
* @deprecated Use `redactedAudio` instead.
|
|
612
|
+
*/
|
|
613
|
+
redactions(id) {
|
|
614
|
+
return this.redactedAudio(id);
|
|
615
|
+
}
|
|
616
|
+
/**
|
|
617
|
+
* Retrieve the redacted audio URL of a transcript.
|
|
618
|
+
* @param id - The identifier of the transcript.
|
|
619
|
+
* @returns A promise that resolves to the details of the redacted audio.
|
|
620
|
+
*/
|
|
621
|
+
redactedAudio(id) {
|
|
622
|
+
return this.fetchJson(`/v2/transcript/${id}/redacted-audio`);
|
|
623
|
+
}
|
|
624
|
+
/**
|
|
625
|
+
* Retrieve the redacted audio file of a transcript.
|
|
626
|
+
* @param id - The identifier of the transcript.
|
|
627
|
+
* @returns A promise that resolves to the fetch HTTP response of the redacted audio file.
|
|
628
|
+
*/
|
|
629
|
+
async redactedAudioFile(id) {
|
|
630
|
+
const { redacted_audio_url, status } = await this.redactedAudio(id);
|
|
631
|
+
if (status !== "redacted_audio_ready") {
|
|
632
|
+
throw new Error(`Redacted audio status is ${status}`);
|
|
633
|
+
}
|
|
634
|
+
const response = await fetch(redacted_audio_url);
|
|
635
|
+
if (!response.ok) {
|
|
636
|
+
throw new Error(`Failed to fetch redacted audio: ${response.statusText}`);
|
|
637
|
+
}
|
|
638
|
+
return {
|
|
639
|
+
arrayBuffer: response.arrayBuffer.bind(response),
|
|
640
|
+
blob: response.blob.bind(response),
|
|
641
|
+
body: response.body,
|
|
642
|
+
bodyUsed: response.bodyUsed,
|
|
643
|
+
};
|
|
644
|
+
}
|
|
645
|
+
}
|
|
646
|
+
function deprecateConformer2(params) {
|
|
647
|
+
if (!params)
|
|
648
|
+
return;
|
|
649
|
+
if (params.speech_model === "conformer-2") {
|
|
650
|
+
console.warn("The speech_model conformer-2 option is deprecated and will stop working in the near future. Use best or nano instead.");
|
|
651
|
+
}
|
|
652
|
+
}
|
|
653
|
+
|
|
654
|
+
const readFile = async function (
|
|
655
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
656
|
+
path) {
|
|
657
|
+
throw new Error("Interacting with the file system is not supported in this environment.");
|
|
658
|
+
};
|
|
659
|
+
|
|
660
|
+
class FileService extends BaseService {
|
|
661
|
+
/**
|
|
662
|
+
* Upload a local file to AssemblyAI.
|
|
663
|
+
* @param input - The local file path to upload, or a stream or buffer of the file to upload.
|
|
664
|
+
* @returns A promise that resolves to the uploaded file URL.
|
|
665
|
+
*/
|
|
666
|
+
async upload(input) {
|
|
667
|
+
let fileData;
|
|
668
|
+
if (typeof input === "string") {
|
|
669
|
+
if (input.startsWith("data:")) {
|
|
670
|
+
fileData = dataUrlToBlob(input);
|
|
671
|
+
}
|
|
672
|
+
else {
|
|
673
|
+
fileData = await readFile();
|
|
674
|
+
}
|
|
675
|
+
}
|
|
676
|
+
else
|
|
677
|
+
fileData = input;
|
|
678
|
+
const data = await this.fetchJson("/v2/upload", {
|
|
679
|
+
method: "POST",
|
|
680
|
+
body: fileData,
|
|
681
|
+
headers: {
|
|
682
|
+
"Content-Type": "application/octet-stream",
|
|
683
|
+
},
|
|
684
|
+
duplex: "half",
|
|
685
|
+
});
|
|
686
|
+
return data.upload_url;
|
|
687
|
+
}
|
|
688
|
+
}
|
|
689
|
+
function dataUrlToBlob(dataUrl) {
|
|
690
|
+
const arr = dataUrl.split(",");
|
|
691
|
+
const mime = arr[0].match(/:(.*?);/)[1];
|
|
692
|
+
const bstr = atob(arr[1]);
|
|
693
|
+
let n = bstr.length;
|
|
694
|
+
const u8arr = new Uint8Array(n);
|
|
695
|
+
while (n--) {
|
|
696
|
+
u8arr[n] = bstr.charCodeAt(n);
|
|
697
|
+
}
|
|
698
|
+
return new Blob([u8arr], { type: mime });
|
|
699
|
+
}
|
|
700
|
+
|
|
701
|
+
const defaultBaseUrl = "https://api.assemblyai.com";
|
|
702
|
+
class AssemblyAI {
|
|
703
|
+
/**
|
|
704
|
+
* Create a new AssemblyAI client.
|
|
705
|
+
* @param params - The parameters for the service, including the API key and base URL, if any.
|
|
706
|
+
*/
|
|
707
|
+
constructor(params) {
|
|
708
|
+
params.baseUrl = params.baseUrl || defaultBaseUrl;
|
|
709
|
+
if (params.baseUrl && params.baseUrl.endsWith("/")) {
|
|
710
|
+
params.baseUrl = params.baseUrl.slice(0, -1);
|
|
711
|
+
}
|
|
712
|
+
this.files = new FileService(params);
|
|
713
|
+
this.transcripts = new TranscriptService(params, this.files);
|
|
714
|
+
this.lemur = new LemurService(params);
|
|
715
|
+
this.realtime = new RealtimeTranscriberFactory(params);
|
|
716
|
+
}
|
|
717
|
+
}
|
|
718
|
+
|
|
719
|
+
export { AssemblyAI, FileService, LemurService, RealtimeService, RealtimeServiceFactory, RealtimeTranscriber, RealtimeTranscriberFactory, TranscriptService };
|