Skip to content

Commit

Permalink
Merge pull request #68 from AssemblyAI/E07417BDFEA3614F5967B1520F8B2F61
Browse files Browse the repository at this point in the history
Sync from internal repo (2024/10/08)
  • Loading branch information
Swimburger authored Oct 8, 2024
2 parents c126ce0 + cc22907 commit 91f38f2
Show file tree
Hide file tree
Showing 15 changed files with 153 additions and 39 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
[![AssemblyAI Twitter](https://img.shields.io/twitter/follow/AssemblyAI?label=%40AssemblyAI&style=social)](https://twitter.com/AssemblyAI)
[![AssemblyAI YouTube](https://img.shields.io/youtube/channel/subscribers/UCtatfZMf-8EkIwASXM4ts0A)](https://www.youtube.com/@AssemblyAI)
[![Discord](https://img.shields.io/discord/875120158014853141?logo=discord&label=Discord&link=https%3A%2F%2Fdiscord.com%2Fchannels%2F875120158014853141&style=social)
](https://assemblyai.com/discord)
](https://assembly.ai/discord)

# AssemblyAI JavaScript SDK

Expand Down Expand Up @@ -97,7 +97,7 @@ When you create a transcript, you can either pass in a URL to an audio file or u
```js
// Transcribe file at remote URL
let transcript = await client.transcripts.transcribe({
audio: "https://storage.googleapis.com/aai-web-samples/espn-bears.m4a",
audio: "https://assembly.ai/espn.m4a",
});
```

Expand All @@ -110,7 +110,7 @@ If you don't want to wait until the transcript is ready, you can use `submit`:

```js
let transcript = await client.transcripts.submit({
audio: "https://storage.googleapis.com/aai-web-samples/espn-bears.m4a",
audio: "https://assembly.ai/espn.m4a",
});
```

Expand Down Expand Up @@ -151,7 +151,7 @@ For example, here's how to enable [Speaker diarization](https://www.assemblyai.c

```js
let transcript = await client.transcripts.transcribe({
audio: "https://storage.googleapis.com/aai-web-samples/espn-bears.m4a",
audio: "https://assembly.ai/espn.m4a",
speaker_labels: true,
});
for (let utterance of transcript.utterances) {
Expand Down
53 changes: 32 additions & 21 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,15 @@
"browser": "./src/polyfills/websocket/browser.ts",
"node": "./src/polyfills/websocket/default.ts",
"default": "./src/polyfills/websocket/default.ts"
},
"#conditions": {
"bun": "./src/utils/conditions/bun.ts",
"deno": "./src/utils/conditions/deno.ts",
"workerd": "./src/utils/conditions/workerd.ts",
"react-native": "./src/utils/conditions/react-native.ts",
"browser": "./src/utils/conditions/browser.ts",
"node": "./src/utils/conditions/node.ts",
"default": "./src/utils/conditions/default.ts"
}
},
"type": "commonjs",
Expand Down Expand Up @@ -115,51 +124,53 @@
"docs"
],
"devDependencies": {
"@babel/preset-env": "^7.24.7",
"@babel/preset-env": "^7.25.4",
"@babel/preset-typescript": "^7.24.7",
"@rollup/plugin-node-resolve": "^15.2.3",
"@rollup/plugin-replace": "^5.0.7",
"@rollup/plugin-node-resolve": "^15.3.0",
"@rollup/plugin-replace": "^6.0.1",
"@rollup/plugin-terser": "^0.4.4",
"@rollup/plugin-typescript": "^11.1.6",
"@types/jest": "^29.5.12",
"@types/node": "^18.19.38",
"@rollup/plugin-typescript": "^12.1.0",
"@types/jest": "^29.5.13",
"@types/node": "^18.19.53",
"@types/websocket": "^1.0.10",
"@types/ws": "^8.5.10",
"@typescript-eslint/eslint-plugin": "^7.13.1",
"@types/ws": "^8.5.12",
"@typescript-eslint/eslint-plugin": "^7.18.0",
"dotenv": "^16.4.5",
"eslint": "^8.57.0",
"eslint": "^8.57.1",
"eslint-plugin-tsdoc": "^0.3.0",
"jest": "^29.7.0",
"jest-cli": "^29.7.0",
"jest-fetch-mock": "^3.0.3",
"jest-junit": "^16.0.0",
"jest-websocket-mock": "^2.5.0",
"mock-socket": "^9.3.1",
"openapi-typescript": "^6.7.5",
"prettier": "^3.3.2",
"publint": "^0.2.8",
"rimraf": "^5.0.7",
"rollup": "^4.18.0",
"ts-jest": "^29.1.5",
"tslib": "^2.6.3",
"typedoc": "^0.25.13",
"typedoc-plugin-extras": "^3.0.0",
"openapi-typescript": "^6.7.6",
"prettier": "^3.3.3",
"publint": "^0.2.11",
"rimraf": "^6.0.1",
"rollup": "^4.22.4",
"ts-jest": "^29.2.5",
"tslib": "^2.7.0",
"typedoc": "^0.26.7",
"typedoc-plugin-extras": "^3.1.0",
"typescript": "^5.4.5"
},
"dependencies": {
"ws": "^8.17.1"
"ws": "^8.18.0"
},
"pnpm": {
"packageExtensions": {
"ws": {
"peerDependencies": {
"@types/ws": "^8.5.10"
"@types/ws": "^8.5.12"
}
}
},
"overrides": {
"undici@<5.28.4": ">=5.28.4",
"braces@<3.0.3": ">=3.0.3"
"braces@<3.0.3": ">=3.0.3",
"micromatch@<4.0.8": ">=4.0.8",
"rollup@<2.79.2": ">=2.79.2"
}
}
}
11 changes: 7 additions & 4 deletions src/services/base.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { conditions } from "#conditions";
import { DEFAULT_FETCH_INIT } from "#fetch";
import { BaseServiceParams, Error as JsonError } from "..";
import { buildUserAgent } from "../utils/userAgent";
Expand Down Expand Up @@ -33,10 +34,12 @@ export abstract class BaseService {

if (this.userAgent) {
(headers as Record<string, string>)["User-Agent"] = this.userAgent;
// chromium browsers have a bug where the user agent can't be modified
if (typeof window !== "undefined" && "chrome" in window) {
(headers as Record<string, string>)["AssemblyAI-Agent"] =
this.userAgent;
if (conditions.browser || conditions.default) {
// chromium browsers have a bug where the user agent can't be modified
if (typeof window !== "undefined" && "chrome" in window) {
(headers as Record<string, string>)["AssemblyAI-Agent"] =
this.userAgent;
}
}
}
init.headers = headers;
Expand Down
8 changes: 8 additions & 0 deletions src/services/realtime/service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { WritableStream } from "#streams";
import { conditions } from "#conditions";
import {
PolyfillWebSocket,
factory as polyfillWebSocketFactory,
Expand Down Expand Up @@ -190,6 +191,13 @@ export class RealtimeTranscriber {
if (this.token) {
this.socket = polyfillWebSocketFactory(url.toString());
} else {
if (conditions.browser) {
console.warn(
`API key authentication is not supported for the RealtimeTranscriber in browser environment. Use temporary token authentication instead.
Learn more at https://github.com/AssemblyAI/assemblyai-node-sdk/blob/main/docs/compat.md#browser-compatibility.`,
);
}

this.socket = polyfillWebSocketFactory(url.toString(), {
headers: { Authorization: this.apiKey },
});
Expand Down
12 changes: 6 additions & 6 deletions src/types/openapi.generated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1615,7 +1615,7 @@ export type TopicDetectionResult = {
* "acoustic_model": "assemblyai_default",
* "language_code": "en_us",
* "status": "completed",
* "audio_url": "https://github.com/AssemblyAI-Examples/audio-examples/raw/main/20230607_me_canadian_wildfires.mp3",
* "audio_url": "https://assembly.ai/wildfires.mp3",
* "text": "Smoke from hundreds of wildfires in Canada is triggering air quality alerts throughout the US. Skylines from Maine to Maryland to Minnesota are gray and smoggy. And in some places, the air quality warnings include the warning to stay inside. We wanted to better understand what's happening here and why, so we called Peter de Carlo, an associate professor in the Department of Environmental Health and Engineering at Johns Hopkins University Varsity. Good morning, professor. Good morning. What is it about the conditions right now that have caused this round of wildfires to affect so many people so far away? Well, there's a couple of things. The season has been pretty dry already. And then the fact that we're getting hit in the US. Is because there's a couple of weather systems that are essentially channeling the smoke from those Canadian wildfires through Pennsylvania into the Mid Atlantic and the Northeast and kind of just dropping the smoke there. So what is it in this haze that makes it harmful? And I'm assuming it is harmful. It is. The levels outside right now in Baltimore are considered unhealthy. And most of that is due to what's called particulate matter, which are tiny particles, microscopic smaller than the width of your hair that can get into your lungs and impact your respiratory system, your cardiovascular system, and even your neurological your brain. What makes this particularly harmful? Is it the volume of particulant? Is it something in particular? What is it exactly? Can you just drill down on that a little bit more? Yeah. So the concentration of particulate matter I was looking at some of the monitors that we have was reaching levels of what are, in science, big 150 micrograms per meter cubed, which is more than ten times what the annual average should be and about four times higher than what you're supposed to have on a 24 hours average. And so the concentrations of these particles in the air are just much, much higher than we typically see. And exposure to those high levels can lead to a host of health problems. And who is most vulnerable? I noticed that in New York City, for example, they're canceling outdoor activities. And so here it is in the early days of summer, and they have to keep all the kids inside. So who tends to be vulnerable in a situation like this? It's the youngest. So children, obviously, whose bodies are still developing. The elderly, who are their bodies are more in decline and they're more susceptible to the health impacts of breathing, the poor air quality. And then people who have preexisting health conditions, people with respiratory conditions or heart conditions can be triggered by high levels of air pollution. Could this get worse? That's a good question. In some areas, it's much worse than others. And it just depends on kind of where the smoke is concentrated. I think New York has some of the higher concentrations right now, but that's going to change as that air moves away from the New York area. But over the course of the next few days, we will see different areas being hit at different times with the highest concentrations. I was going to ask you about more fires start burning. I don't expect the concentrations to go up too much higher. I was going to ask you how and you started to answer this, but how much longer could this last? Or forgive me if I'm asking you to speculate, but what do you think? Well, I think the fires are going to burn for a little bit longer, but the key for us in the US. Is the weather system changing. And so right now, it's kind of the weather systems that are pulling that air into our mid Atlantic and Northeast region. As those weather systems change and shift, we'll see that smoke going elsewhere and not impact us in this region as much. And so I think that's going to be the defining factor. And I think the next couple of days we're going to see a shift in that weather pattern and start to push the smoke away from where we are. And finally, with the impacts of climate change, we are seeing more wildfires. Will we be seeing more of these kinds of wide ranging air quality consequences or circumstances? I mean, that is one of the predictions for climate change. Looking into the future, the fire season is starting earlier and lasting longer, and we're seeing more frequent fires. So, yeah, this is probably something that we'll be seeing more frequently. This tends to be much more of an issue in the Western US. So the eastern US. Getting hit right now is a little bit new. But yeah, I think with climate change moving forward, this is something that is going to happen more frequently. That's Peter De Carlo, associate professor in the Department of Environmental Health and Engineering at Johns Hopkins University. Sergeant Carlo, thanks so much for joining us and sharing this expertise with us. Thank you for having me.",
* "words": [
* {
Expand Down Expand Up @@ -2717,16 +2717,16 @@ export type TranscriptLanguageCode =
* "status": "error",
* "created": "2024-03-11T21:23:59.979420",
* "completed": null,
* "audio_url": "https://storage.googleapis.com/client-docs-samples/nbc.mp3",
* "error": "Download error, unable to download https://storage.googleapis.com/client-docs-samples/nbc.mp3. Please make sure the file exists and is accessible from the internet."
* "audio_url": "https://storage.googleapis.com/client-docs-samples/nbc.oopsie",
* "error": "Download error, unable to download https://storage.googleapis.com/client-docs-samples/nbc.oopsie. Please make sure the file exists and is accessible from the internet."
* },
* {
* "id": "28a73d01-98db-41dd-9e98-2533ba0af117",
* "resource_url": "https://api.assemblyai.com/v2/transcript/28a73d01-98db-41dd-9e98-2533ba0af117",
* "status": "completed",
* "created": "2024-03-11T21:12:57.372215",
* "completed": "2024-03-11T21:13:03.267020",
* "audio_url": "https://storage.googleapis.com/aai-docs-samples/nbc.mp3",
* "audio_url": "https://assembly.ai/nbc.mp3",
* "error": null
* }
* ]
Expand All @@ -2747,7 +2747,7 @@ export type TranscriptList = {
* "status": "completed",
* "created": "2023-11-02T21:49:25.586965",
* "completed": "2023-11-02T21:49:25.586965",
* "audio_url": "https://github.com/AssemblyAI-Examples/audio-examples/raw/main/20230607_me_canadian_wildfires.mp3",
* "audio_url": "https://assembly.ai/wildfires.mp3",
* "error": null
* }
* ```
Expand Down Expand Up @@ -3039,7 +3039,7 @@ export type TranscriptParagraph = {
* {
* "speech_model": null,
* "language_code": "en_us",
* "audio_url": "https://github.com/AssemblyAI-Examples/audio-examples/raw/main/20230607_me_canadian_wildfires.mp3",
* "audio_url": "https://assembly.ai/wildfires.mp3",
* "punctuate": true,
* "format_text": true,
* "dual_channel": true,
Expand Down
12 changes: 12 additions & 0 deletions src/utils/conditions/browser.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { Conditions } from "./conditions";

export const conditions: Conditions = {
name: "browser",
default: false,
node: false,
browser: true,
bun: false,
deno: false,
workerd: false,
reactNative: false,
};
12 changes: 12 additions & 0 deletions src/utils/conditions/bun.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { Conditions } from "./conditions";

export const conditions: Conditions = {
name: "bun",
default: false,
node: false,
browser: false,
bun: true,
deno: false,
workerd: false,
reactNative: false,
};
10 changes: 10 additions & 0 deletions src/utils/conditions/conditions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export type Conditions = {
name: string;
default: boolean;
node: boolean;
browser: boolean;
bun: boolean;
deno: boolean;
workerd: boolean;
reactNative: boolean;
};
12 changes: 12 additions & 0 deletions src/utils/conditions/default.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { Conditions } from "./conditions";

export const conditions: Conditions = {
name: "default",
default: true,
node: false,
browser: false,
bun: false,
deno: false,
workerd: false,
reactNative: false,
};
12 changes: 12 additions & 0 deletions src/utils/conditions/deno.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { Conditions } from "./conditions";

export const conditions: Conditions = {
name: "deno",
default: false,
node: false,
browser: false,
bun: false,
deno: true,
workerd: false,
reactNative: false,
};
12 changes: 12 additions & 0 deletions src/utils/conditions/node.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { Conditions } from "./conditions";

export const conditions: Conditions = {
name: "node",
default: false,
node: true,
browser: false,
bun: false,
deno: false,
workerd: false,
reactNative: false,
};
12 changes: 12 additions & 0 deletions src/utils/conditions/react-native.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { Conditions } from "./conditions";

export const conditions: Conditions = {
name: "react-native",
default: false,
node: false,
browser: false,
bun: false,
deno: false,
workerd: false,
reactNative: true,
};
12 changes: 12 additions & 0 deletions src/utils/conditions/workerd.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { Conditions } from "./conditions";

export const conditions: Conditions = {
name: "workerd",
default: false,
node: false,
browser: false,
bun: false,
deno: false,
workerd: true,
reactNative: false,
};
3 changes: 1 addition & 2 deletions tests/integration/transcript.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ import { AssemblyAI } from "../../src";
import { readFile } from "fs/promises";

const testDir = process.env["TESTDATA_DIR"] ?? "tests/static";
const remoteAudioUrl =
"https://storage.googleapis.com/aai-web-samples/espn-bears.m4a";
const remoteAudioUrl = "https://assembly.ai/espn.m4a";
const badRemoteAudioURL =
"https://storage.googleapis.com/aai-web-samples/does-not-exist.m4a";
const knownTranscriptId = process.env.TEST_TRANSCRIPT_ID!;
Expand Down
3 changes: 1 addition & 2 deletions tests/unit/transcript.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ const assembly = createClient();

const knownTranscriptIds = ["transcript_123"];
const transcriptId = knownTranscriptIds[0];
const remoteAudioURL =
"https://storage.googleapis.com/aai-web-samples/espn-bears.m4a";
const remoteAudioURL = "https://assembly.ai/espn.m4a";
const badRemoteAudioURL =
"https://storage.googleapis.com/aai-web-samples/does-not-exist.m4a";

Expand Down

0 comments on commit 91f38f2

Please sign in to comment.