Skip to content

Commit 2f75167

Browse files
authored
Merge pull request #58 from AssemblyAI/E07417BDFEA3614F5967B1520F8B2F61
Sync from internal repo (2024/06/12)
2 parents fe04165 + 737d9b6 commit 2f75167

File tree

10 files changed

+120
-17
lines changed

10 files changed

+120
-17
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Changelog
22

3+
## [4.4.7]
4+
5+
- Rename `TranscriptService.redactions` function to `TranscriptService.redactedAudio`.
6+
- Add `TranscriptService.redactedAudioFile` function.
7+
- Add `workerd` export to fix `cache` issue with `fetch` on Cloudflare Workers.
8+
39
## [4.4.6]
410

511
- Fix Rollup exports so \_\_SDK_VERSION\_\_ is properly replaced with the version of the SDK.

package.json

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "assemblyai",
3-
"version": "4.4.6",
3+
"version": "4.4.7",
44
"description": "The AssemblyAI JavaScript SDK provides an easy-to-use interface for interacting with the AssemblyAI API, which supports async and real-time transcription, as well as the latest LeMUR models.",
55
"engines": {
66
"node": ">=18"
@@ -16,7 +16,7 @@
1616
"types": "./dist/exports/index.d.ts",
1717
"default": "./dist/deno.mjs"
1818
},
19-
"workerd": "./dist/index.mjs",
19+
"workerd": "./dist/workerd.mjs",
2020
"browser": "./dist/browser.mjs",
2121
"react-native": "./dist/browser.mjs",
2222
"node": {
@@ -38,6 +38,10 @@
3838
"./package.json": "./package.json"
3939
},
4040
"imports": {
41+
"#fetch": {
42+
"workerd": "./src/polyfills/fetch/workerd.ts",
43+
"default": "./src/polyfills/fetch/default.ts"
44+
},
4145
"#fs": {
4246
"node": "./src/polyfills/fs/node.ts",
4347
"bun": "./src/polyfills/fs/bun.ts",

rollup.config.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,21 @@ module.exports = [
117117
},
118118
],
119119
},
120+
{
121+
input: "src/exports/index.ts",
122+
plugins: [
123+
replacePlugin,
124+
ts({ compilerOptions: { customConditions: ["workerd"] } }),
125+
],
126+
external: ["ws"],
127+
output: [
128+
{
129+
file: `./dist/workerd.mjs`,
130+
format: "es",
131+
exports: "named",
132+
},
133+
],
134+
},
120135
// Browser ESM build
121136
{
122137
input: "src/exports/index.ts",

src/polyfills/fetch/default.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export const DEFAULT_FETCH_INIT: Record<string, unknown> = {
2+
cache: "no-store",
3+
};

src/polyfills/fetch/workerd.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const DEFAULT_FETCH_INIT: Record<string, unknown> = {};

src/services/base.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { BaseServiceParams } from "..";
2-
import { Error as JsonError } from "..";
1+
import { DEFAULT_FETCH_INIT } from "#fetch";
2+
import { BaseServiceParams, Error as JsonError } from "..";
33
import { buildUserAgent } from "../utils/userAgent";
44

55
/**
@@ -22,13 +22,14 @@ export abstract class BaseService {
2222
input: string,
2323
init?: RequestInit | undefined,
2424
): Promise<Response> {
25-
init = init ?? {};
26-
let headers = init.headers ?? {};
27-
headers = {
25+
init = { ...DEFAULT_FETCH_INIT, ...init };
26+
let headers = {
2827
Authorization: this.params.apiKey,
2928
"Content-Type": "application/json",
30-
...init.headers,
3129
};
30+
if (DEFAULT_FETCH_INIT?.headers)
31+
headers = { ...headers, ...DEFAULT_FETCH_INIT.headers };
32+
if (init?.headers) headers = { ...headers, ...init.headers };
3233

3334
if (this.userAgent) {
3435
(headers as Record<string, string>)["User-Agent"] = this.userAgent;
@@ -40,7 +41,6 @@ export abstract class BaseService {
4041
}
4142
init.headers = headers;
4243

43-
init.cache = "no-store";
4444
if (!input.startsWith("http")) input = this.params.baseUrl + input;
4545

4646
const response = await fetch(input, init);

src/services/transcripts/index.ts

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import {
1616
TranscribeOptions,
1717
SubmitParams,
1818
SpeechModel,
19+
RedactedAudioFile,
1920
} from "../..";
2021
import { FileService } from "../files";
2122
import { getPath } from "../../utils/path";
@@ -244,15 +245,47 @@ export class TranscriptService extends BaseService {
244245
}
245246

246247
/**
247-
* Retrieve redactions of a transcript.
248+
* Retrieve the redacted audio URL of a transcript.
248249
* @param id - The identifier of the transcript.
249-
* @returns A promise that resolves to the subtitles text.
250+
* @returns A promise that resolves to the details of the redacted audio.
251+
* @deprecated Use `redactedAudio` instead.
250252
*/
251253
redactions(id: string): Promise<RedactedAudioResponse> {
254+
return this.redactedAudio(id);
255+
}
256+
257+
/**
258+
* Retrieve the redacted audio URL of a transcript.
259+
* @param id - The identifier of the transcript.
260+
* @returns A promise that resolves to the details of the redacted audio.
261+
*/
262+
redactedAudio(id: string): Promise<RedactedAudioResponse> {
252263
return this.fetchJson<RedactedAudioResponse>(
253264
`/v2/transcript/${id}/redacted-audio`,
254265
);
255266
}
267+
268+
/**
269+
* Retrieve the redacted audio file of a transcript.
270+
* @param id - The identifier of the transcript.
271+
* @returns A promise that resolves to the fetch HTTP response of the redacted audio file.
272+
*/
273+
async redactedAudioFile(id: string): Promise<RedactedAudioFile> {
274+
const { redacted_audio_url, status } = await this.redactedAudio(id);
275+
if (status !== "redacted_audio_ready") {
276+
throw new Error(`Redacted audio status is ${status}`);
277+
}
278+
const response = await fetch(redacted_audio_url);
279+
if (!response.ok) {
280+
throw new Error(`Failed to fetch redacted audio: ${response.statusText}`);
281+
}
282+
return {
283+
arrayBuffer: response.arrayBuffer.bind(response),
284+
blob: response.blob.bind(response),
285+
body: response.body,
286+
bodyUsed: response.bodyUsed,
287+
};
288+
}
256289
}
257290

258291
function deprecateConformer2(params: { speech_model?: SpeechModel | null }) {

src/types/transcripts/index.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,13 @@ export type SubmitParams = TranscribeParams;
5454
* The options to transcribe an audio file, including polling options.
5555
*/
5656
export type TranscribeOptions = PollingOptions;
57+
58+
/**
59+
* The PII redacted audio file, transmitted over the network.
60+
*/
61+
export type RedactedAudioFile = {
62+
arrayBuffer: () => Promise<ArrayBuffer>;
63+
blob: () => Promise<Blob>;
64+
body: ReadableStream<Uint8Array> | null;
65+
bodyUsed: boolean;
66+
};

tests/integration/transcript.test.ts

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ describe("transcript", () => {
123123
expect(subtitle).toBeTruthy();
124124
});
125125

126-
it("should get redactions", async () => {
126+
it("should get redacted audio", async () => {
127127
const transcript = await client.transcripts.transcribe({
128128
audio: remoteAudioUrl,
129129
redact_pii: true,
@@ -132,19 +132,33 @@ describe("transcript", () => {
132132
redact_pii_policies: ["medical_condition"],
133133
redact_pii_sub: "hash",
134134
});
135-
const redactedAudioResponse = await client.transcripts.redactions(
135+
const redactedAudioResponse = await client.transcripts.redactedAudio(
136136
transcript.id,
137137
);
138138
expect(redactedAudioResponse.status).toBe("redacted_audio_ready");
139139
expect(redactedAudioResponse.redacted_audio_url).toBeTruthy();
140-
});
140+
}, 30_000);
141+
142+
it("should get redacted audio file", async () => {
143+
const transcript = await client.transcripts.transcribe({
144+
audio: remoteAudioUrl,
145+
redact_pii: true,
146+
redact_pii_audio: true,
147+
redact_pii_audio_quality: "wav",
148+
redact_pii_policies: ["medical_condition"],
149+
redact_pii_sub: "hash",
150+
});
151+
const redactedAudioResponse = await client.transcripts.redactedAudioFile(
152+
transcript.id,
153+
);
154+
expect((await redactedAudioResponse.blob()).size).toBeGreaterThan(0);
155+
}, 60_000);
141156

142157
it("should word search", async () => {
143158
const searchResponse = await client.transcripts.wordSearch(
144159
knownTranscriptId,
145160
["Giants"],
146161
);
147-
console.log(searchResponse);
148162
expect(searchResponse.id).toBe(knownTranscriptId);
149163
expect(searchResponse.total_count).toBeGreaterThan(0);
150164
expect(Array.isArray(searchResponse.matches)).toBeTruthy();

tests/unit/transcript.test.ts

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -419,7 +419,7 @@ describe("transcript", () => {
419419
expect(subtitle).toBeTruthy();
420420
});
421421

422-
it("should get redactions", async () => {
422+
it("should get redacted audio", async () => {
423423
fetchMock.doMockOnceIf(
424424
requestMatches({
425425
url: `/v2/transcript/${transcriptId}/redacted-audio`,
@@ -431,11 +431,28 @@ describe("transcript", () => {
431431
}),
432432
);
433433
const redactedAudioResponse =
434-
await assembly.transcripts.redactions(transcriptId);
434+
await assembly.transcripts.redactedAudio(transcriptId);
435435
expect(redactedAudioResponse.status).toBe("redacted_audio_ready");
436436
expect(redactedAudioResponse.redacted_audio_url).toBeTruthy();
437437
});
438438

439+
it("should get redacted audio file", async () => {
440+
fetchMock.doMockOnceIf(
441+
requestMatches({
442+
url: `/v2/transcript/${transcriptId}/redacted-audio`,
443+
method: "GET",
444+
}),
445+
JSON.stringify({
446+
status: "redacted_audio_ready",
447+
redacted_audio_url: "https://some-url.com",
448+
}),
449+
);
450+
fetchMock.doMockOnceIf("https://some-url.com/", "audio data");
451+
const redactedAudioResponse =
452+
await assembly.transcripts.redactedAudioFile(transcriptId);
453+
expect(redactedAudioResponse).toBeTruthy();
454+
});
455+
439456
it("should word search", async () => {
440457
fetchMock.mockResponseOnce(
441458
JSON.stringify({

0 commit comments

Comments
 (0)