assemblyai 4.5.0-beta.0 → 4.6.0
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 +41 -0
- package/README.md +53 -9
- package/dist/assemblyai.streaming.umd.js +317 -0
- package/dist/assemblyai.streaming.umd.min.js +1 -0
- package/dist/assemblyai.umd.js +160 -12
- package/dist/assemblyai.umd.min.js +1 -1
- package/dist/browser.mjs +154 -11
- package/dist/bun.mjs +154 -11
- package/dist/deno.mjs +154 -11
- package/dist/exports/index.d.ts +2 -0
- package/dist/exports/streaming.d.ts +4 -0
- package/dist/index.cjs +160 -12
- package/dist/index.mjs +160 -12
- package/dist/node.cjs +154 -11
- package/dist/node.mjs +154 -11
- package/dist/polyfills/fetch/default.d.ts +1 -0
- package/dist/polyfills/fetch/workerd.d.ts +1 -0
- package/dist/services/base.d.ts +1 -0
- package/dist/services/lemur/index.d.ts +8 -1
- package/dist/services/realtime/service.d.ts +60 -0
- package/dist/services/transcripts/index.d.ts +16 -3
- package/dist/streaming.browser.mjs +268 -0
- package/dist/streaming.cjs +306 -0
- package/dist/streaming.mjs +303 -0
- package/dist/types/index.d.ts +7 -0
- package/dist/types/openapi.generated.d.ts +75 -29
- package/dist/types/services/index.d.ts +7 -0
- package/dist/types/transcripts/index.d.ts +9 -0
- package/dist/utils/userAgent.d.ts +2 -0
- package/dist/workerd.mjs +751 -0
- package/docs/reference-types-from-js.md +27 -0
- package/package.json +57 -25
- package/src/exports/index.ts +2 -0
- package/src/exports/streaming.ts +4 -0
- package/src/polyfills/fetch/default.ts +3 -0
- package/src/polyfills/fetch/workerd.ts +1 -0
- package/src/services/base.ts +27 -7
- package/src/services/files/index.ts +19 -2
- package/src/services/index.ts +2 -1
- package/src/services/lemur/index.ts +12 -0
- package/src/services/realtime/service.ts +65 -0
- package/src/services/transcripts/index.ts +41 -4
- package/src/types/index.ts +9 -0
- package/src/types/openapi.generated.ts +224 -48
- package/src/types/services/index.ts +8 -0
- package/src/types/transcripts/index.ts +10 -0
- package/src/utils/path.ts +1 -0
- package/src/utils/userAgent.ts +51 -0
package/dist/index.cjs
CHANGED
|
@@ -46,6 +46,45 @@ typeof SuppressedError === "function" ? SuppressedError : function (error, suppr
|
|
|
46
46
|
return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
|
|
47
47
|
};
|
|
48
48
|
|
|
49
|
+
const DEFAULT_FETCH_INIT = {
|
|
50
|
+
cache: "no-store",
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
const buildUserAgent = (userAgent) => defaultUserAgentString +
|
|
54
|
+
(userAgent === false
|
|
55
|
+
? ""
|
|
56
|
+
: " AssemblyAI/1.0 (" +
|
|
57
|
+
Object.entries(Object.assign(Object.assign({}, defaultUserAgent), userAgent))
|
|
58
|
+
.map(([key, item]) => item ? `${key}=${item.name}/${item.version}` : "")
|
|
59
|
+
.join(" ") +
|
|
60
|
+
")");
|
|
61
|
+
let defaultUserAgentString = "";
|
|
62
|
+
if (typeof navigator !== "undefined" && navigator.userAgent) {
|
|
63
|
+
defaultUserAgentString += navigator.userAgent;
|
|
64
|
+
}
|
|
65
|
+
const defaultUserAgent = {
|
|
66
|
+
sdk: { name: "JavaScript", version: "4.6.0" },
|
|
67
|
+
};
|
|
68
|
+
if (typeof process !== "undefined") {
|
|
69
|
+
if (process.versions.node && defaultUserAgentString.indexOf("Node") === -1) {
|
|
70
|
+
defaultUserAgent.runtime_env = {
|
|
71
|
+
name: "Node",
|
|
72
|
+
version: process.versions.node,
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
if (process.versions.bun && defaultUserAgentString.indexOf("Bun") === -1) {
|
|
76
|
+
defaultUserAgent.runtime_env = {
|
|
77
|
+
name: "Bun",
|
|
78
|
+
version: process.versions.bun,
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
if (typeof Deno !== "undefined") {
|
|
83
|
+
if (process.versions.bun && defaultUserAgentString.indexOf("Deno") === -1) {
|
|
84
|
+
defaultUserAgent.runtime_env = { name: "Deno", version: Deno.version.deno };
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
49
88
|
/**
|
|
50
89
|
* Base class for services that communicate with the API.
|
|
51
90
|
*/
|
|
@@ -56,13 +95,33 @@ class BaseService {
|
|
|
56
95
|
*/
|
|
57
96
|
constructor(params) {
|
|
58
97
|
this.params = params;
|
|
98
|
+
if (params.userAgent === false) {
|
|
99
|
+
this.userAgent = undefined;
|
|
100
|
+
}
|
|
101
|
+
else {
|
|
102
|
+
this.userAgent = buildUserAgent(params.userAgent || {});
|
|
103
|
+
}
|
|
59
104
|
}
|
|
60
105
|
fetch(input, init) {
|
|
61
106
|
return __awaiter(this, void 0, void 0, function* () {
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
107
|
+
init = Object.assign(Object.assign({}, DEFAULT_FETCH_INIT), init);
|
|
108
|
+
let headers = {
|
|
109
|
+
Authorization: this.params.apiKey,
|
|
110
|
+
"Content-Type": "application/json",
|
|
111
|
+
};
|
|
112
|
+
if (DEFAULT_FETCH_INIT === null || DEFAULT_FETCH_INIT === void 0 ? void 0 : DEFAULT_FETCH_INIT.headers)
|
|
113
|
+
headers = Object.assign(Object.assign({}, headers), DEFAULT_FETCH_INIT.headers);
|
|
114
|
+
if (init === null || init === void 0 ? void 0 : init.headers)
|
|
115
|
+
headers = Object.assign(Object.assign({}, headers), init.headers);
|
|
116
|
+
if (this.userAgent) {
|
|
117
|
+
headers["User-Agent"] = this.userAgent;
|
|
118
|
+
// chromium browsers have a bug where the user agent can't be modified
|
|
119
|
+
if (typeof window !== "undefined" && "chrome" in window) {
|
|
120
|
+
headers["AssemblyAI-Agent"] =
|
|
121
|
+
this.userAgent;
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
init.headers = headers;
|
|
66
125
|
if (!input.startsWith("http"))
|
|
67
126
|
input = this.params.baseUrl + input;
|
|
68
127
|
const response = yield fetch(input, init);
|
|
@@ -73,7 +132,7 @@ class BaseService {
|
|
|
73
132
|
try {
|
|
74
133
|
json = JSON.parse(text);
|
|
75
134
|
}
|
|
76
|
-
catch (
|
|
135
|
+
catch (_a) {
|
|
77
136
|
/* empty */
|
|
78
137
|
}
|
|
79
138
|
if (json === null || json === void 0 ? void 0 : json.error)
|
|
@@ -118,6 +177,9 @@ class LemurService extends BaseService {
|
|
|
118
177
|
body: JSON.stringify(params),
|
|
119
178
|
});
|
|
120
179
|
}
|
|
180
|
+
getResponse(id) {
|
|
181
|
+
return this.fetchJson(`/lemur/v3/${id}`);
|
|
182
|
+
}
|
|
121
183
|
/**
|
|
122
184
|
* Delete the data for a previously submitted LeMUR request.
|
|
123
185
|
* @param id - ID of the LeMUR request
|
|
@@ -181,7 +243,14 @@ class RealtimeError extends Error {
|
|
|
181
243
|
const defaultRealtimeUrl = "wss://api.assemblyai.com/v2/realtime/ws";
|
|
182
244
|
const forceEndOfUtteranceMessage = `{"force_end_utterance":true}`;
|
|
183
245
|
const terminateSessionMessage = `{"terminate_session":true}`;
|
|
246
|
+
/**
|
|
247
|
+
* RealtimeTranscriber connects to the Streaming Speech-to-Text API and lets you transcribe audio in real-time.
|
|
248
|
+
*/
|
|
184
249
|
class RealtimeTranscriber {
|
|
250
|
+
/**
|
|
251
|
+
* Create a new RealtimeTranscriber.
|
|
252
|
+
* @param params - Parameters to configure the RealtimeTranscriber
|
|
253
|
+
*/
|
|
185
254
|
constructor(params) {
|
|
186
255
|
var _a, _b;
|
|
187
256
|
this.listeners = {};
|
|
@@ -222,10 +291,19 @@ class RealtimeTranscriber {
|
|
|
222
291
|
url.search = searchParams.toString();
|
|
223
292
|
return url;
|
|
224
293
|
}
|
|
294
|
+
/**
|
|
295
|
+
* Add a listener for an event.
|
|
296
|
+
* @param event - The event to listen for.
|
|
297
|
+
* @param listener - The function to call when the event is emitted.
|
|
298
|
+
*/
|
|
225
299
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
226
300
|
on(event, listener) {
|
|
227
301
|
this.listeners[event] = listener;
|
|
228
302
|
}
|
|
303
|
+
/**
|
|
304
|
+
* Connect to the server and begin a new session.
|
|
305
|
+
* @returns A promise that resolves when the connection is established and the session begins.
|
|
306
|
+
*/
|
|
229
307
|
connect() {
|
|
230
308
|
return new Promise((resolve) => {
|
|
231
309
|
if (this.socket) {
|
|
@@ -307,9 +385,17 @@ class RealtimeTranscriber {
|
|
|
307
385
|
};
|
|
308
386
|
});
|
|
309
387
|
}
|
|
388
|
+
/**
|
|
389
|
+
* Send audio data to the server.
|
|
390
|
+
* @param audio - The audio data to send to the server.
|
|
391
|
+
*/
|
|
310
392
|
sendAudio(audio) {
|
|
311
393
|
this.send(audio);
|
|
312
394
|
}
|
|
395
|
+
/**
|
|
396
|
+
* Create a writable stream that can be used to send audio data to the server.
|
|
397
|
+
* @returns A writable stream that can be used to send audio data to the server.
|
|
398
|
+
*/
|
|
313
399
|
stream() {
|
|
314
400
|
return new WritableStream({
|
|
315
401
|
write: (chunk) => {
|
|
@@ -337,6 +423,11 @@ class RealtimeTranscriber {
|
|
|
337
423
|
}
|
|
338
424
|
this.socket.send(data);
|
|
339
425
|
}
|
|
426
|
+
/**
|
|
427
|
+
* Close the connection to the server.
|
|
428
|
+
* @param waitForSessionTermination - If true, the method will wait for the session to be terminated before closing the connection.
|
|
429
|
+
* While waiting for the session to be terminated, you will receive the final transcript and session information.
|
|
430
|
+
*/
|
|
340
431
|
close() {
|
|
341
432
|
return __awaiter(this, arguments, void 0, function* (waitForSessionTermination = true) {
|
|
342
433
|
var _a;
|
|
@@ -407,6 +498,8 @@ function getPath(path) {
|
|
|
407
498
|
return null;
|
|
408
499
|
if (path.startsWith("https"))
|
|
409
500
|
return null;
|
|
501
|
+
if (path.startsWith("data:"))
|
|
502
|
+
return null;
|
|
410
503
|
if (path.startsWith("file://"))
|
|
411
504
|
return path.substring(7);
|
|
412
505
|
if (path.startsWith("file:"))
|
|
@@ -451,8 +544,13 @@ class TranscriptService extends BaseService {
|
|
|
451
544
|
audioUrl = yield this.files.upload(path);
|
|
452
545
|
}
|
|
453
546
|
else {
|
|
454
|
-
|
|
455
|
-
|
|
547
|
+
if (audio.startsWith("data:")) {
|
|
548
|
+
audioUrl = yield this.files.upload(audio);
|
|
549
|
+
}
|
|
550
|
+
else {
|
|
551
|
+
// audio is not a local path, and not a data-URI, assume it's a normal URL
|
|
552
|
+
audioUrl = audio;
|
|
553
|
+
}
|
|
456
554
|
}
|
|
457
555
|
}
|
|
458
556
|
else {
|
|
@@ -617,13 +715,45 @@ class TranscriptService extends BaseService {
|
|
|
617
715
|
});
|
|
618
716
|
}
|
|
619
717
|
/**
|
|
620
|
-
* Retrieve
|
|
718
|
+
* Retrieve the redacted audio URL of a transcript.
|
|
621
719
|
* @param id - The identifier of the transcript.
|
|
622
|
-
* @returns A promise that resolves to the
|
|
720
|
+
* @returns A promise that resolves to the details of the redacted audio.
|
|
721
|
+
* @deprecated Use `redactedAudio` instead.
|
|
623
722
|
*/
|
|
624
723
|
redactions(id) {
|
|
724
|
+
return this.redactedAudio(id);
|
|
725
|
+
}
|
|
726
|
+
/**
|
|
727
|
+
* Retrieve the redacted audio URL of a transcript.
|
|
728
|
+
* @param id - The identifier of the transcript.
|
|
729
|
+
* @returns A promise that resolves to the details of the redacted audio.
|
|
730
|
+
*/
|
|
731
|
+
redactedAudio(id) {
|
|
625
732
|
return this.fetchJson(`/v2/transcript/${id}/redacted-audio`);
|
|
626
733
|
}
|
|
734
|
+
/**
|
|
735
|
+
* Retrieve the redacted audio file of a transcript.
|
|
736
|
+
* @param id - The identifier of the transcript.
|
|
737
|
+
* @returns A promise that resolves to the fetch HTTP response of the redacted audio file.
|
|
738
|
+
*/
|
|
739
|
+
redactedAudioFile(id) {
|
|
740
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
741
|
+
const { redacted_audio_url, status } = yield this.redactedAudio(id);
|
|
742
|
+
if (status !== "redacted_audio_ready") {
|
|
743
|
+
throw new Error(`Redacted audio status is ${status}`);
|
|
744
|
+
}
|
|
745
|
+
const response = yield fetch(redacted_audio_url);
|
|
746
|
+
if (!response.ok) {
|
|
747
|
+
throw new Error(`Failed to fetch redacted audio: ${response.statusText}`);
|
|
748
|
+
}
|
|
749
|
+
return {
|
|
750
|
+
arrayBuffer: response.arrayBuffer.bind(response),
|
|
751
|
+
blob: response.blob.bind(response),
|
|
752
|
+
body: response.body,
|
|
753
|
+
bodyUsed: response.bodyUsed,
|
|
754
|
+
};
|
|
755
|
+
});
|
|
756
|
+
}
|
|
627
757
|
}
|
|
628
758
|
function deprecateConformer2(params) {
|
|
629
759
|
if (!params)
|
|
@@ -650,8 +780,14 @@ class FileService extends BaseService {
|
|
|
650
780
|
upload(input) {
|
|
651
781
|
return __awaiter(this, void 0, void 0, function* () {
|
|
652
782
|
let fileData;
|
|
653
|
-
if (typeof input === "string")
|
|
654
|
-
|
|
783
|
+
if (typeof input === "string") {
|
|
784
|
+
if (input.startsWith("data:")) {
|
|
785
|
+
fileData = dataUrlToBlob(input);
|
|
786
|
+
}
|
|
787
|
+
else {
|
|
788
|
+
fileData = yield readFile();
|
|
789
|
+
}
|
|
790
|
+
}
|
|
655
791
|
else
|
|
656
792
|
fileData = input;
|
|
657
793
|
const data = yield this.fetchJson("/v2/upload", {
|
|
@@ -666,6 +802,17 @@ class FileService extends BaseService {
|
|
|
666
802
|
});
|
|
667
803
|
}
|
|
668
804
|
}
|
|
805
|
+
function dataUrlToBlob(dataUrl) {
|
|
806
|
+
const arr = dataUrl.split(",");
|
|
807
|
+
const mime = arr[0].match(/:(.*?);/)[1];
|
|
808
|
+
const bstr = atob(arr[1]);
|
|
809
|
+
let n = bstr.length;
|
|
810
|
+
const u8arr = new Uint8Array(n);
|
|
811
|
+
while (n--) {
|
|
812
|
+
u8arr[n] = bstr.charCodeAt(n);
|
|
813
|
+
}
|
|
814
|
+
return new Blob([u8arr], { type: mime });
|
|
815
|
+
}
|
|
669
816
|
|
|
670
817
|
const defaultBaseUrl = "https://api.assemblyai.com";
|
|
671
818
|
class AssemblyAI {
|
|
@@ -675,8 +822,9 @@ class AssemblyAI {
|
|
|
675
822
|
*/
|
|
676
823
|
constructor(params) {
|
|
677
824
|
params.baseUrl = params.baseUrl || defaultBaseUrl;
|
|
678
|
-
if (params.baseUrl && params.baseUrl.endsWith("/"))
|
|
825
|
+
if (params.baseUrl && params.baseUrl.endsWith("/")) {
|
|
679
826
|
params.baseUrl = params.baseUrl.slice(0, -1);
|
|
827
|
+
}
|
|
680
828
|
this.files = new FileService(params);
|
|
681
829
|
this.transcripts = new TranscriptService(params, this.files);
|
|
682
830
|
this.lemur = new LemurService(params);
|
package/dist/index.mjs
CHANGED
|
@@ -44,6 +44,45 @@ typeof SuppressedError === "function" ? SuppressedError : function (error, suppr
|
|
|
44
44
|
return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
|
|
45
45
|
};
|
|
46
46
|
|
|
47
|
+
const DEFAULT_FETCH_INIT = {
|
|
48
|
+
cache: "no-store",
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
const buildUserAgent = (userAgent) => defaultUserAgentString +
|
|
52
|
+
(userAgent === false
|
|
53
|
+
? ""
|
|
54
|
+
: " AssemblyAI/1.0 (" +
|
|
55
|
+
Object.entries(Object.assign(Object.assign({}, defaultUserAgent), userAgent))
|
|
56
|
+
.map(([key, item]) => item ? `${key}=${item.name}/${item.version}` : "")
|
|
57
|
+
.join(" ") +
|
|
58
|
+
")");
|
|
59
|
+
let defaultUserAgentString = "";
|
|
60
|
+
if (typeof navigator !== "undefined" && navigator.userAgent) {
|
|
61
|
+
defaultUserAgentString += navigator.userAgent;
|
|
62
|
+
}
|
|
63
|
+
const defaultUserAgent = {
|
|
64
|
+
sdk: { name: "JavaScript", version: "4.6.0" },
|
|
65
|
+
};
|
|
66
|
+
if (typeof process !== "undefined") {
|
|
67
|
+
if (process.versions.node && defaultUserAgentString.indexOf("Node") === -1) {
|
|
68
|
+
defaultUserAgent.runtime_env = {
|
|
69
|
+
name: "Node",
|
|
70
|
+
version: process.versions.node,
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
if (process.versions.bun && defaultUserAgentString.indexOf("Bun") === -1) {
|
|
74
|
+
defaultUserAgent.runtime_env = {
|
|
75
|
+
name: "Bun",
|
|
76
|
+
version: process.versions.bun,
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
if (typeof Deno !== "undefined") {
|
|
81
|
+
if (process.versions.bun && defaultUserAgentString.indexOf("Deno") === -1) {
|
|
82
|
+
defaultUserAgent.runtime_env = { name: "Deno", version: Deno.version.deno };
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
47
86
|
/**
|
|
48
87
|
* Base class for services that communicate with the API.
|
|
49
88
|
*/
|
|
@@ -54,13 +93,33 @@ class BaseService {
|
|
|
54
93
|
*/
|
|
55
94
|
constructor(params) {
|
|
56
95
|
this.params = params;
|
|
96
|
+
if (params.userAgent === false) {
|
|
97
|
+
this.userAgent = undefined;
|
|
98
|
+
}
|
|
99
|
+
else {
|
|
100
|
+
this.userAgent = buildUserAgent(params.userAgent || {});
|
|
101
|
+
}
|
|
57
102
|
}
|
|
58
103
|
fetch(input, init) {
|
|
59
104
|
return __awaiter(this, void 0, void 0, function* () {
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
105
|
+
init = Object.assign(Object.assign({}, DEFAULT_FETCH_INIT), init);
|
|
106
|
+
let headers = {
|
|
107
|
+
Authorization: this.params.apiKey,
|
|
108
|
+
"Content-Type": "application/json",
|
|
109
|
+
};
|
|
110
|
+
if (DEFAULT_FETCH_INIT === null || DEFAULT_FETCH_INIT === void 0 ? void 0 : DEFAULT_FETCH_INIT.headers)
|
|
111
|
+
headers = Object.assign(Object.assign({}, headers), DEFAULT_FETCH_INIT.headers);
|
|
112
|
+
if (init === null || init === void 0 ? void 0 : init.headers)
|
|
113
|
+
headers = Object.assign(Object.assign({}, headers), init.headers);
|
|
114
|
+
if (this.userAgent) {
|
|
115
|
+
headers["User-Agent"] = this.userAgent;
|
|
116
|
+
// chromium browsers have a bug where the user agent can't be modified
|
|
117
|
+
if (typeof window !== "undefined" && "chrome" in window) {
|
|
118
|
+
headers["AssemblyAI-Agent"] =
|
|
119
|
+
this.userAgent;
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
init.headers = headers;
|
|
64
123
|
if (!input.startsWith("http"))
|
|
65
124
|
input = this.params.baseUrl + input;
|
|
66
125
|
const response = yield fetch(input, init);
|
|
@@ -71,7 +130,7 @@ class BaseService {
|
|
|
71
130
|
try {
|
|
72
131
|
json = JSON.parse(text);
|
|
73
132
|
}
|
|
74
|
-
catch (
|
|
133
|
+
catch (_a) {
|
|
75
134
|
/* empty */
|
|
76
135
|
}
|
|
77
136
|
if (json === null || json === void 0 ? void 0 : json.error)
|
|
@@ -116,6 +175,9 @@ class LemurService extends BaseService {
|
|
|
116
175
|
body: JSON.stringify(params),
|
|
117
176
|
});
|
|
118
177
|
}
|
|
178
|
+
getResponse(id) {
|
|
179
|
+
return this.fetchJson(`/lemur/v3/${id}`);
|
|
180
|
+
}
|
|
119
181
|
/**
|
|
120
182
|
* Delete the data for a previously submitted LeMUR request.
|
|
121
183
|
* @param id - ID of the LeMUR request
|
|
@@ -179,7 +241,14 @@ class RealtimeError extends Error {
|
|
|
179
241
|
const defaultRealtimeUrl = "wss://api.assemblyai.com/v2/realtime/ws";
|
|
180
242
|
const forceEndOfUtteranceMessage = `{"force_end_utterance":true}`;
|
|
181
243
|
const terminateSessionMessage = `{"terminate_session":true}`;
|
|
244
|
+
/**
|
|
245
|
+
* RealtimeTranscriber connects to the Streaming Speech-to-Text API and lets you transcribe audio in real-time.
|
|
246
|
+
*/
|
|
182
247
|
class RealtimeTranscriber {
|
|
248
|
+
/**
|
|
249
|
+
* Create a new RealtimeTranscriber.
|
|
250
|
+
* @param params - Parameters to configure the RealtimeTranscriber
|
|
251
|
+
*/
|
|
183
252
|
constructor(params) {
|
|
184
253
|
var _a, _b;
|
|
185
254
|
this.listeners = {};
|
|
@@ -220,10 +289,19 @@ class RealtimeTranscriber {
|
|
|
220
289
|
url.search = searchParams.toString();
|
|
221
290
|
return url;
|
|
222
291
|
}
|
|
292
|
+
/**
|
|
293
|
+
* Add a listener for an event.
|
|
294
|
+
* @param event - The event to listen for.
|
|
295
|
+
* @param listener - The function to call when the event is emitted.
|
|
296
|
+
*/
|
|
223
297
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
224
298
|
on(event, listener) {
|
|
225
299
|
this.listeners[event] = listener;
|
|
226
300
|
}
|
|
301
|
+
/**
|
|
302
|
+
* Connect to the server and begin a new session.
|
|
303
|
+
* @returns A promise that resolves when the connection is established and the session begins.
|
|
304
|
+
*/
|
|
227
305
|
connect() {
|
|
228
306
|
return new Promise((resolve) => {
|
|
229
307
|
if (this.socket) {
|
|
@@ -305,9 +383,17 @@ class RealtimeTranscriber {
|
|
|
305
383
|
};
|
|
306
384
|
});
|
|
307
385
|
}
|
|
386
|
+
/**
|
|
387
|
+
* Send audio data to the server.
|
|
388
|
+
* @param audio - The audio data to send to the server.
|
|
389
|
+
*/
|
|
308
390
|
sendAudio(audio) {
|
|
309
391
|
this.send(audio);
|
|
310
392
|
}
|
|
393
|
+
/**
|
|
394
|
+
* Create a writable stream that can be used to send audio data to the server.
|
|
395
|
+
* @returns A writable stream that can be used to send audio data to the server.
|
|
396
|
+
*/
|
|
311
397
|
stream() {
|
|
312
398
|
return new WritableStream({
|
|
313
399
|
write: (chunk) => {
|
|
@@ -335,6 +421,11 @@ class RealtimeTranscriber {
|
|
|
335
421
|
}
|
|
336
422
|
this.socket.send(data);
|
|
337
423
|
}
|
|
424
|
+
/**
|
|
425
|
+
* Close the connection to the server.
|
|
426
|
+
* @param waitForSessionTermination - If true, the method will wait for the session to be terminated before closing the connection.
|
|
427
|
+
* While waiting for the session to be terminated, you will receive the final transcript and session information.
|
|
428
|
+
*/
|
|
338
429
|
close() {
|
|
339
430
|
return __awaiter(this, arguments, void 0, function* (waitForSessionTermination = true) {
|
|
340
431
|
var _a;
|
|
@@ -405,6 +496,8 @@ function getPath(path) {
|
|
|
405
496
|
return null;
|
|
406
497
|
if (path.startsWith("https"))
|
|
407
498
|
return null;
|
|
499
|
+
if (path.startsWith("data:"))
|
|
500
|
+
return null;
|
|
408
501
|
if (path.startsWith("file://"))
|
|
409
502
|
return path.substring(7);
|
|
410
503
|
if (path.startsWith("file:"))
|
|
@@ -449,8 +542,13 @@ class TranscriptService extends BaseService {
|
|
|
449
542
|
audioUrl = yield this.files.upload(path);
|
|
450
543
|
}
|
|
451
544
|
else {
|
|
452
|
-
|
|
453
|
-
|
|
545
|
+
if (audio.startsWith("data:")) {
|
|
546
|
+
audioUrl = yield this.files.upload(audio);
|
|
547
|
+
}
|
|
548
|
+
else {
|
|
549
|
+
// audio is not a local path, and not a data-URI, assume it's a normal URL
|
|
550
|
+
audioUrl = audio;
|
|
551
|
+
}
|
|
454
552
|
}
|
|
455
553
|
}
|
|
456
554
|
else {
|
|
@@ -615,13 +713,45 @@ class TranscriptService extends BaseService {
|
|
|
615
713
|
});
|
|
616
714
|
}
|
|
617
715
|
/**
|
|
618
|
-
* Retrieve
|
|
716
|
+
* Retrieve the redacted audio URL of a transcript.
|
|
619
717
|
* @param id - The identifier of the transcript.
|
|
620
|
-
* @returns A promise that resolves to the
|
|
718
|
+
* @returns A promise that resolves to the details of the redacted audio.
|
|
719
|
+
* @deprecated Use `redactedAudio` instead.
|
|
621
720
|
*/
|
|
622
721
|
redactions(id) {
|
|
722
|
+
return this.redactedAudio(id);
|
|
723
|
+
}
|
|
724
|
+
/**
|
|
725
|
+
* Retrieve the redacted audio URL of a transcript.
|
|
726
|
+
* @param id - The identifier of the transcript.
|
|
727
|
+
* @returns A promise that resolves to the details of the redacted audio.
|
|
728
|
+
*/
|
|
729
|
+
redactedAudio(id) {
|
|
623
730
|
return this.fetchJson(`/v2/transcript/${id}/redacted-audio`);
|
|
624
731
|
}
|
|
732
|
+
/**
|
|
733
|
+
* Retrieve the redacted audio file of a transcript.
|
|
734
|
+
* @param id - The identifier of the transcript.
|
|
735
|
+
* @returns A promise that resolves to the fetch HTTP response of the redacted audio file.
|
|
736
|
+
*/
|
|
737
|
+
redactedAudioFile(id) {
|
|
738
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
739
|
+
const { redacted_audio_url, status } = yield this.redactedAudio(id);
|
|
740
|
+
if (status !== "redacted_audio_ready") {
|
|
741
|
+
throw new Error(`Redacted audio status is ${status}`);
|
|
742
|
+
}
|
|
743
|
+
const response = yield fetch(redacted_audio_url);
|
|
744
|
+
if (!response.ok) {
|
|
745
|
+
throw new Error(`Failed to fetch redacted audio: ${response.statusText}`);
|
|
746
|
+
}
|
|
747
|
+
return {
|
|
748
|
+
arrayBuffer: response.arrayBuffer.bind(response),
|
|
749
|
+
blob: response.blob.bind(response),
|
|
750
|
+
body: response.body,
|
|
751
|
+
bodyUsed: response.bodyUsed,
|
|
752
|
+
};
|
|
753
|
+
});
|
|
754
|
+
}
|
|
625
755
|
}
|
|
626
756
|
function deprecateConformer2(params) {
|
|
627
757
|
if (!params)
|
|
@@ -648,8 +778,14 @@ class FileService extends BaseService {
|
|
|
648
778
|
upload(input) {
|
|
649
779
|
return __awaiter(this, void 0, void 0, function* () {
|
|
650
780
|
let fileData;
|
|
651
|
-
if (typeof input === "string")
|
|
652
|
-
|
|
781
|
+
if (typeof input === "string") {
|
|
782
|
+
if (input.startsWith("data:")) {
|
|
783
|
+
fileData = dataUrlToBlob(input);
|
|
784
|
+
}
|
|
785
|
+
else {
|
|
786
|
+
fileData = yield readFile();
|
|
787
|
+
}
|
|
788
|
+
}
|
|
653
789
|
else
|
|
654
790
|
fileData = input;
|
|
655
791
|
const data = yield this.fetchJson("/v2/upload", {
|
|
@@ -664,6 +800,17 @@ class FileService extends BaseService {
|
|
|
664
800
|
});
|
|
665
801
|
}
|
|
666
802
|
}
|
|
803
|
+
function dataUrlToBlob(dataUrl) {
|
|
804
|
+
const arr = dataUrl.split(",");
|
|
805
|
+
const mime = arr[0].match(/:(.*?);/)[1];
|
|
806
|
+
const bstr = atob(arr[1]);
|
|
807
|
+
let n = bstr.length;
|
|
808
|
+
const u8arr = new Uint8Array(n);
|
|
809
|
+
while (n--) {
|
|
810
|
+
u8arr[n] = bstr.charCodeAt(n);
|
|
811
|
+
}
|
|
812
|
+
return new Blob([u8arr], { type: mime });
|
|
813
|
+
}
|
|
667
814
|
|
|
668
815
|
const defaultBaseUrl = "https://api.assemblyai.com";
|
|
669
816
|
class AssemblyAI {
|
|
@@ -673,8 +820,9 @@ class AssemblyAI {
|
|
|
673
820
|
*/
|
|
674
821
|
constructor(params) {
|
|
675
822
|
params.baseUrl = params.baseUrl || defaultBaseUrl;
|
|
676
|
-
if (params.baseUrl && params.baseUrl.endsWith("/"))
|
|
823
|
+
if (params.baseUrl && params.baseUrl.endsWith("/")) {
|
|
677
824
|
params.baseUrl = params.baseUrl.slice(0, -1);
|
|
825
|
+
}
|
|
678
826
|
this.files = new FileService(params);
|
|
679
827
|
this.transcripts = new TranscriptService(params, this.files);
|
|
680
828
|
this.lemur = new LemurService(params);
|