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/node.cjs
CHANGED
|
@@ -5,6 +5,45 @@ var ws = require('ws');
|
|
|
5
5
|
var fs = require('fs');
|
|
6
6
|
var stream = require('stream');
|
|
7
7
|
|
|
8
|
+
const DEFAULT_FETCH_INIT = {
|
|
9
|
+
cache: "no-store",
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
const buildUserAgent = (userAgent) => defaultUserAgentString +
|
|
13
|
+
(userAgent === false
|
|
14
|
+
? ""
|
|
15
|
+
: " AssemblyAI/1.0 (" +
|
|
16
|
+
Object.entries({ ...defaultUserAgent, ...userAgent })
|
|
17
|
+
.map(([key, item]) => item ? `${key}=${item.name}/${item.version}` : "")
|
|
18
|
+
.join(" ") +
|
|
19
|
+
")");
|
|
20
|
+
let defaultUserAgentString = "";
|
|
21
|
+
if (typeof navigator !== "undefined" && navigator.userAgent) {
|
|
22
|
+
defaultUserAgentString += navigator.userAgent;
|
|
23
|
+
}
|
|
24
|
+
const defaultUserAgent = {
|
|
25
|
+
sdk: { name: "JavaScript", version: "4.6.0" },
|
|
26
|
+
};
|
|
27
|
+
if (typeof process !== "undefined") {
|
|
28
|
+
if (process.versions.node && defaultUserAgentString.indexOf("Node") === -1) {
|
|
29
|
+
defaultUserAgent.runtime_env = {
|
|
30
|
+
name: "Node",
|
|
31
|
+
version: process.versions.node,
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
if (process.versions.bun && defaultUserAgentString.indexOf("Bun") === -1) {
|
|
35
|
+
defaultUserAgent.runtime_env = {
|
|
36
|
+
name: "Bun",
|
|
37
|
+
version: process.versions.bun,
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
if (typeof Deno !== "undefined") {
|
|
42
|
+
if (process.versions.bun && defaultUserAgentString.indexOf("Deno") === -1) {
|
|
43
|
+
defaultUserAgent.runtime_env = { name: "Deno", version: Deno.version.deno };
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
8
47
|
/**
|
|
9
48
|
* Base class for services that communicate with the API.
|
|
10
49
|
*/
|
|
@@ -15,15 +54,32 @@ class BaseService {
|
|
|
15
54
|
*/
|
|
16
55
|
constructor(params) {
|
|
17
56
|
this.params = params;
|
|
57
|
+
if (params.userAgent === false) {
|
|
58
|
+
this.userAgent = undefined;
|
|
59
|
+
}
|
|
60
|
+
else {
|
|
61
|
+
this.userAgent = buildUserAgent(params.userAgent || {});
|
|
62
|
+
}
|
|
18
63
|
}
|
|
19
64
|
async fetch(input, init) {
|
|
20
|
-
init = init
|
|
21
|
-
|
|
22
|
-
init.headers = {
|
|
65
|
+
init = { ...DEFAULT_FETCH_INIT, ...init };
|
|
66
|
+
let headers = {
|
|
23
67
|
Authorization: this.params.apiKey,
|
|
24
68
|
"Content-Type": "application/json",
|
|
25
|
-
...init.headers,
|
|
26
69
|
};
|
|
70
|
+
if (DEFAULT_FETCH_INIT?.headers)
|
|
71
|
+
headers = { ...headers, ...DEFAULT_FETCH_INIT.headers };
|
|
72
|
+
if (init?.headers)
|
|
73
|
+
headers = { ...headers, ...init.headers };
|
|
74
|
+
if (this.userAgent) {
|
|
75
|
+
headers["User-Agent"] = this.userAgent;
|
|
76
|
+
// chromium browsers have a bug where the user agent can't be modified
|
|
77
|
+
if (typeof window !== "undefined" && "chrome" in window) {
|
|
78
|
+
headers["AssemblyAI-Agent"] =
|
|
79
|
+
this.userAgent;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
init.headers = headers;
|
|
27
83
|
if (!input.startsWith("http"))
|
|
28
84
|
input = this.params.baseUrl + input;
|
|
29
85
|
const response = await fetch(input, init);
|
|
@@ -76,6 +132,9 @@ class LemurService extends BaseService {
|
|
|
76
132
|
body: JSON.stringify(params),
|
|
77
133
|
});
|
|
78
134
|
}
|
|
135
|
+
getResponse(id) {
|
|
136
|
+
return this.fetchJson(`/lemur/v3/${id}`);
|
|
137
|
+
}
|
|
79
138
|
/**
|
|
80
139
|
* Delete the data for a previously submitted LeMUR request.
|
|
81
140
|
* @param id - ID of the LeMUR request
|
|
@@ -133,7 +192,14 @@ class RealtimeError extends Error {
|
|
|
133
192
|
const defaultRealtimeUrl = "wss://api.assemblyai.com/v2/realtime/ws";
|
|
134
193
|
const forceEndOfUtteranceMessage = `{"force_end_utterance":true}`;
|
|
135
194
|
const terminateSessionMessage = `{"terminate_session":true}`;
|
|
195
|
+
/**
|
|
196
|
+
* RealtimeTranscriber connects to the Streaming Speech-to-Text API and lets you transcribe audio in real-time.
|
|
197
|
+
*/
|
|
136
198
|
class RealtimeTranscriber {
|
|
199
|
+
/**
|
|
200
|
+
* Create a new RealtimeTranscriber.
|
|
201
|
+
* @param params - Parameters to configure the RealtimeTranscriber
|
|
202
|
+
*/
|
|
137
203
|
constructor(params) {
|
|
138
204
|
this.listeners = {};
|
|
139
205
|
this.realtimeUrl = params.realtimeUrl ?? defaultRealtimeUrl;
|
|
@@ -173,10 +239,19 @@ class RealtimeTranscriber {
|
|
|
173
239
|
url.search = searchParams.toString();
|
|
174
240
|
return url;
|
|
175
241
|
}
|
|
242
|
+
/**
|
|
243
|
+
* Add a listener for an event.
|
|
244
|
+
* @param event - The event to listen for.
|
|
245
|
+
* @param listener - The function to call when the event is emitted.
|
|
246
|
+
*/
|
|
176
247
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
177
248
|
on(event, listener) {
|
|
178
249
|
this.listeners[event] = listener;
|
|
179
250
|
}
|
|
251
|
+
/**
|
|
252
|
+
* Connect to the server and begin a new session.
|
|
253
|
+
* @returns A promise that resolves when the connection is established and the session begins.
|
|
254
|
+
*/
|
|
180
255
|
connect() {
|
|
181
256
|
return new Promise((resolve) => {
|
|
182
257
|
if (this.socket) {
|
|
@@ -255,9 +330,17 @@ class RealtimeTranscriber {
|
|
|
255
330
|
};
|
|
256
331
|
});
|
|
257
332
|
}
|
|
333
|
+
/**
|
|
334
|
+
* Send audio data to the server.
|
|
335
|
+
* @param audio - The audio data to send to the server.
|
|
336
|
+
*/
|
|
258
337
|
sendAudio(audio) {
|
|
259
338
|
this.send(audio);
|
|
260
339
|
}
|
|
340
|
+
/**
|
|
341
|
+
* Create a writable stream that can be used to send audio data to the server.
|
|
342
|
+
* @returns A writable stream that can be used to send audio data to the server.
|
|
343
|
+
*/
|
|
261
344
|
stream() {
|
|
262
345
|
return new web.WritableStream({
|
|
263
346
|
write: (chunk) => {
|
|
@@ -285,6 +368,11 @@ class RealtimeTranscriber {
|
|
|
285
368
|
}
|
|
286
369
|
this.socket.send(data);
|
|
287
370
|
}
|
|
371
|
+
/**
|
|
372
|
+
* Close the connection to the server.
|
|
373
|
+
* @param waitForSessionTermination - If true, the method will wait for the session to be terminated before closing the connection.
|
|
374
|
+
* While waiting for the session to be terminated, you will receive the final transcript and session information.
|
|
375
|
+
*/
|
|
288
376
|
async close(waitForSessionTermination = true) {
|
|
289
377
|
if (this.socket) {
|
|
290
378
|
if (this.socket.readyState === this.socket.OPEN) {
|
|
@@ -350,6 +438,8 @@ function getPath(path) {
|
|
|
350
438
|
return null;
|
|
351
439
|
if (path.startsWith("https"))
|
|
352
440
|
return null;
|
|
441
|
+
if (path.startsWith("data:"))
|
|
442
|
+
return null;
|
|
353
443
|
if (path.startsWith("file://"))
|
|
354
444
|
return path.substring(7);
|
|
355
445
|
if (path.startsWith("file:"))
|
|
@@ -391,8 +481,13 @@ class TranscriptService extends BaseService {
|
|
|
391
481
|
audioUrl = await this.files.upload(path);
|
|
392
482
|
}
|
|
393
483
|
else {
|
|
394
|
-
|
|
395
|
-
|
|
484
|
+
if (audio.startsWith("data:")) {
|
|
485
|
+
audioUrl = await this.files.upload(audio);
|
|
486
|
+
}
|
|
487
|
+
else {
|
|
488
|
+
// audio is not a local path, and not a data-URI, assume it's a normal URL
|
|
489
|
+
audioUrl = audio;
|
|
490
|
+
}
|
|
396
491
|
}
|
|
397
492
|
}
|
|
398
493
|
else {
|
|
@@ -543,13 +638,43 @@ class TranscriptService extends BaseService {
|
|
|
543
638
|
return await response.text();
|
|
544
639
|
}
|
|
545
640
|
/**
|
|
546
|
-
* Retrieve
|
|
641
|
+
* Retrieve the redacted audio URL of a transcript.
|
|
547
642
|
* @param id - The identifier of the transcript.
|
|
548
|
-
* @returns A promise that resolves to the
|
|
643
|
+
* @returns A promise that resolves to the details of the redacted audio.
|
|
644
|
+
* @deprecated Use `redactedAudio` instead.
|
|
549
645
|
*/
|
|
550
646
|
redactions(id) {
|
|
647
|
+
return this.redactedAudio(id);
|
|
648
|
+
}
|
|
649
|
+
/**
|
|
650
|
+
* Retrieve the redacted audio URL of a transcript.
|
|
651
|
+
* @param id - The identifier of the transcript.
|
|
652
|
+
* @returns A promise that resolves to the details of the redacted audio.
|
|
653
|
+
*/
|
|
654
|
+
redactedAudio(id) {
|
|
551
655
|
return this.fetchJson(`/v2/transcript/${id}/redacted-audio`);
|
|
552
656
|
}
|
|
657
|
+
/**
|
|
658
|
+
* Retrieve the redacted audio file of a transcript.
|
|
659
|
+
* @param id - The identifier of the transcript.
|
|
660
|
+
* @returns A promise that resolves to the fetch HTTP response of the redacted audio file.
|
|
661
|
+
*/
|
|
662
|
+
async redactedAudioFile(id) {
|
|
663
|
+
const { redacted_audio_url, status } = await this.redactedAudio(id);
|
|
664
|
+
if (status !== "redacted_audio_ready") {
|
|
665
|
+
throw new Error(`Redacted audio status is ${status}`);
|
|
666
|
+
}
|
|
667
|
+
const response = await fetch(redacted_audio_url);
|
|
668
|
+
if (!response.ok) {
|
|
669
|
+
throw new Error(`Failed to fetch redacted audio: ${response.statusText}`);
|
|
670
|
+
}
|
|
671
|
+
return {
|
|
672
|
+
arrayBuffer: response.arrayBuffer.bind(response),
|
|
673
|
+
blob: response.blob.bind(response),
|
|
674
|
+
body: response.body,
|
|
675
|
+
bodyUsed: response.bodyUsed,
|
|
676
|
+
};
|
|
677
|
+
}
|
|
553
678
|
}
|
|
554
679
|
function deprecateConformer2(params) {
|
|
555
680
|
if (!params)
|
|
@@ -569,8 +694,14 @@ class FileService extends BaseService {
|
|
|
569
694
|
*/
|
|
570
695
|
async upload(input) {
|
|
571
696
|
let fileData;
|
|
572
|
-
if (typeof input === "string")
|
|
573
|
-
|
|
697
|
+
if (typeof input === "string") {
|
|
698
|
+
if (input.startsWith("data:")) {
|
|
699
|
+
fileData = dataUrlToBlob(input);
|
|
700
|
+
}
|
|
701
|
+
else {
|
|
702
|
+
fileData = await readFile(input);
|
|
703
|
+
}
|
|
704
|
+
}
|
|
574
705
|
else
|
|
575
706
|
fileData = input;
|
|
576
707
|
const data = await this.fetchJson("/v2/upload", {
|
|
@@ -584,6 +715,17 @@ class FileService extends BaseService {
|
|
|
584
715
|
return data.upload_url;
|
|
585
716
|
}
|
|
586
717
|
}
|
|
718
|
+
function dataUrlToBlob(dataUrl) {
|
|
719
|
+
const arr = dataUrl.split(",");
|
|
720
|
+
const mime = arr[0].match(/:(.*?);/)[1];
|
|
721
|
+
const bstr = atob(arr[1]);
|
|
722
|
+
let n = bstr.length;
|
|
723
|
+
const u8arr = new Uint8Array(n);
|
|
724
|
+
while (n--) {
|
|
725
|
+
u8arr[n] = bstr.charCodeAt(n);
|
|
726
|
+
}
|
|
727
|
+
return new Blob([u8arr], { type: mime });
|
|
728
|
+
}
|
|
587
729
|
|
|
588
730
|
const defaultBaseUrl = "https://api.assemblyai.com";
|
|
589
731
|
class AssemblyAI {
|
|
@@ -593,8 +735,9 @@ class AssemblyAI {
|
|
|
593
735
|
*/
|
|
594
736
|
constructor(params) {
|
|
595
737
|
params.baseUrl = params.baseUrl || defaultBaseUrl;
|
|
596
|
-
if (params.baseUrl && params.baseUrl.endsWith("/"))
|
|
738
|
+
if (params.baseUrl && params.baseUrl.endsWith("/")) {
|
|
597
739
|
params.baseUrl = params.baseUrl.slice(0, -1);
|
|
740
|
+
}
|
|
598
741
|
this.files = new FileService(params);
|
|
599
742
|
this.transcripts = new TranscriptService(params, this.files);
|
|
600
743
|
this.lemur = new LemurService(params);
|
package/dist/node.mjs
CHANGED
|
@@ -3,6 +3,45 @@ import ws from 'ws';
|
|
|
3
3
|
import { createReadStream } from 'fs';
|
|
4
4
|
import { Readable } from 'stream';
|
|
5
5
|
|
|
6
|
+
const DEFAULT_FETCH_INIT = {
|
|
7
|
+
cache: "no-store",
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
const buildUserAgent = (userAgent) => defaultUserAgentString +
|
|
11
|
+
(userAgent === false
|
|
12
|
+
? ""
|
|
13
|
+
: " AssemblyAI/1.0 (" +
|
|
14
|
+
Object.entries({ ...defaultUserAgent, ...userAgent })
|
|
15
|
+
.map(([key, item]) => item ? `${key}=${item.name}/${item.version}` : "")
|
|
16
|
+
.join(" ") +
|
|
17
|
+
")");
|
|
18
|
+
let defaultUserAgentString = "";
|
|
19
|
+
if (typeof navigator !== "undefined" && navigator.userAgent) {
|
|
20
|
+
defaultUserAgentString += navigator.userAgent;
|
|
21
|
+
}
|
|
22
|
+
const defaultUserAgent = {
|
|
23
|
+
sdk: { name: "JavaScript", version: "4.6.0" },
|
|
24
|
+
};
|
|
25
|
+
if (typeof process !== "undefined") {
|
|
26
|
+
if (process.versions.node && defaultUserAgentString.indexOf("Node") === -1) {
|
|
27
|
+
defaultUserAgent.runtime_env = {
|
|
28
|
+
name: "Node",
|
|
29
|
+
version: process.versions.node,
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
if (process.versions.bun && defaultUserAgentString.indexOf("Bun") === -1) {
|
|
33
|
+
defaultUserAgent.runtime_env = {
|
|
34
|
+
name: "Bun",
|
|
35
|
+
version: process.versions.bun,
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
if (typeof Deno !== "undefined") {
|
|
40
|
+
if (process.versions.bun && defaultUserAgentString.indexOf("Deno") === -1) {
|
|
41
|
+
defaultUserAgent.runtime_env = { name: "Deno", version: Deno.version.deno };
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
6
45
|
/**
|
|
7
46
|
* Base class for services that communicate with the API.
|
|
8
47
|
*/
|
|
@@ -13,15 +52,32 @@ class BaseService {
|
|
|
13
52
|
*/
|
|
14
53
|
constructor(params) {
|
|
15
54
|
this.params = params;
|
|
55
|
+
if (params.userAgent === false) {
|
|
56
|
+
this.userAgent = undefined;
|
|
57
|
+
}
|
|
58
|
+
else {
|
|
59
|
+
this.userAgent = buildUserAgent(params.userAgent || {});
|
|
60
|
+
}
|
|
16
61
|
}
|
|
17
62
|
async fetch(input, init) {
|
|
18
|
-
init = init
|
|
19
|
-
|
|
20
|
-
init.headers = {
|
|
63
|
+
init = { ...DEFAULT_FETCH_INIT, ...init };
|
|
64
|
+
let headers = {
|
|
21
65
|
Authorization: this.params.apiKey,
|
|
22
66
|
"Content-Type": "application/json",
|
|
23
|
-
...init.headers,
|
|
24
67
|
};
|
|
68
|
+
if (DEFAULT_FETCH_INIT?.headers)
|
|
69
|
+
headers = { ...headers, ...DEFAULT_FETCH_INIT.headers };
|
|
70
|
+
if (init?.headers)
|
|
71
|
+
headers = { ...headers, ...init.headers };
|
|
72
|
+
if (this.userAgent) {
|
|
73
|
+
headers["User-Agent"] = this.userAgent;
|
|
74
|
+
// chromium browsers have a bug where the user agent can't be modified
|
|
75
|
+
if (typeof window !== "undefined" && "chrome" in window) {
|
|
76
|
+
headers["AssemblyAI-Agent"] =
|
|
77
|
+
this.userAgent;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
init.headers = headers;
|
|
25
81
|
if (!input.startsWith("http"))
|
|
26
82
|
input = this.params.baseUrl + input;
|
|
27
83
|
const response = await fetch(input, init);
|
|
@@ -74,6 +130,9 @@ class LemurService extends BaseService {
|
|
|
74
130
|
body: JSON.stringify(params),
|
|
75
131
|
});
|
|
76
132
|
}
|
|
133
|
+
getResponse(id) {
|
|
134
|
+
return this.fetchJson(`/lemur/v3/${id}`);
|
|
135
|
+
}
|
|
77
136
|
/**
|
|
78
137
|
* Delete the data for a previously submitted LeMUR request.
|
|
79
138
|
* @param id - ID of the LeMUR request
|
|
@@ -131,7 +190,14 @@ class RealtimeError extends Error {
|
|
|
131
190
|
const defaultRealtimeUrl = "wss://api.assemblyai.com/v2/realtime/ws";
|
|
132
191
|
const forceEndOfUtteranceMessage = `{"force_end_utterance":true}`;
|
|
133
192
|
const terminateSessionMessage = `{"terminate_session":true}`;
|
|
193
|
+
/**
|
|
194
|
+
* RealtimeTranscriber connects to the Streaming Speech-to-Text API and lets you transcribe audio in real-time.
|
|
195
|
+
*/
|
|
134
196
|
class RealtimeTranscriber {
|
|
197
|
+
/**
|
|
198
|
+
* Create a new RealtimeTranscriber.
|
|
199
|
+
* @param params - Parameters to configure the RealtimeTranscriber
|
|
200
|
+
*/
|
|
135
201
|
constructor(params) {
|
|
136
202
|
this.listeners = {};
|
|
137
203
|
this.realtimeUrl = params.realtimeUrl ?? defaultRealtimeUrl;
|
|
@@ -171,10 +237,19 @@ class RealtimeTranscriber {
|
|
|
171
237
|
url.search = searchParams.toString();
|
|
172
238
|
return url;
|
|
173
239
|
}
|
|
240
|
+
/**
|
|
241
|
+
* Add a listener for an event.
|
|
242
|
+
* @param event - The event to listen for.
|
|
243
|
+
* @param listener - The function to call when the event is emitted.
|
|
244
|
+
*/
|
|
174
245
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
175
246
|
on(event, listener) {
|
|
176
247
|
this.listeners[event] = listener;
|
|
177
248
|
}
|
|
249
|
+
/**
|
|
250
|
+
* Connect to the server and begin a new session.
|
|
251
|
+
* @returns A promise that resolves when the connection is established and the session begins.
|
|
252
|
+
*/
|
|
178
253
|
connect() {
|
|
179
254
|
return new Promise((resolve) => {
|
|
180
255
|
if (this.socket) {
|
|
@@ -253,9 +328,17 @@ class RealtimeTranscriber {
|
|
|
253
328
|
};
|
|
254
329
|
});
|
|
255
330
|
}
|
|
331
|
+
/**
|
|
332
|
+
* Send audio data to the server.
|
|
333
|
+
* @param audio - The audio data to send to the server.
|
|
334
|
+
*/
|
|
256
335
|
sendAudio(audio) {
|
|
257
336
|
this.send(audio);
|
|
258
337
|
}
|
|
338
|
+
/**
|
|
339
|
+
* Create a writable stream that can be used to send audio data to the server.
|
|
340
|
+
* @returns A writable stream that can be used to send audio data to the server.
|
|
341
|
+
*/
|
|
259
342
|
stream() {
|
|
260
343
|
return new WritableStream({
|
|
261
344
|
write: (chunk) => {
|
|
@@ -283,6 +366,11 @@ class RealtimeTranscriber {
|
|
|
283
366
|
}
|
|
284
367
|
this.socket.send(data);
|
|
285
368
|
}
|
|
369
|
+
/**
|
|
370
|
+
* Close the connection to the server.
|
|
371
|
+
* @param waitForSessionTermination - If true, the method will wait for the session to be terminated before closing the connection.
|
|
372
|
+
* While waiting for the session to be terminated, you will receive the final transcript and session information.
|
|
373
|
+
*/
|
|
286
374
|
async close(waitForSessionTermination = true) {
|
|
287
375
|
if (this.socket) {
|
|
288
376
|
if (this.socket.readyState === this.socket.OPEN) {
|
|
@@ -348,6 +436,8 @@ function getPath(path) {
|
|
|
348
436
|
return null;
|
|
349
437
|
if (path.startsWith("https"))
|
|
350
438
|
return null;
|
|
439
|
+
if (path.startsWith("data:"))
|
|
440
|
+
return null;
|
|
351
441
|
if (path.startsWith("file://"))
|
|
352
442
|
return path.substring(7);
|
|
353
443
|
if (path.startsWith("file:"))
|
|
@@ -389,8 +479,13 @@ class TranscriptService extends BaseService {
|
|
|
389
479
|
audioUrl = await this.files.upload(path);
|
|
390
480
|
}
|
|
391
481
|
else {
|
|
392
|
-
|
|
393
|
-
|
|
482
|
+
if (audio.startsWith("data:")) {
|
|
483
|
+
audioUrl = await this.files.upload(audio);
|
|
484
|
+
}
|
|
485
|
+
else {
|
|
486
|
+
// audio is not a local path, and not a data-URI, assume it's a normal URL
|
|
487
|
+
audioUrl = audio;
|
|
488
|
+
}
|
|
394
489
|
}
|
|
395
490
|
}
|
|
396
491
|
else {
|
|
@@ -541,13 +636,43 @@ class TranscriptService extends BaseService {
|
|
|
541
636
|
return await response.text();
|
|
542
637
|
}
|
|
543
638
|
/**
|
|
544
|
-
* Retrieve
|
|
639
|
+
* Retrieve the redacted audio URL of a transcript.
|
|
545
640
|
* @param id - The identifier of the transcript.
|
|
546
|
-
* @returns A promise that resolves to the
|
|
641
|
+
* @returns A promise that resolves to the details of the redacted audio.
|
|
642
|
+
* @deprecated Use `redactedAudio` instead.
|
|
547
643
|
*/
|
|
548
644
|
redactions(id) {
|
|
645
|
+
return this.redactedAudio(id);
|
|
646
|
+
}
|
|
647
|
+
/**
|
|
648
|
+
* Retrieve the redacted audio URL of a transcript.
|
|
649
|
+
* @param id - The identifier of the transcript.
|
|
650
|
+
* @returns A promise that resolves to the details of the redacted audio.
|
|
651
|
+
*/
|
|
652
|
+
redactedAudio(id) {
|
|
549
653
|
return this.fetchJson(`/v2/transcript/${id}/redacted-audio`);
|
|
550
654
|
}
|
|
655
|
+
/**
|
|
656
|
+
* Retrieve the redacted audio file of a transcript.
|
|
657
|
+
* @param id - The identifier of the transcript.
|
|
658
|
+
* @returns A promise that resolves to the fetch HTTP response of the redacted audio file.
|
|
659
|
+
*/
|
|
660
|
+
async redactedAudioFile(id) {
|
|
661
|
+
const { redacted_audio_url, status } = await this.redactedAudio(id);
|
|
662
|
+
if (status !== "redacted_audio_ready") {
|
|
663
|
+
throw new Error(`Redacted audio status is ${status}`);
|
|
664
|
+
}
|
|
665
|
+
const response = await fetch(redacted_audio_url);
|
|
666
|
+
if (!response.ok) {
|
|
667
|
+
throw new Error(`Failed to fetch redacted audio: ${response.statusText}`);
|
|
668
|
+
}
|
|
669
|
+
return {
|
|
670
|
+
arrayBuffer: response.arrayBuffer.bind(response),
|
|
671
|
+
blob: response.blob.bind(response),
|
|
672
|
+
body: response.body,
|
|
673
|
+
bodyUsed: response.bodyUsed,
|
|
674
|
+
};
|
|
675
|
+
}
|
|
551
676
|
}
|
|
552
677
|
function deprecateConformer2(params) {
|
|
553
678
|
if (!params)
|
|
@@ -567,8 +692,14 @@ class FileService extends BaseService {
|
|
|
567
692
|
*/
|
|
568
693
|
async upload(input) {
|
|
569
694
|
let fileData;
|
|
570
|
-
if (typeof input === "string")
|
|
571
|
-
|
|
695
|
+
if (typeof input === "string") {
|
|
696
|
+
if (input.startsWith("data:")) {
|
|
697
|
+
fileData = dataUrlToBlob(input);
|
|
698
|
+
}
|
|
699
|
+
else {
|
|
700
|
+
fileData = await readFile(input);
|
|
701
|
+
}
|
|
702
|
+
}
|
|
572
703
|
else
|
|
573
704
|
fileData = input;
|
|
574
705
|
const data = await this.fetchJson("/v2/upload", {
|
|
@@ -582,6 +713,17 @@ class FileService extends BaseService {
|
|
|
582
713
|
return data.upload_url;
|
|
583
714
|
}
|
|
584
715
|
}
|
|
716
|
+
function dataUrlToBlob(dataUrl) {
|
|
717
|
+
const arr = dataUrl.split(",");
|
|
718
|
+
const mime = arr[0].match(/:(.*?);/)[1];
|
|
719
|
+
const bstr = atob(arr[1]);
|
|
720
|
+
let n = bstr.length;
|
|
721
|
+
const u8arr = new Uint8Array(n);
|
|
722
|
+
while (n--) {
|
|
723
|
+
u8arr[n] = bstr.charCodeAt(n);
|
|
724
|
+
}
|
|
725
|
+
return new Blob([u8arr], { type: mime });
|
|
726
|
+
}
|
|
585
727
|
|
|
586
728
|
const defaultBaseUrl = "https://api.assemblyai.com";
|
|
587
729
|
class AssemblyAI {
|
|
@@ -591,8 +733,9 @@ class AssemblyAI {
|
|
|
591
733
|
*/
|
|
592
734
|
constructor(params) {
|
|
593
735
|
params.baseUrl = params.baseUrl || defaultBaseUrl;
|
|
594
|
-
if (params.baseUrl && params.baseUrl.endsWith("/"))
|
|
736
|
+
if (params.baseUrl && params.baseUrl.endsWith("/")) {
|
|
595
737
|
params.baseUrl = params.baseUrl.slice(0, -1);
|
|
738
|
+
}
|
|
596
739
|
this.files = new FileService(params);
|
|
597
740
|
this.transcripts = new TranscriptService(params, this.files);
|
|
598
741
|
this.lemur = new LemurService(params);
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const DEFAULT_FETCH_INIT: Record<string, unknown>;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const DEFAULT_FETCH_INIT: Record<string, unknown>;
|
package/dist/services/base.d.ts
CHANGED
|
@@ -1,10 +1,17 @@
|
|
|
1
|
-
import { LemurSummaryParams, LemurActionItemsParams, LemurQuestionAnswerParams, LemurTaskParams, LemurSummaryResponse, LemurQuestionAnswerResponse, LemurActionItemsResponse, LemurTaskResponse, PurgeLemurRequestDataResponse } from "../..";
|
|
1
|
+
import { LemurSummaryParams, LemurActionItemsParams, LemurQuestionAnswerParams, LemurTaskParams, LemurSummaryResponse, LemurQuestionAnswerResponse, LemurActionItemsResponse, LemurTaskResponse, PurgeLemurRequestDataResponse, LemurResponse } from "../..";
|
|
2
2
|
import { BaseService } from "../base";
|
|
3
3
|
export declare class LemurService extends BaseService {
|
|
4
4
|
summary(params: LemurSummaryParams): Promise<LemurSummaryResponse>;
|
|
5
5
|
questionAnswer(params: LemurQuestionAnswerParams): Promise<LemurQuestionAnswerResponse>;
|
|
6
6
|
actionItems(params: LemurActionItemsParams): Promise<LemurActionItemsResponse>;
|
|
7
7
|
task(params: LemurTaskParams): Promise<LemurTaskResponse>;
|
|
8
|
+
/**
|
|
9
|
+
* Retrieve a LeMUR response that was previously generated.
|
|
10
|
+
* @param id - The ID of the LeMUR request you previously made. This would be found in the response of the original request.
|
|
11
|
+
* @returns The LeMUR response.
|
|
12
|
+
*/
|
|
13
|
+
getResponse<T extends LemurResponse>(id: string): Promise<T>;
|
|
14
|
+
getResponse(id: string): Promise<LemurResponse>;
|
|
8
15
|
/**
|
|
9
16
|
* Delete the data for a previously submitted LeMUR request.
|
|
10
17
|
* @param id - ID of the LeMUR request
|
|
@@ -1,4 +1,7 @@
|
|
|
1
1
|
import { RealtimeTranscriberParams, RealtimeTranscript, PartialTranscript, FinalTranscript, SessionBeginsEventData, AudioData, SessionInformation } from "../..";
|
|
2
|
+
/**
|
|
3
|
+
* RealtimeTranscriber connects to the Streaming Speech-to-Text API and lets you transcribe audio in real-time.
|
|
4
|
+
*/
|
|
2
5
|
export declare class RealtimeTranscriber {
|
|
3
6
|
private realtimeUrl;
|
|
4
7
|
private sampleRate;
|
|
@@ -11,17 +14,69 @@ export declare class RealtimeTranscriber {
|
|
|
11
14
|
private socket?;
|
|
12
15
|
private listeners;
|
|
13
16
|
private sessionTerminatedResolve?;
|
|
17
|
+
/**
|
|
18
|
+
* Create a new RealtimeTranscriber.
|
|
19
|
+
* @param params - Parameters to configure the RealtimeTranscriber
|
|
20
|
+
*/
|
|
14
21
|
constructor(params: RealtimeTranscriberParams);
|
|
15
22
|
private connectionUrl;
|
|
23
|
+
/**
|
|
24
|
+
* Listen for the open event which is emitted when the connection is established and the session begins.
|
|
25
|
+
* @param event - The open event.
|
|
26
|
+
* @param listener - The function to call when the event is emitted.
|
|
27
|
+
*/
|
|
16
28
|
on(event: "open", listener: (event: SessionBeginsEventData) => void): void;
|
|
29
|
+
/**
|
|
30
|
+
* Listen for the transcript event which is emitted when a partian or final transcript is received.
|
|
31
|
+
* @param event - The transcript event.
|
|
32
|
+
* @param listener - The function to call when the event is emitted.
|
|
33
|
+
*/
|
|
17
34
|
on(event: "transcript", listener: (transcript: RealtimeTranscript) => void): void;
|
|
35
|
+
/**
|
|
36
|
+
* Listen for the partial transcript event which is emitted when a partial transcript is received.
|
|
37
|
+
* @param event - The partial transcript event.
|
|
38
|
+
* @param listener - The function to call when the event is emitted.
|
|
39
|
+
*/
|
|
18
40
|
on(event: "transcript.partial", listener: (transcript: PartialTranscript) => void): void;
|
|
41
|
+
/**
|
|
42
|
+
* Listen for the final transcript event which is emitted when a final transcript is received.
|
|
43
|
+
* @param event - The final transcript event.
|
|
44
|
+
* @param listener - The function to call when the event is emitted.
|
|
45
|
+
*/
|
|
19
46
|
on(event: "transcript.final", listener: (transcript: FinalTranscript) => void): void;
|
|
47
|
+
/**
|
|
48
|
+
* Listen for the session information event which is emitted when session information is received.
|
|
49
|
+
* The session information is sent right before the session is terminated.
|
|
50
|
+
* @param event - The session information event.
|
|
51
|
+
* @param listener - The function to call when the event is emitted.
|
|
52
|
+
*/
|
|
20
53
|
on(event: "session_information", listener: (info: SessionInformation) => void): void;
|
|
54
|
+
/**
|
|
55
|
+
* Listen for the error event which is emitted when an error occurs.
|
|
56
|
+
* @param event - The error event.
|
|
57
|
+
* @param listener - The function to call when the event is emitted.
|
|
58
|
+
*/
|
|
21
59
|
on(event: "error", listener: (error: Error) => void): void;
|
|
60
|
+
/**
|
|
61
|
+
* Listen for the close event which is emitted when the connection is closed.
|
|
62
|
+
* @param event - The close event.
|
|
63
|
+
* @param listener - The function to call when the event is emitted.
|
|
64
|
+
*/
|
|
22
65
|
on(event: "close", listener: (code: number, reason: string) => void): void;
|
|
66
|
+
/**
|
|
67
|
+
* Connect to the server and begin a new session.
|
|
68
|
+
* @returns A promise that resolves when the connection is established and the session begins.
|
|
69
|
+
*/
|
|
23
70
|
connect(): Promise<SessionBeginsEventData>;
|
|
71
|
+
/**
|
|
72
|
+
* Send audio data to the server.
|
|
73
|
+
* @param audio - The audio data to send to the server.
|
|
74
|
+
*/
|
|
24
75
|
sendAudio(audio: AudioData): void;
|
|
76
|
+
/**
|
|
77
|
+
* Create a writable stream that can be used to send audio data to the server.
|
|
78
|
+
* @returns A writable stream that can be used to send audio data to the server.
|
|
79
|
+
*/
|
|
25
80
|
stream(): WritableStream<AudioData>;
|
|
26
81
|
/**
|
|
27
82
|
* Manually end an utterance
|
|
@@ -34,6 +89,11 @@ export declare class RealtimeTranscriber {
|
|
|
34
89
|
*/
|
|
35
90
|
configureEndUtteranceSilenceThreshold(threshold: number): void;
|
|
36
91
|
private send;
|
|
92
|
+
/**
|
|
93
|
+
* Close the connection to the server.
|
|
94
|
+
* @param waitForSessionTermination - If true, the method will wait for the session to be terminated before closing the connection.
|
|
95
|
+
* While waiting for the session to be terminated, you will receive the final transcript and session information.
|
|
96
|
+
*/
|
|
37
97
|
close(waitForSessionTermination?: boolean): Promise<void>;
|
|
38
98
|
}
|
|
39
99
|
/**
|