assemblyai 3.0.0 → 3.0.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/dist/index.esm.js +102 -95
- package/dist/index.js +103 -96
- package/dist/services/base.d.ts +6 -4
- package/dist/services/files/index.d.ts +2 -2
- package/dist/services/index.d.ts +1 -1
- package/dist/services/lemur/index.d.ts +2 -2
- package/dist/services/realtime/factory.d.ts +5 -6
- package/dist/services/realtime/index.d.ts +2 -2
- package/dist/services/realtime/service.d.ts +2 -3
- package/dist/services/transcripts/index.d.ts +4 -5
- package/dist/types/files/index.d.ts +1 -2
- package/dist/types/openapi.generated.d.ts +24 -15
- package/package.json +2 -2
- package/src/services/base.ts +44 -3
- package/src/services/files/index.ts +10 -12
- package/src/services/index.ts +10 -8
- package/src/services/lemur/index.ts +28 -27
- package/src/services/realtime/factory.ts +17 -12
- package/src/services/realtime/index.ts +2 -2
- package/src/services/realtime/service.ts +5 -6
- package/src/services/transcripts/index.ts +35 -46
- package/src/types/files/index.ts +2 -1
- package/src/types/openapi.generated.ts +25 -15
- package/dist/utils/axios.d.ts +0 -3
- package/src/utils/axios.ts +0 -19
package/dist/index.esm.js
CHANGED
|
@@ -1,24 +1,7 @@
|
|
|
1
|
-
import axios, { isAxiosError } from 'axios';
|
|
2
1
|
import WebSocket from 'ws';
|
|
3
|
-
import
|
|
2
|
+
import Stream from 'stream';
|
|
4
3
|
import fs from 'fs';
|
|
5
4
|
|
|
6
|
-
function createAxiosClient(params) {
|
|
7
|
-
const client = axios.create({
|
|
8
|
-
baseURL: params.baseUrl,
|
|
9
|
-
headers: { Authorization: params.apiKey },
|
|
10
|
-
});
|
|
11
|
-
client.interceptors.response.use(undefined, throwApiError);
|
|
12
|
-
return client;
|
|
13
|
-
}
|
|
14
|
-
function throwApiError(error) {
|
|
15
|
-
var _a, _b;
|
|
16
|
-
if (isAxiosError(error) && ((_b = (_a = error.response) === null || _a === void 0 ? void 0 : _a.data) === null || _b === void 0 ? void 0 : _b.error)) {
|
|
17
|
-
return Promise.reject(new Error(error.response.data.error));
|
|
18
|
-
}
|
|
19
|
-
return Promise.reject(error);
|
|
20
|
-
}
|
|
21
|
-
|
|
22
5
|
/******************************************************************************
|
|
23
6
|
Copyright (c) Microsoft Corporation.
|
|
24
7
|
|
|
@@ -57,36 +40,70 @@ typeof SuppressedError === "function" ? SuppressedError : function (error, suppr
|
|
|
57
40
|
class BaseService {
|
|
58
41
|
/**
|
|
59
42
|
* Create a new service.
|
|
60
|
-
* @param params The
|
|
43
|
+
* @param params The parameters to use for the service.
|
|
61
44
|
*/
|
|
62
|
-
constructor(
|
|
63
|
-
this.
|
|
45
|
+
constructor(params) {
|
|
46
|
+
this.params = params;
|
|
47
|
+
}
|
|
48
|
+
fetch(input, init) {
|
|
49
|
+
var _a;
|
|
50
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
51
|
+
init = init !== null && init !== void 0 ? init : {};
|
|
52
|
+
init.headers = (_a = init.headers) !== null && _a !== void 0 ? _a : {};
|
|
53
|
+
init.headers = Object.assign({ Authorization: this.params.apiKey, "Content-Type": "application/json" }, init.headers);
|
|
54
|
+
if (!input.startsWith("http"))
|
|
55
|
+
input = this.params.baseUrl + input;
|
|
56
|
+
const response = yield fetch(input, init);
|
|
57
|
+
if (response.status >= 400) {
|
|
58
|
+
let json;
|
|
59
|
+
const text = yield response.text();
|
|
60
|
+
if (text) {
|
|
61
|
+
try {
|
|
62
|
+
json = JSON.parse(text);
|
|
63
|
+
}
|
|
64
|
+
catch (_b) {
|
|
65
|
+
/* empty */
|
|
66
|
+
}
|
|
67
|
+
if (json === null || json === void 0 ? void 0 : json.error)
|
|
68
|
+
throw new Error(json.error);
|
|
69
|
+
throw new Error(text);
|
|
70
|
+
}
|
|
71
|
+
throw new Error(`HTTP Error: ${response.status} ${response.statusText}`);
|
|
72
|
+
}
|
|
73
|
+
return response;
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
fetchJson(input, init) {
|
|
77
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
78
|
+
const response = yield this.fetch(input, init);
|
|
79
|
+
return response.json();
|
|
80
|
+
});
|
|
64
81
|
}
|
|
65
82
|
}
|
|
66
83
|
|
|
67
84
|
class LemurService extends BaseService {
|
|
68
85
|
summary(params) {
|
|
69
|
-
return
|
|
70
|
-
|
|
71
|
-
|
|
86
|
+
return this.fetchJson("/lemur/v3/generate/summary", {
|
|
87
|
+
method: "POST",
|
|
88
|
+
body: JSON.stringify(params),
|
|
72
89
|
});
|
|
73
90
|
}
|
|
74
91
|
questionAnswer(params) {
|
|
75
|
-
return
|
|
76
|
-
|
|
77
|
-
|
|
92
|
+
return this.fetchJson("/lemur/v3/generate/question-answer", {
|
|
93
|
+
method: "POST",
|
|
94
|
+
body: JSON.stringify(params),
|
|
78
95
|
});
|
|
79
96
|
}
|
|
80
97
|
actionItems(params) {
|
|
81
|
-
return
|
|
82
|
-
|
|
83
|
-
|
|
98
|
+
return this.fetchJson("/lemur/v3/generate/action-items", {
|
|
99
|
+
method: "POST",
|
|
100
|
+
body: JSON.stringify(params),
|
|
84
101
|
});
|
|
85
102
|
}
|
|
86
103
|
task(params) {
|
|
87
|
-
return
|
|
88
|
-
|
|
89
|
-
|
|
104
|
+
return this.fetchJson("/lemur/v3/generate/task", {
|
|
105
|
+
method: "POST",
|
|
106
|
+
body: JSON.stringify(params),
|
|
90
107
|
});
|
|
91
108
|
}
|
|
92
109
|
/**
|
|
@@ -94,9 +111,8 @@ class LemurService extends BaseService {
|
|
|
94
111
|
* @param id ID of the LeMUR request
|
|
95
112
|
*/
|
|
96
113
|
purgeRequestData(id) {
|
|
97
|
-
return
|
|
98
|
-
|
|
99
|
-
return data;
|
|
114
|
+
return this.fetchJson(`/lemur/v3/${id}`, {
|
|
115
|
+
method: "DELETE",
|
|
100
116
|
});
|
|
101
117
|
}
|
|
102
118
|
}
|
|
@@ -145,12 +161,11 @@ class RealtimeError extends Error {
|
|
|
145
161
|
const defaultRealtimeUrl = "wss://api.assemblyai.com/v2/realtime/ws";
|
|
146
162
|
class RealtimeService {
|
|
147
163
|
constructor(params) {
|
|
148
|
-
var _a, _b
|
|
164
|
+
var _a, _b;
|
|
149
165
|
this.listeners = {};
|
|
150
166
|
this.realtimeUrl = (_a = params.realtimeUrl) !== null && _a !== void 0 ? _a : defaultRealtimeUrl;
|
|
151
167
|
this.sampleRate = (_b = params.sampleRate) !== null && _b !== void 0 ? _b : 16000;
|
|
152
168
|
this.wordBoost = params.wordBoost;
|
|
153
|
-
this.realtimeUrl = (_c = params.realtimeUrl) !== null && _c !== void 0 ? _c : defaultRealtimeUrl;
|
|
154
169
|
if ("apiKey" in params)
|
|
155
170
|
this.apiKey = params.apiKey;
|
|
156
171
|
if ("token" in params)
|
|
@@ -258,7 +273,7 @@ class RealtimeService {
|
|
|
258
273
|
this.socket.send(JSON.stringify(payload));
|
|
259
274
|
}
|
|
260
275
|
stream() {
|
|
261
|
-
const stream = new Writable({
|
|
276
|
+
const stream = new Stream.Writable({
|
|
262
277
|
write: (chunk, encoding, next) => {
|
|
263
278
|
this.sendAudio(chunk);
|
|
264
279
|
next();
|
|
@@ -291,30 +306,33 @@ class RealtimeService {
|
|
|
291
306
|
}
|
|
292
307
|
}
|
|
293
308
|
|
|
294
|
-
class RealtimeServiceFactory {
|
|
295
|
-
constructor(
|
|
296
|
-
|
|
297
|
-
this.
|
|
309
|
+
class RealtimeServiceFactory extends BaseService {
|
|
310
|
+
constructor(params) {
|
|
311
|
+
super(params);
|
|
312
|
+
this.rtFactoryParams = params;
|
|
298
313
|
}
|
|
299
314
|
createService(params) {
|
|
300
315
|
if (!params)
|
|
301
|
-
params = { apiKey: this.
|
|
316
|
+
params = { apiKey: this.rtFactoryParams.apiKey };
|
|
302
317
|
else if (!("token" in params) && !params.apiKey) {
|
|
303
|
-
params.apiKey = this.
|
|
318
|
+
params.apiKey = this.rtFactoryParams.apiKey;
|
|
304
319
|
}
|
|
305
320
|
return new RealtimeService(params);
|
|
306
321
|
}
|
|
307
322
|
createTemporaryToken(params) {
|
|
308
323
|
return __awaiter(this, void 0, void 0, function* () {
|
|
309
|
-
const
|
|
310
|
-
|
|
324
|
+
const data = yield this.fetchJson("/v2/realtime/token", {
|
|
325
|
+
method: "POST",
|
|
326
|
+
body: JSON.stringify(params),
|
|
327
|
+
});
|
|
328
|
+
return data.token;
|
|
311
329
|
});
|
|
312
330
|
}
|
|
313
331
|
}
|
|
314
332
|
|
|
315
333
|
class TranscriptService extends BaseService {
|
|
316
|
-
constructor(
|
|
317
|
-
super(
|
|
334
|
+
constructor(params, files) {
|
|
335
|
+
super(params);
|
|
318
336
|
this.files = files;
|
|
319
337
|
}
|
|
320
338
|
/**
|
|
@@ -331,11 +349,14 @@ class TranscriptService extends BaseService {
|
|
|
331
349
|
const uploadUrl = yield this.files.upload(path);
|
|
332
350
|
params.audio_url = uploadUrl;
|
|
333
351
|
}
|
|
334
|
-
const
|
|
352
|
+
const data = yield this.fetchJson("/v2/transcript", {
|
|
353
|
+
method: "POST",
|
|
354
|
+
body: JSON.stringify(params),
|
|
355
|
+
});
|
|
335
356
|
if ((_a = options === null || options === void 0 ? void 0 : options.poll) !== null && _a !== void 0 ? _a : true) {
|
|
336
|
-
return yield this.poll(
|
|
357
|
+
return yield this.poll(data.id, options);
|
|
337
358
|
}
|
|
338
|
-
return
|
|
359
|
+
return data;
|
|
339
360
|
});
|
|
340
361
|
}
|
|
341
362
|
poll(transcriptId, options) {
|
|
@@ -364,10 +385,7 @@ class TranscriptService extends BaseService {
|
|
|
364
385
|
* @returns A promise that resolves to the transcript.
|
|
365
386
|
*/
|
|
366
387
|
get(id) {
|
|
367
|
-
return
|
|
368
|
-
const res = yield this.client.get(`/v2/transcript/${id}`);
|
|
369
|
-
return res.data;
|
|
370
|
-
});
|
|
388
|
+
return this.fetchJson(`/v2/transcript/${id}`);
|
|
371
389
|
}
|
|
372
390
|
/**
|
|
373
391
|
* Retrieves a page of transcript listings.
|
|
@@ -376,16 +394,19 @@ class TranscriptService extends BaseService {
|
|
|
376
394
|
list(parameters) {
|
|
377
395
|
return __awaiter(this, void 0, void 0, function* () {
|
|
378
396
|
let url = "/v2/transcript";
|
|
379
|
-
let query;
|
|
380
397
|
if (typeof parameters === "string") {
|
|
381
398
|
url = parameters;
|
|
382
399
|
}
|
|
383
400
|
else if (parameters) {
|
|
384
|
-
|
|
401
|
+
url = `${url}?${new URLSearchParams(Object.keys(parameters).map((key) => {
|
|
402
|
+
var _a;
|
|
403
|
+
return [
|
|
404
|
+
key,
|
|
405
|
+
((_a = parameters[key]) === null || _a === void 0 ? void 0 : _a.toString()) || "",
|
|
406
|
+
];
|
|
407
|
+
}))}`;
|
|
385
408
|
}
|
|
386
|
-
const
|
|
387
|
-
params: query,
|
|
388
|
-
});
|
|
409
|
+
const data = yield this.fetchJson(url);
|
|
389
410
|
for (const transcriptListItem of data.transcripts) {
|
|
390
411
|
transcriptListItem.created = new Date(transcriptListItem.created);
|
|
391
412
|
if (transcriptListItem.completed) {
|
|
@@ -401,27 +422,17 @@ class TranscriptService extends BaseService {
|
|
|
401
422
|
* @returns A promise that resolves to the transcript.
|
|
402
423
|
*/
|
|
403
424
|
delete(id) {
|
|
404
|
-
return
|
|
405
|
-
const res = yield this.client.delete(`/v2/transcript/${id}`);
|
|
406
|
-
return res.data;
|
|
407
|
-
});
|
|
425
|
+
return this.fetchJson(`/v2/transcript/${id}`, { method: "DELETE" });
|
|
408
426
|
}
|
|
409
427
|
/**
|
|
410
428
|
* Search through the transcript for a specific set of keywords.
|
|
411
429
|
* You can search for individual words, numbers, or phrases containing up to five words or numbers.
|
|
412
430
|
* @param id The identifier of the transcript.
|
|
413
|
-
* @param
|
|
431
|
+
* @param words Keywords to search for.
|
|
414
432
|
* @return A promise that resolves to the sentences.
|
|
415
433
|
*/
|
|
416
434
|
wordSearch(id, words) {
|
|
417
|
-
return
|
|
418
|
-
const { data } = yield this.client.get(`/v2/transcript/${id}/word-search`, {
|
|
419
|
-
params: {
|
|
420
|
-
words: JSON.stringify(words),
|
|
421
|
-
},
|
|
422
|
-
});
|
|
423
|
-
return data;
|
|
424
|
-
});
|
|
435
|
+
return this.fetchJson(`/v2/transcript/${id}/word-search?words=${JSON.stringify(words)}`);
|
|
425
436
|
}
|
|
426
437
|
/**
|
|
427
438
|
* Retrieve all sentences of a transcript.
|
|
@@ -429,10 +440,7 @@ class TranscriptService extends BaseService {
|
|
|
429
440
|
* @return A promise that resolves to the sentences.
|
|
430
441
|
*/
|
|
431
442
|
sentences(id) {
|
|
432
|
-
return
|
|
433
|
-
const { data } = yield this.client.get(`/v2/transcript/${id}/sentences`);
|
|
434
|
-
return data;
|
|
435
|
-
});
|
|
443
|
+
return this.fetchJson(`/v2/transcript/${id}/sentences`);
|
|
436
444
|
}
|
|
437
445
|
/**
|
|
438
446
|
* Retrieve all paragraphs of a transcript.
|
|
@@ -440,10 +448,7 @@ class TranscriptService extends BaseService {
|
|
|
440
448
|
* @return A promise that resolves to the paragraphs.
|
|
441
449
|
*/
|
|
442
450
|
paragraphs(id) {
|
|
443
|
-
return
|
|
444
|
-
const { data } = yield this.client.get(`/v2/transcript/${id}/paragraphs`);
|
|
445
|
-
return data;
|
|
446
|
-
});
|
|
451
|
+
return this.fetchJson(`/v2/transcript/${id}/paragraphs`);
|
|
447
452
|
}
|
|
448
453
|
/**
|
|
449
454
|
* Retrieve subtitles of a transcript.
|
|
@@ -453,8 +458,8 @@ class TranscriptService extends BaseService {
|
|
|
453
458
|
*/
|
|
454
459
|
subtitles(id, format = "srt") {
|
|
455
460
|
return __awaiter(this, void 0, void 0, function* () {
|
|
456
|
-
const
|
|
457
|
-
return
|
|
461
|
+
const response = yield this.fetch(`/v2/transcript/${id}/${format}`);
|
|
462
|
+
return yield response.text();
|
|
458
463
|
});
|
|
459
464
|
}
|
|
460
465
|
/**
|
|
@@ -463,10 +468,7 @@ class TranscriptService extends BaseService {
|
|
|
463
468
|
* @return A promise that resolves to the subtitles text.
|
|
464
469
|
*/
|
|
465
470
|
redactions(id) {
|
|
466
|
-
return
|
|
467
|
-
const { data } = yield this.client.get(`/v2/transcript/${id}/redacted-audio`);
|
|
468
|
-
return data;
|
|
469
|
-
});
|
|
471
|
+
return this.fetchJson(`/v2/transcript/${id}/redacted-audio`);
|
|
470
472
|
}
|
|
471
473
|
}
|
|
472
474
|
function getPath(path) {
|
|
@@ -496,28 +498,33 @@ class FileService extends BaseService {
|
|
|
496
498
|
fileData = fs.createReadStream(input);
|
|
497
499
|
else
|
|
498
500
|
fileData = input;
|
|
499
|
-
const
|
|
501
|
+
const data = yield this.fetchJson("/v2/upload", {
|
|
502
|
+
method: "POST",
|
|
503
|
+
body: fileData,
|
|
500
504
|
headers: {
|
|
501
505
|
"Content-Type": "application/octet-stream",
|
|
502
506
|
},
|
|
507
|
+
duplex: "half",
|
|
503
508
|
});
|
|
504
509
|
return data.upload_url;
|
|
505
510
|
});
|
|
506
511
|
}
|
|
507
512
|
}
|
|
508
513
|
|
|
514
|
+
const defaultBaseUrl = "https://api.assemblyai.com";
|
|
509
515
|
class AssemblyAI {
|
|
510
516
|
/**
|
|
511
517
|
* Create a new AssemblyAI client.
|
|
512
518
|
* @param params The parameters for the service, including the API key and base URL, if any.
|
|
513
519
|
*/
|
|
514
520
|
constructor(params) {
|
|
515
|
-
params.baseUrl = params.baseUrl ||
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
this.
|
|
519
|
-
this.
|
|
520
|
-
this.
|
|
521
|
+
params.baseUrl = params.baseUrl || defaultBaseUrl;
|
|
522
|
+
if (params.baseUrl && params.baseUrl.endsWith("/"))
|
|
523
|
+
params.baseUrl = params.baseUrl.slice(0, -1);
|
|
524
|
+
this.files = new FileService(params);
|
|
525
|
+
this.transcripts = new TranscriptService(params, this.files);
|
|
526
|
+
this.lemur = new LemurService(params);
|
|
527
|
+
this.realtime = new RealtimeServiceFactory(params);
|
|
521
528
|
}
|
|
522
529
|
}
|
|
523
530
|
|