assemblyai 4.5.0-beta.0 → 4.5.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 +34 -0
- package/README.md +53 -9
- package/dist/assemblyai.streaming.umd.js +288 -0
- package/dist/assemblyai.streaming.umd.min.js +1 -0
- package/dist/assemblyai.umd.js +131 -12
- package/dist/assemblyai.umd.min.js +1 -1
- package/dist/browser.mjs +125 -11
- package/dist/bun.mjs +125 -11
- package/dist/deno.mjs +125 -11
- package/dist/exports/index.d.ts +2 -0
- package/dist/exports/streaming.d.ts +4 -0
- package/dist/index.cjs +131 -12
- package/dist/index.mjs +131 -12
- package/dist/node.cjs +125 -11
- package/dist/node.mjs +125 -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/transcripts/index.d.ts +16 -3
- package/dist/streaming.browser.mjs +239 -0
- package/dist/streaming.cjs +277 -0
- package/dist/streaming.mjs +274 -0
- package/dist/types/index.d.ts +7 -0
- package/dist/types/openapi.generated.d.ts +60 -23
- 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 +722 -0
- package/docs/reference-types-from-js.md +27 -0
- package/package.json +39 -19
- 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/transcripts/index.ts +41 -4
- package/src/types/index.ts +9 -0
- package/src/types/openapi.generated.ts +198 -41
- 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.5.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
|
|
@@ -407,6 +469,8 @@ function getPath(path) {
|
|
|
407
469
|
return null;
|
|
408
470
|
if (path.startsWith("https"))
|
|
409
471
|
return null;
|
|
472
|
+
if (path.startsWith("data:"))
|
|
473
|
+
return null;
|
|
410
474
|
if (path.startsWith("file://"))
|
|
411
475
|
return path.substring(7);
|
|
412
476
|
if (path.startsWith("file:"))
|
|
@@ -451,8 +515,13 @@ class TranscriptService extends BaseService {
|
|
|
451
515
|
audioUrl = yield this.files.upload(path);
|
|
452
516
|
}
|
|
453
517
|
else {
|
|
454
|
-
|
|
455
|
-
|
|
518
|
+
if (audio.startsWith("data:")) {
|
|
519
|
+
audioUrl = yield this.files.upload(audio);
|
|
520
|
+
}
|
|
521
|
+
else {
|
|
522
|
+
// audio is not a local path, and not a data-URI, assume it's a normal URL
|
|
523
|
+
audioUrl = audio;
|
|
524
|
+
}
|
|
456
525
|
}
|
|
457
526
|
}
|
|
458
527
|
else {
|
|
@@ -617,13 +686,45 @@ class TranscriptService extends BaseService {
|
|
|
617
686
|
});
|
|
618
687
|
}
|
|
619
688
|
/**
|
|
620
|
-
* Retrieve
|
|
689
|
+
* Retrieve the redacted audio URL of a transcript.
|
|
621
690
|
* @param id - The identifier of the transcript.
|
|
622
|
-
* @returns A promise that resolves to the
|
|
691
|
+
* @returns A promise that resolves to the details of the redacted audio.
|
|
692
|
+
* @deprecated Use `redactedAudio` instead.
|
|
623
693
|
*/
|
|
624
694
|
redactions(id) {
|
|
695
|
+
return this.redactedAudio(id);
|
|
696
|
+
}
|
|
697
|
+
/**
|
|
698
|
+
* Retrieve the redacted audio URL of a transcript.
|
|
699
|
+
* @param id - The identifier of the transcript.
|
|
700
|
+
* @returns A promise that resolves to the details of the redacted audio.
|
|
701
|
+
*/
|
|
702
|
+
redactedAudio(id) {
|
|
625
703
|
return this.fetchJson(`/v2/transcript/${id}/redacted-audio`);
|
|
626
704
|
}
|
|
705
|
+
/**
|
|
706
|
+
* Retrieve the redacted audio file of a transcript.
|
|
707
|
+
* @param id - The identifier of the transcript.
|
|
708
|
+
* @returns A promise that resolves to the fetch HTTP response of the redacted audio file.
|
|
709
|
+
*/
|
|
710
|
+
redactedAudioFile(id) {
|
|
711
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
712
|
+
const { redacted_audio_url, status } = yield this.redactedAudio(id);
|
|
713
|
+
if (status !== "redacted_audio_ready") {
|
|
714
|
+
throw new Error(`Redacted audio status is ${status}`);
|
|
715
|
+
}
|
|
716
|
+
const response = yield fetch(redacted_audio_url);
|
|
717
|
+
if (!response.ok) {
|
|
718
|
+
throw new Error(`Failed to fetch redacted audio: ${response.statusText}`);
|
|
719
|
+
}
|
|
720
|
+
return {
|
|
721
|
+
arrayBuffer: response.arrayBuffer.bind(response),
|
|
722
|
+
blob: response.blob.bind(response),
|
|
723
|
+
body: response.body,
|
|
724
|
+
bodyUsed: response.bodyUsed,
|
|
725
|
+
};
|
|
726
|
+
});
|
|
727
|
+
}
|
|
627
728
|
}
|
|
628
729
|
function deprecateConformer2(params) {
|
|
629
730
|
if (!params)
|
|
@@ -650,8 +751,14 @@ class FileService extends BaseService {
|
|
|
650
751
|
upload(input) {
|
|
651
752
|
return __awaiter(this, void 0, void 0, function* () {
|
|
652
753
|
let fileData;
|
|
653
|
-
if (typeof input === "string")
|
|
654
|
-
|
|
754
|
+
if (typeof input === "string") {
|
|
755
|
+
if (input.startsWith("data:")) {
|
|
756
|
+
fileData = dataUrlToBlob(input);
|
|
757
|
+
}
|
|
758
|
+
else {
|
|
759
|
+
fileData = yield readFile();
|
|
760
|
+
}
|
|
761
|
+
}
|
|
655
762
|
else
|
|
656
763
|
fileData = input;
|
|
657
764
|
const data = yield this.fetchJson("/v2/upload", {
|
|
@@ -666,6 +773,17 @@ class FileService extends BaseService {
|
|
|
666
773
|
});
|
|
667
774
|
}
|
|
668
775
|
}
|
|
776
|
+
function dataUrlToBlob(dataUrl) {
|
|
777
|
+
const arr = dataUrl.split(",");
|
|
778
|
+
const mime = arr[0].match(/:(.*?);/)[1];
|
|
779
|
+
const bstr = atob(arr[1]);
|
|
780
|
+
let n = bstr.length;
|
|
781
|
+
const u8arr = new Uint8Array(n);
|
|
782
|
+
while (n--) {
|
|
783
|
+
u8arr[n] = bstr.charCodeAt(n);
|
|
784
|
+
}
|
|
785
|
+
return new Blob([u8arr], { type: mime });
|
|
786
|
+
}
|
|
669
787
|
|
|
670
788
|
const defaultBaseUrl = "https://api.assemblyai.com";
|
|
671
789
|
class AssemblyAI {
|
|
@@ -675,8 +793,9 @@ class AssemblyAI {
|
|
|
675
793
|
*/
|
|
676
794
|
constructor(params) {
|
|
677
795
|
params.baseUrl = params.baseUrl || defaultBaseUrl;
|
|
678
|
-
if (params.baseUrl && params.baseUrl.endsWith("/"))
|
|
796
|
+
if (params.baseUrl && params.baseUrl.endsWith("/")) {
|
|
679
797
|
params.baseUrl = params.baseUrl.slice(0, -1);
|
|
798
|
+
}
|
|
680
799
|
this.files = new FileService(params);
|
|
681
800
|
this.transcripts = new TranscriptService(params, this.files);
|
|
682
801
|
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.5.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
|
|
@@ -405,6 +467,8 @@ function getPath(path) {
|
|
|
405
467
|
return null;
|
|
406
468
|
if (path.startsWith("https"))
|
|
407
469
|
return null;
|
|
470
|
+
if (path.startsWith("data:"))
|
|
471
|
+
return null;
|
|
408
472
|
if (path.startsWith("file://"))
|
|
409
473
|
return path.substring(7);
|
|
410
474
|
if (path.startsWith("file:"))
|
|
@@ -449,8 +513,13 @@ class TranscriptService extends BaseService {
|
|
|
449
513
|
audioUrl = yield this.files.upload(path);
|
|
450
514
|
}
|
|
451
515
|
else {
|
|
452
|
-
|
|
453
|
-
|
|
516
|
+
if (audio.startsWith("data:")) {
|
|
517
|
+
audioUrl = yield this.files.upload(audio);
|
|
518
|
+
}
|
|
519
|
+
else {
|
|
520
|
+
// audio is not a local path, and not a data-URI, assume it's a normal URL
|
|
521
|
+
audioUrl = audio;
|
|
522
|
+
}
|
|
454
523
|
}
|
|
455
524
|
}
|
|
456
525
|
else {
|
|
@@ -615,13 +684,45 @@ class TranscriptService extends BaseService {
|
|
|
615
684
|
});
|
|
616
685
|
}
|
|
617
686
|
/**
|
|
618
|
-
* Retrieve
|
|
687
|
+
* Retrieve the redacted audio URL of a transcript.
|
|
619
688
|
* @param id - The identifier of the transcript.
|
|
620
|
-
* @returns A promise that resolves to the
|
|
689
|
+
* @returns A promise that resolves to the details of the redacted audio.
|
|
690
|
+
* @deprecated Use `redactedAudio` instead.
|
|
621
691
|
*/
|
|
622
692
|
redactions(id) {
|
|
693
|
+
return this.redactedAudio(id);
|
|
694
|
+
}
|
|
695
|
+
/**
|
|
696
|
+
* Retrieve the redacted audio URL of a transcript.
|
|
697
|
+
* @param id - The identifier of the transcript.
|
|
698
|
+
* @returns A promise that resolves to the details of the redacted audio.
|
|
699
|
+
*/
|
|
700
|
+
redactedAudio(id) {
|
|
623
701
|
return this.fetchJson(`/v2/transcript/${id}/redacted-audio`);
|
|
624
702
|
}
|
|
703
|
+
/**
|
|
704
|
+
* Retrieve the redacted audio file of a transcript.
|
|
705
|
+
* @param id - The identifier of the transcript.
|
|
706
|
+
* @returns A promise that resolves to the fetch HTTP response of the redacted audio file.
|
|
707
|
+
*/
|
|
708
|
+
redactedAudioFile(id) {
|
|
709
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
710
|
+
const { redacted_audio_url, status } = yield this.redactedAudio(id);
|
|
711
|
+
if (status !== "redacted_audio_ready") {
|
|
712
|
+
throw new Error(`Redacted audio status is ${status}`);
|
|
713
|
+
}
|
|
714
|
+
const response = yield fetch(redacted_audio_url);
|
|
715
|
+
if (!response.ok) {
|
|
716
|
+
throw new Error(`Failed to fetch redacted audio: ${response.statusText}`);
|
|
717
|
+
}
|
|
718
|
+
return {
|
|
719
|
+
arrayBuffer: response.arrayBuffer.bind(response),
|
|
720
|
+
blob: response.blob.bind(response),
|
|
721
|
+
body: response.body,
|
|
722
|
+
bodyUsed: response.bodyUsed,
|
|
723
|
+
};
|
|
724
|
+
});
|
|
725
|
+
}
|
|
625
726
|
}
|
|
626
727
|
function deprecateConformer2(params) {
|
|
627
728
|
if (!params)
|
|
@@ -648,8 +749,14 @@ class FileService extends BaseService {
|
|
|
648
749
|
upload(input) {
|
|
649
750
|
return __awaiter(this, void 0, void 0, function* () {
|
|
650
751
|
let fileData;
|
|
651
|
-
if (typeof input === "string")
|
|
652
|
-
|
|
752
|
+
if (typeof input === "string") {
|
|
753
|
+
if (input.startsWith("data:")) {
|
|
754
|
+
fileData = dataUrlToBlob(input);
|
|
755
|
+
}
|
|
756
|
+
else {
|
|
757
|
+
fileData = yield readFile();
|
|
758
|
+
}
|
|
759
|
+
}
|
|
653
760
|
else
|
|
654
761
|
fileData = input;
|
|
655
762
|
const data = yield this.fetchJson("/v2/upload", {
|
|
@@ -664,6 +771,17 @@ class FileService extends BaseService {
|
|
|
664
771
|
});
|
|
665
772
|
}
|
|
666
773
|
}
|
|
774
|
+
function dataUrlToBlob(dataUrl) {
|
|
775
|
+
const arr = dataUrl.split(",");
|
|
776
|
+
const mime = arr[0].match(/:(.*?);/)[1];
|
|
777
|
+
const bstr = atob(arr[1]);
|
|
778
|
+
let n = bstr.length;
|
|
779
|
+
const u8arr = new Uint8Array(n);
|
|
780
|
+
while (n--) {
|
|
781
|
+
u8arr[n] = bstr.charCodeAt(n);
|
|
782
|
+
}
|
|
783
|
+
return new Blob([u8arr], { type: mime });
|
|
784
|
+
}
|
|
667
785
|
|
|
668
786
|
const defaultBaseUrl = "https://api.assemblyai.com";
|
|
669
787
|
class AssemblyAI {
|
|
@@ -673,8 +791,9 @@ class AssemblyAI {
|
|
|
673
791
|
*/
|
|
674
792
|
constructor(params) {
|
|
675
793
|
params.baseUrl = params.baseUrl || defaultBaseUrl;
|
|
676
|
-
if (params.baseUrl && params.baseUrl.endsWith("/"))
|
|
794
|
+
if (params.baseUrl && params.baseUrl.endsWith("/")) {
|
|
677
795
|
params.baseUrl = params.baseUrl.slice(0, -1);
|
|
796
|
+
}
|
|
678
797
|
this.files = new FileService(params);
|
|
679
798
|
this.transcripts = new TranscriptService(params, this.files);
|
|
680
799
|
this.lemur = new LemurService(params);
|
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.5.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
|
|
@@ -350,6 +409,8 @@ function getPath(path) {
|
|
|
350
409
|
return null;
|
|
351
410
|
if (path.startsWith("https"))
|
|
352
411
|
return null;
|
|
412
|
+
if (path.startsWith("data:"))
|
|
413
|
+
return null;
|
|
353
414
|
if (path.startsWith("file://"))
|
|
354
415
|
return path.substring(7);
|
|
355
416
|
if (path.startsWith("file:"))
|
|
@@ -391,8 +452,13 @@ class TranscriptService extends BaseService {
|
|
|
391
452
|
audioUrl = await this.files.upload(path);
|
|
392
453
|
}
|
|
393
454
|
else {
|
|
394
|
-
|
|
395
|
-
|
|
455
|
+
if (audio.startsWith("data:")) {
|
|
456
|
+
audioUrl = await this.files.upload(audio);
|
|
457
|
+
}
|
|
458
|
+
else {
|
|
459
|
+
// audio is not a local path, and not a data-URI, assume it's a normal URL
|
|
460
|
+
audioUrl = audio;
|
|
461
|
+
}
|
|
396
462
|
}
|
|
397
463
|
}
|
|
398
464
|
else {
|
|
@@ -543,13 +609,43 @@ class TranscriptService extends BaseService {
|
|
|
543
609
|
return await response.text();
|
|
544
610
|
}
|
|
545
611
|
/**
|
|
546
|
-
* Retrieve
|
|
612
|
+
* Retrieve the redacted audio URL of a transcript.
|
|
547
613
|
* @param id - The identifier of the transcript.
|
|
548
|
-
* @returns A promise that resolves to the
|
|
614
|
+
* @returns A promise that resolves to the details of the redacted audio.
|
|
615
|
+
* @deprecated Use `redactedAudio` instead.
|
|
549
616
|
*/
|
|
550
617
|
redactions(id) {
|
|
618
|
+
return this.redactedAudio(id);
|
|
619
|
+
}
|
|
620
|
+
/**
|
|
621
|
+
* Retrieve the redacted audio URL of a transcript.
|
|
622
|
+
* @param id - The identifier of the transcript.
|
|
623
|
+
* @returns A promise that resolves to the details of the redacted audio.
|
|
624
|
+
*/
|
|
625
|
+
redactedAudio(id) {
|
|
551
626
|
return this.fetchJson(`/v2/transcript/${id}/redacted-audio`);
|
|
552
627
|
}
|
|
628
|
+
/**
|
|
629
|
+
* Retrieve the redacted audio file of a transcript.
|
|
630
|
+
* @param id - The identifier of the transcript.
|
|
631
|
+
* @returns A promise that resolves to the fetch HTTP response of the redacted audio file.
|
|
632
|
+
*/
|
|
633
|
+
async redactedAudioFile(id) {
|
|
634
|
+
const { redacted_audio_url, status } = await this.redactedAudio(id);
|
|
635
|
+
if (status !== "redacted_audio_ready") {
|
|
636
|
+
throw new Error(`Redacted audio status is ${status}`);
|
|
637
|
+
}
|
|
638
|
+
const response = await fetch(redacted_audio_url);
|
|
639
|
+
if (!response.ok) {
|
|
640
|
+
throw new Error(`Failed to fetch redacted audio: ${response.statusText}`);
|
|
641
|
+
}
|
|
642
|
+
return {
|
|
643
|
+
arrayBuffer: response.arrayBuffer.bind(response),
|
|
644
|
+
blob: response.blob.bind(response),
|
|
645
|
+
body: response.body,
|
|
646
|
+
bodyUsed: response.bodyUsed,
|
|
647
|
+
};
|
|
648
|
+
}
|
|
553
649
|
}
|
|
554
650
|
function deprecateConformer2(params) {
|
|
555
651
|
if (!params)
|
|
@@ -569,8 +665,14 @@ class FileService extends BaseService {
|
|
|
569
665
|
*/
|
|
570
666
|
async upload(input) {
|
|
571
667
|
let fileData;
|
|
572
|
-
if (typeof input === "string")
|
|
573
|
-
|
|
668
|
+
if (typeof input === "string") {
|
|
669
|
+
if (input.startsWith("data:")) {
|
|
670
|
+
fileData = dataUrlToBlob(input);
|
|
671
|
+
}
|
|
672
|
+
else {
|
|
673
|
+
fileData = await readFile(input);
|
|
674
|
+
}
|
|
675
|
+
}
|
|
574
676
|
else
|
|
575
677
|
fileData = input;
|
|
576
678
|
const data = await this.fetchJson("/v2/upload", {
|
|
@@ -584,6 +686,17 @@ class FileService extends BaseService {
|
|
|
584
686
|
return data.upload_url;
|
|
585
687
|
}
|
|
586
688
|
}
|
|
689
|
+
function dataUrlToBlob(dataUrl) {
|
|
690
|
+
const arr = dataUrl.split(",");
|
|
691
|
+
const mime = arr[0].match(/:(.*?);/)[1];
|
|
692
|
+
const bstr = atob(arr[1]);
|
|
693
|
+
let n = bstr.length;
|
|
694
|
+
const u8arr = new Uint8Array(n);
|
|
695
|
+
while (n--) {
|
|
696
|
+
u8arr[n] = bstr.charCodeAt(n);
|
|
697
|
+
}
|
|
698
|
+
return new Blob([u8arr], { type: mime });
|
|
699
|
+
}
|
|
587
700
|
|
|
588
701
|
const defaultBaseUrl = "https://api.assemblyai.com";
|
|
589
702
|
class AssemblyAI {
|
|
@@ -593,8 +706,9 @@ class AssemblyAI {
|
|
|
593
706
|
*/
|
|
594
707
|
constructor(params) {
|
|
595
708
|
params.baseUrl = params.baseUrl || defaultBaseUrl;
|
|
596
|
-
if (params.baseUrl && params.baseUrl.endsWith("/"))
|
|
709
|
+
if (params.baseUrl && params.baseUrl.endsWith("/")) {
|
|
597
710
|
params.baseUrl = params.baseUrl.slice(0, -1);
|
|
711
|
+
}
|
|
598
712
|
this.files = new FileService(params);
|
|
599
713
|
this.transcripts = new TranscriptService(params, this.files);
|
|
600
714
|
this.lemur = new LemurService(params);
|