assemblyai 1.0.1 → 2.0.1-beta
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/LICENSE +21 -0
- package/README.md +192 -83
- package/dist/index.d.ts +4 -0
- package/dist/index.esm.js +473 -0
- package/dist/index.js +482 -0
- package/dist/services/base.d.ts +13 -0
- package/dist/services/files/index.d.ts +9 -0
- package/dist/services/index.d.ts +29 -0
- package/dist/services/lemur/index.d.ts +8 -0
- package/dist/services/realtime/factory.d.ts +10 -0
- package/dist/services/realtime/index.d.ts +2 -0
- package/dist/services/realtime/service.d.ts +22 -0
- package/dist/services/transcripts/index.d.ts +59 -0
- package/dist/types/asyncapi.generated.d.ts +87 -0
- package/dist/types/index.d.ts +5 -0
- package/dist/types/openapi.generated.d.ts +685 -0
- package/dist/types/realtime/index.d.ts +36 -0
- package/dist/types/services/abstractions.d.ts +52 -0
- package/dist/types/services/index.d.ts +6 -0
- package/dist/types/transcripts/index.d.ts +5 -0
- package/dist/utils/axios.d.ts +3 -0
- package/dist/utils/errors/index.d.ts +1 -0
- package/dist/utils/errors/realtime.d.ts +23 -0
- package/package.json +58 -21
- package/src/index.ts +5 -0
- package/src/services/base.ts +14 -0
- package/src/services/files/index.ts +22 -0
- package/src/services/index.ts +49 -0
- package/src/services/lemur/index.ts +49 -0
- package/src/services/realtime/factory.ts +32 -0
- package/src/services/realtime/index.ts +2 -0
- package/src/services/realtime/service.ts +184 -0
- package/src/services/transcripts/index.ts +178 -0
- package/src/types/asyncapi.generated.ts +124 -0
- package/src/types/index.ts +5 -0
- package/src/types/openapi.generated.ts +834 -0
- package/src/types/realtime/index.ts +68 -0
- package/src/types/services/abstractions.ts +56 -0
- package/src/types/services/index.ts +7 -0
- package/src/types/transcripts/index.ts +5 -0
- package/src/utils/.gitkeep +0 -0
- package/src/utils/axios.ts +19 -0
- package/src/utils/errors/index.ts +5 -0
- package/src/utils/errors/realtime.ts +45 -0
- package/.eslintrc.json +0 -3
- package/index.js +0 -15
- package/src/Client.js +0 -28
- package/src/api/Http/Request.js +0 -108
- package/src/api/Http/Response.js +0 -23
- package/src/api/Model.js +0 -17
- package/src/api/Transcript.js +0 -16
- package/src/api/Upload.js +0 -41
- package/src/api/util.js +0 -48
|
@@ -0,0 +1,473 @@
|
|
|
1
|
+
import axios, { isAxiosError } from 'axios';
|
|
2
|
+
import WebSocket from 'ws';
|
|
3
|
+
import { readFile } from 'fs/promises';
|
|
4
|
+
|
|
5
|
+
function createAxiosClient(params) {
|
|
6
|
+
const client = axios.create({
|
|
7
|
+
baseURL: params.baseUrl,
|
|
8
|
+
headers: { Authorization: params.apiKey },
|
|
9
|
+
});
|
|
10
|
+
client.interceptors.response.use(undefined, throwApiError);
|
|
11
|
+
return client;
|
|
12
|
+
}
|
|
13
|
+
function throwApiError(error) {
|
|
14
|
+
var _a, _b;
|
|
15
|
+
if (isAxiosError(error) && ((_b = (_a = error.response) === null || _a === void 0 ? void 0 : _a.data) === null || _b === void 0 ? void 0 : _b.error)) {
|
|
16
|
+
return Promise.reject(new Error(error.response.data.error));
|
|
17
|
+
}
|
|
18
|
+
return Promise.reject(error);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/******************************************************************************
|
|
22
|
+
Copyright (c) Microsoft Corporation.
|
|
23
|
+
|
|
24
|
+
Permission to use, copy, modify, and/or distribute this software for any
|
|
25
|
+
purpose with or without fee is hereby granted.
|
|
26
|
+
|
|
27
|
+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
|
28
|
+
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
|
29
|
+
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
|
30
|
+
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
|
31
|
+
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
|
32
|
+
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
33
|
+
PERFORMANCE OF THIS SOFTWARE.
|
|
34
|
+
***************************************************************************** */
|
|
35
|
+
/* global Reflect, Promise, SuppressedError, Symbol */
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
function __awaiter(thisArg, _arguments, P, generator) {
|
|
39
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
40
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
41
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
42
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
43
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
44
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
|
|
49
|
+
var e = new Error(message);
|
|
50
|
+
return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Base class for services that communicate with the API.
|
|
55
|
+
*/
|
|
56
|
+
class BaseService {
|
|
57
|
+
/**
|
|
58
|
+
* Create a new service.
|
|
59
|
+
* @param params The AxiosInstance to send HTTP requests to the API.
|
|
60
|
+
*/
|
|
61
|
+
constructor(client) {
|
|
62
|
+
this.client = client;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
class LemurService extends BaseService {
|
|
67
|
+
summary(params) {
|
|
68
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
69
|
+
const { data } = yield this.client.post("/lemur/v3/generate/summary", params);
|
|
70
|
+
return data;
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
questionAnswer(params) {
|
|
74
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
75
|
+
const { data } = yield this.client.post("/lemur/v3/generate/question-answer", params);
|
|
76
|
+
return data;
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
actionItems(params) {
|
|
80
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
81
|
+
const { data } = yield this.client.post("/lemur/v3/generate/action-items", params);
|
|
82
|
+
return data;
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
task(params) {
|
|
86
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
87
|
+
const { data } = yield this.client.post("/lemur/v3/generate/task", params);
|
|
88
|
+
return data;
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
var RealtimeErrorType;
|
|
94
|
+
(function (RealtimeErrorType) {
|
|
95
|
+
RealtimeErrorType[RealtimeErrorType["BadSampleRate"] = 4000] = "BadSampleRate";
|
|
96
|
+
RealtimeErrorType[RealtimeErrorType["AuthFailed"] = 4001] = "AuthFailed";
|
|
97
|
+
// Both InsufficientFunds and FreeAccount error use 4002
|
|
98
|
+
RealtimeErrorType[RealtimeErrorType["InsufficientFundsOrFreeAccount"] = 4002] = "InsufficientFundsOrFreeAccount";
|
|
99
|
+
RealtimeErrorType[RealtimeErrorType["NonexistentSessionId"] = 4004] = "NonexistentSessionId";
|
|
100
|
+
RealtimeErrorType[RealtimeErrorType["SessionExpired"] = 4008] = "SessionExpired";
|
|
101
|
+
RealtimeErrorType[RealtimeErrorType["ClosedSession"] = 4010] = "ClosedSession";
|
|
102
|
+
RealtimeErrorType[RealtimeErrorType["RateLimited"] = 4029] = "RateLimited";
|
|
103
|
+
RealtimeErrorType[RealtimeErrorType["UniqueSessionViolation"] = 4030] = "UniqueSessionViolation";
|
|
104
|
+
RealtimeErrorType[RealtimeErrorType["SessionTimeout"] = 4031] = "SessionTimeout";
|
|
105
|
+
RealtimeErrorType[RealtimeErrorType["AudioTooShort"] = 4032] = "AudioTooShort";
|
|
106
|
+
RealtimeErrorType[RealtimeErrorType["AudioTooLong"] = 4033] = "AudioTooLong";
|
|
107
|
+
RealtimeErrorType[RealtimeErrorType["BadJson"] = 4100] = "BadJson";
|
|
108
|
+
RealtimeErrorType[RealtimeErrorType["BadSchema"] = 4101] = "BadSchema";
|
|
109
|
+
RealtimeErrorType[RealtimeErrorType["TooManyStreams"] = 4102] = "TooManyStreams";
|
|
110
|
+
RealtimeErrorType[RealtimeErrorType["Reconnected"] = 4103] = "Reconnected";
|
|
111
|
+
RealtimeErrorType[RealtimeErrorType["ReconnectAttemptsExhausted"] = 1013] = "ReconnectAttemptsExhausted";
|
|
112
|
+
})(RealtimeErrorType || (RealtimeErrorType = {}));
|
|
113
|
+
const RealtimeErrorMessages = {
|
|
114
|
+
[RealtimeErrorType.BadSampleRate]: "Sample rate must be a positive integer",
|
|
115
|
+
[RealtimeErrorType.AuthFailed]: "Not Authorized",
|
|
116
|
+
[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.",
|
|
117
|
+
[RealtimeErrorType.NonexistentSessionId]: "Session ID does not exist",
|
|
118
|
+
[RealtimeErrorType.SessionExpired]: "Session has expired",
|
|
119
|
+
[RealtimeErrorType.ClosedSession]: "Session is closed",
|
|
120
|
+
[RealtimeErrorType.RateLimited]: "Rate limited",
|
|
121
|
+
[RealtimeErrorType.UniqueSessionViolation]: "Unique session violation",
|
|
122
|
+
[RealtimeErrorType.SessionTimeout]: "Session Timeout",
|
|
123
|
+
[RealtimeErrorType.AudioTooShort]: "Audio too short",
|
|
124
|
+
[RealtimeErrorType.AudioTooLong]: "Audio too long",
|
|
125
|
+
[RealtimeErrorType.BadJson]: "Bad JSON",
|
|
126
|
+
[RealtimeErrorType.BadSchema]: "Bad schema",
|
|
127
|
+
[RealtimeErrorType.TooManyStreams]: "Too many streams",
|
|
128
|
+
[RealtimeErrorType.Reconnected]: "Reconnected",
|
|
129
|
+
[RealtimeErrorType.ReconnectAttemptsExhausted]: "Reconnect attempts exhausted",
|
|
130
|
+
};
|
|
131
|
+
class RealtimeError extends Error {
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
const defaultRealtimeUrl = "wss://api.assemblyai.com/v2/realtime/ws";
|
|
135
|
+
class RealtimeService {
|
|
136
|
+
constructor(params) {
|
|
137
|
+
var _a, _b, _c;
|
|
138
|
+
this.listeners = {};
|
|
139
|
+
this.realtimeUrl = (_a = params.realtimeUrl) !== null && _a !== void 0 ? _a : defaultRealtimeUrl;
|
|
140
|
+
this.sampleRate = (_b = params.sampleRate) !== null && _b !== void 0 ? _b : 16000;
|
|
141
|
+
this.wordBoost = params.wordBoost;
|
|
142
|
+
this.realtimeUrl = (_c = params.realtimeUrl) !== null && _c !== void 0 ? _c : defaultRealtimeUrl;
|
|
143
|
+
if ("apiKey" in params)
|
|
144
|
+
this.apiKey = params.apiKey;
|
|
145
|
+
if ("token" in params)
|
|
146
|
+
this.token = params.token;
|
|
147
|
+
if (!(this.apiKey || this.token)) {
|
|
148
|
+
throw new Error("API key or temporary token is required.");
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
connectionUrl() {
|
|
152
|
+
const url = new URL(this.realtimeUrl);
|
|
153
|
+
if (url.protocol !== "wss:") {
|
|
154
|
+
throw new Error("Invalid protocol, must be wss");
|
|
155
|
+
}
|
|
156
|
+
const searchParams = new URLSearchParams();
|
|
157
|
+
if (this.token) {
|
|
158
|
+
searchParams.set("token", this.token);
|
|
159
|
+
}
|
|
160
|
+
searchParams.set("sample_rate", this.sampleRate.toString());
|
|
161
|
+
if (this.wordBoost && this.wordBoost.length > 0) {
|
|
162
|
+
searchParams.set("word_boost", JSON.stringify(this.wordBoost));
|
|
163
|
+
}
|
|
164
|
+
url.search = searchParams.toString();
|
|
165
|
+
return url;
|
|
166
|
+
}
|
|
167
|
+
on(event, listener) {
|
|
168
|
+
this.listeners[event] = listener;
|
|
169
|
+
}
|
|
170
|
+
connect() {
|
|
171
|
+
return new Promise((resolve, _) => {
|
|
172
|
+
if (this.socket) {
|
|
173
|
+
throw new Error("Already connected");
|
|
174
|
+
}
|
|
175
|
+
const url = this.connectionUrl();
|
|
176
|
+
let headers;
|
|
177
|
+
if (this.token) {
|
|
178
|
+
headers = undefined;
|
|
179
|
+
}
|
|
180
|
+
else if (this.apiKey) {
|
|
181
|
+
headers = { Authorization: this.apiKey };
|
|
182
|
+
}
|
|
183
|
+
this.socket = new WebSocket(url.toString(), { headers });
|
|
184
|
+
this.socket.onclose = ({ code, reason }) => {
|
|
185
|
+
var _a, _b;
|
|
186
|
+
if (!reason) {
|
|
187
|
+
if (code in RealtimeErrorType) {
|
|
188
|
+
reason = RealtimeErrorMessages[code];
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
(_b = (_a = this.listeners).close) === null || _b === void 0 ? void 0 : _b.call(_a, code, reason);
|
|
192
|
+
};
|
|
193
|
+
this.socket.onerror = (errorEvent) => {
|
|
194
|
+
var _a, _b, _c, _d;
|
|
195
|
+
if (errorEvent.error)
|
|
196
|
+
(_b = (_a = this.listeners).error) === null || _b === void 0 ? void 0 : _b.call(_a, errorEvent.error);
|
|
197
|
+
else
|
|
198
|
+
(_d = (_c = this.listeners).error) === null || _d === void 0 ? void 0 : _d.call(_c, new Error(errorEvent.message));
|
|
199
|
+
};
|
|
200
|
+
this.socket.onmessage = ({ data }) => {
|
|
201
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o;
|
|
202
|
+
const message = JSON.parse(data.toString());
|
|
203
|
+
if ("error" in message) {
|
|
204
|
+
(_b = (_a = this.listeners).error) === null || _b === void 0 ? void 0 : _b.call(_a, new RealtimeError(message.error));
|
|
205
|
+
return;
|
|
206
|
+
}
|
|
207
|
+
switch (message.message_type) {
|
|
208
|
+
case "SessionBegins": {
|
|
209
|
+
const openObject = {
|
|
210
|
+
sessionId: message.session_id,
|
|
211
|
+
expiresAt: new Date(message.expires_at),
|
|
212
|
+
};
|
|
213
|
+
resolve(openObject);
|
|
214
|
+
(_d = (_c = this.listeners).open) === null || _d === void 0 ? void 0 : _d.call(_c, openObject);
|
|
215
|
+
break;
|
|
216
|
+
}
|
|
217
|
+
case "PartialTranscript": {
|
|
218
|
+
// message.created is actually a string when coming from the socket
|
|
219
|
+
message.created = new Date(message.created);
|
|
220
|
+
(_f = (_e = this.listeners).transcript) === null || _f === void 0 ? void 0 : _f.call(_e, message);
|
|
221
|
+
(_h = (_g = this.listeners)["transcript.partial"]) === null || _h === void 0 ? void 0 : _h.call(_g, message);
|
|
222
|
+
break;
|
|
223
|
+
}
|
|
224
|
+
case "FinalTranscript": {
|
|
225
|
+
// message.created is actually a string when coming from the socket
|
|
226
|
+
message.created = new Date(message.created);
|
|
227
|
+
(_k = (_j = this.listeners).transcript) === null || _k === void 0 ? void 0 : _k.call(_j, message);
|
|
228
|
+
(_m = (_l = this.listeners)["transcript.final"]) === null || _m === void 0 ? void 0 : _m.call(_l, message);
|
|
229
|
+
break;
|
|
230
|
+
}
|
|
231
|
+
case "SessionTerminated": {
|
|
232
|
+
(_o = this.sessionTerminatedResolve) === null || _o === void 0 ? void 0 : _o.call(this);
|
|
233
|
+
break;
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
};
|
|
237
|
+
});
|
|
238
|
+
}
|
|
239
|
+
sendAudio(audio) {
|
|
240
|
+
if (!this.socket || this.socket.readyState !== WebSocket.OPEN) {
|
|
241
|
+
throw new Error("Socket is not open for communication");
|
|
242
|
+
}
|
|
243
|
+
const payload = {
|
|
244
|
+
audio_data: Buffer.from(audio).toString("base64"),
|
|
245
|
+
};
|
|
246
|
+
this.socket.send(JSON.stringify(payload));
|
|
247
|
+
}
|
|
248
|
+
close(waitForSessionTermination = true) {
|
|
249
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
250
|
+
if (this.socket) {
|
|
251
|
+
if (this.socket.readyState === WebSocket.OPEN) {
|
|
252
|
+
const terminateSessionMessage = `{"terminate_session": true}`;
|
|
253
|
+
if (waitForSessionTermination) {
|
|
254
|
+
const sessionTerminatedPromise = new Promise((resolve, _) => {
|
|
255
|
+
this.sessionTerminatedResolve = resolve;
|
|
256
|
+
});
|
|
257
|
+
this.socket.send(terminateSessionMessage);
|
|
258
|
+
yield sessionTerminatedPromise;
|
|
259
|
+
}
|
|
260
|
+
else {
|
|
261
|
+
this.socket.send(terminateSessionMessage);
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
this.socket.removeAllListeners();
|
|
265
|
+
this.socket.close();
|
|
266
|
+
}
|
|
267
|
+
this.listeners = {};
|
|
268
|
+
this.socket = undefined;
|
|
269
|
+
});
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
class RealtimeServiceFactory {
|
|
274
|
+
constructor(client, params) {
|
|
275
|
+
this.client = client;
|
|
276
|
+
this.params = params;
|
|
277
|
+
}
|
|
278
|
+
createService(params) {
|
|
279
|
+
if (!params)
|
|
280
|
+
params = { apiKey: this.params.apiKey };
|
|
281
|
+
else if (!("token" in params) && !params.apiKey) {
|
|
282
|
+
params.apiKey = this.params.apiKey;
|
|
283
|
+
}
|
|
284
|
+
return new RealtimeService(params);
|
|
285
|
+
}
|
|
286
|
+
createTemporaryToken(params) {
|
|
287
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
288
|
+
const response = yield this.client.post("/v2/realtime/token", params);
|
|
289
|
+
return response.data.token;
|
|
290
|
+
});
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
class TranscriptService extends BaseService {
|
|
295
|
+
constructor(client, files) {
|
|
296
|
+
super(client);
|
|
297
|
+
this.files = files;
|
|
298
|
+
}
|
|
299
|
+
/**
|
|
300
|
+
* Create a transcript.
|
|
301
|
+
* @param params The parameters to create a transcript.
|
|
302
|
+
* @param options The options used for creating the new transcript.
|
|
303
|
+
* @returns A promise that resolves to the newly created transcript.
|
|
304
|
+
*/
|
|
305
|
+
create(params, options) {
|
|
306
|
+
var _a;
|
|
307
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
308
|
+
const path = getPath(params.audio_url);
|
|
309
|
+
if (path !== null) {
|
|
310
|
+
const uploadUrl = yield this.files.upload(path);
|
|
311
|
+
params.audio_url = uploadUrl;
|
|
312
|
+
}
|
|
313
|
+
const res = yield this.client.post("/v2/transcript", params);
|
|
314
|
+
if ((_a = options === null || options === void 0 ? void 0 : options.poll) !== null && _a !== void 0 ? _a : true) {
|
|
315
|
+
return yield this.poll(res.data.id, options);
|
|
316
|
+
}
|
|
317
|
+
return res.data;
|
|
318
|
+
});
|
|
319
|
+
}
|
|
320
|
+
poll(transcriptId, options) {
|
|
321
|
+
var _a;
|
|
322
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
323
|
+
const startTime = Date.now();
|
|
324
|
+
while (true) {
|
|
325
|
+
const transcript = yield this.get(transcriptId);
|
|
326
|
+
if (transcript.status === "completed" || transcript.status === "error") {
|
|
327
|
+
return transcript;
|
|
328
|
+
}
|
|
329
|
+
else if (Date.now() - startTime <
|
|
330
|
+
((_a = options === null || options === void 0 ? void 0 : options.pollingTimeout) !== null && _a !== void 0 ? _a : 180000)) {
|
|
331
|
+
yield new Promise((resolve) => { var _a; return setTimeout(resolve, (_a = options === null || options === void 0 ? void 0 : options.pollingInterval) !== null && _a !== void 0 ? _a : 3000); });
|
|
332
|
+
}
|
|
333
|
+
else {
|
|
334
|
+
throw new Error("Polling timeout");
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
});
|
|
338
|
+
}
|
|
339
|
+
/**
|
|
340
|
+
* Retrieve a transcript.
|
|
341
|
+
* @param id The identifier of the transcript.
|
|
342
|
+
* @returns A promise that resolves to the transcript.
|
|
343
|
+
*/
|
|
344
|
+
get(id) {
|
|
345
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
346
|
+
const res = yield this.client.get(`/v2/transcript/${id}`);
|
|
347
|
+
return res.data;
|
|
348
|
+
});
|
|
349
|
+
}
|
|
350
|
+
// TODO: add options overload to support list querystring parameters
|
|
351
|
+
/**
|
|
352
|
+
* Retrieves a paged list of transcript listings.
|
|
353
|
+
* @param nextUrl The URL to retrieve the transcript list from. If not provided, the first page will be retrieved.
|
|
354
|
+
* @returns
|
|
355
|
+
*/
|
|
356
|
+
list(nextUrl) {
|
|
357
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
358
|
+
const { data } = yield this.client.get(nextUrl !== null && nextUrl !== void 0 ? nextUrl : "/v2/transcript");
|
|
359
|
+
for (const transcriptListItem of data.transcripts) {
|
|
360
|
+
transcriptListItem.created = new Date(transcriptListItem.created);
|
|
361
|
+
if (transcriptListItem.completed) {
|
|
362
|
+
transcriptListItem.completed = new Date(transcriptListItem.completed);
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
return data;
|
|
366
|
+
});
|
|
367
|
+
}
|
|
368
|
+
/**
|
|
369
|
+
* Delete a transcript
|
|
370
|
+
* @param id The identifier of the transcript.
|
|
371
|
+
* @returns A promise that resolves to the transcript.
|
|
372
|
+
*/
|
|
373
|
+
delete(id) {
|
|
374
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
375
|
+
const res = yield this.client.delete(`/v2/transcript/${id}`);
|
|
376
|
+
return res.data;
|
|
377
|
+
});
|
|
378
|
+
}
|
|
379
|
+
/**
|
|
380
|
+
* Retrieve all sentences of a transcript.
|
|
381
|
+
* @param id The identifier of the transcript.
|
|
382
|
+
* @return A promise that resolves to the sentences.
|
|
383
|
+
*/
|
|
384
|
+
sentences(id) {
|
|
385
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
386
|
+
const { data } = yield this.client.get(`/v2/transcript/${id}/sentences`);
|
|
387
|
+
return data;
|
|
388
|
+
});
|
|
389
|
+
}
|
|
390
|
+
/**
|
|
391
|
+
* Retrieve all paragraphs of a transcript.
|
|
392
|
+
* @param id The identifier of the transcript.
|
|
393
|
+
* @return A promise that resolves to the paragraphs.
|
|
394
|
+
*/
|
|
395
|
+
paragraphs(id) {
|
|
396
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
397
|
+
const { data } = yield this.client.get(`/v2/transcript/${id}/paragraphs`);
|
|
398
|
+
return data;
|
|
399
|
+
});
|
|
400
|
+
}
|
|
401
|
+
/**
|
|
402
|
+
* Retrieve subtitles of a transcript.
|
|
403
|
+
* @param id The identifier of the transcript.
|
|
404
|
+
* @param format The format of the subtitles.
|
|
405
|
+
* @return A promise that resolves to the subtitles text.
|
|
406
|
+
*/
|
|
407
|
+
subtitles(id, format = "srt") {
|
|
408
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
409
|
+
const { data } = yield this.client.get(`/v2/transcript/${id}/${format}`);
|
|
410
|
+
return data;
|
|
411
|
+
});
|
|
412
|
+
}
|
|
413
|
+
/**
|
|
414
|
+
* Retrieve redactions of a transcript.
|
|
415
|
+
* @param id The identifier of the transcript.
|
|
416
|
+
* @return A promise that resolves to the subtitles text.
|
|
417
|
+
*/
|
|
418
|
+
redactions(id) {
|
|
419
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
420
|
+
const { data } = yield this.client.get(`/v2/transcript/${id}/redacted-audio`);
|
|
421
|
+
return data;
|
|
422
|
+
});
|
|
423
|
+
}
|
|
424
|
+
}
|
|
425
|
+
function getPath(path) {
|
|
426
|
+
let url;
|
|
427
|
+
try {
|
|
428
|
+
url = new URL(path);
|
|
429
|
+
if (url.protocol === "file:")
|
|
430
|
+
return url.pathname;
|
|
431
|
+
else
|
|
432
|
+
return null;
|
|
433
|
+
}
|
|
434
|
+
catch (_a) {
|
|
435
|
+
return path;
|
|
436
|
+
}
|
|
437
|
+
}
|
|
438
|
+
|
|
439
|
+
class FileService extends BaseService {
|
|
440
|
+
/**
|
|
441
|
+
* Upload a local file to AssemblyAI.
|
|
442
|
+
* @param path The local file to upload.
|
|
443
|
+
* @return A promise that resolves to the uploaded file URL.
|
|
444
|
+
*/
|
|
445
|
+
upload(path) {
|
|
446
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
447
|
+
const file = yield readFile(path);
|
|
448
|
+
const { data } = yield this.client.post("/v2/upload", file, {
|
|
449
|
+
headers: {
|
|
450
|
+
"Content-Type": "application/octet-stream",
|
|
451
|
+
},
|
|
452
|
+
});
|
|
453
|
+
return data.upload_url;
|
|
454
|
+
});
|
|
455
|
+
}
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
class AssemblyAI {
|
|
459
|
+
/**
|
|
460
|
+
* Create a new AssemblyAI client.
|
|
461
|
+
* @param params The parameters for the service, including the API key and base URL, if any.
|
|
462
|
+
*/
|
|
463
|
+
constructor(params) {
|
|
464
|
+
params.baseUrl = params.baseUrl || "https://api.assemblyai.com";
|
|
465
|
+
const client = createAxiosClient(params);
|
|
466
|
+
this.files = new FileService(client);
|
|
467
|
+
this.transcripts = new TranscriptService(client, this.files);
|
|
468
|
+
this.lemur = new LemurService(client);
|
|
469
|
+
this.realtime = new RealtimeServiceFactory(client, params);
|
|
470
|
+
}
|
|
471
|
+
}
|
|
472
|
+
|
|
473
|
+
export { FileService, LemurService, RealtimeService, RealtimeServiceFactory, TranscriptService, AssemblyAI as default };
|