Skip to content

Commit

Permalink
Merge pull request #58 from AssemblyAI/E07417BDFEA3614F5967B1520F8B2F61
Browse files Browse the repository at this point in the history
Sync from internal repo (2024/06/12)
  • Loading branch information
Swimburger authored Jun 12, 2024
2 parents fe04165 + 737d9b6 commit 2f75167
Show file tree
Hide file tree
Showing 10 changed files with 120 additions and 17 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## [4.4.7]

- Rename `TranscriptService.redactions` function to `TranscriptService.redactedAudio`.
- Add `TranscriptService.redactedAudioFile` function.
- Add `workerd` export to fix `cache` issue with `fetch` on Cloudflare Workers.

## [4.4.6]

- Fix Rollup exports so \_\_SDK_VERSION\_\_ is properly replaced with the version of the SDK.
Expand Down
8 changes: 6 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "assemblyai",
"version": "4.4.6",
"version": "4.4.7",
"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.",
"engines": {
"node": ">=18"
Expand All @@ -16,7 +16,7 @@
"types": "./dist/exports/index.d.ts",
"default": "./dist/deno.mjs"
},
"workerd": "./dist/index.mjs",
"workerd": "./dist/workerd.mjs",
"browser": "./dist/browser.mjs",
"react-native": "./dist/browser.mjs",
"node": {
Expand All @@ -38,6 +38,10 @@
"./package.json": "./package.json"
},
"imports": {
"#fetch": {
"workerd": "./src/polyfills/fetch/workerd.ts",
"default": "./src/polyfills/fetch/default.ts"
},
"#fs": {
"node": "./src/polyfills/fs/node.ts",
"bun": "./src/polyfills/fs/bun.ts",
Expand Down
15 changes: 15 additions & 0 deletions rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,21 @@ module.exports = [
},
],
},
{
input: "src/exports/index.ts",
plugins: [
replacePlugin,
ts({ compilerOptions: { customConditions: ["workerd"] } }),
],
external: ["ws"],
output: [
{
file: `./dist/workerd.mjs`,
format: "es",
exports: "named",
},
],
},
// Browser ESM build
{
input: "src/exports/index.ts",
Expand Down
3 changes: 3 additions & 0 deletions src/polyfills/fetch/default.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const DEFAULT_FETCH_INIT: Record<string, unknown> = {
cache: "no-store",
};
1 change: 1 addition & 0 deletions src/polyfills/fetch/workerd.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const DEFAULT_FETCH_INIT: Record<string, unknown> = {};
14 changes: 7 additions & 7 deletions src/services/base.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { BaseServiceParams } from "..";
import { Error as JsonError } from "..";
import { DEFAULT_FETCH_INIT } from "#fetch";
import { BaseServiceParams, Error as JsonError } from "..";
import { buildUserAgent } from "../utils/userAgent";

/**
Expand All @@ -22,13 +22,14 @@ export abstract class BaseService {
input: string,
init?: RequestInit | undefined,
): Promise<Response> {
init = init ?? {};
let headers = init.headers ?? {};
headers = {
init = { ...DEFAULT_FETCH_INIT, ...init };
let headers = {
Authorization: this.params.apiKey,
"Content-Type": "application/json",
...init.headers,
};
if (DEFAULT_FETCH_INIT?.headers)
headers = { ...headers, ...DEFAULT_FETCH_INIT.headers };
if (init?.headers) headers = { ...headers, ...init.headers };

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

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

const response = await fetch(input, init);
Expand Down
37 changes: 35 additions & 2 deletions src/services/transcripts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
TranscribeOptions,
SubmitParams,
SpeechModel,
RedactedAudioFile,
} from "../..";
import { FileService } from "../files";
import { getPath } from "../../utils/path";
Expand Down Expand Up @@ -244,15 +245,47 @@ export class TranscriptService extends BaseService {
}

/**
* Retrieve redactions of a transcript.
* Retrieve the redacted audio URL of a transcript.
* @param id - The identifier of the transcript.
* @returns A promise that resolves to the subtitles text.
* @returns A promise that resolves to the details of the redacted audio.
* @deprecated Use `redactedAudio` instead.
*/
redactions(id: string): Promise<RedactedAudioResponse> {
return this.redactedAudio(id);
}

/**
* Retrieve the redacted audio URL of a transcript.
* @param id - The identifier of the transcript.
* @returns A promise that resolves to the details of the redacted audio.
*/
redactedAudio(id: string): Promise<RedactedAudioResponse> {
return this.fetchJson<RedactedAudioResponse>(
`/v2/transcript/${id}/redacted-audio`,
);
}

/**
* Retrieve the redacted audio file of a transcript.
* @param id - The identifier of the transcript.
* @returns A promise that resolves to the fetch HTTP response of the redacted audio file.
*/
async redactedAudioFile(id: string): Promise<RedactedAudioFile> {
const { redacted_audio_url, status } = await this.redactedAudio(id);
if (status !== "redacted_audio_ready") {
throw new Error(`Redacted audio status is ${status}`);
}
const response = await fetch(redacted_audio_url);
if (!response.ok) {
throw new Error(`Failed to fetch redacted audio: ${response.statusText}`);
}
return {
arrayBuffer: response.arrayBuffer.bind(response),
blob: response.blob.bind(response),
body: response.body,
bodyUsed: response.bodyUsed,
};
}
}

function deprecateConformer2(params: { speech_model?: SpeechModel | null }) {
Expand Down
10 changes: 10 additions & 0 deletions src/types/transcripts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,13 @@ export type SubmitParams = TranscribeParams;
* The options to transcribe an audio file, including polling options.
*/
export type TranscribeOptions = PollingOptions;

/**
* The PII redacted audio file, transmitted over the network.
*/
export type RedactedAudioFile = {
arrayBuffer: () => Promise<ArrayBuffer>;
blob: () => Promise<Blob>;
body: ReadableStream<Uint8Array> | null;
bodyUsed: boolean;
};
22 changes: 18 additions & 4 deletions tests/integration/transcript.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ describe("transcript", () => {
expect(subtitle).toBeTruthy();
});

it("should get redactions", async () => {
it("should get redacted audio", async () => {
const transcript = await client.transcripts.transcribe({
audio: remoteAudioUrl,
redact_pii: true,
Expand All @@ -132,19 +132,33 @@ describe("transcript", () => {
redact_pii_policies: ["medical_condition"],
redact_pii_sub: "hash",
});
const redactedAudioResponse = await client.transcripts.redactions(
const redactedAudioResponse = await client.transcripts.redactedAudio(
transcript.id,
);
expect(redactedAudioResponse.status).toBe("redacted_audio_ready");
expect(redactedAudioResponse.redacted_audio_url).toBeTruthy();
});
}, 30_000);

it("should get redacted audio file", async () => {
const transcript = await client.transcripts.transcribe({
audio: remoteAudioUrl,
redact_pii: true,
redact_pii_audio: true,
redact_pii_audio_quality: "wav",
redact_pii_policies: ["medical_condition"],
redact_pii_sub: "hash",
});
const redactedAudioResponse = await client.transcripts.redactedAudioFile(
transcript.id,
);
expect((await redactedAudioResponse.blob()).size).toBeGreaterThan(0);
}, 60_000);

it("should word search", async () => {
const searchResponse = await client.transcripts.wordSearch(
knownTranscriptId,
["Giants"],
);
console.log(searchResponse);
expect(searchResponse.id).toBe(knownTranscriptId);
expect(searchResponse.total_count).toBeGreaterThan(0);
expect(Array.isArray(searchResponse.matches)).toBeTruthy();
Expand Down
21 changes: 19 additions & 2 deletions tests/unit/transcript.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,7 @@ describe("transcript", () => {
expect(subtitle).toBeTruthy();
});

it("should get redactions", async () => {
it("should get redacted audio", async () => {
fetchMock.doMockOnceIf(
requestMatches({
url: `/v2/transcript/${transcriptId}/redacted-audio`,
Expand All @@ -431,11 +431,28 @@ describe("transcript", () => {
}),
);
const redactedAudioResponse =
await assembly.transcripts.redactions(transcriptId);
await assembly.transcripts.redactedAudio(transcriptId);
expect(redactedAudioResponse.status).toBe("redacted_audio_ready");
expect(redactedAudioResponse.redacted_audio_url).toBeTruthy();
});

it("should get redacted audio file", async () => {
fetchMock.doMockOnceIf(
requestMatches({
url: `/v2/transcript/${transcriptId}/redacted-audio`,
method: "GET",
}),
JSON.stringify({
status: "redacted_audio_ready",
redacted_audio_url: "https://some-url.com",
}),
);
fetchMock.doMockOnceIf("https://some-url.com/", "audio data");
const redactedAudioResponse =
await assembly.transcripts.redactedAudioFile(transcriptId);
expect(redactedAudioResponse).toBeTruthy();
});

it("should word search", async () => {
fetchMock.mockResponseOnce(
JSON.stringify({
Expand Down

0 comments on commit 2f75167

Please sign in to comment.